diff --git a/libavcodec/ac3enc.c b/libavcodec/ac3enc.c index 6adc11f83c..296c156832 100644 --- a/libavcodec/ac3enc.c +++ b/libavcodec/ac3enc.c @@ -1294,13 +1294,45 @@ static av_cold int validate_options(AVCodecContext *avctx, AC3EncodeContext *s) } +/** + * Set bandwidth for all channels. + * The user can optionally supply a cutoff frequency. Otherwise an appropriate + * default value will be used. + */ +static av_cold void set_bandwidth(AC3EncodeContext *s, int cutoff) +{ + int ch, bw_code; + + if (cutoff) { + /* calculate bandwidth based on user-specified cutoff frequency */ + int fbw_coeffs; + cutoff = av_clip(cutoff, 1, s->sample_rate >> 1); + fbw_coeffs = cutoff * 2 * AC3_MAX_COEFS / s->sample_rate; + bw_code = av_clip((fbw_coeffs - 73) / 3, 0, 60); + } else { + /* use default bandwidth setting */ + /* XXX: should compute the bandwidth according to the frame + size, so that we avoid annoying high frequency artifacts */ + bw_code = 50; + } + + /* set number of coefficients for each channel */ + for (ch = 0; ch < s->fbw_channels; ch++) { + s->bandwidth_code[ch] = bw_code; + s->nb_coefs[ch] = bw_code * 3 + 73; + } + if (s->lfe_on) + s->nb_coefs[s->lfe_channel] = 7; /* LFE channel always has 7 coefs */ +} + + /** * Initialize the encoder. */ static av_cold int ac3_encode_init(AVCodecContext *avctx) { AC3EncodeContext *s = avctx->priv_data; - int ch, bw_code, ret; + int ret; avctx->frame_size = AC3_FRAME_SIZE; @@ -1318,25 +1350,7 @@ static av_cold int ac3_encode_init(AVCodecContext *avctx) s->samples_written = 0; s->frame_size = s->frame_size_min; - /* set bandwidth */ - if (avctx->cutoff) { - /* calculate bandwidth based on user-specified cutoff frequency */ - int cutoff = av_clip(avctx->cutoff, 1, s->sample_rate >> 1); - int fbw_coeffs = cutoff * 2 * AC3_MAX_COEFS / s->sample_rate; - bw_code = av_clip((fbw_coeffs - 73) / 3, 0, 60); - } else { - /* use default bandwidth setting */ - /* XXX: should compute the bandwidth according to the frame - size, so that we avoid annoying high frequency artifacts */ - bw_code = 50; - } - for (ch = 0; ch < s->fbw_channels; ch++) { - /* bandwidth for each channel */ - s->bandwidth_code[ch] = bw_code; - s->nb_coefs[ch] = bw_code * 3 + 73; - } - if (s->lfe_on) - s->nb_coefs[s->lfe_channel] = 7; /* LFE channel always has 7 coefs */ + set_bandwidth(s, avctx->cutoff); /* initial snr offset */ s->coarse_snr_offset = 40;