1 /*
2 * AAC encoder
3 * Copyright (C) 2008 Konstantin Shishkov
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * AAC encoder
25 */
26
27 /***********************************
28 * TODOs:
29 * add sane pulse detection
30 ***********************************/
32
46
52
54
55 /**
56 * List of PCE (Program Configuration Element) for the channel layouts listed
57 * in channel_layout.h
58 *
59 * For those wishing in the future to add other layouts:
60 *
61 * - num_ele: number of elements in each group of front, side, back, lfe channels
62 * (an element is of type SCE (single channel), CPE (channel pair) for
63 * the first 3 groups; and is LFE for LFE group).
64 *
65 * - pairing: 0 for an SCE element or 1 for a CPE; does not apply to LFE group
66 *
67 * - index: there are three independent indices for SCE, CPE and LFE;
68 * they are incremented irrespective of the group to which the element belongs;
69 * they are not reset when going from one group to another
70 *
71 * Example: for 7.0 channel layout,
72 * .pairing = { { 1, 0 }, { 1 }, { 1 }, }, (3 CPE and 1 SCE in front group)
73 * .index = { { 0, 0 }, { 1 }, { 2 }, },
74 * (index is 0 for the single SCE but goes from 0 to 2 for the CPEs)
75 *
76 * The index order impacts the channel ordering. But is otherwise arbitrary
77 * (the sequence could have been 2, 0, 1 instead of 0, 1, 2).
78 *
79 * Spec allows for discontinuous indices, e.g. if one has a total of two SCE,
80 * SCE.0 SCE.15 is OK per spec; BUT it won't be decoded by our AAC decoder
81 * which at this time requires that indices fully cover some range starting
82 * from 0 (SCE.1 SCE.0 is OK but not SCE.0 SCE.15).
83 *
84 * - config_map: total number of elements and their types. Beware, the way the
85 * types are ordered impacts the final channel ordering.
86 *
87 * - reorder_map: reorders the channels.
88 *
89 */
91 {
93 .num_ele = { 1, 0, 0, 0 },
94 .pairing = { { 0 }, },
95 .index = { { 0 }, },
97 .reorder_map = { 0 },
98 },
99 {
101 .num_ele = { 1, 0, 0, 0 },
102 .pairing = { { 1 }, },
103 .index = { { 0 }, },
105 .reorder_map = { 0, 1 },
106 },
107 {
109 .num_ele = { 1, 0, 0, 1 },
110 .pairing = { { 1 }, },
111 .index = { { 0 },{ 0 },{ 0 },{ 0 } },
113 .reorder_map = { 0, 1, 2 },
114 },
115 {
117 .num_ele = { 1, 0, 1, 0 },
118 .pairing = { { 1 },{ 0 },{ 0 } },
119 .index = { { 0 },{ 0 },{ 0 }, },
121 .reorder_map = { 0, 1, 2 },
122 },
123 {
125 .num_ele = { 2, 0, 0, 0 },
126 .pairing = { { 1, 0 }, },
127 .index = { { 0, 0 }, },
129 .reorder_map = { 0, 1, 2 },
130 },
131 {
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 },
138 },
139 {
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 },
146 },
147 {
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 },
154 },
155 {
157 .num_ele = { 1, 1, 0, 0 },
158 .pairing = { { 1 }, { 1 }, },
159 .index = { { 0 }, { 1 }, },
161 .reorder_map = { 0, 1, 2, 3 },
162 },
163 {
165 .num_ele = { 1, 0, 1, 0 },
166 .pairing = { { 1 }, { 0 }, { 1 }, },
167 .index = { { 0 }, { 0 }, { 1 } },
169 .reorder_map = { 0, 1, 2, 3 },
170 },
171 {
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 },
178 },
179 {
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 },
186 },
187 {
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 },
194 },
195 {
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 },
202 },
203 {
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 },
210 },
211 {
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 },
218 },
219 {
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 },
226 },
227 {
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 },
234 },
235 {
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 },
242 },
243 {
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 },
250 },
251 {
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 },
258 },
259 {
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 },
266 },
267 {
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 },
274 },
275 {
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 },
282 },
283 {
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 },
290 },
291 {
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 },
298 },
299 { /* Meant for order 2/mixed ambisonics */
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 },
307 },
308 { /* Meant for order 2/mixed ambisonics */
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 },
317 },
318 {
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 },
325 },
326 };
327
329 {
335
337
340
345 put_bits(pb, 3, 0);
/* Assoc data */
347
348 put_bits(pb, 1, 0);
/* Stereo mixdown */
349 put_bits(pb, 1, 0);
/* Mono mixdown */
350 put_bits(pb, 1, 0);
/* Something else */
351
352 for (
i = 0;
i < 4;
i++) {
353 for (j = 0; j < pce->
num_ele[
i]; j++) {
357 }
358 }
359
363 }
364
365 /**
366 * Make AAC audio config object.
367 * @see 1.6.2.1 "Syntax - AudioSpecificConfig"
368 */
370 {
373 int channels = (!
s->needs_pce)*(
s->channels - (
s->channels == 8 ? 1 : 0));
374 const int max_size = 32;
375
379
381 put_bits(&pb, 5,
s->profile+1);
//profile
382 put_bits(&pb, 4,
s->samplerate_index);
//sample rate index
384 //GASpecificConfig
385 put_bits(&pb, 1, 0);
//frame length - 1024 samples
386 put_bits(&pb, 1, 0);
//does not depend on core coder
387 put_bits(&pb, 1, 0);
//is not extension
390
391 //Explicitly Mark SBR absent
392 put_bits(&pb, 11, 0x2b7);
//sync extension
397
398 return 0;
399 }
400
402 {
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;
407 }
408 }
409
410 #define WINDOW_FUNC(type) \
411 static void apply_ ##type ##_window(AVFloatDSPContext *fdsp, \
412 SingleChannelElement *sce, \
413 const float *audio)
414
416 {
419 float *
out = sce->ret_buf;
420
421 fdsp->vector_fmul (
out, audio, lwindow, 1024);
422 fdsp->vector_fmul_reverse(
out + 1024, audio + 1024, pwindow, 1024);
423 }
424
426 {
429 float *
out = sce->ret_buf;
430
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);
435 }
436
438 {
441 float *
out = sce->ret_buf;
442
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);
447 }
448
450 {
453 const float *in = audio + 448;
454 float *
out = sce->ret_buf;
456
457 for (
w = 0;
w < 8;
w++) {
458 fdsp->vector_fmul (
out, in,
w ? pwindow : swindow, 128);
460 in += 128;
461 fdsp->vector_fmul_reverse(
out, in, swindow, 128);
463 }
464 }
465
468 const float *audio) = {
473 };
474
476 float *audio)
477 {
480
482
485 else
486 for (
i = 0;
i < 1024;
i += 128)
488 memcpy(audio, audio + 1024, sizeof(audio[0]) * 1024);
490 }
491
492 /**
493 * Encode ics_info element.
494 * @see Table 4.6 (syntax of ics_info)
495 */
497 {
499
500 put_bits(&
s->pb, 1, 0);
// ics_reserved bit
505 put_bits(&
s->pb, 1, 0);
/* No predictor present */
506 } else {
508 for (
w = 1;
w < 8;
w++)
510 }
511 }
512
513 /**
514 * Encode MS data.
515 * @see 4.6.8.1 "Joint Coding - M/S Stereo"
516 */
518 {
520
526 }
527
528 /**
529 * Produce integer coefficients from scalefactors provided by the model.
530 */
532 {
534 int maxsfb, cmaxsfb;
535
536 for (ch = 0; ch < chans; ch++) {
538 maxsfb = 0;
541 for (cmaxsfb = ics->
num_swb; cmaxsfb > 0 && cpe->
ch[ch].
zeroes[
w*16+cmaxsfb-1]; cmaxsfb--)
542 ;
543 maxsfb =
FFMAX(maxsfb, cmaxsfb);
544 }
546
547 //adjust zero bands for window groups
554 break;
555 }
556 }
558 }
559 }
560 }
561
565 int msc = 0;
571 msc++;
572 if (msc == 0 || ics0->
max_sfb == 0)
574 else
576 }
577 }
578
580 {
584 return;
587 int start = (
w+w2) * 128;
593 continue;
594 }
601 }
603 }
604 }
605 }
606 }
607
609 {
613 return;
616 int start = (
w+w2) * 128;
618 /* ms_mask can be used for other purposes in PNS and I/S,
619 * so must not apply M/S if any band uses either, even if
620 * ms_mask is set.
621 */
626 continue;
627 }
633 }
635 }
636 }
637 }
638 }
639
640 /**
641 * Encode scalefactor band coding type.
642 */
644 {
646
647 if (
s->coder->set_special_band_scalefactors)
648 s->coder->set_special_band_scalefactors(
s, sce);
649
652 }
653
654 /**
655 * Encode scalefactors.
656 */
659 {
661 int off_is = 0, noise_flag = 1;
663
670 if (noise_flag-- > 0) {
672 continue;
673 }
678 } else {
681 }
685 }
686 }
687 }
688 }
689
690 /**
691 * Encode pulse data.
692 */
694 {
696
699 return;
700
706 }
707 }
708
709 /**
710 * Encode spectral coefficients processed by psychoacoustic model.
711 */
713 {
715
717 start = 0;
721 continue;
722 }
724 s->coder->quantize_and_encode_band(
s, &
s->pb,
725 &sce->
coeffs[start + w2*128],
731 }
733 }
734 }
735 }
736
737 /**
738 * Downscale spectral coefficients for near-clipping windows to avoid artifacts
739 */
741 {
743
746 start = 0;
748 float *swb_coeffs = &sce->
coeffs[start +
w*128];
752 }
753 }
754 }
755 }
756
757 /**
758 * Encode one channel of audio data.
759 */
762 int common_window)
763 {
765 if (!common_window)
771 if (
s->coder->encode_tns_info)
772 s->coder->encode_tns_info(
s, sce);
775 return 0;
776 }
777
778 /**
779 * Write some auxiliary information about the created AAC file.
780 */
782 {
783 int i, namelen, padbits;
784
785 namelen = strlen(
name) + 2;
788 if (namelen >= 15)
790 put_bits(&
s->pb, 4, 0);
//extension type - filler
793 for (
i = 0;
i < namelen - 2;
i++)
796 }
797
798 /*
799 * Copy input samples.
800 * Channels are reordered from libavcodec's default order to AAC order.
801 */
803 {
804 int ch;
805 int end = 2048 + (
frame ?
frame->nb_samples : 0);
807
808 /* copy and remap input samples */
809 for (ch = 0; ch <
s->channels; ch++) {
810 /* copy last 1024 samples of previous frame to the start of the current frame */
811 memcpy(&
s->planar_samples[ch][1024], &
s->planar_samples[ch][2048], 1024 *
sizeof(
s->planar_samples[0][0]));
812
813 /* copy new samples and zero any remaining samples */
815 memcpy(&
s->planar_samples[ch][2048],
817 frame->nb_samples *
sizeof(
s->planar_samples[0][0]));
818 }
819 memset(&
s->planar_samples[ch][end], 0,
820 (3072 - end) *
sizeof(
s->planar_samples[0][0]));
821 }
822 }
823
826 {
828 float **
samples =
s->planar_samples, *samples2, *la, *overlap;
832 int i, its, ch,
w, chans,
tag, start_ch,
ret, frame_bits;
833 int target_bits, rate_bits, too_many_bits, too_few_bits;
834 int ms_mode = 0, is_mode = 0, tns_mode = 0, pred_mode = 0;
835 int chan_el_counter[4];
837
838 /* add current frame to queue */
842 } else {
843 if (!
s->afq.remaining_samples || (!
s->afq.frame_alloc && !
s->afq.frame_count))
844 return 0;
845 }
846
848
850 return 0;
851
852 start_ch = 0;
853 for (
i = 0;
i <
s->chan_map[0];
i++) {
855 tag =
s->chan_map[
i+1];
858 for (ch = 0; ch < chans; ch++) {
859 int k;
860 float clip_avoidance_factor;
863 s->cur_channel = start_ch + ch;
864 overlap = &
samples[
s->cur_channel][0];
865 samples2 = overlap + 1024;
866 la = samples2 + (448+64);
875
876 /* Only the lowest 12 coefficients are used in a LFE channel.
877 * The expression below results in only the bottom 8 coefficients
878 * being used for 11.025kHz to 16kHz sample rates.
879 */
880 ics->
num_swb =
s->samplerate_index >= 8 ? 1 : 3;
881 } else {
882 wi[ch] =
s->psy.model->window(&
s->psy, samples2, la,
s->cur_channel,
884 }
899
902
903 /* Calculate input sample maximums and evaluate clipping risk */
904 clip_avoidance_factor = 0.0f;
906 const float *wbuf = overlap +
w * 128;
909 int j;
910 /* mdct input is 2 * output */
911 for (j = 0; j < wlen; j++)
914 }
918 clip_avoidance_factor =
FFMAX(clip_avoidance_factor, wi[ch].clipping[
w]);
919 } else {
921 }
922 }
925 } else {
927 }
928
930
931 for (k = 0; k < 1024; k++) {
932 if (!(
fabs(cpe->
ch[ch].
coeffs[k]) < 1E16)) {
// Ensure headroom for energy calculation
935 }
936 }
938 }
939 start_ch += chans;
940 }
943 frame_bits = its = 0;
944 do {
946
949 start_ch = 0;
950 target_bits = 0;
951 memset(chan_el_counter, 0, sizeof(chan_el_counter));
952 for (
i = 0;
i <
s->chan_map[0];
i++) {
954 const float *coeffs[2];
955 tag =
s->chan_map[
i+1];
963 for (ch = 0; ch < chans; ch++) {
967 for (
w = 0;
w < 128;
w++)
970 }
971 s->psy.bitres.alloc = -1;
972 s->psy.bitres.bits =
s->last_frame_pb_count /
s->channels;
973 s->psy.model->analyze(&
s->psy, start_ch, coeffs, wi);
974 if (
s->psy.bitres.alloc > 0) {
975 /* Lambda unused here on purpose, we need to take psy's unscaled allocation */
976 target_bits +=
s->psy.bitres.alloc
978 s->psy.bitres.alloc /= chans;
979 }
981 for (ch = 0; ch < chans; ch++) {
982 s->cur_channel = start_ch + ch;
983 if (
s->options.pns &&
s->coder->mark_pns)
984 s->coder->mark_pns(
s, avctx, &cpe->
ch[ch]);
985 s->coder->search_for_quantizers(avctx,
s, &cpe->
ch[ch],
s->lambda);
986 }
987 if (chans > 1
988 && wi[0].window_type[0] == wi[1].window_type[0]
989 && wi[0].window_shape == wi[1].window_shape) {
990
993 if (wi[0].grouping[
w] != wi[1].grouping[
w]) {
995 break;
996 }
997 }
998 }
999 for (ch = 0; ch < chans; ch++) { /* TNS and PNS */
1001 s->cur_channel = start_ch + ch;
1002 if (
s->options.tns &&
s->coder->search_for_tns)
1003 s->coder->search_for_tns(
s, sce);
1004 if (
s->options.tns &&
s->coder->apply_tns_filt)
1005 s->coder->apply_tns_filt(
s, sce);
1007 tns_mode = 1;
1008 if (
s->options.pns &&
s->coder->search_for_pns)
1009 s->coder->search_for_pns(
s, avctx, sce);
1010 }
1011 s->cur_channel = start_ch;
1012 if (
s->options.intensity_stereo) {
/* Intensity Stereo */
1013 if (
s->coder->search_for_is)
1014 s->coder->search_for_is(
s, avctx, cpe);
1015 if (cpe->
is_mode) is_mode = 1;
1017 }
1018 if (
s->options.mid_side) {
/* Mid/Side stereo */
1019 if (
s->options.mid_side == -1 &&
s->coder->search_for_ms)
1020 s->coder->search_for_ms(
s, cpe);
1024 }
1026 if (chans == 2) {
1031 if (cpe->
ms_mode) ms_mode = 1;
1032 }
1033 }
1034 for (ch = 0; ch < chans; ch++) {
1035 s->cur_channel = start_ch + ch;
1037 }
1038 start_ch += chans;
1039 }
1040
1042 /* When using a constant Q-scale, don't mess with lambda */
1043 break;
1044 }
1045
1046 /* rate control stuff
1047 * allow between the nominal bitrate, and what psy's bit reservoir says to target
1048 * but drift towards the nominal bitrate always
1049 */
1052 rate_bits =
FFMIN(rate_bits, 6144 *
s->channels - 3);
1053 too_many_bits =
FFMAX(target_bits, rate_bits);
1054 too_many_bits =
FFMIN(too_many_bits, 6144 *
s->channels - 3);
1055 too_few_bits =
FFMIN(
FFMAX(rate_bits - rate_bits/4, target_bits), too_many_bits);
1056
1057 /* When strict bit-rate control is demanded */
1059 if (rate_bits < frame_bits) {
1060 float ratio = ((
float)rate_bits) / frame_bits;
1061 s->lambda *=
FFMIN(0.9
f, ratio);
1062 continue;
1063 }
1064 /* reset lambda when solution is found */
1066 break;
1067 }
1068
1069 /* When using ABR, be strict (but only for increasing) */
1070 too_few_bits = too_few_bits - too_few_bits/8;
1071 too_many_bits = too_many_bits + too_many_bits/2;
1072
1073 if ( its == 0 /* for steady-state Q-scale tracking */
1074 || (its < 5 && (frame_bits < too_few_bits || frame_bits > too_many_bits))
1075 || frame_bits >= 6144 *
s->channels - 3 )
1076 {
1077 float ratio = ((
float)rate_bits) / frame_bits;
1078
1079 if (frame_bits >= too_few_bits && frame_bits <= too_many_bits) {
1080 /*
1081 * This path is for steady-state Q-scale tracking
1082 * When frame bits fall within the stable range, we still need to adjust
1083 * lambda to maintain it like so in a stable fashion (large jumps in lambda
1084 * create artifacts and should be avoided), but slowly
1085 */
1088 } else {
1089 /* Not so fast though */
1090 ratio =
sqrtf(ratio);
1091 }
1092 s->lambda =
av_clipf(
s->lambda * ratio, FLT_EPSILON, 65536.f);
1093
1094 /* Keep iterating if we must reduce and lambda is in the sky */
1095 if (ratio > 0.9
f && ratio < 1.1
f) {
1096 break;
1097 } else {
1098 if (is_mode || ms_mode || tns_mode || pred_mode) {
1099 for (
i = 0;
i <
s->chan_map[0];
i++) {
1100 // Must restore coeffs
1103 for (ch = 0; ch < chans; ch++)
1105 }
1106 }
1107 its++;
1108 }
1109 } else {
1110 break;
1111 }
1112 } while (1);
1113
1116
1119
1120 s->lambda_sum +=
s->lambda;
1122
1125
1127
1128 *got_packet_ptr = 1;
1129 return 0;
1130 }
1131
1133 {
1135
1137
1146 return 0;
1147 }
1148
1150 {
1152 float scale = 32768.0f;
1153
1157
1159 1024, &
scale, 0)) < 0)
1162 128, &
scale, 0)) < 0)
1164
1165 return 0;
1166 }
1167
1169 {
1170 int ch;
1174
1175 for(ch = 0; ch <
s->channels; ch++)
1176 s->planar_samples[ch] =
s->buffer.samples + 3 * 1024 * ch;
1177
1178 return 0;
1179 }
1180
1182 {
1185 const uint8_t *
sizes[2];
1187 int lengths[2];
1188
1189 /* Constants */
1190 s->last_frame_pb_count = 0;
1194
1195 /* Channel map and unspecified bitrate guessing */
1197
1201 s->needs_pce =
s->options.pce;
1202 break;
1203 }
1204 }
1205
1207 char buf[64];
1210 break;
1215 }
1216 av_log(avctx,
AV_LOG_INFO,
"Using a PCE to encode channel layout \"%s\"\n", buf);
1218 s->reorder_map =
s->pce.reorder_map;
1219 s->chan_map =
s->pce.config_map;
1220 } else {
1223 }
1224
1226 for (
i = 1;
i <=
s->chan_map[0];
i++) {
1228 s->chan_map[
i] ==
TYPE_LFE ? 16000 :
/* LFE */
1229 69000 ; /* SCE */
1230 }
1231 }
1232
1233 /* Samplerate */
1234 for (
int i = 0;;
i++) {
1237 s->samplerate_index =
i;
1238 break;
1239 }
1240 }
1241
1242 /* Bitrate limiting */
1244 "Too many bits %f > %d per frame requested, clamping to max\n",
1246 6144 *
s->channels);
1249
1250 /* Profile and option setting */
1255 break;
1260 "PNS unavailable in the \"mpeg2_aac_low\" profile, turning off\n");
1262 }
1264
1265 /* Coder limitations */
1267
1268 /* M/S introduces horrible artifacts with multichannel files, this is temporary */
1269 if (
s->channels > 3)
1270 s->options.mid_side = 0;
1271
1272 // Initialize static tables
1274
1277
1280
1283
1288 for (
i = 0;
i <
s->chan_map[0];
i++)
1291 s->chan_map[0], grouping)) < 0)
1294 s->random_state = 0x1f2e3d4c;
1295
1297
1299
1300 return 0;
1301 }
1302
1303 #define AACENC_FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
1305 {
"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"},
1315 };
1316
1322 };
1323
1325 { "b", "0" },
1327 };
1328
1345 };