Skip to content

Commit 32cd4b4

Browse files
committed
Honor preAllocatedOutputTensor with filtergraph
1 parent 9d7b240 commit 32cd4b4

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

src/torchcodec/decoders/_core/VideoDecoder.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,14 @@ VideoDecoder::DecodedOutput VideoDecoder::convertAVFrameToDecodedOutput(
890890
return output;
891891
}
892892

893+
// Note [preAllocatedOutputTensor with swscale and filtergraph]:
894+
// Callers may pass a pre-allocated tensor, where the output frame tensor will
895+
// be stored. This parameter is honored in any case, but it only leads to a
896+
// speed-up when swscale is used. With swscale, we can tell ffmpeg to place the
897+
// decoded frame directly into `preAllocatedtensor.data_ptr()`. That's not
898+
// possible with filtegraph, leading to an extra copy.
899+
// Dimension order of the preAllocatedOutputTensor must be HWC, regardless of
900+
// `dimension_order` parameter. It's up to callers to re-shape it if needed.
893901
void VideoDecoder::convertAVFrameToDecodedOutputOnCPU(
894902
VideoDecoder::RawDecodedOutput& rawOutput,
895903
DecodedOutput& output,
@@ -926,6 +934,9 @@ void VideoDecoder::convertAVFrameToDecodedOutputOnCPU(
926934
streamInfo.colorConversionLibrary ==
927935
ColorConversionLibrary::FILTERGRAPH) {
928936
output.frame = convertFrameToTensorUsingFilterGraph(streamIndex, frame);
937+
if (preAllocatedOutputTensor.has_value()) {
938+
preAllocatedOutputTensor.value().copy_(output.frame);
939+
}
929940
} else {
930941
throw std::runtime_error(
931942
"Invalid color conversion library: " +
@@ -1077,10 +1088,6 @@ VideoDecoder::BatchDecodedOutput VideoDecoder::getFramesAtIndices(
10771088
} else {
10781089
DecodedOutput singleOut = getFrameAtIndex(
10791090
streamIndex, indexInVideo, output.frames[indexInOutput]);
1080-
if (options.colorConversionLibrary ==
1081-
ColorConversionLibrary::FILTERGRAPH) {
1082-
output.frames[indexInOutput] = singleOut.frame;
1083-
}
10841091
output.ptsSeconds[indexInOutput] = singleOut.ptsSeconds;
10851092
output.durationSeconds[indexInOutput] = singleOut.durationSeconds;
10861093
}
@@ -1157,9 +1164,6 @@ VideoDecoder::BatchDecodedOutput VideoDecoder::getFramesInRange(
11571164

11581165
for (int64_t i = start, f = 0; i < stop; i += step, ++f) {
11591166
DecodedOutput singleOut = getFrameAtIndex(streamIndex, i, output.frames[f]);
1160-
if (options.colorConversionLibrary == ColorConversionLibrary::FILTERGRAPH) {
1161-
output.frames[f] = singleOut.frame;
1162-
}
11631167
output.ptsSeconds[f] = singleOut.ptsSeconds;
11641168
output.durationSeconds[f] = singleOut.durationSeconds;
11651169
}
@@ -1253,9 +1257,6 @@ VideoDecoder::getFramesDisplayedByTimestampInRange(
12531257
BatchDecodedOutput output(numFrames, options, streamMetadata);
12541258
for (int64_t i = startFrameIndex, f = 0; i < stopFrameIndex; ++i, ++f) {
12551259
DecodedOutput singleOut = getFrameAtIndex(streamIndex, i, output.frames[f]);
1256-
if (options.colorConversionLibrary == ColorConversionLibrary::FILTERGRAPH) {
1257-
output.frames[f] = singleOut.frame;
1258-
}
12591260
output.ptsSeconds[f] = singleOut.ptsSeconds;
12601261
output.durationSeconds[f] = singleOut.durationSeconds;
12611262
}

0 commit comments

Comments
 (0)