2014-04-17 13:51:03 +03:00
|
|
|
/*
|
|
|
|
* Opus decoder
|
|
|
|
* Copyright (c) 2012 Andrew D'Addesio
|
|
|
|
* Copyright (c) 2013-2014 Mozilla Corporation
|
|
|
|
*
|
2014-05-15 15:50:12 +03:00
|
|
|
* This file is part of FFmpeg.
|
2014-04-17 13:51:03 +03:00
|
|
|
*
|
2014-05-15 15:50:12 +03:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2014-04-17 13:51:03 +03:00
|
|
|
* 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.
|
|
|
|
*
|
2014-05-15 15:50:12 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2014-04-17 13:51:03 +03:00
|
|
|
* 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
|
2014-05-15 15:50:12 +03:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2014-04-17 13:51:03 +03:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Opus decoder
|
|
|
|
* @author Andrew D'Addesio, Anton Khirnov
|
|
|
|
*
|
|
|
|
* Codec homepage: http://opus-codec.org/
|
|
|
|
* Specification: http://tools.ietf.org/html/rfc6716
|
|
|
|
* Ogg Opus specification: https://tools.ietf.org/html/draft-ietf-codec-oggopus-03
|
|
|
|
*
|
|
|
|
* Ogg-contained .opus files can be produced with opus-tools:
|
|
|
|
* http://git.xiph.org/?p=opus-tools.git
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include "libavutil/attributes.h"
|
|
|
|
#include "libavutil/audio_fifo.h"
|
|
|
|
#include "libavutil/channel_layout.h"
|
|
|
|
#include "libavutil/opt.h"
|
|
|
|
|
2014-05-15 19:28:45 +03:00
|
|
|
#include "libswresample/swresample.h"
|
2014-04-17 13:51:03 +03:00
|
|
|
|
|
|
|
#include "avcodec.h"
|
2022-03-16 19:18:28 +02:00
|
|
|
#include "codec_internal.h"
|
2022-08-24 21:28:16 +02:00
|
|
|
#include "decode.h"
|
2014-04-17 13:51:03 +03:00
|
|
|
#include "opus.h"
|
2016-12-18 09:10:48 +02:00
|
|
|
#include "opustab.h"
|
2017-02-11 02:25:07 +02:00
|
|
|
#include "opus_celt.h"
|
2014-04-17 13:51:03 +03:00
|
|
|
|
|
|
|
static const uint16_t silk_frame_duration_ms[16] = {
|
|
|
|
10, 20, 40, 60,
|
|
|
|
10, 20, 40, 60,
|
|
|
|
10, 20, 40, 60,
|
|
|
|
10, 20,
|
|
|
|
10, 20,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* number of samples of silence to feed to the resampler
|
|
|
|
* at the beginning */
|
|
|
|
static const int silk_resample_delay[] = {
|
|
|
|
4, 8, 11, 11, 11
|
|
|
|
};
|
|
|
|
|
|
|
|
static int get_silk_samplerate(int config)
|
|
|
|
{
|
|
|
|
if (config < 4)
|
|
|
|
return 8000;
|
|
|
|
else if (config < 8)
|
|
|
|
return 12000;
|
|
|
|
return 16000;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void opus_fade(float *out,
|
|
|
|
const float *in1, const float *in2,
|
|
|
|
const float *window, int len)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
out[i] = in2[i] * window[i] + in1[i] * (1.0 - window[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int opus_flush_resample(OpusStreamContext *s, int nb_samples)
|
|
|
|
{
|
|
|
|
int celt_size = av_audio_fifo_size(s->celt_delay);
|
|
|
|
int ret, i;
|
2014-05-15 19:28:45 +03:00
|
|
|
ret = swr_convert(s->swr,
|
2020-12-28 18:46:44 +02:00
|
|
|
(uint8_t**)s->cur_out, nb_samples,
|
2014-05-15 19:28:45 +03:00
|
|
|
NULL, 0);
|
2014-04-17 13:51:03 +03:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
else if (ret != nb_samples) {
|
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "Wrong number of flushed samples: %d\n",
|
|
|
|
ret);
|
|
|
|
return AVERROR_BUG;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (celt_size) {
|
|
|
|
if (celt_size != nb_samples) {
|
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "Wrong number of CELT delay samples.\n");
|
|
|
|
return AVERROR_BUG;
|
|
|
|
}
|
|
|
|
av_audio_fifo_read(s->celt_delay, (void**)s->celt_output, nb_samples);
|
|
|
|
for (i = 0; i < s->output_channels; i++) {
|
2020-12-28 18:46:44 +02:00
|
|
|
s->fdsp->vector_fmac_scalar(s->cur_out[i],
|
2014-04-17 13:51:03 +03:00
|
|
|
s->celt_output[i], 1.0,
|
|
|
|
nb_samples);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->redundancy_idx) {
|
|
|
|
for (i = 0; i < s->output_channels; i++)
|
2020-12-28 18:46:44 +02:00
|
|
|
opus_fade(s->cur_out[i], s->cur_out[i],
|
2014-04-17 13:51:03 +03:00
|
|
|
s->redundancy_output[i] + 120 + s->redundancy_idx,
|
|
|
|
ff_celt_window2 + s->redundancy_idx, 120 - s->redundancy_idx);
|
|
|
|
s->redundancy_idx = 0;
|
|
|
|
}
|
|
|
|
|
2020-12-28 18:46:44 +02:00
|
|
|
s->cur_out[0] += nb_samples;
|
|
|
|
s->cur_out[1] += nb_samples;
|
|
|
|
s->remaining_out_size -= nb_samples * sizeof(float);
|
2014-04-17 13:51:03 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int opus_init_resample(OpusStreamContext *s)
|
|
|
|
{
|
2014-05-16 05:38:10 +03:00
|
|
|
static const float delay[16] = { 0.0 };
|
|
|
|
const uint8_t *delayptr[2] = { (uint8_t*)delay, (uint8_t*)delay };
|
2014-04-17 13:51:03 +03:00
|
|
|
int ret;
|
|
|
|
|
2014-05-15 19:28:45 +03:00
|
|
|
av_opt_set_int(s->swr, "in_sample_rate", s->silk_samplerate, 0);
|
|
|
|
ret = swr_init(s->swr);
|
2014-04-17 13:51:03 +03:00
|
|
|
if (ret < 0) {
|
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "Error opening the resampler.\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-05-15 19:28:45 +03:00
|
|
|
ret = swr_convert(s->swr,
|
|
|
|
NULL, 0,
|
|
|
|
delayptr, silk_resample_delay[s->packet.bandwidth]);
|
2014-04-17 13:51:03 +03:00
|
|
|
if (ret < 0) {
|
|
|
|
av_log(s->avctx, AV_LOG_ERROR,
|
|
|
|
"Error feeding initial silence to the resampler.\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int opus_decode_redundancy(OpusStreamContext *s, const uint8_t *data, int size)
|
|
|
|
{
|
2017-07-06 19:51:18 +02:00
|
|
|
int ret = ff_opus_rc_dec_init(&s->redundancy_rc, data, size);
|
2014-04-17 13:51:03 +03:00
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
2016-11-08 00:33:11 +02:00
|
|
|
ff_opus_rc_dec_raw_init(&s->redundancy_rc, data + size, size);
|
2014-04-17 13:51:03 +03:00
|
|
|
|
|
|
|
ret = ff_celt_decode_frame(s->celt, &s->redundancy_rc,
|
|
|
|
s->redundancy_output,
|
|
|
|
s->packet.stereo + 1, 240,
|
2017-02-11 02:25:07 +02:00
|
|
|
0, ff_celt_band_end[s->packet.bandwidth]);
|
2014-04-17 13:51:03 +03:00
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
fail:
|
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "Error decoding the redundancy frame.\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int opus_decode_frame(OpusStreamContext *s, const uint8_t *data, int size)
|
|
|
|
{
|
|
|
|
int samples = s->packet.frame_duration;
|
|
|
|
int redundancy = 0;
|
|
|
|
int redundancy_size, redundancy_pos;
|
|
|
|
int ret, i, consumed;
|
|
|
|
int delayed_samples = s->delayed_samples;
|
|
|
|
|
2016-11-08 00:33:11 +02:00
|
|
|
ret = ff_opus_rc_dec_init(&s->rc, data, size);
|
2014-04-17 13:51:03 +03:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* decode the silk frame */
|
|
|
|
if (s->packet.mode == OPUS_MODE_SILK || s->packet.mode == OPUS_MODE_HYBRID) {
|
2014-05-15 19:28:45 +03:00
|
|
|
if (!swr_is_initialized(s->swr)) {
|
2014-04-17 13:51:03 +03:00
|
|
|
ret = opus_init_resample(s);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
samples = ff_silk_decode_superframe(s->silk, &s->rc, s->silk_output,
|
|
|
|
FFMIN(s->packet.bandwidth, OPUS_BANDWIDTH_WIDEBAND),
|
|
|
|
s->packet.stereo + 1,
|
|
|
|
silk_frame_duration_ms[s->packet.config]);
|
|
|
|
if (samples < 0) {
|
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "Error decoding a SILK frame.\n");
|
|
|
|
return samples;
|
|
|
|
}
|
2014-05-15 19:28:45 +03:00
|
|
|
samples = swr_convert(s->swr,
|
2020-12-28 18:46:44 +02:00
|
|
|
(uint8_t**)s->cur_out, s->packet.frame_duration,
|
2014-05-16 05:38:10 +03:00
|
|
|
(const uint8_t**)s->silk_output, samples);
|
2014-04-17 13:51:03 +03:00
|
|
|
if (samples < 0) {
|
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "Error resampling SILK data.\n");
|
|
|
|
return samples;
|
|
|
|
}
|
2014-05-25 01:38:20 +03:00
|
|
|
av_assert2((samples & 7) == 0);
|
2014-04-17 13:51:03 +03:00
|
|
|
s->delayed_samples += s->packet.frame_duration - samples;
|
|
|
|
} else
|
|
|
|
ff_silk_flush(s->silk);
|
|
|
|
|
|
|
|
// decode redundancy information
|
|
|
|
consumed = opus_rc_tell(&s->rc);
|
|
|
|
if (s->packet.mode == OPUS_MODE_HYBRID && consumed + 37 <= size * 8)
|
2016-11-08 00:33:11 +02:00
|
|
|
redundancy = ff_opus_rc_dec_log(&s->rc, 12);
|
2014-04-17 13:51:03 +03:00
|
|
|
else if (s->packet.mode == OPUS_MODE_SILK && consumed + 17 <= size * 8)
|
|
|
|
redundancy = 1;
|
|
|
|
|
|
|
|
if (redundancy) {
|
2016-11-08 00:33:11 +02:00
|
|
|
redundancy_pos = ff_opus_rc_dec_log(&s->rc, 1);
|
2014-04-17 13:51:03 +03:00
|
|
|
|
|
|
|
if (s->packet.mode == OPUS_MODE_HYBRID)
|
2016-11-08 00:33:11 +02:00
|
|
|
redundancy_size = ff_opus_rc_dec_uint(&s->rc, 256) + 2;
|
2014-04-17 13:51:03 +03:00
|
|
|
else
|
|
|
|
redundancy_size = size - (consumed + 7) / 8;
|
|
|
|
size -= redundancy_size;
|
|
|
|
if (size < 0) {
|
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "Invalid redundancy frame size.\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (redundancy_pos) {
|
|
|
|
ret = opus_decode_redundancy(s, data + size, redundancy_size);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
ff_celt_flush(s->celt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* decode the CELT frame */
|
|
|
|
if (s->packet.mode == OPUS_MODE_CELT || s->packet.mode == OPUS_MODE_HYBRID) {
|
2020-12-28 18:46:44 +02:00
|
|
|
float *out_tmp[2] = { s->cur_out[0], s->cur_out[1] };
|
2014-04-17 13:51:03 +03:00
|
|
|
float **dst = (s->packet.mode == OPUS_MODE_CELT) ?
|
|
|
|
out_tmp : s->celt_output;
|
|
|
|
int celt_output_samples = samples;
|
|
|
|
int delay_samples = av_audio_fifo_size(s->celt_delay);
|
|
|
|
|
|
|
|
if (delay_samples) {
|
|
|
|
if (s->packet.mode == OPUS_MODE_HYBRID) {
|
|
|
|
av_audio_fifo_read(s->celt_delay, (void**)s->celt_output, delay_samples);
|
|
|
|
|
|
|
|
for (i = 0; i < s->output_channels; i++) {
|
|
|
|
s->fdsp->vector_fmac_scalar(out_tmp[i], s->celt_output[i], 1.0,
|
|
|
|
delay_samples);
|
|
|
|
out_tmp[i] += delay_samples;
|
|
|
|
}
|
|
|
|
celt_output_samples -= delay_samples;
|
|
|
|
} else {
|
|
|
|
av_log(s->avctx, AV_LOG_WARNING,
|
|
|
|
"Spurious CELT delay samples present.\n");
|
|
|
|
av_audio_fifo_drain(s->celt_delay, delay_samples);
|
|
|
|
if (s->avctx->err_recognition & AV_EF_EXPLODE)
|
|
|
|
return AVERROR_BUG;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-08 00:33:11 +02:00
|
|
|
ff_opus_rc_dec_raw_init(&s->rc, data + size, size);
|
2014-04-17 13:51:03 +03:00
|
|
|
|
|
|
|
ret = ff_celt_decode_frame(s->celt, &s->rc, dst,
|
|
|
|
s->packet.stereo + 1,
|
|
|
|
s->packet.frame_duration,
|
|
|
|
(s->packet.mode == OPUS_MODE_HYBRID) ? 17 : 0,
|
2017-02-11 02:25:07 +02:00
|
|
|
ff_celt_band_end[s->packet.bandwidth]);
|
2014-04-17 13:51:03 +03:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (s->packet.mode == OPUS_MODE_HYBRID) {
|
|
|
|
int celt_delay = s->packet.frame_duration - celt_output_samples;
|
|
|
|
void *delaybuf[2] = { s->celt_output[0] + celt_output_samples,
|
|
|
|
s->celt_output[1] + celt_output_samples };
|
|
|
|
|
|
|
|
for (i = 0; i < s->output_channels; i++) {
|
|
|
|
s->fdsp->vector_fmac_scalar(out_tmp[i],
|
|
|
|
s->celt_output[i], 1.0,
|
|
|
|
celt_output_samples);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = av_audio_fifo_write(s->celt_delay, delaybuf, celt_delay);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
ff_celt_flush(s->celt);
|
|
|
|
|
|
|
|
if (s->redundancy_idx) {
|
|
|
|
for (i = 0; i < s->output_channels; i++)
|
2020-12-28 18:46:44 +02:00
|
|
|
opus_fade(s->cur_out[i], s->cur_out[i],
|
2014-04-17 13:51:03 +03:00
|
|
|
s->redundancy_output[i] + 120 + s->redundancy_idx,
|
|
|
|
ff_celt_window2 + s->redundancy_idx, 120 - s->redundancy_idx);
|
|
|
|
s->redundancy_idx = 0;
|
|
|
|
}
|
|
|
|
if (redundancy) {
|
|
|
|
if (!redundancy_pos) {
|
|
|
|
ff_celt_flush(s->celt);
|
|
|
|
ret = opus_decode_redundancy(s, data + size, redundancy_size);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
for (i = 0; i < s->output_channels; i++) {
|
2020-12-28 18:46:44 +02:00
|
|
|
opus_fade(s->cur_out[i] + samples - 120 + delayed_samples,
|
|
|
|
s->cur_out[i] + samples - 120 + delayed_samples,
|
2014-04-17 13:51:03 +03:00
|
|
|
s->redundancy_output[i] + 120,
|
|
|
|
ff_celt_window2, 120 - delayed_samples);
|
|
|
|
if (delayed_samples)
|
|
|
|
s->redundancy_idx = 120 - delayed_samples;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (i = 0; i < s->output_channels; i++) {
|
2020-12-28 18:46:44 +02:00
|
|
|
memcpy(s->cur_out[i] + delayed_samples, s->redundancy_output[i], 120 * sizeof(float));
|
|
|
|
opus_fade(s->cur_out[i] + 120 + delayed_samples,
|
2014-04-17 13:51:03 +03:00
|
|
|
s->redundancy_output[i] + 120,
|
2020-12-28 18:46:44 +02:00
|
|
|
s->cur_out[i] + 120 + delayed_samples,
|
2014-04-17 13:51:03 +03:00
|
|
|
ff_celt_window2, 120);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return samples;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int opus_decode_subpacket(OpusStreamContext *s,
|
|
|
|
const uint8_t *buf, int buf_size,
|
|
|
|
int nb_samples)
|
|
|
|
{
|
|
|
|
int output_samples = 0;
|
|
|
|
int flush_needed = 0;
|
|
|
|
int i, j, ret;
|
|
|
|
|
2020-12-28 18:46:44 +02:00
|
|
|
s->cur_out[0] = s->out[0];
|
|
|
|
s->cur_out[1] = s->out[1];
|
|
|
|
s->remaining_out_size = s->out_size;
|
2015-07-27 11:13:53 +02:00
|
|
|
|
2014-04-17 13:51:03 +03:00
|
|
|
/* check if we need to flush the resampler */
|
2014-05-15 19:28:45 +03:00
|
|
|
if (swr_is_initialized(s->swr)) {
|
2014-04-17 13:51:03 +03:00
|
|
|
if (buf) {
|
|
|
|
int64_t cur_samplerate;
|
2014-05-15 19:28:45 +03:00
|
|
|
av_opt_get_int(s->swr, "in_sample_rate", 0, &cur_samplerate);
|
2014-04-17 13:51:03 +03:00
|
|
|
flush_needed = (s->packet.mode == OPUS_MODE_CELT) || (cur_samplerate != s->silk_samplerate);
|
|
|
|
} else {
|
|
|
|
flush_needed = !!s->delayed_samples;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!buf && !flush_needed)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* use dummy output buffers if the channel is not mapped to anything */
|
2020-12-28 18:46:44 +02:00
|
|
|
if (!s->cur_out[0] ||
|
|
|
|
(s->output_channels == 2 && !s->cur_out[1])) {
|
|
|
|
av_fast_malloc(&s->out_dummy, &s->out_dummy_allocated_size,
|
|
|
|
s->remaining_out_size);
|
2014-04-17 13:51:03 +03:00
|
|
|
if (!s->out_dummy)
|
|
|
|
return AVERROR(ENOMEM);
|
2020-12-28 18:46:44 +02:00
|
|
|
if (!s->cur_out[0])
|
|
|
|
s->cur_out[0] = s->out_dummy;
|
|
|
|
if (!s->cur_out[1])
|
|
|
|
s->cur_out[1] = s->out_dummy;
|
2014-04-17 13:51:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* flush the resampler if necessary */
|
|
|
|
if (flush_needed) {
|
|
|
|
ret = opus_flush_resample(s, s->delayed_samples);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "Error flushing the resampler.\n");
|
|
|
|
return ret;
|
|
|
|
}
|
2014-05-15 19:28:45 +03:00
|
|
|
swr_close(s->swr);
|
2014-04-17 13:51:03 +03:00
|
|
|
output_samples += s->delayed_samples;
|
|
|
|
s->delayed_samples = 0;
|
|
|
|
|
|
|
|
if (!buf)
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* decode all the frames in the packet */
|
|
|
|
for (i = 0; i < s->packet.frame_count; i++) {
|
|
|
|
int size = s->packet.frame_size[i];
|
|
|
|
int samples = opus_decode_frame(s, buf + s->packet.frame_offset[i], size);
|
|
|
|
|
|
|
|
if (samples < 0) {
|
|
|
|
av_log(s->avctx, AV_LOG_ERROR, "Error decoding an Opus frame.\n");
|
|
|
|
if (s->avctx->err_recognition & AV_EF_EXPLODE)
|
|
|
|
return samples;
|
|
|
|
|
|
|
|
for (j = 0; j < s->output_channels; j++)
|
2020-12-28 18:46:44 +02:00
|
|
|
memset(s->cur_out[j], 0, s->packet.frame_duration * sizeof(float));
|
2014-04-17 13:51:03 +03:00
|
|
|
samples = s->packet.frame_duration;
|
|
|
|
}
|
|
|
|
output_samples += samples;
|
|
|
|
|
|
|
|
for (j = 0; j < s->output_channels; j++)
|
2020-12-28 18:46:44 +02:00
|
|
|
s->cur_out[j] += samples;
|
|
|
|
s->remaining_out_size -= samples * sizeof(float);
|
2014-04-17 13:51:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
finish:
|
2020-12-28 18:46:44 +02:00
|
|
|
s->cur_out[0] = s->cur_out[1] = NULL;
|
|
|
|
s->remaining_out_size = 0;
|
2014-04-17 13:51:03 +03:00
|
|
|
|
|
|
|
return output_samples;
|
|
|
|
}
|
|
|
|
|
2022-03-30 21:33:24 +02:00
|
|
|
static int opus_decode_packet(AVCodecContext *avctx, AVFrame *frame,
|
2014-04-17 13:51:03 +03:00
|
|
|
int *got_frame_ptr, AVPacket *avpkt)
|
|
|
|
{
|
|
|
|
OpusContext *c = avctx->priv_data;
|
|
|
|
const uint8_t *buf = avpkt->data;
|
|
|
|
int buf_size = avpkt->size;
|
|
|
|
int coded_samples = 0;
|
2015-07-27 11:13:53 +02:00
|
|
|
int decoded_samples = INT_MAX;
|
2015-03-07 16:34:19 +02:00
|
|
|
int delayed_samples = 0;
|
2014-04-17 13:51:03 +03:00
|
|
|
int i, ret;
|
|
|
|
|
2015-07-27 11:13:53 +02:00
|
|
|
/* calculate the number of delayed samples */
|
2015-03-07 16:11:39 +02:00
|
|
|
for (i = 0; i < c->nb_streams; i++) {
|
|
|
|
OpusStreamContext *s = &c->streams[i];
|
|
|
|
s->out[0] =
|
|
|
|
s->out[1] = NULL;
|
2015-07-27 11:13:53 +02:00
|
|
|
delayed_samples = FFMAX(delayed_samples,
|
2020-12-28 18:46:44 +02:00
|
|
|
s->delayed_samples + av_audio_fifo_size(s->sync_buffer));
|
2015-03-07 16:11:39 +02:00
|
|
|
}
|
|
|
|
|
2014-04-17 13:51:03 +03:00
|
|
|
/* decode the header of the first sub-packet to find out the sample count */
|
|
|
|
if (buf) {
|
|
|
|
OpusPacket *pkt = &c->streams[0].packet;
|
|
|
|
ret = ff_opus_parse_packet(pkt, buf, buf_size, c->nb_streams > 1);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Error parsing the packet header.\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
coded_samples += pkt->frame_count * pkt->frame_duration;
|
|
|
|
c->streams[0].silk_samplerate = get_silk_samplerate(pkt->config);
|
|
|
|
}
|
|
|
|
|
2015-03-07 16:34:19 +02:00
|
|
|
frame->nb_samples = coded_samples + delayed_samples;
|
2014-04-17 13:51:03 +03:00
|
|
|
|
|
|
|
/* no input or buffered data => nothing to do */
|
|
|
|
if (!frame->nb_samples) {
|
|
|
|
*got_frame_ptr = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* setup the data buffers */
|
|
|
|
ret = ff_get_buffer(avctx, frame, 0);
|
2015-02-10 16:20:32 +02:00
|
|
|
if (ret < 0)
|
2014-04-17 13:51:03 +03:00
|
|
|
return ret;
|
|
|
|
frame->nb_samples = 0;
|
|
|
|
|
2019-05-28 14:38:10 +02:00
|
|
|
for (i = 0; i < avctx->ch_layout.nb_channels; i++) {
|
2014-04-17 13:51:03 +03:00
|
|
|
ChannelMap *map = &c->channel_maps[i];
|
|
|
|
if (!map->copy)
|
2020-12-28 18:46:44 +02:00
|
|
|
c->streams[map->stream_idx].out[map->channel_idx] = (float*)frame->extended_data[i];
|
2014-04-17 13:51:03 +03:00
|
|
|
}
|
|
|
|
|
2015-07-27 11:13:53 +02:00
|
|
|
/* read the data from the sync buffers */
|
|
|
|
for (i = 0; i < c->nb_streams; i++) {
|
2020-12-28 18:46:44 +02:00
|
|
|
OpusStreamContext *s = &c->streams[i];
|
|
|
|
float **out = s->out;
|
|
|
|
int sync_size = av_audio_fifo_size(s->sync_buffer);
|
2015-07-27 11:13:53 +02:00
|
|
|
|
|
|
|
float sync_dummy[32];
|
|
|
|
int out_dummy = (!out[0]) | ((!out[1]) << 1);
|
|
|
|
|
|
|
|
if (!out[0])
|
|
|
|
out[0] = sync_dummy;
|
|
|
|
if (!out[1])
|
|
|
|
out[1] = sync_dummy;
|
|
|
|
if (out_dummy && sync_size > FF_ARRAY_ELEMS(sync_dummy))
|
|
|
|
return AVERROR_BUG;
|
|
|
|
|
2020-12-28 18:46:44 +02:00
|
|
|
ret = av_audio_fifo_read(s->sync_buffer, (void**)out, sync_size);
|
2015-07-27 11:13:53 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (out_dummy & 1)
|
|
|
|
out[0] = NULL;
|
|
|
|
else
|
|
|
|
out[0] += ret;
|
|
|
|
if (out_dummy & 2)
|
|
|
|
out[1] = NULL;
|
|
|
|
else
|
|
|
|
out[1] += ret;
|
|
|
|
|
2020-12-28 18:46:44 +02:00
|
|
|
s->out_size = frame->linesize[0] - ret * sizeof(float);
|
2015-07-27 11:13:53 +02:00
|
|
|
}
|
2014-04-17 13:51:03 +03:00
|
|
|
|
|
|
|
/* decode each sub-packet */
|
|
|
|
for (i = 0; i < c->nb_streams; i++) {
|
|
|
|
OpusStreamContext *s = &c->streams[i];
|
|
|
|
|
|
|
|
if (i && buf) {
|
|
|
|
ret = ff_opus_parse_packet(&s->packet, buf, buf_size, i != c->nb_streams - 1);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Error parsing the packet header.\n");
|
|
|
|
return ret;
|
|
|
|
}
|
2014-11-24 12:16:46 +02:00
|
|
|
if (coded_samples != s->packet.frame_count * s->packet.frame_duration) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"Mismatching coded sample count in substream %d.\n", i);
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2014-04-17 13:51:03 +03:00
|
|
|
s->silk_samplerate = get_silk_samplerate(s->packet.config);
|
|
|
|
}
|
|
|
|
|
2015-07-27 11:13:53 +02:00
|
|
|
ret = opus_decode_subpacket(&c->streams[i], buf, s->packet.data_size,
|
2020-12-28 18:46:44 +02:00
|
|
|
coded_samples);
|
2014-04-17 13:51:03 +03:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2020-12-28 18:46:44 +02:00
|
|
|
s->decoded_samples = ret;
|
2015-07-27 11:13:53 +02:00
|
|
|
decoded_samples = FFMIN(decoded_samples, ret);
|
|
|
|
|
2014-04-17 13:51:03 +03:00
|
|
|
buf += s->packet.packet_size;
|
|
|
|
buf_size -= s->packet.packet_size;
|
|
|
|
}
|
|
|
|
|
2015-07-27 11:13:53 +02:00
|
|
|
/* buffer the extra samples */
|
|
|
|
for (i = 0; i < c->nb_streams; i++) {
|
2020-12-28 18:46:44 +02:00
|
|
|
OpusStreamContext *s = &c->streams[i];
|
|
|
|
int buffer_samples = s->decoded_samples - decoded_samples;
|
2015-07-27 11:13:53 +02:00
|
|
|
if (buffer_samples) {
|
2020-12-28 18:46:44 +02:00
|
|
|
float *buf[2] = { s->out[0] ? s->out[0] : (float*)frame->extended_data[0],
|
|
|
|
s->out[1] ? s->out[1] : (float*)frame->extended_data[0] };
|
2015-10-27 02:18:22 +02:00
|
|
|
buf[0] += decoded_samples;
|
|
|
|
buf[1] += decoded_samples;
|
2020-12-28 18:46:44 +02:00
|
|
|
ret = av_audio_fifo_write(s->sync_buffer, (void**)buf, buffer_samples);
|
2015-07-27 11:13:53 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 14:38:10 +02:00
|
|
|
for (i = 0; i < avctx->ch_layout.nb_channels; i++) {
|
2014-04-17 13:51:03 +03:00
|
|
|
ChannelMap *map = &c->channel_maps[i];
|
|
|
|
|
|
|
|
/* handle copied channels */
|
|
|
|
if (map->copy) {
|
|
|
|
memcpy(frame->extended_data[i],
|
|
|
|
frame->extended_data[map->copy_idx],
|
|
|
|
frame->linesize[0]);
|
|
|
|
} else if (map->silence) {
|
|
|
|
memset(frame->extended_data[i], 0, frame->linesize[0]);
|
|
|
|
}
|
|
|
|
|
2015-10-27 01:09:44 +02:00
|
|
|
if (c->gain_i && decoded_samples > 0) {
|
2014-12-03 15:39:43 +02:00
|
|
|
c->fdsp->vector_fmul_scalar((float*)frame->extended_data[i],
|
2014-04-17 13:51:03 +03:00
|
|
|
(float*)frame->extended_data[i],
|
|
|
|
c->gain, FFALIGN(decoded_samples, 8));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
frame->nb_samples = decoded_samples;
|
|
|
|
*got_frame_ptr = !!decoded_samples;
|
|
|
|
|
|
|
|
return avpkt->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold void opus_decode_flush(AVCodecContext *ctx)
|
|
|
|
{
|
|
|
|
OpusContext *c = ctx->priv_data;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < c->nb_streams; i++) {
|
|
|
|
OpusStreamContext *s = &c->streams[i];
|
|
|
|
|
|
|
|
memset(&s->packet, 0, sizeof(s->packet));
|
|
|
|
s->delayed_samples = 0;
|
|
|
|
|
2020-12-26 14:09:49 +02:00
|
|
|
av_audio_fifo_drain(s->celt_delay, av_audio_fifo_size(s->celt_delay));
|
2014-05-15 19:28:45 +03:00
|
|
|
swr_close(s->swr);
|
2014-04-17 13:51:03 +03:00
|
|
|
|
2020-12-28 18:46:44 +02:00
|
|
|
av_audio_fifo_drain(s->sync_buffer, av_audio_fifo_size(s->sync_buffer));
|
2015-07-27 11:13:53 +02:00
|
|
|
|
2014-04-17 13:51:03 +03:00
|
|
|
ff_silk_flush(s->silk);
|
|
|
|
ff_celt_flush(s->celt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int opus_decode_close(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
OpusContext *c = avctx->priv_data;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < c->nb_streams; i++) {
|
|
|
|
OpusStreamContext *s = &c->streams[i];
|
|
|
|
|
|
|
|
ff_silk_free(&s->silk);
|
|
|
|
ff_celt_free(&s->celt);
|
|
|
|
|
|
|
|
av_freep(&s->out_dummy);
|
|
|
|
s->out_dummy_allocated_size = 0;
|
|
|
|
|
2020-12-28 18:46:44 +02:00
|
|
|
av_audio_fifo_free(s->sync_buffer);
|
2014-04-17 13:51:03 +03:00
|
|
|
av_audio_fifo_free(s->celt_delay);
|
2014-05-15 19:28:45 +03:00
|
|
|
swr_free(&s->swr);
|
2014-04-17 13:51:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
av_freep(&c->streams);
|
2015-07-27 11:13:53 +02:00
|
|
|
|
2014-04-17 13:51:03 +03:00
|
|
|
c->nb_streams = 0;
|
|
|
|
|
|
|
|
av_freep(&c->channel_maps);
|
2014-12-03 15:39:43 +02:00
|
|
|
av_freep(&c->fdsp);
|
2014-04-17 13:51:03 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int opus_decode_init(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
OpusContext *c = avctx->priv_data;
|
|
|
|
int ret, i, j;
|
|
|
|
|
|
|
|
avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
|
|
|
|
avctx->sample_rate = 48000;
|
|
|
|
|
2014-12-03 15:39:43 +02:00
|
|
|
c->fdsp = avpriv_float_dsp_alloc(0);
|
|
|
|
if (!c->fdsp)
|
|
|
|
return AVERROR(ENOMEM);
|
2014-04-17 13:51:03 +03:00
|
|
|
|
|
|
|
/* find out the channel configuration */
|
|
|
|
ret = ff_opus_parse_extradata(avctx, c);
|
2020-12-28 18:58:43 +02:00
|
|
|
if (ret < 0)
|
2014-04-17 13:51:03 +03:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* allocate and init each independent decoder */
|
2021-09-14 21:31:53 +02:00
|
|
|
c->streams = av_calloc(c->nb_streams, sizeof(*c->streams));
|
2020-12-28 18:46:44 +02:00
|
|
|
if (!c->streams) {
|
2014-04-17 13:51:03 +03:00
|
|
|
c->nb_streams = 0;
|
2020-12-28 18:58:43 +02:00
|
|
|
return AVERROR(ENOMEM);
|
2014-04-17 13:51:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < c->nb_streams; i++) {
|
|
|
|
OpusStreamContext *s = &c->streams[i];
|
2022-09-23 21:43:00 +02:00
|
|
|
AVChannelLayout layout;
|
2014-04-17 13:51:03 +03:00
|
|
|
|
|
|
|
s->output_channels = (i < c->nb_stereo_streams) ? 2 : 1;
|
|
|
|
|
|
|
|
s->avctx = avctx;
|
|
|
|
|
|
|
|
for (j = 0; j < s->output_channels; j++) {
|
|
|
|
s->silk_output[j] = s->silk_buf[j];
|
|
|
|
s->celt_output[j] = s->celt_buf[j];
|
|
|
|
s->redundancy_output[j] = s->redundancy_buf[j];
|
|
|
|
}
|
|
|
|
|
2014-12-03 15:39:43 +02:00
|
|
|
s->fdsp = c->fdsp;
|
2014-04-17 13:51:03 +03:00
|
|
|
|
2014-05-15 19:28:45 +03:00
|
|
|
s->swr =swr_alloc();
|
2020-12-28 18:58:43 +02:00
|
|
|
if (!s->swr)
|
|
|
|
return AVERROR(ENOMEM);
|
2014-04-17 13:51:03 +03:00
|
|
|
|
2022-09-23 21:43:00 +02:00
|
|
|
layout = (s->output_channels == 1) ? (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO :
|
|
|
|
(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
|
2014-05-15 19:28:45 +03:00
|
|
|
av_opt_set_int(s->swr, "in_sample_fmt", avctx->sample_fmt, 0);
|
|
|
|
av_opt_set_int(s->swr, "out_sample_fmt", avctx->sample_fmt, 0);
|
2022-09-23 21:43:00 +02:00
|
|
|
av_opt_set_chlayout(s->swr, "in_chlayout", &layout, 0);
|
|
|
|
av_opt_set_chlayout(s->swr, "out_chlayout", &layout, 0);
|
2014-05-15 19:28:45 +03:00
|
|
|
av_opt_set_int(s->swr, "out_sample_rate", avctx->sample_rate, 0);
|
|
|
|
av_opt_set_int(s->swr, "filter_size", 16, 0);
|
2014-04-17 13:51:03 +03:00
|
|
|
|
|
|
|
ret = ff_silk_init(avctx, &s->silk, s->output_channels);
|
|
|
|
if (ret < 0)
|
2020-12-28 18:58:43 +02:00
|
|
|
return ret;
|
2014-04-17 13:51:03 +03:00
|
|
|
|
2017-12-04 08:36:42 +02:00
|
|
|
ret = ff_celt_init(avctx, &s->celt, s->output_channels, c->apply_phase_inv);
|
2014-04-17 13:51:03 +03:00
|
|
|
if (ret < 0)
|
2020-12-28 18:58:43 +02:00
|
|
|
return ret;
|
2014-04-17 13:51:03 +03:00
|
|
|
|
|
|
|
s->celt_delay = av_audio_fifo_alloc(avctx->sample_fmt,
|
|
|
|
s->output_channels, 1024);
|
2020-12-28 18:58:43 +02:00
|
|
|
if (!s->celt_delay)
|
|
|
|
return AVERROR(ENOMEM);
|
2015-07-27 11:13:53 +02:00
|
|
|
|
2020-12-28 18:46:44 +02:00
|
|
|
s->sync_buffer = av_audio_fifo_alloc(avctx->sample_fmt,
|
|
|
|
s->output_channels, 32);
|
2020-12-28 18:58:43 +02:00
|
|
|
if (!s->sync_buffer)
|
|
|
|
return AVERROR(ENOMEM);
|
2014-04-17 13:51:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-12-04 08:36:42 +02:00
|
|
|
#define OFFSET(x) offsetof(OpusContext, x)
|
|
|
|
#define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
|
|
|
|
static const AVOption opus_options[] = {
|
|
|
|
{ "apply_phase_inv", "Apply intensity stereo phase inversion", OFFSET(apply_phase_inv), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, AD },
|
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVClass opus_class = {
|
|
|
|
.class_name = "Opus Decoder",
|
|
|
|
.item_name = av_default_item_name,
|
|
|
|
.option = opus_options,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
};
|
|
|
|
|
2022-03-16 22:09:54 +02:00
|
|
|
const FFCodec ff_opus_decoder = {
|
|
|
|
.p.name = "opus",
|
2022-08-29 13:38:02 +02:00
|
|
|
CODEC_LONG_NAME("Opus"),
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.priv_class = &opus_class,
|
|
|
|
.p.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.p.id = AV_CODEC_ID_OPUS,
|
2014-04-17 13:51:03 +03:00
|
|
|
.priv_data_size = sizeof(OpusContext),
|
|
|
|
.init = opus_decode_init,
|
|
|
|
.close = opus_decode_close,
|
2022-03-30 23:28:24 +02:00
|
|
|
FF_CODEC_DECODE_CB(opus_decode_packet),
|
2014-04-17 13:51:03 +03:00
|
|
|
.flush = opus_decode_flush,
|
2022-03-16 22:09:54 +02:00
|
|
|
.p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_CHANNEL_CONF,
|
2022-07-10 00:05:45 +02:00
|
|
|
.caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
|
2014-04-17 13:51:03 +03:00
|
|
|
};
|