@@ -569,10 +569,8 @@ VideoDecoder::FrameOutput VideoDecoder::getNextFrame() {
569569VideoDecoder::FrameOutput VideoDecoder::getNextFrameInternal (
570570 std::optional<torch::Tensor> preAllocatedOutputTensor) {
571571 validateActiveStream (AVMEDIA_TYPE_VIDEO);
572- AVFrameStream avFrameStream = decodeAVFrame ([this ](AVFrame* avFrame) {
573- StreamInfo& activeStreamInfo = streamInfos_[activeStreamIndex_];
574- return avFrame->pts >= activeStreamInfo.discardFramesBeforePts ;
575- });
572+ AVFrameStream avFrameStream = decodeAVFrame (
573+ [this ](AVFrame* avFrame) { return avFrame->pts >= cursor_; });
576574 return convertAVFrameToFrameOutput (avFrameStream, preAllocatedOutputTensor);
577575}
578576
@@ -909,7 +907,9 @@ torch::Tensor VideoDecoder::getFramesPlayedInRangeAudio(
909907// --------------------------------------------------------------------------
910908
911909void VideoDecoder::setCursorPtsInSeconds (double seconds) {
912- desiredPtsSeconds_ = seconds;
910+ cursorWasJustSet_ = true ;
911+ cursor_ =
912+ secondsToClosestPts (seconds, streamInfos_[activeStreamIndex_].timeBase );
913913}
914914
915915/*
@@ -937,29 +937,29 @@ I P P P I P P P I P P I P P I P
937937
938938(2) is more efficient than (1) if there is an I frame between x and y.
939939*/
940- bool VideoDecoder::canWeAvoidSeeking (int64_t targetPts ) const {
940+ bool VideoDecoder::canWeAvoidSeeking () const {
941941 const StreamInfo& streamInfo = streamInfos_.at (activeStreamIndex_);
942942 if (streamInfo.avMediaType == AVMEDIA_TYPE_AUDIO) {
943943 return true ;
944944 }
945-
946- int64_t lastDecodedAvFramePts = streamInfo .lastDecodedAvFramePts ;
947- if (targetPts < lastDecodedAvFramePts) {
945+ int64_t lastDecodedAvFramePts =
946+ streamInfos_. at (activeStreamIndex_) .lastDecodedAvFramePts ;
947+ if (cursor_ < lastDecodedAvFramePts) {
948948 // We can never skip a seek if we are seeking backwards.
949949 return false ;
950950 }
951- if (lastDecodedAvFramePts == targetPts ) {
951+ if (lastDecodedAvFramePts == cursor_ ) {
952952 // We are seeking to the exact same frame as we are currently at. Without
953953 // caching we have to rewind back and decode the frame again.
954954 // TODO: https://github.com/pytorch-labs/torchcodec/issues/84 we could
955955 // implement caching.
956956 return false ;
957957 }
958958 // We are seeking forwards.
959- // We can only skip a seek if both lastDecodedAvFramePts and targetPts share
960- // the same keyframe.
959+ // We can only skip a seek if both lastDecodedAvFramePts and
960+ // cursor_ share the same keyframe.
961961 int lastDecodedAvFrameIndex = getKeyFrameIndexForPts (lastDecodedAvFramePts);
962- int targetKeyFrameIndex = getKeyFrameIndexForPts (targetPts );
962+ int targetKeyFrameIndex = getKeyFrameIndexForPts (cursor_ );
963963 return lastDecodedAvFrameIndex >= 0 && targetKeyFrameIndex >= 0 &&
964964 lastDecodedAvFrameIndex == targetKeyFrameIndex;
965965}
@@ -971,16 +971,14 @@ void VideoDecoder::maybeSeekToBeforeDesiredPts() {
971971 validateActiveStream ();
972972 StreamInfo& streamInfo = streamInfos_[activeStreamIndex_];
973973
974- int64_t desiredPts =
975- secondsToClosestPts (*desiredPtsSeconds_, streamInfo.timeBase );
976- streamInfo.discardFramesBeforePts = desiredPts;
977-
978974 decodeStats_.numSeeksAttempted ++;
979- if (canWeAvoidSeeking (desiredPts )) {
975+ if (canWeAvoidSeeking ()) {
980976 decodeStats_.numSeeksSkipped ++;
981977 return ;
982978 }
983979
980+ int64_t desiredPts = cursor_;
981+
984982 // For some encodings like H265, FFMPEG sometimes seeks past the point we
985983 // set as the max_ts. So we use our own index to give it the exact pts of
986984 // the key frame that we want to seek to.
@@ -1019,10 +1017,9 @@ VideoDecoder::AVFrameStream VideoDecoder::decodeAVFrame(
10191017
10201018 resetDecodeStats ();
10211019
1022- // Seek if needed.
1023- if (desiredPtsSeconds_.has_value ()) {
1020+ if (cursorWasJustSet_) {
10241021 maybeSeekToBeforeDesiredPts ();
1025- desiredPtsSeconds_ = std:: nullopt ;
1022+ cursorWasJustSet_ = false ;
10261023 }
10271024
10281025 StreamInfo& streamInfo = streamInfos_[activeStreamIndex_];
0 commit comments