yading@10: /* yading@10: * Copyright (C) 2008 David Conrad yading@10: * yading@10: * This file is part of FFmpeg. yading@10: * yading@10: * FFmpeg is free software; you can redistribute it and/or yading@10: * modify it under the terms of the GNU Lesser General Public yading@10: * License as published by the Free Software Foundation; either yading@10: * version 2.1 of the License, or (at your option) any later version. yading@10: * yading@10: * FFmpeg is distributed in the hope that it will be useful, yading@10: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@10: * Lesser General Public License for more details. yading@10: * yading@10: * You should have received a copy of the GNU Lesser General Public yading@10: * License along with FFmpeg; if not, write to the Free Software yading@10: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@10: */ yading@10: yading@10: #include yading@10: #include yading@10: #include yading@10: #include yading@10: yading@10: #include "libavutil/channel_layout.h" yading@10: #include "libavutil/common.h" yading@10: #include "avcodec.h" yading@10: #include "internal.h" yading@10: yading@10: typedef struct { yading@10: SpeexBits bits; yading@10: SpeexStereoState stereo; yading@10: void *dec_state; yading@10: int frame_size; yading@10: } LibSpeexContext; yading@10: yading@10: yading@10: static av_cold int libspeex_decode_init(AVCodecContext *avctx) yading@10: { yading@10: LibSpeexContext *s = avctx->priv_data; yading@10: const SpeexMode *mode; yading@10: SpeexHeader *header = NULL; yading@10: int spx_mode; yading@10: yading@10: avctx->sample_fmt = AV_SAMPLE_FMT_S16; yading@10: if (avctx->extradata && avctx->extradata_size >= 80) { yading@10: header = speex_packet_to_header(avctx->extradata, yading@10: avctx->extradata_size); yading@10: if (!header) yading@10: av_log(avctx, AV_LOG_WARNING, "Invalid Speex header\n"); yading@10: } yading@10: if (avctx->codec_tag == MKTAG('S', 'P', 'X', 'N')) { yading@10: if (!avctx->extradata || avctx->extradata && avctx->extradata_size < 47) { yading@10: av_log(avctx, AV_LOG_ERROR, "Missing or invalid extradata.\n"); yading@10: return AVERROR_INVALIDDATA; yading@10: } yading@10: if (avctx->extradata[37] != 10) { yading@10: av_log(avctx, AV_LOG_ERROR, "Unsupported quality mode.\n"); yading@10: return AVERROR_PATCHWELCOME; yading@10: } yading@10: spx_mode = 0; yading@10: } else if (header) { yading@10: avctx->sample_rate = header->rate; yading@10: avctx->channels = header->nb_channels; yading@10: spx_mode = header->mode; yading@10: speex_header_free(header); yading@10: } else { yading@10: switch (avctx->sample_rate) { yading@10: case 8000: spx_mode = 0; break; yading@10: case 16000: spx_mode = 1; break; yading@10: case 32000: spx_mode = 2; break; yading@10: default: yading@10: /* libspeex can handle any mode if initialized as ultra-wideband */ yading@10: av_log(avctx, AV_LOG_WARNING, "Invalid sample rate: %d\n" yading@10: "Decoding as 32kHz ultra-wideband\n", yading@10: avctx->sample_rate); yading@10: spx_mode = 2; yading@10: } yading@10: } yading@10: yading@10: mode = speex_lib_get_mode(spx_mode); yading@10: if (!mode) { yading@10: av_log(avctx, AV_LOG_ERROR, "Unknown Speex mode %d", spx_mode); yading@10: return AVERROR_INVALIDDATA; yading@10: } yading@10: s->frame_size = 160 << spx_mode; yading@10: if (!avctx->sample_rate) yading@10: avctx->sample_rate = 8000 << spx_mode; yading@10: yading@10: if (avctx->channels < 1 || avctx->channels > 2) { yading@10: /* libspeex can handle mono or stereo if initialized as stereo */ yading@10: av_log(avctx, AV_LOG_ERROR, "Invalid channel count: %d.\n" yading@10: "Decoding as stereo.\n", avctx->channels); yading@10: avctx->channels = 2; yading@10: } yading@10: avctx->channel_layout = avctx->channels == 2 ? AV_CH_LAYOUT_STEREO : yading@10: AV_CH_LAYOUT_MONO; yading@10: yading@10: speex_bits_init(&s->bits); yading@10: s->dec_state = speex_decoder_init(mode); yading@10: if (!s->dec_state) { yading@10: av_log(avctx, AV_LOG_ERROR, "Error initializing libspeex decoder.\n"); yading@10: return -1; yading@10: } yading@10: yading@10: if (avctx->channels == 2) { yading@10: SpeexCallback callback; yading@10: callback.callback_id = SPEEX_INBAND_STEREO; yading@10: callback.func = speex_std_stereo_request_handler; yading@10: callback.data = &s->stereo; yading@10: s->stereo = (SpeexStereoState)SPEEX_STEREO_STATE_INIT; yading@10: speex_decoder_ctl(s->dec_state, SPEEX_SET_HANDLER, &callback); yading@10: } yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: static int libspeex_decode_frame(AVCodecContext *avctx, void *data, yading@10: int *got_frame_ptr, AVPacket *avpkt) yading@10: { yading@10: uint8_t *buf = avpkt->data; yading@10: int buf_size = avpkt->size; yading@10: LibSpeexContext *s = avctx->priv_data; yading@10: AVFrame *frame = data; yading@10: int16_t *output; yading@10: int ret, consumed = 0; yading@10: yading@10: /* get output buffer */ yading@10: frame->nb_samples = s->frame_size; yading@10: if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) yading@10: return ret; yading@10: output = (int16_t *)frame->data[0]; yading@10: yading@10: /* if there is not enough data left for the smallest possible frame or the yading@10: next 5 bits are a terminator code, reset the libspeex buffer using the yading@10: current packet, otherwise ignore the current packet and keep decoding yading@10: frames from the libspeex buffer. */ yading@10: if (speex_bits_remaining(&s->bits) < 5 || yading@10: speex_bits_peek_unsigned(&s->bits, 5) == 0xF) { yading@10: /* check for flush packet */ yading@10: if (!buf || !buf_size) { yading@10: *got_frame_ptr = 0; yading@10: return buf_size; yading@10: } yading@10: /* set new buffer */ yading@10: speex_bits_read_from(&s->bits, buf, buf_size); yading@10: consumed = buf_size; yading@10: } yading@10: yading@10: /* decode a single frame */ yading@10: ret = speex_decode_int(s->dec_state, &s->bits, output); yading@10: if (ret <= -2) { yading@10: av_log(avctx, AV_LOG_ERROR, "Error decoding Speex frame.\n"); yading@10: return AVERROR_INVALIDDATA; yading@10: } yading@10: if (avctx->channels == 2) yading@10: speex_decode_stereo_int(output, s->frame_size, &s->stereo); yading@10: yading@10: *got_frame_ptr = 1; yading@10: yading@10: return consumed; yading@10: } yading@10: yading@10: static av_cold int libspeex_decode_close(AVCodecContext *avctx) yading@10: { yading@10: LibSpeexContext *s = avctx->priv_data; yading@10: yading@10: speex_bits_destroy(&s->bits); yading@10: speex_decoder_destroy(s->dec_state); yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: static av_cold void libspeex_decode_flush(AVCodecContext *avctx) yading@10: { yading@10: LibSpeexContext *s = avctx->priv_data; yading@10: speex_bits_reset(&s->bits); yading@10: } yading@10: yading@10: AVCodec ff_libspeex_decoder = { yading@10: .name = "libspeex", yading@10: .type = AVMEDIA_TYPE_AUDIO, yading@10: .id = AV_CODEC_ID_SPEEX, yading@10: .priv_data_size = sizeof(LibSpeexContext), yading@10: .init = libspeex_decode_init, yading@10: .close = libspeex_decode_close, yading@10: .decode = libspeex_decode_frame, yading@10: .flush = libspeex_decode_flush, yading@10: .capabilities = CODEC_CAP_SUBFRAMES | CODEC_CAP_DELAY | CODEC_CAP_DR1, yading@10: .long_name = NULL_IF_CONFIG_SMALL("libspeex Speex"), yading@10: };