Skip to content

Commit 133c213

Browse files
committed
minor opt
1 parent f7a70ba commit 133c213

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/torchcodec/decoders/_core/VideoDecoder.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,11 +1040,13 @@ VideoDecoder::BatchDecodedOutput VideoDecoder::getFramesAtIndices(
10401040
const auto& options = stream.options;
10411041
BatchDecodedOutput output(frameIndices.size(), options, streamMetadata);
10421042

1043-
std::vector<size_t> argsort(frameIndices.size());
1044-
for (size_t i = 0; i < argsort.size(); ++i) {
1045-
argsort[i] = i;
1046-
}
1043+
std::vector<size_t> argsort;
1044+
10471045
if (sortIndices) {
1046+
argsort.resize(frameIndices.size());
1047+
for (size_t i = 0; i < argsort.size(); ++i) {
1048+
argsort[i] = i;
1049+
}
10481050
std::sort(
10491051
argsort.begin(), argsort.end(), [&frameIndices](size_t a, size_t b) {
10501052
return frameIndices[a] < frameIndices[b];
@@ -1053,15 +1055,15 @@ VideoDecoder::BatchDecodedOutput VideoDecoder::getFramesAtIndices(
10531055

10541056
auto previousIndexInVideo = -1;
10551057
for (auto f = 0; f < frameIndices.size(); ++f) {
1056-
auto indexInOutput = argsort[f];
1057-
auto indexInVideo = frameIndices[argsort[f]];
1058+
auto indexInOutput = sortIndices ? argsort[f] : f;
1059+
auto indexInVideo = frameIndices[indexInOutput];
10581060
if (indexInVideo < 0 || indexInVideo >= stream.allFrames.size()) {
10591061
throw std::runtime_error(
10601062
"Invalid frame index=" + std::to_string(indexInVideo));
10611063
}
10621064
if ((f > 0) && (indexInVideo == previousIndexInVideo)) {
10631065
// Avoid decoding the same frame twice
1064-
auto previousIndexInOutput = argsort[f - 1];
1066+
auto previousIndexInOutput = sortIndices ? argsort[f - 1] : f - 1;
10651067
output.frames[indexInOutput].copy_(output.frames[previousIndexInOutput]);
10661068
output.ptsSeconds[indexInOutput] =
10671069
output.ptsSeconds[previousIndexInOutput];

0 commit comments

Comments
 (0)