xref: /btstack/test/sbc/pklg_msbc_test.c (revision d28a35d9559e84fd650e64c168103b610844f8eb)
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 char wav_filename[1000];
61 static char pklg_filename[1000];
62 
63 static int wav_writer_opened;
64 static int total_num_samples = 0;
65 static int frame_count = 0;
66 
67 
68 static void show_usage(void){
69     printf("\n\nUsage: ./pklg_msbc_test input_file \n");
70     printf("Example: ./pklg_msbc_test pklg/test1 \n");
71 }
72 
73 static ssize_t __read(int fd, void *buf, size_t count){
74     ssize_t len, pos = 0;
75 
76     while (count > 0) {
77         len = read(fd, (int8_t * )buf + pos, count);
78         if (len <= 0)
79             return pos;
80 
81         count -= len;
82         pos   += len;
83     }
84     return pos;
85 }
86 
87 
88 static void handle_pcm_data(int16_t * data, int num_samples, int num_channels, int sample_rate, void * context){
89     (void) context;
90     if (!wav_writer_opened){
91         wav_writer_opened = 1;
92         wav_writer_open(wav_filename, num_channels, sample_rate);
93 
94     }
95 
96     // printf("Sampels: num_samples %u, num_channels %u, sample_rate %u\n", num_samples, num_channels, sample_rate);
97     // printf_hexdump(data, num_samples * num_channels * 2);
98     // int i;
99     // for (i=0;i<num_channels*num_samples;i += 2){
100     //     if ((i%24) == 0) printf("\n");
101     //     printf ("%12d ", data[i]);
102     // }
103     // printf("\n");
104     wav_writer_write_int16(num_samples*num_channels, data);
105 
106     total_num_samples+=num_samples*num_channels;
107     frame_count++;
108 }
109 
110 int main (int argc, const char * argv[]){
111     if (argc < 2){
112         show_usage();
113         return -1;
114     }
115     int argv_pos = 1;
116     const char * filename = argv[argv_pos++];
117 
118 #ifdef OCTAVE_OUTPUT
119     printf("defined OCTAVE OUTPUT\n");
120     octave_set_base_name(filename);
121 #endif
122 
123     btstack_sbc_mode_t mode = SBC_MODE_mSBC;
124 
125     strcpy(pklg_filename, filename);
126     strcat(pklg_filename, ".pklg");
127 
128     strcpy(wav_filename, filename);
129     strcat(wav_filename, ".wav");
130 
131 
132     int fd = open(pklg_filename, O_RDONLY);
133     if (fd < 0) {
134         printf("Can't open file %s", pklg_filename);
135         return -1;
136     }
137     printf("Open pklg file: %s\n", pklg_filename);
138 
139 
140     btstack_sbc_decoder_state_t state;
141     btstack_sbc_decoder_init(&state, mode, &handle_pcm_data, NULL);
142 
143     // btstack_sbc_decoder_test_disable_plc();
144 
145     int sco_packet_counter = 0;
146     while (1){
147         int bytes_read;
148         // get next packet
149         uint8_t header[13];
150         bytes_read = __read(fd, header, sizeof(header));
151         if (0 >= bytes_read) break;
152 
153         uint8_t packet[256];
154         uint32_t size = big_endian_read_32(header, 0) - 9;
155         bytes_read = __read(fd, packet, size);
156 
157         uint8_t type = header[12];
158         if (type != 9) continue;
159         sco_packet_counter++;
160 
161         // printf("Packet %4u, flags %x: ", sco_packet_counter, (packet[1] >> 4));
162         // printf_hexdump(packet+3, size-3);
163         btstack_sbc_decoder_process_data(&state, (packet[1] >> 4) & 3, packet+3, size-3);
164     }
165 
166     wav_writer_close();
167     close(fd);
168 
169     int good = state.good_frames_nr;
170     int bad  = state.bad_frames_nr + state.zero_frames_nr;
171     int consecutive;
172     int total_frames_nr = good + bad;
173 
174     printf("WAV Writer (SBC): Decoding done. Processed totaly %d frames:\n - %d good\n - %d bad\n", total_frames_nr, good, bad);
175 
176     good = state.plc_state.good_frames_nr;
177     bad =  state.plc_state.bad_frames_nr;
178     consecutive = state.plc_state.max_consecutive_bad_frames_nr;
179     total_frames_nr = good + bad;
180     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);
181 
182     printf("Write %d frames to wav file: %s\n\n", frame_count, wav_filename);
183 }
184