20 #include <fdk-aac/aacenc_lib.h>
33 #ifdef AACENCODER_LIB_VL0
34 #define FDKENC_VER_AT_LEAST(vl0, vl1) \
35 ((AACENCODER_LIB_VL0 > vl0) || \
36 (AACENCODER_LIB_VL0 == vl0 && AACENCODER_LIB_VL1 >= vl1))
38 #define FDKENC_VER_AT_LEAST(vl0, vl1) 0
66 {
"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 },
67 #if FDKENC_VER_AT_LEAST(4, 0) // 4.0.0
70 {
"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, .unit =
"signaling" },
71 {
"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, .unit =
"signaling" },
73 {
"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, .unit =
"signaling" },
100 case AACENC_INVALID_HANDLE:
101 return "Invalid handle";
102 case AACENC_MEMORY_ERROR:
103 return "Memory allocation error";
104 case AACENC_UNSUPPORTED_PARAMETER:
105 return "Unsupported parameter";
106 case AACENC_INVALID_CONFIG:
107 return "Invalid config";
108 case AACENC_INIT_ERROR:
109 return "Initialization error";
110 case AACENC_INIT_AAC_ERROR:
111 return "AAC library initialization error";
112 case AACENC_INIT_SBR_ERROR:
113 return "SBR library initialization error";
114 case AACENC_INIT_TP_ERROR:
115 return "Transport library initialization error";
116 case AACENC_INIT_META_ERROR:
117 return "Metadata library initialization error";
118 case AACENC_ENCODE_ERROR:
119 return "Encoding error";
120 case AACENC_ENCODE_EOF:
121 return "End of file";
123 return "Unknown error";
132 aacEncClose(&
s->handle);
141 AACENC_BufDesc in_buf = { 0 }, out_buf = { 0 };
142 AACENC_InArgs in_args = { 0 };
143 AACENC_OutArgs out_args;
145 uint8_t dummy_in[1], dummy_out[1];
146 int in_buffer_identifiers[] = { IN_AUDIO_DATA, IN_METADATA_SETUP };
147 int in_buffer_element_sizes[] = { 2,
sizeof(AACENC_MetaData) };
148 int in_buffer_sizes[] = { 0,
sizeof(
s->metaDataSetup) };
149 int out_buffer_identifier = OUT_BITSTREAM_DATA;
150 int out_buffer_size =
sizeof(dummy_out), out_buffer_element_size = 1;
151 void* inBuffer[] = { dummy_in, &
s->metaDataSetup };
152 void *out_ptr = dummy_out;
157 in_buf.bufs = (
void **)inBuffer;
158 in_buf.numBufs =
s->metadata_mode == 0 ? 1 : 2;
159 in_buf.bufferIdentifiers = in_buffer_identifiers;
160 in_buf.bufSizes = in_buffer_sizes;
161 in_buf.bufElSizes = in_buffer_element_sizes;
164 out_buf.bufs = &out_ptr;
165 out_buf.bufferIdentifiers = &out_buffer_identifier;
166 out_buf.bufSizes = &out_buffer_size;
167 out_buf.bufElSizes = &out_buffer_element_size;
169 err = aacEncEncode(
s->handle, &in_buf, &out_buf, &in_args, &out_args);
170 if (err != AACENC_OK) {
180 AACENC_InfoStruct
info = { 0 };
184 int sce = 0, cpe = 0;
195 if ((err = aacEncoder_SetParam(
s->handle, AACENC_AOT, aot)) != AACENC_OK) {
202 if ((err = aacEncoder_SetParam(
s->handle, AACENC_SBR_MODE,
210 if (
s->frame_length >= 0) {
211 if ((err = aacEncoder_SetParam(
s->handle, AACENC_GRANULE_LENGTH,
212 s->frame_length)) != AACENC_OK) {
219 if ((err = aacEncoder_SetParam(
s->handle, AACENC_SAMPLERATE,
227 case 1:
mode = MODE_1; sce = 1; cpe = 0;
break;
229 #if FDKENC_VER_AT_LEAST(4, 0) // 4.0.0
232 if ((err = aacEncoder_SetParam(
s->handle, AACENC_CHANNELMODE,
233 128)) != AACENC_OK) {
250 case 3:
mode = MODE_1_2; sce = 1; cpe = 1;
break;
251 case 4:
mode = MODE_1_2_1; sce = 2; cpe = 1;
break;
252 case 5:
mode = MODE_1_2_2; sce = 1; cpe = 2;
break;
253 case 6:
mode = MODE_1_2_2_1; sce = 2; cpe = 2;
break;
254 #if FDKENC_VER_AT_LEAST(4, 0) // 4.0.0
255 case 7:
mode = MODE_6_1; sce = 3; cpe = 2;
break;
259 #if FDKENC_VER_AT_LEAST(3, 4) // 3.4.12
264 mode = MODE_7_1_REAR_SURROUND;
265 #if FDKENC_VER_AT_LEAST(4, 0) // 4.0.0
267 mode = MODE_7_1_TOP_FRONT;
271 mode = MODE_7_1_FRONT_CENTER;
281 if ((err = aacEncoder_SetParam(
s->handle, AACENC_CHANNELMODE,
282 mode)) != AACENC_OK) {
288 if ((err = aacEncoder_SetParam(
s->handle, AACENC_CHANNELORDER,
291 "Unable to set wav channel order %d: %s\n",
300 "VBR quality %d out of range, should be 1-5\n",
mode);
304 "Note, the VBR setting is unsupported and only works with "
305 "some parameter combinations\n");
306 if ((err = aacEncoder_SetParam(
s->handle, AACENC_BITRATEMODE,
307 mode)) != AACENC_OK) {
325 if ((err = aacEncoder_SetParam(
s->handle, AACENC_BITRATE,
335 if ((err = aacEncoder_SetParam(
s->handle, AACENC_TRANSMUX,
337 s->latm ? TT_MP4_LOAS : TT_MP4_ADTS)) != AACENC_OK) {
343 if (
s->latm &&
s->header_period) {
344 if ((err = aacEncoder_SetParam(
s->handle, AACENC_HEADER_PERIOD,
345 s->header_period)) != AACENC_OK) {
355 if (
s->signaling < 0)
358 if ((err = aacEncoder_SetParam(
s->handle, AACENC_SIGNALING_MODE,
359 s->signaling)) != AACENC_OK) {
365 if ((err = aacEncoder_SetParam(
s->handle, AACENC_AFTERBURNER,
366 s->afterburner)) != AACENC_OK) {
378 if ((err = aacEncoder_SetParam(
s->handle, AACENC_BANDWIDTH,
379 avctx->
cutoff)) != AACENC_OK) {
386 s->metadata_mode = 0;
388 s->metadata_mode = 1;
389 s->metaDataSetup.prog_ref_level_present = 1;
390 s->metaDataSetup.prog_ref_level =
s->prog_ref << 16;
392 if (
s->drc_profile) {
393 s->metadata_mode = 1;
394 s->metaDataSetup.drc_profile =
s->drc_profile;
395 s->metaDataSetup.drc_TargetRefLevel =
s->drc_target_ref << 16;
396 if (
s->comp_profile) {
398 s->metadata_mode = 2;
399 s->metaDataSetup.comp_profile =
s->comp_profile;
400 s->metaDataSetup.comp_TargetRefLevel =
s->comp_target_ref << 16;
404 if ((err = aacEncoder_SetParam(
s->handle, AACENC_METADATA_MODE,
s->metadata_mode)) != AACENC_OK) {
416 if ((err = aacEncInfo(
s->handle, &
info)) != AACENC_OK) {
423 #if FDKENC_VER_AT_LEAST(4, 0) // 4.0.0
451 AACENC_BufDesc in_buf = { 0 }, out_buf = { 0 };
452 AACENC_InArgs in_args = { 0 };
453 AACENC_OutArgs out_args = { 0 };
454 void* inBuffer[] = { 0, &
s->metaDataSetup };
455 int in_buffer_identifiers[] = { IN_AUDIO_DATA, IN_METADATA_SETUP };
456 int in_buffer_element_sizes[] = { 2,
sizeof(AACENC_MetaData) };
457 int in_buffer_sizes[] = { 0,
sizeof(
s->metaDataSetup) };
458 int out_buffer_identifier = OUT_BITSTREAM_DATA;
459 int out_buffer_size, out_buffer_element_size;
461 int ret, discard_padding;
462 uint8_t dummy_buf[1];
469 inBuffer[0] = dummy_buf;
471 in_args.numInSamples = -1;
473 inBuffer[0] =
frame->data[0];
483 if (
s->metadata_mode == 0) {
489 in_buf.bufs = (
void**)inBuffer;
490 in_buf.bufferIdentifiers = in_buffer_identifiers;
491 in_buf.bufSizes = in_buffer_sizes;
492 in_buf.bufElSizes = in_buffer_element_sizes;
499 out_ptr = avpkt->
data;
500 out_buffer_size = avpkt->
size;
501 out_buffer_element_size = 1;
503 out_buf.bufs = &out_ptr;
504 out_buf.bufferIdentifiers = &out_buffer_identifier;
505 out_buf.bufSizes = &out_buffer_size;
506 out_buf.bufElSizes = &out_buffer_element_size;
508 if ((err = aacEncEncode(
s->handle, &in_buf, &out_buf, &in_args,
509 &out_args)) != AACENC_OK) {
510 if (!
frame && err == AACENC_ENCODE_EOF)
517 if (!out_args.numOutBytes)
530 if ((!
s->delay_sent && avctx->
initial_padding > 0) || discard_padding > 0) {
535 if (!
s->delay_sent) {
539 AV_WL32(side_data + 4, discard_padding);
542 avpkt->
size = out_args.numOutBytes;
568 #if FDKENC_VER_AT_LEAST(4, 0) // 4.0.0
571 #if FDKENC_VER_AT_LEAST(3, 4) // 3.4.12
575 #if FDKENC_VER_AT_LEAST(4, 0) // 4.0.0
582 96000, 88200, 64000, 48000, 44100, 32000,
583 24000, 22050, 16000, 12000, 11025, 8000, 0
587 .
p.
name =
"libfdk_aac",
606 .p.wrapper_name =
"libfdk",