FFmpeg
Loading...
Searching...
No Matches
libfdk-aacdec.c
Go to the documentation of this file.
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
23#include "libavutil/common.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
49
50typedef struct FDKAACDecContext {
51 const AVClass *class;
52 HANDLE_AACDECODER handle;
55 uint8_t *anc_buffer;
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;
71 int64_t last_dts;
72#endif
75
76
77#define DMX_ANC_BUFFSIZE 128
78#define DECODER_MAX_CHANNELS 8
79#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
83static 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
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
120{
121 FDKAACDecContext *s = avctx->priv_data;
122 CStreamInfo *info = aacDecoder_GetStreamInfo(s->handle);
123 int channel_counts[0x24] = { 0 };
124 int i, ch_error = 0;
125 uint64_t ch_layout = 0;
126
127 if (!info) {
128 av_log(avctx, AV_LOG_ERROR, "Unable to get stream info\n");
129 return AVERROR_UNKNOWN;
130 }
131
132 if (info->sampleRate <= 0) {
133 av_log(avctx, AV_LOG_ERROR, "Stream info not initialized\n");
134 return AVERROR_UNKNOWN;
135 }
136 avctx->sample_rate = info->sampleRate;
137 avctx->frame_size = info->frameSize;
138 avctx->profile = info->aot - 1;
139
140 frame->flags |= AV_FRAME_FLAG_KEY * !!(info->flags & AC_INDEP);
141#if FDKDEC_VER_AT_LEAST(2, 5) // 2.5.10
142 if (!s->output_delay_set && info->outputDelay) {
143 // Set this only once.
144 s->flush_samples = info->outputDelay;
145 s->skip_samples = info->outputDelay;
146 s->output_delay = info->outputDelay;
147 s->last_pts = AV_NOPTS_VALUE;
148 s->last_dts = AV_NOPTS_VALUE;
149 s->output_delay_set = 1;
150 }
151#endif
152
153 for (i = 0; i < info->numChannels; i++) {
154 AUDIO_CHANNEL_TYPE ctype = info->pChannelType[i];
155 if (ctype <= ACT_NONE || ctype >= FF_ARRAY_ELEMS(channel_counts)) {
156 av_log(avctx, AV_LOG_WARNING, "unknown channel type\n");
157 break;
158 }
159 channel_counts[ctype]++;
160 }
161 av_log(avctx, AV_LOG_DEBUG,
162 "%d channels - front:%d side:%d back:%d lfe:%d top:%d bottom %d\n",
163 info->numChannels,
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]);
170
171 switch (channel_counts[ACT_FRONT]) {
172 case 5:
173 case 4:
174 ch_layout |= AV_CH_FRONT_LEFT_OF_CENTER |
177 case 3:
178 case 2:
179 ch_layout |= AV_CH_LAYOUT_STEREO;
181 case 1:
182 if (channel_counts[ACT_FRONT] & 1)
183 ch_layout |= AV_CH_FRONT_CENTER;
184 break;
185 default:
186 av_log(avctx, AV_LOG_WARNING,
187 "unsupported number of front channels: %d\n",
188 channel_counts[ACT_FRONT]);
189 ch_error = 1;
190 break;
191 }
192
193 if (channel_counts[ACT_SIDE] > 0) {
194 if (channel_counts[ACT_SIDE] == 2) {
195 ch_layout |= AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT;
196 } else {
197 av_log(avctx, AV_LOG_WARNING,
198 "unsupported number of side channels: %d\n",
199 channel_counts[ACT_SIDE]);
200 ch_error = 1;
201 }
202 }
203 if (channel_counts[ACT_BACK] > 0) {
204 switch (channel_counts[ACT_BACK]) {
205 case 4:
206 ch_layout |= AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT;
208 case 3:
209 case 2:
210 ch_layout |= AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT;
212 case 1:
213 if (channel_counts[ACT_BACK] & 1)
214 ch_layout |= AV_CH_BACK_CENTER;
215 break;
216 default:
217 av_log(avctx, AV_LOG_WARNING,
218 "unsupported number of back channels: %d\n",
219 channel_counts[ACT_BACK]);
220 ch_error = 1;
221 break;
222 }
223 }
224 if (channel_counts[ACT_LFE] > 0) {
225 if (channel_counts[ACT_LFE] == 1) {
226 ch_layout |= AV_CH_LOW_FREQUENCY;
227 } else {
228 av_log(avctx, AV_LOG_WARNING,
229 "unsupported number of LFE channels: %d\n",
230 channel_counts[ACT_LFE]);
231 ch_error = 1;
232 }
233 }
234 if (channel_counts[ACT_FRONT_TOP] > 0) {
235 switch (channel_counts[ACT_FRONT_TOP]) {
236 case 3:
237 case 2:
240 case 1:
241 if (channel_counts[ACT_FRONT_TOP] & 1)
242 ch_layout |= AV_CH_TOP_FRONT_CENTER;
243 break;
244 default:
245 av_log(avctx, AV_LOG_WARNING,
246 "unsupported number of top front channels: %d\n",
247 channel_counts[ACT_FRONT_TOP]);
248 ch_error = 1;
249 break;
250 }
251 }
252 if (channel_counts[ACT_BACK_TOP] > 0) {
253 switch (channel_counts[ACT_BACK_TOP]) {
254 case 3:
255 case 2:
258 case 1:
259 if (channel_counts[ACT_BACK_TOP] & 1)
260 ch_layout |= AV_CH_TOP_BACK_CENTER;
261 break;
262 default:
263 av_log(avctx, AV_LOG_WARNING,
264 "unsupported number of top back channels: %d\n",
265 channel_counts[ACT_BACK_TOP]);
266 ch_error = 1;
267 break;
268 }
269 }
270 if (channel_counts[ACT_SIDE_TOP] > 0) {
271 switch (channel_counts[ACT_SIDE_TOP]) {
272 case 3:
273 case 2:
276 case 1:
277 if (channel_counts[ACT_SIDE_TOP] & 1)
278 ch_layout |= AV_CH_TOP_CENTER;
279 break;
280 default:
281 av_log(avctx, AV_LOG_WARNING,
282 "unsupported number of top side channels: %d\n",
283 channel_counts[ACT_SIDE_TOP]);
284 ch_error = 1;
285 break;
286 }
287 }
288 if (channel_counts[ACT_FRONT_BOTTOM] > 0) {
289 switch (channel_counts[ACT_FRONT_BOTTOM]) {
290 case 3:
291 case 2:
294 case 1:
295 if (channel_counts[ACT_FRONT_BOTTOM] & 1)
296 ch_layout |= AV_CH_BOTTOM_FRONT_CENTER;
297 break;
298 default:
299 av_log(avctx, AV_LOG_WARNING,
300 "unsupported number of bottom front channels: %d\n",
301 channel_counts[ACT_FRONT_BOTTOM]);
302 ch_error = 1;
303 break;
304 }
305 }
306
308 av_channel_layout_from_mask(&avctx->ch_layout, ch_layout);
309 if (!ch_error && avctx->ch_layout.nb_channels != info->numChannels) {
310 av_log(avctx, AV_LOG_WARNING, "unsupported channel configuration\n");
311 ch_error = 1;
312 }
313 if (ch_error)
315
316 return 0;
317}
318
320{
321 FDKAACDecContext *s = avctx->priv_data;
322
323 if (s->handle)
324 aacDecoder_Close(s->handle);
325 av_freep(&s->decoder_buffer);
326 av_freep(&s->anc_buffer);
327
328 return 0;
329}
330
332{
333 FDKAACDecContext *s = avctx->priv_data;
334 AAC_DECODER_ERROR err;
335
336 s->handle = aacDecoder_Open(avctx->extradata_size ? TT_MP4_RAW : TT_MP4_ADTS, 1);
337 if (!s->handle) {
338 av_log(avctx, AV_LOG_ERROR, "Error opening decoder\n");
339 return AVERROR_UNKNOWN;
340 }
341
342 if (avctx->extradata_size) {
343 if ((err = aacDecoder_ConfigRaw(s->handle, &avctx->extradata,
344 &avctx->extradata_size)) != AAC_DEC_OK) {
345 av_log(avctx, AV_LOG_ERROR, "Unable to set extradata\n");
346 return AVERROR_INVALIDDATA;
347 }
348 }
349
350 if ((err = aacDecoder_SetParam(s->handle, AAC_CONCEAL_METHOD,
351 s->conceal_method)) != AAC_DEC_OK) {
352 av_log(avctx, AV_LOG_ERROR, "Unable to set error concealment method\n");
353 return AVERROR_UNKNOWN;
354 }
355
356 if (s->downmix_layout.nb_channels > 0 &&
357 s->downmix_layout.order == AV_CHANNEL_ORDER_NATIVE) {
358 int downmix_channels = -1;
359
360 switch (s->downmix_layout.u.mask) {
363 downmix_channels = 2;
364 break;
366 downmix_channels = 1;
367 break;
368 default:
369 av_log(avctx, AV_LOG_WARNING, "Invalid downmix option\n");
370 break;
371 }
372
373 if (downmix_channels != -1) {
374 if (aacDecoder_SetParam(s->handle, AAC_PCM_MAX_OUTPUT_CHANNELS,
375 downmix_channels) != AAC_DEC_OK) {
376 av_log(avctx, AV_LOG_WARNING, "Unable to set output channels in the decoder\n");
377 } else {
378 s->anc_buffer = av_malloc(DMX_ANC_BUFFSIZE);
379 if (!s->anc_buffer) {
380 av_log(avctx, AV_LOG_ERROR, "Unable to allocate ancillary buffer for the decoder\n");
381 return AVERROR(ENOMEM);
382 }
383 if (aacDecoder_AncDataInit(s->handle, s->anc_buffer, DMX_ANC_BUFFSIZE)) {
384 av_log(avctx, AV_LOG_ERROR, "Unable to register downmix ancillary buffer in the decoder\n");
385 return AVERROR_UNKNOWN;
386 }
387 }
388 }
389#if FDKDEC_VER_AT_LEAST(2, 5)
390 } else {
391 // AAC_PCM_MAX_OUTPUT_CHANNELS == 0 means outputting all the coded channels
392 if (aacDecoder_SetParam(s->handle, AAC_PCM_MAX_OUTPUT_CHANNELS, 0) != AAC_DEC_OK)
393 av_log(avctx, AV_LOG_WARNING, "Unable to set output channels in the decoder\n");
394#endif
395 }
396
397 if (s->drc_boost != -1) {
398 if (aacDecoder_SetParam(s->handle, AAC_DRC_BOOST_FACTOR, s->drc_boost) != AAC_DEC_OK) {
399 av_log(avctx, AV_LOG_ERROR, "Unable to set DRC boost factor in the decoder\n");
400 return AVERROR_UNKNOWN;
401 }
402 }
403
404 if (s->drc_cut != -1) {
405 if (aacDecoder_SetParam(s->handle, AAC_DRC_ATTENUATION_FACTOR, s->drc_cut) != AAC_DEC_OK) {
406 av_log(avctx, AV_LOG_ERROR, "Unable to set DRC attenuation factor in the decoder\n");
407 return AVERROR_UNKNOWN;
408 }
409 }
410
411 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 if (aacDecoder_SetParam(s->handle, AAC_DRC_REFERENCE_LEVEL, s->drc_level) != AAC_DEC_OK) {
419 av_log(avctx, AV_LOG_ERROR, "Unable to set DRC reference level in the decoder\n");
420 return AVERROR_UNKNOWN;
421 }
422 }
423
424 if (s->drc_heavy != -1) {
425 if (aacDecoder_SetParam(s->handle, AAC_DRC_HEAVY_COMPRESSION, s->drc_heavy) != AAC_DEC_OK) {
426 av_log(avctx, AV_LOG_ERROR, "Unable to set DRC heavy compression in the decoder\n");
427 return AVERROR_UNKNOWN;
428 }
429 }
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 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");
435 return AVERROR_UNKNOWN;
436 }
437#endif
438
439#if FDKDEC_VER_AT_LEAST(3, 0) // 3.0.0
440 if (s->drc_effect != -1) {
441 if (aacDecoder_SetParam(s->handle, AAC_UNIDRC_SET_EFFECT, s->drc_effect) != AAC_DEC_OK) {
442 av_log(avctx, AV_LOG_ERROR, "Unable to set DRC effect type in the decoder\n");
443 return AVERROR_UNKNOWN;
444 }
445 }
446#endif
447
448#if FDKDEC_VER_AT_LEAST(3, 1) // 3.1.0
449 if (s->album_mode != -1) {
450 if (aacDecoder_SetParam(s->handle, AAC_UNIDRC_ALBUM_MODE, s->album_mode) != AAC_DEC_OK) {
451 av_log(avctx, AV_LOG_ERROR, "Unable to set album mode in the decoder\n");
452 return AVERROR_UNKNOWN;
453 }
454 }
455#endif
456
458
459 s->decoder_buffer_size = DECODER_BUFFSIZE * DECODER_MAX_CHANNELS;
460 s->decoder_buffer = av_malloc(s->decoder_buffer_size);
461 if (!s->decoder_buffer)
462 return AVERROR(ENOMEM);
463
464 return 0;
465}
466
468 int *got_frame_ptr, AVPacket *avpkt)
469{
470 FDKAACDecContext *s = avctx->priv_data;
471 int ret;
472 AAC_DECODER_ERROR err;
473 UINT valid = avpkt->size;
474 UINT flags = 0;
475
476 if (avpkt->size) {
477 err = aacDecoder_Fill(s->handle, &avpkt->data, &avpkt->size, &valid);
478 if (err != AAC_DEC_OK) {
479 av_log(avctx, AV_LOG_ERROR, "aacDecoder_Fill() failed: %x\n", err);
480 return AVERROR_INVALIDDATA;
481 }
482 } else {
483#if FDKDEC_VER_AT_LEAST(2, 5) // 2.5.10
484 /* Handle decoder draining */
485 if (s->flush_samples > 0) {
486 flags |= AACDEC_FLUSH;
487 } else {
488 return 0;
489 }
490#else
491 return 0;
492#endif
493 }
494
495 err = aacDecoder_DecodeFrame(s->handle, (INT_PCM *) s->decoder_buffer,
496 s->decoder_buffer_size / sizeof(INT_PCM),
497 flags);
498 if (err == AAC_DEC_NOT_ENOUGH_BITS) {
499 ret = avpkt->size - valid;
500 goto end;
501 }
502 if (err != AAC_DEC_OK) {
503 av_log(avctx, AV_LOG_ERROR,
504 "aacDecoder_DecodeFrame() failed: %x\n", err);
505 ret = AVERROR_UNKNOWN;
506 goto end;
507 }
508
509 if ((ret = get_stream_info(avctx, frame)) < 0)
510 goto end;
511 frame->nb_samples = avctx->frame_size;
512
513 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
514 goto end;
515
516#if FDKDEC_VER_AT_LEAST(2, 5) // 2.5.10
517 if (flags & AACDEC_FLUSH) {
518 if (s->last_pts != AV_NOPTS_VALUE)
519 frame->pts = av_sat_add64(s->last_pts,
520 av_rescale_q(frame->nb_samples, avctx->time_base,
521 (AVRational){ 1, avctx->sample_rate }));
522 if (s->last_dts != AV_NOPTS_VALUE)
523 frame->pkt_dts = av_sat_add64(s->last_dts,
524 av_rescale_q(frame->nb_samples, avctx->time_base,
525 (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 frame->nb_samples = FFMIN(s->flush_samples, frame->nb_samples);
530 av_log(s, AV_LOG_DEBUG, "Returning %d/%d delayed samples.\n",
531 frame->nb_samples, s->flush_samples);
532 if (s->skip_samples || s->discard_padding) {
534 if (!sd) {
536 if (sd)
537 memset(sd->data, 0, 10);
538 }
539 if (sd && sd->size >= 10) {
540 AV_WL32(sd->data, s->skip_samples);
541 AV_WL32(sd->data + 4, s->discard_padding);
542 }
543 s->skip_samples = 0;
544 s->discard_padding = 0;
545 }
546 s->flush_samples -= frame->nb_samples;
547 s->last_pts = frame->pts;
548 s->last_dts = frame->pkt_dts;
549 } else {
551 if (!sd && s->skip_samples) {
553 if (sd)
554 memset(sd->data, 0, 10);
555 }
556 if (sd && sd->size >= 10) {
557 int skip_samples = AV_RL32(sd->data);
558 s->discard_padding = AV_RL32(sd->data + 4);
559 if (s->discard_padding >= s->output_delay) {
560 AV_WL32(sd->data + 4, s->discard_padding - s->output_delay);
561 s->discard_padding = s->output_delay;
562 } else
563 AV_WL32(sd->data + 4, 0);
564 AV_WL32(sd->data, skip_samples + s->skip_samples);
565 s->skip_samples = 0;
566 }
567 if (frame->pts != AV_NOPTS_VALUE && s->output_delay)
568 frame->pts = av_sat_sub64(frame->pts,
569 av_rescale_q(s->output_delay,
570 (AVRational){ 1, avctx->sample_rate },
571 avctx->time_base));
572 if (frame->pkt_dts != AV_NOPTS_VALUE && s->output_delay)
573 frame->pkt_dts = av_sat_sub64(frame->pkt_dts,
574 av_rescale_q(s->output_delay,
575 (AVRational){ 1, avctx->sample_rate },
576 avctx->time_base));
577 s->last_pts = frame->pts;
578 s->last_dts = frame->pkt_dts;
579 }
580#endif
581
582 memcpy(frame->extended_data[0], s->decoder_buffer,
583 avctx->ch_layout.nb_channels * frame->nb_samples *
585
586 *got_frame_ptr = 1;
587 ret = avpkt->size - valid;
588
589end:
590 return ret;
591}
592
594{
595 FDKAACDecContext *s = avctx->priv_data;
596 AAC_DECODER_ERROR err;
597
598 if (!s->handle)
599 return;
600
601 if ((err = aacDecoder_SetParam(s->handle,
602 AAC_TPDEC_CLEAR_BUFFER, 1)) != AAC_DEC_OK)
603 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 s->skip_samples = 0;
607 s->discard_padding = 0;
608 s->flush_samples = 0;
609 s->output_delay = 0;
610 s->last_pts = AV_NOPTS_VALUE;
611 s->last_dts = AV_NOPTS_VALUE;
612 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 aacDecoder_DecodeFrame(s->handle, (INT_PCM *) s->decoder_buffer,
620 s->decoder_buffer_size / sizeof(INT_PCM),
621 AACDEC_FLUSH | AACDEC_CLRHIST);
622#endif
623}
624
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),
633 .close = fdk_aac_decode_close,
634 .flush = fdk_aac_decode_flush,
636#if FDKDEC_VER_AT_LEAST(2, 5) // 2.5.10
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};
#define ctype
const FFCodec ff_libfdk_aac_decoder
Libavcodec external API header.
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
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
#define av_sat_sub64
Definition common.h:142
#define av_sat_add64
Definition common.h:139
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1781
static int64_t last_pts
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
#define AD
Definition evrcdec.c:920
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_CHLAYOUT
Underlying C type is AVChannelLayout.
Definition opt.h:330
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
#define AV_CH_LAYOUT_STEREO_DOWNMIX
#define AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_STEREO
#define AV_CH_SIDE_LEFT
#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_BACK_RIGHT
#define AV_CH_TOP_SIDE_RIGHT
#define AV_CH_FRONT_CENTER
#define AV_CH_TOP_BACK_RIGHT
#define AV_CH_TOP_CENTER
#define AV_CH_SIDE_RIGHT
#define AV_CH_BACK_LEFT
#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...
Definition codec.h:79
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition codec.h:94
@ AV_CODEC_ID_AAC
Definition codec_id.h:456
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.
Definition error.h:73
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition frame.h:687
AVFrameSideData * av_frame_get_side_data(const AVFrame *frame, enum AVFrameSideDataType type)
Definition frame.c:659
AVFrameSideData * av_frame_new_side_data(AVFrame *frame, enum AVFrameSideDataType type, size_t size)
Add a new side data to a frame.
Definition frame.c:647
@ AV_FRAME_DATA_SKIP_SAMPLES
Recommends skipping the specified number of samples.
Definition frame.h:109
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition samplefmt.c:109
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition samplefmt.h:58
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
#define AV_WL32(p, v)
#define AV_RL32(p)
#define av_fallthrough
Definition attributes.h:67
#define av_cold
Definition attributes.h:117
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 DECODER_BUFFSIZE
#define AAC_PCM_MAX_OUTPUT_CHANNELS
static av_cold int fdk_aac_decode_close(AVCodecContext *avctx)
#define OFFSET(x)
ConcealMethod
@ CONCEAL_METHOD_NB
@ CONCEAL_METHOD_SPECTRAL_MUTING
@ CONCEAL_METHOD_ENERGY_INTERPOLATION
@ CONCEAL_METHOD_NOISE_SUBSTITUTION
static const AVClass fdk_aac_dec_class
#define DMX_ANC_BUFFSIZE
static int fdk_aac_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
#define DECODER_MAX_CHANNELS
#define FFMIN(a, b)
Definition macros.h:49
Memory handling functions.
#define av_malloc(s)
Definition ops_static.c:52
AVOptions.
#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.
Definition log.h:76
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1055
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1047
int profile
profile
Definition avcodec.h:1641
int sample_rate
samples per second
Definition avcodec.h:1040
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition avcodec.h:547
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition avcodec.h:526
int extradata_size
Definition avcodec.h:527
int frame_size
Number of samples per channel in an audio frame.
Definition avcodec.h:1068
void * priv_data
Definition avcodec.h:470
Structure to hold side data for an AVFrame.
Definition frame.h:327
size_t size
Definition frame.h:330
uint8_t * data
Definition frame.h:329
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
Rational number (pair of numerator and denominator).
Definition rational.h:58
uint8_t * anc_buffer
uint8_t * decoder_buffer
HANDLE_AACDECODER handle
AVChannelLayout downmix_layout
#define av_freep(p)
#define av_log(a,...)