It is much simpler to understand and debug when the bit counting follows the
frame syntax. The speed difference is negligible.
---
libavcodec/ac3enc.c | 119 +++++++++++++++++++++++---------------------------
1 files changed, 55 insertions(+), 64 deletions(-)
diff --git a/libavcodec/ac3enc.c b/libavcodec/ac3enc.c
index 3846930..466fdc4 100644
--- a/libavcodec/ac3enc.c
+++ b/libavcodec/ac3enc.c
@@ -178,7 +178,6 @@ typedef struct AC3EncodeContext {
int coarse_snr_offset; ///< coarse SNR offsets (csnroffst)
int fast_gain_code[AC3_MAX_CHANNELS]; ///< fast gain codes (signal-to-mask ratio) (fgaincod)
int fine_snr_offset[AC3_MAX_CHANNELS]; ///< fine SNR offsets (fsnroffst)
- int frame_bits_fixed; ///< number of non-coefficient bits for fixed parameters
int frame_bits; ///< all frame bits except exponents and mantissas
int exponent_bits; ///< number of bits used for exponents
@@ -798,60 +797,6 @@ static void process_exponents(AC3EncodeContext *s)
/**
- * 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)
-{
- static const int frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 };
- int blk;
- int frame_bits;
-
- /* assumptions:
- * no dynamic range codes
- * no channel coupling
- * 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;
- frame_bits += frame_bits_inc[s->channel_mode];
-
- /* audio blocks */
- for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
- frame_bits += s->fbw_channels * 2 + 2; /* blksw * c, dithflag * c, dynrnge, cplstre */
- if (s->channel_mode == AC3_CHMODE_STEREO) {
- frame_bits++; /* rematstr */
- }
- frame_bits += 2 * s->fbw_channels; /* chexpstr[2] * c */
- if (s->lfe_on)
- frame_bits++; /* lfeexpstr */
- frame_bits++; /* baie */
- frame_bits++; /* snr */
- frame_bits += 2; /* delta / skip */
- }
- frame_bits++; /* cplinu for block 0 */
- /* bit alloc info */
- /* sdcycod[2], fdcycod[2], sgaincod[2], dbpbcod[2], floorcod[3] */
- /* csnroffset[6] */
- /* (fsnoffset[4] + fgaincod[4]) * c */
- frame_bits += 2*4 + 3 + 6 + s->channels * (4 + 3);
-
- /* auxdatae, crcrsv */
- frame_bits += 2;
-
- /* CRC */
- frame_bits += 16;
-
- s->frame_bits_fixed = frame_bits;
-}
-
-
-/**
* Initialize bit allocation.
* Set default parameter codes and calculate parameter values.
*/
@@ -879,22 +824,21 @@ static void bit_alloc_init(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];
-
- count_frame_bits_fixed(s);
}
/**
* Count the bits used to encode the frame, minus exponents and mantissas.
- * 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)
{
AC3EncOptions *opt = &s->options;
int blk, ch;
- int frame_bits = 0;
+ int frame_bits;
+ static const int frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 };
+ /* header */
+ frame_bits = 65 + frame_bits_inc[s->channel_mode];
if (opt->audio_production_info)
frame_bits += 7;
if (s->bitstream_id == 6) {
@@ -905,18 +849,65 @@ static void count_frame_bits(AC3EncodeContext *s)
}
for (blk = 0; blk < AC3_MAX_BLOCKS; blk++) {
+ AC3Block *block = &s->blocks[blk];
+
+ /* block switch flags */
+ frame_bits += s->fbw_channels;
+
+ /* dither flags */
+ frame_bits += s->fbw_channels;
+
+ /* dynamic range */
+ frame_bits++;
+
+ /* coupling strategy */
+ frame_bits++;
+ if (!blk) {
+ frame_bits++;
+ }
+
/* stereo rematrixing */
- if (s->channel_mode == AC3_CHMODE_STEREO &&
- s->blocks[blk].new_rematrixing_strategy) {
- frame_bits += s->num_rematrixing_bands;
+ if (s->channel_mode == AC3_CHMODE_STEREO) {
+ frame_bits++;
+ if (block->new_rematrixing_strategy)
+ frame_bits += s->num_rematrixing_bands;
}
+ /* bandwidth codes & gain range */
for (ch = 0; ch < s->fbw_channels; ch++) {
if (s->exp_strategy[ch][blk] != EXP_REUSE)
frame_bits += 6 + 2; /* chbwcod[6], gainrng[2] */
}
+
+ /* exponent strategy */
+ frame_bits += 2 * s->fbw_channels;
+ if (s->lfe_on)
+ frame_bits++;
+
+ /* bit alloc params */
+ frame_bits++;
+ if (!blk)
+ frame_bits += 2 + 2 + 2 + 2 + 3;
+
+ /* snr offsets and fast gain codes */
+ frame_bits++;
+ if (!blk)
+ frame_bits += 6 + s->channels * (4 + 3);
+
+ /* delta bit allocation */
+ frame_bits++;
+
+ /* skipped data */
+ frame_bits++;
}
- s->frame_bits = s->frame_bits_fixed + frame_bits;
+
+ /* auxiliary data */
+ frame_bits++;
+
+ /* CRC */
+ frame_bits += 1 + 16;
+
+ s->frame_bits = frame_bits;
}
_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel