-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy patha2dp_decoder_lhdc.cpp
More file actions
1220 lines (1058 loc) · 40.1 KB
/
Copy patha2dp_decoder_lhdc.cpp
File metadata and controls
1220 lines (1058 loc) · 40.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
*
* Copyright 2015-2019 BES.
* All rights reserved. All unpublished rights reserved.
*
* No part of this work may be used or reproduced in any form or by any
* means, or stored in a database or retrieval system, without prior written
* permission of BES.
*
* Use of this work is governed by a license granted by BES.
* This work contains confidential and proprietary information of
* BES. which is protected by copyright, trade secret,
* trademark and other intellectual property rights.
*
****************************************************************************/
// Standard C Included Files
#include "a2dp_decoder_internal.h"
#include "cmsis.h"
#include "hal_location.h"
#include "hal_timer.h"
#include "heap_api.h"
#include "plat_types.h"
#include <string.h>
typedef struct {
uint16_t sequenceNumber;
uint32_t timestamp;
uint16_t curSubSequenceNumber;
uint16_t totalSubSequenceNumber;
uint8_t *buffer;
uint32_t buffer_len;
} a2dp_audio_lhdc_decoder_frame_t;
#define LHDC_MTU_LIMITER (100)
#if defined(A2DP_LHDC_V3)
#define A2DP_LHDC_OUTPUT_FRAME_SAMPLES (256)
#else
#define A2DP_LHDC_OUTPUT_FRAME_SAMPLES (512)
#endif
#define A2DP_LHDC_DEFAULT_LATENCY (1)
#if defined(A2DP_LHDC_V3)
#define PACKET_BUFFER_LENGTH (2 * 1024)
#else
#define PACKET_BUFFER_LENGTH (4 * 1024)
#endif
#define LHDC_READBUF_SIZE (512)
#define A2DP_LHDC_HDR_F_MSK 0x80
#define A2DP_LHDC_HDR_S_MSK 0x40
#define A2DP_LHDC_HDR_L_MSK 0x20
#define A2DP_LHDC_HDR_FLAG_MSK \
(A2DP_LHDC_HDR_F_MSK | A2DP_LHDC_HDR_S_MSK | A2DP_LHDC_HDR_L_MSK)
#define A2DP_LHDC_HDR_LATENCY_LOW 0x00
#define A2DP_LHDC_HDR_LATENCY_MID 0x01
#define A2DP_LHDC_HDR_LATENCY_HIGH 0x02
#define A2DP_LHDC_HDR_LATENCY_MASK \
(A2DP_LHDC_HDR_LATENCY_MID | A2DP_LHDC_HDR_LATENCY_HIGH)
#if defined(A2DP_LHDC_V3)
#define A2DP_LHDC_HDR_FRAME_NO_MASK 0xfc
#else
#define A2DP_LHDC_HDR_FRAME_NO_MASK 0x1c
#endif
typedef enum {
ASM_PKT_WAT_STR,
ASM_PKT_WAT_LST,
} ASM_PKT_STATUS;
typedef enum { VERSION_2 = 200, VERSION_3 = 300 } lhdc_ver_t;
/**
* get lhdc frame header
*/
/**
LHDC frame
*/
typedef struct _lhdc_frame_Info {
uint32_t
frame_len; // 该 frame 的长处,若是分离压缩,则表示单一声道的 frame 长度。
uint32_t isSplit; // 是否为分离方式压缩
uint32_t isLeft; // 左声道 == true, 右声道 == false
} lhdc_frame_Info_t;
#ifdef A2DP_CP_ACCEL
struct A2DP_CP_LHDC_IN_FRM_INFO_T {
uint16_t sequenceNumber;
uint32_t timestamp;
uint16_t curSubSequenceNumber;
uint16_t totalSubSequenceNumber;
};
struct A2DP_CP_LHDC_OUT_FRM_INFO_T {
struct A2DP_CP_LHDC_IN_FRM_INFO_T in_info;
uint16_t frame_samples;
uint16_t decoded_frames;
uint16_t frame_idx;
uint16_t pcm_len;
};
#endif
extern "C" {
typedef struct bes_bt_local_info_t {
uint8_t bt_addr[BTIF_BD_ADDR_SIZE];
const char *bt_name;
uint8_t bt_len;
uint8_t ble_addr[BTIF_BD_ADDR_SIZE];
const char *ble_name;
uint8_t ble_len;
} bes_bt_local_info;
typedef int (*LHDC_GET_BT_INFO)(bes_bt_local_info *bt_info);
void lhdcInit(uint32_t bitPerSample, uint32_t sampleRate,
uint32_t scaleTo16Bits, lhdc_ver_t version);
// void lhdcInit(uint32_t bitPerSample, uint32_t sampleRate, uint32_t
// scaleTo16Bits);
#if defined(A2DP_LHDC_V3)
uint32_t lhdcPutData(uint8_t *pIn, uint32_t len);
#else
uint32_t lhdcPutData(uint8_t *pIn, int32_t len);
#endif
uint32_t lhdcDecodeProcess(uint8_t *pOutBuf);
void lhdcDestroy();
bool lhdcSetLicenseKeyTable(uint8_t *licTable, LHDC_GET_BT_INFO pFunc);
const char *getVersionCode();
bool larcIsEnabled();
}
static A2DP_AUDIO_CONTEXT_T *a2dp_audio_context_p = NULL;
static A2DP_AUDIO_DECODER_LASTFRAME_INFO_T a2dp_audio_lhdc_lastframe_info;
static A2DP_AUDIO_OUTPUT_CONFIG_T a2dp_audio_lhdc_output_config;
static uint16_t lhdc_mtu_limiter = LHDC_MTU_LIMITER;
static uint8_t serial_no;
static bool is_synced;
static ASM_PKT_STATUS asm_pkt_st;
static uint8_t packet_buffer[PACKET_BUFFER_LENGTH];
static uint32_t packet_buf_len = 0;
static uint32_t lhdc_total_frame_nb = 0;
#if defined(A2DP_LHDC_LARC)
static uint32_t lhdc_drop_frame = 0;
static uint32_t lhdc_last_time = 0;
static struct A2DP_CP_LHDC_IN_FRM_INFO_T last_frame_info;
// static uint16_t demo_frame_cnt = 0;
#endif
extern int bes_bt_local_info_get(bes_bt_local_info *local_info);
// Local API declare
void sav_lhdc_log_bytes_len(uint32_t bytes_len);
#define STATISTICS_UPDATE_INTERVAL 1000 // in ms
#define INTERVAL_TIMEBASE 1000 // in ms
typedef struct {
uint32_t sum_bytes;
uint32_t last_times; // in ms
float last_avg_val;
int32_t update_interval; // in ms
} LHDC_TRACFIC_STATISTICS;
LHDC_TRACFIC_STATISTICS statistic = {0, 0, 0, STATISTICS_UPDATE_INTERVAL};
void sav_lhdc_log_bytes_len(uint32_t bytes_len) {
uint32_t time_current = GET_CURRENT_MS();
float time_diff = time_current - statistic.last_times;
statistic.sum_bytes += bytes_len;
if (time_diff >= statistic.update_interval) {
statistic.last_avg_val = ((float)(statistic.sum_bytes * 8) / 1000) /
(time_diff / INTERVAL_TIMEBASE);
TRACE_A2DP_DECODER_I("Avarage rate about %d kbps",
(int)statistic.last_avg_val);
statistic.sum_bytes = 0;
statistic.last_times = time_current;
}
}
#if defined(A2DP_LHDC_LARC) && defined(A2DP_LHDC_V3)
static void sav_lhdc_save_last_info(struct A2DP_CP_LHDC_IN_FRM_INFO_T *f_info) {
memcpy(&last_frame_info, f_info, sizeof(struct A2DP_CP_LHDC_IN_FRM_INFO_T));
}
static void sav_lhdc_get_next_info(struct A2DP_CP_LHDC_IN_FRM_INFO_T *f_info) {
f_info->timestamp = last_frame_info.timestamp;
f_info->sequenceNumber = last_frame_info.sequenceNumber;
f_info->totalSubSequenceNumber = last_frame_info.totalSubSequenceNumber;
f_info->curSubSequenceNumber = last_frame_info.curSubSequenceNumber + 1;
if (f_info->curSubSequenceNumber > last_frame_info.totalSubSequenceNumber) {
f_info->curSubSequenceNumber = 1;
f_info->timestamp =
last_frame_info.timestamp + (A2DP_LHDC_OUTPUT_FRAME_SAMPLES *
last_frame_info.totalSubSequenceNumber);
f_info->sequenceNumber = last_frame_info.sequenceNumber + 1;
}
}
#endif
static void *a2dp_audio_lhdc_frame_malloc(uint32_t packet_len) {
a2dp_audio_lhdc_decoder_frame_t *decoder_frame_p = NULL;
uint8_t *buffer = NULL;
buffer = (uint8_t *)a2dp_audio_heap_malloc(packet_len);
decoder_frame_p = (a2dp_audio_lhdc_decoder_frame_t *)a2dp_audio_heap_malloc(
sizeof(a2dp_audio_lhdc_decoder_frame_t));
decoder_frame_p->buffer = buffer;
decoder_frame_p->buffer_len = packet_len;
return (void *)decoder_frame_p;
}
static void reset_lhdc_assmeble_packet(void) {
is_synced = false;
asm_pkt_st = ASM_PKT_WAT_STR;
packet_buf_len = 0;
}
static void initial_lhdc_assemble_packet(bool splitFlg) {
memset(packet_buffer, 0, PACKET_BUFFER_LENGTH);
reset_lhdc_assmeble_packet();
serial_no = 0xff;
}
/**
* A2DP packet 组包
*/
#if defined(A2DP_LHDC_V3)
int assemble_lhdc_packet(uint8_t *input, uint32_t input_len, uint8_t **pLout,
uint32_t *pLlen) {
uint8_t hdr = 0, seqno = 0xff;
int ret = -1;
uint32_t status = 0;
hdr = (*input);
input++;
seqno = (*input);
input++;
input_len -= 2;
// Check latency and update value when changed.
status = hdr & A2DP_LHDC_HDR_LATENCY_MASK;
// Get number of frame in packet.
status = (hdr & A2DP_LHDC_HDR_FRAME_NO_MASK) >> 2;
if (status <= 0) {
TRACE_A2DP_DECODER_I("No any frame in packet.");
return 0;
}
lhdc_total_frame_nb = status;
if (seqno != serial_no) {
TRACE_A2DP_DECODER_I("Packet lost! now(%d), expect(%d)", seqno, serial_no);
}
serial_no = seqno + 1;
sav_lhdc_log_bytes_len(input_len);
if (pLlen && pLout) {
*pLlen = input_len;
*pLout = input;
}
ret = lhdc_total_frame_nb;
return ret;
}
#else
int assemble_lhdc_packet(uint8_t *input, uint32_t input_len, uint8_t **pLout,
uint32_t *pLlen) {
uint8_t hdr = 0, seqno = 0xff;
int ret = -1;
// uint32_t status = 0;
hdr = (*input);
input++;
seqno = (*input);
input++;
input_len -= 2;
if (is_synced) {
if (seqno != serial_no) {
reset_lhdc_assmeble_packet();
if ((hdr & A2DP_LHDC_HDR_FLAG_MSK) == 0 ||
(hdr & A2DP_LHDC_HDR_S_MSK) != 0) {
goto lhdc_start;
} else
TRACE_A2DP_DECODER_I("drop packet No. %u", seqno);
return 0;
}
serial_no = seqno + 1;
}
lhdc_start:
switch (asm_pkt_st) {
case ASM_PKT_WAT_STR: {
if ((hdr & A2DP_LHDC_HDR_FLAG_MSK) == 0) {
memcpy(&packet_buffer[0], input, input_len);
if (pLlen && pLout) {
*pLlen = input_len;
*pLout = packet_buffer;
}
// TRACE_A2DP_DECODER_I("Single payload size = %d", *pLlen);
asm_pkt_st = ASM_PKT_WAT_STR;
packet_buf_len = 0; //= packet_buf_left_len = packet_buf_right_len = 0;
lhdc_total_frame_nb = (hdr & A2DP_LHDC_HDR_FRAME_NO_MASK) >> 2;
;
// TRACE_A2DP_DECODER_I("Single packet. total %d frames",
// lhdc_total_frame_nb);
ret = lhdc_total_frame_nb;
} else if (hdr & A2DP_LHDC_HDR_S_MSK) {
ret = 0;
if (packet_buf_len + input_len >= PACKET_BUFFER_LENGTH) {
packet_buf_len = 0;
asm_pkt_st = ASM_PKT_WAT_STR;
TRACE_A2DP_DECODER_I("ASM_PKT_WAT_STR:Frame buffer overflow!(%d)",
packet_buf_len);
break;
}
memcpy(&packet_buffer, input, input_len);
packet_buf_len = input_len;
asm_pkt_st = ASM_PKT_WAT_LST;
lhdc_total_frame_nb = (hdr & A2DP_LHDC_HDR_FRAME_NO_MASK) >> 2;
// TRACE_A2DP_DECODER_I("start of multi packet.");
} else
ret = -1;
if (ret >= 0) {
if (!is_synced) {
is_synced = true;
serial_no = seqno + 1;
}
}
break;
}
case ASM_PKT_WAT_LST: {
if (packet_buf_len + input_len >= PACKET_BUFFER_LENGTH) {
packet_buf_len = 0;
asm_pkt_st = ASM_PKT_WAT_STR;
TRACE_A2DP_DECODER_I("ASM_PKT_WAT_LST:Frame buffer overflow(%d)",
packet_buf_len);
break;
}
memcpy(&packet_buffer[packet_buf_len], input, input_len);
// TRACE_A2DP_DECODER_I("multi:payload size = %d", input_len);
packet_buf_len += input_len;
ret = 0;
if (hdr & A2DP_LHDC_HDR_L_MSK) {
if (pLlen && pLout) {
*pLlen = packet_buf_len;
*pLout = packet_buffer;
}
// TRACE_A2DP_DECODER_I("end of multi packet. total %d frames.",
// lhdc_total_frame_nb);
packet_buf_len = 0; // packet_buf_left_len = packet_buf_right_len = 0;
ret = lhdc_total_frame_nb;
asm_pkt_st = ASM_PKT_WAT_STR;
}
break;
}
default:
ret = 0;
break;
}
return ret;
}
#endif
static int parse_lhdc_info(uint8_t *in, lhdc_frame_Info_t *h) {
#define LHDC_HDR_LEN 4
uint32_t hdr = 0;
int ret = -1;
memcpy(&hdr, in, LHDC_HDR_LEN);
h->frame_len = (int)((hdr >> 8) & 0x1fff);
h->isSplit = ((hdr & 0x00600000) == 0x00600000);
h->isLeft = ((hdr & 0xf) == 0);
if ((hdr & 0xff000000) != 0x4c000000) {
} else {
ret = 0;
}
return ret;
}
#ifdef A2DP_CP_ACCEL
extern "C" uint32_t get_in_cp_frame_cnt(void);
extern "C" unsigned int set_cp_reset_flag(uint8_t evt);
extern uint32_t app_bt_stream_get_dma_buffer_samples(void);
int a2dp_cp_lhdc_cp_decode(void);
TEXT_LHDC_LOC
static int a2dp_cp_lhdc_after_cache_underflow(void) {
int ret = 0;
#ifdef A2DP_CP_ACCEL
a2dp_cp_deinit();
lhdcDestroy();
ret = a2dp_cp_init(a2dp_cp_lhdc_cp_decode, CP_PROC_DELAY_2_FRAMES);
ASSERT(ret == 0, "%s: a2dp_cp_init() failed: ret=%d", __func__, ret);
#endif
return ret;
}
// uint32_t demoTimer = 0;
static int a2dp_cp_lhdc_mcu_decode(uint8_t *buffer, uint32_t buffer_bytes) {
a2dp_audio_lhdc_decoder_frame_t *lhdc_decoder_frame_p = NULL;
list_node_t *node = NULL;
list_t *list = a2dp_audio_context_p->audio_datapath.input_raw_packet_list;
int ret, dec_ret;
struct A2DP_CP_LHDC_IN_FRM_INFO_T in_info;
struct A2DP_CP_LHDC_OUT_FRM_INFO_T *p_out_info;
uint8_t *out;
uint32_t out_len;
uint32_t out_frame_len;
uint32_t cp_buffer_frames_max = 0;
#if defined(A2DP_LHDC_LARC)
// bool forceRecovery = false;
bool larcEnabled = larcIsEnabled();
uint32_t frame_number = 0;
if (larcEnabled) {
/* The LARC is enabled. */
uint32_t time_diff = 0, time_now = GET_CURRENT_MS();
if (lhdc_last_time == 0) {
lhdc_last_time = time_now;
}
// if (demoTimer == 0) {
// demoTimer = time_now;
// }
time_diff = time_now - lhdc_last_time;
lhdc_last_time = time_now;
frame_number = (uint32_t)((float)time_diff /
((A2DP_LHDC_OUTPUT_FRAME_SAMPLES * 1000) /
a2dp_audio_lhdc_output_config.sample_rate));
TRACE_A2DP_DECODER_I(
"%s:current total list len %d, need frames %d", __func__,
a2dp_audio_list_length(list) + get_in_cp_frame_cnt(), frame_number);
// if (time_now - demoTimer >= (5 * 1000)) {
// forceRecovery = true;
// demoTimer = time_now;
// demo_frame_cnt = (a2dp_audio_lhdc_output_config.sample_rate * 120) /
// (A2DP_LHDC_OUTPUT_FRAME_SAMPLES * 1000);
// }
}
#endif
cp_buffer_frames_max = app_bt_stream_get_dma_buffer_samples() / 2;
if (cp_buffer_frames_max % (a2dp_audio_lhdc_lastframe_info.frame_samples)) {
cp_buffer_frames_max =
cp_buffer_frames_max / (a2dp_audio_lhdc_lastframe_info.frame_samples) +
1;
} else {
cp_buffer_frames_max =
cp_buffer_frames_max / (a2dp_audio_lhdc_lastframe_info.frame_samples);
}
out_frame_len = sizeof(*p_out_info) + buffer_bytes;
ret = a2dp_cp_decoder_init(out_frame_len, cp_buffer_frames_max * 8);
if (ret) {
TRACE_A2DP_DECODER_I("%s: a2dp_cp_decoder_init() failed: ret=%d", __func__,
ret);
set_cp_reset_flag(true);
return A2DP_DECODER_DECODE_ERROR;
}
#if defined(A2DP_LHDC_LARC)
uint32_t put_cnt = 0;
uint32_t drop_cnt = 0;
while (larcEnabled && ((a2dp_audio_list_length(list) < frame_number &&
get_in_cp_frame_cnt() == 0))) {
sav_lhdc_get_next_info(&in_info);
ret = a2dp_cp_put_in_frame(&in_info, sizeof(in_info), NULL, 0x55);
if (ret) {
// TRACE_A2DP_DECODER_I("%s piff !!!!!!ret: %d ",__func__, ret);
break;
}
sav_lhdc_save_last_info(&in_info);
lhdc_drop_frame++;
frame_number--;
// demo_frame_cnt--;
put_cnt++;
}
// forceRecovery = false;
TRACE_A2DP_DECODER_I("Recover %d frames", put_cnt);
put_cnt = 0;
drop_cnt = 0;
#endif
while ((node = a2dp_audio_list_begin(list)) != NULL) {
lhdc_decoder_frame_p =
(a2dp_audio_lhdc_decoder_frame_t *)a2dp_audio_list_node(node);
in_info.sequenceNumber = lhdc_decoder_frame_p->sequenceNumber;
in_info.timestamp = lhdc_decoder_frame_p->timestamp;
in_info.curSubSequenceNumber = lhdc_decoder_frame_p->curSubSequenceNumber;
in_info.totalSubSequenceNumber =
lhdc_decoder_frame_p->totalSubSequenceNumber;
#if defined(A2DP_LHDC_LARC)
if (!lhdc_drop_frame) {
ret = a2dp_cp_put_in_frame(&in_info, sizeof(in_info),
lhdc_decoder_frame_p->buffer,
lhdc_decoder_frame_p->buffer_len);
if (ret) {
// TRACE_A2DP_DECODER_I("%s piff !!!!!!ret: %d ",__func__, ret);
break;
}
sav_lhdc_save_last_info(&in_info);
put_cnt++;
} else {
lhdc_drop_frame--;
drop_cnt++;
}
#else
ret = a2dp_cp_put_in_frame(&in_info, sizeof(in_info),
lhdc_decoder_frame_p->buffer,
lhdc_decoder_frame_p->buffer_len);
if (ret) {
// TRACE_A2DP_DECODER_I("%s piff !!!!!!ret: %d ",__func__, ret);
break;
}
#endif
a2dp_audio_list_remove(list, lhdc_decoder_frame_p);
node = a2dp_audio_list_begin(list);
}
#if defined(A2DP_LHDC_LARC)
TRACE_A2DP_DECODER_I("Put %d frames, drop %d frames", put_cnt, drop_cnt);
#endif
ret = a2dp_cp_get_full_out_frame((void **)&out, &out_len);
if (ret) {
TRACE_A2DP_DECODER_I("%s %d cp find cache underflow", __func__, ret);
TRACE_A2DP_DECODER_I("aud_list_len:%d. cp_get_in_frame:%d",
a2dp_audio_list_length(list), get_in_cp_frame_cnt());
a2dp_cp_lhdc_after_cache_underflow();
return A2DP_DECODER_CACHE_UNDERFLOW_ERROR;
}
if (out_len == 0) {
memset(buffer, 0, buffer_bytes);
a2dp_cp_consume_full_out_frame();
TRACE_A2DP_DECODER_I("%s olz!!!%d ", __func__, __LINE__);
return A2DP_DECODER_NO_ERROR;
}
if (out_len != out_frame_len) {
TRACE_A2DP_DECODER_I("%s: Bad out len %u (should be %u)", __func__, out_len,
out_frame_len);
set_cp_reset_flag(true);
return A2DP_DECODER_DECODE_ERROR;
}
p_out_info = (struct A2DP_CP_LHDC_OUT_FRM_INFO_T *)out;
if (p_out_info->pcm_len) {
a2dp_audio_lhdc_lastframe_info.sequenceNumber =
p_out_info->in_info.sequenceNumber;
a2dp_audio_lhdc_lastframe_info.timestamp = p_out_info->in_info.timestamp;
a2dp_audio_lhdc_lastframe_info.curSubSequenceNumber =
p_out_info->in_info.curSubSequenceNumber;
a2dp_audio_lhdc_lastframe_info.totalSubSequenceNumber =
p_out_info->in_info.totalSubSequenceNumber;
a2dp_audio_lhdc_lastframe_info.frame_samples = p_out_info->frame_samples;
a2dp_audio_lhdc_lastframe_info.decoded_frames += p_out_info->decoded_frames;
a2dp_audio_lhdc_lastframe_info.undecode_frames =
a2dp_audio_list_length(list) +
a2dp_cp_get_in_frame_cnt_by_index(p_out_info->frame_idx) - 1;
a2dp_audio_decoder_internal_lastframe_info_set(
&a2dp_audio_lhdc_lastframe_info);
TRACE_A2DP_DECODER_I(
"lhdc_decoder seq:%d cursub:%d ttlsub:%d decoded:%d/%d",
a2dp_audio_lhdc_lastframe_info.sequenceNumber,
a2dp_audio_lhdc_lastframe_info.curSubSequenceNumber,
a2dp_audio_lhdc_lastframe_info.totalSubSequenceNumber,
a2dp_audio_lhdc_lastframe_info.decoded_frames,
a2dp_audio_lhdc_lastframe_info.undecode_frames);
}
if (p_out_info->pcm_len == buffer_bytes) {
memcpy(buffer, p_out_info + 1, p_out_info->pcm_len);
dec_ret = A2DP_DECODER_NO_ERROR;
} else {
TRACE_A2DP_DECODER_I("%s %d cp decoder error !!!!!!", __func__, __LINE__);
set_cp_reset_flag(true);
return A2DP_DECODER_DECODE_ERROR;
}
ret = a2dp_cp_consume_full_out_frame();
if (ret) {
TRACE_A2DP_DECODER_I("%s: a2dp_cp_consume_full_out_frame() failed: ret=%d",
__func__, ret);
set_cp_reset_flag(true);
return A2DP_DECODER_DECODE_ERROR;
}
return dec_ret;
}
#ifdef __CP_EXCEPTION_TEST__
static bool _cp_assert = false;
int cp_assert(void) {
_cp_assert = true;
return 0;
}
#endif
#define LHDC_DECODED_FRAME_SIZE 1024
TEXT_LHDC_LOC
int a2dp_cp_lhdc_cp_decode(void) {
int ret;
enum CP_EMPTY_OUT_FRM_T out_frm_st;
uint8_t *out;
uint32_t out_len;
uint8_t *dec_start;
uint32_t dec_len;
struct A2DP_CP_LHDC_IN_FRM_INFO_T *p_in_info;
struct A2DP_CP_LHDC_OUT_FRM_INFO_T *p_out_info;
uint8_t *in_buf;
uint32_t in_len;
int32_t dec_sum;
uint32_t lhdc_out_len = 0;
#ifdef __CP_EXCEPTION_TEST__
if (_cp_assert) {
_cp_assert = false;
*(int *)0 = 1;
// ASSERT(0, "ASSERT %s %d", __func__, __LINE__);
}
#endif
out_frm_st = a2dp_cp_get_emtpy_out_frame((void **)&out, &out_len);
if (out_frm_st != CP_EMPTY_OUT_FRM_OK &&
out_frm_st != CP_EMPTY_OUT_FRM_WORKING) {
return out_frm_st;
}
ASSERT(out_len > sizeof(*p_out_info), "%s: Bad out_len %u (should > %u)",
__func__, out_len, sizeof(*p_out_info));
p_out_info = (struct A2DP_CP_LHDC_OUT_FRM_INFO_T *)out;
if (out_frm_st == CP_EMPTY_OUT_FRM_OK) {
p_out_info->pcm_len = 0;
p_out_info->decoded_frames = 0;
}
ASSERT(out_len > sizeof(*p_out_info) + p_out_info->pcm_len,
"%s: Bad out_len %u (should > %u + %u)", __func__, out_len,
sizeof(*p_out_info), p_out_info->pcm_len);
dec_start = (uint8_t *)(p_out_info + 1) + p_out_info->pcm_len;
dec_len = out_len - (dec_start - (uint8_t *)out);
dec_sum = 0;
while (dec_sum < (int32_t)dec_len) {
uint32_t lhdc_decode_temp = 0;
ret = a2dp_cp_get_in_frame((void **)&in_buf, &in_len);
if (ret) {
TRACE_A2DP_DECODER_I("cp_get_int_frame fail, ret=%d", ret);
return 4;
}
ASSERT(in_len > sizeof(*p_in_info), "%s: Bad in_len %u (should > %u)",
__func__, in_len, sizeof(*p_in_info));
p_in_info = (struct A2DP_CP_LHDC_IN_FRM_INFO_T *)in_buf;
in_buf += sizeof(*p_in_info);
in_len -= sizeof(*p_in_info);
#if defined(A2DP_LHDC_V3)
#if defined(A2DP_LHDC_LARC)
if (in_len == 0x55 && larcIsEnabled()) {
// TRACE_A2DP_DECODER_I("recover:sn(%d), tt(%d), csssn(%d), ttsssn(%d)",
// p_in_info->sequenceNumber, p_in_info->timestamp,
// p_in_info->curSubSequenceNumber, p_in_info->totalSubSequenceNumber);
in_len = 0;
}
#endif
lhdcPutData(in_buf, in_len);
// TRACE_A2DP_DECODER_I("%s:put length=%d, dec_len=%d", __func__, in_len,
// dec_len);
#else
lhdcPutData(in_buf, in_len);
#endif
uint32_t loop_cnt = 0;
do {
// TRACE_A2DP_DECODER_I("loop %d , dec_start %p, dec_sum %d,
// lhdc_decode_temp %d", loop_cnt, dec_start, dec_sum, lhdc_decode_temp);
lhdc_out_len = lhdcDecodeProcess(dec_start + dec_sum + lhdc_decode_temp);
if (lhdc_out_len > 0) {
lhdc_decode_temp += lhdc_out_len;
loop_cnt++;
} else {
// TRACE_A2DP_DECODER_I("decodeProcess error!!! ret=%d", lhdc_out_len);
break;
}
} while (lhdc_decode_temp < dec_len && lhdc_decode_temp > 0);
// TRACE_A2DP_DECODER_I("lhdc_cp_decode seq:%d len:%d err:%d",
// p_in_info->sequenceNumber,
// dec_len - dec_sum,
// lhdc_out_len);
// TRACE_A2DP_DECODER_I("%s:decode loop run times=%d, lhdc_decode_temp=%d",
// __func__, loop_cnt, lhdc_decode_temp);
dec_sum += lhdc_decode_temp;
if ((lhdc_decode_temp % LHDC_DECODED_FRAME_SIZE)) {
TRACE_A2DP_DECODER_I("error!!! dec_sum: %d decode_temp: %d", dec_sum,
lhdc_decode_temp);
return -1;
}
ret = a2dp_cp_consume_in_frame();
if (ret != 0) {
TRACE_A2DP_DECODER_I("%s: a2dp_cp_consume_in_frame() failed: ret=%d",
__func__, ret);
}
ASSERT(ret == 0, "%s: a2dp_cp_consume_in_frame() failed: ret=%d", __func__,
ret);
memcpy(&p_out_info->in_info, p_in_info, sizeof(*p_in_info));
p_out_info->decoded_frames++;
p_out_info->frame_samples = A2DP_LHDC_OUTPUT_FRAME_SAMPLES;
p_out_info->frame_idx = a2dp_cp_get_in_frame_index();
}
if ((dec_sum % LHDC_DECODED_FRAME_SIZE)) {
TRACE_A2DP_DECODER_I("error!!! dec_sum:%d != dec_len:%d", dec_sum,
dec_len);
ASSERT(0, "%s", __func__);
}
p_out_info->pcm_len += dec_sum;
if (out_len <= sizeof(*p_out_info) + p_out_info->pcm_len) {
ret = a2dp_cp_consume_emtpy_out_frame();
ASSERT(ret == 0, "%s: a2dp_cp_consume_emtpy_out_frame() failed: ret=%d",
__func__, ret);
}
return 0;
}
#endif
#if 1
static int a2dp_audio_lhdc_list_checker(void) {
list_t *list = a2dp_audio_context_p->audio_datapath.input_raw_packet_list;
list_node_t *node = NULL;
a2dp_audio_lhdc_decoder_frame_t *lhdc_decoder_frame_p = NULL;
int cnt = 0;
do {
lhdc_decoder_frame_p =
(a2dp_audio_lhdc_decoder_frame_t *)a2dp_audio_lhdc_frame_malloc(
LHDC_READBUF_SIZE);
if (lhdc_decoder_frame_p) {
a2dp_audio_list_append(list, lhdc_decoder_frame_p);
}
cnt++;
} while (lhdc_decoder_frame_p && cnt < LHDC_MTU_LIMITER);
do {
node = a2dp_audio_list_begin(list);
if (node) {
lhdc_decoder_frame_p =
(a2dp_audio_lhdc_decoder_frame_t *)a2dp_audio_list_node(node);
a2dp_audio_list_remove(list, lhdc_decoder_frame_p);
}
} while (node);
TRACE_A2DP_DECODER_I("%s cnt:%d list:%d", __func__, cnt,
a2dp_audio_list_length(list));
return 0;
}
#endif
int a2dp_audio_lhdc_mcu_decode_frame(uint8_t *buffer, uint32_t buffer_bytes) {
list_t *list = a2dp_audio_context_p->audio_datapath.input_raw_packet_list;
list_node_t *node = NULL;
a2dp_audio_lhdc_decoder_frame_t *lhdc_decoder_frame_p = NULL;
bool cache_underflow = false;
int output_byte = 0;
uint32_t lhdc_out_len = 0;
node = a2dp_audio_list_begin(list);
if (!node) {
TRACE_A2DP_DECODER_I("lhdc_decode cache underflow");
cache_underflow = true;
goto exit;
} else {
lhdc_decoder_frame_p =
(a2dp_audio_lhdc_decoder_frame_t *)a2dp_audio_list_node(node);
#if defined(A2DP_LHDC_V3)
lhdcPutData(lhdc_decoder_frame_p->buffer, lhdc_decoder_frame_p->buffer_len);
#else
lhdcPutData(lhdc_decoder_frame_p->buffer, lhdc_decoder_frame_p->buffer_len);
#endif
do {
lhdc_out_len = lhdcDecodeProcess(buffer + output_byte);
if (lhdc_out_len > 0) {
output_byte += lhdc_out_len;
} else {
break;
}
} while (output_byte < (int)buffer_bytes && output_byte > 0);
if (output_byte != (int)buffer_bytes) {
TRACE_A2DP_DECODER_I("[warning] lhdc_decode_frame output_byte:%d "
"lhdc_out_len:%d buffer_bytes:%d",
output_byte, lhdc_out_len, buffer_bytes);
TRACE_A2DP_DECODER_I("[warning] lhdc_decode_frame frame_len:%d rtp "
"seq:%d timestamp:%d decoder_frame:%d/%d ",
lhdc_decoder_frame_p->buffer_len,
lhdc_decoder_frame_p->sequenceNumber,
lhdc_decoder_frame_p->timestamp,
lhdc_decoder_frame_p->curSubSequenceNumber,
lhdc_decoder_frame_p->totalSubSequenceNumber);
output_byte = buffer_bytes;
int32_t dump_byte = lhdc_decoder_frame_p->buffer_len;
int32_t dump_offset = 0;
while (1) {
uint32_t dump_byte_output = 0;
dump_byte_output = dump_byte > 32 ? 32 : dump_byte;
DUMP8("%02x ", lhdc_decoder_frame_p->buffer + dump_offset,
dump_byte_output);
dump_offset += dump_byte_output;
dump_byte -= dump_byte_output;
if (dump_byte <= 0) {
break;
}
}
ASSERT(0, "%s", __func__);
}
a2dp_audio_lhdc_lastframe_info.sequenceNumber =
lhdc_decoder_frame_p->sequenceNumber;
a2dp_audio_lhdc_lastframe_info.timestamp = lhdc_decoder_frame_p->timestamp;
a2dp_audio_lhdc_lastframe_info.curSubSequenceNumber =
lhdc_decoder_frame_p->curSubSequenceNumber;
a2dp_audio_lhdc_lastframe_info.totalSubSequenceNumber =
lhdc_decoder_frame_p->totalSubSequenceNumber;
a2dp_audio_lhdc_lastframe_info.frame_samples =
A2DP_LHDC_OUTPUT_FRAME_SAMPLES;
a2dp_audio_lhdc_lastframe_info.decoded_frames++;
a2dp_audio_lhdc_lastframe_info.undecode_frames =
a2dp_audio_list_length(list) - 1;
a2dp_audio_decoder_internal_lastframe_info_set(
&a2dp_audio_lhdc_lastframe_info);
a2dp_audio_list_remove(list, lhdc_decoder_frame_p);
}
exit:
if (cache_underflow) {
reset_lhdc_assmeble_packet();
a2dp_audio_lhdc_lastframe_info.undecode_frames = 0;
a2dp_audio_decoder_internal_lastframe_info_set(
&a2dp_audio_lhdc_lastframe_info);
output_byte = A2DP_DECODER_CACHE_UNDERFLOW_ERROR;
}
return output_byte;
}
int a2dp_audio_lhdc_decode_frame(uint8_t *buffer, uint32_t buffer_bytes) {
#ifdef A2DP_CP_ACCEL
return a2dp_cp_lhdc_mcu_decode(buffer, buffer_bytes);
#else
return a2dp_audio_lhdc_mcu_decode_frame(buffer, buffer_bytes);
#endif
}
int a2dp_audio_lhdc_preparse_packet(btif_media_header_t *header,
uint8_t *buffer, uint32_t buffer_bytes) {
a2dp_audio_lhdc_lastframe_info.sequenceNumber = header->sequenceNumber;
a2dp_audio_lhdc_lastframe_info.timestamp = header->timestamp;
a2dp_audio_lhdc_lastframe_info.curSubSequenceNumber = 0;
a2dp_audio_lhdc_lastframe_info.totalSubSequenceNumber = 0;
a2dp_audio_lhdc_lastframe_info.frame_samples = A2DP_LHDC_OUTPUT_FRAME_SAMPLES;
a2dp_audio_lhdc_lastframe_info.list_samples = A2DP_LHDC_OUTPUT_FRAME_SAMPLES;
a2dp_audio_lhdc_lastframe_info.decoded_frames = 0;
a2dp_audio_lhdc_lastframe_info.undecode_frames = 0;
a2dp_audio_decoder_internal_lastframe_info_set(
&a2dp_audio_lhdc_lastframe_info);
TRACE_A2DP_DECODER_I("%s seq:%d timestamp:%08x", __func__,
header->sequenceNumber, header->timestamp);
return A2DP_DECODER_NO_ERROR;
}
void a2dp_audio_lhdc_free(void *packet) {
a2dp_audio_lhdc_decoder_frame_t *decoder_frame_p =
(a2dp_audio_lhdc_decoder_frame_t *)packet;
a2dp_audio_heap_free(decoder_frame_p->buffer);
a2dp_audio_heap_free(decoder_frame_p);
}
int a2dp_audio_lhdc_store_packet(btif_media_header_t *header, uint8_t *buffer,
uint32_t buffer_bytes) {
list_t *list = a2dp_audio_context_p->audio_datapath.input_raw_packet_list;
int nRet = A2DP_DECODER_NO_ERROR;
uint32_t frame_num = 0;
uint32_t frame_cnt = 0;
uint32_t lSize = 0;
uint8_t *lPTR = NULL;
lhdc_frame_Info_t lhdc_frame_Info;
uint32_t ptr_offset = 0;
if ((frame_num = assemble_lhdc_packet(buffer, buffer_bytes, &lPTR, &lSize)) >
0) {
if (lPTR != NULL && lSize != 0) {
ptr_offset = 0;
// TRACE_A2DP_DECODER_I("%s: There are %d frames in packet", __func__,
// frame_num);
while (parse_lhdc_info(lPTR + ptr_offset, &lhdc_frame_Info) == 0 &&
ptr_offset < lSize && frame_cnt < frame_num) {
a2dp_audio_lhdc_decoder_frame_t *decoder_frame_p = NULL;
if (!lhdc_frame_Info.frame_len) {
DUMP8("%02x ", lPTR + ptr_offset, 32);
ASSERT(0,
"lhdc_frame_Info error frame_len:%d offset:%d ptr:%08x/%08x",
lhdc_frame_Info.frame_len, ptr_offset, (uint32_t)buffer,
(uint32_t)lPTR);
}
ASSERT(lhdc_frame_Info.frame_len <= (lSize - ptr_offset),
"%s frame_len:%d ptr_offset:%d buffer_bytes:%d", __func__,
lhdc_frame_Info.frame_len, ptr_offset, lSize);
uint32_t list_length = a2dp_audio_list_length(list);
if (list_length < lhdc_mtu_limiter) {
decoder_frame_p =
(a2dp_audio_lhdc_decoder_frame_t *)a2dp_audio_lhdc_frame_malloc(
lhdc_frame_Info.frame_len);
} else {
nRet = A2DP_DECODER_MTU_LIMTER_ERROR;
break;
}
frame_cnt++;
decoder_frame_p->sequenceNumber = header->sequenceNumber;
decoder_frame_p->timestamp = header->timestamp;
decoder_frame_p->curSubSequenceNumber = frame_cnt;
decoder_frame_p->totalSubSequenceNumber = frame_num;
memcpy(decoder_frame_p->buffer, lPTR + ptr_offset,
lhdc_frame_Info.frame_len);
decoder_frame_p->buffer_len = lhdc_frame_Info.frame_len;
a2dp_audio_list_append(list, decoder_frame_p);
ptr_offset += lhdc_frame_Info.frame_len;
#if 0
TRACE_A2DP_DECODER_I("lhdc_store_packet save seq:%d timestamp:%d len:%d lSize:%d list_length:%d frame_len:%d Split:%d/%d",
header->sequenceNumber,
header->timestamp,
buffer_bytes,
lSize,
list_length,
lhdc_frame_Info.frame_len,
lhdc_frame_Info.isSplit,
lhdc_frame_Info.isLeft);
#endif
}
}
} else {
// TRACE_A2DP_DECODER_I("lhdc_store_packet skip seq:%d timestamp:%d
// len:%d l:%d", header->sequenceNumber,
// header->timestamp,buffer_bytes, lSize);
}
return nRet;
}
int a2dp_audio_lhdc_discards_packet(uint32_t packets) {
#ifdef A2DP_CP_ACCEL
a2dp_cp_reset_frame();
#endif
int nRet = a2dp_audio_context_p->audio_decoder
.audio_decoder_synchronize_dest_packet_mut(
a2dp_audio_context_p->dest_packet_mut);
reset_lhdc_assmeble_packet();