1 /*
2 * AAC encoder wrapper
3 * Copyright (c) 2012 Martin Storsjo
4 *
5 * This file is part of FFmpeg.
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20 #include <fdk-aac/aacenc_lib.h>
21
30
31 #ifdef AACENCODER_LIB_VL0
32 #define FDKENC_VER_AT_LEAST(vl0, vl1) \
33 ((AACENCODER_LIB_VL0 > vl0) || \
34 (AACENCODER_LIB_VL0 == vl0 && AACENCODER_LIB_VL1 >= vl1))
35 #else
36 #define FDKENC_VER_AT_LEAST(vl0, vl1) 0
37 #endif
38
49
52
55 {
"eld_sbr",
"Enable SBR for ELD (for SBR in other configurations, use the -profile parameter)", offsetof(
AACContext, eld_sbr),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1,
AV_OPT_FLAG_AUDIO_PARAM |
AV_OPT_FLAG_ENCODING_PARAM },
56 #if FDKENC_VER_AT_LEAST(4, 0) // 4.0.0
58 #endif
59 {
"signaling",
"SBR/PS signaling style", offsetof(
AACContext, signaling),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 2,
AV_OPT_FLAG_AUDIO_PARAM |
AV_OPT_FLAG_ENCODING_PARAM,
"signaling" },
60 {
"default",
"Choose signaling implicitly (explicit hierarchical by default, implicit if global header is disabled)", 0,
AV_OPT_TYPE_CONST, { .i64 = -1 }, 0, 0,
AV_OPT_FLAG_AUDIO_PARAM |
AV_OPT_FLAG_ENCODING_PARAM,
"signaling" },
62 {
"explicit_sbr",
"Explicit SBR, implicit PS signaling", 0,
AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0,
AV_OPT_FLAG_AUDIO_PARAM |
AV_OPT_FLAG_ENCODING_PARAM,
"signaling" },
69 };
70
76 };
77
79 {
80 switch (err) {
81 case AACENC_OK:
82 return "No error";
83 case AACENC_INVALID_HANDLE:
84 return "Invalid handle";
85 case AACENC_MEMORY_ERROR:
86 return "Memory allocation error";
87 case AACENC_UNSUPPORTED_PARAMETER:
88 return "Unsupported parameter";
89 case AACENC_INVALID_CONFIG:
90 return "Invalid config";
91 case AACENC_INIT_ERROR:
92 return "Initialization error";
93 case AACENC_INIT_AAC_ERROR:
94 return "AAC library initialization error";
95 case AACENC_INIT_SBR_ERROR:
96 return "SBR library initialization error";
97 case AACENC_INIT_TP_ERROR:
98 return "Transport library initialization error";
99 case AACENC_INIT_META_ERROR:
100 return "Metadata library initialization error";
101 case AACENC_ENCODE_ERROR:
102 return "Encoding error";
103 case AACENC_ENCODE_EOF:
104 return "End of file";
105 default:
106 return "Unknown error";
107 }
108 }
109
111 {
113
115 aacEncClose(&
s->handle);
117
118 return 0;
119 }
120
122 {
125 AACENC_InfoStruct
info = { 0 };
127 AACENC_ERROR err;
129 int sce = 0, cpe = 0;
130
135 }
136
139
140 if ((err = aacEncoder_SetParam(
s->handle, AACENC_AOT, aot)) != AACENC_OK) {
144 }
145
147 if ((err = aacEncoder_SetParam(
s->handle, AACENC_SBR_MODE,
148 1)) != AACENC_OK) {
152 }
153 }
154
155 if ((err = aacEncoder_SetParam(
s->handle, AACENC_SAMPLERATE,
160 }
161
163 case 1:
mode = MODE_1; sce = 1; cpe = 0;
break;
164 case 2:
165 #if FDKENC_VER_AT_LEAST(4, 0) // 4.0.0
166 // (profile + 1) to map from profile range to AOT range
168 if ((err = aacEncoder_SetParam(
s->handle, AACENC_CHANNELMODE,
169 128)) != AACENC_OK) {
173 } else {
175 sce = 1;
176 cpe = 0;
177 }
178 } else
179 #endif
180 {
182 sce = 0;
183 cpe = 1;
184 }
185 break;
186 case 3:
mode = MODE_1_2; sce = 1; cpe = 1;
break;
187 case 4:
mode = MODE_1_2_1; sce = 2; cpe = 1;
break;
188 case 5:
mode = MODE_1_2_2; sce = 1; cpe = 2;
break;
189 case 6:
mode = MODE_1_2_2_1; sce = 2; cpe = 2;
break;
190 #if FDKENC_VER_AT_LEAST(4, 0) // 4.0.0
191 case 7:
mode = MODE_6_1; sce = 3; cpe = 2;
break;
192 #endif
193 /* The version macro is introduced the same time as the 7.1 support, so this
194 should suffice. */
195 #if FDKENC_VER_AT_LEAST(3, 4) // 3.4.12
196 case 8:
197 sce = 2;
198 cpe = 3;
200 mode = MODE_7_1_REAR_SURROUND;
201 #if FDKENC_VER_AT_LEAST(4, 0) // 4.0.0
203 mode = MODE_7_1_TOP_FRONT;
204 #endif
205 } else {
206 // MODE_1_2_2_2_1 and MODE_7_1_FRONT_CENTER use the same channel layout
207 mode = MODE_7_1_FRONT_CENTER;
208 }
209 break;
210 #endif
211 default:
215 }
216
217 if ((err = aacEncoder_SetParam(
s->handle, AACENC_CHANNELMODE,
218 mode)) != AACENC_OK) {
222 }
223
224 if ((err = aacEncoder_SetParam(
s->handle, AACENC_CHANNELORDER,
225 1)) != AACENC_OK) {
227 "Unable to set wav channel order %d: %s\n",
230 }
231
236 "VBR quality %d out of range, should be 1-5\n",
mode);
238 }
240 "Note, the VBR setting is unsupported and only works with "
241 "some parameter combinations\n");
242 if ((err = aacEncoder_SetParam(
s->handle, AACENC_BITRATEMODE,
243 mode)) != AACENC_OK) {
247 }
248 } else {
251 sce = 1;
252 cpe = 0;
253 }
260 }
261 if ((err = aacEncoder_SetParam(
s->handle, AACENC_BITRATE,
266 }
267 }
268
269 /* Choose bitstream format - if global header is requested, use
270 * raw access units, otherwise use ADTS. */
271 if ((err = aacEncoder_SetParam(
s->handle, AACENC_TRANSMUX,
273 s->latm ? TT_MP4_LOAS : TT_MP4_ADTS)) != AACENC_OK) {
277 }
278
279 if (
s->latm &&
s->header_period) {
280 if ((err = aacEncoder_SetParam(
s->handle, AACENC_HEADER_PERIOD,
281 s->header_period)) != AACENC_OK) {
285 }
286 }
287
288 /* If no signaling mode is chosen, use explicit hierarchical signaling
289 * if using mp4 mode (raw access units, with global header) and
290 * implicit signaling if using ADTS. */
291 if (
s->signaling < 0)
293
294 if ((err = aacEncoder_SetParam(
s->handle, AACENC_SIGNALING_MODE,
295 s->signaling)) != AACENC_OK) {
299 }
300
301 if ((err = aacEncoder_SetParam(
s->handle, AACENC_AFTERBURNER,
302 s->afterburner)) != AACENC_OK) {
306 }
307
313 }
314 if ((err = aacEncoder_SetParam(
s->handle, AACENC_BANDWIDTH,
315 avctx->
cutoff)) != AACENC_OK) {
319 }
320 }
321
326 }
327
328 if ((err = aacEncInfo(
s->handle, &
info)) != AACENC_OK) {
332 }
333
335 #if FDKENC_VER_AT_LEAST(4, 0) // 4.0.0
337 #else
339 #endif
341
349 }
350
352 }
353 return 0;
357 }
358
361 {
363 AACENC_BufDesc in_buf = { 0 }, out_buf = { 0 };
364 AACENC_InArgs in_args = { 0 };
365 AACENC_OutArgs out_args = { 0 };
366 int in_buffer_identifier = IN_AUDIO_DATA;
367 int in_buffer_size, in_buffer_element_size;
368 int out_buffer_identifier = OUT_BITSTREAM_DATA;
369 int out_buffer_size, out_buffer_element_size;
370 void *in_ptr, *out_ptr;
372 uint8_t dummy_buf[1];
373 AACENC_ERROR err;
374
375 /* handle end-of-stream small frame and flushing */
377 /* Must be a non-null pointer, even if it's a dummy. We could use
378 * the address of anything else on the stack as well. */
379 in_ptr = dummy_buf;
380 in_buffer_size = 0;
381
382 in_args.numInSamples = -1;
383 } else {
384 in_ptr =
frame->data[0];
386
388
389 /* add current frame to the queue */
392 }
393
394 in_buffer_element_size = 2;
395 in_buf.numBufs = 1;
396 in_buf.bufs = &in_ptr;
397 in_buf.bufferIdentifiers = &in_buffer_identifier;
398 in_buf.bufSizes = &in_buffer_size;
399 in_buf.bufElSizes = &in_buffer_element_size;
400
401 /* The maximum packet size is 6144 bits aka 768 bytes per channel. */
405
406 out_ptr = avpkt->
data;
407 out_buffer_size = avpkt->
size;
408 out_buffer_element_size = 1;
409 out_buf.numBufs = 1;
410 out_buf.bufs = &out_ptr;
411 out_buf.bufferIdentifiers = &out_buffer_identifier;
412 out_buf.bufSizes = &out_buffer_size;
413 out_buf.bufElSizes = &out_buffer_element_size;
414
415 if ((err = aacEncEncode(
s->handle, &in_buf, &out_buf, &in_args,
416 &out_args)) != AACENC_OK) {
417 if (!
frame && err == AACENC_ENCODE_EOF)
418 return 0;
422 }
423
424 if (!out_args.numOutBytes)
425 return 0;
426
427 /* Get the next frame pts & duration */
430
431 avpkt->
size = out_args.numOutBytes;
432 *got_packet_ptr = 1;
433 return 0;
434 }
435
443 };
444
446 { "b", "0" },
448 };
449
450 #if FF_API_OLD_CHANNEL_LAYOUT
451 static const uint64_t aac_channel_layout[] = {
458 #if FDKENC_VER_AT_LEAST(4, 0) // 4.0.0
460 #endif
461 #if FDKENC_VER_AT_LEAST(3, 4) // 3.4.12
464 #endif
465 #if FDKENC_VER_AT_LEAST(4, 0) // 4.0.0
467 #endif
468 0,
469 };
470 #endif /* FF_API_OLD_CHANNEL_LAYOUT */
471
479 #if FDKENC_VER_AT_LEAST(4, 0) // 4.0.0
481 #endif
482 #if FDKENC_VER_AT_LEAST(3, 4) // 3.4.12
485 #endif
486 #if FDKENC_VER_AT_LEAST(4, 0) // 4.0.0
488 #endif
489 { 0 },
490 };
491
493 96000, 88200, 64000, 48000, 44100, 32000,
494 24000, 22050, 16000, 12000, 11025, 8000, 0
495 };
496
498 .
p.
name =
"libfdk_aac",
515 .p.wrapper_name = "libfdk",
518 };