@@ -1030,8 +1030,7 @@ 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
@@ -1040,12 +1039,15 @@ VideoDecoder::BatchDecodedOutput VideoDecoder::getFramesAtIndices(
10401039 const auto & options = stream.options ;
10411040 BatchDecodedOutput output (frameIndices.size (), options, streamMetadata);
10421041
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]
1042+ auto indicesAreSorted =
1043+ std::is_sorted (frameIndices.begin (), frameIndices.end ());
1044+
10471045 std::vector<size_t > argsort;
1048- if (sortIndices) {
1046+ if (!indicesAreSorted) {
1047+ // if frameIndices is [13, 10, 12, 11]
1048+ // when sorted, it's [10, 11, 12, 13] <-- this is the sorted order we want
1049+ // to use to decode the frames
1050+ // and argsort is [ 1, 3, 2, 0]
10491051 argsort.resize (frameIndices.size ());
10501052 for (size_t i = 0 ; i < argsort.size (); ++i) {
10511053 argsort[i] = i;
@@ -1058,15 +1060,15 @@ VideoDecoder::BatchDecodedOutput VideoDecoder::getFramesAtIndices(
10581060
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];
@@ -1090,8 +1092,7 @@ VideoDecoder::BatchDecodedOutput VideoDecoder::getFramesAtIndices(
10901092
10911093VideoDecoder::BatchDecodedOutput VideoDecoder::getFramesAtPtss (
10921094 int streamIndex,
1093- const std::vector<double >& framePtss,
1094- const bool sortPtss) {
1095+ const std::vector<double >& framePtss){
10951096 validateUserProvidedStreamIndex (streamIndex);
10961097 validateScannedAllStreams (" getFramesAtPtss" );
10971098
@@ -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