Skip to content

Commit 8e06aa6

Browse files
committed
pass a pointer, segfaults
1 parent 82924d2 commit 8e06aa6

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/torchcodec/decoders/_core/VideoDecoder.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -878,23 +878,27 @@ VideoDecoder::DecodedOutput VideoDecoder::convertAVFrameToDecodedOutput(
878878

879879
void VideoDecoder::convertAVFrameToDecodedOutputOnCPU(
880880
VideoDecoder::RawDecodedOutput& rawOutput,
881-
DecodedOutput& output) {
881+
DecodedOutput& output,
882+
torch::Tensor* tensor
883+
) {
882884
int streamIndex = rawOutput.streamIndex;
883885
AVFrame* frame = rawOutput.frame.get();
884886
auto& streamInfo = streams_[streamIndex];
885887
if (output.streamType == AVMEDIA_TYPE_VIDEO) {
886888
if (streamInfo.colorConversionLibrary == ColorConversionLibrary::SWSCALE) {
887889
int width = streamInfo.options.width.value_or(frame->width);
888890
int height = streamInfo.options.height.value_or(frame->height);
889-
torch::Tensor tensor = torch::empty(
891+
auto tmp = torch::empty(
890892
{height, width, 3}, torch::TensorOptions().dtype({torch::kUInt8}));
891-
rawOutput.data = tensor.data_ptr<uint8_t>();
893+
tensor = &(tmp);
894+
rawOutput.data = tensor->data_ptr<uint8_t>();
892895
convertFrameToBufferUsingSwsScale(rawOutput);
893896

894897
if (streamInfo.options.dimensionOrder == "NCHW") {
895-
tensor = tensor.permute({2, 0, 1});
898+
auto tmp = tensor->permute({2, 0, 1});
899+
tensor = &(tmp);
896900
}
897-
output.frame = tensor;
901+
output.frame = *tensor;
898902
} else if (
899903
streamInfo.colorConversionLibrary ==
900904
ColorConversionLibrary::FILTERGRAPH) {

src/torchcodec/decoders/_core/VideoDecoder.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,11 @@ class VideoDecoder {
366366
DecodedOutput convertAVFrameToDecodedOutput(RawDecodedOutput& rawOutput);
367367
void convertAVFrameToDecodedOutputOnCPU(
368368
RawDecodedOutput& rawOutput,
369-
DecodedOutput& output);
369+
DecodedOutput& output,
370+
// TODO: Unable to use std::optional<torch::Tensor>& tensor = std::nullopt
371+
// on a non-const tensor :( ?
372+
torch::Tensor* tensor = nullptr
373+
);
370374

371375
DecoderOptions options_;
372376
ContainerMetadata containerMetadata_;

0 commit comments

Comments
 (0)