]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/ac3enc.c
Remove unused ac3_parametric_bit_allocation function.
[ffmpeg] / libavcodec / ac3enc.c
index e815c66640998892d2c87f5ec00f01ee384a6e69..f058d79882a7a3c17f5ea5581cc8b362206efdba 100644 (file)
 /** Maximum number of exponent groups. +1 for separate DC exponent. */
 #define AC3_MAX_EXP_GROUPS 85
 
+/* stereo rematrixing algorithms */
+#define AC3_REMATRIXING_IS_STATIC 0x1
+#define AC3_REMATRIXING_SUMS    0
+#define AC3_REMATRIXING_NONE    1
+#define AC3_REMATRIXING_ALWAYS  3
+
 /** Scale a float value by 2^bits and convert to an integer. */
 #define SCALE_FLOAT(a, bits) lrintf((a) * (float)(1 << (bits)))
 
 typedef struct AC3Block {
     uint8_t  **bap;                             ///< bit allocation pointers (bap)
     CoefType **mdct_coef;                       ///< MDCT coefficients
+    int32_t  **fixed_coef;                      ///< fixed-point MDCT coefficients
     uint8_t  **exp;                             ///< original exponents
     uint8_t  **grouped_exp;                     ///< grouped exponents
     int16_t  **psd;                             ///< psd per frequency bin
     int16_t  **band_psd;                        ///< psd per critical band
     int16_t  **mask;                            ///< masking curve
     uint16_t **qmant;                           ///< quantized mantissas
-    uint8_t  exp_strategy[AC3_MAX_CHANNELS];    ///< exponent strategies
     int8_t   exp_shift[AC3_MAX_CHANNELS];       ///< exponent shift values
+    uint8_t  new_rematrixing_strategy;          ///< send new rematrixing flags in this block
+    uint8_t  rematrixing_flags[4];              ///< rematrixing flags
 } AC3Block;
 
 /**
@@ -106,6 +114,8 @@ typedef struct AC3EncodeContext {
     int bandwidth_code[AC3_MAX_CHANNELS];   ///< bandwidth code (0 to 60)               (chbwcod)
     int nb_coefs[AC3_MAX_CHANNELS];
 
+    int rematrixing;                        ///< determines how rematrixing strategy is calculated
+
     /* bitrate allocation control */
     int slow_gain_code;                     ///< slow gain code                         (sgaincod)
     int slow_decay_code;                    ///< slow decay code                        (sdcycod)
@@ -128,6 +138,7 @@ typedef struct AC3EncodeContext {
     uint8_t *bap_buffer;
     uint8_t *bap1_buffer;
     CoefType *mdct_coef_buffer;
+    int32_t *fixed_coef_buffer;
     uint8_t *exp_buffer;
     uint8_t *grouped_exp_buffer;
     int16_t *psd_buffer;
@@ -135,6 +146,8 @@ typedef struct AC3EncodeContext {
     int16_t *mask_buffer;
     uint16_t *qmant_buffer;
 
+    uint8_t exp_strategy[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS]; ///< exponent strategies
+
     DECLARE_ALIGNED(16, SampleType, windowed_samples)[AC3_WINDOW_SIZE];
 } AC3EncodeContext;
 
@@ -148,11 +161,13 @@ static av_cold int mdct_init(AVCodecContext *avctx, AC3MDCTContext *mdct,
 
 static void mdct512(AC3MDCTContext *mdct, CoefType *out, SampleType *in);
 
-static void apply_window(SampleType *output, const SampleType *input,
+static void apply_window(DSPContext *dsp, SampleType *output, const SampleType *input,
                          const SampleType *window, int n);
 
 static int normalize_samples(AC3EncodeContext *s);
 
+static void scale_coefficients(AC3EncodeContext *s);
+
 
 /**
  * LUT for number of exponent groups.
@@ -247,7 +262,7 @@ static void apply_mdct(AC3EncodeContext *s)
             AC3Block *block = &s->blocks[blk];
             const SampleType *input_samples = &s->planar_samples[ch][blk * AC3_BLOCK_SIZE];
 
-            apply_window(s->windowed_samples, input_samples, s->mdct.window, AC3_WINDOW_SIZE);
+            apply_window(&s->dsp, s->windowed_samples, input_samples, s->mdct.window, AC3_WINDOW_SIZE);
 
             block->exp_shift[ch] = normalize_samples(s);
 
@@ -257,6 +272,114 @@ static void apply_mdct(AC3EncodeContext *s)
 }
 
 
+/**
+ * Initialize stereo rematrixing.
+ * If the strategy does not change for each frame, set the rematrixing flags.
+ */
+static void rematrixing_init(AC3EncodeContext *s)
+{
+    if (s->channel_mode == AC3_CHMODE_STEREO)
+        s->rematrixing = AC3_REMATRIXING_SUMS;
+    else
+        s->rematrixing = AC3_REMATRIXING_NONE;
+    /* NOTE: AC3_REMATRIXING_ALWAYS might be used in
+             the future in conjunction with channel coupling. */
+
+    if (s->rematrixing & AC3_REMATRIXING_IS_STATIC) {
+        int flag = (s->rematrixing == AC3_REMATRIXING_ALWAYS);
+        s->blocks[0].new_rematrixing_strategy = 1;
+        memset(s->blocks[0].rematrixing_flags, flag,
+               sizeof(s->blocks[0].rematrixing_flags));
+    }
+}
+
+
+/**
+ * Determine rematrixing flags for each block and band.
+ */
+static void compute_rematrixing_strategy(AC3EncodeContext *s)
+{
+    int nb_coefs;
+    int blk, bnd, i;
+    AC3Block *block, *block0;
+
+    if (s->rematrixing & AC3_REMATRIXING_IS_STATIC)
+        return;
+
+    nb_coefs = FFMIN(s->nb_coefs[0], s->nb_coefs[1]);
+
+    s->blocks[0].new_rematrixing_strategy = 1;
+    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
+        block = &s->blocks[blk];
+        for (bnd = 0; bnd < 4; bnd++) {
+            /* calculate calculate sum of squared coeffs for one band in one block */
+            int start = ff_ac3_rematrix_band_tab[bnd];
+            int end   = FFMIN(nb_coefs, ff_ac3_rematrix_band_tab[bnd+1]);
+            CoefSumType sum[4] = {0,};
+            for (i = start; i < end; i++) {
+                CoefType lt = block->mdct_coef[0][i];
+                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;
+            }
+
+            /* compare sums to determine if rematrixing will be used for this band */
+            if (FFMIN(sum[2], sum[3]) < FFMIN(sum[0], sum[1]))
+                block->rematrixing_flags[bnd] = 1;
+            else
+                block->rematrixing_flags[bnd] = 0;
+
+            /* determine if new rematrixing flags will be sent */
+            if (blk &&
+                !block->new_rematrixing_strategy &&
+                block->rematrixing_flags[bnd] != block0->rematrixing_flags[bnd]) {
+                block->new_rematrixing_strategy = 1;
+            }
+        }
+        block0 = block;
+    }
+}
+
+
+/**
+ * Apply stereo rematrixing to coefficients based on rematrixing flags.
+ */
+static void apply_rematrixing(AC3EncodeContext *s)
+{
+    int nb_coefs;
+    int blk, bnd, i;
+    int start, end;
+    uint8_t *flags;
+
+    if (s->rematrixing == AC3_REMATRIXING_NONE)
+        return;
+
+    nb_coefs = FFMIN(s->nb_coefs[0], s->nb_coefs[1]);
+
+    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
+        AC3Block *block = &s->blocks[blk];
+        if (block->new_rematrixing_strategy)
+            flags = block->rematrixing_flags;
+        for (bnd = 0; bnd < 4; bnd++) {
+            if (flags[bnd]) {
+                start = ff_ac3_rematrix_band_tab[bnd];
+                end   = FFMIN(nb_coefs, ff_ac3_rematrix_band_tab[bnd+1]);
+                for (i = start; i < end; i++) {
+                    int32_t lt = block->fixed_coef[0][i];
+                    int32_t rt = block->fixed_coef[1][i];
+                    block->fixed_coef[0][i] = (lt + rt) >> 1;
+                    block->fixed_coef[1][i] = (lt - rt) >> 1;
+                }
+            }
+        }
+    }
+}
+
+
 /**
  * Initialize exponent tables.
  */
@@ -286,11 +409,11 @@ static void extract_exponents(AC3EncodeContext *s)
         for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
             AC3Block *block = &s->blocks[blk];
             uint8_t *exp   = block->exp[ch];
-            CoefType *coef = block->mdct_coef[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(SCALE_COEF(coef[i]));
+                int v = abs(coef[i]);
                 if (v == 0)
                     e = 24;
                 else {
@@ -318,7 +441,7 @@ static void extract_exponents(AC3EncodeContext *s)
  * Calculate exponent strategies for all blocks in a single channel.
  */
 static void compute_exp_strategy_ch(AC3EncodeContext *s, uint8_t *exp_strategy,
-                                    uint8_t **exp)
+                                    uint8_t *exp)
 {
     int blk, blk1;
     int exp_diff;
@@ -326,12 +449,14 @@ static void compute_exp_strategy_ch(AC3EncodeContext *s, uint8_t *exp_strategy,
     /* estimate if the exponent variation & decide if they should be
        reused in the next frame */
     exp_strategy[0] = EXP_NEW;
+    exp += AC3_MAX_COEFS;
     for (blk = 1; blk < AC3_MAX_BLOCKS; blk++) {
-        exp_diff = s->dsp.sad[0](NULL, exp[blk], exp[blk-1], 16, 16);
+        exp_diff = s->dsp.sad[0](NULL, exp, exp - AC3_MAX_COEFS, 16, 16);
         if (exp_diff > EXP_DIFF_THRESHOLD)
             exp_strategy[blk] = EXP_NEW;
         else
             exp_strategy[blk] = EXP_REUSE;
+        exp += AC3_MAX_COEFS;
     }
     emms_c();
 
@@ -359,41 +484,41 @@ static void compute_exp_strategy_ch(AC3EncodeContext *s, uint8_t *exp_strategy,
  */
 static void compute_exp_strategy(AC3EncodeContext *s)
 {
-    uint8_t *exp1[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS];
-    uint8_t exp_str1[AC3_MAX_CHANNELS][AC3_MAX_BLOCKS];
     int ch, blk;
 
     for (ch = 0; ch < s->fbw_channels; ch++) {
-        for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
-            exp1[ch][blk]     = s->blocks[blk].exp[ch];
-            exp_str1[ch][blk] = s->blocks[blk].exp_strategy[ch];
-        }
-
-        compute_exp_strategy_ch(s, exp_str1[ch], exp1[ch]);
-
-        for (blk = 0; blk < AC3_MAX_BLOCKS; blk++)
-            s->blocks[blk].exp_strategy[ch] = exp_str1[ch][blk];
+        compute_exp_strategy_ch(s, s->exp_strategy[ch], s->blocks[0].exp[ch]);
     }
     if (s->lfe_on) {
         ch = s->lfe_channel;
-        s->blocks[0].exp_strategy[ch] = EXP_D15;
+        s->exp_strategy[ch][0] = EXP_D15;
         for (blk = 1; blk < AC3_MAX_BLOCKS; blk++)
-            s->blocks[blk].exp_strategy[ch] = EXP_REUSE;
+            s->exp_strategy[ch][blk] = EXP_REUSE;
     }
 }
 
 
 /**
  * Set each encoded exponent in a block to the minimum of itself and the
- * exponent in the same frequency bin of a following block.
- * exp[i] = min(exp[i], exp1[i]
+ * exponents in the same frequency bin of up to 5 following blocks.
  */
-static void exponent_min(uint8_t *exp, uint8_t *exp1, int n)
+static void exponent_min(uint8_t *exp, int num_reuse_blocks, int nb_coefs)
 {
-    int i;
-    for (i = 0; i < n; i++) {
-        if (exp1[i] < exp[i])
-            exp[i] = exp1[i];
+    int blk, i;
+
+    if (!num_reuse_blocks)
+        return;
+
+    for (i = 0; i < nb_coefs; i++) {
+        uint8_t min_exp = *exp;
+        uint8_t *exp1 = exp + AC3_MAX_COEFS;
+        for (blk = 0; blk < num_reuse_blocks; blk++) {
+            uint8_t next_exp = *exp1;
+            if (next_exp < min_exp)
+                min_exp = next_exp;
+            exp1 += AC3_MAX_COEFS;
+        }
+        *exp++ = min_exp;
     }
 }
 
@@ -472,31 +597,38 @@ static void encode_exponents_blk_ch(uint8_t *exp, int nb_exps, int exp_strategy)
  */
 static void encode_exponents(AC3EncodeContext *s)
 {
-    int blk, blk1, blk2, ch;
-    AC3Block *block, *block1, *block2;
+    int blk, blk1, ch;
+    uint8_t *exp, *exp1, *exp_strategy;
+    int nb_coefs, num_reuse_blocks;
 
     for (ch = 0; ch < s->channels; ch++) {
+        exp          = s->blocks[0].exp[ch];
+        exp_strategy = s->exp_strategy[ch];
+        nb_coefs     = s->nb_coefs[ch];
+
         blk = 0;
-        block = &s->blocks[0];
         while (blk < AC3_MAX_BLOCKS) {
             blk1 = blk + 1;
-            block1 = block + 1;
-            /* for the EXP_REUSE case we select the min of the exponents */
-            while (blk1 < AC3_MAX_BLOCKS && block1->exp_strategy[ch] == EXP_REUSE) {
-                exponent_min(block->exp[ch], block1->exp[ch], s->nb_coefs[ch]);
+
+            /* count the number of EXP_REUSE blocks after the current block */
+            while (blk1 < AC3_MAX_BLOCKS && exp_strategy[blk1] == EXP_REUSE)
                 blk1++;
-                block1++;
-            }
-            encode_exponents_blk_ch(block->exp[ch], s->nb_coefs[ch],
-                                    block->exp_strategy[ch]);
+            num_reuse_blocks = blk1 - blk - 1;
+
+            /* for the EXP_REUSE case we select the min of the exponents */
+            exponent_min(exp, num_reuse_blocks, nb_coefs);
+
+            encode_exponents_blk_ch(exp, nb_coefs, exp_strategy[blk]);
+
             /* copy encoded exponents for reuse case */
-            block2 = block + 1;
-            for (blk2 = blk+1; blk2 < blk1; blk2++, block2++) {
-                memcpy(block2->exp[ch], block->exp[ch],
-                       s->nb_coefs[ch] * sizeof(uint8_t));
+            exp1 = exp + AC3_MAX_COEFS;
+            while (blk < blk1-1) {
+                memcpy(exp1, exp, nb_coefs * sizeof(*exp));
+                exp1 += AC3_MAX_COEFS;
+                blk++;
             }
             blk = blk1;
-            block = block1;
+            exp = exp1;
         }
     }
 }
@@ -519,11 +651,11 @@ static void group_exponents(AC3EncodeContext *s)
     for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
         AC3Block *block = &s->blocks[blk];
         for (ch = 0; ch < s->channels; ch++) {
-            if (block->exp_strategy[ch] == EXP_REUSE) {
+            int exp_strategy = s->exp_strategy[ch][blk];
+            if (exp_strategy == EXP_REUSE)
                 continue;
-            }
-            group_size = block->exp_strategy[ch] + (block->exp_strategy[ch] == EXP_D45);
-            nb_groups = exponent_group_tab[block->exp_strategy[ch]-1][s->nb_coefs[ch]];
+            group_size = exp_strategy + (exp_strategy == EXP_D45);
+            nb_groups = exponent_group_tab[exp_strategy-1][s->nb_coefs[ch]];
             bit_count += 4 + (nb_groups * 7);
             p = block->exp[ch];
 
@@ -588,7 +720,6 @@ static void count_frame_bits_fixed(AC3EncodeContext *s)
     /* assumptions:
      *   no dynamic range codes
      *   no channel coupling
-     *   no rematrixing
      *   bit allocation parameters do not change between blocks
      *   SNR offsets do not change between blocks
      *   no delta bit allocation
@@ -605,8 +736,6 @@ static void count_frame_bits_fixed(AC3EncodeContext *s)
         frame_bits += s->fbw_channels * 2 + 2; /* blksw * c, dithflag * c, dynrnge, cplstre */
         if (s->channel_mode == AC3_CHMODE_STEREO) {
             frame_bits++; /* rematstr */
-            if (!blk)
-                frame_bits += 4;
         }
         frame_bits += 2 * s->fbw_channels; /* chexpstr[2] * c */
         if (s->lfe_on)
@@ -676,9 +805,14 @@ static void count_frame_bits(AC3EncodeContext *s)
     int frame_bits = 0;
 
     for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
-        uint8_t *exp_strategy = s->blocks[blk].exp_strategy;
+        /* stereo rematrixing */
+        if (s->channel_mode == AC3_CHMODE_STEREO &&
+            s->blocks[blk].new_rematrixing_strategy) {
+            frame_bits += 4;
+        }
+
         for (ch = 0; ch < s->fbw_channels; ch++) {
-            if (exp_strategy[ch] != EXP_REUSE)
+            if (s->exp_strategy[ch][blk] != EXP_REUSE)
                 frame_bits += 6 + 2; /* chbwcod[6], gainrng[2] */
         }
     }
@@ -741,7 +875,7 @@ static void bit_alloc_masking(AC3EncodeContext *s)
             /* We only need psd and mask for calculating bap.
                Since we currently do not calculate bap when exponent
                strategy is EXP_REUSE we do not need to calculate psd or mask. */
-            if (block->exp_strategy[ch] != EXP_REUSE) {
+            if (s->exp_strategy[ch][blk] != EXP_REUSE) {
                 ff_ac3_bit_alloc_calc_psd(block->exp[ch], 0,
                                           s->nb_coefs[ch],
                                           block->psd[ch], block->band_psd[ch]);
@@ -804,7 +938,7 @@ static int bit_alloc(AC3EncodeContext *s, int snr_offset)
                blocks within a frame are the exponent values.  We can take
                advantage of that by reusing the bit allocation pointers
                whenever we reuse exponents. */
-            if (block->exp_strategy[ch] == EXP_REUSE) {
+            if (s->exp_strategy[ch][blk] == EXP_REUSE) {
                 memcpy(block->bap[ch], s->blocks[blk-1].bap[ch], AC3_MAX_COEFS);
             } else {
                 ff_ac3_bit_alloc_calc_bap(block->mask[ch], block->psd[ch], 0,
@@ -880,16 +1014,16 @@ static int downgrade_exponents(AC3EncodeContext *s)
 
     for (ch = 0; ch < s->fbw_channels; ch++) {
         for (blk = AC3_MAX_BLOCKS-1; blk >= 0; blk--) {
-            if (s->blocks[blk].exp_strategy[ch] == EXP_D15) {
-                s->blocks[blk].exp_strategy[ch] = EXP_D25;
+            if (s->exp_strategy[ch][blk] == EXP_D15) {
+                s->exp_strategy[ch][blk] = EXP_D25;
                 return 0;
             }
         }
     }
     for (ch = 0; ch < s->fbw_channels; ch++) {
         for (blk = AC3_MAX_BLOCKS-1; blk >= 0; blk--) {
-            if (s->blocks[blk].exp_strategy[ch] == EXP_D25) {
-                s->blocks[blk].exp_strategy[ch] = EXP_D45;
+            if (s->exp_strategy[ch][blk] == EXP_D25) {
+                s->exp_strategy[ch][blk] = EXP_D45;
                 return 0;
             }
         }
@@ -898,8 +1032,8 @@ static int downgrade_exponents(AC3EncodeContext *s)
         /* block 0 cannot reuse exponents, so only downgrade D45 to REUSE if
            the block number > 0 */
         for (blk = AC3_MAX_BLOCKS-1; blk > 0; blk--) {
-            if (s->blocks[blk].exp_strategy[ch] > EXP_REUSE) {
-                s->blocks[blk].exp_strategy[ch] = EXP_REUSE;
+            if (s->exp_strategy[ch][blk] > EXP_REUSE) {
+                s->exp_strategy[ch][blk] = EXP_REUSE;
                 return 0;
             }
         }
@@ -1017,7 +1151,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, CoefType *mdct_coef,
+static void quantize_mantissas_blk_ch(AC3EncodeContext *s, int32_t *fixed_coef,
                                       int8_t exp_shift, uint8_t *exp,
                                       uint8_t *bap, uint16_t *qmant, int n)
 {
@@ -1025,7 +1159,7 @@ static void quantize_mantissas_blk_ch(AC3EncodeContext *s, CoefType *mdct_coef,
 
     for (i = 0; i < n; i++) {
         int v;
-        int c = SCALE_COEF(mdct_coef[i]);
+        int c = fixed_coef[i];
         int e = exp[i] - exp_shift;
         int b = bap[i];
         switch (b) {
@@ -1122,7 +1256,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->mdct_coef[ch], block->exp_shift[ch],
+            quantize_mantissas_blk_ch(s, block->fixed_coef[ch], block->exp_shift[ch],
                                       block->exp[ch], block->bap[ch],
                                       block->qmant[ch], s->nb_coefs[ch]);
         }
@@ -1164,10 +1298,10 @@ static void output_frame_header(AC3EncodeContext *s)
 /**
  * Write one audio block to the output bitstream.
  */
-static void output_audio_block(AC3EncodeContext *s, int block_num)
+static void output_audio_block(AC3EncodeContext *s, int blk)
 {
     int ch, i, baie, rbnd;
-    AC3Block *block = &s->blocks[block_num];
+    AC3Block *block = &s->blocks[blk];
 
     /* block switching */
     for (ch = 0; ch < s->fbw_channels; ch++)
@@ -1181,7 +1315,7 @@ static void output_audio_block(AC3EncodeContext *s, int block_num)
     put_bits(&s->pb, 1, 0);
 
     /* channel coupling */
-    if (!block_num) {
+    if (!blk) {
         put_bits(&s->pb, 1, 1); /* coupling strategy present */
         put_bits(&s->pb, 1, 0); /* no coupling strategy */
     } else {
@@ -1190,28 +1324,23 @@ static void output_audio_block(AC3EncodeContext *s, int block_num)
 
     /* stereo rematrixing */
     if (s->channel_mode == AC3_CHMODE_STEREO) {
-        if (!block_num) {
-            /* first block must define rematrixing (rematstr) */
-            put_bits(&s->pb, 1, 1);
-
-            /* dummy rematrixing rematflg(1:4)=0 */
+        put_bits(&s->pb, 1, block->new_rematrixing_strategy);
+        if (block->new_rematrixing_strategy) {
+            /* rematrixing flags */
             for (rbnd = 0; rbnd < 4; rbnd++)
-                put_bits(&s->pb, 1, 0);
-        } else {
-            /* no matrixing (but should be used in the future) */
-            put_bits(&s->pb, 1, 0);
+                put_bits(&s->pb, 1, block->rematrixing_flags[rbnd]);
         }
     }
 
     /* exponent strategy */
     for (ch = 0; ch < s->fbw_channels; ch++)
-        put_bits(&s->pb, 2, block->exp_strategy[ch]);
+        put_bits(&s->pb, 2, s->exp_strategy[ch][blk]);
     if (s->lfe_on)
-        put_bits(&s->pb, 1, block->exp_strategy[s->lfe_channel]);
+        put_bits(&s->pb, 1, s->exp_strategy[s->lfe_channel][blk]);
 
     /* bandwidth */
     for (ch = 0; ch < s->fbw_channels; ch++) {
-        if (block->exp_strategy[ch] != EXP_REUSE)
+        if (s->exp_strategy[ch][blk] != EXP_REUSE)
             put_bits(&s->pb, 6, s->bandwidth_code[ch]);
     }
 
@@ -1219,14 +1348,14 @@ static void output_audio_block(AC3EncodeContext *s, int block_num)
     for (ch = 0; ch < s->channels; ch++) {
         int nb_groups;
 
-        if (block->exp_strategy[ch] == EXP_REUSE)
+        if (s->exp_strategy[ch][blk] == EXP_REUSE)
             continue;
 
         /* DC exponent */
         put_bits(&s->pb, 4, block->grouped_exp[ch][0]);
 
         /* exponent groups */
-        nb_groups = exponent_group_tab[block->exp_strategy[ch]-1][s->nb_coefs[ch]];
+        nb_groups = exponent_group_tab[s->exp_strategy[ch][blk]-1][s->nb_coefs[ch]];
         for (i = 1; i <= nb_groups; i++)
             put_bits(&s->pb, 7, block->grouped_exp[ch][i]);
 
@@ -1236,7 +1365,7 @@ static void output_audio_block(AC3EncodeContext *s, int block_num)
     }
 
     /* bit allocation info */
-    baie = (block_num == 0);
+    baie = (blk == 0);
     put_bits(&s->pb, 1, baie);
     if (baie) {
         put_bits(&s->pb, 2, s->slow_decay_code);
@@ -1390,6 +1519,12 @@ static int ac3_encode_frame(AVCodecContext *avctx, unsigned char *frame,
 
     apply_mdct(s);
 
+    compute_rematrixing_strategy(s);
+
+    scale_coefficients(s);
+
+    apply_rematrixing(s);
+
     process_exponents(s);
 
     ret = compute_bit_allocation(s);
@@ -1420,6 +1555,7 @@ static av_cold int ac3_encode_close(AVCodecContext *avctx)
     av_freep(&s->bap_buffer);
     av_freep(&s->bap1_buffer);
     av_freep(&s->mdct_coef_buffer);
+    av_freep(&s->fixed_coef_buffer);
     av_freep(&s->exp_buffer);
     av_freep(&s->grouped_exp_buffer);
     av_freep(&s->psd_buffer);
@@ -1430,6 +1566,7 @@ static av_cold int ac3_encode_close(AVCodecContext *avctx)
         AC3Block *block = &s->blocks[blk];
         av_freep(&block->bap);
         av_freep(&block->mdct_coef);
+        av_freep(&block->fixed_coef);
         av_freep(&block->exp);
         av_freep(&block->grouped_exp);
         av_freep(&block->psd);
@@ -1628,14 +1765,37 @@ static av_cold int allocate_buffers(AVCodecContext *avctx)
                           alloc_fail);
 
         for (ch = 0; ch < s->channels; ch++) {
+            /* arrangement: block, channel, coeff */
             block->bap[ch]         = &s->bap_buffer        [AC3_MAX_COEFS * (blk * s->channels + ch)];
             block->mdct_coef[ch]   = &s->mdct_coef_buffer  [AC3_MAX_COEFS * (blk * s->channels + ch)];
-            block->exp[ch]         = &s->exp_buffer        [AC3_MAX_COEFS * (blk * s->channels + ch)];
             block->grouped_exp[ch] = &s->grouped_exp_buffer[128           * (blk * s->channels + ch)];
             block->psd[ch]         = &s->psd_buffer        [AC3_MAX_COEFS * (blk * s->channels + ch)];
             block->band_psd[ch]    = &s->band_psd_buffer   [64            * (blk * s->channels + ch)];
             block->mask[ch]        = &s->mask_buffer       [64            * (blk * s->channels + ch)];
             block->qmant[ch]       = &s->qmant_buffer      [AC3_MAX_COEFS * (blk * s->channels + ch)];
+
+            /* arrangement: channel, block, coeff */
+            block->exp[ch]         = &s->exp_buffer        [AC3_MAX_COEFS * (AC3_MAX_BLOCKS * ch + blk)];
+        }
+    }
+
+    if (CONFIG_AC3ENC_FLOAT) {
+        FF_ALLOC_OR_GOTO(avctx, s->fixed_coef_buffer, AC3_MAX_BLOCKS * s->channels *
+                         AC3_MAX_COEFS * sizeof(*s->fixed_coef_buffer), alloc_fail);
+        for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
+            AC3Block *block = &s->blocks[blk];
+            FF_ALLOCZ_OR_GOTO(avctx, block->fixed_coef, s->channels *
+                              sizeof(*block->fixed_coef), alloc_fail);
+            for (ch = 0; ch < s->channels; ch++)
+                block->fixed_coef[ch] = &s->fixed_coef_buffer[AC3_MAX_COEFS * (blk * s->channels + ch)];
+        }
+    } else {
+        for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
+            AC3Block *block = &s->blocks[blk];
+            FF_ALLOCZ_OR_GOTO(avctx, block->fixed_coef, s->channels *
+                              sizeof(*block->fixed_coef), alloc_fail);
+            for (ch = 0; ch < s->channels; ch++)
+                block->fixed_coef[ch] = (int32_t *)block->mdct_coef[ch];
         }
     }
 
@@ -1679,6 +1839,8 @@ static av_cold int ac3_encode_init(AVCodecContext *avctx)
 
     set_bandwidth(s);
 
+    rematrixing_init(s);
+
     exponent_init(s);
 
     bit_alloc_init(s);