2 * Copyright (C) 2008 David Conrad
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <speex/speex.h>
22 #include <speex/speex_header.h>
23 #include <speex/speex_stereo.h>
24 #include <speex/speex_callbacks.h>
26 #include "libavutil/channel_layout.h"
27 #include "libavutil/common.h"
33 SpeexStereoState stereo;
39 static av_cold int libspeex_decode_init(AVCodecContext *avctx)
41 LibSpeexContext *s = avctx->priv_data;
42 const SpeexMode *mode;
43 SpeexHeader *header = NULL;
46 avctx->sample_fmt = AV_SAMPLE_FMT_NONE;
47 if (avctx->extradata && avctx->extradata_size >= 80) {
48 header = speex_packet_to_header(avctx->extradata,
49 avctx->extradata_size);
51 av_log(avctx, AV_LOG_WARNING, "Invalid Speex header\n");
53 if (avctx->codec_tag == MKTAG('S', 'P', 'X', 'N')) {
54 if (!avctx->extradata || avctx->extradata && avctx->extradata_size < 47) {
55 av_log(avctx, AV_LOG_ERROR, "Missing or invalid extradata.\n");
56 return AVERROR_INVALIDDATA;
58 if (avctx->extradata[37] != 10) {
59 av_log(avctx, AV_LOG_ERROR, "Unsupported quality mode.\n");
60 return AVERROR_PATCHWELCOME;
64 avctx->sample_rate = header->rate;
65 avctx->channels = header->nb_channels;
66 spx_mode = header->mode;
67 speex_header_free(header);
69 switch (avctx->sample_rate) {
70 case 8000: spx_mode = 0; break;
71 case 16000: spx_mode = 1; break;
72 case 32000: spx_mode = 2; break;
74 /* libspeex can handle any mode if initialized as ultra-wideband */
75 av_log(avctx, AV_LOG_WARNING, "Invalid sample rate: %d\n"
76 "Decoding as 32kHz ultra-wideband\n",
82 mode = speex_lib_get_mode(spx_mode);
84 av_log(avctx, AV_LOG_ERROR, "Unknown Speex mode %d", spx_mode);
85 return AVERROR_INVALIDDATA;
87 s->frame_size = 160 << spx_mode;
88 if (!avctx->sample_rate)
89 avctx->sample_rate = 8000 << spx_mode;
91 if (avctx->channels < 1 || avctx->channels > 2) {
92 /* libspeex can handle mono or stereo if initialized as stereo */
93 av_log(avctx, AV_LOG_ERROR, "Invalid channel count: %d.\n"
94 "Decoding as stereo.\n", avctx->channels);
97 avctx->channel_layout = avctx->channels == 2 ? AV_CH_LAYOUT_STEREO :
100 speex_bits_init(&s->bits);
101 s->dec_state = speex_decoder_init(mode);
103 av_log(avctx, AV_LOG_ERROR, "Error initializing libspeex decoder.\n");
107 if (avctx->channels == 2) {
108 SpeexCallback callback;
109 callback.callback_id = SPEEX_INBAND_STEREO;
110 callback.func = speex_std_stereo_request_handler;
111 callback.data = &s->stereo;
112 s->stereo = (SpeexStereoState)SPEEX_STEREO_STATE_INIT;
113 speex_decoder_ctl(s->dec_state, SPEEX_SET_HANDLER, &callback);
119 static int libspeex_decode_frame(AVCodecContext *avctx, void *data,
120 int *got_frame_ptr, AVPacket *avpkt)
122 uint8_t *buf = avpkt->data;
123 int buf_size = avpkt->size;
124 LibSpeexContext *s = avctx->priv_data;
125 AVFrame *frame = data;
127 int ret, consumed = 0;
128 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
130 /* get output buffer */
131 frame->nb_samples = s->frame_size;
132 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
134 output = (int16_t *)frame->data[0];
136 /* if there is not enough data left for the smallest possible frame or the
137 next 5 bits are a terminator code, reset the libspeex buffer using the
138 current packet, otherwise ignore the current packet and keep decoding
139 frames from the libspeex buffer. */
140 if (speex_bits_remaining(&s->bits) < 5 ||
141 speex_bits_peek_unsigned(&s->bits, 5) == 0xF) {
142 /* check for flush packet */
143 if (!buf || !buf_size) {
148 speex_bits_read_from(&s->bits, buf, buf_size);
152 /* decode a single frame */
153 ret = speex_decode_int(s->dec_state, &s->bits, output);
155 av_log(avctx, AV_LOG_ERROR, "Error decoding Speex frame.\n");
156 return AVERROR_INVALIDDATA;
158 if (avctx->channels == 2)
159 speex_decode_stereo_int(output, s->frame_size, &s->stereo);
163 if (!avctx->bit_rate)
164 speex_decoder_ctl(s->dec_state, SPEEX_GET_BITRATE, &avctx->bit_rate);
168 static av_cold int libspeex_decode_close(AVCodecContext *avctx)
170 LibSpeexContext *s = avctx->priv_data;
172 speex_bits_destroy(&s->bits);
173 speex_decoder_destroy(s->dec_state);
178 static av_cold void libspeex_decode_flush(AVCodecContext *avctx)
180 LibSpeexContext *s = avctx->priv_data;
181 speex_bits_reset(&s->bits);
184 AVCodec ff_libspeex_decoder = {
186 .long_name = NULL_IF_CONFIG_SMALL("libspeex Speex"),
187 .type = AVMEDIA_TYPE_AUDIO,
188 .id = AV_CODEC_ID_SPEEX,
189 .priv_data_size = sizeof(LibSpeexContext),
190 .init = libspeex_decode_init,
191 .close = libspeex_decode_close,
192 .decode = libspeex_decode_frame,
193 .flush = libspeex_decode_flush,
194 .capabilities = CODEC_CAP_SUBFRAMES | CODEC_CAP_DELAY | CODEC_CAP_DR1,