20#include <fdk-aac/aacdecoder_lib.h>
30#ifdef AACDECODER_LIB_VL0
31#define FDKDEC_VER_AT_LEAST(vl0, vl1) \
32 ((AACDECODER_LIB_VL0 > vl0) || \
33 (AACDECODER_LIB_VL0 == vl0 && AACDECODER_LIB_VL1 >= vl1))
35#define FDKDEC_VER_AT_LEAST(vl0, vl1) 0
38#if !FDKDEC_VER_AT_LEAST(2, 5)
39#define AAC_PCM_MAX_OUTPUT_CHANNELS AAC_PCM_OUTPUT_CHANNELS
63#if FDKDEC_VER_AT_LEAST(2, 5)
72#define DMX_ANC_BUFFSIZE 128
73#define DECODER_MAX_CHANNELS 8
74#define DECODER_BUFFSIZE 2048 * sizeof(INT_PCM)
76#define OFFSET(x) offsetof(FDKAACDecContext, x)
77#define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
83 {
"drc_boost",
"Dynamic Range Control: boost, where [0] is none and [127] is max boost",
85 {
"drc_cut",
"Dynamic Range Control: attenuation factor, where [0] is none and [127] is max compression",
87 {
"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",
89 {
"drc_heavy",
"Dynamic Range Control: heavy compression, where [1] is on (RF mode) and [0] is off",
91#if FDKDEC_VER_AT_LEAST(2, 5)
92 {
"level_limit",
"Signal level limiting",
95#if FDKDEC_VER_AT_LEAST(3, 0)
96 {
"drc_effect",
"Dynamic Range Control: effect type, where e.g. [0] is none and [6] is general",
99#if FDKDEC_VER_AT_LEAST(3, 1)
100 {
"album_mode",
"Dynamic Range Control: album mode, where [0] is off and [1] is on",
108 .class_name =
"libfdk-aac decoder",
117 CStreamInfo *info = aacDecoder_GetStreamInfo(
s->handle);
118 int channel_counts[0x24] = { 0 };
120 uint64_t ch_layout = 0;
127 if (info->sampleRate <= 0) {
133 avctx->
profile = info->aot - 1;
136#if FDKDEC_VER_AT_LEAST(2, 5)
137 if (!
s->output_delay_set && info->outputDelay) {
139 s->flush_samples = info->outputDelay;
140 s->delay_samples = info->outputDelay;
141 s->output_delay_set = 1;
145 for (
i = 0;
i < info->numChannels;
i++) {
146 AUDIO_CHANNEL_TYPE
ctype = info->pChannelType[
i];
151 channel_counts[
ctype]++;
154 "%d channels - front:%d side:%d back:%d lfe:%d top:%d bottom %d\n",
156 channel_counts[ACT_FRONT], channel_counts[ACT_SIDE],
157 channel_counts[ACT_BACK], channel_counts[ACT_LFE],
158 channel_counts[ACT_FRONT_TOP] + channel_counts[ACT_SIDE_TOP] +
159 channel_counts[ACT_BACK_TOP] + channel_counts[ACT_TOP],
160 channel_counts[ACT_FRONT_BOTTOM] + channel_counts[ACT_SIDE_BOTTOM] +
161 channel_counts[ACT_BACK_BOTTOM] + channel_counts[ACT_BOTTOM]);
163 switch (channel_counts[ACT_FRONT]) {
174 if (channel_counts[ACT_FRONT] & 1)
179 "unsupported number of front channels: %d\n",
180 channel_counts[ACT_FRONT]);
185 if (channel_counts[ACT_SIDE] > 0) {
186 if (channel_counts[ACT_SIDE] == 2) {
190 "unsupported number of side channels: %d\n",
191 channel_counts[ACT_SIDE]);
195 if (channel_counts[ACT_BACK] > 0) {
196 switch (channel_counts[ACT_BACK]) {
205 if (channel_counts[ACT_BACK] & 1)
210 "unsupported number of back channels: %d\n",
211 channel_counts[ACT_BACK]);
216 if (channel_counts[ACT_LFE] > 0) {
217 if (channel_counts[ACT_LFE] == 1) {
221 "unsupported number of LFE channels: %d\n",
222 channel_counts[ACT_LFE]);
226 if (channel_counts[ACT_FRONT_TOP] > 0) {
227 switch (channel_counts[ACT_FRONT_TOP]) {
233 if (channel_counts[ACT_FRONT_TOP] & 1)
238 "unsupported number of top front channels: %d\n",
239 channel_counts[ACT_FRONT_TOP]);
244 if (channel_counts[ACT_BACK_TOP] > 0) {
245 switch (channel_counts[ACT_BACK_TOP]) {
251 if (channel_counts[ACT_BACK_TOP] & 1)
256 "unsupported number of top back channels: %d\n",
257 channel_counts[ACT_BACK_TOP]);
262 if (channel_counts[ACT_SIDE_TOP] > 0) {
263 switch (channel_counts[ACT_SIDE_TOP]) {
269 if (channel_counts[ACT_SIDE_TOP] & 1)
274 "unsupported number of top side channels: %d\n",
275 channel_counts[ACT_SIDE_TOP]);
280 if (channel_counts[ACT_FRONT_BOTTOM] > 0) {
281 switch (channel_counts[ACT_FRONT_BOTTOM]) {
287 if (channel_counts[ACT_FRONT_BOTTOM] & 1)
292 "unsupported number of bottom front channels: %d\n",
293 channel_counts[ACT_FRONT_BOTTOM]);
316 aacDecoder_Close(
s->handle);
326 AAC_DECODER_ERROR err;
328 s->handle = aacDecoder_Open(avctx->
extradata_size ? TT_MP4_RAW : TT_MP4_ADTS, 1);
335 if ((err = aacDecoder_ConfigRaw(
s->handle, &avctx->
extradata,
342 if ((err = aacDecoder_SetParam(
s->handle, AAC_CONCEAL_METHOD,
343 s->conceal_method)) != AAC_DEC_OK) {
348 if (
s->downmix_layout.nb_channels > 0 &&
350 int downmix_channels = -1;
352 switch (
s->downmix_layout.u.mask) {
355 downmix_channels = 2;
358 downmix_channels = 1;
365 if (downmix_channels != -1) {
367 downmix_channels) != AAC_DEC_OK) {
371 if (!
s->anc_buffer) {
376 av_log(avctx,
AV_LOG_ERROR,
"Unable to register downmix ancillary buffer in the decoder\n");
381#if FDKDEC_VER_AT_LEAST(2, 5)
389 if (
s->drc_boost != -1) {
390 if (aacDecoder_SetParam(
s->handle, AAC_DRC_BOOST_FACTOR,
s->drc_boost) != AAC_DEC_OK) {
396 if (
s->drc_cut != -1) {
397 if (aacDecoder_SetParam(
s->handle, AAC_DRC_ATTENUATION_FACTOR,
s->drc_cut) != AAC_DEC_OK) {
403 if (
s->drc_level != -1) {
410 if (aacDecoder_SetParam(
s->handle, AAC_DRC_REFERENCE_LEVEL,
s->drc_level) != AAC_DEC_OK) {
416 if (
s->drc_heavy != -1) {
417 if (aacDecoder_SetParam(
s->handle, AAC_DRC_HEAVY_COMPRESSION,
s->drc_heavy) != AAC_DEC_OK) {
423#if FDKDEC_VER_AT_LEAST(2, 5)
425 if (aacDecoder_SetParam(
s->handle, AAC_PCM_LIMITER_ENABLE,
s->level_limit) != AAC_DEC_OK) {
426 av_log(avctx,
AV_LOG_ERROR,
"Unable to set in signal level limiting in the decoder\n");
431#if FDKDEC_VER_AT_LEAST(3, 0)
432 if (
s->drc_effect != -1) {
433 if (aacDecoder_SetParam(
s->handle, AAC_UNIDRC_SET_EFFECT,
s->drc_effect) != AAC_DEC_OK) {
440#if FDKDEC_VER_AT_LEAST(3, 1)
441 if (
s->album_mode != -1) {
442 if (aacDecoder_SetParam(
s->handle, AAC_UNIDRC_ALBUM_MODE,
s->album_mode) != AAC_DEC_OK) {
452 s->decoder_buffer =
av_malloc(
s->decoder_buffer_size);
453 if (!
s->decoder_buffer)
460 int *got_frame_ptr,
AVPacket *avpkt)
464 AAC_DECODER_ERROR err;
465 UINT valid = avpkt->
size;
467 int input_offset = 0;
470 err = aacDecoder_Fill(
s->handle, &avpkt->
data, &avpkt->
size, &valid);
471 if (err != AAC_DEC_OK) {
476#if FDKDEC_VER_AT_LEAST(2, 5)
478 if (
s->flush_samples > 0) {
479 flags |= AACDEC_FLUSH;
488 err = aacDecoder_DecodeFrame(
s->handle, (INT_PCM *)
s->decoder_buffer,
489 s->decoder_buffer_size /
sizeof(INT_PCM),
491 if (err == AAC_DEC_NOT_ENOUGH_BITS) {
492 ret = avpkt->
size - valid;
495 if (err != AAC_DEC_OK) {
497 "aacDecoder_DecodeFrame() failed: %x\n", err);
506#if FDKDEC_VER_AT_LEAST(2, 5)
507 if (
flags & AACDEC_FLUSH) {
512 frame->nb_samples,
s->flush_samples);
513 s->flush_samples -=
frame->nb_samples;
518 if (
s->delay_samples) {
519 int drop_samples =
FFMIN(
s->delay_samples,
frame->nb_samples);
521 drop_samples,
s->delay_samples);
522 s->delay_samples -= drop_samples;
523 frame->nb_samples -= drop_samples;
525 if (
frame->nb_samples <= 0)
534 memcpy(
frame->extended_data[0],
s->decoder_buffer + input_offset,
539 ret = avpkt->
size - valid;
548 AAC_DECODER_ERROR err;
553 if ((err = aacDecoder_SetParam(
s->handle,
554 AAC_TPDEC_CLEAR_BUFFER, 1)) != AAC_DEC_OK)
559 .p.name =
"libfdk_aac",
569#if FDKDEC_VER_AT_LEAST(2, 5)
575 .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 AVERROR_EOF
End of file.
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
#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.
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
@ AV_SAMPLE_FMT_S16
signed 16 bits
#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
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.
This structure describes decoded (raw) audio or video data.
This structure stores compressed data.
AVChannelLayout downmix_layout