Skip to content

Commit 3e9ae6f

Browse files
committed
Avoid seeking checks when decoding frames sequentially
1 parent afd5aba commit 3e9ae6f

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/torchcodec/_core/SingleStreamDecoder.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -618,9 +618,15 @@ FrameOutput SingleStreamDecoder::getFrameAtIndexInternal(
618618
}
619619
validateFrameIndex(streamMetadata, frameIndex);
620620

621-
int64_t pts = getPts(frameIndex);
622-
setCursorPtsInSeconds(ptsToSeconds(pts, streamInfo.timeBase));
623-
return getNextFrameInternal(preAllocatedOutputTensor);
621+
// Only set cursor if we're not decoding sequentially
622+
if (frameIndex != lastDecodedFrameIndex_ + 1) {
623+
int64_t pts = getPts(frameIndex);
624+
setCursorPtsInSeconds(ptsToSeconds(pts, streamInfo.timeBase));
625+
}
626+
627+
auto result = getNextFrameInternal(preAllocatedOutputTensor);
628+
lastDecodedFrameIndex_ = frameIndex;
629+
return result;
624630
}
625631

626632
FrameBatchOutput SingleStreamDecoder::getFramesAtIndices(

src/torchcodec/_core/SingleStreamDecoder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ class SingleStreamDecoder {
345345
bool cursorWasJustSet_ = false;
346346
int64_t lastDecodedAvFramePts_ = 0;
347347
int64_t lastDecodedAvFrameDuration_ = 0;
348+
int64_t lastDecodedFrameIndex_ = INT64_MIN;
348349

349350
// Stores various internal decoding stats.
350351
DecodeStats decodeStats_;

0 commit comments

Comments
 (0)