@@ -1090,10 +1090,43 @@ VideoDecoder::BatchDecodedOutput VideoDecoder::getFramesAtIndices(
10901090
10911091VideoDecoder::BatchDecodedOutput VideoDecoder::getFramesAtPtss (
10921092 int streamIndex,
1093- const std::vector<int64_t >& framePtss,
1093+ const std::vector<double >& framePtss,
10941094 const bool sortPtss) {
1095- return getFramesAtIndices (streamIndex, framePtss, sortPtss);
1096- }
1095+ validateUserProvidedStreamIndex (streamIndex);
1096+ validateScannedAllStreams (" getFramesAtPtss" );
1097+
1098+ // The frame displayed at timestamp t and the one displayed at timestamp `t +
1099+ // eps` are probably the same frame, with the same index. The easiest way to
1100+ // avoid decoding that unique frame twice is to convert the input timestamps
1101+ // to indices, and leverage the de-duplication logic of getFramesAtIndices.
1102+
1103+ const auto & streamMetadata = containerMetadata_.streams [streamIndex];
1104+ const auto & stream = streams_[streamIndex];
1105+ double minSeconds = streamMetadata.minPtsSecondsFromScan .value ();
1106+ double maxSeconds = streamMetadata.maxPtsSecondsFromScan .value ();
1107+
1108+ std::vector<int64_t > frameIndices (framePtss.size ());
1109+ for (auto i = 0 ; i < framePtss.size (); ++i) {
1110+ auto framePts = framePtss[i];
1111+ TORCH_CHECK (
1112+ framePts >= minSeconds && framePts < maxSeconds,
1113+ " frame pts is " + std::to_string (framePts) + " ; must be in range [" +
1114+ std::to_string (minSeconds) + " , " + std::to_string (maxSeconds) +
1115+ " )." );
1116+
1117+ auto it = std::lower_bound (
1118+ stream.allFrames .begin (),
1119+ stream.allFrames .end (),
1120+ framePts,
1121+ [&stream](const FrameInfo& info, double start) {
1122+ return ptsToSeconds (info.nextPts , stream.timeBase ) <= start;
1123+ });
1124+ int64_t frameIndex = it - stream.allFrames .begin ();
1125+ frameIndices[i] = frameIndex;
1126+ }
1127+
1128+ return getFramesAtIndices (streamIndex, frameIndices, sortPtss);
1129+ }
10971130
10981131VideoDecoder::BatchDecodedOutput VideoDecoder::getFramesInRange (
10991132 int streamIndex,
0 commit comments