xref: /btstack/test/sbc/sbc_decoder_test.c (revision 48e0f8060d81282cb1be454247e3afb371ff6dde)
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 "sbc_decoder.h"
53 #include "oi_assert.h"
54 
55 static uint8_t read_buffer[6000];
56 static uint8_t buf[4];
57 
58 typedef struct wav_writer_state {
59     FILE * wav_file;
60     int total_num_samples;
61     int frame_count;
62 } wav_writer_state_t;
63 
64 static void show_usage(void){
65     printf("Usage: ./sbc_decoder_test input.sbc");
66 }
67 
68 static ssize_t __read(int fd, void *buf, size_t count){
69     ssize_t len, pos = 0;
70 
71     while (count > 0) {
72         len = read(fd, buf + pos, count);
73         if (len <= 0)
74             return pos;
75 
76         count -= len;
77         pos   += len;
78     }
79     return pos;
80 }
81 
82 void little_endian_store_16(uint8_t *buffer, uint16_t pos, uint16_t value){
83     buffer[pos++] = value;
84     buffer[pos++] = value >> 8;
85 }
86 
87 void little_endian_store_32(uint8_t *buffer, uint16_t pos, uint32_t value){
88     buffer[pos++] = value;
89     buffer[pos++] = value >> 8;
90     buffer[pos++] = value >> 16;
91     buffer[pos++] = value >> 24;
92 }
93 
94 void little_endian_fstore_16(FILE *wav_file, uint16_t value){
95     little_endian_store_32(buf, 0, value);
96     fwrite(&buf, 1, 2, wav_file);
97 }
98 
99 void little_endian_fstore_32(FILE *wav_file, uint32_t value){
100     little_endian_store_32(buf, 0, value);
101     fwrite(&buf, 1, 4, wav_file);
102 }
103 
104 
105 static void write_wav_header(FILE * wav_file,  int total_num_samples, int num_channels, int sample_rate){
106     unsigned int bytes_per_sample = 2;
107     /* write RIFF header */
108     fwrite("RIFF", 1, 4, wav_file);
109     // num_samples = blocks * subbands
110     uint32_t data_bytes = (uint32_t) (bytes_per_sample * total_num_samples * num_channels);
111     little_endian_fstore_32(wav_file, data_bytes + 36);
112     fwrite("WAVE", 1, 4, wav_file);
113 
114     int byte_rate = sample_rate * num_channels * bytes_per_sample;
115     int bits_per_sample = 8 * bytes_per_sample;
116     int block_align = num_channels * bits_per_sample;
117     int fmt_length = 16;
118     int fmt_format_tag = 1; // PCM
119 
120     /* write fmt chunk */
121     fwrite("fmt ", 1, 4, wav_file);
122     little_endian_fstore_32(wav_file, fmt_length);
123     little_endian_fstore_16(wav_file, fmt_format_tag);
124     little_endian_fstore_16(wav_file, num_channels);
125     little_endian_fstore_32(wav_file, sample_rate);
126     little_endian_fstore_32(wav_file, byte_rate);
127     little_endian_fstore_16(wav_file, block_align);
128     little_endian_fstore_16(wav_file, bits_per_sample);
129 
130     /* write data chunk */
131     fwrite("data", 1, 4, wav_file);
132     little_endian_fstore_32(wav_file, data_bytes);
133 }
134 
135 static void write_wav_data(FILE * wav_file, int num_samples, int num_channels, int16_t * data){
136     int i;
137     for (i=0; i < num_samples; i++){
138         little_endian_fstore_16(wav_file, (uint16_t)data[i]);
139         if (num_channels == 2){
140             little_endian_fstore_16(wav_file, (uint16_t)data);
141         }
142     }
143 }
144 
145 static void handle_pcm_data(int16_t * data, int num_samples, int num_channels, int sample_rate, void * context){
146     wav_writer_state_t * wav_writer_state = (wav_writer_state_t*) context;
147     write_wav_data(wav_writer_state->wav_file, num_samples, num_channels, data);
148     wav_writer_state->total_num_samples+=num_samples;
149     wav_writer_state->frame_count++;
150 }
151 
152 int main (int argc, const char * argv[]){
153     if (argc < 2){
154         show_usage();
155         return -1;
156     }
157 
158     const char * sbc_filename = argv[1];
159     const char * wav_filename = argv[2];
160 
161 
162     int fd = open(sbc_filename, O_RDONLY);
163     if (fd < 0) {
164         printf("Can't open file %s", sbc_filename);
165         return -1;
166     }
167     printf("Open sbc file: %s\n", sbc_filename);
168     FILE * wav_file = fopen(wav_filename, "wb");
169     wav_writer_state_t wav_writer_state;
170     wav_writer_state.wav_file = wav_file;
171     wav_writer_state.frame_count = 0;
172     wav_writer_state.total_num_samples = 0;
173 
174     sbc_decoder_state_t state;
175     sbc_decoder_init(&state, &handle_pcm_data, (void*)&wav_writer_state);
176     write_wav_header(wav_writer_state.wav_file, 0, 0, 0);
177 
178     while (1){
179         // get next chunk
180         int bytes_read = __read(fd, read_buffer, sizeof(read_buffer));
181         if (0 >= bytes_read) break;
182         // process chunk
183         sbc_decoder_process_data(&state, read_buffer, bytes_read);
184     }
185 
186     rewind(wav_file);
187     write_wav_header(wav_writer_state.wav_file, wav_writer_state.total_num_samples, sbc_decoder_num_channels(&state), sbc_decoder_sample_rate(&state));
188 
189     fclose(wav_file);
190     close(fd);
191 
192     printf("Write %d frames to wav file: %s\n", wav_writer_state.frame_count, wav_filename);
193 
194 }
195