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)
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" },
89 .class_name =
"libfdk_aac",
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;
144 uint8_t dummy_in[1], dummy_out[1];
145 int in_buffer_identifiers[] = { IN_AUDIO_DATA, IN_METADATA_SETUP };
146 int in_buffer_element_sizes[] = { 2,
sizeof(AACENC_MetaData) };
147 int in_buffer_sizes[] = { 0,
sizeof(
s->metaDataSetup) };
148 int out_buffer_identifier = OUT_BITSTREAM_DATA;
149 int out_buffer_size =
sizeof(dummy_out), out_buffer_element_size = 1;
150 void* inBuffer[] = { dummy_in, &
s->metaDataSetup };
151 void *out_ptr = dummy_out;
156 in_buf.bufs = (
void **)inBuffer;
157 in_buf.numBufs =
s->metadata_mode == 0 ? 1 : 2;
158 in_buf.bufferIdentifiers = in_buffer_identifiers;
159 in_buf.bufSizes = in_buffer_sizes;
160 in_buf.bufElSizes = in_buffer_element_sizes;
163 out_buf.bufs = &out_ptr;
164 out_buf.bufferIdentifiers = &out_buffer_identifier;
165 out_buf.bufSizes = &out_buffer_size;
166 out_buf.bufElSizes = &out_buffer_element_size;
168 err = aacEncEncode(
s->handle, &in_buf, &out_buf, &in_args, &out_args);
169 if (err != AACENC_OK) {
179 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)
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)
255 case 7:
mode = MODE_6_1; sce = 3; cpe = 2;
break;
259#if FDKENC_VER_AT_LEAST(3, 4)
264 mode = MODE_7_1_REAR_SURROUND;
265#if FDKENC_VER_AT_LEAST(4, 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) {
424#if FDKENC_VER_AT_LEAST(4, 0)
440 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_7POINT1_TOP_BACK
#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_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)