2008-08-05 21:32:01 +02:00
|
|
|
/*
|
|
|
|
* AAC decoder
|
|
|
|
* Copyright (c) 2005-2006 Oded Shimon ( ods15 ods15 dyndns org )
|
|
|
|
* Copyright (c) 2006-2007 Maxim Gavrilov ( maxim.gavrilov gmail com )
|
2013-10-14 22:51:46 +02:00
|
|
|
* Copyright (c) 2008-2013 Alex Converse <alex.converse@gmail.com>
|
2008-08-05 21:32:01 +02:00
|
|
|
*
|
2010-11-02 09:32:04 +01:00
|
|
|
* AAC LATM decoder
|
|
|
|
* Copyright (c) 2008-2010 Paul Kendall <paul@kcbbs.gen.nz>
|
2012-02-22 09:19:34 +01:00
|
|
|
* Copyright (c) 2010 Janne Grunau <janne-libav@jannau.net>
|
2010-11-02 09:32:04 +01:00
|
|
|
*
|
2008-08-05 21:32:01 +02:00
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 16:45:34 +02:00
|
|
|
* @file
|
2008-08-05 21:32:01 +02:00
|
|
|
* AAC decoder
|
|
|
|
* @author Oded Shimon ( ods15 ods15 dyndns org )
|
|
|
|
* @author Maxim Gavrilov ( maxim.gavrilov gmail com )
|
|
|
|
*/
|
|
|
|
|
2015-06-30 11:53:05 +02:00
|
|
|
#define FFT_FLOAT 1
|
|
|
|
#define FFT_FIXED_32 0
|
|
|
|
#define USE_FIXED 0
|
|
|
|
|
2012-05-21 18:58:41 +02:00
|
|
|
#include "libavutil/float_dsp.h"
|
2012-12-30 05:07:12 +01:00
|
|
|
#include "libavutil/opt.h"
|
2008-08-05 21:32:01 +02:00
|
|
|
#include "avcodec.h"
|
2008-12-08 22:21:38 +01:00
|
|
|
#include "internal.h"
|
2009-04-13 18:20:26 +02:00
|
|
|
#include "get_bits.h"
|
2010-03-06 15:34:46 +01:00
|
|
|
#include "fft.h"
|
2015-01-22 06:51:54 +01:00
|
|
|
#include "imdct15.h"
|
2008-09-04 13:03:14 +02:00
|
|
|
#include "lpc.h"
|
2011-03-19 18:19:04 +01:00
|
|
|
#include "kbdwin.h"
|
2011-03-20 00:44:00 +01:00
|
|
|
#include "sinewin.h"
|
2008-08-05 21:32:01 +02:00
|
|
|
|
|
|
|
#include "aac.h"
|
|
|
|
#include "aactab.h"
|
2008-08-09 12:46:27 +02:00
|
|
|
#include "aacdectab.h"
|
2010-03-14 20:59:47 +01:00
|
|
|
#include "cbrt_tablegen.h"
|
2010-03-08 05:33:02 +01:00
|
|
|
#include "sbr.h"
|
|
|
|
#include "aacsbr.h"
|
2008-08-05 21:32:01 +02:00
|
|
|
#include "mpeg4audio.h"
|
2010-07-12 20:52:03 +02:00
|
|
|
#include "aacadtsdec.h"
|
2011-12-12 19:23:51 +01:00
|
|
|
#include "libavutil/intfloat.h"
|
2008-08-05 21:32:01 +02:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <math.h>
|
2013-11-23 21:32:55 +01:00
|
|
|
#include <stdint.h>
|
2008-08-05 21:32:01 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
2010-01-15 03:58:24 +01:00
|
|
|
#if ARCH_ARM
|
2015-06-30 11:53:03 +02:00
|
|
|
# include "arm/aac.h"
|
|
|
|
#elif ARCH_MIPS
|
|
|
|
# include "mips/aacdec_mips.h"
|
|
|
|
#endif
|
2008-08-18 23:36:58 +02:00
|
|
|
|
2015-06-30 11:53:03 +02:00
|
|
|
static av_always_inline void reset_predict_state(PredictorState *ps)
|
2009-08-02 12:50:59 +02:00
|
|
|
{
|
2015-06-30 11:53:03 +02:00
|
|
|
ps->r0 = 0.0f;
|
|
|
|
ps->r1 = 0.0f;
|
|
|
|
ps->cor0 = 0.0f;
|
|
|
|
ps->cor1 = 0.0f;
|
|
|
|
ps->var0 = 1.0f;
|
|
|
|
ps->var1 = 1.0f;
|
2008-08-15 02:05:09 +02:00
|
|
|
}
|
2008-08-11 13:16:06 +02:00
|
|
|
|
2010-01-15 03:58:24 +01:00
|
|
|
#ifndef VMUL2
|
2010-01-13 17:46:28 +01:00
|
|
|
static inline float *VMUL2(float *dst, const float *v, unsigned idx,
|
|
|
|
const float *scale)
|
|
|
|
{
|
|
|
|
float s = *scale;
|
|
|
|
*dst++ = v[idx & 15] * s;
|
|
|
|
*dst++ = v[idx>>4 & 15] * s;
|
|
|
|
return dst;
|
|
|
|
}
|
2010-01-15 03:58:24 +01:00
|
|
|
#endif
|
2010-01-13 17:46:28 +01:00
|
|
|
|
2010-01-15 03:58:24 +01:00
|
|
|
#ifndef VMUL4
|
2010-01-13 17:46:28 +01:00
|
|
|
static inline float *VMUL4(float *dst, const float *v, unsigned idx,
|
|
|
|
const float *scale)
|
|
|
|
{
|
|
|
|
float s = *scale;
|
|
|
|
*dst++ = v[idx & 3] * s;
|
|
|
|
*dst++ = v[idx>>2 & 3] * s;
|
|
|
|
*dst++ = v[idx>>4 & 3] * s;
|
|
|
|
*dst++ = v[idx>>6 & 3] * s;
|
|
|
|
return dst;
|
|
|
|
}
|
2010-01-15 03:58:24 +01:00
|
|
|
#endif
|
2010-01-13 17:46:28 +01:00
|
|
|
|
2010-01-15 03:58:24 +01:00
|
|
|
#ifndef VMUL2S
|
2010-01-13 17:46:28 +01:00
|
|
|
static inline float *VMUL2S(float *dst, const float *v, unsigned idx,
|
|
|
|
unsigned sign, const float *scale)
|
|
|
|
{
|
2011-12-12 19:23:51 +01:00
|
|
|
union av_intfloat32 s0, s1;
|
2010-01-13 17:46:28 +01:00
|
|
|
|
|
|
|
s0.f = s1.f = *scale;
|
|
|
|
s0.i ^= sign >> 1 << 31;
|
|
|
|
s1.i ^= sign << 31;
|
|
|
|
|
|
|
|
*dst++ = v[idx & 15] * s0.f;
|
|
|
|
*dst++ = v[idx>>4 & 15] * s1.f;
|
|
|
|
|
|
|
|
return dst;
|
|
|
|
}
|
2010-01-15 03:58:24 +01:00
|
|
|
#endif
|
2010-01-13 17:46:28 +01:00
|
|
|
|
2010-01-15 03:58:24 +01:00
|
|
|
#ifndef VMUL4S
|
2010-01-13 17:46:28 +01:00
|
|
|
static inline float *VMUL4S(float *dst, const float *v, unsigned idx,
|
|
|
|
unsigned sign, const float *scale)
|
|
|
|
{
|
|
|
|
unsigned nz = idx >> 12;
|
2011-12-12 19:23:51 +01:00
|
|
|
union av_intfloat32 s = { .f = *scale };
|
|
|
|
union av_intfloat32 t;
|
2010-01-13 17:46:28 +01:00
|
|
|
|
2011-04-10 02:22:04 +02:00
|
|
|
t.i = s.i ^ (sign & 1U<<31);
|
2010-01-13 17:46:28 +01:00
|
|
|
*dst++ = v[idx & 3] * t.f;
|
|
|
|
|
|
|
|
sign <<= nz & 1; nz >>= 1;
|
2011-04-10 02:22:04 +02:00
|
|
|
t.i = s.i ^ (sign & 1U<<31);
|
2010-01-13 17:46:28 +01:00
|
|
|
*dst++ = v[idx>>2 & 3] * t.f;
|
|
|
|
|
|
|
|
sign <<= nz & 1; nz >>= 1;
|
2015-06-30 11:53:03 +02:00
|
|
|
t.i = s.i ^ (sign & 1U<<31);
|
|
|
|
*dst++ = v[idx>>4 & 3] * t.f;
|
2008-08-09 12:46:27 +02:00
|
|
|
|
2015-06-30 11:53:03 +02:00
|
|
|
sign <<= nz & 1;
|
|
|
|
t.i = s.i ^ (sign & 1U<<31);
|
|
|
|
*dst++ = v[idx>>6 & 3] * t.f;
|
2008-08-18 23:36:58 +02:00
|
|
|
|
2015-06-30 11:53:03 +02:00
|
|
|
return dst;
|
2008-08-18 23:36:58 +02:00
|
|
|
}
|
2015-06-30 11:53:03 +02:00
|
|
|
#endif
|
2008-08-18 23:36:58 +02:00
|
|
|
|
2015-06-30 11:53:03 +02:00
|
|
|
static av_always_inline float flt16_round(float pf)
|
2011-02-14 10:09:43 +01:00
|
|
|
{
|
2015-06-30 11:53:03 +02:00
|
|
|
union av_intfloat32 tmp;
|
|
|
|
tmp.f = pf;
|
|
|
|
tmp.i = (tmp.i + 0x00008000U) & 0xFFFF0000U;
|
|
|
|
return tmp.f;
|
2011-02-14 10:09:43 +01:00
|
|
|
}
|
|
|
|
|
2015-06-30 11:53:03 +02:00
|
|
|
static av_always_inline float flt16_even(float pf)
|
2011-02-14 10:09:43 +01:00
|
|
|
{
|
2015-06-30 11:53:03 +02:00
|
|
|
union av_intfloat32 tmp;
|
|
|
|
tmp.f = pf;
|
|
|
|
tmp.i = (tmp.i + 0x00007FFFU + (tmp.i & 0x00010000U >> 16)) & 0xFFFF0000U;
|
|
|
|
return tmp.f;
|
2011-02-14 10:09:43 +01:00
|
|
|
}
|
|
|
|
|
2015-06-30 11:53:03 +02:00
|
|
|
static av_always_inline float flt16_trunc(float pf)
|
2011-02-14 10:09:43 +01:00
|
|
|
{
|
2015-06-30 11:53:03 +02:00
|
|
|
union av_intfloat32 pun;
|
|
|
|
pun.f = pf;
|
|
|
|
pun.i &= 0xFFFF0000U;
|
|
|
|
return pun.f;
|
2011-02-14 10:09:43 +01:00
|
|
|
}
|
|
|
|
|
2015-06-30 11:53:03 +02:00
|
|
|
static av_always_inline void predict(PredictorState *ps, float *coef,
|
|
|
|
int output_enable)
|
2009-08-02 12:50:59 +02:00
|
|
|
{
|
2015-06-30 11:53:03 +02:00
|
|
|
const float a = 0.953125; // 61.0 / 64
|
|
|
|
const float alpha = 0.90625; // 29.0 / 32
|
|
|
|
float e0, e1;
|
|
|
|
float pv;
|
|
|
|
float k1, k2;
|
|
|
|
float r0 = ps->r0, r1 = ps->r1;
|
|
|
|
float cor0 = ps->cor0, cor1 = ps->cor1;
|
|
|
|
float var0 = ps->var0, var1 = ps->var1;
|
2008-08-15 02:05:09 +02:00
|
|
|
|
2015-06-30 11:53:03 +02:00
|
|
|
k1 = var0 > 1 ? cor0 * flt16_even(a / var0) : 0;
|
|
|
|
k2 = var1 > 1 ? cor1 * flt16_even(a / var1) : 0;
|
2008-08-15 02:05:09 +02:00
|
|
|
|
2015-06-30 11:53:03 +02:00
|
|
|
pv = flt16_round(k1 * r0 + k2 * r1);
|
|
|
|
if (output_enable)
|
|
|
|
*coef += pv;
|
2013-09-16 22:03:15 +02:00
|
|
|
|
2015-06-30 11:53:03 +02:00
|
|
|
e0 = *coef;
|
|
|
|
e1 = e0 - k1 * r0;
|
2013-09-16 22:03:15 +02:00
|
|
|
|
2015-06-30 11:53:03 +02:00
|
|
|
ps->cor1 = flt16_trunc(alpha * cor1 + r1 * e1);
|
|
|
|
ps->var1 = flt16_trunc(alpha * var1 + 0.5f * (r1 * r1 + e1 * e1));
|
|
|
|
ps->cor0 = flt16_trunc(alpha * cor0 + r0 * e0);
|
|
|
|
ps->var0 = flt16_trunc(alpha * var0 + 0.5f * (r0 * r0 + e0 * e0));
|
2013-10-14 22:51:46 +02:00
|
|
|
|
2015-06-30 11:53:03 +02:00
|
|
|
ps->r1 = flt16_trunc(a * (r0 - k1 * e0));
|
|
|
|
ps->r0 = flt16_trunc(a * e0);
|
2013-10-14 22:51:46 +02:00
|
|
|
}
|
|
|
|
|
2008-08-09 12:46:27 +02:00
|
|
|
/**
|
|
|
|
* Apply dependent channel coupling (applied before IMDCT).
|
|
|
|
*
|
|
|
|
* @param index index into coupling gain array
|
|
|
|
*/
|
2009-08-02 12:50:59 +02:00
|
|
|
static void apply_dependent_coupling(AACContext *ac,
|
|
|
|
SingleChannelElement *target,
|
|
|
|
ChannelElement *cce, int index)
|
|
|
|
{
|
|
|
|
IndividualChannelStream *ics = &cce->ch[0].ics;
|
|
|
|
const uint16_t *offsets = ics->swb_offset;
|
|
|
|
float *dest = target->coeffs;
|
|
|
|
const float *src = cce->ch[0].coeffs;
|
2008-08-09 12:46:27 +02:00
|
|
|
int g, i, group, k, idx = 0;
|
2012-04-11 01:27:28 +02:00
|
|
|
if (ac->oc[1].m4ac.object_type == AOT_AAC_LTP) {
|
2010-06-05 17:22:19 +02:00
|
|
|
av_log(ac->avctx, AV_LOG_ERROR,
|
2008-08-09 12:46:27 +02:00
|
|
|
"Dependent coupling is not supported together with LTP\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (g = 0; g < ics->num_window_groups; g++) {
|
|
|
|
for (i = 0; i < ics->max_sfb; i++, idx++) {
|
2008-10-06 18:22:11 +02:00
|
|
|
if (cce->ch[0].band_type[idx] != ZERO_BT) {
|
2009-03-06 00:10:22 +01:00
|
|
|
const float gain = cce->coup.gain[index][idx];
|
2008-08-09 12:46:27 +02:00
|
|
|
for (group = 0; group < ics->group_len[g]; group++) {
|
2009-08-02 12:50:59 +02:00
|
|
|
for (k = offsets[i]; k < offsets[i + 1]; k++) {
|
2014-01-29 13:12:59 +01:00
|
|
|
// FIXME: SIMDify
|
2009-08-02 12:50:59 +02:00
|
|
|
dest[group * 128 + k] += gain * src[group * 128 + k];
|
2008-08-09 12:46:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-08-02 12:50:59 +02:00
|
|
|
dest += ics->group_len[g] * 128;
|
|
|
|
src += ics->group_len[g] * 128;
|
2008-08-09 12:46:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Apply independent channel coupling (applied after IMDCT).
|
|
|
|
*
|
|
|
|
* @param index index into coupling gain array
|
|
|
|
*/
|
2009-08-02 12:50:59 +02:00
|
|
|
static void apply_independent_coupling(AACContext *ac,
|
|
|
|
SingleChannelElement *target,
|
|
|
|
ChannelElement *cce, int index)
|
|
|
|
{
|
2008-08-09 12:46:27 +02:00
|
|
|
int i;
|
2009-02-26 02:50:00 +01:00
|
|
|
const float gain = cce->coup.gain[index][0];
|
2009-08-02 12:50:59 +02:00
|
|
|
const float *src = cce->ch[0].ret;
|
|
|
|
float *dest = target->ret;
|
2012-04-11 01:27:28 +02:00
|
|
|
const int len = 1024 << (ac->oc[1].m4ac.sbr == 1);
|
2009-02-26 02:50:00 +01:00
|
|
|
|
2010-03-08 05:33:02 +01:00
|
|
|
for (i = 0; i < len; i++)
|
2011-01-27 16:20:43 +01:00
|
|
|
dest[i] += gain * src[i];
|
2008-08-09 12:46:27 +02:00
|
|
|
}
|
|
|
|
|
2015-06-30 11:53:03 +02:00
|
|
|
#include "aacdec_template.c"
|
2010-11-02 09:32:04 +01:00
|
|
|
|
|
|
|
#define LOAS_SYNC_WORD 0x2b7 ///< 11 bits LOAS sync word
|
|
|
|
|
|
|
|
struct LATMContext {
|
2013-06-27 01:22:06 +02:00
|
|
|
AACContext aac_ctx; ///< containing AACContext
|
2013-06-28 10:00:51 +02:00
|
|
|
int initialized; ///< initialized after a valid extradata was seen
|
2010-11-02 09:32:04 +01:00
|
|
|
|
|
|
|
// parser data
|
2013-06-27 01:22:06 +02:00
|
|
|
int audio_mux_version_A; ///< LATM syntax version
|
|
|
|
int frame_length_type; ///< 0/1 variable/fixed frame length
|
|
|
|
int frame_length; ///< frame length for fixed frame length
|
2010-11-02 09:32:04 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static inline uint32_t latm_get_value(GetBitContext *b)
|
|
|
|
{
|
|
|
|
int length = get_bits(b, 2);
|
|
|
|
|
|
|
|
return get_bits_long(b, (length+1)*8);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int latm_decode_audio_specific_config(struct LATMContext *latmctx,
|
2011-09-23 22:34:26 +02:00
|
|
|
GetBitContext *gb, int asclen)
|
2010-11-02 09:32:04 +01:00
|
|
|
{
|
2011-10-22 22:04:00 +02:00
|
|
|
AACContext *ac = &latmctx->aac_ctx;
|
|
|
|
AVCodecContext *avctx = ac->avctx;
|
2012-04-11 01:27:28 +02:00
|
|
|
MPEG4AudioConfig m4ac = { 0 };
|
2011-10-21 17:25:30 +02:00
|
|
|
int config_start_bit = get_bits_count(gb);
|
|
|
|
int sync_extension = 0;
|
|
|
|
int bits_consumed, esize;
|
|
|
|
|
|
|
|
if (asclen) {
|
|
|
|
sync_extension = 1;
|
|
|
|
asclen = FFMIN(asclen, get_bits_left(gb));
|
|
|
|
} else
|
|
|
|
asclen = get_bits_left(gb);
|
2010-11-02 09:32:04 +01:00
|
|
|
|
|
|
|
if (config_start_bit % 8) {
|
2013-03-13 21:18:34 +01:00
|
|
|
avpriv_request_sample(latmctx->aac_ctx.avctx,
|
|
|
|
"Non-byte-aligned audio-specific config");
|
2012-10-09 00:41:34 +02:00
|
|
|
return AVERROR_PATCHWELCOME;
|
2011-10-22 22:04:00 +02:00
|
|
|
}
|
2012-01-11 23:03:57 +01:00
|
|
|
if (asclen <= 0)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2011-10-22 22:04:00 +02:00
|
|
|
bits_consumed = decode_audio_specific_config(NULL, avctx, &m4ac,
|
2010-11-02 09:32:04 +01:00
|
|
|
gb->buffer + (config_start_bit / 8),
|
2011-10-21 17:25:30 +02:00
|
|
|
asclen, sync_extension);
|
2010-11-02 09:32:04 +01:00
|
|
|
|
2011-10-22 22:04:00 +02:00
|
|
|
if (bits_consumed < 0)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2011-12-23 00:14:32 +01:00
|
|
|
if (!latmctx->initialized ||
|
|
|
|
ac->oc[1].m4ac.sample_rate != m4ac.sample_rate ||
|
2012-04-11 01:27:28 +02:00
|
|
|
ac->oc[1].m4ac.chan_config != m4ac.chan_config) {
|
2011-10-22 22:04:00 +02:00
|
|
|
|
2012-07-08 00:20:21 +02:00
|
|
|
if(latmctx->initialized) {
|
|
|
|
av_log(avctx, AV_LOG_INFO, "audio config changed\n");
|
|
|
|
} else {
|
2013-01-17 16:12:59 +01:00
|
|
|
av_log(avctx, AV_LOG_DEBUG, "initializing latmctx\n");
|
2012-07-08 00:20:21 +02:00
|
|
|
}
|
2011-10-22 22:04:00 +02:00
|
|
|
latmctx->initialized = 0;
|
2010-11-02 09:32:04 +01:00
|
|
|
|
|
|
|
esize = (bits_consumed+7) / 8;
|
|
|
|
|
2011-10-22 22:04:00 +02:00
|
|
|
if (avctx->extradata_size < esize) {
|
2010-11-02 09:32:04 +01:00
|
|
|
av_free(avctx->extradata);
|
2015-06-29 23:48:34 +02:00
|
|
|
avctx->extradata = av_malloc(esize + AV_INPUT_BUFFER_PADDING_SIZE);
|
2010-11-02 09:32:04 +01:00
|
|
|
if (!avctx->extradata)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
avctx->extradata_size = esize;
|
|
|
|
memcpy(avctx->extradata, gb->buffer + (config_start_bit/8), esize);
|
2015-06-29 23:48:34 +02:00
|
|
|
memset(avctx->extradata+esize, 0, AV_INPUT_BUFFER_PADDING_SIZE);
|
2010-11-02 09:32:04 +01:00
|
|
|
}
|
2011-10-22 22:04:00 +02:00
|
|
|
skip_bits_long(gb, bits_consumed);
|
2010-11-02 09:32:04 +01:00
|
|
|
|
|
|
|
return bits_consumed;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int read_stream_mux_config(struct LATMContext *latmctx,
|
|
|
|
GetBitContext *gb)
|
|
|
|
{
|
|
|
|
int ret, audio_mux_version = get_bits(gb, 1);
|
|
|
|
|
|
|
|
latmctx->audio_mux_version_A = 0;
|
|
|
|
if (audio_mux_version)
|
|
|
|
latmctx->audio_mux_version_A = get_bits(gb, 1);
|
|
|
|
|
|
|
|
if (!latmctx->audio_mux_version_A) {
|
|
|
|
|
|
|
|
if (audio_mux_version)
|
|
|
|
latm_get_value(gb); // taraFullness
|
|
|
|
|
|
|
|
skip_bits(gb, 1); // allStreamSameTimeFraming
|
|
|
|
skip_bits(gb, 6); // numSubFrames
|
|
|
|
// numPrograms
|
|
|
|
if (get_bits(gb, 4)) { // numPrograms
|
2013-03-13 21:18:34 +01:00
|
|
|
avpriv_request_sample(latmctx->aac_ctx.avctx, "Multiple programs");
|
2010-11-02 09:32:04 +01:00
|
|
|
return AVERROR_PATCHWELCOME;
|
|
|
|
}
|
|
|
|
|
2012-09-09 21:55:23 +02:00
|
|
|
// for each program (which there is only one in DVB)
|
2010-11-02 09:32:04 +01:00
|
|
|
|
2012-09-09 21:55:23 +02:00
|
|
|
// for each layer (which there is only one in DVB)
|
2010-11-02 09:32:04 +01:00
|
|
|
if (get_bits(gb, 3)) { // numLayer
|
2013-03-13 21:18:34 +01:00
|
|
|
avpriv_request_sample(latmctx->aac_ctx.avctx, "Multiple layers");
|
2010-11-02 09:32:04 +01:00
|
|
|
return AVERROR_PATCHWELCOME;
|
|
|
|
}
|
|
|
|
|
|
|
|
// for all but first stream: use_same_config = get_bits(gb, 1);
|
|
|
|
if (!audio_mux_version) {
|
2011-09-23 22:34:26 +02:00
|
|
|
if ((ret = latm_decode_audio_specific_config(latmctx, gb, 0)) < 0)
|
2010-11-02 09:32:04 +01:00
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
int ascLen = latm_get_value(gb);
|
2011-09-23 22:34:26 +02:00
|
|
|
if ((ret = latm_decode_audio_specific_config(latmctx, gb, ascLen)) < 0)
|
2010-11-02 09:32:04 +01:00
|
|
|
return ret;
|
|
|
|
ascLen -= ret;
|
|
|
|
skip_bits_long(gb, ascLen);
|
|
|
|
}
|
|
|
|
|
|
|
|
latmctx->frame_length_type = get_bits(gb, 3);
|
|
|
|
switch (latmctx->frame_length_type) {
|
|
|
|
case 0:
|
|
|
|
skip_bits(gb, 8); // latmBufferFullness
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
latmctx->frame_length = get_bits(gb, 9);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
case 4:
|
|
|
|
case 5:
|
|
|
|
skip_bits(gb, 6); // CELP frame length table index
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
case 7:
|
|
|
|
skip_bits(gb, 1); // HVXC frame length table index
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (get_bits(gb, 1)) { // other data
|
|
|
|
if (audio_mux_version) {
|
|
|
|
latm_get_value(gb); // other_data_bits
|
|
|
|
} else {
|
|
|
|
int esc;
|
|
|
|
do {
|
|
|
|
esc = get_bits(gb, 1);
|
|
|
|
skip_bits(gb, 8);
|
|
|
|
} while (esc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (get_bits(gb, 1)) // crc present
|
|
|
|
skip_bits(gb, 8); // config_crc
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int read_payload_length_info(struct LATMContext *ctx, GetBitContext *gb)
|
|
|
|
{
|
|
|
|
uint8_t tmp;
|
|
|
|
|
|
|
|
if (ctx->frame_length_type == 0) {
|
|
|
|
int mux_slot_length = 0;
|
|
|
|
do {
|
|
|
|
tmp = get_bits(gb, 8);
|
|
|
|
mux_slot_length += tmp;
|
|
|
|
} while (tmp == 255);
|
|
|
|
return mux_slot_length;
|
|
|
|
} else if (ctx->frame_length_type == 1) {
|
|
|
|
return ctx->frame_length;
|
|
|
|
} else if (ctx->frame_length_type == 3 ||
|
|
|
|
ctx->frame_length_type == 5 ||
|
|
|
|
ctx->frame_length_type == 7) {
|
|
|
|
skip_bits(gb, 2); // mux_slot_length_coded
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int read_audio_mux_element(struct LATMContext *latmctx,
|
|
|
|
GetBitContext *gb)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
uint8_t use_same_mux = get_bits(gb, 1);
|
|
|
|
if (!use_same_mux) {
|
|
|
|
if ((err = read_stream_mux_config(latmctx, gb)) < 0)
|
|
|
|
return err;
|
|
|
|
} else if (!latmctx->aac_ctx.avctx->extradata) {
|
|
|
|
av_log(latmctx->aac_ctx.avctx, AV_LOG_DEBUG,
|
|
|
|
"no decoder config found\n");
|
|
|
|
return AVERROR(EAGAIN);
|
|
|
|
}
|
|
|
|
if (latmctx->audio_mux_version_A == 0) {
|
|
|
|
int mux_slot_length_bytes = read_payload_length_info(latmctx, gb);
|
|
|
|
if (mux_slot_length_bytes * 8 > get_bits_left(gb)) {
|
|
|
|
av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR, "incomplete frame\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
} else if (mux_slot_length_bytes * 8 + 256 < get_bits_left(gb)) {
|
|
|
|
av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,
|
|
|
|
"frame length mismatch %d << %d\n",
|
|
|
|
mux_slot_length_bytes * 8, get_bits_left(gb));
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-06 18:17:45 +02:00
|
|
|
static int latm_decode_frame(AVCodecContext *avctx, void *out,
|
|
|
|
int *got_frame_ptr, AVPacket *avpkt)
|
2010-11-02 09:32:04 +01:00
|
|
|
{
|
|
|
|
struct LATMContext *latmctx = avctx->priv_data;
|
|
|
|
int muxlength, err;
|
|
|
|
GetBitContext gb;
|
|
|
|
|
2013-01-27 22:44:29 +01:00
|
|
|
if ((err = init_get_bits8(&gb, avpkt->data, avpkt->size)) < 0)
|
|
|
|
return err;
|
2010-11-02 09:32:04 +01:00
|
|
|
|
|
|
|
// check for LOAS sync word
|
|
|
|
if (get_bits(&gb, 11) != LOAS_SYNC_WORD)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2010-11-07 04:05:12 +01:00
|
|
|
muxlength = get_bits(&gb, 13) + 3;
|
2012-09-09 21:55:23 +02:00
|
|
|
// not enough data, the parser should have sorted this out
|
2010-11-07 04:05:12 +01:00
|
|
|
if (muxlength > avpkt->size)
|
2010-11-02 09:32:04 +01:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
if ((err = read_audio_mux_element(latmctx, &gb)) < 0)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
if (!latmctx->initialized) {
|
|
|
|
if (!avctx->extradata) {
|
2011-09-06 18:17:45 +02:00
|
|
|
*got_frame_ptr = 0;
|
2010-11-02 09:32:04 +01:00
|
|
|
return avpkt->size;
|
|
|
|
} else {
|
2012-04-11 01:27:28 +02:00
|
|
|
push_output_configuration(&latmctx->aac_ctx);
|
2011-10-21 17:13:03 +02:00
|
|
|
if ((err = decode_audio_specific_config(
|
2012-04-11 01:27:28 +02:00
|
|
|
&latmctx->aac_ctx, avctx, &latmctx->aac_ctx.oc[1].m4ac,
|
2015-08-03 01:22:27 +02:00
|
|
|
avctx->extradata, avctx->extradata_size*8LL, 1)) < 0) {
|
2012-04-11 01:27:28 +02:00
|
|
|
pop_output_configuration(&latmctx->aac_ctx);
|
2010-11-02 09:32:04 +01:00
|
|
|
return err;
|
2012-04-11 01:27:28 +02:00
|
|
|
}
|
2010-11-02 09:32:04 +01:00
|
|
|
latmctx->initialized = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (show_bits(&gb, 12) == 0xfff) {
|
|
|
|
av_log(latmctx->aac_ctx.avctx, AV_LOG_ERROR,
|
|
|
|
"ADTS header detected, probably as result of configuration "
|
|
|
|
"misparsing\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2015-01-22 06:24:23 +01:00
|
|
|
switch (latmctx->aac_ctx.oc[1].m4ac.object_type) {
|
|
|
|
case AOT_ER_AAC_LC:
|
|
|
|
case AOT_ER_AAC_LTP:
|
|
|
|
case AOT_ER_AAC_LD:
|
|
|
|
case AOT_ER_AAC_ELD:
|
|
|
|
err = aac_decode_er_frame(avctx, out, got_frame_ptr, &gb);
|
|
|
|
break;
|
|
|
|
default:
|
2015-02-04 13:31:24 +01:00
|
|
|
err = aac_decode_frame_int(avctx, out, got_frame_ptr, &gb, avpkt);
|
2015-01-22 06:24:23 +01:00
|
|
|
}
|
|
|
|
if (err < 0)
|
2010-11-02 09:32:04 +01:00
|
|
|
return err;
|
|
|
|
|
|
|
|
return muxlength;
|
|
|
|
}
|
|
|
|
|
2012-03-30 23:34:22 +02:00
|
|
|
static av_cold int latm_decode_init(AVCodecContext *avctx)
|
2010-11-02 09:32:04 +01:00
|
|
|
{
|
|
|
|
struct LATMContext *latmctx = avctx->priv_data;
|
2011-10-21 17:14:58 +02:00
|
|
|
int ret = aac_decode_init(avctx);
|
2010-11-02 09:32:04 +01:00
|
|
|
|
2011-10-21 17:14:58 +02:00
|
|
|
if (avctx->extradata_size > 0)
|
2010-11-02 09:32:04 +01:00
|
|
|
latmctx->initialized = !ret;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-01-25 22:40:11 +01:00
|
|
|
AVCodec ff_aac_decoder = {
|
2012-04-06 18:19:39 +02:00
|
|
|
.name = "aac",
|
2013-10-03 22:57:53 +02:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("AAC (Advanced Audio Coding)"),
|
2012-04-06 18:19:39 +02:00
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
2012-08-05 11:11:04 +02:00
|
|
|
.id = AV_CODEC_ID_AAC,
|
2012-04-06 18:19:39 +02:00
|
|
|
.priv_data_size = sizeof(AACContext),
|
|
|
|
.init = aac_decode_init,
|
|
|
|
.close = aac_decode_close,
|
|
|
|
.decode = aac_decode_frame,
|
|
|
|
.sample_fmts = (const enum AVSampleFormat[]) {
|
2012-10-14 06:12:55 +02:00
|
|
|
AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE
|
2009-08-02 12:50:59 +02:00
|
|
|
},
|
2015-07-07 02:41:27 +02:00
|
|
|
.capabilities = AV_CODEC_CAP_CHANNEL_CONF | AV_CODEC_CAP_DR1,
|
2015-10-14 20:25:51 +02:00
|
|
|
.caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
|
2009-09-28 17:35:13 +02:00
|
|
|
.channel_layouts = aac_channel_layout,
|
2012-05-07 15:43:42 +02:00
|
|
|
.flush = flush,
|
2012-12-30 05:07:12 +01:00
|
|
|
.priv_class = &aac_decoder_class,
|
2014-11-07 02:26:16 +01:00
|
|
|
.profiles = profiles,
|
2008-08-05 21:32:01 +02:00
|
|
|
};
|
2010-11-02 09:32:04 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
Note: This decoder filter is intended to decode LATM streams transferred
|
|
|
|
in MPEG transport streams which only contain one program.
|
|
|
|
To do a more complex LATM demuxing a separate LATM demuxer should be used.
|
|
|
|
*/
|
2011-01-25 22:40:11 +01:00
|
|
|
AVCodec ff_aac_latm_decoder = {
|
2012-04-06 18:19:39 +02:00
|
|
|
.name = "aac_latm",
|
2013-10-03 22:57:53 +02:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("AAC LATM (Advanced Audio Coding LATM syntax)"),
|
2012-04-06 18:19:39 +02:00
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
2012-08-05 11:11:04 +02:00
|
|
|
.id = AV_CODEC_ID_AAC_LATM,
|
2012-04-06 18:19:39 +02:00
|
|
|
.priv_data_size = sizeof(struct LATMContext),
|
|
|
|
.init = latm_decode_init,
|
|
|
|
.close = aac_decode_close,
|
|
|
|
.decode = latm_decode_frame,
|
|
|
|
.sample_fmts = (const enum AVSampleFormat[]) {
|
2012-10-14 06:12:55 +02:00
|
|
|
AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE
|
2010-11-02 09:32:04 +01:00
|
|
|
},
|
2015-07-07 02:41:27 +02:00
|
|
|
.capabilities = AV_CODEC_CAP_CHANNEL_CONF | AV_CODEC_CAP_DR1,
|
2015-10-14 20:25:51 +02:00
|
|
|
.caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
|
2010-11-02 09:32:04 +01:00
|
|
|
.channel_layouts = aac_channel_layout,
|
2011-11-26 14:37:39 +01:00
|
|
|
.flush = flush,
|
2014-11-07 02:26:16 +01:00
|
|
|
.profiles = profiles,
|
2010-11-02 09:32:04 +01:00
|
|
|
};
|