00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <speex/speex.h>
00022 #include <speex/speex_header.h>
00023 #include <speex/speex_stereo.h>
00024 #include <speex/speex_callbacks.h>
00025 #include "avcodec.h"
00026
00027 typedef struct {
00028 AVFrame frame;
00029 SpeexBits bits;
00030 SpeexStereoState stereo;
00031 void *dec_state;
00032 SpeexHeader *header;
00033 int frame_size;
00034 } LibSpeexContext;
00035
00036
00037 static av_cold int libspeex_decode_init(AVCodecContext *avctx)
00038 {
00039 LibSpeexContext *s = avctx->priv_data;
00040 const SpeexMode *mode;
00041
00042
00043 if (avctx->sample_rate <= 8000)
00044 mode = &speex_nb_mode;
00045 else if (avctx->sample_rate <= 16000)
00046 mode = &speex_wb_mode;
00047 else
00048 mode = &speex_uwb_mode;
00049
00050 if (avctx->extradata_size >= 80)
00051 s->header = speex_packet_to_header(avctx->extradata, avctx->extradata_size);
00052
00053 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00054 if (s->header) {
00055 avctx->sample_rate = s->header->rate;
00056 avctx->channels = s->header->nb_channels;
00057 s->frame_size = s->header->frame_size;
00058
00059 mode = speex_lib_get_mode(s->header->mode);
00060 if (!mode) {
00061 av_log(avctx, AV_LOG_ERROR, "Unknown Speex mode %d", s->header->mode);
00062 return AVERROR_INVALIDDATA;
00063 }
00064 } else
00065 av_log(avctx, AV_LOG_INFO, "Missing Speex header, assuming defaults.\n");
00066
00067 if (avctx->channels > 2) {
00068 av_log(avctx, AV_LOG_ERROR, "Only stereo and mono are supported.\n");
00069 return AVERROR(EINVAL);
00070 }
00071
00072 speex_bits_init(&s->bits);
00073 s->dec_state = speex_decoder_init(mode);
00074 if (!s->dec_state) {
00075 av_log(avctx, AV_LOG_ERROR, "Error initializing libspeex decoder.\n");
00076 return -1;
00077 }
00078
00079 if (!s->header) {
00080 speex_decoder_ctl(s->dec_state, SPEEX_GET_FRAME_SIZE, &s->frame_size);
00081 }
00082
00083 if (avctx->channels == 2) {
00084 SpeexCallback callback;
00085 callback.callback_id = SPEEX_INBAND_STEREO;
00086 callback.func = speex_std_stereo_request_handler;
00087 callback.data = &s->stereo;
00088 s->stereo = (SpeexStereoState)SPEEX_STEREO_STATE_INIT;
00089 speex_decoder_ctl(s->dec_state, SPEEX_SET_HANDLER, &callback);
00090 }
00091
00092 avcodec_get_frame_defaults(&s->frame);
00093 avctx->coded_frame = &s->frame;
00094
00095 return 0;
00096 }
00097
00098 static int libspeex_decode_frame(AVCodecContext *avctx, void *data,
00099 int *got_frame_ptr, AVPacket *avpkt)
00100 {
00101 uint8_t *buf = avpkt->data;
00102 int buf_size = avpkt->size;
00103 LibSpeexContext *s = avctx->priv_data;
00104 int16_t *output;
00105 int ret, consumed = 0;
00106
00107
00108 s->frame.nb_samples = s->frame_size;
00109 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
00110 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00111 return ret;
00112 }
00113 output = (int16_t *)s->frame.data[0];
00114
00115
00116
00117
00118 if (speex_bits_remaining(&s->bits) < 43) {
00119
00120 if (!buf || !buf_size) {
00121 *got_frame_ptr = 0;
00122 return buf_size;
00123 }
00124
00125 speex_bits_read_from(&s->bits, buf, buf_size);
00126 consumed = buf_size;
00127 }
00128
00129
00130 ret = speex_decode_int(s->dec_state, &s->bits, output);
00131 if (ret <= -2) {
00132 av_log(avctx, AV_LOG_ERROR, "Error decoding Speex frame.\n");
00133 return AVERROR_INVALIDDATA;
00134 }
00135 if (avctx->channels == 2)
00136 speex_decode_stereo_int(output, s->frame_size, &s->stereo);
00137
00138 *got_frame_ptr = 1;
00139 *(AVFrame *)data = s->frame;
00140
00141 return consumed;
00142 }
00143
00144 static av_cold int libspeex_decode_close(AVCodecContext *avctx)
00145 {
00146 LibSpeexContext *s = avctx->priv_data;
00147
00148 speex_header_free(s->header);
00149 speex_bits_destroy(&s->bits);
00150 speex_decoder_destroy(s->dec_state);
00151
00152 return 0;
00153 }
00154
00155 static av_cold void libspeex_decode_flush(AVCodecContext *avctx)
00156 {
00157 LibSpeexContext *s = avctx->priv_data;
00158 speex_bits_reset(&s->bits);
00159 }
00160
00161 AVCodec ff_libspeex_decoder = {
00162 .name = "libspeex",
00163 .type = AVMEDIA_TYPE_AUDIO,
00164 .id = CODEC_ID_SPEEX,
00165 .priv_data_size = sizeof(LibSpeexContext),
00166 .init = libspeex_decode_init,
00167 .close = libspeex_decode_close,
00168 .decode = libspeex_decode_frame,
00169 .flush = libspeex_decode_flush,
00170 .capabilities = CODEC_CAP_SUBFRAMES | CODEC_CAP_DELAY | CODEC_CAP_DR1,
00171 .long_name = NULL_IF_CONFIG_SMALL("libspeex Speex"),
00172 };