Split compute_bit_allocation() into 4 separate functions. Also, initialize 
default bit alloc params in AC3_encode_init(). This will allow for other types 
of bit allocation in the future.
---
 libavcodec/ac3enc.c |   86 +++++++++++++++++++++++++++++----------------------
 1 files changed, 49 insertions(+), 37 deletions(-)

diff --git libavcodec/ac3enc.c libavcodec/ac3enc.c
index 26058cf..8346f92 100644
--- libavcodec/ac3enc.c
+++ libavcodec/ac3enc.c
@@ -628,27 +628,9 @@ static int bit_alloc(AC3EncodeContext *s,
     return 16 * s->frame_size - frame_bits;
 }
 
-static int compute_bit_allocation(AC3EncodeContext *s,
-                                  uint8_t bap[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS],
-                                  uint8_t encoded_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS],
-                                  uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS],
-                                  int frame_bits)
+static void set_bit_alloc_params(AC3EncodeContext *s)
 {
-    int blk, ch;
-    int snr_offset, snr_incr;
-    uint8_t bap1[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS];
-    int16_t psd[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS];
-    int16_t mask[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][50];
-    static const int frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 };
-
-    /* init default parameters */
-    s->slow_decay_code = 2;
-    s->fast_decay_code = 1;
-    s->slow_gain_code = 1;
-    s->db_per_bit_code = 2;
-    s->floor_code = 4;
-    for (ch = 0; ch < s->channels; ch++)
-        s->fast_gain_code[ch] = 4;
+    /* TODO: modify bit allocation parameters */
 
     /* compute real values */
     s->bit_alloc.slow_decay = ff_ac3_slow_decay_tab[s->slow_decay_code] >> s->bit_alloc.sr_shift;
@@ -656,6 +638,14 @@ static int compute_bit_allocation(AC3EncodeContext *s,
     s->bit_alloc.slow_gain  = ff_ac3_slow_gain_tab[s->slow_gain_code];
     s->bit_alloc.db_per_bit = ff_ac3_db_per_bit_tab[s->db_per_bit_code];
     s->bit_alloc.floor      = ff_ac3_floor_tab[s->floor_code];
+}
+
+static int count_frame_bits(AC3EncodeContext *s,
+                            uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS],
+                            int frame_bits)
+{
+    int blk, ch;
+    static const int frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 };
 
     /* header size */
     frame_bits += 65;
@@ -694,11 +684,18 @@ static int compute_bit_allocation(AC3EncodeContext *s,
     /* CRC */
     frame_bits += 16;
 
-    /* calculate psd and masking curve before doing bit allocation */
-    bit_alloc_masking(s, encoded_exp, exp_strategy, psd, mask);
+    return frame_bits;
+}
 
-    /* now the big work begins : do the bit allocation. Modify the snr
-       offset until we can pack everything in the requested frame size */
+static int csnr_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],
+                          uint8_t bap[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS],
+                          int frame_bits)
+{
+    int ch;
+    int snr_offset, snr_incr;
+    uint8_t bap1[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS];
 
     snr_offset = s->coarse_snr_offset << 4;
     if ((snr_offset | s->fine_snr_offset[0]) == 1023)
@@ -722,22 +719,28 @@ static int compute_bit_allocation(AC3EncodeContext *s,
     s->coarse_snr_offset = snr_offset >> 4;
     for(ch=0;ch<s->channels;ch++)
         s->fine_snr_offset[ch] = snr_offset & 0xF;
-#ifdef DEBUG_BITALLOC
-    {
-        int i;
-        for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
-            for (ch = 0; ch < s->channels; ch++) {
-                dprintf(s->avctx, "bap[blk=%d][ch=%d]=", blk, ch);
-                for (i = 0; i < s->nb_coefs[ch]; i++)
-                    dprintf(s->avctx, "%d ",bap[blk][ch][i]);
-                dprintf(s->avctx, "\n");
-            }
-        }
-    }
-#endif
+
     return 0;
 }
 
+static int compute_bit_allocation(AC3EncodeContext *s,
+                                  uint8_t bap[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS],
+                                  uint8_t encoded_exp[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS],
+                                  uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS],
+                                  int frame_bits)
+{
+    int16_t psd[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS];
+    int16_t mask[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][50];
+
+    set_bit_alloc_params(s);
+
+    frame_bits = count_frame_bits(s, exp_strategy, frame_bits);
+
+    bit_alloc_masking(s, encoded_exp, exp_strategy, psd, mask);
+
+    return csnr_bit_alloc(s, mask, psd, bap, frame_bits);
+}
+
 static av_cold int set_channel_info(AC3EncodeContext *s, int channels,
                                     int64_t *channel_layout)
 {
@@ -857,6 +860,15 @@ static av_cold int AC3_encode_init(AVCodecContext *avctx)
     /* initial snr offset */
     s->coarse_snr_offset = 40;
 
+    /* default bit allocation parameters */
+    s->slow_decay_code = 2;
+    s->fast_decay_code = 1;
+    s->slow_gain_code  = 1;
+    s->db_per_bit_code = 2;
+    s->floor_code      = 4;
+    for (ch = 0; ch < s->channels; ch++)
+        s->fast_gain_code[ch] = 4;
+
     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