2012-09-28 01:39:23 +00:00
|
|
|
/*
|
|
|
|
* Opus decoder using libopus
|
|
|
|
* Copyright (c) 2012 Nicolas George
|
|
|
|
*
|
|
|
|
* This file is part of Libav.
|
|
|
|
*
|
|
|
|
* Libav 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,
|
|
|
|
* 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
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <opus.h>
|
|
|
|
#include <opus_multistream.h>
|
|
|
|
|
|
|
|
#include "libavutil/intreadwrite.h"
|
2016-04-14 16:24:51 +00:00
|
|
|
|
2012-09-28 01:39:23 +00:00
|
|
|
#include "avcodec.h"
|
2012-11-10 12:22:56 +00:00
|
|
|
#include "internal.h"
|
2012-09-28 01:39:23 +00:00
|
|
|
#include "vorbis.h"
|
|
|
|
#include "mathops.h"
|
2012-09-29 05:16:09 +00:00
|
|
|
#include "libopus.h"
|
2012-09-28 01:39:23 +00:00
|
|
|
|
|
|
|
struct libopus_context {
|
|
|
|
OpusMSDecoder *dec;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define OPUS_HEAD_SIZE 19
|
|
|
|
|
|
|
|
static av_cold int libopus_decode_init(AVCodecContext *avc)
|
|
|
|
{
|
|
|
|
struct libopus_context *opus = avc->priv_data;
|
|
|
|
int ret, channel_map = 0, gain_db = 0, nb_streams, nb_coupled;
|
2012-09-27 08:23:29 +00:00
|
|
|
uint8_t mapping_arr[8] = { 0, 1 }, *mapping;
|
2012-09-28 01:39:23 +00:00
|
|
|
|
|
|
|
avc->sample_rate = 48000;
|
|
|
|
avc->sample_fmt = avc->request_sample_fmt == AV_SAMPLE_FMT_FLT ?
|
|
|
|
AV_SAMPLE_FMT_FLT : AV_SAMPLE_FMT_S16;
|
|
|
|
avc->channel_layout = avc->channels > 8 ? 0 :
|
|
|
|
ff_vorbis_channel_layouts[avc->channels - 1];
|
|
|
|
|
|
|
|
if (avc->extradata_size >= OPUS_HEAD_SIZE) {
|
|
|
|
gain_db = sign_extend(AV_RL16(avc->extradata + 16), 16);
|
|
|
|
channel_map = AV_RL8 (avc->extradata + 18);
|
|
|
|
}
|
|
|
|
if (avc->extradata_size >= OPUS_HEAD_SIZE + 2 + avc->channels) {
|
|
|
|
nb_streams = avc->extradata[OPUS_HEAD_SIZE + 0];
|
|
|
|
nb_coupled = avc->extradata[OPUS_HEAD_SIZE + 1];
|
|
|
|
if (nb_streams + nb_coupled != avc->channels)
|
|
|
|
av_log(avc, AV_LOG_WARNING, "Inconsistent channel mapping.\n");
|
|
|
|
mapping = avc->extradata + OPUS_HEAD_SIZE + 2;
|
|
|
|
} else {
|
|
|
|
if (avc->channels > 2 || channel_map) {
|
|
|
|
av_log(avc, AV_LOG_ERROR,
|
|
|
|
"No channel mapping for %d channels.\n", avc->channels);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
nb_streams = 1;
|
|
|
|
nb_coupled = avc->channels > 1;
|
2012-09-27 08:23:29 +00:00
|
|
|
mapping = mapping_arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (avc->channels > 2 && avc->channels <= 8) {
|
|
|
|
const uint8_t *vorbis_offset = ff_vorbis_channel_layout_offsets[avc->channels - 1];
|
|
|
|
int ch;
|
|
|
|
|
2016-04-27 17:45:23 +00:00
|
|
|
/* Remap channels from Vorbis order to libav order */
|
2012-09-27 08:23:29 +00:00
|
|
|
for (ch = 0; ch < avc->channels; ch++)
|
|
|
|
mapping_arr[ch] = mapping[vorbis_offset[ch]];
|
|
|
|
mapping = mapping_arr;
|
2012-09-28 01:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
opus->dec = opus_multistream_decoder_create(avc->sample_rate, avc->channels,
|
|
|
|
nb_streams, nb_coupled,
|
|
|
|
mapping, &ret);
|
|
|
|
if (!opus->dec) {
|
|
|
|
av_log(avc, AV_LOG_ERROR, "Unable to create decoder: %s\n",
|
|
|
|
opus_strerror(ret));
|
2012-09-29 05:16:09 +00:00
|
|
|
return ff_opus_error_to_averror(ret);
|
2012-09-28 01:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
avc->delay = 3840; /* Decoder delay (in samples) at 48kHz */
|
2012-12-23 23:45:55 +00:00
|
|
|
|
2012-09-28 01:39:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int libopus_decode_close(AVCodecContext *avc)
|
|
|
|
{
|
|
|
|
struct libopus_context *opus = avc->priv_data;
|
|
|
|
|
|
|
|
opus_multistream_decoder_destroy(opus->dec);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MAX_FRAME_SIZE (960 * 6)
|
|
|
|
|
2012-12-23 23:45:55 +00:00
|
|
|
static int libopus_decode(AVCodecContext *avc, void *data,
|
2012-09-28 01:39:23 +00:00
|
|
|
int *got_frame_ptr, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
struct libopus_context *opus = avc->priv_data;
|
2012-12-23 23:45:55 +00:00
|
|
|
AVFrame *frame = data;
|
2012-09-28 01:39:23 +00:00
|
|
|
int ret, nb_samples;
|
|
|
|
|
2012-12-23 23:45:55 +00:00
|
|
|
frame->nb_samples = MAX_FRAME_SIZE;
|
2012-11-21 20:34:46 +00:00
|
|
|
ret = ff_get_buffer(avc, frame, 0);
|
2012-09-28 01:39:23 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
av_log(avc, AV_LOG_ERROR, "get_buffer() failed\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (avc->sample_fmt == AV_SAMPLE_FMT_S16)
|
|
|
|
nb_samples = opus_multistream_decode(opus->dec, pkt->data, pkt->size,
|
2012-12-23 23:45:55 +00:00
|
|
|
(opus_int16 *)frame->data[0],
|
|
|
|
frame->nb_samples, 0);
|
2012-09-28 01:39:23 +00:00
|
|
|
else
|
|
|
|
nb_samples = opus_multistream_decode_float(opus->dec, pkt->data, pkt->size,
|
2012-12-23 23:45:55 +00:00
|
|
|
(float *)frame->data[0],
|
|
|
|
frame->nb_samples, 0);
|
2012-09-28 01:39:23 +00:00
|
|
|
|
|
|
|
if (nb_samples < 0) {
|
|
|
|
av_log(avc, AV_LOG_ERROR, "Decoding error: %s\n",
|
|
|
|
opus_strerror(nb_samples));
|
2012-09-29 05:16:09 +00:00
|
|
|
return ff_opus_error_to_averror(nb_samples);
|
2012-09-28 01:39:23 +00:00
|
|
|
}
|
|
|
|
|
2012-12-23 23:45:55 +00:00
|
|
|
frame->nb_samples = nb_samples;
|
|
|
|
*got_frame_ptr = 1;
|
|
|
|
|
2012-09-28 01:39:23 +00:00
|
|
|
return pkt->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void libopus_flush(AVCodecContext *avc)
|
|
|
|
{
|
|
|
|
struct libopus_context *opus = avc->priv_data;
|
|
|
|
|
|
|
|
opus_multistream_decoder_ctl(opus->dec, OPUS_RESET_STATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
AVCodec ff_libopus_decoder = {
|
|
|
|
.name = "libopus",
|
2013-10-03 20:57:53 +00:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("libopus Opus"),
|
2012-09-28 01:39:23 +00:00
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.id = AV_CODEC_ID_OPUS,
|
|
|
|
.priv_data_size = sizeof(struct libopus_context),
|
|
|
|
.init = libopus_decode_init,
|
|
|
|
.close = libopus_decode_close,
|
|
|
|
.decode = libopus_decode,
|
|
|
|
.flush = libopus_flush,
|
2015-07-07 00:41:27 +00:00
|
|
|
.capabilities = AV_CODEC_CAP_DR1,
|
2012-09-28 01:39:23 +00:00
|
|
|
.sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLT,
|
|
|
|
AV_SAMPLE_FMT_S16,
|
|
|
|
AV_SAMPLE_FMT_NONE },
|
|
|
|
};
|