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>
30 SpeexStereoState stereo;
37 static av_cold int libspeex_decode_init(AVCodecContext *avctx)
39 LibSpeexContext *s = avctx->priv_data;
40 const SpeexMode *mode;
42 // defaults in the case of a missing header
43 if (avctx->sample_rate <= 8000)
44 mode = &speex_nb_mode;
45 else if (avctx->sample_rate <= 16000)
46 mode = &speex_wb_mode;
48 mode = &speex_uwb_mode;
50 if (avctx->extradata_size >= 80)
51 s->header = speex_packet_to_header(avctx->extradata, avctx->extradata_size);
53 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
55 avctx->sample_rate = s->header->rate;
56 avctx->channels = s->header->nb_channels;
57 avctx->frame_size = s->frame_size = s->header->frame_size;
58 if (s->header->frames_per_packet)
59 avctx->frame_size *= s->header->frames_per_packet;
61 mode = speex_lib_get_mode(s->header->mode);
63 av_log(avctx, AV_LOG_ERROR, "Unknown Speex mode %d", s->header->mode);
64 return AVERROR_INVALIDDATA;
67 av_log(avctx, AV_LOG_INFO, "Missing Speex header, assuming defaults.\n");
69 if (avctx->channels > 2) {
70 av_log(avctx, AV_LOG_ERROR, "Only stereo and mono are supported.\n");
71 return AVERROR(EINVAL);
74 speex_bits_init(&s->bits);
75 s->dec_state = speex_decoder_init(mode);
77 av_log(avctx, AV_LOG_ERROR, "Error initializing libspeex decoder.\n");
82 speex_decoder_ctl(s->dec_state, SPEEX_GET_FRAME_SIZE, &s->frame_size);
85 if (avctx->channels == 2) {
86 SpeexCallback callback;
87 callback.callback_id = SPEEX_INBAND_STEREO;
88 callback.func = speex_std_stereo_request_handler;
89 callback.data = &s->stereo;
90 s->stereo = (SpeexStereoState)SPEEX_STEREO_STATE_INIT;
91 speex_decoder_ctl(s->dec_state, SPEEX_SET_HANDLER, &callback);
94 avcodec_get_frame_defaults(&s->frame);
95 avctx->coded_frame = &s->frame;
100 static int libspeex_decode_frame(AVCodecContext *avctx, void *data,
101 int *got_frame_ptr, AVPacket *avpkt)
103 uint8_t *buf = avpkt->data;
104 int buf_size = avpkt->size;
105 LibSpeexContext *s = avctx->priv_data;
107 int ret, consumed = 0;
109 /* get output buffer */
110 s->frame.nb_samples = s->frame_size;
111 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
112 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
115 output = (int16_t *)s->frame.data[0];
117 /* if there is not enough data left for the smallest possible frame,
118 reset the libspeex buffer using the current packet, otherwise ignore
119 the current packet and keep decoding frames from the libspeex buffer. */
120 if (speex_bits_remaining(&s->bits) < 43) {
121 /* check for flush packet */
122 if (!buf || !buf_size) {
127 speex_bits_read_from(&s->bits, buf, buf_size);
131 /* decode a single frame */
132 ret = speex_decode_int(s->dec_state, &s->bits, output);
134 av_log(avctx, AV_LOG_ERROR, "Error decoding Speex frame.\n");
135 return AVERROR_INVALIDDATA;
137 if (avctx->channels == 2)
138 speex_decode_stereo_int(output, s->frame_size, &s->stereo);
141 *(AVFrame *)data = s->frame;
146 static av_cold int libspeex_decode_close(AVCodecContext *avctx)
148 LibSpeexContext *s = avctx->priv_data;
150 speex_header_free(s->header);
151 speex_bits_destroy(&s->bits);
152 speex_decoder_destroy(s->dec_state);
157 static av_cold void libspeex_decode_flush(AVCodecContext *avctx)
159 LibSpeexContext *s = avctx->priv_data;
160 speex_bits_reset(&s->bits);
163 AVCodec ff_libspeex_decoder = {
165 .type = AVMEDIA_TYPE_AUDIO,
166 .id = CODEC_ID_SPEEX,
167 .priv_data_size = sizeof(LibSpeexContext),
168 .init = libspeex_decode_init,
169 .close = libspeex_decode_close,
170 .decode = libspeex_decode_frame,
171 .flush = libspeex_decode_flush,
172 .capabilities = CODEC_CAP_SUBFRAMES | CODEC_CAP_DELAY | CODEC_CAP_DR1,
173 .long_name = NULL_IF_CONFIG_SMALL("libspeex Speex"),