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