mirror of
https://git.ffmpeg.org/ffmpeg.git
synced 2026-10-28 18:15:24 +08:00
After avformat_seek_file(), the decoder is not told what to discard from what it decodes: the frames over the pre-roll are not reliable, and a seek that lands inside the priming at the start of the stream outputs the rest of it. Only some demuxers, such as mov, set the skip for the priming themselves. Mark the streams after a successful seek, and on the first packet of each audio stream, add to the samples to skip, which the decoder applies through AV_PKT_DATA_SKIP_SAMPLES: - on the first packet of the stream, the priming, as when decoding from the start: its exact count when it is known; - on any other packet, the larger of the rest of the priming, which ends at the start time of the stream, and the seek pre-roll of the codec, as the decoder misses what came before. After the priming of a stream whose start the seek targets, no pre-roll is added, as which packet comes first then depends on the demuxer. Each stream compares the target with its own start. The first packet is only recognised when the demuxer knows the first timestamp of the stream, such as after avformat_find_stream_info(); otherwise, the packet gets the pre-roll as after any seek. The priming needs the timestamp of the packet and the start time of the stream, the pre-roll does not. The skip may span several packets, as the decoder carries it over the frames it outputs. Side data the demuxer set on the packet is kept: the larger skip is used, and its padding at the end of the stream is not overwritten. The skip supplies the pre-roll the stream declares, whatever the demuxer landed on; recovery beyond it depends on the codec and the bitstream. The caller keeps or trims what is decoded after it as it wishes. The decoding of the tests without accurate seek now starts one frame later for AAC in MP4, CAF and ADTS, and two 2048 samples frames later for HE-AAC in ADTS and with implicit SBR. A seek that lands on the last packet of the priming of AAC in CAF, with 64 samples of it left, now skips the 1024 samples of pre-roll instead of outputting that priming. For Opus in Matroska, 3840 samples are skipped instead of the 312 the decoder drops from its first frame, and the decoding with accurate seek is cut on frames 24 samples, half a millisecond, earlier, within the precision of the Matroska timestamps. A seek past the end of AAC in CAF and Opus in Matroska skips the pre-roll on the last packet and keeps its padding. A seek on the later of two streams, close to its start, adds the pre-roll to the other stream, which is already past its own start. A seek back to the start of Matroska now skips the exact priming on the first packet: 312 samples for Opus and 481 for MP2.