annotate ffmpeg/libavcodec/libspeexdec.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
rev   line source
yading@10 1 /*
yading@10 2 * Copyright (C) 2008 David Conrad
yading@10 3 *
yading@10 4 * This file is part of FFmpeg.
yading@10 5 *
yading@10 6 * FFmpeg is free software; you can redistribute it and/or
yading@10 7 * modify it under the terms of the GNU Lesser General Public
yading@10 8 * License as published by the Free Software Foundation; either
yading@10 9 * version 2.1 of the License, or (at your option) any later version.
yading@10 10 *
yading@10 11 * FFmpeg is distributed in the hope that it will be useful,
yading@10 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 14 * Lesser General Public License for more details.
yading@10 15 *
yading@10 16 * You should have received a copy of the GNU Lesser General Public
yading@10 17 * License along with FFmpeg; if not, write to the Free Software
yading@10 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 19 */
yading@10 20
yading@10 21 #include <speex/speex.h>
yading@10 22 #include <speex/speex_header.h>
yading@10 23 #include <speex/speex_stereo.h>
yading@10 24 #include <speex/speex_callbacks.h>
yading@10 25
yading@10 26 #include "libavutil/channel_layout.h"
yading@10 27 #include "libavutil/common.h"
yading@10 28 #include "avcodec.h"
yading@10 29 #include "internal.h"
yading@10 30
yading@10 31 typedef struct {
yading@10 32 SpeexBits bits;
yading@10 33 SpeexStereoState stereo;
yading@10 34 void *dec_state;
yading@10 35 int frame_size;
yading@10 36 } LibSpeexContext;
yading@10 37
yading@10 38
yading@10 39 static av_cold int libspeex_decode_init(AVCodecContext *avctx)
yading@10 40 {
yading@10 41 LibSpeexContext *s = avctx->priv_data;
yading@10 42 const SpeexMode *mode;
yading@10 43 SpeexHeader *header = NULL;
yading@10 44 int spx_mode;
yading@10 45
yading@10 46 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
yading@10 47 if (avctx->extradata && avctx->extradata_size >= 80) {
yading@10 48 header = speex_packet_to_header(avctx->extradata,
yading@10 49 avctx->extradata_size);
yading@10 50 if (!header)
yading@10 51 av_log(avctx, AV_LOG_WARNING, "Invalid Speex header\n");
yading@10 52 }
yading@10 53 if (avctx->codec_tag == MKTAG('S', 'P', 'X', 'N')) {
yading@10 54 if (!avctx->extradata || avctx->extradata && avctx->extradata_size < 47) {
yading@10 55 av_log(avctx, AV_LOG_ERROR, "Missing or invalid extradata.\n");
yading@10 56 return AVERROR_INVALIDDATA;
yading@10 57 }
yading@10 58 if (avctx->extradata[37] != 10) {
yading@10 59 av_log(avctx, AV_LOG_ERROR, "Unsupported quality mode.\n");
yading@10 60 return AVERROR_PATCHWELCOME;
yading@10 61 }
yading@10 62 spx_mode = 0;
yading@10 63 } else if (header) {
yading@10 64 avctx->sample_rate = header->rate;
yading@10 65 avctx->channels = header->nb_channels;
yading@10 66 spx_mode = header->mode;
yading@10 67 speex_header_free(header);
yading@10 68 } else {
yading@10 69 switch (avctx->sample_rate) {
yading@10 70 case 8000: spx_mode = 0; break;
yading@10 71 case 16000: spx_mode = 1; break;
yading@10 72 case 32000: spx_mode = 2; break;
yading@10 73 default:
yading@10 74 /* libspeex can handle any mode if initialized as ultra-wideband */
yading@10 75 av_log(avctx, AV_LOG_WARNING, "Invalid sample rate: %d\n"
yading@10 76 "Decoding as 32kHz ultra-wideband\n",
yading@10 77 avctx->sample_rate);
yading@10 78 spx_mode = 2;
yading@10 79 }
yading@10 80 }
yading@10 81
yading@10 82 mode = speex_lib_get_mode(spx_mode);
yading@10 83 if (!mode) {
yading@10 84 av_log(avctx, AV_LOG_ERROR, "Unknown Speex mode %d", spx_mode);
yading@10 85 return AVERROR_INVALIDDATA;
yading@10 86 }
yading@10 87 s->frame_size = 160 << spx_mode;
yading@10 88 if (!avctx->sample_rate)
yading@10 89 avctx->sample_rate = 8000 << spx_mode;
yading@10 90
yading@10 91 if (avctx->channels < 1 || avctx->channels > 2) {
yading@10 92 /* libspeex can handle mono or stereo if initialized as stereo */
yading@10 93 av_log(avctx, AV_LOG_ERROR, "Invalid channel count: %d.\n"
yading@10 94 "Decoding as stereo.\n", avctx->channels);
yading@10 95 avctx->channels = 2;
yading@10 96 }
yading@10 97 avctx->channel_layout = avctx->channels == 2 ? AV_CH_LAYOUT_STEREO :
yading@10 98 AV_CH_LAYOUT_MONO;
yading@10 99
yading@10 100 speex_bits_init(&s->bits);
yading@10 101 s->dec_state = speex_decoder_init(mode);
yading@10 102 if (!s->dec_state) {
yading@10 103 av_log(avctx, AV_LOG_ERROR, "Error initializing libspeex decoder.\n");
yading@10 104 return -1;
yading@10 105 }
yading@10 106
yading@10 107 if (avctx->channels == 2) {
yading@10 108 SpeexCallback callback;
yading@10 109 callback.callback_id = SPEEX_INBAND_STEREO;
yading@10 110 callback.func = speex_std_stereo_request_handler;
yading@10 111 callback.data = &s->stereo;
yading@10 112 s->stereo = (SpeexStereoState)SPEEX_STEREO_STATE_INIT;
yading@10 113 speex_decoder_ctl(s->dec_state, SPEEX_SET_HANDLER, &callback);
yading@10 114 }
yading@10 115
yading@10 116 return 0;
yading@10 117 }
yading@10 118
yading@10 119 static int libspeex_decode_frame(AVCodecContext *avctx, void *data,
yading@10 120 int *got_frame_ptr, AVPacket *avpkt)
yading@10 121 {
yading@10 122 uint8_t *buf = avpkt->data;
yading@10 123 int buf_size = avpkt->size;
yading@10 124 LibSpeexContext *s = avctx->priv_data;
yading@10 125 AVFrame *frame = data;
yading@10 126 int16_t *output;
yading@10 127 int ret, consumed = 0;
yading@10 128
yading@10 129 /* get output buffer */
yading@10 130 frame->nb_samples = s->frame_size;
yading@10 131 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
yading@10 132 return ret;
yading@10 133 output = (int16_t *)frame->data[0];
yading@10 134
yading@10 135 /* if there is not enough data left for the smallest possible frame or the
yading@10 136 next 5 bits are a terminator code, reset the libspeex buffer using the
yading@10 137 current packet, otherwise ignore the current packet and keep decoding
yading@10 138 frames from the libspeex buffer. */
yading@10 139 if (speex_bits_remaining(&s->bits) < 5 ||
yading@10 140 speex_bits_peek_unsigned(&s->bits, 5) == 0xF) {
yading@10 141 /* check for flush packet */
yading@10 142 if (!buf || !buf_size) {
yading@10 143 *got_frame_ptr = 0;
yading@10 144 return buf_size;
yading@10 145 }
yading@10 146 /* set new buffer */
yading@10 147 speex_bits_read_from(&s->bits, buf, buf_size);
yading@10 148 consumed = buf_size;
yading@10 149 }
yading@10 150
yading@10 151 /* decode a single frame */
yading@10 152 ret = speex_decode_int(s->dec_state, &s->bits, output);
yading@10 153 if (ret <= -2) {
yading@10 154 av_log(avctx, AV_LOG_ERROR, "Error decoding Speex frame.\n");
yading@10 155 return AVERROR_INVALIDDATA;
yading@10 156 }
yading@10 157 if (avctx->channels == 2)
yading@10 158 speex_decode_stereo_int(output, s->frame_size, &s->stereo);
yading@10 159
yading@10 160 *got_frame_ptr = 1;
yading@10 161
yading@10 162 return consumed;
yading@10 163 }
yading@10 164
yading@10 165 static av_cold int libspeex_decode_close(AVCodecContext *avctx)
yading@10 166 {
yading@10 167 LibSpeexContext *s = avctx->priv_data;
yading@10 168
yading@10 169 speex_bits_destroy(&s->bits);
yading@10 170 speex_decoder_destroy(s->dec_state);
yading@10 171
yading@10 172 return 0;
yading@10 173 }
yading@10 174
yading@10 175 static av_cold void libspeex_decode_flush(AVCodecContext *avctx)
yading@10 176 {
yading@10 177 LibSpeexContext *s = avctx->priv_data;
yading@10 178 speex_bits_reset(&s->bits);
yading@10 179 }
yading@10 180
yading@10 181 AVCodec ff_libspeex_decoder = {
yading@10 182 .name = "libspeex",
yading@10 183 .type = AVMEDIA_TYPE_AUDIO,
yading@10 184 .id = AV_CODEC_ID_SPEEX,
yading@10 185 .priv_data_size = sizeof(LibSpeexContext),
yading@10 186 .init = libspeex_decode_init,
yading@10 187 .close = libspeex_decode_close,
yading@10 188 .decode = libspeex_decode_frame,
yading@10 189 .flush = libspeex_decode_flush,
yading@10 190 .capabilities = CODEC_CAP_SUBFRAMES | CODEC_CAP_DELAY | CODEC_CAP_DR1,
yading@10 191 .long_name = NULL_IF_CONFIG_SMALL("libspeex Speex"),
yading@10 192 };