annotate ffmpeg/libavcodec/libopusdec.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 * Opus decoder using libopus
yading@10 3 * Copyright (c) 2012 Nicolas George
yading@10 4 *
yading@10 5 * This file is part of FFmpeg.
yading@10 6 *
yading@10 7 * FFmpeg is free software; you can redistribute it and/or
yading@10 8 * modify it under the terms of the GNU Lesser General Public
yading@10 9 * License as published by the Free Software Foundation; either
yading@10 10 * version 2.1 of the License, or (at your option) any later version.
yading@10 11 *
yading@10 12 * FFmpeg is distributed in the hope that it will be useful,
yading@10 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 15 * Lesser General Public License for more details.
yading@10 16 *
yading@10 17 * You should have received a copy of the GNU Lesser General Public
yading@10 18 * License along with FFmpeg; if not, write to the Free Software
yading@10 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 20 */
yading@10 21
yading@10 22 #include <opus.h>
yading@10 23 #include <opus_multistream.h>
yading@10 24
yading@10 25 #include "libavutil/avassert.h"
yading@10 26 #include "libavutil/intreadwrite.h"
yading@10 27 #include "avcodec.h"
yading@10 28 #include "internal.h"
yading@10 29 #include "vorbis.h"
yading@10 30 #include "mathops.h"
yading@10 31 #include "libopus.h"
yading@10 32
yading@10 33 struct libopus_context {
yading@10 34 OpusMSDecoder *dec;
yading@10 35 int pre_skip;
yading@10 36 #ifndef OPUS_SET_GAIN
yading@10 37 union { int i; double d; } gain;
yading@10 38 #endif
yading@10 39 };
yading@10 40
yading@10 41 #define OPUS_HEAD_SIZE 19
yading@10 42
yading@10 43 static av_cold int libopus_decode_init(AVCodecContext *avc)
yading@10 44 {
yading@10 45 struct libopus_context *opus = avc->priv_data;
yading@10 46 int ret, channel_map = 0, gain_db = 0, nb_streams, nb_coupled;
yading@10 47 uint8_t mapping_arr[8] = { 0, 1 }, *mapping;
yading@10 48
yading@10 49 avc->sample_rate = 48000;
yading@10 50 avc->sample_fmt = avc->request_sample_fmt == AV_SAMPLE_FMT_FLT ?
yading@10 51 AV_SAMPLE_FMT_FLT : AV_SAMPLE_FMT_S16;
yading@10 52 avc->channel_layout = avc->channels > 8 ? 0 :
yading@10 53 ff_vorbis_channel_layouts[avc->channels - 1];
yading@10 54
yading@10 55 if (avc->extradata_size >= OPUS_HEAD_SIZE) {
yading@10 56 opus->pre_skip = AV_RL16(avc->extradata + 10);
yading@10 57 gain_db = sign_extend(AV_RL16(avc->extradata + 16), 16);
yading@10 58 channel_map = AV_RL8 (avc->extradata + 18);
yading@10 59 }
yading@10 60 if (avc->extradata_size >= OPUS_HEAD_SIZE + 2 + avc->channels) {
yading@10 61 nb_streams = avc->extradata[OPUS_HEAD_SIZE + 0];
yading@10 62 nb_coupled = avc->extradata[OPUS_HEAD_SIZE + 1];
yading@10 63 if (nb_streams + nb_coupled != avc->channels)
yading@10 64 av_log(avc, AV_LOG_WARNING, "Inconsistent channel mapping.\n");
yading@10 65 mapping = avc->extradata + OPUS_HEAD_SIZE + 2;
yading@10 66 } else {
yading@10 67 if (avc->channels > 2 || channel_map) {
yading@10 68 av_log(avc, AV_LOG_ERROR,
yading@10 69 "No channel mapping for %d channels.\n", avc->channels);
yading@10 70 return AVERROR(EINVAL);
yading@10 71 }
yading@10 72 nb_streams = 1;
yading@10 73 nb_coupled = avc->channels > 1;
yading@10 74 mapping = mapping_arr;
yading@10 75 }
yading@10 76
yading@10 77 if (avc->channels > 2 && avc->channels <= 8) {
yading@10 78 const uint8_t *vorbis_offset = ff_vorbis_channel_layout_offsets[avc->channels - 1];
yading@10 79 int ch;
yading@10 80
yading@10 81 /* Remap channels from vorbis order to ffmpeg order */
yading@10 82 for (ch = 0; ch < avc->channels; ch++)
yading@10 83 mapping_arr[ch] = mapping[vorbis_offset[ch]];
yading@10 84 mapping = mapping_arr;
yading@10 85 }
yading@10 86
yading@10 87 opus->dec = opus_multistream_decoder_create(avc->sample_rate, avc->channels,
yading@10 88 nb_streams, nb_coupled,
yading@10 89 mapping, &ret);
yading@10 90 if (!opus->dec) {
yading@10 91 av_log(avc, AV_LOG_ERROR, "Unable to create decoder: %s\n",
yading@10 92 opus_strerror(ret));
yading@10 93 return ff_opus_error_to_averror(ret);
yading@10 94 }
yading@10 95
yading@10 96 #ifdef OPUS_SET_GAIN
yading@10 97 ret = opus_multistream_decoder_ctl(opus->dec, OPUS_SET_GAIN(gain_db));
yading@10 98 if (ret != OPUS_OK)
yading@10 99 av_log(avc, AV_LOG_WARNING, "Failed to set gain: %s\n",
yading@10 100 opus_strerror(ret));
yading@10 101 #else
yading@10 102 {
yading@10 103 double gain_lin = pow(10, gain_db / (20.0 * 256));
yading@10 104 if (avc->sample_fmt == AV_SAMPLE_FMT_FLT)
yading@10 105 opus->gain.d = gain_lin;
yading@10 106 else
yading@10 107 opus->gain.i = FFMIN(gain_lin * 65536, INT_MAX);
yading@10 108 }
yading@10 109 #endif
yading@10 110
yading@10 111 avc->internal->skip_samples = opus->pre_skip;
yading@10 112 avc->delay = 3840; /* Decoder delay (in samples) at 48kHz */
yading@10 113
yading@10 114 return 0;
yading@10 115 }
yading@10 116
yading@10 117 static av_cold int libopus_decode_close(AVCodecContext *avc)
yading@10 118 {
yading@10 119 struct libopus_context *opus = avc->priv_data;
yading@10 120
yading@10 121 opus_multistream_decoder_destroy(opus->dec);
yading@10 122 return 0;
yading@10 123 }
yading@10 124
yading@10 125 #define MAX_FRAME_SIZE (960 * 6)
yading@10 126
yading@10 127 static int libopus_decode(AVCodecContext *avc, void *data,
yading@10 128 int *got_frame_ptr, AVPacket *pkt)
yading@10 129 {
yading@10 130 struct libopus_context *opus = avc->priv_data;
yading@10 131 AVFrame *frame = data;
yading@10 132 int ret, nb_samples;
yading@10 133
yading@10 134 frame->nb_samples = MAX_FRAME_SIZE;
yading@10 135 if ((ret = ff_get_buffer(avc, frame, 0)) < 0)
yading@10 136 return ret;
yading@10 137
yading@10 138 if (avc->sample_fmt == AV_SAMPLE_FMT_S16)
yading@10 139 nb_samples = opus_multistream_decode(opus->dec, pkt->data, pkt->size,
yading@10 140 (opus_int16 *)frame->data[0],
yading@10 141 frame->nb_samples, 0);
yading@10 142 else
yading@10 143 nb_samples = opus_multistream_decode_float(opus->dec, pkt->data, pkt->size,
yading@10 144 (float *)frame->data[0],
yading@10 145 frame->nb_samples, 0);
yading@10 146
yading@10 147 if (nb_samples < 0) {
yading@10 148 av_log(avc, AV_LOG_ERROR, "Decoding error: %s\n",
yading@10 149 opus_strerror(nb_samples));
yading@10 150 return ff_opus_error_to_averror(nb_samples);
yading@10 151 }
yading@10 152
yading@10 153 #ifndef OPUS_SET_GAIN
yading@10 154 {
yading@10 155 int i = avc->channels * nb_samples;
yading@10 156 if (avc->sample_fmt == AV_SAMPLE_FMT_FLT) {
yading@10 157 float *pcm = (float *)frame->data[0];
yading@10 158 for (; i > 0; i--, pcm++)
yading@10 159 *pcm = av_clipf(*pcm * opus->gain.d, -1, 1);
yading@10 160 } else {
yading@10 161 int16_t *pcm = (int16_t *)frame->data[0];
yading@10 162 for (; i > 0; i--, pcm++)
yading@10 163 *pcm = av_clip_int16(((int64_t)opus->gain.i * *pcm) >> 16);
yading@10 164 }
yading@10 165 }
yading@10 166 #endif
yading@10 167
yading@10 168 frame->nb_samples = nb_samples;
yading@10 169 *got_frame_ptr = 1;
yading@10 170
yading@10 171 return pkt->size;
yading@10 172 }
yading@10 173
yading@10 174 static void libopus_flush(AVCodecContext *avc)
yading@10 175 {
yading@10 176 struct libopus_context *opus = avc->priv_data;
yading@10 177
yading@10 178 opus_multistream_decoder_ctl(opus->dec, OPUS_RESET_STATE);
yading@10 179 /* The stream can have been extracted by a tool that is not Opus-aware.
yading@10 180 Therefore, any packet can become the first of the stream. */
yading@10 181 avc->internal->skip_samples = opus->pre_skip;
yading@10 182 }
yading@10 183
yading@10 184 AVCodec ff_libopus_decoder = {
yading@10 185 .name = "libopus",
yading@10 186 .type = AVMEDIA_TYPE_AUDIO,
yading@10 187 .id = AV_CODEC_ID_OPUS,
yading@10 188 .priv_data_size = sizeof(struct libopus_context),
yading@10 189 .init = libopus_decode_init,
yading@10 190 .close = libopus_decode_close,
yading@10 191 .decode = libopus_decode,
yading@10 192 .flush = libopus_flush,
yading@10 193 .capabilities = CODEC_CAP_DR1,
yading@10 194 .long_name = NULL_IF_CONFIG_SMALL("libopus Opus"),
yading@10 195 .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLT,
yading@10 196 AV_SAMPLE_FMT_S16,
yading@10 197 AV_SAMPLE_FMT_NONE },
yading@10 198 };