sbc decoder test: extract process sbc chunk

This commit is contained in:
Milanka Ringwald 2016-07-01 11:11:19 +02:00
parent 082d19a82b
commit dc2354f3d3

View File

@ -54,13 +54,13 @@
static uint8_t read_buffer[6000];
static uint8_t frame_buffer[5000];
static OI_INT16 pcmData[1000];
static OI_UINT32 pcmBytes = sizeof(pcmData);
static OI_INT16 pcm_data[1000];
static OI_UINT32 pcm_bytes = sizeof(pcm_data);
static uint8_t buf[4];
static OI_UINT32 decoderData[10000];
static OI_CODEC_SBC_DECODER_CONTEXT context;
static OI_UINT32 decoder_data[10000];
static OI_CODEC_SBC_DECODER_CONTEXT decoder_context;
typedef struct wav_writer_state {
FILE * wav_file;
@ -69,20 +69,15 @@ typedef struct wav_writer_state {
} wav_writer_state_t;
typedef struct {
OI_UINT32 bytesRead;
OI_UINT32 frameBytes;
OI_UINT32 inputBufferBytes;
const OI_BYTE *frameData;
OI_UINT32 bytes_read;
OI_UINT32 bytes_in_frame;
const OI_BYTE *frame_data;
void * context;
void (*handle_pcm_data)(int16_t * data, int num_samples, int num_channels, int sample_rate, void * context);
} sbc_state_t;
static sbc_state_t state;
void OI_AssertFail(char* file, int line, char* reason){
printf("AssertFail file %s, line %d, reason %s\n", file, line, reason);
}
static void show_usage(void){
printf("Usage: ./sbc_decoder_test input.sbc");
}
@ -170,52 +165,70 @@ static void handle_pcm_data(int16_t * data, int num_samples, int num_channels, i
wav_writer_state->total_num_samples+=num_samples;
}
static void init_sbc_state(sbc_state_t * state, void (*callback)(int16_t * data, int num_samples, int num_channels, int sample_rate, void * context), void * context){
state->frameBytes = 0;
state->bytesRead = 0;
state->inputBufferBytes = 0;
state->frameData = NULL;
state->handle_pcm_data = callback;
state->context = context;
}
static void append_received_sbc_data(sbc_state_t * state, uint8_t * buffer, int size){
int numFreeBytes = sizeof(frame_buffer) - state->frameBytes;
int numFreeBytes = sizeof(frame_buffer) - state->bytes_in_frame;
if (size > numFreeBytes){
printf("sbc data: more bytes read %u than free bytes in buffer %u", size, numFreeBytes);
exit(10);
}
memcpy(frame_buffer + state->frameBytes, buffer, size);
state->frameBytes += size;
state->bytesRead = state->frameBytes;
state->frameData = frame_buffer;
memcpy(frame_buffer + state->bytes_in_frame, buffer, size);
state->bytes_in_frame += size;
state->bytes_read = state->bytes_in_frame;
state->frame_data = frame_buffer;
}
// assumption: new data fits into buffer
static void handle_received_sbc_data(sbc_state_t * state, uint8_t * buffer, int size, FILE * wav_file){
append_received_sbc_data(state, buffer, size);
while (1){
static inline int num_samples(OI_CODEC_SBC_DECODER_CONTEXT * context){
return context->common.frameInfo.nrof_blocks * context->common.frameInfo.nrof_subbands;
}
static inline int num_channels(OI_CODEC_SBC_DECODER_CONTEXT * context){
return context->common.frameInfo.nrof_channels;
}
static inline int sample_rate(OI_CODEC_SBC_DECODER_CONTEXT * context){
return context->common.frameInfo.frequency;
}
void OI_AssertFail(char* file, int line, char* reason){
printf("AssertFail file %s, line %d, reason %s\n", file, line, reason);
}
void init_sbc_state(sbc_state_t * state, void (*callback)(int16_t * data, int num_samples, int num_channels, int sample_rate, void * context), void * context){
state->bytes_in_frame = 0;
state->bytes_read = 0;
state->frame_data = NULL;
state->handle_pcm_data = callback;
state->context = context;
}
void handle_received_sbc_chunk(sbc_state_t * state, uint8_t * buffer, int size){
int bytes_read = size;
while (bytes_read > 0){
int space_in_frame_buffer = sizeof(frame_buffer) - state->bytes_in_frame;
int bytes_to_append = space_in_frame_buffer > bytes_read ? bytes_read : space_in_frame_buffer;
append_received_sbc_data(state, buffer, bytes_to_append);
OI_STATUS status = OI_CODEC_SBC_DecodeFrame(&context, &(state->frameData), &(state->frameBytes), pcmData, &pcmBytes);
if (status != 0){
if (status != OI_CODEC_SBC_NOT_ENOUGH_HEADER_DATA && status != OI_CODEC_SBC_NOT_ENOUGH_BODY_DATA){
OI_CODEC_SBC_DumpConfig(&(context. common.frameInfo));
printf("Frame decode error %d\n", status);
while (1){
OI_STATUS status = OI_CODEC_SBC_DecodeFrame(&decoder_context, &(state->frame_data), &(state->bytes_in_frame), pcm_data, &pcm_bytes);
if (status != 0){
if (status != OI_CODEC_SBC_NOT_ENOUGH_HEADER_DATA && status != OI_CODEC_SBC_NOT_ENOUGH_BODY_DATA){
OI_CODEC_SBC_DumpConfig(&(decoder_context.common.frameInfo));
printf("Frame decode error %d\n", status);
break;
}
// printf("Not enough data, read next %u bytes, move %d bytes\n", state->bytes_read-state->bytes_in_frame, state->bytes_in_frame);
memmove(frame_buffer, frame_buffer + state->bytes_read - state->bytes_in_frame, state->bytes_in_frame);
break;
}
// printf("Not enough data, read next %u bytes, move %d bytes\n", state->bytesRead-state->frameBytes, state->frameBytes);
memmove(frame_buffer, frame_buffer + state->bytesRead - state->frameBytes, state->frameBytes);
break;
state->handle_pcm_data(pcm_data, num_samples(&decoder_context), num_channels(&decoder_context), sample_rate(&decoder_context), state->context);
}
int num_samples = context.common.frameInfo.nrof_blocks * context.common.frameInfo.nrof_subbands;
int num_channels = context.common.frameInfo.nrof_channels;
int sample_rate = context.common.frameInfo.frequency;
state->handle_pcm_data(pcmData, num_samples, num_channels, sample_rate, state->context);
memmove(buffer, buffer + bytes_to_append, size - bytes_to_append);
bytes_read -= bytes_to_append;
}
}
@ -237,9 +250,9 @@ int main (int argc, const char * argv[]){
}
printf("Open sbc file: %s\n", sbc_filename);
//OI_STATUS status = OI_CODEC_SBC_DecoderReset(&context, decoderData, sizeof(decoderData), 1, 1, FALSE);
//OI_STATUS status = OI_CODEC_SBC_DecoderReset(&context, decoder_data, sizeof(decoder_data), 1, 1, FALSE);
OI_STATUS status = OI_CODEC_mSBC_DecoderReset(&context, decoderData, sizeof(decoderData));
OI_STATUS status = OI_CODEC_mSBC_DecoderReset(&decoder_context, decoder_data, sizeof(decoder_data));
if (status != 0){
printf("Reset decoder error %d\n", status);
return -1;
@ -252,28 +265,20 @@ int main (int argc, const char * argv[]){
write_wav_header(wav_writer_state.wav_file, 0, 0, 0);
while (1){
// get next chunk
int bytes_read = __read(fd, read_buffer, sizeof(read_buffer));
if (0 >= bytes_read) break;
while (bytes_read > 0){
int space_in_frame_buffer = sizeof(frame_buffer) - state.frameBytes;
int bytes_to_append = space_in_frame_buffer > bytes_read ? bytes_read : space_in_frame_buffer;
handle_received_sbc_data(&state, read_buffer, bytes_to_append, wav_file);
memmove(read_buffer, read_buffer + bytes_to_append, sizeof(read_buffer) - bytes_to_append);
bytes_read -= bytes_to_append;
}
// process chunk
handle_received_sbc_chunk(&state, read_buffer, bytes_read);
}
rewind(wav_file);
int num_channels = context.common.frameInfo.nrof_channels;
int sample_rate = context.common.frameInfo.frequency;
write_wav_header(wav_writer_state.wav_file, wav_writer_state.total_num_samples, num_channels, sample_rate);
write_wav_header(wav_writer_state.wav_file, wav_writer_state.total_num_samples, num_channels(&decoder_context), sample_rate(&decoder_context));
fclose(wav_file);
close(fd);
int frame_count = wav_writer_state.total_num_samples/(context.common.frameInfo.nrof_blocks * context.common.frameInfo.nrof_subbands);
int frame_count = wav_writer_state.total_num_samples/num_samples(&decoder_context);
printf("Write %d frames to wav file: %s\n", frame_count, wav_filename);
}