Commit b70d7a4a authored by Anton Khirnov's avatar Anton Khirnov

lavc: add a native Opus decoder.

Initial implementation by Andrew D'Addesio <modchipv12@gmail.com> during
GSoC 2012.

Completion by Anton Khirnov <anton@khirnov.net>, sponsored by the
Mozilla Corporation.

Further contributions by:
Christophe Gisquet <christophe.gisquet@gmail.com>
Janne Grunau <janne-libav@jannau.net>
Luca Barbato <lu_zero@gentoo.org>
parent 7e90133f
......@@ -25,6 +25,7 @@ version <next>:
- On2 AVC (Audio for Video) decoder
- support for decoding through DXVA2 in avconv
- libbs2b-based stereo-to-binaural audio filter
- native Opus decoder
version 10:
......
......@@ -1822,6 +1822,7 @@ nellymoser_decoder_select="mdct sinewin"
nellymoser_encoder_select="audio_frame_queue mdct sinewin"
nuv_decoder_select="dsputil lzo"
on2avc_decoder_select="mdct"
opus_decoder_deps="avresample"
png_decoder_deps="zlib"
png_encoder_deps="zlib"
png_encoder_select="dsputil"
......@@ -4387,6 +4388,8 @@ enabled movie_filter && prepend avfilter_deps "avformat avcodec"
enabled resample_filter && prepend avfilter_deps "avresample"
enabled scale_filter && prepend avfilter_deps "swscale"
enabled opus_decoder && prepend avcodec_deps "avresample"
expand_deps(){
lib_deps=${1}_deps
eval "deps=\$$lib_deps"
......
......@@ -279,6 +279,9 @@ OBJS-$(CONFIG_NELLYMOSER_DECODER) += nellymoserdec.o nellymoser.o
OBJS-$(CONFIG_NELLYMOSER_ENCODER) += nellymoserenc.o nellymoser.o
OBJS-$(CONFIG_NUV_DECODER) += nuv.o rtjpeg.o
OBJS-$(CONFIG_ON2AVC_DECODER) += on2avc.o on2avcdata.o
OBJS-$(CONFIG_OPUS_DECODER) += opusdec.o opus.o opus_celt.o \
opus_imdct.o opus_silk.o \
vorbis_data.o
OBJS-$(CONFIG_PAF_VIDEO_DECODER) += paf.o
OBJS-$(CONFIG_PAF_AUDIO_DECODER) += paf.o
OBJS-$(CONFIG_PAM_DECODER) += pnmdec.o pnm.o
......@@ -653,6 +656,7 @@ OBJS-$(CONFIG_MPEGAUDIO_PARSER) += mpegaudio_parser.o \
mpegaudiodecheader.o mpegaudiodata.o
OBJS-$(CONFIG_MPEGVIDEO_PARSER) += mpegvideo_parser.o \
mpeg12.o mpeg12data.o
OBJS-$(CONFIG_OPUS_PARSER) += opus_parser.o opus.o vorbis_data.o
OBJS-$(CONFIG_PNG_PARSER) += png_parser.o
OBJS-$(CONFIG_PNM_PARSER) += pnm_parser.o pnm.o
OBJS-$(CONFIG_RV30_PARSER) += rv34_parser.o
......
......@@ -331,6 +331,7 @@ void avcodec_register_all(void)
REGISTER_DECODER(MPC8, mpc8);
REGISTER_ENCDEC (NELLYMOSER, nellymoser);
REGISTER_DECODER(ON2AVC, on2avc);
REGISTER_DECODER(OPUS, opus);
REGISTER_DECODER(PAF_AUDIO, paf_audio);
REGISTER_DECODER(QCELP, qcelp);
REGISTER_DECODER(QDM2, qdm2);
......@@ -483,6 +484,7 @@ void avcodec_register_all(void)
REGISTER_PARSER(MPEG4VIDEO, mpeg4video);
REGISTER_PARSER(MPEGAUDIO, mpegaudio);
REGISTER_PARSER(MPEGVIDEO, mpegvideo);
REGISTER_PARSER(OPUS, opus);
REGISTER_PARSER(PNG, png);
REGISTER_PARSER(PNM, pnm);
REGISTER_PARSER(RV30, rv30);
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
/*
* Copyright (c) 2013-2014 Mozilla Corporation
*
* 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
*/
/**
* @file
* Celt non-power of 2 iMDCT
*/
#include <float.h>
#include <math.h>
#include "libavutil/attributes.h"
#include "libavutil/common.h"
#include "fft.h"
#include "opus.h"
// complex c = a * b
#define CMUL3(cre, cim, are, aim, bre, bim) \
do { \
cre = are * bre - aim * bim; \
cim = are * bim + aim * bre; \
} while (0)
#define CMUL(c, a, b) CMUL3((c).re, (c).im, (a).re, (a).im, (b).re, (b).im)
// complex c = a * b
// d = a * conjugate(b)
#define CMUL2(c, d, a, b) \
do { \
float are = (a).re; \
float aim = (a).im; \
float bre = (b).re; \
float bim = (b).im; \
float rr = are * bre; \
float ri = are * bim; \
float ir = aim * bre; \
float ii = aim * bim; \
(c).re = rr - ii; \
(c).im = ri + ir; \
(d).re = rr + ii; \
(d).im = -ri + ir; \
} while (0)
struct CeltIMDCTContext {
int fft_n;
int len2;
int len4;
FFTComplex *tmp;
FFTComplex *twiddle_exptab;
FFTComplex *exptab[6];
};
av_cold void ff_celt_imdct_uninit(CeltIMDCTContext **ps)
{
CeltIMDCTContext *s = *ps;
int i;
if (!s)
return;
for (i = 0; i < FF_ARRAY_ELEMS(s->exptab); i++)
av_freep(&s->exptab[i]);
av_freep(&s->twiddle_exptab);
av_freep(&s->tmp);
av_freep(ps);
}
av_cold int ff_celt_imdct_init(CeltIMDCTContext **ps, int N)
{
CeltIMDCTContext *s;
int len2 = 15 * (1 << N);
int len = 2 * len2;
int i, j;
if (len2 > CELT_MAX_FRAME_SIZE)
return AVERROR(EINVAL);
s = av_mallocz(sizeof(*s));
if (!s)
return AVERROR(ENOMEM);
s->fft_n = N - 1;
s->len4 = len2 / 2;
s->len2 = len2;
s->tmp = av_malloc(len * 2 * sizeof(*s->tmp));
if (!s->tmp)
goto fail;
s->twiddle_exptab = av_malloc(s->len4 * sizeof(*s->twiddle_exptab));
if (!s->twiddle_exptab)
goto fail;
for (i = 0; i < s->len4; i++) {
s->twiddle_exptab[i].re = cos(2 * M_PI * (i + 0.125 + s->len4) / len);
s->twiddle_exptab[i].im = sin(2 * M_PI * (i + 0.125 + s->len4) / len);
}
for (i = 0; i < FF_ARRAY_ELEMS(s->exptab); i++) {
int N = 15 * (1 << i);
s->exptab[i] = av_malloc(sizeof(*s->exptab[i]) * FFMAX(N, 19));
if (!s->exptab[i])
goto fail;
for (j = 0; j < N; j++) {
s->exptab[i][j].re = cos(2 * M_PI * j / N);
s->exptab[i][j].im = sin(2 * M_PI * j / N);
}
}
// wrap around to simplify fft15
for (j = 15; j < 19; j++)
s->exptab[0][j] = s->exptab[0][j - 15];
*ps = s;
return 0;
fail:
ff_celt_imdct_uninit(&s);
return AVERROR(ENOMEM);
}
static void fft5(FFTComplex *out, const FFTComplex *in, int stride)
{
// [0] = exp(2 * i * pi / 5), [1] = exp(2 * i * pi * 2 / 5)
static const FFTComplex fact[] = { { 0.30901699437494745, 0.95105651629515353 },
{ -0.80901699437494734, 0.58778525229247325 } };
FFTComplex z[4][4];
CMUL2(z[0][0], z[0][3], in[1 * stride], fact[0]);
CMUL2(z[0][1], z[0][2], in[1 * stride], fact[1]);
CMUL2(z[1][0], z[1][3], in[2 * stride], fact[0]);
CMUL2(z[1][1], z[1][2], in[2 * stride], fact[1]);
CMUL2(z[2][0], z[2][3], in[3 * stride], fact[0]);
CMUL2(z[2][1], z[2][2], in[3 * stride], fact[1]);
CMUL2(z[3][0], z[3][3], in[4 * stride], fact[0]);
CMUL2(z[3][1], z[3][2], in[4 * stride], fact[1]);
out[0].re = in[0].re + in[stride].re + in[2 * stride].re + in[3 * stride].re + in[4 * stride].re;
out[0].im = in[0].im + in[stride].im + in[2 * stride].im + in[3 * stride].im + in[4 * stride].im;
out[1].re = in[0].re + z[0][0].re + z[1][1].re + z[2][2].re + z[3][3].re;
out[1].im = in[0].im + z[0][0].im + z[1][1].im + z[2][2].im + z[3][3].im;
out[2].re = in[0].re + z[0][1].re + z[1][3].re + z[2][0].re + z[3][2].re;
out[2].im = in[0].im + z[0][1].im + z[1][3].im + z[2][0].im + z[3][2].im;
out[3].re = in[0].re + z[0][2].re + z[1][0].re + z[2][3].re + z[3][1].re;
out[3].im = in[0].im + z[0][2].im + z[1][0].im + z[2][3].im + z[3][1].im;
out[4].re = in[0].re + z[0][3].re + z[1][2].re + z[2][1].re + z[3][0].re;
out[4].im = in[0].im + z[0][3].im + z[1][2].im + z[2][1].im + z[3][0].im;
}
static void fft15(CeltIMDCTContext *s, FFTComplex *out, const FFTComplex *in, int stride)
{
const FFTComplex *exptab = s->exptab[0];
FFTComplex tmp[5];
FFTComplex tmp1[5];
FFTComplex tmp2[5];
int k;
fft5(tmp, in, stride * 3);
fft5(tmp1, in + stride, stride * 3);
fft5(tmp2, in + 2 * stride, stride * 3);
for (k = 0; k < 5; k++) {
FFTComplex t1, t2;
CMUL(t1, tmp1[k], exptab[k]);
CMUL(t2, tmp2[k], exptab[2 * k]);
out[k].re = tmp[k].re + t1.re + t2.re;
out[k].im = tmp[k].im + t1.im + t2.im;
CMUL(t1, tmp1[k], exptab[k + 5]);
CMUL(t2, tmp2[k], exptab[2 * (k + 5)]);
out[k + 5].re = tmp[k].re + t1.re + t2.re;
out[k + 5].im = tmp[k].im + t1.im + t2.im;
CMUL(t1, tmp1[k], exptab[k + 10]);
CMUL(t2, tmp2[k], exptab[2 * k + 5]);
out[k + 10].re = tmp[k].re + t1.re + t2.re;
out[k + 10].im = tmp[k].im + t1.im + t2.im;
}
}
/*
* FFT of the length 15 * (2^N)
*/
static void fft_calc(CeltIMDCTContext *s, FFTComplex *out, const FFTComplex *in, int N, int stride)
{
if (N) {
const FFTComplex *exptab = s->exptab[N];
const int len2 = 15 * (1 << (N - 1));
int k;
fft_calc(s, out, in, N - 1, stride * 2);
fft_calc(s, out + len2, in + stride, N - 1, stride * 2);
for (k = 0; k < len2; k++) {
FFTComplex t;
CMUL(t, out[len2 + k], exptab[k]);
out[len2 + k].re = out[k].re - t.re;
out[len2 + k].im = out[k].im - t.im;
out[k].re += t.re;
out[k].im += t.im;
}
} else
fft15(s, out, in, stride);
}
void ff_celt_imdct_half(CeltIMDCTContext *s, float *dst, const float *src,
int stride, float scale)
{
FFTComplex *z = (FFTComplex *)dst;
const int len8 = s->len4 / 2;
const float *in1 = src;
const float *in2 = src + (s->len2 - 1) * stride;
int i;
for (i = 0; i < s->len4; i++) {
FFTComplex tmp = { *in2, *in1 };
CMUL(s->tmp[i], tmp, s->twiddle_exptab[i]);
in1 += 2 * stride;
in2 -= 2 * stride;
}
fft_calc(s, z, s->tmp, s->fft_n, 1);
for (i = 0; i < len8; i++) {
float r0, i0, r1, i1;
CMUL3(r0, i1, z[len8 - i - 1].im, z[len8 - i - 1].re, s->twiddle_exptab[len8 - i - 1].im, s->twiddle_exptab[len8 - i - 1].re);
CMUL3(r1, i0, z[len8 + i].im, z[len8 + i].re, s->twiddle_exptab[len8 + i].im, s->twiddle_exptab[len8 + i].re);
z[len8 - i - 1].re = scale * r0;
z[len8 - i - 1].im = scale * i0;
z[len8 + i].re = scale * r1;
z[len8 + i].im = scale * i1;
}
}
/*
* Copyright (c) 2013-2014 Mozilla Corporation
*
* 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
*/
/**
* @file
* Opus parser
*
* Determines the duration for each packet.
*/
#include "avcodec.h"
#include "opus.h"
typedef struct OpusParseContext {
OpusContext ctx;
OpusPacket pkt;
int extradata_parsed;
} OpusParseContext;
static int opus_parse(AVCodecParserContext *ctx, AVCodecContext *avctx,
const uint8_t **poutbuf, int *poutbuf_size,
const uint8_t *buf, int buf_size)
{
OpusParseContext *s = ctx->priv_data;
int ret;
if (!buf_size)
return 0;
if (avctx->extradata && !s->extradata_parsed) {
ret = ff_opus_parse_extradata(avctx, &s->ctx);
if (ret < 0) {
av_log(avctx, AV_LOG_ERROR, "Error parsing Ogg extradata.\n");
goto fail;
}
av_freep(&s->ctx.channel_maps);
s->extradata_parsed = 1;
}
ret = ff_opus_parse_packet(&s->pkt, buf, buf_size, s->ctx.nb_streams > 1);
if (ret < 0) {
av_log(avctx, AV_LOG_ERROR, "Error parsing Opus packet header.\n");
goto fail;
}
ctx->duration = s->pkt.frame_count * s->pkt.frame_duration;
fail:
*poutbuf = buf;
*poutbuf_size = buf_size;
return buf_size;
}
AVCodecParser ff_opus_parser = {
.codec_ids = { AV_CODEC_ID_OPUS },
.priv_data_size = sizeof(OpusParseContext),
.parser_parse = opus_parse,
};
This diff is collapsed.
This diff is collapsed.
......@@ -29,8 +29,8 @@
#include "libavutil/version.h"
#define LIBAVCODEC_VERSION_MAJOR 55
#define LIBAVCODEC_VERSION_MINOR 50
#define LIBAVCODEC_VERSION_MICRO 3
#define LIBAVCODEC_VERSION_MINOR 51
#define LIBAVCODEC_VERSION_MICRO 0
#define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
LIBAVCODEC_VERSION_MINOR, \
......
......@@ -93,6 +93,7 @@ include $(SRC_PATH)/tests/fate/microsoft.mak
include $(SRC_PATH)/tests/fate/monkeysaudio.mak
include $(SRC_PATH)/tests/fate/mp3.mak
include $(SRC_PATH)/tests/fate/mpc.mak
include $(SRC_PATH)/tests/fate/opus.mak
include $(SRC_PATH)/tests/fate/pcm.mak
include $(SRC_PATH)/tests/fate/probe.mak
include $(SRC_PATH)/tests/fate/prores.mak
......
# The samples were produced by simply rewrapping the official test vectors from
# their custom format into Matroska.
# The reference files were created with our decoder and tested against the
# libopus output with the official opus_compare tool. We cannot use libopus
# output as reference directly, because the use of different resamplers would
# require too high fuzz values, which can hide bugs.
# Before adding new tests here, always make sure they pass opus_compare.
OPUS_CELT_SAMPLES = $(addprefix testvector, 01 07 11) tron.6ch.tinypkts
OPUS_HYBRID_SAMPLES = $(addprefix testvector, 05 06)
OPUS_SILK_SAMPLES = $(addprefix testvector, 02 03 04)
OPUS_SAMPLES = $(addprefix testvector, 08 09 10 12)
define FATE_OPUS_TEST
FATE_OPUS += fate-opus-$(1)
FATE_OPUS$(2) += fate-opus-$(1)
fate-opus-$(1): CMD = avconv -i $(TARGET_SAMPLES)/opus/$(1).mka -f f32le -
fate-opus-$(1): REF = $(TARGET_SAMPLES)/opus/$(1).f32
endef
$(foreach N,$(OPUS_CELT_SAMPLES), $(eval $(call FATE_OPUS_TEST,$(N),_CELT)))
$(foreach N,$(OPUS_HYBRID_SAMPLES),$(eval $(call FATE_OPUS_TEST,$(N),_HYBRID)))
$(foreach N,$(OPUS_SILK_SAMPLES), $(eval $(call FATE_OPUS_TEST,$(N),_SILK)))
$(foreach N,$(OPUS_SAMPLES), $(eval $(call FATE_OPUS_TEST,$(N),)))
FATE_OPUS := $(sort $(FATE_OPUS))
$(FATE_OPUS): CMP = stddev
$(FATE_OPUS): CMP_UNIT = f32
$(FATE_OPUS): FUZZ = 3
$(FATE_OPUS_CELT): CMP = oneoff
$(FATE_OPUS_CELT): FUZZ = 5
FATE_SAMPLES_AVCONV-$(call DEMDEC, MATROSKA, OPUS) += $(FATE_OPUS)
fate-opus-celt: $(FATE_OPUS_CELT)
fate-opus-hybrid: $(FATE_OPUS_HYBRID)
fate-opus-silk: $(FATE_OPUS_SILK)
fate-opus: $(FATE_OPUS)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment