From 323e6fead07c75f418e4b60704a4f437bb3483b2 Mon Sep 17 00:00:00 2001 From: Justin Date: Fri, 11 Mar 2011 13:03:26 -0500 Subject: ac3enc: do not right-shift fixed-point coefficients in the final MDCT stage. This increases the accuracy of coefficients, leading to improved quality. Rescaling of the coefficients to full 25-bit accuracy is done rather than offsetting the exponent values. This requires coefficient scaling to be done before determining the rematrixing strategy. Also, the rematrixing strategy calculation must use 64-bit math to prevent overflow due to the higher precision coefficients. --- libavcodec/ac3enc.c | 25 ++++++++++++------------ libavcodec/ac3enc_fixed.c | 49 ++++++++++++++++++++++++++++++++++------------- libavcodec/ac3enc_fixed.h | 2 ++ libavcodec/ac3enc_float.h | 2 ++ 4 files changed, 52 insertions(+), 26 deletions(-) (limited to 'libavcodec') diff --git a/libavcodec/ac3enc.c b/libavcodec/ac3enc.c index baa9597977..4c01fe3cbd 100644 --- a/libavcodec/ac3enc.c +++ b/libavcodec/ac3enc.c @@ -78,7 +78,7 @@ typedef struct AC3Block { int16_t **band_psd; ///< psd per critical band int16_t **mask; ///< masking curve uint16_t **qmant; ///< quantized mantissas - int8_t exp_shift[AC3_MAX_CHANNELS]; ///< exponent shift values + uint8_t coeff_shift[AC3_MAX_CHANNELS]; ///< fixed-point coefficient shift values uint8_t new_rematrixing_strategy; ///< send new rematrixing flags in this block uint8_t rematrixing_flags[4]; ///< rematrixing flags } AC3Block; @@ -269,7 +269,7 @@ static void apply_mdct(AC3EncodeContext *s) apply_window(&s->dsp, s->windowed_samples, input_samples, s->mdct.window, AC3_WINDOW_SIZE); - block->exp_shift[ch] = normalize_samples(s); + block->coeff_shift[ch] = normalize_samples(s); mdct512(&s->mdct, block->mdct_coef[ch], s->windowed_samples); } @@ -328,10 +328,10 @@ static void compute_rematrixing_strategy(AC3EncodeContext *s) CoefType rt = block->mdct_coef[1][i]; CoefType md = lt + rt; CoefType sd = lt - rt; - sum[0] += lt * lt; - sum[1] += rt * rt; - sum[2] += md * md; - sum[3] += sd * sd; + MAC_COEF(sum[0], lt, lt); + MAC_COEF(sum[1], rt, rt); + MAC_COEF(sum[2], md, md); + MAC_COEF(sum[3], sd, sd); } /* compare sums to determine if rematrixing will be used for this band */ @@ -416,14 +416,13 @@ static void extract_exponents(AC3EncodeContext *s) AC3Block *block = &s->blocks[blk]; uint8_t *exp = block->exp[ch]; int32_t *coef = block->fixed_coef[ch]; - int exp_shift = block->exp_shift[ch]; for (i = 0; i < AC3_MAX_COEFS; i++) { int e; int v = abs(coef[i]); if (v == 0) e = 24; else { - e = 23 - av_log2(v) + exp_shift; + e = 23 - av_log2(v); if (e >= 24) { e = 24; coef[i] = 0; @@ -1139,7 +1138,7 @@ static inline int asym_quant(int c, int e, int qbits) * Quantize a set of mantissas for a single channel in a single block. */ static void quantize_mantissas_blk_ch(AC3EncodeContext *s, int32_t *fixed_coef, - int8_t exp_shift, uint8_t *exp, + uint8_t *exp, uint8_t *bap, uint16_t *qmant, int n) { int i; @@ -1147,7 +1146,7 @@ static void quantize_mantissas_blk_ch(AC3EncodeContext *s, int32_t *fixed_coef, for (i = 0; i < n; i++) { int v; int c = fixed_coef[i]; - int e = exp[i] - exp_shift; + int e = exp[i]; int b = bap[i]; switch (b) { case 0: @@ -1243,7 +1242,7 @@ static void quantize_mantissas(AC3EncodeContext *s) s->qmant1_ptr = s->qmant2_ptr = s->qmant4_ptr = NULL; for (ch = 0; ch < s->channels; ch++) { - quantize_mantissas_blk_ch(s, block->fixed_coef[ch], block->exp_shift[ch], + quantize_mantissas_blk_ch(s, block->fixed_coef[ch], block->exp[ch], block->bap[ch], block->qmant[ch], s->nb_coefs[ch]); } @@ -1507,10 +1506,10 @@ static int ac3_encode_frame(AVCodecContext *avctx, unsigned char *frame, apply_mdct(s); - compute_rematrixing_strategy(s); - scale_coefficients(s); + compute_rematrixing_strategy(s); + apply_rematrixing(s); process_exponents(s); diff --git a/libavcodec/ac3enc_fixed.c b/libavcodec/ac3enc_fixed.c index 3de00ee484..e750a39038 100644 --- a/libavcodec/ac3enc_fixed.c +++ b/libavcodec/ac3enc_fixed.c @@ -131,10 +131,10 @@ mdct_alloc_fail: /** Complex multiply */ -#define CMUL(pre, pim, are, aim, bre, bim) \ +#define CMUL(pre, pim, are, aim, bre, bim, rshift) \ { \ - pre = (MUL16(are, bre) - MUL16(aim, bim)) >> 15; \ - pim = (MUL16(are, bim) + MUL16(bre, aim)) >> 15; \ + pre = (MUL16(are, bre) - MUL16(aim, bim)) >> rshift; \ + pim = (MUL16(are, bim) + MUL16(bre, aim)) >> rshift; \ } @@ -195,7 +195,7 @@ static void fft(AC3MDCTContext *mdct, IComplex *z, int ln) p++; q++; for(l = nblocks; l < np2; l += nblocks) { - CMUL(tmp_re, tmp_im, mdct->costab[l], -mdct->sintab[l], q->re, q->im); + CMUL(tmp_re, tmp_im, mdct->costab[l], -mdct->sintab[l], q->re, q->im, 15); BF(p->re, p->im, q->re, q->im, p->re, p->im, tmp_re, tmp_im); p++; @@ -234,7 +234,7 @@ static void mdct512(AC3MDCTContext *mdct, int32_t *out, int16_t *in) for (i = 0; i < n4; i++) { re = ((int)rot[ 2*i] - (int)rot[ n-1-2*i]) >> 1; im = -((int)rot[n2+2*i] - (int)rot[n2-1-2*i]) >> 1; - CMUL(x[i].re, x[i].im, re, im, -mdct->xcos1[i], mdct->xsin1[i]); + CMUL(x[i].re, x[i].im, re, im, -mdct->xcos1[i], mdct->xsin1[i], 15); } fft(mdct, x, mdct->nbits - 2); @@ -243,7 +243,7 @@ static void mdct512(AC3MDCTContext *mdct, int32_t *out, int16_t *in) for (i = 0; i < n4; i++) { re = x[i].re; im = x[i].im; - CMUL(out[n2-1-2*i], out[2*i], re, im, mdct->xsin1[i], mdct->xcos1[i]); + CMUL(out[n2-1-2*i], out[2*i], re, im, mdct->xsin1[i], mdct->xcos1[i], 0); } } @@ -294,10 +294,26 @@ static void lshift_tab(int16_t *tab, int n, unsigned int lshift) } +/** + * Right-shift each value in an array of int32_t by a specified amount. + * @param src input array + * @param len number of values in the array + * @param shift right shift amount + */ +static void ac3_rshift_int32_c(int32_t *src, unsigned int len, unsigned int shift) +{ + int i; + + if (shift > 0) { + for (i = 0; i < len; i++) + src[i] >>= shift; + } +} + + /** * Normalize the input samples to use the maximum available precision. - * This assumes signed 16-bit input samples. Exponents are reduced by 9 to - * match the 24-bit internal precision for MDCT coefficients. + * This assumes signed 16-bit input samples. * * @return exponent shift */ @@ -305,18 +321,25 @@ static int normalize_samples(AC3EncodeContext *s) { int v = 14 - log2_tab(s, s->windowed_samples, AC3_WINDOW_SIZE); lshift_tab(s->windowed_samples, AC3_WINDOW_SIZE, v); - return v - 9; + /* +6 to right-shift from 31-bit to 25-bit */ + return v + 6; } /** - * Scale MDCT coefficients from float to fixed-point. + * Scale MDCT coefficients to 25-bit signed fixed-point. */ static void scale_coefficients(AC3EncodeContext *s) { - /* scaling/conversion is obviously not needed for the fixed-point encoder - since the coefficients are already fixed-point. */ - return; + int blk, ch; + + for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) { + AC3Block *block = &s->blocks[blk]; + for (ch = 0; ch < s->channels; ch++) { + ac3_rshift_int32_c(block->mdct_coef[ch], AC3_MAX_COEFS, + block->coeff_shift[ch]); + } + } } diff --git a/libavcodec/ac3enc_fixed.h b/libavcodec/ac3enc_fixed.h index 12c8ace4aa..bad2306321 100644 --- a/libavcodec/ac3enc_fixed.h +++ b/libavcodec/ac3enc_fixed.h @@ -36,6 +36,8 @@ typedef int16_t SampleType; typedef int32_t CoefType; typedef int64_t CoefSumType; +#define MAC_COEF(d,a,b) MAC64(d,a,b) + /** * Compex number. diff --git a/libavcodec/ac3enc_float.h b/libavcodec/ac3enc_float.h index 1726ca045f..a4702bb51a 100644 --- a/libavcodec/ac3enc_float.h +++ b/libavcodec/ac3enc_float.h @@ -36,6 +36,8 @@ typedef float SampleType; typedef float CoefType; typedef float CoefSumType; +#define MAC_COEF(d,a,b) ((d)+=(a)*(b)) + typedef struct AC3MDCTContext { const float *window; ///< MDCT window function -- cgit v1.2.3