xref: /btstack/src/classic/btstack_sbc_decoder_bluedroid.c (revision a305eb53511cf3f48f09c8b0adbf03644957476e)
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 h2_sequence_nr;
77     int search_new_sync_word;
78     int sync_word_found;
79     int first_good_frame_found;
80 } bludroid_decoder_state_t;
81 
82 static btstack_sbc_decoder_state_t * sbc_decoder_state_singleton = NULL;
83 static bludroid_decoder_state_t bd_decoder_state;
84 
85 // Testing only - START
86 static int plc_enabled = 1;
87 static int corrupt_frame_period = -1;
88 // Testing - STOP
89 
90 void btstack_sbc_decoder_test_disable_plc(void){
91     plc_enabled = 0;
92 }
93 
94 void btstack_sbc_decoder_test_simulate_corrupt_frames(int period){
95     corrupt_frame_period = period;
96 }
97 
98 static int find_sequence_of_zeros(const OI_BYTE *frame_data, OI_UINT32 frame_bytes, int seq_length){
99     int zero_seq_count = 0;
100     unsigned int i;
101     for (i=0; i<frame_bytes; i++){
102         if (frame_data[i] == 0) {
103             zero_seq_count++;
104             if (zero_seq_count >= seq_length) return zero_seq_count;
105         } else {
106             zero_seq_count = 0;
107         }
108     }
109     return 0;
110 }
111 
112 static int find_h2_syncword(const OI_BYTE *frame_data, OI_UINT32 frame_bytes){
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             break;
121         }
122         h2_first_byte = h2_second_byte;
123         h2_second_byte = frame_data[i];
124     }
125     if (h2_first_byte != 1) return -1;
126 
127     // check if upper nibble of second byte is 0x08
128     uint8_t ln = h2_second_byte & 0x0F;
129     if (ln != 8) return -1;
130 
131     // check that bits 0+2 == bits 1+3
132     uint8_t hn = h2_second_byte >> 4;
133     if  ( ((hn>>1) & 0x05) != (hn & 0x05) ) return -1;
134 
135     return ((hn & 0x04) >> 1) | (hn & 0x01);
136 }
137 
138 int btstack_sbc_decoder_num_samples_per_frame(btstack_sbc_decoder_state_t * state){
139     bludroid_decoder_state_t * decoder_state = (bludroid_decoder_state_t *) state->decoder_state;
140     return decoder_state->decoder_context.common.frameInfo.nrof_blocks * decoder_state->decoder_context.common.frameInfo.nrof_subbands;
141 }
142 
143 int btstack_sbc_decoder_num_channels(btstack_sbc_decoder_state_t * state){
144     bludroid_decoder_state_t * decoder_state = (bludroid_decoder_state_t *) state->decoder_state;
145     return decoder_state->decoder_context.common.frameInfo.nrof_channels;
146 }
147 
148 int btstack_sbc_decoder_sample_rate(btstack_sbc_decoder_state_t * state){
149     bludroid_decoder_state_t * decoder_state = (bludroid_decoder_state_t *) state->decoder_state;
150     return decoder_state->decoder_context.common.frameInfo.frequency;
151 }
152 
153 #ifdef OI_DEBUG
154 void OI_AssertFail(const char* file, int line, const char* reason){
155     log_error("AssertFail file %s, line %d, reason %s", file, line, reason);
156 }
157 #endif
158 
159 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){
160     if (sbc_decoder_state_singleton && sbc_decoder_state_singleton != state ){
161         log_error("SBC decoder: different sbc decoder state is allready registered");
162     }
163     OI_STATUS status = OI_STATUS_SUCCESS;
164     switch (mode){
165         case SBC_MODE_STANDARD:
166             // note: we always request stereo output, even for mono input
167             status = OI_CODEC_SBC_DecoderReset(&(bd_decoder_state.decoder_context), bd_decoder_state.decoder_data, sizeof(bd_decoder_state.decoder_data), 2, 2, FALSE);
168             break;
169         case SBC_MODE_mSBC:
170             status = OI_CODEC_mSBC_DecoderReset(&(bd_decoder_state.decoder_context), bd_decoder_state.decoder_data, sizeof(bd_decoder_state.decoder_data));
171             break;
172         default:
173             break;
174     }
175 
176     if (status != OI_STATUS_SUCCESS){
177         log_error("SBC decoder: error during reset %d\n", status);
178     }
179 
180     sbc_decoder_state_singleton = state;
181 
182     bd_decoder_state.bytes_in_frame_buffer = 0;
183     bd_decoder_state.pcm_bytes = sizeof(bd_decoder_state.pcm_data);
184     bd_decoder_state.h2_sequence_nr = -1;
185     bd_decoder_state.sync_word_found = 0;
186     bd_decoder_state.search_new_sync_word = 0;
187     if (mode == SBC_MODE_mSBC){
188         bd_decoder_state.search_new_sync_word = 1;
189     }
190     bd_decoder_state.first_good_frame_found = 0;
191 
192     memset(state, 0, sizeof(btstack_sbc_decoder_state_t));
193     state->handle_pcm_data = callback;
194     state->mode = mode;
195     state->context = context;
196     state->decoder_state = &bd_decoder_state;
197     btstack_sbc_plc_init(&state->plc_state);
198 }
199 
200 static void append_received_sbc_data(bludroid_decoder_state_t * state, uint8_t * buffer, int size){
201     int numFreeBytes = sizeof(state->frame_buffer) - state->bytes_in_frame_buffer;
202 
203     if (size > numFreeBytes){
204         log_error("SBC data: more bytes read %u than free bytes in buffer %u", size, numFreeBytes);
205     }
206 
207     memcpy(state->frame_buffer + state->bytes_in_frame_buffer, buffer, 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, int packet_status_flag, 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         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                 if (state->mode == SBC_MODE_mSBC){
253                     decoder_state->search_new_sync_word = 1;
254                     decoder_state->sync_word_found = 0;
255                 }
256 
257                 state->handle_pcm_data(decoder_state->pcm_plc_data,
258                                        btstack_sbc_decoder_num_samples_per_frame(state),
259                                        btstack_sbc_decoder_num_channels(state),
260                                        btstack_sbc_decoder_sample_rate(state), state->context);
261                 state->good_frames_nr++;
262                 break;
263 
264             case OI_CODEC_SBC_NOT_ENOUGH_HEADER_DATA:
265             case OI_CODEC_SBC_NOT_ENOUGH_BODY_DATA:
266             case OI_CODEC_SBC_NOT_ENOUGH_AUDIO_DATA:
267                 if (decoder_state->sync_word_found){
268                     decoder_state->search_new_sync_word = 0;
269                 }
270                 if (input_bytes_to_process > 0){
271                     // Should never occur: The SBC code claims there is not enough bytes in the frame_buffer,
272                     // but the frame_buffer was full. (The frame_buffer is always full before decoding when input_bytes_to_process > 0.)
273                     // Clear frame_buffer.
274                     log_info("SBC decode: frame_buffer too small for frame");
275                     bytes_processed = bytes_in_frame_buffer_before_decoding;
276                 } else {
277                     // Exit decode loop, because there is not enough data in frame_buffer to decode the next frame.
278                     keep_decoding = 0;
279                 }
280                 break;
281 
282             case OI_CODEC_SBC_NO_SYNCWORD:
283                 // This means the entire frame_buffer did not contain the syncword.
284                 // Discard the frame_buffer contents.
285                 log_info("SBC decode: no syncword found");
286                 bytes_processed = bytes_in_frame_buffer_before_decoding;
287                 break;
288 
289             case OI_CODEC_SBC_CHECKSUM_MISMATCH:
290                 // The next frame is somehow corrupt.
291                 log_info("SBC decode: checksum error");
292                 // Did the codec consume any bytes?
293                 if (bytes_processed > 0){
294                     // Good. Nothing to do.
295                 } else {
296                     // Skip the bogus frame by skipping the header.
297                     bytes_processed = 1;
298                 }
299                 break;
300 
301             case OI_STATUS_INVALID_PARAMETERS:
302                 // This caused by corrupt frames.
303                 // The codec apparently does not recover from this.
304                 // Re-initialize the codec.
305                 log_info("SBC decode: invalid parameters: resetting codec");
306                 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){
307                     log_info("SBC decode: resetting codec failed");
308 
309                 }
310                 break;
311             default:
312                 // Anything else went wrong.
313                 // Skip a few bytes and try again.
314                 bytes_processed = 1;
315                 log_info("SBC decode: unknown status %d", status);
316                 break;
317         }
318 
319         // Remove decoded frame from decoder_state->frame_buffer.
320         if (bytes_processed > bytes_in_frame_buffer_before_decoding) {
321             bytes_processed = bytes_in_frame_buffer_before_decoding;
322         }
323         memmove(decoder_state->frame_buffer, decoder_state->frame_buffer + bytes_processed, bytes_in_frame_buffer_before_decoding - bytes_processed);
324         decoder_state->bytes_in_frame_buffer -= bytes_processed;
325     }
326 }
327 
328 
329 static void btstack_sbc_decoder_process_msbc_data(btstack_sbc_decoder_state_t * state, int packet_status_flag, uint8_t * buffer, int size){
330 
331     bludroid_decoder_state_t * decoder_state = (bludroid_decoder_state_t*)state->decoder_state;
332     int input_bytes_to_process = size;
333     unsigned int msbc_frame_size = 57;
334 
335     // printf("<<-- enter -->>\n");
336     // printf("Process data: in buffer %u, new %u\n", decoder_state->bytes_in_frame_buffer, size);
337 
338     while (input_bytes_to_process > 0){
339 
340         // fill buffer with new data
341         int bytes_missing_for_complete_msbc_frame = msbc_frame_size - decoder_state->bytes_in_frame_buffer;
342         int bytes_to_append = btstack_min(input_bytes_to_process, bytes_missing_for_complete_msbc_frame);
343         if (bytes_to_append) {
344             append_received_sbc_data(decoder_state, buffer, bytes_to_append);
345             buffer += bytes_to_append;
346             input_bytes_to_process -= bytes_to_append;
347         }
348 
349         if (decoder_state->bytes_in_frame_buffer < msbc_frame_size){
350             // printf("not enough data %d > %d\n", msbc_frame_size, decoder_state->bytes_in_frame_buffer);
351             if (input_bytes_to_process){
352                 log_error("SHOULD NOT HAPPEN... not enough bytes, but bytes left to process");
353             }
354             break;
355         }
356 
357         uint16_t bytes_in_frame_buffer_before_decoding = decoder_state->bytes_in_frame_buffer;
358         uint16_t bytes_processed = 0;
359         const OI_BYTE *frame_data = decoder_state->frame_buffer;
360 
361         static int frame_count = 0;
362         if (corrupt_frame_period > 0){
363            frame_count++;
364 
365             if (frame_count % corrupt_frame_period == 0){
366                 *(uint8_t*)&frame_data[5] = 0;
367                 frame_count = 0;
368             }
369         }
370 
371         OI_STATUS status = OI_STATUS_SUCCESS;
372         int bad_frame = 0;
373         int zero_seq_found = 0;
374 
375         if (decoder_state->first_good_frame_found){
376             zero_seq_found = find_sequence_of_zeros(frame_data, decoder_state->bytes_in_frame_buffer, 20);
377             bad_frame = zero_seq_found || packet_status_flag;
378         }
379 
380         if (bad_frame){
381             status = OI_CODEC_SBC_CHECKSUM_MISMATCH;
382             decoder_state->bytes_in_frame_buffer = 0;
383         } else {
384             if (decoder_state->search_new_sync_word && !decoder_state->sync_word_found){
385                 int h2_syncword = find_h2_syncword(frame_data, decoder_state->bytes_in_frame_buffer);
386 
387                 if (h2_syncword != -1){
388                     decoder_state->sync_word_found = 1;
389                     decoder_state->h2_sequence_nr = h2_syncword;
390                 }
391             }
392             status = OI_CODEC_SBC_DecodeFrame(&(decoder_state->decoder_context),
393                                                 &frame_data,
394                                                 &(decoder_state->bytes_in_frame_buffer),
395                                                 decoder_state->pcm_plc_data,
396                                                 &(decoder_state->pcm_bytes));
397         }
398 
399         bytes_processed = bytes_in_frame_buffer_before_decoding - decoder_state->bytes_in_frame_buffer;
400         OI_UINT32 bytes_in_frame_buffer = msbc_frame_size;
401 
402         switch(status){
403             case 0:
404                 decoder_state->first_good_frame_found = 1;
405 
406                 if (state->mode == SBC_MODE_mSBC){
407                     decoder_state->search_new_sync_word = 1;
408                     decoder_state->sync_word_found = 0;
409                 }
410 
411                 btstack_sbc_plc_good_frame(&state->plc_state, decoder_state->pcm_plc_data, decoder_state->pcm_data);
412                 state->handle_pcm_data(decoder_state->pcm_data,
413                                     btstack_sbc_decoder_num_samples_per_frame(state),
414                                     btstack_sbc_decoder_num_channels(state),
415                                     btstack_sbc_decoder_sample_rate(state), state->context);
416                 state->good_frames_nr++;
417                 continue;
418             case OI_CODEC_SBC_NOT_ENOUGH_HEADER_DATA:
419             case OI_CODEC_SBC_NOT_ENOUGH_BODY_DATA:
420                 // printf("    NOT_ENOUGH_DATA\n");
421                 if (decoder_state->sync_word_found){
422                     decoder_state->search_new_sync_word = 0;
423                 }
424                 break;
425             case OI_CODEC_SBC_NO_SYNCWORD:
426             case OI_CODEC_SBC_CHECKSUM_MISMATCH:
427                 // printf("NO_SYNCWORD or CHECKSUM_MISMATCH\n");
428                 decoder_state->bytes_in_frame_buffer = 0;
429                 if (!decoder_state->first_good_frame_found) break;
430 
431                 if (state->mode == SBC_MODE_mSBC){
432                     if (!decoder_state->sync_word_found){
433                         decoder_state->h2_sequence_nr = (decoder_state->h2_sequence_nr + 1)%4;
434                     }
435                     decoder_state->search_new_sync_word = 1;
436                     decoder_state->sync_word_found = 0;
437                 }
438 
439                 if (zero_seq_found){
440                     state->zero_frames_nr++;
441                 } else {
442                     state->bad_frames_nr++;
443                 }
444 
445 #ifdef LOG_FRAME_STATUS
446                 if (zero_seq_found){
447                     printf("%d : ZERO FRAME\n", decoder_state->h2_sequence_nr);
448                 } else {
449                     printf("%d : BAD FRAME\n", decoder_state->h2_sequence_nr);
450                 }
451                 if (decoder_state->h2_sequence_nr == 3) printf("\n");
452 #endif
453                 if (!plc_enabled) break;
454 
455                 frame_data = btstack_sbc_plc_zero_signal_frame();
456 
457                 status = OI_CODEC_SBC_DecodeFrame(&(decoder_state->decoder_context),
458                                                     &frame_data,
459                                                     &bytes_in_frame_buffer,
460                                                     decoder_state->pcm_plc_data,
461                                                     &(decoder_state->pcm_bytes));
462 
463                 if (status != 0) {
464                     log_error("SBC decoder: error %d\n", status);
465                 }
466                 btstack_sbc_plc_bad_frame(&state->plc_state, decoder_state->pcm_plc_data, decoder_state->pcm_data);
467                 state->handle_pcm_data(decoder_state->pcm_data,
468                                     btstack_sbc_decoder_num_samples_per_frame(state),
469                                     btstack_sbc_decoder_num_channels(state),
470                                     btstack_sbc_decoder_sample_rate(state), state->context);
471 
472 
473                 break;
474             default:
475                 log_info("Frame decode error: %d", status);
476                 break;
477         }
478 
479         memmove(decoder_state->frame_buffer, decoder_state->frame_buffer + bytes_processed, decoder_state->bytes_in_frame_buffer);
480     }
481 }
482 
483 void btstack_sbc_decoder_process_data(btstack_sbc_decoder_state_t * state, int packet_status_flag, uint8_t * buffer, int size){
484     if (state->mode == SBC_MODE_mSBC){
485         btstack_sbc_decoder_process_msbc_data(state, packet_status_flag, buffer, size);
486     } else {
487         btstack_sbc_decoder_process_sbc_data(state, packet_status_flag, buffer, size);
488     }
489 }
490