-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy patha2dp_decoder_scalable.cpp
More file actions
755 lines (640 loc) · 24 KB
/
Copy patha2dp_decoder_scalable.cpp
File metadata and controls
755 lines (640 loc) · 24 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
/***************************************************************************
*
* 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 "app_audio.h"
#include "cmsis.h"
#include "hal_location.h"
#include "heap_api.h"
#include "plat_types.h"
#include <string.h>
#if defined(A2DP_SCALABLE_ON)
#include "heap_api.h"
#include "ssc.h"
#define SCALABLE_MTU_LIMITER (32)
#define SCALABLE_MEMPOOL_SIZE 1024
#define SCALABLE_OUTPUT_FRAME_SAMPLES (SCALABLE_FRAME_SIZE)
typedef void *HANDLE_DECODER;
static A2DP_AUDIO_CONTEXT_T *a2dp_audio_context_p = NULL;
static A2DP_AUDIO_OUTPUT_CONFIG_T output_config;
static unsigned char *scalable_decoder_place = NULL;
static unsigned char *scalable_decoder_temp_buf = NULL;
static short ss_pcm_buff[SCALABLE_FRAME_SIZE * 4];
static int scalable_uhq_flag __attribute__((unused)) = 0;
static HANDLE_DECODER scalableDec_handle = NULL;
static A2DP_AUDIO_DECODER_LASTFRAME_INFO_T lastframe_info;
typedef struct {
uint16_t sequenceNumber;
uint32_t timestamp;
uint8_t *buffer;
int buffer_len;
} a2dp_audio_scalable_decoder_frame_t;
static void ss_to_24bit_buf(int32_t *out, int32_t *in, int size) {
for (int i = 0; i < size; i++) {
out[i] = in[i];
}
}
static void a2dp_audio_scalable_decoder_init(void) {
if (scalableDec_handle == NULL) {
scalableDec_handle = scalable_decoder_place;
ssc_decoder_init(scalableDec_handle, output_config.num_channels,
output_config.sample_rate);
}
}
static int scalableDecoder_Close(HANDLE_DECODER handle) {
if (handle) {
a2dp_audio_heap_free(handle);
a2dp_audio_heap_free(scalable_decoder_temp_buf);
}
return 0;
}
static void a2dp_audio_scalable_decoder_deinit(void) {
if (scalableDec_handle) {
scalableDecoder_Close(scalableDec_handle);
scalableDec_handle = NULL;
}
}
static void a2dp_audio_scalable_decoder_reinit(void) {
if (scalableDec_handle) {
a2dp_audio_scalable_decoder_deinit();
}
a2dp_audio_scalable_decoder_init();
}
static bool
is_valid_frame(a2dp_audio_scalable_decoder_frame_t *decoder_frame_p) {
int hw_tmp, len, bitrate_bps, frame_len, frame_size;
int sampling_rate = 44100;
unsigned char *input_buf = decoder_frame_p->buffer;
if (decoder_frame_p->buffer_len < SCALABLE_HEAD_SIZE) {
TRACE_A2DP_DECODER_E(
"invalid scalable a2dp frame, length < SCALABLE_HEAD_SIZE !!!!!!!");
return false;
}
scalable_uhq_flag = 0;
switch ((input_buf[3] & 0xf7)) {
case 0xF0:
bitrate_bps = 88000;
break;
case 0xF1:
bitrate_bps = 96000;
break;
case 0xF2:
bitrate_bps = 128000;
break;
case 0xF3:
bitrate_bps = 192000;
break;
case 0xF4:
bitrate_bps = 229000;
break;
case 0xF5:
scalable_uhq_flag = 1;
bitrate_bps = 328000;
sampling_rate = 96000;
break;
default:
bitrate_bps = 192000;
break;
}
frame_size = SCALABLE_FRAME_SIZE;
len = bitrate_bps * frame_size / sampling_rate / 8;
if (scalable_uhq_flag == 0) {
hw_tmp = (len * 3) >> 7;
len = hw_tmp + len;
len = len + ((len & 1) ^ 1);
} else {
len = 369; // 744/2-4+1
}
TRACE_A2DP_DECODER_D(
"scalable a2dp frame, length:%d bitrate:%d sampling_rate:%d",
decoder_frame_p->buffer_len, bitrate_bps, sampling_rate);
frame_len = SCALABLE_HEAD_SIZE + len - 1;
if (decoder_frame_p->buffer_len < frame_len) {
TRACE_A2DP_DECODER_E(
"invalid scalable a2dp frame, length:%d < %d !!!!!!!",
decoder_frame_p->buffer_len, frame_len);
return false;
}
return true;
}
#ifdef A2DP_CP_ACCEL
struct A2DP_CP_scalable_IN_FRM_INFO_T {
uint16_t sequenceNumber;
uint32_t timestamp;
};
struct A2DP_CP_scalable_OUT_FRM_INFO_T {
struct A2DP_CP_scalable_IN_FRM_INFO_T in_info;
uint16_t frame_samples;
uint16_t decoded_frames;
uint16_t frame_idx;
uint16_t pcm_len;
};
static bool cp_codec_reset;
extern uint32_t app_bt_stream_get_dma_buffer_samples(void);
TEXT_SSC_LOC static int a2dp_cp_scalable_mcu_decode(uint8_t *buffer,
uint32_t buffer_bytes) {
a2dp_audio_scalable_decoder_frame_t *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_scalable_IN_FRM_INFO_T in_info;
struct A2DP_CP_scalable_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;
cp_buffer_frames_max = app_bt_stream_get_dma_buffer_samples() / 2;
if (cp_buffer_frames_max % (lastframe_info.frame_samples)) {
cp_buffer_frames_max =
cp_buffer_frames_max / (lastframe_info.frame_samples) + 1;
} else {
cp_buffer_frames_max =
cp_buffer_frames_max / (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 * 2);
ASSERT(ret == 0, "%s: a2dp_cp_decoder_init() failed: ret=%d", __func__, ret);
while ((node = a2dp_audio_list_begin(list)) != NULL) {
decoder_frame_p =
(a2dp_audio_scalable_decoder_frame_t *)a2dp_audio_list_node(node);
if (false == is_valid_frame(decoder_frame_p)) {
return A2DP_DECODER_DECODE_ERROR;
}
in_info.sequenceNumber = decoder_frame_p->sequenceNumber;
in_info.timestamp = decoder_frame_p->timestamp;
ret =
a2dp_cp_put_in_frame(&in_info, sizeof(in_info), decoder_frame_p->buffer,
decoder_frame_p->buffer_len);
if (ret) {
break;
}
a2dp_audio_list_remove(list, decoder_frame_p);
}
ret = a2dp_cp_get_full_out_frame((void **)&out, &out_len);
if (ret) {
return A2DP_DECODER_DECODE_ERROR;
}
if (out_len == 0) {
memset(buffer, 0, buffer_bytes);
a2dp_cp_consume_full_out_frame();
return A2DP_DECODER_NO_ERROR;
}
ASSERT(out_len == out_frame_len, "%s: Bad out len %u (should be %u)",
__func__, out_len, out_frame_len);
p_out_info = (struct A2DP_CP_scalable_OUT_FRM_INFO_T *)out;
if (p_out_info->pcm_len) {
lastframe_info.sequenceNumber = p_out_info->in_info.sequenceNumber;
lastframe_info.timestamp = p_out_info->in_info.timestamp;
lastframe_info.curSubSequenceNumber = 0;
lastframe_info.totalSubSequenceNumber = 0;
lastframe_info.frame_samples = p_out_info->frame_samples;
lastframe_info.decoded_frames += p_out_info->decoded_frames;
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(&lastframe_info);
}
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 {
dec_ret = A2DP_DECODER_DECODE_ERROR;
}
ret = a2dp_cp_consume_full_out_frame();
ASSERT(ret == 0, "%s: a2dp_cp_consume_full_out_frame() failed: ret=%d",
__func__, ret);
return dec_ret;
}
TEXT_SSC_LOC int a2dp_cp_scalable_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_scalable_IN_FRM_INFO_T *p_in_info;
struct A2DP_CP_scalable_OUT_FRM_INFO_T *p_out_info;
uint8_t *in_buf;
uint32_t in_len;
uint32_t dec_sum;
int error, output_samples = 0, output_byte = 0;
if (cp_codec_reset) {
cp_codec_reset = false;
a2dp_audio_scalable_decoder_init();
}
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 1;
}
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_scalable_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);
if (!scalableDec_handle) {
TRACE(0, "scalable_decode not ready");
return 3;
}
dec_sum = 0;
error = 0;
while (dec_sum < dec_len && error == 0) {
ret = a2dp_cp_get_in_frame((void **)&in_buf, &in_len);
if (ret) {
break;
}
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_scalable_IN_FRM_INFO_T *)in_buf;
in_buf += sizeof(*p_in_info);
in_len -= sizeof(*p_in_info);
/* decode one SSC frame */
output_samples =
ssc_decode(scalableDec_handle, (const unsigned char *)in_buf,
ss_pcm_buff, SCALABLE_FRAME_SIZE, 0, 2);
if (0 == output_samples) {
a2dp_audio_scalable_decoder_reinit();
TRACE(0, "scalable_decode reinin codec \n");
error = A2DP_DECODER_DECODE_ERROR;
goto exit;
}
ss_to_24bit_buf((int32_t *)(dec_start + dec_sum), (int32_t *)ss_pcm_buff,
output_samples);
output_byte = output_samples * 4;
dec_sum += output_byte;
exit:
memcpy(&p_out_info->in_info, ss_pcm_buff, sizeof(*p_in_info));
p_out_info->decoded_frames++;
p_out_info->frame_samples = SCALABLE_OUTPUT_FRAME_SAMPLES;
p_out_info->frame_idx = a2dp_cp_get_in_frame_index();
ret = a2dp_cp_consume_in_frame();
ASSERT(ret == 0, "%s: a2dp_cp_consume_in_frame() failed: ret=%d", __func__,
ret);
}
p_out_info->pcm_len += dec_sum;
if (error || 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 error;
}
#endif
static int a2dp_audio_scalable_init(A2DP_AUDIO_OUTPUT_CONFIG_T *config,
void *context) {
TRACE_A2DP_DECODER_D("%s", __func__);
TRACE(0, "\n\nA2DP SSC-LC INIT\n");
a2dp_audio_context_p = (A2DP_AUDIO_CONTEXT_T *)context;
memcpy(&output_config, config, sizeof(A2DP_AUDIO_OUTPUT_CONFIG_T));
memset(&lastframe_info, 0, sizeof(A2DP_AUDIO_DECODER_LASTFRAME_INFO_T));
lastframe_info.stream_info = output_config;
lastframe_info.frame_samples = SCALABLE_FRAME_SIZE;
lastframe_info.list_samples = SCALABLE_FRAME_SIZE;
a2dp_audio_decoder_internal_lastframe_info_set(&lastframe_info);
ASSERT(a2dp_audio_context_p->dest_packet_mut < SCALABLE_MTU_LIMITER,
"%s MTU OVERFLOW:%u/%u", __func__,
a2dp_audio_context_p->dest_packet_mut, SCALABLE_MTU_LIMITER);
int decoder_size;
decoder_size = ssc_decoder_get_size(
output_config.num_channels); // todo: get size with codec capability
TRACE(0, "decoder size %d", decoder_size);
scalable_decoder_place =
(unsigned char *)a2dp_audio_heap_malloc(decoder_size);
ASSERT_A2DP_DECODER(scalable_decoder_place,
"no memory resource for scalable_decoder_place");
scalable_decoder_temp_buf =
(unsigned char *)a2dp_audio_heap_malloc(SCALABLE_FRAME_SIZE * 16);
ASSERT_A2DP_DECODER(scalable_decoder_temp_buf,
"no memory resource for scalable_decoder_temp_buf");
#ifdef A2DP_CP_ACCEL
int ret;
cp_codec_reset = true;
ret = a2dp_cp_init(a2dp_cp_scalable_cp_decode, CP_PROC_DELAY_1_FRAME);
ASSERT(ret == 0, "%s: a2dp_cp_init() failed: ret=%d", __func__, ret);
#else
a2dp_audio_scalable_decoder_init();
#endif
return A2DP_DECODER_NO_ERROR;
}
static int a2dp_audio_scalable_deinit(void) {
#ifdef A2DP_CP_ACCEL
a2dp_cp_deinit();
#endif
a2dp_audio_scalable_decoder_deinit();
TRACE(0, "\n\nA2DP SCALABLE DEINIT\n");
return A2DP_DECODER_NO_ERROR;
}
int a2dp_audio_scalable_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_scalable_decoder_frame_t *decoder_frame_p = NULL;
int ret = A2DP_DECODER_NO_ERROR;
bool cache_underflow = false;
int output_byte = 0, output_samples = 0;
uint8_t *output = buffer;
// TRACE(1,"bbd %d",buffer_bytes );
if (buffer_bytes < (SCALABLE_FRAME_SIZE * output_config.num_channels *
output_config.bits_depth / 8)) {
TRACE(1, "scalable_decode pcm_len = %d \n", buffer_bytes);
return A2DP_DECODER_NO_ERROR;
}
if (!scalableDec_handle) {
TRACE(0, "scalable_decode not ready");
return A2DP_DECODER_NO_ERROR;
}
while (output < buffer + buffer_bytes) {
node = a2dp_audio_list_begin(list);
if (!node) {
TRACE(0, "scalable_decode cache underflow");
cache_underflow = true;
goto exit;
} else {
decoder_frame_p =
(a2dp_audio_scalable_decoder_frame_t *)a2dp_audio_list_node(node);
if (false == is_valid_frame(decoder_frame_p)) {
TRACE_A2DP_DECODER_E("%s %d invalid a2dp frame", __func__, __LINE__);
ret = A2DP_DECODER_DECODE_ERROR;
goto exit;
}
/* decode one SSC frame */
output_samples = ssc_decode(scalableDec_handle, decoder_frame_p->buffer,
ss_pcm_buff, SCALABLE_FRAME_SIZE, 0, 2);
TRACE_A2DP_DECODER_D("scalable_decode seq:%d len:%d output_samples:%d",
decoder_frame_p->sequenceNumber,
decoder_frame_p->buffer_len, output_samples);
if (0 == output_samples) {
TRACE(0, "scalable_decode failed !!!!!!");
// if failed reopen it again
a2dp_audio_scalable_decoder_reinit();
TRACE(0, "scalable_decode reinin codec \n");
ret = A2DP_DECODER_DECODE_ERROR;
goto exit;
}
ss_to_24bit_buf((int32_t *)output, (int32_t *)ss_pcm_buff,
output_samples * 2);
output_byte = output_samples * 8;
output += output_byte;
ASSERT(SCALABLE_FRAME_SIZE == output_samples,
"scalable_decode output mismatch samples:%d", output_samples);
lastframe_info.sequenceNumber = decoder_frame_p->sequenceNumber;
lastframe_info.timestamp = decoder_frame_p->timestamp;
lastframe_info.curSubSequenceNumber = 0;
lastframe_info.totalSubSequenceNumber = 0;
lastframe_info.frame_samples = SCALABLE_FRAME_SIZE;
lastframe_info.decoded_frames++;
lastframe_info.undecode_frames = a2dp_audio_list_length(list) - 1;
a2dp_audio_decoder_internal_lastframe_info_set(&lastframe_info);
a2dp_audio_list_remove(list, decoder_frame_p);
}
}
exit:
if (cache_underflow) {
lastframe_info.undecode_frames = 0;
a2dp_audio_decoder_internal_lastframe_info_set(&lastframe_info);
ret = A2DP_DECODER_CACHE_UNDERFLOW_ERROR;
}
// TRACE(0,"abd");
return ret;
}
static int a2dp_audio_scalable_decode_frame(uint8_t *buffer,
uint32_t buffer_bytes) {
#ifdef A2DP_CP_ACCEL
return a2dp_cp_scalable_mcu_decode(buffer, buffer_bytes);
#else
return a2dp_audio_scalable_mcu_decode_frame(buffer, buffer_bytes);
#endif
}
static int a2dp_audio_scalable_preparse_packet(btif_media_header_t *header,
uint8_t *buffer,
uint32_t buffer_bytes) {
lastframe_info.sequenceNumber = header->sequenceNumber;
lastframe_info.timestamp = header->timestamp;
lastframe_info.curSubSequenceNumber = 0;
lastframe_info.totalSubSequenceNumber = 0;
lastframe_info.frame_samples = SCALABLE_FRAME_SIZE;
lastframe_info.list_samples = SCALABLE_FRAME_SIZE;
lastframe_info.decoded_frames = 0;
lastframe_info.undecode_frames = 0;
a2dp_audio_decoder_internal_lastframe_info_set(&lastframe_info);
TRACE(3, "%s seq:%d timestamp:%08x", __func__, header->sequenceNumber,
header->timestamp);
return A2DP_DECODER_NO_ERROR;
}
static void *a2dp_audio_scalable_frame_malloc(uint32_t packet_len) {
a2dp_audio_scalable_decoder_frame_t *decoder_frame_p = NULL;
uint8_t *buffer = NULL;
buffer = (uint8_t *)a2dp_audio_heap_malloc(SCALABLE_READBUF_SIZE);
decoder_frame_p =
(a2dp_audio_scalable_decoder_frame_t *)a2dp_audio_heap_malloc(
sizeof(a2dp_audio_scalable_decoder_frame_t));
decoder_frame_p->buffer = buffer;
decoder_frame_p->buffer_len = packet_len;
return (void *)decoder_frame_p;
}
static void a2dp_audio_scalable_free(void *packet) {
a2dp_audio_scalable_decoder_frame_t *decoder_frame_p =
(a2dp_audio_scalable_decoder_frame_t *)packet;
a2dp_audio_heap_free(decoder_frame_p->buffer);
a2dp_audio_heap_free(decoder_frame_p);
}
static int a2dp_audio_scalable_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;
if (a2dp_audio_list_length(list) < SCALABLE_MTU_LIMITER) {
a2dp_audio_scalable_decoder_frame_t *decoder_frame_p =
(a2dp_audio_scalable_decoder_frame_t *)a2dp_audio_scalable_frame_malloc(
buffer_bytes);
TRACE_A2DP_DECODER_D("%s seq:%d len:%d", __func__, header->sequenceNumber,
buffer_bytes);
decoder_frame_p->sequenceNumber = header->sequenceNumber;
decoder_frame_p->timestamp = header->timestamp;
memcpy(decoder_frame_p->buffer, buffer, buffer_bytes);
decoder_frame_p->buffer_len = buffer_bytes;
a2dp_audio_list_append(list, decoder_frame_p);
nRet = A2DP_DECODER_NO_ERROR;
} else {
TRACE(2, "%s list full current len:%d", __func__,
a2dp_audio_list_length(list));
nRet = A2DP_DECODER_MTU_LIMTER_ERROR;
}
return nRet;
}
static int a2dp_audio_scalable_discards_packet(uint32_t packets) {
int nRet = A2DP_DECODER_MEMORY_ERROR;
list_t *list = a2dp_audio_context_p->audio_datapath.input_raw_packet_list;
list_node_t *node = NULL;
a2dp_audio_scalable_decoder_frame_t *decoder_frame_p = NULL;
#ifdef A2DP_CP_ACCEL
a2dp_cp_reset_frame();
#endif
if (packets <= a2dp_audio_list_length(list)) {
for (uint8_t i = 0; i < packets; i++) {
if ((node = a2dp_audio_list_begin(list)) != NULL) {
decoder_frame_p =
(a2dp_audio_scalable_decoder_frame_t *)a2dp_audio_list_node(node);
a2dp_audio_list_remove(list, decoder_frame_p);
}
}
nRet = A2DP_DECODER_NO_ERROR;
}
TRACE(3, "%s packets:%d nRet:%d", __func__, packets, nRet);
return nRet;
}
static int a2dp_audio_scalable_headframe_info_get(
A2DP_AUDIO_HEADFRAME_INFO_T *headframe_info) {
list_t *list = a2dp_audio_context_p->audio_datapath.input_raw_packet_list;
list_node_t *node = NULL;
a2dp_audio_scalable_decoder_frame_t *decoder_frame_p = NULL;
if (a2dp_audio_list_length(list)) {
if ((node = a2dp_audio_list_begin(list)) != NULL) {
decoder_frame_p =
(a2dp_audio_scalable_decoder_frame_t *)a2dp_audio_list_node(node);
headframe_info->sequenceNumber = decoder_frame_p->sequenceNumber;
headframe_info->timestamp = decoder_frame_p->timestamp;
headframe_info->curSubSequenceNumber = 0;
headframe_info->totalSubSequenceNumber = 0;
}
} else {
memset(headframe_info, 0, sizeof(A2DP_AUDIO_HEADFRAME_INFO_T));
}
return A2DP_DECODER_NO_ERROR;
}
static int a2dp_audio_scalable_info_get(void *info) {
return A2DP_DECODER_NO_ERROR;
}
static int
a2dp_audio_scalable_synchronize_packet(A2DP_AUDIO_SYNCFRAME_INFO_T *sync_info,
uint32_t mask) {
int nRet = A2DP_DECODER_SYNC_ERROR;
list_t *list = a2dp_audio_context_p->audio_datapath.input_raw_packet_list;
list_node_t *node = NULL;
int list_len;
a2dp_audio_scalable_decoder_frame_t *decoder_frame_p = NULL;
#ifdef A2DP_CP_ACCEL
a2dp_cp_reset_frame();
#endif
list_len = a2dp_audio_list_length(list);
for (uint16_t i = 0; i < list_len; i++) {
if ((node = a2dp_audio_list_begin(list)) != NULL) {
decoder_frame_p =
(a2dp_audio_scalable_decoder_frame_t *)a2dp_audio_list_node(node);
if (A2DP_AUDIO_SYNCFRAME_CHK(decoder_frame_p->sequenceNumber ==
sync_info->sequenceNumber,
A2DP_AUDIO_SYNCFRAME_MASK_SEQ, mask) &&
A2DP_AUDIO_SYNCFRAME_CHK(decoder_frame_p->timestamp ==
sync_info->timestamp,
A2DP_AUDIO_SYNCFRAME_MASK_TIMESTAMP, mask)) {
nRet = A2DP_DECODER_NO_ERROR;
break;
}
a2dp_audio_list_remove(list, decoder_frame_p);
}
}
node = a2dp_audio_list_begin(list);
if (node) {
decoder_frame_p =
(a2dp_audio_scalable_decoder_frame_t *)a2dp_audio_list_node(node);
TRACE(4, "%s nRet:%d SEQ:%d timestamp:%d", __func__, nRet,
decoder_frame_p->sequenceNumber, decoder_frame_p->timestamp);
} else {
TRACE(2, "%s nRet:%d", __func__, nRet);
}
return nRet;
}
static int
a2dp_audio_scalable_synchronize_dest_packet_mut(uint16_t packet_mut) {
list_node_t *node = NULL;
uint32_t list_len = 0;
list_t *list = a2dp_audio_context_p->audio_datapath.input_raw_packet_list;
a2dp_audio_scalable_decoder_frame_t *decoder_frame_p = NULL;
list_len = a2dp_audio_list_length(list);
if (list_len > packet_mut) {
do {
node = a2dp_audio_list_begin(list);
if (node) {
decoder_frame_p =
(a2dp_audio_scalable_decoder_frame_t *)a2dp_audio_list_node(node);
a2dp_audio_list_remove(list, decoder_frame_p);
}
} while (a2dp_audio_list_length(list) > packet_mut);
}
TRACE(2, "%s list:%d", __func__, a2dp_audio_list_length(list));
return A2DP_DECODER_NO_ERROR;
}
int a2dp_audio_scalable_convert_list_to_samples(uint32_t *samples) {
uint32_t list_len = 0;
list_t *list = a2dp_audio_context_p->audio_datapath.input_raw_packet_list;
list_len = a2dp_audio_list_length(list);
*samples = SCALABLE_FRAME_SIZE * list_len;
TRACE(3, "%s list:%d samples:%d", __func__, list_len, *samples);
return A2DP_DECODER_NO_ERROR;
}
int a2dp_audio_scalable_discards_samples(uint32_t samples) {
int nRet = A2DP_DECODER_SYNC_ERROR;
list_t *list = a2dp_audio_context_p->audio_datapath.input_raw_packet_list;
a2dp_audio_scalable_decoder_frame_t *scalable_decoder_frame = NULL;
list_node_t *node = NULL;
int need_remove_list = 0;
uint32_t list_samples = 0;
ASSERT(!(samples % SCALABLE_FRAME_SIZE), "%s samples err:%d", __func__,
samples);
a2dp_audio_scalable_convert_list_to_samples(&list_samples);
if (list_samples >= samples) {
need_remove_list = samples / SCALABLE_FRAME_SIZE;
for (int i = 0; i < need_remove_list; i++) {
node = a2dp_audio_list_begin(list);
if (node) {
scalable_decoder_frame =
(a2dp_audio_scalable_decoder_frame_t *)a2dp_audio_list_node(node);
a2dp_audio_list_remove(list, scalable_decoder_frame);
}
}
nRet = A2DP_DECODER_NO_ERROR;
}
return nRet;
}
A2DP_AUDIO_DECODER_T a2dp_audio_scalable_decoder_config = {
{44100, 2, 16},
1,
a2dp_audio_scalable_init,
a2dp_audio_scalable_deinit,
a2dp_audio_scalable_decode_frame,
a2dp_audio_scalable_preparse_packet,
a2dp_audio_scalable_store_packet,
a2dp_audio_scalable_discards_packet,
a2dp_audio_scalable_synchronize_packet,
a2dp_audio_scalable_synchronize_dest_packet_mut,
a2dp_audio_scalable_convert_list_to_samples,
a2dp_audio_scalable_discards_samples,
a2dp_audio_scalable_headframe_info_get,
a2dp_audio_scalable_info_get,
a2dp_audio_scalable_free,
};
#else
A2DP_AUDIO_DECODER_T a2dp_audio_scalable_decoder_config = {
0,
};
#endif