xref: /btstack/test/sbc/sbc_decoder_test.c (revision 082d19a82bdca3cf4084154e3f0fa0d7acfa770a)
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 wav_writer_state {
66     FILE * wav_file;
67     int sample_rate;
68     int total_num_samples;
69 } wav_writer_state_t;
70 
71 typedef struct {
72     OI_UINT32 bytesRead;
73     OI_UINT32 frameBytes;
74     OI_UINT32 inputBufferBytes;
75     const OI_BYTE *frameData;
76     void * context;
77     void (*handle_pcm_data)(int16_t * data, int num_samples, int num_channels, int sample_rate, void * context);
78 } sbc_state_t;
79 static sbc_state_t state;
80 
81 
82 void OI_AssertFail(char* file, int line, char* reason){
83     printf("AssertFail file %s, line %d, reason %s\n", file, line, reason);
84 }
85 
86 static void show_usage(void){
87     printf("Usage: ./sbc_decoder_test input.sbc");
88 }
89 
90 static ssize_t __read(int fd, void *buf, size_t count){
91     ssize_t len, pos = 0;
92 
93     while (count > 0) {
94         len = read(fd, buf + pos, count);
95         if (len <= 0)
96             return pos;
97 
98         count -= len;
99         pos   += len;
100     }
101     return pos;
102 }
103 
104 void little_endian_store_16(uint8_t *buffer, uint16_t pos, uint16_t value){
105     buffer[pos++] = value;
106     buffer[pos++] = value >> 8;
107 }
108 
109 void little_endian_store_32(uint8_t *buffer, uint16_t pos, uint32_t value){
110     buffer[pos++] = value;
111     buffer[pos++] = value >> 8;
112     buffer[pos++] = value >> 16;
113     buffer[pos++] = value >> 24;
114 }
115 
116 void little_endian_fstore_16(FILE *wav_file, uint16_t value){
117     little_endian_store_32(buf, 0, value);
118     fwrite(&buf, 1, 2, wav_file);
119 }
120 
121 void little_endian_fstore_32(FILE *wav_file, uint32_t value){
122     little_endian_store_32(buf, 0, value);
123     fwrite(&buf, 1, 4, wav_file);
124 }
125 
126 
127 static void write_wav_header(FILE * wav_file,  int total_num_samples, int num_channels, int sample_rate){
128     unsigned int bytes_per_sample = 2;
129     /* write RIFF header */
130     fwrite("RIFF", 1, 4, wav_file);
131     // num_samples = blocks * subbands
132     uint32_t data_bytes = (uint32_t) (bytes_per_sample * total_num_samples * num_channels);
133     little_endian_fstore_32(wav_file, data_bytes + 36);
134     fwrite("WAVE", 1, 4, wav_file);
135 
136     int byte_rate = sample_rate * num_channels * bytes_per_sample;
137     int bits_per_sample = 8 * bytes_per_sample;
138     int block_align = num_channels * bits_per_sample;
139     int fmt_length = 16;
140     int fmt_format_tag = 1; // PCM
141 
142     /* write fmt chunk */
143     fwrite("fmt ", 1, 4, wav_file);
144     little_endian_fstore_32(wav_file, fmt_length);
145     little_endian_fstore_16(wav_file, fmt_format_tag);
146     little_endian_fstore_16(wav_file, num_channels);
147     little_endian_fstore_32(wav_file, sample_rate);
148     little_endian_fstore_32(wav_file, byte_rate);
149     little_endian_fstore_16(wav_file, block_align);
150     little_endian_fstore_16(wav_file, bits_per_sample);
151 
152     /* write data chunk */
153     fwrite("data", 1, 4, wav_file);
154     little_endian_fstore_32(wav_file, data_bytes);
155 }
156 
157 static void write_wav_data(FILE * wav_file, int num_samples, int num_channels, int16_t * data){
158     int i;
159     for (i=0; i < num_samples; i++){
160         little_endian_fstore_16(wav_file, (uint16_t)data[i]);
161         if (num_channels == 2){
162             little_endian_fstore_16(wav_file, (uint16_t)data);
163         }
164     }
165 }
166 
167 static void handle_pcm_data(int16_t * data, int num_samples, int num_channels, int sample_rate, void * context){
168     wav_writer_state_t * wav_writer_state = (wav_writer_state_t*) context;
169     write_wav_data(wav_writer_state->wav_file, num_samples, num_channels, data);
170     wav_writer_state->total_num_samples+=num_samples;
171 }
172 
173 static void init_sbc_state(sbc_state_t * state, void (*callback)(int16_t * data, int num_samples, int num_channels, int sample_rate, void * context), void * context){
174     state->frameBytes = 0;
175     state->bytesRead = 0;
176     state->inputBufferBytes = 0;
177     state->frameData = NULL;
178     state->handle_pcm_data = callback;
179     state->context = context;
180 }
181 
182 static void append_received_sbc_data(sbc_state_t * state, uint8_t * buffer, int size){
183     int numFreeBytes = sizeof(frame_buffer) - state->frameBytes;
184 
185     if (size > numFreeBytes){
186         printf("sbc data: more bytes read %u than free bytes in buffer %u", size, numFreeBytes);
187         exit(10);
188     }
189 
190     memcpy(frame_buffer + state->frameBytes, buffer, size);
191     state->frameBytes += size;
192     state->bytesRead = state->frameBytes;
193     state->frameData = frame_buffer;
194 }
195 
196 // assumption: new data fits into buffer
197 static void handle_received_sbc_data(sbc_state_t * state, uint8_t * buffer, int size, FILE * wav_file){
198     append_received_sbc_data(state, buffer, size);
199     while (1){
200 
201         OI_STATUS status = OI_CODEC_SBC_DecodeFrame(&context, &(state->frameData), &(state->frameBytes), pcmData, &pcmBytes);
202 
203         if (status != 0){
204             if (status != OI_CODEC_SBC_NOT_ENOUGH_HEADER_DATA && status != OI_CODEC_SBC_NOT_ENOUGH_BODY_DATA){
205                 OI_CODEC_SBC_DumpConfig(&(context. common.frameInfo));
206                 printf("Frame decode error %d\n", status);
207                 break;
208             }
209             // printf("Not enough data, read next %u bytes, move %d bytes\n", state->bytesRead-state->frameBytes, state->frameBytes);
210             memmove(frame_buffer, frame_buffer + state->bytesRead - state->frameBytes, state->frameBytes);
211             break;
212         }
213 
214         int num_samples = context.common.frameInfo.nrof_blocks * context.common.frameInfo.nrof_subbands;
215         int num_channels = context.common.frameInfo.nrof_channels;
216         int sample_rate = context.common.frameInfo.frequency;
217 
218         state->handle_pcm_data(pcmData, num_samples, num_channels, sample_rate, state->context);
219     }
220 }
221 
222 
223 int main (int argc, const char * argv[]){
224     if (argc < 2){
225         show_usage();
226         return -1;
227     }
228 
229     const char * sbc_filename = argv[1];
230     const char * wav_filename = argv[2];
231 
232 
233     int fd = open(sbc_filename, O_RDONLY);
234     if (fd < 0) {
235         printf("Can't open file %s", sbc_filename);
236         return -1;
237     }
238     printf("Open sbc file: %s\n", sbc_filename);
239 
240     //OI_STATUS status = OI_CODEC_SBC_DecoderReset(&context, decoderData, sizeof(decoderData), 1, 1, FALSE);
241 
242     OI_STATUS status = OI_CODEC_mSBC_DecoderReset(&context, decoderData, sizeof(decoderData));
243     if (status != 0){
244         printf("Reset decoder error %d\n", status);
245         return -1;
246     }
247 
248     FILE * wav_file = fopen(wav_filename, "wb");
249     wav_writer_state_t wav_writer_state;
250     wav_writer_state.wav_file = wav_file;
251     init_sbc_state(&state, &handle_pcm_data, (void*)&wav_writer_state);
252     write_wav_header(wav_writer_state.wav_file, 0, 0, 0);
253 
254     while (1){
255 
256         int bytes_read = __read(fd, read_buffer, sizeof(read_buffer));
257         if (0 >= bytes_read) break;
258 
259         while (bytes_read > 0){
260             int space_in_frame_buffer = sizeof(frame_buffer) - state.frameBytes;
261             int bytes_to_append = space_in_frame_buffer > bytes_read ? bytes_read : space_in_frame_buffer;
262             handle_received_sbc_data(&state, read_buffer, bytes_to_append, wav_file);
263             memmove(read_buffer, read_buffer + bytes_to_append, sizeof(read_buffer) - bytes_to_append);
264             bytes_read -= bytes_to_append;
265         }
266     }
267 
268     rewind(wav_file);
269     int num_channels = context.common.frameInfo.nrof_channels;
270     int sample_rate = context.common.frameInfo.frequency;
271     write_wav_header(wav_writer_state.wav_file, wav_writer_state.total_num_samples, num_channels, sample_rate);
272 
273     fclose(wav_file);
274     close(fd);
275 
276     int frame_count = wav_writer_state.total_num_samples/(context.common.frameInfo.nrof_blocks * context.common.frameInfo.nrof_subbands);
277     printf("Write %d frames to wav file: %s\n", frame_count, wav_filename);
278 
279 }
280