X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavcodec%2Feac3dec.c;h=89db7d38bca10d176eb598e38420552e14da59bf;hb=ed3b2a999d189771c9b0ada9a9200117595fa474;hp=0b10b41848a1256a9880f83b1b3e8b28143f46ee;hpb=f07fe6e78a6b3974b5fd00599b23f17734d6b4c8;p=ffmpeg diff --git a/libavcodec/eac3dec.c b/libavcodec/eac3dec.c index 0b10b41848a..89db7d38bca 100644 --- a/libavcodec/eac3dec.c +++ b/libavcodec/eac3dec.c @@ -3,28 +3,54 @@ * Copyright (c) 2007 Bartlomiej Wolowiec * Copyright (c) 2008 Justin Ruggles * - * This file is part of FFmpeg. + * This file is part of Libav. * - * FFmpeg is free software; you can redistribute it and/or + * 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. * - * FFmpeg is distributed in the hope that it will be useful, + * 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 FFmpeg; if not, write to the Free Software + * License along with Libav; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +/* + * There are several features of E-AC-3 that this decoder does not yet support. + * + * Enhanced Coupling + * No known samples exist. If any ever surface, this feature should not be + * too difficult to implement. + * + * Reduced Sample Rates + * No known samples exist. The spec also does not give clear information + * on how this is to be implemented. + * + * Dependent Streams + * Only the independent stream is currently decoded. Any dependent + * streams are skipped. We have only come across two examples of this, and + * they are both just test streams, one for HD-DVD and the other for + * Blu-ray. + * + * Transient Pre-noise Processing + * This is side information which a decoder should use to reduce artifacts + * caused by transients. There are samples which are known to have this + * information, but this decoder currently ignores it. + */ + + #include "avcodec.h" +#include "internal.h" +#include "aac_ac3_parser.h" #include "ac3.h" -#include "ac3_parser.h" #include "ac3dec.h" #include "ac3dec_data.h" +#include "eac3_data.h" /** gain adaptive quantization mode */ typedef enum { @@ -36,6 +62,95 @@ typedef enum { #define EAC3_SR_CODE_REDUCED 3 +void ff_eac3_apply_spectral_extension(AC3DecodeContext *s) +{ + int bin, bnd, ch, i; + uint8_t wrapflag[SPX_MAX_BANDS]={1,0,}, num_copy_sections, copy_sizes[SPX_MAX_BANDS]; + float rms_energy[SPX_MAX_BANDS]; + + /* Set copy index mapping table. Set wrap flags to apply a notch filter at + wrap points later on. */ + bin = s->spx_dst_start_freq; + num_copy_sections = 0; + for (bnd = 0; bnd < s->num_spx_bands; bnd++) { + int copysize; + int bandsize = s->spx_band_sizes[bnd]; + if (bin + bandsize > s->spx_src_start_freq) { + copy_sizes[num_copy_sections++] = bin - s->spx_dst_start_freq; + bin = s->spx_dst_start_freq; + wrapflag[bnd] = 1; + } + for (i = 0; i < bandsize; i += copysize) { + if (bin == s->spx_src_start_freq) { + copy_sizes[num_copy_sections++] = bin - s->spx_dst_start_freq; + bin = s->spx_dst_start_freq; + } + copysize = FFMIN(bandsize - i, s->spx_src_start_freq - bin); + bin += copysize; + } + } + copy_sizes[num_copy_sections++] = bin - s->spx_dst_start_freq; + + for (ch = 1; ch <= s->fbw_channels; ch++) { + if (!s->channel_uses_spx[ch]) + continue; + + /* Copy coeffs from normal bands to extension bands */ + bin = s->spx_src_start_freq; + for (i = 0; i < num_copy_sections; i++) { + memcpy(&s->transform_coeffs[ch][bin], + &s->transform_coeffs[ch][s->spx_dst_start_freq], + copy_sizes[i]*sizeof(float)); + bin += copy_sizes[i]; + } + + /* Calculate RMS energy for each SPX band. */ + bin = s->spx_src_start_freq; + for (bnd = 0; bnd < s->num_spx_bands; bnd++) { + int bandsize = s->spx_band_sizes[bnd]; + float accum = 0.0f; + for (i = 0; i < bandsize; i++) { + float coeff = s->transform_coeffs[ch][bin++]; + accum += coeff * coeff; + } + rms_energy[bnd] = sqrtf(accum / bandsize); + } + + /* Apply a notch filter at transitions between normal and extension + bands and at all wrap points. */ + if (s->spx_atten_code[ch] >= 0) { + const float *atten_tab = ff_eac3_spx_atten_tab[s->spx_atten_code[ch]]; + bin = s->spx_src_start_freq - 2; + for (bnd = 0; bnd < s->num_spx_bands; bnd++) { + if (wrapflag[bnd]) { + float *coeffs = &s->transform_coeffs[ch][bin]; + coeffs[0] *= atten_tab[0]; + coeffs[1] *= atten_tab[1]; + coeffs[2] *= atten_tab[2]; + coeffs[3] *= atten_tab[1]; + coeffs[4] *= atten_tab[0]; + } + bin += s->spx_band_sizes[bnd]; + } + } + + /* Apply noise-blended coefficient scaling based on previously + calculated RMS energy, blending factors, and SPX coordinates for + each band. */ + bin = s->spx_src_start_freq; + for (bnd = 0; bnd < s->num_spx_bands; bnd++) { + float nscale = s->spx_noise_blend[ch][bnd] * rms_energy[bnd] * (1.0f / INT32_MIN); + float sscale = s->spx_signal_blend[ch][bnd]; + for (i = 0; i < s->spx_band_sizes[bnd]; i++) { + float noise = nscale * (int32_t)av_lfg_get(&s->dith_state); + s->transform_coeffs[ch][bin] *= sscale; + s->transform_coeffs[ch][bin++] += noise; + } + } + } +} + + /** lrint(M_SQRT2*cos(2*M_PI/12)*(1<<23)) */ #define COEFF_0 10273905LL @@ -145,22 +260,24 @@ void ff_eac3_decode_transform_coeffs_aht_ch(AC3DecodeContext *s, int ch) for (blk = 0; blk < 6; blk++) { int mant = get_sbits(gbc, gbits); - if (mant == -(1 << (gbits-1))) { + if (log_gain && mant == -(1 << (gbits-1))) { /* large mantissa */ int b; - mant = get_sbits(gbc, bits-2+log_gain) << (26-log_gain-bits); + int mbits = bits - (2 - log_gain); + mant = get_sbits(gbc, mbits); + mant <<= (23 - (mbits - 1)); /* remap mantissa value to correct for asymmetric quantization */ if (mant >= 0) - b = 32768 >> (log_gain+8); + b = 1 << (23 - log_gain); else - b = ff_eac3_gaq_remap_2_4_b[hebap-8][log_gain-1]; - mant += (ff_eac3_gaq_remap_2_4_a[hebap-8][log_gain-1] * (mant>>8) + b) >> 7; + b = ff_eac3_gaq_remap_2_4_b[hebap-8][log_gain-1] << 8; + mant += ((ff_eac3_gaq_remap_2_4_a[hebap-8][log_gain-1] * (int64_t)mant) >> 15) + b; } else { /* small mantissa, no GAQ, or Gk=1 */ mant <<= 24 - bits; if (!log_gain) { /* remap mantissa value for no GAQ or Gk=1 */ - mant += (ff_eac3_gaq_remap_1[hebap-8] * (mant>>8)) >> 7; + mant += (ff_eac3_gaq_remap_1[hebap-8] * (int64_t)mant) >> 15; } } s->pre_mantissa[ch][bin][blk] = mant; @@ -182,11 +299,14 @@ int ff_eac3_parse_header(AC3DecodeContext *s) application can select from. each independent stream can also contain dependent streams which are used to add or replace channels. */ if (s->frame_type == EAC3_FRAME_TYPE_DEPENDENT) { - av_log_missing_feature(s->avctx, "Dependent substream decoding", 1); - return AC3_PARSE_ERROR_FRAME_TYPE; + if (!s->eac3_frame_dependent_found) { + s->eac3_frame_dependent_found = 1; + avpriv_request_sample(s->avctx, "Dependent substream decoding"); + } + return AAC_AC3_PARSE_ERROR_FRAME_TYPE; } else if (s->frame_type == EAC3_FRAME_TYPE_RESERVED) { av_log(s->avctx, AV_LOG_ERROR, "Reserved frame type\n"); - return AC3_PARSE_ERROR_FRAME_TYPE; + return AAC_AC3_PARSE_ERROR_FRAME_TYPE; } /* The substream id indicates which substream this frame belongs to. each @@ -194,8 +314,11 @@ int ff_eac3_parse_header(AC3DecodeContext *s) associated to an independent stream have matching substream id's. */ if (s->substreamid) { /* only decode substream with id=0. skip any additional substreams. */ - av_log_missing_feature(s->avctx, "Additional substreams", 1); - return AC3_PARSE_ERROR_FRAME_TYPE; + if (!s->eac3_subsbtreamid_found) { + s->eac3_subsbtreamid_found = 1; + avpriv_request_sample(s->avctx, "Additional substreams"); + } + return AAC_AC3_PARSE_ERROR_FRAME_TYPE; } if (s->bit_alloc_params.sr_code == EAC3_SR_CODE_REDUCED) { @@ -203,8 +326,8 @@ int ff_eac3_parse_header(AC3DecodeContext *s) rates in bit allocation. The best assumption would be that it is handled like AC-3 DolbyNet, but we cannot be sure until we have a sample which utilizes this feature. */ - av_log_missing_feature(s->avctx, "Reduced sampling rates", 1); - return -1; + avpriv_request_sample(s->avctx, "Reduced sampling rate"); + return AVERROR_PATCHWELCOME; } skip_bits(gbc, 5); // skip bitstream id @@ -227,23 +350,22 @@ int ff_eac3_parse_header(AC3DecodeContext *s) if (get_bits1(gbc)) { /* center and surround mix levels */ if (s->channel_mode > AC3_CHMODE_STEREO) { - skip_bits(gbc, 2); // skip preferred stereo downmix mode + s->preferred_downmix = get_bits(gbc, 2); if (s->channel_mode & 1) { /* if three front channels exist */ - skip_bits(gbc, 3); //skip Lt/Rt center mix level - s->center_mix_level = get_bits(gbc, 3); + s->center_mix_level_ltrt = get_bits(gbc, 3); + s->center_mix_level = get_bits(gbc, 3); } if (s->channel_mode & 4) { /* if a surround channel exists */ - skip_bits(gbc, 3); //skip Lt/Rt surround mix level - s->surround_mix_level = get_bits(gbc, 3); + s->surround_mix_level_ltrt = av_clip(get_bits(gbc, 3), 3, 7); + s->surround_mix_level = av_clip(get_bits(gbc, 3), 3, 7); } } /* lfe mix level */ - if (s->lfe_on && get_bits1(gbc)) { - // TODO: use LFE mix level - skip_bits(gbc, 5); // skip LFE mix level code + if (s->lfe_on && (s->lfe_mix_level_exists = get_bits1(gbc))) { + s->lfe_mix_level = get_bits(gbc, 5); } /* info for mixing with other streams and substreams */ @@ -292,13 +414,14 @@ int ff_eac3_parse_header(AC3DecodeContext *s) /* informational metadata */ if (get_bits1(gbc)) { - skip_bits(gbc, 3); // skip bit stream mode + s->bitstream_mode = get_bits(gbc, 3); skip_bits(gbc, 2); // skip copyright bit and original bitstream bit if (s->channel_mode == AC3_CHMODE_STEREO) { - skip_bits(gbc, 4); // skip Dolby surround and headphone mode + s->dolby_surround_mode = get_bits(gbc, 2); + s->dolby_headphone_mode = get_bits(gbc, 2); } if (s->channel_mode >= AC3_CHMODE_2F2R) { - skip_bits(gbc, 2); // skip Dolby surround EX mode + s->dolby_surround_ex_mode = get_bits(gbc, 2); } for (i = 0; i < (s->channel_mode ? 1 : 2); i++) { if (get_bits1(gbc)) { @@ -373,7 +496,7 @@ int ff_eac3_parse_header(AC3DecodeContext *s) s->skip_syntax = get_bits1(gbc); parse_spx_atten_data = get_bits1(gbc); - /* coupling strategy occurance and coupling use per block */ + /* coupling strategy occurrence and coupling use per block */ num_cpl_blocks = 0; if (s->channel_mode > 1) { for (blk = 0; blk < s->num_blocks; blk++) { @@ -459,12 +582,11 @@ int ff_eac3_parse_header(AC3DecodeContext *s) } /* spectral extension attenuation data */ - if (parse_spx_atten_data) { - av_log_missing_feature(s->avctx, "Spectral extension attenuation", 1); - for (ch = 1; ch <= s->fbw_channels; ch++) { - if (get_bits1(gbc)) { // channel has spx attenuation - skip_bits(gbc, 5); // skip spx attenuation code - } + for (ch = 1; ch <= s->fbw_channels; ch++) { + if (parse_spx_atten_data && get_bits1(gbc)) { + s->spx_atten_code[ch] = get_bits(gbc, 5); + } else { + s->spx_atten_code[ch] = -1; } } @@ -475,11 +597,13 @@ int ff_eac3_parse_header(AC3DecodeContext *s) The spec does not say what this data is or what it's used for. It is likely the offset of each block within the frame. */ int block_start_bits = (s->num_blocks-1) * (4 + av_log2(s->frame_size-2)); - skip_bits(gbc, block_start_bits); + skip_bits_long(gbc, block_start_bits); + avpriv_request_sample(s->avctx, "Block start info"); } /* syntax state initialization */ for (ch = 1; ch <= s->fbw_channels; ch++) { + s->first_spx_coords[ch] = 1; s->first_cpl_coords[ch] = 1; } s->first_cpl_leak = 1;