]> git.sesse.net Git - ffmpeg/commitdiff
ac3enc: do not right-shift fixed-point coefficients in the final MDCT stage.
authorJustin <justin.ruggles@gmail.com>
Fri, 11 Mar 2011 18:03:26 +0000 (13:03 -0500)
committerJustin Ruggles <justin.ruggles@gmail.com>
Mon, 14 Mar 2011 12:45:26 +0000 (08:45 -0400)
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
libavcodec/ac3enc_fixed.c
libavcodec/ac3enc_fixed.h
libavcodec/ac3enc_float.h
tests/ref/acodec/ac3_fixed
tests/ref/lavf/rm
tests/ref/seek/ac3_rm

index baa95979777777af86b3b9888c711f4faccaded8..4c01fe3cbdbc7a91ab04cdde69ed4ed0e96b0d1c 100644 (file)
@@ -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);
index 3de00ee484646905111a54ad1de170905ae6688d..e750a3903855f2a3cc6baf136a7bcb3c75496183 100644 (file)
@@ -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]);
+        }
+    }
 }
 
 
index 12c8ace4aa4829283d5536443b9c12fa12f7e7ec..bad2306321df1e85dac294ed2afba304b65a4d5d 100644 (file)
@@ -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.
index 1726ca045f6f404ae78c29eb8f1f918912659b19..a4702bb51a2cc7dca26e868306c5a920acaabe0c 100644 (file)
@@ -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
index f32443d3fbd70a3871c5b4e88b600d38453e8b19..4ef69ffd93b5d86742a039f263b32f7ee5ca42cf 100644 (file)
@@ -1,2 +1,2 @@
-07bd593823ebd721b3a32ef298bdfc20 *./tests/data/acodec/ac3.rm
+b3a8f0a8809a58b2ece90744f06fff96 *./tests/data/acodec/ac3.rm
 98751 ./tests/data/acodec/ac3.rm
index b8bf220778f66e4c539b63faf79a150b800bfd56..53b1255c83b621b34b729bbe67f8508ce14bf5ab 100644 (file)
@@ -1,2 +1,2 @@
-d149fc272dfd21fb8908ee21d7b1651b *./tests/data/lavf/lavf.rm
+7da378131db880bcf2e58305d54418ec *./tests/data/lavf/lavf.rm
 346706 ./tests/data/lavf/lavf.rm
index 07fcfa1ec65494e3c7cd9e5335db28cc9d81f812..9b7db9ae9cd9157283b228c18257dbce16689c9d 100644 (file)
@@ -4,28 +4,29 @@ ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:    271 size:   556
 ret: 0         st:-1 flags:1  ts: 1.894167
 ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:    271 size:   556
 ret: 0         st: 0 flags:0  ts: 0.788000
-ret: 0         st: 0 flags:1 dts:4160806.587000 pts:4160806.587000 pos:   3883 size:   116
+ret: 0         st: 0 flags:1 dts:12581.487000 pts:12581.487000 pos:   5822 size:   916
 ret: 0         st: 0 flags:1  ts:-0.317000
 ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:    271 size:   556
 ret: 0         st:-1 flags:0  ts: 2.576668
-ret: 0         st: 0 flags:1 dts:4160806.587000 pts:4160806.587000 pos:   3883 size:   116
+ret: 0         st: 0 flags:1 dts:524.800000 pts:524.800000 pos:   6155 size:   244
 ret:-1         st:-1 flags:1  ts: 1.470835
 ret: 0         st: 0 flags:0  ts: 0.365000
-ret: 0         st: 0 flags:1 dts:4160806.587000 pts:4160806.587000 pos:   3883 size:   116
+ret: 0         st: 0 flags:1 dts:12581.487000 pts:12581.487000 pos:   5822 size:   916
 ret: 0         st: 0 flags:1  ts:-0.741000
 ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:    271 size:   556
 ret:-1         st:-1 flags:0  ts: 2.153336
-ret:-1         st:-1 flags:1  ts: 1.047503
+ret: 0         st:-1 flags:1  ts: 1.047503
+ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:    271 size:   556
 ret: 0         st: 0 flags:0  ts:-0.058000
 ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:    271 size:   556
-ret:-1         st: 0 flags:1  ts: 2.836000
+ret: 0         st: 0 flags:1  ts: 2.836000
+ret: 0         st: 0 flags:1 dts: 2.681000 pts: 2.681000 pos:  44105 size:   558
 ret:-1         st:-1 flags:0  ts: 1.730004
 ret: 0         st:-1 flags:1  ts: 0.624171
 ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:    271 size:   556
 ret: 0         st: 0 flags:0  ts:-0.482000
 ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:    271 size:   556
-ret: 0         st: 0 flags:1  ts: 2.413000
-ret: 0         st: 0 flags:1 dts: 2.229000 pts: 2.229000 pos:  36705 size:   556
+ret:-1         st: 0 flags:1  ts: 2.413000
 ret:-1         st:-1 flags:0  ts: 1.306672
 ret: 0         st:-1 flags:1  ts: 0.200839
 ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:    271 size:   556
@@ -33,13 +34,12 @@ ret: 0         st: 0 flags:0  ts:-0.905000
 ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:    271 size:   556
 ret:-1         st: 0 flags:1  ts: 1.989000
 ret: 0         st:-1 flags:0  ts: 0.883340
-ret: 0         st: 0 flags:1 dts:4160806.587000 pts:4160806.587000 pos:   3883 size:   116
+ret: 0         st: 0 flags:1 dts:12581.487000 pts:12581.487000 pos:   5822 size:   916
 ret: 0         st:-1 flags:1  ts:-0.222493
 ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:    271 size:   556
-ret: 0         st: 0 flags:0  ts: 2.672000
-ret: 0         st: 0 flags:1 dts:6354.691000 pts:6354.691000 pos:  10783 size:   304
+ret:-1         st: 0 flags:0  ts: 2.672000
 ret:-1         st: 0 flags:1  ts: 1.566000
 ret: 0         st:-1 flags:0  ts: 0.460008
-ret: 0         st: 0 flags:1 dts:4160806.587000 pts:4160806.587000 pos:   3883 size:   116
+ret: 0         st: 0 flags:1 dts:12581.487000 pts:12581.487000 pos:   5822 size:   916
 ret: 0         st:-1 flags:1  ts:-0.645825
 ret: 0         st: 0 flags:1 dts: 0.000000 pts: 0.000000 pos:    271 size:   556