From 82d21ba8d2597fe24af581ef1a63fc1e52ae3261 Mon Sep 17 00:00:00 2001 From: arch1t3cht Date: Thu, 28 Mar 2024 22:06:21 +0100 Subject: [PATCH] Avoid seeking too close to the end of the stream This can throw off delay predictions. Hidden frames don't make it into the delayed picture buffer, so the distance needs to be computed in terms of real frames. Signed-off-by: Derek Buitenhuis --- src/core/videosource.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/core/videosource.cpp b/src/core/videosource.cpp index 826fdefb8b..4c34a1cd34 100644 --- a/src/core/videosource.cpp +++ b/src/core/videosource.cpp @@ -811,6 +811,13 @@ bool FFMS_VideoSource::SeekTo(int n, int SeekOffset) { throw FFMS_Exception(FFMS_ERROR_SEEKING, FFMS_ERROR_UNKNOWN, "Frame accurate seeking is not possible in this file"); + // Seeking too close to the end of the stream can result in a different decoder delay since + // frames are returned as soon as draining starts, so avoid this to keep the delay predictable. + // Is the +1 necessary here? Not sure, but let's keep it to be safe. + int EndOfStreamDist = CodecContext->has_b_frames + 1; + + TargetFrame = std::min(TargetFrame, Frames.RealFrameNumber(std::max(0, VP.NumFrames - 1 - EndOfStreamDist))); + if (SeekMode < 3) TargetFrame = Frames.FindClosestVideoKeyFrame(TargetFrame);