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 #define PAKET_TYPE_SCO_OUT 8
61 #define PAKET_TYPE_SCO_IN 9
62
63 static int total_num_samples = 0;
64 static int frame_count = 0;
65
show_usage(void)66 static void show_usage(void){
67 printf("\n\nUsage: ./pklg_msbc_test input_file \n");
68 printf("Example: ./pklg_msbc_test pklg/test1 \n");
69 }
70
__read(int fd,void * buf,size_t count)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, (int8_t * )buf + pos, count);
76 if (len <= 0)
77 return pos;
78
79 count -= len;
80 pos += len;
81 }
82 return pos;
83 }
84
85
handle_pcm_data(int16_t * data,int num_samples,int num_channels,int sample_rate,void * context)86 static void handle_pcm_data(int16_t * data, int num_samples, int num_channels, int sample_rate, void * context){
87 (void) context;
88 (void) sample_rate;
89 wav_writer_write_int16(num_samples*num_channels, data);
90 total_num_samples += num_samples*num_channels;
91 frame_count++;
92 }
93
process_file(const char * pklg_path,const char * wav_path,int packet_type,int plc_enabled)94 static void process_file(const char * pklg_path, const char * wav_path, int packet_type, int plc_enabled){
95
96 printf("Processing %s -> %s: PLC enabled: %u, direction %s\n", pklg_path, wav_path, plc_enabled, packet_type == PAKET_TYPE_SCO_OUT ? "Out" : "In");
97
98 int oflags = O_RDONLY;
99 #ifdef _WIN32
100 oflags |= O_BINARY;
101 #endif
102 int fd = open(pklg_path, oflags);
103 if (fd < 0) {
104 printf("Can't open file %s", pklg_path);
105 return;
106 }
107
108 wav_writer_open(wav_path, 1, 16000);
109
110 total_num_samples = 0;
111 frame_count = 0;
112
113 btstack_sbc_mode_t mode = SBC_MODE_mSBC;
114
115 btstack_sbc_decoder_state_t state;
116 btstack_sbc_decoder_init(&state, mode, &handle_pcm_data, NULL);
117
118 btstack_sbc_decoder_test_set_plc_enabled(plc_enabled);
119
120 int sco_packet_counter = 0;
121 while (1){
122 int bytes_read;
123 // get next packet
124 uint8_t header[13];
125 bytes_read = __read(fd, header, sizeof(header));
126 if (0 >= bytes_read) break;
127 uint8_t type = header[12];
128
129 uint32_t size = big_endian_read_32(header, 0);
130
131 // auto-detect endianess of size param
132 if (size >0xffff){
133 size = little_endian_read_32(header, 0);
134 }
135 // subtract header
136 size -= 9;
137
138 uint8_t packet[300];
139
140 if (type == packet_type){
141 if (size > sizeof(packet) ){
142 printf("Error: size %d, packet counter %d \n", size, sco_packet_counter);
143 exit(10);
144 }
145
146 bytes_read = __read(fd, packet, size);
147 sco_packet_counter++;
148 btstack_sbc_decoder_process_data(&state, (packet[1] >> 4) & 3, packet+3, size-3);
149 } else {
150 // skip data
151 while (size){
152 int bytes_to_read = btstack_min(sizeof(packet), size);
153 __read(fd, packet, bytes_to_read);
154 size -= bytes_to_read;
155 }
156 }
157 }
158
159 wav_writer_close();
160 close(fd);
161
162 int good = state.good_frames_nr;
163 int bad = state.bad_frames_nr + state.zero_frames_nr;
164 int consecutive;
165 int total_frames_nr = good + bad;
166
167 printf("WAV Writer (SBC): Decoding done. Processed totaly %d frames:\n - %d good\n - %d bad\n", total_frames_nr, good, bad);
168
169 good = state.plc_state.good_frames_nr;
170 bad = state.plc_state.bad_frames_nr;
171 consecutive = state.plc_state.max_consecutive_bad_frames_nr;
172 total_frames_nr = good + bad;
173 printf("WAV Writer (PLC): Decoding done. Processed totaly %d frames:\n - %d good\n - %d bad\n - %d consecutive\n", total_frames_nr, good, bad, consecutive);
174
175 printf("Write %d frames to wav file: %s\n\n", frame_count, wav_path);
176 }
177
178
main(int argc,const char * argv[])179 int main (int argc, const char * argv[]){
180
181 char wav_path[1000];
182 char pklg_path[1000];
183
184 if (argc < 2){
185 show_usage();
186 return -1;
187 }
188
189 int argv_pos = 1;
190 const char * filename = argv[argv_pos++];
191
192 #ifdef OCTAVE_OUTPUT
193 printf("OCTAVE OUTPUT active\n");
194 btstack_sbc_plc_octave_set_base_name(filename);
195 #endif
196
197 btstack_strcpy(pklg_path, sizeof(pklg_path), filename);
198 btstack_strcat(pklg_path, sizeof(pklg_path), ".pklg");
199
200 // in file, no plc
201 strcpy(wav_path, filename);
202 strcat(wav_path, "_in_raw.wav");
203 process_file(pklg_path, wav_path, PAKET_TYPE_SCO_IN, 0);
204
205 // in file, plc
206 strcpy(wav_path, filename);
207 strcat(wav_path, "_in_plc.wav");
208 process_file(pklg_path, wav_path, PAKET_TYPE_SCO_IN, 1);
209
210 // out file, no plc
211 strcpy(wav_path, filename);
212 strcat(wav_path, "_out.wav");
213 process_file(pklg_path, wav_path, PAKET_TYPE_SCO_OUT, 0);
214 }
215