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