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 * add temporal noise shaping
31 ***********************************/
32
41
45
47
48 #define AAC_MAX_CHANNELS 6
49
50 #define ERROR_IF(cond, ...) \
51 if (cond) { \
52 av_log(avctx, AV_LOG_ERROR, __VA_ARGS__); \
53 return AVERROR(EINVAL); \
54 }
55
57
59 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8,
60 12, 12, 12, 12, 12, 16, 16, 24, 28, 36, 44,
61 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
62 };
63
65 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8,
66 12, 12, 12, 16, 16, 16, 20, 24, 24, 28, 36,
67 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40
68 };
69
71 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8,
72 12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28,
73 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
74 96
75 };
76
78 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8,
79 12, 12, 12, 12, 16, 16, 20, 20, 24, 24, 28, 28,
80 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32
81 };
82
84 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
85 12, 12, 12, 12, 16, 16, 16, 20, 20, 24, 24, 28, 28,
86 32, 36, 36, 40, 44, 48, 52, 52, 64, 64, 64, 64, 64
87 };
88
90 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
91 12, 12, 12, 12, 12, 12, 12, 12, 12, 16, 16, 16, 16, 20, 20, 20, 24, 24, 28, 28,
92 32, 36, 40, 40, 44, 48, 52, 56, 60, 64, 64, 64
93 };
94
96 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
97 16, 16, 16, 16, 16, 16, 16, 20, 20, 20, 20, 24, 24, 24, 28, 28,
98 32, 36, 36, 40, 44, 48, 52, 56, 60, 64, 80
99 };
100
106 };
107
109 4, 4, 4, 4, 4, 4, 8, 8, 8, 16, 28, 36
110 };
111
113 4, 4, 4, 4, 4, 8, 8, 8, 12, 12, 12, 16, 16, 16
114 };
115
117 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 12, 12, 16, 16, 20
118 };
119
121 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 12, 12, 16, 20, 20
122 };
123
125 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 12, 16, 20, 20
126 };
127
129 /* the last entry on the following row is swb_size_128_64 but is a
130 duplicate of swb_size_128_96 */
135 };
136
137 /** default channel configurations */
139 {1,
TYPE_SCE},
// 1 channel - single channel element
140 {1,
TYPE_CPE},
// 2 channels - channel pair
143 {3,
TYPE_SCE,
TYPE_CPE, TYPE_CPE},
// 5 channels - front center + stereo + back stereo
145 };
146
147 /**
148 * Table to remap channels from libavcodec's default order to AAC order.
149 */
151 { 0 },
152 { 0, 1 },
153 { 2, 0, 1 },
154 { 2, 0, 1, 3 },
155 { 2, 0, 1, 3, 4 },
156 { 2, 0, 1, 4, 5, 3 },
157 };
158
159 /**
160 * Make AAC audio config object.
161 * @see 1.6.2.1 "Syntax - AudioSpecificConfig"
162 */
164 {
167
169 put_bits(&pb, 5, 2);
//object type - AAC-LC
172 //GASpecificConfig
173 put_bits(&pb, 1, 0);
//frame length - 1024 samples
174 put_bits(&pb, 1, 0);
//does not depend on core coder
175 put_bits(&pb, 1, 0);
//is not extension
176
177 //Explicitly Mark SBR absent
178 put_bits(&pb, 11, 0x2b7);
//sync extension
182 }
183
184 #define WINDOW_FUNC(type) \
185 static void apply_ ##type ##_window(AVFloatDSPContext *fdsp, \
186 SingleChannelElement *sce, \
187 const float *audio)
188
190 {
193 float *
out = sce->ret_buf;
194
195 fdsp->vector_fmul (out, audio, lwindow, 1024);
196 fdsp->vector_fmul_reverse(out + 1024, audio + 1024, pwindow, 1024);
197 }
198
200 {
203 float *
out = sce->ret_buf;
204
205 fdsp->vector_fmul(out, audio, lwindow, 1024);
206 memcpy(out + 1024, audio + 1024, sizeof(out[0]) * 448);
207 fdsp->vector_fmul_reverse(out + 1024 + 448, audio + 1024 + 448, swindow, 128);
208 memset(out + 1024 + 576, 0, sizeof(out[0]) * 448);
209 }
210
212 {
215 float *
out = sce->ret_buf;
216
217 memset(out, 0, sizeof(out[0]) * 448);
218 fdsp->vector_fmul(out + 448, audio + 448, swindow, 128);
219 memcpy(out + 576, audio + 576, sizeof(out[0]) * 448);
220 fdsp->vector_fmul_reverse(out + 1024, audio + 1024, lwindow, 1024);
221 }
222
224 {
227 const float *
in = audio + 448;
228 float *
out = sce->ret_buf;
229 int w;
230
231 for (w = 0; w < 8; w++) {
232 fdsp->vector_fmul (out, in, w ? pwindow : swindow, 128);
233 out += 128;
234 in += 128;
235 fdsp->vector_fmul_reverse(out, in, swindow, 128);
236 out += 128;
237 }
238 }
239
242 const float *audio) = {
247 };
248
250 float *audio)
251 {
252 int i;
254
256
259 else
260 for (i = 0; i < 1024; i += 128)
262 memcpy(audio, audio + 1024, sizeof(audio[0]) * 1024);
263 }
264
265 /**
266 * Encode ics_info element.
267 * @see Table 4.6 (syntax of ics_info)
268 */
270 {
271 int w;
272
279 } else {
281 for (w = 1; w < 8; w++)
283 }
284 }
285
286 /**
287 * Encode MS data.
288 * @see 4.6.8.1 "Joint Coding - M/S Stereo"
289 */
291 {
292 int i, w;
293
299 }
300
301 /**
302 * Produce integer coefficients from scalefactors provided by the model.
303 */
305 {
307 int start, maxsfb, cmaxsfb;
308
309 for (ch = 0; ch < chans; ch++) {
311 start = 0;
312 maxsfb = 0;
315 for (g = 0; g < ics->
num_swb; g++) {
316 //apply M/S
321 }
322 }
324 }
325 for (cmaxsfb = ics->
num_swb; cmaxsfb > 0 && cpe->
ch[ch].
zeroes[w+cmaxsfb-1]; cmaxsfb--)
326 ;
327 maxsfb =
FFMAX(maxsfb, cmaxsfb);
328 }
330
331 //adjust zero bands for window groups
333 for (g = 0; g < ics->
max_sfb; g++) {
334 i = 1;
335 for (w2 = w; w2 < w + ics->
group_len[w]; w2++) {
336 if (!cpe->
ch[ch].
zeroes[w2*16 + g]) {
337 i = 0;
338 break;
339 }
340 }
342 }
343 }
344 }
345
349 int msc = 0;
353 for (i = 0; i < ics0->
max_sfb; i++)
355 msc++;
356 if (msc == 0 || ics0->
max_sfb == 0)
358 else
360 }
361 }
362
363 /**
364 * Encode scalefactor band coding type.
365 */
367 {
368 int w;
369
372 }
373
374 /**
375 * Encode scalefactors.
376 */
379 {
381 int i, w;
382
385 if (!sce->
zeroes[w*16 + i]) {
388 off = sce->
sf_idx[w*16 + i];
390 }
391 }
392 }
393 }
394
395 /**
396 * Encode pulse data.
397 */
399 {
400 int i;
401
404 return;
405
411 }
412 }
413
414 /**
415 * Encode spectral coefficients processed by psychoacoustic model.
416 */
418 {
420
422 start = 0;
424 if (sce->
zeroes[w*16 + i]) {
426 continue;
427 }
435 }
436 }
437 }
438
439 /**
440 * Encode one channel of audio data.
441 */
444 int common_window)
445 {
447 if (!common_window)
455 return 0;
456 }
457
458 /**
459 * Write some auxiliary information about the created AAC file.
460 */
462 {
463 int i, namelen, padbits;
464
465 namelen = strlen(name) + 2;
468 if (namelen >= 15)
470 put_bits(&s->
pb, 4, 0);
//extension type - filler
473 for (i = 0; i < namelen - 2; i++)
476 }
477
478 /*
479 * Copy input samples.
480 * Channels are reordered from libavcodec's default order to AAC order.
481 */
483 {
484 int ch;
487
488 /* copy and remap input samples */
489 for (ch = 0; ch < s->
channels; ch++) {
490 /* copy last 1024 samples of previous frame to the start of the current frame */
492
493 /* copy new samples and zero any remaining samples */
494 if (frame) {
498 }
501 }
502 }
503
506 {
510 int i, ch, w,
g, chans,
tag, start_ch,
ret;
511 int chan_el_counter[4];
513
515 return 0;
516
517 /* add current frame to queue */
518 if (frame) {
521 }
522
526
528 return 0;
529
530 start_ch = 0;
531 for (i = 0; i < s->
chan_map[0]; i++) {
536 for (ch = 0; ch < chans; ch++) {
538 int cur_channel = start_ch + ch;
539 overlap = &samples[cur_channel][0];
540 samples2 = overlap + 1024;
541 la = samples2 + (448+64);
542 if (!frame)
543 la = NULL;
549
550 /* Only the lowest 12 coefficients are used in a LFE channel.
551 * The expression below results in only the bottom 8 coefficients
552 * being used for 11.025kHz to 16kHz sample rates.
553 */
555 } else {
558 }
568
570 }
571 start_ch += chans;
572 }
575 do {
576 int frame_bits;
577
579
582 start_ch = 0;
583 memset(chan_el_counter, 0, sizeof(chan_el_counter));
584 for (i = 0; i < s->
chan_map[0]; i++) {
592 for (ch = 0; ch < chans; ch++)
595 for (ch = 0; ch < chans; ch++) {
598 }
600 if (chans > 1
601 && wi[0].window_type[0] == wi[1].window_type[0]
602 && wi[0].window_shape == wi[1].window_shape) {
603
606 if (wi[0].grouping[w] != wi[1].grouping[w]) {
608 break;
609 }
610 }
611 }
617 for (g = 0; g < ics->
num_swb; g++)
621 }
622 }
624 if (chans == 2) {
629 }
630 }
631 for (ch = 0; ch < chans; ch++) {
634 }
635 start_ch += chans;
636 }
637
639 if (frame_bits <= 6144 * s->channels - 3) {
641 break;
642 }
643
645
646 } while (1);
647
651
652 // rate control stuff
657 }
658
659 if (!frame)
661
664
666 *got_packet_ptr = 1;
667 return 0;
668 }
669
671 {
673
682 return 0;
683 }
684
686 {
688
690
691 // window init
696
701
702 return 0;
703 }
704
706 {
707 int ch;
711
714
715 return 0;
716 alloc_fail:
718 }
719
721 {
726 int lengths[2];
727
729
730 for (i = 0; i < 16; i++)
732 break;
733
735
737 "Unsupported sample rate %d\n", avctx->
sample_rate);
739 "Unsupported number of channels: %d\n", s->
channels);
741 "Unsupported profile %d\n", avctx->
profile);
743 "Too many bits per frame requested\n");
744
746
748
750 goto fail;
751
753 goto fail;
754
757
762 for (i = 0; i < s->
chan_map[0]; i++)
765 goto fail;
768
769 if (HAVE_MIPSDSPR1)
771
773
775
776 for (i = 0; i < 428; i++)
778
781
782 return 0;
783 fail:
786 }
787
788 #define AACENC_FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
790 {
"stereo_mode",
"Stereo coding method", offsetof(
AACEncContext,
options.stereo_mode),
AV_OPT_TYPE_INT, {.i64 = 0}, -1, 1,
AACENC_FLAGS,
"stereo_mode"},
793 {
"ms_force",
"Force Mid/Side for the whole frame if possible", 0,
AV_OPT_TYPE_CONST, {.i64 = 1 }, INT_MIN, INT_MAX,
AACENC_FLAGS,
"stereo_mode"},
795 {NULL}
796 };
797
799 "AAC encoder",
803 };
804
805 /* duplicated from avpriv_mpeg4audio_sample_rates to avoid shared build
806 * failures */
808 96000, 88200, 64000, 48000, 44100, 32000,
809 24000, 22050, 16000, 12000, 11025, 8000, 7350
810 };
811
827 };