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