20#include <fdk-aac/aacdecoder_lib.h>
31#ifdef AACDECODER_LIB_VL0
32#define FDKDEC_VER_AT_LEAST(vl0, vl1) \
33 ((AACDECODER_LIB_VL0 > vl0) || \
34 (AACDECODER_LIB_VL0 == vl0 && AACDECODER_LIB_VL1 >= vl1))
36#define FDKDEC_VER_AT_LEAST(vl0, vl1) 0
39#if !FDKDEC_VER_AT_LEAST(2, 5)
40#define AAC_PCM_MAX_OUTPUT_CHANNELS AAC_PCM_OUTPUT_CHANNELS
64#if FDKDEC_VER_AT_LEAST(2, 5)
77#define DMX_ANC_BUFFSIZE 128
78#define DECODER_MAX_CHANNELS 8
79#define DECODER_BUFFSIZE 2048 * sizeof(INT_PCM)
81#define OFFSET(x) offsetof(FDKAACDecContext, x)
82#define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
88 {
"drc_boost",
"Dynamic Range Control: boost, where [0] is none and [127] is max boost",
90 {
"drc_cut",
"Dynamic Range Control: attenuation factor, where [0] is none and [127] is max compression",
92 {
"drc_level",
"Dynamic Range Control: reference level, quantized to 0.25dB steps where [0] is 0dB and [127] is -31.75dB, -1 for auto, and -2 for disabled",
94 {
"drc_heavy",
"Dynamic Range Control: heavy compression, where [1] is on (RF mode) and [0] is off",
96#if FDKDEC_VER_AT_LEAST(2, 5)
97 {
"level_limit",
"Signal level limiting",
100#if FDKDEC_VER_AT_LEAST(3, 0)
101 {
"drc_effect",
"Dynamic Range Control: effect type, where e.g. [0] is none and [6] is general",
104#if FDKDEC_VER_AT_LEAST(3, 1)
105 {
"album_mode",
"Dynamic Range Control: album mode, where [0] is off and [1] is on",
113 .class_name =
"libfdk-aac decoder",
122 CStreamInfo *info = aacDecoder_GetStreamInfo(
s->handle);
123 int channel_counts[0x24] = { 0 };
125 uint64_t ch_layout = 0;
132 if (info->sampleRate <= 0) {
138 avctx->
profile = info->aot - 1;
141#if FDKDEC_VER_AT_LEAST(2, 5)
142 if (!
s->output_delay_set && info->outputDelay) {
144 s->flush_samples = info->outputDelay;
145 s->skip_samples = info->outputDelay;
146 s->output_delay = info->outputDelay;
149 s->output_delay_set = 1;
153 for (
i = 0;
i < info->numChannels;
i++) {
154 AUDIO_CHANNEL_TYPE
ctype = info->pChannelType[
i];
159 channel_counts[
ctype]++;
162 "%d channels - front:%d side:%d back:%d lfe:%d top:%d bottom %d\n",
164 channel_counts[ACT_FRONT], channel_counts[ACT_SIDE],
165 channel_counts[ACT_BACK], channel_counts[ACT_LFE],
166 channel_counts[ACT_FRONT_TOP] + channel_counts[ACT_SIDE_TOP] +
167 channel_counts[ACT_BACK_TOP] + channel_counts[ACT_TOP],
168 channel_counts[ACT_FRONT_BOTTOM] + channel_counts[ACT_SIDE_BOTTOM] +
169 channel_counts[ACT_BACK_BOTTOM] + channel_counts[ACT_BOTTOM]);
171 switch (channel_counts[ACT_FRONT]) {
182 if (channel_counts[ACT_FRONT] & 1)
187 "unsupported number of front channels: %d\n",
188 channel_counts[ACT_FRONT]);
193 if (channel_counts[ACT_SIDE] > 0) {
194 if (channel_counts[ACT_SIDE] == 2) {
198 "unsupported number of side channels: %d\n",
199 channel_counts[ACT_SIDE]);
203 if (channel_counts[ACT_BACK] > 0) {
204 switch (channel_counts[ACT_BACK]) {
213 if (channel_counts[ACT_BACK] & 1)
218 "unsupported number of back channels: %d\n",
219 channel_counts[ACT_BACK]);
224 if (channel_counts[ACT_LFE] > 0) {
225 if (channel_counts[ACT_LFE] == 1) {
229 "unsupported number of LFE channels: %d\n",
230 channel_counts[ACT_LFE]);
234 if (channel_counts[ACT_FRONT_TOP] > 0) {
235 switch (channel_counts[ACT_FRONT_TOP]) {
241 if (channel_counts[ACT_FRONT_TOP] & 1)
246 "unsupported number of top front channels: %d\n",
247 channel_counts[ACT_FRONT_TOP]);
252 if (channel_counts[ACT_BACK_TOP] > 0) {
253 switch (channel_counts[ACT_BACK_TOP]) {
259 if (channel_counts[ACT_BACK_TOP] & 1)
264 "unsupported number of top back channels: %d\n",
265 channel_counts[ACT_BACK_TOP]);
270 if (channel_counts[ACT_SIDE_TOP] > 0) {
271 switch (channel_counts[ACT_SIDE_TOP]) {
277 if (channel_counts[ACT_SIDE_TOP] & 1)
282 "unsupported number of top side channels: %d\n",
283 channel_counts[ACT_SIDE_TOP]);
288 if (channel_counts[ACT_FRONT_BOTTOM] > 0) {
289 switch (channel_counts[ACT_FRONT_BOTTOM]) {
295 if (channel_counts[ACT_FRONT_BOTTOM] & 1)
300 "unsupported number of bottom front channels: %d\n",
301 channel_counts[ACT_FRONT_BOTTOM]);
324 aacDecoder_Close(
s->handle);
334 AAC_DECODER_ERROR err;
336 s->handle = aacDecoder_Open(avctx->
extradata_size ? TT_MP4_RAW : TT_MP4_ADTS, 1);
343 if ((err = aacDecoder_ConfigRaw(
s->handle, &avctx->
extradata,
350 if ((err = aacDecoder_SetParam(
s->handle, AAC_CONCEAL_METHOD,
351 s->conceal_method)) != AAC_DEC_OK) {
356 if (
s->downmix_layout.nb_channels > 0 &&
358 int downmix_channels = -1;
360 switch (
s->downmix_layout.u.mask) {
363 downmix_channels = 2;
366 downmix_channels = 1;
373 if (downmix_channels != -1) {
375 downmix_channels) != AAC_DEC_OK) {
379 if (!
s->anc_buffer) {
384 av_log(avctx,
AV_LOG_ERROR,
"Unable to register downmix ancillary buffer in the decoder\n");
389#if FDKDEC_VER_AT_LEAST(2, 5)
397 if (
s->drc_boost != -1) {
398 if (aacDecoder_SetParam(
s->handle, AAC_DRC_BOOST_FACTOR,
s->drc_boost) != AAC_DEC_OK) {
404 if (
s->drc_cut != -1) {
405 if (aacDecoder_SetParam(
s->handle, AAC_DRC_ATTENUATION_FACTOR,
s->drc_cut) != AAC_DEC_OK) {
411 if (
s->drc_level != -1) {
418 if (aacDecoder_SetParam(
s->handle, AAC_DRC_REFERENCE_LEVEL,
s->drc_level) != AAC_DEC_OK) {
424 if (
s->drc_heavy != -1) {
425 if (aacDecoder_SetParam(
s->handle, AAC_DRC_HEAVY_COMPRESSION,
s->drc_heavy) != AAC_DEC_OK) {
431#if FDKDEC_VER_AT_LEAST(2, 5)
433 if (aacDecoder_SetParam(
s->handle, AAC_PCM_LIMITER_ENABLE,
s->level_limit) != AAC_DEC_OK) {
434 av_log(avctx,
AV_LOG_ERROR,
"Unable to set in signal level limiting in the decoder\n");
439#if FDKDEC_VER_AT_LEAST(3, 0)
440 if (
s->drc_effect != -1) {
441 if (aacDecoder_SetParam(
s->handle, AAC_UNIDRC_SET_EFFECT,
s->drc_effect) != AAC_DEC_OK) {
448#if FDKDEC_VER_AT_LEAST(3, 1)
449 if (
s->album_mode != -1) {
450 if (aacDecoder_SetParam(
s->handle, AAC_UNIDRC_ALBUM_MODE,
s->album_mode) != AAC_DEC_OK) {
460 s->decoder_buffer =
av_malloc(
s->decoder_buffer_size);
461 if (!
s->decoder_buffer)
468 int *got_frame_ptr,
AVPacket *avpkt)
472 AAC_DECODER_ERROR err;
473 UINT valid = avpkt->
size;
477 err = aacDecoder_Fill(
s->handle, &avpkt->
data, &avpkt->
size, &valid);
478 if (err != AAC_DEC_OK) {
483#if FDKDEC_VER_AT_LEAST(2, 5)
485 if (
s->flush_samples > 0) {
486 flags |= AACDEC_FLUSH;
495 err = aacDecoder_DecodeFrame(
s->handle, (INT_PCM *)
s->decoder_buffer,
496 s->decoder_buffer_size /
sizeof(INT_PCM),
498 if (err == AAC_DEC_NOT_ENOUGH_BITS) {
499 ret = avpkt->
size - valid;
502 if (err != AAC_DEC_OK) {
504 "aacDecoder_DecodeFrame() failed: %x\n", err);
516#if FDKDEC_VER_AT_LEAST(2, 5)
517 if (
flags & AACDEC_FLUSH) {
531 frame->nb_samples,
s->flush_samples);
532 if (
s->skip_samples ||
s->discard_padding) {
537 memset(sd->
data, 0, 10);
539 if (sd && sd->
size >= 10) {
544 s->discard_padding = 0;
546 s->flush_samples -=
frame->nb_samples;
548 s->last_dts =
frame->pkt_dts;
551 if (!sd &&
s->skip_samples) {
554 memset(sd->
data, 0, 10);
556 if (sd && sd->
size >= 10) {
559 if (
s->discard_padding >=
s->output_delay) {
561 s->discard_padding =
s->output_delay;
578 s->last_dts =
frame->pkt_dts;
582 memcpy(
frame->extended_data[0],
s->decoder_buffer,
587 ret = avpkt->
size - valid;
596 AAC_DECODER_ERROR err;
601 if ((err = aacDecoder_SetParam(
s->handle,
602 AAC_TPDEC_CLEAR_BUFFER, 1)) != AAC_DEC_OK)
605#if FDKDEC_VER_AT_LEAST(2, 5)
607 s->discard_padding = 0;
608 s->flush_samples = 0;
612 s->output_delay_set = 0;
619 aacDecoder_DecodeFrame(
s->handle, (INT_PCM *)
s->decoder_buffer,
620 s->decoder_buffer_size /
sizeof(INT_PCM),
621 AACDEC_FLUSH | AACDEC_CLRHIST);
626 .p.name =
"libfdk_aac",
636#if FDKDEC_VER_AT_LEAST(2, 5)
642 .p.wrapper_name =
"libfdk",
const FFCodec ff_libfdk_aac_decoder
Libavcodec external API header.
#define flags(name, subs,...)
#define i(width, name, range_min, range_max)
Public libavutil channel layout APIs header.
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
common internal and external API header
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
int(* init)(AVBSFContext *ctx)
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
@ AV_OPT_TYPE_INT
Underlying C type is int.
@ AV_OPT_TYPE_CHLAYOUT
Underlying C type is AVChannelLayout.
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
#define AV_CH_LAYOUT_STEREO_DOWNMIX
#define AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_STEREO
#define AV_CH_TOP_FRONT_LEFT
#define AV_CH_TOP_BACK_CENTER
#define AV_CH_BOTTOM_FRONT_CENTER
#define AV_CH_FRONT_RIGHT_OF_CENTER
#define AV_CH_BACK_CENTER
#define AV_CH_TOP_FRONT_CENTER
#define AV_CH_FRONT_LEFT_OF_CENTER
#define AV_CH_TOP_SIDE_RIGHT
#define AV_CH_FRONT_CENTER
#define AV_CH_TOP_BACK_RIGHT
#define AV_CH_TOP_SIDE_LEFT
#define AV_CH_TOP_BACK_LEFT
#define AV_CH_LOW_FREQUENCY
#define AV_CH_BOTTOM_FRONT_RIGHT
#define AV_CH_TOP_FRONT_RIGHT
#define AV_CH_BOTTOM_FRONT_LEFT
#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_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
int av_channel_layout_from_mask(AVChannelLayout *channel_layout, uint64_t mask)
Initialize a native channel layout from a bitmask indicating which channels are present.
@ AV_CHANNEL_ORDER_NATIVE
The native channel order, i.e.
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
AVFrameSideData * av_frame_get_side_data(const AVFrame *frame, enum AVFrameSideDataType type)
AVFrameSideData * av_frame_new_side_data(AVFrame *frame, enum AVFrameSideDataType type, size_t size)
Add a new side data to a frame.
@ AV_FRAME_DATA_SKIP_SAMPLES
Recommends skipping the specified number of samples.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#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.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
@ AV_SAMPLE_FMT_S16
signed 16 bits
#define AV_NOPTS_VALUE
Undefined timestamp value.
#define LIBAVUTIL_VERSION_INT
static av_cold void fdk_aac_decode_flush(AVCodecContext *avctx)
static const AVOption fdk_aac_dec_options[]
static int get_stream_info(AVCodecContext *avctx, AVFrame *frame)
static av_cold int fdk_aac_decode_init(AVCodecContext *avctx)
#define AAC_PCM_MAX_OUTPUT_CHANNELS
static av_cold int fdk_aac_decode_close(AVCodecContext *avctx)
@ CONCEAL_METHOD_SPECTRAL_MUTING
@ CONCEAL_METHOD_ENERGY_INTERPOLATION
@ CONCEAL_METHOD_NOISE_SUBSTITUTION
static const AVClass fdk_aac_dec_class
static int fdk_aac_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
#define DECODER_MAX_CHANNELS
Memory handling functions.
#define FF_ARRAY_ELEMS(a)
An AVChannelLayout holds information about the channel layout of audio data.
enum AVChannelOrder order
Channel order used in this layout.
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.
enum AVSampleFormat sample_fmt
audio sample format
int sample_rate
samples per second
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
int frame_size
Number of samples per channel in an audio frame.
Structure to hold side data for an AVFrame.
This structure describes decoded (raw) audio or video data.
This structure stores compressed data.
Rational number (pair of numerator and denominator).
AVChannelLayout downmix_layout