xref: /btstack/test/sbc/sbc_decoder_test.c (revision 4f0d422d954afb3f75abc1476b57adb4fd2cc605)
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 
53 #include "oi_assert.h"
54 
55 #include "btstack.h"
56 #include "sbc_decoder.h"
57 
58 static uint8_t read_buffer[24];
59 
60 typedef struct wav_writer_state {
61     FILE * wav_file;
62     int total_num_samples;
63     int frame_count;
64 } wav_writer_state_t;
65 
66 static void show_usage(void){
67     printf("\n\nUsage: ./sbc_decoder_test input_file msbc|sbc plc_enabled(0|1) corrupt_frame_period\n\n");
68 }
69 
70 static ssize_t __read(int fd, void *buf, size_t count){
71     ssize_t len, pos = 0;
72 
73     while (count > 0) {
74         len = read(fd, buf + pos, count);
75         if (len <= 0)
76             return pos;
77 
78         count -= len;
79         pos   += len;
80     }
81     return pos;
82 }
83 
84 void little_endian_fstore_16(FILE *wav_file, uint16_t value){
85     uint8_t buf[2];
86     little_endian_store_32(buf, 0, value);
87     fwrite(&buf, 1, 2, wav_file);
88 }
89 
90 void little_endian_fstore_32(FILE *wav_file, uint32_t value){
91     uint8_t buf[4];
92     little_endian_store_32(buf, 0, value);
93     fwrite(&buf, 1, 4, wav_file);
94 }
95 
96 
97 static void write_wav_header(FILE * wav_file,  int total_num_samples, int num_channels, int sample_rate){
98     unsigned int bytes_per_sample = 2;
99     /* write RIFF header */
100     fwrite("RIFF", 1, 4, wav_file);
101     // num_samples = blocks * subbands
102     uint32_t data_bytes = (uint32_t) (bytes_per_sample * total_num_samples * num_channels);
103     little_endian_fstore_32(wav_file, data_bytes + 36);
104     fwrite("WAVE", 1, 4, wav_file);
105 
106     int byte_rate = sample_rate * num_channels * bytes_per_sample;
107     int bits_per_sample = 8 * bytes_per_sample;
108     int block_align = num_channels * bits_per_sample;
109     int fmt_length = 16;
110     int fmt_format_tag = 1; // PCM
111 
112     /* write fmt chunk */
113     fwrite("fmt ", 1, 4, wav_file);
114     little_endian_fstore_32(wav_file, fmt_length);
115     little_endian_fstore_16(wav_file, fmt_format_tag);
116     little_endian_fstore_16(wav_file, num_channels);
117     little_endian_fstore_32(wav_file, sample_rate);
118     little_endian_fstore_32(wav_file, byte_rate);
119     little_endian_fstore_16(wav_file, block_align);
120     little_endian_fstore_16(wav_file, bits_per_sample);
121 
122     /* write data chunk */
123     fwrite("data", 1, 4, wav_file);
124     little_endian_fstore_32(wav_file, data_bytes);
125 }
126 
127 static void write_wav_data(FILE * wav_file, int num_samples, int num_channels, int16_t * data){
128     fwrite(data, num_samples, 2, wav_file);
129 }
130 
131 static void handle_pcm_data(int16_t * data, int num_samples, int num_channels, int sample_rate, void * context){
132     wav_writer_state_t * wav_writer_state = (wav_writer_state_t*) context;
133     write_wav_data(wav_writer_state->wav_file, num_samples, num_channels, data);
134     wav_writer_state->total_num_samples+=num_samples;
135     wav_writer_state->frame_count++;
136 }
137 
138 int main (int argc, const char * argv[]){
139     if (argc < 4){
140         show_usage();
141         return -1;
142     }
143 
144     sbc_mode_t mode = SBC_MODE_STANDARD;
145 
146     const char * filename = argv[1];
147 
148     char sbc_filename[30];
149     char wav_filename[30];
150 
151     strcpy(sbc_filename, filename);
152     strcpy(wav_filename, filename);
153 
154     const int plc_enabled = atoi(argv[3]);
155     const int corrupt_frame_period = atoi(argv[4]);
156 
157     printf("\n");
158     if (strncmp(argv[2], "msbc", 4) == 0 ){
159         mode = SBC_MODE_mSBC;
160         printf("Using SBC_MODE_mSBC mode.\n");
161     } else {
162         printf("Using SBC_MODE_STANDARD mode.\n");
163     }
164 
165     if (plc_enabled){
166         printf("PLC enabled.\n");
167     } else {
168         printf("PLC disbled.\n");
169     }
170 
171     if (corrupt_frame_period > 0){
172         printf("Corrupt frame period: every %d frames.\n", corrupt_frame_period);
173     }
174     if (mode == SBC_MODE_mSBC){
175         strcat(sbc_filename, ".msbc");
176     } else {
177         strcat(sbc_filename, ".sbc");
178     }
179 
180     if (plc_enabled){
181         strcat(wav_filename, "_plc.wav");
182     } else {
183         strcat(wav_filename, ".wav");
184     }
185 
186     int fd = open(sbc_filename, O_RDONLY);
187     if (fd < 0) {
188         printf("Can't open file %s", sbc_filename);
189         return -1;
190     }
191     printf("Open sbc file: %s\n", sbc_filename);
192     FILE * wav_file = fopen(wav_filename, "wb");
193     wav_writer_state_t wav_writer_state;
194     wav_writer_state.wav_file = wav_file;
195     wav_writer_state.frame_count = 0;
196     wav_writer_state.total_num_samples = 0;
197 
198     sbc_decoder_state_t state;
199     sbc_decoder_init(&state, mode, &handle_pcm_data, (void*)&wav_writer_state);
200     if (!plc_enabled){
201         sbc_decoder_test_disable_plc();
202     }
203     if (corrupt_frame_period > 0){
204         sbc_decoder_test_simulate_corrupt_frames(corrupt_frame_period);
205     }
206 
207     write_wav_header(wav_writer_state.wav_file, 0, 0, 0);
208 
209     while (1){
210         // get next chunk
211         int bytes_read = __read(fd, read_buffer, sizeof(read_buffer));
212         if (0 >= bytes_read) break;
213         // process chunk
214         sbc_decoder_process_data(&state, read_buffer, bytes_read);
215     }
216 
217     rewind(wav_file);
218     write_wav_header(wav_writer_state.wav_file, wav_writer_state.total_num_samples, sbc_decoder_num_channels(&state), sbc_decoder_sample_rate(&state));
219 
220     fclose(wav_file);
221     close(fd);
222     int total_frames_nr = state.good_frames_nr + state.bad_frames_nr + state.zero_frames_nr;
223 
224     printf("\nDecoding done. Processed totaly %d frames:\n - %d good\n - %d bad\n - %d zero frames\n", total_frames_nr, state.good_frames_nr, state.bad_frames_nr, state.zero_frames_nr);
225     printf("Write %d frames to wav file: %s\n\n", wav_writer_state.frame_count, wav_filename);
226 
227 }
228