xref: /btstack/src/classic/btstack_sbc_decoder_bluedroid.c (revision 5df9dc7819db26ff2f834e23204a29e4dd68d594)
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         // Use PLC to insert missing frames (after first sync found)
329         while (decoder_state->first_good_frame_found && (decoder_state->msbc_bad_bytes >= MSBC_FRAME_SIZE)){
330 
331             decoder_state->msbc_bad_bytes -= MSBC_FRAME_SIZE;
332             state->bad_frames_nr++;
333 
334             // prepare zero signal frame
335             const OI_BYTE * frame_data  = btstack_sbc_plc_zero_signal_frame();
336             OI_UINT32 bytes_in_frame_buffer = 57;
337 
338             // log_info("Trace bad frame generator, bad bytes %u", decoder_state->msbc_bad_bytes);
339             OI_STATUS status = status = OI_CODEC_SBC_DecodeFrame(&(decoder_state->decoder_context),
340                                                 &frame_data,
341                                                 &bytes_in_frame_buffer,
342                                                 decoder_state->pcm_plc_data,
343                                                 &(decoder_state->pcm_bytes));
344 
345             if (status) {
346                 log_error("SBC decoder for ZIR frame: error %d\n", status);
347             }
348 
349             if (bytes_in_frame_buffer){
350                 log_error("PLC: not all bytes of zero frame processed, left %u\n", bytes_in_frame_buffer);
351             }
352 
353             if (plc_enabled) {
354                 btstack_sbc_plc_bad_frame(&state->plc_state, decoder_state->pcm_plc_data, decoder_state->pcm_data);
355             } else {
356                 memcpy(decoder_state->pcm_data, decoder_state->pcm_plc_data, decoder_state->pcm_bytes);
357             }
358 
359             state->handle_pcm_data(decoder_state->pcm_data,
360                                 btstack_sbc_decoder_num_samples_per_frame(state),
361                                 btstack_sbc_decoder_num_channels(state),
362                                 btstack_sbc_decoder_sample_rate(state), state->context);
363         }
364 
365 
366         // fill buffer with new data
367         int bytes_missing_for_complete_msbc_frame = MSBC_FRAME_SIZE - decoder_state->bytes_in_frame_buffer;
368         int bytes_to_append = btstack_min(input_bytes_to_process, bytes_missing_for_complete_msbc_frame);
369         if (bytes_to_append) {
370             append_received_sbc_data(decoder_state, buffer, bytes_to_append);
371             buffer += bytes_to_append;
372             input_bytes_to_process -= bytes_to_append;
373         }
374 
375         // complete frame in  buffer?
376         if (decoder_state->bytes_in_frame_buffer < MSBC_FRAME_SIZE) break;
377 
378         uint16_t bytes_in_frame_buffer_before_decoding = decoder_state->bytes_in_frame_buffer;
379         uint16_t bytes_processed = 0;
380         const OI_BYTE *frame_data = decoder_state->frame_buffer;
381 
382         // testing only - corrupt frame periodically
383         static int frame_count = 0;
384         if (corrupt_frame_period > 0){
385            frame_count++;
386 
387             if ((frame_count % corrupt_frame_period) == 0){
388                 *(uint8_t*)&frame_data[5] = 0;
389                 frame_count = 0;
390             }
391         }
392 
393         // assert frame looks like this: 01 x8 AD [rest of frame 56 bytes] 00
394         int h2_syncword = 0;
395         int h2_sync_pos = find_h2_sync(frame_data, decoder_state->bytes_in_frame_buffer, &h2_syncword);
396         if (h2_sync_pos < 0){
397             // no sync found, discard all but last 2 bytes
398             bytes_processed = decoder_state->bytes_in_frame_buffer - 2;
399             memmove(decoder_state->frame_buffer, decoder_state->frame_buffer + bytes_processed, decoder_state->bytes_in_frame_buffer);
400             decoder_state->bytes_in_frame_buffer -= bytes_processed;    // == 2
401             // don't try PLC without at least a single good frame
402             if (decoder_state->first_good_frame_found){
403                 decoder_state->msbc_bad_bytes += bytes_processed;
404             }
405             continue;
406         }
407 
408         decoder_state->h2_sequence_nr = h2_syncword;
409 
410         // drop data before it
411         bytes_processed = h2_sync_pos - 2;
412         if (bytes_processed > 2){
413             memmove(decoder_state->frame_buffer, decoder_state->frame_buffer + bytes_processed, decoder_state->bytes_in_frame_buffer);
414             decoder_state->bytes_in_frame_buffer -= bytes_processed;
415             // don't try PLC without at least a single good frame
416             if (decoder_state->first_good_frame_found){
417                 decoder_state->msbc_bad_bytes += bytes_processed;
418             }
419             continue;
420         }
421 
422         int bad_frame = 0;
423         int zero_seq_found = find_sequence_of_zeros(frame_data, decoder_state->bytes_in_frame_buffer, 20);
424 
425         // after first valid frame, zero sequences count as bad frames
426         if (decoder_state->first_good_frame_found){
427             bad_frame = zero_seq_found || packet_status_flag;
428         }
429 
430         if (bad_frame){
431             // stats
432             if (zero_seq_found){
433                 state->zero_frames_nr++;
434             } else {
435                 state->bad_frames_nr++;
436             }
437 #ifdef LOG_FRAME_STATUS
438             if (zero_seq_found){
439                 printf("%d : ZERO FRAME\n", decoder_state->h2_sequence_nr);
440             } else {
441                 printf("%d : BAD FRAME\n", decoder_state->h2_sequence_nr);
442             }
443 #endif
444             // retry after dropoing 3 byte sync
445             bytes_processed = 3;
446             memmove(decoder_state->frame_buffer, decoder_state->frame_buffer + bytes_processed, decoder_state->bytes_in_frame_buffer);
447             decoder_state->bytes_in_frame_buffer -= bytes_processed;
448             decoder_state->msbc_bad_bytes        += bytes_processed;
449             // log_info("Trace bad frame");
450             continue;
451         }
452 
453         // ready to decode frame
454         OI_STATUS status = OI_CODEC_SBC_DecodeFrame(&(decoder_state->decoder_context),
455                                             &frame_data,
456                                             &(decoder_state->bytes_in_frame_buffer),
457                                             decoder_state->pcm_plc_data,
458                                             &(decoder_state->pcm_bytes));
459 
460         bytes_processed = bytes_in_frame_buffer_before_decoding - decoder_state->bytes_in_frame_buffer;
461         // log_info("Trace decode status %u, processed %u (bad bytes %u), bytes in buffer %u", (int) status, bytes_processed, decoder_state->msbc_bad_bytes, decoder_state->bytes_in_frame_buffer);
462 
463         switch(status){
464             case 0:
465                 // synced
466                 decoder_state->first_good_frame_found = 1;
467 
468                 // get rid of padding byte, not processed by SBC decoder
469                 decoder_state->bytes_in_frame_buffer = 0;
470 
471                 // restart counting bad bytes
472                 decoder_state->msbc_bad_bytes = 0;
473 
474                 // feed good frame into PLC history
475                 btstack_sbc_plc_good_frame(&state->plc_state, decoder_state->pcm_plc_data, decoder_state->pcm_data);
476 
477                 // deliver PCM data
478                 state->handle_pcm_data(decoder_state->pcm_data,
479                                     btstack_sbc_decoder_num_samples_per_frame(state),
480                                     btstack_sbc_decoder_num_channels(state),
481                                     btstack_sbc_decoder_sample_rate(state), state->context);
482 
483                 // stats
484                 state->good_frames_nr++;
485                 continue;
486 
487             case OI_CODEC_SBC_NOT_ENOUGH_HEADER_DATA:
488                 log_info("OI_CODEC_SBC_NOT_ENOUGH_HEADER_DATA");
489                 break;
490             case OI_CODEC_SBC_NOT_ENOUGH_BODY_DATA:
491                 log_info("OI_CODEC_SBC_NOT_ENOUGH_BODY_DATA");
492                 break;
493             case OI_CODEC_SBC_NOT_ENOUGH_AUDIO_DATA:
494                 log_info("OI_CODEC_SBC_NOT_ENOUGH_AUDIO_DATA");
495                 break;
496             case OI_CODEC_SBC_NO_SYNCWORD:
497                 log_info("OI_CODEC_SBC_NO_SYNCWORD");
498                 break;
499 
500             case OI_CODEC_SBC_CHECKSUM_MISMATCH:
501                 // The next frame is somehow corrupt.
502                 log_info("OI_CODEC_SBC_CHECKSUM_MISMATCH");
503                 // Did the codec consume any bytes?
504                 if (bytes_processed > 0){
505                     // Good. Nothing to do.
506                 } else {
507                     // Skip the bogus frame by skipping the header.
508                     bytes_processed = 1;
509                 }
510                 break;
511 
512             case OI_STATUS_INVALID_PARAMETERS:
513                 // This caused by corrupt frames.
514                 // The codec apparently does not recover from this.
515                 // Re-initialize the codec.
516                 log_info("SBC decode: invalid parameters: resetting codec");
517                 if (OI_CODEC_mSBC_DecoderReset(&(bd_decoder_state.decoder_context), bd_decoder_state.decoder_data, sizeof(bd_decoder_state.decoder_data)) != OI_STATUS_SUCCESS){
518                     log_info("SBC decode: resetting codec failed");
519                 }
520                 break;
521             default:
522                 log_info("Frame decode error: %d", status);
523                 break;
524         }
525 
526         // on success, while loop was restarted, so all processed bytes have been "bad"
527         decoder_state->msbc_bad_bytes += bytes_processed;
528 
529         // drop processed bytes from frame buffer
530         memmove(decoder_state->frame_buffer, decoder_state->frame_buffer + bytes_processed, decoder_state->bytes_in_frame_buffer);
531     }
532 }
533 
534 void btstack_sbc_decoder_process_data(btstack_sbc_decoder_state_t * state, int packet_status_flag, uint8_t * buffer, int size){
535     if (state->mode == SBC_MODE_mSBC){
536         btstack_sbc_decoder_process_msbc_data(state, packet_status_flag, buffer, size);
537     } else {
538         btstack_sbc_decoder_process_sbc_data(state, buffer, size);
539     }
540 }
541