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