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