@@ -1030,22 +1030,19 @@ VideoDecoder::DecodedOutput VideoDecoder::getFrameAtIndex(
10301030
10311031VideoDecoder::BatchDecodedOutput VideoDecoder::getFramesAtIndices (
10321032 int streamIndex,
1033- const std::vector<int64_t >& frameIndices,
1034- const bool sortIndices) {
1033+ const std::vector<int64_t >& frameIndices) {
10351034 validateUserProvidedStreamIndex (streamIndex);
10361035 validateScannedAllStreams (" getFramesAtIndices" );
10371036
1038- const auto & streamMetadata = containerMetadata_.streams [streamIndex];
1039- const auto & stream = streams_[streamIndex];
1040- const auto & options = stream.options ;
1041- BatchDecodedOutput output (frameIndices.size (), options, streamMetadata);
1037+ auto indicesAreSorted =
1038+ std::is_sorted (frameIndices.begin (), frameIndices.end ());
10421039
1043- // if frameIndices is [13, 10, 12, 11]
1044- // when sorted, it's [10, 11, 12, 13] <-- this is the sorted order we want
1045- // to use to decode the frames
1046- // and argsort is [ 1, 3, 2, 0]
10471040 std::vector<size_t > argsort;
1048- if (sortIndices) {
1041+ if (!indicesAreSorted) {
1042+ // if frameIndices is [13, 10, 12, 11]
1043+ // when sorted, it's [10, 11, 12, 13] <-- this is the sorted order we want
1044+ // to use to decode the frames
1045+ // and argsort is [ 1, 3, 2, 0]
10491046 argsort.resize (frameIndices.size ());
10501047 for (size_t i = 0 ; i < argsort.size (); ++i) {
10511048 argsort[i] = i;
@@ -1056,17 +1053,22 @@ VideoDecoder::BatchDecodedOutput VideoDecoder::getFramesAtIndices(
10561053 });
10571054 }
10581055
1056+ const auto & streamMetadata = containerMetadata_.streams [streamIndex];
1057+ const auto & stream = streams_[streamIndex];
1058+ const auto & options = stream.options ;
1059+ BatchDecodedOutput output (frameIndices.size (), options, streamMetadata);
1060+
10591061 auto previousIndexInVideo = -1 ;
10601062 for (auto f = 0 ; f < frameIndices.size (); ++f) {
1061- auto indexInOutput = sortIndices ? argsort[f] : f ;
1063+ auto indexInOutput = indicesAreSorted ? f : argsort[f] ;
10621064 auto indexInVideo = frameIndices[indexInOutput];
10631065 if (indexInVideo < 0 || indexInVideo >= stream.allFrames .size ()) {
10641066 throw std::runtime_error (
10651067 " Invalid frame index=" + std::to_string (indexInVideo));
10661068 }
10671069 if ((f > 0 ) && (indexInVideo == previousIndexInVideo)) {
10681070 // Avoid decoding the same frame twice
1069- auto previousIndexInOutput = sortIndices ? argsort[ f - 1 ] : f - 1 ;
1071+ auto previousIndexInOutput = indicesAreSorted ? f - 1 : argsort[ f - 1 ] ;
10701072 output.frames [indexInOutput].copy_ (output.frames [previousIndexInOutput]);
10711073 output.ptsSeconds [indexInOutput] =
10721074 output.ptsSeconds [previousIndexInOutput];
@@ -1088,12 +1090,11 @@ VideoDecoder::BatchDecodedOutput VideoDecoder::getFramesAtIndices(
10881090 return output;
10891091}
10901092
1091- VideoDecoder::BatchDecodedOutput VideoDecoder::getFramesAtPtss (
1093+ VideoDecoder::BatchDecodedOutput VideoDecoder::getFramesDisplayedByTimestamps (
10921094 int streamIndex,
1093- const std::vector<double >& framePtss,
1094- const bool sortPtss) {
1095+ const std::vector<double >& framePtss) {
10951096 validateUserProvidedStreamIndex (streamIndex);
1096- validateScannedAllStreams (" getFramesAtPtss " );
1097+ validateScannedAllStreams (" getFramesDisplayedByTimestamps " );
10971098
10981099 // The frame displayed at timestamp t and the one displayed at timestamp `t +
10991100 // eps` are probably the same frame, with the same index. The easiest way to
@@ -1116,7 +1117,7 @@ VideoDecoder::BatchDecodedOutput VideoDecoder::getFramesAtPtss(
11161117
11171118 auto it = std::lower_bound (
11181119 stream.allFrames .begin (),
1119- stream.allFrames .end (),
1120+ stream.allFrames .end () - 1 ,
11201121 framePts,
11211122 [&stream](const FrameInfo& info, double start) {
11221123 return ptsToSeconds (info.nextPts , stream.timeBase ) <= start;
@@ -1125,7 +1126,7 @@ VideoDecoder::BatchDecodedOutput VideoDecoder::getFramesAtPtss(
11251126 frameIndices[i] = frameIndex;
11261127 }
11271128
1128- return getFramesAtIndices (streamIndex, frameIndices, sortPtss );
1129+ return getFramesAtIndices (streamIndex, frameIndices);
11291130}
11301131
11311132VideoDecoder::BatchDecodedOutput VideoDecoder::getFramesInRange (
0 commit comments