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> 49bbc383feSMilanka Ringwald 50c37cd8f3SMatthias Ringwald #include <stdlib.h> 51c37cd8f3SMatthias Ringwald #include <string.h> 52c37cd8f3SMatthias Ringwald 53bbc383feSMilanka Ringwald #ifdef LOG_FRAME_STATUS 54bbc383feSMilanka Ringwald #include <stdio.h> 55bbc383feSMilanka Ringwald #endif 56bbc383feSMilanka Ringwald 57*7c76cd61SMatthias Ringwald #include "btstack_debug.h" 58*7c76cd61SMatthias Ringwald #include "btstack_util.h" 59c37cd8f3SMatthias Ringwald #include "btstack_sbc.h" 60c37cd8f3SMatthias Ringwald #include "btstack_sbc_plc.h" 61c37cd8f3SMatthias Ringwald 62c37cd8f3SMatthias Ringwald #include "oi_codec_sbc.h" 63c37cd8f3SMatthias Ringwald #include "oi_assert.h" 64c37cd8f3SMatthias Ringwald 65c37cd8f3SMatthias Ringwald #define mSBC_SYNCWORD 0xad 66c37cd8f3SMatthias Ringwald #define SBC_SYNCWORD 0x9c 67c37cd8f3SMatthias Ringwald #define SBC_MAX_CHANNELS 2 68c37cd8f3SMatthias Ringwald // #define LOG_FRAME_STATUS 69c37cd8f3SMatthias Ringwald 70c37cd8f3SMatthias 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) 71c37cd8f3SMatthias Ringwald 72c37cd8f3SMatthias Ringwald typedef struct { 73c37cd8f3SMatthias Ringwald OI_UINT32 bytes_in_frame_buffer; 74c37cd8f3SMatthias Ringwald OI_CODEC_SBC_DECODER_CONTEXT decoder_context; 75c37cd8f3SMatthias Ringwald 76c37cd8f3SMatthias Ringwald uint8_t frame_buffer[SBC_MAX_FRAME_LEN]; 77c37cd8f3SMatthias Ringwald int16_t pcm_plc_data[SBC_MAX_CHANNELS * SBC_MAX_BANDS * SBC_MAX_BLOCKS]; 78c37cd8f3SMatthias Ringwald int16_t pcm_data[SBC_MAX_CHANNELS * SBC_MAX_BANDS * SBC_MAX_BLOCKS]; 79c37cd8f3SMatthias Ringwald uint32_t pcm_bytes; 80c37cd8f3SMatthias Ringwald OI_UINT32 decoder_data[(DECODER_DATA_SIZE+3)/4]; 81c37cd8f3SMatthias Ringwald int first_good_frame_found; 8257f61fbeSMilanka Ringwald int h2_sequence_nr; 8357f61fbeSMilanka Ringwald uint16_t msbc_bad_bytes; 84c37cd8f3SMatthias Ringwald } bludroid_decoder_state_t; 85c37cd8f3SMatthias Ringwald 86c37cd8f3SMatthias Ringwald static btstack_sbc_decoder_state_t * sbc_decoder_state_singleton = NULL; 87c37cd8f3SMatthias Ringwald static bludroid_decoder_state_t bd_decoder_state; 88c37cd8f3SMatthias Ringwald 89c37cd8f3SMatthias Ringwald // Testing only - START 90c37cd8f3SMatthias Ringwald static int plc_enabled = 1; 91c37cd8f3SMatthias Ringwald static int corrupt_frame_period = -1; 92c37cd8f3SMatthias Ringwald // Testing - STOP 93c37cd8f3SMatthias Ringwald 949137e118SMatthias Ringwald void btstack_sbc_decoder_test_set_plc_enabled(int enabled){ 959137e118SMatthias Ringwald plc_enabled = enabled; 96c37cd8f3SMatthias Ringwald } 97c37cd8f3SMatthias Ringwald 98c37cd8f3SMatthias Ringwald void btstack_sbc_decoder_test_simulate_corrupt_frames(int period){ 99c37cd8f3SMatthias Ringwald corrupt_frame_period = period; 100c37cd8f3SMatthias Ringwald } 101c37cd8f3SMatthias Ringwald 102c37cd8f3SMatthias Ringwald static int find_sequence_of_zeros(const OI_BYTE *frame_data, OI_UINT32 frame_bytes, int seq_length){ 103c37cd8f3SMatthias Ringwald int zero_seq_count = 0; 104c37cd8f3SMatthias Ringwald unsigned int i; 105c37cd8f3SMatthias Ringwald for (i=0; i<frame_bytes; i++){ 106c37cd8f3SMatthias Ringwald if (frame_data[i] == 0) { 107c37cd8f3SMatthias Ringwald zero_seq_count++; 108c37cd8f3SMatthias Ringwald if (zero_seq_count >= seq_length) return zero_seq_count; 109c37cd8f3SMatthias Ringwald } else { 110c37cd8f3SMatthias Ringwald zero_seq_count = 0; 111c37cd8f3SMatthias Ringwald } 112c37cd8f3SMatthias Ringwald } 113c37cd8f3SMatthias Ringwald return 0; 114c37cd8f3SMatthias Ringwald } 115c37cd8f3SMatthias Ringwald 11657f61fbeSMilanka Ringwald // returns position of mSBC sync word 11757f61fbeSMilanka Ringwald static int find_h2_sync(const OI_BYTE *frame_data, OI_UINT32 frame_bytes, int * sync_word_nr){ 118c37cd8f3SMatthias Ringwald int syncword = mSBC_SYNCWORD; 119c37cd8f3SMatthias Ringwald uint8_t h2_first_byte = 0; 120c37cd8f3SMatthias Ringwald uint8_t h2_second_byte = 0; 121c37cd8f3SMatthias Ringwald 122f6f968ddSMatthias Ringwald unsigned int i; 123c37cd8f3SMatthias Ringwald for (i=0; i<frame_bytes; i++){ 124c37cd8f3SMatthias Ringwald if (frame_data[i] == syncword) { 12557f61fbeSMilanka Ringwald // check: first byte == 1 12657f61fbeSMilanka Ringwald if (h2_first_byte == 1) { 12757f61fbeSMilanka Ringwald // check lower nibble of second byte == 0x08 12857f61fbeSMilanka Ringwald uint8_t ln = h2_second_byte & 0x0F; 12957f61fbeSMilanka Ringwald if (ln == 8) { 13057f61fbeSMilanka Ringwald // check if bits 0+2 == bits 1+3 13157f61fbeSMilanka Ringwald uint8_t hn = h2_second_byte >> 4; 13257f61fbeSMilanka Ringwald if ( ((hn>>1) & 0x05) == (hn & 0x05) ) { 13357f61fbeSMilanka Ringwald *sync_word_nr = ((hn & 0x04) >> 1) | (hn & 0x01); 13457f61fbeSMilanka Ringwald return i; 13557f61fbeSMilanka Ringwald } 13657f61fbeSMilanka Ringwald } 13757f61fbeSMilanka Ringwald } 138c37cd8f3SMatthias Ringwald } 139c37cd8f3SMatthias Ringwald h2_first_byte = h2_second_byte; 140c37cd8f3SMatthias Ringwald h2_second_byte = frame_data[i]; 141c37cd8f3SMatthias Ringwald } 14257f61fbeSMilanka Ringwald return -1; 143c37cd8f3SMatthias Ringwald } 144c37cd8f3SMatthias Ringwald 145c37cd8f3SMatthias Ringwald int btstack_sbc_decoder_num_samples_per_frame(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_blocks * decoder_state->decoder_context.common.frameInfo.nrof_subbands; 148c37cd8f3SMatthias Ringwald } 149c37cd8f3SMatthias Ringwald 150c37cd8f3SMatthias Ringwald int btstack_sbc_decoder_num_channels(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.nrof_channels; 153c37cd8f3SMatthias Ringwald } 154c37cd8f3SMatthias Ringwald 155c37cd8f3SMatthias Ringwald int btstack_sbc_decoder_sample_rate(btstack_sbc_decoder_state_t * state){ 156c37cd8f3SMatthias Ringwald bludroid_decoder_state_t * decoder_state = (bludroid_decoder_state_t *) state->decoder_state; 157c37cd8f3SMatthias Ringwald return decoder_state->decoder_context.common.frameInfo.frequency; 158c37cd8f3SMatthias Ringwald } 159c37cd8f3SMatthias Ringwald 160c37cd8f3SMatthias Ringwald #ifdef OI_DEBUG 161c37cd8f3SMatthias Ringwald void OI_AssertFail(const char* file, int line, const char* reason){ 162c37cd8f3SMatthias Ringwald log_error("AssertFail file %s, line %d, reason %s", file, line, reason); 163c37cd8f3SMatthias Ringwald } 164c37cd8f3SMatthias Ringwald #endif 165c37cd8f3SMatthias Ringwald 166c37cd8f3SMatthias 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){ 167c1ab6cc1SMatthias Ringwald if (sbc_decoder_state_singleton && (sbc_decoder_state_singleton != state) ){ 1684e5badd2SMatthias Ringwald log_error("SBC decoder: different sbc decoder state already registered"); 169c37cd8f3SMatthias Ringwald } 170c37cd8f3SMatthias Ringwald OI_STATUS status = OI_STATUS_SUCCESS; 171c37cd8f3SMatthias Ringwald switch (mode){ 172c37cd8f3SMatthias Ringwald case SBC_MODE_STANDARD: 173c37cd8f3SMatthias Ringwald // note: we always request stereo output, even for mono input 174c37cd8f3SMatthias 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); 175c37cd8f3SMatthias Ringwald break; 176c37cd8f3SMatthias Ringwald case SBC_MODE_mSBC: 177c37cd8f3SMatthias Ringwald status = OI_CODEC_mSBC_DecoderReset(&(bd_decoder_state.decoder_context), bd_decoder_state.decoder_data, sizeof(bd_decoder_state.decoder_data)); 178c37cd8f3SMatthias Ringwald break; 179c37cd8f3SMatthias Ringwald default: 180c37cd8f3SMatthias Ringwald break; 181c37cd8f3SMatthias Ringwald } 182c37cd8f3SMatthias Ringwald 183c37cd8f3SMatthias Ringwald if (status != OI_STATUS_SUCCESS){ 184c37cd8f3SMatthias Ringwald log_error("SBC decoder: error during reset %d\n", status); 185c37cd8f3SMatthias Ringwald } 186c37cd8f3SMatthias Ringwald 187c37cd8f3SMatthias Ringwald sbc_decoder_state_singleton = state; 188c37cd8f3SMatthias Ringwald 189c37cd8f3SMatthias Ringwald bd_decoder_state.bytes_in_frame_buffer = 0; 190c37cd8f3SMatthias Ringwald bd_decoder_state.pcm_bytes = sizeof(bd_decoder_state.pcm_data); 191c37cd8f3SMatthias Ringwald bd_decoder_state.h2_sequence_nr = -1; 192c37cd8f3SMatthias Ringwald bd_decoder_state.first_good_frame_found = 0; 193c37cd8f3SMatthias Ringwald 194c37cd8f3SMatthias Ringwald memset(state, 0, sizeof(btstack_sbc_decoder_state_t)); 195c37cd8f3SMatthias Ringwald state->handle_pcm_data = callback; 196c37cd8f3SMatthias Ringwald state->mode = mode; 197c37cd8f3SMatthias Ringwald state->context = context; 198c37cd8f3SMatthias Ringwald state->decoder_state = &bd_decoder_state; 199c37cd8f3SMatthias Ringwald btstack_sbc_plc_init(&state->plc_state); 200c37cd8f3SMatthias Ringwald } 201c37cd8f3SMatthias Ringwald 202c37cd8f3SMatthias Ringwald static void append_received_sbc_data(bludroid_decoder_state_t * state, uint8_t * buffer, int size){ 203c37cd8f3SMatthias Ringwald int numFreeBytes = sizeof(state->frame_buffer) - state->bytes_in_frame_buffer; 204c37cd8f3SMatthias Ringwald 205c37cd8f3SMatthias Ringwald if (size > numFreeBytes){ 206c37cd8f3SMatthias Ringwald log_error("SBC data: more bytes read %u than free bytes in buffer %u", size, numFreeBytes); 207c37cd8f3SMatthias Ringwald } 208c37cd8f3SMatthias Ringwald 2096535961aSMatthias Ringwald (void)memcpy(state->frame_buffer + state->bytes_in_frame_buffer, buffer, 2106535961aSMatthias Ringwald size); 211c37cd8f3SMatthias Ringwald state->bytes_in_frame_buffer += size; 212c37cd8f3SMatthias Ringwald } 213c37cd8f3SMatthias Ringwald 21437732e0bSMilanka Ringwald static void btstack_sbc_decoder_bluedroid_simulate_error(const OI_BYTE *frame_data) { 2155c7e2c74SMatthias Ringwald static int frame_count = 0; 2165c7e2c74SMatthias Ringwald if (corrupt_frame_period > 0){ 2175c7e2c74SMatthias Ringwald frame_count++; 2185c7e2c74SMatthias Ringwald 2195c7e2c74SMatthias Ringwald if ((frame_count % corrupt_frame_period) == 0){ 2205c7e2c74SMatthias Ringwald *(uint8_t*)&frame_data[5] = 0; 2215c7e2c74SMatthias Ringwald frame_count = 0; 2225c7e2c74SMatthias Ringwald } 2235c7e2c74SMatthias Ringwald } 2245c7e2c74SMatthias Ringwald } 225c37cd8f3SMatthias Ringwald 2266276d22aSMilanka Ringwald static void btstack_sbc_decoder_process_sbc_data(btstack_sbc_decoder_state_t * state, uint8_t * buffer, int size){ 227c37cd8f3SMatthias Ringwald bludroid_decoder_state_t * decoder_state = (bludroid_decoder_state_t*)state->decoder_state; 228c37cd8f3SMatthias Ringwald int input_bytes_to_process = size; 229a305eb53SMilanka Ringwald int keep_decoding = 1; 230c37cd8f3SMatthias Ringwald 231a305eb53SMilanka Ringwald while (keep_decoding) { 232a305eb53SMilanka Ringwald // Fill decoder_state->frame_buffer as much as possible. 233a305eb53SMilanka Ringwald int bytes_free_in_frame_buffer = SBC_MAX_FRAME_LEN - decoder_state->bytes_in_frame_buffer; 234a305eb53SMilanka Ringwald int bytes_to_append = btstack_min(input_bytes_to_process, bytes_free_in_frame_buffer); 2357c5b4db4SMatthias Ringwald if (bytes_to_append){ 236c37cd8f3SMatthias Ringwald append_received_sbc_data(decoder_state, buffer, bytes_to_append); 237c37cd8f3SMatthias Ringwald buffer += bytes_to_append; 238c37cd8f3SMatthias Ringwald input_bytes_to_process -= bytes_to_append; 2397c5b4db4SMatthias Ringwald } 240c37cd8f3SMatthias Ringwald 241a305eb53SMilanka Ringwald // Decode the next frame in decoder_state->frame_buffer. 242a305eb53SMilanka Ringwald int bytes_in_frame_buffer_before_decoding = decoder_state->bytes_in_frame_buffer; 243c37cd8f3SMatthias Ringwald const OI_BYTE *frame_data = decoder_state->frame_buffer; 244a305eb53SMilanka Ringwald OI_UINT32 frame_data_len = decoder_state->bytes_in_frame_buffer; 245a305eb53SMilanka Ringwald OI_STATUS status = OI_CODEC_SBC_DecodeFrame(&(decoder_state->decoder_context), 246a305eb53SMilanka Ringwald &frame_data, 247a305eb53SMilanka Ringwald &frame_data_len, 248a305eb53SMilanka Ringwald decoder_state->pcm_plc_data, 249a305eb53SMilanka Ringwald &(decoder_state->pcm_bytes)); 250a305eb53SMilanka Ringwald uint16_t bytes_processed = bytes_in_frame_buffer_before_decoding - frame_data_len; 251c37cd8f3SMatthias Ringwald 25257f61fbeSMilanka Ringwald // testing only - corrupt frame periodically 2535c7e2c74SMatthias Ringwald btstack_sbc_decoder_bluedroid_simulate_error(frame_data); 254c37cd8f3SMatthias Ringwald 255a305eb53SMilanka Ringwald // Handle decoding result. 256c37cd8f3SMatthias Ringwald switch(status){ 257c37cd8f3SMatthias Ringwald case OI_STATUS_SUCCESS: 258a305eb53SMilanka Ringwald case OI_CODEC_SBC_PARTIAL_DECODE: 259c37cd8f3SMatthias Ringwald state->handle_pcm_data(decoder_state->pcm_plc_data, 260c37cd8f3SMatthias Ringwald btstack_sbc_decoder_num_samples_per_frame(state), 261c37cd8f3SMatthias Ringwald btstack_sbc_decoder_num_channels(state), 262c37cd8f3SMatthias Ringwald btstack_sbc_decoder_sample_rate(state), state->context); 263c37cd8f3SMatthias Ringwald state->good_frames_nr++; 264a305eb53SMilanka Ringwald break; 265a305eb53SMilanka Ringwald 266c37cd8f3SMatthias Ringwald case OI_CODEC_SBC_NOT_ENOUGH_HEADER_DATA: 267c37cd8f3SMatthias Ringwald case OI_CODEC_SBC_NOT_ENOUGH_BODY_DATA: 268a305eb53SMilanka Ringwald case OI_CODEC_SBC_NOT_ENOUGH_AUDIO_DATA: 269a305eb53SMilanka Ringwald if (input_bytes_to_process > 0){ 2704e5badd2SMatthias Ringwald // Should never occur: The SBC codec claims there is not enough bytes in the frame_buffer, 271a305eb53SMilanka Ringwald // but the frame_buffer was full. (The frame_buffer is always full before decoding when input_bytes_to_process > 0.) 272a305eb53SMilanka Ringwald // Clear frame_buffer. 273a305eb53SMilanka Ringwald log_info("SBC decode: frame_buffer too small for frame"); 274a305eb53SMilanka Ringwald bytes_processed = bytes_in_frame_buffer_before_decoding; 275c37cd8f3SMatthias Ringwald } else { 276a305eb53SMilanka Ringwald // Exit decode loop, because there is not enough data in frame_buffer to decode the next frame. 277a305eb53SMilanka Ringwald keep_decoding = 0; 278c37cd8f3SMatthias Ringwald } 279a305eb53SMilanka Ringwald break; 280a305eb53SMilanka Ringwald 281a305eb53SMilanka Ringwald case OI_CODEC_SBC_NO_SYNCWORD: 282a305eb53SMilanka Ringwald // This means the entire frame_buffer did not contain the syncword. 283a305eb53SMilanka Ringwald // Discard the frame_buffer contents. 284a305eb53SMilanka Ringwald log_info("SBC decode: no syncword found"); 285a305eb53SMilanka Ringwald bytes_processed = bytes_in_frame_buffer_before_decoding; 286a305eb53SMilanka Ringwald break; 287a305eb53SMilanka Ringwald 288a305eb53SMilanka Ringwald case OI_CODEC_SBC_CHECKSUM_MISMATCH: 289a305eb53SMilanka Ringwald // The next frame is somehow corrupt. 290a305eb53SMilanka Ringwald log_info("SBC decode: checksum error"); 291a305eb53SMilanka Ringwald // Did the codec consume any bytes? 292a305eb53SMilanka Ringwald if (bytes_processed > 0){ 293a305eb53SMilanka Ringwald // Good. Nothing to do. 294a305eb53SMilanka Ringwald } else { 295a305eb53SMilanka Ringwald // Skip the bogus frame by skipping the header. 296a305eb53SMilanka Ringwald bytes_processed = 1; 297a305eb53SMilanka Ringwald } 298a305eb53SMilanka Ringwald break; 299a305eb53SMilanka Ringwald 300a305eb53SMilanka Ringwald case OI_STATUS_INVALID_PARAMETERS: 301a305eb53SMilanka Ringwald // This caused by corrupt frames. 302a305eb53SMilanka Ringwald // The codec apparently does not recover from this. 303a305eb53SMilanka Ringwald // Re-initialize the codec. 304a305eb53SMilanka Ringwald log_info("SBC decode: invalid parameters: resetting codec"); 305a305eb53SMilanka 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){ 306a305eb53SMilanka Ringwald log_info("SBC decode: resetting codec failed"); 307a305eb53SMilanka Ringwald 308a305eb53SMilanka Ringwald } 309c37cd8f3SMatthias Ringwald break; 310c37cd8f3SMatthias Ringwald default: 311a305eb53SMilanka Ringwald // Anything else went wrong. 312a305eb53SMilanka Ringwald // Skip a few bytes and try again. 313a305eb53SMilanka Ringwald bytes_processed = 1; 314a305eb53SMilanka Ringwald log_info("SBC decode: unknown status %d", status); 315c37cd8f3SMatthias Ringwald break; 316c37cd8f3SMatthias Ringwald } 317c37cd8f3SMatthias Ringwald 318a305eb53SMilanka Ringwald // Remove decoded frame from decoder_state->frame_buffer. 319a305eb53SMilanka Ringwald if (bytes_processed > bytes_in_frame_buffer_before_decoding) { 320a305eb53SMilanka Ringwald bytes_processed = bytes_in_frame_buffer_before_decoding; 321c37cd8f3SMatthias Ringwald } 322a305eb53SMilanka Ringwald memmove(decoder_state->frame_buffer, decoder_state->frame_buffer + bytes_processed, bytes_in_frame_buffer_before_decoding - bytes_processed); 323a305eb53SMilanka Ringwald decoder_state->bytes_in_frame_buffer -= bytes_processed; 324c37cd8f3SMatthias Ringwald } 325c37cd8f3SMatthias Ringwald } 326c37cd8f3SMatthias Ringwald 327c37cd8f3SMatthias Ringwald 32837732e0bSMilanka Ringwald static void btstack_sbc_decoder_insert_missing_frames(btstack_sbc_decoder_state_t *state) { 329c37cd8f3SMatthias Ringwald bludroid_decoder_state_t * decoder_state = (bludroid_decoder_state_t*)state->decoder_state; 3305c7e2c74SMatthias Ringwald const unsigned int MSBC_FRAME_SIZE = 60; 331c37cd8f3SMatthias Ringwald 332c1ab6cc1SMatthias Ringwald while (decoder_state->first_good_frame_found && (decoder_state->msbc_bad_bytes >= MSBC_FRAME_SIZE)){ 333ce1fc992SMatthias Ringwald 334ce1fc992SMatthias Ringwald decoder_state->msbc_bad_bytes -= MSBC_FRAME_SIZE; 335ce1fc992SMatthias Ringwald state->bad_frames_nr++; 336ce1fc992SMatthias Ringwald 337ce1fc992SMatthias Ringwald // prepare zero signal frame 338ce1fc992SMatthias Ringwald const OI_BYTE * frame_data = btstack_sbc_plc_zero_signal_frame(); 339ce1fc992SMatthias Ringwald OI_UINT32 bytes_in_frame_buffer = 57; 340ce1fc992SMatthias Ringwald 341ce1fc992SMatthias Ringwald // log_info("Trace bad frame generator, bad bytes %u", decoder_state->msbc_bad_bytes); 342ce1fc992SMatthias Ringwald OI_STATUS status = status = OI_CODEC_SBC_DecodeFrame(&(decoder_state->decoder_context), 343ce1fc992SMatthias Ringwald &frame_data, 344ce1fc992SMatthias Ringwald &bytes_in_frame_buffer, 345ce1fc992SMatthias Ringwald decoder_state->pcm_plc_data, 346ce1fc992SMatthias Ringwald &(decoder_state->pcm_bytes)); 347ce1fc992SMatthias Ringwald 348ce1fc992SMatthias Ringwald if (status) { 349ce1fc992SMatthias Ringwald log_error("SBC decoder for ZIR frame: error %d\n", status); 350ce1fc992SMatthias Ringwald } 351ce1fc992SMatthias Ringwald 352ce1fc992SMatthias Ringwald if (bytes_in_frame_buffer){ 35397d2cfbcSMatthias Ringwald log_error("PLC: not all bytes of zero frame processed, left %u\n", (unsigned int) bytes_in_frame_buffer); 354ce1fc992SMatthias Ringwald } 355ce1fc992SMatthias Ringwald 356ce1fc992SMatthias Ringwald if (plc_enabled) { 357ce1fc992SMatthias Ringwald btstack_sbc_plc_bad_frame(&state->plc_state, decoder_state->pcm_plc_data, decoder_state->pcm_data); 358ce1fc992SMatthias Ringwald } else { 3596535961aSMatthias Ringwald (void)memcpy(decoder_state->pcm_data, 3606535961aSMatthias Ringwald decoder_state->pcm_plc_data, 3616535961aSMatthias Ringwald decoder_state->pcm_bytes); 362ce1fc992SMatthias Ringwald } 363ce1fc992SMatthias Ringwald 364ce1fc992SMatthias Ringwald state->handle_pcm_data(decoder_state->pcm_data, 365ce1fc992SMatthias Ringwald btstack_sbc_decoder_num_samples_per_frame(state), 366ce1fc992SMatthias Ringwald btstack_sbc_decoder_num_channels(state), 367ce1fc992SMatthias Ringwald btstack_sbc_decoder_sample_rate(state), state->context); 368ce1fc992SMatthias Ringwald } 3695c7e2c74SMatthias Ringwald } 370ce1fc992SMatthias Ringwald 3715c7e2c74SMatthias Ringwald static void btstack_sbc_decoder_process_msbc_data(btstack_sbc_decoder_state_t * state, int packet_status_flag, uint8_t * buffer, int size){ 3725c7e2c74SMatthias Ringwald bludroid_decoder_state_t * decoder_state = (bludroid_decoder_state_t*)state->decoder_state; 3735c7e2c74SMatthias Ringwald int input_bytes_to_process = size; 3745c7e2c74SMatthias Ringwald const unsigned int MSBC_FRAME_SIZE = 60; 3755c7e2c74SMatthias Ringwald 3765c7e2c74SMatthias Ringwald while (input_bytes_to_process > 0){ 3775c7e2c74SMatthias Ringwald 3785c7e2c74SMatthias Ringwald // Use PLC to insert missing frames (after first sync found) 3795c7e2c74SMatthias Ringwald btstack_sbc_decoder_insert_missing_frames(state); 380ce1fc992SMatthias Ringwald 3817c5b4db4SMatthias Ringwald // fill buffer with new data 382ce1fc992SMatthias Ringwald int bytes_missing_for_complete_msbc_frame = MSBC_FRAME_SIZE - decoder_state->bytes_in_frame_buffer; 383c37cd8f3SMatthias Ringwald int bytes_to_append = btstack_min(input_bytes_to_process, bytes_missing_for_complete_msbc_frame); 3847c5b4db4SMatthias Ringwald if (bytes_to_append) { 385c37cd8f3SMatthias Ringwald append_received_sbc_data(decoder_state, buffer, bytes_to_append); 386c37cd8f3SMatthias Ringwald buffer += bytes_to_append; 387c37cd8f3SMatthias Ringwald input_bytes_to_process -= bytes_to_append; 3887c5b4db4SMatthias Ringwald } 389c37cd8f3SMatthias Ringwald 390ce1fc992SMatthias Ringwald // complete frame in buffer? 391ce1fc992SMatthias Ringwald if (decoder_state->bytes_in_frame_buffer < MSBC_FRAME_SIZE) break; 392c37cd8f3SMatthias Ringwald 393a305eb53SMilanka Ringwald uint16_t bytes_in_frame_buffer_before_decoding = decoder_state->bytes_in_frame_buffer; 394c37cd8f3SMatthias Ringwald uint16_t bytes_processed = 0; 395c37cd8f3SMatthias Ringwald const OI_BYTE *frame_data = decoder_state->frame_buffer; 396c37cd8f3SMatthias Ringwald 39757f61fbeSMilanka Ringwald // testing only - corrupt frame periodically 3985c7e2c74SMatthias Ringwald btstack_sbc_decoder_bluedroid_simulate_error(frame_data); 399c37cd8f3SMatthias Ringwald 400ce1fc992SMatthias Ringwald // assert frame looks like this: 01 x8 AD [rest of frame 56 bytes] 00 401cca2ddccSMatthias Ringwald int h2_syncword = 0; 402cca2ddccSMatthias Ringwald int h2_sync_pos = find_h2_sync(frame_data, decoder_state->bytes_in_frame_buffer, &h2_syncword); 403cca2ddccSMatthias Ringwald if (h2_sync_pos < 0){ 404cca2ddccSMatthias Ringwald // no sync found, discard all but last 2 bytes 405cca2ddccSMatthias Ringwald bytes_processed = decoder_state->bytes_in_frame_buffer - 2; 406cca2ddccSMatthias Ringwald memmove(decoder_state->frame_buffer, decoder_state->frame_buffer + bytes_processed, decoder_state->bytes_in_frame_buffer); 407cca2ddccSMatthias Ringwald decoder_state->bytes_in_frame_buffer -= bytes_processed; // == 2 408cca2ddccSMatthias Ringwald // don't try PLC without at least a single good frame 409cca2ddccSMatthias Ringwald if (decoder_state->first_good_frame_found){ 410cca2ddccSMatthias Ringwald decoder_state->msbc_bad_bytes += bytes_processed; 411cca2ddccSMatthias Ringwald } 412cca2ddccSMatthias Ringwald continue; 413cca2ddccSMatthias Ringwald } 414cca2ddccSMatthias Ringwald 415cca2ddccSMatthias Ringwald decoder_state->h2_sequence_nr = h2_syncword; 416cca2ddccSMatthias Ringwald 417cca2ddccSMatthias Ringwald // drop data before it 418cca2ddccSMatthias Ringwald bytes_processed = h2_sync_pos - 2; 419cca2ddccSMatthias Ringwald if (bytes_processed > 2){ 420cca2ddccSMatthias Ringwald memmove(decoder_state->frame_buffer, decoder_state->frame_buffer + bytes_processed, decoder_state->bytes_in_frame_buffer); 421cca2ddccSMatthias Ringwald decoder_state->bytes_in_frame_buffer -= bytes_processed; 422cca2ddccSMatthias Ringwald // don't try PLC without at least a single good frame 423cca2ddccSMatthias Ringwald if (decoder_state->first_good_frame_found){ 424cca2ddccSMatthias Ringwald decoder_state->msbc_bad_bytes += bytes_processed; 425cca2ddccSMatthias Ringwald } 426cca2ddccSMatthias Ringwald continue; 427cca2ddccSMatthias Ringwald } 428cca2ddccSMatthias Ringwald 429c37cd8f3SMatthias Ringwald int bad_frame = 0; 43057f61fbeSMilanka Ringwald int zero_seq_found = find_sequence_of_zeros(frame_data, decoder_state->bytes_in_frame_buffer, 20); 431c37cd8f3SMatthias Ringwald 43257f61fbeSMilanka Ringwald // after first valid frame, zero sequences count as bad frames 433c37cd8f3SMatthias Ringwald if (decoder_state->first_good_frame_found){ 434c37cd8f3SMatthias Ringwald bad_frame = zero_seq_found || packet_status_flag; 435c37cd8f3SMatthias Ringwald } 436c37cd8f3SMatthias Ringwald 437c37cd8f3SMatthias Ringwald if (bad_frame){ 43857f61fbeSMilanka Ringwald // stats 439c37cd8f3SMatthias Ringwald if (zero_seq_found){ 440c37cd8f3SMatthias Ringwald state->zero_frames_nr++; 441c37cd8f3SMatthias Ringwald } else { 442c37cd8f3SMatthias Ringwald state->bad_frames_nr++; 443c37cd8f3SMatthias Ringwald } 444c37cd8f3SMatthias Ringwald #ifdef LOG_FRAME_STATUS 445c37cd8f3SMatthias Ringwald if (zero_seq_found){ 446c37cd8f3SMatthias Ringwald printf("%d : ZERO FRAME\n", decoder_state->h2_sequence_nr); 447c37cd8f3SMatthias Ringwald } else { 448c37cd8f3SMatthias Ringwald printf("%d : BAD FRAME\n", decoder_state->h2_sequence_nr); 449c37cd8f3SMatthias Ringwald } 450c37cd8f3SMatthias Ringwald #endif 451cca2ddccSMatthias Ringwald // retry after dropoing 3 byte sync 452cca2ddccSMatthias Ringwald bytes_processed = 3; 45357f61fbeSMilanka Ringwald memmove(decoder_state->frame_buffer, decoder_state->frame_buffer + bytes_processed, decoder_state->bytes_in_frame_buffer); 45457f61fbeSMilanka Ringwald decoder_state->bytes_in_frame_buffer -= bytes_processed; 45557f61fbeSMilanka Ringwald decoder_state->msbc_bad_bytes += bytes_processed; 456ce1fc992SMatthias Ringwald // log_info("Trace bad frame"); 45757f61fbeSMilanka Ringwald continue; 45857f61fbeSMilanka Ringwald } 45957f61fbeSMilanka Ringwald 46057f61fbeSMilanka Ringwald // ready to decode frame 46157f61fbeSMilanka Ringwald OI_STATUS status = OI_CODEC_SBC_DecodeFrame(&(decoder_state->decoder_context), 462c37cd8f3SMatthias Ringwald &frame_data, 46357f61fbeSMilanka Ringwald &(decoder_state->bytes_in_frame_buffer), 464c37cd8f3SMatthias Ringwald decoder_state->pcm_plc_data, 465c37cd8f3SMatthias Ringwald &(decoder_state->pcm_bytes)); 466c37cd8f3SMatthias Ringwald 46757f61fbeSMilanka Ringwald bytes_processed = bytes_in_frame_buffer_before_decoding - decoder_state->bytes_in_frame_buffer; 468ce1fc992SMatthias 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); 46957f61fbeSMilanka Ringwald 47057f61fbeSMilanka Ringwald switch(status){ 47157f61fbeSMilanka Ringwald case 0: 472ce1fc992SMatthias Ringwald // synced 47357f61fbeSMilanka Ringwald decoder_state->first_good_frame_found = 1; 47457f61fbeSMilanka Ringwald 475ce1fc992SMatthias Ringwald // get rid of padding byte, not processed by SBC decoder 476ce1fc992SMatthias Ringwald decoder_state->bytes_in_frame_buffer = 0; 477ce1fc992SMatthias Ringwald 478ce1fc992SMatthias Ringwald // restart counting bad bytes 479ce1fc992SMatthias Ringwald decoder_state->msbc_bad_bytes = 0; 480ce1fc992SMatthias Ringwald 481ce1fc992SMatthias Ringwald // feed good frame into PLC history 48257f61fbeSMilanka Ringwald btstack_sbc_plc_good_frame(&state->plc_state, decoder_state->pcm_plc_data, decoder_state->pcm_data); 483ce1fc992SMatthias Ringwald 484ce1fc992SMatthias Ringwald // deliver PCM data 485c37cd8f3SMatthias Ringwald state->handle_pcm_data(decoder_state->pcm_data, 486c37cd8f3SMatthias Ringwald btstack_sbc_decoder_num_samples_per_frame(state), 487c37cd8f3SMatthias Ringwald btstack_sbc_decoder_num_channels(state), 488c37cd8f3SMatthias Ringwald btstack_sbc_decoder_sample_rate(state), state->context); 489c37cd8f3SMatthias Ringwald 490ce1fc992SMatthias Ringwald // stats 49157f61fbeSMilanka Ringwald state->good_frames_nr++; 49257f61fbeSMilanka Ringwald continue; 493c37cd8f3SMatthias Ringwald 49457f61fbeSMilanka Ringwald case OI_CODEC_SBC_CHECKSUM_MISMATCH: 49557f61fbeSMilanka Ringwald // The next frame is somehow corrupt. 4964e5badd2SMatthias Ringwald log_debug("OI_CODEC_SBC_CHECKSUM_MISMATCH"); 49757f61fbeSMilanka Ringwald // Did the codec consume any bytes? 49857f61fbeSMilanka Ringwald if (bytes_processed > 0){ 49957f61fbeSMilanka Ringwald // Good. Nothing to do. 50057f61fbeSMilanka Ringwald } else { 50157f61fbeSMilanka Ringwald // Skip the bogus frame by skipping the header. 50257f61fbeSMilanka Ringwald bytes_processed = 1; 50357f61fbeSMilanka Ringwald } 50457f61fbeSMilanka Ringwald break; 50557f61fbeSMilanka Ringwald 50604ec07bfSMilanka Ringwald case OI_STATUS_INVALID_PARAMETERS: 50704ec07bfSMilanka Ringwald // This caused by corrupt frames. 50804ec07bfSMilanka Ringwald // The codec apparently does not recover from this. 50904ec07bfSMilanka Ringwald // Re-initialize the codec. 51004ec07bfSMilanka Ringwald log_info("SBC decode: invalid parameters: resetting codec"); 511d6ec9a32SMilanka 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){ 51204ec07bfSMilanka Ringwald log_info("SBC decode: resetting codec failed"); 51304ec07bfSMilanka Ringwald } 51404ec07bfSMilanka Ringwald break; 515c37cd8f3SMatthias Ringwald default: 516c37cd8f3SMatthias Ringwald log_info("Frame decode error: %d", status); 517c37cd8f3SMatthias Ringwald break; 518c37cd8f3SMatthias Ringwald } 519c37cd8f3SMatthias Ringwald 520ce1fc992SMatthias Ringwald // on success, while loop was restarted, so all processed bytes have been "bad" 52157f61fbeSMilanka Ringwald decoder_state->msbc_bad_bytes += bytes_processed; 52257f61fbeSMilanka Ringwald 523ce1fc992SMatthias Ringwald // drop processed bytes from frame buffer 524c37cd8f3SMatthias Ringwald memmove(decoder_state->frame_buffer, decoder_state->frame_buffer + bytes_processed, decoder_state->bytes_in_frame_buffer); 525c37cd8f3SMatthias Ringwald } 526c37cd8f3SMatthias Ringwald } 527c37cd8f3SMatthias Ringwald 528c37cd8f3SMatthias Ringwald void btstack_sbc_decoder_process_data(btstack_sbc_decoder_state_t * state, int packet_status_flag, uint8_t * buffer, int size){ 529c37cd8f3SMatthias Ringwald if (state->mode == SBC_MODE_mSBC){ 530c37cd8f3SMatthias Ringwald btstack_sbc_decoder_process_msbc_data(state, packet_status_flag, buffer, size); 531c37cd8f3SMatthias Ringwald } else { 5326276d22aSMilanka Ringwald btstack_sbc_decoder_process_sbc_data(state, buffer, size); 533c37cd8f3SMatthias Ringwald } 534c37cd8f3SMatthias Ringwald } 535