blob: 2a34685aecca17c765bd8a1c1f16f8af0d20abba [file] [log] [blame]
Kostya Shishkovc03d9d02008-08-14 05:52:291/*
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/**
Diego Biurrunba87f082010-04-20 14:45:3423 * @file
Kostya Shishkovc03d9d02008-08-14 05:52:2924 * AAC encoder
25 */
26
27/***********************************
28 * TODOs:
Kostya Shishkov817015e2008-08-16 05:47:1829 * add sane pulse detection
Kostya Shishkovc03d9d02008-08-14 05:52:2930 ***********************************/
Michael Niedermayera7a7f322021-05-28 19:37:2631#include <float.h>
Kostya Shishkovc03d9d02008-08-14 05:52:2932
Andreas Rheinhardt1be3d8a2021-06-11 23:10:5833#include "libavutil/channel_layout.h"
Ganesh Ajjanagadde2e4fd162016-01-14 22:55:5634#include "libavutil/libm.h"
Justin Rugglesd5a72292012-05-21 16:58:4135#include "libavutil/float_dsp.h"
Nathan Caldwellcc9947f2011-06-01 05:38:0936#include "libavutil/opt.h"
Kostya Shishkovc03d9d02008-08-14 05:52:2937#include "avcodec.h"
Andreas Rheinhardta688f3c2022-03-16 17:18:2838#include "codec_internal.h"
Andreas Rheinhardt56e9e022021-05-11 13:17:1339#include "encode.h"
Alex Converse78e65cd2009-07-08 20:01:3140#include "put_bits.h"
Kostya Shishkovc03d9d02008-08-14 05:52:2941#include "mpeg4audio.h"
Mans Rullgard45387292011-03-19 23:44:0042#include "sinewin.h"
Marton Balintaee036c2020-05-10 18:38:1143#include "profiles.h"
Martin Storsjöf2da2e12022-02-23 10:55:4544#include "version.h"
Kostya Shishkovc03d9d02008-08-14 05:52:2945
Kostya Shishkovc03d9d02008-08-14 05:52:2946#include "aac.h"
47#include "aactab.h"
Alex Converse78e65cd2009-07-08 20:01:3148#include "aacenc.h"
Rostislav Pehlivanovc47c7812015-07-29 04:44:2649#include "aacenctab.h"
Rostislav Pehlivanovef8e5a62015-07-29 04:44:2750#include "aacenc_utils.h"
Alex Converse78e65cd2009-07-08 20:01:3151
52#include "psymodel.h"
Kostya Shishkovc03d9d02008-08-14 05:52:2953
Andreas Rheinhardtc488ee62022-08-01 03:42:0654/**
55 * List of PCE (Program Configuration Element) for the channel layouts listed
56 * in channel_layout.h
57 *
58 * For those wishing in the future to add other layouts:
59 *
60 * - num_ele: number of elements in each group of front, side, back, lfe channels
61 * (an element is of type SCE (single channel), CPE (channel pair) for
62 * the first 3 groups; and is LFE for LFE group).
63 *
64 * - pairing: 0 for an SCE element or 1 for a CPE; does not apply to LFE group
65 *
66 * - index: there are three independent indices for SCE, CPE and LFE;
67 * they are incremented irrespective of the group to which the element belongs;
68 * they are not reset when going from one group to another
69 *
70 * Example: for 7.0 channel layout,
71 * .pairing = { { 1, 0 }, { 1 }, { 1 }, }, (3 CPE and 1 SCE in front group)
72 * .index = { { 0, 0 }, { 1 }, { 2 }, },
73 * (index is 0 for the single SCE but goes from 0 to 2 for the CPEs)
74 *
75 * The index order impacts the channel ordering. But is otherwise arbitrary
76 * (the sequence could have been 2, 0, 1 instead of 0, 1, 2).
77 *
78 * Spec allows for discontinuous indices, e.g. if one has a total of two SCE,
79 * SCE.0 SCE.15 is OK per spec; BUT it won't be decoded by our AAC decoder
80 * which at this time requires that indices fully cover some range starting
81 * from 0 (SCE.1 SCE.0 is OK but not SCE.0 SCE.15).
82 *
83 * - config_map: total number of elements and their types. Beware, the way the
84 * types are ordered impacts the final channel ordering.
85 *
86 * - reorder_map: reorders the channels.
87 *
88 */
89static const AACPCEInfo aac_pce_configs[] = {
90 {
91 .layout = AV_CHANNEL_LAYOUT_MONO,
92 .num_ele = { 1, 0, 0, 0 },
93 .pairing = { { 0 }, },
94 .index = { { 0 }, },
95 .config_map = { 1, TYPE_SCE, },
96 .reorder_map = { 0 },
97 },
98 {
99 .layout = AV_CHANNEL_LAYOUT_STEREO,
100 .num_ele = { 1, 0, 0, 0 },
101 .pairing = { { 1 }, },
102 .index = { { 0 }, },
103 .config_map = { 1, TYPE_CPE, },
104 .reorder_map = { 0, 1 },
105 },
106 {
107 .layout = AV_CHANNEL_LAYOUT_2POINT1,
108 .num_ele = { 1, 0, 0, 1 },
109 .pairing = { { 1 }, },
110 .index = { { 0 },{ 0 },{ 0 },{ 0 } },
111 .config_map = { 2, TYPE_CPE, TYPE_LFE },
112 .reorder_map = { 0, 1, 2 },
113 },
114 {
115 .layout = AV_CHANNEL_LAYOUT_2_1,
116 .num_ele = { 1, 0, 1, 0 },
117 .pairing = { { 1 },{ 0 },{ 0 } },
118 .index = { { 0 },{ 0 },{ 0 }, },
119 .config_map = { 2, TYPE_CPE, TYPE_SCE },
120 .reorder_map = { 0, 1, 2 },
121 },
122 {
123 .layout = AV_CHANNEL_LAYOUT_SURROUND,
124 .num_ele = { 2, 0, 0, 0 },
125 .pairing = { { 1, 0 }, },
126 .index = { { 0, 0 }, },
127 .config_map = { 2, TYPE_CPE, TYPE_SCE, },
128 .reorder_map = { 0, 1, 2 },
129 },
130 {
131 .layout = AV_CHANNEL_LAYOUT_3POINT1,
132 .num_ele = { 2, 0, 0, 1 },
133 .pairing = { { 1, 0 }, },
134 .index = { { 0, 0 }, { 0 }, { 0 }, { 0 }, },
135 .config_map = { 3, TYPE_CPE, TYPE_SCE, TYPE_LFE },
136 .reorder_map = { 0, 1, 2, 3 },
137 },
138 {
139 .layout = AV_CHANNEL_LAYOUT_4POINT0,
140 .num_ele = { 2, 0, 1, 0 },
141 .pairing = { { 1, 0 }, { 0 }, { 0 }, },
142 .index = { { 0, 0 }, { 0 }, { 1 } },
143 .config_map = { 3, TYPE_CPE, TYPE_SCE, TYPE_SCE },
144 .reorder_map = { 0, 1, 2, 3 },
145 },
146 {
147 .layout = AV_CHANNEL_LAYOUT_4POINT1,
148 .num_ele = { 2, 1, 1, 0 },
149 .pairing = { { 1, 0 }, { 0 }, { 0 }, },
150 .index = { { 0, 0 }, { 1 }, { 2 }, { 0 } },
151 .config_map = { 4, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_SCE },
152 .reorder_map = { 0, 1, 2, 3, 4 },
153 },
154 {
155 .layout = AV_CHANNEL_LAYOUT_2_2,
156 .num_ele = { 1, 1, 0, 0 },
157 .pairing = { { 1 }, { 1 }, },
158 .index = { { 0 }, { 1 }, },
159 .config_map = { 2, TYPE_CPE, TYPE_CPE },
160 .reorder_map = { 0, 1, 2, 3 },
161 },
162 {
163 .layout = AV_CHANNEL_LAYOUT_QUAD,
164 .num_ele = { 1, 0, 1, 0 },
165 .pairing = { { 1 }, { 0 }, { 1 }, },
166 .index = { { 0 }, { 0 }, { 1 } },
167 .config_map = { 2, TYPE_CPE, TYPE_CPE },
168 .reorder_map = { 0, 1, 2, 3 },
169 },
170 {
171 .layout = AV_CHANNEL_LAYOUT_5POINT0,
172 .num_ele = { 2, 1, 0, 0 },
173 .pairing = { { 1, 0 }, { 1 }, },
174 .index = { { 0, 0 }, { 1 } },
175 .config_map = { 3, TYPE_CPE, TYPE_SCE, TYPE_CPE },
176 .reorder_map = { 0, 1, 2, 3, 4 },
177 },
178 {
179 .layout = AV_CHANNEL_LAYOUT_5POINT1,
180 .num_ele = { 2, 1, 1, 0 },
181 .pairing = { { 1, 0 }, { 0 }, { 1 }, },
182 .index = { { 0, 0 }, { 1 }, { 1 } },
183 .config_map = { 4, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE },
184 .reorder_map = { 0, 1, 2, 3, 4, 5 },
185 },
186 {
187 .layout = AV_CHANNEL_LAYOUT_5POINT0_BACK,
188 .num_ele = { 2, 0, 1, 0 },
189 .pairing = { { 1, 0 }, { 0 }, { 1 } },
190 .index = { { 0, 0 }, { 0 }, { 1 } },
191 .config_map = { 3, TYPE_CPE, TYPE_SCE, TYPE_CPE },
192 .reorder_map = { 0, 1, 2, 3, 4 },
193 },
194 {
195 .layout = AV_CHANNEL_LAYOUT_5POINT1_BACK,
196 .num_ele = { 2, 1, 1, 0 },
197 .pairing = { { 1, 0 }, { 0 }, { 1 }, },
198 .index = { { 0, 0 }, { 1 }, { 1 } },
199 .config_map = { 4, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE },
200 .reorder_map = { 0, 1, 2, 3, 4, 5 },
201 },
202 {
203 .layout = AV_CHANNEL_LAYOUT_6POINT0,
204 .num_ele = { 2, 1, 1, 0 },
205 .pairing = { { 1, 0 }, { 1 }, { 0 }, },
206 .index = { { 0, 0 }, { 1 }, { 1 } },
207 .config_map = { 4, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_SCE },
208 .reorder_map = { 0, 1, 2, 3, 4, 5 },
209 },
210 {
211 .layout = AV_CHANNEL_LAYOUT_6POINT0_FRONT,
212 .num_ele = { 2, 1, 0, 0 },
213 .pairing = { { 1, 1 }, { 1 } },
214 .index = { { 1, 0 }, { 2 }, },
215 .config_map = { 3, TYPE_CPE, TYPE_CPE, TYPE_CPE, },
216 .reorder_map = { 0, 1, 2, 3, 4, 5 },
217 },
218 {
219 .layout = AV_CHANNEL_LAYOUT_HEXAGONAL,
220 .num_ele = { 2, 0, 2, 0 },
221 .pairing = { { 1, 0 },{ 0 },{ 1, 0 }, },
222 .index = { { 0, 0 },{ 0 },{ 1, 1 } },
223 .config_map = { 4, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_SCE, },
224 .reorder_map = { 0, 1, 2, 3, 4, 5 },
225 },
226 {
227 .layout = AV_CHANNEL_LAYOUT_6POINT1,
228 .num_ele = { 2, 1, 2, 0 },
229 .pairing = { { 1, 0 },{ 0 },{ 1, 0 }, },
230 .index = { { 0, 0 },{ 1 },{ 1, 2 } },
231 .config_map = { 5, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE, TYPE_SCE },
232 .reorder_map = { 0, 1, 2, 3, 4, 5, 6 },
233 },
234 {
235 .layout = AV_CHANNEL_LAYOUT_6POINT1_BACK,
236 .num_ele = { 2, 1, 2, 0 },
237 .pairing = { { 1, 0 }, { 0 }, { 1, 0 }, },
238 .index = { { 0, 0 }, { 1 }, { 1, 2 } },
239 .config_map = { 5, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE, TYPE_SCE },
240 .reorder_map = { 0, 1, 2, 3, 4, 5, 6 },
241 },
242 {
243 .layout = AV_CHANNEL_LAYOUT_6POINT1_FRONT,
244 .num_ele = { 2, 1, 2, 0 },
245 .pairing = { { 1, 0 }, { 0 }, { 1, 0 }, },
246 .index = { { 0, 0 }, { 1 }, { 1, 2 } },
247 .config_map = { 5, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE, TYPE_SCE },
248 .reorder_map = { 0, 1, 2, 3, 4, 5, 6 },
249 },
250 {
251 .layout = AV_CHANNEL_LAYOUT_7POINT0,
252 .num_ele = { 2, 1, 1, 0 },
253 .pairing = { { 1, 0 }, { 1 }, { 1 }, },
254 .index = { { 0, 0 }, { 1 }, { 2 }, },
255 .config_map = { 4, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_CPE },
256 .reorder_map = { 0, 1, 2, 3, 4, 5, 6 },
257 },
258 {
259 .layout = AV_CHANNEL_LAYOUT_7POINT0_FRONT,
260 .num_ele = { 2, 1, 1, 0 },
261 .pairing = { { 1, 0 }, { 1 }, { 1 }, },
262 .index = { { 0, 0 }, { 1 }, { 2 }, },
263 .config_map = { 4, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_CPE },
264 .reorder_map = { 0, 1, 2, 3, 4, 5, 6 },
265 },
266 {
267 .layout = AV_CHANNEL_LAYOUT_7POINT1,
268 .num_ele = { 2, 1, 2, 0 },
269 .pairing = { { 1, 0 }, { 0 }, { 1, 1 }, },
270 .index = { { 0, 0 }, { 1 }, { 1, 2 }, { 0 } },
271 .config_map = { 5, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE, TYPE_CPE },
272 .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7 },
273 },
274 {
275 .layout = AV_CHANNEL_LAYOUT_7POINT1_WIDE,
276 .num_ele = { 2, 1, 2, 0 },
277 .pairing = { { 1, 0 }, { 0 },{ 1, 1 }, },
278 .index = { { 0, 0 }, { 1 }, { 1, 2 }, { 0 } },
279 .config_map = { 5, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE, TYPE_CPE },
280 .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7 },
281 },
282 {
283 .layout = AV_CHANNEL_LAYOUT_7POINT1_WIDE_BACK,
284 .num_ele = { 2, 1, 2, 0 },
285 .pairing = { { 1, 0 }, { 0 }, { 1, 1 }, },
286 .index = { { 0, 0 }, { 1 }, { 1, 2 }, { 0 } },
287 .config_map = { 5, TYPE_CPE, TYPE_SCE, TYPE_SCE, TYPE_CPE, TYPE_CPE },
288 .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7 },
289 },
290 {
291 .layout = AV_CHANNEL_LAYOUT_OCTAGONAL,
292 .num_ele = { 2, 1, 2, 0 },
293 .pairing = { { 1, 0 }, { 1 }, { 1, 0 }, },
294 .index = { { 0, 0 }, { 1 }, { 2, 1 } },
295 .config_map = { 5, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_SCE },
296 .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7 },
297 },
298 { /* Meant for order 2/mixed ambisonics */
299 .layout = { .order = AV_CHANNEL_ORDER_NATIVE, .nb_channels = 9,
300 .u.mask = AV_CH_LAYOUT_OCTAGONAL | AV_CH_TOP_CENTER },
301 .num_ele = { 2, 2, 2, 0 },
302 .pairing = { { 1, 0 }, { 1, 0 }, { 1, 0 }, },
303 .index = { { 0, 0 }, { 1, 1 }, { 2, 2 } },
304 .config_map = { 6, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_SCE },
305 .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7, 8 },
306 },
307 { /* Meant for order 2/mixed ambisonics */
308 .layout = { .order = AV_CHANNEL_ORDER_NATIVE, .nb_channels = 10,
309 .u.mask = AV_CH_LAYOUT_6POINT0_FRONT | AV_CH_BACK_CENTER |
310 AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT | AV_CH_TOP_CENTER },
311 .num_ele = { 2, 2, 2, 0 },
312 .pairing = { { 1, 1 }, { 1, 0 }, { 1, 0 }, },
313 .index = { { 0, 1 }, { 2, 0 }, { 3, 1 } },
314 .config_map = { 6, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_SCE },
315 .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
316 },
317 {
318 .layout = AV_CHANNEL_LAYOUT_HEXADECAGONAL,
319 .num_ele = { 4, 2, 4, 0 },
320 .pairing = { { 1, 0, 1, 0 }, { 1, 1 }, { 1, 0, 1, 0 }, },
321 .index = { { 0, 0, 1, 1 }, { 2, 3 }, { 4, 2, 5, 3 } },
322 .config_map = { 10, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_CPE, TYPE_CPE, TYPE_SCE, TYPE_CPE, TYPE_SCE },
323 .reorder_map = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
324 },
325};
326
Rostislav Pehlivanovfbf295e2016-10-03 18:53:11327static void put_pce(PutBitContext *pb, AVCodecContext *avctx)
328{
329 int i, j;
330 AACEncContext *s = avctx->priv_data;
331 AACPCEInfo *pce = &s->pce;
Rostislav Pehlivanov7b7775a2017-11-09 04:30:07332 const int bitexact = avctx->flags & AV_CODEC_FLAG_BITEXACT;
333 const char *aux_data = bitexact ? "Lavc" : LIBAVCODEC_IDENT;
Rostislav Pehlivanovfbf295e2016-10-03 18:53:11334
335 put_bits(pb, 4, 0);
336
337 put_bits(pb, 2, avctx->profile);
338 put_bits(pb, 4, s->samplerate_index);
339
340 put_bits(pb, 4, pce->num_ele[0]); /* Front */
341 put_bits(pb, 4, pce->num_ele[1]); /* Side */
342 put_bits(pb, 4, pce->num_ele[2]); /* Back */
343 put_bits(pb, 2, pce->num_ele[3]); /* LFE */
344 put_bits(pb, 3, 0); /* Assoc data */
345 put_bits(pb, 4, 0); /* CCs */
346
347 put_bits(pb, 1, 0); /* Stereo mixdown */
348 put_bits(pb, 1, 0); /* Mono mixdown */
349 put_bits(pb, 1, 0); /* Something else */
350
351 for (i = 0; i < 4; i++) {
352 for (j = 0; j < pce->num_ele[i]; j++) {
353 if (i < 3)
354 put_bits(pb, 1, pce->pairing[i][j]);
355 put_bits(pb, 4, pce->index[i][j]);
356 }
357 }
358
Anton Khirnov717503f2020-10-26 12:36:06359 align_put_bits(pb);
Rostislav Pehlivanov7b7775a2017-11-09 04:30:07360 put_bits(pb, 8, strlen(aux_data));
Anton Khirnov944ba302020-10-26 12:41:39361 ff_put_string(pb, aux_data, 0);
Rostislav Pehlivanovfbf295e2016-10-03 18:53:11362}
363
Nathan Caldwell9b8e2a82011-12-15 02:43:56364/**
Kostya Shishkovc03d9d02008-08-14 05:52:29365 * Make AAC audio config object.
366 * @see 1.6.2.1 "Syntax - AudioSpecificConfig"
367 */
Rostislav Pehlivanov7b7775a2017-11-09 04:30:07368static int put_audio_specific_config(AVCodecContext *avctx)
Kostya Shishkovc03d9d02008-08-14 05:52:29369{
370 PutBitContext pb;
371 AACEncContext *s = avctx->priv_data;
Rostislav Pehlivanovfbf295e2016-10-03 18:53:11372 int channels = (!s->needs_pce)*(s->channels - (s->channels == 8 ? 1 : 0));
Rostislav Pehlivanov7b7775a2017-11-09 04:30:07373 const int max_size = 32;
Kostya Shishkovc03d9d02008-08-14 05:52:29374
Rostislav Pehlivanov7b7775a2017-11-09 04:30:07375 avctx->extradata = av_mallocz(max_size);
376 if (!avctx->extradata)
377 return AVERROR(ENOMEM);
378
379 init_put_bits(&pb, avctx->extradata, max_size);
Rostislav Pehlivanoveab12d02015-08-21 18:20:22380 put_bits(&pb, 5, s->profile+1); //profile
Kostya Shishkovc03d9d02008-08-14 05:52:29381 put_bits(&pb, 4, s->samplerate_index); //sample rate index
Rostislav Pehlivanovb3deaec2015-10-12 14:53:17382 put_bits(&pb, 4, channels);
Kostya Shishkovc03d9d02008-08-14 05:52:29383 //GASpecificConfig
384 put_bits(&pb, 1, 0); //frame length - 1024 samples
385 put_bits(&pb, 1, 0); //does not depend on core coder
386 put_bits(&pb, 1, 0); //is not extension
Rostislav Pehlivanovfbf295e2016-10-03 18:53:11387 if (s->needs_pce)
388 put_pce(&pb, avctx);
Alex Conversed67a6aa2011-01-22 05:23:43389
390 //Explicitly Mark SBR absent
Janne Grunau604eb1522011-01-23 14:45:19391 put_bits(&pb, 11, 0x2b7); //sync extension
Alex Conversed67a6aa2011-01-22 05:23:43392 put_bits(&pb, 5, AOT_SBR);
393 put_bits(&pb, 1, 0);
Kostya Shishkovc03d9d02008-08-14 05:52:29394 flush_put_bits(&pb);
Andreas Rheinhardtc81b8e02021-03-25 11:52:56395 avctx->extradata_size = put_bytes_output(&pb);
Rostislav Pehlivanov7b7775a2017-11-09 04:30:07396
397 return 0;
Kostya Shishkovc03d9d02008-08-14 05:52:29398}
399
Claudio Freireb629c672015-10-12 06:56:22400void ff_quantize_band_cost_cache_init(struct AACEncContext *s)
401{
Reimar Döffingerb91e3762016-03-06 16:28:42402 ++s->quantize_band_cost_cache_generation;
403 if (s->quantize_band_cost_cache_generation == 0) {
404 memset(s->quantize_band_cost_cache, 0, sizeof(s->quantize_band_cost_cache));
405 s->quantize_band_cost_cache_generation = 1;
Claudio Freireb629c672015-10-12 06:56:22406 }
407}
408
Nathan Caldwell9292fe42011-12-24 00:36:15409#define WINDOW_FUNC(type) \
Ronald S. Bultje42d32462013-01-20 21:20:30410static void apply_ ##type ##_window(AVFloatDSPContext *fdsp, \
Justin Rugglesd5a72292012-05-21 16:58:41411 SingleChannelElement *sce, \
412 const float *audio)
Nathan Caldwell9292fe42011-12-24 00:36:15413
414WINDOW_FUNC(only_long)
Kostya Shishkovc03d9d02008-08-14 05:52:29415{
Nathan Caldwell9292fe42011-12-24 00:36:15416 const float *lwindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
417 const float *pwindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
Michael Niedermayer59b68ee2012-11-26 14:15:02418 float *out = sce->ret_buf;
Kostya Shishkovc03d9d02008-08-14 05:52:29419
Ronald S. Bultje42d32462013-01-20 21:20:30420 fdsp->vector_fmul (out, audio, lwindow, 1024);
421 fdsp->vector_fmul_reverse(out + 1024, audio + 1024, pwindow, 1024);
Kostya Shishkovc03d9d02008-08-14 05:52:29422}
423
Nathan Caldwell9292fe42011-12-24 00:36:15424WINDOW_FUNC(long_start)
Alex Converse78e65cd2009-07-08 20:01:31425{
Nathan Caldwell9292fe42011-12-24 00:36:15426 const float *lwindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
427 const float *swindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
Michael Niedermayer59b68ee2012-11-26 14:15:02428 float *out = sce->ret_buf;
Nathan Caldwell9292fe42011-12-24 00:36:15429
Justin Rugglesd5a72292012-05-21 16:58:41430 fdsp->vector_fmul(out, audio, lwindow, 1024);
Nathan Caldwell2e626dd2012-01-28 05:23:41431 memcpy(out + 1024, audio + 1024, sizeof(out[0]) * 448);
Ronald S. Bultje42d32462013-01-20 21:20:30432 fdsp->vector_fmul_reverse(out + 1024 + 448, audio + 1024 + 448, swindow, 128);
Nathan Caldwell9292fe42011-12-24 00:36:15433 memset(out + 1024 + 576, 0, sizeof(out[0]) * 448);
434}
435
436WINDOW_FUNC(long_stop)
437{
438 const float *lwindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
439 const float *swindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
Michael Niedermayer59b68ee2012-11-26 14:15:02440 float *out = sce->ret_buf;
Nathan Caldwell9292fe42011-12-24 00:36:15441
442 memset(out, 0, sizeof(out[0]) * 448);
Justin Rugglesd5a72292012-05-21 16:58:41443 fdsp->vector_fmul(out + 448, audio + 448, swindow, 128);
Nathan Caldwell9292fe42011-12-24 00:36:15444 memcpy(out + 576, audio + 576, sizeof(out[0]) * 448);
Ronald S. Bultje42d32462013-01-20 21:20:30445 fdsp->vector_fmul_reverse(out + 1024, audio + 1024, lwindow, 1024);
Nathan Caldwell9292fe42011-12-24 00:36:15446}
447
448WINDOW_FUNC(eight_short)
449{
450 const float *swindow = sce->ics.use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
451 const float *pwindow = sce->ics.use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
452 const float *in = audio + 448;
Michael Niedermayer59b68ee2012-11-26 14:15:02453 float *out = sce->ret_buf;
Mans Rullgard3715d8412012-01-29 20:55:10454 int w;
Nathan Caldwell9292fe42011-12-24 00:36:15455
Mans Rullgard3715d8412012-01-29 20:55:10456 for (w = 0; w < 8; w++) {
Ronald S. Bultje42d32462013-01-20 21:20:30457 fdsp->vector_fmul (out, in, w ? pwindow : swindow, 128);
Nathan Caldwell9292fe42011-12-24 00:36:15458 out += 128;
459 in += 128;
Ronald S. Bultje42d32462013-01-20 21:20:30460 fdsp->vector_fmul_reverse(out, in, swindow, 128);
Nathan Caldwell9292fe42011-12-24 00:36:15461 out += 128;
462 }
463}
464
Ronald S. Bultje42d32462013-01-20 21:20:30465static void (*const apply_window[4])(AVFloatDSPContext *fdsp,
Justin Rugglesd5a72292012-05-21 16:58:41466 SingleChannelElement *sce,
467 const float *audio) = {
Nathan Caldwell9292fe42011-12-24 00:36:15468 [ONLY_LONG_SEQUENCE] = apply_only_long_window,
469 [LONG_START_SEQUENCE] = apply_long_start_window,
470 [EIGHT_SHORT_SEQUENCE] = apply_eight_short_window,
471 [LONG_STOP_SEQUENCE] = apply_long_stop_window
472};
473
Nathan Caldwell04af2ef2011-12-07 22:20:10474static void apply_window_and_mdct(AACEncContext *s, SingleChannelElement *sce,
475 float *audio)
Alex Converse78e65cd2009-07-08 20:01:31476{
Nathan Caldwell9292fe42011-12-24 00:36:15477 int i;
Lynne8f3e0622022-10-29 12:01:57478 float *output = sce->ret_buf;
Alex Converse78e65cd2009-07-08 20:01:31479
Michael Niedermayer14285c32014-11-29 17:58:13480 apply_window[sce->ics.window_sequence[0]](s->fdsp, sce, audio);
Nathan Caldwell9292fe42011-12-24 00:36:15481
482 if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE)
Lynne8f3e0622022-10-29 12:01:57483 s->mdct1024_fn(s->mdct1024, sce->coeffs, output, sizeof(float));
Nathan Caldwell9292fe42011-12-24 00:36:15484 else
485 for (i = 0; i < 1024; i += 128)
Lynne8f3e0622022-10-29 12:01:57486 s->mdct128_fn(s->mdct128, &sce->coeffs[i], output + i*2, sizeof(float));
Nathan Caldwell9292fe42011-12-24 00:36:15487 memcpy(audio, audio + 1024, sizeof(audio[0]) * 1024);
Claudio Freire6394aca2015-03-03 06:43:06488 memcpy(sce->pcoeffs, sce->coeffs, sizeof(sce->pcoeffs));
Alex Converse78e65cd2009-07-08 20:01:31489}
490
Kostya Shishkovc03d9d02008-08-14 05:52:29491/**
492 * Encode ics_info element.
493 * @see Table 4.6 (syntax of ics_info)
494 */
Kostya Shishkove43b0a72008-08-24 05:56:23495static void put_ics_info(AACEncContext *s, IndividualChannelStream *info)
Kostya Shishkovc03d9d02008-08-14 05:52:29496{
Alex Converse78e65cd2009-07-08 20:01:31497 int w;
Kostya Shishkovc03d9d02008-08-14 05:52:29498
499 put_bits(&s->pb, 1, 0); // ics_reserved bit
500 put_bits(&s->pb, 2, info->window_sequence[0]);
501 put_bits(&s->pb, 1, info->use_kb_window[0]);
Alex Conversefd257dc2009-07-08 20:36:45502 if (info->window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
Kostya Shishkovc03d9d02008-08-14 05:52:29503 put_bits(&s->pb, 6, info->max_sfb);
Rostislav Pehlivanov76b81b12015-08-21 18:38:05504 put_bits(&s->pb, 1, !!info->predictor_present);
Alex Conversefd257dc2009-07-08 20:36:45505 } else {
Kostya Shishkovc03d9d02008-08-14 05:52:29506 put_bits(&s->pb, 4, info->max_sfb);
Diego Biurrunc8f47d82009-07-08 21:36:33507 for (w = 1; w < 8; w++)
Alex Converse78e65cd2009-07-08 20:01:31508 put_bits(&s->pb, 1, !info->group_len[w]);
Kostya Shishkovc03d9d02008-08-14 05:52:29509 }
510}
511
512/**
Alex Converse78e65cd2009-07-08 20:01:31513 * Encode MS data.
514 * @see 4.6.8.1 "Joint Coding - M/S Stereo"
Kostya Shishkove43b0a72008-08-24 05:56:23515 */
Alex Converse78e65cd2009-07-08 20:01:31516static void encode_ms_info(PutBitContext *pb, ChannelElement *cpe)
Kostya Shishkove43b0a72008-08-24 05:56:23517{
Kostya Shishkove43b0a72008-08-24 05:56:23518 int i, w;
Alex Converse78e65cd2009-07-08 20:01:31519
520 put_bits(pb, 2, cpe->ms_mode);
Diego Biurrunc8f47d82009-07-08 21:36:33521 if (cpe->ms_mode == 1)
522 for (w = 0; w < cpe->ch[0].ics.num_windows; w += cpe->ch[0].ics.group_len[w])
Alex Conversefd257dc2009-07-08 20:36:45523 for (i = 0; i < cpe->ch[0].ics.max_sfb; i++)
Alex Converse78e65cd2009-07-08 20:01:31524 put_bits(pb, 1, cpe->ms_mask[w*16 + i]);
Alex Converse78e65cd2009-07-08 20:01:31525}
526
527/**
528 * Produce integer coefficients from scalefactors provided by the model.
529 */
Diego Biurrun72c758f2012-10-24 17:16:08530static void adjust_frame_information(ChannelElement *cpe, int chans)
Alex Converse78e65cd2009-07-08 20:01:31531{
532 int i, w, w2, g, ch;
Rostislav Pehlivanov0b233902015-07-02 18:13:06533 int maxsfb, cmaxsfb;
Rostislav Pehlivanov0b233902015-07-02 18:13:06534
535 for (ch = 0; ch < chans; ch++) {
536 IndividualChannelStream *ics = &cpe->ch[ch].ics;
537 maxsfb = 0;
538 cpe->ch[ch].pulse.num_pulse = 0;
539 for (w = 0; w < ics->num_windows; w += ics->group_len[w]) {
540 for (w2 = 0; w2 < ics->group_len[w]; w2++) {
Claudio Freire6394aca2015-03-03 06:43:06541 for (cmaxsfb = ics->num_swb; cmaxsfb > 0 && cpe->ch[ch].zeroes[w*16+cmaxsfb-1]; cmaxsfb--)
542 ;
543 maxsfb = FFMAX(maxsfb, cmaxsfb);
Alex Converse78e65cd2009-07-08 20:01:31544 }
Alex Converse78e65cd2009-07-08 20:01:31545 }
546 ics->max_sfb = maxsfb;
547
548 //adjust zero bands for window groups
Alex Conversefd257dc2009-07-08 20:36:45549 for (w = 0; w < ics->num_windows; w += ics->group_len[w]) {
550 for (g = 0; g < ics->max_sfb; g++) {
Alex Converse78e65cd2009-07-08 20:01:31551 i = 1;
Alex Conversefd257dc2009-07-08 20:36:45552 for (w2 = w; w2 < w + ics->group_len[w]; w2++) {
553 if (!cpe->ch[ch].zeroes[w2*16 + g]) {
Alex Converse78e65cd2009-07-08 20:01:31554 i = 0;
555 break;
556 }
557 }
558 cpe->ch[ch].zeroes[w*16 + g] = i;
559 }
560 }
561 }
562
Alex Conversefd257dc2009-07-08 20:36:45563 if (chans > 1 && cpe->common_window) {
Alex Converse78e65cd2009-07-08 20:01:31564 IndividualChannelStream *ics0 = &cpe->ch[0].ics;
565 IndividualChannelStream *ics1 = &cpe->ch[1].ics;
566 int msc = 0;
567 ics0->max_sfb = FFMAX(ics0->max_sfb, ics1->max_sfb);
568 ics1->max_sfb = ics0->max_sfb;
Alex Conversefd257dc2009-07-08 20:36:45569 for (w = 0; w < ics0->num_windows*16; w += 16)
570 for (i = 0; i < ics0->max_sfb; i++)
Diego Biurrunc8f47d82009-07-08 21:36:33571 if (cpe->ms_mask[w+i])
572 msc++;
Diego Biurrun99d61d32009-07-08 21:16:06573 if (msc == 0 || ics0->max_sfb == 0)
574 cpe->ms_mode = 0;
575 else
Nathan Caldwell98add742011-06-29 03:11:39576 cpe->ms_mode = msc < ics0->max_sfb * ics0->num_windows ? 1 : 2;
Alex Converse78e65cd2009-07-08 20:01:31577 }
578}
579
Rostislav Pehlivanov20dc5272015-09-02 05:26:45580static void apply_intensity_stereo(ChannelElement *cpe)
581{
582 int w, w2, g, i;
583 IndividualChannelStream *ics = &cpe->ch[0].ics;
584 if (!cpe->common_window)
585 return;
586 for (w = 0; w < ics->num_windows; w += ics->group_len[w]) {
587 for (w2 = 0; w2 < ics->group_len[w]; w2++) {
588 int start = (w+w2) * 128;
589 for (g = 0; g < ics->num_swb; g++) {
590 int p = -1 + 2 * (cpe->ch[1].band_type[w*16+g] - 14);
591 float scale = cpe->ch[0].is_ener[w*16+g];
592 if (!cpe->is_mask[w*16 + g]) {
593 start += ics->swb_sizes[g];
594 continue;
595 }
Claudio Freire01ecb712015-10-11 20:29:50596 if (cpe->ms_mask[w*16 + g])
597 p *= -1;
Rostislav Pehlivanov20dc5272015-09-02 05:26:45598 for (i = 0; i < ics->swb_sizes[g]; i++) {
599 float sum = (cpe->ch[0].coeffs[start+i] + p*cpe->ch[1].coeffs[start+i])*scale;
600 cpe->ch[0].coeffs[start+i] = sum;
601 cpe->ch[1].coeffs[start+i] = 0.0f;
602 }
603 start += ics->swb_sizes[g];
604 }
605 }
606 }
607}
608
609static void apply_mid_side_stereo(ChannelElement *cpe)
610{
611 int w, w2, g, i;
612 IndividualChannelStream *ics = &cpe->ch[0].ics;
613 if (!cpe->common_window)
614 return;
615 for (w = 0; w < ics->num_windows; w += ics->group_len[w]) {
616 for (w2 = 0; w2 < ics->group_len[w]; w2++) {
617 int start = (w+w2) * 128;
618 for (g = 0; g < ics->num_swb; g++) {
Claudio Freirefc36d852015-11-26 06:27:06619 /* ms_mask can be used for other purposes in PNS and I/S,
620 * so must not apply M/S if any band uses either, even if
621 * ms_mask is set.
622 */
623 if (!cpe->ms_mask[w*16 + g] || cpe->is_mask[w*16 + g]
Claudio Freire509f1682016-01-08 08:04:37624 || cpe->ch[0].band_type[w*16 + g] >= NOISE_BT
625 || cpe->ch[1].band_type[w*16 + g] >= NOISE_BT) {
Rostislav Pehlivanov20dc5272015-09-02 05:26:45626 start += ics->swb_sizes[g];
627 continue;
628 }
629 for (i = 0; i < ics->swb_sizes[g]; i++) {
630 float L = (cpe->ch[0].coeffs[start+i] + cpe->ch[1].coeffs[start+i]) * 0.5f;
631 float R = L - cpe->ch[1].coeffs[start+i];
632 cpe->ch[0].coeffs[start+i] = L;
633 cpe->ch[1].coeffs[start+i] = R;
634 }
635 start += ics->swb_sizes[g];
636 }
637 }
638 }
639}
640
Alex Converse78e65cd2009-07-08 20:01:31641/**
642 * Encode scalefactor band coding type.
643 */
644static void encode_band_info(AACEncContext *s, SingleChannelElement *sce)
645{
646 int w;
647
Rostislav Pehlivanov20dc5272015-09-02 05:26:45648 if (s->coder->set_special_band_scalefactors)
649 s->coder->set_special_band_scalefactors(s, sce);
650
Diego Biurrunc8f47d82009-07-08 21:36:33651 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w])
Alex Converse78e65cd2009-07-08 20:01:31652 s->coder->encode_window_bands_info(s, sce, w, sce->ics.group_len[w], s->lambda);
Alex Converse78e65cd2009-07-08 20:01:31653}
654
655/**
656 * Encode scalefactors.
657 */
Diego Biurrun99d61d32009-07-08 21:16:06658static void encode_scale_factors(AVCodecContext *avctx, AACEncContext *s,
659 SingleChannelElement *sce)
Alex Converse78e65cd2009-07-08 20:01:31660{
Rostislav Pehlivanov013498b2015-04-13 23:33:51661 int diff, off_sf = sce->sf_idx[0], off_pns = sce->sf_idx[0] - NOISE_OFFSET;
Rostislav Pehlivanov7c10b872015-06-26 20:16:34662 int off_is = 0, noise_flag = 1;
Alex Converse78e65cd2009-07-08 20:01:31663 int i, w;
664
Alex Conversefd257dc2009-07-08 20:36:45665 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
666 for (i = 0; i < sce->ics.max_sfb; i++) {
667 if (!sce->zeroes[w*16 + i]) {
Rostislav Pehlivanovf7f71b52015-04-12 04:50:34668 if (sce->band_type[w*16 + i] == NOISE_BT) {
669 diff = sce->sf_idx[w*16 + i] - off_pns;
670 off_pns = sce->sf_idx[w*16 + i];
671 if (noise_flag-- > 0) {
672 put_bits(&s->pb, NOISE_PRE_BITS, diff + NOISE_PRE);
673 continue;
674 }
Rostislav Pehlivanov7c10b872015-06-26 20:16:34675 } else if (sce->band_type[w*16 + i] == INTENSITY_BT ||
676 sce->band_type[w*16 + i] == INTENSITY_BT2) {
677 diff = sce->sf_idx[w*16 + i] - off_is;
678 off_is = sce->sf_idx[w*16 + i];
Rostislav Pehlivanovf7f71b52015-04-12 04:50:34679 } else {
680 diff = sce->sf_idx[w*16 + i] - off_sf;
681 off_sf = sce->sf_idx[w*16 + i];
682 }
683 diff += SCALE_DIFF_ZERO;
Michael Niedermayerf69f9b32012-10-26 00:11:20684 av_assert0(diff >= 0 && diff <= 120);
Alex Converse78e65cd2009-07-08 20:01:31685 put_bits(&s->pb, ff_aac_scalefactor_bits[diff], ff_aac_scalefactor_code[diff]);
686 }
687 }
688 }
Kostya Shishkove43b0a72008-08-24 05:56:23689}
690
691/**
Kostya Shishkov817015e2008-08-16 05:47:18692 * Encode pulse data.
693 */
Kostya Shishkovcda00de2008-08-23 15:25:57694static void encode_pulses(AACEncContext *s, Pulse *pulse)
Kostya Shishkov817015e2008-08-16 05:47:18695{
696 int i;
697
698 put_bits(&s->pb, 1, !!pulse->num_pulse);
Diego Biurrun99d61d32009-07-08 21:16:06699 if (!pulse->num_pulse)
700 return;
Kostya Shishkov817015e2008-08-16 05:47:18701
702 put_bits(&s->pb, 2, pulse->num_pulse - 1);
703 put_bits(&s->pb, 6, pulse->start);
Alex Conversefd257dc2009-07-08 20:36:45704 for (i = 0; i < pulse->num_pulse; i++) {
Kostya Shishkovf5c3eae2008-08-16 11:59:36705 put_bits(&s->pb, 5, pulse->pos[i]);
Kostya Shishkov817015e2008-08-16 05:47:18706 put_bits(&s->pb, 4, pulse->amp[i]);
707 }
708}
709
710/**
711 * Encode spectral coefficients processed by psychoacoustic model.
712 */
Kostya Shishkovcda00de2008-08-23 15:25:57713static void encode_spectral_coeffs(AACEncContext *s, SingleChannelElement *sce)
Kostya Shishkov817015e2008-08-16 05:47:18714{
Alex Converse78e65cd2009-07-08 20:01:31715 int start, i, w, w2;
Kostya Shishkov817015e2008-08-16 05:47:18716
Alex Conversefd257dc2009-07-08 20:36:45717 for (w = 0; w < sce->ics.num_windows; w += sce->ics.group_len[w]) {
Kostya Shishkov817015e2008-08-16 05:47:18718 start = 0;
Alex Conversefd257dc2009-07-08 20:36:45719 for (i = 0; i < sce->ics.max_sfb; i++) {
720 if (sce->zeroes[w*16 + i]) {
Kostya Shishkovcda00de2008-08-23 15:25:57721 start += sce->ics.swb_sizes[i];
Kostya Shishkov817015e2008-08-16 05:47:18722 continue;
723 }
Rostislav Pehlivanov44ddee92015-08-29 05:34:08724 for (w2 = w; w2 < w + sce->ics.group_len[w]; w2++) {
Rostislav Pehlivanov43b378a2015-08-21 17:53:14725 s->coder->quantize_and_encode_band(s, &s->pb,
726 &sce->coeffs[start + w2*128],
Rostislav Pehlivanov44ddee92015-08-29 05:34:08727 NULL, sce->ics.swb_sizes[i],
Diego Biurrun99d61d32009-07-08 21:16:06728 sce->sf_idx[w*16 + i],
729 sce->band_type[w*16 + i],
Rostislav Pehlivanov43b378a2015-08-21 17:53:14730 s->lambda,
731 sce->ics.window_clipping[w]);
Rostislav Pehlivanov44ddee92015-08-29 05:34:08732 }
Kostya Shishkovcda00de2008-08-23 15:25:57733 start += sce->ics.swb_sizes[i];
Kostya Shishkov817015e2008-08-16 05:47:18734 }
Kostya Shishkov817015e2008-08-16 05:47:18735 }
736}
737
738/**
Claudio Freire59216e02015-07-21 01:53:24739 * Downscale spectral coefficients for near-clipping windows to avoid artifacts
740 */
741static void avoid_clipping(AACEncContext *s, SingleChannelElement *sce)
742{
743 int start, i, j, w;
744
745 if (sce->ics.clip_avoidance_factor < 1.0f) {
746 for (w = 0; w < sce->ics.num_windows; w++) {
747 start = 0;
748 for (i = 0; i < sce->ics.max_sfb; i++) {
Rostislav Pehlivanov32be2642015-08-21 17:30:51749 float *swb_coeffs = &sce->coeffs[start + w*128];
Claudio Freire59216e02015-07-21 01:53:24750 for (j = 0; j < sce->ics.swb_sizes[i]; j++)
751 swb_coeffs[j] *= sce->ics.clip_avoidance_factor;
752 start += sce->ics.swb_sizes[i];
753 }
754 }
755 }
756}
757
758/**
Alex Converse78e65cd2009-07-08 20:01:31759 * Encode one channel of audio data.
760 */
Diego Biurrun99d61d32009-07-08 21:16:06761static int encode_individual_channel(AVCodecContext *avctx, AACEncContext *s,
762 SingleChannelElement *sce,
763 int common_window)
Alex Converse78e65cd2009-07-08 20:01:31764{
765 put_bits(&s->pb, 8, sce->sf_idx[0]);
Rostislav Pehlivanov76b81b12015-08-21 18:38:05766 if (!common_window) {
Diego Biurrun99d61d32009-07-08 21:16:06767 put_ics_info(s, &sce->ics);
Rostislav Pehlivanov76b81b12015-08-21 18:38:05768 if (s->coder->encode_main_pred)
769 s->coder->encode_main_pred(s, sce);
Rostislav Pehlivanov27d23ae2015-10-17 01:22:51770 if (s->coder->encode_ltp_info)
771 s->coder->encode_ltp_info(s, sce, 0);
Rostislav Pehlivanov76b81b12015-08-21 18:38:05772 }
Alex Converse78e65cd2009-07-08 20:01:31773 encode_band_info(s, sce);
774 encode_scale_factors(avctx, s, sce);
775 encode_pulses(s, &sce->pulse);
Rostislav Pehlivanovf20b6712015-08-29 05:47:31776 put_bits(&s->pb, 1, !!sce->tns.present);
Rostislav Pehlivanova1c487e2015-08-21 18:27:38777 if (s->coder->encode_tns_info)
778 s->coder->encode_tns_info(s, sce);
Alex Converse78e65cd2009-07-08 20:01:31779 put_bits(&s->pb, 1, 0); //ssr
780 encode_spectral_coeffs(s, sce);
781 return 0;
782}
783
784/**
Kostya Shishkovc03d9d02008-08-14 05:52:29785 * Write some auxiliary information about the created AAC file.
786 */
Diego Biurrun72c758f2012-10-24 17:16:08787static void put_bitstream_info(AACEncContext *s, const char *name)
Kostya Shishkovc03d9d02008-08-14 05:52:29788{
789 int i, namelen, padbits;
790
791 namelen = strlen(name) + 2;
Kostya Shishkovf5c3eae2008-08-16 11:59:36792 put_bits(&s->pb, 3, TYPE_FIL);
Kostya Shishkovc03d9d02008-08-14 05:52:29793 put_bits(&s->pb, 4, FFMIN(namelen, 15));
Alex Conversefd257dc2009-07-08 20:36:45794 if (namelen >= 15)
Michael Niedermayer018a6642012-01-22 00:40:10795 put_bits(&s->pb, 8, namelen - 14);
Kostya Shishkovc03d9d02008-08-14 05:52:29796 put_bits(&s->pb, 4, 0); //extension type - filler
Alex Converseefe68072012-01-23 21:08:32797 padbits = -put_bits_count(&s->pb) & 7;
Anton Khirnov717503f2020-10-26 12:36:06798 align_put_bits(&s->pb);
Alex Conversefd257dc2009-07-08 20:36:45799 for (i = 0; i < namelen - 2; i++)
Kostya Shishkovc03d9d02008-08-14 05:52:29800 put_bits(&s->pb, 8, name[i]);
801 put_bits(&s->pb, 12 - padbits, 0);
802}
803
Nathan Caldwell9b8e2a82011-12-15 02:43:56804/*
Justin Rugglesf3e2d682012-08-25 17:04:33805 * Copy input samples.
Michael Niedermayer4b4d3d72012-02-01 02:46:11806 * Channels are reordered from libavcodec's default order to AAC order.
Nathan Caldwell9b8e2a82011-12-15 02:43:56807 */
Justin Rugglesf3e2d682012-08-25 17:04:33808static void copy_input_samples(AACEncContext *s, const AVFrame *frame)
Nathan Caldwell9b8e2a82011-12-15 02:43:56809{
Justin Rugglesf3e2d682012-08-25 17:04:33810 int ch;
811 int end = 2048 + (frame ? frame->nb_samples : 0);
Rostislav Pehlivanovfbf295e2016-10-03 18:53:11812 const uint8_t *channel_map = s->reorder_map;
Nathan Caldwell9b8e2a82011-12-15 02:43:56813
Justin Rugglesf3e2d682012-08-25 17:04:33814 /* copy and remap input samples */
815 for (ch = 0; ch < s->channels; ch++) {
Nathan Caldwell9b8e2a82011-12-15 02:43:56816 /* copy last 1024 samples of previous frame to the start of the current frame */
Nathan Caldwelldc7e7d42012-01-28 05:23:40817 memcpy(&s->planar_samples[ch][1024], &s->planar_samples[ch][2048], 1024 * sizeof(s->planar_samples[0][0]));
Nathan Caldwell9b8e2a82011-12-15 02:43:56818
Justin Rugglesf3e2d682012-08-25 17:04:33819 /* copy new samples and zero any remaining samples */
Justin Rugglesad953072012-01-28 17:28:01820 if (frame) {
Justin Rugglesf3e2d682012-08-25 17:04:33821 memcpy(&s->planar_samples[ch][2048],
822 frame->extended_data[channel_map[ch]],
823 frame->nb_samples * sizeof(s->planar_samples[0][0]));
Nathan Caldwell9b8e2a82011-12-15 02:43:56824 }
Justin Rugglesf3e2d682012-08-25 17:04:33825 memset(&s->planar_samples[ch][end], 0,
826 (3072 - end) * sizeof(s->planar_samples[0][0]));
Nathan Caldwell9b8e2a82011-12-15 02:43:56827 }
828}
829
Justin Rugglesad953072012-01-28 17:28:01830static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
831 const AVFrame *frame, int *got_packet_ptr)
Alex Converse78e65cd2009-07-08 20:01:31832{
833 AACEncContext *s = avctx->priv_data;
Nathan Caldwell7946a5a2011-12-24 22:24:16834 float **samples = s->planar_samples, *samples2, *la, *overlap;
Alex Converse78e65cd2009-07-08 20:01:31835 ChannelElement *cpe;
Rostislav Pehlivanovd1ca7142015-08-21 18:13:26836 SingleChannelElement *sce;
Rostislav Pehlivanov2d9b5ae2015-10-17 01:13:00837 IndividualChannelStream *ics;
Claudio Freire7ec74ae2015-09-23 05:13:56838 int i, its, ch, w, chans, tag, start_ch, ret, frame_bits;
Claudio Freire01ecb712015-10-11 20:29:50839 int target_bits, rate_bits, too_many_bits, too_few_bits;
Rostislav Pehlivanovd1ca7142015-08-21 18:13:26840 int ms_mode = 0, is_mode = 0, tns_mode = 0, pred_mode = 0;
Alex Converse78e65cd2009-07-08 20:01:31841 int chan_el_counter[4];
Måns Rullgård86e41bc32010-07-06 00:06:15842 FFPsyWindowInfo windows[AAC_MAX_CHANNELS];
Alex Converse78e65cd2009-07-08 20:01:31843
Justin Rugglesad953072012-01-28 17:28:01844 /* add current frame to queue */
845 if (frame) {
Michael Niedermayer98fed592013-01-13 23:02:50846 if ((ret = ff_af_queue_add(&s->afq, frame)) < 0)
Justin Rugglesad953072012-01-28 17:28:01847 return ret;
Rostislav Pehlivanov0cf68532016-11-07 02:11:50848 } else {
849 if (!s->afq.remaining_samples || (!s->afq.frame_alloc && !s->afq.frame_count))
850 return 0;
Justin Rugglesad953072012-01-28 17:28:01851 }
852
Justin Rugglesf3e2d682012-08-25 17:04:33853 copy_input_samples(s, frame);
Justin Ruggles89eea6d2012-01-28 22:51:22854 if (s->psypp)
855 ff_psy_preprocess(s->psypp, s->planar_samples, s->channels);
Nathan Caldwell9b8e2a82011-12-15 02:43:56856
Marton Balint6b6f7db2023-01-23 23:35:54857 if (!avctx->frame_num)
Alex Converse78e65cd2009-07-08 20:01:31858 return 0;
Alex Converse78e65cd2009-07-08 20:01:31859
Alex Converse78e65cd2009-07-08 20:01:31860 start_ch = 0;
Nathan Caldwell1bb52042011-06-20 04:29:37861 for (i = 0; i < s->chan_map[0]; i++) {
Alex Converse5962f6b2009-07-08 23:10:13862 FFPsyWindowInfo* wi = windows + start_ch;
Nathan Caldwell1bb52042011-06-20 04:29:37863 tag = s->chan_map[i+1];
Diego Biurrun99d61d32009-07-08 21:16:06864 chans = tag == TYPE_CPE ? 2 : 1;
865 cpe = &s->cpe[i];
Nathan Caldwell5b29af62011-05-06 07:19:51866 for (ch = 0; ch < chans; ch++) {
Michael Niedermayer2cb8ede2016-01-20 14:05:38867 int k;
Michael Niedermayerc38a6072015-10-17 11:40:04868 float clip_avoidance_factor;
Rostislav Pehlivanov2d9b5ae2015-10-17 01:13:00869 sce = &cpe->ch[ch];
870 ics = &sce->ics;
871 s->cur_channel = start_ch + ch;
Rostislav Pehlivanov2d9b5ae2015-10-17 01:13:00872 overlap = &samples[s->cur_channel][0];
Nathan Caldwell7946a5a2011-12-24 22:24:16873 samples2 = overlap + 1024;
Nathan Caldwell9b8e2a82011-12-15 02:43:56874 la = samples2 + (448+64);
Justin Rugglesad953072012-01-28 17:28:01875 if (!frame)
Nathan Caldwell2bb1d0e2010-07-19 18:22:44876 la = NULL;
Alex Converse03d5d9b2010-07-07 21:48:51877 if (tag == TYPE_LFE) {
Claudio Freire8005b6d2016-04-06 02:13:44878 wi[ch].window_type[0] = wi[ch].window_type[1] = ONLY_LONG_SEQUENCE;
Nathan Caldwell5b29af62011-05-06 07:19:51879 wi[ch].window_shape = 0;
880 wi[ch].num_windows = 1;
881 wi[ch].grouping[0] = 1;
Claudio Freire8005b6d2016-04-06 02:13:44882 wi[ch].clipping[0] = 0;
Nathan Caldwell24efdea2011-07-29 20:49:04883
884 /* Only the lowest 12 coefficients are used in a LFE channel.
885 * The expression below results in only the bottom 8 coefficients
886 * being used for 11.025kHz to 16kHz sample rates.
887 */
888 ics->num_swb = s->samplerate_index >= 8 ? 1 : 3;
Alex Converse03d5d9b2010-07-07 21:48:51889 } else {
Rostislav Pehlivanov2d9b5ae2015-10-17 01:13:00890 wi[ch] = s->psy.model->window(&s->psy, samples2, la, s->cur_channel,
Alex Converse26784382010-07-07 21:50:50891 ics->window_sequence[0]);
Alex Converse03d5d9b2010-07-07 21:48:51892 }
Alex Converse78e65cd2009-07-08 20:01:31893 ics->window_sequence[1] = ics->window_sequence[0];
Nathan Caldwell5b29af62011-05-06 07:19:51894 ics->window_sequence[0] = wi[ch].window_type[0];
Alex Converse78e65cd2009-07-08 20:01:31895 ics->use_kb_window[1] = ics->use_kb_window[0];
Nathan Caldwell5b29af62011-05-06 07:19:51896 ics->use_kb_window[0] = wi[ch].window_shape;
897 ics->num_windows = wi[ch].num_windows;
Alex Converse78e65cd2009-07-08 20:01:31898 ics->swb_sizes = s->psy.bands [ics->num_windows == 8];
Nathan Caldwell24efdea2011-07-29 20:49:04899 ics->num_swb = tag == TYPE_LFE ? ics->num_swb : s->psy.num_bands[ics->num_windows == 8];
Andreas Cadhalpun5b0da692015-12-06 20:35:08900 ics->max_sfb = FFMIN(ics->max_sfb, ics->num_swb);
Rostislav Pehlivanov23e786b2015-08-21 17:40:44901 ics->swb_offset = wi[ch].window_type[0] == EIGHT_SHORT_SEQUENCE ?
902 ff_swb_offset_128 [s->samplerate_index]:
903 ff_swb_offset_1024[s->samplerate_index];
Rostislav Pehlivanov5ed5ca72015-09-01 05:20:24904 ics->tns_max_bands = wi[ch].window_type[0] == EIGHT_SHORT_SEQUENCE ?
905 ff_tns_max_bands_128 [s->samplerate_index]:
906 ff_tns_max_bands_1024[s->samplerate_index];
Claudio Freire8005b6d2016-04-06 02:13:44907
Nathan Caldwell5b29af62011-05-06 07:19:51908 for (w = 0; w < ics->num_windows; w++)
909 ics->group_len[w] = wi[ch].grouping[w];
Claudio Freire8005b6d2016-04-06 02:13:44910
911 /* Calculate input sample maximums and evaluate clipping risk */
912 clip_avoidance_factor = 0.0f;
913 for (w = 0; w < ics->num_windows; w++) {
914 const float *wbuf = overlap + w * 128;
915 const int wlen = 2048 / ics->num_windows;
916 float max = 0;
917 int j;
918 /* mdct input is 2 * output */
919 for (j = 0; j < wlen; j++)
920 max = FFMAX(max, fabsf(wbuf[j]));
921 wi[ch].clipping[w] = max;
922 }
Claudio Freire59216e02015-07-21 01:53:24923 for (w = 0; w < ics->num_windows; w++) {
924 if (wi[ch].clipping[w] > CLIP_AVOIDANCE_FACTOR) {
925 ics->window_clipping[w] = 1;
926 clip_avoidance_factor = FFMAX(clip_avoidance_factor, wi[ch].clipping[w]);
927 } else {
928 ics->window_clipping[w] = 0;
929 }
930 }
931 if (clip_avoidance_factor > CLIP_AVOIDANCE_FACTOR) {
932 ics->clip_avoidance_factor = CLIP_AVOIDANCE_FACTOR / clip_avoidance_factor;
933 } else {
934 ics->clip_avoidance_factor = 1.0f;
935 }
Alex Converse78e65cd2009-07-08 20:01:31936
Rostislav Pehlivanov2d9b5ae2015-10-17 01:13:00937 apply_window_and_mdct(s, sce, overlap);
Rostislav Pehlivanov27d23ae2015-10-17 01:22:51938
939 if (s->options.ltp && s->coder->update_ltp) {
940 s->coder->update_ltp(s, sce);
941 apply_window[sce->ics.window_sequence[0]](s->fdsp, sce, &sce->ltp_state[0]);
Lynne8f3e0622022-10-29 12:01:57942 s->mdct1024_fn(s->mdct1024, sce->lcoeffs, sce->ret_buf, sizeof(float));
Rostislav Pehlivanov27d23ae2015-10-17 01:22:51943 }
944
Michael Niedermayer2cb8ede2016-01-20 14:05:38945 for (k = 0; k < 1024; k++) {
Michael Niedermayer77bf96b2016-08-23 09:00:29946 if (!(fabs(cpe->ch[ch].coeffs[k]) < 1E16)) { // Ensure headroom for energy calculation
947 av_log(avctx, AV_LOG_ERROR, "Input contains (near) NaN/+-Inf\n");
Michael Niedermayer2cb8ede2016-01-20 14:05:38948 return AVERROR(EINVAL);
949 }
Michael Niedermayerf9fa5602014-11-08 22:32:39950 }
Rostislav Pehlivanov2d9b5ae2015-10-17 01:13:00951 avoid_clipping(s, sce);
Alex Converse5962f6b2009-07-08 23:10:13952 }
953 start_ch += chans;
954 }
Andreas Rheinhardt56e9e022021-05-11 13:17:13955 if ((ret = ff_alloc_packet(avctx, avpkt, 8192 * s->channels)) < 0)
Reimar Döffingerecd74552012-04-06 13:25:05956 return ret;
Claudio Freire7ec74ae2015-09-23 05:13:56957 frame_bits = its = 0;
Alex Converse48d20c12009-07-08 23:12:53958 do {
Justin Rugglesad953072012-01-28 17:28:01959 init_put_bits(&s->pb, avpkt->data, avpkt->size);
960
Marton Balint6b6f7db2023-01-23 23:35:54961 if ((avctx->frame_num & 0xFF)==1 && !(avctx->flags & AV_CODEC_FLAG_BITEXACT))
Diego Biurrun72c758f2012-10-24 17:16:08962 put_bitstream_info(s, LIBAVCODEC_IDENT);
Alex Conversef11bfe32009-07-08 23:14:47963 start_ch = 0;
Claudio Freire7ec74ae2015-09-23 05:13:56964 target_bits = 0;
Alex Conversef11bfe32009-07-08 23:14:47965 memset(chan_el_counter, 0, sizeof(chan_el_counter));
Nathan Caldwell1bb52042011-06-20 04:29:37966 for (i = 0; i < s->chan_map[0]; i++) {
Alex Conversef11bfe32009-07-08 23:14:47967 FFPsyWindowInfo* wi = windows + start_ch;
Nathan Caldwell01344fe2011-05-19 05:14:59968 const float *coeffs[2];
Nathan Caldwell1bb52042011-06-20 04:29:37969 tag = s->chan_map[i+1];
Alex Conversef11bfe32009-07-08 23:14:47970 chans = tag == TYPE_CPE ? 2 : 1;
971 cpe = &s->cpe[i];
Rostislav Pehlivanova0079aa2015-09-01 06:00:10972 cpe->common_window = 0;
Rostislav Pehlivanov9f4f5782015-07-02 18:13:03973 memset(cpe->is_mask, 0, sizeof(cpe->is_mask));
974 memset(cpe->ms_mask, 0, sizeof(cpe->ms_mask));
Alex Converse8e4c11e2010-08-30 23:52:03975 put_bits(&s->pb, 3, tag);
976 put_bits(&s->pb, 4, chan_el_counter[tag]++);
Rostislav Pehlivanove6c9f3a2015-08-21 17:36:09977 for (ch = 0; ch < chans; ch++) {
978 sce = &cpe->ch[ch];
979 coeffs[ch] = sce->coeffs;
Rostislav Pehlivanov76b81b12015-08-21 18:38:05980 sce->ics.predictor_present = 0;
Rostislav Pehlivanov27d23ae2015-10-17 01:22:51981 sce->ics.ltp.present = 0;
982 memset(sce->ics.ltp.used, 0, sizeof(sce->ics.ltp.used));
Rostislav Pehlivanov3f3be1c2015-10-17 01:14:10983 memset(sce->ics.prediction_used, 0, sizeof(sce->ics.prediction_used));
Rostislav Pehlivanova1c487e2015-08-21 18:27:38984 memset(&sce->tns, 0, sizeof(TemporalNoiseShaping));
Rostislav Pehlivanove6c9f3a2015-08-21 17:36:09985 for (w = 0; w < 128; w++)
986 if (sce->band_type[w] > RESERVED_BT)
987 sce->band_type[w] = 0;
988 }
Claudio Freire7ec74ae2015-09-23 05:13:56989 s->psy.bitres.alloc = -1;
Rostislav Pehlivanov4386f172015-12-18 14:27:13990 s->psy.bitres.bits = s->last_frame_pb_count / s->channels;
Nathan Caldwelld3a6c2a2011-05-19 05:23:22991 s->psy.model->analyze(&s->psy, start_ch, coeffs, wi);
Claudio Freire7ec74ae2015-09-23 05:13:56992 if (s->psy.bitres.alloc > 0) {
993 /* Lambda unused here on purpose, we need to take psy's unscaled allocation */
Claudio Freire01ecb712015-10-11 20:29:50994 target_bits += s->psy.bitres.alloc
995 * (s->lambda / (avctx->global_quality ? avctx->global_quality : 120));
Claudio Freire7ec74ae2015-09-23 05:13:56996 s->psy.bitres.alloc /= chans;
997 }
998 s->cur_type = tag;
Nathan Caldwell5b29af62011-05-06 07:19:51999 for (ch = 0; ch < chans; ch++) {
Claudio Freiree41cd3c2013-05-12 07:38:401000 s->cur_channel = start_ch + ch;
Claudio Freire01ecb712015-10-11 20:29:501001 if (s->options.pns && s->coder->mark_pns)
1002 s->coder->mark_pns(s, avctx, &cpe->ch[ch]);
Nathan Caldwell5b29af62011-05-06 07:19:511003 s->coder->search_for_quantizers(avctx, s, &cpe->ch[ch], s->lambda);
Alex Conversef11bfe32009-07-08 23:14:471004 }
Alex Conversef11bfe32009-07-08 23:14:471005 if (chans > 1
1006 && wi[0].window_type[0] == wi[1].window_type[0]
1007 && wi[0].window_shape == wi[1].window_shape) {
Alex Converse78e65cd2009-07-08 20:01:311008
Alex Conversef11bfe32009-07-08 23:14:471009 cpe->common_window = 1;
Nathan Caldwell5b29af62011-05-06 07:19:511010 for (w = 0; w < wi[0].num_windows; w++) {
1011 if (wi[0].grouping[w] != wi[1].grouping[w]) {
Alex Conversef11bfe32009-07-08 23:14:471012 cpe->common_window = 0;
1013 break;
1014 }
Alex Converse78e65cd2009-07-08 20:01:311015 }
1016 }
Rostislav Pehlivanov20dc5272015-09-02 05:26:451017 for (ch = 0; ch < chans; ch++) { /* TNS and PNS */
Rostislav Pehlivanova1c487e2015-08-21 18:27:381018 sce = &cpe->ch[ch];
1019 s->cur_channel = start_ch + ch;
Rostislav Pehlivanova1c487e2015-08-21 18:27:381020 if (s->options.tns && s->coder->search_for_tns)
1021 s->coder->search_for_tns(s, sce);
Rostislav Pehlivanovf20b6712015-08-29 05:47:311022 if (s->options.tns && s->coder->apply_tns_filt)
Rostislav Pehlivanovf3f6c6b2015-09-01 05:44:071023 s->coder->apply_tns_filt(s, sce);
Rostislav Pehlivanova1c487e2015-08-21 18:27:381024 if (sce->tns.present)
1025 tns_mode = 1;
Rostislav Pehlivanovb32e9892015-12-06 13:32:401026 if (s->options.pns && s->coder->search_for_pns)
1027 s->coder->search_for_pns(s, avctx, sce);
Rostislav Pehlivanov38fd4c22015-07-02 18:13:051028 }
Claudio Freiree41cd3c2013-05-12 07:38:401029 s->cur_channel = start_ch;
Rostislav Pehlivanov20dc5272015-09-02 05:26:451030 if (s->options.intensity_stereo) { /* Intensity Stereo */
1031 if (s->coder->search_for_is)
1032 s->coder->search_for_is(s, avctx, cpe);
Rostislav Pehlivanove8576dc2015-07-02 18:13:071033 if (cpe->is_mode) is_mode = 1;
Rostislav Pehlivanov20dc5272015-09-02 05:26:451034 apply_intensity_stereo(cpe);
Rostislav Pehlivanove8576dc2015-07-02 18:13:071035 }
Rostislav Pehlivanov20dc5272015-09-02 05:26:451036 if (s->options.pred) { /* Prediction */
1037 for (ch = 0; ch < chans; ch++) {
1038 sce = &cpe->ch[ch];
1039 s->cur_channel = start_ch + ch;
1040 if (s->options.pred && s->coder->search_for_pred)
1041 s->coder->search_for_pred(s, sce);
1042 if (cpe->ch[ch].ics.predictor_present) pred_mode = 1;
1043 }
Rostislav Pehlivanov93e6b232015-10-12 22:33:071044 if (s->coder->adjust_common_pred)
1045 s->coder->adjust_common_pred(s, cpe);
Rostislav Pehlivanov20dc5272015-09-02 05:26:451046 for (ch = 0; ch < chans; ch++) {
1047 sce = &cpe->ch[ch];
1048 s->cur_channel = start_ch + ch;
1049 if (s->options.pred && s->coder->apply_main_pred)
1050 s->coder->apply_main_pred(s, sce);
1051 }
1052 s->cur_channel = start_ch;
1053 }
Rostislav Pehlivanov0f4334d2015-10-12 15:50:101054 if (s->options.mid_side) { /* Mid/Side stereo */
1055 if (s->options.mid_side == -1 && s->coder->search_for_ms)
Rostislav Pehlivanov20dc5272015-09-02 05:26:451056 s->coder->search_for_ms(s, cpe);
1057 else if (cpe->common_window)
1058 memset(cpe->ms_mask, 1, sizeof(cpe->ms_mask));
Rostislav Pehlivanov20dc5272015-09-02 05:26:451059 apply_mid_side_stereo(cpe);
1060 }
Diego Biurrun72c758f2012-10-24 17:16:081061 adjust_frame_information(cpe, chans);
Rostislav Pehlivanov27d23ae2015-10-17 01:22:511062 if (s->options.ltp) { /* LTP */
1063 for (ch = 0; ch < chans; ch++) {
1064 sce = &cpe->ch[ch];
1065 s->cur_channel = start_ch + ch;
1066 if (s->coder->search_for_ltp)
1067 s->coder->search_for_ltp(s, sce, cpe->common_window);
1068 if (sce->ics.ltp.present) pred_mode = 1;
1069 }
1070 s->cur_channel = start_ch;
1071 if (s->coder->adjust_common_ltp)
1072 s->coder->adjust_common_ltp(s, cpe);
1073 }
Alex Conversef11bfe32009-07-08 23:14:471074 if (chans == 2) {
1075 put_bits(&s->pb, 1, cpe->common_window);
1076 if (cpe->common_window) {
1077 put_ics_info(s, &cpe->ch[0].ics);
Rostislav Pehlivanov76b81b12015-08-21 18:38:051078 if (s->coder->encode_main_pred)
1079 s->coder->encode_main_pred(s, &cpe->ch[0]);
Rostislav Pehlivanov27d23ae2015-10-17 01:22:511080 if (s->coder->encode_ltp_info)
1081 s->coder->encode_ltp_info(s, &cpe->ch[0], 1);
Alex Conversef11bfe32009-07-08 23:14:471082 encode_ms_info(&s->pb, cpe);
Claudio Freire6394aca2015-03-03 06:43:061083 if (cpe->ms_mode) ms_mode = 1;
Alex Conversef11bfe32009-07-08 23:14:471084 }
Alex Converse78e65cd2009-07-08 20:01:311085 }
Nathan Caldwell5b29af62011-05-06 07:19:511086 for (ch = 0; ch < chans; ch++) {
1087 s->cur_channel = start_ch + ch;
1088 encode_individual_channel(avctx, s, &cpe->ch[ch], cpe->common_window);
Alex Conversef11bfe32009-07-08 23:14:471089 }
1090 start_ch += chans;
Alex Converse78e65cd2009-07-08 20:01:311091 }
Alex Converse78e65cd2009-07-08 20:01:311092
James Almerf5c8d002017-03-26 00:35:151093 if (avctx->flags & AV_CODEC_FLAG_QSCALE) {
Claudio Freire7ec74ae2015-09-23 05:13:561094 /* When using a constant Q-scale, don't mess with lambda */
Alex Converse48d20c12009-07-08 23:12:531095 break;
Nathan Caldwell230c1a92011-01-05 08:32:161096 }
Claudio Freire7ec74ae2015-09-23 05:13:561097
1098 /* rate control stuff
Claudio Freire01ecb712015-10-11 20:29:501099 * allow between the nominal bitrate, and what psy's bit reservoir says to target
1100 * but drift towards the nominal bitrate always
Claudio Freire7ec74ae2015-09-23 05:13:561101 */
Claudio Freire7ec74ae2015-09-23 05:13:561102 frame_bits = put_bits_count(&s->pb);
Claudio Freire01ecb712015-10-11 20:29:501103 rate_bits = avctx->bit_rate * 1024 / avctx->sample_rate;
1104 rate_bits = FFMIN(rate_bits, 6144 * s->channels - 3);
1105 too_many_bits = FFMAX(target_bits, rate_bits);
1106 too_many_bits = FFMIN(too_many_bits, 6144 * s->channels - 3);
1107 too_few_bits = FFMIN(FFMAX(rate_bits - rate_bits/4, target_bits), too_many_bits);
Claudio Freire7ec74ae2015-09-23 05:13:561108
Jeremy Wub92af7b2023-04-25 06:09:521109 /* When strict bit-rate control is demanded */
1110 if (avctx->bit_rate_tolerance == 0) {
1111 if (rate_bits < frame_bits) {
1112 float ratio = ((float)rate_bits) / frame_bits;
1113 s->lambda *= FFMIN(0.9f, ratio);
1114 continue;
1115 }
1116 /* reset lambda when solution is found */
1117 s->lambda = avctx->global_quality > 0 ? avctx->global_quality : 120;
1118 break;
1119 }
1120
Claudio Freire7ec74ae2015-09-23 05:13:561121 /* When using ABR, be strict (but only for increasing) */
Claudio Freire01ecb712015-10-11 20:29:501122 too_few_bits = too_few_bits - too_few_bits/8;
1123 too_many_bits = too_many_bits + too_many_bits/2;
Claudio Freire7ec74ae2015-09-23 05:13:561124
1125 if ( its == 0 /* for steady-state Q-scale tracking */
1126 || (its < 5 && (frame_bits < too_few_bits || frame_bits > too_many_bits))
1127 || frame_bits >= 6144 * s->channels - 3 )
1128 {
Claudio Freire01ecb712015-10-11 20:29:501129 float ratio = ((float)rate_bits) / frame_bits;
Claudio Freire7ec74ae2015-09-23 05:13:561130
1131 if (frame_bits >= too_few_bits && frame_bits <= too_many_bits) {
1132 /*
1133 * This path is for steady-state Q-scale tracking
1134 * When frame bits fall within the stable range, we still need to adjust
1135 * lambda to maintain it like so in a stable fashion (large jumps in lambda
1136 * create artifacts and should be avoided), but slowly
1137 */
1138 ratio = sqrtf(sqrtf(ratio));
1139 ratio = av_clipf(ratio, 0.9f, 1.1f);
1140 } else {
1141 /* Not so fast though */
1142 ratio = sqrtf(ratio);
Claudio Freire6394aca2015-03-03 06:43:061143 }
Michael Niedermayer4b89cf72021-06-01 08:07:051144 s->lambda = av_clipf(s->lambda * ratio, FLT_EPSILON, 65536.f);
Claudio Freire7ec74ae2015-09-23 05:13:561145
1146 /* Keep iterating if we must reduce and lambda is in the sky */
Claudio Freireca203e92015-12-01 06:28:361147 if (ratio > 0.9f && ratio < 1.1f) {
Claudio Freire7ec74ae2015-09-23 05:13:561148 break;
1149 } else {
1150 if (is_mode || ms_mode || tns_mode || pred_mode) {
1151 for (i = 0; i < s->chan_map[0]; i++) {
1152 // Must restore coeffs
1153 chans = tag == TYPE_CPE ? 2 : 1;
1154 cpe = &s->cpe[i];
1155 for (ch = 0; ch < chans; ch++)
1156 memcpy(cpe->ch[ch].coeffs, cpe->ch[ch].pcoeffs, sizeof(cpe->ch[ch].coeffs));
1157 }
1158 }
1159 its++;
1160 }
1161 } else {
1162 break;
Claudio Freire6394aca2015-03-03 06:43:061163 }
Alex Converse48d20c12009-07-08 23:12:531164 } while (1);
1165
Rostislav Pehlivanov27d23ae2015-10-17 01:22:511166 if (s->options.ltp && s->coder->ltp_insert_new_frame)
1167 s->coder->ltp_insert_new_frame(s);
1168
Alex Converse78e65cd2009-07-08 20:01:311169 put_bits(&s->pb, 3, TYPE_END);
1170 flush_put_bits(&s->pb);
Hendrik Leppkes362028c2015-12-18 13:39:151171
Rostislav Pehlivanov4386f172015-12-18 14:27:131172 s->last_frame_pb_count = put_bits_count(&s->pb);
Andreas Rheinhardtc81b8e02021-03-25 11:52:561173 avpkt->size = put_bytes_output(&s->pb);
Alex Converse78e65cd2009-07-08 20:01:311174
Claudio Freire01ecb712015-10-11 20:29:501175 s->lambda_sum += s->lambda;
1176 s->lambda_count++;
Alex Converse78e65cd2009-07-08 20:01:311177
Justin Rugglesad953072012-01-28 17:28:011178 ff_af_queue_remove(&s->afq, avctx->frame_size, &avpkt->pts,
1179 &avpkt->duration);
1180
Justin Rugglesad953072012-01-28 17:28:011181 *got_packet_ptr = 1;
1182 return 0;
Alex Converse78e65cd2009-07-08 20:01:311183}
1184
Kostya Shishkovc03d9d02008-08-14 05:52:291185static av_cold int aac_encode_end(AVCodecContext *avctx)
1186{
1187 AACEncContext *s = avctx->priv_data;
1188
Michael Niedermayerc520b982021-05-29 15:49:221189 av_log(avctx, AV_LOG_INFO, "Qavg: %.3f\n", s->lambda_count ? s->lambda_sum / s->lambda_count : NAN);
Claudio Freire01ecb712015-10-11 20:29:501190
Lynne8f3e0622022-10-29 12:01:571191 av_tx_uninit(&s->mdct1024);
1192 av_tx_uninit(&s->mdct128);
Alex Converse78e65cd2009-07-08 20:01:311193 ff_psy_end(&s->psy);
Rostislav Pehlivanovb47a1e52015-08-21 17:43:091194 ff_lpc_end(&s->lpc);
Nathan Caldwell53107042011-12-15 02:50:231195 if (s->psypp)
1196 ff_psy_preprocess_end(s->psypp);
Nathan Caldwell9b8e2a82011-12-15 02:43:561197 av_freep(&s->buffer.samples);
Kostya Shishkovc03d9d02008-08-14 05:52:291198 av_freep(&s->cpe);
Michael Niedermayer14285c32014-11-29 17:58:131199 av_freep(&s->fdsp);
Justin Rugglesad953072012-01-28 17:28:011200 ff_af_queue_close(&s->afq);
Kostya Shishkovc03d9d02008-08-14 05:52:291201 return 0;
1202}
1203
Nathan Caldwell53107042011-12-15 02:50:231204static av_cold int dsp_init(AVCodecContext *avctx, AACEncContext *s)
1205{
1206 int ret = 0;
Lynne8f3e0622022-10-29 12:01:571207 float scale = 32768.0f;
Nathan Caldwell53107042011-12-15 02:50:231208
Michael Niedermayer94d68a42015-07-27 19:14:311209 s->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
Michael Niedermayer14285c32014-11-29 17:58:131210 if (!s->fdsp)
1211 return AVERROR(ENOMEM);
Nathan Caldwell53107042011-12-15 02:50:231212
1213 // window init
Andreas Rheinhardt86b8c252020-11-22 22:33:031214 ff_aac_float_common_init();
Nathan Caldwell53107042011-12-15 02:50:231215
Lynne8f3e0622022-10-29 12:01:571216 if ((ret = av_tx_init(&s->mdct1024, &s->mdct1024_fn, AV_TX_FLOAT_MDCT, 0,
1217 1024, &scale, 0)) < 0)
Nathan Caldwell53107042011-12-15 02:50:231218 return ret;
Lynne8f3e0622022-10-29 12:01:571219 if ((ret = av_tx_init(&s->mdct128, &s->mdct128_fn, AV_TX_FLOAT_MDCT, 0,
1220 128, &scale, 0)) < 0)
Nathan Caldwell53107042011-12-15 02:50:231221 return ret;
1222
1223 return 0;
1224}
1225
1226static av_cold int alloc_buffers(AVCodecContext *avctx, AACEncContext *s)
1227{
Mans Rullgard3715d8412012-01-29 20:55:101228 int ch;
Limin Wangebf2a8a2020-06-01 14:48:281229 if (!FF_ALLOCZ_TYPED_ARRAY(s->buffer.samples, s->channels * 3 * 1024) ||
1230 !FF_ALLOCZ_TYPED_ARRAY(s->cpe, s->chan_map[0]))
1231 return AVERROR(ENOMEM);
Nathan Caldwell53107042011-12-15 02:50:231232
Mans Rullgard3715d8412012-01-29 20:55:101233 for(ch = 0; ch < s->channels; ch++)
Nathan Caldwell7946a5a2011-12-24 22:24:161234 s->planar_samples[ch] = s->buffer.samples + 3 * 1024 * ch;
Nathan Caldwell9b8e2a82011-12-15 02:43:561235
Nathan Caldwell53107042011-12-15 02:50:231236 return 0;
Nathan Caldwell53107042011-12-15 02:50:231237}
1238
1239static av_cold int aac_encode_init(AVCodecContext *avctx)
1240{
1241 AACEncContext *s = avctx->priv_data;
1242 int i, ret = 0;
1243 const uint8_t *sizes[2];
1244 uint8_t grouping[AAC_MAX_CHANNELS];
1245 int lengths[2];
1246
Rostislav Pehlivanovf0a82122016-02-12 18:34:181247 /* Constants */
Rostislav Pehlivanov4386f172015-12-18 14:27:131248 s->last_frame_pb_count = 0;
Nathan Caldwell53107042011-12-15 02:50:231249 avctx->frame_size = 1024;
Rostislav Pehlivanov0f4334d2015-10-12 15:50:101250 avctx->initial_padding = 1024;
Rostislav Pehlivanovf0a82122016-02-12 18:34:181251 s->lambda = avctx->global_quality > 0 ? avctx->global_quality : 120;
Nathan Caldwell53107042011-12-15 02:50:231252
Rostislav Pehlivanovf0a82122016-02-12 18:34:181253 /* Channel map and unspecified bitrate guessing */
Anton Khirnov494760f2013-05-07 05:20:321254 s->channels = avctx->ch_layout.nb_channels;
Rostislav Pehlivanovfbf295e2016-10-03 18:53:111255
1256 s->needs_pce = 1;
1257 for (i = 0; i < FF_ARRAY_ELEMS(aac_normal_chan_layouts); i++) {
Anton Khirnov494760f2013-05-07 05:20:321258 if (!av_channel_layout_compare(&avctx->ch_layout, &aac_normal_chan_layouts[i])) {
Rostislav Pehlivanovfbf295e2016-10-03 18:53:111259 s->needs_pce = s->options.pce;
1260 break;
1261 }
1262 }
1263
1264 if (s->needs_pce) {
Moritz Barsnick1693a682018-07-06 21:49:501265 char buf[64];
Rostislav Pehlivanovfbf295e2016-10-03 18:53:111266 for (i = 0; i < FF_ARRAY_ELEMS(aac_pce_configs); i++)
Anton Khirnov494760f2013-05-07 05:20:321267 if (!av_channel_layout_compare(&avctx->ch_layout, &aac_pce_configs[i].layout))
Rostislav Pehlivanovfbf295e2016-10-03 18:53:111268 break;
Anton Khirnov494760f2013-05-07 05:20:321269 av_channel_layout_describe(&avctx->ch_layout, buf, sizeof(buf));
1270 if (i == FF_ARRAY_ELEMS(aac_pce_configs)) {
1271 av_log(avctx, AV_LOG_ERROR, "Unsupported channel layout \"%s\"\n", buf);
1272 return AVERROR(EINVAL);
1273 }
Moritz Barsnick1693a682018-07-06 21:49:501274 av_log(avctx, AV_LOG_INFO, "Using a PCE to encode channel layout \"%s\"\n", buf);
Rostislav Pehlivanovfbf295e2016-10-03 18:53:111275 s->pce = aac_pce_configs[i];
1276 s->reorder_map = s->pce.reorder_map;
1277 s->chan_map = s->pce.config_map;
1278 } else {
1279 s->reorder_map = aac_chan_maps[s->channels - 1];
1280 s->chan_map = aac_chan_configs[s->channels - 1];
1281 }
1282
Rostislav Pehlivanovf0a82122016-02-12 18:34:181283 if (!avctx->bit_rate) {
1284 for (i = 1; i <= s->chan_map[0]; i++) {
1285 avctx->bit_rate += s->chan_map[i] == TYPE_CPE ? 128000 : /* Pair */
1286 s->chan_map[i] == TYPE_LFE ? 16000 : /* LFE */
1287 69000 ; /* SCE */
1288 }
1289 }
1290
1291 /* Samplerate */
Nathan Caldwell53107042011-12-15 02:50:231292 for (i = 0; i < 16; i++)
Andreas Rheinhardt49bf9452021-02-10 18:37:371293 if (avctx->sample_rate == ff_mpeg4audio_sample_rates[i])
Nathan Caldwell53107042011-12-15 02:50:231294 break;
Rostislav Pehlivanov0f4334d2015-10-12 15:50:101295 s->samplerate_index = i;
Rostislav Pehlivanov0f4334d2015-10-12 15:50:101296 ERROR_IF(s->samplerate_index == 16 ||
1297 s->samplerate_index >= ff_aac_swb_size_1024_len ||
1298 s->samplerate_index >= ff_aac_swb_size_128_len,
Nathan Caldwell53107042011-12-15 02:50:231299 "Unsupported sample rate %d\n", avctx->sample_rate);
Rostislav Pehlivanovf0a82122016-02-12 18:34:181300
1301 /* Bitrate limiting */
Claudio Freire6dbbb982015-03-06 07:05:321302 WARN_IF(1024.0 * avctx->bit_rate / avctx->sample_rate > 6144 * s->channels,
Hendrik Leppkes92186f22015-12-08 08:59:451303 "Too many bits %f > %d per frame requested, clamping to max\n",
Luca Barbatob8054822015-11-28 11:28:211304 1024.0 * avctx->bit_rate / avctx->sample_rate,
1305 6144 * s->channels);
Rostislav Pehlivanovf0a82122016-02-12 18:34:181306 avctx->bit_rate = (int64_t)FFMIN(6144 * s->channels / 1024.0 * avctx->sample_rate,
1307 avctx->bit_rate);
Claudio Freire6dbbb982015-03-06 07:05:321308
Rostislav Pehlivanovf0a82122016-02-12 18:34:181309 /* Profile and option setting */
Andreas Rheinhardt8238bc02023-09-02 12:57:411310 avctx->profile = avctx->profile == AV_PROFILE_UNKNOWN ? AV_PROFILE_AAC_LOW :
Rostislav Pehlivanovf0a82122016-02-12 18:34:181311 avctx->profile;
Rostislav Pehlivanove9299df2015-10-17 02:15:441312 for (i = 0; i < FF_ARRAY_ELEMS(aacenc_profiles); i++)
1313 if (avctx->profile == aacenc_profiles[i])
Rostislav Pehlivanov0f4334d2015-10-12 15:50:101314 break;
Andreas Rheinhardt8238bc02023-09-02 12:57:411315 if (avctx->profile == AV_PROFILE_MPEG2_AAC_LOW) {
1316 avctx->profile = AV_PROFILE_AAC_LOW;
Rostislav Pehlivanove9299df2015-10-17 02:15:441317 ERROR_IF(s->options.pred,
1318 "Main prediction unavailable in the \"mpeg2_aac_low\" profile\n");
1319 ERROR_IF(s->options.ltp,
1320 "LTP prediction unavailable in the \"mpeg2_aac_low\" profile\n");
1321 WARN_IF(s->options.pns,
1322 "PNS unavailable in the \"mpeg2_aac_low\" profile, turning off\n");
1323 s->options.pns = 0;
Andreas Rheinhardt8238bc02023-09-02 12:57:411324 } else if (avctx->profile == AV_PROFILE_AAC_LTP) {
Rostislav Pehlivanove9299df2015-10-17 02:15:441325 s->options.ltp = 1;
1326 ERROR_IF(s->options.pred,
1327 "Main prediction unavailable in the \"aac_ltp\" profile\n");
Andreas Rheinhardt8238bc02023-09-02 12:57:411328 } else if (avctx->profile == AV_PROFILE_AAC_MAIN) {
Rostislav Pehlivanove9299df2015-10-17 02:15:441329 s->options.pred = 1;
1330 ERROR_IF(s->options.ltp,
1331 "LTP prediction unavailable in the \"aac_main\" profile\n");
1332 } else if (s->options.ltp) {
Andreas Rheinhardt8238bc02023-09-02 12:57:411333 avctx->profile = AV_PROFILE_AAC_LTP;
Rostislav Pehlivanove9299df2015-10-17 02:15:441334 WARN_IF(1,
1335 "Chainging profile to \"aac_ltp\"\n");
1336 ERROR_IF(s->options.pred,
1337 "Main prediction unavailable in the \"aac_ltp\" profile\n");
1338 } else if (s->options.pred) {
Andreas Rheinhardt8238bc02023-09-02 12:57:411339 avctx->profile = AV_PROFILE_AAC_MAIN;
Rostislav Pehlivanove9299df2015-10-17 02:15:441340 WARN_IF(1,
1341 "Chainging profile to \"aac_main\"\n");
Rostislav Pehlivanov31125012015-12-05 18:43:171342 ERROR_IF(s->options.ltp,
Rostislav Pehlivanove9299df2015-10-17 02:15:441343 "LTP prediction unavailable in the \"aac_main\" profile\n");
Rostislav Pehlivanov0f4334d2015-10-12 15:50:101344 }
Rostislav Pehlivanove9299df2015-10-17 02:15:441345 s->profile = avctx->profile;
Rostislav Pehlivanov0f4334d2015-10-12 15:50:101346
Rostislav Pehlivanovf0a82122016-02-12 18:34:181347 /* Coder limitations */
1348 s->coder = &ff_aac_coders[s->options.coder];
Rostislav Pehlivanovfb0abb32016-08-06 23:49:341349 if (s->options.coder == AAC_CODER_ANMR) {
Rostislav Pehlivanovb270ec92015-12-05 14:41:411350 ERROR_IF(avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL,
Rostislav Pehlivanov6612d042016-08-13 16:34:581351 "The ANMR coder is considered experimental, add -strict -2 to enable!\n");
Rostislav Pehlivanov8ffe1cb2015-09-01 11:07:001352 s->options.intensity_stereo = 0;
1353 s->options.pns = 0;
1354 }
Rostislav Pehlivanova72b1ea2016-01-20 16:49:551355 ERROR_IF(s->options.ltp && avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL,
1356 "The LPT profile requires experimental compliance, add -strict -2 to enable!\n");
1357
Rostislav Pehlivanov0fe0e212016-02-13 12:23:221358 /* M/S introduces horrible artifacts with multichannel files, this is temporary */
1359 if (s->channels > 3)
1360 s->options.mid_side = 0;
1361
Vittorio Giovara971099f2014-12-17 13:53:431362 if ((ret = dsp_init(avctx, s)) < 0)
Limin Wangebf2a8a2020-06-01 14:48:281363 return ret;
Nathan Caldwell53107042011-12-15 02:50:231364
Vittorio Giovara971099f2014-12-17 13:53:431365 if ((ret = alloc_buffers(avctx, s)) < 0)
Limin Wangebf2a8a2020-06-01 14:48:281366 return ret;
Nathan Caldwell53107042011-12-15 02:50:231367
Rostislav Pehlivanov7b7775a2017-11-09 04:30:071368 if ((ret = put_audio_specific_config(avctx)))
Limin Wangebf2a8a2020-06-01 14:48:281369 return ret;
Nathan Caldwell53107042011-12-15 02:50:231370
Rostislav Pehlivanov0f4334d2015-10-12 15:50:101371 sizes[0] = ff_aac_swb_size_1024[s->samplerate_index];
1372 sizes[1] = ff_aac_swb_size_128[s->samplerate_index];
1373 lengths[0] = ff_aac_num_swb_1024[s->samplerate_index];
1374 lengths[1] = ff_aac_num_swb_128[s->samplerate_index];
Nathan Caldwell53107042011-12-15 02:50:231375 for (i = 0; i < s->chan_map[0]; i++)
1376 grouping[i] = s->chan_map[i + 1] == TYPE_CPE;
Vittorio Giovara971099f2014-12-17 13:53:431377 if ((ret = ff_psy_init(&s->psy, avctx, 2, sizes, lengths,
1378 s->chan_map[0], grouping)) < 0)
Limin Wangebf2a8a2020-06-01 14:48:281379 return ret;
Nathan Caldwell53107042011-12-15 02:50:231380 s->psypp = ff_psy_preprocess_init(avctx);
Rostislav Pehlivanovd09f9c42015-09-01 05:40:121381 ff_lpc_init(&s->lpc, 2*avctx->frame_size, TNS_MAX_ORDER, FF_LPC_TYPE_LEVINSON);
Rostislav Pehlivanov230178d2016-10-08 00:47:041382 s->random_state = 0x1f2e3d4c;
Nathan Caldwell53107042011-12-15 02:50:231383
Rostislav Pehlivanovd2ae5f72016-10-08 14:59:141384 s->abs_pow34 = abs_pow34_v;
1385 s->quant_bands = quantize_bands;
1386
Andreas Rheinhardt40e65752022-06-12 03:51:121387#if ARCH_X86
1388 ff_aac_dsp_init_x86(s);
1389#endif
Rostislav Pehlivanovd2ae5f72016-10-08 14:59:141390
Andreas Rheinhardt40e65752022-06-12 03:51:121391#if HAVE_MIPSDSP
1392 ff_aac_coder_init_mips(s);
1393#endif
Bojan Zivkovic26f39242013-03-06 13:55:051394
Justin Rugglesad953072012-01-28 17:28:011395 ff_af_queue_init(avctx, &s->afq);
Andreas Rheinhardt195d8ce2020-11-19 15:54:451396 ff_aac_tableinit();
Justin Rugglesad953072012-01-28 17:28:011397
Nathan Caldwell53107042011-12-15 02:50:231398 return 0;
Nathan Caldwell53107042011-12-15 02:50:231399}
1400
Nathan Caldwellcc9947f2011-06-01 05:38:091401#define AACENC_FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
1402static const AVOption aacenc_options[] = {
Lynne660d1d82021-05-21 15:39:361403 {"aac_coder", "Coding algorithm", offsetof(AACEncContext, options.coder), AV_OPT_TYPE_INT, {.i64 = AAC_CODER_TWOLOOP}, 0, AAC_CODER_NB-1, AACENC_FLAGS, "coder"},
Rostislav Pehlivanov0f4334d2015-10-12 15:50:101404 {"anmr", "ANMR method", 0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_ANMR}, INT_MIN, INT_MAX, AACENC_FLAGS, "coder"},
1405 {"twoloop", "Two loop searching method", 0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_TWOLOOP}, INT_MIN, INT_MAX, AACENC_FLAGS, "coder"},
Rostislav Pehlivanovfcb681a2018-01-13 11:46:291406 {"fast", "Default fast search", 0, AV_OPT_TYPE_CONST, {.i64 = AAC_CODER_FAST}, INT_MIN, INT_MAX, AACENC_FLAGS, "coder"},
Rostislav Pehlivanov83900c02015-10-17 01:20:131407 {"aac_ms", "Force M/S stereo coding", offsetof(AACEncContext, options.mid_side), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, AACENC_FLAGS},
Rostislav Pehlivanove9299df2015-10-17 02:15:441408 {"aac_is", "Intensity stereo coding", offsetof(AACEncContext, options.intensity_stereo), AV_OPT_TYPE_BOOL, {.i64 = 1}, -1, 1, AACENC_FLAGS},
1409 {"aac_pns", "Perceptual noise substitution", offsetof(AACEncContext, options.pns), AV_OPT_TYPE_BOOL, {.i64 = 1}, -1, 1, AACENC_FLAGS},
Rostislav Pehlivanovdfba1be2015-10-17 10:06:051410 {"aac_tns", "Temporal noise shaping", offsetof(AACEncContext, options.tns), AV_OPT_TYPE_BOOL, {.i64 = 1}, -1, 1, AACENC_FLAGS},
Rostislav Pehlivanove9299df2015-10-17 02:15:441411 {"aac_ltp", "Long term prediction", offsetof(AACEncContext, options.ltp), AV_OPT_TYPE_BOOL, {.i64 = 0}, -1, 1, AACENC_FLAGS},
1412 {"aac_pred", "AAC-Main prediction", offsetof(AACEncContext, options.pred), AV_OPT_TYPE_BOOL, {.i64 = 0}, -1, 1, AACENC_FLAGS},
Rostislav Pehlivanovfbf295e2016-10-03 18:53:111413 {"aac_pce", "Forces the use of PCEs", offsetof(AACEncContext, options.pce), AV_OPT_TYPE_BOOL, {.i64 = 0}, -1, 1, AACENC_FLAGS},
Marton Balintaee036c2020-05-10 18:38:111414 FF_AAC_PROFILE_OPTS
Nathan Caldwellcc9947f2011-06-01 05:38:091415 {NULL}
1416};
1417
1418static const AVClass aacenc_class = {
Diego Biurrun97cfe1d2017-06-10 14:45:061419 .class_name = "AAC encoder",
1420 .item_name = av_default_item_name,
1421 .option = aacenc_options,
1422 .version = LIBAVUTIL_VERSION_INT,
Nathan Caldwellcc9947f2011-06-01 05:38:091423};
1424
Andreas Rheinhardt5aabb252022-03-16 20:26:111425static const FFCodecDefault aac_encode_defaults[] = {
Rostislav Pehlivanovf0a82122016-02-12 18:34:181426 { "b", "0" },
1427 { NULL }
1428};
1429
Andreas Rheinhardt20f97272022-03-16 20:09:541430const FFCodec ff_aac_encoder = {
1431 .p.name = "aac",
Andreas Rheinhardt48286d42022-08-29 11:38:021432 CODEC_LONG_NAME("AAC (Advanced Audio Coding)"),
Andreas Rheinhardt20f97272022-03-16 20:09:541433 .p.type = AVMEDIA_TYPE_AUDIO,
1434 .p.id = AV_CODEC_ID_AAC,
Andreas Rheinhardta499b432021-05-11 18:52:131435 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
1436 AV_CODEC_CAP_SMALL_LAST_FRAME,
Anton Khirnovec6402b2011-07-17 10:54:311437 .priv_data_size = sizeof(AACEncContext),
1438 .init = aac_encode_init,
Andreas Rheinhardt4243da42022-03-30 21:28:241439 FF_CODEC_ENCODE_CB(aac_encode_frame),
Anton Khirnovec6402b2011-07-17 10:54:311440 .close = aac_encode_end,
Rostislav Pehlivanovf0a82122016-02-12 18:34:181441 .defaults = aac_encode_defaults,
Andreas Rheinhardt20f97272022-03-16 20:09:541442 .p.supported_samplerates = ff_mpeg4audio_sample_rates,
Andreas Rheinhardt21b23ce2022-07-09 22:05:451443 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
Andreas Rheinhardt20f97272022-03-16 20:09:541444 .p.sample_fmts = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_FLTP,
Martin Storsjö00c3b672012-04-06 16:19:391445 AV_SAMPLE_FMT_NONE },
Andreas Rheinhardt20f97272022-03-16 20:09:541446 .p.priv_class = &aacenc_class,
Kostya Shishkovc03d9d02008-08-14 05:52:291447};