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) ){ 1634e5badd2SMatthias Ringwald log_error("SBC decoder: different sbc decoder state already 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 2066535961aSMatthias Ringwald (void)memcpy(state->frame_buffer + state->bytes_in_frame_buffer, buffer, 2076535961aSMatthias Ringwald size); 208c37cd8f3SMatthias Ringwald state->bytes_in_frame_buffer += size; 209c37cd8f3SMatthias Ringwald } 210c37cd8f3SMatthias Ringwald 211*37732e0bSMilanka Ringwald static void btstack_sbc_decoder_bluedroid_simulate_error(const OI_BYTE *frame_data) { 2125c7e2c74SMatthias Ringwald static int frame_count = 0; 2135c7e2c74SMatthias Ringwald if (corrupt_frame_period > 0){ 2145c7e2c74SMatthias Ringwald frame_count++; 2155c7e2c74SMatthias Ringwald 2165c7e2c74SMatthias Ringwald if ((frame_count % corrupt_frame_period) == 0){ 2175c7e2c74SMatthias Ringwald *(uint8_t*)&frame_data[5] = 0; 2185c7e2c74SMatthias Ringwald frame_count = 0; 2195c7e2c74SMatthias Ringwald } 2205c7e2c74SMatthias Ringwald } 2215c7e2c74SMatthias Ringwald } 222c37cd8f3SMatthias Ringwald 2236276d22aSMilanka Ringwald static void btstack_sbc_decoder_process_sbc_data(btstack_sbc_decoder_state_t * state, uint8_t * buffer, int size){ 224c37cd8f3SMatthias Ringwald bludroid_decoder_state_t * decoder_state = (bludroid_decoder_state_t*)state->decoder_state; 225c37cd8f3SMatthias Ringwald int input_bytes_to_process = size; 226a305eb53SMilanka Ringwald int keep_decoding = 1; 227c37cd8f3SMatthias Ringwald 228a305eb53SMilanka Ringwald while (keep_decoding) { 229a305eb53SMilanka Ringwald // Fill decoder_state->frame_buffer as much as possible. 230a305eb53SMilanka Ringwald int bytes_free_in_frame_buffer = SBC_MAX_FRAME_LEN - decoder_state->bytes_in_frame_buffer; 231a305eb53SMilanka Ringwald int bytes_to_append = btstack_min(input_bytes_to_process, bytes_free_in_frame_buffer); 2327c5b4db4SMatthias Ringwald if (bytes_to_append){ 233c37cd8f3SMatthias Ringwald append_received_sbc_data(decoder_state, buffer, bytes_to_append); 234c37cd8f3SMatthias Ringwald buffer += bytes_to_append; 235c37cd8f3SMatthias Ringwald input_bytes_to_process -= bytes_to_append; 2367c5b4db4SMatthias Ringwald } 237c37cd8f3SMatthias Ringwald 238a305eb53SMilanka Ringwald // Decode the next frame in decoder_state->frame_buffer. 239a305eb53SMilanka Ringwald int bytes_in_frame_buffer_before_decoding = decoder_state->bytes_in_frame_buffer; 240c37cd8f3SMatthias Ringwald const OI_BYTE *frame_data = decoder_state->frame_buffer; 241a305eb53SMilanka Ringwald OI_UINT32 frame_data_len = decoder_state->bytes_in_frame_buffer; 242a305eb53SMilanka Ringwald OI_STATUS status = OI_CODEC_SBC_DecodeFrame(&(decoder_state->decoder_context), 243a305eb53SMilanka Ringwald &frame_data, 244a305eb53SMilanka Ringwald &frame_data_len, 245a305eb53SMilanka Ringwald decoder_state->pcm_plc_data, 246a305eb53SMilanka Ringwald &(decoder_state->pcm_bytes)); 247a305eb53SMilanka Ringwald uint16_t bytes_processed = bytes_in_frame_buffer_before_decoding - frame_data_len; 248c37cd8f3SMatthias Ringwald 24957f61fbeSMilanka Ringwald // testing only - corrupt frame periodically 2505c7e2c74SMatthias Ringwald btstack_sbc_decoder_bluedroid_simulate_error(frame_data); 251c37cd8f3SMatthias Ringwald 252a305eb53SMilanka Ringwald // Handle decoding result. 253c37cd8f3SMatthias Ringwald switch(status){ 254c37cd8f3SMatthias Ringwald case OI_STATUS_SUCCESS: 255a305eb53SMilanka Ringwald case OI_CODEC_SBC_PARTIAL_DECODE: 256c37cd8f3SMatthias Ringwald state->handle_pcm_data(decoder_state->pcm_plc_data, 257c37cd8f3SMatthias Ringwald btstack_sbc_decoder_num_samples_per_frame(state), 258c37cd8f3SMatthias Ringwald btstack_sbc_decoder_num_channels(state), 259c37cd8f3SMatthias Ringwald btstack_sbc_decoder_sample_rate(state), state->context); 260c37cd8f3SMatthias Ringwald state->good_frames_nr++; 261a305eb53SMilanka Ringwald break; 262a305eb53SMilanka Ringwald 263c37cd8f3SMatthias Ringwald case OI_CODEC_SBC_NOT_ENOUGH_HEADER_DATA: 264c37cd8f3SMatthias Ringwald case OI_CODEC_SBC_NOT_ENOUGH_BODY_DATA: 265a305eb53SMilanka Ringwald case OI_CODEC_SBC_NOT_ENOUGH_AUDIO_DATA: 266a305eb53SMilanka Ringwald if (input_bytes_to_process > 0){ 2674e5badd2SMatthias Ringwald // Should never occur: The SBC codec claims there is not enough bytes in the frame_buffer, 268a305eb53SMilanka Ringwald // but the frame_buffer was full. (The frame_buffer is always full before decoding when input_bytes_to_process > 0.) 269a305eb53SMilanka Ringwald // Clear frame_buffer. 270a305eb53SMilanka Ringwald log_info("SBC decode: frame_buffer too small for frame"); 271a305eb53SMilanka Ringwald bytes_processed = bytes_in_frame_buffer_before_decoding; 272c37cd8f3SMatthias Ringwald } else { 273a305eb53SMilanka Ringwald // Exit decode loop, because there is not enough data in frame_buffer to decode the next frame. 274a305eb53SMilanka Ringwald keep_decoding = 0; 275c37cd8f3SMatthias Ringwald } 276a305eb53SMilanka Ringwald break; 277a305eb53SMilanka Ringwald 278a305eb53SMilanka Ringwald case OI_CODEC_SBC_NO_SYNCWORD: 279a305eb53SMilanka Ringwald // This means the entire frame_buffer did not contain the syncword. 280a305eb53SMilanka Ringwald // Discard the frame_buffer contents. 281a305eb53SMilanka Ringwald log_info("SBC decode: no syncword found"); 282a305eb53SMilanka Ringwald bytes_processed = bytes_in_frame_buffer_before_decoding; 283a305eb53SMilanka Ringwald break; 284a305eb53SMilanka Ringwald 285a305eb53SMilanka Ringwald case OI_CODEC_SBC_CHECKSUM_MISMATCH: 286a305eb53SMilanka Ringwald // The next frame is somehow corrupt. 287a305eb53SMilanka Ringwald log_info("SBC decode: checksum error"); 288a305eb53SMilanka Ringwald // Did the codec consume any bytes? 289a305eb53SMilanka Ringwald if (bytes_processed > 0){ 290a305eb53SMilanka Ringwald // Good. Nothing to do. 291a305eb53SMilanka Ringwald } else { 292a305eb53SMilanka Ringwald // Skip the bogus frame by skipping the header. 293a305eb53SMilanka Ringwald bytes_processed = 1; 294a305eb53SMilanka Ringwald } 295a305eb53SMilanka Ringwald break; 296a305eb53SMilanka Ringwald 297a305eb53SMilanka Ringwald case OI_STATUS_INVALID_PARAMETERS: 298a305eb53SMilanka Ringwald // This caused by corrupt frames. 299a305eb53SMilanka Ringwald // The codec apparently does not recover from this. 300a305eb53SMilanka Ringwald // Re-initialize the codec. 301a305eb53SMilanka Ringwald log_info("SBC decode: invalid parameters: resetting codec"); 302a305eb53SMilanka 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){ 303a305eb53SMilanka Ringwald log_info("SBC decode: resetting codec failed"); 304a305eb53SMilanka Ringwald 305a305eb53SMilanka Ringwald } 306c37cd8f3SMatthias Ringwald break; 307c37cd8f3SMatthias Ringwald default: 308a305eb53SMilanka Ringwald // Anything else went wrong. 309a305eb53SMilanka Ringwald // Skip a few bytes and try again. 310a305eb53SMilanka Ringwald bytes_processed = 1; 311a305eb53SMilanka Ringwald log_info("SBC decode: unknown status %d", status); 312c37cd8f3SMatthias Ringwald break; 313c37cd8f3SMatthias Ringwald } 314c37cd8f3SMatthias Ringwald 315a305eb53SMilanka Ringwald // Remove decoded frame from decoder_state->frame_buffer. 316a305eb53SMilanka Ringwald if (bytes_processed > bytes_in_frame_buffer_before_decoding) { 317a305eb53SMilanka Ringwald bytes_processed = bytes_in_frame_buffer_before_decoding; 318c37cd8f3SMatthias Ringwald } 319a305eb53SMilanka Ringwald memmove(decoder_state->frame_buffer, decoder_state->frame_buffer + bytes_processed, bytes_in_frame_buffer_before_decoding - bytes_processed); 320a305eb53SMilanka Ringwald decoder_state->bytes_in_frame_buffer -= bytes_processed; 321c37cd8f3SMatthias Ringwald } 322c37cd8f3SMatthias Ringwald } 323c37cd8f3SMatthias Ringwald 324c37cd8f3SMatthias Ringwald 325*37732e0bSMilanka Ringwald static void btstack_sbc_decoder_insert_missing_frames(btstack_sbc_decoder_state_t *state) { 326c37cd8f3SMatthias Ringwald bludroid_decoder_state_t * decoder_state = (bludroid_decoder_state_t*)state->decoder_state; 3275c7e2c74SMatthias Ringwald const unsigned int MSBC_FRAME_SIZE = 60; 328c37cd8f3SMatthias Ringwald 329c1ab6cc1SMatthias Ringwald while (decoder_state->first_good_frame_found && (decoder_state->msbc_bad_bytes >= MSBC_FRAME_SIZE)){ 330ce1fc992SMatthias Ringwald 331ce1fc992SMatthias Ringwald decoder_state->msbc_bad_bytes -= MSBC_FRAME_SIZE; 332ce1fc992SMatthias Ringwald state->bad_frames_nr++; 333ce1fc992SMatthias Ringwald 334ce1fc992SMatthias Ringwald // prepare zero signal frame 335ce1fc992SMatthias Ringwald const OI_BYTE * frame_data = btstack_sbc_plc_zero_signal_frame(); 336ce1fc992SMatthias Ringwald OI_UINT32 bytes_in_frame_buffer = 57; 337ce1fc992SMatthias Ringwald 338ce1fc992SMatthias Ringwald // log_info("Trace bad frame generator, bad bytes %u", decoder_state->msbc_bad_bytes); 339ce1fc992SMatthias Ringwald OI_STATUS status = status = OI_CODEC_SBC_DecodeFrame(&(decoder_state->decoder_context), 340ce1fc992SMatthias Ringwald &frame_data, 341ce1fc992SMatthias Ringwald &bytes_in_frame_buffer, 342ce1fc992SMatthias Ringwald decoder_state->pcm_plc_data, 343ce1fc992SMatthias Ringwald &(decoder_state->pcm_bytes)); 344ce1fc992SMatthias Ringwald 345ce1fc992SMatthias Ringwald if (status) { 346ce1fc992SMatthias Ringwald log_error("SBC decoder for ZIR frame: error %d\n", status); 347ce1fc992SMatthias Ringwald } 348ce1fc992SMatthias Ringwald 349ce1fc992SMatthias Ringwald if (bytes_in_frame_buffer){ 350ce1fc992SMatthias Ringwald log_error("PLC: not all bytes of zero frame processed, left %u\n", bytes_in_frame_buffer); 351ce1fc992SMatthias Ringwald } 352ce1fc992SMatthias Ringwald 353ce1fc992SMatthias Ringwald if (plc_enabled) { 354ce1fc992SMatthias Ringwald btstack_sbc_plc_bad_frame(&state->plc_state, decoder_state->pcm_plc_data, decoder_state->pcm_data); 355ce1fc992SMatthias Ringwald } else { 3566535961aSMatthias Ringwald (void)memcpy(decoder_state->pcm_data, 3576535961aSMatthias Ringwald decoder_state->pcm_plc_data, 3586535961aSMatthias Ringwald decoder_state->pcm_bytes); 359ce1fc992SMatthias Ringwald } 360ce1fc992SMatthias Ringwald 361ce1fc992SMatthias Ringwald state->handle_pcm_data(decoder_state->pcm_data, 362ce1fc992SMatthias Ringwald btstack_sbc_decoder_num_samples_per_frame(state), 363ce1fc992SMatthias Ringwald btstack_sbc_decoder_num_channels(state), 364ce1fc992SMatthias Ringwald btstack_sbc_decoder_sample_rate(state), state->context); 365ce1fc992SMatthias Ringwald } 3665c7e2c74SMatthias Ringwald } 367ce1fc992SMatthias Ringwald 3685c7e2c74SMatthias Ringwald static void btstack_sbc_decoder_process_msbc_data(btstack_sbc_decoder_state_t * state, int packet_status_flag, uint8_t * buffer, int size){ 3695c7e2c74SMatthias Ringwald bludroid_decoder_state_t * decoder_state = (bludroid_decoder_state_t*)state->decoder_state; 3705c7e2c74SMatthias Ringwald int input_bytes_to_process = size; 3715c7e2c74SMatthias Ringwald const unsigned int MSBC_FRAME_SIZE = 60; 3725c7e2c74SMatthias Ringwald 3735c7e2c74SMatthias Ringwald while (input_bytes_to_process > 0){ 3745c7e2c74SMatthias Ringwald 3755c7e2c74SMatthias Ringwald // Use PLC to insert missing frames (after first sync found) 3765c7e2c74SMatthias Ringwald btstack_sbc_decoder_insert_missing_frames(state); 377ce1fc992SMatthias Ringwald 3787c5b4db4SMatthias Ringwald // fill buffer with new data 379ce1fc992SMatthias Ringwald int bytes_missing_for_complete_msbc_frame = MSBC_FRAME_SIZE - decoder_state->bytes_in_frame_buffer; 380c37cd8f3SMatthias Ringwald int bytes_to_append = btstack_min(input_bytes_to_process, bytes_missing_for_complete_msbc_frame); 3817c5b4db4SMatthias Ringwald if (bytes_to_append) { 382c37cd8f3SMatthias Ringwald append_received_sbc_data(decoder_state, buffer, bytes_to_append); 383c37cd8f3SMatthias Ringwald buffer += bytes_to_append; 384c37cd8f3SMatthias Ringwald input_bytes_to_process -= bytes_to_append; 3857c5b4db4SMatthias Ringwald } 386c37cd8f3SMatthias Ringwald 387ce1fc992SMatthias Ringwald // complete frame in buffer? 388ce1fc992SMatthias Ringwald if (decoder_state->bytes_in_frame_buffer < MSBC_FRAME_SIZE) break; 389c37cd8f3SMatthias Ringwald 390a305eb53SMilanka Ringwald uint16_t bytes_in_frame_buffer_before_decoding = decoder_state->bytes_in_frame_buffer; 391c37cd8f3SMatthias Ringwald uint16_t bytes_processed = 0; 392c37cd8f3SMatthias Ringwald const OI_BYTE *frame_data = decoder_state->frame_buffer; 393c37cd8f3SMatthias Ringwald 39457f61fbeSMilanka Ringwald // testing only - corrupt frame periodically 3955c7e2c74SMatthias Ringwald btstack_sbc_decoder_bluedroid_simulate_error(frame_data); 396c37cd8f3SMatthias Ringwald 397ce1fc992SMatthias Ringwald // assert frame looks like this: 01 x8 AD [rest of frame 56 bytes] 00 398cca2ddccSMatthias Ringwald int h2_syncword = 0; 399cca2ddccSMatthias Ringwald int h2_sync_pos = find_h2_sync(frame_data, decoder_state->bytes_in_frame_buffer, &h2_syncword); 400cca2ddccSMatthias Ringwald if (h2_sync_pos < 0){ 401cca2ddccSMatthias Ringwald // no sync found, discard all but last 2 bytes 402cca2ddccSMatthias Ringwald bytes_processed = decoder_state->bytes_in_frame_buffer - 2; 403cca2ddccSMatthias Ringwald memmove(decoder_state->frame_buffer, decoder_state->frame_buffer + bytes_processed, decoder_state->bytes_in_frame_buffer); 404cca2ddccSMatthias Ringwald decoder_state->bytes_in_frame_buffer -= bytes_processed; // == 2 405cca2ddccSMatthias Ringwald // don't try PLC without at least a single good frame 406cca2ddccSMatthias Ringwald if (decoder_state->first_good_frame_found){ 407cca2ddccSMatthias Ringwald decoder_state->msbc_bad_bytes += bytes_processed; 408cca2ddccSMatthias Ringwald } 409cca2ddccSMatthias Ringwald continue; 410cca2ddccSMatthias Ringwald } 411cca2ddccSMatthias Ringwald 412cca2ddccSMatthias Ringwald decoder_state->h2_sequence_nr = h2_syncword; 413cca2ddccSMatthias Ringwald 414cca2ddccSMatthias Ringwald // drop data before it 415cca2ddccSMatthias Ringwald bytes_processed = h2_sync_pos - 2; 416cca2ddccSMatthias Ringwald if (bytes_processed > 2){ 417cca2ddccSMatthias Ringwald memmove(decoder_state->frame_buffer, decoder_state->frame_buffer + bytes_processed, decoder_state->bytes_in_frame_buffer); 418cca2ddccSMatthias Ringwald decoder_state->bytes_in_frame_buffer -= bytes_processed; 419cca2ddccSMatthias Ringwald // don't try PLC without at least a single good frame 420cca2ddccSMatthias Ringwald if (decoder_state->first_good_frame_found){ 421cca2ddccSMatthias Ringwald decoder_state->msbc_bad_bytes += bytes_processed; 422cca2ddccSMatthias Ringwald } 423cca2ddccSMatthias Ringwald continue; 424cca2ddccSMatthias Ringwald } 425cca2ddccSMatthias Ringwald 426c37cd8f3SMatthias Ringwald int bad_frame = 0; 42757f61fbeSMilanka Ringwald int zero_seq_found = find_sequence_of_zeros(frame_data, decoder_state->bytes_in_frame_buffer, 20); 428c37cd8f3SMatthias Ringwald 42957f61fbeSMilanka Ringwald // after first valid frame, zero sequences count as bad frames 430c37cd8f3SMatthias Ringwald if (decoder_state->first_good_frame_found){ 431c37cd8f3SMatthias Ringwald bad_frame = zero_seq_found || packet_status_flag; 432c37cd8f3SMatthias Ringwald } 433c37cd8f3SMatthias Ringwald 434c37cd8f3SMatthias Ringwald if (bad_frame){ 43557f61fbeSMilanka Ringwald // stats 436c37cd8f3SMatthias Ringwald if (zero_seq_found){ 437c37cd8f3SMatthias Ringwald state->zero_frames_nr++; 438c37cd8f3SMatthias Ringwald } else { 439c37cd8f3SMatthias Ringwald state->bad_frames_nr++; 440c37cd8f3SMatthias Ringwald } 441c37cd8f3SMatthias Ringwald #ifdef LOG_FRAME_STATUS 442c37cd8f3SMatthias Ringwald if (zero_seq_found){ 443c37cd8f3SMatthias Ringwald printf("%d : ZERO FRAME\n", decoder_state->h2_sequence_nr); 444c37cd8f3SMatthias Ringwald } else { 445c37cd8f3SMatthias Ringwald printf("%d : BAD FRAME\n", decoder_state->h2_sequence_nr); 446c37cd8f3SMatthias Ringwald } 447c37cd8f3SMatthias Ringwald #endif 448cca2ddccSMatthias Ringwald // retry after dropoing 3 byte sync 449cca2ddccSMatthias Ringwald bytes_processed = 3; 45057f61fbeSMilanka Ringwald memmove(decoder_state->frame_buffer, decoder_state->frame_buffer + bytes_processed, decoder_state->bytes_in_frame_buffer); 45157f61fbeSMilanka Ringwald decoder_state->bytes_in_frame_buffer -= bytes_processed; 45257f61fbeSMilanka Ringwald decoder_state->msbc_bad_bytes += bytes_processed; 453ce1fc992SMatthias Ringwald // log_info("Trace bad frame"); 45457f61fbeSMilanka Ringwald continue; 45557f61fbeSMilanka Ringwald } 45657f61fbeSMilanka Ringwald 45757f61fbeSMilanka Ringwald // ready to decode frame 45857f61fbeSMilanka Ringwald OI_STATUS status = OI_CODEC_SBC_DecodeFrame(&(decoder_state->decoder_context), 459c37cd8f3SMatthias Ringwald &frame_data, 46057f61fbeSMilanka Ringwald &(decoder_state->bytes_in_frame_buffer), 461c37cd8f3SMatthias Ringwald decoder_state->pcm_plc_data, 462c37cd8f3SMatthias Ringwald &(decoder_state->pcm_bytes)); 463c37cd8f3SMatthias Ringwald 46457f61fbeSMilanka Ringwald bytes_processed = bytes_in_frame_buffer_before_decoding - decoder_state->bytes_in_frame_buffer; 465ce1fc992SMatthias 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); 46657f61fbeSMilanka Ringwald 46757f61fbeSMilanka Ringwald switch(status){ 46857f61fbeSMilanka Ringwald case 0: 469ce1fc992SMatthias Ringwald // synced 47057f61fbeSMilanka Ringwald decoder_state->first_good_frame_found = 1; 47157f61fbeSMilanka Ringwald 472ce1fc992SMatthias Ringwald // get rid of padding byte, not processed by SBC decoder 473ce1fc992SMatthias Ringwald decoder_state->bytes_in_frame_buffer = 0; 474ce1fc992SMatthias Ringwald 475ce1fc992SMatthias Ringwald // restart counting bad bytes 476ce1fc992SMatthias Ringwald decoder_state->msbc_bad_bytes = 0; 477ce1fc992SMatthias Ringwald 478ce1fc992SMatthias Ringwald // feed good frame into PLC history 47957f61fbeSMilanka Ringwald btstack_sbc_plc_good_frame(&state->plc_state, decoder_state->pcm_plc_data, decoder_state->pcm_data); 480ce1fc992SMatthias Ringwald 481ce1fc992SMatthias Ringwald // deliver PCM data 482c37cd8f3SMatthias Ringwald state->handle_pcm_data(decoder_state->pcm_data, 483c37cd8f3SMatthias Ringwald btstack_sbc_decoder_num_samples_per_frame(state), 484c37cd8f3SMatthias Ringwald btstack_sbc_decoder_num_channels(state), 485c37cd8f3SMatthias Ringwald btstack_sbc_decoder_sample_rate(state), state->context); 486c37cd8f3SMatthias Ringwald 487ce1fc992SMatthias Ringwald // stats 48857f61fbeSMilanka Ringwald state->good_frames_nr++; 48957f61fbeSMilanka Ringwald continue; 490c37cd8f3SMatthias Ringwald 49157f61fbeSMilanka Ringwald case OI_CODEC_SBC_CHECKSUM_MISMATCH: 49257f61fbeSMilanka Ringwald // The next frame is somehow corrupt. 4934e5badd2SMatthias Ringwald log_debug("OI_CODEC_SBC_CHECKSUM_MISMATCH"); 49457f61fbeSMilanka Ringwald // Did the codec consume any bytes? 49557f61fbeSMilanka Ringwald if (bytes_processed > 0){ 49657f61fbeSMilanka Ringwald // Good. Nothing to do. 49757f61fbeSMilanka Ringwald } else { 49857f61fbeSMilanka Ringwald // Skip the bogus frame by skipping the header. 49957f61fbeSMilanka Ringwald bytes_processed = 1; 50057f61fbeSMilanka Ringwald } 50157f61fbeSMilanka Ringwald break; 50257f61fbeSMilanka Ringwald 50304ec07bfSMilanka Ringwald case OI_STATUS_INVALID_PARAMETERS: 50404ec07bfSMilanka Ringwald // This caused by corrupt frames. 50504ec07bfSMilanka Ringwald // The codec apparently does not recover from this. 50604ec07bfSMilanka Ringwald // Re-initialize the codec. 50704ec07bfSMilanka Ringwald log_info("SBC decode: invalid parameters: resetting codec"); 508d6ec9a32SMilanka 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){ 50904ec07bfSMilanka Ringwald log_info("SBC decode: resetting codec failed"); 51004ec07bfSMilanka Ringwald } 51104ec07bfSMilanka Ringwald break; 512c37cd8f3SMatthias Ringwald default: 513c37cd8f3SMatthias Ringwald log_info("Frame decode error: %d", status); 514c37cd8f3SMatthias Ringwald break; 515c37cd8f3SMatthias Ringwald } 516c37cd8f3SMatthias Ringwald 517ce1fc992SMatthias Ringwald // on success, while loop was restarted, so all processed bytes have been "bad" 51857f61fbeSMilanka Ringwald decoder_state->msbc_bad_bytes += bytes_processed; 51957f61fbeSMilanka Ringwald 520ce1fc992SMatthias Ringwald // drop processed bytes from frame buffer 521c37cd8f3SMatthias Ringwald memmove(decoder_state->frame_buffer, decoder_state->frame_buffer + bytes_processed, decoder_state->bytes_in_frame_buffer); 522c37cd8f3SMatthias Ringwald } 523c37cd8f3SMatthias Ringwald } 524c37cd8f3SMatthias Ringwald 525c37cd8f3SMatthias Ringwald void btstack_sbc_decoder_process_data(btstack_sbc_decoder_state_t * state, int packet_status_flag, uint8_t * buffer, int size){ 526c37cd8f3SMatthias Ringwald if (state->mode == SBC_MODE_mSBC){ 527c37cd8f3SMatthias Ringwald btstack_sbc_decoder_process_msbc_data(state, packet_status_flag, buffer, size); 528c37cd8f3SMatthias Ringwald } else { 5296276d22aSMilanka Ringwald btstack_sbc_decoder_process_sbc_data(state, buffer, size); 530c37cd8f3SMatthias Ringwald } 531c37cd8f3SMatthias Ringwald } 532