@@ -34,6 +34,10 @@ double ptsToSeconds(int64_t pts, const AVRational& timeBase) {
34
34
return ptsToSeconds (pts, timeBase.den );
35
35
}
36
36
37
+ int64_t secondsToClosestPts (double seconds, const AVRational& timeBase) {
38
+ return static_cast <int64_t >(std::round (seconds * timeBase.den ));
39
+ }
40
+
37
41
struct AVInput {
38
42
UniqueAVFormatContext formatContext;
39
43
std::unique_ptr<AVIOBytesContext> ioBytesContext;
@@ -663,7 +667,7 @@ void VideoDecoder::maybeSeekToBeforeDesiredPts() {
663
667
for (int streamIndex : activeStreamIndices_) {
664
668
StreamInfo& streamInfo = streams_[streamIndex];
665
669
// clang-format off: clang format clashes
666
- streamInfo.discardFramesBeforePts = *maybeDesiredPts_ * streamInfo.timeBase . den ;
670
+ streamInfo.discardFramesBeforePts = secondsToClosestPts ( *maybeDesiredPts_, streamInfo.timeBase ) ;
667
671
// clang-format on
668
672
}
669
673
@@ -686,16 +690,18 @@ void VideoDecoder::maybeSeekToBeforeDesiredPts() {
686
690
}
687
691
int firstActiveStreamIndex = *activeStreamIndices_.begin ();
688
692
const auto & firstStreamInfo = streams_[firstActiveStreamIndex];
689
- int64_t desiredPts = *maybeDesiredPts_ * firstStreamInfo.timeBase .den ;
693
+ int64_t desiredPts =
694
+ secondsToClosestPts (*maybeDesiredPts_, firstStreamInfo.timeBase );
690
695
691
696
// For some encodings like H265, FFMPEG sometimes seeks past the point we
692
697
// set as the max_ts. So we use our own index to give it the exact pts of
693
698
// the key frame that we want to seek to.
694
699
// See https://github.com/pytorch/torchcodec/issues/179 for more details.
695
700
// See https://trac.ffmpeg.org/ticket/11137 for the underlying ffmpeg bug.
696
701
if (!firstStreamInfo.keyFrames .empty ()) {
697
- int desiredKeyFrameIndex =
698
- getKeyFrameIndexForPts (firstStreamInfo, desiredPts);
702
+ int desiredKeyFrameIndex = getKeyFrameIndexForPtsUsingScannedIndex (
703
+ firstStreamInfo.keyFrames , desiredPts);
704
+ desiredKeyFrameIndex = std::max (desiredKeyFrameIndex, 0 );
699
705
desiredPts = firstStreamInfo.keyFrames [desiredKeyFrameIndex].pts ;
700
706
}
701
707
0 commit comments