From ca6e7708b42e7d33ba3053bcd447d52a077bca25 Mon Sep 17 00:00:00 2001 From: Carl Eugen Hoyos Date: Tue, 30 Mar 2010 22:09:14 +0000 Subject: Add spectral extension to the E-AC-3 decoder. Original patch by Justin, updated and resubmitted by Christophe Gisquet, christophe D gisquet A gmail Originally committed as revision 22734 to svn://svn.ffmpeg.org/ffmpeg/trunk --- libavcodec/eac3dec.c | 105 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 95 insertions(+), 10 deletions(-) (limited to 'libavcodec/eac3dec.c') diff --git a/libavcodec/eac3dec.c b/libavcodec/eac3dec.c index 3784ccfb1d..52d15c83f8 100644 --- a/libavcodec/eac3dec.c +++ b/libavcodec/eac3dec.c @@ -23,10 +23,6 @@ /* * There are several features of E-AC-3 that this decoder does not yet support. * - * Spectral Extension - * There is a patch to get this working for the two samples we have that - * use it, but it needs some minor changes in order to be accepted. - * * Enhanced Coupling * No known samples exist. If any ever surface, this feature should not be * too difficult to implement. @@ -67,6 +63,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/(1<<31)); + 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 @@ -492,12 +577,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; } } @@ -514,6 +598,7 @@ int ff_eac3_parse_header(AC3DecodeContext *s) /* 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; -- cgit v1.2.3