Coverage Report

Created: 2026-10-28 08:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/libfdk-aacdec.c
Line
Count
Source
1
/*
2
 * AAC decoder wrapper
3
 * Copyright (c) 2012 Martin Storsjo
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * Permission to use, copy, modify, and/or distribute this software for any
8
 * purpose with or without fee is hereby granted, provided that the above
9
 * copyright notice and this permission notice appear in all copies.
10
 *
11
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
 */
19
20
#include <fdk-aac/aacdecoder_lib.h>
21
22
#include "libavutil/channel_layout.h"
23
#include "libavutil/common.h"
24
#include "libavutil/intreadwrite.h"
25
#include "libavutil/mem.h"
26
#include "libavutil/opt.h"
27
#include "avcodec.h"
28
#include "codec_internal.h"
29
#include "decode.h"
30
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))
35
#else
36
#define FDKDEC_VER_AT_LEAST(vl0, vl1) 0
37
#endif
38
39
#if !FDKDEC_VER_AT_LEAST(2, 5) // < 2.5.10
40
#define AAC_PCM_MAX_OUTPUT_CHANNELS AAC_PCM_OUTPUT_CHANNELS
41
#endif
42
43
enum ConcealMethod {
44
    CONCEAL_METHOD_SPECTRAL_MUTING      =  0,
45
    CONCEAL_METHOD_NOISE_SUBSTITUTION   =  1,
46
    CONCEAL_METHOD_ENERGY_INTERPOLATION =  2,
47
    CONCEAL_METHOD_NB,
48
};
49
50
typedef struct FDKAACDecContext {
51
    const AVClass *class;
52
    HANDLE_AACDECODER handle;
53
    uint8_t *decoder_buffer;
54
    int decoder_buffer_size;
55
    uint8_t *anc_buffer;
56
    int conceal_method;
57
    int drc_level;
58
    int drc_boost;
59
    int drc_heavy;
60
    int drc_effect;
61
    int drc_cut;
62
    int album_mode;
63
    int level_limit;
64
#if FDKDEC_VER_AT_LEAST(2, 5) // 2.5.10
65
    int output_delay_set;
66
    int flush_samples;
67
    int skip_samples;
68
    int output_delay;
69
    int discard_padding;
70
    int64_t last_pts;
71
    int64_t last_dts;
72
#endif
73
    AVChannelLayout downmix_layout;
74
} FDKAACDecContext;
75
76
77
0
#define DMX_ANC_BUFFSIZE       128
78
11.9k
#define DECODER_MAX_CHANNELS     8
79
11.9k
#define DECODER_BUFFSIZE      2048 * sizeof(INT_PCM)
80
81
#define OFFSET(x) offsetof(FDKAACDecContext, x)
82
#define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
83
static const AVOption fdk_aac_dec_options[] = {
84
    { "conceal", "Error concealment method", OFFSET(conceal_method), AV_OPT_TYPE_INT, { .i64 = CONCEAL_METHOD_NOISE_SUBSTITUTION }, CONCEAL_METHOD_SPECTRAL_MUTING, CONCEAL_METHOD_NB - 1, AD, .unit = "conceal" },
85
    { "spectral", "Spectral muting",      0, AV_OPT_TYPE_CONST, { .i64 = CONCEAL_METHOD_SPECTRAL_MUTING },      INT_MIN, INT_MAX, AD, .unit = "conceal" },
86
    { "noise",    "Noise Substitution",   0, AV_OPT_TYPE_CONST, { .i64 = CONCEAL_METHOD_NOISE_SUBSTITUTION },   INT_MIN, INT_MAX, AD, .unit = "conceal" },
87
    { "energy",   "Energy Interpolation", 0, AV_OPT_TYPE_CONST, { .i64 = CONCEAL_METHOD_ENERGY_INTERPOLATION }, INT_MIN, INT_MAX, AD, .unit = "conceal" },
88
    { "drc_boost", "Dynamic Range Control: boost, where [0] is none and [127] is max boost",
89
                     OFFSET(drc_boost),      AV_OPT_TYPE_INT,   { .i64 = -1 }, -1, 127, AD, .unit = NULL    },
90
    { "drc_cut",   "Dynamic Range Control: attenuation factor, where [0] is none and [127] is max compression",
91
                     OFFSET(drc_cut),        AV_OPT_TYPE_INT,   { .i64 = -1 }, -1, 127, AD, .unit = NULL    },
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",
93
                     OFFSET(drc_level),      AV_OPT_TYPE_INT,   { .i64 = -1},  -2, 127, AD, .unit = NULL    },
94
    { "drc_heavy", "Dynamic Range Control: heavy compression, where [1] is on (RF mode) and [0] is off",
95
                     OFFSET(drc_heavy),      AV_OPT_TYPE_INT,   { .i64 = -1},  -1, 1,   AD, .unit = NULL    },
96
#if FDKDEC_VER_AT_LEAST(2, 5) // 2.5.10
97
    { "level_limit", "Signal level limiting",
98
                     OFFSET(level_limit),    AV_OPT_TYPE_BOOL,  { .i64 = -1 }, -1, 1, AD },
99
#endif
100
#if FDKDEC_VER_AT_LEAST(3, 0) // 3.0.0
101
    { "drc_effect","Dynamic Range Control: effect type, where e.g. [0] is none and [6] is general",
102
                     OFFSET(drc_effect),     AV_OPT_TYPE_INT,   { .i64 = -1},  -1, 8,   AD, .unit = NULL    },
103
#endif
104
#if FDKDEC_VER_AT_LEAST(3, 1) // 3.1.0
105
    { "album_mode","Dynamic Range Control: album mode, where [0] is off and [1] is on",
106
                     OFFSET(album_mode),     AV_OPT_TYPE_INT,   { .i64 = -1},  -1, 1,   AD, .unit = NULL    },
107
#endif
108
    { "downmix", "Request a specific channel layout from the decoder", OFFSET(downmix_layout), AV_OPT_TYPE_CHLAYOUT, {.str = NULL}, .flags = AD },
109
    { NULL }
110
};
111
112
static const AVClass fdk_aac_dec_class = {
113
    .class_name = "libfdk-aac decoder",
114
    .item_name  = av_default_item_name,
115
    .option     = fdk_aac_dec_options,
116
    .version    = LIBAVUTIL_VERSION_INT,
117
};
118
119
static int get_stream_info(AVCodecContext *avctx, AVFrame *frame)
120
444k
{
121
444k
    FDKAACDecContext *s   = avctx->priv_data;
122
444k
    CStreamInfo *info     = aacDecoder_GetStreamInfo(s->handle);
123
444k
    int channel_counts[0x24] = { 0 };
124
444k
    int i, ch_error       = 0;
125
444k
    uint64_t ch_layout    = 0;
126
127
444k
    if (!info) {
128
0
        av_log(avctx, AV_LOG_ERROR, "Unable to get stream info\n");
129
0
        return AVERROR_UNKNOWN;
130
0
    }
131
132
444k
    if (info->sampleRate <= 0) {
133
0
        av_log(avctx, AV_LOG_ERROR, "Stream info not initialized\n");
134
0
        return AVERROR_UNKNOWN;
135
0
    }
136
444k
    avctx->sample_rate = info->sampleRate;
137
444k
    avctx->frame_size  = info->frameSize;
138
444k
    avctx->profile     = info->aot - 1;
139
140
444k
    frame->flags |= AV_FRAME_FLAG_KEY * !!(info->flags & AC_INDEP);
141
#if FDKDEC_VER_AT_LEAST(2, 5) // 2.5.10
142
444k
    if (!s->output_delay_set && info->outputDelay) {
143
        // Set this only once.
144
225k
        s->flush_samples    = info->outputDelay;
145
225k
        s->skip_samples     = info->outputDelay;
146
225k
        s->output_delay     = info->outputDelay;
147
225k
        s->last_pts         = AV_NOPTS_VALUE;
148
225k
        s->last_dts         = AV_NOPTS_VALUE;
149
225k
        s->output_delay_set = 1;
150
225k
    }
151
444k
#endif
152
153
1.10M
    for (i = 0; i < info->numChannels; i++) {
154
659k
        AUDIO_CHANNEL_TYPE ctype = info->pChannelType[i];
155
659k
        if (ctype <= ACT_NONE || ctype >= FF_ARRAY_ELEMS(channel_counts)) {
156
0
            av_log(avctx, AV_LOG_WARNING, "unknown channel type\n");
157
0
            break;
158
0
        }
159
659k
        channel_counts[ctype]++;
160
659k
    }
161
444k
    av_log(avctx, AV_LOG_DEBUG,
162
444k
           "%d channels - front:%d side:%d back:%d lfe:%d top:%d bottom %d\n",
163
444k
           info->numChannels,
164
444k
           channel_counts[ACT_FRONT], channel_counts[ACT_SIDE],
165
444k
           channel_counts[ACT_BACK],  channel_counts[ACT_LFE],
166
444k
           channel_counts[ACT_FRONT_TOP] + channel_counts[ACT_SIDE_TOP] +
167
444k
           channel_counts[ACT_BACK_TOP]  + channel_counts[ACT_TOP],
168
444k
           channel_counts[ACT_FRONT_BOTTOM] + channel_counts[ACT_SIDE_BOTTOM] +
169
444k
           channel_counts[ACT_BACK_BOTTOM]  + channel_counts[ACT_BOTTOM]);
170
171
444k
    switch (channel_counts[ACT_FRONT]) {
172
1.42k
    case 5:
173
1.42k
    case 4:
174
1.42k
        ch_layout |= AV_CH_FRONT_LEFT_OF_CENTER |
175
1.42k
                     AV_CH_FRONT_RIGHT_OF_CENTER;
176
1.42k
        av_fallthrough;
177
15.8k
    case 3:
178
167k
    case 2:
179
167k
        ch_layout |= AV_CH_LAYOUT_STEREO;
180
167k
        av_fallthrough;
181
442k
    case 1:
182
442k
        if (channel_counts[ACT_FRONT] & 1)
183
291k
            ch_layout |= AV_CH_FRONT_CENTER;
184
442k
        break;
185
1.62k
    default:
186
1.62k
        av_log(avctx, AV_LOG_WARNING,
187
1.62k
               "unsupported number of front channels: %d\n",
188
1.62k
               channel_counts[ACT_FRONT]);
189
1.62k
        ch_error = 1;
190
1.62k
        break;
191
444k
    }
192
193
444k
    if (channel_counts[ACT_SIDE] > 0) {
194
1.12k
        if (channel_counts[ACT_SIDE] == 2) {
195
357
            ch_layout |= AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT;
196
766
        } else {
197
766
            av_log(avctx, AV_LOG_WARNING,
198
766
                   "unsupported number of side channels: %d\n",
199
766
                   channel_counts[ACT_SIDE]);
200
766
            ch_error = 1;
201
766
        }
202
1.12k
    }
203
444k
    if (channel_counts[ACT_BACK] > 0) {
204
9.14k
        switch (channel_counts[ACT_BACK]) {
205
3.30k
        case 4:
206
3.30k
            ch_layout |= AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT;
207
3.30k
            av_fallthrough;
208
3.75k
        case 3:
209
6.38k
        case 2:
210
6.38k
            ch_layout |= AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT;
211
6.38k
            av_fallthrough;
212
9.14k
        case 1:
213
9.14k
            if (channel_counts[ACT_BACK] & 1)
214
3.21k
                ch_layout |= AV_CH_BACK_CENTER;
215
9.14k
            break;
216
0
        default:
217
0
            av_log(avctx, AV_LOG_WARNING,
218
0
                   "unsupported number of back channels: %d\n",
219
0
                   channel_counts[ACT_BACK]);
220
0
            ch_error = 1;
221
0
            break;
222
9.14k
        }
223
9.14k
    }
224
444k
    if (channel_counts[ACT_LFE] > 0) {
225
5.73k
        if (channel_counts[ACT_LFE] == 1) {
226
5.72k
            ch_layout |= AV_CH_LOW_FREQUENCY;
227
5.72k
        } else {
228
4
            av_log(avctx, AV_LOG_WARNING,
229
4
                   "unsupported number of LFE channels: %d\n",
230
4
                   channel_counts[ACT_LFE]);
231
4
            ch_error = 1;
232
4
        }
233
5.73k
    }
234
444k
    if (channel_counts[ACT_FRONT_TOP] > 0) {
235
90
        switch (channel_counts[ACT_FRONT_TOP]) {
236
0
        case 3:
237
90
        case 2:
238
90
            ch_layout |= AV_CH_TOP_FRONT_LEFT | AV_CH_TOP_FRONT_RIGHT;
239
90
            av_fallthrough;
240
90
        case 1:
241
90
            if (channel_counts[ACT_FRONT_TOP] & 1)
242
0
                ch_layout |= AV_CH_TOP_FRONT_CENTER;
243
90
            break;
244
0
        default:
245
0
            av_log(avctx, AV_LOG_WARNING,
246
0
                   "unsupported number of top front channels: %d\n",
247
0
                   channel_counts[ACT_FRONT_TOP]);
248
0
            ch_error = 1;
249
0
            break;
250
90
        }
251
90
    }
252
444k
    if (channel_counts[ACT_BACK_TOP] > 0) {
253
0
        switch (channel_counts[ACT_BACK_TOP]) {
254
0
        case 3:
255
0
        case 2:
256
0
            ch_layout |= AV_CH_TOP_BACK_LEFT | AV_CH_TOP_BACK_RIGHT;
257
0
            av_fallthrough;
258
0
        case 1:
259
0
            if (channel_counts[ACT_BACK_TOP] & 1)
260
0
                ch_layout |= AV_CH_TOP_BACK_CENTER;
261
0
            break;
262
0
        default:
263
0
            av_log(avctx, AV_LOG_WARNING,
264
0
                   "unsupported number of top back channels: %d\n",
265
0
                   channel_counts[ACT_BACK_TOP]);
266
0
            ch_error = 1;
267
0
            break;
268
0
        }
269
0
    }
270
444k
    if (channel_counts[ACT_SIDE_TOP] > 0) {
271
0
        switch (channel_counts[ACT_SIDE_TOP]) {
272
0
        case 3:
273
0
        case 2:
274
0
            ch_layout |= AV_CH_TOP_SIDE_LEFT | AV_CH_TOP_SIDE_RIGHT;
275
0
            av_fallthrough;
276
0
        case 1:
277
0
            if (channel_counts[ACT_SIDE_TOP] & 1)
278
0
                ch_layout |= AV_CH_TOP_CENTER;
279
0
            break;
280
0
        default:
281
0
            av_log(avctx, AV_LOG_WARNING,
282
0
                   "unsupported number of top side channels: %d\n",
283
0
                   channel_counts[ACT_SIDE_TOP]);
284
0
            ch_error = 1;
285
0
            break;
286
0
        }
287
0
    }
288
444k
    if (channel_counts[ACT_FRONT_BOTTOM] > 0) {
289
143
        switch (channel_counts[ACT_FRONT_BOTTOM]) {
290
0
        case 3:
291
0
        case 2:
292
0
            ch_layout |= AV_CH_BOTTOM_FRONT_LEFT | AV_CH_BOTTOM_FRONT_RIGHT;
293
0
            av_fallthrough;
294
143
        case 1:
295
143
            if (channel_counts[ACT_FRONT_BOTTOM] & 1)
296
143
                ch_layout |= AV_CH_BOTTOM_FRONT_CENTER;
297
143
            break;
298
0
        default:
299
0
            av_log(avctx, AV_LOG_WARNING,
300
0
                   "unsupported number of bottom front channels: %d\n",
301
0
                   channel_counts[ACT_FRONT_BOTTOM]);
302
0
            ch_error = 1;
303
0
            break;
304
143
        }
305
143
    }
306
307
444k
    av_channel_layout_uninit(&avctx->ch_layout);
308
444k
    av_channel_layout_from_mask(&avctx->ch_layout, ch_layout);
309
444k
    if (!ch_error && avctx->ch_layout.nb_channels != info->numChannels) {
310
0
        av_log(avctx, AV_LOG_WARNING, "unsupported channel configuration\n");
311
0
        ch_error = 1;
312
0
    }
313
444k
    if (ch_error)
314
1.62k
        avctx->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
315
316
444k
    return 0;
317
444k
}
318
319
static av_cold int fdk_aac_decode_close(AVCodecContext *avctx)
320
12.7k
{
321
12.7k
    FDKAACDecContext *s = avctx->priv_data;
322
323
12.7k
    if (s->handle)
324
12.7k
        aacDecoder_Close(s->handle);
325
12.7k
    av_freep(&s->decoder_buffer);
326
12.7k
    av_freep(&s->anc_buffer);
327
328
12.7k
    return 0;
329
12.7k
}
330
331
static av_cold int fdk_aac_decode_init(AVCodecContext *avctx)
332
12.7k
{
333
12.7k
    FDKAACDecContext *s = avctx->priv_data;
334
12.7k
    AAC_DECODER_ERROR err;
335
336
12.7k
    s->handle = aacDecoder_Open(avctx->extradata_size ? TT_MP4_RAW : TT_MP4_ADTS, 1);
337
12.7k
    if (!s->handle) {
338
0
        av_log(avctx, AV_LOG_ERROR, "Error opening decoder\n");
339
0
        return AVERROR_UNKNOWN;
340
0
    }
341
342
12.7k
    if (avctx->extradata_size) {
343
7.77k
        if ((err = aacDecoder_ConfigRaw(s->handle, &avctx->extradata,
344
7.77k
                                        &avctx->extradata_size)) != AAC_DEC_OK) {
345
811
            av_log(avctx, AV_LOG_ERROR, "Unable to set extradata\n");
346
811
            return AVERROR_INVALIDDATA;
347
811
        }
348
7.77k
    }
349
350
11.9k
    if ((err = aacDecoder_SetParam(s->handle, AAC_CONCEAL_METHOD,
351
11.9k
                                   s->conceal_method)) != AAC_DEC_OK) {
352
0
        av_log(avctx, AV_LOG_ERROR, "Unable to set error concealment method\n");
353
0
        return AVERROR_UNKNOWN;
354
0
    }
355
356
11.9k
    if (s->downmix_layout.nb_channels > 0 &&
357
0
        s->downmix_layout.order == AV_CHANNEL_ORDER_NATIVE) {
358
0
        int downmix_channels = -1;
359
360
0
        switch (s->downmix_layout.u.mask) {
361
0
        case AV_CH_LAYOUT_STEREO:
362
0
        case AV_CH_LAYOUT_STEREO_DOWNMIX:
363
0
            downmix_channels = 2;
364
0
            break;
365
0
        case AV_CH_LAYOUT_MONO:
366
0
            downmix_channels = 1;
367
0
            break;
368
0
        default:
369
0
            av_log(avctx, AV_LOG_WARNING, "Invalid downmix option\n");
370
0
            break;
371
0
        }
372
373
0
        if (downmix_channels != -1) {
374
0
            if (aacDecoder_SetParam(s->handle, AAC_PCM_MAX_OUTPUT_CHANNELS,
375
0
                                    downmix_channels) != AAC_DEC_OK) {
376
0
               av_log(avctx, AV_LOG_WARNING, "Unable to set output channels in the decoder\n");
377
0
            } else {
378
0
               s->anc_buffer = av_malloc(DMX_ANC_BUFFSIZE);
379
0
               if (!s->anc_buffer) {
380
0
                   av_log(avctx, AV_LOG_ERROR, "Unable to allocate ancillary buffer for the decoder\n");
381
0
                   return AVERROR(ENOMEM);
382
0
               }
383
0
               if (aacDecoder_AncDataInit(s->handle, s->anc_buffer, DMX_ANC_BUFFSIZE)) {
384
0
                   av_log(avctx, AV_LOG_ERROR, "Unable to register downmix ancillary buffer in the decoder\n");
385
0
                   return AVERROR_UNKNOWN;
386
0
               }
387
0
            }
388
0
        }
389
0
#if FDKDEC_VER_AT_LEAST(2, 5)
390
11.9k
    } else {
391
        // AAC_PCM_MAX_OUTPUT_CHANNELS == 0 means outputting all the coded channels
392
11.9k
        if (aacDecoder_SetParam(s->handle, AAC_PCM_MAX_OUTPUT_CHANNELS, 0) != AAC_DEC_OK)
393
0
           av_log(avctx, AV_LOG_WARNING, "Unable to set output channels in the decoder\n");
394
11.9k
#endif
395
11.9k
    }
396
397
11.9k
    if (s->drc_boost != -1) {
398
0
        if (aacDecoder_SetParam(s->handle, AAC_DRC_BOOST_FACTOR, s->drc_boost) != AAC_DEC_OK) {
399
0
            av_log(avctx, AV_LOG_ERROR, "Unable to set DRC boost factor in the decoder\n");
400
0
            return AVERROR_UNKNOWN;
401
0
        }
402
0
    }
403
404
11.9k
    if (s->drc_cut != -1) {
405
0
        if (aacDecoder_SetParam(s->handle, AAC_DRC_ATTENUATION_FACTOR, s->drc_cut) != AAC_DEC_OK) {
406
0
            av_log(avctx, AV_LOG_ERROR, "Unable to set DRC attenuation factor in the decoder\n");
407
0
            return AVERROR_UNKNOWN;
408
0
        }
409
0
    }
410
411
11.9k
    if (s->drc_level != -1) {
412
        // This option defaults to -1, i.e. not calling
413
        // aacDecoder_SetParam(AAC_DRC_REFERENCE_LEVEL) at all, which defaults
414
        // to the level from DRC metadata, if available. The user can set
415
        // -drc_level -2, which calls aacDecoder_SetParam(
416
        // AAC_DRC_REFERENCE_LEVEL) with a negative value, which then
417
        // explicitly disables the feature.
418
0
        if (aacDecoder_SetParam(s->handle, AAC_DRC_REFERENCE_LEVEL, s->drc_level) != AAC_DEC_OK) {
419
0
            av_log(avctx, AV_LOG_ERROR, "Unable to set DRC reference level in the decoder\n");
420
0
            return AVERROR_UNKNOWN;
421
0
        }
422
0
    }
423
424
11.9k
    if (s->drc_heavy != -1) {
425
0
        if (aacDecoder_SetParam(s->handle, AAC_DRC_HEAVY_COMPRESSION, s->drc_heavy) != AAC_DEC_OK) {
426
0
            av_log(avctx, AV_LOG_ERROR, "Unable to set DRC heavy compression in the decoder\n");
427
0
            return AVERROR_UNKNOWN;
428
0
        }
429
0
    }
430
431
#if FDKDEC_VER_AT_LEAST(2, 5) // 2.5.10
432
    // Setting this parameter to -1 enables the auto behaviour in the library.
433
11.9k
    if (aacDecoder_SetParam(s->handle, AAC_PCM_LIMITER_ENABLE, s->level_limit) != AAC_DEC_OK) {
434
0
        av_log(avctx, AV_LOG_ERROR, "Unable to set in signal level limiting in the decoder\n");
435
0
        return AVERROR_UNKNOWN;
436
0
    }
437
11.9k
#endif
438
439
#if FDKDEC_VER_AT_LEAST(3, 0) // 3.0.0
440
11.9k
    if (s->drc_effect != -1) {
441
0
        if (aacDecoder_SetParam(s->handle, AAC_UNIDRC_SET_EFFECT, s->drc_effect) != AAC_DEC_OK) {
442
0
            av_log(avctx, AV_LOG_ERROR, "Unable to set DRC effect type in the decoder\n");
443
0
            return AVERROR_UNKNOWN;
444
0
        }
445
0
    }
446
11.9k
#endif
447
448
#if FDKDEC_VER_AT_LEAST(3, 1) // 3.1.0
449
11.9k
    if (s->album_mode != -1) {
450
0
        if (aacDecoder_SetParam(s->handle, AAC_UNIDRC_ALBUM_MODE, s->album_mode) != AAC_DEC_OK) {
451
0
            av_log(avctx, AV_LOG_ERROR, "Unable to set album mode in the decoder\n");
452
0
            return AVERROR_UNKNOWN;
453
0
        }
454
0
    }
455
11.9k
#endif
456
457
11.9k
    avctx->sample_fmt = AV_SAMPLE_FMT_S16;
458
459
11.9k
    s->decoder_buffer_size = DECODER_BUFFSIZE * DECODER_MAX_CHANNELS;
460
11.9k
    s->decoder_buffer = av_malloc(s->decoder_buffer_size);
461
11.9k
    if (!s->decoder_buffer)
462
0
        return AVERROR(ENOMEM);
463
464
11.9k
    return 0;
465
11.9k
}
466
467
static int fdk_aac_decode_frame(AVCodecContext *avctx, AVFrame *frame,
468
                                int *got_frame_ptr, AVPacket *avpkt)
469
4.78M
{
470
4.78M
    FDKAACDecContext *s = avctx->priv_data;
471
4.78M
    int ret;
472
4.78M
    AAC_DECODER_ERROR err;
473
4.78M
    UINT valid = avpkt->size;
474
4.78M
    UINT flags = 0;
475
476
4.78M
    if (avpkt->size) {
477
4.76M
        err = aacDecoder_Fill(s->handle, &avpkt->data, &avpkt->size, &valid);
478
4.76M
        if (err != AAC_DEC_OK) {
479
36
            av_log(avctx, AV_LOG_ERROR, "aacDecoder_Fill() failed: %x\n", err);
480
36
            return AVERROR_INVALIDDATA;
481
36
        }
482
4.76M
    } else {
483
#if FDKDEC_VER_AT_LEAST(2, 5) // 2.5.10
484
        /* Handle decoder draining */
485
15.1k
        if (s->flush_samples > 0) {
486
4.58k
            flags |= AACDEC_FLUSH;
487
10.5k
        } else {
488
10.5k
            return 0;
489
10.5k
        }
490
#else
491
        return 0;
492
#endif
493
15.1k
    }
494
495
4.77M
    err = aacDecoder_DecodeFrame(s->handle, (INT_PCM *) s->decoder_buffer,
496
4.77M
                                 s->decoder_buffer_size / sizeof(INT_PCM),
497
4.77M
                                 flags);
498
4.77M
    if (err == AAC_DEC_NOT_ENOUGH_BITS) {
499
485k
        ret = avpkt->size - valid;
500
485k
        goto end;
501
485k
    }
502
4.28M
    if (err != AAC_DEC_OK) {
503
3.84M
        av_log(avctx, AV_LOG_ERROR,
504
3.84M
               "aacDecoder_DecodeFrame() failed: %x\n", err);
505
3.84M
        ret = AVERROR_UNKNOWN;
506
3.84M
        goto end;
507
3.84M
    }
508
509
444k
    if ((ret = get_stream_info(avctx, frame)) < 0)
510
0
        goto end;
511
444k
    frame->nb_samples = avctx->frame_size;
512
513
444k
    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
514
766
        goto end;
515
516
#if FDKDEC_VER_AT_LEAST(2, 5) // 2.5.10
517
443k
    if (flags & AACDEC_FLUSH) {
518
3.23k
        if (s->last_pts != AV_NOPTS_VALUE)
519
0
            frame->pts = av_sat_add64(s->last_pts,
520
0
                                      av_rescale_q(frame->nb_samples, avctx->time_base,
521
0
                                                   (AVRational){ 1, avctx->sample_rate }));
522
3.23k
        if (s->last_dts != AV_NOPTS_VALUE)
523
0
            frame->pkt_dts = av_sat_add64(s->last_dts,
524
0
                                          av_rescale_q(frame->nb_samples, avctx->time_base,
525
0
                                                       (AVRational){ 1, avctx->sample_rate }));
526
527
        // Only return the right amount of samples at the end; if calling the
528
        // decoder with AACDEC_FLUSH, it will keep returning frames indefinitely.
529
3.23k
        frame->nb_samples = FFMIN(s->flush_samples, frame->nb_samples);
530
3.23k
        av_log(s, AV_LOG_DEBUG, "Returning %d/%d delayed samples.\n",
531
3.23k
                                frame->nb_samples, s->flush_samples);
532
3.23k
        if (s->skip_samples || s->discard_padding) {
533
1
            AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES);
534
1
            if (!sd) {
535
1
                sd = av_frame_new_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES, 10);
536
1
                if (sd)
537
1
                    memset(sd->data, 0, 10);
538
1
            }
539
1
            if (sd && sd->size >= 10) {
540
1
                AV_WL32(sd->data,     s->skip_samples);
541
1
                AV_WL32(sd->data + 4, s->discard_padding);
542
1
            }
543
1
            s->skip_samples = 0;
544
1
            s->discard_padding = 0;
545
1
        }
546
3.23k
        s->flush_samples -= frame->nb_samples;
547
3.23k
        s->last_pts = frame->pts;
548
3.23k
        s->last_dts = frame->pkt_dts;
549
440k
    } else {
550
440k
        AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES);
551
440k
        if (!sd && s->skip_samples) {
552
224k
            sd = av_frame_new_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES, 10);
553
224k
            if (sd)
554
224k
                memset(sd->data, 0, 10);
555
224k
        }
556
440k
        if (sd && sd->size >= 10) {
557
224k
            int skip_samples = AV_RL32(sd->data);
558
224k
            s->discard_padding = AV_RL32(sd->data + 4);
559
224k
            if (s->discard_padding >= s->output_delay) {
560
0
                AV_WL32(sd->data + 4, s->discard_padding - s->output_delay);
561
0
                s->discard_padding = s->output_delay;
562
0
            } else
563
224k
                AV_WL32(sd->data + 4, 0);
564
224k
            AV_WL32(sd->data, skip_samples + s->skip_samples);
565
224k
            s->skip_samples = 0;
566
224k
        }
567
440k
        if (frame->pts != AV_NOPTS_VALUE && s->output_delay)
568
0
            frame->pts = av_sat_sub64(frame->pts,
569
0
                                      av_rescale_q(s->output_delay,
570
0
                                                   (AVRational){ 1, avctx->sample_rate },
571
0
                                                   avctx->time_base));
572
440k
        if (frame->pkt_dts != AV_NOPTS_VALUE && s->output_delay)
573
0
            frame->pkt_dts = av_sat_sub64(frame->pkt_dts,
574
0
                                          av_rescale_q(s->output_delay,
575
0
                                                       (AVRational){ 1, avctx->sample_rate },
576
0
                                                       avctx->time_base));
577
440k
        s->last_pts = frame->pts;
578
440k
        s->last_dts = frame->pkt_dts;
579
440k
    }
580
443k
#endif
581
582
443k
    memcpy(frame->extended_data[0], s->decoder_buffer,
583
443k
           avctx->ch_layout.nb_channels * frame->nb_samples *
584
443k
           av_get_bytes_per_sample(avctx->sample_fmt));
585
586
443k
    *got_frame_ptr = 1;
587
443k
    ret = avpkt->size - valid;
588
589
4.77M
end:
590
4.77M
    return ret;
591
443k
}
592
593
static av_cold void fdk_aac_decode_flush(AVCodecContext *avctx)
594
1.49M
{
595
1.49M
    FDKAACDecContext *s = avctx->priv_data;
596
1.49M
    AAC_DECODER_ERROR err;
597
598
1.49M
    if (!s->handle)
599
0
        return;
600
601
1.49M
    if ((err = aacDecoder_SetParam(s->handle,
602
1.49M
                                   AAC_TPDEC_CLEAR_BUFFER, 1)) != AAC_DEC_OK)
603
0
        av_log(avctx, AV_LOG_WARNING, "failed to clear buffer when flushing\n");
604
605
#if FDKDEC_VER_AT_LEAST(2, 5) // 2.5.10
606
1.49M
    s->skip_samples = 0;
607
1.49M
    s->discard_padding = 0;
608
1.49M
    s->flush_samples = 0;
609
1.49M
    s->output_delay = 0;
610
1.49M
    s->last_pts = AV_NOPTS_VALUE;
611
1.49M
    s->last_dts = AV_NOPTS_VALUE;
612
1.49M
    s->output_delay_set = 0;
613
614
    // Call aacDecoder_DecodeFrame() with flush and clear history flags as the
615
    // above aacDecoder_SetParam() call is seemingly not sufficient.
616
    // Ignore the return code given it will not be AAC_DEC_OK if nothing is
617
    // buffered internally (e.g. trying to flush the decoder before passing a
618
    // single packet to it).
619
1.49M
    aacDecoder_DecodeFrame(s->handle, (INT_PCM *) s->decoder_buffer,
620
1.49M
                           s->decoder_buffer_size / sizeof(INT_PCM),
621
1.49M
                           AACDEC_FLUSH | AACDEC_CLRHIST);
622
1.49M
#endif
623
1.49M
}
624
625
const FFCodec ff_libfdk_aac_decoder = {
626
    .p.name         = "libfdk_aac",
627
    CODEC_LONG_NAME("Fraunhofer FDK AAC"),
628
    .p.type         = AVMEDIA_TYPE_AUDIO,
629
    .p.id           = AV_CODEC_ID_AAC,
630
    .priv_data_size = sizeof(FDKAACDecContext),
631
    .init           = fdk_aac_decode_init,
632
    FF_CODEC_DECODE_CB(fdk_aac_decode_frame),
633
    .close          = fdk_aac_decode_close,
634
    .flush          = fdk_aac_decode_flush,
635
    .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF
636
#if FDKDEC_VER_AT_LEAST(2, 5) // 2.5.10
637
                      | AV_CODEC_CAP_DELAY
638
#endif
639
    ,
640
    .p.priv_class   = &fdk_aac_dec_class,
641
    .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
642
    .p.wrapper_name = "libfdk",
643
};