FFmpeg
Loading...
Searching...
No Matches
aacenc.c
Go to the documentation of this file.
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 ***********************************/
31#include <float.h>
32
34#include "libavutil/crc.h"
35#include "libavutil/libm.h"
36#include "libavutil/float_dsp.h"
37#include "libavutil/mem.h"
38#include "libavutil/opt.h"
39#include "avcodec.h"
40#include "codec_internal.h"
41#include "encode.h"
42#include "put_bits.h"
43#include "mpeg4audio.h"
44#include "sinewin.h"
45#include "profiles.h"
46#include "version.h"
47
48#include "aac.h"
49#include "aactab.h"
50#include "aacenc.h"
51#include "aacenctab.h"
52#include "aacenc_utils.h"
53
54#include "psymodel.h"
55
56/**
57 * List of PCE (Program Configuration Element) for the channel layouts listed
58 * in channel_layout.h
59 *
60 * For those wishing in the future to add other layouts:
61 *
62 * - num_ele: number of elements in each group of front, side, back, lfe channels
63 * (an element is of type SCE (single channel), CPE (channel pair) for
64 * the first 3 groups; and is LFE for LFE group).
65 *
66 * - pairing: 0 for an SCE element or 1 for a CPE; does not apply to LFE group
67 *
68 * - index: there are three independent indices for SCE, CPE and LFE;
69 * they are incremented irrespective of the group to which the element belongs;
70 * they are not reset when going from one group to another
71 *
72 * Example: for 7.0 channel layout,
73 * .pairing = { { 1, 0 }, { 1 }, { 1 }, }, (3 CPE and 1 SCE in front group)
74 * .index = { { 0, 0 }, { 1 }, { 2 }, },
75 * (index is 0 for the single SCE but goes from 0 to 2 for the CPEs)
76 *
77 * The index order impacts the channel ordering. But is otherwise arbitrary
78 * (the sequence could have been 2, 0, 1 instead of 0, 1, 2).
79 *
80 * Spec allows for discontinuous indices, e.g. if one has a total of two SCE,
81 * SCE.0 SCE.15 is OK per spec; BUT it won't be decoded by our AAC decoder
82 * which at this time requires that indices fully cover some range starting
83 * from 0 (SCE.1 SCE.0 is OK but not SCE.0 SCE.15).
84 *
85 * - height: 0 for a base layer element, 1 for a top layer element, 2 for a bottom
86 * layer element.
87 *
88 * - config_map: total number of elements and their types. Beware, the way the
89 * types are ordered impacts the final channel ordering.
90 *
91 * - reorder_map: reorders the channels.
92 *
93 */
94static const AACPCEInfo aac_pce_configs[] = {
95 {
96 .layout = AV_CHANNEL_LAYOUT_MONO,
97 .num_ele = { 1, 0, 0, 0 },
98 .pairing = { { 0 }, },
99 .index = { { 0 }, },
100 .config_map = { 1, TYPE_SCE, },
101 .reorder_map = { 0 },
102 },
103 {
104 .layout = AV_CHANNEL_LAYOUT_STEREO,
105 .num_ele = { 1, 0, 0, 0 },
106 .pairing = { { 1 }, },
107 .index = { { 0 }, },
108 .config_map = { 1, TYPE_CPE, },
109 .reorder_map = { 0, 1 },
110 },
111 {
113 .num_ele = { 1, 0, 0, 1 },
114 .pairing = { { 1 }, },
115 .index = { { 0 },{ 0 },{ 0 },{ 0 } },
116 .config_map = { 2, TYPE_CPE, TYPE_LFE },
117 .reorder_map = { 0, 1, 2 },
118 },
119 {
120 .layout = AV_CHANNEL_LAYOUT_2_1,
121 .num_ele = { 1, 0, 1, 0 },
122 .pairing = { { 1 },{ 0 },{ 0 } },
123 .index = { { 0 },{ 0 },{ 0 }, },
124 .config_map = { 2, TYPE_CPE, TYPE_SCE },
125 .reorder_map = { 0, 1, 2 },
126 },
127 {
129 .num_ele = { 2, 0, 0, 0 },
130 .pairing = { { 0, 1 }, },
131 .index = { { 0, 0 }, },
132 .config_map = { 2, TYPE_SCE, TYPE_CPE },
133 .reorder_map = { 2, 0, 1 },
134 },
135 {
137 .num_ele = { 2, 0, 0, 1 },
138 .pairing = { { 0, 1 }, },
139 .index = { { 0, 0 }, { 0 }, { 0 }, { 0 }, },
140 .config_map = { 3, TYPE_SCE, TYPE_CPE, TYPE_LFE },
141 .reorder_map = { 2, 0, 1, 3 },
142 },
143 {
145 .num_ele = { 2, 0, 1, 0 },
146 .pairing = { { 0, 1 }, { 0 }, { 0 }, },
147 .index = { { 0, 0 }, { 0 }, { 1 } },
148 .config_map = { 3, TYPE_SCE, TYPE_CPE, TYPE_SCE },
149 .reorder_map = { 2, 0, 1, 3 },
150 },
151 {
153 .num_ele = { 2, 0, 1, 1 },
154 .pairing = { { 0, 1 }, { 0 }, { 0 }, },
155 .index = { { 0, 0 }, { 0 }, { 1 }, { 0 } },
156 .config_map = { 4, TYPE_SCE, TYPE_CPE, TYPE_SCE, TYPE_LFE },
157 .reorder_map = { 2, 0, 1, 4, 3 },
158 },
159 {
160 .layout = AV_CHANNEL_LAYOUT_2_2,
161 .num_ele = { 1, 0, 1, 0 },
162 .pairing = { { 1 }, { 0 }, { 1 }, },
163 .index = { { 0 }, { 0 }, { 1 } },
164 .config_map = { 2, TYPE_CPE, TYPE_CPE },
165 .reorder_map = { 0, 1, 2, 3 },
166 },
167 {
168 .layout = AV_CHANNEL_LAYOUT_QUAD,
169 .num_ele = { 1, 0, 1, 0 },
170 .pairing = { { 1 }, { 0 }, { 1 }, },
171 .index = { { 0 }, { 0 }, { 1 } },
172 .config_map = { 2, TYPE_CPE, TYPE_CPE },
173 .reorder_map = { 0, 1, 2, 3 },
174 },
175 {
177 .num_ele = { 2, 0, 1, 0 },
178 .pairing = { { 0, 1 }, { 0 }, { 1 } },
179 .index = { { 0, 0 }, { 0 }, { 1 } },
180 .config_map = { 3, TYPE_SCE, TYPE_CPE, TYPE_CPE },
181 .reorder_map = { 2, 0, 1, 3, 4 },
182 },
183 {
185 .num_ele = { 2, 0, 1, 1 },
186 .pairing = { { 0, 1 }, { 0 }, { 1 }, },
187 .index = { { 0, 0 }, { 0 }, { 1 }, { 0 } },
188 .config_map = { 4, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_LFE },
189 .reorder_map = { 2, 0, 1, 4, 5, 3 },
190 },
191 {
193 .num_ele = { 2, 0, 1, 0 },
194 .pairing = { { 0, 1 }, { 0 }, { 1 } },
195 .index = { { 0, 0 }, { 0 }, { 1 } },
196 .config_map = { 3, TYPE_SCE, TYPE_CPE, TYPE_CPE },
197 .reorder_map = { 2, 0, 1, 3, 4 },
198 },
199 {
201 .num_ele = { 2, 0, 1, 1 },
202 .pairing = { { 0, 1 }, { 0 }, { 1 }, },
203 .index = { { 0, 0 }, { 0 }, { 1 }, { 0 } },
204 .config_map = { 4, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_LFE },
205 .reorder_map = { 2, 0, 1, 4, 5, 3 },
206 },
207 {
209 .num_ele = { 2, 0, 2, 0 },
210 .pairing = { { 0, 1 }, { 0 }, { 1, 0 } },
211 .index = { { 0, 0 }, { 0 }, { 1, 1 } },
212 .config_map = { 4, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_SCE },
213 .reorder_map = { 2, 0, 1, 4, 5, 3 },
214 },
215 {
217 .num_ele = { 2, 0, 1, 0 },
218 .pairing = { { 1, 1 }, { 0 }, { 1 } },
219 .index = { { 0, 1 }, { 0 }, { 2 }, },
220 .config_map = { 3, TYPE_CPE, TYPE_CPE, TYPE_CPE, },
221 .reorder_map = { 2, 3, 0, 1, 4, 5 },
222 },
223 {
225 .num_ele = { 2, 0, 2, 0 },
226 .pairing = { { 0, 1 }, { 0 }, { 1, 0 } },
227 .index = { { 0, 0 }, { 0 }, { 1, 1 } },
228 .config_map = { 4, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_SCE },
229 .reorder_map = { 2, 0, 1, 3, 4, 5 },
230 },
231 {
233 .num_ele = { 2, 0, 2, 1 },
234 .pairing = { { 0, 1 }, { 0 }, { 1, 0 }, },
235 .index = { { 0, 0 }, { 0 }, { 1, 1 }, { 0 } },
236 .config_map = { 5, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_SCE, TYPE_LFE },
237 .reorder_map = { 2, 0, 1, 5, 6, 4, 3 },
238 },
239 {
241 .num_ele = { 2, 0, 2, 1 },
242 .pairing = { { 0, 1 },{ 0 },{ 1, 0 }, },
243 .index = { { 0, 0 },{ 0 },{ 1, 1 },{ 0 } },
244 .config_map = { 5, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_SCE, TYPE_LFE },
245 .reorder_map = { 2, 0, 1, 4, 5, 6, 3 },
246 },
247 {
249 .num_ele = { 2, 0, 1, 1 },
250 .pairing = { { 1, 1 }, { 0 }, { 1 }, },
251 .index = { { 0, 1 }, { 0 }, { 2 }, { 0 }, },
252 .config_map = { 4, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_LFE, },
253 .reorder_map = { 3, 4, 0, 1, 5, 6, 2 },
254 },
255 {
257 .num_ele = { 2, 0, 2, 0 },
258 .pairing = { { 0, 1 }, { 0 }, { 1, 1 }, },
259 .index = { { 0, 0 }, { 0 }, { 2, 1 }, },
260 .config_map = { 4, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_CPE },
261 .reorder_map = { 2, 0, 1, 3, 4, 5, 6 },
262 },
263 {
265 .num_ele = { 3, 0, 1, 0 },
266 .pairing = { { 0, 1, 1 }, { 0 }, { 1 }, },
267 .index = { { 0, 0, 1 }, { 0 }, { 2 }, },
268 .config_map = { 4, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_CPE },
269 .reorder_map = { 2, 3, 4, 0, 1, 5, 6 },
270 },
271 {
273 .num_ele = { 2, 0, 2, 1 },
274 .pairing = { { 0, 1 }, { 0 }, { 1, 1 }, },
275 .index = { { 0, 0 }, { 0 }, { 2, 1 }, { 0 } },
276 .config_map = { 5, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_LFE },
277 .reorder_map = { 2, 0, 1, 4, 5, 6, 7, 3 },
278 },
279 {
281 .num_ele = { 3, 0, 1, 1 },
282 .pairing = { { 0, 1, 1 }, { 0 }, { 1 }, },
283 .index = { { 0, 0, 1 }, { 0 }, { 2 }, { 0 }, },
284 .config_map = { 5, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_LFE },
285 .reorder_map = { 2, 4, 5, 0, 1, 6, 7, 3 },
286 },
287 {
289 .num_ele = { 3, 0, 1, 1 },
290 .pairing = { { 0, 1, 1 }, { 0 }, { 1 } },
291 .index = { { 0, 0, 1 }, { 0 }, { 2 }, { 0 } },
292 .config_map = { 5, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_LFE },
293 .reorder_map = { 2, 6, 7, 0, 1, 4, 5, 3 },
294 },
295 {
297 .num_ele = { 2, 0, 3, 0 },
298 .pairing = { { 0, 1 }, { 0 }, { 1, 1, 0 }, },
299 .index = { { 0, 0 }, { 0 }, { 1, 2, 1 }, },
300 .config_map = { 5, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_SCE },
301 .reorder_map = { 2, 0, 1, 6, 7, 3, 4, 5 },
302 },
303 {
305 .num_ele = { 3, 0, 1, 1 },
306 .pairing = { { 0, 1, 1 }, { 0 }, { 1 }, },
307 .index = { { 0, 0, 2 }, { 0 }, { 1 }, { 0 }, },
308 .height = { { 0, 0, 1 }, { 0 }, { 0 } },
309 .config_map = { 5, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_LFE, TYPE_CPE },
310 .reorder_map = { 2, 0, 1, 4, 5, 3, 6, 7 },
311 },
312 {
313 // ITU-R BS.2051-3 Sound System C
315 .num_ele = { 3, 0, 1, 1 },
316 .pairing = { { 0, 1, 1 }, { 0 }, { 1 }, },
317 .index = { { 0, 0, 2 }, { 0 }, { 1 }, { 0 }, },
318 .height = { { 0, 0, 1 }, { 0 }, { 0 } },
319 .config_map = { 5, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_LFE, TYPE_CPE },
320 .reorder_map = { 2, 0, 1, 4, 5, 3, 6, 7 },
321 },
322 {
324 .num_ele = { 3, 0, 2, 1 },
325 .pairing = { { 0, 1, 1 }, { 0 }, { 1, 1 }, },
326 .index = { { 0, 0, 2 }, { 0 }, { 1, 3 }, { 0 }, },
327 .height = { { 0, 0, 1 }, { 0 }, { 0, 1 } },
328 .config_map = { 6, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_LFE, TYPE_CPE, TYPE_CPE },
329 .reorder_map = { 2, 0, 1, 4, 5, 3, 6, 7, 8, 9 },
330 },
331 // ITU-R BS.2051-3 Sound System D
332 {
333 .layout = {
334 .nb_channels = 10,
337 },
338 .num_ele = { 3, 0, 2, 1 },
339 .pairing = { { 0, 1, 1 }, { 0 }, { 1, 1 }, },
340 .index = { { 0, 0, 2 }, { 0 }, { 1, 3 }, { 0 }, },
341 .height = { { 0, 0, 1 }, { 0 }, { 0, 1 } },
342 .config_map = { 6, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_LFE, TYPE_CPE, TYPE_CPE },
343 .reorder_map = { 2, 0, 1, 4, 5, 3, 6, 7, 8, 9 },
344 },
345 {
346 // ITU-R BS.2051-3 Sound System E
347 .layout = {
348 .nb_channels = 11,
351 },
352 .num_ele = { 4, 0, 2, 1 },
353 .pairing = { { 0, 1, 1, 0 }, { 0 }, { 1, 1 }, },
354 .index = { { 0, 0, 2, 1 }, { 0 }, { 1, 3 }, { 0 }, },
355 .height = { { 0, 0, 1, 2 }, { 0 }, { 0, 1 } },
356 .config_map = { 7, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_LFE, TYPE_CPE, TYPE_CPE, TYPE_SCE },
357 .reorder_map = { 2, 0, 1, 4, 5, 3, 6, 7, 8, 9, 10 },
358 },
359 {
361 .num_ele = { 3, 0, 2, 1 },
362 .pairing = { { 0, 1, 1 }, { 0 }, { 1, 1 }, },
363 .index = { { 0, 0, 3 }, { 0 }, { 2, 1 }, { 0 } },
364 .height = { { 0, 0, 1 }, { 0 }, { 0, 0 } },
365 .config_map = { 6, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_LFE, TYPE_CPE },
366 .reorder_map = { 2, 0, 1, 4, 5, 6, 7, 3, 8, 9 },
367 },
368 {
369 // ITU-R BS.2051-3 Sound System F
371 .num_ele = { 3, 0, 3, 2 },
372 .pairing = { { 0, 1, 1 }, { 0 }, { 1, 1, 0 }, },
373 .index = { { 0, 0, 3 }, { 0 }, { 2, 1, 1 }, { 0, 1 } },
374 .height = { { 0, 0, 1 }, { 0 }, { 0, 0, 1 } },
376 .reorder_map = { 2, 0, 1, 4, 5, 6, 7, 3, 11, 8, 9, 10 },
377 },
378 {
379 // ITU-R BS.2051-3 Sound System J
381 .num_ele = { 3, 0, 3, 1 },
382 .pairing = { { 0, 1, 1 }, { 0 }, { 1, 1, 1 }, },
383 .index = { { 0, 0, 3 }, { 0 }, { 2, 1, 4 }, { 0 } },
384 .height = { { 0, 0, 1 }, { 0 }, { 0, 0, 1 } },
385 .config_map = { 7, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_LFE, TYPE_CPE, TYPE_CPE },
386 .reorder_map = { 2, 0, 1, 4, 5, 6, 7, 3, 8, 9, 10, 11 },
387 },
388 {
389 // ITU-R BS.2051-3 Sound System G
391 .num_ele = { 4, 0, 3, 1 },
392 .pairing = { { 0, 1, 1, 1 }, { 0 }, { 1, 1, 1 }, },
393 .index = { { 0, 0, 1, 4 }, { 0 }, { 2, 3, 5 }, { 0 } },
394 .height = { { 0, 0, 0, 1 }, { 0 }, { 0, 0, 1 } },
396 .reorder_map = { 2, 6, 7, 0, 1, 8, 9, 4, 5, 3, 10, 11, 12, 13 },
397 },
398 {
400 .num_ele = { 4, 1, 3, 1 },
401 .pairing = { { 0, 1, 1, 1 }, { 1 }, { 1, 1, 1 }, },
402 .index = { { 0, 0, 1, 4 }, { 5 }, { 2, 3, 6 }, { 0 } },
403 .height = { { 0, 0, 0, 1 }, { 1 }, { 0, 0, 1 } },
405 .reorder_map = { 2, 6, 7, 0, 1, 8, 9, 4, 5, 3, 10, 11, 14, 15, 12, 13 },
406 },
407 {
409 .num_ele = { 1, 0, 1, 0 },
410 .pairing = { { 1 }, { 0 }, { 1 }, },
411 .index = { { 0 }, { 0 }, { 1 } },
412 .config_map = { 2, TYPE_CPE, TYPE_CPE },
413 .reorder_map = { 0, 1, 2, 3 },
414 },
415 {
416 .layout = { .order = AV_CHANNEL_ORDER_AMBISONIC, .nb_channels = 9 },
417 .num_ele = { 3, 0, 2, 0 },
418 .pairing = { { 0, 1, 1 }, { 0 }, { 1, 1 }, },
419 .index = { { 0, 0, 1 }, { 0 }, { 2, 3 }, },
420 .config_map = { 5, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_CPE },
421 .reorder_map = { 2, 5, 6, 0, 1, 7, 8, 3, 4 },
422 },
423 {
424 .layout = { .order = AV_CHANNEL_ORDER_AMBISONIC, .nb_channels = 16 },
425 .num_ele = { 4, 0, 4, 0 },
426 .pairing = { { 1, 1, 1, 1 }, { 0 }, { 1, 1, 1, 1 }, },
427 .index = { { 0, 1, 2, 3 }, { 0 }, { 4, 5, 6, 7 }, },
429 .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
430 },
431};
432
433static void put_pce(PutBitContext *pb, AVCodecContext *avctx)
434{
435 int i, j;
436 AACEncContext *s = avctx->priv_data;
437 AACPCEInfo *pce = &s->pce;
438 const int bitexact = avctx->flags & AV_CODEC_FLAG_BITEXACT;
439 const char *aux_data = bitexact ? "Lavc" : LIBAVCODEC_IDENT;
440
441 put_bits(pb, 4, 0);
442
443 put_bits(pb, 2, avctx->profile);
444 put_bits(pb, 4, s->samplerate_index);
445
446 put_bits(pb, 4, pce->num_ele[0]); /* Front */
447 put_bits(pb, 4, pce->num_ele[1]); /* Side */
448 put_bits(pb, 4, pce->num_ele[2]); /* Back */
449 put_bits(pb, 2, pce->num_ele[3]); /* LFE */
450 put_bits(pb, 3, 0); /* Assoc data */
451 put_bits(pb, 4, 0); /* CCs */
452
453 put_bits(pb, 1, 0); /* Stereo mixdown */
454 put_bits(pb, 1, 0); /* Mono mixdown */
455 put_bits(pb, 1, 0); /* Something else */
456
457 for (i = 0; i < 4; i++) {
458 for (j = 0; j < pce->num_ele[i]; j++) {
459 if (i < 3)
460 put_bits(pb, 1, pce->pairing[i][j]);
461 put_bits(pb, 4, pce->index[i][j]);
462 }
463 }
464
465 align_put_bits(pb);
466 if (s->needs_height_ext) {
467 const AVCRC *crc_ctx = av_crc_get_table(AV_CRC_8_ATM);
468 PutBitContext height_pb;
469 uint8_t buf[16];
470 int bits = 8 + pce->num_ele[0] * 2 + pce->num_ele[1] * 2 + pce->num_ele[2] * 2;
471 int bytes = (bits + 7) / 8;
472
473 init_put_bits(&height_pb, buf, bytes);
474 put_bits(&height_pb, 8, 0xAC);
475 for (i = 0; i < 3; i++)
476 for (j = 0; j < pce->num_ele[i]; j++)
477 put_bits(&height_pb, 2, pce->height[i][j]);
478 flush_put_bits(&height_pb);
479
480 put_bits(pb, 8, bytes + 1);
481 ff_copy_bits(pb, buf, bits);
482 align_put_bits(pb);
483 put_bits(pb, 8, av_crc(crc_ctx, 0xFF, buf, bytes));
484 } else {
485 put_bits(pb, 8, strlen(aux_data));
486 ff_put_string(pb, aux_data, 0);
487 }
488}
489
490/**
491 * Make AAC audio config object.
492 * @see 1.6.2.1 "Syntax - AudioSpecificConfig"
493 */
494static int put_audio_specific_config(AVCodecContext *avctx, int chcfg)
495{
496 PutBitContext pb;
497 AACEncContext *s = avctx->priv_data;
498 const int max_size = 32;
499
500 avctx->extradata = av_mallocz(max_size);
501 if (!avctx->extradata)
502 return AVERROR(ENOMEM);
503
504 init_put_bits(&pb, avctx->extradata, max_size);
505 put_bits(&pb, 5, s->profile+1); //profile
506 put_bits(&pb, 4, s->samplerate_index); //sample rate index
507 put_bits(&pb, 4, chcfg);
508 //GASpecificConfig
509 put_bits(&pb, 1, 0); //frame length - 1024 samples
510 put_bits(&pb, 1, 0); //does not depend on core coder
511 put_bits(&pb, 1, 0); //is not extension
512 if (s->needs_pce)
513 put_pce(&pb, avctx);
514
515 //Explicitly Mark SBR absent
516 put_bits(&pb, 11, 0x2b7); //sync extension
517 put_bits(&pb, 5, AOT_SBR);
518 put_bits(&pb, 1, 0);
519 flush_put_bits(&pb);
520 avctx->extradata_size = put_bytes_output(&pb);
521
522 return 0;
523}
524
526{
527 ++s->quantize_band_cost_cache_generation;
528 if (s->quantize_band_cost_cache_generation == 0) {
529 memset(s->quantize_band_cost_cache, 0, sizeof(s->quantize_band_cost_cache));
530 s->quantize_band_cost_cache_generation = 1;
531 }
532}
533
534#define WINDOW_FUNC(type) \
535static void apply_ ##type ##_window(AVFloatDSPContext *fdsp, \
536 SingleChannelElement *sce, \
537 const float *audio)
538
539WINDOW_FUNC(only_long)
540{
541 const float *lwindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
542 const float *pwindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
543 float *out = sce->ret_buf;
544
545 fdsp->vector_fmul (out, audio, lwindow, 1024);
546 fdsp->vector_fmul_reverse(out + 1024, audio + 1024, pwindow, 1024);
547}
548
549WINDOW_FUNC(long_start)
550{
551 const float *lwindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
552 const float *swindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
553 float *out = sce->ret_buf;
554
555 fdsp->vector_fmul(out, audio, lwindow, 1024);
556 memcpy(out + 1024, audio + 1024, sizeof(out[0]) * 448);
557 fdsp->vector_fmul_reverse(out + 1024 + 448, audio + 1024 + 448, swindow, 128);
558 memset(out + 1024 + 576, 0, sizeof(out[0]) * 448);
559}
560
561WINDOW_FUNC(long_stop)
562{
563 const float *lwindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
564 const float *swindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
565 float *out = sce->ret_buf;
566
567 memset(out, 0, sizeof(out[0]) * 448);
568 fdsp->vector_fmul(out + 448, audio + 448, swindow, 128);
569 memcpy(out + 576, audio + 576, sizeof(out[0]) * 448);
570 fdsp->vector_fmul_reverse(out + 1024, audio + 1024, lwindow, 1024);
571}
572
573WINDOW_FUNC(eight_short)
574{
575 const float *swindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
576 const float *pwindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
577 const float *in = audio + 448;
578 float *out = sce->ret_buf;
579 int w;
580
581 for (w = 0; w < 8; w++) {
582 fdsp->vector_fmul (out, in, w ? pwindow : swindow, 128);
583 out += 128;
584 in += 128;
585 fdsp->vector_fmul_reverse(out, in, swindow, 128);
586 out += 128;
587 }
588}
589
590static void (*const apply_window[4])(AVFloatDSPContext *fdsp,
592 const float *audio) = {
593 [ONLY_LONG_SEQUENCE] = apply_only_long_window,
594 [LONG_START_SEQUENCE] = apply_long_start_window,
595 [EIGHT_SHORT_SEQUENCE] = apply_eight_short_window,
596 [LONG_STOP_SEQUENCE] = apply_long_stop_window
597};
598
600 float *audio)
601{
602 int i;
603 float *output = sce->ret_buf;
604
605 apply_window[sce->ics.window_sequence[0]](s->fdsp, sce, audio);
606
608 s->mdct1024_fn(s->mdct1024, sce->coeffs, output, sizeof(float));
609 else
610 for (i = 0; i < 1024; i += 128)
611 s->mdct128_fn(s->mdct128, &sce->coeffs[i], output + i*2, sizeof(float));
612 memcpy(audio, audio + 1024, sizeof(audio[0]) * 1024);
613 memcpy(sce->pcoeffs, sce->coeffs, sizeof(sce->pcoeffs));
614}
615
616/**
617 * Encode ics_info element.
618 * @see Table 4.6 (syntax of ics_info)
619 */
621{
622 int w;
623
624 put_bits(&s->pb, 1, 0); // ics_reserved bit
625 put_bits(&s->pb, 2, info->window_sequence[0]);
626 put_bits(&s->pb, 1, info->use_kb_window[0]);
627 if (info->window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
628 put_bits(&s->pb, 6, info->max_sfb);
629 put_bits(&s->pb, 1, 0); /* No predictor present */
630 } else {
631 put_bits(&s->pb, 4, info->max_sfb);
632 for (w = 1; w < 8; w++)
633 put_bits(&s->pb, 1, !info->group_len[w]);
634 }
635}
636
637/**
638 * Encode MS data.
639 * @see 4.6.8.1 "Joint Coding - M/S Stereo"
640 */
642{
643 int i, w;
644
645 put_bits(pb, 2, cpe->ms_mode);
646 if (cpe->ms_mode == 1)
647 for (w = 0; w < cpe->ch[0].ics.num_windows; w += cpe->ch[0].ics.group_len[w])
648 for (i = 0; i < cpe->ch[0].ics.max_sfb; i++)
649 put_bits(pb, 1, cpe->ms_mask[w*16 + i]);
650}
651
652/**
653 * Produce integer coefficients from scalefactors provided by the model.
654 */
655static void adjust_frame_information(ChannelElement *cpe, int chans)
656{
657 int i, w, w2, g, ch;
658 int maxsfb, cmaxsfb;
659
660 for (ch = 0; ch < chans; ch++) {
661 IndividualChannelStream *ics = &cpe->ch[ch].ics;
662 maxsfb = 0;
663 cpe->ch[ch].pulse.num_pulse = 0;
664 for (w = 0; w < ics->num_windows; w += ics->group_len[w]) {
665 for (cmaxsfb = ics->num_swb; cmaxsfb > 0 && cpe->ch[ch].zeroes[w*16+cmaxsfb-1]; cmaxsfb--)
666 ;
667 maxsfb = FFMAX(maxsfb, cmaxsfb);
668 }
669 ics->max_sfb = maxsfb;
670
671 //adjust zero bands for window groups
672 for (w = 0; w < ics->num_windows; w += ics->group_len[w]) {
673 for (g = 0; g < ics->max_sfb; g++) {
674 i = 1;
675 for (w2 = w; w2 < w + ics->group_len[w]; w2++) {
676 if (!cpe->ch[ch].zeroes[w2*16 + g]) {
677 i = 0;
678 break;
679 }
680 }
681 cpe->ch[ch].zeroes[w*16 + g] = i;
682 }
683 }
684 }
685
686 if (chans > 1 && cpe->common_window) {
687 IndividualChannelStream *ics0 = &cpe->ch[0].ics;
688 IndividualChannelStream *ics1 = &cpe->ch[1].ics;
689 int msc = 0;
690 ics0->max_sfb = FFMAX(ics0->max_sfb, ics1->max_sfb);
691 ics1->max_sfb = ics0->max_sfb;
692 for (w = 0; w < ics0->num_windows*16; w += 16)
693 for (i = 0; i < ics0->max_sfb; i++)
694 if (cpe->ms_mask[w+i])
695 msc++;
696 if (msc == 0 || ics0->max_sfb == 0)
697 cpe->ms_mode = 0;
698 else
699 cpe->ms_mode = msc < ics0->max_sfb * ics0->num_windows ? 1 : 2;
700 }
701}
702
704{
705 int w, w2, g, i;
706 IndividualChannelStream *ics = &cpe->ch[0].ics;
707 if (!cpe->common_window)
708 return;
709 for (w = 0; w < ics->num_windows; w += ics->group_len[w]) {
710 for (w2 = 0; w2 < ics->group_len[w]; w2++) {
711 int start = (w+w2) * 128;
712 for (g = 0; g < ics->num_swb; g++) {
713 int p = -1 + 2 * (cpe->ch[1].band_type[w*16+g] - 14);
714 float scale = cpe->ch[0].is_ener[w*16+g];
715 if (!cpe->is_mask[w*16 + g]) {
716 start += ics->swb_sizes[g];
717 continue;
718 }
719 if (cpe->ms_mask[w*16 + g])
720 p *= -1;
721 for (i = 0; i < ics->swb_sizes[g]; i++) {
722 float sum = (cpe->ch[0].coeffs[start+i] + p*cpe->ch[1].coeffs[start+i])*scale;
723 cpe->ch[0].coeffs[start+i] = sum;
724 cpe->ch[1].coeffs[start+i] = 0.0f;
725 }
726 start += ics->swb_sizes[g];
727 }
728 }
729 }
730}
731
732/* I/S acceptance level for the image-error EMA at full rate pressure */
733#define NMR_IS_IMG_GATE 8000.0f
734
735/* Frequency in Hz for the lower limit of intensity stereo */
736#define NMR_IS_LOW_LIMIT 6100
737
738/* M/S adoption: es < 0.5*em, content-driven and rate-free */
739#define NMR_MS_EQUIV 0.5f
740#define NMR_MS_MASK 0.0f
741
742/* Pair decouple threshold on the joint-tool candidacy fraction EMA: pairs
743 * whose joint tools are mostly dead (diffuse decorrelated content) window
744 * per-channel and skip M/S; recouple above 1.3x. */
745#define NMR_DECORR_LO 0.20f
746
747/* Stereo-decision hysteresis: leaving a joint mode costs a margin. */
748#define NMR_STICKY 2.0f
749
750/* Decision statistics are EMA-smoothed across frames. */
751#define NMR_SDEC_EMA 0.75f
752
753/* PNS-stereo gate: substitute only clearly-decorrelated (wide) bands. */
754#define NMR_PNS_STEREO_DECORR 0.6f
755
756/* Recode one band's window group as mid+side in place. */
758 int w, int g, int start, int len, int gl)
759{
760 SingleChannelElement *sce0 = &cpe->ch[0];
761 SingleChannelElement *sce1 = &cpe->ch[1];
762 cpe->ms_mask[w*16+g] = 1;
763 for (int w2 = 0; w2 < gl; w2++) {
764 FFPsyBand *b0 = &s->psy.ch[s->cur_channel+0].psy_bands[(w+w2)*16+g];
765 FFPsyBand *b1 = &s->psy.ch[s->cur_channel+1].psy_bands[(w+w2)*16+g];
766 float *L = sce0->coeffs + start + (w+w2)*128;
767 float *R = sce1->coeffs + start + (w+w2)*128;
768 float em = 0.0f, es = 0.0f;
769 for (int i = 0; i < len; i++) {
770 float m = (L[i] + R[i]) * 0.5f;
771 R[i] = m - R[i]; L[i] = m;
772 em += L[i]*L[i]; es += R[i]*R[i];
773 }
774 b0->threshold = FFMIN(b0->threshold, b1->threshold) * 0.5f;
775 b1->threshold = b0->threshold;
776 b0->energy = em; b1->energy = es;
777 }
778}
779
780/* I/S perceptual test: reconstruction image error vs the pair's masks. */
782 int w, int g, int start, int len, int gl,
783 float ener0, float ener1, float dot,
784 float minthr0, float minthr1, float *ratio_out,
785 float *scale_out, float *sr_out, int *p_out)
786{
787 int p = dot >= 0.0f ? 1 : -1;
788 float ener01 = ener0 + ener1 + 2*p*dot; /* energy of L + p*R */
789 *ratio_out = FLT_MAX;
790 if (ener01 <= FLT_MIN)
791 return 0;
792 float scale = sqrtf(ener0 / ener01); /* carrier = (L + p*R)*scale */
793 float sr_ = sqrtf(ener1 / ener0); /* decoder: R = p*sr_*carrier */
794 float img0 = 0.0f, img1 = 0.0f;
795 for (int w2 = 0; w2 < gl; w2++) {
796 const float *L = cpe->ch[0].coeffs + start + (w+w2)*128;
797 const float *R = cpe->ch[1].coeffs + start + (w+w2)*128;
798 for (int i = 0; i < len; i++) {
799 float c = (L[i] + p*R[i]) * scale;
800 float dl = L[i] - c, dr = R[i] - p*sr_*c;
801 img0 += dl*dl; img1 += dr*dr;
802 }
803 }
804 *ratio_out = FFMAX(img0 / FFMAX(minthr0 * gl, FLT_MIN),
805 img1 / FFMAX(minthr1 * gl, FLT_MIN));
806 *scale_out = scale; *sr_out = sr_; *p_out = p;
807 return 1;
808}
809
810/* Recode one band's window group as intensity stereo in place: replace L with the
811 * carrier, zero R, signal the phase via the side channel's band type, and fold the
812 * pair's masking into the surviving (carrier) channel. */
814 int w, int g, int start, int len, int gl,
815 float scale, float sr_, int p,
816 float ener0, float ener1)
817{
818 cpe->is_mask[w*16+g] = 1;
819 cpe->ch[0].is_ener[w*16+g] = scale;
820 cpe->ch[1].is_ener[w*16+g] = ener0 / ener1;
821 cpe->ch[1].band_type[w*16+g] = p > 0 ? INTENSITY_BT : INTENSITY_BT2;
822 for (int w2 = 0; w2 < gl; w2++) {
823 FFPsyBand *b0 = &s->psy.ch[s->cur_channel+0].psy_bands[(w+w2)*16+g];
824 FFPsyBand *b1 = &s->psy.ch[s->cur_channel+1].psy_bands[(w+w2)*16+g];
825 float *L = cpe->ch[0].coeffs + start + (w+w2)*128;
826 float *R = cpe->ch[1].coeffs + start + (w+w2)*128;
827 float ec = 0.0f;
828 for (int i = 0; i < len; i++) {
829 L[i] = (L[i] + p*R[i]) * scale;
830 R[i] = 0.0f;
831 ec += L[i]*L[i];
832 }
833 b0->threshold = FFMIN(b0->threshold, b1->threshold / FFMAX(sr_*sr_, 1e-9f));
834 b0->energy = ec; b1->energy = 0.0f;
835 }
836}
837
838/*
839 * Per-band stereo-mode decision (L/R vs M/S vs intensity) for the NMR coder,
840 * made before quantization from the psychoacoustic model alone, so the
841 * quantizer search allocates natively on the spectra that are actually coded.
842 */
844{
845 SingleChannelElement *sce0 = &cpe->ch[0];
846 SingleChannelElement *sce1 = &cpe->ch[1];
847 IndividualChannelStream *ics = &sce0->ics;
848 const AVCodecContext *avctx = s->psy.avctx;
849 const float freq_mult = avctx->sample_rate / (1024.0f / ics->num_windows) / 2.0f;
850 int is_count = 0;
851
852 if (s->nmr) {
853 int pi = (s->cur_channel >> 1) & 7;
854 pi = pi * 2 + (ics->num_windows == 8); /* per-grid state bank */
855 if (!s->nmr->sinit[pi]) {
856 /* one-time init; per-grid banks persist across window switches
857 * (wiping them churned stereo modes audibly) */
858 memset(s->nmr->smode[pi], 0, sizeof(s->nmr->smode[pi]));
859 for (int b = 0; b < 128; b++) {
860 s->nmr->sema_em[pi][b] = 0.0f;
861 s->nmr->sema_img[pi][b] = -1.0f;
862 }
863 s->nmr->sinit[pi] = 1;
864 }
865 }
866
867 /* Per-band stereo decision (L/R vs M/S vs I/S), made pre-quantization from
868 * the psy model so the trellis allocates on the coded spectra. */
869
870 /* I/S engages under SUSTAINED strain only: rate pressure gated by the
871 * lambda floor (pressure spikes at a comfortable operating point must
872 * not admit it). Unengaged candidates fall back to M/S. */
873 float is_ramp = s->nmr ? s->nmr->press *
874 av_clipf((s->nmr->lam_floor - 40.0f) / (120.0f - 40.0f), 0.0f, 1.0f) : 0.0f;
875 const int allow_is = s->options.intensity_stereo && is_ramp > 0.0f;
876
877 const int pidx = (s->cur_channel >> 1) & 15;
878 const int decoupled = s->psy.pair_decoupled[pidx];
879 int njoint = 0, nbands = 0; /* joint-tool candidacy census, decouple feed */
880
881 for (int w = 0; w < ics->num_windows; w += ics->group_len[w]) {
882 int start = 0;
883 for (int g = 0; g < ics->num_swb; start += ics->swb_sizes[g++]) {
884 int len = ics->swb_sizes[g], gl = ics->group_len[w];
885 float ener0 = 0.0f, ener1 = 0.0f, dot = 0.0f, es_tot = 0.0f, em_tot = 0.0f;
886 float minthr0 = FLT_MAX, minthr1 = FLT_MAX;
887
888 cpe->is_mask[w*16+g] = 0;
889 cpe->ms_mask[w*16+g] = 0;
890
891 for (int w2 = 0; w2 < gl; w2++) {
892 FFPsyBand *b0 = &s->psy.ch[s->cur_channel+0].psy_bands[(w+w2)*16+g];
893 FFPsyBand *b1 = &s->psy.ch[s->cur_channel+1].psy_bands[(w+w2)*16+g];
894 const float *L = sce0->coeffs + start + (w+w2)*128;
895 const float *R = sce1->coeffs + start + (w+w2)*128;
896 float el = 0.0f, er = 0.0f, em = 0.0f, es = 0.0f, d = 0.0f;
897 for (int i = 0; i < len; i++) {
898 float m = (L[i] + R[i]) * 0.5f;
899 float sv = m - R[i];
900 el += L[i]*L[i]; er += R[i]*R[i];
901 em += m*m; es += sv*sv; d += L[i]*R[i];
902 }
903 ener0 += el; ener1 += er; dot += d; es_tot += es; em_tot += em;
904 minthr0 = FFMIN(minthr0, b0->threshold);
905 minthr1 = FFMIN(minthr1, b1->threshold);
906 }
907 float thr_g = FFMIN(minthr0, minthr1) * gl; /* group masking budget */
908
909 /* PNS-stereo reservation: keep clearly-wide noise bands for PNS. */
910 const int sidx = w*16+g;
911 {
912 float es_w = es_tot, em_w = em_tot;
913 if (s->nmr) {
914 int pi_ = ((s->cur_channel >> 1) & 7) * 2 + (cpe->ch[0].ics.num_windows == 8);
915 float pe = s->nmr->sema_es[pi_][sidx];
916 float pm = s->nmr->sema_em[pi_][sidx];
917 if (pm > 0.0f) {
918 es_w = NMR_SDEC_EMA * pe + (1.0f - NMR_SDEC_EMA) * es_tot;
919 em_w = NMR_SDEC_EMA * pm + (1.0f - NMR_SDEC_EMA) * em_tot;
920 }
921 }
922 if (cpe->ch[0].can_pns[w*16+g] && cpe->ch[1].can_pns[w*16+g] &&
923 es_w > NMR_PNS_STEREO_DECORR * em_w)
924 continue;
925 }
926 cpe->ch[0].can_pns[w*16+g] = cpe->ch[1].can_pns[w*16+g] = 0;
927
928 int pi = ((s->cur_channel >> 1) & 7) * 2 + (cpe->ch[0].ics.num_windows == 8);
929 uint8_t *pmode = s->nmr ? s->nmr->smode[pi] : NULL;
930 int prev = pmode ? pmode[sidx] : 0;
931 float eqgate = NMR_MS_EQUIV * (prev == 1 ? 1.5f : 1.0f); /* stay-until es>0.75em */
932 /* I/S = lossy economy: image-error budget scales with pressure */
933 float imgate = NMR_IS_IMG_GATE * is_ramp * (prev == 2 ? NMR_STICKY : 1.0f);
934 float es_d = es_tot, em_d = em_tot;
935 if (s->nmr) {
936 float *ees = &s->nmr->sema_es[pi][sidx];
937 float *eem = &s->nmr->sema_em[pi][sidx];
938 if (*eem <= 0.0f) { *ees = es_tot; *eem = em_tot; }
939 else {
940 *ees = NMR_SDEC_EMA * *ees + (1.0f - NMR_SDEC_EMA) * es_tot;
941 *eem = NMR_SDEC_EMA * *eem + (1.0f - NMR_SDEC_EMA) * em_tot;
942 }
943 es_d = *ees; em_d = *eem;
944 }
945 int ms_would = s->options.mid_side &&
946 (s->options.mid_side == 1 ||
947 es_d < eqgate * em_d ||
948 es_tot < NMR_MS_MASK * thr_g);
949 int ms_ok = ms_would && !decoupled;
950 float scale, sr_, imgratio; int p;
951 /* I/S competes with M/S above the frequency limit (candidacy must
952 * not be gated on !ms_ok - that leaves only unrenderable bands) */
953 int is_cand = start * freq_mult > NMR_IS_LOW_LIMIT &&
954 ener0 > FLT_MIN && ener1 > FLT_MIN &&
955 nmr_is_image_masked(s, cpe, w, g, start, len, gl,
956 ener0, ener1, dot, minthr0, minthr1,
957 &imgratio, &scale, &sr_, &p);
958 int is_ok = is_cand;
959 if (s->nmr && start * freq_mult > NMR_IS_LOW_LIMIT) {
960 /* smoothed image-error; updated only while candidate (fail-value
961 * feeding jammed it permanently high) */
962 float *eim = &s->nmr->sema_img[pi][sidx];
963 if (is_cand) {
964 /* seed from first measurement; freeze when not candidate */
965 if (*eim < 0.0f) *eim = imgratio;
966 else *eim = NMR_SDEC_EMA * *eim + (1.0f - NMR_SDEC_EMA) * FFMIN(imgratio, 100.0f * NMR_IS_IMG_GATE);
967 }
968 is_ok = is_cand && *eim >= 0.0f && *eim < imgate;
969 }
970
971 njoint += ms_would || is_ok; nbands++;
972 if (pmode) {
973 int m_ = (is_ok && allow_is) ? 2 : ms_ok ? 1 :
974 (is_ok && s->options.mid_side) ? 1 : 0;
975 pmode[sidx] = m_;
976 s->nmr->smode_band[(s->cur_channel >> 1) & 7][w*16+g] = m_;
977 }
978 if (is_ok && allow_is) {
979 nmr_apply_is_band(s, cpe, w, g, start, len, gl,
980 scale, sr_, p, ener0, ener1);
981 is_count++;
982 } else if (ms_ok || (is_ok && s->options.mid_side)) {
983 nmr_apply_ms_band(s, cpe, w, g, start, len, gl);
984 }
985 /* else: keep full L/R stereo */
986 }
987 }
988 cpe->is_mode = !!is_count;
989
990 if (nbands > 0) {
991 /* Pair joint-tool value, read next frame by the psy pair-synced window
992 * decision and the M/S candidacy above. Measured as CANDIDACY (not
993 * adoption) so decoupling cannot starve its own signal and self-lock. */
994 float r = (float)njoint / nbands;
995 float *pj = &s->psy.pair_joint[pidx];
996 *pj = *pj > 0.0f ? 0.95f * *pj + 0.05f * r : r;
997 s->psy.pair_decoupled[pidx] = *pj <
998 (s->psy.pair_decoupled[pidx] ? 1.3f * NMR_DECORR_LO : NMR_DECORR_LO);
999 }
1000}
1001
1003{
1004 int w, w2, g, i;
1005 IndividualChannelStream *ics = &cpe->ch[0].ics;
1006 if (!cpe->common_window)
1007 return;
1008 for (w = 0; w < ics->num_windows; w += ics->group_len[w]) {
1009 for (w2 = 0; w2 < ics->group_len[w]; w2++) {
1010 int start = (w+w2) * 128;
1011 for (g = 0; g < ics->num_swb; g++) {
1012 /* ms_mask can be used for other purposes in PNS and I/S,
1013 * so must not apply M/S if any band uses either, even if
1014 * ms_mask is set.
1015 */
1016 if (!cpe->ms_mask[w*16 + g] || cpe->is_mask[w*16 + g]
1017 || cpe->ch[0].band_type[w*16 + g] >= NOISE_BT
1018 || cpe->ch[1].band_type[w*16 + g] >= NOISE_BT) {
1019 start += ics->swb_sizes[g];
1020 continue;
1021 }
1022 for (i = 0; i < ics->swb_sizes[g]; i++) {
1023 float L = (cpe->ch[0].coeffs[start+i] + cpe->ch[1].coeffs[start+i]) * 0.5f;
1024 float R = L - cpe->ch[1].coeffs[start+i];
1025 cpe->ch[0].coeffs[start+i] = L;
1026 cpe->ch[1].coeffs[start+i] = R;
1027 }
1028 start += ics->swb_sizes[g];
1029 }
1030 }
1031 }
1032}
1033
1034/**
1035 * Encode scalefactor band coding type.
1036 */
1038{
1039 int w;
1040
1041 if (s->coder->set_special_band_scalefactors)
1042 s->coder->set_special_band_scalefactors(s, sce);
1043
1044 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
1045 s->coder->encode_window_bands_info(s, sce, w, sce->ics.group_len[w], s->lambda);
1046}
1047
1048/**
1049 * Encode scalefactors.
1050 */
1053{
1054 int diff, off_sf = sce->sf_idx[0], off_pns = sce->sf_idx[0] - NOISE_OFFSET;
1055 int off_is = 0, noise_flag = 1;
1056 int i, w;
1057
1058 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
1059 for (i = 0; i < sce->ics.max_sfb; i++) {
1060 if (!sce->zeroes[w*16 + i]) {
1061 if (sce->band_type[w*16 + i] == NOISE_BT) {
1062 diff = sce->sf_idx[w*16 + i] - off_pns;
1063 off_pns = sce->sf_idx[w*16 + i];
1064 if (noise_flag-- > 0) {
1066 continue;
1067 }
1068 } else if (sce->band_type[w*16 + i] == INTENSITY_BT ||
1069 sce->band_type[w*16 + i] == INTENSITY_BT2) {
1070 diff = sce->sf_idx[w*16 + i] - off_is;
1071 off_is = sce->sf_idx[w*16 + i];
1072 } else {
1073 diff = sce->sf_idx[w*16 + i] - off_sf;
1074 off_sf = sce->sf_idx[w*16 + i];
1075 }
1077 av_assert0(diff >= 0 && diff <= 120);
1079 }
1080 }
1081 }
1082}
1083
1084/**
1085 * Encode pulse data.
1086 */
1087static void encode_pulses(AACEncContext *s, Pulse *pulse)
1088{
1089 int i;
1090
1091 put_bits(&s->pb, 1, !!pulse->num_pulse);
1092 if (!pulse->num_pulse)
1093 return;
1094
1095 put_bits(&s->pb, 2, pulse->num_pulse - 1);
1096 put_bits(&s->pb, 6, pulse->start);
1097 for (i = 0; i < pulse->num_pulse; i++) {
1098 put_bits(&s->pb, 5, pulse->pos[i]);
1099 put_bits(&s->pb, 4, pulse->amp[i]);
1100 }
1101}
1102
1103/**
1104 * Encode spectral coefficients processed by psychoacoustic model.
1105 */
1107{
1108 int start, i, w, w2;
1109
1110 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
1111 start = 0;
1112 for (i = 0; i < sce->ics.max_sfb; i++) {
1113 if (sce->zeroes[w*16 + i]) {
1114 start += sce->ics.swb_sizes[i];
1115 continue;
1116 }
1117 for (w2 = w; w2 < w + sce->ics.group_len[w]; w2++) {
1118 s->coder->quantize_and_encode_band(s, &s->pb,
1119 &sce->coeffs[start + w2*128],
1120 NULL, sce->ics.swb_sizes[i],
1121 sce->sf_idx[w*16 + i],
1122 sce->band_type[w*16 + i],
1123 s->lambda,
1124 sce->ics.window_clipping[w]);
1125 }
1126 start += sce->ics.swb_sizes[i];
1127 }
1128 }
1129}
1130
1131/**
1132 * Downscale spectral coefficients for near-clipping windows to avoid artifacts
1133 */
1135{
1136 int start, i, j, w;
1137
1138 if (sce->ics.clip_avoidance_factor < 1.0f) {
1139 for (w = 0; w < sce->ics.num_windows; w++) {
1140 start = 0;
1141 for (i = 0; i < sce->ics.max_sfb; i++) {
1142 float *swb_coeffs = &sce->coeffs[start + w*128];
1143 for (j = 0; j < sce->ics.swb_sizes[i]; j++)
1144 swb_coeffs[j] *= sce->ics.clip_avoidance_factor;
1145 start += sce->ics.swb_sizes[i];
1146 }
1147 }
1148 }
1149}
1150
1151/**
1152 * Encode one channel of audio data.
1153 */
1156 int common_window)
1157{
1158 put_bits(&s->pb, 8, sce->sf_idx[0]);
1159 if (!common_window)
1160 put_ics_info(s, &sce->ics);
1161 encode_band_info(s, sce);
1162 encode_scale_factors(avctx, s, sce);
1163 encode_pulses(s, &sce->pulse);
1164 put_bits(&s->pb, 1, !!sce->tns.present);
1165 if (s->coder->encode_tns_info)
1166 s->coder->encode_tns_info(s, sce);
1167 put_bits(&s->pb, 1, 0); //ssr
1169 return 0;
1170}
1171
1172/**
1173 * Write some auxiliary information about the created AAC file.
1174 */
1175static void put_bitstream_info(AACEncContext *s, const char *name)
1176{
1177 int i, namelen, padbits;
1178
1179 namelen = strlen(name) + 2;
1180 put_bits(&s->pb, 3, TYPE_FIL);
1181 put_bits(&s->pb, 4, FFMIN(namelen, 15));
1182 if (namelen >= 15)
1183 put_bits(&s->pb, 8, namelen - 14);
1184 put_bits(&s->pb, 4, 0); //extension type - filler
1185 padbits = -put_bits_count(&s->pb) & 7;
1186 align_put_bits(&s->pb);
1187 for (i = 0; i < namelen - 2; i++)
1188 put_bits(&s->pb, 8, name[i]);
1189 put_bits(&s->pb, 12 - padbits, 0);
1190}
1191
1192/*
1193 * Copy input samples.
1194 * Channels are reordered from libavcodec's default order to AAC order.
1195 */
1197{
1198 int ch;
1199 int end = 2048 + (frame ? frame->nb_samples : 0);
1200 const uint8_t *channel_map = s->reorder_map;
1201
1202 /* copy and remap input samples */
1203 for (ch = 0; ch < s->channels; ch++) {
1204 /* copy last 1024 samples of previous frame to the start of the current frame */
1205 memcpy(&s->planar_samples[ch][1024], &s->planar_samples[ch][2048], 1024 * sizeof(s->planar_samples[0][0]));
1206
1207 /* copy new samples and zero any remaining samples */
1208 if (frame) {
1209 memcpy(&s->planar_samples[ch][2048],
1210 frame->extended_data[channel_map[ch]],
1211 frame->nb_samples * sizeof(s->planar_samples[0][0]));
1212 }
1213 memset(&s->planar_samples[ch][end], 0,
1214 (3072 - end) * sizeof(s->planar_samples[0][0]));
1215 }
1216}
1217
1218static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
1219 const AVFrame *frame, int *got_packet_ptr)
1220{
1221 AACEncContext *s = avctx->priv_data;
1222 float **samples = s->planar_samples, *samples2, *la, *overlap;
1223 ChannelElement *cpe;
1226 int i, its, ch, w, chans, tag, start_ch, ret, frame_bits;
1227 int target_bits, rate_bits, too_many_bits, too_few_bits;
1228 int ms_mode = 0, is_mode = 0, tns_mode = 0, pred_mode = 0;
1229 int chan_el_counter[4];
1231
1232 /* add current frame to queue */
1233 if (frame) {
1234 if ((ret = ff_af_queue_add(&s->afq, frame)) < 0)
1235 return ret;
1236 } else {
1237 if (!s->afq.remaining_samples || (!s->afq.frame_alloc && !s->afq.frame_count))
1238 return 0;
1239 }
1240
1242
1243 if (!avctx->frame_num)
1244 return 0;
1245
1246 start_ch = 0;
1247 for (i = 0; i < s->chan_map[0]; i++) {
1248 FFPsyWindowInfo* wi = windows + start_ch;
1249 tag = s->chan_map[i+1];
1250 chans = tag == TYPE_CPE ? 2 : 1;
1251 cpe = &s->cpe[i];
1252 {
1253 int wi_paired = 0;
1254 /* Synced pair windows: decide both channels of a CPE together so
1255 * their block switching never diverges (see psy window_pair). */
1256 if (chans == 2 && tag != TYPE_LFE && s->psy.model->window_pair && frame) {
1257 const float *ov0 = &samples[start_ch][0], *ov1 = &samples[start_ch + 1][0];
1258 s->psy.model->window_pair(&s->psy,
1259 ov0 + 1024, ov0 + 1024 + 448 + 64,
1260 ov1 + 1024, ov1 + 1024 + 448 + 64,
1261 start_ch, start_ch + 1,
1262 cpe->ch[0].ics.window_sequence[0],
1263 cpe->ch[1].ics.window_sequence[0],
1264 wi);
1265 wi_paired = 1;
1266 }
1267 for (ch = 0; ch < chans; ch++) {
1268 int k;
1269 float clip_avoidance_factor;
1270 sce = &cpe->ch[ch];
1271 ics = &sce->ics;
1272 s->cur_channel = start_ch + ch;
1273 overlap = &samples[s->cur_channel][0];
1274 samples2 = overlap + 1024;
1275 la = samples2 + (448+64);
1276 if (!frame)
1277 la = NULL;
1278 if (tag == TYPE_LFE) {
1279 wi[ch].window_type[0] = wi[ch].window_type[1] = ONLY_LONG_SEQUENCE;
1280 wi[ch].window_shape = 0;
1281 wi[ch].num_windows = 1;
1282 wi[ch].grouping[0] = 1;
1283 wi[ch].clipping[0] = 0;
1284
1285 /* Only the lowest 12 coefficients are used in a LFE channel.
1286 * The expression below results in only the bottom 8 coefficients
1287 * being used for 11.025kHz to 16kHz sample rates.
1288 */
1289 ics->num_swb = s->samplerate_index >= 8 ? 1 : 3;
1290 } else if (!wi_paired) {
1291 wi[ch] = s->psy.model->window(&s->psy, samples2, la, s->cur_channel,
1292 ics->window_sequence[0]);
1293 }
1294 ics->window_sequence[1] = ics->window_sequence[0];
1295 ics->window_sequence[0] = wi[ch].window_type[0];
1296 ics->use_kb_window[1] = ics->use_kb_window[0];
1297 ics->use_kb_window[0] = wi[ch].window_shape;
1298 ics->num_windows = wi[ch].num_windows;
1299 ics->swb_sizes = s->psy.bands [ics->num_windows == 8];
1300 ics->num_swb = tag == TYPE_LFE ? ics->num_swb : s->psy.num_bands[ics->num_windows == 8];
1301 ics->max_sfb = FFMIN(ics->max_sfb, ics->num_swb);
1302 ics->swb_offset = wi[ch].window_type[0] == EIGHT_SHORT_SEQUENCE ?
1303 ff_swb_offset_128 [s->samplerate_index]:
1304 ff_swb_offset_1024[s->samplerate_index];
1305 ics->tns_max_bands = wi[ch].window_type[0] == EIGHT_SHORT_SEQUENCE ?
1306 ff_tns_max_bands_128 [s->samplerate_index]:
1307 ff_tns_max_bands_1024[s->samplerate_index];
1308
1309 for (w = 0; w < ics->num_windows; w++)
1310 ics->group_len[w] = wi[ch].grouping[w];
1311
1312 /* Calculate input sample maximums and evaluate clipping risk */
1313 clip_avoidance_factor = 0.0f;
1314 for (w = 0; w < ics->num_windows; w++) {
1315 const float *wbuf = overlap + w * 128;
1316 const int wlen = 2048 / ics->num_windows;
1317 float max = 0;
1318 int j;
1319 /* mdct input is 2 * output */
1320 for (j = 0; j < wlen; j++)
1321 max = FFMAX(max, fabsf(wbuf[j]));
1322 wi[ch].clipping[w] = max;
1323 }
1324 for (w = 0; w < ics->num_windows; w++) {
1325 if (wi[ch].clipping[w] > CLIP_AVOIDANCE_FACTOR) {
1326 ics->window_clipping[w] = 1;
1327 clip_avoidance_factor = FFMAX(clip_avoidance_factor, wi[ch].clipping[w]);
1328 } else {
1329 ics->window_clipping[w] = 0;
1330 }
1331 }
1332 if (clip_avoidance_factor > CLIP_AVOIDANCE_FACTOR) {
1333 ics->clip_avoidance_factor = CLIP_AVOIDANCE_FACTOR / clip_avoidance_factor;
1334 } else {
1335 ics->clip_avoidance_factor = 1.0f;
1336 }
1337
1338 apply_window_and_mdct(s, sce, overlap);
1339
1340 for (k = 0; k < 1024; k++) {
1341 if (!(fabs(cpe->ch[ch].coeffs[k]) < 1E16)) { // Ensure headroom for energy calculation
1342 av_log(avctx, AV_LOG_ERROR, "Input contains (near) NaN/+-Inf\n");
1343 return AVERROR(EINVAL);
1344 }
1345 }
1346 avoid_clipping(s, sce);
1347 }
1348 }
1349 start_ch += chans;
1350 }
1351 if ((ret = ff_alloc_packet(avctx, avpkt, 8192 * s->channels)) < 0)
1352 return ret;
1353 frame_bits = its = 0;
1354 do {
1355 init_put_bits(&s->pb, avpkt->data, avpkt->size);
1356
1357 if ((avctx->frame_num & 0xFF)==1 && !(avctx->flags & AV_CODEC_FLAG_BITEXACT))
1359 start_ch = 0;
1360 target_bits = 0;
1361 memset(chan_el_counter, 0, sizeof(chan_el_counter));
1362 for (i = 0; i < s->chan_map[0]; i++) {
1363 FFPsyWindowInfo* wi = windows + start_ch;
1364 const float *coeffs[2];
1365 tag = s->chan_map[i+1];
1366 chans = tag == TYPE_CPE ? 2 : 1;
1367 cpe = &s->cpe[i];
1368 cpe->common_window = 0;
1369 memset(cpe->is_mask, 0, sizeof(cpe->is_mask));
1370 memset(cpe->ms_mask, 0, sizeof(cpe->ms_mask));
1371 put_bits(&s->pb, 3, tag);
1372 put_bits(&s->pb, 4, chan_el_counter[tag]++);
1373 for (ch = 0; ch < chans; ch++) {
1374 sce = &cpe->ch[ch];
1375 coeffs[ch] = sce->coeffs;
1376 memset(&sce->tns, 0, sizeof(TemporalNoiseShaping));
1377 for (w = 0; w < 128; w++)
1378 if (sce->band_type[w] > RESERVED_BT)
1379 sce->band_type[w] = 0;
1380 }
1381 s->psy.bitres.alloc = -1;
1382 s->psy.bitres.bits = s->last_frame_pb_count / s->channels;
1383 s->psy.model->analyze(&s->psy, start_ch, coeffs, wi);
1384 if (s->psy.bitres.alloc > 0) {
1385 /* Lambda unused here on purpose, we need to take psy's unscaled allocation */
1386 target_bits += s->psy.bitres.alloc
1387 * (s->lambda / (avctx->global_quality ? avctx->global_quality : 120));
1388 s->psy.bitres.alloc /= chans;
1389 }
1390 s->cur_type = tag;
1391 if (chans > 1
1392 && wi[0].window_type[0] == wi[1].window_type[0]
1393 && wi[0].window_shape == wi[1].window_shape) {
1394
1395 cpe->common_window = 1;
1396 for (w = 0; w < wi[0].num_windows; w++) {
1397 if (wi[0].grouping[w] != wi[1].grouping[w]) {
1398 cpe->common_window = 0;
1399 break;
1400 }
1401 }
1402 }
1403
1404 const int use_tns = s->options.tns && s->coder->search_for_tns &&
1405 s->coder->apply_tns_filt;
1406
1407 /* The NMR coder rate-controls itself and never re-quantizes, so TNS must run
1408 * before the quantizer */
1409 const int tns_first = s->options.coder == AAC_CODER_NMR;
1410 if (tns_first && use_tns) {
1411 for (ch = 0; ch < chans; ch++) {
1412 sce = &cpe->ch[ch];
1413 s->cur_channel = start_ch + ch;
1414 /* mono: mark_pns before TNS so the region cap sees PNS bands. Stereo
1415 * PNS is marked in its own block (below) after the stereo decision. */
1416 if (chans == 1 && s->options.pns && s->coder->mark_pns)
1417 s->coder->mark_pns(s, avctx, sce);
1418 s->coder->search_for_tns(s, sce);
1419 s->coder->apply_tns_filt(s, sce);
1420 if (sce->tns.present)
1421 tns_mode = 1;
1422 }
1423 }
1424
1425 /* NMR stereo PNS (imaging-safe). Mark each channel's noise-like bands on the
1426 * original L/R psy, then keep PNS only where BOTH channels are noise-like. */
1427 if (chans == 2 && cpe->common_window && tns_first &&
1428 s->options.pns && s->coder->mark_pns) {
1429 s->cur_channel = start_ch; s->coder->mark_pns(s, avctx, &cpe->ch[0]);
1430 s->cur_channel = start_ch + 1; s->coder->mark_pns(s, avctx, &cpe->ch[1]);
1431 for (int b = 0; b < 128; b++)
1432 if (!cpe->ch[0].can_pns[b] || !cpe->ch[1].can_pns[b])
1433 cpe->ch[0].can_pns[b] = cpe->ch[1].can_pns[b] = 0;
1434 }
1435
1436 /* The NMR coder decides I/S and M/S BEFORE quantization, from the psy model,
1437 * and the trellis then allocates natively on the coeffs actually coded. */
1438 if (chans == 2 && cpe->common_window && s->options.coder == AAC_CODER_NMR &&
1439 (s->options.mid_side || s->options.intensity_stereo)) {
1440 s->cur_channel = start_ch;
1441 nmr_decide_stereo(s, cpe);
1442 }
1443 /* NMR pools the CPE bit budget: both channels of a pair are solved
1444 * jointly under one shared lambda (see aaccoder_nmr.h). */
1445 if (s->options.coder == AAC_CODER_NMR && s->nmr)
1446 s->nmr->pair = (chans == 2);
1447 for (ch = 0; ch < chans; ch++) {
1448 s->cur_channel = start_ch + ch;
1449 /* NMR PNS is mono-only */
1450 if (s->options.pns && s->coder->mark_pns && !tns_first)
1451 s->coder->mark_pns(s, avctx, &cpe->ch[ch]);
1452 s->coder->search_for_quantizers(avctx, s, &cpe->ch[ch], s->lambda);
1453 }
1454 for (ch = 0; ch < chans; ch++) { /* TNS (non-NMR) and PNS */
1455 sce = &cpe->ch[ch];
1456 s->cur_channel = start_ch + ch;
1457 if (!tns_first && use_tns) {
1458 s->coder->search_for_tns(s, sce);
1459 s->coder->apply_tns_filt(s, sce);
1460 if (sce->tns.present)
1461 tns_mode = 1;
1462 }
1463 if (s->options.pns && s->coder->search_for_pns)
1464 s->coder->search_for_pns(s, avctx, sce);
1465 }
1466 s->cur_channel = start_ch;
1467 if (s->options.intensity_stereo) { /* Intensity Stereo */
1468 if (s->options.coder != AAC_CODER_NMR) { /* NMR: decided pre-search */
1469 if (s->coder->search_for_is)
1470 s->coder->search_for_is(s, avctx, cpe);
1472 }
1473 if (cpe->is_mode) is_mode = 1;
1474 }
1475 if (s->options.mid_side && s->options.coder != AAC_CODER_NMR) { /* Mid/Side stereo */
1476 if (s->options.mid_side == -1 && s->coder->search_for_ms)
1477 s->coder->search_for_ms(s, cpe);
1478 else if (cpe->common_window)
1479 memset(cpe->ms_mask, 1, sizeof(cpe->ms_mask));
1481 }
1482 adjust_frame_information(cpe, chans);
1483 if (chans == 2) {
1484 put_bits(&s->pb, 1, cpe->common_window);
1485 if (cpe->common_window) {
1486 put_ics_info(s, &cpe->ch[0].ics);
1487 encode_ms_info(&s->pb, cpe);
1488 if (cpe->ms_mode) ms_mode = 1;
1489 }
1490 }
1491 for (ch = 0; ch < chans; ch++) {
1492 s->cur_channel = start_ch + ch;
1493 encode_individual_channel(avctx, s, &cpe->ch[ch], cpe->common_window);
1494 }
1495 start_ch += chans;
1496 }
1497
1498 if (avctx->flags & AV_CODEC_FLAG_QSCALE) {
1499 /* When using a constant Q-scale, don't mess with lambda */
1500 break;
1501 }
1502
1503 frame_bits = put_bits_count(&s->pb);
1504
1505 /* The NMR coder rate-controls itself (global-lambda reservoir servo):
1506 * per-frame bits intentionally float around the nominal rate, so skip
1507 * the lambda rate loop and only intervene on a hard overflow. */
1508 if (s->options.coder == AAC_CODER_NMR && avctx->bit_rate_tolerance != 0 &&
1509 frame_bits < 6144 * s->channels - 3)
1510 break;
1511
1512 /* rate control stuff
1513 * allow between the nominal bitrate, and what psy's bit reservoir says to target
1514 * but drift towards the nominal bitrate always
1515 */
1516 rate_bits = avctx->bit_rate * 1024 / avctx->sample_rate;
1517 rate_bits = FFMIN(rate_bits, 6144 * s->channels - 3);
1518 too_many_bits = FFMAX(target_bits, rate_bits);
1519 too_many_bits = FFMIN(too_many_bits, 6144 * s->channels - 3);
1520 too_few_bits = FFMIN(FFMAX(rate_bits - rate_bits/4, target_bits), too_many_bits);
1521
1522 /* When strict bit-rate control is demanded */
1523 if (avctx->bit_rate_tolerance == 0) {
1524 if (rate_bits < frame_bits) {
1525 float ratio = ((float)rate_bits) / frame_bits;
1526 s->lambda *= FFMIN(0.9f, ratio);
1527 continue;
1528 }
1529 /* reset lambda when solution is found */
1530 s->lambda = avctx->global_quality > 0 ? avctx->global_quality : 120;
1531 break;
1532 }
1533
1534 /* When using ABR, be strict (but only for increasing) */
1535 too_few_bits = too_few_bits - too_few_bits/8;
1536 too_many_bits = too_many_bits + too_many_bits/2;
1537
1538 if ( its == 0 /* for steady-state Q-scale tracking */
1539 || (its < 5 && (frame_bits < too_few_bits || frame_bits > too_many_bits))
1540 || frame_bits >= 6144 * s->channels - 3 )
1541 {
1542 float ratio = ((float)rate_bits) / frame_bits;
1543
1544 if (frame_bits >= too_few_bits && frame_bits <= too_many_bits) {
1545 /*
1546 * This path is for steady-state Q-scale tracking
1547 * When frame bits fall within the stable range, we still need to adjust
1548 * lambda to maintain it like so in a stable fashion (large jumps in lambda
1549 * create artifacts and should be avoided), but slowly
1550 */
1551 ratio = sqrtf(sqrtf(ratio));
1552 ratio = av_clipf(ratio, 0.9f, 1.1f);
1553 } else {
1554 /* Not so fast though */
1555 ratio = sqrtf(ratio);
1556 }
1557 s->lambda = av_clipf(s->lambda * ratio, FLT_EPSILON, 65536.f);
1558
1559 /* Keep iterating if we must reduce and lambda is in the sky */
1560 if (ratio > 0.9f && ratio < 1.1f) {
1561 break;
1562 } else {
1563 if (is_mode || ms_mode || tns_mode || pred_mode) {
1564 for (i = 0; i < s->chan_map[0]; i++) {
1565 // Must restore coeffs
1566 chans = tag == TYPE_CPE ? 2 : 1;
1567 cpe = &s->cpe[i];
1568 for (ch = 0; ch < chans; ch++)
1569 memcpy(cpe->ch[ch].coeffs, cpe->ch[ch].pcoeffs, sizeof(cpe->ch[ch].coeffs));
1570 }
1571 }
1572 its++;
1573 }
1574 } else {
1575 break;
1576 }
1577 } while (1);
1578
1579 /* tool-usage stats over the final per-band decisions of this frame */
1580 for (i = 0; i < s->chan_map[0]; i++) {
1581 int etag = s->chan_map[i + 1], echans = etag == TYPE_CPE ? 2 : 1;
1582 ChannelElement *ce = &s->cpe[i];
1583 IndividualChannelStream *ics = &ce->ch[0].ics;
1584 for (ch = 0; ch < echans; ch++) { /* per-channel frame stats */
1585 int is_short = ce->ch[ch].ics.window_sequence[0] == EIGHT_SHORT_SEQUENCE;
1586 s->stat_chans++;
1587 if (is_short)
1588 s->stat_short++;
1589 if (ce->ch[ch].tns.present) {
1590 if (is_short) s->stat_tns_short++;
1591 else s->stat_tns_long++;
1592 }
1593 }
1594 for (w = 0; w < ics->num_windows; w += ics->group_len[w]) {
1595 for (int g = 0; g < ics->num_swb; g++) {
1596 int idx = w*16 + g, coded = 0;
1597 for (ch = 0; ch < echans; ch++) {
1598 SingleChannelElement *sce = &ce->ch[ch];
1599 if (sce->zeroes[idx] && sce->band_type[idx] == 0)
1600 continue;
1601 s->stat_ch_bands++;
1602 if (sce->band_type[idx] == NOISE_BT)
1603 s->stat_pns++;
1604 coded = 1;
1605 }
1606 if (etag == TYPE_CPE && coded) {
1607 s->stat_cpe_bands++;
1608 if (ce->ms_mask[idx]) s->stat_ms++;
1609 if (ce->is_mask[idx]) s->stat_is++;
1610 }
1611 }
1612 }
1613 }
1614
1615 put_bits(&s->pb, 3, TYPE_END);
1616 flush_put_bits(&s->pb);
1617
1618 s->last_frame_pb_count = put_bits_count(&s->pb);
1619
1620 /* NMR rate accounting: how many bits the frame really took beyond what the
1621 * trellis counted; feeds the next frame's budget correction */
1622 if (s->nmr) {
1623 int counted = 0;
1624 for (i = 0; i < s->channels; i++)
1625 counted += s->nmr->counted[i];
1626 if (counted > 0) {
1627 float side = (float)s->last_frame_pb_count - counted;
1628 if (s->nmr->side_inited) {
1629 s->nmr->side_ema += 0.125f * (side - s->nmr->side_ema);
1630 } else {
1631 s->nmr->side_ema = side;
1632 s->nmr->side_inited = 1;
1633 }
1634 }
1635 }
1636 avpkt->size = put_bytes_output(&s->pb);
1637
1638 s->lambda_sum += (s->nmr && s->nmr->lam_rc > 0.0f) ? s->nmr->lam_rc : s->lambda;
1639 s->lambda_count++;
1640
1641 ret = ff_af_queue_remove(&s->afq, avctx->frame_size, avpkt);
1642 if (ret < 0)
1643 return ret;
1644
1645 avpkt->flags |= AV_PKT_FLAG_KEY;
1646
1647 *got_packet_ptr = 1;
1648 return 0;
1649}
1650
1652{
1653 AACEncContext *s = avctx->priv_data;
1654
1655 av_log(avctx, AV_LOG_INFO,
1656 "Qavg: %.3f Tr: %.1f%% TNS(L): %.1f%% TNS(S): %.1f%% M/S: %.1f%% I/S: %.1f%% PNS: %.1f%%\n",
1657 s->lambda_count ? s->lambda_sum / s->lambda_count : NAN,
1658 s->stat_chans ? 100.0 * s->stat_short / s->stat_chans : 0.0,
1659 s->stat_chans - s->stat_short ? 100.0 * s->stat_tns_long / (s->stat_chans - s->stat_short) : 0.0,
1660 s->stat_short ? 100.0 * s->stat_tns_short / s->stat_short : 0.0,
1661 s->stat_cpe_bands ? 100.0 * s->stat_ms / s->stat_cpe_bands : 0.0,
1662 s->stat_cpe_bands ? 100.0 * s->stat_is / s->stat_cpe_bands : 0.0,
1663 s->stat_ch_bands ? 100.0 * s->stat_pns / s->stat_ch_bands : 0.0);
1664
1665 av_tx_uninit(&s->mdct1024);
1666 av_tx_uninit(&s->mdct128);
1667 ff_psy_end(&s->psy);
1668 ff_lpc_end(&s->lpc);
1669 av_freep(&s->buffer.samples);
1670 av_freep(&s->cpe);
1671 av_freep(&s->fdsp);
1672 av_freep(&s->nmr);
1673 ff_af_queue_close(&s->afq);
1674 return 0;
1675}
1676
1678{
1679 int ret = 0;
1680 float scale = 32768.0f;
1681
1683 if (!s->fdsp)
1684 return AVERROR(ENOMEM);
1685
1686 if ((ret = av_tx_init(&s->mdct1024, &s->mdct1024_fn, AV_TX_FLOAT_MDCT, 0,
1687 1024, &scale, 0)) < 0)
1688 return ret;
1689 if ((ret = av_tx_init(&s->mdct128, &s->mdct128_fn, AV_TX_FLOAT_MDCT, 0,
1690 128, &scale, 0)) < 0)
1691 return ret;
1692
1693 return 0;
1694}
1695
1697{
1698 int ch;
1699 if (!FF_ALLOCZ_TYPED_ARRAY(s->buffer.samples, s->channels * 3 * 1024) ||
1700 !FF_ALLOCZ_TYPED_ARRAY(s->cpe, s->chan_map[0]))
1701 return AVERROR(ENOMEM);
1702
1703 for(ch = 0; ch < s->channels; ch++)
1704 s->planar_samples[ch] = s->buffer.samples + 3 * 1024 * ch;
1705
1706 if (s->options.coder == AAC_CODER_NMR) {
1707 s->nmr = av_mallocz(sizeof(*s->nmr));
1708 if (!s->nmr)
1709 return AVERROR(ENOMEM);
1710 }
1711
1712 return 0;
1713}
1714
1716{
1717 for (int i = 0; i < avctx->ch_layout.nb_channels; i++) {
1720 return 1;
1721 // Layouts with TOP_SIDE channels also include the above.
1722 }
1723
1724 return 0;
1725}
1726
1728{
1729 AACEncContext *s = avctx->priv_data;
1730 int i, ret = 0;
1731 int chcfg;
1732 const uint8_t *sizes[2];
1733 uint8_t grouping[AAC_MAX_CHANNELS];
1734 int lengths[2];
1735
1736 /* Constants */
1737 s->last_frame_pb_count = 0;
1738 avctx->frame_size = 1024;
1739 avctx->initial_padding = 1024;
1740 s->lambda = avctx->global_quality > 0 ? avctx->global_quality : 120;
1741
1742 /* Channel map and unspecified bitrate guessing */
1743 s->channels = avctx->ch_layout.nb_channels;
1744
1745 s->needs_pce = 1;
1746 for (chcfg = 1; chcfg < FF_ARRAY_ELEMS(aac_normal_chan_layouts); chcfg++) {
1748 s->needs_pce = s->options.pce;
1749 break;
1750 }
1751 }
1752
1753 if (s->needs_pce) {
1754 char buf[64];
1755 for (i = 0; i < FF_ARRAY_ELEMS(aac_pce_configs); i++)
1756 if (!av_channel_layout_compare(&avctx->ch_layout, &aac_pce_configs[i].layout))
1757 break;
1758 av_channel_layout_describe(&avctx->ch_layout, buf, sizeof(buf));
1760 av_log(avctx, AV_LOG_ERROR, "Unsupported channel layout \"%s\"\n", buf);
1761 return AVERROR(EINVAL);
1762 }
1763 av_log(avctx, AV_LOG_INFO, "Using a PCE to encode channel layout \"%s\"\n", buf);
1764 s->pce = aac_pce_configs[i];
1765 s->reorder_map = s->pce.reorder_map;
1766 s->chan_map = s->pce.config_map;
1767 s->needs_height_ext = check_height_ext(avctx, s);
1768 chcfg = 0;
1769 } else {
1770 s->reorder_map = aac_chan_maps[chcfg - 1];
1771 s->chan_map = aac_chan_configs[chcfg - 1];
1772 }
1773
1774 if (!avctx->bit_rate) {
1775 for (i = 1; i <= s->chan_map[0]; i++) {
1776 avctx->bit_rate += s->chan_map[i] == TYPE_CPE ? 128000 : /* Pair */
1777 s->chan_map[i] == TYPE_LFE ? 16000 : /* LFE */
1778 69000 ; /* SCE */
1779 }
1780 }
1781
1782 /* Samplerate */
1783 for (int i = 0;; i++) {
1784 av_assert1(i < 13);
1785 if (avctx->sample_rate == ff_mpeg4audio_sample_rates[i]) {
1786 s->samplerate_index = i;
1787 break;
1788 }
1789 }
1790
1791 /* Bitrate limiting */
1792 WARN_IF(1024.0 * avctx->bit_rate / avctx->sample_rate > 6144 * s->channels,
1793 "Too many bits %f > %d per frame requested, clamping to max\n",
1794 1024.0 * avctx->bit_rate / avctx->sample_rate,
1795 6144 * s->channels);
1796 avctx->bit_rate = (int64_t)FFMIN(6144 * s->channels / 1024.0 * avctx->sample_rate,
1797 avctx->bit_rate);
1798
1799 /* Profile and option setting */
1801 avctx->profile;
1802 for (i = 0; i < FF_ARRAY_ELEMS(aacenc_profiles); i++)
1803 if (avctx->profile == aacenc_profiles[i])
1804 break;
1805 ERROR_IF(i == FF_ARRAY_ELEMS(aacenc_profiles), "Profile not supported!\n");
1806 if (avctx->profile == AV_PROFILE_MPEG2_AAC_LOW) {
1807 avctx->profile = AV_PROFILE_AAC_LOW;
1808 WARN_IF(s->options.pns,
1809 "PNS unavailable in the \"mpeg2_aac_low\" profile, turning off\n");
1810 s->options.pns = 0;
1811 }
1812 s->profile = avctx->profile;
1813
1814 /* Coder limitations */
1815 s->coder = &ff_aac_coders[s->options.coder];
1816
1817 /* M/S introduces horrible artifacts with multichannel files, this is temporary */
1818 if (s->channels > 3)
1819 s->options.mid_side = 0;
1820
1821 /* Coding bandwidth, fixed at init time */
1822 if (avctx->cutoff > 0) {
1823 s->bandwidth = avctx->cutoff;
1824 } else {
1825 int frame_br = (avctx->flags & AV_CODEC_FLAG_QSCALE) ?
1826 (avctx->bit_rate / 2.0f * (s->lambda / 120.f) * 1.5f) :
1827 (avctx->bit_rate / avctx->ch_layout.nb_channels);
1828
1829 if (s->options.coder == AAC_CODER_NMR && frame_br >= 24000) {
1830 static const int rates[] = { 24000, 32000, 48000, 64000, 96000, 192000 };
1831 static const int bws[] = { 14000, 14000, 18500, 20000, 21000, 22000 };
1832 int bw_i = 0;
1833 for (; bw_i < FF_ARRAY_ELEMS(rates) - 2 && frame_br > rates[bw_i + 1]; bw_i++);
1834 s->bandwidth = bws[bw_i] + (int)((int64_t)(bws[bw_i + 1] - bws[bw_i]) *
1835 (frame_br - rates[bw_i]) / (rates[bw_i + 1] - rates[bw_i]));
1836 s->bandwidth = FFMIN3(s->bandwidth, 22000, avctx->sample_rate / 2);
1837 } else {
1838 if (s->options.pns || s->options.intensity_stereo)
1839 frame_br *= 1.15f;
1840 s->bandwidth = FFMAX(3000, AAC_CUTOFF_FROM_BITRATE(frame_br, 1,
1841 avctx->sample_rate));
1842 }
1843
1844 s->bandwidth = FFMIN(FFMAX(s->bandwidth, 8000), avctx->sample_rate / 2);
1845 }
1846
1847 if (!(avctx->flags & AV_CODEC_FLAG_QSCALE) && avctx->bit_rate > 0) {
1848 int bpc = avctx->bit_rate / avctx->ch_layout.nb_channels;
1849 if (bpc <= 32000 && avctx->sample_rate > 32000)
1850 av_log(avctx, AV_LOG_INFO,
1851 "%d kb/s per channel at %d Hz: consider resampling the "
1852 "input to 32000 Hz or lower for better quality.\n",
1853 bpc / 1000, avctx->sample_rate);
1854 }
1855
1856 // Initialize static tables
1858
1859 if ((ret = dsp_init(avctx, s)) < 0)
1860 return ret;
1861
1862 if ((ret = alloc_buffers(avctx, s)) < 0)
1863 return ret;
1864
1865 if ((ret = put_audio_specific_config(avctx, chcfg)))
1866 return ret;
1867
1868 sizes[0] = ff_aac_swb_size_1024[s->samplerate_index];
1869 sizes[1] = ff_aac_swb_size_128[s->samplerate_index];
1870 lengths[0] = ff_aac_num_swb_1024[s->samplerate_index];
1871 lengths[1] = ff_aac_num_swb_128[s->samplerate_index];
1872 for (i = 0; i < s->chan_map[0]; i++)
1873 grouping[i] = s->chan_map[i + 1] == TYPE_CPE;
1874 if ((ret = ff_psy_init(&s->psy, avctx, 2, sizes, lengths,
1875 s->chan_map[0], grouping, s->bandwidth)) < 0)
1876 return ret;
1878 s->random_state = 0x1f2e3d4c;
1879
1880 ff_aacenc_dsp_init(&s->aacdsp);
1881
1882 ff_af_queue_init(avctx, &s->afq);
1883
1884 return 0;
1885}
1886
1887#define AACENC_FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
1888static const AVOption aacenc_options[] = {
1889 {"aac_coder", "Coding algorithm", offsetof(AACEncContext, options.coder), AV_OPT_TYPE_INT, {.i64 = AAC_CODER_NMR}, 0, AAC_CODER_NB-1, AACENC_FLAGS, .unit = "coder"},
1890 {"twoloop", "Two loop searching method", 0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_TWOLOOP}, INT_MIN, INT_MAX, AACENC_FLAGS, .unit = "coder"},
1891 {"fast", "Fast search", 0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_FAST}, INT_MIN, INT_MAX, AACENC_FLAGS, .unit = "coder"},
1892 {"nmr", "Noise-to-mask ratio scalefactor trellis", 0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_NMR}, INT_MIN, INT_MAX, AACENC_FLAGS, .unit = "coder"},
1893 {"aac_ms", "Force M/S stereo coding", offsetof(AACEncContext, options.mid_side), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, AACENC_FLAGS},
1894 {"aac_is", "Intensity stereo coding", offsetof(AACEncContext, options.intensity_stereo), AV_OPT_TYPE_BOOL, {.i64 = 1}, -1, 1, AACENC_FLAGS},
1895 {"aac_pns", "Perceptual noise substitution", offsetof(AACEncContext, options.pns), AV_OPT_TYPE_BOOL, {.i64 = 1}, -1, 1, AACENC_FLAGS},
1896 {"aac_tns", "Temporal noise shaping", offsetof(AACEncContext, options.tns), AV_OPT_TYPE_BOOL, {.i64 = 1}, -1, 1, AACENC_FLAGS},
1897 {"aac_pce", "Forces the use of PCEs", offsetof(AACEncContext, options.pce), AV_OPT_TYPE_BOOL, {.i64 = 0}, -1, 1, AACENC_FLAGS},
1898 {"aac_nmr_speed", "NMR coder speed level: 0 = slowest/best, higher trades quality for speed", offsetof(AACEncContext, options.nmr_speed), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 4, AACENC_FLAGS},
1900 {NULL}
1901};
1902
1903static const AVClass aacenc_class = {
1904 .class_name = "AAC encoder",
1905 .item_name = av_default_item_name,
1906 .option = aacenc_options,
1907 .version = LIBAVUTIL_VERSION_INT,
1908};
1909
1911 { "b", "0" },
1912 { NULL }
1913};
1914
1916 .p.name = "aac",
1917 CODEC_LONG_NAME("AAC (Advanced Audio Coding)"),
1918 .p.type = AVMEDIA_TYPE_AUDIO,
1919 .p.id = AV_CODEC_ID_AAC,
1920 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
1922 .priv_data_size = sizeof(AACEncContext),
1925 .close = aac_encode_end,
1926 .defaults = aac_encode_defaults,
1928 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1930 .p.priv_class = &aacenc_class,
1931};
AAC definitions and structures.
@ EIGHT_SHORT_SEQUENCE
Definition aac.h:66
@ LONG_STOP_SEQUENCE
Definition aac.h:67
@ ONLY_LONG_SEQUENCE
Definition aac.h:64
@ LONG_START_SEQUENCE
Definition aac.h:65
#define NOISE_PRE
preamble for NOISE_BT, put in bitstream with the first noise band
Definition aac.h:99
@ INTENSITY_BT
Scalefactor data are intensity stereo positions (in phase).
Definition aac.h:77
@ INTENSITY_BT2
Scalefactor data are intensity stereo positions (out of phase).
Definition aac.h:76
@ RESERVED_BT
Band types following are encoded differently from others.
Definition aac.h:74
@ NOISE_BT
Spectral data are scaled white noise not coded in the bitstream.
Definition aac.h:75
@ TYPE_CPE
Definition aac.h:45
@ TYPE_SCE
Definition aac.h:44
@ TYPE_FIL
Definition aac.h:50
@ TYPE_LFE
Definition aac.h:47
@ TYPE_END
Definition aac.h:51
#define TNS_MAX_ORDER
Definition aac.h:36
#define NOISE_PRE_BITS
length of preamble
Definition aac.h:100
#define SCALE_DIFF_ZERO
codebook index corresponding to zero scalefactor indices difference
Definition aac.h:95
#define NOISE_OFFSET
subtracted from global gain, used as offset for the preamble
Definition aac.h:101
const AACCoefficientsEncoder ff_aac_coders[AAC_CODER_NB]
Definition aaccoder.c:827
static void put_bitstream_info(AACEncContext *s, const char *name)
Write some auxiliary information about the created AAC file.
Definition aacenc.c:1175
static void avoid_clipping(AACEncContext *s, SingleChannelElement *sce)
Downscale spectral coefficients for near-clipping windows to avoid artifacts.
Definition aacenc.c:1134
static void apply_mid_side_stereo(ChannelElement *cpe)
Definition aacenc.c:1002
static void adjust_frame_information(ChannelElement *cpe, int chans)
Produce integer coefficients from scalefactors provided by the model.
Definition aacenc.c:655
static int encode_individual_channel(AVCodecContext *avctx, AACEncContext *s, SingleChannelElement *sce, int common_window)
Encode one channel of audio data.
Definition aacenc.c:1154
#define NMR_SDEC_EMA
Definition aacenc.c:751
static av_cold int dsp_init(AVCodecContext *avctx, AACEncContext *s)
Definition aacenc.c:1677
static const AVOption aacenc_options[]
Definition aacenc.c:1888
static int put_audio_specific_config(AVCodecContext *avctx, int chcfg)
Make AAC audio config object.
Definition aacenc.c:494
const FFCodec ff_aac_encoder
Definition aacenc.c:1915
static void(*const apply_window[4])(AVFloatDSPContext *fdsp, SingleChannelElement *sce, const float *audio)
Definition aacenc.c:590
static void nmr_apply_ms_band(AACEncContext *s, ChannelElement *cpe, int w, int g, int start, int len, int gl)
Definition aacenc.c:757
static void encode_pulses(AACEncContext *s, Pulse *pulse)
Encode pulse data.
Definition aacenc.c:1087
#define WINDOW_FUNC(type)
Definition aacenc.c:534
static const AVClass aacenc_class
Definition aacenc.c:1903
static av_cold int aac_encode_end(AVCodecContext *avctx)
Definition aacenc.c:1651
static void encode_band_info(AACEncContext *s, SingleChannelElement *sce)
Encode scalefactor band coding type.
Definition aacenc.c:1037
static int nmr_is_image_masked(AACEncContext *s, ChannelElement *cpe, int w, int g, int start, int len, int gl, float ener0, float ener1, float dot, float minthr0, float minthr1, float *ratio_out, float *scale_out, float *sr_out, int *p_out)
Definition aacenc.c:781
#define NMR_MS_MASK
Definition aacenc.c:740
static const AACPCEInfo aac_pce_configs[]
List of PCE (Program Configuration Element) for the channel layouts listed in channel_layout....
Definition aacenc.c:94
static void nmr_decide_stereo(AACEncContext *s, ChannelElement *cpe)
Definition aacenc.c:843
static void apply_window_and_mdct(AACEncContext *s, SingleChannelElement *sce, float *audio)
Definition aacenc.c:599
#define NMR_IS_IMG_GATE
Definition aacenc.c:733
static av_cold int aac_encode_init(AVCodecContext *avctx)
Definition aacenc.c:1727
static void apply_intensity_stereo(ChannelElement *cpe)
Definition aacenc.c:703
static void put_pce(PutBitContext *pb, AVCodecContext *avctx)
Definition aacenc.c:433
#define NMR_IS_LOW_LIMIT
Definition aacenc.c:736
static void encode_ms_info(PutBitContext *pb, ChannelElement *cpe)
Encode MS data.
Definition aacenc.c:641
static void put_ics_info(AACEncContext *s, IndividualChannelStream *info)
Encode ics_info element.
Definition aacenc.c:620
#define AACENC_FLAGS
Definition aacenc.c:1887
static const FFCodecDefault aac_encode_defaults[]
Definition aacenc.c:1910
static void encode_spectral_coeffs(AACEncContext *s, SingleChannelElement *sce)
Encode spectral coefficients processed by psychoacoustic model.
Definition aacenc.c:1106
#define NMR_STICKY
Definition aacenc.c:748
static av_cold int check_height_ext(AVCodecContext *avctx, AACEncContext *s)
Definition aacenc.c:1715
static void nmr_apply_is_band(AACEncContext *s, ChannelElement *cpe, int w, int g, int start, int len, int gl, float scale, float sr_, int p, float ener0, float ener1)
Definition aacenc.c:813
static av_cold int alloc_buffers(AVCodecContext *avctx, AACEncContext *s)
Definition aacenc.c:1696
#define NMR_MS_EQUIV
Definition aacenc.c:739
static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition aacenc.c:1218
static void copy_input_samples(AACEncContext *s, const AVFrame *frame)
Definition aacenc.c:1196
#define NMR_DECORR_LO
Definition aacenc.c:745
static void encode_scale_factors(AVCodecContext *avctx, AACEncContext *s, SingleChannelElement *sce)
Encode scalefactors.
Definition aacenc.c:1051
#define NMR_PNS_STEREO_DECORR
Definition aacenc.c:754
void ff_quantize_band_cost_cache_init(struct AACEncContext *s)
Definition aacenc.c:525
@ AAC_CODER_FAST
Definition aacenc.h:46
@ AAC_CODER_NMR
Definition aacenc.h:47
@ AAC_CODER_NB
Definition aacenc.h:49
@ AAC_CODER_TWOLOOP
Definition aacenc.h:45
#define CLIP_AVOIDANCE_FACTOR
Definition aacenc.h:42
AAC encoder utilities.
#define WARN_IF(cond,...)
#define ERROR_IF(cond,...)
const uint8_t *const ff_aac_swb_size_1024[]
Definition aacenctab.c:97
const uint8_t *const ff_aac_swb_size_128[]
Definition aacenctab.c:89
AAC encoder data.
#define AAC_MAX_CHANNELS
Definition aacenctab.h:41
static const uint8_t aac_chan_maps[14][AAC_MAX_CHANNELS]
Table to remap channels from libavcodec's default order to AAC order.
Definition aacenctab.h:86
static const AVChannelLayout aac_normal_chan_layouts[15]
Definition aacenctab.h:47
static const uint8_t aac_chan_configs[14][6]
default channel configurations
Definition aacenctab.h:66
static const int aacenc_profiles[]
Definition aacenctab.h:145
const uint32_t ff_aac_scalefactor_code[121]
Definition aactab.c:181
const uint8_t ff_tns_max_bands_1024[]
Definition aactab.c:1974
const uint16_t *const ff_swb_offset_128[]
Definition aactab.c:1940
const uint16_t *const ff_swb_offset_1024[]
Definition aactab.c:1900
const uint8_t ff_aac_scalefactor_bits[121]
Definition aactab.c:200
const uint8_t ff_aac_num_swb_1024[]
Definition aactab.c:149
const uint8_t ff_aac_num_swb_128[]
Definition aactab.c:169
const uint8_t ff_tns_max_bands_128[]
Definition aactab.c:1990
AAC data declarations.
float ff_aac_kbd_long_1024[1024]
void ff_aac_float_common_init(void)
float ff_aac_kbd_short_128[128]
static FILE * out
channels
Definition aptx.h:31
#define L(x)
Definition vpx_arith.h:36
static const uint8_t channel_map[8][8]
av_cold void ff_af_queue_close(AudioFrameQueue *afq)
Close AudioFrameQueue.
av_cold void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
Initialize AudioFrameQueue.
int ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, AVPacket *pkt)
Remove frame(s) from the queue.
int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f)
Add a frame to the queue.
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition avassert.h:58
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
Libavcodec external API header.
void ff_copy_bits(PutBitContext *pb, const uint8_t *src, int length)
Copy the content of src to the bitstream.
Definition bitstream.c:49
void ff_put_string(PutBitContext *pb, const char *string, int terminate_string)
Put the string string in the bitstream.
Definition bitstream.c:39
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
#define CODEC_SAMPLERATES_ARRAY(array)
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define CODEC_SAMPLEFMTS(...)
#define av_clipf
Definition common.h:145
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
Public header for CRC hash function implementation.
static __device__ float sqrtf(float a)
static __device__ float fabsf(float a)
static __device__ float fabs(float a)
#define max(a, b)
#define AV_PROFILE_MPEG2_AAC_LOW
Definition defs.h:77
#define AV_PROFILE_UNKNOWN
Definition defs.h:65
#define AV_PROFILE_AAC_LOW
Definition defs.h:69
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
Definition encode.c:62
static const uint8_t bits[8]
Definition fastaudio.c:100
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
#define AV_CH_LAYOUT_5POINT1POINT2_BACK
#define AV_CH_LAYOUT_5POINT1POINT4
#define AV_CH_BOTTOM_FRONT_CENTER
#define AV_CH_TOP_BACK_RIGHT
#define AV_CH_TOP_BACK_LEFT
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition avcodec.h:322
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition codec.h:79
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_FLAG_QSCALE
Use fixed qscale.
Definition avcodec.h:213
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
Definition codec.h:84
@ AV_CODEC_ID_AAC
Definition codec_id.h:455
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:650
#define AV_CHANNEL_LAYOUT_4POINT0
#define AV_CHANNEL_LAYOUT_9POINT1POINT4
#define AV_CHANNEL_LAYOUT_7POINT2POINT3
#define AV_CHANNEL_LAYOUT_5POINT1POINT2
#define AV_CHANNEL_LAYOUT_HEXAGONAL
#define AV_CHANNEL_LAYOUT_4POINT1
#define AV_CHANNEL_LAYOUT_3POINT1
#define AV_CHANNEL_LAYOUT_7POINT0_FRONT
#define AV_CHANNEL_LAYOUT_5POINT1_BACK
#define AV_CHANNEL_LAYOUT_7POINT1_WIDE
#define AV_CHANNEL_LAYOUT_7POINT1POINT2
#define AV_CHANNEL_LAYOUT_6POINT1_FRONT
#define AV_CHANNEL_LAYOUT_5POINT0
#define AV_CHANNEL_LAYOUT_AMBISONIC_FIRST_ORDER
#define AV_CHANNEL_LAYOUT_7POINT1POINT4
#define AV_CHANNEL_LAYOUT_STEREO
int av_channel_layout_compare(const AVChannelLayout *chl, const AVChannelLayout *chl1)
Check whether two channel layouts are semantically the same, i.e.
#define AV_CHANNEL_LAYOUT_6POINT0
#define AV_CHANNEL_LAYOUT_2_2
#define AV_CHANNEL_LAYOUT_6POINT1_BACK
#define AV_CHANNEL_LAYOUT_5POINT0_BACK
#define AV_CHANNEL_LAYOUT_5POINT1
#define AV_CHANNEL_LAYOUT_MONO
enum AVChannel av_channel_layout_channel_from_index(const AVChannelLayout *channel_layout, unsigned int idx)
Get the channel with the given index in a channel layout.
#define AV_CHANNEL_LAYOUT_7POINT0
AVChannel
#define AV_CHANNEL_LAYOUT_SURROUND
#define AV_CHANNEL_LAYOUT_5POINT1POINT2_BACK
#define AV_CHANNEL_LAYOUT_2_1
#define AV_CHANNEL_LAYOUT_OCTAGONAL
int av_channel_layout_describe(const AVChannelLayout *channel_layout, char *buf, size_t buf_size)
Get a human-readable string describing the channel layout properties.
#define AV_CHANNEL_LAYOUT_7POINT1
#define AV_CHANNEL_LAYOUT_5POINT1POINT4
#define AV_CHANNEL_LAYOUT_9POINT1POINT6
#define AV_CHANNEL_LAYOUT_QUAD
#define AV_CHANNEL_LAYOUT_6POINT0_FRONT
#define AV_CHANNEL_LAYOUT_2POINT1
#define AV_CHANNEL_LAYOUT_7POINT1_WIDE_BACK
#define AV_CHANNEL_LAYOUT_6POINT1
@ AV_CHANNEL_ORDER_NATIVE
The native channel order, i.e.
@ AV_CHANNEL_ORDER_AMBISONIC
The audio is represented as the decomposition of the sound field into spherical harmonics.
@ AV_CHAN_TOP_FRONT_LEFT
@ AV_CHAN_TOP_BACK_RIGHT
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition crc.c:389
uint32_t AVCRC
Definition crc.h:46
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
Definition crc.c:421
@ AV_CRC_8_ATM
Definition crc.h:49
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition samplefmt.h:66
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
#define R
Definition huffyuv.h:44
static const int sizes[][2]
Definition img2dec.c:62
#define r
Definition input.c:42
#define b
Definition input.c:43
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition intra.c:278
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition j2kenc.c:154
void ff_aacenc_dsp_init(AACEncDSPContext *s)
Definition aacencdsp.c:75
av_cold void ff_lpc_end(LPCContext *s)
Uninitialize LPCContext.
Definition lpc.c:367
av_cold int ff_lpc_init(LPCContext *s, int blocksize, int max_order, enum FFLPCType lpc_type)
Initialize LPCContext.
Definition lpc.c:342
Libavcodec version macros.
#define LIBAVCODEC_IDENT
Definition version.h:43
#define av_cold
Definition attributes.h:117
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
Definition float_dsp.c:135
#define FF_ALLOCZ_TYPED_ARRAY(p, nelem)
Definition internal.h:72
Replacements for frequently missing libm functions.
uint8_t w
Definition llvidencdsp.c:39
@ FF_LPC_TYPE_LEVINSON
Levinson-Durbin recursion.
Definition lpc.h:46
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
#define FFMIN3(a, b, c)
Definition macros.h:50
#define NAN
Memory handling functions.
uint32_t tag
Definition movenc.c:2087
const int ff_mpeg4audio_sample_rates[16]
@ AOT_SBR
Y Spectral Band Replication.
Definition mpeg4audio.h:78
AVOptions.
#define FF_AAC_PROFILE_OPTS
Definition profiles.h:29
av_cold int ff_psy_init(FFPsyContext *ctx, AVCodecContext *avctx, int num_lens, const uint8_t **bands, const int *num_bands, int num_groups, const uint8_t *group_map, int cutoff)
Initialize psychoacoustic model.
Definition psymodel.c:28
av_cold void ff_psy_end(FFPsyContext *ctx)
Cleanup model context at the end.
Definition psymodel.c:77
#define AAC_CUTOFF_FROM_BITRATE(bit_rate, channels, sample_rate)
Definition psymodel.h:35
bitstream writer API
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition put_bits.h:62
static int put_bits_count(PutBitContext *s)
Definition put_bits.h:90
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition put_bits.h:153
static int put_bytes_output(const PutBitContext *s)
Definition put_bits.h:99
static void align_put_bits(PutBitContext *s)
Pad the bitstream with zeros up to the next byte boundary.
Definition put_bits.h:445
const char * name
Definition qsvenc.c:142
#define FF_ARRAY_ELEMS(a)
AAC encoder context.
Definition aacenc.h:259
uint8_t num_ele[4]
front, side, back, lfe
Definition aacenc.h:248
uint8_t index[4][8]
front, side, back, lfe
Definition aacenc.h:250
uint8_t pairing[3][8]
front, side, back
Definition aacenc.h:249
uint8_t height[3][8]
front, side, back
Definition aacenc.h:251
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1055
int global_quality
Global quality for codecs which cannot change it per frame.
Definition avcodec.h:1235
int64_t frame_num
Frame counter, set by libavcodec.
Definition avcodec.h:1883
int bit_rate_tolerance
number of bits the bitstream is allowed to diverge from the reference.
Definition avcodec.h:1227
int64_t bit_rate
the average bitrate
Definition avcodec.h:493
int profile
profile
Definition avcodec.h:1636
int initial_padding
Audio only.
Definition avcodec.h:1114
int sample_rate
samples per second
Definition avcodec.h:1040
int flags
AV_CODEC_FLAG_*.
Definition avcodec.h:500
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition avcodec.h:526
int extradata_size
Definition avcodec.h:527
int cutoff
Audio cutoff bandwidth (0 means "automatic")
Definition avcodec.h:1082
int frame_size
Number of samples per channel in an audio frame.
Definition avcodec.h:1068
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
int flags
A combination of AV_PKT_FLAG values.
Definition packet.h:609
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
channel element - generic struct for SCE/CPE/CCE/LFE
Definition aacdec.h:296
uint8_t ms_mask[128]
Set if mid/side stereo is used for each scalefactor window band.
Definition aacdec.h:300
SingleChannelElement ch[2]
Definition aacdec.h:302
uint8_t is_mask[128]
Set if intensity stereo is used.
Definition aacenc.h:135
int ms_mode
Signals mid/side stereo flags coding mode.
Definition aacenc.h:132
int common_window
Set if channels share a common 'IndividualChannelStream' in bitstream.
Definition aacenc.h:131
uint8_t is_mode
Set if any bands have been encoded using intensity stereo.
Definition aacenc.h:133
single band psychoacoustic information
Definition psymodel.h:50
windowing related information
Definition psymodel.h:77
int num_windows
number of windows in a frame
Definition psymodel.h:80
int grouping[8]
window grouping (for e.g. AAC)
Definition psymodel.h:81
float clipping[8]
maximum absolute normalized intensity in the given window for clip avoidance
Definition psymodel.h:82
int window_shape
window shape (sine/KBD/whatever)
Definition psymodel.h:79
int window_type[3]
window type (short/long/transitional, etc.) - current, previous and next
Definition psymodel.h:78
Individual Channel Stream.
Definition aacdec.h:169
uint8_t max_sfb
number of scalefactor bands per group
Definition aacdec.h:170
int num_swb
number of scalefactor window bands
Definition aacdec.h:178
uint8_t group_len[8]
Definition aacdec.h:175
uint8_t use_kb_window[2]
If set, use Kaiser-Bessel window, otherwise use a sine window.
Definition aacdec.h:172
float clip_avoidance_factor
set if any window is near clipping to the necessary atennuation factor to avoid it
Definition aacenc.h:90
const uint8_t * swb_sizes
table of scalefactor band sizes for a particular window
Definition aacenc.h:85
enum WindowSequence window_sequence[2]
Definition aacdec.h:171
const uint16_t * swb_offset
table of offsets to the lowest spectral coefficient of a scalefactor band, sfb, for a particular wind...
Definition aacdec.h:177
uint8_t window_clipping[8]
set if a certain window is near clipping
Definition aacdec.h:185
Definition aac.h:103
int pos[4]
Definition aac.h:106
int start
Definition aac.h:105
int amp[4]
Definition aac.h:107
int num_pulse
Definition aac.h:104
Single Channel Element - used for both SCE and LFE elements.
Definition aacdec.h:217
float pcoeffs[1024]
coefficients for IMDCT, pristine
Definition aacenc.h:120
uint8_t zeroes[128]
band is not coded
Definition aacenc.h:116
float coeffs[1024]
coefficients for IMDCT, maybe processed
Definition aacenc.h:121
float is_ener[128]
Intensity stereo pos.
Definition aacenc.h:118
uint8_t can_pns[128]
band is allowed to PNS (informative)
Definition aacenc.h:117
TemporalNoiseShaping tns
Definition aacdec.h:220
float ret_buf[2048]
PCM output buffer.
Definition aacenc.h:122
enum BandType band_type[128]
band types
Definition aacdec.h:221
IndividualChannelStream ics
Definition aacdec.h:218
int sf_idx[128]
scalefactor indices
Definition aacenc.h:115
Temporal Noise Shaping.
Definition aacdec.h:191
#define av_mallocz(s)
#define av_freep(p)
#define av_log(a,...)
static const int rates[]
Definition swresample.c:99
av_cold void av_tx_uninit(AVTXContext **ctx)
Frees a context and sets *ctx to NULL, does nothing when *ctx == NULL.
Definition tx.c:295
av_cold int av_tx_init(AVTXContext **ctx, av_tx_fn *tx, enum AVTXType type, int inv, int len, const void *scale, uint64_t flags)
Initialize a transform context with the given configuration (i)MDCTs with an odd length are currently...
Definition tx.c:903
@ AV_TX_FLOAT_MDCT
Standard MDCT with a sample data type of float, double or int32_t, respectively.
Definition tx.h:68
const char * g
Definition vf_curves.c:128
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
static double b1(void *priv, double x, double y)
Definition vf_xfade.c:2034
static double b0(void *priv, double x, double y)
Definition vf_xfade.c:2033
int len
static double c[64]