Skip to content

Commit 578094e

Browse files
committed
Let getFramesAtIndices rely on getFrameAtIndex
1 parent 22126c4 commit 578094e

File tree

1 file changed

+10
-26
lines changed

1 file changed

+10
-26
lines changed

src/torchcodec/decoders/_core/VideoDecoder.cpp

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,39 +1035,23 @@ VideoDecoder::BatchDecodedOutput VideoDecoder::getFramesAtIndices(
10351035
validateScannedAllStreams("getFramesAtIndices");
10361036

10371037
const auto& streamMetadata = containerMetadata_.streams[streamIndex];
1038-
const auto& options = streams_[streamIndex].options;
1038+
const auto& stream = streams_[streamIndex];
1039+
const auto& options = stream.options;
10391040
BatchDecodedOutput output(frameIndices.size(), options, streamMetadata);
10401041

1041-
int i = 0;
1042-
const auto& stream = streams_[streamIndex];
1043-
for (int64_t frameIndex : frameIndices) {
1042+
for (auto f = 0; f < frameIndices.size(); ++f) {
1043+
auto frameIndex = frameIndices[f];
10441044
if (frameIndex < 0 || frameIndex >= stream.allFrames.size()) {
10451045
throw std::runtime_error(
10461046
"Invalid frame index=" + std::to_string(frameIndex));
10471047
}
1048-
int64_t pts = stream.allFrames[frameIndex].pts;
1049-
setCursorPtsInSeconds(ptsToSeconds(pts, stream.timeBase));
1050-
auto rawSingleOutput = getNextRawDecodedOutputNoDemux();
1051-
if (stream.colorConversionLibrary == ColorConversionLibrary::SWSCALE) {
1052-
// We are using sws_scale to convert the frame to tensor. sws_scale can
1053-
// convert to a pre-allocated buffer so we can do the color-conversion
1054-
// in-place on the output tensor's data_ptr.
1055-
rawSingleOutput.data = output.frames[i].data_ptr<uint8_t>();
1056-
convertFrameToBufferUsingSwsScale(rawSingleOutput);
1057-
} else if (
1058-
stream.colorConversionLibrary == ColorConversionLibrary::FILTERGRAPH) {
1059-
// We are using a filter graph to convert the frame to tensor. The
1060-
// filter graph returns us an AVFrame allocated by FFMPEG. So we need to
1061-
// copy the AVFrame to the output tensor.
1062-
torch::Tensor frame = convertFrameToTensorUsingFilterGraph(
1063-
rawSingleOutput.streamIndex, rawSingleOutput.frame.get());
1064-
output.frames[i] = frame;
1065-
} else {
1066-
throw std::runtime_error(
1067-
"Invalid color conversion library: " +
1068-
std::to_string(static_cast<int>(stream.colorConversionLibrary)));
1048+
DecodedOutput singleOut =
1049+
getFrameAtIndex(streamIndex, frameIndex, output.frames[f]);
1050+
if (options.colorConversionLibrary == ColorConversionLibrary::FILTERGRAPH) {
1051+
output.frames[f] = singleOut.frame;
10691052
}
1070-
i++;
1053+
// Note that for now we ignore the pts and duration parts of the output,
1054+
// because they're never used in any caller.
10711055
}
10721056
output.frames = MaybeHWC2CHW(options, output.frames);
10731057
return output;

0 commit comments

Comments
 (0)