Move exponent and frame bit counts to AC3EncodeContext. This avoids passing the
value around between many functions, making the code easier to read.
---
libavcodec/ac3enc.c | 63 ++++++++++++++++++++++++++-------------------------
1 files changed, 32 insertions(+), 31 deletions(-)
diff --git libavcodec/ac3enc.c libavcodec/ac3enc.c
index 46be2c7..5817a28 100644
--- libavcodec/ac3enc.c
+++ libavcodec/ac3enc.c
@@ -60,6 +60,8 @@ typedef struct AC3EncodeContext {
int nb_coefs[AC3_MAX_CHANNELS]; ///< number of frequency coefficients
/* bitrate allocation control */
+ 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)
int slow_decay_code; ///< slow decay code (sdcycod)
int fast_decay_code; ///< fast decay code (fdcycod)
@@ -472,12 +474,11 @@ static int encode_exp_blk_ch(uint8_t *exp, int nb_exps, int exp_strategy)
*/
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],
- int *frame_bits)
+ uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS])
{
int ch, blk;
- *frame_bits = 0;
+ s->exp_bits = 0;
for (ch = 0; ch < s->channels; ch++) {
blk = 0;
while (blk < AC3_MAX_BLOCKS) {
@@ -487,7 +488,7 @@ static void encode_exponents(AC3EncodeContext *s,
exponent_min(exp[blk][ch], exp[blk1][ch], s->nb_coefs[ch]);
blk1++;
}
- *frame_bits += encode_exp_blk_ch(exp[blk][ch], s->nb_coefs[ch],
+ s->exp_bits += encode_exp_blk_ch(exp[blk][ch], s->nb_coefs[ch],
exp_strategy[blk][ch]);
/* copy encoded exponents for reuse case */
for (blk2 = blk+1; blk2 < blk1; blk2++) {
@@ -503,8 +504,7 @@ 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],
- int *frame_bits)
+ int8_t exp_shift[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS])
{
int ch;
@@ -514,7 +514,7 @@ static void process_exponents(AC3EncodeContext *s,
compute_exp_strategy(exp_strategy, exp, ch, ch == s->lfe_ch);
}
- encode_exponents(s, exp, exp_strategy, frame_bits);
+ encode_exponents(s, exp, exp_strategy);
}
/* return the size in bits taken by the mantissa */
@@ -601,9 +601,10 @@ 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],
uint8_t bap[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS],
- int frame_bits, int snr_offset)
+ int snr_offset)
{
int blk, ch;
+ int mant_bits = 0;
snr_offset = (snr_offset - 240) << 2;
@@ -617,7 +618,7 @@ static int bit_alloc(AC3EncodeContext *s,
s->nb_coefs[ch], snr_offset,
s->bit_alloc.floor, ff_ac3_bap_tab,
bap[blk][ch]);
- frame_bits += compute_mantissa_size(s, bap[blk][ch], s->nb_coefs[ch]);
+ mant_bits += compute_mantissa_size(s, bap[blk][ch], s->nb_coefs[ch]);
}
}
#if 0
@@ -625,7 +626,7 @@ static int bit_alloc(AC3EncodeContext *s,
coarse_snr_offset, fine_snr_offset, frame_bits,
16 * s->frame_size - ((frame_bits + 7) & ~7));
#endif
- return 16 * s->frame_size - frame_bits;
+ return mant_bits;
}
static void set_bit_alloc_params(AC3EncodeContext *s)
@@ -640,15 +641,15 @@ static void set_bit_alloc_params(AC3EncodeContext *s)
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)
+static void count_frame_bits(AC3EncodeContext *s,
+ uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS])
{
int blk, ch;
+ int frame_bits;
static const int frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 };
/* header size */
- frame_bits += 65;
+ frame_bits = 65;
// if (s->channel_mode == 2)
// frame_bits += 2;
frame_bits += frame_bits_inc[s->channel_mode];
@@ -684,25 +685,26 @@ static int count_frame_bits(AC3EncodeContext *s,
/* CRC */
frame_bits += 16;
- return frame_bits;
+ s->frame_bits = frame_bits;
}
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)
+ uint8_t bap[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS])
{
int ch;
- int snr_offset, snr_incr;
+ int snr_offset, snr_incr, bits_left;
uint8_t bap1[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS];
+ bits_left = 16 * s->frame_size - (s->frame_bits + s->exp_bits);
+
snr_offset = s->coarse_snr_offset << 4;
if ((snr_offset | s->fine_snr_offset[0]) == 1023)
snr_offset = 1023;
while (snr_offset >= 0 &&
- bit_alloc(s, mask, psd, bap, frame_bits, snr_offset) < 0)
+ bit_alloc(s, mask, psd, bap, snr_offset) > bits_left)
snr_offset -= 64;
if (snr_offset < 0) {
return -1;
@@ -710,7 +712,7 @@ static int csnr_bit_alloc(AC3EncodeContext *s,
for (snr_incr = 64; snr_incr > 0; snr_incr >>= 2) {
while (snr_offset + snr_incr <= 1023 &&
- bit_alloc(s, mask, psd, bap1, frame_bits, snr_offset + snr_incr) >= 0) {
+ bit_alloc(s, mask, psd, bap1, snr_offset + snr_incr) <= bits_left) {
snr_offset += snr_incr;
memcpy(bap, bap1, sizeof(bap1));
}
@@ -726,8 +728,7 @@ static int csnr_bit_alloc(AC3EncodeContext *s,
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)
+ 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];
@@ -736,11 +737,11 @@ static int compute_bit_allocation(AC3EncodeContext *s,
init. When this changes, uncomment the line below. */
//set_bit_alloc_params(s);
- frame_bits = count_frame_bits(s, exp_strategy, frame_bits);
+ count_frame_bits(s, exp_strategy);
bit_alloc_masking(s, encoded_exp, exp_strategy, psd, mask);
- return csnr_bit_alloc(s, mask, psd, bap, frame_bits);
+ return csnr_bit_alloc(s, mask, psd, bap);
}
static av_cold int set_channel_info(AC3EncodeContext *s, int channels,
@@ -1371,7 +1372,7 @@ static int AC3_encode_frame(AVCodecContext *avctx,
uint8_t exp_strategy[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS];
uint8_t bap[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS][AC3_MAX_COEFS];
int8_t exp_shift[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS];
- int frame_bits, frame_size, err;
+ int frame_size, err;
adjust_frame_size(s);
@@ -1379,22 +1380,22 @@ static int AC3_encode_frame(AVCodecContext *avctx,
apply_mdct(s, mdct_coef, exp_shift);
- process_exponents(s, mdct_coef, exp, exp_strategy, exp_shift, &frame_bits);
+ process_exponents(s, mdct_coef, exp, exp_strategy, exp_shift);
- err = compute_bit_allocation(s, bap, exp, exp_strategy, frame_bits);
+ err = compute_bit_allocation(s, bap, exp, exp_strategy);
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, &frame_bits);
- err = compute_bit_allocation(s, bap, exp, exp_strategy, frame_bits);
+ encode_exponents(s, exp, exp_strategy);
+ err = compute_bit_allocation(s, bap, exp, exp_strategy);
continue;
}
/* fallback 2: reduce bandwidth code down to 0 */
if (reduce_bandwidth(s, 0)) {
- process_exponents(s, mdct_coef, exp, exp_strategy, exp_shift, &frame_bits);
- err = compute_bit_allocation(s, bap, exp, exp_strategy, frame_bits);
+ process_exponents(s, mdct_coef, exp, exp_strategy, exp_shift);
+ err = compute_bit_allocation(s, bap, exp, exp_strategy);
continue;
}
_______________________________________________
FFmpeg-soc mailing list
[email protected]
https://lists.mplayerhq.hu/mailman/listinfo/ffmpeg-soc