summaryrefslogtreecommitdiff
path: root/libavcodec/libopusdec.c
diff options
context:
space:
mode:
Diffstat (limited to 'libavcodec/libopusdec.c')
-rw-r--r--libavcodec/libopusdec.c69
1 files changed, 48 insertions, 21 deletions
diff --git a/libavcodec/libopusdec.c b/libavcodec/libopusdec.c
index 4335bd6eca..24488fe9dd 100644
--- a/libavcodec/libopusdec.c
+++ b/libavcodec/libopusdec.c
@@ -2,20 +2,20 @@
* Opus decoder using libopus
* Copyright (c) 2012 Nicolas George
*
- * This file is part of Libav.
+ * This file is part of FFmpeg.
*
- * Libav is free software; you can redistribute it and/or
+ * FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
- * Libav is distributed in the hope that it will be useful,
+ * FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
- * License along with Libav; if not, write to the Free Software
+ * License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
@@ -26,33 +26,30 @@
#include "libavutil/avassert.h"
#include "libavutil/intreadwrite.h"
#include "avcodec.h"
+#include "internal.h"
#include "vorbis.h"
#include "mathops.h"
struct libopus_context {
OpusMSDecoder *dec;
AVFrame frame;
+ int pre_skip;
+#ifndef OPUS_SET_GAIN
+ union { int i; double d; } gain;
+#endif
};
static int opus_error_to_averror(int err)
{
switch (err) {
- case OPUS_BAD_ARG:
- return AVERROR(EINVAL);
- case OPUS_BUFFER_TOO_SMALL:
- return AVERROR_UNKNOWN;
- case OPUS_INTERNAL_ERROR:
- return AVERROR(EFAULT);
- case OPUS_INVALID_PACKET:
- return AVERROR_INVALIDDATA;
- case OPUS_UNIMPLEMENTED:
- return AVERROR(ENOSYS);
- case OPUS_INVALID_STATE:
- return AVERROR_UNKNOWN;
- case OPUS_ALLOC_FAIL:
- return AVERROR(ENOMEM);
- default:
- return AVERROR(EINVAL);
+ case OPUS_BAD_ARG: return AVERROR(EINVAL);
+ case OPUS_BUFFER_TOO_SMALL: return AVERROR_BUFFER_TOO_SMALL;
+ case OPUS_INTERNAL_ERROR: return AVERROR(EFAULT);
+ case OPUS_INVALID_PACKET: return AVERROR_INVALIDDATA;
+ case OPUS_UNIMPLEMENTED: return AVERROR(ENOSYS);
+ case OPUS_INVALID_STATE: return AVERROR_EXTERNAL;
+ case OPUS_ALLOC_FAIL: return AVERROR(ENOMEM);
+ default: return AVERROR(EINVAL);
}
}
@@ -71,6 +68,7 @@ static av_cold int libopus_decode_init(AVCodecContext *avc)
ff_vorbis_channel_layouts[avc->channels - 1];
if (avc->extradata_size >= OPUS_HEAD_SIZE) {
+ opus->pre_skip = AV_RL16(avc->extradata + 10);
gain_db = sign_extend(AV_RL16(avc->extradata + 16), 16);
channel_map = AV_RL8 (avc->extradata + 18);
}
@@ -95,7 +93,7 @@ static av_cold int libopus_decode_init(AVCodecContext *avc)
const uint8_t *vorbis_offset = ff_vorbis_channel_layout_offsets[avc->channels - 1];
int ch;
- /* Remap channels from vorbis order to libav order */
+ /* Remap channels from vorbis order to ffmpeg order */
for (ch = 0; ch < avc->channels; ch++)
mapping_arr[ch] = mapping[vorbis_offset[ch]];
mapping = mapping_arr;
@@ -110,11 +108,22 @@ static av_cold int libopus_decode_init(AVCodecContext *avc)
return opus_error_to_averror(ret);
}
+#ifdef OPUS_SET_GAIN
ret = opus_multistream_decoder_ctl(opus->dec, OPUS_SET_GAIN(gain_db));
if (ret != OPUS_OK)
av_log(avc, AV_LOG_WARNING, "Failed to set gain: %s\n",
opus_strerror(ret));
+#else
+ {
+ double gain_lin = pow(10, gain_db / (20.0 * 256));
+ if (avc->sample_fmt == AV_SAMPLE_FMT_FLT)
+ opus->gain.d = gain_lin;
+ else
+ opus->gain.i = FFMIN(gain_lin * 65536, INT_MAX);
+ }
+#endif
+ avc->internal->skip_samples = opus->pre_skip;
avc->delay = 3840; /* Decoder delay (in samples) at 48kHz */
avcodec_get_frame_defaults(&opus->frame);
avc->coded_frame = &opus->frame;
@@ -159,6 +168,21 @@ static int libopus_decode(AVCodecContext *avc, void *frame,
return opus_error_to_averror(nb_samples);
}
+#ifndef OPUS_SET_GAIN
+ {
+ int i = avc->channels * nb_samples;
+ if (avc->sample_fmt == AV_SAMPLE_FMT_FLT) {
+ float *pcm = (float *)opus->frame.data[0];
+ for (; i > 0; i--, pcm++)
+ *pcm = av_clipf(*pcm * opus->gain.d, -1, 1);
+ } else {
+ int16_t *pcm = (int16_t *)opus->frame.data[0];
+ for (; i > 0; i--, pcm++)
+ *pcm = av_clip_int16(((int64_t)opus->gain.i * *pcm) >> 16);
+ }
+ }
+#endif
+
opus->frame.nb_samples = nb_samples;
*(AVFrame *)frame = opus->frame;
*got_frame_ptr = 1;
@@ -170,6 +194,9 @@ static void libopus_flush(AVCodecContext *avc)
struct libopus_context *opus = avc->priv_data;
opus_multistream_decoder_ctl(opus->dec, OPUS_RESET_STATE);
+ /* The stream can have been extracted by a tool that is not Opus-aware.
+ Therefore, any packet can become the first of the stream. */
+ avc->internal->skip_samples = opus->pre_skip;
}
AVCodec ff_libopus_decoder = {