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 38c37cd8f3SMatthias 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 h2_sequence_nr; 77c37cd8f3SMatthias Ringwald int search_new_sync_word; 78c37cd8f3SMatthias Ringwald int sync_word_found; 79c37cd8f3SMatthias Ringwald int first_good_frame_found; 80c37cd8f3SMatthias Ringwald } bludroid_decoder_state_t; 81c37cd8f3SMatthias Ringwald 82c37cd8f3SMatthias Ringwald static btstack_sbc_decoder_state_t * sbc_decoder_state_singleton = NULL; 83c37cd8f3SMatthias Ringwald static bludroid_decoder_state_t bd_decoder_state; 84c37cd8f3SMatthias Ringwald 85c37cd8f3SMatthias Ringwald // Testing only - START 86c37cd8f3SMatthias Ringwald static int plc_enabled = 1; 87c37cd8f3SMatthias Ringwald static int corrupt_frame_period = -1; 88c37cd8f3SMatthias Ringwald // Testing - STOP 89c37cd8f3SMatthias Ringwald 90c37cd8f3SMatthias Ringwald void btstack_sbc_decoder_test_disable_plc(void){ 91c37cd8f3SMatthias Ringwald plc_enabled = 0; 92c37cd8f3SMatthias Ringwald } 93c37cd8f3SMatthias Ringwald 94c37cd8f3SMatthias Ringwald void btstack_sbc_decoder_test_simulate_corrupt_frames(int period){ 95c37cd8f3SMatthias Ringwald corrupt_frame_period = period; 96c37cd8f3SMatthias Ringwald } 97c37cd8f3SMatthias Ringwald 98c37cd8f3SMatthias Ringwald static int find_sequence_of_zeros(const OI_BYTE *frame_data, OI_UINT32 frame_bytes, int seq_length){ 99c37cd8f3SMatthias Ringwald int zero_seq_count = 0; 100c37cd8f3SMatthias Ringwald unsigned int i; 101c37cd8f3SMatthias Ringwald for (i=0; i<frame_bytes; i++){ 102c37cd8f3SMatthias Ringwald if (frame_data[i] == 0) { 103c37cd8f3SMatthias Ringwald zero_seq_count++; 104c37cd8f3SMatthias Ringwald if (zero_seq_count >= seq_length) return zero_seq_count; 105c37cd8f3SMatthias Ringwald } else { 106c37cd8f3SMatthias Ringwald zero_seq_count = 0; 107c37cd8f3SMatthias Ringwald } 108c37cd8f3SMatthias Ringwald } 109c37cd8f3SMatthias Ringwald return 0; 110c37cd8f3SMatthias Ringwald } 111c37cd8f3SMatthias Ringwald 112c37cd8f3SMatthias Ringwald static int find_h2_syncword(const OI_BYTE *frame_data, OI_UINT32 frame_bytes){ 113c37cd8f3SMatthias Ringwald int syncword = mSBC_SYNCWORD; 114c37cd8f3SMatthias Ringwald uint8_t h2_first_byte = 0; 115c37cd8f3SMatthias Ringwald uint8_t h2_second_byte = 0; 116c37cd8f3SMatthias Ringwald 117c37cd8f3SMatthias Ringwald unsigned int i; 118c37cd8f3SMatthias Ringwald for (i=0; i<frame_bytes; i++){ 119c37cd8f3SMatthias Ringwald if (frame_data[i] == syncword) { 120c37cd8f3SMatthias Ringwald break; 121c37cd8f3SMatthias Ringwald } 122c37cd8f3SMatthias Ringwald h2_first_byte = h2_second_byte; 123c37cd8f3SMatthias Ringwald h2_second_byte = frame_data[i]; 124c37cd8f3SMatthias Ringwald } 125c37cd8f3SMatthias Ringwald if (h2_first_byte != 1) return -1; 126c37cd8f3SMatthias Ringwald 127c37cd8f3SMatthias Ringwald // check if upper nibble of second byte is 0x08 128c37cd8f3SMatthias Ringwald uint8_t ln = h2_second_byte & 0x0F; 129c37cd8f3SMatthias Ringwald if (ln != 8) return -1; 130c37cd8f3SMatthias Ringwald 131c37cd8f3SMatthias Ringwald // check that bits 0+2 == bits 1+3 132c37cd8f3SMatthias Ringwald uint8_t hn = h2_second_byte >> 4; 133c37cd8f3SMatthias Ringwald if ( ((hn>>1) & 0x05) != (hn & 0x05) ) return -1; 134c37cd8f3SMatthias Ringwald 135c37cd8f3SMatthias Ringwald return ((hn & 0x04) >> 1) | (hn & 0x01); 136c37cd8f3SMatthias Ringwald } 137c37cd8f3SMatthias Ringwald 138c37cd8f3SMatthias Ringwald int btstack_sbc_decoder_num_samples_per_frame(btstack_sbc_decoder_state_t * state){ 139c37cd8f3SMatthias Ringwald bludroid_decoder_state_t * decoder_state = (bludroid_decoder_state_t *) state->decoder_state; 140c37cd8f3SMatthias Ringwald return decoder_state->decoder_context.common.frameInfo.nrof_blocks * decoder_state->decoder_context.common.frameInfo.nrof_subbands; 141c37cd8f3SMatthias Ringwald } 142c37cd8f3SMatthias Ringwald 143c37cd8f3SMatthias Ringwald int btstack_sbc_decoder_num_channels(btstack_sbc_decoder_state_t * state){ 144c37cd8f3SMatthias Ringwald bludroid_decoder_state_t * decoder_state = (bludroid_decoder_state_t *) state->decoder_state; 145c37cd8f3SMatthias Ringwald return decoder_state->decoder_context.common.frameInfo.nrof_channels; 146c37cd8f3SMatthias Ringwald } 147c37cd8f3SMatthias Ringwald 148c37cd8f3SMatthias Ringwald int btstack_sbc_decoder_sample_rate(btstack_sbc_decoder_state_t * state){ 149c37cd8f3SMatthias Ringwald bludroid_decoder_state_t * decoder_state = (bludroid_decoder_state_t *) state->decoder_state; 150c37cd8f3SMatthias Ringwald return decoder_state->decoder_context.common.frameInfo.frequency; 151c37cd8f3SMatthias Ringwald } 152c37cd8f3SMatthias Ringwald 153c37cd8f3SMatthias Ringwald #ifdef OI_DEBUG 154c37cd8f3SMatthias Ringwald void OI_AssertFail(const char* file, int line, const char* reason){ 155c37cd8f3SMatthias Ringwald log_error("AssertFail file %s, line %d, reason %s", file, line, reason); 156c37cd8f3SMatthias Ringwald } 157c37cd8f3SMatthias Ringwald #endif 158c37cd8f3SMatthias Ringwald 159c37cd8f3SMatthias 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){ 160c37cd8f3SMatthias Ringwald if (sbc_decoder_state_singleton && sbc_decoder_state_singleton != state ){ 161c37cd8f3SMatthias Ringwald log_error("SBC decoder: different sbc decoder state is allready registered"); 162c37cd8f3SMatthias Ringwald } 163c37cd8f3SMatthias Ringwald OI_STATUS status = OI_STATUS_SUCCESS; 164c37cd8f3SMatthias Ringwald switch (mode){ 165c37cd8f3SMatthias Ringwald case SBC_MODE_STANDARD: 166c37cd8f3SMatthias Ringwald // note: we always request stereo output, even for mono input 167c37cd8f3SMatthias 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); 168c37cd8f3SMatthias Ringwald break; 169c37cd8f3SMatthias Ringwald case SBC_MODE_mSBC: 170c37cd8f3SMatthias Ringwald status = OI_CODEC_mSBC_DecoderReset(&(bd_decoder_state.decoder_context), bd_decoder_state.decoder_data, sizeof(bd_decoder_state.decoder_data)); 171c37cd8f3SMatthias Ringwald break; 172c37cd8f3SMatthias Ringwald default: 173c37cd8f3SMatthias Ringwald break; 174c37cd8f3SMatthias Ringwald } 175c37cd8f3SMatthias Ringwald 176c37cd8f3SMatthias Ringwald if (status != OI_STATUS_SUCCESS){ 177c37cd8f3SMatthias Ringwald log_error("SBC decoder: error during reset %d\n", status); 178c37cd8f3SMatthias Ringwald } 179c37cd8f3SMatthias Ringwald 180c37cd8f3SMatthias Ringwald sbc_decoder_state_singleton = state; 181c37cd8f3SMatthias Ringwald 182c37cd8f3SMatthias Ringwald bd_decoder_state.bytes_in_frame_buffer = 0; 183c37cd8f3SMatthias Ringwald bd_decoder_state.pcm_bytes = sizeof(bd_decoder_state.pcm_data); 184c37cd8f3SMatthias Ringwald bd_decoder_state.h2_sequence_nr = -1; 185c37cd8f3SMatthias Ringwald bd_decoder_state.sync_word_found = 0; 186c37cd8f3SMatthias Ringwald bd_decoder_state.search_new_sync_word = 0; 187c37cd8f3SMatthias Ringwald if (mode == SBC_MODE_mSBC){ 188c37cd8f3SMatthias Ringwald bd_decoder_state.search_new_sync_word = 1; 189c37cd8f3SMatthias Ringwald } 190c37cd8f3SMatthias Ringwald bd_decoder_state.first_good_frame_found = 0; 191c37cd8f3SMatthias Ringwald 192c37cd8f3SMatthias Ringwald memset(state, 0, sizeof(btstack_sbc_decoder_state_t)); 193c37cd8f3SMatthias Ringwald state->handle_pcm_data = callback; 194c37cd8f3SMatthias Ringwald state->mode = mode; 195c37cd8f3SMatthias Ringwald state->context = context; 196c37cd8f3SMatthias Ringwald state->decoder_state = &bd_decoder_state; 197c37cd8f3SMatthias Ringwald btstack_sbc_plc_init(&state->plc_state); 198c37cd8f3SMatthias Ringwald } 199c37cd8f3SMatthias Ringwald 200c37cd8f3SMatthias Ringwald static void append_received_sbc_data(bludroid_decoder_state_t * state, uint8_t * buffer, int size){ 201c37cd8f3SMatthias Ringwald int numFreeBytes = sizeof(state->frame_buffer) - state->bytes_in_frame_buffer; 202c37cd8f3SMatthias Ringwald 203c37cd8f3SMatthias Ringwald if (size > numFreeBytes){ 204c37cd8f3SMatthias Ringwald log_error("SBC data: more bytes read %u than free bytes in buffer %u", size, numFreeBytes); 205c37cd8f3SMatthias Ringwald } 206c37cd8f3SMatthias Ringwald 207c37cd8f3SMatthias Ringwald memcpy(state->frame_buffer + state->bytes_in_frame_buffer, buffer, size); 208c37cd8f3SMatthias Ringwald state->bytes_in_frame_buffer += size; 209c37cd8f3SMatthias Ringwald } 210c37cd8f3SMatthias Ringwald 211c37cd8f3SMatthias Ringwald 212c37cd8f3SMatthias Ringwald static void btstack_sbc_decoder_process_sbc_data(btstack_sbc_decoder_state_t * state, int packet_status_flag, 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; 215c37cd8f3SMatthias Ringwald 216c37cd8f3SMatthias Ringwald while (input_bytes_to_process){ 217*7c5b4db4SMatthias Ringwald 218*7c5b4db4SMatthias Ringwald // fill buffer with new data 219c37cd8f3SMatthias Ringwald int bytes_free_in_buffer = SBC_MAX_FRAME_LEN - decoder_state->bytes_in_frame_buffer; 220c37cd8f3SMatthias Ringwald int bytes_to_append = btstack_min(input_bytes_to_process, bytes_free_in_buffer); 221*7c5b4db4SMatthias 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; 225*7c5b4db4SMatthias Ringwald } 226c37cd8f3SMatthias Ringwald 227c37cd8f3SMatthias Ringwald uint16_t bytes_in_buffer_before = decoder_state->bytes_in_frame_buffer; 228c37cd8f3SMatthias Ringwald uint16_t bytes_processed = 0; 229c37cd8f3SMatthias Ringwald const OI_BYTE *frame_data = decoder_state->frame_buffer; 230c37cd8f3SMatthias Ringwald 231c37cd8f3SMatthias Ringwald static int frame_count = 0; 232c37cd8f3SMatthias Ringwald while (1){ 233c37cd8f3SMatthias Ringwald if (corrupt_frame_period > 0){ 234c37cd8f3SMatthias Ringwald frame_count++; 235c37cd8f3SMatthias Ringwald 236c37cd8f3SMatthias Ringwald if (frame_count % corrupt_frame_period == 0){ 237c37cd8f3SMatthias Ringwald *(uint8_t*)&frame_data[5] = 0; 238c37cd8f3SMatthias Ringwald frame_count = 0; 239c37cd8f3SMatthias Ringwald } 240c37cd8f3SMatthias Ringwald } 241c37cd8f3SMatthias Ringwald 242c37cd8f3SMatthias Ringwald OI_STATUS status = OI_STATUS_SUCCESS; 243c37cd8f3SMatthias Ringwald int bad_frame = 0; 244c37cd8f3SMatthias Ringwald int zero_seq_found = 0; 245c37cd8f3SMatthias Ringwald 246c37cd8f3SMatthias Ringwald if (decoder_state->first_good_frame_found){ 247c37cd8f3SMatthias Ringwald zero_seq_found = find_sequence_of_zeros(frame_data, decoder_state->bytes_in_frame_buffer, 20); 248c37cd8f3SMatthias Ringwald bad_frame = zero_seq_found || packet_status_flag; 249c37cd8f3SMatthias Ringwald } 250c37cd8f3SMatthias Ringwald 251c37cd8f3SMatthias Ringwald if (bad_frame){ 252c37cd8f3SMatthias Ringwald status = OI_CODEC_SBC_CHECKSUM_MISMATCH; 253c37cd8f3SMatthias Ringwald decoder_state->bytes_in_frame_buffer = 0; 254c37cd8f3SMatthias Ringwald } else { 255c37cd8f3SMatthias Ringwald memset(decoder_state->pcm_plc_data, 0x55, SBC_MAX_CHANNELS * SBC_MAX_BANDS * SBC_MAX_BLOCKS * 2); 256c37cd8f3SMatthias Ringwald status = OI_CODEC_SBC_DecodeFrame(&(decoder_state->decoder_context), 257c37cd8f3SMatthias Ringwald &frame_data, 258c37cd8f3SMatthias Ringwald &(decoder_state->bytes_in_frame_buffer), 259c37cd8f3SMatthias Ringwald decoder_state->pcm_plc_data, 260c37cd8f3SMatthias Ringwald &(decoder_state->pcm_bytes)); 261c37cd8f3SMatthias Ringwald } 262c37cd8f3SMatthias Ringwald 263c37cd8f3SMatthias Ringwald bytes_processed = bytes_in_buffer_before - decoder_state->bytes_in_frame_buffer; 264c37cd8f3SMatthias Ringwald switch(status){ 265c37cd8f3SMatthias Ringwald case OI_STATUS_SUCCESS: 266c37cd8f3SMatthias Ringwald decoder_state->first_good_frame_found = 1; 267c37cd8f3SMatthias Ringwald 268c37cd8f3SMatthias Ringwald if (state->mode == SBC_MODE_mSBC){ 269c37cd8f3SMatthias Ringwald decoder_state->search_new_sync_word = 1; 270c37cd8f3SMatthias Ringwald decoder_state->sync_word_found = 0; 271c37cd8f3SMatthias Ringwald } 272c37cd8f3SMatthias Ringwald 273c37cd8f3SMatthias Ringwald state->handle_pcm_data(decoder_state->pcm_plc_data, 274c37cd8f3SMatthias Ringwald btstack_sbc_decoder_num_samples_per_frame(state), 275c37cd8f3SMatthias Ringwald btstack_sbc_decoder_num_channels(state), 276c37cd8f3SMatthias Ringwald btstack_sbc_decoder_sample_rate(state), state->context); 277c37cd8f3SMatthias Ringwald state->good_frames_nr++; 278c37cd8f3SMatthias Ringwald continue; 279c37cd8f3SMatthias Ringwald case OI_CODEC_SBC_NOT_ENOUGH_HEADER_DATA: 280c37cd8f3SMatthias Ringwald case OI_CODEC_SBC_NOT_ENOUGH_BODY_DATA: 281c37cd8f3SMatthias Ringwald // printf(" NOT_ENOUGH_DATA\n"); 282c37cd8f3SMatthias Ringwald if (decoder_state->sync_word_found){ 283c37cd8f3SMatthias Ringwald decoder_state->search_new_sync_word = 0; 284c37cd8f3SMatthias Ringwald } 285c37cd8f3SMatthias Ringwald break; 286c37cd8f3SMatthias Ringwald case OI_CODEC_SBC_NO_SYNCWORD: 287c37cd8f3SMatthias Ringwald case OI_CODEC_SBC_CHECKSUM_MISMATCH: 288c37cd8f3SMatthias Ringwald // printf("NO_SYNCWORD or CHECKSUM_MISMATCH\n"); 289c37cd8f3SMatthias Ringwald decoder_state->bytes_in_frame_buffer = 0; 290c37cd8f3SMatthias Ringwald if (!decoder_state->first_good_frame_found) break; 291c37cd8f3SMatthias Ringwald 292c37cd8f3SMatthias Ringwald if (zero_seq_found){ 293c37cd8f3SMatthias Ringwald state->zero_frames_nr++; 294c37cd8f3SMatthias Ringwald } else { 295c37cd8f3SMatthias Ringwald state->bad_frames_nr++; 296c37cd8f3SMatthias Ringwald } 297c37cd8f3SMatthias Ringwald if (!plc_enabled) break; 298c37cd8f3SMatthias Ringwald break; 299c37cd8f3SMatthias Ringwald default: 300c37cd8f3SMatthias Ringwald log_info("Frame decode error: %d", status); 301c37cd8f3SMatthias Ringwald break; 302c37cd8f3SMatthias Ringwald } 303c37cd8f3SMatthias Ringwald 304c37cd8f3SMatthias Ringwald memmove(decoder_state->frame_buffer, decoder_state->frame_buffer + bytes_processed, decoder_state->bytes_in_frame_buffer); 305c37cd8f3SMatthias Ringwald if ((status != OI_STATUS_SUCCESS) || (decoder_state->bytes_in_frame_buffer == 0)) break; 306c37cd8f3SMatthias Ringwald 307c37cd8f3SMatthias Ringwald } 308c37cd8f3SMatthias Ringwald } 309c37cd8f3SMatthias Ringwald } 310c37cd8f3SMatthias Ringwald 311c37cd8f3SMatthias Ringwald 312c37cd8f3SMatthias Ringwald static void btstack_sbc_decoder_process_msbc_data(btstack_sbc_decoder_state_t * state, int packet_status_flag, uint8_t * buffer, int size){ 313c37cd8f3SMatthias Ringwald 314c37cd8f3SMatthias Ringwald bludroid_decoder_state_t * decoder_state = (bludroid_decoder_state_t*)state->decoder_state; 315c37cd8f3SMatthias Ringwald int input_bytes_to_process = size; 316c37cd8f3SMatthias Ringwald unsigned int msbc_frame_size = 57; 317c37cd8f3SMatthias Ringwald 318c37cd8f3SMatthias Ringwald // printf("<<-- enter -->>\n"); 319c37cd8f3SMatthias Ringwald // printf("Process data: in buffer %u, new %u\n", decoder_state->bytes_in_frame_buffer, size); 320c37cd8f3SMatthias Ringwald 321c37cd8f3SMatthias Ringwald while (input_bytes_to_process > 0){ 322c37cd8f3SMatthias Ringwald 323*7c5b4db4SMatthias Ringwald // fill buffer with new data 324c37cd8f3SMatthias Ringwald int bytes_missing_for_complete_msbc_frame = msbc_frame_size - decoder_state->bytes_in_frame_buffer; 325c37cd8f3SMatthias Ringwald int bytes_to_append = btstack_min(input_bytes_to_process, bytes_missing_for_complete_msbc_frame); 326*7c5b4db4SMatthias Ringwald if (bytes_to_append) { 327c37cd8f3SMatthias Ringwald append_received_sbc_data(decoder_state, buffer, bytes_to_append); 328c37cd8f3SMatthias Ringwald buffer += bytes_to_append; 329c37cd8f3SMatthias Ringwald input_bytes_to_process -= bytes_to_append; 330*7c5b4db4SMatthias Ringwald } 331c37cd8f3SMatthias Ringwald 332c37cd8f3SMatthias Ringwald if (decoder_state->bytes_in_frame_buffer < msbc_frame_size){ 333c37cd8f3SMatthias Ringwald // printf("not enough data %d > %d\n", msbc_frame_size, decoder_state->bytes_in_frame_buffer); 334c37cd8f3SMatthias Ringwald if (input_bytes_to_process){ 335c37cd8f3SMatthias Ringwald log_error("SHOULD NOT HAPPEN... not enough bytes, but bytes left to process"); 336c37cd8f3SMatthias Ringwald } 337c37cd8f3SMatthias Ringwald break; 338c37cd8f3SMatthias Ringwald } 339c37cd8f3SMatthias Ringwald 340c37cd8f3SMatthias Ringwald uint16_t bytes_in_buffer_before = decoder_state->bytes_in_frame_buffer; 341c37cd8f3SMatthias Ringwald uint16_t bytes_processed = 0; 342c37cd8f3SMatthias Ringwald const OI_BYTE *frame_data = decoder_state->frame_buffer; 343c37cd8f3SMatthias Ringwald 344c37cd8f3SMatthias Ringwald static int frame_count = 0; 345c37cd8f3SMatthias Ringwald if (corrupt_frame_period > 0){ 346c37cd8f3SMatthias Ringwald frame_count++; 347c37cd8f3SMatthias Ringwald 348c37cd8f3SMatthias Ringwald if (frame_count % corrupt_frame_period == 0){ 349c37cd8f3SMatthias Ringwald *(uint8_t*)&frame_data[5] = 0; 350c37cd8f3SMatthias Ringwald frame_count = 0; 351c37cd8f3SMatthias Ringwald } 352c37cd8f3SMatthias Ringwald } 353c37cd8f3SMatthias Ringwald 354c37cd8f3SMatthias Ringwald OI_STATUS status = OI_STATUS_SUCCESS; 355c37cd8f3SMatthias Ringwald int bad_frame = 0; 356c37cd8f3SMatthias Ringwald int zero_seq_found = 0; 357c37cd8f3SMatthias Ringwald 358c37cd8f3SMatthias Ringwald if (decoder_state->first_good_frame_found){ 359c37cd8f3SMatthias Ringwald zero_seq_found = find_sequence_of_zeros(frame_data, decoder_state->bytes_in_frame_buffer, 20); 360c37cd8f3SMatthias Ringwald bad_frame = zero_seq_found || packet_status_flag; 361c37cd8f3SMatthias Ringwald } 362c37cd8f3SMatthias Ringwald 363c37cd8f3SMatthias Ringwald if (bad_frame){ 364c37cd8f3SMatthias Ringwald status = OI_CODEC_SBC_CHECKSUM_MISMATCH; 365c37cd8f3SMatthias Ringwald decoder_state->bytes_in_frame_buffer = 0; 366c37cd8f3SMatthias Ringwald } else { 367c37cd8f3SMatthias Ringwald if (decoder_state->search_new_sync_word && !decoder_state->sync_word_found){ 368c37cd8f3SMatthias Ringwald int h2_syncword = find_h2_syncword(frame_data, decoder_state->bytes_in_frame_buffer); 369c37cd8f3SMatthias Ringwald 370c37cd8f3SMatthias Ringwald if (h2_syncword != -1){ 371c37cd8f3SMatthias Ringwald decoder_state->sync_word_found = 1; 372c37cd8f3SMatthias Ringwald decoder_state->h2_sequence_nr = h2_syncword; 373c37cd8f3SMatthias Ringwald } 374c37cd8f3SMatthias Ringwald } 375c37cd8f3SMatthias Ringwald status = OI_CODEC_SBC_DecodeFrame(&(decoder_state->decoder_context), 376c37cd8f3SMatthias Ringwald &frame_data, 377c37cd8f3SMatthias Ringwald &(decoder_state->bytes_in_frame_buffer), 378c37cd8f3SMatthias Ringwald decoder_state->pcm_plc_data, 379c37cd8f3SMatthias Ringwald &(decoder_state->pcm_bytes)); 380c37cd8f3SMatthias Ringwald } 381c37cd8f3SMatthias Ringwald 382c37cd8f3SMatthias Ringwald bytes_processed = bytes_in_buffer_before - decoder_state->bytes_in_frame_buffer; 383c37cd8f3SMatthias Ringwald OI_UINT32 bytes_in_frame_buffer = msbc_frame_size; 384c37cd8f3SMatthias Ringwald 385c37cd8f3SMatthias Ringwald switch(status){ 386c37cd8f3SMatthias Ringwald case 0: 387c37cd8f3SMatthias Ringwald decoder_state->first_good_frame_found = 1; 388c37cd8f3SMatthias Ringwald 389c37cd8f3SMatthias Ringwald if (state->mode == SBC_MODE_mSBC){ 390c37cd8f3SMatthias Ringwald decoder_state->search_new_sync_word = 1; 391c37cd8f3SMatthias Ringwald decoder_state->sync_word_found = 0; 392c37cd8f3SMatthias Ringwald } 393c37cd8f3SMatthias Ringwald 394c37cd8f3SMatthias Ringwald btstack_sbc_plc_good_frame(&state->plc_state, decoder_state->pcm_plc_data, decoder_state->pcm_data); 395c37cd8f3SMatthias Ringwald state->handle_pcm_data(decoder_state->pcm_data, 396c37cd8f3SMatthias Ringwald btstack_sbc_decoder_num_samples_per_frame(state), 397c37cd8f3SMatthias Ringwald btstack_sbc_decoder_num_channels(state), 398c37cd8f3SMatthias Ringwald btstack_sbc_decoder_sample_rate(state), state->context); 399c37cd8f3SMatthias Ringwald state->good_frames_nr++; 400c37cd8f3SMatthias Ringwald continue; 401c37cd8f3SMatthias Ringwald case OI_CODEC_SBC_NOT_ENOUGH_HEADER_DATA: 402c37cd8f3SMatthias Ringwald case OI_CODEC_SBC_NOT_ENOUGH_BODY_DATA: 403c37cd8f3SMatthias Ringwald // printf(" NOT_ENOUGH_DATA\n"); 404c37cd8f3SMatthias Ringwald if (decoder_state->sync_word_found){ 405c37cd8f3SMatthias Ringwald decoder_state->search_new_sync_word = 0; 406c37cd8f3SMatthias Ringwald } 407c37cd8f3SMatthias Ringwald break; 408c37cd8f3SMatthias Ringwald case OI_CODEC_SBC_NO_SYNCWORD: 409c37cd8f3SMatthias Ringwald case OI_CODEC_SBC_CHECKSUM_MISMATCH: 410c37cd8f3SMatthias Ringwald // printf("NO_SYNCWORD or CHECKSUM_MISMATCH\n"); 411c37cd8f3SMatthias Ringwald decoder_state->bytes_in_frame_buffer = 0; 412c37cd8f3SMatthias Ringwald if (!decoder_state->first_good_frame_found) break; 413c37cd8f3SMatthias Ringwald 414c37cd8f3SMatthias Ringwald if (state->mode == SBC_MODE_mSBC){ 415c37cd8f3SMatthias Ringwald if (!decoder_state->sync_word_found){ 416c37cd8f3SMatthias Ringwald decoder_state->h2_sequence_nr = (decoder_state->h2_sequence_nr + 1)%4; 417c37cd8f3SMatthias Ringwald } 418c37cd8f3SMatthias Ringwald decoder_state->search_new_sync_word = 1; 419c37cd8f3SMatthias Ringwald decoder_state->sync_word_found = 0; 420c37cd8f3SMatthias Ringwald } 421c37cd8f3SMatthias Ringwald 422c37cd8f3SMatthias Ringwald if (zero_seq_found){ 423c37cd8f3SMatthias Ringwald state->zero_frames_nr++; 424c37cd8f3SMatthias Ringwald } else { 425c37cd8f3SMatthias Ringwald state->bad_frames_nr++; 426c37cd8f3SMatthias Ringwald } 427c37cd8f3SMatthias Ringwald 428c37cd8f3SMatthias Ringwald #ifdef LOG_FRAME_STATUS 429c37cd8f3SMatthias Ringwald if (zero_seq_found){ 430c37cd8f3SMatthias Ringwald printf("%d : ZERO FRAME\n", decoder_state->h2_sequence_nr); 431c37cd8f3SMatthias Ringwald } else { 432c37cd8f3SMatthias Ringwald printf("%d : BAD FRAME\n", decoder_state->h2_sequence_nr); 433c37cd8f3SMatthias Ringwald } 434c37cd8f3SMatthias Ringwald if (decoder_state->h2_sequence_nr == 3) printf("\n"); 435c37cd8f3SMatthias Ringwald #endif 436c37cd8f3SMatthias Ringwald if (!plc_enabled) break; 437c37cd8f3SMatthias Ringwald 438c37cd8f3SMatthias Ringwald frame_data = btstack_sbc_plc_zero_signal_frame(); 439c37cd8f3SMatthias Ringwald 440c37cd8f3SMatthias Ringwald status = OI_CODEC_SBC_DecodeFrame(&(decoder_state->decoder_context), 441c37cd8f3SMatthias Ringwald &frame_data, 442c37cd8f3SMatthias Ringwald &bytes_in_frame_buffer, 443c37cd8f3SMatthias Ringwald decoder_state->pcm_plc_data, 444c37cd8f3SMatthias Ringwald &(decoder_state->pcm_bytes)); 445c37cd8f3SMatthias Ringwald 446c37cd8f3SMatthias Ringwald if (status != 0) { 447c37cd8f3SMatthias Ringwald log_error("SBC decoder: error %d\n", status); 448c37cd8f3SMatthias Ringwald } 449c37cd8f3SMatthias Ringwald btstack_sbc_plc_bad_frame(&state->plc_state, decoder_state->pcm_plc_data, decoder_state->pcm_data); 450c37cd8f3SMatthias Ringwald state->handle_pcm_data(decoder_state->pcm_data, 451c37cd8f3SMatthias Ringwald btstack_sbc_decoder_num_samples_per_frame(state), 452c37cd8f3SMatthias Ringwald btstack_sbc_decoder_num_channels(state), 453c37cd8f3SMatthias Ringwald btstack_sbc_decoder_sample_rate(state), state->context); 454c37cd8f3SMatthias Ringwald 455c37cd8f3SMatthias Ringwald 456c37cd8f3SMatthias Ringwald break; 457c37cd8f3SMatthias Ringwald default: 458c37cd8f3SMatthias Ringwald log_info("Frame decode error: %d", status); 459c37cd8f3SMatthias Ringwald break; 460c37cd8f3SMatthias Ringwald } 461c37cd8f3SMatthias Ringwald 462c37cd8f3SMatthias Ringwald memmove(decoder_state->frame_buffer, decoder_state->frame_buffer + bytes_processed, decoder_state->bytes_in_frame_buffer); 463c37cd8f3SMatthias Ringwald } 464c37cd8f3SMatthias Ringwald } 465c37cd8f3SMatthias Ringwald 466c37cd8f3SMatthias Ringwald void btstack_sbc_decoder_process_data(btstack_sbc_decoder_state_t * state, int packet_status_flag, uint8_t * buffer, int size){ 467c37cd8f3SMatthias Ringwald if (state->mode == SBC_MODE_mSBC){ 468c37cd8f3SMatthias Ringwald btstack_sbc_decoder_process_msbc_data(state, packet_status_flag, buffer, size); 469c37cd8f3SMatthias Ringwald } else { 470c37cd8f3SMatthias Ringwald btstack_sbc_decoder_process_sbc_data(state, packet_status_flag, buffer, size); 471c37cd8f3SMatthias Ringwald } 472c37cd8f3SMatthias Ringwald } 473