srs_tc_adc_speex.cpp 1.7 KB
#include "speex/speex.h"
#include "srs_tc_av_codec.hpp"
#include "srs_tc_common.hpp"


typedef struct {
    SpeexBits bits;
    void *dec_state;
    int dec_frame_size;
    int dec_buffer_size;
    spx_int16_t* output_buffer;
} handle_adc_spx_t;


static int open_codec_spx(tc_audio_opt* opt) {
    handle_adc_spx_t* s = (handle_adc_spx_t*)calloc(1, sizeof(handle_adc_spx_t));
    opt->handle = s;
    speex_bits_init(&s->bits);
    s->dec_state = speex_decoder_init(&speex_wb_mode);
    speex_decoder_ctl(s->dec_state, SPEEX_GET_FRAME_SIZE, &s->dec_frame_size);
    s->dec_buffer_size = s->dec_frame_size * sizeof(spx_int16_t);
    opt->sample_rate = 16000;
    opt->channels = 1;
    opt->buffer_size = s->dec_buffer_size ;
    opt->frame_size = s->dec_frame_size;
    return 0;
}


static int decode_frame_spx(tc_audio_opt* opt, uint8_t* inData, uint32_t inDataSize, uint8_t* outData) {
    handle_adc_spx_t* s = (handle_adc_spx_t*)opt->handle;
    int consumed = 0;
    if (speex_bits_remaining(&s->bits) < 5 ||
        speex_bits_peek_unsigned(&s->bits, 5) == 0xF) {
        speex_bits_read_from(&s->bits, (char*)inData, inDataSize);
        consumed = inDataSize;
    }
    
	int err = 0;
	if ((err = speex_decode_int(s->dec_state, &s->bits, (spx_int16_t *)outData)) == 0) {
        return consumed;
    } else {
		tc_log(LOG_LEVEL_ERROR, "speex_decode_int error: %d", err);
        return -1;
    }
}

static int close_codec_spx(tc_audio_opt* opt) {
    handle_adc_spx_t* s = (handle_adc_spx_t*)opt->handle;
    speex_decoder_destroy(s->dec_state);
    speex_bits_destroy(&s->bits);
    return 0;
}


const tc_av_codec_t adc_spx = {
    11,
	"adc_spx",
    open_codec_spx,
    decode_frame_spx,
    NULL,
    close_codec_spx
};