1c37cd8f3SMatthias Ringwald /* 2c37cd8f3SMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 3c37cd8f3SMatthias Ringwald * 4c37cd8f3SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 5c37cd8f3SMatthias Ringwald * modification, are permitted provided that the following conditions 6c37cd8f3SMatthias Ringwald * are met: 7c37cd8f3SMatthias Ringwald * 8c37cd8f3SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 9c37cd8f3SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 10c37cd8f3SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 11c37cd8f3SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 12c37cd8f3SMatthias Ringwald * documentation and/or other materials provided with the distribution. 13c37cd8f3SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 14c37cd8f3SMatthias Ringwald * contributors may be used to endorse or promote products derived 15c37cd8f3SMatthias Ringwald * from this software without specific prior written permission. 16c37cd8f3SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 17c37cd8f3SMatthias Ringwald * personal benefit and not for any commercial purpose or for 18c37cd8f3SMatthias Ringwald * monetary gain. 19c37cd8f3SMatthias Ringwald * 20c37cd8f3SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21c37cd8f3SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22c37cd8f3SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23c37cd8f3SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24c37cd8f3SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25c37cd8f3SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26c37cd8f3SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27c37cd8f3SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28c37cd8f3SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29c37cd8f3SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30c37cd8f3SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31c37cd8f3SMatthias Ringwald * SUCH DAMAGE. 32c37cd8f3SMatthias Ringwald * 33c37cd8f3SMatthias Ringwald * Please inquire about commercial licensing options at 34c37cd8f3SMatthias Ringwald * [email protected] 35c37cd8f3SMatthias Ringwald * 36c37cd8f3SMatthias Ringwald */ 37c37cd8f3SMatthias Ringwald 38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "btstack_sbc_decoder_bluedroid.c" 39c37cd8f3SMatthias Ringwald 40c37cd8f3SMatthias Ringwald // ***************************************************************************** 41c37cd8f3SMatthias Ringwald // 42c37cd8f3SMatthias Ringwald // SBC decoder based on Bluedroid library 43c37cd8f3SMatthias Ringwald // 44c37cd8f3SMatthias Ringwald // ***************************************************************************** 45c37cd8f3SMatthias Ringwald 46c37cd8f3SMatthias Ringwald #include "btstack_config.h" 47c37cd8f3SMatthias Ringwald 48c37cd8f3SMatthias Ringwald #include <stdint.h> 49c37cd8f3SMatthias Ringwald #include <stdio.h> 50c37cd8f3SMatthias Ringwald #include <stdlib.h> 51c37cd8f3SMatthias Ringwald #include <string.h> 52c37cd8f3SMatthias Ringwald 53c37cd8f3SMatthias Ringwald #include "btstack_sbc.h" 54c37cd8f3SMatthias Ringwald #include "btstack_sbc_plc.h" 55c37cd8f3SMatthias Ringwald 56c37cd8f3SMatthias Ringwald #include "oi_codec_sbc.h" 57c37cd8f3SMatthias Ringwald #include "oi_assert.h" 58c37cd8f3SMatthias Ringwald #include "btstack.h" 59c37cd8f3SMatthias Ringwald 60c37cd8f3SMatthias Ringwald #define mSBC_SYNCWORD 0xad 61c37cd8f3SMatthias Ringwald #define SBC_SYNCWORD 0x9c 62c37cd8f3SMatthias Ringwald #define SBC_MAX_CHANNELS 2 63c37cd8f3SMatthias Ringwald // #define LOG_FRAME_STATUS 64c37cd8f3SMatthias Ringwald 65c37cd8f3SMatthias Ringwald #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) 66c37cd8f3SMatthias Ringwald 67c37cd8f3SMatthias Ringwald typedef struct { 68c37cd8f3SMatthias Ringwald OI_UINT32 bytes_in_frame_buffer; 69c37cd8f3SMatthias Ringwald OI_CODEC_SBC_DECODER_CONTEXT decoder_context; 70c37cd8f3SMatthias Ringwald 71c37cd8f3SMatthias Ringwald uint8_t frame_buffer[SBC_MAX_FRAME_LEN]; 72c37cd8f3SMatthias Ringwald int16_t pcm_plc_data[SBC_MAX_CHANNELS * SBC_MAX_BANDS * SBC_MAX_BLOCKS]; 73c37cd8f3SMatthias Ringwald int16_t pcm_data[SBC_MAX_CHANNELS * SBC_MAX_BANDS * SBC_MAX_BLOCKS]; 74c37cd8f3SMatthias Ringwald uint32_t pcm_bytes; 75c37cd8f3SMatthias Ringwald OI_UINT32 decoder_data[(DECODER_DATA_SIZE+3)/4]; 76c37cd8f3SMatthias Ringwald int first_good_frame_found; 7757f61fbeSMilanka Ringwald int h2_sequence_nr; 7857f61fbeSMilanka Ringwald uint16_t msbc_bad_bytes; 79c37cd8f3SMatthias Ringwald } bludroid_decoder_state_t; 80c37cd8f3SMatthias Ringwald 81c37cd8f3SMatthias Ringwald static btstack_sbc_decoder_state_t * sbc_decoder_state_singleton = NULL; 82c37cd8f3SMatthias Ringwald static bludroid_decoder_state_t bd_decoder_state; 83c37cd8f3SMatthias Ringwald 84c37cd8f3SMatthias Ringwald // Testing only - START 85c37cd8f3SMatthias Ringwald static int plc_enabled = 1; 86c37cd8f3SMatthias Ringwald static int corrupt_frame_period = -1; 87c37cd8f3SMatthias Ringwald // Testing - STOP 88c37cd8f3SMatthias Ringwald 899137e118SMatthias Ringwald void btstack_sbc_decoder_test_set_plc_enabled(int enabled){ 909137e118SMatthias Ringwald plc_enabled = enabled; 91c37cd8f3SMatthias Ringwald } 92c37cd8f3SMatthias Ringwald 93c37cd8f3SMatthias Ringwald void btstack_sbc_decoder_test_simulate_corrupt_frames(int period){ 94c37cd8f3SMatthias Ringwald corrupt_frame_period = period; 95c37cd8f3SMatthias Ringwald } 96c37cd8f3SMatthias Ringwald 97c37cd8f3SMatthias Ringwald static int find_sequence_of_zeros(const OI_BYTE *frame_data, OI_UINT32 frame_bytes, int seq_length){ 98c37cd8f3SMatthias Ringwald int zero_seq_count = 0; 99c37cd8f3SMatthias Ringwald unsigned int i; 100c37cd8f3SMatthias Ringwald for (i=0; i<frame_bytes; i++){ 101c37cd8f3SMatthias Ringwald if (frame_data[i] == 0) { 102c37cd8f3SMatthias Ringwald zero_seq_count++; 103c37cd8f3SMatthias Ringwald if (zero_seq_count >= seq_length) return zero_seq_count; 104c37cd8f3SMatthias Ringwald } else { 105c37cd8f3SMatthias Ringwald zero_seq_count = 0; 106c37cd8f3SMatthias Ringwald } 107c37cd8f3SMatthias Ringwald } 108c37cd8f3SMatthias Ringwald return 0; 109c37cd8f3SMatthias Ringwald } 110c37cd8f3SMatthias Ringwald 11157f61fbeSMilanka Ringwald // returns position of mSBC sync word 11257f61fbeSMilanka Ringwald static int find_h2_sync(const OI_BYTE *frame_data, OI_UINT32 frame_bytes, int * sync_word_nr){ 113c37cd8f3SMatthias Ringwald int syncword = mSBC_SYNCWORD; 114c37cd8f3SMatthias Ringwald uint8_t h2_first_byte = 0; 115c37cd8f3SMatthias Ringwald uint8_t h2_second_byte = 0; 116c37cd8f3SMatthias Ringwald 117f6f968ddSMatthias Ringwald unsigned int i; 118c37cd8f3SMatthias Ringwald for (i=0; i<frame_bytes; i++){ 119c37cd8f3SMatthias Ringwald if (frame_data[i] == syncword) { 12057f61fbeSMilanka Ringwald // check: first byte == 1 12157f61fbeSMilanka Ringwald if (h2_first_byte == 1) { 12257f61fbeSMilanka Ringwald // check lower nibble of second byte == 0x08 12357f61fbeSMilanka Ringwald uint8_t ln = h2_second_byte & 0x0F; 12457f61fbeSMilanka Ringwald if (ln == 8) { 12557f61fbeSMilanka Ringwald // check if bits 0+2 == bits 1+3 12657f61fbeSMilanka Ringwald uint8_t hn = h2_second_byte >> 4; 12757f61fbeSMilanka Ringwald if ( ((hn>>1) & 0x05) == (hn & 0x05) ) { 12857f61fbeSMilanka Ringwald *sync_word_nr = ((hn & 0x04) >> 1) | (hn & 0x01); 12957f61fbeSMilanka Ringwald return i; 13057f61fbeSMilanka Ringwald } 13157f61fbeSMilanka Ringwald } 13257f61fbeSMilanka Ringwald } 133c37cd8f3SMatthias Ringwald } 134c37cd8f3SMatthias Ringwald h2_first_byte = h2_second_byte; 135c37cd8f3SMatthias Ringwald h2_second_byte = frame_data[i]; 136c37cd8f3SMatthias Ringwald } 13757f61fbeSMilanka Ringwald return -1; 138c37cd8f3SMatthias Ringwald } 139c37cd8f3SMatthias Ringwald 140c37cd8f3SMatthias Ringwald int btstack_sbc_decoder_num_samples_per_frame(btstack_sbc_decoder_state_t * state){ 141c37cd8f3SMatthias Ringwald bludroid_decoder_state_t * decoder_state = (bludroid_decoder_state_t *) state->decoder_state; 142c37cd8f3SMatthias Ringwald return decoder_state->decoder_context.common.frameInfo.nrof_blocks * decoder_state->decoder_context.common.frameInfo.nrof_subbands; 143c37cd8f3SMatthias Ringwald } 144c37cd8f3SMatthias Ringwald 145c37cd8f3SMatthias Ringwald int btstack_sbc_decoder_num_channels(btstack_sbc_decoder_state_t * state){ 146c37cd8f3SMatthias Ringwald bludroid_decoder_state_t * decoder_state = (bludroid_decoder_state_t *) state->decoder_state; 147c37cd8f3SMatthias Ringwald return decoder_state->decoder_context.common.frameInfo.nrof_channels; 148c37cd8f3SMatthias Ringwald } 149c37cd8f3SMatthias Ringwald 150c37cd8f3SMatthias Ringwald int btstack_sbc_decoder_sample_rate(btstack_sbc_decoder_state_t * state){ 151c37cd8f3SMatthias Ringwald bludroid_decoder_state_t * decoder_state = (bludroid_decoder_state_t *) state->decoder_state; 152c37cd8f3SMatthias Ringwald return decoder_state->decoder_context.common.frameInfo.frequency; 153c37cd8f3SMatthias Ringwald } 154c37cd8f3SMatthias Ringwald 155c37cd8f3SMatthias Ringwald #ifdef OI_DEBUG 156c37cd8f3SMatthias Ringwald void OI_AssertFail(const char* file, int line, const char* reason){ 157c37cd8f3SMatthias Ringwald log_error("AssertFail file %s, line %d, reason %s", file, line, reason); 158c37cd8f3SMatthias Ringwald } 159c37cd8f3SMatthias Ringwald #endif 160c37cd8f3SMatthias Ringwald 161c37cd8f3SMatthias Ringwald 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){ 162c1ab6cc1SMatthias Ringwald if (sbc_decoder_state_singleton && (sbc_decoder_state_singleton != state) ){ 163c37cd8f3SMatthias Ringwald log_error("SBC decoder: different sbc decoder state is allready registered"); 164c37cd8f3SMatthias Ringwald } 165c37cd8f3SMatthias Ringwald OI_STATUS status = OI_STATUS_SUCCESS; 166c37cd8f3SMatthias Ringwald switch (mode){ 167c37cd8f3SMatthias Ringwald case SBC_MODE_STANDARD: 168c37cd8f3SMatthias Ringwald // note: we always request stereo output, even for mono input 169c37cd8f3SMatthias Ringwald status = OI_CODEC_SBC_DecoderReset(&(bd_decoder_state.decoder_context), bd_decoder_state.decoder_data, sizeof(bd_decoder_state.decoder_data), 2, 2, FALSE); 170c37cd8f3SMatthias Ringwald break; 171c37cd8f3SMatthias Ringwald case SBC_MODE_mSBC: 172c37cd8f3SMatthias Ringwald status = OI_CODEC_mSBC_DecoderReset(&(bd_decoder_state.decoder_context), bd_decoder_state.decoder_data, sizeof(bd_decoder_state.decoder_data)); 173c37cd8f3SMatthias Ringwald break; 174c37cd8f3SMatthias Ringwald default: 175c37cd8f3SMatthias Ringwald break; 176c37cd8f3SMatthias Ringwald } 177c37cd8f3SMatthias Ringwald 178c37cd8f3SMatthias Ringwald if (status != OI_STATUS_SUCCESS){ 179c37cd8f3SMatthias Ringwald log_error("SBC decoder: error during reset %d\n", status); 180c37cd8f3SMatthias Ringwald } 181c37cd8f3SMatthias Ringwald 182c37cd8f3SMatthias Ringwald sbc_decoder_state_singleton = state; 183c37cd8f3SMatthias Ringwald 184c37cd8f3SMatthias Ringwald bd_decoder_state.bytes_in_frame_buffer = 0; 185c37cd8f3SMatthias Ringwald bd_decoder_state.pcm_bytes = sizeof(bd_decoder_state.pcm_data); 186c37cd8f3SMatthias Ringwald bd_decoder_state.h2_sequence_nr = -1; 187c37cd8f3SMatthias Ringwald bd_decoder_state.first_good_frame_found = 0; 188c37cd8f3SMatthias Ringwald 189c37cd8f3SMatthias Ringwald memset(state, 0, sizeof(btstack_sbc_decoder_state_t)); 190c37cd8f3SMatthias Ringwald state->handle_pcm_data = callback; 191c37cd8f3SMatthias Ringwald state->mode = mode; 192c37cd8f3SMatthias Ringwald state->context = context; 193c37cd8f3SMatthias Ringwald state->decoder_state = &bd_decoder_state; 194c37cd8f3SMatthias Ringwald btstack_sbc_plc_init(&state->plc_state); 195c37cd8f3SMatthias Ringwald } 196c37cd8f3SMatthias Ringwald 197c37cd8f3SMatthias Ringwald static void append_received_sbc_data(bludroid_decoder_state_t * state, uint8_t * buffer, int size){ 198c37cd8f3SMatthias Ringwald int numFreeBytes = sizeof(state->frame_buffer) - state->bytes_in_frame_buffer; 199c37cd8f3SMatthias Ringwald 20057f61fbeSMilanka Ringwald // 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); 20157f61fbeSMilanka Ringwald 202c37cd8f3SMatthias Ringwald if (size > numFreeBytes){ 203c37cd8f3SMatthias Ringwald log_error("SBC data: more bytes read %u than free bytes in buffer %u", size, numFreeBytes); 204c37cd8f3SMatthias Ringwald } 205c37cd8f3SMatthias Ringwald 206*6535961aSMatthias Ringwald (void)memcpy(state->frame_buffer + state->bytes_in_frame_buffer, buffer, 207*6535961aSMatthias Ringwald size); 208c37cd8f3SMatthias Ringwald state->bytes_in_frame_buffer += size; 209c37cd8f3SMatthias Ringwald } 210c37cd8f3SMatthias Ringwald 211c37cd8f3SMatthias Ringwald 2126276d22aSMilanka Ringwald static void btstack_sbc_decoder_process_sbc_data(btstack_sbc_decoder_state_t * state, uint8_t * buffer, int size){ 213c37cd8f3SMatthias Ringwald bludroid_decoder_state_t * decoder_state = (bludroid_decoder_state_t*)state->decoder_state; 214c37cd8f3SMatthias Ringwald int input_bytes_to_process = size; 215a305eb53SMilanka Ringwald int keep_decoding = 1; 216c37cd8f3SMatthias Ringwald 217a305eb53SMilanka Ringwald while (keep_decoding) { 218a305eb53SMilanka Ringwald // Fill decoder_state->frame_buffer as much as possible. 219a305eb53SMilanka Ringwald int bytes_free_in_frame_buffer = SBC_MAX_FRAME_LEN - decoder_state->bytes_in_frame_buffer; 220a305eb53SMilanka Ringwald int bytes_to_append = btstack_min(input_bytes_to_process, bytes_free_in_frame_buffer); 2217c5b4db4SMatthias Ringwald if (bytes_to_append){ 222c37cd8f3SMatthias Ringwald append_received_sbc_data(decoder_state, buffer, bytes_to_append); 223c37cd8f3SMatthias Ringwald buffer += bytes_to_append; 224c37cd8f3SMatthias Ringwald input_bytes_to_process -= bytes_to_append; 2257c5b4db4SMatthias Ringwald } 226c37cd8f3SMatthias Ringwald 227a305eb53SMilanka Ringwald // Decode the next frame in decoder_state->frame_buffer. 228a305eb53SMilanka Ringwald int bytes_in_frame_buffer_before_decoding = decoder_state->bytes_in_frame_buffer; 229c37cd8f3SMatthias Ringwald const OI_BYTE *frame_data = decoder_state->frame_buffer; 230a305eb53SMilanka Ringwald OI_UINT32 frame_data_len = decoder_state->bytes_in_frame_buffer; 231a305eb53SMilanka Ringwald OI_STATUS status = OI_CODEC_SBC_DecodeFrame(&(decoder_state->decoder_context), 232a305eb53SMilanka Ringwald &frame_data, 233a305eb53SMilanka Ringwald &frame_data_len, 234a305eb53SMilanka Ringwald decoder_state->pcm_plc_data, 235a305eb53SMilanka Ringwald &(decoder_state->pcm_bytes)); 236a305eb53SMilanka Ringwald uint16_t bytes_processed = bytes_in_frame_buffer_before_decoding - frame_data_len; 237c37cd8f3SMatthias Ringwald 23857f61fbeSMilanka Ringwald // testing only - corrupt frame periodically 239c37cd8f3SMatthias Ringwald static int frame_count = 0; 240c37cd8f3SMatthias Ringwald if (corrupt_frame_period > 0){ 241c37cd8f3SMatthias Ringwald frame_count++; 242c37cd8f3SMatthias Ringwald 243c1ab6cc1SMatthias Ringwald if ((frame_count % corrupt_frame_period) == 0){ 244c37cd8f3SMatthias Ringwald *(uint8_t*)&frame_data[5] = 0; 245c37cd8f3SMatthias Ringwald frame_count = 0; 246c37cd8f3SMatthias Ringwald } 247c37cd8f3SMatthias Ringwald } 248c37cd8f3SMatthias Ringwald 249a305eb53SMilanka Ringwald // Handle decoding result. 250c37cd8f3SMatthias Ringwald switch(status){ 251c37cd8f3SMatthias Ringwald case OI_STATUS_SUCCESS: 252a305eb53SMilanka Ringwald case OI_CODEC_SBC_PARTIAL_DECODE: 253c37cd8f3SMatthias Ringwald state->handle_pcm_data(decoder_state->pcm_plc_data, 254c37cd8f3SMatthias Ringwald btstack_sbc_decoder_num_samples_per_frame(state), 255c37cd8f3SMatthias Ringwald btstack_sbc_decoder_num_channels(state), 256c37cd8f3SMatthias Ringwald btstack_sbc_decoder_sample_rate(state), state->context); 257c37cd8f3SMatthias Ringwald state->good_frames_nr++; 258a305eb53SMilanka Ringwald break; 259a305eb53SMilanka Ringwald 260c37cd8f3SMatthias Ringwald case OI_CODEC_SBC_NOT_ENOUGH_HEADER_DATA: 261c37cd8f3SMatthias Ringwald case OI_CODEC_SBC_NOT_ENOUGH_BODY_DATA: 262a305eb53SMilanka Ringwald case OI_CODEC_SBC_NOT_ENOUGH_AUDIO_DATA: 263a305eb53SMilanka Ringwald if (input_bytes_to_process > 0){ 264a305eb53SMilanka Ringwald // Should never occur: The SBC code claims there is not enough bytes in the frame_buffer, 265a305eb53SMilanka Ringwald // but the frame_buffer was full. (The frame_buffer is always full before decoding when input_bytes_to_process > 0.) 266a305eb53SMilanka Ringwald // Clear frame_buffer. 267a305eb53SMilanka Ringwald log_info("SBC decode: frame_buffer too small for frame"); 268a305eb53SMilanka Ringwald bytes_processed = bytes_in_frame_buffer_before_decoding; 269c37cd8f3SMatthias Ringwald } else { 270a305eb53SMilanka Ringwald // Exit decode loop, because there is not enough data in frame_buffer to decode the next frame. 271a305eb53SMilanka Ringwald keep_decoding = 0; 272c37cd8f3SMatthias Ringwald } 273a305eb53SMilanka Ringwald break; 274a305eb53SMilanka Ringwald 275a305eb53SMilanka Ringwald case OI_CODEC_SBC_NO_SYNCWORD: 276a305eb53SMilanka Ringwald // This means the entire frame_buffer did not contain the syncword. 277a305eb53SMilanka Ringwald // Discard the frame_buffer contents. 278a305eb53SMilanka Ringwald log_info("SBC decode: no syncword found"); 279a305eb53SMilanka Ringwald bytes_processed = bytes_in_frame_buffer_before_decoding; 280a305eb53SMilanka Ringwald break; 281a305eb53SMilanka Ringwald 282a305eb53SMilanka Ringwald case OI_CODEC_SBC_CHECKSUM_MISMATCH: 283a305eb53SMilanka Ringwald // The next frame is somehow corrupt. 284a305eb53SMilanka Ringwald log_info("SBC decode: checksum error"); 285a305eb53SMilanka Ringwald // Did the codec consume any bytes? 286a305eb53SMilanka Ringwald if (bytes_processed > 0){ 287a305eb53SMilanka Ringwald // Good. Nothing to do. 288a305eb53SMilanka Ringwald } else { 289a305eb53SMilanka Ringwald // Skip the bogus frame by skipping the header. 290a305eb53SMilanka Ringwald bytes_processed = 1; 291a305eb53SMilanka Ringwald } 292a305eb53SMilanka Ringwald break; 293a305eb53SMilanka Ringwald 294a305eb53SMilanka Ringwald case OI_STATUS_INVALID_PARAMETERS: 295a305eb53SMilanka Ringwald // This caused by corrupt frames. 296a305eb53SMilanka Ringwald // The codec apparently does not recover from this. 297a305eb53SMilanka Ringwald // Re-initialize the codec. 298a305eb53SMilanka Ringwald log_info("SBC decode: invalid parameters: resetting codec"); 299a305eb53SMilanka Ringwald 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){ 300a305eb53SMilanka Ringwald log_info("SBC decode: resetting codec failed"); 301a305eb53SMilanka Ringwald 302a305eb53SMilanka Ringwald } 303c37cd8f3SMatthias Ringwald break; 304c37cd8f3SMatthias Ringwald default: 305a305eb53SMilanka Ringwald // Anything else went wrong. 306a305eb53SMilanka Ringwald // Skip a few bytes and try again. 307a305eb53SMilanka Ringwald bytes_processed = 1; 308a305eb53SMilanka Ringwald log_info("SBC decode: unknown status %d", status); 309c37cd8f3SMatthias Ringwald break; 310c37cd8f3SMatthias Ringwald } 311c37cd8f3SMatthias Ringwald 312a305eb53SMilanka Ringwald // Remove decoded frame from decoder_state->frame_buffer. 313a305eb53SMilanka Ringwald if (bytes_processed > bytes_in_frame_buffer_before_decoding) { 314a305eb53SMilanka Ringwald bytes_processed = bytes_in_frame_buffer_before_decoding; 315c37cd8f3SMatthias Ringwald } 316a305eb53SMilanka Ringwald memmove(decoder_state->frame_buffer, decoder_state->frame_buffer + bytes_processed, bytes_in_frame_buffer_before_decoding - bytes_processed); 317a305eb53SMilanka Ringwald decoder_state->bytes_in_frame_buffer -= bytes_processed; 318c37cd8f3SMatthias Ringwald } 319c37cd8f3SMatthias Ringwald } 320c37cd8f3SMatthias Ringwald 321c37cd8f3SMatthias Ringwald 322c37cd8f3SMatthias Ringwald static void btstack_sbc_decoder_process_msbc_data(btstack_sbc_decoder_state_t * state, int packet_status_flag, uint8_t * buffer, int size){ 323c37cd8f3SMatthias Ringwald bludroid_decoder_state_t * decoder_state = (bludroid_decoder_state_t*)state->decoder_state; 324c37cd8f3SMatthias Ringwald int input_bytes_to_process = size; 325ce1fc992SMatthias Ringwald unsigned int MSBC_FRAME_SIZE = 60; 326c37cd8f3SMatthias Ringwald 327c37cd8f3SMatthias Ringwald while (input_bytes_to_process > 0){ 328c37cd8f3SMatthias Ringwald 329ce1fc992SMatthias Ringwald // Use PLC to insert missing frames (after first sync found) 330c1ab6cc1SMatthias Ringwald while (decoder_state->first_good_frame_found && (decoder_state->msbc_bad_bytes >= MSBC_FRAME_SIZE)){ 331ce1fc992SMatthias Ringwald 332ce1fc992SMatthias Ringwald decoder_state->msbc_bad_bytes -= MSBC_FRAME_SIZE; 333ce1fc992SMatthias Ringwald state->bad_frames_nr++; 334ce1fc992SMatthias Ringwald 335ce1fc992SMatthias Ringwald // prepare zero signal frame 336ce1fc992SMatthias Ringwald const OI_BYTE * frame_data = btstack_sbc_plc_zero_signal_frame(); 337ce1fc992SMatthias Ringwald OI_UINT32 bytes_in_frame_buffer = 57; 338ce1fc992SMatthias Ringwald 339ce1fc992SMatthias Ringwald // log_info("Trace bad frame generator, bad bytes %u", decoder_state->msbc_bad_bytes); 340ce1fc992SMatthias Ringwald OI_STATUS status = status = OI_CODEC_SBC_DecodeFrame(&(decoder_state->decoder_context), 341ce1fc992SMatthias Ringwald &frame_data, 342ce1fc992SMatthias Ringwald &bytes_in_frame_buffer, 343ce1fc992SMatthias Ringwald decoder_state->pcm_plc_data, 344ce1fc992SMatthias Ringwald &(decoder_state->pcm_bytes)); 345ce1fc992SMatthias Ringwald 346ce1fc992SMatthias Ringwald if (status) { 347ce1fc992SMatthias Ringwald log_error("SBC decoder for ZIR frame: error %d\n", status); 348ce1fc992SMatthias Ringwald } 349ce1fc992SMatthias Ringwald 350ce1fc992SMatthias Ringwald if (bytes_in_frame_buffer){ 351ce1fc992SMatthias Ringwald log_error("PLC: not all bytes of zero frame processed, left %u\n", bytes_in_frame_buffer); 352ce1fc992SMatthias Ringwald } 353ce1fc992SMatthias Ringwald 354ce1fc992SMatthias Ringwald if (plc_enabled) { 355ce1fc992SMatthias Ringwald btstack_sbc_plc_bad_frame(&state->plc_state, decoder_state->pcm_plc_data, decoder_state->pcm_data); 356ce1fc992SMatthias Ringwald } else { 357*6535961aSMatthias Ringwald (void)memcpy(decoder_state->pcm_data, 358*6535961aSMatthias Ringwald decoder_state->pcm_plc_data, 359*6535961aSMatthias Ringwald decoder_state->pcm_bytes); 360ce1fc992SMatthias Ringwald } 361ce1fc992SMatthias Ringwald 362ce1fc992SMatthias Ringwald state->handle_pcm_data(decoder_state->pcm_data, 363ce1fc992SMatthias Ringwald btstack_sbc_decoder_num_samples_per_frame(state), 364ce1fc992SMatthias Ringwald btstack_sbc_decoder_num_channels(state), 365ce1fc992SMatthias Ringwald btstack_sbc_decoder_sample_rate(state), state->context); 366ce1fc992SMatthias Ringwald } 367ce1fc992SMatthias Ringwald 368ce1fc992SMatthias Ringwald 3697c5b4db4SMatthias Ringwald // fill buffer with new data 370ce1fc992SMatthias Ringwald int bytes_missing_for_complete_msbc_frame = MSBC_FRAME_SIZE - decoder_state->bytes_in_frame_buffer; 371c37cd8f3SMatthias Ringwald int bytes_to_append = btstack_min(input_bytes_to_process, bytes_missing_for_complete_msbc_frame); 3727c5b4db4SMatthias Ringwald if (bytes_to_append) { 373c37cd8f3SMatthias Ringwald append_received_sbc_data(decoder_state, buffer, bytes_to_append); 374c37cd8f3SMatthias Ringwald buffer += bytes_to_append; 375c37cd8f3SMatthias Ringwald input_bytes_to_process -= bytes_to_append; 3767c5b4db4SMatthias Ringwald } 377c37cd8f3SMatthias Ringwald 378ce1fc992SMatthias Ringwald // complete frame in buffer? 379ce1fc992SMatthias Ringwald if (decoder_state->bytes_in_frame_buffer < MSBC_FRAME_SIZE) break; 380c37cd8f3SMatthias Ringwald 381a305eb53SMilanka Ringwald uint16_t bytes_in_frame_buffer_before_decoding = decoder_state->bytes_in_frame_buffer; 382c37cd8f3SMatthias Ringwald uint16_t bytes_processed = 0; 383c37cd8f3SMatthias Ringwald const OI_BYTE *frame_data = decoder_state->frame_buffer; 384c37cd8f3SMatthias Ringwald 38557f61fbeSMilanka Ringwald // testing only - corrupt frame periodically 386c37cd8f3SMatthias Ringwald static int frame_count = 0; 387c37cd8f3SMatthias Ringwald if (corrupt_frame_period > 0){ 388c37cd8f3SMatthias Ringwald frame_count++; 389c37cd8f3SMatthias Ringwald 390c1ab6cc1SMatthias Ringwald if ((frame_count % corrupt_frame_period) == 0){ 391c37cd8f3SMatthias Ringwald *(uint8_t*)&frame_data[5] = 0; 392c37cd8f3SMatthias Ringwald frame_count = 0; 393c37cd8f3SMatthias Ringwald } 394c37cd8f3SMatthias Ringwald } 395c37cd8f3SMatthias Ringwald 396ce1fc992SMatthias Ringwald // assert frame looks like this: 01 x8 AD [rest of frame 56 bytes] 00 397cca2ddccSMatthias Ringwald int h2_syncword = 0; 398cca2ddccSMatthias Ringwald int h2_sync_pos = find_h2_sync(frame_data, decoder_state->bytes_in_frame_buffer, &h2_syncword); 399cca2ddccSMatthias Ringwald if (h2_sync_pos < 0){ 400cca2ddccSMatthias Ringwald // no sync found, discard all but last 2 bytes 401cca2ddccSMatthias Ringwald bytes_processed = decoder_state->bytes_in_frame_buffer - 2; 402cca2ddccSMatthias Ringwald memmove(decoder_state->frame_buffer, decoder_state->frame_buffer + bytes_processed, decoder_state->bytes_in_frame_buffer); 403cca2ddccSMatthias Ringwald decoder_state->bytes_in_frame_buffer -= bytes_processed; // == 2 404cca2ddccSMatthias Ringwald // don't try PLC without at least a single good frame 405cca2ddccSMatthias Ringwald if (decoder_state->first_good_frame_found){ 406cca2ddccSMatthias Ringwald decoder_state->msbc_bad_bytes += bytes_processed; 407cca2ddccSMatthias Ringwald } 408cca2ddccSMatthias Ringwald continue; 409cca2ddccSMatthias Ringwald } 410cca2ddccSMatthias Ringwald 411cca2ddccSMatthias Ringwald decoder_state->h2_sequence_nr = h2_syncword; 412cca2ddccSMatthias Ringwald 413cca2ddccSMatthias Ringwald // drop data before it 414cca2ddccSMatthias Ringwald bytes_processed = h2_sync_pos - 2; 415cca2ddccSMatthias Ringwald if (bytes_processed > 2){ 416cca2ddccSMatthias Ringwald memmove(decoder_state->frame_buffer, decoder_state->frame_buffer + bytes_processed, decoder_state->bytes_in_frame_buffer); 417cca2ddccSMatthias Ringwald decoder_state->bytes_in_frame_buffer -= bytes_processed; 418cca2ddccSMatthias Ringwald // don't try PLC without at least a single good frame 419cca2ddccSMatthias Ringwald if (decoder_state->first_good_frame_found){ 420cca2ddccSMatthias Ringwald decoder_state->msbc_bad_bytes += bytes_processed; 421cca2ddccSMatthias Ringwald } 422cca2ddccSMatthias Ringwald continue; 423cca2ddccSMatthias Ringwald } 424cca2ddccSMatthias Ringwald 425c37cd8f3SMatthias Ringwald int bad_frame = 0; 42657f61fbeSMilanka Ringwald int zero_seq_found = find_sequence_of_zeros(frame_data, decoder_state->bytes_in_frame_buffer, 20); 427c37cd8f3SMatthias Ringwald 42857f61fbeSMilanka Ringwald // after first valid frame, zero sequences count as bad frames 429c37cd8f3SMatthias Ringwald if (decoder_state->first_good_frame_found){ 430c37cd8f3SMatthias Ringwald bad_frame = zero_seq_found || packet_status_flag; 431c37cd8f3SMatthias Ringwald } 432c37cd8f3SMatthias Ringwald 433c37cd8f3SMatthias Ringwald if (bad_frame){ 43457f61fbeSMilanka Ringwald // stats 435c37cd8f3SMatthias Ringwald if (zero_seq_found){ 436c37cd8f3SMatthias Ringwald state->zero_frames_nr++; 437c37cd8f3SMatthias Ringwald } else { 438c37cd8f3SMatthias Ringwald state->bad_frames_nr++; 439c37cd8f3SMatthias Ringwald } 440c37cd8f3SMatthias Ringwald #ifdef LOG_FRAME_STATUS 441c37cd8f3SMatthias Ringwald if (zero_seq_found){ 442c37cd8f3SMatthias Ringwald printf("%d : ZERO FRAME\n", decoder_state->h2_sequence_nr); 443c37cd8f3SMatthias Ringwald } else { 444c37cd8f3SMatthias Ringwald printf("%d : BAD FRAME\n", decoder_state->h2_sequence_nr); 445c37cd8f3SMatthias Ringwald } 446c37cd8f3SMatthias Ringwald #endif 447cca2ddccSMatthias Ringwald // retry after dropoing 3 byte sync 448cca2ddccSMatthias Ringwald bytes_processed = 3; 44957f61fbeSMilanka Ringwald memmove(decoder_state->frame_buffer, decoder_state->frame_buffer + bytes_processed, decoder_state->bytes_in_frame_buffer); 45057f61fbeSMilanka Ringwald decoder_state->bytes_in_frame_buffer -= bytes_processed; 45157f61fbeSMilanka Ringwald decoder_state->msbc_bad_bytes += bytes_processed; 452ce1fc992SMatthias Ringwald // log_info("Trace bad frame"); 45357f61fbeSMilanka Ringwald continue; 45457f61fbeSMilanka Ringwald } 45557f61fbeSMilanka Ringwald 45657f61fbeSMilanka Ringwald // ready to decode frame 45757f61fbeSMilanka Ringwald OI_STATUS status = OI_CODEC_SBC_DecodeFrame(&(decoder_state->decoder_context), 458c37cd8f3SMatthias Ringwald &frame_data, 45957f61fbeSMilanka Ringwald &(decoder_state->bytes_in_frame_buffer), 460c37cd8f3SMatthias Ringwald decoder_state->pcm_plc_data, 461c37cd8f3SMatthias Ringwald &(decoder_state->pcm_bytes)); 462c37cd8f3SMatthias Ringwald 46357f61fbeSMilanka Ringwald bytes_processed = bytes_in_frame_buffer_before_decoding - decoder_state->bytes_in_frame_buffer; 464ce1fc992SMatthias Ringwald // 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); 46557f61fbeSMilanka Ringwald 46657f61fbeSMilanka Ringwald switch(status){ 46757f61fbeSMilanka Ringwald case 0: 468ce1fc992SMatthias Ringwald // synced 46957f61fbeSMilanka Ringwald decoder_state->first_good_frame_found = 1; 47057f61fbeSMilanka Ringwald 471ce1fc992SMatthias Ringwald // get rid of padding byte, not processed by SBC decoder 472ce1fc992SMatthias Ringwald decoder_state->bytes_in_frame_buffer = 0; 473ce1fc992SMatthias Ringwald 474ce1fc992SMatthias Ringwald // restart counting bad bytes 475ce1fc992SMatthias Ringwald decoder_state->msbc_bad_bytes = 0; 476ce1fc992SMatthias Ringwald 477ce1fc992SMatthias Ringwald // feed good frame into PLC history 47857f61fbeSMilanka Ringwald btstack_sbc_plc_good_frame(&state->plc_state, decoder_state->pcm_plc_data, decoder_state->pcm_data); 479ce1fc992SMatthias Ringwald 480ce1fc992SMatthias Ringwald // deliver PCM data 481c37cd8f3SMatthias Ringwald state->handle_pcm_data(decoder_state->pcm_data, 482c37cd8f3SMatthias Ringwald btstack_sbc_decoder_num_samples_per_frame(state), 483c37cd8f3SMatthias Ringwald btstack_sbc_decoder_num_channels(state), 484c37cd8f3SMatthias Ringwald btstack_sbc_decoder_sample_rate(state), state->context); 485c37cd8f3SMatthias Ringwald 486ce1fc992SMatthias Ringwald // stats 48757f61fbeSMilanka Ringwald state->good_frames_nr++; 48857f61fbeSMilanka Ringwald continue; 489c37cd8f3SMatthias Ringwald 49057f61fbeSMilanka Ringwald case OI_CODEC_SBC_NOT_ENOUGH_HEADER_DATA: 491ce1fc992SMatthias Ringwald log_info("OI_CODEC_SBC_NOT_ENOUGH_HEADER_DATA"); 492c37cd8f3SMatthias Ringwald break; 49357f61fbeSMilanka Ringwald case OI_CODEC_SBC_NOT_ENOUGH_BODY_DATA: 494ce1fc992SMatthias Ringwald log_info("OI_CODEC_SBC_NOT_ENOUGH_BODY_DATA"); 49557f61fbeSMilanka Ringwald break; 49657f61fbeSMilanka Ringwald case OI_CODEC_SBC_NOT_ENOUGH_AUDIO_DATA: 497ce1fc992SMatthias Ringwald log_info("OI_CODEC_SBC_NOT_ENOUGH_AUDIO_DATA"); 49857f61fbeSMilanka Ringwald break; 49957f61fbeSMilanka Ringwald case OI_CODEC_SBC_NO_SYNCWORD: 500ce1fc992SMatthias Ringwald log_info("OI_CODEC_SBC_NO_SYNCWORD"); 50157f61fbeSMilanka Ringwald break; 50257f61fbeSMilanka Ringwald 50357f61fbeSMilanka Ringwald case OI_CODEC_SBC_CHECKSUM_MISMATCH: 50457f61fbeSMilanka Ringwald // The next frame is somehow corrupt. 50557f61fbeSMilanka Ringwald log_info("OI_CODEC_SBC_CHECKSUM_MISMATCH"); 50657f61fbeSMilanka Ringwald // Did the codec consume any bytes? 50757f61fbeSMilanka Ringwald if (bytes_processed > 0){ 50857f61fbeSMilanka Ringwald // Good. Nothing to do. 50957f61fbeSMilanka Ringwald } else { 51057f61fbeSMilanka Ringwald // Skip the bogus frame by skipping the header. 51157f61fbeSMilanka Ringwald bytes_processed = 1; 51257f61fbeSMilanka Ringwald } 51357f61fbeSMilanka Ringwald break; 51457f61fbeSMilanka Ringwald 51504ec07bfSMilanka Ringwald case OI_STATUS_INVALID_PARAMETERS: 51604ec07bfSMilanka Ringwald // This caused by corrupt frames. 51704ec07bfSMilanka Ringwald // The codec apparently does not recover from this. 51804ec07bfSMilanka Ringwald // Re-initialize the codec. 51904ec07bfSMilanka Ringwald log_info("SBC decode: invalid parameters: resetting codec"); 520d6ec9a32SMilanka Ringwald if (OI_CODEC_mSBC_DecoderReset(&(bd_decoder_state.decoder_context), bd_decoder_state.decoder_data, sizeof(bd_decoder_state.decoder_data)) != OI_STATUS_SUCCESS){ 52104ec07bfSMilanka Ringwald log_info("SBC decode: resetting codec failed"); 52204ec07bfSMilanka Ringwald } 52304ec07bfSMilanka Ringwald break; 524c37cd8f3SMatthias Ringwald default: 525c37cd8f3SMatthias Ringwald log_info("Frame decode error: %d", status); 526c37cd8f3SMatthias Ringwald break; 527c37cd8f3SMatthias Ringwald } 528c37cd8f3SMatthias Ringwald 529ce1fc992SMatthias Ringwald // on success, while loop was restarted, so all processed bytes have been "bad" 53057f61fbeSMilanka Ringwald decoder_state->msbc_bad_bytes += bytes_processed; 53157f61fbeSMilanka Ringwald 532ce1fc992SMatthias Ringwald // drop processed bytes from frame buffer 533c37cd8f3SMatthias Ringwald memmove(decoder_state->frame_buffer, decoder_state->frame_buffer + bytes_processed, decoder_state->bytes_in_frame_buffer); 534c37cd8f3SMatthias Ringwald } 535c37cd8f3SMatthias Ringwald } 536c37cd8f3SMatthias Ringwald 537c37cd8f3SMatthias Ringwald void btstack_sbc_decoder_process_data(btstack_sbc_decoder_state_t * state, int packet_status_flag, uint8_t * buffer, int size){ 538c37cd8f3SMatthias Ringwald if (state->mode == SBC_MODE_mSBC){ 539c37cd8f3SMatthias Ringwald btstack_sbc_decoder_process_msbc_data(state, packet_status_flag, buffer, size); 540c37cd8f3SMatthias Ringwald } else { 5416276d22aSMilanka Ringwald btstack_sbc_decoder_process_sbc_data(state, buffer, size); 542c37cd8f3SMatthias Ringwald } 543c37cd8f3SMatthias Ringwald } 544