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