Skip to content

Commit 88a655d

Browse files
committed
Avoid seeking in range APIs
1 parent 6f01650 commit 88a655d

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

src/torchcodec/decoders/_core/VideoDecoder.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,10 @@ VideoDecoder::getFramesInRange(int64_t start, int64_t stop, int64_t step) {
653653
getFrameAtIndexInternal(i, frameBatchOutput.data[f]);
654654
frameBatchOutput.ptsSeconds[f] = frameOutput.ptsSeconds;
655655
frameBatchOutput.durationSeconds[f] = frameOutput.durationSeconds;
656+
// We know the frames are consecutive, there's no need to seek
657+
mustSkipSeeking_ = (step == 1);
656658
}
659+
mustSkipSeeking_ = false; // reset to original value
657660
frameBatchOutput.data = maybePermuteHWC2CHW(frameBatchOutput.data);
658661
return frameBatchOutput;
659662
}
@@ -807,7 +810,10 @@ VideoDecoder::FrameBatchOutput VideoDecoder::getFramesPlayedInRange(
807810
getFrameAtIndexInternal(i, frameBatchOutput.data[f]);
808811
frameBatchOutput.ptsSeconds[f] = frameOutput.ptsSeconds;
809812
frameBatchOutput.durationSeconds[f] = frameOutput.durationSeconds;
813+
// We know the frames are consecutive, there's no need to seek
814+
mustSkipSeeking_ = true;
810815
}
816+
mustSkipSeeking_ = false; // reset to original value
811817
frameBatchOutput.data = maybePermuteHWC2CHW(frameBatchOutput.data);
812818

813819
return frameBatchOutput;
@@ -847,6 +853,9 @@ I P P P I P P P I P P I P P I P
847853
(2) is more efficient than (1) if there is an I frame between x and y.
848854
*/
849855
bool VideoDecoder::canWeAvoidSeeking(int64_t targetPts) const {
856+
if (mustSkipSeeking_) {
857+
return true;
858+
}
850859
int64_t lastDecodedAvFramePts =
851860
streamInfos_.at(activeStreamIndex_).lastDecodedAvFramePts;
852861
if (targetPts < lastDecodedAvFramePts) {

src/torchcodec/decoders/_core/VideoDecoder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ class VideoDecoder {
473473
DecodeStats decodeStats_;
474474
// Stores the AVIOContext for the input buffer.
475475
std::unique_ptr<AVIOBytesContext> ioBytesContext_;
476+
bool mustSkipSeeking_ = false;
476477
// Whether or not we have already scanned all streams to update the metadata.
477478
bool scannedAllStreams_ = false;
478479
// Tracks that we've already been initialized.

0 commit comments

Comments
 (0)