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 216 while (input_bytes_to_process){ 217 218 // fill buffer with new data 219 int bytes_free_in_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_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 uint16_t bytes_in_buffer_before = decoder_state->bytes_in_frame_buffer; 228 uint16_t bytes_processed = 0; 229 const OI_BYTE *frame_data = decoder_state->frame_buffer; 230 231 static int frame_count = 0; 232 while (1){ 233 if (corrupt_frame_period > 0){ 234 frame_count++; 235 236 if (frame_count % corrupt_frame_period == 0){ 237 *(uint8_t*)&frame_data[5] = 0; 238 frame_count = 0; 239 } 240 } 241 242 OI_STATUS status = OI_STATUS_SUCCESS; 243 int bad_frame = 0; 244 int zero_seq_found = 0; 245 246 if (decoder_state->first_good_frame_found){ 247 zero_seq_found = find_sequence_of_zeros(frame_data, decoder_state->bytes_in_frame_buffer, 20); 248 bad_frame = zero_seq_found || packet_status_flag; 249 } 250 251 if (bad_frame){ 252 status = OI_CODEC_SBC_CHECKSUM_MISMATCH; 253 decoder_state->bytes_in_frame_buffer = 0; 254 } else { 255 memset(decoder_state->pcm_plc_data, 0x55, SBC_MAX_CHANNELS * SBC_MAX_BANDS * SBC_MAX_BLOCKS * 2); 256 status = OI_CODEC_SBC_DecodeFrame(&(decoder_state->decoder_context), 257 &frame_data, 258 &(decoder_state->bytes_in_frame_buffer), 259 decoder_state->pcm_plc_data, 260 &(decoder_state->pcm_bytes)); 261 } 262 263 bytes_processed = bytes_in_buffer_before - decoder_state->bytes_in_frame_buffer; 264 switch(status){ 265 case OI_STATUS_SUCCESS: 266 decoder_state->first_good_frame_found = 1; 267 268 if (state->mode == SBC_MODE_mSBC){ 269 decoder_state->search_new_sync_word = 1; 270 decoder_state->sync_word_found = 0; 271 } 272 273 state->handle_pcm_data(decoder_state->pcm_plc_data, 274 btstack_sbc_decoder_num_samples_per_frame(state), 275 btstack_sbc_decoder_num_channels(state), 276 btstack_sbc_decoder_sample_rate(state), state->context); 277 state->good_frames_nr++; 278 continue; 279 case OI_CODEC_SBC_NOT_ENOUGH_HEADER_DATA: 280 case OI_CODEC_SBC_NOT_ENOUGH_BODY_DATA: 281 // printf(" NOT_ENOUGH_DATA\n"); 282 if (decoder_state->sync_word_found){ 283 decoder_state->search_new_sync_word = 0; 284 } 285 break; 286 case OI_CODEC_SBC_NO_SYNCWORD: 287 case OI_CODEC_SBC_CHECKSUM_MISMATCH: 288 // printf("NO_SYNCWORD or CHECKSUM_MISMATCH\n"); 289 decoder_state->bytes_in_frame_buffer = 0; 290 if (!decoder_state->first_good_frame_found) break; 291 292 if (zero_seq_found){ 293 state->zero_frames_nr++; 294 } else { 295 state->bad_frames_nr++; 296 } 297 if (!plc_enabled) break; 298 break; 299 default: 300 log_info("Frame decode error: %d", status); 301 break; 302 } 303 304 memmove(decoder_state->frame_buffer, decoder_state->frame_buffer + bytes_processed, decoder_state->bytes_in_frame_buffer); 305 if ((status != OI_STATUS_SUCCESS) || (decoder_state->bytes_in_frame_buffer == 0)) break; 306 307 } 308 } 309 } 310 311 312 static void btstack_sbc_decoder_process_msbc_data(btstack_sbc_decoder_state_t * state, int packet_status_flag, uint8_t * buffer, int size){ 313 314 bludroid_decoder_state_t * decoder_state = (bludroid_decoder_state_t*)state->decoder_state; 315 int input_bytes_to_process = size; 316 unsigned int msbc_frame_size = 57; 317 318 // printf("<<-- enter -->>\n"); 319 // printf("Process data: in buffer %u, new %u\n", decoder_state->bytes_in_frame_buffer, size); 320 321 while (input_bytes_to_process > 0){ 322 323 // fill buffer with new data 324 int bytes_missing_for_complete_msbc_frame = msbc_frame_size - decoder_state->bytes_in_frame_buffer; 325 int bytes_to_append = btstack_min(input_bytes_to_process, bytes_missing_for_complete_msbc_frame); 326 if (bytes_to_append) { 327 append_received_sbc_data(decoder_state, buffer, bytes_to_append); 328 buffer += bytes_to_append; 329 input_bytes_to_process -= bytes_to_append; 330 } 331 332 if (decoder_state->bytes_in_frame_buffer < msbc_frame_size){ 333 // printf("not enough data %d > %d\n", msbc_frame_size, decoder_state->bytes_in_frame_buffer); 334 if (input_bytes_to_process){ 335 log_error("SHOULD NOT HAPPEN... not enough bytes, but bytes left to process"); 336 } 337 break; 338 } 339 340 uint16_t bytes_in_buffer_before = decoder_state->bytes_in_frame_buffer; 341 uint16_t bytes_processed = 0; 342 const OI_BYTE *frame_data = decoder_state->frame_buffer; 343 344 static int frame_count = 0; 345 if (corrupt_frame_period > 0){ 346 frame_count++; 347 348 if (frame_count % corrupt_frame_period == 0){ 349 *(uint8_t*)&frame_data[5] = 0; 350 frame_count = 0; 351 } 352 } 353 354 OI_STATUS status = OI_STATUS_SUCCESS; 355 int bad_frame = 0; 356 int zero_seq_found = 0; 357 358 if (decoder_state->first_good_frame_found){ 359 zero_seq_found = find_sequence_of_zeros(frame_data, decoder_state->bytes_in_frame_buffer, 20); 360 bad_frame = zero_seq_found || packet_status_flag; 361 } 362 363 if (bad_frame){ 364 status = OI_CODEC_SBC_CHECKSUM_MISMATCH; 365 decoder_state->bytes_in_frame_buffer = 0; 366 } else { 367 if (decoder_state->search_new_sync_word && !decoder_state->sync_word_found){ 368 int h2_syncword = find_h2_syncword(frame_data, decoder_state->bytes_in_frame_buffer); 369 370 if (h2_syncword != -1){ 371 decoder_state->sync_word_found = 1; 372 decoder_state->h2_sequence_nr = h2_syncword; 373 } 374 } 375 status = OI_CODEC_SBC_DecodeFrame(&(decoder_state->decoder_context), 376 &frame_data, 377 &(decoder_state->bytes_in_frame_buffer), 378 decoder_state->pcm_plc_data, 379 &(decoder_state->pcm_bytes)); 380 } 381 382 bytes_processed = bytes_in_buffer_before - decoder_state->bytes_in_frame_buffer; 383 OI_UINT32 bytes_in_frame_buffer = msbc_frame_size; 384 385 switch(status){ 386 case 0: 387 decoder_state->first_good_frame_found = 1; 388 389 if (state->mode == SBC_MODE_mSBC){ 390 decoder_state->search_new_sync_word = 1; 391 decoder_state->sync_word_found = 0; 392 } 393 394 btstack_sbc_plc_good_frame(&state->plc_state, decoder_state->pcm_plc_data, decoder_state->pcm_data); 395 state->handle_pcm_data(decoder_state->pcm_data, 396 btstack_sbc_decoder_num_samples_per_frame(state), 397 btstack_sbc_decoder_num_channels(state), 398 btstack_sbc_decoder_sample_rate(state), state->context); 399 state->good_frames_nr++; 400 continue; 401 case OI_CODEC_SBC_NOT_ENOUGH_HEADER_DATA: 402 case OI_CODEC_SBC_NOT_ENOUGH_BODY_DATA: 403 // printf(" NOT_ENOUGH_DATA\n"); 404 if (decoder_state->sync_word_found){ 405 decoder_state->search_new_sync_word = 0; 406 } 407 break; 408 case OI_CODEC_SBC_NO_SYNCWORD: 409 case OI_CODEC_SBC_CHECKSUM_MISMATCH: 410 // printf("NO_SYNCWORD or CHECKSUM_MISMATCH\n"); 411 decoder_state->bytes_in_frame_buffer = 0; 412 if (!decoder_state->first_good_frame_found) break; 413 414 if (state->mode == SBC_MODE_mSBC){ 415 if (!decoder_state->sync_word_found){ 416 decoder_state->h2_sequence_nr = (decoder_state->h2_sequence_nr + 1)%4; 417 } 418 decoder_state->search_new_sync_word = 1; 419 decoder_state->sync_word_found = 0; 420 } 421 422 if (zero_seq_found){ 423 state->zero_frames_nr++; 424 } else { 425 state->bad_frames_nr++; 426 } 427 428 #ifdef LOG_FRAME_STATUS 429 if (zero_seq_found){ 430 printf("%d : ZERO FRAME\n", decoder_state->h2_sequence_nr); 431 } else { 432 printf("%d : BAD FRAME\n", decoder_state->h2_sequence_nr); 433 } 434 if (decoder_state->h2_sequence_nr == 3) printf("\n"); 435 #endif 436 if (!plc_enabled) break; 437 438 frame_data = btstack_sbc_plc_zero_signal_frame(); 439 440 status = OI_CODEC_SBC_DecodeFrame(&(decoder_state->decoder_context), 441 &frame_data, 442 &bytes_in_frame_buffer, 443 decoder_state->pcm_plc_data, 444 &(decoder_state->pcm_bytes)); 445 446 if (status != 0) { 447 log_error("SBC decoder: error %d\n", status); 448 } 449 btstack_sbc_plc_bad_frame(&state->plc_state, decoder_state->pcm_plc_data, decoder_state->pcm_data); 450 state->handle_pcm_data(decoder_state->pcm_data, 451 btstack_sbc_decoder_num_samples_per_frame(state), 452 btstack_sbc_decoder_num_channels(state), 453 btstack_sbc_decoder_sample_rate(state), state->context); 454 455 456 break; 457 default: 458 log_info("Frame decode error: %d", status); 459 break; 460 } 461 462 memmove(decoder_state->frame_buffer, decoder_state->frame_buffer + bytes_processed, decoder_state->bytes_in_frame_buffer); 463 } 464 } 465 466 void btstack_sbc_decoder_process_data(btstack_sbc_decoder_state_t * state, int packet_status_flag, uint8_t * buffer, int size){ 467 if (state->mode == SBC_MODE_mSBC){ 468 btstack_sbc_decoder_process_msbc_data(state, packet_status_flag, buffer, size); 469 } else { 470 btstack_sbc_decoder_process_sbc_data(state, packet_status_flag, buffer, size); 471 } 472 } 473