From 160d85f5b058a1a3b9b2eeb19b878cf8f2a86161 Mon Sep 17 00:00:00 2001 From: Justin Ruggles Date: Tue, 14 Dec 2010 14:53:06 +0000 Subject: [PATCH] Split frame size adjustment into a separate function. Originally committed as revision 25992 to svn://svn.ffmpeg.org/ffmpeg/trunk --- libavcodec/ac3enc.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/libavcodec/ac3enc.c b/libavcodec/ac3enc.c index c26d868bbb..60ba60e25a 100644 --- a/libavcodec/ac3enc.c +++ b/libavcodec/ac3enc.c @@ -105,6 +105,22 @@ static int16_t xcos1[128]; static int16_t xsin1[128]; +/** + * Adjust the frame size to make the average bit rate match the target bit rate. + * This is only needed for 11025, 22050, and 44100 sample rates. + */ +static void adjust_frame_size(AC3EncodeContext *s) +{ + while (s->bits_written >= s->bit_rate && s->samples_written >= s->sample_rate) { + s->bits_written -= s->bit_rate; + s->samples_written -= s->sample_rate; + } + s->frame_size = s->frame_size_min + 2 * (s->bits_written * s->sample_rate < s->samples_written * s->bit_rate); + s->bits_written += s->frame_size * 8; + s->samples_written += AC3_FRAME_SIZE; +} + + /** * Deinterleave input samples. * Channels are reordered from FFmpeg's default order to AC-3 order. @@ -1296,21 +1312,15 @@ static int ac3_encode_frame(AVCodecContext *avctx, int8_t exp_shift[AC3_MAX_BLOCKS][AC3_MAX_CHANNELS]; int frame_bits; + if (s->bit_alloc.sr_code == 1) + adjust_frame_size(s); + deinterleave_input_samples(s, samples, planar_samples); apply_mdct(s, planar_samples, exp_shift, mdct_coef); frame_bits = process_exponents(s, mdct_coef, exp_shift, exp, exp_strategy, encoded_exp); - /* adjust for fractional frame sizes */ - while (s->bits_written >= s->bit_rate && s->samples_written >= s->sample_rate) { - s->bits_written -= s->bit_rate; - s->samples_written -= s->sample_rate; - } - s->frame_size = s->frame_size_min + 2 * (s->bits_written * s->sample_rate < s->samples_written * s->bit_rate); - s->bits_written += s->frame_size * 8; - s->samples_written += AC3_FRAME_SIZE; - compute_bit_allocation(s, bap, encoded_exp, exp_strategy, frame_bits); /* everything is known... let's output the frame */ output_frame_header(s, frame);