+ ff_af_queue_close(&s->afq);
+ return 0;
+}
+
+static av_cold int dsp_init(AVCodecContext *avctx, AACEncContext *s)
+{
+ int ret = 0;
+
+ 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;
+
+ 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);
+