static void bit_alloc_init(AC3EncodeContext *s)
{
    int ch;
    s->slow_decay_code = 2;
    s->fast_decay_code = 1;
    s->slow_gain_code  = 1;
    s->db_per_bit_code = s->eac3 ? 2 : 3;
    s->floor_code      = 7;
    for (ch = 0; ch <= s->channels; ch++)
        s->fast_gain_code[ch] = 4;
    s->coarse_snr_offset = 40;
    s->bit_alloc.slow_decay = ff_ac3_slow_decay_tab[s->slow_decay_code] >> s->bit_alloc.sr_shift;
    s->bit_alloc.fast_decay = ff_ac3_fast_decay_tab[s->fast_decay_code] >> s->bit_alloc.sr_shift;
    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];
    s->bit_alloc.cpl_fast_leak = 0;
    s->bit_alloc.cpl_slow_leak = 0;
    count_frame_bits_fixed(s);
}
['int ff_ac3_encode_init(AVCodecContext *avctx)\n{\n    AC3EncodeContext *s = avctx->priv_data;\n    int ret, frame_size_58;\n    s->avctx = avctx;\n    s->eac3 = avctx->codec_id == CODEC_ID_EAC3;\n    avctx->frame_size = AC3_FRAME_SIZE;\n    ff_ac3_common_init();\n    ret = validate_options(s);\n    if (ret)\n        return ret;\n    s->bitstream_mode = avctx->audio_service_type;\n    if (s->bitstream_mode == AV_AUDIO_SERVICE_TYPE_KARAOKE)\n        s->bitstream_mode = 0x7;\n    s->bits_written    = 0;\n    s->samples_written = 0;\n    frame_size_58 = (( s->frame_size    >> 2) + ( s->frame_size    >> 4)) << 1;\n    s->crc_inv[0] = pow_poly((CRC16_POLY >> 1), (8 * frame_size_58) - 16, CRC16_POLY);\n    if (s->bit_alloc.sr_code == 1) {\n        frame_size_58 = (((s->frame_size+2) >> 2) + ((s->frame_size+2) >> 4)) << 1;\n        s->crc_inv[1] = pow_poly((CRC16_POLY >> 1), (8 * frame_size_58) - 16, CRC16_POLY);\n    }\n    if (CONFIG_AC3_FIXED_ENCODER && s->fixed_point) {\n        s->mdct_end                     = ff_ac3_fixed_mdct_end;\n        s->mdct_init                    = ff_ac3_fixed_mdct_init;\n        s->allocate_sample_buffers      = ff_ac3_fixed_allocate_sample_buffers;\n    } else if (CONFIG_AC3_ENCODER || CONFIG_EAC3_ENCODER) {\n        s->mdct_end                     = ff_ac3_float_mdct_end;\n        s->mdct_init                    = ff_ac3_float_mdct_init;\n        s->allocate_sample_buffers      = ff_ac3_float_allocate_sample_buffers;\n    }\n    if (CONFIG_EAC3_ENCODER && s->eac3)\n        s->output_frame_header = ff_eac3_output_frame_header;\n    else\n        s->output_frame_header = ac3_output_frame_header;\n    set_bandwidth(s);\n    exponent_init(s);\n    bit_alloc_init(s);\n    ret = s->mdct_init(s);\n    if (ret)\n        goto init_fail;\n    ret = allocate_buffers(s);\n    if (ret)\n        goto init_fail;\n    avctx->coded_frame= avcodec_alloc_frame();\n    dsputil_init(&s->dsp, avctx);\n    ff_ac3dsp_init(&s->ac3dsp, avctx->flags & CODEC_FLAG_BITEXACT);\n    dprint_options(s);\n    return 0;\ninit_fail:\n    ff_ac3_encode_close(avctx);\n    return ret;\n}', 'static av_cold int validate_options(AC3EncodeContext *s)\n{\n    AVCodecContext *avctx = s->avctx;\n    int i, ret, max_sr;\n    if (!avctx->channel_layout) {\n        av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The "\n                                      "encoder will guess the layout, but it "\n                                      "might be incorrect.\\n");\n    }\n    ret = set_channel_info(s, avctx->channels, &avctx->channel_layout);\n    if (ret) {\n        av_log(avctx, AV_LOG_ERROR, "invalid channel layout\\n");\n        return ret;\n    }\n    max_sr = s->eac3 ? 2 : 8;\n    for (i = 0; i <= max_sr; i++) {\n        if ((ff_ac3_sample_rate_tab[i % 3] >> (i / 3)) == avctx->sample_rate)\n            break;\n    }\n    if (i > max_sr) {\n        av_log(avctx, AV_LOG_ERROR, "invalid sample rate\\n");\n        return AVERROR(EINVAL);\n    }\n    s->sample_rate        = avctx->sample_rate;\n    s->bit_alloc.sr_shift = i / 3;\n    s->bit_alloc.sr_code  = i % 3;\n    s->bitstream_id       = s->eac3 ? 16 : 8 + s->bit_alloc.sr_shift;\n    if (s->eac3) {\n        int max_br, min_br, wpf, min_br_dist, min_br_code;\n        max_br = 2048 * s->sample_rate / AC3_FRAME_SIZE * 16;\n        min_br = ((s->sample_rate + (AC3_FRAME_SIZE-1)) / AC3_FRAME_SIZE) * 16;\n        if (avctx->bit_rate < min_br || avctx->bit_rate > max_br) {\n            av_log(avctx, AV_LOG_ERROR, "invalid bit rate. must be %d to %d "\n                   "for this sample rate\\n", min_br, max_br);\n            return AVERROR(EINVAL);\n        }\n        wpf = (avctx->bit_rate / 16) * AC3_FRAME_SIZE / s->sample_rate;\n        av_assert1(wpf > 0 && wpf <= 2048);\n        min_br_code = -1;\n        min_br_dist = INT_MAX;\n        for (i = 0; i < 19; i++) {\n            int br_dist = abs(ff_ac3_bitrate_tab[i] * 1000 - avctx->bit_rate);\n            if (br_dist < min_br_dist) {\n                min_br_dist = br_dist;\n                min_br_code = i;\n            }\n        }\n        s->frame_size_code = min_br_code << 1;\n        while (wpf > 1 && wpf * s->sample_rate / AC3_FRAME_SIZE * 16 > avctx->bit_rate)\n            wpf--;\n        s->frame_size_min = 2 * wpf;\n    } else {\n        for (i = 0; i < 19; i++) {\n            if ((ff_ac3_bitrate_tab[i] >> s->bit_alloc.sr_shift)*1000 == avctx->bit_rate)\n                break;\n        }\n        if (i == 19) {\n            av_log(avctx, AV_LOG_ERROR, "invalid bit rate\\n");\n            return AVERROR(EINVAL);\n        }\n        s->frame_size_code = i << 1;\n        s->frame_size_min  = 2 * ff_ac3_frame_size_tab[s->frame_size_code][s->bit_alloc.sr_code];\n    }\n    s->bit_rate   = avctx->bit_rate;\n    s->frame_size = s->frame_size_min;\n    if (avctx->cutoff < 0) {\n        av_log(avctx, AV_LOG_ERROR, "invalid cutoff frequency\\n");\n        return AVERROR(EINVAL);\n    }\n    s->cutoff = avctx->cutoff;\n    if (s->cutoff > (s->sample_rate >> 1))\n        s->cutoff = s->sample_rate >> 1;\n    if ((avctx->audio_service_type == AV_AUDIO_SERVICE_TYPE_KARAOKE &&\n         avctx->channels == 1) ||\n        ((avctx->audio_service_type == AV_AUDIO_SERVICE_TYPE_COMMENTARY ||\n          avctx->audio_service_type == AV_AUDIO_SERVICE_TYPE_EMERGENCY  ||\n          avctx->audio_service_type == AV_AUDIO_SERVICE_TYPE_VOICE_OVER)\n         && avctx->channels > 1)) {\n        av_log(avctx, AV_LOG_ERROR, "invalid audio service type for the "\n                                    "specified number of channels\\n");\n        return AVERROR(EINVAL);\n    }\n    if (!s->eac3) {\n        ret = ff_ac3_validate_metadata(s);\n        if (ret)\n            return ret;\n    }\n    s->rematrixing_enabled = s->options.stereo_rematrixing &&\n                             (s->channel_mode == AC3_CHMODE_STEREO);\n    s->cpl_enabled = s->options.channel_coupling &&\n                     s->channel_mode >= AC3_CHMODE_STEREO && !s->fixed_point;\n    return 0;\n}', 'static av_cold int set_channel_info(AC3EncodeContext *s, int channels,\n                                    int64_t *channel_layout)\n{\n    int ch_layout;\n    if (channels < 1 || channels > AC3_MAX_CHANNELS)\n        return AVERROR(EINVAL);\n    if ((uint64_t)*channel_layout > 0x7FF)\n        return AVERROR(EINVAL);\n    ch_layout = *channel_layout;\n    if (!ch_layout)\n        ch_layout = avcodec_guess_channel_layout(channels, CODEC_ID_AC3, NULL);\n    s->lfe_on       = !!(ch_layout & AV_CH_LOW_FREQUENCY);\n    s->channels     = channels;\n    s->fbw_channels = channels - s->lfe_on;\n    s->lfe_channel  = s->lfe_on ? s->fbw_channels + 1 : -1;\n    if (s->lfe_on)\n        ch_layout -= AV_CH_LOW_FREQUENCY;\n    switch (ch_layout) {\n    case AV_CH_LAYOUT_MONO:           s->channel_mode = AC3_CHMODE_MONO;   break;\n    case AV_CH_LAYOUT_STEREO:         s->channel_mode = AC3_CHMODE_STEREO; break;\n    case AV_CH_LAYOUT_SURROUND:       s->channel_mode = AC3_CHMODE_3F;     break;\n    case AV_CH_LAYOUT_2_1:            s->channel_mode = AC3_CHMODE_2F1R;   break;\n    case AV_CH_LAYOUT_4POINT0:        s->channel_mode = AC3_CHMODE_3F1R;   break;\n    case AV_CH_LAYOUT_QUAD:\n    case AV_CH_LAYOUT_2_2:            s->channel_mode = AC3_CHMODE_2F2R;   break;\n    case AV_CH_LAYOUT_5POINT0:\n    case AV_CH_LAYOUT_5POINT0_BACK:   s->channel_mode = AC3_CHMODE_3F2R;   break;\n    default:\n        return AVERROR(EINVAL);\n    }\n    s->has_center   = (s->channel_mode & 0x01) && s->channel_mode != AC3_CHMODE_MONO;\n    s->has_surround =  s->channel_mode & 0x04;\n    s->channel_map  = ff_ac3_enc_channel_map[s->channel_mode][s->lfe_on];\n    *channel_layout = ch_layout;\n    if (s->lfe_on)\n        *channel_layout |= AV_CH_LOW_FREQUENCY;\n    return 0;\n}', 'static void bit_alloc_init(AC3EncodeContext *s)\n{\n    int ch;\n    s->slow_decay_code = 2;\n    s->fast_decay_code = 1;\n    s->slow_gain_code  = 1;\n    s->db_per_bit_code = s->eac3 ? 2 : 3;\n    s->floor_code      = 7;\n    for (ch = 0; ch <= s->channels; ch++)\n        s->fast_gain_code[ch] = 4;\n    s->coarse_snr_offset = 40;\n    s->bit_alloc.slow_decay = ff_ac3_slow_decay_tab[s->slow_decay_code] >> s->bit_alloc.sr_shift;\n    s->bit_alloc.fast_decay = ff_ac3_fast_decay_tab[s->fast_decay_code] >> s->bit_alloc.sr_shift;\n    s->bit_alloc.slow_gain  = ff_ac3_slow_gain_tab[s->slow_gain_code];\n    s->bit_alloc.db_per_bit = ff_ac3_db_per_bit_tab[s->db_per_bit_code];\n    s->bit_alloc.floor      = ff_ac3_floor_tab[s->floor_code];\n    s->bit_alloc.cpl_fast_leak = 0;\n    s->bit_alloc.cpl_slow_leak = 0;\n    count_frame_bits_fixed(s);\n}']