From 23c1a2dcb4023985fa0763a29157ebf39aca5d0d Mon Sep 17 00:00:00 2001 From: Romain Beauxis Date: Thu, 15 Oct 2026 15:04:12 +0200 Subject: [PATCH] avcodec/codec_par: do not turn the container's priming into decoder delay avcodec_parameters_to_context() copied AVCodecParameters.initial_padding into both AVCodecContext.initial_padding and AVCodecContext.delay. Since ff98352c3d, avcodec_open2() sets skip_samples from AVCodecContext.delay for every decoder, so a decoder opened on a stream with container priming, such as AAC in MP4, started out with that many samples pending to be discarded. The two are different things. initial_padding is the encoder priming at the start of the stream, which the demuxer already attaches to the first packet as skip-samples side data. delay is a property of the decoder instance, which only decoders that know their own delay export in init. Applying the container's value at open discards it from whatever the first decoded packet turns out to be, so seeking before the first decode drops real samples. For a mono 44.1 kHz AAC-LC file with 2048 priming, ffmpeg -ss 530 -t 10 -i in.m4a returned 439152 instead of 441000 samples, with the audio starting 1848 samples late. Stop copying initial_padding into delay. Decoders that export a delay, such as libopus, native opus, wma, lc3, ape and mpegh, set it themselves and are unaffected, and decoding from the start of a stream is unchanged since the side data carries the priming. The documentation of AVCodecContext.delay now says it is discarded when the decoder is opened, and points to seek_preroll for seeking. The seek tests of AAC in MP4 and CAF now start at the requested time instead of late by the container's priming. --- doc/APIchanges | 4 ++++ libavcodec/avcodec.h | 5 +++-- libavcodec/codec_par.c | 1 - libavcodec/version.h | 2 +- tests/ref/fate/mov-aac-2048-priming-seek | 2 ++ tests/ref/fate/preroll-aac-caf-decode | 4 +++- tests/ref/fate/preroll-aac-caf-decode-in-priming | 8 ++++---- tests/ref/fate/preroll-aac-caf-decode-noaccurate | 8 ++++---- tests/ref/fate/preroll-aac-mov-decode | 4 +++- tests/ref/fate/preroll-aac-mov-decode-noaccurate | 8 ++++---- 10 files changed, 28 insertions(+), 18 deletions(-) diff --git a/doc/APIchanges b/doc/APIchanges index 748e7d938f..bd4efbb40d 100644 --- a/doc/APIchanges +++ b/doc/APIchanges @@ -2,6 +2,10 @@ The last version increases of all libraries were on 2026-06-23. API changes, most recent first: +2026-10-xx - xxxxxxxxxx - lavc 63.23.101 - codec_par.h + avcodec_parameters_to_context() no longer sets AVCodecContext.delay from + AVCodecParameters.initial_padding. + 2026-10-23 - xxxxxxxxxxx - lavf 63.14.100 - avformat.h Add AV_STREAM_GROUP_PARAMS_ALPHA. diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index 593ce08036..8dde6d9f6c 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -578,8 +578,9 @@ typedef struct AVCodecContext { * For encoding, this field is unused (see initial_padding). * * For decoding, this is the number of samples the decoder needs to - * output before the decoder's output is valid. When seeking, you should - * start decoding this many samples prior to your desired seek point. + * output before the decoder's output is valid. They are discarded when + * the decoder is opened. When seeking, start decoding seek_preroll + * samples prior to the desired seek point instead. * * - encoding: Set by libavcodec. * - decoding: Set by libavcodec. diff --git a/libavcodec/codec_par.c b/libavcodec/codec_par.c index d8c01775d2..d086fd443e 100644 --- a/libavcodec/codec_par.c +++ b/libavcodec/codec_par.c @@ -242,7 +242,6 @@ int avcodec_parameters_to_context(AVCodecContext *codec, codec->sample_rate = par->sample_rate; codec->block_align = par->block_align; codec->frame_size = par->frame_size; - codec->delay = codec->initial_padding = par->initial_padding; codec->trailing_padding = par->trailing_padding; codec->seek_preroll = par->seek_preroll; diff --git a/libavcodec/version.h b/libavcodec/version.h index 728ab8839d..3af30cdc3b 100644 --- a/libavcodec/version.h +++ b/libavcodec/version.h @@ -30,7 +30,7 @@ #include "version_major.h" #define LIBAVCODEC_VERSION_MINOR 23 -#define LIBAVCODEC_VERSION_MICRO 100 +#define LIBAVCODEC_VERSION_MICRO 101 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ LIBAVCODEC_VERSION_MINOR, \ diff --git a/tests/ref/fate/mov-aac-2048-priming-seek b/tests/ref/fate/mov-aac-2048-priming-seek index fb0223e0d2..834ecaf44d 100644 --- a/tests/ref/fate/mov-aac-2048-priming-seek +++ b/tests/ref/fate/mov-aac-2048-priming-seek @@ -3,6 +3,8 @@ #codec_id 0: pcm_s16le #sample_rate 0: 44100 #channel_layout_name 0: mono +0, 0, 0, 970, 1940, 0x00000000 +0, 970, 970, 1024, 2048, 0x00000000 0, 1994, 1994, 1024, 2048, 0x00000000 0, 3018, 3018, 1024, 2048, 0x00000000 0, 4042, 4042, 1024, 2048, 0x00000000 diff --git a/tests/ref/fate/preroll-aac-caf-decode b/tests/ref/fate/preroll-aac-caf-decode index e687d4d4c7..b1ddddbda8 100644 --- a/tests/ref/fate/preroll-aac-caf-decode +++ b/tests/ref/fate/preroll-aac-caf-decode @@ -3,7 +3,9 @@ #codec_id 0: pcm_s16le #sample_rate 0: 48000 #channel_layout_name 0: stereo -0, 1184, 1184, 960, 3840, 0x00000000 +0, 0, 0, 96, 384, 0x00000000 +0, 96, 96, 1024, 4096, 0x00000000 +0, 1120, 1120, 1024, 4096, 0x00000000 0, 2144, 2144, 1024, 4096, 0x00000000 0, 3168, 3168, 1024, 4096, 0x00000000 0, 4192, 4192, 1024, 4096, 0x00000000 diff --git a/tests/ref/fate/preroll-aac-caf-decode-in-priming b/tests/ref/fate/preroll-aac-caf-decode-in-priming index c7fe14ad30..c6b442753c 100644 --- a/tests/ref/fate/preroll-aac-caf-decode-in-priming +++ b/tests/ref/fate/preroll-aac-caf-decode-in-priming @@ -3,8 +3,8 @@ #codec_id 0: pcm_s16le #sample_rate 0: 48000 #channel_layout_name 0: stereo -0, 1568, 1568, 960, 3840, 0x00000000 +0, -544, -544, 1024, 4096, 0x00000000 +0, 480, 480, 1024, 4096, 0x00000000 +0, 1504, 1504, 1024, 4096, 0x00000000 0, 2528, 2528, 1024, 4096, 0x00000000 -0, 3552, 3552, 1024, 4096, 0x00000000 -0, 4576, 4576, 1024, 4096, 0x00000000 -0, 5600, 5600, 768, 3072, 0x00000000 +0, 3552, 3552, 704, 2816, 0x00000000 diff --git a/tests/ref/fate/preroll-aac-caf-decode-noaccurate b/tests/ref/fate/preroll-aac-caf-decode-noaccurate index 32e7585257..840ce39541 100644 --- a/tests/ref/fate/preroll-aac-caf-decode-noaccurate +++ b/tests/ref/fate/preroll-aac-caf-decode-noaccurate @@ -3,11 +3,11 @@ #codec_id 0: pcm_s16le #sample_rate 0: 48000 #channel_layout_name 0: stereo -0, 1184, 1184, 960, 3840, 0x00000000 +0, -928, -928, 1024, 4096, 0x00000000 +0, 96, 96, 1024, 4096, 0x00000000 +0, 1120, 1120, 1024, 4096, 0x00000000 0, 2144, 2144, 1024, 4096, 0x00000000 0, 3168, 3168, 1024, 4096, 0x00000000 0, 4192, 4192, 1024, 4096, 0x00000000 0, 5216, 5216, 1024, 4096, 0x00000000 -0, 6240, 6240, 1024, 4096, 0x00000000 -0, 7264, 7264, 1024, 4096, 0x00000000 -0, 8288, 8288, 96, 384, 0x00000000 +0, 6240, 6240, 32, 128, 0x00000000 diff --git a/tests/ref/fate/preroll-aac-mov-decode b/tests/ref/fate/preroll-aac-mov-decode index a2c70e25a5..83c3db280f 100644 --- a/tests/ref/fate/preroll-aac-mov-decode +++ b/tests/ref/fate/preroll-aac-mov-decode @@ -3,7 +3,9 @@ #codec_id 0: pcm_s16le #sample_rate 0: 44100 #channel_layout_name 0: stereo -0, 1203, 1203, 960, 3840, 0x00000000 +0, 0, 0, 115, 460, 0x00000000 +0, 115, 115, 1024, 4096, 0x00000000 +0, 1139, 1139, 1024, 4096, 0x00000000 0, 2163, 2163, 1024, 4096, 0x00000000 0, 3187, 3187, 1024, 4096, 0x00000000 0, 4211, 4211, 1024, 4096, 0x00000000 diff --git a/tests/ref/fate/preroll-aac-mov-decode-noaccurate b/tests/ref/fate/preroll-aac-mov-decode-noaccurate index b04a555267..967afc6260 100644 --- a/tests/ref/fate/preroll-aac-mov-decode-noaccurate +++ b/tests/ref/fate/preroll-aac-mov-decode-noaccurate @@ -3,10 +3,10 @@ #codec_id 0: pcm_s16le #sample_rate 0: 44100 #channel_layout_name 0: stereo -0, 1203, 1203, 960, 3840, 0x00000000 +0, -909, -909, 1024, 4096, 0x00000000 +0, 115, 115, 1024, 4096, 0x00000000 +0, 1139, 1139, 1024, 4096, 0x00000000 0, 2163, 2163, 1024, 4096, 0x00000000 0, 3187, 3187, 1024, 4096, 0x00000000 0, 4211, 4211, 1024, 4096, 0x00000000 -0, 5235, 5235, 1024, 4096, 0x00000000 -0, 6259, 6259, 1024, 4096, 0x00000000 -0, 7283, 7283, 535, 2140, 0x00000000 +0, 5235, 5235, 471, 1884, 0x00000000