From 3e9ae6f439d3ec2c31014fbad040e1e5c8a77aaf Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Fri, 7 Nov 2025 11:29:08 +0000 Subject: [PATCH] Avoid seeking checks when decoding frames sequentially --- src/torchcodec/_core/SingleStreamDecoder.cpp | 12 +++++++++--- src/torchcodec/_core/SingleStreamDecoder.h | 1 + 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/torchcodec/_core/SingleStreamDecoder.cpp b/src/torchcodec/_core/SingleStreamDecoder.cpp index 72cd7afac..0db09f058 100644 --- a/src/torchcodec/_core/SingleStreamDecoder.cpp +++ b/src/torchcodec/_core/SingleStreamDecoder.cpp @@ -618,9 +618,15 @@ FrameOutput SingleStreamDecoder::getFrameAtIndexInternal( } validateFrameIndex(streamMetadata, frameIndex); - int64_t pts = getPts(frameIndex); - setCursorPtsInSeconds(ptsToSeconds(pts, streamInfo.timeBase)); - return getNextFrameInternal(preAllocatedOutputTensor); + // Only set cursor if we're not decoding sequentially + if (frameIndex != lastDecodedFrameIndex_ + 1) { + int64_t pts = getPts(frameIndex); + setCursorPtsInSeconds(ptsToSeconds(pts, streamInfo.timeBase)); + } + + auto result = getNextFrameInternal(preAllocatedOutputTensor); + lastDecodedFrameIndex_ = frameIndex; + return result; } FrameBatchOutput SingleStreamDecoder::getFramesAtIndices( diff --git a/src/torchcodec/_core/SingleStreamDecoder.h b/src/torchcodec/_core/SingleStreamDecoder.h index 4b41811ff..96768989a 100644 --- a/src/torchcodec/_core/SingleStreamDecoder.h +++ b/src/torchcodec/_core/SingleStreamDecoder.h @@ -345,6 +345,7 @@ class SingleStreamDecoder { bool cursorWasJustSet_ = false; int64_t lastDecodedAvFramePts_ = 0; int64_t lastDecodedAvFrameDuration_ = 0; + int64_t lastDecodedFrameIndex_ = INT64_MIN; // Stores various internal decoding stats. DecodeStats decodeStats_;