---
 libavcodec/ac3enc.c |   49 +++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 41 insertions(+), 8 deletions(-)

diff --git libavcodec/ac3enc.c libavcodec/ac3enc.c
index 68ba403..ce5b73a 100644
--- libavcodec/ac3enc.c
+++ libavcodec/ac3enc.c
@@ -68,6 +68,7 @@ typedef struct AC3EncodeContext {
     int nb_coefs[AC3_MAX_CHANNELS];         ///< number of frequency coefficients
 
     /* bitrate allocation control */
+    int frame_bits_fixed;                   ///< number of non-coefficient bits for fixed parameters
     int frame_bits;                         ///< number of non-coefficient bits
     int exp_bits;                           ///< number of bits used by exponents
     int slow_gain_code;                     ///< slow gain code                         (sgaincod)
@@ -653,13 +654,27 @@ static void set_bit_alloc_params(AC3EncodeContext *s)
     s->bit_alloc.floor      = ff_ac3_floor_tab[s->floor_code];
 }
 
-static void count_frame_bits(AC3EncodeContext *s,
-                             uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS])
+/**
+ * Count frame bits that are based solely on fixed parameters.
+ * This only has to be run once when the encoder is initialized.
+ */
+static void count_frame_bits_fixed(AC3EncodeContext *s)
 {
-    int blk, ch;
+    int blk;
     int frame_bits;
     static const int frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 };
 
+    /* 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
+     *   no skipped data
+     *   no auxilliary data
+     */
+
     /* header size */
     frame_bits = 65;
     // if (s->channel_mode == 2)
@@ -676,10 +691,6 @@ static void count_frame_bits(AC3EncodeContext *s,
         frame_bits += 2 * s->fbw_channels; /* chexpstr[2] * c */
         if (s->lfe_on)
             frame_bits++; /* lfeexpstr */
-        for (ch = 0; ch < s->fbw_channels; ch++) {
-            if (exp_strategy[blk][ch] != EXP_REUSE)
-                frame_bits += 6 + 2; /* chbwcod[6], gainrng[2] */
-        }
         frame_bits++; /* baie */
         frame_bits++; /* snr */
         frame_bits += 2; /* delta / skip */
@@ -697,7 +708,27 @@ static void count_frame_bits(AC3EncodeContext *s,
     /* CRC */
     frame_bits += 16;
 
-    s->frame_bits = frame_bits;
+    s->frame_bits_fixed = frame_bits;
+}
+
+/**
+ * Count frame bits.
+ * 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])
+{
+    int blk, ch;
+    int frame_bits = 0;
+
+    for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
+        for (ch = 0; ch < s->fbw_channels; ch++) {
+            if (exp_strategy[blk][ch] != EXP_REUSE)
+                frame_bits += 6 + 2; /* chbwcod[6], gainrng[2] */
+        }
+    }
+    s->frame_bits = s->frame_bits_fixed + frame_bits;
 }
 
 /**
@@ -935,6 +966,8 @@ static av_cold int AC3_encode_init(AVCodecContext *avctx)
     /* for now bit alloc params do not change so set them here to avoid unnecessary calls */
     set_bit_alloc_params(s);
 
+    count_frame_bits_fixed(s);
+
     mdct_init();
 
     for (j = 0; j < 256; j++) {
_______________________________________________
FFmpeg-soc mailing list
[email protected]
https://lists.mplayerhq.hu/mailman/listinfo/ffmpeg-soc

Reply via email to