xref: /btstack/test/sbc/pklg_msbc_test.c (revision d1550f906a6f7312c10521c9150f12effd00dedc)
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 fd = open(pklg_path, O_RDONLY);
99     if (fd < 0) {
100         printf("Can't open file %s", pklg_path);
101         return;
102     }
103 
104     wav_writer_open(wav_path, 1, 16000);
105 
106     total_num_samples = 0;
107     frame_count       = 0;
108 
109     btstack_sbc_mode_t mode = SBC_MODE_mSBC;
110 
111     btstack_sbc_decoder_state_t state;
112     btstack_sbc_decoder_init(&state, mode, &handle_pcm_data, NULL);
113 
114     // btstack_sbc_decoder_test_set_plc_enabled(plc_enabled);
115 
116     int sco_packet_counter = 0;
117     while (1){
118         int bytes_read;
119         // get next packet
120         uint8_t header[13];
121         bytes_read = __read(fd, header, sizeof(header));
122         if (0 >= bytes_read) break;
123 
124         uint8_t packet[256];
125         uint32_t size = big_endian_read_32(header, 0) - 9;
126         bytes_read = __read(fd, packet, size);
127 
128         uint8_t type = header[12];
129 
130         if (type != packet_type) continue;
131 
132         sco_packet_counter++;
133 
134         btstack_sbc_decoder_process_data(&state, (packet[1] >> 4) & 3, packet+3, size-3);
135     }
136 
137     wav_writer_close();
138     close(fd);
139 
140     int good = state.good_frames_nr;
141     int bad  = state.bad_frames_nr + state.zero_frames_nr;
142     int consecutive;
143     int total_frames_nr = good + bad;
144 
145     printf("WAV Writer (SBC): Decoding done. Processed totaly %d frames:\n - %d good\n - %d bad\n", total_frames_nr, good, bad);
146 
147     good = state.plc_state.good_frames_nr;
148     bad =  state.plc_state.bad_frames_nr;
149     consecutive = state.plc_state.max_consecutive_bad_frames_nr;
150     total_frames_nr = good + bad;
151     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);
152 
153     printf("Write %d frames to wav file: %s\n\n", frame_count, wav_path);
154 }
155 
156 
157 int main (int argc, const char * argv[]){
158 
159     char wav_path[1000];
160     char pklg_path[1000];
161 
162     if (argc < 2){
163         show_usage();
164         return -1;
165     }
166 
167     int argv_pos = 1;
168     const char * filename = argv[argv_pos++];
169 
170 #ifdef OCTAVE_OUTPUT
171     printf("OCTAVE OUTPUT active\n");
172     btstack_sbc_plc_octave_set_base_name(filename);
173 #endif
174 
175     strcpy(pklg_path, filename);
176     strcat(pklg_path, ".pklg");
177 
178     // in file, no plc
179     strcpy(wav_path, filename);
180     strcat(wav_path, "_in_raw.wav");
181     process_file(pklg_path, wav_path, PAKET_TYPE_SCO_IN, 0);
182 
183     // in file, plc
184     strcpy(wav_path, filename);
185     strcat(wav_path, "_in_plc.wav");
186     process_file(pklg_path, wav_path, PAKET_TYPE_SCO_IN, 1);
187 
188     // out file, no plc
189     strcpy(wav_path, filename);
190     strcat(wav_path, "_out.wav");
191     process_file(pklg_path, wav_path, PAKET_TYPE_SCO_OUT, 0);
192 }
193