xref: /btstack/src/classic/btstack_sbc_decoder_bluedroid.c (revision 7cdc89a533ca236b2c2564b759993b788bae89d3)
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     (void)memcpy(state->frame_buffer + state->bytes_in_frame_buffer, buffer,
207                  size);
208     state->bytes_in_frame_buffer += size;
209 }
210 
211 
212 static void btstack_sbc_decoder_process_sbc_data(btstack_sbc_decoder_state_t * state, uint8_t * buffer, int size){
213     bludroid_decoder_state_t * decoder_state = (bludroid_decoder_state_t*)state->decoder_state;
214     int input_bytes_to_process = size;
215     int keep_decoding = 1;
216 
217     while (keep_decoding) {
218         // Fill decoder_state->frame_buffer as much as possible.
219         int bytes_free_in_frame_buffer = SBC_MAX_FRAME_LEN - decoder_state->bytes_in_frame_buffer;
220         int bytes_to_append = btstack_min(input_bytes_to_process, bytes_free_in_frame_buffer);
221         if (bytes_to_append){
222             append_received_sbc_data(decoder_state, buffer, bytes_to_append);
223             buffer += bytes_to_append;
224             input_bytes_to_process -= bytes_to_append;
225         }
226 
227         // Decode the next frame in decoder_state->frame_buffer.
228         int bytes_in_frame_buffer_before_decoding = decoder_state->bytes_in_frame_buffer;
229         const OI_BYTE *frame_data = decoder_state->frame_buffer;
230         OI_UINT32 frame_data_len = decoder_state->bytes_in_frame_buffer;
231         OI_STATUS status = OI_CODEC_SBC_DecodeFrame(&(decoder_state->decoder_context),
232                                                     &frame_data,
233                                                     &frame_data_len,
234                                                     decoder_state->pcm_plc_data,
235                                                     &(decoder_state->pcm_bytes));
236         uint16_t bytes_processed = bytes_in_frame_buffer_before_decoding - frame_data_len;
237 
238         // testing only - corrupt frame periodically
239         static int frame_count = 0;
240         if (corrupt_frame_period > 0){
241             frame_count++;
242 
243             if ((frame_count % corrupt_frame_period) == 0){
244                 *(uint8_t*)&frame_data[5] = 0;
245                 frame_count = 0;
246             }
247         }
248 
249         // Handle decoding result.
250         switch(status){
251             case OI_STATUS_SUCCESS:
252             case OI_CODEC_SBC_PARTIAL_DECODE:
253                 state->handle_pcm_data(decoder_state->pcm_plc_data,
254                                        btstack_sbc_decoder_num_samples_per_frame(state),
255                                        btstack_sbc_decoder_num_channels(state),
256                                        btstack_sbc_decoder_sample_rate(state), state->context);
257                 state->good_frames_nr++;
258                 break;
259 
260             case OI_CODEC_SBC_NOT_ENOUGH_HEADER_DATA:
261             case OI_CODEC_SBC_NOT_ENOUGH_BODY_DATA:
262             case OI_CODEC_SBC_NOT_ENOUGH_AUDIO_DATA:
263                 if (input_bytes_to_process > 0){
264                     // Should never occur: The SBC code claims there is not enough bytes in the frame_buffer,
265                     // but the frame_buffer was full. (The frame_buffer is always full before decoding when input_bytes_to_process > 0.)
266                     // Clear frame_buffer.
267                     log_info("SBC decode: frame_buffer too small for frame");
268                     bytes_processed = bytes_in_frame_buffer_before_decoding;
269                 } else {
270                     // Exit decode loop, because there is not enough data in frame_buffer to decode the next frame.
271                     keep_decoding = 0;
272                 }
273                 break;
274 
275             case OI_CODEC_SBC_NO_SYNCWORD:
276                 // This means the entire frame_buffer did not contain the syncword.
277                 // Discard the frame_buffer contents.
278                 log_info("SBC decode: no syncword found");
279                 bytes_processed = bytes_in_frame_buffer_before_decoding;
280                 break;
281 
282             case OI_CODEC_SBC_CHECKSUM_MISMATCH:
283                 // The next frame is somehow corrupt.
284                 log_info("SBC decode: checksum error");
285                 // Did the codec consume any bytes?
286                 if (bytes_processed > 0){
287                     // Good. Nothing to do.
288                 } else {
289                     // Skip the bogus frame by skipping the header.
290                     bytes_processed = 1;
291                 }
292                 break;
293 
294             case OI_STATUS_INVALID_PARAMETERS:
295                 // This caused by corrupt frames.
296                 // The codec apparently does not recover from this.
297                 // Re-initialize the codec.
298                 log_info("SBC decode: invalid parameters: resetting codec");
299                 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){
300                     log_info("SBC decode: resetting codec failed");
301 
302                 }
303                 break;
304             default:
305                 // Anything else went wrong.
306                 // Skip a few bytes and try again.
307                 bytes_processed = 1;
308                 log_info("SBC decode: unknown status %d", status);
309                 break;
310         }
311 
312         // Remove decoded frame from decoder_state->frame_buffer.
313         if (bytes_processed > bytes_in_frame_buffer_before_decoding) {
314             bytes_processed = bytes_in_frame_buffer_before_decoding;
315         }
316         memmove(decoder_state->frame_buffer, decoder_state->frame_buffer + bytes_processed, bytes_in_frame_buffer_before_decoding - bytes_processed);
317         decoder_state->bytes_in_frame_buffer -= bytes_processed;
318     }
319 }
320 
321 
322 static void btstack_sbc_decoder_process_msbc_data(btstack_sbc_decoder_state_t * state, int packet_status_flag, uint8_t * buffer, int size){
323     bludroid_decoder_state_t * decoder_state = (bludroid_decoder_state_t*)state->decoder_state;
324     int input_bytes_to_process = size;
325     unsigned int MSBC_FRAME_SIZE = 60;
326 
327     while (input_bytes_to_process > 0){
328 
329         // Use PLC to insert missing frames (after first sync found)
330         while (decoder_state->first_good_frame_found && (decoder_state->msbc_bad_bytes >= MSBC_FRAME_SIZE)){
331 
332             decoder_state->msbc_bad_bytes -= MSBC_FRAME_SIZE;
333             state->bad_frames_nr++;
334 
335             // prepare zero signal frame
336             const OI_BYTE * frame_data  = btstack_sbc_plc_zero_signal_frame();
337             OI_UINT32 bytes_in_frame_buffer = 57;
338 
339             // log_info("Trace bad frame generator, bad bytes %u", decoder_state->msbc_bad_bytes);
340             OI_STATUS status = status = OI_CODEC_SBC_DecodeFrame(&(decoder_state->decoder_context),
341                                                 &frame_data,
342                                                 &bytes_in_frame_buffer,
343                                                 decoder_state->pcm_plc_data,
344                                                 &(decoder_state->pcm_bytes));
345 
346             if (status) {
347                 log_error("SBC decoder for ZIR frame: error %d\n", status);
348             }
349 
350             if (bytes_in_frame_buffer){
351                 log_error("PLC: not all bytes of zero frame processed, left %u\n", bytes_in_frame_buffer);
352             }
353 
354             if (plc_enabled) {
355                 btstack_sbc_plc_bad_frame(&state->plc_state, decoder_state->pcm_plc_data, decoder_state->pcm_data);
356             } else {
357                 (void)memcpy(decoder_state->pcm_data,
358                              decoder_state->pcm_plc_data,
359                              decoder_state->pcm_bytes);
360             }
361 
362             state->handle_pcm_data(decoder_state->pcm_data,
363                                 btstack_sbc_decoder_num_samples_per_frame(state),
364                                 btstack_sbc_decoder_num_channels(state),
365                                 btstack_sbc_decoder_sample_rate(state), state->context);
366         }
367 
368 
369         // fill buffer with new data
370         int bytes_missing_for_complete_msbc_frame = MSBC_FRAME_SIZE - decoder_state->bytes_in_frame_buffer;
371         int bytes_to_append = btstack_min(input_bytes_to_process, bytes_missing_for_complete_msbc_frame);
372         if (bytes_to_append) {
373             append_received_sbc_data(decoder_state, buffer, bytes_to_append);
374             buffer += bytes_to_append;
375             input_bytes_to_process -= bytes_to_append;
376         }
377 
378         // complete frame in  buffer?
379         if (decoder_state->bytes_in_frame_buffer < MSBC_FRAME_SIZE) break;
380 
381         uint16_t bytes_in_frame_buffer_before_decoding = decoder_state->bytes_in_frame_buffer;
382         uint16_t bytes_processed = 0;
383         const OI_BYTE *frame_data = decoder_state->frame_buffer;
384 
385         // testing only - corrupt frame periodically
386         static int frame_count = 0;
387         if (corrupt_frame_period > 0){
388            frame_count++;
389 
390             if ((frame_count % corrupt_frame_period) == 0){
391                 *(uint8_t*)&frame_data[5] = 0;
392                 frame_count = 0;
393             }
394         }
395 
396         // assert frame looks like this: 01 x8 AD [rest of frame 56 bytes] 00
397         int h2_syncword = 0;
398         int h2_sync_pos = find_h2_sync(frame_data, decoder_state->bytes_in_frame_buffer, &h2_syncword);
399         if (h2_sync_pos < 0){
400             // no sync found, discard all but last 2 bytes
401             bytes_processed = decoder_state->bytes_in_frame_buffer - 2;
402             memmove(decoder_state->frame_buffer, decoder_state->frame_buffer + bytes_processed, decoder_state->bytes_in_frame_buffer);
403             decoder_state->bytes_in_frame_buffer -= bytes_processed;    // == 2
404             // don't try PLC without at least a single good frame
405             if (decoder_state->first_good_frame_found){
406                 decoder_state->msbc_bad_bytes += bytes_processed;
407             }
408             continue;
409         }
410 
411         decoder_state->h2_sequence_nr = h2_syncword;
412 
413         // drop data before it
414         bytes_processed = h2_sync_pos - 2;
415         if (bytes_processed > 2){
416             memmove(decoder_state->frame_buffer, decoder_state->frame_buffer + bytes_processed, decoder_state->bytes_in_frame_buffer);
417             decoder_state->bytes_in_frame_buffer -= bytes_processed;
418             // don't try PLC without at least a single good frame
419             if (decoder_state->first_good_frame_found){
420                 decoder_state->msbc_bad_bytes += bytes_processed;
421             }
422             continue;
423         }
424 
425         int bad_frame = 0;
426         int zero_seq_found = find_sequence_of_zeros(frame_data, decoder_state->bytes_in_frame_buffer, 20);
427 
428         // after first valid frame, zero sequences count as bad frames
429         if (decoder_state->first_good_frame_found){
430             bad_frame = zero_seq_found || packet_status_flag;
431         }
432 
433         if (bad_frame){
434             // stats
435             if (zero_seq_found){
436                 state->zero_frames_nr++;
437             } else {
438                 state->bad_frames_nr++;
439             }
440 #ifdef LOG_FRAME_STATUS
441             if (zero_seq_found){
442                 printf("%d : ZERO FRAME\n", decoder_state->h2_sequence_nr);
443             } else {
444                 printf("%d : BAD FRAME\n", decoder_state->h2_sequence_nr);
445             }
446 #endif
447             // retry after dropoing 3 byte sync
448             bytes_processed = 3;
449             memmove(decoder_state->frame_buffer, decoder_state->frame_buffer + bytes_processed, decoder_state->bytes_in_frame_buffer);
450             decoder_state->bytes_in_frame_buffer -= bytes_processed;
451             decoder_state->msbc_bad_bytes        += bytes_processed;
452             // log_info("Trace bad frame");
453             continue;
454         }
455 
456         // ready to decode frame
457         OI_STATUS status = OI_CODEC_SBC_DecodeFrame(&(decoder_state->decoder_context),
458                                             &frame_data,
459                                             &(decoder_state->bytes_in_frame_buffer),
460                                             decoder_state->pcm_plc_data,
461                                             &(decoder_state->pcm_bytes));
462 
463         bytes_processed = bytes_in_frame_buffer_before_decoding - decoder_state->bytes_in_frame_buffer;
464         // 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);
465 
466         switch(status){
467             case 0:
468                 // synced
469                 decoder_state->first_good_frame_found = 1;
470 
471                 // get rid of padding byte, not processed by SBC decoder
472                 decoder_state->bytes_in_frame_buffer = 0;
473 
474                 // restart counting bad bytes
475                 decoder_state->msbc_bad_bytes = 0;
476 
477                 // feed good frame into PLC history
478                 btstack_sbc_plc_good_frame(&state->plc_state, decoder_state->pcm_plc_data, decoder_state->pcm_data);
479 
480                 // deliver PCM data
481                 state->handle_pcm_data(decoder_state->pcm_data,
482                                     btstack_sbc_decoder_num_samples_per_frame(state),
483                                     btstack_sbc_decoder_num_channels(state),
484                                     btstack_sbc_decoder_sample_rate(state), state->context);
485 
486                 // stats
487                 state->good_frames_nr++;
488                 continue;
489 
490             case OI_CODEC_SBC_NOT_ENOUGH_HEADER_DATA:
491                 log_info("OI_CODEC_SBC_NOT_ENOUGH_HEADER_DATA");
492                 break;
493             case OI_CODEC_SBC_NOT_ENOUGH_BODY_DATA:
494                 log_info("OI_CODEC_SBC_NOT_ENOUGH_BODY_DATA");
495                 break;
496             case OI_CODEC_SBC_NOT_ENOUGH_AUDIO_DATA:
497                 log_info("OI_CODEC_SBC_NOT_ENOUGH_AUDIO_DATA");
498                 break;
499             case OI_CODEC_SBC_NO_SYNCWORD:
500                 log_info("OI_CODEC_SBC_NO_SYNCWORD");
501                 break;
502 
503             case OI_CODEC_SBC_CHECKSUM_MISMATCH:
504                 // The next frame is somehow corrupt.
505                 log_info("OI_CODEC_SBC_CHECKSUM_MISMATCH");
506                 // Did the codec consume any bytes?
507                 if (bytes_processed > 0){
508                     // Good. Nothing to do.
509                 } else {
510                     // Skip the bogus frame by skipping the header.
511                     bytes_processed = 1;
512                 }
513                 break;
514 
515             case OI_STATUS_INVALID_PARAMETERS:
516                 // This caused by corrupt frames.
517                 // The codec apparently does not recover from this.
518                 // Re-initialize the codec.
519                 log_info("SBC decode: invalid parameters: resetting codec");
520                 if (OI_CODEC_mSBC_DecoderReset(&(bd_decoder_state.decoder_context), bd_decoder_state.decoder_data, sizeof(bd_decoder_state.decoder_data)) != OI_STATUS_SUCCESS){
521                     log_info("SBC decode: resetting codec failed");
522                 }
523                 break;
524             default:
525                 log_info("Frame decode error: %d", status);
526                 break;
527         }
528 
529         // on success, while loop was restarted, so all processed bytes have been "bad"
530         decoder_state->msbc_bad_bytes += bytes_processed;
531 
532         // drop processed bytes from frame buffer
533         memmove(decoder_state->frame_buffer, decoder_state->frame_buffer + bytes_processed, decoder_state->bytes_in_frame_buffer);
534     }
535 }
536 
537 void btstack_sbc_decoder_process_data(btstack_sbc_decoder_state_t * state, int packet_status_flag, uint8_t * buffer, int size){
538     if (state->mode == SBC_MODE_mSBC){
539         btstack_sbc_decoder_process_msbc_data(state, packet_status_flag, buffer, size);
540     } else {
541         btstack_sbc_decoder_process_sbc_data(state, buffer, size);
542     }
543 }
544