Reduced memory usage due to adjusting allocated size based on number of 
channels. Also aligns arrays for future SIMD optimization.
---
 libavcodec/ac3enc.c |  242 +++++++++++++++++++++++++++------------------------
 1 files changed, 129 insertions(+), 113 deletions(-)

diff --git libavcodec/ac3enc.c libavcodec/ac3enc.c
index 38a26b2..994c316 100644
--- libavcodec/ac3enc.c
+++ libavcodec/ac3enc.c
@@ -38,6 +38,12 @@
 
 typedef struct AC3Block {
     uint8_t **bap;                          ///< bap for each channel in this block
+    int32_t **mdct_coef;                    ///< MDCT coefficients for each channel in this block
+    uint8_t **exp;                          ///< exponents for each channel in this block
+    int16_t **psd;                          ///< PSD for each channel in this block
+    int16_t **mask;                         ///< masking curve for each channel in this block
+    uint8_t exp_strategy[AC3_MAX_CHANNELS]; ///< exponent strategies for this block
+    int8_t  exp_shift[AC3_MAX_CHANNELS];    ///< exponent shift for this block
 } AC3Block;
 
 typedef struct AC3EncodeContext {
@@ -90,6 +96,10 @@ typedef struct AC3EncodeContext {
     int16_t *planar_samples[AC3_MAX_CHANNELS];  ///< full input frame, deinterleaved
     uint8_t *bap_buffer;
     uint8_t *bap1_buffer;
+    int32_t *mdct_coef_buffer;
+    uint8_t *exp_buffer;
+    int16_t *psd_buffer;
+    int16_t *mask_buffer;
 } AC3EncodeContext;
 
 /**
@@ -285,9 +295,7 @@ static void lshift_tab(int16_t *tab, int n, int lshift)
     }
 }
 
-static void apply_mdct(AC3EncodeContext *s,
-                       int32_t mdct_coef[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS],
-                       int8_t exp_shift[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS])
+static void apply_mdct(AC3EncodeContext *s)
 {
     int blk, ch, i, v;
 
@@ -307,11 +315,11 @@ static void apply_mdct(AC3EncodeContext *s,
             /* Normalize the samples to use the maximum available precision */
             v = 14 - log2_tab(s->windowed_samples, AC3_BLOCK_SIZE*2);
             v = FFMAX(0, v);
-            exp_shift[blk][ch] = v - 9;
+            s->blocks[blk].exp_shift[ch] = v - 9;
             lshift_tab(s->windowed_samples, AC3_BLOCK_SIZE*2, v);
 
             /* do the MDCT */
-            mdct512(mdct_coef[blk][ch], s->windowed_samples);
+            mdct512(s->blocks[blk].mdct_coef[ch], s->windowed_samples);
         }
     }
 }
@@ -319,10 +327,7 @@ static void apply_mdct(AC3EncodeContext *s,
 /**
  * Calculate MDCT coefficient exponents.
  */
-static void calculate_exponents(AC3EncodeContext *s,
-                                int32_t mdct_coef[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS],
-                                uint8_t exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS],
-                                int8_t exp_shift[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS])
+static void calculate_exponents(AC3EncodeContext *s)
 {
     int ch, blk, i, v;
 
@@ -330,19 +335,20 @@ static void calculate_exponents(AC3EncodeContext *s,
         /* fixed mdct to the six sub blocks & exponent computation */
         for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
             /* compute "exponents". We take into account the normalization there */
+            AC3Block *block = &s->blocks[blk];
             for (i = 0; i < AC3_MAX_COEFS; i++) {
                 int e;
-                v = abs(mdct_coef[blk][ch][i]);
+                v = abs(block->mdct_coef[ch][i]);
                 if (v == 0)
                     e = 24;
                 else {
-                    e = 23 - av_log2(v) + exp_shift[blk][ch];
+                    e = 23 - av_log2(v) + block->exp_shift[ch];
                     if (e >= 24) {
                         e = 24;
-                        mdct_coef[blk][ch][i] = 0;
+                        block->mdct_coef[ch][i] = 0;
                     }
                 }
-                exp[blk][ch][i] = e;
+                block->exp[ch][i] = e;
             }
         }
     }
@@ -404,9 +410,7 @@ static void compute_exp_strategy_ch(uint8_t exp_strategy[AC3_MAX_BLOCKS],
     }
 }
 
-static void compute_exp_strategy(AC3EncodeContext *s,
-                                 uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS],
-                                 uint8_t exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS])
+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];
@@ -414,21 +418,21 @@ static void compute_exp_strategy(AC3EncodeContext *s,
 
     for (ch = 0; ch < s->fbw_channels; ch++) {
         for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
-            exp1[ch][blk] = exp[blk][ch];
-            exp_str1[ch][blk] = exp_strategy[blk][ch];
+            exp1[ch][blk]     = s->blocks[blk].exp[ch];
+            exp_str1[ch][blk] = s->blocks[blk].exp_strategy[ch];
         }
         compute_exp_strategy_ch(exp_str1[ch], exp1[ch]);
         for (blk = 0; blk < AC3_MAX_BLOCKS; blk++)
-            exp_strategy[blk][ch] = exp_str1[ch][blk];
+            s->blocks[blk].exp_strategy[ch] = exp_str1[ch][blk];
     }
     if (s->lfe_on) {
         ch = s->lfe_ch;
-        exp_strategy[0][ch] = EXP_D15;
-        exp_strategy[1][ch] =
-        exp_strategy[2][ch] =
-        exp_strategy[3][ch] =
-        exp_strategy[4][ch] =
-        exp_strategy[5][ch] = EXP_REUSE;
+        s->blocks[0].exp_strategy[ch] = EXP_D15;
+        s->blocks[1].exp_strategy[ch] =
+        s->blocks[2].exp_strategy[ch] =
+        s->blocks[3].exp_strategy[ch] =
+        s->blocks[4].exp_strategy[ch] =
+        s->blocks[5].exp_strategy[ch] = EXP_REUSE;
     }
 }
 
@@ -512,45 +516,46 @@ static int encode_exp_blk_ch(uint8_t *exp, int nb_exps, int exp_strategy)
  * Compute the exponents as the decoder will see them.
  * The EXP_REUSE case must be handled carefully : we select the min of the exponents.
  */
-static void encode_exponents(AC3EncodeContext *s,
-                             uint8_t exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS],
-                             uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS])
+static void encode_exponents(AC3EncodeContext *s)
 {
+    AC3Block *block;
     int ch, blk;
 
     s->exp_bits = 0;
     for (ch = 0; ch < s->channels; ch++) {
         blk = 0;
+        block = &s->blocks[blk];
         while (blk < AC3_MAX_BLOCKS) {
+            AC3Block *block2;
+            AC3Block *block1 = block + 1;
             int blk2;
             int blk1 = blk + 1;
-            while (blk1 < AC3_MAX_BLOCKS && exp_strategy[blk1][ch] == EXP_REUSE) {
-                exponent_min(exp[blk][ch], exp[blk1][ch], s->nb_coefs[ch]);
+            while (blk1 < AC3_MAX_BLOCKS && block1->exp_strategy[ch] == EXP_REUSE) {
+                exponent_min(block->exp[ch], block1->exp[ch], s->nb_coefs[ch]);
                 blk1++;
+                block1++;
             }
-            s->exp_bits += encode_exp_blk_ch(exp[blk][ch], s->nb_coefs[ch],
-                                             exp_strategy[blk][ch]);
+            s->exp_bits += encode_exp_blk_ch(block->exp[ch], s->nb_coefs[ch],
+                                             block->exp_strategy[ch]);
             /* copy encoded exponents for reuse case */
-            for (blk2 = blk+1; blk2 < blk1; blk2++) {
-                memcpy(exp[blk2][ch], exp[blk][ch],
+            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));
             }
             blk = blk1;
+            block = block1;
         }
     }
 }
 
-static void process_exponents(AC3EncodeContext *s,
-                              int32_t mdct_coef[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS],
-                              uint8_t exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS],
-                              uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS],
-                              int8_t exp_shift[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS])
+static void process_exponents(AC3EncodeContext *s)
 {
-    calculate_exponents(s, mdct_coef, exp, exp_shift);
+    calculate_exponents(s);
 
-    compute_exp_strategy(s, exp_strategy, exp);
+    compute_exp_strategy(s);
 
-    encode_exponents(s, exp, exp_strategy);
+    encode_exponents(s);
 }
 
 /* return the size in bits taken by the mantissa */
@@ -604,38 +609,33 @@ static int compute_mantissa_size(AC3EncodeContext *s, uint8_t *m, int nb_coefs)
 }
 
 
-static void bit_alloc_masking(AC3EncodeContext *s,
-                              uint8_t encoded_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS],
-                              uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS],
-                              int16_t psd[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS],
-                              int16_t mask[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][50])
+static void bit_alloc_masking(AC3EncodeContext *s)
 {
     int blk, ch;
-    int16_t band_psd[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][50];
+    int16_t band_psd[50];
 
     for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
+        AC3Block *block = &s->blocks[blk];
         for (ch = 0; ch < s->channels; ch++) {
-            if (exp_strategy[blk][ch] == EXP_REUSE) {
-                memcpy(psd[blk][ch], psd[blk-1][ch], AC3_MAX_COEFS*sizeof(int16_t));
-                memcpy(mask[blk][ch], mask[blk-1][ch], 50*sizeof(int16_t));
+            if (block->exp_strategy[ch] == EXP_REUSE) {
+                memcpy(block->psd[ch], s->blocks[blk-1].psd[ch], AC3_MAX_COEFS*sizeof(int16_t));
+                memcpy(block->mask[ch], s->blocks[blk-1].mask[ch], 64*sizeof(int16_t));
             } else {
-                ff_ac3_bit_alloc_calc_psd(encoded_exp[blk][ch], 0,
+                ff_ac3_bit_alloc_calc_psd(block->exp[ch], 0,
                                           s->nb_coefs[ch],
-                                          psd[blk][ch], band_psd[blk][ch]);
-                ff_ac3_bit_alloc_calc_mask(&s->bit_alloc, band_psd[blk][ch],
+                                          block->psd[ch], band_psd);
+                ff_ac3_bit_alloc_calc_mask(&s->bit_alloc, band_psd,
                                            0, s->nb_coefs[ch],
                                            ff_ac3_fast_gain_tab[s->fast_gain_code[ch]],
                                            ch == s->lfe_ch,
                                            DBA_NONE, 0, NULL, NULL, NULL,
-                                           mask[blk][ch]);
+                                           block->mask[ch]);
             }
         }
     }
 }
 
 static int bit_alloc(AC3EncodeContext *s,
-                     int16_t mask[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][50],
-                     int16_t psd[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS],
                      int snr_offset)
 {
     int blk, ch;
@@ -651,7 +651,7 @@ static int bit_alloc(AC3EncodeContext *s,
         s->mant4_cnt = 0;
         for (ch = 0; ch < s->channels; ch++) {
             block->bap[ch] = &s->bap_buffer[AC3_MAX_COEFS * (blk * s->channels + ch)];
-            ff_ac3_bit_alloc_calc_bap(mask[blk][ch], psd[blk][ch], 0,
+            ff_ac3_bit_alloc_calc_bap(block->mask[ch], block->psd[ch], 0,
                                       s->nb_coefs[ch], snr_offset,
                                       s->bit_alloc.floor, ff_ac3_bap_tab,
                                       block->bap[ch]);
@@ -739,15 +739,15 @@ static void count_frame_bits_fixed(AC3EncodeContext *s)
  * Bits based on fixed parameters have already been counted, so now we just
  * have to add the bits based on parameters that change during encoding.
  */
-static void count_frame_bits(AC3EncodeContext *s,
-                             uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS])
+static void count_frame_bits(AC3EncodeContext *s)
 {
     int blk, ch;
     int frame_bits = 0;
 
     for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
+        uint8_t *exp_strategy = s->blocks[blk].exp_strategy;
         for (ch = 0; ch < s->fbw_channels; ch++) {
-            if (exp_strategy[blk][ch] != EXP_REUSE)
+            if (exp_strategy[ch] != EXP_REUSE)
                 frame_bits += 6 + 2; /* chbwcod[6], gainrng[2] */
         }
     }
@@ -765,9 +765,7 @@ static void reset_block_bap(AC3EncodeContext *s)
             s->blocks[blk].bap[ch] = &s->bap_buffer[AC3_MAX_COEFS * (blk * s->channels + ch)];
 }
 
-static int cbr_bit_alloc(AC3EncodeContext *s,
-                         int16_t mask[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][50],
-                         int16_t psd[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS])
+static int cbr_bit_alloc(AC3EncodeContext *s)
 {
     int ch;
     int snr_offset, snr_incr, bits_left;
@@ -779,7 +777,7 @@ static int cbr_bit_alloc(AC3EncodeContext *s,
         snr_offset = 1023;
 
     while (snr_offset >= 0 &&
-           bit_alloc(s, mask, psd, snr_offset) > bits_left)
+           bit_alloc(s, snr_offset) > bits_left)
         snr_offset -= 64;
     if (snr_offset < 0) {
         return -1;
@@ -788,7 +786,7 @@ static int cbr_bit_alloc(AC3EncodeContext *s,
     FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);
     for (snr_incr = 64; snr_incr > 0; snr_incr >>= 2) {
         while (snr_offset + snr_incr <= 1023 &&
-               bit_alloc(s, mask, psd, snr_offset + snr_incr) <= bits_left) {
+               bit_alloc(s, snr_offset + snr_incr) <= bits_left) {
             snr_offset += snr_incr;
             FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);
         }
@@ -803,22 +801,17 @@ static int cbr_bit_alloc(AC3EncodeContext *s,
     return 0;
 }
 
-static int compute_bit_allocation(AC3EncodeContext *s,
-                                  uint8_t encoded_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS],
-                                  uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS])
+static int compute_bit_allocation(AC3EncodeContext *s)
 {
-    int16_t psd[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS];
-    int16_t mask[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][50];
-
     /* For now bit alloc params do not change and they have already been set at
        init. When this changes, uncomment the line below. */
     //set_bit_alloc_params(s);
 
-    count_frame_bits(s, exp_strategy);
+    count_frame_bits(s);
 
-    bit_alloc_masking(s, encoded_exp, exp_strategy, psd, mask);
+    bit_alloc_masking(s);
 
-    return cbr_bit_alloc(s, mask, psd);
+    return cbr_bit_alloc(s);
 }
 
 static av_cold int set_channel_info(AC3EncodeContext *s, int channels,
@@ -1015,12 +1008,36 @@ static av_cold int AC3_encode_init(AVCodecContext *avctx)
                      AC3_MAX_COEFS * sizeof(*s->bap_buffer),  alloc_fail);
     FF_ALLOC_OR_GOTO(avctx, s->bap1_buffer, AC3_MAX_BLOCKS * s->channels *
                      AC3_MAX_COEFS * sizeof(*s->bap1_buffer), alloc_fail);
+    FF_ALLOC_OR_GOTO(avctx, s->mdct_coef_buffer, AC3_MAX_BLOCKS * s->channels *
+                     AC3_MAX_COEFS * sizeof(*s->mdct_coef_buffer), alloc_fail);
+    FF_ALLOC_OR_GOTO(avctx, s->exp_buffer, AC3_MAX_BLOCKS * s->channels *
+                     AC3_MAX_COEFS * sizeof(*s->exp_buffer), alloc_fail);
+    FF_ALLOC_OR_GOTO(avctx, s->psd_buffer, AC3_MAX_BLOCKS * s->channels *
+                     AC3_MAX_COEFS * sizeof(*s->psd_buffer), alloc_fail);
+    FF_ALLOC_OR_GOTO(avctx, s->mask_buffer, AC3_MAX_BLOCKS * s->channels *
+                     64 * sizeof(*s->mask_buffer), alloc_fail);
     for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
         AC3Block *block = &s->blocks[blk];
         FF_ALLOCZ_OR_GOTO(avctx, block->bap, s->channels * sizeof(*block->bap),
                           alloc_fail);
+        FF_ALLOCZ_OR_GOTO(avctx, block->mdct_coef, s->channels * sizeof(*block->mdct_coef),
+                          alloc_fail);
+        FF_ALLOCZ_OR_GOTO(avctx, block->exp, s->channels * sizeof(*block->exp),
+                          alloc_fail);
+        FF_ALLOCZ_OR_GOTO(avctx, block->psd, s->channels * sizeof(*block->psd),
+                          alloc_fail);
+        FF_ALLOCZ_OR_GOTO(avctx, block->mask, s->channels * sizeof(*block->mask),
+                          alloc_fail);
     }
     reset_block_bap(s);
+    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
+        for (ch = 0; ch < s->channels; ch++) {
+            s->blocks[blk].mdct_coef[ch] = &s->mdct_coef_buffer[AC3_MAX_COEFS * (blk * s->channels + ch)];
+            s->blocks[blk].exp[ch]       = &s->exp_buffer      [AC3_MAX_COEFS * (blk * s->channels + ch)];
+            s->blocks[blk].psd[ch]       = &s->psd_buffer      [AC3_MAX_COEFS * (blk * s->channels + ch)];
+            s->blocks[blk].mask[ch]      = &s->mask_buffer     [64            * (blk * s->channels + ch)];
+        }
+    }
 
     avctx->coded_frame = avcodec_alloc_frame();
     avctx->coded_frame->key_frame= 1;
@@ -1100,12 +1117,9 @@ static inline int asym_quant(int c, int e, int qbits)
 /* Output one audio block. There are AC3_MAX_BLOCKS audio blocks in one AC-3
    frame */
 static void output_audio_block(AC3EncodeContext *s,
-                               uint8_t exp_strategy[AC3_MAX_CHANNELS],
-                               uint8_t encoded_exp[AC3_MAX_CHANNELS][AC3_MAX_COEFS],
-                               int32_t mdct_coefs[AC3_MAX_CHANNELS][AC3_MAX_COEFS],
-                               int8_t exp_shift[AC3_MAX_CHANNELS],
                                int block_num)
 {
+    AC3Block *block = &s->blocks[block_num];
     int ch, nb_groups, group_size, i, baie, rbnd;
     uint8_t *p;
     uint16_t qmant[AC3_MAX_CHANNELS][AC3_MAX_COEFS];
@@ -1144,23 +1158,23 @@ static void output_audio_block(AC3EncodeContext *s,
 
     /* exponent strategy */
     for (ch = 0; ch < s->fbw_channels; ch++)
-        put_bits(&s->pb, 2, exp_strategy[ch]);
+        put_bits(&s->pb, 2, block->exp_strategy[ch]);
 
     if (s->lfe_on)
-        put_bits(&s->pb, 1, exp_strategy[s->lfe_ch]);
+        put_bits(&s->pb, 1, block->exp_strategy[s->lfe_ch]);
 
     for (ch = 0; ch < s->fbw_channels; ch++) {
-        if (exp_strategy[ch] != EXP_REUSE)
+        if (block->exp_strategy[ch] != EXP_REUSE)
             put_bits(&s->pb, 6, s->bandwidth_code[ch]);
     }
 
     /* exponents */
     for (ch = 0; ch < s->channels; ch++) {
-        if (exp_strategy[ch] == EXP_REUSE)
+        if (block->exp_strategy[ch] == EXP_REUSE)
             continue;
-        group_size = exp_strategy[ch] + (exp_strategy[ch] == EXP_D45);
-        nb_groups  = exponent_group_tab[exp_strategy[ch]-1][s->nb_coefs[ch]];
-        p = encoded_exp[ch];
+        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]];
+        p = block->exp[ch];
 
         /* first exponent */
         exp1 = *p++;
@@ -1227,9 +1241,9 @@ static void output_audio_block(AC3EncodeContext *s,
         int b, c, e, v;
 
         for (i = 0; i < s->nb_coefs[ch]; i++) {
-            c = mdct_coefs[ch][i];
-            e = encoded_exp[ch][i] - exp_shift[ch];
-            b = s->blocks[block_num].bap[ch][i];
+            c = block->mdct_coef[ch][i];
+            e = block->exp[ch][i] - block->exp_shift[ch];
+            b = block->bap[ch][i];
             switch(b) {
             case 0:
                 v = 0;
@@ -1315,7 +1329,7 @@ static void output_audio_block(AC3EncodeContext *s,
 
         for (i = 0; i < s->nb_coefs[ch]; i++) {
             q = qmant[ch][i];
-            b = s->blocks[block_num].bap[ch][i];
+            b = block->bap[ch][i];
             switch(b) {
             case  0:                                         break;
             case  1: if (q != 128) put_bits(&s->pb,  5,  q); break;
@@ -1398,23 +1412,22 @@ static void adjust_frame_size(AC3EncodeContext *s)
  * strategy in 1 channel of 1 block.
  * @return non-zero if downgrade was successful
  */
-static int downgrade_exponents(AC3EncodeContext *s,
-                               uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS])
+static int downgrade_exponents(AC3EncodeContext *s)
 {
     int ch, blk;
 
     for (ch = 0; ch < s->channels; ch++) {
         for (blk = AC3_MAX_BLOCKS-1; blk >= 0; blk--) {
-            if (exp_strategy[blk][ch] == EXP_D15) {
-                exp_strategy[blk][ch] = EXP_D25;
+            if (s->blocks[blk].exp_strategy[ch] == EXP_D15) {
+                s->blocks[blk].exp_strategy[ch] = EXP_D25;
                 return 1;
             }
         }
     }
     for (ch = 0; ch < s->channels; ch++) {
         for (blk = AC3_MAX_BLOCKS-1; blk >= 0; blk--) {
-            if (exp_strategy[blk][ch] == EXP_D25) {
-                exp_strategy[blk][ch] = EXP_D45;
+            if (s->blocks[blk].exp_strategy[ch] == EXP_D25) {
+                s->blocks[blk].exp_strategy[ch] = EXP_D45;
                 return 1;
             }
         }
@@ -1423,8 +1436,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 (exp_strategy[blk][ch] == EXP_D45) {
-                exp_strategy[blk][ch] = EXP_REUSE;
+            if (s->blocks[blk].exp_strategy[ch] == EXP_D45) {
+                s->blocks[blk].exp_strategy[ch] = EXP_REUSE;
                 return 1;
             }
         }
@@ -1457,34 +1470,30 @@ static int AC3_encode_frame(AVCodecContext *avctx,
 {
     AC3EncodeContext *s = avctx->priv_data;
     int blk;
-    int32_t mdct_coef[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS];
-    uint8_t exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS];
-    uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS];
-    int8_t exp_shift[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS];
     int err;
 
     adjust_frame_size(s);
 
     deinterleave_samples(s, data);
 
-    apply_mdct(s, mdct_coef, exp_shift);
+    apply_mdct(s);
 
-    process_exponents(s, mdct_coef, exp, exp_strategy, exp_shift);
+    process_exponents(s);
 
-    err = compute_bit_allocation(s, exp, exp_strategy);
+    err = compute_bit_allocation(s);
     while (err) {
         /* fallback 1: downgrade exponents */
-        if (downgrade_exponents(s, exp_strategy)) {
-            calculate_exponents(s, mdct_coef, exp, exp_shift);
-            encode_exponents(s, exp, exp_strategy);
-            err = compute_bit_allocation(s, exp, exp_strategy);
+        if (downgrade_exponents(s)) {
+            calculate_exponents(s);
+            encode_exponents(s);
+            err = compute_bit_allocation(s);
             continue;
         }
 
         /* fallback 2: reduce bandwidth code down to 0 */
         if (reduce_bandwidth(s, 0)) {
-            process_exponents(s, mdct_coef, exp, exp_strategy, exp_shift);
-            err = compute_bit_allocation(s, exp, exp_strategy);
+            process_exponents(s);
+            err = compute_bit_allocation(s);
             continue;
         }
 
@@ -1495,8 +1504,7 @@ static int AC3_encode_frame(AVCodecContext *avctx,
     output_frame_header(s, frame);
 
     for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
-        output_audio_block(s, exp_strategy[blk], exp[blk],
-                           mdct_coef[blk], exp_shift[blk], blk);
+        output_audio_block(s, blk);
     }
     output_frame_end(s);
 
@@ -1514,11 +1522,19 @@ static av_cold int AC3_encode_close(AVCodecContext *avctx)
     av_freep(&s->windowed_samples);
     av_freep(&s->bap_buffer);
     av_freep(&s->bap1_buffer);
+    av_freep(&s->mdct_coef_buffer);
+    av_freep(&s->exp_buffer);
+    av_freep(&s->psd_buffer);
+    av_freep(&s->mask_buffer);
     for (ch = 0; ch < s->channels; ch++) {
         av_freep(&s->planar_samples[ch]);
     }
     for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
         av_freep(&s->blocks[blk].bap);
+        av_freep(&s->blocks[blk].mdct_coef);
+        av_freep(&s->blocks[blk].exp);
+        av_freep(&s->blocks[blk].psd);
+        av_freep(&s->blocks[blk].mask);
     }
 
     av_freep(&avctx->coded_frame);
_______________________________________________
FFmpeg-soc mailing list
[email protected]
https://lists.mplayerhq.hu/mailman/listinfo/ffmpeg-soc

Reply via email to