xref: /btstack/src/classic/btstack_sbc_decoder_bluedroid.c (revision b3551ec76d5c0b6ab3b22419c3c92b0ddca9b670)
1 /*
2  * Copyright (C) 2014 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 #define __BTSTACK_FILE__ "btstack_sbc_decoder_bluedroid.c"
39 
40 // *****************************************************************************
41 //
42 // SBC decoder based on Bluedroid library
43 //
44 // *****************************************************************************
45 
46 #include "btstack_config.h"
47 
48 #include <stdint.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 
53 #include "btstack_sbc.h"
54 #include "btstack_sbc_plc.h"
55 
56 #include "oi_codec_sbc.h"
57 #include "oi_assert.h"
58 #include "btstack.h"
59 
60 #define mSBC_SYNCWORD 0xad
61 #define SBC_SYNCWORD 0x9c
62 #define SBC_MAX_CHANNELS 2
63 // #define LOG_FRAME_STATUS
64 
65 #define DECODER_DATA_SIZE (SBC_MAX_CHANNELS*SBC_MAX_BLOCKS*SBC_MAX_BANDS * 4 + SBC_CODEC_MIN_FILTER_BUFFERS*SBC_MAX_BANDS*SBC_MAX_CHANNELS * 2)
66 
67 typedef struct {
68     OI_UINT32 bytes_in_frame_buffer;
69     OI_CODEC_SBC_DECODER_CONTEXT decoder_context;
70 
71     uint8_t   frame_buffer[SBC_MAX_FRAME_LEN];
72     int16_t   pcm_plc_data[SBC_MAX_CHANNELS * SBC_MAX_BANDS * SBC_MAX_BLOCKS];
73     int16_t   pcm_data[SBC_MAX_CHANNELS * SBC_MAX_BANDS * SBC_MAX_BLOCKS];
74     uint32_t  pcm_bytes;
75     OI_UINT32 decoder_data[(DECODER_DATA_SIZE+3)/4];
76     int       first_good_frame_found;
77     int       h2_sequence_nr;
78     uint16_t  msbc_bad_bytes;
79 } bludroid_decoder_state_t;
80 
81 static btstack_sbc_decoder_state_t * sbc_decoder_state_singleton = NULL;
82 static bludroid_decoder_state_t bd_decoder_state;
83 
84 // Testing only - START
85 static int plc_enabled = 1;
86 static int corrupt_frame_period = -1;
87 // Testing - STOP
88 
89 void btstack_sbc_decoder_test_set_plc_enabled(int enabled){
90     plc_enabled = enabled;
91 }
92 
93 void btstack_sbc_decoder_test_simulate_corrupt_frames(int period){
94     corrupt_frame_period = period;
95 }
96 
97 static int find_sequence_of_zeros(const OI_BYTE *frame_data, OI_UINT32 frame_bytes, int seq_length){
98     int zero_seq_count = 0;
99     unsigned int i;
100     for (i=0; i<frame_bytes; i++){
101         if (frame_data[i] == 0) {
102             zero_seq_count++;
103             if (zero_seq_count >= seq_length) return zero_seq_count;
104         } else {
105             zero_seq_count = 0;
106         }
107     }
108     return 0;
109 }
110 
111 // returns position of mSBC sync word
112 static int find_h2_sync(const OI_BYTE *frame_data, OI_UINT32 frame_bytes, int * sync_word_nr){
113     int syncword = mSBC_SYNCWORD;
114     uint8_t h2_first_byte = 0;
115     uint8_t h2_second_byte = 0;
116 
117     unsigned int i;
118     for (i=0; i<frame_bytes; i++){
119         if (frame_data[i] == syncword) {
120             // check: first byte == 1
121             if (h2_first_byte == 1) {
122                 // check lower nibble of second byte == 0x08
123                 uint8_t ln = h2_second_byte & 0x0F;
124                 if (ln == 8) {
125                     // check if bits 0+2 == bits 1+3
126                     uint8_t hn = h2_second_byte >> 4;
127                     if  ( ((hn>>1) & 0x05) == (hn & 0x05) ) {
128                         *sync_word_nr = ((hn & 0x04) >> 1) | (hn & 0x01);
129                         return i;
130                     }
131                 }
132             }
133         }
134         h2_first_byte = h2_second_byte;
135         h2_second_byte = frame_data[i];
136     }
137     return -1;
138 }
139 
140 int btstack_sbc_decoder_num_samples_per_frame(btstack_sbc_decoder_state_t * state){
141     bludroid_decoder_state_t * decoder_state = (bludroid_decoder_state_t *) state->decoder_state;
142     return decoder_state->decoder_context.common.frameInfo.nrof_blocks * decoder_state->decoder_context.common.frameInfo.nrof_subbands;
143 }
144 
145 int btstack_sbc_decoder_num_channels(btstack_sbc_decoder_state_t * state){
146     bludroid_decoder_state_t * decoder_state = (bludroid_decoder_state_t *) state->decoder_state;
147     return decoder_state->decoder_context.common.frameInfo.nrof_channels;
148 }
149 
150 int btstack_sbc_decoder_sample_rate(btstack_sbc_decoder_state_t * state){
151     bludroid_decoder_state_t * decoder_state = (bludroid_decoder_state_t *) state->decoder_state;
152     return decoder_state->decoder_context.common.frameInfo.frequency;
153 }
154 
155 #ifdef OI_DEBUG
156 void OI_AssertFail(const char* file, int line, const char* reason){
157     log_error("AssertFail file %s, line %d, reason %s", file, line, reason);
158 }
159 #endif
160 
161 void btstack_sbc_decoder_init(btstack_sbc_decoder_state_t * state, btstack_sbc_mode_t mode, void (*callback)(int16_t * data, int num_samples, int num_channels, int sample_rate, void * context), void * context){
162     if (sbc_decoder_state_singleton && sbc_decoder_state_singleton != state ){
163         log_error("SBC decoder: different sbc decoder state is allready registered");
164     }
165     OI_STATUS status = OI_STATUS_SUCCESS;
166     switch (mode){
167         case SBC_MODE_STANDARD:
168             // note: we always request stereo output, even for mono input
169             status = OI_CODEC_SBC_DecoderReset(&(bd_decoder_state.decoder_context), bd_decoder_state.decoder_data, sizeof(bd_decoder_state.decoder_data), 2, 2, FALSE);
170             break;
171         case SBC_MODE_mSBC:
172             status = OI_CODEC_mSBC_DecoderReset(&(bd_decoder_state.decoder_context), bd_decoder_state.decoder_data, sizeof(bd_decoder_state.decoder_data));
173             break;
174         default:
175             break;
176     }
177 
178     if (status != OI_STATUS_SUCCESS){
179         log_error("SBC decoder: error during reset %d\n", status);
180     }
181 
182     sbc_decoder_state_singleton = state;
183 
184     bd_decoder_state.bytes_in_frame_buffer = 0;
185     bd_decoder_state.pcm_bytes = sizeof(bd_decoder_state.pcm_data);
186     bd_decoder_state.h2_sequence_nr = -1;
187     bd_decoder_state.first_good_frame_found = 0;
188 
189     memset(state, 0, sizeof(btstack_sbc_decoder_state_t));
190     state->handle_pcm_data = callback;
191     state->mode = mode;
192     state->context = context;
193     state->decoder_state = &bd_decoder_state;
194     btstack_sbc_plc_init(&state->plc_state);
195 }
196 
197 static void append_received_sbc_data(bludroid_decoder_state_t * state, uint8_t * buffer, int size){
198     int numFreeBytes = sizeof(state->frame_buffer) - state->bytes_in_frame_buffer;
199 
200     // printf("append_received_sbc_data: bytes to append %u, sizeof %u, bytes in buffer %u, free %u\n", size,sizeof(state->frame_buffer), state->bytes_in_frame_buffer, numFreeBytes);
201 
202     if (size > numFreeBytes){
203         log_error("SBC data: more bytes read %u than free bytes in buffer %u", size, numFreeBytes);
204     }
205 
206     memcpy(state->frame_buffer + state->bytes_in_frame_buffer, buffer, size);
207     state->bytes_in_frame_buffer += size;
208 }
209 
210 
211 static void btstack_sbc_decoder_process_sbc_data(btstack_sbc_decoder_state_t * state, uint8_t * buffer, int size){
212     bludroid_decoder_state_t * decoder_state = (bludroid_decoder_state_t*)state->decoder_state;
213     int input_bytes_to_process = size;
214     int keep_decoding = 1;
215 
216     while (keep_decoding) {
217         // Fill decoder_state->frame_buffer as much as possible.
218         int bytes_free_in_frame_buffer = SBC_MAX_FRAME_LEN - decoder_state->bytes_in_frame_buffer;
219         int bytes_to_append = btstack_min(input_bytes_to_process, bytes_free_in_frame_buffer);
220         if (bytes_to_append){
221             append_received_sbc_data(decoder_state, buffer, bytes_to_append);
222             buffer += bytes_to_append;
223             input_bytes_to_process -= bytes_to_append;
224         }
225 
226         // Decode the next frame in decoder_state->frame_buffer.
227         int bytes_in_frame_buffer_before_decoding = decoder_state->bytes_in_frame_buffer;
228         const OI_BYTE *frame_data = decoder_state->frame_buffer;
229         OI_UINT32 frame_data_len = decoder_state->bytes_in_frame_buffer;
230         OI_STATUS status = OI_CODEC_SBC_DecodeFrame(&(decoder_state->decoder_context),
231                                                     &frame_data,
232                                                     &frame_data_len,
233                                                     decoder_state->pcm_plc_data,
234                                                     &(decoder_state->pcm_bytes));
235         uint16_t bytes_processed = bytes_in_frame_buffer_before_decoding - frame_data_len;
236 
237         // testing only - corrupt frame periodically
238         static int frame_count = 0;
239         if (corrupt_frame_period > 0){
240             frame_count++;
241 
242             if (frame_count % corrupt_frame_period == 0){
243                 *(uint8_t*)&frame_data[5] = 0;
244                 frame_count = 0;
245             }
246         }
247 
248         // Handle decoding result.
249         switch(status){
250             case OI_STATUS_SUCCESS:
251             case OI_CODEC_SBC_PARTIAL_DECODE:
252                 state->handle_pcm_data(decoder_state->pcm_plc_data,
253                                        btstack_sbc_decoder_num_samples_per_frame(state),
254                                        btstack_sbc_decoder_num_channels(state),
255                                        btstack_sbc_decoder_sample_rate(state), state->context);
256                 state->good_frames_nr++;
257                 break;
258 
259             case OI_CODEC_SBC_NOT_ENOUGH_HEADER_DATA:
260             case OI_CODEC_SBC_NOT_ENOUGH_BODY_DATA:
261             case OI_CODEC_SBC_NOT_ENOUGH_AUDIO_DATA:
262                 if (input_bytes_to_process > 0){
263                     // Should never occur: The SBC code claims there is not enough bytes in the frame_buffer,
264                     // but the frame_buffer was full. (The frame_buffer is always full before decoding when input_bytes_to_process > 0.)
265                     // Clear frame_buffer.
266                     log_info("SBC decode: frame_buffer too small for frame");
267                     bytes_processed = bytes_in_frame_buffer_before_decoding;
268                 } else {
269                     // Exit decode loop, because there is not enough data in frame_buffer to decode the next frame.
270                     keep_decoding = 0;
271                 }
272                 break;
273 
274             case OI_CODEC_SBC_NO_SYNCWORD:
275                 // This means the entire frame_buffer did not contain the syncword.
276                 // Discard the frame_buffer contents.
277                 log_info("SBC decode: no syncword found");
278                 bytes_processed = bytes_in_frame_buffer_before_decoding;
279                 break;
280 
281             case OI_CODEC_SBC_CHECKSUM_MISMATCH:
282                 // The next frame is somehow corrupt.
283                 log_info("SBC decode: checksum error");
284                 // Did the codec consume any bytes?
285                 if (bytes_processed > 0){
286                     // Good. Nothing to do.
287                 } else {
288                     // Skip the bogus frame by skipping the header.
289                     bytes_processed = 1;
290                 }
291                 break;
292 
293             case OI_STATUS_INVALID_PARAMETERS:
294                 // This caused by corrupt frames.
295                 // The codec apparently does not recover from this.
296                 // Re-initialize the codec.
297                 log_info("SBC decode: invalid parameters: resetting codec");
298                 if (OI_CODEC_SBC_DecoderReset(&(bd_decoder_state.decoder_context), bd_decoder_state.decoder_data, sizeof(bd_decoder_state.decoder_data), 2, 2, FALSE) != OI_STATUS_SUCCESS){
299                     log_info("SBC decode: resetting codec failed");
300 
301                 }
302                 break;
303             default:
304                 // Anything else went wrong.
305                 // Skip a few bytes and try again.
306                 bytes_processed = 1;
307                 log_info("SBC decode: unknown status %d", status);
308                 break;
309         }
310 
311         // Remove decoded frame from decoder_state->frame_buffer.
312         if (bytes_processed > bytes_in_frame_buffer_before_decoding) {
313             bytes_processed = bytes_in_frame_buffer_before_decoding;
314         }
315         memmove(decoder_state->frame_buffer, decoder_state->frame_buffer + bytes_processed, bytes_in_frame_buffer_before_decoding - bytes_processed);
316         decoder_state->bytes_in_frame_buffer -= bytes_processed;
317     }
318 }
319 
320 
321 static void btstack_sbc_decoder_process_msbc_data(btstack_sbc_decoder_state_t * state, int packet_status_flag, uint8_t * buffer, int size){
322     bludroid_decoder_state_t * decoder_state = (bludroid_decoder_state_t*)state->decoder_state;
323     int input_bytes_to_process = size;
324     unsigned int msbc_frame_size = 60;
325 
326     while (input_bytes_to_process > 0){
327 
328         // fill buffer with new data
329         int bytes_missing_for_complete_msbc_frame = msbc_frame_size - decoder_state->bytes_in_frame_buffer;
330         int bytes_to_append = btstack_min(input_bytes_to_process, bytes_missing_for_complete_msbc_frame);
331         if (bytes_to_append) {
332             append_received_sbc_data(decoder_state, buffer, bytes_to_append);
333             buffer += bytes_to_append;
334             input_bytes_to_process -= bytes_to_append;
335         }
336 
337         if (decoder_state->bytes_in_frame_buffer < msbc_frame_size) break;
338 
339         // printf_hexdump(decoder_state->frame_buffer, decoder_state->bytes_in_frame_buffer);
340 
341         uint16_t bytes_in_frame_buffer_before_decoding = decoder_state->bytes_in_frame_buffer;
342         uint16_t bytes_processed = 0;
343         const OI_BYTE *frame_data = decoder_state->frame_buffer;
344 
345         // testing only - corrupt frame periodically
346         static int frame_count = 0;
347         if (corrupt_frame_period > 0){
348            frame_count++;
349 
350             if (frame_count % corrupt_frame_period == 0){
351                 *(uint8_t*)&frame_data[5] = 0;
352                 frame_count = 0;
353             }
354         }
355 
356         int h2_syncword = 0;
357         int h2_sync_pos = find_h2_sync(frame_data, decoder_state->bytes_in_frame_buffer, &h2_syncword);
358         // printf("Sync Pos: %d, nr %d\n", h2_sync_pos, h2_syncword);
359         if (h2_sync_pos < 0){
360             // no sync found, discard all but last 2 bytes
361             bytes_processed = decoder_state->bytes_in_frame_buffer - 2;
362             memmove(decoder_state->frame_buffer, decoder_state->frame_buffer + bytes_processed, decoder_state->bytes_in_frame_buffer);
363             decoder_state->bytes_in_frame_buffer -= bytes_processed;    // == 2
364             // don't try PLC without at least a single good frame
365             if (decoder_state->first_good_frame_found){
366                 decoder_state->msbc_bad_bytes += bytes_processed;
367             }
368             continue;
369         }
370 
371         decoder_state->h2_sequence_nr = h2_syncword;
372 
373         // drop data before it
374         bytes_processed = h2_sync_pos - 2;
375         if (bytes_processed > 2){
376             memmove(decoder_state->frame_buffer, decoder_state->frame_buffer + bytes_processed, decoder_state->bytes_in_frame_buffer);
377             decoder_state->bytes_in_frame_buffer -= bytes_processed;
378             // don't try PLC without at least a single good frame
379             if (decoder_state->first_good_frame_found){
380                 decoder_state->msbc_bad_bytes += bytes_processed;
381             }
382             continue;
383         }
384 
385         int bad_frame = 0;
386         int zero_seq_found = find_sequence_of_zeros(frame_data, decoder_state->bytes_in_frame_buffer, 20);
387 
388         // after first valid frame, zero sequences count as bad frames
389         if (decoder_state->first_good_frame_found){
390             bad_frame = zero_seq_found || packet_status_flag;
391         }
392 
393         if (bad_frame){
394             // stats
395             if (zero_seq_found){
396                 state->zero_frames_nr++;
397             } else {
398                 state->bad_frames_nr++;
399             }
400 #ifdef LOG_FRAME_STATUS
401             if (zero_seq_found){
402                 printf("%d : ZERO FRAME\n", decoder_state->h2_sequence_nr);
403             } else {
404                 printf("%d : BAD FRAME\n", decoder_state->h2_sequence_nr);
405             }
406 #endif
407             // retry after dropoing 3 byte sync
408             bytes_processed = 3;
409             memmove(decoder_state->frame_buffer, decoder_state->frame_buffer + bytes_processed, decoder_state->bytes_in_frame_buffer);
410             decoder_state->bytes_in_frame_buffer -= bytes_processed;
411             decoder_state->msbc_bad_bytes        += bytes_processed;
412             continue;
413         }
414 
415         // ready to decode frame
416         // log_info("Before: bytes in buffer %u, pcm bytes %u", decoder_state->bytes_in_frame_buffer,  decoder_state->pcm_bytes);
417         OI_STATUS status = OI_CODEC_SBC_DecodeFrame(&(decoder_state->decoder_context),
418                                             &frame_data,
419                                             &(decoder_state->bytes_in_frame_buffer),
420                                             decoder_state->pcm_plc_data,
421                                             &(decoder_state->pcm_bytes));
422 
423         bytes_processed = bytes_in_frame_buffer_before_decoding - decoder_state->bytes_in_frame_buffer;
424         // log_info("After: bytes in buffer %u, pcm bytes %u, processed %u", decoder_state->bytes_in_frame_buffer,  decoder_state->pcm_bytes, bytes_processed);
425 
426         switch(status){
427             case 0:
428                 decoder_state->first_good_frame_found = 1;
429 
430                 btstack_sbc_plc_good_frame(&state->plc_state, decoder_state->pcm_plc_data, decoder_state->pcm_data);
431                 state->handle_pcm_data(decoder_state->pcm_data,
432                                     btstack_sbc_decoder_num_samples_per_frame(state),
433                                     btstack_sbc_decoder_num_channels(state),
434                                     btstack_sbc_decoder_sample_rate(state), state->context);
435 
436                 state->good_frames_nr++;
437                 decoder_state->msbc_bad_bytes = 0;
438                 continue;
439 
440             case OI_CODEC_SBC_NOT_ENOUGH_HEADER_DATA:
441                 log_error("OI_CODEC_SBC_NOT_ENOUGH_HEADER_DATA");
442                 break;
443             case OI_CODEC_SBC_NOT_ENOUGH_BODY_DATA:
444                 log_error("OI_CODEC_SBC_NOT_ENOUGH_BODY_DATA");
445                 break;
446             case OI_CODEC_SBC_NOT_ENOUGH_AUDIO_DATA:
447                 log_error("OI_CODEC_SBC_NOT_ENOUGH_AUDIO_DATA");
448                 break;
449             case OI_CODEC_SBC_NO_SYNCWORD:
450                 log_error("OI_CODEC_SBC_NO_SYNCWORD");
451                 break;
452 
453             case OI_CODEC_SBC_CHECKSUM_MISMATCH:
454                 // The next frame is somehow corrupt.
455                 log_info("OI_CODEC_SBC_CHECKSUM_MISMATCH");
456                 // Did the codec consume any bytes?
457                 if (bytes_processed > 0){
458                     // Good. Nothing to do.
459                 } else {
460                     // Skip the bogus frame by skipping the header.
461                     bytes_processed = 1;
462                 }
463                 break;
464 
465             case OI_STATUS_INVALID_PARAMETERS:
466                 // This caused by corrupt frames.
467                 // The codec apparently does not recover from this.
468                 // Re-initialize the codec.
469                 log_info("SBC decode: invalid parameters: resetting codec");
470                 if (OI_CODEC_mSBC_DecoderReset(&(bd_decoder_state.decoder_context), bd_decoder_state.decoder_data, sizeof(bd_decoder_state.decoder_data)) != OI_STATUS_SUCCESS){
471                     log_info("SBC decode: resetting codec failed");
472                 }
473                 break;
474             default:
475                 log_info("Frame decode error: %d", status);
476                 break;
477         }
478 
479         if (status != 0){
480             decoder_state->msbc_bad_bytes += bytes_processed;
481         }
482 
483         memmove(decoder_state->frame_buffer, decoder_state->frame_buffer + bytes_processed, decoder_state->bytes_in_frame_buffer);
484     }
485 
486     // ignore bad data before first sync
487     if (!decoder_state->first_good_frame_found) return;
488 
489 
490     // PLC
491     while (decoder_state->msbc_bad_bytes >= msbc_frame_size){
492         // printf("BAD Bytes %u\n",  decoder_state->msbc_bad_bytes);
493         decoder_state->msbc_bad_bytes -= msbc_frame_size;
494         state->bad_frames_nr++;
495 
496         // prepare zero signal frame
497         const OI_BYTE * frame_data  = btstack_sbc_plc_zero_signal_frame();
498         OI_UINT32 bytes_in_frame_buffer = 57;
499 
500         // printf("Bad-Before: bytes in buffer %u, pcm bytes %u\n", bytes_in_frame_buffer,  decoder_state->pcm_bytes);
501 
502         OI_STATUS status = status = OI_CODEC_SBC_DecodeFrame(&(decoder_state->decoder_context),
503                                             &frame_data,
504                                             &bytes_in_frame_buffer,
505                                             decoder_state->pcm_plc_data,
506                                             &(decoder_state->pcm_bytes));
507         // printf("Bad-After: bytes in buffer %u, pcm bytes %u\n", bytes_in_frame_buffer,  decoder_state->pcm_bytes);
508 
509         if (status) {
510             log_error("SBC decoder: error %d\n", status);
511         }
512         if (bytes_in_frame_buffer){
513             log_error("PLC: not all bytes of zero frame processed, left %u\n", bytes_in_frame_buffer);
514         }
515 
516         // printf_hexdump(decoder_state->pcm_plc_data, decoder_state->pcm_bytes);
517 
518         if (plc_enabled) {
519             btstack_sbc_plc_bad_frame(&state->plc_state, decoder_state->pcm_plc_data, decoder_state->pcm_data);
520         } else {
521             memcpy(decoder_state->pcm_data, decoder_state->pcm_plc_data, decoder_state->pcm_bytes);
522         }
523         // printf_hexdump(decoder_state->pcm_data, decoder_state->pcm_bytes);
524 
525         state->handle_pcm_data(decoder_state->pcm_data,
526                             btstack_sbc_decoder_num_samples_per_frame(state),
527                             btstack_sbc_decoder_num_channels(state),
528                             btstack_sbc_decoder_sample_rate(state), state->context);
529     }
530 }
531 
532 void btstack_sbc_decoder_process_data(btstack_sbc_decoder_state_t * state, int packet_status_flag, uint8_t * buffer, int size){
533     if (state->mode == SBC_MODE_mSBC){
534         btstack_sbc_decoder_process_msbc_data(state, packet_status_flag, buffer, size);
535     } else {
536         btstack_sbc_decoder_process_sbc_data(state, buffer, size);
537     }
538 }
539