From 07d067b79075c1305551050106dd8263707ae2af Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Thu, 13 Feb 2025 17:17:37 +0000 Subject: [PATCH] Simplify seek --- src/torchcodec/decoders/_core/VideoDecoder.cpp | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/torchcodec/decoders/_core/VideoDecoder.cpp b/src/torchcodec/decoders/_core/VideoDecoder.cpp index eebbfcef5..69778ea2d 100644 --- a/src/torchcodec/decoders/_core/VideoDecoder.cpp +++ b/src/torchcodec/decoders/_core/VideoDecoder.cpp @@ -863,10 +863,10 @@ bool VideoDecoder::canWeAvoidSeeking(int64_t targetPts) const { // We are seeking forwards. // We can only skip a seek if both lastDecodedAvFramePts and targetPts share // the same keyframe. - int currentKeyFrameIndex = getKeyFrameIndexForPts(lastDecodedAvFramePts); + int lastDecodedAvFrameIndex = getKeyFrameIndexForPts(lastDecodedAvFramePts); int targetKeyFrameIndex = getKeyFrameIndexForPts(targetPts); - return currentKeyFrameIndex >= 0 && targetKeyFrameIndex >= 0 && - currentKeyFrameIndex == targetKeyFrameIndex; + return lastDecodedAvFrameIndex >= 0 && targetKeyFrameIndex >= 0 && + lastDecodedAvFrameIndex == targetKeyFrameIndex; } // This method looks at currentPts and desiredPts and seeks in the @@ -875,18 +875,16 @@ bool VideoDecoder::canWeAvoidSeeking(int64_t targetPts) const { void VideoDecoder::maybeSeekToBeforeDesiredPts() { validateActiveStream(); StreamInfo& streamInfo = streamInfos_[activeStreamIndex_]; - streamInfo.discardFramesBeforePts = + + int64_t desiredPts = secondsToClosestPts(*desiredPtsSeconds_, streamInfo.timeBase); + streamInfo.discardFramesBeforePts = desiredPts; decodeStats_.numSeeksAttempted++; - - int64_t desiredPtsForStream = *desiredPtsSeconds_ * streamInfo.timeBase.den; - if (canWeAvoidSeeking(desiredPtsForStream)) { + if (canWeAvoidSeeking(desiredPts)) { decodeStats_.numSeeksSkipped++; return; } - int64_t desiredPts = - secondsToClosestPts(*desiredPtsSeconds_, streamInfo.timeBase); // For some encodings like H265, FFMPEG sometimes seeks past the point we // set as the max_ts. So we use our own index to give it the exact pts of