xref: /btstack/test/sbc/pklg_msbc_test.c (revision 88949f844376f979826afad7a30727a235ee4f0c)
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 
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 
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 
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 
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 
128         uint8_t packet[256];
129         uint32_t size = big_endian_read_32(header, 0) - 9;
130         bytes_read = __read(fd, packet, size);
131 
132         uint8_t type = header[12];
133 
134         if (type != packet_type) continue;
135 
136         sco_packet_counter++;
137 
138         btstack_sbc_decoder_process_data(&state, (packet[1] >> 4) & 3, packet+3, size-3);
139     }
140 
141     wav_writer_close();
142     close(fd);
143 
144     int good = state.good_frames_nr;
145     int bad  = state.bad_frames_nr + state.zero_frames_nr;
146     int consecutive;
147     int total_frames_nr = good + bad;
148 
149     printf("WAV Writer (SBC): Decoding done. Processed totaly %d frames:\n - %d good\n - %d bad\n", total_frames_nr, good, bad);
150 
151     good = state.plc_state.good_frames_nr;
152     bad =  state.plc_state.bad_frames_nr;
153     consecutive = state.plc_state.max_consecutive_bad_frames_nr;
154     total_frames_nr = good + bad;
155     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);
156 
157     printf("Write %d frames to wav file: %s\n\n", frame_count, wav_path);
158 }
159 
160 
161 int main (int argc, const char * argv[]){
162 
163     char wav_path[1000];
164     char pklg_path[1000];
165 
166     if (argc < 2){
167         show_usage();
168         return -1;
169     }
170 
171     int argv_pos = 1;
172     const char * filename = argv[argv_pos++];
173 
174 #ifdef OCTAVE_OUTPUT
175     printf("OCTAVE OUTPUT active\n");
176     btstack_sbc_plc_octave_set_base_name(filename);
177 #endif
178 
179     strcpy(pklg_path, filename);
180     strcat(pklg_path, ".pklg");
181 
182     // in file, no plc
183     strcpy(wav_path, filename);
184     strcat(wav_path, "_in_raw.wav");
185     process_file(pklg_path, wav_path, PAKET_TYPE_SCO_IN, 0);
186 
187     // in file, plc
188     strcpy(wav_path, filename);
189     strcat(wav_path, "_in_plc.wav");
190     process_file(pklg_path, wav_path, PAKET_TYPE_SCO_IN, 1);
191 
192     // out file, no plc
193     strcpy(wav_path, filename);
194     strcat(wav_path, "_out.wav");
195     process_file(pklg_path, wav_path, PAKET_TYPE_SCO_OUT, 0);
196 }
197