93 .num_ele = { 1, 0, 0, 0 },
94 .pairing = { { 0 }, },
101 .num_ele = { 1, 0, 0, 0 },
102 .pairing = { { 1 }, },
105 .reorder_map = { 0, 1 },
109 .num_ele = { 1, 0, 0, 1 },
110 .pairing = { { 1 }, },
111 .index = { { 0 },{ 0 },{ 0 },{ 0 } },
113 .reorder_map = { 0, 1, 2 },
117 .num_ele = { 1, 0, 1, 0 },
118 .pairing = { { 1 },{ 0 },{ 0 } },
119 .index = { { 0 },{ 0 },{ 0 }, },
121 .reorder_map = { 0, 1, 2 },
125 .num_ele = { 2, 0, 0, 0 },
126 .pairing = { { 0, 1 }, },
127 .index = { { 0, 0 }, },
129 .reorder_map = { 2, 0, 1 },
133 .num_ele = { 2, 0, 0, 1 },
134 .pairing = { { 0, 1 }, },
135 .index = { { 0, 0 }, { 0 }, { 0 }, { 0 }, },
137 .reorder_map = { 2, 0, 1, 3 },
141 .num_ele = { 2, 0, 1, 0 },
142 .pairing = { { 0, 1 }, { 0 }, { 0 }, },
143 .index = { { 0, 0 }, { 0 }, { 1 } },
145 .reorder_map = { 2, 0, 1, 3 },
149 .num_ele = { 2, 0, 1, 1 },
150 .pairing = { { 0, 1 }, { 0 }, { 0 }, },
151 .index = { { 0, 0 }, { 0 }, { 1 }, { 0 } },
153 .reorder_map = { 2, 0, 1, 4, 3 },
157 .num_ele = { 1, 1, 0, 0 },
158 .pairing = { { 1 }, { 1 }, },
159 .index = { { 0 }, { 1 }, },
161 .reorder_map = { 0, 1, 2, 3 },
165 .num_ele = { 1, 0, 1, 0 },
166 .pairing = { { 1 }, { 0 }, { 1 }, },
167 .index = { { 0 }, { 0 }, { 1 } },
169 .reorder_map = { 0, 1, 2, 3 },
173 .num_ele = { 2, 1, 0, 0 },
174 .pairing = { { 0, 1 }, { 1 }, },
175 .index = { { 0, 0 }, { 1 } },
177 .reorder_map = { 2, 0, 1, 3, 4 },
181 .num_ele = { 2, 1, 0, 1 },
182 .pairing = { { 0, 1 }, { 1 }, },
183 .index = { { 0, 0 }, { 1 }, { 0 }, { 0 }, },
185 .reorder_map = { 2, 0, 1, 4, 5, 3 },
189 .num_ele = { 2, 0, 1, 0 },
190 .pairing = { { 0, 1 }, { 0 }, { 1 } },
191 .index = { { 0, 0 }, { 0 }, { 1 } },
193 .reorder_map = { 2, 0, 1, 3, 4 },
197 .num_ele = { 2, 0, 1, 1 },
198 .pairing = { { 0, 1 }, { 0 }, { 1 }, },
199 .index = { { 0, 0 }, { 0 }, { 1 }, { 0 } },
201 .reorder_map = { 2, 0, 1, 4, 5, 3 },
205 .num_ele = { 2, 1, 1, 0 },
206 .pairing = { { 0, 1 }, { 1 }, { 0 }, },
207 .index = { { 0, 0 }, { 1 }, { 1 } },
209 .reorder_map = { 2, 0, 1, 4, 5, 3 },
213 .num_ele = { 2, 1, 0, 0 },
214 .pairing = { { 1, 1 }, { 1 } },
215 .index = { { 0, 1 }, { 2 }, },
217 .reorder_map = { 0, 1, 2, 3, 4, 5 },
221 .num_ele = { 2, 0, 2, 0 },
222 .pairing = { { 0, 1 }, { 0 }, { 1, 0 } },
223 .index = { { 0, 0 }, { 0 }, { 1, 1 } },
225 .reorder_map = { 2, 0, 1, 3, 4, 5 },
229 .num_ele = { 2, 1, 1, 1 },
230 .pairing = { { 0, 1 },{ 1 },{ 0 }, },
231 .index = { { 0, 0 },{ 1 },{ 1 },{ 0 } },
233 .reorder_map = { 2, 0, 1, 5, 6, 4, 3 },
237 .num_ele = { 2, 0, 2, 1 },
238 .pairing = { { 0, 1 },{ 1, 0 },{ 0 }, },
239 .index = { { 0, 0 },{ 0 },{ 1, 1 },{ 0 } },
241 .reorder_map = { 2, 0, 1, 4, 5, 6, 3 },
245 .num_ele = { 2, 1, 0, 1 },
246 .pairing = { { 1, 1 },{ 1 } },
247 .index = { { 0, 1 },{ 2 },{ 0 },{ 0 } },
249 .reorder_map = { 0, 1, 3, 4, 5, 6, 2 },
253 .num_ele = { 2, 0, 2, 0 },
254 .pairing = { { 0, 1 }, { 0 }, { 1, 1 }, },
255 .index = { { 0, 0 }, { 0 }, { 2, 1 }, },
257 .reorder_map = { 2, 0, 1, 3, 4, 5, 6 },
261 .num_ele = { 3, 1, 0, 0 },
262 .pairing = { { 0, 1, 1 }, { 1 }, },
263 .index = { { 0, 0, 1 }, { 2 }, },
265 .reorder_map = { 2, 0, 1, 3, 4, 5, 6 },
269 .num_ele = { 2, 0, 2, 1 },
270 .pairing = { { 0, 1 }, { 0 }, { 1, 1 }, },
271 .index = { { 0, 0 }, { 0 }, { 2, 1 }, { 0 } },
273 .reorder_map = { 2, 0, 1, 4, 5, 6, 7, 3 },
277 .num_ele = { 3, 1, 0, 1 },
278 .pairing = { { 0, 1, 1 }, { 1 }, },
279 .index = { { 0, 0, 1 }, { 2 }, },
281 .reorder_map = { 2, 0, 1, 4, 5, 6, 7, 3 },
285 .num_ele = { 3, 0, 1, 1 },
286 .pairing = { { 0, 1, 1 }, { 0 }, { 1 } },
287 .index = { { 0, 0, 2 }, { 0 }, { 1 }, { 0 } },
289 .reorder_map = { 2, 0, 1, 4, 5, 6, 7, 3 },
293 .num_ele = { 2, 0, 3, 0 },
294 .pairing = { { 0, 1 }, { 0 }, { 1, 1, 0 }, },
295 .index = { { 0, 0 }, { 0 }, { 1, 2, 1 }, },
297 .reorder_map = { 2, 0, 1, 6, 7, 3, 4, 5 },
325 for (
i = 0;
i < 4;
i++) {
326 for (j = 0; j < pce->
num_ele[
i]; j++) {
346 const int max_size = 32;
375 ++
s->quantize_band_cost_cache_generation;
376 if (
s->quantize_band_cost_cache_generation == 0) {
377 memset(
s->quantize_band_cost_cache, 0,
sizeof(
s->quantize_band_cost_cache));
378 s->quantize_band_cost_cache_generation = 1;
382 #define WINDOW_FUNC(type) \
383 static void apply_ ##type ##_window(AVFloatDSPContext *fdsp, \
384 SingleChannelElement *sce, \
391 float *
out = sce->ret_buf;
393 fdsp->vector_fmul (
out, audio, lwindow, 1024);
394 fdsp->vector_fmul_reverse(
out + 1024, audio + 1024, pwindow, 1024);
401 float *
out = sce->ret_buf;
403 fdsp->vector_fmul(
out, audio, lwindow, 1024);
404 memcpy(
out + 1024, audio + 1024,
sizeof(
out[0]) * 448);
405 fdsp->vector_fmul_reverse(
out + 1024 + 448, audio + 1024 + 448, swindow, 128);
406 memset(
out + 1024 + 576, 0,
sizeof(
out[0]) * 448);
413 float *
out = sce->ret_buf;
415 memset(
out, 0,
sizeof(
out[0]) * 448);
416 fdsp->vector_fmul(
out + 448, audio + 448, swindow, 128);
417 memcpy(
out + 576, audio + 576,
sizeof(
out[0]) * 448);
418 fdsp->vector_fmul_reverse(
out + 1024, audio + 1024, lwindow, 1024);
425 const float *in = audio + 448;
426 float *
out = sce->ret_buf;
429 for (
w = 0;
w < 8;
w++) {
430 fdsp->vector_fmul (
out, in,
w ? pwindow : swindow, 128);
433 fdsp->vector_fmul_reverse(
out, in, swindow, 128);
440 const float *audio) = {
458 for (
i = 0;
i < 1024;
i += 128)
460 memcpy(audio, audio + 1024,
sizeof(audio[0]) * 1024);
480 for (
w = 1;
w < 8;
w++)
508 for (ch = 0; ch < chans; ch++) {
513 for (cmaxsfb = ics->
num_swb; cmaxsfb > 0 && cpe->
ch[ch].
zeroes[
w*16+cmaxsfb-1]; cmaxsfb--)
515 maxsfb =
FFMAX(maxsfb, cmaxsfb);
544 if (msc == 0 || ics0->
max_sfb == 0)
559 int start = (
w+w2) * 128;
588 int start = (
w+w2) * 128;
619 if (
s->coder->set_special_band_scalefactors)
620 s->coder->set_special_band_scalefactors(
s, sce);
633 int off_is = 0, noise_flag = 1;
642 if (noise_flag-- > 0) {
696 s->coder->quantize_and_encode_band(
s, &
s->pb,
697 &sce->
coeffs[start + w2*128],
720 float *swb_coeffs = &sce->
coeffs[start +
w*128];
743 if (
s->coder->encode_tns_info)
744 s->coder->encode_tns_info(
s, sce);
755 int i, namelen, padbits;
757 namelen = strlen(
name) + 2;
765 for (
i = 0;
i < namelen - 2;
i++)
777 int end = 2048 + (
frame ?
frame->nb_samples : 0);
781 for (ch = 0; ch <
s->channels; ch++) {
783 memcpy(&
s->planar_samples[ch][1024], &
s->planar_samples[ch][2048], 1024 *
sizeof(
s->planar_samples[0][0]));
787 memcpy(&
s->planar_samples[ch][2048],
789 frame->nb_samples *
sizeof(
s->planar_samples[0][0]));
791 memset(&
s->planar_samples[ch][end], 0,
792 (3072 - end) *
sizeof(
s->planar_samples[0][0]));
800 float **
samples =
s->planar_samples, *samples2, *la, *overlap;
804 int i, its, ch,
w, chans,
tag, start_ch,
ret, frame_bits;
805 int target_bits, rate_bits, too_many_bits, too_few_bits;
806 int ms_mode = 0, is_mode = 0, tns_mode = 0, pred_mode = 0;
807 int chan_el_counter[4];
815 if (!
s->afq.remaining_samples || (!
s->afq.frame_alloc && !
s->afq.frame_count))
825 for (
i = 0;
i <
s->chan_map[0];
i++) {
827 tag =
s->chan_map[
i+1];
830 for (ch = 0; ch < chans; ch++) {
832 float clip_avoidance_factor;
835 s->cur_channel = start_ch + ch;
836 overlap = &
samples[
s->cur_channel][0];
837 samples2 = overlap + 1024;
838 la = samples2 + (448+64);
852 ics->
num_swb =
s->samplerate_index >= 8 ? 1 : 3;
854 wi[ch] =
s->psy.model->window(&
s->psy, samples2, la,
s->cur_channel,
876 clip_avoidance_factor = 0.0f;
878 const float *wbuf = overlap +
w * 128;
883 for (j = 0; j < wlen; j++)
890 clip_avoidance_factor =
FFMAX(clip_avoidance_factor, wi[ch].clipping[
w]);
903 for (k = 0; k < 1024; k++) {
915 frame_bits = its = 0;
923 memset(chan_el_counter, 0,
sizeof(chan_el_counter));
924 for (
i = 0;
i <
s->chan_map[0];
i++) {
926 const float *coeffs[2];
927 tag =
s->chan_map[
i+1];
935 for (ch = 0; ch < chans; ch++) {
939 for (
w = 0;
w < 128;
w++)
943 s->psy.bitres.alloc = -1;
944 s->psy.bitres.bits =
s->last_frame_pb_count /
s->channels;
945 s->psy.model->analyze(&
s->psy, start_ch, coeffs, wi);
946 if (
s->psy.bitres.alloc > 0) {
948 target_bits +=
s->psy.bitres.alloc
950 s->psy.bitres.alloc /= chans;
953 for (ch = 0; ch < chans; ch++) {
954 s->cur_channel = start_ch + ch;
955 if (
s->options.pns &&
s->coder->mark_pns)
956 s->coder->mark_pns(
s, avctx, &cpe->
ch[ch]);
957 s->coder->search_for_quantizers(avctx,
s, &cpe->
ch[ch],
s->lambda);
960 && wi[0].window_type[0] == wi[1].window_type[0]
961 && wi[0].window_shape == wi[1].window_shape) {
965 if (wi[0].grouping[
w] != wi[1].grouping[
w]) {
971 for (ch = 0; ch < chans; ch++) {
973 s->cur_channel = start_ch + ch;
974 if (
s->options.tns &&
s->coder->search_for_tns)
975 s->coder->search_for_tns(
s, sce);
976 if (
s->options.tns &&
s->coder->apply_tns_filt)
977 s->coder->apply_tns_filt(
s, sce);
980 if (
s->options.pns &&
s->coder->search_for_pns)
981 s->coder->search_for_pns(
s, avctx, sce);
983 s->cur_channel = start_ch;
984 if (
s->options.intensity_stereo) {
985 if (
s->coder->search_for_is)
986 s->coder->search_for_is(
s, avctx, cpe);
990 if (
s->options.mid_side) {
991 if (
s->options.mid_side == -1 &&
s->coder->search_for_ms)
992 s->coder->search_for_ms(
s, cpe);
1003 if (cpe->
ms_mode) ms_mode = 1;
1006 for (ch = 0; ch < chans; ch++) {
1007 s->cur_channel = start_ch + ch;
1024 rate_bits =
FFMIN(rate_bits, 6144 *
s->channels - 3);
1025 too_many_bits =
FFMAX(target_bits, rate_bits);
1026 too_many_bits =
FFMIN(too_many_bits, 6144 *
s->channels - 3);
1027 too_few_bits =
FFMIN(
FFMAX(rate_bits - rate_bits/4, target_bits), too_many_bits);
1031 if (rate_bits < frame_bits) {
1032 float ratio = ((
float)rate_bits) / frame_bits;
1033 s->lambda *=
FFMIN(0.9
f, ratio);
1042 too_few_bits = too_few_bits - too_few_bits/8;
1043 too_many_bits = too_many_bits + too_many_bits/2;
1046 || (its < 5 && (frame_bits < too_few_bits || frame_bits > too_many_bits))
1047 || frame_bits >= 6144 *
s->channels - 3 )
1049 float ratio = ((
float)rate_bits) / frame_bits;
1051 if (frame_bits >= too_few_bits && frame_bits <= too_many_bits) {
1062 ratio =
sqrtf(ratio);
1064 s->lambda =
av_clipf(
s->lambda * ratio, FLT_EPSILON, 65536.f);
1067 if (ratio > 0.9
f && ratio < 1.1
f) {
1070 if (is_mode || ms_mode || tns_mode || pred_mode) {
1071 for (
i = 0;
i <
s->chan_map[0];
i++) {
1075 for (ch = 0; ch < chans; ch++)
1092 s->lambda_sum +=
s->lambda;
1100 *got_packet_ptr = 1;
1124 float scale = 32768.0f;
1131 1024, &
scale, 0)) < 0)
1134 128, &
scale, 0)) < 0)
1147 for(ch = 0; ch <
s->channels; ch++)
1148 s->planar_samples[ch] =
s->buffer.samples + 3 * 1024 * ch;
1158 const uint8_t *
sizes[2];
1163 s->last_frame_pb_count = 0;
1174 s->needs_pce =
s->options.pce;
1189 av_log(avctx,
AV_LOG_INFO,
"Using a PCE to encode channel layout \"%s\"\n", buf);
1191 s->reorder_map =
s->pce.reorder_map;
1192 s->chan_map =
s->pce.config_map;
1200 for (
i = 1;
i <=
s->chan_map[0];
i++) {
1208 for (
int i = 0;;
i++) {
1211 s->samplerate_index =
i;
1218 "Too many bits %f > %d per frame requested, clamping to max\n",
1220 6144 *
s->channels);
1234 "PNS unavailable in the \"mpeg2_aac_low\" profile, turning off\n");
1243 if (
s->channels > 3)
1244 s->options.mid_side = 0;
1262 for (
i = 0;
i <
s->chan_map[0];
i++)
1265 s->chan_map[0], grouping)) < 0)
1268 s->random_state = 0x1f2e3d4c;
1277 #define AACENC_FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
1279 {
"aac_coder",
"Coding algorithm", offsetof(
AACEncContext,
options.coder),
AV_OPT_TYPE_INT, {.i64 =
AAC_CODER_TWOLOOP}, 0,
AAC_CODER_NB-1,
AACENC_FLAGS, .unit =
"coder"},