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++) {
542 for (cmaxsfb = ics->
num_swb; cmaxsfb > 0 && cpe->
ch[ch].
zeroes[
w*16+cmaxsfb-1]; cmaxsfb--)
544 maxsfb =
FFMAX(maxsfb, cmaxsfb);
574 if (msc == 0 || ics0->
max_sfb == 0)
589 int start = (
w+w2) * 128;
618 int start = (
w+w2) * 128;
649 if (
s->coder->set_special_band_scalefactors)
650 s->coder->set_special_band_scalefactors(
s, sce);
663 int off_is = 0, noise_flag = 1;
672 if (noise_flag-- > 0) {
726 s->coder->quantize_and_encode_band(
s, &
s->pb,
727 &sce->
coeffs[start + w2*128],
750 float *swb_coeffs = &sce->
coeffs[start +
w*128];
767 if (!common_window) {
769 if (
s->coder->encode_main_pred)
770 s->coder->encode_main_pred(
s, sce);
771 if (
s->coder->encode_ltp_info)
772 s->coder->encode_ltp_info(
s, sce, 0);
778 if (
s->coder->encode_tns_info)
779 s->coder->encode_tns_info(
s, sce);
790 int i, namelen, padbits;
792 namelen = strlen(
name) + 2;
800 for (
i = 0;
i < namelen - 2;
i++)
812 int end = 2048 + (
frame ?
frame->nb_samples : 0);
816 for (ch = 0; ch <
s->channels; ch++) {
818 memcpy(&
s->planar_samples[ch][1024], &
s->planar_samples[ch][2048], 1024 *
sizeof(
s->planar_samples[0][0]));
822 memcpy(&
s->planar_samples[ch][2048],
824 frame->nb_samples *
sizeof(
s->planar_samples[0][0]));
826 memset(&
s->planar_samples[ch][end], 0,
827 (3072 - end) *
sizeof(
s->planar_samples[0][0]));
835 float **
samples =
s->planar_samples, *samples2, *la, *overlap;
839 int i, its, ch,
w, chans,
tag, start_ch,
ret, frame_bits;
840 int target_bits, rate_bits, too_many_bits, too_few_bits;
841 int ms_mode = 0, is_mode = 0, tns_mode = 0, pred_mode = 0;
842 int chan_el_counter[4];
850 if (!
s->afq.remaining_samples || (!
s->afq.frame_alloc && !
s->afq.frame_count))
862 for (
i = 0;
i <
s->chan_map[0];
i++) {
864 tag =
s->chan_map[
i+1];
867 for (ch = 0; ch < chans; ch++) {
869 float clip_avoidance_factor;
872 s->cur_channel = start_ch + ch;
873 overlap = &
samples[
s->cur_channel][0];
874 samples2 = overlap + 1024;
875 la = samples2 + (448+64);
889 ics->
num_swb =
s->samplerate_index >= 8 ? 1 : 3;
891 wi[ch] =
s->psy.model->window(&
s->psy, samples2, la,
s->cur_channel,
913 clip_avoidance_factor = 0.0f;
915 const float *wbuf = overlap +
w * 128;
920 for (j = 0; j < wlen; j++)
927 clip_avoidance_factor =
FFMAX(clip_avoidance_factor, wi[ch].clipping[
w]);
940 if (
s->options.ltp &&
s->coder->update_ltp) {
941 s->coder->update_ltp(
s, sce);
946 for (k = 0; k < 1024; k++) {
958 frame_bits = its = 0;
966 memset(chan_el_counter, 0,
sizeof(chan_el_counter));
967 for (
i = 0;
i <
s->chan_map[0];
i++) {
969 const float *coeffs[2];
970 tag =
s->chan_map[
i+1];
978 for (ch = 0; ch < chans; ch++) {
986 for (
w = 0;
w < 128;
w++)
990 s->psy.bitres.alloc = -1;
991 s->psy.bitres.bits =
s->last_frame_pb_count /
s->channels;
992 s->psy.model->analyze(&
s->psy, start_ch, coeffs, wi);
993 if (
s->psy.bitres.alloc > 0) {
995 target_bits +=
s->psy.bitres.alloc
997 s->psy.bitres.alloc /= chans;
1000 for (ch = 0; ch < chans; ch++) {
1001 s->cur_channel = start_ch + ch;
1002 if (
s->options.pns &&
s->coder->mark_pns)
1003 s->coder->mark_pns(
s, avctx, &cpe->
ch[ch]);
1004 s->coder->search_for_quantizers(avctx,
s, &cpe->
ch[ch],
s->lambda);
1007 && wi[0].window_type[0] == wi[1].window_type[0]
1008 && wi[0].window_shape == wi[1].window_shape) {
1012 if (wi[0].grouping[
w] != wi[1].grouping[
w]) {
1018 for (ch = 0; ch < chans; ch++) {
1020 s->cur_channel = start_ch + ch;
1021 if (
s->options.tns &&
s->coder->search_for_tns)
1022 s->coder->search_for_tns(
s, sce);
1023 if (
s->options.tns &&
s->coder->apply_tns_filt)
1024 s->coder->apply_tns_filt(
s, sce);
1027 if (
s->options.pns &&
s->coder->search_for_pns)
1028 s->coder->search_for_pns(
s, avctx, sce);
1030 s->cur_channel = start_ch;
1031 if (
s->options.intensity_stereo) {
1032 if (
s->coder->search_for_is)
1033 s->coder->search_for_is(
s, avctx, cpe);
1034 if (cpe->
is_mode) is_mode = 1;
1037 if (
s->options.pred) {
1038 for (ch = 0; ch < chans; ch++) {
1040 s->cur_channel = start_ch + ch;
1041 if (
s->options.pred &&
s->coder->search_for_pred)
1042 s->coder->search_for_pred(
s, sce);
1045 if (
s->coder->adjust_common_pred)
1046 s->coder->adjust_common_pred(
s, cpe);
1047 for (ch = 0; ch < chans; ch++) {
1049 s->cur_channel = start_ch + ch;
1050 if (
s->options.pred &&
s->coder->apply_main_pred)
1051 s->coder->apply_main_pred(
s, sce);
1053 s->cur_channel = start_ch;
1055 if (
s->options.mid_side) {
1056 if (
s->options.mid_side == -1 &&
s->coder->search_for_ms)
1057 s->coder->search_for_ms(
s, cpe);
1063 if (
s->options.ltp) {
1064 for (ch = 0; ch < chans; ch++) {
1066 s->cur_channel = start_ch + ch;
1067 if (
s->coder->search_for_ltp)
1071 s->cur_channel = start_ch;
1072 if (
s->coder->adjust_common_ltp)
1073 s->coder->adjust_common_ltp(
s, cpe);
1079 if (
s->coder->encode_main_pred)
1080 s->coder->encode_main_pred(
s, &cpe->
ch[0]);
1081 if (
s->coder->encode_ltp_info)
1082 s->coder->encode_ltp_info(
s, &cpe->
ch[0], 1);
1084 if (cpe->
ms_mode) ms_mode = 1;
1087 for (ch = 0; ch < chans; ch++) {
1088 s->cur_channel = start_ch + ch;
1105 rate_bits =
FFMIN(rate_bits, 6144 *
s->channels - 3);
1106 too_many_bits =
FFMAX(target_bits, rate_bits);
1107 too_many_bits =
FFMIN(too_many_bits, 6144 *
s->channels - 3);
1108 too_few_bits =
FFMIN(
FFMAX(rate_bits - rate_bits/4, target_bits), too_many_bits);
1112 if (rate_bits < frame_bits) {
1113 float ratio = ((
float)rate_bits) / frame_bits;
1114 s->lambda *=
FFMIN(0.9
f, ratio);
1123 too_few_bits = too_few_bits - too_few_bits/8;
1124 too_many_bits = too_many_bits + too_many_bits/2;
1127 || (its < 5 && (frame_bits < too_few_bits || frame_bits > too_many_bits))
1128 || frame_bits >= 6144 *
s->channels - 3 )
1130 float ratio = ((
float)rate_bits) / frame_bits;
1132 if (frame_bits >= too_few_bits && frame_bits <= too_many_bits) {
1143 ratio =
sqrtf(ratio);
1145 s->lambda =
av_clipf(
s->lambda * ratio, FLT_EPSILON, 65536.f);
1148 if (ratio > 0.9
f && ratio < 1.1
f) {
1151 if (is_mode || ms_mode || tns_mode || pred_mode) {
1152 for (
i = 0;
i <
s->chan_map[0];
i++) {
1156 for (ch = 0; ch < chans; ch++)
1167 if (
s->options.ltp &&
s->coder->ltp_insert_new_frame)
1168 s->coder->ltp_insert_new_frame(
s);
1176 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"},