From 86ec99ede58da518ced7d3fbfba02d841e72ce04 Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Tue, 11 Nov 2025 15:56:20 +0000 Subject: [PATCH 1/2] Simplify seek skipping logic --- src/torchcodec/_core/SingleStreamDecoder.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/torchcodec/_core/SingleStreamDecoder.cpp b/src/torchcodec/_core/SingleStreamDecoder.cpp index 4a81c9a8f..b6cdf055b 100644 --- a/src/torchcodec/_core/SingleStreamDecoder.cpp +++ b/src/torchcodec/_core/SingleStreamDecoder.cpp @@ -1100,6 +1100,9 @@ I P P P I P P P I P P I P P I P */ bool SingleStreamDecoder::canWeAvoidSeeking() const { const StreamInfo& streamInfo = streamInfos_.at(activeStreamIndex_); + if (!cursorWasJustSet_) { + return true; + } if (streamInfo.avMediaType == AVMEDIA_TYPE_AUDIO) { // For audio, we only need to seek if a backwards seek was requested // within getFramesPlayedInRangeAudio(), when setCursorPtsInSeconds() was @@ -1181,10 +1184,8 @@ UniqueAVFrame SingleStreamDecoder::decodeAVFrame( resetDecodeStats(); - if (cursorWasJustSet_) { - maybeSeekToBeforeDesiredPts(); - cursorWasJustSet_ = false; - } + maybeSeekToBeforeDesiredPts(); + cursorWasJustSet_ = false; UniqueAVFrame avFrame(av_frame_alloc()); AutoAVPacket autoAVPacket; From abc2cb1e3d4b06f326612a4bd7e0d0c4947d5173 Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Tue, 11 Nov 2025 18:49:55 +0000 Subject: [PATCH 2/2] Minor change --- src/torchcodec/_core/SingleStreamDecoder.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/torchcodec/_core/SingleStreamDecoder.cpp b/src/torchcodec/_core/SingleStreamDecoder.cpp index b6cdf055b..0097f278d 100644 --- a/src/torchcodec/_core/SingleStreamDecoder.cpp +++ b/src/torchcodec/_core/SingleStreamDecoder.cpp @@ -1100,15 +1100,16 @@ I P P P I P P P I P P I P P I P */ bool SingleStreamDecoder::canWeAvoidSeeking() const { const StreamInfo& streamInfo = streamInfos_.at(activeStreamIndex_); - if (!cursorWasJustSet_) { - return true; - } if (streamInfo.avMediaType == AVMEDIA_TYPE_AUDIO) { // For audio, we only need to seek if a backwards seek was requested // within getFramesPlayedInRangeAudio(), when setCursorPtsInSeconds() was // called. For more context, see [Audio Decoding Design] return !cursorWasJustSet_; + } else if (!cursorWasJustSet_) { + // For videos, when decoding consecutive frames, we don't need to seek. + return true; } + if (cursor_ < lastDecodedAvFramePts_) { // We can never skip a seek if we are seeking backwards. return false;