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 = { { 1, 0 }, },
127 .index = { { 0, 0 }, },
129 .reorder_map = { 0, 1, 2 },
133 .num_ele = { 2, 0, 0, 1 },
134 .pairing = { { 1, 0 }, },
135 .index = { { 0, 0 }, { 0 }, { 0 }, { 0 }, },
137 .reorder_map = { 0, 1, 2, 3 },
141 .num_ele = { 2, 0, 1, 0 },
142 .pairing = { { 1, 0 }, { 0 }, { 0 }, },
143 .index = { { 0, 0 }, { 0 }, { 1 } },
145 .reorder_map = { 0, 1, 2, 3 },
149 .num_ele = { 2, 1, 1, 0 },
150 .pairing = { { 1, 0 }, { 0 }, { 0 }, },
151 .index = { { 0, 0 }, { 1 }, { 2 }, { 0 } },
153 .reorder_map = { 0, 1, 2, 3, 4 },
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 = { { 1, 0 }, { 1 }, },
175 .index = { { 0, 0 }, { 1 } },
177 .reorder_map = { 0, 1, 2, 3, 4 },
181 .num_ele = { 2, 1, 1, 0 },
182 .pairing = { { 1, 0 }, { 0 }, { 1 }, },
183 .index = { { 0, 0 }, { 1 }, { 1 } },
185 .reorder_map = { 0, 1, 2, 3, 4, 5 },
189 .num_ele = { 2, 0, 1, 0 },
190 .pairing = { { 1, 0 }, { 0 }, { 1 } },
191 .index = { { 0, 0 }, { 0 }, { 1 } },
193 .reorder_map = { 0, 1, 2, 3, 4 },
197 .num_ele = { 2, 1, 1, 0 },
198 .pairing = { { 1, 0 }, { 0 }, { 1 }, },
199 .index = { { 0, 0 }, { 1 }, { 1 } },
201 .reorder_map = { 0, 1, 2, 3, 4, 5 },
205 .num_ele = { 2, 1, 1, 0 },
206 .pairing = { { 1, 0 }, { 1 }, { 0 }, },
207 .index = { { 0, 0 }, { 1 }, { 1 } },
209 .reorder_map = { 0, 1, 2, 3, 4, 5 },
213 .num_ele = { 2, 1, 0, 0 },
214 .pairing = { { 1, 1 }, { 1 } },
215 .index = { { 1, 0 }, { 2 }, },
217 .reorder_map = { 0, 1, 2, 3, 4, 5 },
221 .num_ele = { 2, 0, 2, 0 },
222 .pairing = { { 1, 0 },{ 0 },{ 1, 0 }, },
223 .index = { { 0, 0 },{ 0 },{ 1, 1 } },
225 .reorder_map = { 0, 1, 2, 3, 4, 5 },
229 .num_ele = { 2, 1, 2, 0 },
230 .pairing = { { 1, 0 },{ 0 },{ 1, 0 }, },
231 .index = { { 0, 0 },{ 1 },{ 1, 2 } },
233 .reorder_map = { 0, 1, 2, 3, 4, 5, 6 },
237 .num_ele = { 2, 1, 2, 0 },
238 .pairing = { { 1, 0 }, { 0 }, { 1, 0 }, },
239 .index = { { 0, 0 }, { 1 }, { 1, 2 } },
241 .reorder_map = { 0, 1, 2, 3, 4, 5, 6 },
245 .num_ele = { 2, 1, 2, 0 },
246 .pairing = { { 1, 0 }, { 0 }, { 1, 0 }, },
247 .index = { { 0, 0 }, { 1 }, { 1, 2 } },
249 .reorder_map = { 0, 1, 2, 3, 4, 5, 6 },
253 .num_ele = { 2, 1, 1, 0 },
254 .pairing = { { 1, 0 }, { 1 }, { 1 }, },
255 .index = { { 0, 0 }, { 1 }, { 2 }, },
257 .reorder_map = { 0, 1, 2, 3, 4, 5, 6 },
261 .num_ele = { 2, 1, 1, 0 },
262 .pairing = { { 1, 0 }, { 1 }, { 1 }, },
263 .index = { { 0, 0 }, { 1 }, { 2 }, },
265 .reorder_map = { 0, 1, 2, 3, 4, 5, 6 },
269 .num_ele = { 2, 1, 2, 0 },
270 .pairing = { { 1, 0 }, { 0 }, { 1, 1 }, },
271 .index = { { 0, 0 }, { 1 }, { 1, 2 }, { 0 } },
273 .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7 },
277 .num_ele = { 2, 1, 2, 0 },
278 .pairing = { { 1, 0 }, { 0 },{ 1, 1 }, },
279 .index = { { 0, 0 }, { 1 }, { 1, 2 }, { 0 } },
281 .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7 },
285 .num_ele = { 2, 1, 2, 0 },
286 .pairing = { { 1, 0 }, { 0 }, { 1, 1 }, },
287 .index = { { 0, 0 }, { 1 }, { 1, 2 }, { 0 } },
289 .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7 },
293 .num_ele = { 2, 1, 2, 0 },
294 .pairing = { { 1, 0 }, { 1 }, { 1, 0 }, },
295 .index = { { 0, 0 }, { 1 }, { 2, 1 } },
297 .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7 },
302 .num_ele = { 2, 2, 2, 0 },
303 .pairing = { { 1, 0 }, { 1, 0 }, { 1, 0 }, },
304 .index = { { 0, 0 }, { 1, 1 }, { 2, 2 } },
306 .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7, 8 },
312 .num_ele = { 2, 2, 2, 0 },
313 .pairing = { { 1, 1 }, { 1, 0 }, { 1, 0 }, },
314 .index = { { 0, 1 }, { 2, 0 }, { 3, 1 } },
316 .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
320 .num_ele = { 4, 2, 4, 0 },
321 .pairing = { { 1, 0, 1, 0 }, { 1, 1 }, { 1, 0, 1, 0 }, },
322 .index = { { 0, 0, 1, 1 }, { 2, 3 }, { 4, 2, 5, 3 } },
323 .config_map = { 10,
TYPE_CPE,
TYPE_SCE,
TYPE_CPE,
TYPE_SCE,
TYPE_CPE,
TYPE_CPE,
TYPE_CPE,
TYPE_SCE,
TYPE_CPE,
TYPE_SCE },
324 .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
352 for (
i = 0;
i < 4;
i++) {
353 for (j = 0; j < pce->
num_ele[
i]; j++) {
373 int channels = (!
s->needs_pce)*(
s->channels - (
s->channels == 8 ? 1 : 0));
374 const int max_size = 32;
403 ++
s->quantize_band_cost_cache_generation;
404 if (
s->quantize_band_cost_cache_generation == 0) {
405 memset(
s->quantize_band_cost_cache, 0,
sizeof(
s->quantize_band_cost_cache));
406 s->quantize_band_cost_cache_generation = 1;
410 #define WINDOW_FUNC(type) \
411 static void apply_ ##type ##_window(AVFloatDSPContext *fdsp, \
412 SingleChannelElement *sce, \
419 float *
out = sce->ret_buf;
421 fdsp->vector_fmul (
out, audio, lwindow, 1024);
422 fdsp->vector_fmul_reverse(
out + 1024, audio + 1024, pwindow, 1024);
429 float *
out = sce->ret_buf;
431 fdsp->vector_fmul(
out, audio, lwindow, 1024);
432 memcpy(
out + 1024, audio + 1024,
sizeof(
out[0]) * 448);
433 fdsp->vector_fmul_reverse(
out + 1024 + 448, audio + 1024 + 448, swindow, 128);
434 memset(
out + 1024 + 576, 0,
sizeof(
out[0]) * 448);
441 float *
out = sce->ret_buf;
443 memset(
out, 0,
sizeof(
out[0]) * 448);
444 fdsp->vector_fmul(
out + 448, audio + 448, swindow, 128);
445 memcpy(
out + 576, audio + 576,
sizeof(
out[0]) * 448);
446 fdsp->vector_fmul_reverse(
out + 1024, audio + 1024, lwindow, 1024);
453 const float *in = audio + 448;
454 float *
out = sce->ret_buf;
457 for (
w = 0;
w < 8;
w++) {
458 fdsp->vector_fmul (
out, in,
w ? pwindow : swindow, 128);
461 fdsp->vector_fmul_reverse(
out, in, swindow, 128);
468 const float *audio) = {
486 for (
i = 0;
i < 1024;
i += 128)
488 memcpy(audio, audio + 1024,
sizeof(audio[0]) * 1024);
508 for (
w = 1;
w < 8;
w++)
536 for (ch = 0; ch < chans; ch++) {
541 for (cmaxsfb = ics->
num_swb; cmaxsfb > 0 && cpe->
ch[ch].
zeroes[
w*16+cmaxsfb-1]; cmaxsfb--)
543 maxsfb =
FFMAX(maxsfb, cmaxsfb);
572 if (msc == 0 || ics0->
max_sfb == 0)
587 int start = (
w+w2) * 128;
616 int start = (
w+w2) * 128;
647 if (
s->coder->set_special_band_scalefactors)
648 s->coder->set_special_band_scalefactors(
s, sce);
661 int off_is = 0, noise_flag = 1;
670 if (noise_flag-- > 0) {
724 s->coder->quantize_and_encode_band(
s, &
s->pb,
725 &sce->
coeffs[start + w2*128],
748 float *swb_coeffs = &sce->
coeffs[start +
w*128];
765 if (!common_window) {
767 if (
s->coder->encode_main_pred)
768 s->coder->encode_main_pred(
s, sce);
769 if (
s->coder->encode_ltp_info)
770 s->coder->encode_ltp_info(
s, sce, 0);
776 if (
s->coder->encode_tns_info)
777 s->coder->encode_tns_info(
s, sce);
788 int i, namelen, padbits;
790 namelen = strlen(
name) + 2;
798 for (
i = 0;
i < namelen - 2;
i++)
810 int end = 2048 + (
frame ?
frame->nb_samples : 0);
814 for (ch = 0; ch <
s->channels; ch++) {
816 memcpy(&
s->planar_samples[ch][1024], &
s->planar_samples[ch][2048], 1024 *
sizeof(
s->planar_samples[0][0]));
820 memcpy(&
s->planar_samples[ch][2048],
822 frame->nb_samples *
sizeof(
s->planar_samples[0][0]));
824 memset(&
s->planar_samples[ch][end], 0,
825 (3072 - end) *
sizeof(
s->planar_samples[0][0]));
833 float **
samples =
s->planar_samples, *samples2, *la, *overlap;
837 int i, its, ch,
w, chans,
tag, start_ch,
ret, frame_bits;
838 int target_bits, rate_bits, too_many_bits, too_few_bits;
839 int ms_mode = 0, is_mode = 0, tns_mode = 0, pred_mode = 0;
840 int chan_el_counter[4];
848 if (!
s->afq.remaining_samples || (!
s->afq.frame_alloc && !
s->afq.frame_count))
860 for (
i = 0;
i <
s->chan_map[0];
i++) {
862 tag =
s->chan_map[
i+1];
865 for (ch = 0; ch < chans; ch++) {
867 float clip_avoidance_factor;
870 s->cur_channel = start_ch + ch;
871 overlap = &
samples[
s->cur_channel][0];
872 samples2 = overlap + 1024;
873 la = samples2 + (448+64);
887 ics->
num_swb =
s->samplerate_index >= 8 ? 1 : 3;
889 wi[ch] =
s->psy.model->window(&
s->psy, samples2, la,
s->cur_channel,
911 clip_avoidance_factor = 0.0f;
913 const float *wbuf = overlap +
w * 128;
918 for (j = 0; j < wlen; j++)
925 clip_avoidance_factor =
FFMAX(clip_avoidance_factor, wi[ch].clipping[
w]);
938 if (
s->options.ltp &&
s->coder->update_ltp) {
939 s->coder->update_ltp(
s, sce);
944 for (k = 0; k < 1024; k++) {
956 frame_bits = its = 0;
964 memset(chan_el_counter, 0,
sizeof(chan_el_counter));
965 for (
i = 0;
i <
s->chan_map[0];
i++) {
967 const float *coeffs[2];
968 tag =
s->chan_map[
i+1];
976 for (ch = 0; ch < chans; ch++) {
984 for (
w = 0;
w < 128;
w++)
988 s->psy.bitres.alloc = -1;
989 s->psy.bitres.bits =
s->last_frame_pb_count /
s->channels;
990 s->psy.model->analyze(&
s->psy, start_ch, coeffs, wi);
991 if (
s->psy.bitres.alloc > 0) {
993 target_bits +=
s->psy.bitres.alloc
995 s->psy.bitres.alloc /= chans;
998 for (ch = 0; ch < chans; ch++) {
999 s->cur_channel = start_ch + ch;
1000 if (
s->options.pns &&
s->coder->mark_pns)
1001 s->coder->mark_pns(
s, avctx, &cpe->
ch[ch]);
1002 s->coder->search_for_quantizers(avctx,
s, &cpe->
ch[ch],
s->lambda);
1005 && wi[0].window_type[0] == wi[1].window_type[0]
1006 && wi[0].window_shape == wi[1].window_shape) {
1010 if (wi[0].grouping[
w] != wi[1].grouping[
w]) {
1016 for (ch = 0; ch < chans; ch++) {
1018 s->cur_channel = start_ch + ch;
1019 if (
s->options.tns &&
s->coder->search_for_tns)
1020 s->coder->search_for_tns(
s, sce);
1021 if (
s->options.tns &&
s->coder->apply_tns_filt)
1022 s->coder->apply_tns_filt(
s, sce);
1025 if (
s->options.pns &&
s->coder->search_for_pns)
1026 s->coder->search_for_pns(
s, avctx, sce);
1028 s->cur_channel = start_ch;
1029 if (
s->options.intensity_stereo) {
1030 if (
s->coder->search_for_is)
1031 s->coder->search_for_is(
s, avctx, cpe);
1032 if (cpe->
is_mode) is_mode = 1;
1035 if (
s->options.pred) {
1036 for (ch = 0; ch < chans; ch++) {
1038 s->cur_channel = start_ch + ch;
1039 if (
s->options.pred &&
s->coder->search_for_pred)
1040 s->coder->search_for_pred(
s, sce);
1043 if (
s->coder->adjust_common_pred)
1044 s->coder->adjust_common_pred(
s, cpe);
1045 for (ch = 0; ch < chans; ch++) {
1047 s->cur_channel = start_ch + ch;
1048 if (
s->options.pred &&
s->coder->apply_main_pred)
1049 s->coder->apply_main_pred(
s, sce);
1051 s->cur_channel = start_ch;
1053 if (
s->options.mid_side) {
1054 if (
s->options.mid_side == -1 &&
s->coder->search_for_ms)
1055 s->coder->search_for_ms(
s, cpe);
1061 if (
s->options.ltp) {
1062 for (ch = 0; ch < chans; ch++) {
1064 s->cur_channel = start_ch + ch;
1065 if (
s->coder->search_for_ltp)
1069 s->cur_channel = start_ch;
1070 if (
s->coder->adjust_common_ltp)
1071 s->coder->adjust_common_ltp(
s, cpe);
1077 if (
s->coder->encode_main_pred)
1078 s->coder->encode_main_pred(
s, &cpe->
ch[0]);
1079 if (
s->coder->encode_ltp_info)
1080 s->coder->encode_ltp_info(
s, &cpe->
ch[0], 1);
1082 if (cpe->
ms_mode) ms_mode = 1;
1085 for (ch = 0; ch < chans; ch++) {
1086 s->cur_channel = start_ch + ch;
1103 rate_bits =
FFMIN(rate_bits, 6144 *
s->channels - 3);
1104 too_many_bits =
FFMAX(target_bits, rate_bits);
1105 too_many_bits =
FFMIN(too_many_bits, 6144 *
s->channels - 3);
1106 too_few_bits =
FFMIN(
FFMAX(rate_bits - rate_bits/4, target_bits), too_many_bits);
1110 if (rate_bits < frame_bits) {
1111 float ratio = ((
float)rate_bits) / frame_bits;
1112 s->lambda *=
FFMIN(0.9
f, ratio);
1121 too_few_bits = too_few_bits - too_few_bits/8;
1122 too_many_bits = too_many_bits + too_many_bits/2;
1125 || (its < 5 && (frame_bits < too_few_bits || frame_bits > too_many_bits))
1126 || frame_bits >= 6144 *
s->channels - 3 )
1128 float ratio = ((
float)rate_bits) / frame_bits;
1130 if (frame_bits >= too_few_bits && frame_bits <= too_many_bits) {
1141 ratio =
sqrtf(ratio);
1143 s->lambda =
av_clipf(
s->lambda * ratio, FLT_EPSILON, 65536.f);
1146 if (ratio > 0.9
f && ratio < 1.1
f) {
1149 if (is_mode || ms_mode || tns_mode || pred_mode) {
1150 for (
i = 0;
i <
s->chan_map[0];
i++) {
1154 for (ch = 0; ch < chans; ch++)
1165 if (
s->options.ltp &&
s->coder->ltp_insert_new_frame)
1166 s->coder->ltp_insert_new_frame(
s);
1174 s->lambda_sum +=
s->lambda;
1182 *got_packet_ptr = 1;
1208 float scale = 32768.0f;
1215 1024, &
scale, 0)) < 0)
1218 128, &
scale, 0)) < 0)
1231 for(ch = 0; ch <
s->channels; ch++)
1232 s->planar_samples[ch] =
s->buffer.samples + 3 * 1024 * ch;
1241 const uint8_t *
sizes[2];
1246 s->last_frame_pb_count = 0;
1257 s->needs_pce =
s->options.pce;
1272 av_log(avctx,
AV_LOG_INFO,
"Using a PCE to encode channel layout \"%s\"\n", buf);
1274 s->reorder_map =
s->pce.reorder_map;
1275 s->chan_map =
s->pce.config_map;
1282 for (
i = 1;
i <=
s->chan_map[0];
i++) {
1290 for (
i = 0;
i < 16;
i++)
1293 s->samplerate_index =
i;
1297 "Unsupported sample rate %d\n", avctx->
sample_rate);
1301 "Too many bits %f > %d per frame requested, clamping to max\n",
1303 6144 *
s->channels);
1316 "Main prediction unavailable in the \"mpeg2_aac_low\" profile\n");
1318 "LTP prediction unavailable in the \"mpeg2_aac_low\" profile\n");
1320 "PNS unavailable in the \"mpeg2_aac_low\" profile, turning off\n");
1325 "Main prediction unavailable in the \"aac_ltp\" profile\n");
1327 s->options.pred = 1;
1329 "LTP prediction unavailable in the \"aac_main\" profile\n");
1330 }
else if (
s->options.ltp) {
1333 "Chainging profile to \"aac_ltp\"\n");
1335 "Main prediction unavailable in the \"aac_ltp\" profile\n");
1336 }
else if (
s->options.pred) {
1339 "Chainging profile to \"aac_main\"\n");
1341 "LTP prediction unavailable in the \"aac_main\" profile\n");
1349 "The ANMR coder is considered experimental, add -strict -2 to enable!\n");
1350 s->options.intensity_stereo = 0;
1354 "The LPT profile requires experimental compliance, add -strict -2 to enable!\n");
1357 if (
s->channels > 3)
1358 s->options.mid_side = 0;
1376 for (
i = 0;
i <
s->chan_map[0];
i++)
1379 s->chan_map[0], grouping)) < 0)
1383 s->random_state = 0x1f2e3d4c;
1392 #define AACENC_FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
1394 {
"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"},