xref: /btstack/test/sbc/sbc_decoder_test.c (revision 091c1bfd0aa5f8b62e35df65338a6c4ccddea874)
1 /*
2  * Copyright (C) 2014 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 // *****************************************************************************
39 //
40 // SBC decoder tests
41 //
42 // *****************************************************************************
43 
44 #include "btstack_config.h"
45 
46 #include <stdint.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <fcntl.h>
51 #include <unistd.h>
52 #include "oi_codec_sbc.h"
53 #include "oi_assert.h"
54 
55 static uint8_t read_buffer[6000];
56 static uint8_t frame_buffer[5000];
57 static OI_INT16 pcmData[1000];
58 static OI_UINT32 pcmBytes = sizeof(pcmData);
59 
60 static uint8_t buf[4];
61 
62 static OI_UINT32 decoderData[10000];
63 static OI_CODEC_SBC_DECODER_CONTEXT context;
64 
65 typedef struct {
66     OI_UINT32 bytesRead;
67     OI_UINT32 frameBytes;
68     OI_UINT32 inputBufferBytes;
69     int frame_count;
70     const OI_BYTE *frameData;
71 } sbc_state_t;
72 static sbc_state_t state;
73 
74 
75 void OI_AssertFail(char* file, int line, char* reason){
76     printf("AssertFail file %s, line %d, reason %s\n", file, line, reason);
77 }
78 
79 static void show_usage(void){
80     printf("Usage: ./sbc_decoder_test input.sbc");
81 }
82 
83 static ssize_t __read(int fd, void *buf, size_t count){
84     ssize_t len, pos = 0;
85 
86     while (count > 0) {
87         len = read(fd, buf + pos, count);
88         if (len <= 0)
89             return pos;
90 
91         count -= len;
92         pos   += len;
93     }
94     return pos;
95 }
96 
97 void little_endian_store_16(uint8_t *buffer, uint16_t pos, uint16_t value){
98     buffer[pos++] = value;
99     buffer[pos++] = value >> 8;
100 }
101 
102 void little_endian_store_32(uint8_t *buffer, uint16_t pos, uint32_t value){
103     buffer[pos++] = value;
104     buffer[pos++] = value >> 8;
105     buffer[pos++] = value >> 16;
106     buffer[pos++] = value >> 24;
107 }
108 
109 void little_endian_fstore_16(FILE *wav_file, uint16_t value){
110     little_endian_store_32(buf, 0, value);
111     fwrite(&buf, 1, 2, wav_file);
112 }
113 
114 void little_endian_fstore_32(FILE *wav_file, uint32_t value){
115     little_endian_store_32(buf, 0, value);
116     fwrite(&buf, 1, 4, wav_file);
117 }
118 
119 
120 static void write_wav_header(FILE * wav_file, OI_CODEC_SBC_DECODER_CONTEXT * decoderContext, int frame_count){
121     unsigned int bytes_per_sample = 2;
122     int num_samples = decoderContext->common.frameInfo.nrof_blocks * decoderContext->common.frameInfo.nrof_subbands;
123     int num_channels = decoderContext->common.frameInfo.nrof_channels;
124     int sample_rate = decoderContext->common.frameInfo.frequency;
125 
126     /* write RIFF header */
127     fwrite("RIFF", 1, 4, wav_file);
128     // num_samples = blocks * subbands
129     uint32_t data_bytes = (uint32_t) (bytes_per_sample * num_samples * frame_count * num_channels);
130     little_endian_fstore_32(wav_file, data_bytes + 36);
131     fwrite("WAVE", 1, 4, wav_file);
132 
133     int byte_rate = sample_rate * num_channels * bytes_per_sample;
134     int bits_per_sample = 8 * bytes_per_sample;
135     int block_align = num_channels * bits_per_sample;
136     int fmt_length = 16;
137     int fmt_format_tag = 1; // PCM
138 
139     /* write fmt chunk */
140     fwrite("fmt ", 1, 4, wav_file);
141     little_endian_fstore_32(wav_file, fmt_length);
142     little_endian_fstore_16(wav_file, fmt_format_tag);
143     little_endian_fstore_16(wav_file, num_channels);
144     little_endian_fstore_32(wav_file, sample_rate);
145     little_endian_fstore_32(wav_file, byte_rate);
146     little_endian_fstore_16(wav_file, block_align);
147     little_endian_fstore_16(wav_file, bits_per_sample);
148 
149     /* write data chunk */
150     fwrite("data", 1, 4, wav_file);
151     little_endian_fstore_32(wav_file, data_bytes);
152 }
153 
154 static void write_wav_data(FILE * wav_file, OI_CODEC_SBC_DECODER_CONTEXT * decoderContext, int16_t * data){
155     int num_samples = decoderContext->common.frameInfo.nrof_blocks * decoderContext->common.frameInfo.nrof_subbands;
156     int num_channels = decoderContext->common.frameInfo.nrof_channels;
157     int i;
158     for (i=0; i < num_samples; i++){
159         little_endian_fstore_16(wav_file, (uint16_t)data[i]);
160         if (num_channels == 2){
161             little_endian_fstore_16(wav_file, (uint16_t)data);
162         }
163     }
164 }
165 
166 static void init_sbc_state(sbc_state_t * state){
167     state->frameBytes = 0;
168     state->bytesRead = 0;
169     state->frame_count = 0;
170     state->inputBufferBytes = 0;
171     state->frameData = NULL;
172 }
173 
174 static void append_received_sbc_data(sbc_state_t * state, uint8_t * buffer, int size){
175     int numFreeBytes = sizeof(frame_buffer) - state->frameBytes;
176 
177     if (size > numFreeBytes){
178         printf("sbc data: more bytes read %u than free bytes in buffer %u", size, numFreeBytes);
179         exit(10);
180     }
181 
182     memcpy(frame_buffer + state->frameBytes, buffer, size);
183     state->frameBytes += size;
184     state->bytesRead = state->frameBytes;
185     state->frameData = frame_buffer;
186 }
187 
188 // assumption: new data fits into buffer
189 static void handle_received_sbc_data(sbc_state_t * state, uint8_t * buffer, int size, FILE * wav_file){
190     append_received_sbc_data(state, buffer, size);
191     while (1){
192 
193         OI_STATUS status = OI_CODEC_SBC_DecodeFrame(&context, &(state->frameData), &(state->frameBytes), pcmData, &pcmBytes);
194 
195         if (status != 0){
196             if (status != OI_CODEC_SBC_NOT_ENOUGH_HEADER_DATA && status != OI_CODEC_SBC_NOT_ENOUGH_BODY_DATA){
197                 OI_CODEC_SBC_DumpConfig(&(context. common.frameInfo));
198                 printf("Frame decode error %d\n", status);
199                 break;
200             }
201 
202             printf("Not enough data, read next %u bytes, move %d bytes\n", state->bytesRead-state->frameBytes, state->frameBytes);
203             memmove(frame_buffer, frame_buffer + state->bytesRead - state->frameBytes, state->frameBytes);
204             break;
205         }
206 
207         if (state->frame_count == 0){
208             write_wav_header(wav_file, &context, 0);
209         }
210 
211         write_wav_data(wav_file, &context, pcmData);
212         state->frame_count++;
213     }
214 }
215 
216 
217 int main (int argc, const char * argv[]){
218     if (argc < 2){
219         show_usage();
220         return -1;
221     }
222 
223     const char * sbc_filename = argv[1];
224     const char * wav_filename = argv[2];
225 
226 
227     int fd = open(sbc_filename, O_RDONLY);
228     if (fd < 0) {
229         printf("Can't open file %s", sbc_filename);
230         return -1;
231     }
232     printf("Open sbc file: %s\n", sbc_filename);
233 
234     //OI_STATUS status = OI_CODEC_SBC_DecoderReset(&context, decoderData, sizeof(decoderData), 1, 1, FALSE);
235 
236     OI_STATUS status = OI_CODEC_mSBC_DecoderReset(&context, decoderData, sizeof(decoderData));
237     if (status != 0){
238         printf("Reset decoder error %d\n", status);
239         return -1;
240     }
241 
242     FILE * wav_file = fopen(wav_filename, "wb");
243     init_sbc_state(&state);
244 
245     while (1){
246 
247         int bytes_read = __read(fd, read_buffer, sizeof(read_buffer));
248         if (0 >= bytes_read) break;
249 
250         while (bytes_read > 0){
251             int space_in_frame_buffer = sizeof(frame_buffer) - state.frameBytes;
252             int bytes_to_append = space_in_frame_buffer > bytes_read ? bytes_read : space_in_frame_buffer;
253             handle_received_sbc_data(&state, read_buffer, bytes_to_append, wav_file);
254             memmove(read_buffer, read_buffer + bytes_to_append, sizeof(read_buffer) - bytes_to_append);
255             bytes_read -= bytes_to_append;
256         }
257     }
258 
259     rewind(wav_file);
260     write_wav_header(wav_file, &context, state.frame_count);
261 
262     fclose(wav_file);
263     printf("Write %d frames to wav file: %s\n", state.frame_count, wav_filename);
264 
265     close(fd);
266 }
267