xref: /btstack/test/sbc/sbc_decoder_test.c (revision 435e3c4e0d833cf29306bcbe25c84a0f767a8539)
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 
56 static uint8_t data[10000];
57 static OI_INT16 pcmData[1000];
58 static uint8_t buf[4];
59 
60 static OI_UINT32 decoderData[10000];
61 static OI_CODEC_SBC_DECODER_CONTEXT context;
62 
63 void OI_AssertFail(char* file, int line, char* reason){
64     printf("AssertFail file %s, line %d, reason %s\n", file, line, reason);
65 }
66 
67 static void show_usage(void){
68     printf("Usage: ./sbc_decoder_test input.sbc");
69 }
70 
71 static ssize_t __read(int fd, void *buf, size_t count){
72     ssize_t len, pos = 0;
73 
74     while (count > 0) {
75         len = read(fd, buf + pos, count);
76         if (len <= 0)
77             return pos;
78 
79         count -= len;
80         pos   += len;
81     }
82     return pos;
83 }
84 
85 void little_endian_store_16(uint8_t *buffer, uint16_t pos, uint16_t value){
86     buffer[pos++] = value;
87     buffer[pos++] = value >> 8;
88 }
89 
90 void little_endian_store_32(uint8_t *buffer, uint16_t pos, uint32_t value){
91     buffer[pos++] = value;
92     buffer[pos++] = value >> 8;
93     buffer[pos++] = value >> 16;
94     buffer[pos++] = value >> 24;
95 }
96 
97 void little_endian_fstore_16(FILE *wav_file, uint16_t value){
98     little_endian_store_32(buf, 0, value);
99     fwrite(&buf, 1, 2, wav_file);
100 }
101 
102 void little_endian_fstore_32(FILE *wav_file, uint32_t value){
103     little_endian_store_32(buf, 0, value);
104     fwrite(&buf, 1, 4, wav_file);
105 }
106 
107 
108 static void write_wav_header(FILE * wav_file, int num_samples, int num_channels, int sample_rate, int frame_count){
109     unsigned int bytes_per_sample = 2;
110 
111     /* write RIFF header */
112     fwrite("RIFF", 1, 4, wav_file);
113     // num_samples = blocks * subbands
114     uint32_t data_bytes = (uint32_t) (bytes_per_sample * num_samples * frame_count * num_channels);
115     little_endian_fstore_32(wav_file, data_bytes + 36);
116     fwrite("WAVE", 1, 4, wav_file);
117 
118     int byte_rate = sample_rate * num_channels * bytes_per_sample;
119     int bits_per_sample = 8 * bytes_per_sample;
120     int block_align = num_channels * bits_per_sample;
121     int fmt_length = 16;
122     int fmt_format_tag = 1; // PCM
123 
124     /* write fmt chunk */
125     fwrite("fmt ", 1, 4, wav_file);
126     little_endian_fstore_32(wav_file, fmt_length);
127     little_endian_fstore_16(wav_file, fmt_format_tag);
128     little_endian_fstore_16(wav_file, num_channels);
129     little_endian_fstore_32(wav_file, sample_rate);
130     little_endian_fstore_32(wav_file, byte_rate);
131     little_endian_fstore_16(wav_file, block_align);
132     little_endian_fstore_16(wav_file, bits_per_sample);
133 
134     /* write data chunk */
135     fwrite("data", 1, 4, wav_file);
136     little_endian_fstore_32(wav_file, data_bytes);
137 }
138 
139 static void write_wav_data(FILE * wav_file, unsigned long num_samples, unsigned int num_channels, int16_t * data){
140     int i;
141     for (i=0; i < num_samples; i++){
142         little_endian_fstore_16(wav_file, (uint16_t)data[i]);
143         if (num_channels == 2){
144             little_endian_fstore_16(wav_file, (uint16_t)data);
145         }
146     }
147 }
148 
149 
150 int main (int argc, const char * argv[]){
151     if (argc < 2){
152         show_usage();
153         return -1;
154     }
155 
156     const char * sbc_filename = argv[1];
157     const char * wav_filename = argv[2];
158 
159 
160     int fd = open(sbc_filename, O_RDONLY);
161     if (fd < 0) {
162         printf("Can't open file %s", sbc_filename);
163         return -1;
164     }
165     printf("Open sbc file: %s\n", sbc_filename);
166 
167     OI_UINT32 frameBytes = 0;
168 
169     OI_UINT32 bytesRead = __read(fd, data, sizeof(data));
170     frameBytes += bytesRead;
171     const OI_BYTE *frameData = data;
172 
173     OI_UINT32 pcmBytes = sizeof(pcmData);
174     // OI_STATUS status = OI_CODEC_SBC_DecoderReset(&context, decoderData, sizeof(decoderData), 1, 1, FALSE);
175 
176     OI_STATUS status = OI_CODEC_mSBC_DecoderReset(&context, decoderData, sizeof(decoderData));
177     if (status != 0){
178         printf("Reset decoder error %d\n", status);
179         return -1;
180     }
181     int num_samples = 0;
182     int num_channels = 0;
183     int sample_rate = 0;
184     int frame_count = 0;
185     FILE * wav_file = fopen(wav_filename, "wb");
186 
187     while (frameBytes != 0){
188         status = OI_CODEC_SBC_DecodeFrame(&context, &frameData, &frameBytes, pcmData, &pcmBytes);
189         num_samples = context.common.frameInfo.nrof_blocks * context.common.frameInfo.nrof_subbands;
190         num_channels = context.common.frameInfo.nrof_channels;
191         sample_rate = context.common.frameInfo.frequency;
192 
193         if (status != 0){
194             if (status != OI_CODEC_SBC_NOT_ENOUGH_HEADER_DATA && status != OI_CODEC_SBC_NOT_ENOUGH_BODY_DATA){
195                 printf("Frame decode error %d\n", status);
196                 break;
197             }
198             printf("Not enough data, read next %u bytes\n", bytesRead-frameBytes);
199 
200             memmove(data, data + bytesRead - frameBytes, frameBytes);
201             bytesRead = __read(fd, data + frameBytes, sizeof(data) - frameBytes);
202             frameBytes += bytesRead;
203             bytesRead = frameBytes;
204             frameData = data;
205             continue;
206         }
207 
208         if (frame_count == 0){
209             write_wav_header(wav_file, num_samples, num_channels, sample_rate, 0);
210         }
211 
212         write_wav_data(wav_file, num_samples, num_channels, pcmData);
213         frame_count++;
214     }
215     rewind(wav_file);
216     write_wav_header(wav_file, num_samples, num_channels, sample_rate, frame_count);
217 
218     fclose(wav_file);
219     printf("Write %d frames to wav file: %s\n", frame_count, wav_filename);
220 
221     close(fd);
222 }
223