diff options
author | Michael Niedermayer <michaelni@gmx.at> | 2011-06-28 02:29:39 +0200 |
---|---|---|
committer | Michael Niedermayer <michaelni@gmx.at> | 2011-06-28 02:30:04 +0200 |
commit | 7b7c47c8f70e51d3835a6153c3e467e59acf8a8b (patch) | |
tree | ecb00d3a1688c44780d275b2b3209c355f548e0a /libavcodec/ac3enc.c | |
parent | 65eae2a7a2a14926c9f9683544e99982ba565f93 (diff) | |
parent | 8683c6a638f2e323d11b520c5e130b46b1eb1eda (diff) | |
download | ffmpeg-streaming-7b7c47c8f70e51d3835a6153c3e467e59acf8a8b.zip ffmpeg-streaming-7b7c47c8f70e51d3835a6153c3e467e59acf8a8b.tar.gz |
Merge remote-tracking branch 'qatar/master'
* qatar/master:
ac3enc: move ff_ac3_encode_frame() to ac3enc_template.c
ac3enc: merge log2_tab() into normalize_samples()
ac3enc: Remove bit allocation fallbacks.
Merged-by: Michael Niedermayer <michaelni@gmx.at>
Diffstat (limited to 'libavcodec/ac3enc.c')
-rw-r--r-- | libavcodec/ac3enc.c | 153 |
1 files changed, 10 insertions, 143 deletions
diff --git a/libavcodec/ac3enc.c b/libavcodec/ac3enc.c index 749967f..cd9f8d1 100644 --- a/libavcodec/ac3enc.c +++ b/libavcodec/ac3enc.c @@ -177,7 +177,7 @@ static const int8_t ac3_coupling_start_tab[6][3][19] = { * 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 or any E-AC-3. */ -static void adjust_frame_size(AC3EncodeContext *s) +void ff_ac3_adjust_frame_size(AC3EncodeContext *s) { while (s->bits_written >= s->bit_rate && s->samples_written >= s->sample_rate) { s->bits_written -= s->bit_rate; @@ -190,7 +190,7 @@ static void adjust_frame_size(AC3EncodeContext *s) } -static void compute_coupling_strategy(AC3EncodeContext *s) +void ff_ac3_compute_coupling_strategy(AC3EncodeContext *s) { int blk, ch; int got_cpl_snr; @@ -254,7 +254,7 @@ static void compute_coupling_strategy(AC3EncodeContext *s) /** * Apply stereo rematrixing to coefficients based on rematrixing flags. */ -static void apply_rematrixing(AC3EncodeContext *s) +void ff_ac3_apply_rematrixing(AC3EncodeContext *s) { int nb_coefs; int blk, bnd, i; @@ -570,7 +570,7 @@ static void group_exponents(AC3EncodeContext *s) * Extract exponents from MDCT coefficients, calculate exponent strategies, * and encode final exponents. */ -static void process_exponents(AC3EncodeContext *s) +void ff_ac3_process_exponents(AC3EncodeContext *s) { extract_exponents(s); @@ -1023,87 +1023,18 @@ static int cbr_bit_allocation(AC3EncodeContext *s) /** - * Downgrade exponent strategies to reduce the bits used by the exponents. - * This is a fallback for when bit allocation fails with the normal exponent - * strategies. Each time this function is run it only downgrades the - * strategy in 1 channel of 1 block. - * @return non-zero if downgrade was unsuccessful - */ -static int downgrade_exponents(AC3EncodeContext *s) -{ - int ch, blk; - - for (blk = AC3_MAX_BLOCKS-1; blk >= 0; blk--) { - for (ch = !s->blocks[blk].cpl_in_use; ch <= s->fbw_channels; ch++) { - if (s->exp_strategy[ch][blk] == EXP_D15) { - s->exp_strategy[ch][blk] = EXP_D25; - return 0; - } - } - } - for (blk = AC3_MAX_BLOCKS-1; blk >= 0; blk--) { - for (ch = !s->blocks[blk].cpl_in_use; ch <= s->fbw_channels; ch++) { - if (s->exp_strategy[ch][blk] == EXP_D25) { - s->exp_strategy[ch][blk] = EXP_D45; - return 0; - } - } - } - /* block 0 cannot reuse exponents, so only downgrade D45 to REUSE if - the block number > 0 */ - for (blk = AC3_MAX_BLOCKS-1; blk > 0; blk--) { - for (ch = !s->blocks[blk].cpl_in_use; ch <= s->fbw_channels; ch++) { - if (s->exp_strategy[ch][blk] > EXP_REUSE) { - s->exp_strategy[ch][blk] = EXP_REUSE; - return 0; - } - } - } - return -1; -} - - -/** * Perform bit allocation search. * Finds the SNR offset value that maximizes quality and fits in the specified * frame size. Output is the SNR offset and a set of bit allocation pointers * used to quantize the mantissas. */ -static int compute_bit_allocation(AC3EncodeContext *s) +int ff_ac3_compute_bit_allocation(AC3EncodeContext *s) { - int ret; - count_frame_bits(s); bit_alloc_masking(s); - ret = cbr_bit_allocation(s); - while (ret) { - /* fallback 1: disable channel coupling */ - if (s->cpl_on) { - s->cpl_on = 0; - compute_coupling_strategy(s); - s->compute_rematrixing_strategy(s); - apply_rematrixing(s); - process_exponents(s); - ret = compute_bit_allocation(s); - continue; - } - - /* fallback 2: downgrade exponents */ - if (!downgrade_exponents(s)) { - extract_exponents(s); - encode_exponents(s); - group_exponents(s); - ret = compute_bit_allocation(s); - continue; - } - - /* fallbacks were not enough... */ - break; - } - - return ret; + return cbr_bit_allocation(s); } @@ -1232,7 +1163,7 @@ static void quantize_mantissas_blk_ch(AC3Mant *s, int32_t *fixed_coef, /** * Quantize mantissas using coefficients, exponents, and bit allocation pointers. */ -static void quantize_mantissas(AC3EncodeContext *s) +void ff_ac3_quantize_mantissas(AC3EncodeContext *s) { int blk, ch, ch0=0, got_cpl; @@ -1591,7 +1522,7 @@ static void output_frame_end(AC3EncodeContext *s) /** * Write the frame to the output bitstream. */ -static void output_frame(AC3EncodeContext *s, unsigned char *frame) +void ff_ac3_output_frame(AC3EncodeContext *s, unsigned char *frame) { int blk; @@ -1760,7 +1691,7 @@ static void validate_mix_level(void *log_ctx, const char *opt_name, * Validate metadata options as set by AVOption system. * These values can optionally be changed per-frame. */ -static int validate_metadata(AVCodecContext *avctx) +int ff_ac3_validate_metadata(AVCodecContext *avctx) { AC3EncodeContext *s = avctx->priv_data; AC3EncOptions *opt = &s->options; @@ -1870,57 +1801,6 @@ static int validate_metadata(AVCodecContext *avctx) /** - * Encode a single AC-3 frame. - */ -int ff_ac3_encode_frame(AVCodecContext *avctx, unsigned char *frame, - int buf_size, void *data) -{ - AC3EncodeContext *s = avctx->priv_data; - const SampleType *samples = data; - int ret; - - if (!s->eac3 && s->options.allow_per_frame_metadata) { - ret = validate_metadata(avctx); - if (ret) - return ret; - } - - if (s->bit_alloc.sr_code == 1 || s->eac3) - adjust_frame_size(s); - - s->deinterleave_input_samples(s, samples); - - s->apply_mdct(s); - - s->scale_coefficients(s); - - s->cpl_on = s->cpl_enabled; - compute_coupling_strategy(s); - - if (s->cpl_on) - s->apply_channel_coupling(s); - - s->compute_rematrixing_strategy(s); - - apply_rematrixing(s); - - process_exponents(s); - - ret = compute_bit_allocation(s); - if (ret) { - av_log(avctx, AV_LOG_ERROR, "Bit allocation failed. Try increasing the bitrate.\n"); - return ret; - } - - quantize_mantissas(s); - - output_frame(s, frame); - - return s->frame_size; -} - - -/** * Finalize encoding and free any memory allocated by the encoder. */ av_cold int ff_ac3_encode_close(AVCodecContext *avctx) @@ -2116,7 +1996,7 @@ static av_cold int validate_options(AVCodecContext *avctx, AC3EncodeContext *s) } if (!s->eac3) { - ret = validate_metadata(avctx); + ret = ff_ac3_validate_metadata(avctx); if (ret) return ret; } @@ -2342,24 +2222,11 @@ av_cold int ff_ac3_encode_init(AVCodecContext *avctx) if (CONFIG_AC3_FIXED_ENCODER && s->fixed_point) { s->mdct_end = ff_ac3_fixed_mdct_end; s->mdct_init = ff_ac3_fixed_mdct_init; - s->apply_window = ff_ac3_fixed_apply_window; - s->normalize_samples = ff_ac3_fixed_normalize_samples; - s->scale_coefficients = ff_ac3_fixed_scale_coefficients; s->allocate_sample_buffers = ff_ac3_fixed_allocate_sample_buffers; - s->deinterleave_input_samples = ff_ac3_fixed_deinterleave_input_samples; - s->apply_mdct = ff_ac3_fixed_apply_mdct; - s->apply_channel_coupling = ff_ac3_fixed_apply_channel_coupling; - s->compute_rematrixing_strategy = ff_ac3_fixed_compute_rematrixing_strategy; } else if (CONFIG_AC3_ENCODER || CONFIG_EAC3_ENCODER) { s->mdct_end = ff_ac3_float_mdct_end; s->mdct_init = ff_ac3_float_mdct_init; - s->apply_window = ff_ac3_float_apply_window; - s->scale_coefficients = ff_ac3_float_scale_coefficients; s->allocate_sample_buffers = ff_ac3_float_allocate_sample_buffers; - s->deinterleave_input_samples = ff_ac3_float_deinterleave_input_samples; - s->apply_mdct = ff_ac3_float_apply_mdct; - s->apply_channel_coupling = ff_ac3_float_apply_channel_coupling; - s->compute_rematrixing_strategy = ff_ac3_float_compute_rematrixing_strategy; } if (CONFIG_EAC3_ENCODER && s->eac3) s->output_frame_header = ff_eac3_output_frame_header; |