xref: /btstack/src/classic/btstack_sbc_decoder_bluedroid.c (revision a305eb53511cf3f48f09c8b0adbf03644957476e)
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;
215*a305eb53SMilanka Ringwald     int keep_decoding = 1;
216c37cd8f3SMatthias Ringwald 
217*a305eb53SMilanka Ringwald     while (keep_decoding) {
218*a305eb53SMilanka Ringwald         // Fill decoder_state->frame_buffer as much as possible.
219*a305eb53SMilanka Ringwald         int bytes_free_in_frame_buffer = SBC_MAX_FRAME_LEN - decoder_state->bytes_in_frame_buffer;
220*a305eb53SMilanka Ringwald         int bytes_to_append = btstack_min(input_bytes_to_process, bytes_free_in_frame_buffer);
2217c5b4db4SMatthias Ringwald         if (bytes_to_append){
222c37cd8f3SMatthias Ringwald             append_received_sbc_data(decoder_state, buffer, bytes_to_append);
223c37cd8f3SMatthias Ringwald             buffer += bytes_to_append;
224c37cd8f3SMatthias Ringwald             input_bytes_to_process -= bytes_to_append;
2257c5b4db4SMatthias Ringwald         }
226c37cd8f3SMatthias Ringwald 
227*a305eb53SMilanka Ringwald         // Decode the next frame in decoder_state->frame_buffer.
228*a305eb53SMilanka Ringwald         int bytes_in_frame_buffer_before_decoding = decoder_state->bytes_in_frame_buffer;
229c37cd8f3SMatthias Ringwald         const OI_BYTE *frame_data = decoder_state->frame_buffer;
230*a305eb53SMilanka Ringwald         OI_UINT32 frame_data_len = decoder_state->bytes_in_frame_buffer;
231*a305eb53SMilanka Ringwald         OI_STATUS status = OI_CODEC_SBC_DecodeFrame(&(decoder_state->decoder_context),
232*a305eb53SMilanka Ringwald                                                     &frame_data,
233*a305eb53SMilanka Ringwald                                                     &frame_data_len,
234*a305eb53SMilanka Ringwald                                                     decoder_state->pcm_plc_data,
235*a305eb53SMilanka Ringwald                                                     &(decoder_state->pcm_bytes));
236*a305eb53SMilanka Ringwald         uint16_t bytes_processed = bytes_in_frame_buffer_before_decoding - frame_data_len;
237c37cd8f3SMatthias Ringwald 
238c37cd8f3SMatthias Ringwald         static int frame_count = 0;
239c37cd8f3SMatthias Ringwald         if (corrupt_frame_period > 0){
240c37cd8f3SMatthias Ringwald             frame_count++;
241c37cd8f3SMatthias Ringwald 
242c37cd8f3SMatthias Ringwald             if (frame_count % corrupt_frame_period == 0){
243c37cd8f3SMatthias Ringwald                 *(uint8_t*)&frame_data[5] = 0;
244c37cd8f3SMatthias Ringwald                 frame_count = 0;
245c37cd8f3SMatthias Ringwald             }
246c37cd8f3SMatthias Ringwald         }
247c37cd8f3SMatthias Ringwald 
248*a305eb53SMilanka Ringwald         // Handle decoding result.
249c37cd8f3SMatthias Ringwald         switch(status){
250c37cd8f3SMatthias Ringwald             case OI_STATUS_SUCCESS:
251*a305eb53SMilanka Ringwald             case OI_CODEC_SBC_PARTIAL_DECODE:
252c37cd8f3SMatthias Ringwald                 if (state->mode == SBC_MODE_mSBC){
253c37cd8f3SMatthias Ringwald                     decoder_state->search_new_sync_word = 1;
254c37cd8f3SMatthias Ringwald                     decoder_state->sync_word_found = 0;
255c37cd8f3SMatthias Ringwald                 }
256c37cd8f3SMatthias Ringwald 
257c37cd8f3SMatthias Ringwald                 state->handle_pcm_data(decoder_state->pcm_plc_data,
258c37cd8f3SMatthias Ringwald                                        btstack_sbc_decoder_num_samples_per_frame(state),
259c37cd8f3SMatthias Ringwald                                        btstack_sbc_decoder_num_channels(state),
260c37cd8f3SMatthias Ringwald                                        btstack_sbc_decoder_sample_rate(state), state->context);
261c37cd8f3SMatthias Ringwald                 state->good_frames_nr++;
262*a305eb53SMilanka Ringwald                 break;
263*a305eb53SMilanka Ringwald 
264c37cd8f3SMatthias Ringwald             case OI_CODEC_SBC_NOT_ENOUGH_HEADER_DATA:
265c37cd8f3SMatthias Ringwald             case OI_CODEC_SBC_NOT_ENOUGH_BODY_DATA:
266*a305eb53SMilanka Ringwald             case OI_CODEC_SBC_NOT_ENOUGH_AUDIO_DATA:
267c37cd8f3SMatthias Ringwald                 if (decoder_state->sync_word_found){
268c37cd8f3SMatthias Ringwald                     decoder_state->search_new_sync_word = 0;
269c37cd8f3SMatthias Ringwald                 }
270*a305eb53SMilanka Ringwald                 if (input_bytes_to_process > 0){
271*a305eb53SMilanka Ringwald                     // Should never occur: The SBC code claims there is not enough bytes in the frame_buffer,
272*a305eb53SMilanka Ringwald                     // but the frame_buffer was full. (The frame_buffer is always full before decoding when input_bytes_to_process > 0.)
273*a305eb53SMilanka Ringwald                     // Clear frame_buffer.
274*a305eb53SMilanka Ringwald                     log_info("SBC decode: frame_buffer too small for frame");
275*a305eb53SMilanka Ringwald                     bytes_processed = bytes_in_frame_buffer_before_decoding;
276c37cd8f3SMatthias Ringwald                 } else {
277*a305eb53SMilanka Ringwald                     // Exit decode loop, because there is not enough data in frame_buffer to decode the next frame.
278*a305eb53SMilanka Ringwald                     keep_decoding = 0;
279c37cd8f3SMatthias Ringwald                 }
280*a305eb53SMilanka Ringwald                 break;
281*a305eb53SMilanka Ringwald 
282*a305eb53SMilanka Ringwald             case OI_CODEC_SBC_NO_SYNCWORD:
283*a305eb53SMilanka Ringwald                 // This means the entire frame_buffer did not contain the syncword.
284*a305eb53SMilanka Ringwald                 // Discard the frame_buffer contents.
285*a305eb53SMilanka Ringwald                 log_info("SBC decode: no syncword found");
286*a305eb53SMilanka Ringwald                 bytes_processed = bytes_in_frame_buffer_before_decoding;
287*a305eb53SMilanka Ringwald                 break;
288*a305eb53SMilanka Ringwald 
289*a305eb53SMilanka Ringwald             case OI_CODEC_SBC_CHECKSUM_MISMATCH:
290*a305eb53SMilanka Ringwald                 // The next frame is somehow corrupt.
291*a305eb53SMilanka Ringwald                 log_info("SBC decode: checksum error");
292*a305eb53SMilanka Ringwald                 // Did the codec consume any bytes?
293*a305eb53SMilanka Ringwald                 if (bytes_processed > 0){
294*a305eb53SMilanka Ringwald                     // Good. Nothing to do.
295*a305eb53SMilanka Ringwald                 } else {
296*a305eb53SMilanka Ringwald                     // Skip the bogus frame by skipping the header.
297*a305eb53SMilanka Ringwald                     bytes_processed = 1;
298*a305eb53SMilanka Ringwald                 }
299*a305eb53SMilanka Ringwald                 break;
300*a305eb53SMilanka Ringwald 
301*a305eb53SMilanka Ringwald             case OI_STATUS_INVALID_PARAMETERS:
302*a305eb53SMilanka Ringwald                 // This caused by corrupt frames.
303*a305eb53SMilanka Ringwald                 // The codec apparently does not recover from this.
304*a305eb53SMilanka Ringwald                 // Re-initialize the codec.
305*a305eb53SMilanka Ringwald                 log_info("SBC decode: invalid parameters: resetting codec");
306*a305eb53SMilanka 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){
307*a305eb53SMilanka Ringwald                     log_info("SBC decode: resetting codec failed");
308*a305eb53SMilanka Ringwald 
309*a305eb53SMilanka Ringwald                 }
310c37cd8f3SMatthias Ringwald                 break;
311c37cd8f3SMatthias Ringwald             default:
312*a305eb53SMilanka Ringwald                 // Anything else went wrong.
313*a305eb53SMilanka Ringwald                 // Skip a few bytes and try again.
314*a305eb53SMilanka Ringwald                 bytes_processed = 1;
315*a305eb53SMilanka Ringwald                 log_info("SBC decode: unknown status %d", status);
316c37cd8f3SMatthias Ringwald                 break;
317c37cd8f3SMatthias Ringwald         }
318c37cd8f3SMatthias Ringwald 
319*a305eb53SMilanka Ringwald         // Remove decoded frame from decoder_state->frame_buffer.
320*a305eb53SMilanka Ringwald         if (bytes_processed > bytes_in_frame_buffer_before_decoding) {
321*a305eb53SMilanka Ringwald             bytes_processed = bytes_in_frame_buffer_before_decoding;
322c37cd8f3SMatthias Ringwald         }
323*a305eb53SMilanka Ringwald         memmove(decoder_state->frame_buffer, decoder_state->frame_buffer + bytes_processed, bytes_in_frame_buffer_before_decoding - bytes_processed);
324*a305eb53SMilanka Ringwald         decoder_state->bytes_in_frame_buffer -= bytes_processed;
325c37cd8f3SMatthias Ringwald     }
326c37cd8f3SMatthias Ringwald }
327c37cd8f3SMatthias Ringwald 
328c37cd8f3SMatthias Ringwald 
329c37cd8f3SMatthias Ringwald static void btstack_sbc_decoder_process_msbc_data(btstack_sbc_decoder_state_t * state, int packet_status_flag, uint8_t * buffer, int size){
330c37cd8f3SMatthias Ringwald 
331c37cd8f3SMatthias Ringwald     bludroid_decoder_state_t * decoder_state = (bludroid_decoder_state_t*)state->decoder_state;
332c37cd8f3SMatthias Ringwald     int input_bytes_to_process = size;
333c37cd8f3SMatthias Ringwald     unsigned int msbc_frame_size = 57;
334c37cd8f3SMatthias Ringwald 
335c37cd8f3SMatthias Ringwald     // printf("<<-- enter -->>\n");
336c37cd8f3SMatthias Ringwald     // printf("Process data: in buffer %u, new %u\n", decoder_state->bytes_in_frame_buffer, size);
337c37cd8f3SMatthias Ringwald 
338c37cd8f3SMatthias Ringwald     while (input_bytes_to_process > 0){
339c37cd8f3SMatthias Ringwald 
3407c5b4db4SMatthias Ringwald         // fill buffer with new data
341c37cd8f3SMatthias Ringwald         int bytes_missing_for_complete_msbc_frame = msbc_frame_size - decoder_state->bytes_in_frame_buffer;
342c37cd8f3SMatthias Ringwald         int bytes_to_append = btstack_min(input_bytes_to_process, bytes_missing_for_complete_msbc_frame);
3437c5b4db4SMatthias Ringwald         if (bytes_to_append) {
344c37cd8f3SMatthias Ringwald             append_received_sbc_data(decoder_state, buffer, bytes_to_append);
345c37cd8f3SMatthias Ringwald             buffer += bytes_to_append;
346c37cd8f3SMatthias Ringwald             input_bytes_to_process -= bytes_to_append;
3477c5b4db4SMatthias Ringwald         }
348c37cd8f3SMatthias Ringwald 
349c37cd8f3SMatthias Ringwald         if (decoder_state->bytes_in_frame_buffer < msbc_frame_size){
350c37cd8f3SMatthias Ringwald             // printf("not enough data %d > %d\n", msbc_frame_size, decoder_state->bytes_in_frame_buffer);
351c37cd8f3SMatthias Ringwald             if (input_bytes_to_process){
352c37cd8f3SMatthias Ringwald                 log_error("SHOULD NOT HAPPEN... not enough bytes, but bytes left to process");
353c37cd8f3SMatthias Ringwald             }
354c37cd8f3SMatthias Ringwald             break;
355c37cd8f3SMatthias Ringwald         }
356c37cd8f3SMatthias Ringwald 
357*a305eb53SMilanka Ringwald         uint16_t bytes_in_frame_buffer_before_decoding = decoder_state->bytes_in_frame_buffer;
358c37cd8f3SMatthias Ringwald         uint16_t bytes_processed = 0;
359c37cd8f3SMatthias Ringwald         const OI_BYTE *frame_data = decoder_state->frame_buffer;
360c37cd8f3SMatthias Ringwald 
361c37cd8f3SMatthias Ringwald         static int frame_count = 0;
362c37cd8f3SMatthias Ringwald         if (corrupt_frame_period > 0){
363c37cd8f3SMatthias Ringwald            frame_count++;
364c37cd8f3SMatthias Ringwald 
365c37cd8f3SMatthias Ringwald             if (frame_count % corrupt_frame_period == 0){
366c37cd8f3SMatthias Ringwald                 *(uint8_t*)&frame_data[5] = 0;
367c37cd8f3SMatthias Ringwald                 frame_count = 0;
368c37cd8f3SMatthias Ringwald             }
369c37cd8f3SMatthias Ringwald         }
370c37cd8f3SMatthias Ringwald 
371c37cd8f3SMatthias Ringwald         OI_STATUS status = OI_STATUS_SUCCESS;
372c37cd8f3SMatthias Ringwald         int bad_frame = 0;
373c37cd8f3SMatthias Ringwald         int zero_seq_found = 0;
374c37cd8f3SMatthias Ringwald 
375c37cd8f3SMatthias Ringwald         if (decoder_state->first_good_frame_found){
376c37cd8f3SMatthias Ringwald             zero_seq_found = find_sequence_of_zeros(frame_data, decoder_state->bytes_in_frame_buffer, 20);
377c37cd8f3SMatthias Ringwald             bad_frame = zero_seq_found || packet_status_flag;
378c37cd8f3SMatthias Ringwald         }
379c37cd8f3SMatthias Ringwald 
380c37cd8f3SMatthias Ringwald         if (bad_frame){
381c37cd8f3SMatthias Ringwald             status = OI_CODEC_SBC_CHECKSUM_MISMATCH;
382c37cd8f3SMatthias Ringwald             decoder_state->bytes_in_frame_buffer = 0;
383c37cd8f3SMatthias Ringwald         } else {
384c37cd8f3SMatthias Ringwald             if (decoder_state->search_new_sync_word && !decoder_state->sync_word_found){
385c37cd8f3SMatthias Ringwald                 int h2_syncword = find_h2_syncword(frame_data, decoder_state->bytes_in_frame_buffer);
386c37cd8f3SMatthias Ringwald 
387c37cd8f3SMatthias Ringwald                 if (h2_syncword != -1){
388c37cd8f3SMatthias Ringwald                     decoder_state->sync_word_found = 1;
389c37cd8f3SMatthias Ringwald                     decoder_state->h2_sequence_nr = h2_syncword;
390c37cd8f3SMatthias Ringwald                 }
391c37cd8f3SMatthias Ringwald             }
392c37cd8f3SMatthias Ringwald             status = OI_CODEC_SBC_DecodeFrame(&(decoder_state->decoder_context),
393c37cd8f3SMatthias Ringwald                                                 &frame_data,
394c37cd8f3SMatthias Ringwald                                                 &(decoder_state->bytes_in_frame_buffer),
395c37cd8f3SMatthias Ringwald                                                 decoder_state->pcm_plc_data,
396c37cd8f3SMatthias Ringwald                                                 &(decoder_state->pcm_bytes));
397c37cd8f3SMatthias Ringwald         }
398c37cd8f3SMatthias Ringwald 
399*a305eb53SMilanka Ringwald         bytes_processed = bytes_in_frame_buffer_before_decoding - decoder_state->bytes_in_frame_buffer;
400c37cd8f3SMatthias Ringwald         OI_UINT32 bytes_in_frame_buffer = msbc_frame_size;
401c37cd8f3SMatthias Ringwald 
402c37cd8f3SMatthias Ringwald         switch(status){
403c37cd8f3SMatthias Ringwald             case 0:
404c37cd8f3SMatthias Ringwald                 decoder_state->first_good_frame_found = 1;
405c37cd8f3SMatthias Ringwald 
406c37cd8f3SMatthias Ringwald                 if (state->mode == SBC_MODE_mSBC){
407c37cd8f3SMatthias Ringwald                     decoder_state->search_new_sync_word = 1;
408c37cd8f3SMatthias Ringwald                     decoder_state->sync_word_found = 0;
409c37cd8f3SMatthias Ringwald                 }
410c37cd8f3SMatthias Ringwald 
411c37cd8f3SMatthias Ringwald                 btstack_sbc_plc_good_frame(&state->plc_state, decoder_state->pcm_plc_data, decoder_state->pcm_data);
412c37cd8f3SMatthias Ringwald                 state->handle_pcm_data(decoder_state->pcm_data,
413c37cd8f3SMatthias Ringwald                                     btstack_sbc_decoder_num_samples_per_frame(state),
414c37cd8f3SMatthias Ringwald                                     btstack_sbc_decoder_num_channels(state),
415c37cd8f3SMatthias Ringwald                                     btstack_sbc_decoder_sample_rate(state), state->context);
416c37cd8f3SMatthias Ringwald                 state->good_frames_nr++;
417c37cd8f3SMatthias Ringwald                 continue;
418c37cd8f3SMatthias Ringwald             case OI_CODEC_SBC_NOT_ENOUGH_HEADER_DATA:
419c37cd8f3SMatthias Ringwald             case OI_CODEC_SBC_NOT_ENOUGH_BODY_DATA:
420c37cd8f3SMatthias Ringwald                 // printf("    NOT_ENOUGH_DATA\n");
421c37cd8f3SMatthias Ringwald                 if (decoder_state->sync_word_found){
422c37cd8f3SMatthias Ringwald                     decoder_state->search_new_sync_word = 0;
423c37cd8f3SMatthias Ringwald                 }
424c37cd8f3SMatthias Ringwald                 break;
425c37cd8f3SMatthias Ringwald             case OI_CODEC_SBC_NO_SYNCWORD:
426c37cd8f3SMatthias Ringwald             case OI_CODEC_SBC_CHECKSUM_MISMATCH:
427c37cd8f3SMatthias Ringwald                 // printf("NO_SYNCWORD or CHECKSUM_MISMATCH\n");
428c37cd8f3SMatthias Ringwald                 decoder_state->bytes_in_frame_buffer = 0;
429c37cd8f3SMatthias Ringwald                 if (!decoder_state->first_good_frame_found) break;
430c37cd8f3SMatthias Ringwald 
431c37cd8f3SMatthias Ringwald                 if (state->mode == SBC_MODE_mSBC){
432c37cd8f3SMatthias Ringwald                     if (!decoder_state->sync_word_found){
433c37cd8f3SMatthias Ringwald                         decoder_state->h2_sequence_nr = (decoder_state->h2_sequence_nr + 1)%4;
434c37cd8f3SMatthias Ringwald                     }
435c37cd8f3SMatthias Ringwald                     decoder_state->search_new_sync_word = 1;
436c37cd8f3SMatthias Ringwald                     decoder_state->sync_word_found = 0;
437c37cd8f3SMatthias Ringwald                 }
438c37cd8f3SMatthias Ringwald 
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 
445c37cd8f3SMatthias Ringwald #ifdef LOG_FRAME_STATUS
446c37cd8f3SMatthias Ringwald                 if (zero_seq_found){
447c37cd8f3SMatthias Ringwald                     printf("%d : ZERO FRAME\n", decoder_state->h2_sequence_nr);
448c37cd8f3SMatthias Ringwald                 } else {
449c37cd8f3SMatthias Ringwald                     printf("%d : BAD FRAME\n", decoder_state->h2_sequence_nr);
450c37cd8f3SMatthias Ringwald                 }
451c37cd8f3SMatthias Ringwald                 if (decoder_state->h2_sequence_nr == 3) printf("\n");
452c37cd8f3SMatthias Ringwald #endif
453c37cd8f3SMatthias Ringwald                 if (!plc_enabled) break;
454c37cd8f3SMatthias Ringwald 
455c37cd8f3SMatthias Ringwald                 frame_data = btstack_sbc_plc_zero_signal_frame();
456c37cd8f3SMatthias Ringwald 
457c37cd8f3SMatthias Ringwald                 status = OI_CODEC_SBC_DecodeFrame(&(decoder_state->decoder_context),
458c37cd8f3SMatthias Ringwald                                                     &frame_data,
459c37cd8f3SMatthias Ringwald                                                     &bytes_in_frame_buffer,
460c37cd8f3SMatthias Ringwald                                                     decoder_state->pcm_plc_data,
461c37cd8f3SMatthias Ringwald                                                     &(decoder_state->pcm_bytes));
462c37cd8f3SMatthias Ringwald 
463c37cd8f3SMatthias Ringwald                 if (status != 0) {
464c37cd8f3SMatthias Ringwald                     log_error("SBC decoder: error %d\n", status);
465c37cd8f3SMatthias Ringwald                 }
466c37cd8f3SMatthias Ringwald                 btstack_sbc_plc_bad_frame(&state->plc_state, decoder_state->pcm_plc_data, decoder_state->pcm_data);
467c37cd8f3SMatthias Ringwald                 state->handle_pcm_data(decoder_state->pcm_data,
468c37cd8f3SMatthias Ringwald                                     btstack_sbc_decoder_num_samples_per_frame(state),
469c37cd8f3SMatthias Ringwald                                     btstack_sbc_decoder_num_channels(state),
470c37cd8f3SMatthias Ringwald                                     btstack_sbc_decoder_sample_rate(state), state->context);
471c37cd8f3SMatthias Ringwald 
472c37cd8f3SMatthias Ringwald 
473c37cd8f3SMatthias Ringwald                 break;
474c37cd8f3SMatthias Ringwald             default:
475c37cd8f3SMatthias Ringwald                 log_info("Frame decode error: %d", status);
476c37cd8f3SMatthias Ringwald                 break;
477c37cd8f3SMatthias Ringwald         }
478c37cd8f3SMatthias Ringwald 
479c37cd8f3SMatthias Ringwald         memmove(decoder_state->frame_buffer, decoder_state->frame_buffer + bytes_processed, decoder_state->bytes_in_frame_buffer);
480c37cd8f3SMatthias Ringwald     }
481c37cd8f3SMatthias Ringwald }
482c37cd8f3SMatthias Ringwald 
483c37cd8f3SMatthias Ringwald void btstack_sbc_decoder_process_data(btstack_sbc_decoder_state_t * state, int packet_status_flag, uint8_t * buffer, int size){
484c37cd8f3SMatthias Ringwald     if (state->mode == SBC_MODE_mSBC){
485c37cd8f3SMatthias Ringwald         btstack_sbc_decoder_process_msbc_data(state, packet_status_flag, buffer, size);
486c37cd8f3SMatthias Ringwald     } else {
487c37cd8f3SMatthias Ringwald         btstack_sbc_decoder_process_sbc_data(state, packet_status_flag, buffer, size);
488c37cd8f3SMatthias Ringwald     }
489c37cd8f3SMatthias Ringwald }
490