xref: /btstack/test/sbc/sbc_decoder_test.c (revision cc528b9d66cd63ff4662ca3aba554b3ddf559fb2)
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 "btstack_sbc.h"
57 
58 #include "wav_util.h"
59 
60 static uint8_t read_buffer[118];
61 static int total_num_samples = 0;
62 static int frame_count = 0;
63 static int wav_writer_opened = 0;
64 static char wav_filename[1000];
65 
66 static uint8_t indices0[] = { 0xad, 0x00, 0x00, 0xc5, 0x00, 0x00, 0x00, 0x00, 0x77, 0x6d,
67 0xb6, 0xdd, 0xdb, 0x6d, 0xb7, 0x76, 0xdb, 0x6d, 0xdd, 0xb6, 0xdb, 0x77, 0x6d,
68 0xb6, 0xdd, 0xdb, 0x6d, 0xb7, 0x76, 0xdb, 0x6d, 0xdd, 0xb6, 0xdb, 0x77, 0x6d,
69 0xb6, 0xdd, 0xdb, 0x6d, 0xb7, 0x76, 0xdb, 0x6d, 0xdd, 0xb6, 0xdb, 0x77, 0x6d,
70 0xb6, 0xdd, 0xdb, 0x6d, 0xb7, 0x76, 0xdb, 0x6c};
71 
72 
73 static void show_usage(void){
74     printf("\n\nUsage: ./sbc_decoder_test input_file [0-sbc|1-msbc] enable_partial_frame_corruption enable_plc corrupt_whole_frame_period \n\n");
75     printf("Example: ./sbc_decoder_test data/data/sine-stereo 0 1 0 0\n");
76     printf("Example: ./sbc_decoder_test data/data/sine-stereo 1 1 0 0\n\n");
77 }
78 
79 static ssize_t __read(int fd, void *buf, size_t count){
80     ssize_t len, pos = 0;
81 
82     while (count > 0) {
83         len = read(fd, (int8_t * )buf + pos, count);
84         if (len <= 0)
85             return pos;
86 
87         count -= len;
88         pos   += len;
89     }
90     return pos;
91 }
92 
93 static void handle_pcm_data(int16_t * data, int num_samples, int num_channels, int sample_rate, void * context){
94     UNUSED(context);
95     if (!wav_writer_opened){
96         wav_writer_opened = 1;
97         wav_writer_open(wav_filename, num_channels, sample_rate);
98 
99     }
100 
101     // printf("Sampels: num_samples %u, num_channels %u, sample_rate %u\n", num_samples, num_channels, sample_rate);
102     // printf_hexdump(data, num_samples * num_channels * 2);
103     // int i;
104     // for (i=0;i<num_channels*num_samples;i += 2){
105     //     if ((i%24) == 0) printf("\n");
106     //     printf ("%12d ", data[i]);
107     // }
108     // printf("\n");
109     wav_writer_write_int16(num_samples*num_channels, data);
110 
111     total_num_samples+=num_samples*num_channels;
112     frame_count++;
113 }
114 
115 int main (int argc, const char * argv[]){
116     if (argc < 6){
117         show_usage();
118         return -1;
119     }
120 
121     char sbc_filename[1000];
122     int argv_pos = 1;
123     const char * filename = argv[argv_pos++];
124 
125     btstack_sbc_mode_t mode = atoi(argv[argv_pos++]) == 0? SBC_MODE_STANDARD : SBC_MODE_mSBC;
126     const int partial_frame_corruption = atoi(argv[argv_pos++]);
127     const int plc_enabled = atoi(argv[argv_pos++]);
128     const int corrupt_frame_period = atoi(argv[argv_pos++]);
129 
130     static unsigned g_phase = 0;
131     static unsigned g_phase2 = 0;
132 
133     strcpy(sbc_filename, filename);
134     strcpy(wav_filename, filename);
135 
136     printf("\n");
137     if (mode == SBC_MODE_mSBC){
138         printf("Using SBC_MODE_mSBC mode.\n");
139         strcat(sbc_filename, ".msbc");
140     } else {
141         printf("Using SBC_MODE_STANDARD mode.\n");
142         strcat(sbc_filename, ".sbc");
143     }
144 
145     if (plc_enabled){
146         printf("PLC enabled.\n");
147         strcat(wav_filename, "_decoded_bludroid_plc.wav");
148     } else {
149         printf("PLC disbled.\n");
150         strcat(wav_filename, "_decoded_bludroid.wav");
151     }
152 
153     if (corrupt_frame_period > 0){
154         printf("Corrupt frame period: every %d frames.\n", corrupt_frame_period);
155     }
156 
157 
158     int fd = open(sbc_filename, O_RDONLY);
159     if (fd < 0) {
160         printf("Can't open file %s", sbc_filename);
161         return -1;
162     }
163     printf("Open sbc file: %s\n", sbc_filename);
164 
165 
166     btstack_sbc_decoder_state_t state;
167     btstack_sbc_decoder_init(&state, mode, &handle_pcm_data, NULL);
168 
169     if (!plc_enabled){
170         btstack_sbc_decoder_test_set_plc_enabled(0);
171     }
172     if (corrupt_frame_period > 0){
173         btstack_sbc_decoder_test_simulate_corrupt_frames(corrupt_frame_period);
174     }
175 
176     while (1){
177         // get next chunk
178         int bytes_read = __read(fd, read_buffer, sizeof(read_buffer));
179         if (0 >= bytes_read) break;
180 
181         if (partial_frame_corruption) {
182             // Inject errors every now and then.
183             if ((g_phase & 0x7f) >= 128-8){
184                 uint8_t * data = indices0;
185                 data[g_phase2] = 0xff;
186                 g_phase2++;
187             } else {
188                 g_phase2 = 0;
189             }
190             g_phase++;
191         }
192 
193         btstack_sbc_decoder_process_data(&state, 0, indices0, sizeof(indices0));
194     }
195 
196     wav_writer_close();
197     close(fd);
198     int total_frames_nr = state.good_frames_nr + state.bad_frames_nr + state.zero_frames_nr;
199 
200     printf("WAV Writer: Decoding done. Processed totaly %d frames:\n - %d good\n - %d bad\n", total_frames_nr, state.good_frames_nr, total_frames_nr - state.good_frames_nr);
201     printf("Write %d frames to wav file: %s\n\n", frame_count, wav_filename);
202 
203 }
204