+static av_cold int dsp_init(AVCodecContext *avctx, AACEncContext *s)
+{
+ int ret = 0;
+
+ ff_dsputil_init(&s->dsp, avctx);
+ avpriv_float_dsp_init(&s->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
+
+ // window init
+ ff_kbd_window_init(ff_aac_kbd_long_1024, 4.0, 1024);
+ ff_kbd_window_init(ff_aac_kbd_short_128, 6.0, 128);
+ ff_init_ff_sine_windows(10);
+ ff_init_ff_sine_windows(7);
+
+ if (ret = ff_mdct_init(&s->mdct1024, 11, 0, 32768.0))
+ return ret;
+ if (ret = ff_mdct_init(&s->mdct128, 8, 0, 32768.0))
+ return ret;
+
+ return 0;
+}
+
+static av_cold int alloc_buffers(AVCodecContext *avctx, AACEncContext *s)
+{
+ int ch;
+ FF_ALLOCZ_OR_GOTO(avctx, s->buffer.samples, 3 * 1024 * s->channels * sizeof(s->buffer.samples[0]), alloc_fail);
+ FF_ALLOCZ_OR_GOTO(avctx, s->cpe, sizeof(ChannelElement) * s->chan_map[0], alloc_fail);
+ FF_ALLOCZ_OR_GOTO(avctx, avctx->extradata, 5 + FF_INPUT_BUFFER_PADDING_SIZE, alloc_fail);
+
+ for(ch = 0; ch < s->channels; ch++)
+ s->planar_samples[ch] = s->buffer.samples + 3 * 1024 * ch;
+
+#if FF_API_OLD_ENCODE_AUDIO
+ if (!(avctx->coded_frame = avcodec_alloc_frame()))
+ goto alloc_fail;
+#endif
+
+ return 0;
+alloc_fail:
+ return AVERROR(ENOMEM);
+}
+
+static av_cold int aac_encode_init(AVCodecContext *avctx)
+{
+ AACEncContext *s = avctx->priv_data;
+ int i, ret = 0;
+ const uint8_t *sizes[2];
+ uint8_t grouping[AAC_MAX_CHANNELS];
+ int lengths[2];
+
+ avctx->frame_size = 1024;
+
+ for (i = 0; i < 16; i++)
+ if (avctx->sample_rate == avpriv_mpeg4audio_sample_rates[i])
+ break;
+
+ s->channels = avctx->channels;
+
+ ERROR_IF(i == 16,
+ "Unsupported sample rate %d\n", avctx->sample_rate);
+ ERROR_IF(s->channels > AAC_MAX_CHANNELS,
+ "Unsupported number of channels: %d\n", s->channels);
+ ERROR_IF(avctx->profile != FF_PROFILE_UNKNOWN && avctx->profile != FF_PROFILE_AAC_LOW,
+ "Unsupported profile %d\n", avctx->profile);
+ ERROR_IF(1024.0 * avctx->bit_rate / avctx->sample_rate > 6144 * s->channels,
+ "Too many bits per frame requested\n");
+
+ s->samplerate_index = i;
+
+ s->chan_map = aac_chan_configs[s->channels-1];
+
+ if (ret = dsp_init(avctx, s))
+ goto fail;
+
+ if (ret = alloc_buffers(avctx, s))
+ goto fail;
+
+ avctx->extradata_size = 5;
+ put_audio_specific_config(avctx);
+
+ sizes[0] = swb_size_1024[i];
+ sizes[1] = swb_size_128[i];
+ lengths[0] = ff_aac_num_swb_1024[i];
+ lengths[1] = ff_aac_num_swb_128[i];
+ for (i = 0; i < s->chan_map[0]; i++)
+ grouping[i] = s->chan_map[i + 1] == TYPE_CPE;
+ if (ret = ff_psy_init(&s->psy, avctx, 2, sizes, lengths, s->chan_map[0], grouping))
+ goto fail;
+ s->psypp = ff_psy_preprocess_init(avctx);
+ s->coder = &ff_aac_coders[2];
+
+ s->lambda = avctx->global_quality ? avctx->global_quality : 120;
+
+ ff_aac_tableinit();
+
+ for (i = 0; i < 428; i++)
+ ff_aac_pow34sf_tab[i] = sqrt(ff_aac_pow2sf_tab[i] * sqrt(ff_aac_pow2sf_tab[i]));
+
+ avctx->delay = 1024;
+ ff_af_queue_init(avctx, &s->afq);
+
+ return 0;
+fail:
+ aac_encode_end(avctx);
+ return ret;
+}
+
+#define AACENC_FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
+static const AVOption aacenc_options[] = {
+ {"stereo_mode", "Stereo coding method", offsetof(AACEncContext, options.stereo_mode), AV_OPT_TYPE_INT, {.i64 = 0}, -1, 1, AACENC_FLAGS, "stereo_mode"},
+ {"auto", "Selected by the Encoder", 0, AV_OPT_TYPE_CONST, {.i64 = -1 }, INT_MIN, INT_MAX, AACENC_FLAGS, "stereo_mode"},
+ {"ms_off", "Disable Mid/Side coding", 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, INT_MIN, INT_MAX, AACENC_FLAGS, "stereo_mode"},
+ {"ms_force", "Force Mid/Side for the whole frame if possible", 0, AV_OPT_TYPE_CONST, {.i64 = 1 }, INT_MIN, INT_MAX, AACENC_FLAGS, "stereo_mode"},
+ {NULL}
+};
+
+static const AVClass aacenc_class = {
+ "AAC encoder",
+ av_default_item_name,
+ aacenc_options,
+ LIBAVUTIL_VERSION_INT,
+};
+
+AVCodec ff_aac_encoder = {
+ .name = "aac",
+ .type = AVMEDIA_TYPE_AUDIO,
+ .id = AV_CODEC_ID_AAC,
+ .priv_data_size = sizeof(AACEncContext),
+ .init = aac_encode_init,
+ .encode2 = aac_encode_frame,
+ .close = aac_encode_end,
+ .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY |
+ CODEC_CAP_EXPERIMENTAL,
+ .sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLTP,
+ AV_SAMPLE_FMT_NONE },
+ .long_name = NULL_IF_CONFIG_SMALL("AAC (Advanced Audio Coding)"),
+ .priv_class = &aacenc_class,