PR #24528 opened by James Almer (jamrial) URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/24528 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/24528.patch
Shift timestamps when available and take into account the caller requested samples to be discarded. The decoder can't know where a seek landed, so the "delay_samples" way to discard the decoder delay in the first frame is not truly viable, so it needs to be removed. Should fix issues #24526 and #24527. >From 9cdc8210596364a4b18d491a4cb7f65584b8da97 Mon Sep 17 00:00:00 2001 From: James Almer <[email protected]> Date: Tue, 15 Sep 2026 22:35:25 -0300 Subject: [PATCH 1/2] avcodec/libfdk-aacdec: properly handle decoder delay Shift timestamps when available and take into account the caller requested samples to be discarded. Should fix issue #24526. Signed-off-by: James Almer <[email protected]> --- libavcodec/libfdk-aacdec.c | 63 +++++++++++++++++++++++++++++++++----- 1 file changed, 56 insertions(+), 7 deletions(-) diff --git a/libavcodec/libfdk-aacdec.c b/libavcodec/libfdk-aacdec.c index 41ff4887a6..856a3f3e57 100644 --- a/libavcodec/libfdk-aacdec.c +++ b/libavcodec/libfdk-aacdec.c @@ -21,6 +21,7 @@ #include "libavutil/channel_layout.h" #include "libavutil/common.h" +#include "libavutil/intreadwrite.h" #include "libavutil/mem.h" #include "libavutil/opt.h" #include "avcodec.h" @@ -64,6 +65,10 @@ typedef struct FDKAACDecContext { int output_delay_set; int flush_samples; int delay_samples; + int output_delay; + int discard_padding; + int64_t last_pts; + int64_t last_dts; #endif AVChannelLayout downmix_layout; } FDKAACDecContext; @@ -138,6 +143,9 @@ static int get_stream_info(AVCodecContext *avctx, AVFrame *frame) // Set this only once. s->flush_samples = info->outputDelay; s->delay_samples = info->outputDelay; + s->output_delay = info->outputDelay; + s->last_pts = AV_NOPTS_VALUE; + s->last_dts = AV_NOPTS_VALUE; s->output_delay_set = 1; } #endif @@ -503,19 +511,63 @@ static int fdk_aac_decode_frame(AVCodecContext *avctx, AVFrame *frame, goto end; frame->nb_samples = avctx->frame_size; + if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) + goto end; + #if FDKDEC_VER_AT_LEAST(2, 5) // 2.5.10 if (flags & AACDEC_FLUSH) { + if (frame->pts != AV_NOPTS_VALUE) { + if (s->last_pts != AV_NOPTS_VALUE) + frame->pts = av_sat_add64(s->last_pts, + av_rescale_q(frame->nb_samples, avctx->time_base, + (AVRational){ 1, avctx->sample_rate })); + if (s->last_dts != AV_NOPTS_VALUE) + frame->pkt_dts = av_sat_add64(s->last_dts, + av_rescale_q(frame->nb_samples, avctx->time_base, + (AVRational){ 1, avctx->sample_rate })); + } + // Only return the right amount of samples at the end; if calling the // decoder with AACDEC_FLUSH, it will keep returning frames indefinitely. frame->nb_samples = FFMIN(s->flush_samples, frame->nb_samples); av_log(s, AV_LOG_DEBUG, "Returning %d/%d delayed samples.\n", frame->nb_samples, s->flush_samples); + if (s->discard_padding) { + AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES); + if (!sd) + sd = av_frame_new_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES, 10); + if (sd && sd->size >= 10) + AV_WL32(sd->data + 4, s->discard_padding); + s->discard_padding = 0; + } s->flush_samples -= frame->nb_samples; + s->last_pts = frame->pts; + s->last_dts = frame->pkt_dts; } else { - // Trim off samples from the start to compensate for extra decoder - // delay. We could also just adjust the pts, but this avoids - // including the extra samples in the output altogether. - if (s->delay_samples) { + AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_SKIP_SAMPLES); + if (sd && sd->size >= 10) { + s->discard_padding = AV_RL32(sd->data + 4); + if (s->discard_padding >= s->output_delay) { + AV_WL32(sd->data + 4, s->discard_padding - s->output_delay); + s->discard_padding = s->output_delay; + } + } + if (frame->pts != AV_NOPTS_VALUE) { + frame->pts = av_sat_sub64(frame->pts, + av_rescale_q(s->output_delay, + (AVRational){ 1, avctx->sample_rate }, + avctx->time_base)); + if (frame->pkt_dts != AV_NOPTS_VALUE) + frame->pkt_dts = av_sat_sub64(frame->pkt_dts, + av_rescale_q(s->output_delay, + (AVRational){ 1, avctx->sample_rate }, + avctx->time_base)); + s->delay_samples = 0; + s->last_pts = frame->pts; + s->last_dts = frame->pkt_dts; + } else if (s->delay_samples) { + // Trim off samples from the start to compensate for extra decoder + // delay, in the absense of timestamps. int drop_samples = FFMIN(s->delay_samples, frame->nb_samples); av_log(s, AV_LOG_DEBUG, "Dropping %d/%d delayed samples.\n", drop_samples, s->delay_samples); @@ -528,9 +580,6 @@ static int fdk_aac_decode_frame(AVCodecContext *avctx, AVFrame *frame, } #endif - if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) - goto end; - memcpy(frame->extended_data[0], s->decoder_buffer + input_offset, avctx->ch_layout.nb_channels * frame->nb_samples * av_get_bytes_per_sample(avctx->sample_fmt)); -- 2.52.0 >From 1e33984d76780a8a2dc99565638967aec418c2c7 Mon Sep 17 00:00:00 2001 From: James Almer <[email protected]> Date: Tue, 15 Sep 2026 22:39:17 -0300 Subject: [PATCH 2/2] avcodec/libfdk-aacdec: clear and reset some fields when flushing The decoder can't know where a seek landed, so the "delay_samples" way to discard the decoder delay in the first frame is not truly viable, so it needs to be removed. Should fix issue #24527. Signed-off-by: James Almer <[email protected]> --- libavcodec/libfdk-aacdec.c | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/libavcodec/libfdk-aacdec.c b/libavcodec/libfdk-aacdec.c index 856a3f3e57..1407cc489e 100644 --- a/libavcodec/libfdk-aacdec.c +++ b/libavcodec/libfdk-aacdec.c @@ -64,7 +64,6 @@ typedef struct FDKAACDecContext { #if FDKDEC_VER_AT_LEAST(2, 5) // 2.5.10 int output_delay_set; int flush_samples; - int delay_samples; int output_delay; int discard_padding; int64_t last_pts; @@ -142,7 +141,6 @@ static int get_stream_info(AVCodecContext *avctx, AVFrame *frame) if (!s->output_delay_set && info->outputDelay) { // Set this only once. s->flush_samples = info->outputDelay; - s->delay_samples = info->outputDelay; s->output_delay = info->outputDelay; s->last_pts = AV_NOPTS_VALUE; s->last_dts = AV_NOPTS_VALUE; @@ -562,20 +560,8 @@ static int fdk_aac_decode_frame(AVCodecContext *avctx, AVFrame *frame, av_rescale_q(s->output_delay, (AVRational){ 1, avctx->sample_rate }, avctx->time_base)); - s->delay_samples = 0; s->last_pts = frame->pts; s->last_dts = frame->pkt_dts; - } else if (s->delay_samples) { - // Trim off samples from the start to compensate for extra decoder - // delay, in the absense of timestamps. - int drop_samples = FFMIN(s->delay_samples, frame->nb_samples); - av_log(s, AV_LOG_DEBUG, "Dropping %d/%d delayed samples.\n", - drop_samples, s->delay_samples); - s->delay_samples -= drop_samples; - frame->nb_samples -= drop_samples; - input_offset = drop_samples * avctx->ch_layout.nb_channels; - if (frame->nb_samples <= 0) - return 0; } } #endif @@ -602,6 +588,9 @@ static av_cold void fdk_aac_decode_flush(AVCodecContext *avctx) if ((err = aacDecoder_SetParam(s->handle, AAC_TPDEC_CLEAR_BUFFER, 1)) != AAC_DEC_OK) av_log(avctx, AV_LOG_WARNING, "failed to clear buffer when flushing\n"); + + s->flush_samples = s->output_delay; + s->discard_padding = 0; } const FFCodec ff_libfdk_aac_decoder = { -- 2.52.0 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
