1
0
mirror of https://git.ffmpeg.org/ffmpeg.git synced 2026-10-24 02:09:03 +03:00

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.
This commit is contained in:
Romain Beauxis
2026-10-15 15:04:12 +02:00
committed by Romain Beauxis
parent 9edc0748b6
commit 23c1a2dcb4
10 changed files with 28 additions and 18 deletions

View File

@@ -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.

View File

@@ -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.

View File

@@ -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;

View File

@@ -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, \

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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