---
 libavcodec/ac3enc.c |  118 +++++++++++++++++++++++++-------------------------
 1 files changed, 59 insertions(+), 59 deletions(-)

diff --git libavcodec/ac3enc.c libavcodec/ac3enc.c
index f574246..f973f9a 100644
--- libavcodec/ac3enc.c
+++ libavcodec/ac3enc.c
@@ -76,6 +76,12 @@ typedef struct AC3EncodeContext {
     int16_t last_samples[AC3_MAX_CHANNELS][AC3_BLOCK_SIZE]; ///< last 256 samples from previous frame
 } AC3EncodeContext;
 
+/**
+ * LUT for number of exponent groups.
+ * exponent_group_tab[exponent strategy][number of coefficients]
+ */
+int exponent_group_tab[3][256];
+
 static int16_t costab[64];
 static int16_t sintab[64];
 static int16_t xcos1[128];
@@ -308,58 +314,56 @@ static void exponent_min(uint8_t exp[AC3_MAX_COEFS], uint8_t exp1[AC3_MAX_COEFS]
 
 /* update the exponents so that they are the ones the decoder will
    decode. Return the number of bits used to code the exponents */
-static int encode_exp(uint8_t encoded_exp[AC3_MAX_COEFS],
-                      uint8_t exp[AC3_MAX_COEFS], int nb_exps, int exp_strategy)
+static int encode_exp(uint8_t *exp, int nb_exps, int exp_strategy)
 {
-    int group_size, nb_groups, i, j, k, exp_min;
-    uint8_t exp1[AC3_MAX_COEFS];
+    int nb_groups, i, k;
+    int exp_min1, exp_min2;
 
-    switch(exp_strategy) {
-    case EXP_D15:
-        group_size = 1;
-        break;
+    nb_groups = exponent_group_tab[exp_strategy-1][nb_exps] * 3;
+
+    /* constraint for DC exponent */
+    exp[0] = FFMIN(exp[0], 15);
+
+    /* for each group, compute the minimum exponent */
+    switch (exp_strategy) {
     case EXP_D25:
-        group_size = 2;
+        for (i = 1, k = 1; i <= nb_groups; i++) {
+            exp[i] = FFMIN(exp[k], exp[k+1]);
+            k += 2;
+        }
         break;
-    default:
     case EXP_D45:
-        group_size = 4;
-        break;
-    }
-    nb_groups = ((nb_exps + (group_size * 3) - 4) / (3 * group_size)) * 3;
-
-    /* for each group, compute the minimum exponent */
-    exp1[0] = exp[0]; /* DC exponent is handled separately */
-    k = 1;
-    for (i = 1; i <= nb_groups; i++) {
-        exp_min = exp[k];
-        assert(exp_min >= 0 && exp_min <= 24);
-        for (j = 1; j < group_size; j++) {
-            if (exp[k+j] < exp_min)
-                exp_min = exp[k+j];
+        for (i = 1, k = 1; i <= nb_groups; i++) {
+            exp_min1 = FFMIN(exp[k  ], exp[k+1]);
+            exp_min2 = FFMIN(exp[k+2], exp[k+3]);
+            exp[i]   = FFMIN(exp_min1, exp_min2);
+            k += 4;
         }
-        exp1[i] = exp_min;
-        k += group_size;
+        break;
     }
 
-    /* constraint for DC exponent */
-    if (exp1[0] > 15)
-        exp1[0] = 15;
-
     /* Decrease the delta between each groups to within 2
      * so that they can be differentially encoded */
     for (i = 1; i <= nb_groups; i++)
-        exp1[i] = FFMIN(exp1[i], exp1[i-1] + 2);
-    for (i = nb_groups-1; i >= 0; i--)
-        exp1[i] = FFMIN(exp1[i], exp1[i+1] + 2);
+        exp[i] = FFMIN(exp[i], exp[i-1] + 2);
+    i--;
+    while(--i >= 0)
+        exp[i] = FFMIN(exp[i], exp[i+1] + 2);
 
     /* now we have the exponent values the decoder will see */
-    encoded_exp[0] = exp1[0];
-    k = 1;
-    for (i = 1; i <= nb_groups; i++) {
-        for (j = 0; j < group_size; j++)
-            encoded_exp[k+j] = exp1[i];
-        k += group_size;
+    switch (exp_strategy) {
+    case EXP_D25:
+        for (i = nb_groups, k = nb_groups*2; i > 0; i--) {
+            exp[k] = exp[k-1] = exp[i];
+            k -= 2;
+        }
+        break;
+    case EXP_D45:
+        for (i = nb_groups, k = nb_groups*4; i > 0; i--) {
+            exp[k] = exp[k-1] = exp[k-2] = exp[k-3] = exp[i];
+            k -= 4;
+        }
+        break;
     }
 
 #ifdef DEBUG
@@ -734,6 +738,15 @@ static av_cold int AC3_encode_init(AVCodecContext *avctx)
 
     mdct_init();
 
+    for (j = 0; j < 256; j++) {
+        exponent_group_tab[0][j] = (j - 1) /  3;
+        exponent_group_tab[1][j] = (j + 2) /  6;
+        exponent_group_tab[2][j] = (j + 8) / 12;
+    }
+    exponent_group_tab[0][7] =
+    exponent_group_tab[1][7] =
+    exponent_group_tab[2][7] = 2;
+
     avctx->coded_frame = avcodec_alloc_frame();
     avctx->coded_frame->key_frame= 1;
 
@@ -867,21 +880,10 @@ static void output_audio_block(AC3EncodeContext *s,
 
     /* exponents */
     for (ch = 0; ch < s->channels; ch++) {
-        switch(exp_strategy[ch]) {
-        case EXP_REUSE:
+        if (exp_strategy[ch] == EXP_REUSE)
             continue;
-        case EXP_D15:
-            group_size = 1;
-            break;
-        case EXP_D25:
-            group_size = 2;
-            break;
-        default:
-        case EXP_D45:
-            group_size = 4;
-            break;
-        }
-        nb_groups = (s->nb_coefs[ch] + (group_size * 3) - 4) / (3 * group_size);
+        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];
 
         /* first exponent */
@@ -1154,7 +1156,6 @@ static int AC3_encode_frame(AVCodecContext *avctx,
     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];
-    uint8_t encoded_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS];
     uint8_t bap[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS];
     int8_t exp_samples[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS];
     int frame_bits;
@@ -1223,12 +1224,11 @@ static int AC3_encode_frame(AVCodecContext *avctx,
                 exponent_min(exp[i][ch], exp[j][ch], s->nb_coefs[ch]);
                 j++;
             }
-            frame_bits += encode_exp(encoded_exp[i][ch],
-                                     exp[i][ch], s->nb_coefs[ch],
+            frame_bits += encode_exp(exp[i][ch], s->nb_coefs[ch],
                                      exp_strategy[i][ch]);
             /* copy encoded exponents for reuse case */
             for (k = i+1; k < j; k++) {
-                memcpy(encoded_exp[k][ch], encoded_exp[i][ch],
+                memcpy(exp[k][ch], exp[i][ch],
                        s->nb_coefs[ch] * sizeof(uint8_t));
             }
             i = j;
@@ -1244,12 +1244,12 @@ static int AC3_encode_frame(AVCodecContext *avctx,
     s->bits_written    += s->frame_size * 16;
     s->samples_written += AC3_FRAME_SIZE;
 
-    compute_bit_allocation(s, bap, encoded_exp, exp_strategy, frame_bits);
+    compute_bit_allocation(s, bap, exp, exp_strategy, frame_bits);
     /* everything is known... let's output the frame */
     output_frame_header(s, frame);
 
     for (i = 0; i < AC3_MAX_BLOCKS; i++) {
-        output_audio_block(s, exp_strategy[i], encoded_exp[i], bap[i],
+        output_audio_block(s, exp_strategy[i], exp[i], bap[i],
                            mdct_coef[i], exp_samples[i], i);
     }
     return output_frame_end(s);
_______________________________________________
FFmpeg-soc mailing list
[email protected]
https://lists.mplayerhq.hu/mailman/listinfo/ffmpeg-soc

Reply via email to