summaryrefslogtreecommitdiff
path: root/libavcodec/ac3dec.c
diff options
context:
space:
mode:
Diffstat (limited to 'libavcodec/ac3dec.c')
-rw-r--r--libavcodec/ac3dec.c535
1 files changed, 370 insertions, 165 deletions
diff --git a/libavcodec/ac3dec.c b/libavcodec/ac3dec.c
index 4be0f1f411..eaa327a3ee 100644
--- a/libavcodec/ac3dec.c
+++ b/libavcodec/ac3dec.c
@@ -7,20 +7,20 @@
* Copyright (c) 2007-2008 Bartlomiej Wolowiec <bartek.wolowiec@gmail.com>
* Copyright (c) 2007 Justin Ruggles <justin.ruggles@gmail.com>
*
- * This file is part of Libav.
+ * This file is part of FFmpeg.
*
- * Libav is free software; you can redistribute it and/or
+ * 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.
*
- * Libav is distributed in the hope that it will be useful,
+ * 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 Libav; if not, write to the Free Software
+ * License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
@@ -63,8 +63,11 @@ static const uint8_t quantization_tab[16] = {
5, 6, 7, 8, 9, 10, 11, 12, 14, 16
};
+#if (!USE_FIXED)
/** dynamic range table. converts codes to scale factors. */
static float dynamic_range_tab[256];
+float ff_ac3_heavy_dynamic_range_tab[256];
+#endif
/** Adjustments in dB gain */
static const float gain_levels[9] = {
@@ -111,7 +114,7 @@ static const uint8_t ac3_default_coeffs[8][5][2] = {
static inline int
symmetric_dequant(int code, int levels)
{
- return ((code - (levels >> 1)) << 24) / levels;
+ return ((code - (levels >> 1)) * (1 << 24)) / levels;
}
/*
@@ -158,12 +161,21 @@ static av_cold void ac3_tables_init(void)
b5_mantissas[i] = symmetric_dequant(i, 15);
}
+#if (!USE_FIXED)
/* generate dynamic range table
reference: Section 7.7.1 Dynamic Range Control */
for (i = 0; i < 256; i++) {
int v = (i >> 5) - ((i >> 7) << 3) - 5;
dynamic_range_tab[i] = powf(2.0f, v) * ((i & 0x1F) | 0x20);
}
+
+ /* generate compr dynamic range table
+ reference: Section 7.7.2 Heavy Compression */
+ for (i = 0; i < 256; i++) {
+ int v = (i >> 4) - ((i >> 7) << 4) - 4;
+ ff_ac3_heavy_dynamic_range_tab[i] = powf(2.0f, v) * ((i & 0xF) | 0x10);
+ }
+#endif
}
/**
@@ -176,18 +188,26 @@ static av_cold int ac3_decode_init(AVCodecContext *avctx)
s->avctx = avctx;
- ff_ac3_common_init();
ac3_tables_init();
ff_mdct_init(&s->imdct_256, 8, 1, 1.0);
ff_mdct_init(&s->imdct_512, 9, 1, 1.0);
- ff_kbd_window_init(s->window, 5.0, 256);
+ AC3_RENAME(ff_kbd_window_init)(s->window, 5.0, 256);
ff_bswapdsp_init(&s->bdsp);
- avpriv_float_dsp_init(&s->fdsp, avctx->flags & AV_CODEC_FLAG_BITEXACT);
- ff_ac3dsp_init(&s->ac3dsp, avctx->flags & AV_CODEC_FLAG_BITEXACT);
+
+#if (USE_FIXED)
+ s->fdsp = avpriv_alloc_fixed_dsp(avctx->flags & AV_CODEC_FLAG_BITEXACT);
+#else
+ s->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
ff_fmt_convert_init(&s->fmt_conv, avctx);
+#endif
+
+ ff_ac3dsp_init(&s->ac3dsp, avctx->flags & AV_CODEC_FLAG_BITEXACT);
av_lfg_init(&s->dith_state, 0);
- avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
+ if (USE_FIXED)
+ avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
+ else
+ avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
/* allow downmixing to stereo or mono */
if (avctx->channels > 1 &&
@@ -219,9 +239,19 @@ static int ac3_parse_header(AC3DecodeContext *s)
/* read the rest of the bsi. read twice for dual mono mode. */
i = !s->channel_mode;
do {
- skip_bits(gbc, 5); // skip dialog normalization
- if (get_bits1(gbc))
- skip_bits(gbc, 8); //skip compression
+ s->dialog_normalization[(!s->channel_mode)-i] = -get_bits(gbc, 5);
+ if (s->dialog_normalization[(!s->channel_mode)-i] == 0) {
+ s->dialog_normalization[(!s->channel_mode)-i] = -31;
+ }
+ if (s->target_level != 0) {
+ s->level_gain[(!s->channel_mode)-i] = powf(2.0f,
+ (float)(s->target_level -
+ s->dialog_normalization[(!s->channel_mode)-i])/6.0f);
+ }
+ if (s->compression_exists[(!s->channel_mode)-i] = get_bits1(gbc)) {
+ s->heavy_dynamic_range[(!s->channel_mode)-i] =
+ AC3_HEAVY_RANGE(get_bits(gbc, 8));
+ }
if (get_bits1(gbc))
skip_bits(gbc, 8); //skip language code
if (get_bits1(gbc))
@@ -287,6 +317,7 @@ static int parse_frame_header(AC3DecodeContext *s)
s->fbw_channels = s->channels - s->lfe_on;
s->lfe_ch = s->fbw_channels + 1;
s->frame_size = hdr.frame_size;
+ s->superframe_size += hdr.frame_size;
s->preferred_downmix = AC3_DMIXMOD_NOTINDICATED;
s->center_mix_level = hdr.center_mix_level;
s->center_mix_level_ltrt = 4; // -3.0dB
@@ -338,48 +369,53 @@ static int set_downmix_coeffs(AC3DecodeContext *s)
float cmix = gain_levels[s-> center_mix_level];
float smix = gain_levels[s->surround_mix_level];
float norm0, norm1;
+ float downmix_coeffs[2][AC3_MAX_CHANNELS];
if (!s->downmix_coeffs[0]) {
- s->downmix_coeffs[0] = av_malloc(2 * AC3_MAX_CHANNELS *
- sizeof(**s->downmix_coeffs));
+ s->downmix_coeffs[0] = av_malloc_array(2 * AC3_MAX_CHANNELS,
+ sizeof(**s->downmix_coeffs));
if (!s->downmix_coeffs[0])
return AVERROR(ENOMEM);
s->downmix_coeffs[1] = s->downmix_coeffs[0] + AC3_MAX_CHANNELS;
}
for (i = 0; i < s->fbw_channels; i++) {
- s->downmix_coeffs[0][i] = gain_levels[ac3_default_coeffs[s->channel_mode][i][0]];
- s->downmix_coeffs[1][i] = gain_levels[ac3_default_coeffs[s->channel_mode][i][1]];
+ downmix_coeffs[0][i] = gain_levels[ac3_default_coeffs[s->channel_mode][i][0]];
+ downmix_coeffs[1][i] = gain_levels[ac3_default_coeffs[s->channel_mode][i][1]];
}
if (s->channel_mode > 1 && s->channel_mode & 1) {
- s->downmix_coeffs[0][1] = s->downmix_coeffs[1][1] = cmix;
+ downmix_coeffs[0][1] = downmix_coeffs[1][1] = cmix;
}
if (s->channel_mode == AC3_CHMODE_2F1R || s->channel_mode == AC3_CHMODE_3F1R) {
int nf = s->channel_mode - 2;
- s->downmix_coeffs[0][nf] = s->downmix_coeffs[1][nf] = smix * LEVEL_MINUS_3DB;
+ downmix_coeffs[0][nf] = downmix_coeffs[1][nf] = smix * LEVEL_MINUS_3DB;
}
if (s->channel_mode == AC3_CHMODE_2F2R || s->channel_mode == AC3_CHMODE_3F2R) {
int nf = s->channel_mode - 4;
- s->downmix_coeffs[0][nf] = s->downmix_coeffs[1][nf+1] = smix;
+ downmix_coeffs[0][nf] = downmix_coeffs[1][nf+1] = smix;
}
/* renormalize */
norm0 = norm1 = 0.0;
for (i = 0; i < s->fbw_channels; i++) {
- norm0 += s->downmix_coeffs[0][i];
- norm1 += s->downmix_coeffs[1][i];
+ norm0 += downmix_coeffs[0][i];
+ norm1 += downmix_coeffs[1][i];
}
norm0 = 1.0f / norm0;
norm1 = 1.0f / norm1;
for (i = 0; i < s->fbw_channels; i++) {
- s->downmix_coeffs[0][i] *= norm0;
- s->downmix_coeffs[1][i] *= norm1;
+ downmix_coeffs[0][i] *= norm0;
+ downmix_coeffs[1][i] *= norm1;
}
if (s->output_mode == AC3_CHMODE_MONO) {
for (i = 0; i < s->fbw_channels; i++)
- s->downmix_coeffs[0][i] = (s->downmix_coeffs[0][i] +
- s->downmix_coeffs[1][i]) * LEVEL_MINUS_3DB;
+ downmix_coeffs[0][i] = (downmix_coeffs[0][i] +
+ downmix_coeffs[1][i]) * LEVEL_MINUS_3DB;
+ }
+ for (i = 0; i < s->fbw_channels; i++) {
+ s->downmix_coeffs[0][i] = FIXR12(downmix_coeffs[0][i]);
+ s->downmix_coeffs[1][i] = FIXR12(downmix_coeffs[1][i]);
}
return 0;
@@ -389,7 +425,8 @@ static int set_downmix_coeffs(AC3DecodeContext *s)
* Decode the grouped exponents according to exponent strategy.
* reference: Section 7.1.3 Exponent Decoding
*/
-static int decode_exponents(GetBitContext *gbc, int exp_strategy, int ngrps,
+static int decode_exponents(AC3DecodeContext *s,
+ GetBitContext *gbc, int exp_strategy, int ngrps,
uint8_t absexp, int8_t *dexps)
{
int i, j, grp, group_size;
@@ -400,6 +437,10 @@ static int decode_exponents(GetBitContext *gbc, int exp_strategy, int ngrps,
group_size = exp_strategy + (exp_strategy == EXP_D45);
for (grp = 0, i = 0; grp < ngrps; grp++) {
expacc = get_bits(gbc, 7);
+ if (expacc >= 125) {
+ av_log(s->avctx, AV_LOG_ERROR, "expacc %d is out-of-range\n", expacc);
+ return AVERROR_INVALIDDATA;
+ }
dexp[i++] = ungroup_3_in_7_bits_tab[expacc][0];
dexp[i++] = ungroup_3_in_7_bits_tab[expacc][1];
dexp[i++] = ungroup_3_in_7_bits_tab[expacc][2];
@@ -409,8 +450,10 @@ static int decode_exponents(GetBitContext *gbc, int exp_strategy, int ngrps,
prevexp = absexp;
for (i = 0, j = 0; i < ngrps * 3; i++) {
prevexp += dexp[i] - 2;
- if (prevexp > 24U)
- return -1;
+ if (prevexp > 24U) {
+ av_log(s->avctx, AV_LOG_ERROR, "exponent %d is out-of-range\n", prevexp);
+ return AVERROR_INVALIDDATA;
+ }
switch (group_size) {
case 4: dexps[j++] = prevexp;
dexps[j++] = prevexp;
@@ -439,7 +482,7 @@ static void calc_transform_coeffs_cpl(AC3DecodeContext *s)
int cpl_coord = s->cpl_coords[ch][band] << 5;
for (bin = band_start; bin < band_end; bin++) {
s->fixed_coeffs[ch][bin] =
- MULH(s->fixed_coeffs[CPL_CH][bin] << 4, cpl_coord);
+ MULH(s->fixed_coeffs[CPL_CH][bin] * (1 << 4), cpl_coord);
}
if (ch == 2 && s->phase_flags[band]) {
for (bin = band_start; bin < band_end; bin++)
@@ -485,7 +528,7 @@ static void ac3_decode_transform_coeffs_ch(AC3DecodeContext *s, int ch_index, ma
case 0:
/* random noise with approximate range of -0.707 to 0.707 */
if (dither)
- mantissa = (av_lfg_get(&s->dith_state) / 362) - 5932275;
+ mantissa = (((av_lfg_get(&s->dith_state)>>8)*181)>>8) - 5931008;
else
mantissa = 0;
break;
@@ -532,8 +575,11 @@ static void ac3_decode_transform_coeffs_ch(AC3DecodeContext *s, int ch_index, ma
break;
default: /* 6 to 15 */
/* Shift mantissa and sign-extend it. */
- mantissa = get_sbits(gbc, quantization_tab[bap]);
- mantissa <<= 24 - quantization_tab[bap];
+ if (bap > 15) {
+ av_log(s->avctx, AV_LOG_ERROR, "bap %d is invalid in plain AC-3\n", bap);
+ bap = 15;
+ }
+ mantissa = (unsigned)get_sbits(gbc, quantization_tab[bap]) << (24 - quantization_tab[bap]);
break;
}
coeffs[freq] = mantissa >> exps[freq];
@@ -567,7 +613,7 @@ static inline void decode_transform_coeffs_ch(AC3DecodeContext *s, int blk,
/* if AHT is used, mantissas for all blocks are encoded in the first
block of the frame. */
int bin;
- if (!blk && CONFIG_EAC3_DECODER)
+ if (CONFIG_EAC3_DECODER && !blk)
ff_eac3_decode_transform_coeffs_aht_ch(s, ch);
for (bin = s->start_freq[ch]; bin < s->end_freq[ch]; bin++) {
s->fixed_coeffs[ch][bin] = s->pre_mantissa[ch][bin][blk] >> s->dexps[ch][bin];
@@ -638,27 +684,37 @@ static void do_rematrixing(AC3DecodeContext *s)
* Convert frequency domain coefficients to time-domain audio samples.
* reference: Section 7.9.4 Transformation Equations
*/
-static inline void do_imdct(AC3DecodeContext *s, int channels)
+static inline void do_imdct(AC3DecodeContext *s, int channels, int offset)
{
int ch;
for (ch = 1; ch <= channels; ch++) {
if (s->block_switch[ch]) {
int i;
- float *x = s->tmp_output + 128;
+ FFTSample *x = s->tmp_output + 128;
for (i = 0; i < 128; i++)
x[i] = s->transform_coeffs[ch][2 * i];
s->imdct_256.imdct_half(&s->imdct_256, s->tmp_output, x);
- s->fdsp.vector_fmul_window(s->outptr[ch - 1], s->delay[ch - 1],
+#if USE_FIXED
+ s->fdsp->vector_fmul_window_scaled(s->outptr[ch - 1], s->delay[ch - 1 + offset],
+ s->tmp_output, s->window, 128, 8);
+#else
+ s->fdsp->vector_fmul_window(s->outptr[ch - 1], s->delay[ch - 1 + offset],
s->tmp_output, s->window, 128);
+#endif
for (i = 0; i < 128; i++)
x[i] = s->transform_coeffs[ch][2 * i + 1];
- s->imdct_256.imdct_half(&s->imdct_256, s->delay[ch - 1], x);
+ s->imdct_256.imdct_half(&s->imdct_256, s->delay[ch - 1 + offset], x);
} else {
s->imdct_512.imdct_half(&s->imdct_512, s->tmp_output, s->transform_coeffs[ch]);
- s->fdsp.vector_fmul_window(s->outptr[ch - 1], s->delay[ch - 1],
+#if USE_FIXED
+ s->fdsp->vector_fmul_window_scaled(s->outptr[ch - 1], s->delay[ch - 1 + offset],
+ s->tmp_output, s->window, 128, 8);
+#else
+ s->fdsp->vector_fmul_window(s->outptr[ch - 1], s->delay[ch - 1 + offset],
s->tmp_output, s->window, 128);
- memcpy(s->delay[ch - 1], s->tmp_output + 128, 128 * sizeof(float));
+#endif
+ memcpy(s->delay[ch - 1 + offset], s->tmp_output + 128, 128 * sizeof(FFTSample));
}
}
}
@@ -706,30 +762,31 @@ static void ac3_upmix_delay(AC3DecodeContext *s)
* @param[in] default_band_struct default band structure table
* @param[out] num_bands number of bands (optionally NULL)
* @param[out] band_sizes array containing the number of bins in each band (optionally NULL)
+ * @param[in,out] band_struct current band structure
*/
static void decode_band_structure(GetBitContext *gbc, int blk, int eac3,
int ecpl, int start_subband, int end_subband,
const uint8_t *default_band_struct,
- int *num_bands, uint8_t *band_sizes)
+ int *num_bands, uint8_t *band_sizes,
+ uint8_t *band_struct, int band_struct_size)
{
int subbnd, bnd, n_subbands, n_bands=0;
uint8_t bnd_sz[22];
- uint8_t coded_band_struct[22];
- const uint8_t *band_struct;
n_subbands = end_subband - start_subband;
+ if (!blk)
+ memcpy(band_struct, default_band_struct, band_struct_size);
+
+ av_assert0(band_struct_size >= start_subband + n_subbands);
+
+ band_struct += start_subband + 1;
+
/* decode band structure from bitstream or use default */
if (!eac3 || get_bits1(gbc)) {
for (subbnd = 0; subbnd < n_subbands - 1; subbnd++) {
- coded_band_struct[subbnd] = get_bits1(gbc);
+ band_struct[subbnd] = get_bits1(gbc);
}
- band_struct = coded_band_struct;
- } else if (!blk) {
- band_struct = &default_band_struct[start_subband+1];
- } else {
- /* no change in band structure */
- return;
}
/* calculate number of bands and band sizes based on band structure.
@@ -778,6 +835,9 @@ static inline int spx_strategy(AC3DecodeContext *s, int blk)
if (start_subband > 7)
start_subband += start_subband - 7;
end_subband = get_bits(bc, 3) + 5;
+#if USE_FIXED
+ s->spx_dst_end_freq = end_freq_inv_tab[end_subband-5];
+#endif
if (end_subband > 7)
end_subband += end_subband - 7;
dst_start_freq = dst_start_freq * 12 + 25;
@@ -798,14 +858,15 @@ static inline int spx_strategy(AC3DecodeContext *s, int blk)
s->spx_dst_start_freq = dst_start_freq;
s->spx_src_start_freq = src_start_freq;
- s->spx_dst_end_freq = dst_end_freq;
+ if (!USE_FIXED)
+ s->spx_dst_end_freq = dst_end_freq;
decode_band_structure(bc, blk, s->eac3, 0,
start_subband, end_subband,
ff_eac3_default_spx_band_struct,
&s->num_spx_bands,
- s->spx_band_sizes);
-
+ s->spx_band_sizes,
+ s->spx_band_struct, sizeof(s->spx_band_struct));
return 0;
}
@@ -818,26 +879,47 @@ static inline void spx_coordinates(AC3DecodeContext *s)
for (ch = 1; ch <= fbw_channels; ch++) {
if (s->channel_uses_spx[ch]) {
if (s->first_spx_coords[ch] || get_bits1(bc)) {
- float spx_blend;
+ INTFLOAT spx_blend;
int bin, master_spx_coord;
s->first_spx_coords[ch] = 0;
- spx_blend = get_bits(bc, 5) * (1.0f / 32);
+ spx_blend = AC3_SPX_BLEND(get_bits(bc, 5));
master_spx_coord = get_bits(bc, 2) * 3;
bin = s->spx_src_start_freq;
for (bnd = 0; bnd < s->num_spx_bands; bnd++) {
- int bandsize;
+ int bandsize = s->spx_band_sizes[bnd];
int spx_coord_exp, spx_coord_mant;
- float nratio, sblend, nblend, spx_coord;
+ INTFLOAT nratio, sblend, nblend;
+#if USE_FIXED
+ /* calculate blending factors */
+ int64_t accu = ((bin << 23) + (bandsize << 22))
+ * (int64_t)s->spx_dst_end_freq;
+ nratio = (int)(accu >> 32);
+ nratio -= spx_blend << 18;
+
+ if (nratio < 0) {
+ nblend = 0;
+ sblend = 0x800000;
+ } else if (nratio > 0x7fffff) {
+ nblend = 14529495; // sqrt(3) in FP.23
+ sblend = 0;
+ } else {
+ nblend = fixed_sqrt(nratio, 23);
+ accu = (int64_t)nblend * 1859775393;
+ nblend = (int)((accu + (1<<29)) >> 30);
+ sblend = fixed_sqrt(0x800000 - nratio, 23);
+ }
+#else
+ float spx_coord;
/* calculate blending factors */
- bandsize = s->spx_band_sizes[bnd];
nratio = ((float)((bin + (bandsize >> 1))) / s->spx_dst_end_freq) - spx_blend;
nratio = av_clipf(nratio, 0.0f, 1.0f);
nblend = sqrtf(3.0f * nratio); // noise is scaled by sqrt(3)
// to give unity variance
sblend = sqrtf(1.0f - nratio);
+#endif
bin += bandsize;
/* decode spx coordinates */
@@ -846,11 +928,18 @@ static inline void spx_coordinates(AC3DecodeContext *s)
if (spx_coord_exp == 15) spx_coord_mant <<= 1;
else spx_coord_mant += 4;
spx_coord_mant <<= (25 - spx_coord_exp - master_spx_coord);
- spx_coord = spx_coord_mant * (1.0f / (1 << 23));
/* multiply noise and signal blending factors by spx coordinate */
+#if USE_FIXED
+ accu = (int64_t)nblend * spx_coord_mant;
+ s->spx_noise_blend[ch][bnd] = (int)((accu + (1<<22)) >> 23);
+ accu = (int64_t)sblend * spx_coord_mant;
+ s->spx_signal_blend[ch][bnd] = (int)((accu + (1<<22)) >> 23);
+#else
+ spx_coord = spx_coord_mant * (1.0f / (1 << 23));
s->spx_noise_blend [ch][bnd] = nblend * spx_coord;
s->spx_signal_blend[ch][bnd] = sblend * spx_coord;
+#endif
}
}
} else {
@@ -914,7 +1003,8 @@ static inline int coupling_strategy(AC3DecodeContext *s, int blk,
decode_band_structure(bc, blk, s->eac3, 0, cpl_start_subband,
cpl_end_subband,
ff_eac3_default_cpl_band_struct,
- &s->num_cpl_bands, s->cpl_band_sizes);
+ &s->num_cpl_bands, s->cpl_band_sizes,
+ s->cpl_band_struct, sizeof(s->cpl_band_struct));
} else {
/* coupling not in use */
for (ch = 1; ch <= fbw_channels; ch++) {
@@ -974,7 +1064,7 @@ static inline int coupling_coordinates(AC3DecodeContext *s, int blk)
/**
* Decode a single audio block from the AC-3 bitstream.
*/
-static int decode_audio_block(AC3DecodeContext *s, int blk)
+static int decode_audio_block(AC3DecodeContext *s, int blk, int offset)
{
int fbw_channels = s->fbw_channels;
int channel_mode = s->channel_mode;
@@ -1008,13 +1098,14 @@ static int decode_audio_block(AC3DecodeContext *s, int blk)
if (get_bits1(gbc)) {
/* Allow asymmetric application of DRC when drc_scale > 1.
Amplification of quiet sounds is enhanced */
- float range = dynamic_range_tab[get_bits(gbc, 8)];
- if (range > 1.0 || s->drc_scale <= 1.0)
- s->dynamic_range[i] = powf(range, s->drc_scale);
+ int range_bits = get_bits(gbc, 8);
+ INTFLOAT range = AC3_RANGE(range_bits);
+ if (range_bits <= 127 || s->drc_scale <= 1.0)
+ s->dynamic_range[i] = AC3_DYNAMIC_RANGE(range);
else
s->dynamic_range[i] = range;
} else if (blk == 0) {
- s->dynamic_range[i] = 1.0f;
+ s->dynamic_range[i] = AC3_DYNAMIC_RANGE1;
}
} while (i--);
@@ -1024,11 +1115,13 @@ static int decode_audio_block(AC3DecodeContext *s, int blk)
if (s->spx_in_use) {
if ((ret = spx_strategy(s, blk)) < 0)
return ret;
- } else {
- for (ch = 1; ch <= fbw_channels; ch++) {
- s->channel_uses_spx[ch] = 0;
- s->first_spx_coords[ch] = 1;
- }
+ }
+ }
+ if (!s->eac3 || !s->spx_in_use) {
+ s->spx_in_use = 0;
+ for (ch = 1; ch <= fbw_channels; ch++) {
+ s->channel_uses_spx[ch] = 0;
+ s->first_spx_coords[ch] = 1;
}
}
@@ -1116,10 +1209,9 @@ static int decode_audio_block(AC3DecodeContext *s, int blk)
for (ch = !cpl_in_use; ch <= s->channels; ch++) {
if (s->exp_strategy[blk][ch] != EXP_REUSE) {
s->dexps[ch][0] = get_bits(gbc, 4) << !ch;
- if (decode_exponents(gbc, s->exp_strategy[blk][ch],
+ if (decode_exponents(s, gbc, s->exp_strategy[blk][ch],
s->num_exp_groups[ch], s->dexps[ch][0],
&s->dexps[ch][s->start_freq[ch]+!!ch])) {
- av_log(s->avctx, AV_LOG_ERROR, "exponent out-of-range\n");
return AVERROR_INVALIDDATA;
}
if (ch != CPL_CH && ch != s->lfe_ch)
@@ -1296,18 +1388,28 @@ static int decode_audio_block(AC3DecodeContext *s, int blk)
/* apply scaling to coefficients (headroom, dynrng) */
for (ch = 1; ch <= s->channels; ch++) {
- float gain = 1.0 / 4194304.0f;
- if (s->channel_mode == AC3_CHMODE_DUALMONO) {
- gain *= s->dynamic_range[2 - ch];
- } else {
- gain *= s->dynamic_range[0];
- }
+ int audio_channel = 0;
+ INTFLOAT gain;
+ if (s->channel_mode == AC3_CHMODE_DUALMONO && ch <= 2)
+ audio_channel = 2-ch;
+ if (s->heavy_compression && s->compression_exists[audio_channel])
+ gain = s->heavy_dynamic_range[audio_channel];
+ else
+ gain = s->dynamic_range[audio_channel];
+
+#if USE_FIXED
+ scale_coefs(s->transform_coeffs[ch], s->fixed_coeffs[ch], gain, 256);
+#else
+ if (s->target_level != 0)
+ gain = gain * s->level_gain[audio_channel];
+ gain *= 1.0 / 4194304.0f;
s->fmt_conv.int32_to_float_fmul_scalar(s->transform_coeffs[ch],
s->fixed_coeffs[ch], gain, 256);
+#endif
}
/* apply spectral extension to high frequency bins */
- if (s->spx_in_use && CONFIG_EAC3_DECODER) {
+ if (CONFIG_EAC3_DECODER && s->spx_in_use) {
ff_eac3_apply_spectral_extension(s);
}
@@ -1325,25 +1427,30 @@ static int decode_audio_block(AC3DecodeContext *s, int blk)
ac3_upmix_delay(s);
}
- do_imdct(s, s->channels);
+ do_imdct(s, s->channels, offset);
if (downmix_output) {
+#if USE_FIXED
+ ac3_downmix_c_fixed16(s->outptr, s->downmix_coeffs,
+ s->out_channels, s->fbw_channels, 256);
+#else
ff_ac3dsp_downmix(&s->ac3dsp, s->outptr, s->downmix_coeffs,
s->out_channels, s->fbw_channels, 256);
+#endif
}
} else {
if (downmix_output) {
- ff_ac3dsp_downmix(&s->ac3dsp, s->xcfptr + 1, s->downmix_coeffs,
- s->out_channels, s->fbw_channels, 256);
+ AC3_RENAME(ff_ac3dsp_downmix)(&s->ac3dsp, s->xcfptr + 1, s->downmix_coeffs,
+ s->out_channels, s->fbw_channels, 256);
}
if (downmix_output && !s->downmixed) {
s->downmixed = 1;
- ff_ac3dsp_downmix(&s->ac3dsp, s->dlyptr, s->downmix_coeffs,
- s->out_channels, s->fbw_channels, 128);
+ AC3_RENAME(ff_ac3dsp_downmix)(&s->ac3dsp, s->dlyptr, s->downmix_coeffs,
+ s->out_channels, s->fbw_channels, 128);
}
- do_imdct(s, s->out_channels);
+ do_imdct(s, s->out_channels, offset);
}
return 0;
@@ -1357,14 +1464,37 @@ static int ac3_decode_frame(AVCodecContext * avctx, void *data,
{
AVFrame *frame = data;
const uint8_t *buf = avpkt->data;
- int buf_size = avpkt->size;
+ int buf_size, full_buf_size = avpkt->size;
AC3DecodeContext *s = avctx->priv_data;
- int blk, ch, err, ret;
+ int blk, ch, err, offset, ret;
+ int i;
+ int skip = 0, got_independent_frame = 0;
const uint8_t *channel_map;
- const float *output[AC3_MAX_CHANNELS];
+ uint8_t extended_channel_map[EAC3_MAX_CHANNELS];
+ const SHORTFLOAT *output[AC3_MAX_CHANNELS];
enum AVMatrixEncoding matrix_encoding;
AVDownmixInfo *downmix_info;
+ s->superframe_size = 0;
+
+ buf_size = full_buf_size;
+ for (i = 1; i < buf_size; i += 2) {
+ if (buf[i] == 0x77 || buf[i] == 0x0B) {
+ if ((buf[i] ^ buf[i-1]) == (0x77 ^ 0x0B)) {
+ i--;
+ break;
+ } else if ((buf[i] ^ buf[i+1]) == (0x77 ^ 0x0B)) {
+ break;
+ }
+ }
+ }
+ if (i >= buf_size)
+ return AVERROR_INVALIDDATA;
+ if (i > 10)
+ return i;
+ buf += i;
+ buf_size -= i;
+
/* copy input buffer to decoder context to avoid reading past the end
of the buffer, which can be caused by a damaged input stream. */
if (buf_size >= 2 && AV_RB16(buf) == 0x770B) {
@@ -1374,9 +1504,18 @@ static int ac3_decode_frame(AVCodecContext * avctx, void *data,
(const uint16_t *) buf, cnt);
} else
memcpy(s->input_buffer, buf, FFMIN(buf_size, AC3_FRAME_BUFFER_SIZE));
+
+ /* if consistent noise generation is enabled, seed the linear feedback generator
+ * with the contents of the AC-3 frame so that the noise is identical across
+ * decodes given the same AC-3 frame data, for use with non-linear edititing software. */
+ if (s->consistent_noise_generation)
+ av_lfg_init_from_data(&s->dith_state, s->input_buffer, FFMIN(buf_size, AC3_FRAME_BUFFER_SIZE));
+
buf = s->input_buffer;
+dependent_frame:
/* initialize the GetBitContext with the start of valid AC-3 Frame */
- init_get_bits(&s->gbc, buf, buf_size * 8);
+ if ((ret = init_get_bits8(&s->gbc, buf, buf_size)) < 0)
+ return ret;
/* parse the syncinfo */
err = parse_frame_header(s);
@@ -1397,11 +1536,11 @@ static int ac3_decode_frame(AVCodecContext * avctx, void *data,
break;
case AAC_AC3_PARSE_ERROR_FRAME_TYPE:
/* skip frame if CRC is ok. otherwise use error concealment. */
- /* TODO: add support for substreams and dependent frames */
- if (s->frame_type == EAC3_FRAME_TYPE_DEPENDENT || s->substreamid) {
+ /* TODO: add support for substreams */
+ if (s->substreamid) {
av_log(avctx, AV_LOG_DEBUG,
- "unsupported frame type %d: skipping frame\n",
- s->frame_type);
+ "unsupported substream %d: skipping frame\n",
+ s->substreamid);
*got_frame_ptr = 0;
return buf_size;
} else {
@@ -1420,7 +1559,7 @@ static int ac3_decode_frame(AVCodecContext * avctx, void *data,
if (s->frame_size > buf_size) {
av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");
err = AAC_AC3_PARSE_ERROR_FRAME_SIZE;
- } else if (avctx->err_recognition & AV_EF_CRCCHECK) {
+ } else if (avctx->err_recognition & (AV_EF_CRCCHECK|AV_EF_CAREFUL)) {
/* check for crc mismatch */
if (av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, &buf[2],
s->frame_size - 2)) {
@@ -1432,10 +1571,10 @@ static int ac3_decode_frame(AVCodecContext * avctx, void *data,
}
}
- /* if frame is ok, set audio parameters */
- if (!err) {
- avctx->sample_rate = s->sample_rate;
- avctx->bit_rate = s->bit_rate;
+ if (s->frame_type == EAC3_FRAME_TYPE_DEPENDENT && !got_independent_frame) {
+ av_log(avctx, AV_LOG_WARNING, "Ignoring dependent frame without independent frame.\n");
+ *got_frame_ptr = 0;
+ return FFMIN(full_buf_size, s->frame_size);
}
/* channel config */
@@ -1454,6 +1593,10 @@ static int ac3_decode_frame(AVCodecContext * avctx, void *data,
s->output_mode = AC3_CHMODE_STEREO;
}
+ s->loro_center_mix_level = gain_levels[s-> center_mix_level];
+ s->loro_surround_mix_level = gain_levels[s->surround_mix_level];
+ s->ltrt_center_mix_level = LEVEL_MINUS_3DB;
+ s->ltrt_surround_mix_level = LEVEL_MINUS_3DB;
/* set downmixing coefficients if needed */
if (s->channels != s->out_channels && !((s->output_mode & AC3_OUTPUT_LFEON) &&
s->fbw_channels == s->out_channels)) {
@@ -1476,39 +1619,147 @@ static int ac3_decode_frame(AVCodecContext * avctx, void *data,
if (s->bitstream_mode == 0x7 && s->channels > 1)
avctx->audio_service_type = AV_AUDIO_SERVICE_TYPE_KARAOKE;
- /* get output buffer */
- frame->nb_samples = s->num_blocks * AC3_BLOCK_SIZE;
- if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
- av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
- return ret;
- }
-
/* decode the audio blocks */
channel_map = ff_ac3_dec_channel_map[s->output_mode & ~AC3_OUTPUT_LFEON][s->lfe_on];
+ offset = s->frame_type == EAC3_FRAME_TYPE_DEPENDENT ? AC3_MAX_CHANNELS : 0;
+ for (ch = 0; ch < AC3_MAX_CHANNELS; ch++) {
+ output[ch] = s->output[ch + offset];
+ s->outptr[ch] = s->output[ch + offset];
+ }
for (ch = 0; ch < s->channels; ch++) {
if (ch < s->out_channels)
- s->outptr[channel_map[ch]] = (float *)frame->data[ch];
- else
- s->outptr[ch] = s->output[ch];
- output[ch] = s->output[ch];
+ s->outptr[channel_map[ch]] = s->output_buffer[ch + offset];
}
for (blk = 0; blk < s->num_blocks; blk++) {
- if (!err && decode_audio_block(s, blk)) {
+ if (!err && decode_audio_block(s, blk, offset)) {
av_log(avctx, AV_LOG_ERROR, "error decoding the audio block\n");
err = 1;
}
if (err)
for (ch = 0; ch < s->out_channels; ch++)
- memcpy(s->outptr[channel_map[ch]], output[ch], sizeof(**output) * AC3_BLOCK_SIZE);
+ memcpy(s->output_buffer[ch + offset] + AC3_BLOCK_SIZE*blk, output[ch], AC3_BLOCK_SIZE*sizeof(SHORTFLOAT));
for (ch = 0; ch < s->out_channels; ch++)
output[ch] = s->outptr[channel_map[ch]];
- for (ch = 0; ch < s->out_channels; ch++)
- s->outptr[ch] += AC3_BLOCK_SIZE;
+ for (ch = 0; ch < s->out_channels; ch++) {
+ if (!ch || channel_map[ch])
+ s->outptr[channel_map[ch]] += AC3_BLOCK_SIZE;
+ }
}
/* keep last block for error concealment in next frame */
for (ch = 0; ch < s->out_channels; ch++)
- memcpy(s->output[ch], output[ch], sizeof(**output) * AC3_BLOCK_SIZE);
+ memcpy(s->output[ch + offset], output[ch], AC3_BLOCK_SIZE*sizeof(SHORTFLOAT));
+
+ /* check if there is dependent frame */
+ if (buf_size > s->frame_size) {
+ AC3HeaderInfo hdr;
+ int err;
+
+ if (buf_size - s->frame_size <= 16) {
+ skip = buf_size - s->frame_size;
+ goto skip;
+ }
+
+ if ((ret = init_get_bits8(&s->gbc, buf + s->frame_size, buf_size - s->frame_size)) < 0)
+ return ret;
+
+ err = ff_ac3_parse_header(&s->gbc, &hdr);
+ if (err)
+ return err;
+
+ if (hdr.frame_type == EAC3_FRAME_TYPE_DEPENDENT) {
+ if (hdr.num_blocks != s->num_blocks || s->sample_rate != hdr.sample_rate) {
+ av_log(avctx, AV_LOG_WARNING, "Ignoring non-compatible dependent frame.\n");
+ } else {
+ buf += s->frame_size;
+ buf_size -= s->frame_size;
+ s->prev_output_mode = s->output_mode;
+ s->prev_bit_rate = s->bit_rate;
+ got_independent_frame = 1;
+ goto dependent_frame;
+ }
+ }
+ }
+skip:
+
+ frame->decode_error_flags = err ? FF_DECODE_ERROR_INVALID_BITSTREAM : 0;
+
+ /* if frame is ok, set audio parameters */
+ if (!err) {
+ avctx->sample_rate = s->sample_rate;
+ avctx->bit_rate = s->bit_rate + s->prev_bit_rate;
+ }
+
+ for (ch = 0; ch < EAC3_MAX_CHANNELS; ch++)
+ extended_channel_map[ch] = ch;
+
+ if (s->frame_type == EAC3_FRAME_TYPE_DEPENDENT) {
+ uint64_t ich_layout = avpriv_ac3_channel_layout_tab[s->prev_output_mode & ~AC3_OUTPUT_LFEON];
+ int channel_map_size = ff_ac3_channels_tab[s->output_mode & ~AC3_OUTPUT_LFEON] + s->lfe_on;
+ uint64_t channel_layout;
+ int extend = 0;
+
+ if (s->prev_output_mode & AC3_OUTPUT_LFEON)
+ ich_layout |= AV_CH_LOW_FREQUENCY;
+
+ channel_layout = ich_layout;
+ for (ch = 0; ch < 16; ch++) {
+ if (s->channel_map & (1 << (EAC3_MAX_CHANNELS - ch - 1))) {
+ channel_layout |= ff_eac3_custom_channel_map_locations[ch][1];
+ }
+ }
+ if (av_get_channel_layout_nb_channels(channel_layout) > EAC3_MAX_CHANNELS) {
+ av_log(avctx, AV_LOG_ERROR, "Too many channels (%d) coded\n",
+ av_get_channel_layout_nb_channels(channel_layout));
+ return AVERROR_INVALIDDATA;
+ }
+
+ avctx->channel_layout = channel_layout;
+ avctx->channels = av_get_channel_layout_nb_channels(channel_layout);
+
+ for (ch = 0; ch < EAC3_MAX_CHANNELS; ch++) {
+ if (s->channel_map & (1 << (EAC3_MAX_CHANNELS - ch - 1))) {
+ if (ff_eac3_custom_channel_map_locations[ch][0]) {
+ int index = av_get_channel_layout_channel_index(channel_layout,
+ ff_eac3_custom_channel_map_locations[ch][1]);
+ if (index < 0)
+ return AVERROR_INVALIDDATA;
+ if (extend >= channel_map_size)
+ return AVERROR_INVALIDDATA;
+
+ extended_channel_map[index] = offset + channel_map[extend++];
+ } else {
+ int i;
+
+ for (i = 0; i < 64; i++) {
+ if ((1ULL << i) & ff_eac3_custom_channel_map_locations[ch][1]) {
+ int index = av_get_channel_layout_channel_index(channel_layout,
+ 1ULL << i);
+ if (index < 0)
+ return AVERROR_INVALIDDATA;
+ if (extend >= channel_map_size)
+ return AVERROR_INVALIDDATA;
+
+ extended_channel_map[index] = offset + channel_map[extend++];
+ }
+ }
+ }
+ }
+ }
+ }
+
+ /* get output buffer */
+ frame->nb_samples = s->num_blocks * AC3_BLOCK_SIZE;
+ if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
+ return ret;
+
+ for (ch = 0; ch < avctx->channels; ch++) {
+ int map = extended_channel_map[ch];
+ av_assert0(ch>=AV_NUM_DATA_POINTERS || frame->extended_data[ch] == frame->data[ch]);
+ memcpy((SHORTFLOAT *)frame->extended_data[ch],
+ s->output_buffer[map],
+ s->num_blocks * AC3_BLOCK_SIZE * sizeof(SHORTFLOAT));
+ }
/*
* AVMatrixEncoding
@@ -1568,7 +1819,10 @@ static int ac3_decode_frame(AVCodecContext * avctx, void *data,
*got_frame_ptr = 1;
- return FFMIN(buf_size, s->frame_size);
+ if (!s->superframe_size)
+ return FFMIN(full_buf_size, s->frame_size + skip);
+
+ return FFMIN(full_buf_size, s->superframe_size + skip);
}
/**
@@ -1579,6 +1833,7 @@ static av_cold int ac3_decode_end(AVCodecContext *avctx)
AC3DecodeContext *s = avctx->priv_data;
ff_mdct_end(&s->imdct_512);
ff_mdct_end(&s->imdct_256);
+ av_freep(&s->fdsp);
av_freep(&s->downmix_coeffs[0]);
return 0;
@@ -1586,53 +1841,3 @@ static av_cold int ac3_decode_end(AVCodecContext *avctx)
#define OFFSET(x) offsetof(AC3DecodeContext, x)
#define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM)
-static const AVOption options[] = {
- { "drc_scale", "percentage of dynamic range compression to apply", OFFSET(drc_scale), AV_OPT_TYPE_FLOAT, {.dbl = 1.0}, 0.0, 6.0, PAR },
- { NULL},
-};
-
-static const AVClass ac3_decoder_class = {
- .class_name = "AC3 decoder",
- .item_name = av_default_item_name,
- .option = options,
- .version = LIBAVUTIL_VERSION_INT,
-};
-
-AVCodec ff_ac3_decoder = {
- .name = "ac3",
- .long_name = NULL_IF_CONFIG_SMALL("ATSC A/52A (AC-3)"),
- .type = AVMEDIA_TYPE_AUDIO,
- .id = AV_CODEC_ID_AC3,
- .priv_data_size = sizeof (AC3DecodeContext),
- .init = ac3_decode_init,
- .close = ac3_decode_end,
- .decode = ac3_decode_frame,
- .capabilities = AV_CODEC_CAP_DR1,
- .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
- AV_SAMPLE_FMT_NONE },
- .priv_class = &ac3_decoder_class,
-};
-
-#if CONFIG_EAC3_DECODER
-static const AVClass eac3_decoder_class = {
- .class_name = "E-AC3 decoder",
- .item_name = av_default_item_name,
- .option = options,
- .version = LIBAVUTIL_VERSION_INT,
-};
-
-AVCodec ff_eac3_decoder = {
- .name = "eac3",
- .long_name = NULL_IF_CONFIG_SMALL("ATSC A/52B (AC-3, E-AC-3)"),
- .type = AVMEDIA_TYPE_AUDIO,
- .id = AV_CODEC_ID_EAC3,
- .priv_data_size = sizeof (AC3DecodeContext),
- .init = ac3_decode_init,
- .close = ac3_decode_end,
- .decode = ac3_decode_frame,
- .capabilities = AV_CODEC_CAP_DR1,
- .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP,
- AV_SAMPLE_FMT_NONE },
- .priv_class = &eac3_decoder_class,
-};
-#endif