btstack/test/sbc/sbc_decoder_test.c

178 lines
5.7 KiB
C
Raw Normal View History

2016-06-10 08:40:10 +00:00
/*
* Copyright (C) 2014 BlueKitchen GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* 4. Any redistribution, use, or modification is done solely for
* personal benefit and not for any commercial purpose or for
* monetary gain.
*
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Please inquire about commercial licensing options at
* contact@bluekitchen-gmbh.com
*
*/
// *****************************************************************************
//
// SBC decoder tests
//
// *****************************************************************************
#include "btstack_config.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
2016-07-14 09:21:39 +00:00
2016-06-10 08:40:10 +00:00
#include "oi_assert.h"
2016-07-01 12:56:16 +00:00
#include "btstack.h"
2016-07-27 09:03:55 +00:00
#include "btstack_sbc.h"
2016-06-10 08:40:10 +00:00
2016-09-19 13:02:59 +00:00
#include "wav_util.h"
2016-10-05 14:32:01 +00:00
static uint8_t read_buffer[118];
2016-09-19 13:02:59 +00:00
static int total_num_samples = 0;
static int frame_count = 0;
2016-10-05 14:32:01 +00:00
static int wav_writer_opened = 0;
static char wav_filename[1000];
2016-06-10 08:40:10 +00:00
static void show_usage(void){
printf("\n\nUsage: ./sbc_decoder_test input_file [0-sbc|1-msbc] plc_enabled corrupt_frame_period \n\n");
2016-06-10 08:40:10 +00:00
}
static ssize_t __read(int fd, void *buf, size_t count){
ssize_t len, pos = 0;
while (count > 0) {
2016-09-19 13:02:59 +00:00
len = read(fd, (int8_t * )buf + pos, count);
2016-06-10 08:40:10 +00:00
if (len <= 0)
return pos;
count -= len;
pos += len;
}
return pos;
}
static void handle_pcm_data(int16_t * data, int num_samples, int num_channels, int sample_rate, void * context){
2016-10-05 14:32:01 +00:00
if (!wav_writer_opened){
wav_writer_opened = 1;
wav_writer_open(wav_filename, num_channels, sample_rate);
2016-10-12 14:53:34 +00:00
2016-10-05 14:32:01 +00:00
}
2016-10-12 14:53:34 +00:00
printf("Sampels: num_samples %u, num_channels %u, sample_rate %u\n", num_samples, num_channels, sample_rate);
// printf_hexdump(data, num_samples * num_channels * 2);
int i;
for (i=0;i<num_channels*num_samples;i += 2){
if ((i%24) == 0) printf("\n");
printf ("%12d ", data[i]);
}
printf("\n");
2016-10-05 14:32:01 +00:00
wav_writer_write_int16(num_samples*num_channels, data);
total_num_samples+=num_samples*num_channels;
2016-09-19 13:02:59 +00:00
frame_count++;
}
2016-06-10 08:40:10 +00:00
int main (int argc, const char * argv[]){
2016-10-05 14:32:01 +00:00
if (argc < 5){
2016-06-10 08:40:10 +00:00
show_usage();
return -1;
}
char sbc_filename[1000];
2016-10-05 14:32:01 +00:00
int argv_pos = 1;
const char * filename = argv[argv_pos++];
btstack_sbc_mode_t mode = atoi(argv[argv_pos++]) == 0? SBC_MODE_STANDARD : SBC_MODE_mSBC;
const int plc_enabled = atoi(argv[argv_pos++]);
const int corrupt_frame_period = atoi(argv[argv_pos++]);
strcpy(sbc_filename, filename);
strcpy(wav_filename, filename);
2016-06-10 08:40:10 +00:00
2016-07-15 15:31:41 +00:00
printf("\n");
2016-10-05 14:32:01 +00:00
if (mode == SBC_MODE_mSBC){
2016-07-15 15:31:41 +00:00
printf("Using SBC_MODE_mSBC mode.\n");
2016-10-05 14:32:01 +00:00
strcat(sbc_filename, ".msbc");
} else {
2016-07-15 15:31:41 +00:00
printf("Using SBC_MODE_STANDARD mode.\n");
2016-10-05 14:32:01 +00:00
strcat(sbc_filename, ".sbc");
}
2016-07-15 15:31:41 +00:00
if (plc_enabled){
printf("PLC enabled.\n");
2016-10-12 14:53:34 +00:00
strcat(wav_filename, "_decoded_bludroid_plc.wav");
2016-07-15 15:31:41 +00:00
} else {
printf("PLC disbled.\n");
2016-10-12 14:53:34 +00:00
strcat(wav_filename, "_decoded_bludroid.wav");
2016-07-15 15:31:41 +00:00
}
2016-07-15 15:31:41 +00:00
if (corrupt_frame_period > 0){
printf("Corrupt frame period: every %d frames.\n", corrupt_frame_period);
}
2016-10-05 14:32:01 +00:00
2016-06-10 08:40:10 +00:00
int fd = open(sbc_filename, O_RDONLY);
if (fd < 0) {
printf("Can't open file %s", sbc_filename);
return -1;
}
printf("Open sbc file: %s\n", sbc_filename);
2016-09-19 13:02:59 +00:00
2016-07-27 08:18:15 +00:00
btstack_sbc_decoder_state_t state;
2016-09-19 13:02:59 +00:00
btstack_sbc_decoder_init(&state, mode, &handle_pcm_data, NULL);
2016-10-05 14:32:01 +00:00
//if (!plc_enabled){
2016-07-27 08:18:15 +00:00
btstack_sbc_decoder_test_disable_plc();
2016-10-05 14:32:01 +00:00
//}
if (corrupt_frame_period > 0){
2016-07-27 08:18:15 +00:00
btstack_sbc_decoder_test_simulate_corrupt_frames(corrupt_frame_period);
}
while (1){
// get next chunk
int bytes_read = __read(fd, read_buffer, sizeof(read_buffer));
if (0 >= bytes_read) break;
// process chunk
2016-07-27 08:18:15 +00:00
btstack_sbc_decoder_process_data(&state, 0, read_buffer, bytes_read);
2016-06-10 08:40:10 +00:00
}
2016-09-19 13:02:59 +00:00
wav_writer_close();
2016-06-10 08:40:10 +00:00
close(fd);
2016-07-15 15:31:41 +00:00
int total_frames_nr = state.good_frames_nr + state.bad_frames_nr + state.zero_frames_nr;
2016-07-15 15:31:41 +00:00
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);
2016-09-19 13:02:59 +00:00
printf("Write %d frames to wav file: %s\n\n", frame_count, wav_filename);
2016-06-10 08:40:10 +00:00
}