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 uint8_t read_buffer[118]; 61 static int total_num_samples = 0; 62 static int frame_count = 0; 63 static int wav_writer_opened = 0; 64 static char wav_filename[1000]; 65 66 static void show_usage(void){ 67 printf("\n\nUsage: ./sbc_decoder_test input_file [0-sbc|1-msbc] plc_enabled corrupt_frame_period \n\n"); 68 } 69 70 static ssize_t __read(int fd, void *buf, size_t count){ 71 ssize_t len, pos = 0; 72 73 while (count > 0) { 74 len = read(fd, (int8_t * )buf + pos, count); 75 if (len <= 0) 76 return pos; 77 78 count -= len; 79 pos += len; 80 } 81 return pos; 82 } 83 84 static void handle_pcm_data(int16_t * data, int num_samples, int num_channels, int sample_rate, void * context){ 85 if (!wav_writer_opened){ 86 wav_writer_opened = 1; 87 wav_writer_open(wav_filename, num_channels, sample_rate); 88 89 } 90 91 printf("Sampels: num_samples %u, num_channels %u, sample_rate %u\n", num_samples, num_channels, sample_rate); 92 // printf_hexdump(data, num_samples * num_channels * 2); 93 int i; 94 for (i=0;i<num_channels*num_samples;i += 2){ 95 if ((i%24) == 0) printf("\n"); 96 printf ("%12d ", data[i]); 97 } 98 printf("\n"); 99 wav_writer_write_int16(num_samples*num_channels, data); 100 101 total_num_samples+=num_samples*num_channels; 102 frame_count++; 103 } 104 105 int main (int argc, const char * argv[]){ 106 if (argc < 5){ 107 show_usage(); 108 return -1; 109 } 110 111 char sbc_filename[1000]; 112 int argv_pos = 1; 113 const char * filename = argv[argv_pos++]; 114 115 btstack_sbc_mode_t mode = atoi(argv[argv_pos++]) == 0? SBC_MODE_STANDARD : SBC_MODE_mSBC; 116 const int plc_enabled = atoi(argv[argv_pos++]); 117 const int corrupt_frame_period = atoi(argv[argv_pos++]); 118 119 strcpy(sbc_filename, filename); 120 strcpy(wav_filename, filename); 121 122 printf("\n"); 123 if (mode == SBC_MODE_mSBC){ 124 printf("Using SBC_MODE_mSBC mode.\n"); 125 strcat(sbc_filename, ".msbc"); 126 } else { 127 printf("Using SBC_MODE_STANDARD mode.\n"); 128 strcat(sbc_filename, ".sbc"); 129 } 130 131 if (plc_enabled){ 132 printf("PLC enabled.\n"); 133 strcat(wav_filename, "_decoded_bludroid_plc.wav"); 134 } else { 135 printf("PLC disbled.\n"); 136 strcat(wav_filename, "_decoded_bludroid.wav"); 137 } 138 139 if (corrupt_frame_period > 0){ 140 printf("Corrupt frame period: every %d frames.\n", corrupt_frame_period); 141 } 142 143 144 int fd = open(sbc_filename, O_RDONLY); 145 if (fd < 0) { 146 printf("Can't open file %s", sbc_filename); 147 return -1; 148 } 149 printf("Open sbc file: %s\n", sbc_filename); 150 151 152 btstack_sbc_decoder_state_t state; 153 btstack_sbc_decoder_init(&state, mode, &handle_pcm_data, NULL); 154 155 //if (!plc_enabled){ 156 btstack_sbc_decoder_test_disable_plc(); 157 //} 158 if (corrupt_frame_period > 0){ 159 btstack_sbc_decoder_test_simulate_corrupt_frames(corrupt_frame_period); 160 } 161 162 while (1){ 163 // get next chunk 164 int bytes_read = __read(fd, read_buffer, sizeof(read_buffer)); 165 if (0 >= bytes_read) break; 166 // process chunk 167 btstack_sbc_decoder_process_data(&state, 0, read_buffer, bytes_read); 168 } 169 170 wav_writer_close(); 171 close(fd); 172 int total_frames_nr = state.good_frames_nr + state.bad_frames_nr + state.zero_frames_nr; 173 174 printf("\nDecoding done. Processed totaly %d frames:\n - %d good\n - %d bad\n - %d zero frames\n", total_frames_nr, state.good_frames_nr, state.bad_frames_nr, state.zero_frames_nr); 175 printf("Write %d frames to wav file: %s\n\n", frame_count, wav_filename); 176 177 } 178