92 .num_ele = { 1, 0, 0, 0 },
93 .pairing = { { 0 }, },
100 .num_ele = { 1, 0, 0, 0 },
101 .pairing = { { 1 }, },
104 .reorder_map = { 0, 1 },
108 .num_ele = { 1, 0, 0, 1 },
109 .pairing = { { 1 }, },
110 .index = { { 0 },{ 0 },{ 0 },{ 0 } },
112 .reorder_map = { 0, 1, 2 },
116 .num_ele = { 1, 0, 1, 0 },
117 .pairing = { { 1 },{ 0 },{ 0 } },
118 .index = { { 0 },{ 0 },{ 0 }, },
120 .reorder_map = { 0, 1, 2 },
124 .num_ele = { 2, 0, 0, 0 },
125 .pairing = { { 1, 0 }, },
126 .index = { { 0, 0 }, },
128 .reorder_map = { 0, 1, 2 },
132 .num_ele = { 2, 0, 0, 1 },
133 .pairing = { { 1, 0 }, },
134 .index = { { 0, 0 }, { 0 }, { 0 }, { 0 }, },
136 .reorder_map = { 0, 1, 2, 3 },
140 .num_ele = { 2, 0, 1, 0 },
141 .pairing = { { 1, 0 }, { 0 }, { 0 }, },
142 .index = { { 0, 0 }, { 0 }, { 1 } },
144 .reorder_map = { 0, 1, 2, 3 },
148 .num_ele = { 2, 1, 1, 0 },
149 .pairing = { { 1, 0 }, { 0 }, { 0 }, },
150 .index = { { 0, 0 }, { 1 }, { 2 }, { 0 } },
152 .reorder_map = { 0, 1, 2, 3, 4 },
156 .num_ele = { 1, 1, 0, 0 },
157 .pairing = { { 1 }, { 1 }, },
158 .index = { { 0 }, { 1 }, },
160 .reorder_map = { 0, 1, 2, 3 },
164 .num_ele = { 1, 0, 1, 0 },
165 .pairing = { { 1 }, { 0 }, { 1 }, },
166 .index = { { 0 }, { 0 }, { 1 } },
168 .reorder_map = { 0, 1, 2, 3 },
172 .num_ele = { 2, 1, 0, 0 },
173 .pairing = { { 1, 0 }, { 1 }, },
174 .index = { { 0, 0 }, { 1 } },
176 .reorder_map = { 0, 1, 2, 3, 4 },
180 .num_ele = { 2, 1, 1, 0 },
181 .pairing = { { 1, 0 }, { 0 }, { 1 }, },
182 .index = { { 0, 0 }, { 1 }, { 1 } },
184 .reorder_map = { 0, 1, 2, 3, 4, 5 },
188 .num_ele = { 2, 0, 1, 0 },
189 .pairing = { { 1, 0 }, { 0 }, { 1 } },
190 .index = { { 0, 0 }, { 0 }, { 1 } },
192 .reorder_map = { 0, 1, 2, 3, 4 },
196 .num_ele = { 2, 1, 1, 0 },
197 .pairing = { { 1, 0 }, { 0 }, { 1 }, },
198 .index = { { 0, 0 }, { 1 }, { 1 } },
200 .reorder_map = { 0, 1, 2, 3, 4, 5 },
204 .num_ele = { 2, 1, 1, 0 },
205 .pairing = { { 1, 0 }, { 1 }, { 0 }, },
206 .index = { { 0, 0 }, { 1 }, { 1 } },
208 .reorder_map = { 0, 1, 2, 3, 4, 5 },
212 .num_ele = { 2, 1, 0, 0 },
213 .pairing = { { 1, 1 }, { 1 } },
214 .index = { { 1, 0 }, { 2 }, },
216 .reorder_map = { 0, 1, 2, 3, 4, 5 },
220 .num_ele = { 2, 0, 2, 0 },
221 .pairing = { { 1, 0 },{ 0 },{ 1, 0 }, },
222 .index = { { 0, 0 },{ 0 },{ 1, 1 } },
224 .reorder_map = { 0, 1, 2, 3, 4, 5 },
228 .num_ele = { 2, 1, 2, 0 },
229 .pairing = { { 1, 0 },{ 0 },{ 1, 0 }, },
230 .index = { { 0, 0 },{ 1 },{ 1, 2 } },
232 .reorder_map = { 0, 1, 2, 3, 4, 5, 6 },
236 .num_ele = { 2, 1, 2, 0 },
237 .pairing = { { 1, 0 }, { 0 }, { 1, 0 }, },
238 .index = { { 0, 0 }, { 1 }, { 1, 2 } },
240 .reorder_map = { 0, 1, 2, 3, 4, 5, 6 },
244 .num_ele = { 2, 1, 2, 0 },
245 .pairing = { { 1, 0 }, { 0 }, { 1, 0 }, },
246 .index = { { 0, 0 }, { 1 }, { 1, 2 } },
248 .reorder_map = { 0, 1, 2, 3, 4, 5, 6 },
252 .num_ele = { 2, 1, 1, 0 },
253 .pairing = { { 1, 0 }, { 1 }, { 1 }, },
254 .index = { { 0, 0 }, { 1 }, { 2 }, },
256 .reorder_map = { 0, 1, 2, 3, 4, 5, 6 },
260 .num_ele = { 2, 1, 1, 0 },
261 .pairing = { { 1, 0 }, { 1 }, { 1 }, },
262 .index = { { 0, 0 }, { 1 }, { 2 }, },
264 .reorder_map = { 0, 1, 2, 3, 4, 5, 6 },
268 .num_ele = { 2, 1, 2, 0 },
269 .pairing = { { 1, 0 }, { 0 }, { 1, 1 }, },
270 .index = { { 0, 0 }, { 1 }, { 1, 2 }, { 0 } },
272 .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7 },
276 .num_ele = { 2, 1, 2, 0 },
277 .pairing = { { 1, 0 }, { 0 },{ 1, 1 }, },
278 .index = { { 0, 0 }, { 1 }, { 1, 2 }, { 0 } },
280 .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7 },
284 .num_ele = { 2, 1, 2, 0 },
285 .pairing = { { 1, 0 }, { 0 }, { 1, 1 }, },
286 .index = { { 0, 0 }, { 1 }, { 1, 2 }, { 0 } },
288 .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7 },
292 .num_ele = { 2, 1, 2, 0 },
293 .pairing = { { 1, 0 }, { 1 }, { 1, 0 }, },
294 .index = { { 0, 0 }, { 1 }, { 2, 1 } },
296 .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7 },
301 .num_ele = { 2, 2, 2, 0 },
302 .pairing = { { 1, 0 }, { 1, 0 }, { 1, 0 }, },
303 .index = { { 0, 0 }, { 1, 1 }, { 2, 2 } },
305 .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7, 8 },
311 .num_ele = { 2, 2, 2, 0 },
312 .pairing = { { 1, 1 }, { 1, 0 }, { 1, 0 }, },
313 .index = { { 0, 1 }, { 2, 0 }, { 3, 1 } },
315 .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
319 .num_ele = { 4, 2, 4, 0 },
320 .pairing = { { 1, 0, 1, 0 }, { 1, 1 }, { 1, 0, 1, 0 }, },
321 .index = { { 0, 0, 1, 1 }, { 2, 3 }, { 4, 2, 5, 3 } },
322 .config_map = { 10,
TYPE_CPE,
TYPE_SCE,
TYPE_CPE,
TYPE_SCE,
TYPE_CPE,
TYPE_CPE,
TYPE_CPE,
TYPE_SCE,
TYPE_CPE,
TYPE_SCE },
323 .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
351 for (
i = 0;
i < 4;
i++) {
352 for (j = 0; j < pce->
num_ele[
i]; j++) {
372 int channels = (!
s->needs_pce)*(
s->channels - (
s->channels == 8 ? 1 : 0));
373 const int max_size = 32;
402 ++
s->quantize_band_cost_cache_generation;
403 if (
s->quantize_band_cost_cache_generation == 0) {
404 memset(
s->quantize_band_cost_cache, 0,
sizeof(
s->quantize_band_cost_cache));
405 s->quantize_band_cost_cache_generation = 1;
409 #define WINDOW_FUNC(type) \
410 static void apply_ ##type ##_window(AVFloatDSPContext *fdsp, \
411 SingleChannelElement *sce, \
418 float *
out = sce->ret_buf;
420 fdsp->vector_fmul (
out, audio, lwindow, 1024);
421 fdsp->vector_fmul_reverse(
out + 1024, audio + 1024, pwindow, 1024);
428 float *
out = sce->ret_buf;
430 fdsp->vector_fmul(
out, audio, lwindow, 1024);
431 memcpy(
out + 1024, audio + 1024,
sizeof(
out[0]) * 448);
432 fdsp->vector_fmul_reverse(
out + 1024 + 448, audio + 1024 + 448, swindow, 128);
433 memset(
out + 1024 + 576, 0,
sizeof(
out[0]) * 448);
440 float *
out = sce->ret_buf;
442 memset(
out, 0,
sizeof(
out[0]) * 448);
443 fdsp->vector_fmul(
out + 448, audio + 448, swindow, 128);
444 memcpy(
out + 576, audio + 576,
sizeof(
out[0]) * 448);
445 fdsp->vector_fmul_reverse(
out + 1024, audio + 1024, lwindow, 1024);
452 const float *in = audio + 448;
453 float *
out = sce->ret_buf;
456 for (
w = 0;
w < 8;
w++) {
457 fdsp->vector_fmul (
out, in,
w ? pwindow : swindow, 128);
460 fdsp->vector_fmul_reverse(
out, in, swindow, 128);
467 const float *audio) = {
485 for (
i = 0;
i < 1024;
i += 128)
487 memcpy(audio, audio + 1024,
sizeof(audio[0]) * 1024);
507 for (
w = 1;
w < 8;
w++)
535 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);
573 if (msc == 0 || ics0->
max_sfb == 0)
588 int start = (
w+w2) * 128;
617 int start = (
w+w2) * 128;
648 if (
s->coder->set_special_band_scalefactors)
649 s->coder->set_special_band_scalefactors(
s, sce);
662 int off_is = 0, noise_flag = 1;
671 if (noise_flag-- > 0) {
725 s->coder->quantize_and_encode_band(
s, &
s->pb,
726 &sce->
coeffs[start + w2*128],
749 float *swb_coeffs = &sce->
coeffs[start +
w*128];
766 if (!common_window) {
768 if (
s->coder->encode_main_pred)
769 s->coder->encode_main_pred(
s, sce);
770 if (
s->coder->encode_ltp_info)
771 s->coder->encode_ltp_info(
s, sce, 0);
777 if (
s->coder->encode_tns_info)
778 s->coder->encode_tns_info(
s, sce);
789 int i, namelen, padbits;
791 namelen = strlen(
name) + 2;
799 for (
i = 0;
i < namelen - 2;
i++)
815 for (ch = 0; ch <
s->channels; ch++) {
817 memcpy(&
s->planar_samples[ch][1024], &
s->planar_samples[ch][2048], 1024 *
sizeof(
s->planar_samples[0][0]));
821 memcpy(&
s->planar_samples[ch][2048],
825 memset(&
s->planar_samples[ch][end], 0,
826 (3072 - end) *
sizeof(
s->planar_samples[0][0]));
834 float **
samples =
s->planar_samples, *samples2, *la, *overlap;
838 int i, its, ch,
w, chans,
tag, start_ch,
ret, frame_bits;
839 int target_bits, rate_bits, too_many_bits, too_few_bits;
840 int ms_mode = 0, is_mode = 0, tns_mode = 0, pred_mode = 0;
841 int chan_el_counter[4];
849 if (!
s->afq.remaining_samples || (!
s->afq.frame_alloc && !
s->afq.frame_count))
861 for (
i = 0;
i <
s->chan_map[0];
i++) {
863 tag =
s->chan_map[
i+1];
866 for (ch = 0; ch < chans; ch++) {
868 float clip_avoidance_factor;
871 s->cur_channel = start_ch + ch;
872 overlap = &
samples[
s->cur_channel][0];
873 samples2 = overlap + 1024;
874 la = samples2 + (448+64);
888 ics->
num_swb =
s->samplerate_index >= 8 ? 1 : 3;
890 wi[ch] =
s->psy.model->window(&
s->psy, samples2, la,
s->cur_channel,
912 clip_avoidance_factor = 0.0f;
914 const float *wbuf = overlap +
w * 128;
919 for (j = 0; j < wlen; j++)
926 clip_avoidance_factor =
FFMAX(clip_avoidance_factor, wi[ch].clipping[
w]);
939 if (
s->options.ltp &&
s->coder->update_ltp) {
940 s->coder->update_ltp(
s, sce);
945 for (k = 0; k < 1024; k++) {
957 frame_bits = its = 0;
965 memset(chan_el_counter, 0,
sizeof(chan_el_counter));
966 for (
i = 0;
i <
s->chan_map[0];
i++) {
968 const float *coeffs[2];
969 tag =
s->chan_map[
i+1];
977 for (ch = 0; ch < chans; ch++) {
985 for (
w = 0;
w < 128;
w++)
989 s->psy.bitres.alloc = -1;
990 s->psy.bitres.bits =
s->last_frame_pb_count /
s->channels;
991 s->psy.model->analyze(&
s->psy, start_ch, coeffs, wi);
992 if (
s->psy.bitres.alloc > 0) {
994 target_bits +=
s->psy.bitres.alloc
996 s->psy.bitres.alloc /= chans;
999 for (ch = 0; ch < chans; ch++) {
1000 s->cur_channel = start_ch + ch;
1001 if (
s->options.pns &&
s->coder->mark_pns)
1002 s->coder->mark_pns(
s, avctx, &cpe->
ch[ch]);
1003 s->coder->search_for_quantizers(avctx,
s, &cpe->
ch[ch],
s->lambda);
1006 && wi[0].window_type[0] == wi[1].window_type[0]
1007 && wi[0].window_shape == wi[1].window_shape) {
1011 if (wi[0].grouping[
w] != wi[1].grouping[
w]) {
1017 for (ch = 0; ch < chans; ch++) {
1019 s->cur_channel = start_ch + ch;
1020 if (
s->options.tns &&
s->coder->search_for_tns)
1021 s->coder->search_for_tns(
s, sce);
1022 if (
s->options.tns &&
s->coder->apply_tns_filt)
1023 s->coder->apply_tns_filt(
s, sce);
1026 if (
s->options.pns &&
s->coder->search_for_pns)
1027 s->coder->search_for_pns(
s, avctx, sce);
1029 s->cur_channel = start_ch;
1030 if (
s->options.intensity_stereo) {
1031 if (
s->coder->search_for_is)
1032 s->coder->search_for_is(
s, avctx, cpe);
1033 if (cpe->
is_mode) is_mode = 1;
1036 if (
s->options.pred) {
1037 for (ch = 0; ch < chans; ch++) {
1039 s->cur_channel = start_ch + ch;
1040 if (
s->options.pred &&
s->coder->search_for_pred)
1041 s->coder->search_for_pred(
s, sce);
1044 if (
s->coder->adjust_common_pred)
1045 s->coder->adjust_common_pred(
s, cpe);
1046 for (ch = 0; ch < chans; ch++) {
1048 s->cur_channel = start_ch + ch;
1049 if (
s->options.pred &&
s->coder->apply_main_pred)
1050 s->coder->apply_main_pred(
s, sce);
1052 s->cur_channel = start_ch;
1054 if (
s->options.mid_side) {
1055 if (
s->options.mid_side == -1 &&
s->coder->search_for_ms)
1056 s->coder->search_for_ms(
s, cpe);
1062 if (
s->options.ltp) {
1063 for (ch = 0; ch < chans; ch++) {
1065 s->cur_channel = start_ch + ch;
1066 if (
s->coder->search_for_ltp)
1070 s->cur_channel = start_ch;
1071 if (
s->coder->adjust_common_ltp)
1072 s->coder->adjust_common_ltp(
s, cpe);
1078 if (
s->coder->encode_main_pred)
1079 s->coder->encode_main_pred(
s, &cpe->
ch[0]);
1080 if (
s->coder->encode_ltp_info)
1081 s->coder->encode_ltp_info(
s, &cpe->
ch[0], 1);
1083 if (cpe->
ms_mode) ms_mode = 1;
1086 for (ch = 0; ch < chans; ch++) {
1087 s->cur_channel = start_ch + ch;
1104 rate_bits =
FFMIN(rate_bits, 6144 *
s->channels - 3);
1105 too_many_bits =
FFMAX(target_bits, rate_bits);
1106 too_many_bits =
FFMIN(too_many_bits, 6144 *
s->channels - 3);
1107 too_few_bits =
FFMIN(
FFMAX(rate_bits - rate_bits/4, target_bits), too_many_bits);
1111 if (rate_bits < frame_bits) {
1112 float ratio = ((
float)rate_bits) / frame_bits;
1113 s->lambda *=
FFMIN(0.9
f, ratio);
1122 too_few_bits = too_few_bits - too_few_bits/8;
1123 too_many_bits = too_many_bits + too_many_bits/2;
1126 || (its < 5 && (frame_bits < too_few_bits || frame_bits > too_many_bits))
1127 || frame_bits >= 6144 *
s->channels - 3 )
1129 float ratio = ((
float)rate_bits) / frame_bits;
1131 if (frame_bits >= too_few_bits && frame_bits <= too_many_bits) {
1142 ratio =
sqrtf(ratio);
1144 s->lambda =
av_clipf(
s->lambda * ratio, FLT_EPSILON, 65536.f);
1147 if (ratio > 0.9
f && ratio < 1.1
f) {
1150 if (is_mode || ms_mode || tns_mode || pred_mode) {
1151 for (
i = 0;
i <
s->chan_map[0];
i++) {
1155 for (ch = 0; ch < chans; ch++)
1166 if (
s->options.ltp &&
s->coder->ltp_insert_new_frame)
1167 s->coder->ltp_insert_new_frame(
s);
1175 s->lambda_sum +=
s->lambda;
1181 *got_packet_ptr = 1;
1207 float scale = 32768.0f;
1217 1024, &
scale, 0)) < 0)
1220 128, &
scale, 0)) < 0)
1233 for(ch = 0; ch <
s->channels; ch++)
1234 s->planar_samples[ch] =
s->buffer.samples + 3 * 1024 * ch;
1243 const uint8_t *
sizes[2];
1248 s->last_frame_pb_count = 0;
1259 s->needs_pce =
s->options.pce;
1274 av_log(avctx,
AV_LOG_INFO,
"Using a PCE to encode channel layout \"%s\"\n", buf);
1276 s->reorder_map =
s->pce.reorder_map;
1277 s->chan_map =
s->pce.config_map;
1284 for (
i = 1;
i <=
s->chan_map[0];
i++) {
1292 for (
i = 0;
i < 16;
i++)
1295 s->samplerate_index =
i;
1299 "Unsupported sample rate %d\n", avctx->
sample_rate);
1303 "Too many bits %f > %d per frame requested, clamping to max\n",
1305 6144 *
s->channels);
1318 "Main prediction unavailable in the \"mpeg2_aac_low\" profile\n");
1320 "LTP prediction unavailable in the \"mpeg2_aac_low\" profile\n");
1322 "PNS unavailable in the \"mpeg2_aac_low\" profile, turning off\n");
1327 "Main prediction unavailable in the \"aac_ltp\" profile\n");
1329 s->options.pred = 1;
1331 "LTP prediction unavailable in the \"aac_main\" profile\n");
1332 }
else if (
s->options.ltp) {
1335 "Chainging profile to \"aac_ltp\"\n");
1337 "Main prediction unavailable in the \"aac_ltp\" profile\n");
1338 }
else if (
s->options.pred) {
1341 "Chainging profile to \"aac_main\"\n");
1343 "LTP prediction unavailable in the \"aac_main\" profile\n");
1351 "The ANMR coder is considered experimental, add -strict -2 to enable!\n");
1352 s->options.intensity_stereo = 0;
1356 "The LPT profile requires experimental compliance, add -strict -2 to enable!\n");
1359 if (
s->channels > 3)
1360 s->options.mid_side = 0;
1375 for (
i = 0;
i <
s->chan_map[0];
i++)
1378 s->chan_map[0], grouping)) < 0)
1382 s->random_state = 0x1f2e3d4c;
1401 #define AACENC_FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
1403 {
"aac_coder",
"Coding algorithm", offsetof(
AACEncContext,
options.coder),
AV_OPT_TYPE_INT, {.i64 =
AAC_CODER_TWOLOOP}, 0,
AAC_CODER_NB-1,
AACENC_FLAGS,
"coder"},