Skip to content

Commit b35005d

Browse files
authored
Avoid seeking checks when decoding frames sequentially (#1028)
1 parent 982102b commit b35005d

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/torchcodec/_core/SingleStreamDecoder.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -626,9 +626,17 @@ FrameOutput SingleStreamDecoder::getFrameAtIndexInternal(
626626
}
627627
validateFrameIndex(streamMetadata, frameIndex);
628628

629-
int64_t pts = getPts(frameIndex);
630-
setCursorPtsInSeconds(ptsToSeconds(pts, streamInfo.timeBase));
631-
return getNextFrameInternal(preAllocatedOutputTensor);
629+
// Only set cursor if we're not decoding sequentially: when decoding
630+
// sequentially, we don't need to seek anywhere, so by *not* setting the
631+
// cursor we allow canWeAvoidSeeking() to return true early.
632+
if (frameIndex != lastDecodedFrameIndex_ + 1) {
633+
int64_t pts = getPts(frameIndex);
634+
setCursorPtsInSeconds(ptsToSeconds(pts, streamInfo.timeBase));
635+
}
636+
637+
auto result = getNextFrameInternal(preAllocatedOutputTensor);
638+
lastDecodedFrameIndex_ = frameIndex;
639+
return result;
632640
}
633641

634642
FrameBatchOutput SingleStreamDecoder::getFramesAtIndices(

src/torchcodec/_core/SingleStreamDecoder.h

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

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

0 commit comments

Comments
 (0)