2012-06-24 12:39:05 +03:00
|
|
|
/*
|
|
|
|
* Opus decoder using libopus
|
|
|
|
* Copyright (c) 2012 Nicolas George
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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 FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <opus.h>
|
|
|
|
#include <opus_multistream.h>
|
2012-09-28 04:39:23 +03:00
|
|
|
|
2015-12-23 20:27:59 +02:00
|
|
|
#include "libavutil/internal.h"
|
2012-09-28 04:39:23 +03:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2017-11-23 21:22:56 +02:00
|
|
|
#include "libavutil/ffmath.h"
|
2018-02-05 11:54:53 +02:00
|
|
|
#include "libavutil/opt.h"
|
2016-04-14 18:24:51 +02:00
|
|
|
|
2012-06-24 12:39:05 +03:00
|
|
|
#include "avcodec.h"
|
2022-03-16 19:18:28 +02:00
|
|
|
#include "codec_internal.h"
|
2012-06-24 12:39:05 +03:00
|
|
|
#include "internal.h"
|
|
|
|
#include "vorbis.h"
|
2012-09-28 04:39:23 +03:00
|
|
|
#include "mathops.h"
|
2012-09-29 08:16:09 +03:00
|
|
|
#include "libopus.h"
|
2012-06-24 12:39:05 +03:00
|
|
|
|
|
|
|
struct libopus_context {
|
2018-02-05 11:54:53 +02:00
|
|
|
AVClass *class;
|
2012-06-24 12:39:05 +03:00
|
|
|
OpusMSDecoder *dec;
|
|
|
|
int pre_skip;
|
|
|
|
#ifndef OPUS_SET_GAIN
|
|
|
|
union { int i; double d; } gain;
|
|
|
|
#endif
|
2018-02-05 11:54:53 +02:00
|
|
|
#ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
|
|
|
|
int apply_phase_inv;
|
|
|
|
#endif
|
2012-06-24 12:39:05 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
#define OPUS_HEAD_SIZE 19
|
|
|
|
|
2012-09-28 04:39:23 +03:00
|
|
|
static av_cold int libopus_decode_init(AVCodecContext *avc)
|
2012-06-24 12:39:05 +03:00
|
|
|
{
|
|
|
|
struct libopus_context *opus = avc->priv_data;
|
2019-06-03 17:54:20 +02:00
|
|
|
int ret, channel_map = 0, gain_db = 0, nb_streams, nb_coupled, channels;
|
2012-09-27 11:23:29 +03:00
|
|
|
uint8_t mapping_arr[8] = { 0, 1 }, *mapping;
|
2012-06-24 12:39:05 +03:00
|
|
|
|
2019-06-03 17:54:20 +02:00
|
|
|
channels = avc->extradata_size >= 10 ? avc->extradata[9] : (avc->ch_layout.nb_channels == 1) ? 1 : 2;
|
|
|
|
if (channels <= 0) {
|
2016-11-14 22:41:45 +02:00
|
|
|
av_log(avc, AV_LOG_WARNING,
|
2019-06-03 17:54:20 +02:00
|
|
|
"Invalid number of channels %d, defaulting to stereo\n", channels);
|
|
|
|
channels = 2;
|
2016-11-14 22:41:45 +02:00
|
|
|
}
|
|
|
|
|
2012-09-28 04:39:23 +03:00
|
|
|
avc->sample_rate = 48000;
|
|
|
|
avc->sample_fmt = avc->request_sample_fmt == AV_SAMPLE_FMT_FLT ?
|
|
|
|
AV_SAMPLE_FMT_FLT : AV_SAMPLE_FMT_S16;
|
2019-06-03 17:54:20 +02:00
|
|
|
av_channel_layout_uninit(&avc->ch_layout);
|
|
|
|
if (channels > 8) {
|
|
|
|
avc->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
|
|
|
|
avc->ch_layout.nb_channels = channels;
|
|
|
|
} else {
|
|
|
|
av_channel_layout_copy(&avc->ch_layout, &ff_vorbis_ch_layouts[channels - 1]);
|
|
|
|
}
|
2012-06-24 12:39:05 +03:00
|
|
|
|
|
|
|
if (avc->extradata_size >= OPUS_HEAD_SIZE) {
|
|
|
|
opus->pre_skip = AV_RL16(avc->extradata + 10);
|
2012-09-28 04:39:23 +03:00
|
|
|
gain_db = sign_extend(AV_RL16(avc->extradata + 16), 16);
|
|
|
|
channel_map = AV_RL8 (avc->extradata + 18);
|
2012-06-24 12:39:05 +03:00
|
|
|
}
|
2019-06-03 17:54:20 +02:00
|
|
|
if (avc->extradata_size >= OPUS_HEAD_SIZE + 2 + channels) {
|
2012-06-24 12:39:05 +03:00
|
|
|
nb_streams = avc->extradata[OPUS_HEAD_SIZE + 0];
|
|
|
|
nb_coupled = avc->extradata[OPUS_HEAD_SIZE + 1];
|
2019-06-03 17:54:20 +02:00
|
|
|
if (nb_streams + nb_coupled != channels)
|
2012-06-24 12:39:05 +03:00
|
|
|
av_log(avc, AV_LOG_WARNING, "Inconsistent channel mapping.\n");
|
|
|
|
mapping = avc->extradata + OPUS_HEAD_SIZE + 2;
|
|
|
|
} else {
|
2019-06-03 17:54:20 +02:00
|
|
|
if (channels > 2 || channel_map) {
|
2012-06-24 12:39:05 +03:00
|
|
|
av_log(avc, AV_LOG_ERROR,
|
2019-06-03 17:54:20 +02:00
|
|
|
"No channel mapping for %d channels.\n", channels);
|
2012-06-24 12:39:05 +03:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
nb_streams = 1;
|
2019-06-03 17:54:20 +02:00
|
|
|
nb_coupled = channels > 1;
|
2012-09-27 11:23:29 +03:00
|
|
|
mapping = mapping_arr;
|
|
|
|
}
|
|
|
|
|
2019-06-03 17:54:20 +02:00
|
|
|
if (channels > 2 && channels <= 8) {
|
|
|
|
const uint8_t *vorbis_offset = ff_vorbis_channel_layout_offsets[channels - 1];
|
2018-11-29 01:54:08 +02:00
|
|
|
int ch;
|
2012-09-27 11:23:29 +03:00
|
|
|
|
2018-11-29 01:54:08 +02:00
|
|
|
/* Remap channels from Vorbis order to ffmpeg order */
|
2019-06-03 17:54:20 +02:00
|
|
|
for (ch = 0; ch < channels; ch++)
|
2018-11-29 01:54:08 +02:00
|
|
|
mapping_arr[ch] = mapping[vorbis_offset[ch]];
|
|
|
|
mapping = mapping_arr;
|
2012-06-24 12:39:05 +03:00
|
|
|
}
|
|
|
|
|
2019-06-03 17:54:20 +02:00
|
|
|
opus->dec = opus_multistream_decoder_create(avc->sample_rate, channels,
|
2012-09-28 04:39:23 +03:00
|
|
|
nb_streams, nb_coupled,
|
|
|
|
mapping, &ret);
|
2012-06-24 12:39:05 +03:00
|
|
|
if (!opus->dec) {
|
|
|
|
av_log(avc, AV_LOG_ERROR, "Unable to create decoder: %s\n",
|
|
|
|
opus_strerror(ret));
|
2012-09-29 08:16:09 +03:00
|
|
|
return ff_opus_error_to_averror(ret);
|
2012-06-24 12:39:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#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
|
|
|
|
{
|
2015-12-23 20:27:59 +02:00
|
|
|
double gain_lin = ff_exp10(gain_db / (20.0 * 256));
|
2012-06-24 12:39:05 +03:00
|
|
|
if (avc->sample_fmt == AV_SAMPLE_FMT_FLT)
|
|
|
|
opus->gain.d = gain_lin;
|
|
|
|
else
|
|
|
|
opus->gain.i = FFMIN(gain_lin * 65536, INT_MAX);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-02-05 11:54:53 +02:00
|
|
|
#ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
|
|
|
|
ret = opus_multistream_decoder_ctl(opus->dec,
|
|
|
|
OPUS_SET_PHASE_INVERSION_DISABLED(!opus->apply_phase_inv));
|
|
|
|
if (ret != OPUS_OK)
|
|
|
|
av_log(avc, AV_LOG_WARNING,
|
|
|
|
"Unable to set phase inversion: %s\n",
|
|
|
|
opus_strerror(ret));
|
|
|
|
#endif
|
|
|
|
|
2013-12-20 09:10:41 +03:00
|
|
|
/* Decoder delay (in samples) at 48kHz */
|
|
|
|
avc->delay = avc->internal->skip_samples = opus->pre_skip;
|
2012-12-24 02:45:55 +03:00
|
|
|
|
2012-06-24 12:39:05 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-09-28 04:39:23 +03:00
|
|
|
static av_cold int libopus_decode_close(AVCodecContext *avc)
|
2012-06-24 12:39:05 +03:00
|
|
|
{
|
|
|
|
struct libopus_context *opus = avc->priv_data;
|
|
|
|
|
2018-04-10 22:59:25 +02:00
|
|
|
if (opus->dec) {
|
|
|
|
opus_multistream_decoder_destroy(opus->dec);
|
|
|
|
opus->dec = NULL;
|
|
|
|
}
|
2012-06-24 12:39:05 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-09-28 04:39:23 +03:00
|
|
|
#define MAX_FRAME_SIZE (960 * 6)
|
2012-06-24 12:39:05 +03:00
|
|
|
|
2022-03-30 21:33:24 +02:00
|
|
|
static int libopus_decode(AVCodecContext *avc, AVFrame *frame,
|
2012-09-28 04:39:23 +03:00
|
|
|
int *got_frame_ptr, AVPacket *pkt)
|
2012-06-24 12:39:05 +03:00
|
|
|
{
|
|
|
|
struct libopus_context *opus = avc->priv_data;
|
|
|
|
int ret, nb_samples;
|
|
|
|
|
2012-12-24 02:45:55 +03:00
|
|
|
frame->nb_samples = MAX_FRAME_SIZE;
|
lavc: factorize ff_{thread_,re,}get_buffer error messages.
Coccinelle profile used:
@@
expression r, ctx, f, loglevel, str, flags;
@@
-if ((r = ff_get_buffer(ctx, f, flags)) < 0) {
- av_log(ctx, loglevel, str);
- return r;
-}
+if ((r = ff_get_buffer(ctx, f, flags)) < 0)
+ return r;
@@
expression r, ctx, f, loglevel, str;
@@
-if ((r = ff_reget_buffer(ctx, f)) < 0) {
- av_log(ctx, loglevel, str);
- return r;
-}
+if ((r = ff_reget_buffer(ctx, f)) < 0)
+ return r;
@@
expression r, ctx, f, loglevel, str, flags;
@@
-if ((r = ff_thread_get_buffer(ctx, f, flags)) < 0) {
- av_log(ctx, loglevel, str);
- return r;
-}
+if ((r = ff_thread_get_buffer(ctx, f, flags)) < 0)
+ return r;
...along with some manual patches for the remaining ones.
2013-03-12 10:41:53 +03:00
|
|
|
if ((ret = ff_get_buffer(avc, frame, 0)) < 0)
|
2012-06-24 12:39:05 +03:00
|
|
|
return ret;
|
|
|
|
|
2012-09-28 04:39:23 +03:00
|
|
|
if (avc->sample_fmt == AV_SAMPLE_FMT_S16)
|
|
|
|
nb_samples = opus_multistream_decode(opus->dec, pkt->data, pkt->size,
|
2012-12-24 02:45:55 +03:00
|
|
|
(opus_int16 *)frame->data[0],
|
|
|
|
frame->nb_samples, 0);
|
2012-09-28 04:39:23 +03:00
|
|
|
else
|
|
|
|
nb_samples = opus_multistream_decode_float(opus->dec, pkt->data, pkt->size,
|
2012-12-24 02:45:55 +03:00
|
|
|
(float *)frame->data[0],
|
|
|
|
frame->nb_samples, 0);
|
2012-09-28 04:39:23 +03:00
|
|
|
|
2012-06-24 12:39:05 +03:00
|
|
|
if (nb_samples < 0) {
|
|
|
|
av_log(avc, AV_LOG_ERROR, "Decoding error: %s\n",
|
|
|
|
opus_strerror(nb_samples));
|
2012-09-29 08:16:09 +03:00
|
|
|
return ff_opus_error_to_averror(nb_samples);
|
2012-06-24 12:39:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef OPUS_SET_GAIN
|
|
|
|
{
|
2019-06-03 17:54:20 +02:00
|
|
|
int i = avc->ch_layout.nb_channels * nb_samples;
|
2012-06-24 12:39:05 +03:00
|
|
|
if (avc->sample_fmt == AV_SAMPLE_FMT_FLT) {
|
2013-02-13 14:14:46 +03:00
|
|
|
float *pcm = (float *)frame->data[0];
|
2012-06-24 12:39:05 +03:00
|
|
|
for (; i > 0; i--, pcm++)
|
|
|
|
*pcm = av_clipf(*pcm * opus->gain.d, -1, 1);
|
|
|
|
} else {
|
2013-02-13 14:14:46 +03:00
|
|
|
int16_t *pcm = (int16_t *)frame->data[0];
|
2012-06-24 12:39:05 +03:00
|
|
|
for (; i > 0; i--, pcm++)
|
|
|
|
*pcm = av_clip_int16(((int64_t)opus->gain.i * *pcm) >> 16);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-12-24 02:45:55 +03:00
|
|
|
frame->nb_samples = nb_samples;
|
|
|
|
*got_frame_ptr = 1;
|
|
|
|
|
2012-06-24 12:39:05 +03:00
|
|
|
return pkt->size;
|
|
|
|
}
|
|
|
|
|
2012-09-28 04:39:23 +03:00
|
|
|
static void libopus_flush(AVCodecContext *avc)
|
2012-06-24 12:39:05 +03:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-02-05 11:54:53 +02:00
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(struct libopus_context, x)
|
|
|
|
#define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
|
|
|
|
static const AVOption libopusdec_options[] = {
|
|
|
|
#ifdef OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST
|
|
|
|
{ "apply_phase_inv", "Apply intensity stereo phase inversion", OFFSET(apply_phase_inv), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
|
|
|
|
#endif
|
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVClass libopusdec_class = {
|
|
|
|
.class_name = "libopusdec",
|
|
|
|
.item_name = av_default_item_name,
|
|
|
|
.option = libopusdec_options,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2022-03-16 22:09:54 +02:00
|
|
|
const FFCodec ff_libopus_decoder = {
|
|
|
|
.p.name = "libopus",
|
|
|
|
.p.long_name = NULL_IF_CONFIG_SMALL("libopus Opus"),
|
|
|
|
.p.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.p.id = AV_CODEC_ID_OPUS,
|
2012-06-24 12:39:05 +03:00
|
|
|
.priv_data_size = sizeof(struct libopus_context),
|
2012-09-28 04:39:23 +03:00
|
|
|
.init = libopus_decode_init,
|
|
|
|
.close = libopus_decode_close,
|
2022-03-30 23:28:24 +02:00
|
|
|
FF_CODEC_DECODE_CB(libopus_decode),
|
2012-09-28 04:39:23 +03:00
|
|
|
.flush = libopus_flush,
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
|
2018-04-10 22:59:25 +02:00
|
|
|
.caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLT,
|
2012-09-28 04:39:23 +03:00
|
|
|
AV_SAMPLE_FMT_S16,
|
|
|
|
AV_SAMPLE_FMT_NONE },
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.priv_class = &libopusdec_class,
|
|
|
|
.p.wrapper_name = "libopus",
|
2012-06-24 12:39:05 +03:00
|
|
|
};
|