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\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]);
161 switch (channel_counts[ACT_FRONT]) {
172 if (channel_counts[ACT_FRONT] & 1)
177 "unsupported number of front channels: %d\n",
178 channel_counts[ACT_FRONT]);
183 if (channel_counts[ACT_SIDE] > 0) {
184 if (channel_counts[ACT_SIDE] == 2) {
188 "unsupported number of side channels: %d\n",
189 channel_counts[ACT_SIDE]);
193 if (channel_counts[ACT_BACK] > 0) {
194 switch (channel_counts[ACT_BACK]) {
203 if (channel_counts[ACT_BACK] & 1)
208 "unsupported number of back channels: %d\n",
209 channel_counts[ACT_BACK]);
214 if (channel_counts[ACT_LFE] > 0) {
215 if (channel_counts[ACT_LFE] == 1) {
219 "unsupported number of LFE channels: %d\n",
220 channel_counts[ACT_LFE]);
224 if (channel_counts[ACT_FRONT_TOP] > 0) {
225 switch (channel_counts[ACT_FRONT_TOP]) {
231 if (channel_counts[ACT_FRONT_TOP] & 1)
236 "unsupported number of top front channels: %d\n",
237 channel_counts[ACT_FRONT_TOP]);
260 aacDecoder_Close(
s->handle);
270 AAC_DECODER_ERROR err;
272 s->handle = aacDecoder_Open(avctx->
extradata_size ? TT_MP4_RAW : TT_MP4_ADTS, 1);
279 if ((err = aacDecoder_ConfigRaw(
s->handle, &avctx->
extradata,
286 if ((err = aacDecoder_SetParam(
s->handle, AAC_CONCEAL_METHOD,
287 s->conceal_method)) != AAC_DEC_OK) {
292 if (
s->downmix_layout.nb_channels > 0 &&
294 int downmix_channels = -1;
296 switch (
s->downmix_layout.u.mask) {
299 downmix_channels = 2;
302 downmix_channels = 1;
309 if (downmix_channels != -1) {
311 downmix_channels) != AAC_DEC_OK) {
315 if (!
s->anc_buffer) {
320 av_log(avctx,
AV_LOG_ERROR,
"Unable to register downmix ancillary buffer in the decoder\n");
325#if FDKDEC_VER_AT_LEAST(2, 5)
333 if (
s->drc_boost != -1) {
334 if (aacDecoder_SetParam(
s->handle, AAC_DRC_BOOST_FACTOR,
s->drc_boost) != AAC_DEC_OK) {
340 if (
s->drc_cut != -1) {
341 if (aacDecoder_SetParam(
s->handle, AAC_DRC_ATTENUATION_FACTOR,
s->drc_cut) != AAC_DEC_OK) {
347 if (
s->drc_level != -1) {
354 if (aacDecoder_SetParam(
s->handle, AAC_DRC_REFERENCE_LEVEL,
s->drc_level) != AAC_DEC_OK) {
360 if (
s->drc_heavy != -1) {
361 if (aacDecoder_SetParam(
s->handle, AAC_DRC_HEAVY_COMPRESSION,
s->drc_heavy) != AAC_DEC_OK) {
367#if FDKDEC_VER_AT_LEAST(2, 5)
369 if (aacDecoder_SetParam(
s->handle, AAC_PCM_LIMITER_ENABLE,
s->level_limit) != AAC_DEC_OK) {
370 av_log(avctx,
AV_LOG_ERROR,
"Unable to set in signal level limiting in the decoder\n");
375#if FDKDEC_VER_AT_LEAST(3, 0)
376 if (
s->drc_effect != -1) {
377 if (aacDecoder_SetParam(
s->handle, AAC_UNIDRC_SET_EFFECT,
s->drc_effect) != AAC_DEC_OK) {
384#if FDKDEC_VER_AT_LEAST(3, 1)
385 if (
s->album_mode != -1) {
386 if (aacDecoder_SetParam(
s->handle, AAC_UNIDRC_ALBUM_MODE,
s->album_mode) != AAC_DEC_OK) {
396 s->decoder_buffer =
av_malloc(
s->decoder_buffer_size);
397 if (!
s->decoder_buffer)
404 int *got_frame_ptr,
AVPacket *avpkt)
408 AAC_DECODER_ERROR err;
409 UINT valid = avpkt->
size;
411 int input_offset = 0;
414 err = aacDecoder_Fill(
s->handle, &avpkt->
data, &avpkt->
size, &valid);
415 if (err != AAC_DEC_OK) {
420#if FDKDEC_VER_AT_LEAST(2, 5)
422 if (
s->flush_samples > 0) {
423 flags |= AACDEC_FLUSH;
432 err = aacDecoder_DecodeFrame(
s->handle, (INT_PCM *)
s->decoder_buffer,
433 s->decoder_buffer_size /
sizeof(INT_PCM),
435 if (err == AAC_DEC_NOT_ENOUGH_BITS) {
436 ret = avpkt->
size - valid;
439 if (err != AAC_DEC_OK) {
441 "aacDecoder_DecodeFrame() failed: %x\n", err);
450#if FDKDEC_VER_AT_LEAST(2, 5)
451 if (
flags & AACDEC_FLUSH) {
456 frame->nb_samples,
s->flush_samples);
457 s->flush_samples -=
frame->nb_samples;
462 if (
s->delay_samples) {
463 int drop_samples =
FFMIN(
s->delay_samples,
frame->nb_samples);
465 drop_samples,
s->delay_samples);
466 s->delay_samples -= drop_samples;
467 frame->nb_samples -= drop_samples;
469 if (
frame->nb_samples <= 0)
478 memcpy(
frame->extended_data[0],
s->decoder_buffer + input_offset,
483 ret = avpkt->
size - valid;
492 AAC_DECODER_ERROR err;
497 if ((err = aacDecoder_SetParam(
s->handle,
498 AAC_TPDEC_CLEAR_BUFFER, 1)) != AAC_DEC_OK)
503 .p.name =
"libfdk_aac",
513#if FDKDEC_VER_AT_LEAST(2, 5)
519 .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_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_FRONT_CENTER
#define AV_CH_LOW_FREQUENCY
#define AV_CH_TOP_FRONT_RIGHT
#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