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
65 {
"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 },
66#if FDKENC_VER_AT_LEAST(4, 0)
69 {
"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" },
70 {
"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" },
72 {
"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" },
88 .class_name =
"libfdk_aac",
99 case AACENC_INVALID_HANDLE:
100 return "Invalid handle";
101 case AACENC_MEMORY_ERROR:
102 return "Memory allocation error";
103 case AACENC_UNSUPPORTED_PARAMETER:
104 return "Unsupported parameter";
105 case AACENC_INVALID_CONFIG:
106 return "Invalid config";
107 case AACENC_INIT_ERROR:
108 return "Initialization error";
109 case AACENC_INIT_AAC_ERROR:
110 return "AAC library initialization error";
111 case AACENC_INIT_SBR_ERROR:
112 return "SBR library initialization error";
113 case AACENC_INIT_TP_ERROR:
114 return "Transport library initialization error";
115 case AACENC_INIT_META_ERROR:
116 return "Metadata library initialization error";
117 case AACENC_ENCODE_ERROR:
118 return "Encoding error";
119 case AACENC_ENCODE_EOF:
120 return "End of file";
122 return "Unknown error";
131 aacEncClose(&
s->handle);
140 AACENC_BufDesc in_buf = { 0 }, out_buf = { 0 };
141 AACENC_InArgs in_args = { 0 };
142 AACENC_OutArgs out_args;
143 uint8_t dummy_in[1], dummy_out[1];
144 int in_buffer_identifiers[] = { IN_AUDIO_DATA, IN_METADATA_SETUP };
145 int in_buffer_element_sizes[] = { 2,
sizeof(AACENC_MetaData) };
146 int in_buffer_sizes[] = { 0,
sizeof(
s->metaDataSetup) };
147 int out_buffer_identifier = OUT_BITSTREAM_DATA;
148 int out_buffer_size =
sizeof(dummy_out), out_buffer_element_size = 1;
149 void* inBuffer[] = { dummy_in, &
s->metaDataSetup };
150 void *out_ptr = dummy_out;
155 in_buf.bufs = (
void **)inBuffer;
156 in_buf.numBufs =
s->metadata_mode == 0 ? 1 : 2;
157 in_buf.bufferIdentifiers = in_buffer_identifiers;
158 in_buf.bufSizes = in_buffer_sizes;
159 in_buf.bufElSizes = in_buffer_element_sizes;
162 out_buf.bufs = &out_ptr;
163 out_buf.bufferIdentifiers = &out_buffer_identifier;
164 out_buf.bufSizes = &out_buffer_size;
165 out_buf.bufElSizes = &out_buffer_element_size;
167 err = aacEncEncode(
s->handle, &in_buf, &out_buf, &in_args, &out_args);
168 if (err != AACENC_OK) {
178 AACENC_InfoStruct info = { 0 };
183 int sce = 0, cpe = 0;
194 if ((err = aacEncoder_SetParam(
s->handle, AACENC_AOT, aot)) != AACENC_OK) {
201 if ((err = aacEncoder_SetParam(
s->handle, AACENC_SBR_MODE,
209 if (
s->frame_length >= 0) {
210 if ((err = aacEncoder_SetParam(
s->handle, AACENC_GRANULE_LENGTH,
211 s->frame_length)) != AACENC_OK) {
218 if ((err = aacEncoder_SetParam(
s->handle, AACENC_SAMPLERATE,
226 case 1:
mode = MODE_1; sce = 1; cpe = 0;
break;
228#if FDKENC_VER_AT_LEAST(4, 0)
231 if ((err = aacEncoder_SetParam(
s->handle, AACENC_CHANNELMODE,
232 128)) != AACENC_OK) {
249 case 3:
mode = MODE_1_2; sce = 1; cpe = 1;
break;
250 case 4:
mode = MODE_1_2_1; sce = 2; cpe = 1;
break;
251 case 5:
mode = MODE_1_2_2; sce = 1; cpe = 2;
break;
252 case 6:
mode = MODE_1_2_2_1; sce = 2; cpe = 2;
break;
253#if FDKENC_VER_AT_LEAST(4, 0)
254 case 7:
mode = MODE_6_1; sce = 3; cpe = 2;
break;
258#if FDKENC_VER_AT_LEAST(3, 4)
263 mode = MODE_7_1_REAR_SURROUND;
264#if FDKENC_VER_AT_LEAST(4, 0)
266 mode = MODE_7_1_TOP_FRONT;
270 mode = MODE_7_1_FRONT_CENTER;
280 if ((err = aacEncoder_SetParam(
s->handle, AACENC_CHANNELMODE,
281 mode)) != AACENC_OK) {
287 if ((err = aacEncoder_SetParam(
s->handle, AACENC_CHANNELORDER,
290 "Unable to set wav channel order %d: %s\n",
299 "VBR quality %d out of range, should be 1-5\n",
mode);
303 "Note, the VBR setting is unsupported and only works with "
304 "some parameter combinations\n");
305 if ((err = aacEncoder_SetParam(
s->handle, AACENC_BITRATEMODE,
306 mode)) != AACENC_OK) {
324 if ((err = aacEncoder_SetParam(
s->handle, AACENC_BITRATE,
334 if ((err = aacEncoder_SetParam(
s->handle, AACENC_TRANSMUX,
336 s->latm ? TT_MP4_LOAS : TT_MP4_ADTS)) != AACENC_OK) {
342 if (
s->latm &&
s->header_period) {
343 if ((err = aacEncoder_SetParam(
s->handle, AACENC_HEADER_PERIOD,
344 s->header_period)) != AACENC_OK) {
354 if (
s->signaling < 0)
357 if ((err = aacEncoder_SetParam(
s->handle, AACENC_SIGNALING_MODE,
358 s->signaling)) != AACENC_OK) {
364 if ((err = aacEncoder_SetParam(
s->handle, AACENC_AFTERBURNER,
365 s->afterburner)) != AACENC_OK) {
377 if ((err = aacEncoder_SetParam(
s->handle, AACENC_BANDWIDTH,
378 avctx->
cutoff)) != AACENC_OK) {
385 s->metadata_mode = 0;
387 s->metadata_mode = 1;
388 s->metaDataSetup.prog_ref_level_present = 1;
389 s->metaDataSetup.prog_ref_level =
s->prog_ref << 16;
391 if (
s->drc_profile) {
392 s->metadata_mode = 1;
393 s->metaDataSetup.drc_profile =
s->drc_profile;
394 s->metaDataSetup.drc_TargetRefLevel =
s->drc_target_ref << 16;
395 if (
s->comp_profile) {
397 s->metadata_mode = 2;
398 s->metaDataSetup.comp_profile =
s->comp_profile;
399 s->metaDataSetup.comp_TargetRefLevel =
s->comp_target_ref << 16;
403 if ((err = aacEncoder_SetParam(
s->handle, AACENC_METADATA_MODE,
s->metadata_mode)) != AACENC_OK) {
415 if ((err = aacEncInfo(
s->handle, &info)) != AACENC_OK) {
422#if FDKENC_VER_AT_LEAST(4, 0)
438 memcpy(avctx->
extradata, info.confBuf, info.confSize);
460 AACENC_BufDesc in_buf = { 0 }, out_buf = { 0 };
461 AACENC_InArgs in_args = { 0 };
462 AACENC_OutArgs out_args = { 0 };
463 void* inBuffer[] = { 0, &
s->metaDataSetup };
464 int in_buffer_identifiers[] = { IN_AUDIO_DATA, IN_METADATA_SETUP };
465 int in_buffer_element_sizes[] = { 2,
sizeof(AACENC_MetaData) };
466 int in_buffer_sizes[] = { 0,
sizeof(
s->metaDataSetup) };
467 int out_buffer_identifier = OUT_BITSTREAM_DATA;
468 int out_buffer_size, out_buffer_element_size;
471 uint8_t dummy_buf[1];
478 inBuffer[0] = dummy_buf;
480 in_args.numInSamples = -1;
482 inBuffer[0] =
frame->data[0];
492 if (
s->metadata_mode == 0) {
498 in_buf.bufs = (
void**)inBuffer;
499 in_buf.bufferIdentifiers = in_buffer_identifiers;
500 in_buf.bufSizes = in_buffer_sizes;
501 in_buf.bufElSizes = in_buffer_element_sizes;
508 out_ptr = avpkt->
data;
509 out_buffer_size = avpkt->
size;
510 out_buffer_element_size = 1;
512 out_buf.bufs = &out_ptr;
513 out_buf.bufferIdentifiers = &out_buffer_identifier;
514 out_buf.bufSizes = &out_buffer_size;
515 out_buf.bufElSizes = &out_buffer_element_size;
517 if ((err = aacEncEncode(
s->handle, &in_buf, &out_buf, &in_args,
518 &out_args)) != AACENC_OK) {
519 if (!
frame && err == AACENC_ENCODE_EOF)
526 if (!out_args.numOutBytes)
534 avpkt->
size = out_args.numOutBytes;
561#if FDKENC_VER_AT_LEAST(4, 0)
564#if FDKENC_VER_AT_LEAST(3, 4)
568#if FDKENC_VER_AT_LEAST(4, 0)
575 96000, 88200, 64000, 48000, 44100, 32000,
576 24000, 22050, 16000, 12000, 11025, 8000, 0
580 .p.name =
"libfdk_aac",
598 .p.wrapper_name =
"libfdk",
static av_cold int aac_encode_init(AVCodecContext *avctx)
static const FFCodecDefault aac_encode_defaults[]
static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
const FFCodec ff_libfdk_aac_encoder
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.
Libavcodec external API header.
Public libavutil channel layout APIs header.
#define CODEC_SAMPLERATES_ARRAY(array)
#define FF_CODEC_CAP_NOT_INIT_THREADSAFE
The codec is not known to be init-threadsafe (i.e.
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
#define CODEC_SAMPLEFMTS(...)
#define CODEC_CH_LAYOUTS_ARRAY(array)
common internal and external API header
#define AV_PROFILE_AAC_HE
#define AV_PROFILE_UNKNOWN
#define AV_PROFILE_AAC_LOW
#define AV_PROFILE_AAC_HE_V2
#define AV_PROFILE_AAC_LD
#define AV_PROFILE_AAC_ELD
#define AV_PROFILE_MPEG2_AAC_HE
int(* init)(AVBSFContext *ctx)
mode
Use these values in ebur128_init (or'ed).
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
AVCPBProperties * ff_encode_add_cpb_side_data(AVCodecContext *avctx)
Add a CPB properties side data to an encoding context.
#define AV_OPT_FLAG_AUDIO_PARAM
#define AV_OPT_FLAG_ENCODING_PARAM
A generic parameter which can be set by the user for muxing or encoding.
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
@ AV_OPT_TYPE_INT
Underlying C type is int.
#define AV_CODEC_CAP_ENCODER_FLUSH
This encoder can be flushed using avcodec_flush_buffers().
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
#define AV_CODEC_FLAG_QSCALE
Use fixed qscale.
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
#define AV_CHANNEL_LAYOUT_4POINT0
#define AV_CHANNEL_LAYOUT_5POINT1_BACK
#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_6POINT1_BACK
#define AV_CHANNEL_LAYOUT_5POINT0_BACK
#define AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_SURROUND
#define AV_CHANNEL_LAYOUT_5POINT1POINT2_BACK
#define AV_CHANNEL_LAYOUT_7POINT1
#define AV_CHANNEL_LAYOUT_7POINT1_WIDE_BACK
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
const char * av_default_item_name(void *ptr)
Return the context name.
@ AV_SAMPLE_FMT_S16
signed 16 bits
#define LIBAVUTIL_VERSION_INT
static const int aac_sample_rates[]
static const AVChannelLayout aac_ch_layouts[16]
static const char * aac_get_error(AACENC_ERROR err)
static void aac_encode_flush(AVCodecContext *avctx)
static const AVProfile profiles[]
static const AVOption aac_enc_options[]
static int aac_encode_close(AVCodecContext *avctx)
static av_cold int aac_encode_init(AVCodecContext *avctx)
static const AVClass aac_enc_class
static int aac_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Memory handling functions.
#define FF_AAC_PROFILE_OPTS
AACENC_MetaData metaDataSetup
This structure describes the bitrate properties of an encoded bitstream.
int64_t avg_bitrate
Average bitrate of the stream, in bits per second.
int64_t max_bitrate
Maximum bitrate of the stream, in bits per second.
int64_t min_bitrate
Minimum bitrate of the stream, in bits per second.
An AVChannelLayout holds information about the channel layout of audio data.
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
main external API structure.
AVChannelLayout ch_layout
Audio channel layout.
int global_quality
Global quality for codecs which cannot change it per frame.
int64_t bit_rate
the average bitrate
int initial_padding
Audio only.
int sample_rate
samples per second
int flags
AV_CODEC_FLAG_*.
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
int cutoff
Audio cutoff bandwidth (0 means "automatic")
int frame_size
Number of samples per channel in an audio frame.
This structure describes decoded (raw) audio or video data.
This structure stores compressed data.
int flags
A combination of AV_PKT_FLAG values.
static void error(const char *err)