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