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, 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 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 = 57; 325 326 // printf("<<-- enter -->>\n"); 327 // printf("Process data: in buffer %u, new %u\n", decoder_state->bytes_in_frame_buffer, size); 328 329 while (input_bytes_to_process > 0){ 330 331 // fill buffer with new data 332 int bytes_missing_for_complete_msbc_frame = msbc_frame_size - decoder_state->bytes_in_frame_buffer; 333 int bytes_to_append = btstack_min(input_bytes_to_process, bytes_missing_for_complete_msbc_frame); 334 if (bytes_to_append) { 335 append_received_sbc_data(decoder_state, buffer, bytes_to_append); 336 buffer += bytes_to_append; 337 input_bytes_to_process -= bytes_to_append; 338 } 339 340 if (decoder_state->bytes_in_frame_buffer < msbc_frame_size){ 341 // printf("not enough data %d > %d\n", msbc_frame_size, decoder_state->bytes_in_frame_buffer); 342 if (input_bytes_to_process){ 343 log_error("SHOULD NOT HAPPEN... not enough bytes, but bytes left to process"); 344 } 345 break; 346 } 347 348 uint16_t bytes_in_frame_buffer_before_decoding = decoder_state->bytes_in_frame_buffer; 349 uint16_t bytes_processed = 0; 350 const OI_BYTE *frame_data = decoder_state->frame_buffer; 351 352 static int frame_count = 0; 353 if (corrupt_frame_period > 0){ 354 frame_count++; 355 356 if (frame_count % corrupt_frame_period == 0){ 357 *(uint8_t*)&frame_data[5] = 0; 358 frame_count = 0; 359 } 360 } 361 362 OI_STATUS status = OI_STATUS_SUCCESS; 363 int bad_frame = 0; 364 int zero_seq_found = 0; 365 366 if (decoder_state->first_good_frame_found){ 367 zero_seq_found = find_sequence_of_zeros(frame_data, decoder_state->bytes_in_frame_buffer, 20); 368 bad_frame = zero_seq_found || packet_status_flag; 369 } 370 371 if (bad_frame){ 372 status = OI_CODEC_SBC_CHECKSUM_MISMATCH; 373 decoder_state->bytes_in_frame_buffer = 0; 374 } else { 375 if (decoder_state->search_new_sync_word && !decoder_state->sync_word_found){ 376 int h2_syncword = find_h2_syncword(frame_data, decoder_state->bytes_in_frame_buffer); 377 378 if (h2_syncword != -1){ 379 decoder_state->sync_word_found = 1; 380 decoder_state->h2_sequence_nr = h2_syncword; 381 } 382 } 383 status = OI_CODEC_SBC_DecodeFrame(&(decoder_state->decoder_context), 384 &frame_data, 385 &(decoder_state->bytes_in_frame_buffer), 386 decoder_state->pcm_plc_data, 387 &(decoder_state->pcm_bytes)); 388 } 389 390 bytes_processed = bytes_in_frame_buffer_before_decoding - decoder_state->bytes_in_frame_buffer; 391 OI_UINT32 bytes_in_frame_buffer = msbc_frame_size; 392 393 switch(status){ 394 case 0: 395 decoder_state->first_good_frame_found = 1; 396 decoder_state->search_new_sync_word = 1; 397 decoder_state->sync_word_found = 0; 398 399 btstack_sbc_plc_good_frame(&state->plc_state, decoder_state->pcm_plc_data, decoder_state->pcm_data); 400 state->handle_pcm_data(decoder_state->pcm_data, 401 btstack_sbc_decoder_num_samples_per_frame(state), 402 btstack_sbc_decoder_num_channels(state), 403 btstack_sbc_decoder_sample_rate(state), state->context); 404 state->good_frames_nr++; 405 continue; 406 case OI_CODEC_SBC_NOT_ENOUGH_HEADER_DATA: 407 case OI_CODEC_SBC_NOT_ENOUGH_BODY_DATA: 408 case OI_CODEC_SBC_NOT_ENOUGH_AUDIO_DATA: 409 // printf(" NOT_ENOUGH_DATA\n"); 410 if (decoder_state->sync_word_found){ 411 decoder_state->search_new_sync_word = 0; 412 } 413 break; 414 case OI_CODEC_SBC_NO_SYNCWORD: 415 case OI_CODEC_SBC_CHECKSUM_MISMATCH: 416 // printf("NO_SYNCWORD or CHECKSUM_MISMATCH\n"); 417 decoder_state->bytes_in_frame_buffer = 0; 418 if (!decoder_state->first_good_frame_found) break; 419 420 if (!decoder_state->sync_word_found){ 421 decoder_state->h2_sequence_nr = (decoder_state->h2_sequence_nr + 1)%4; 422 } 423 decoder_state->search_new_sync_word = 1; 424 decoder_state->sync_word_found = 0; 425 426 if (zero_seq_found){ 427 state->zero_frames_nr++; 428 } else { 429 state->bad_frames_nr++; 430 } 431 432 #ifdef LOG_FRAME_STATUS 433 if (zero_seq_found){ 434 printf("%d : ZERO FRAME\n", decoder_state->h2_sequence_nr); 435 } else { 436 printf("%d : BAD FRAME\n", decoder_state->h2_sequence_nr); 437 } 438 if (decoder_state->h2_sequence_nr == 3) printf("\n"); 439 #endif 440 if (!plc_enabled) break; 441 442 frame_data = btstack_sbc_plc_zero_signal_frame(); 443 status = OI_CODEC_SBC_DecodeFrame(&(decoder_state->decoder_context), 444 &frame_data, 445 &bytes_in_frame_buffer, 446 decoder_state->pcm_plc_data, 447 &(decoder_state->pcm_bytes)); 448 449 if (status != 0) { 450 log_error("SBC decoder: error %d\n", status); 451 } 452 btstack_sbc_plc_bad_frame(&state->plc_state, decoder_state->pcm_plc_data, decoder_state->pcm_data); 453 state->handle_pcm_data(decoder_state->pcm_data, 454 btstack_sbc_decoder_num_samples_per_frame(state), 455 btstack_sbc_decoder_num_channels(state), 456 btstack_sbc_decoder_sample_rate(state), state->context); 457 458 459 break; 460 case OI_STATUS_INVALID_PARAMETERS: 461 // This caused by corrupt frames. 462 // The codec apparently does not recover from this. 463 // Re-initialize the codec. 464 log_info("SBC decode: invalid parameters: resetting codec"); 465 if (OI_CODEC_mSBC_DecoderReset(&(bd_decoder_state.decoder_context), bd_decoder_state.decoder_data, sizeof(bd_decoder_state.decoder_data)) != OI_STATUS_SUCCESS){ 466 log_info("SBC decode: resetting codec failed"); 467 468 } 469 break; 470 default: 471 log_info("Frame decode error: %d", status); 472 break; 473 } 474 475 memmove(decoder_state->frame_buffer, decoder_state->frame_buffer + bytes_processed, decoder_state->bytes_in_frame_buffer); 476 } 477 } 478 479 void btstack_sbc_decoder_process_data(btstack_sbc_decoder_state_t * state, int packet_status_flag, uint8_t * buffer, int size){ 480 if (state->mode == SBC_MODE_mSBC){ 481 btstack_sbc_decoder_process_msbc_data(state, packet_status_flag, buffer, size); 482 } else { 483 btstack_sbc_decoder_process_sbc_data(state, buffer, size); 484 } 485 } 486