Skip to content

Commit 26e6203

Browse files
committed
Use FrameDims class instead of tuple
1 parent 2cd2059 commit 26e6203

File tree

3 files changed

+30
-26
lines changed

3 files changed

+30
-26
lines changed

src/torchcodec/decoders/_core/CudaDevice.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,9 @@ void convertAVFrameToDecodedOutputOnCuda(
197197
src->format == AV_PIX_FMT_CUDA,
198198
"Expected format to be AV_PIX_FMT_CUDA, got " +
199199
std::string(av_get_pix_fmt_name((AVPixelFormat)src->format)));
200-
int height = 0, width = 0;
201-
std::tie(height, width) =
202-
getHeightAndWidthFromOptionsOrMetadata(options, metadata);
200+
auto frameDims = getHeightAndWidthFromOptionsOrMetadata(options, metadata);
201+
int height = frameDims.height;
202+
int width = frameDims.width;
203203
NppiSize oSizeROI = {width, height};
204204
Npp8u* input[2] = {src->data[0], src->data[1]};
205205
torch::Tensor& dst = output.frame;

src/torchcodec/decoders/_core/VideoDecoder.cpp

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,9 @@ VideoDecoder::BatchDecodedOutput::BatchDecodedOutput(
197197
const StreamMetadata& metadata)
198198
: ptsSeconds(torch::empty({numFrames}, {torch::kFloat64})),
199199
durationSeconds(torch::empty({numFrames}, {torch::kFloat64})) {
200-
int height = 0;
201-
int width = 0;
202-
std::tie(height, width) =
203-
getHeightAndWidthFromOptionsOrMetadata(options, metadata);
200+
auto frameDims = getHeightAndWidthFromOptionsOrMetadata(options, metadata);
201+
int height = frameDims.height;
202+
int width = frameDims.width;
204203
frames = allocateEmptyHWCTensor(height, width, options.device, numFrames);
205204
}
206205

@@ -364,10 +363,10 @@ void VideoDecoder::initializeFilterGraphForStream(
364363
inputs->pad_idx = 0;
365364
inputs->next = nullptr;
366365
char description[512];
367-
int height = 0;
368-
int width = 0;
369-
std::tie(height, width) = getHeightAndWidthFromOptionsOrMetadata(
366+
auto frameDims = getHeightAndWidthFromOptionsOrMetadata(
370367
options, containerMetadata_.streams[streamIndex]);
368+
int height = frameDims.height;
369+
int width = frameDims.width;
371370

372371
std::snprintf(
373372
description,
@@ -898,10 +897,10 @@ void VideoDecoder::convertAVFrameToDecodedOutputOnCPU(
898897
torch::Tensor tensor;
899898
if (output.streamType == AVMEDIA_TYPE_VIDEO) {
900899
if (streamInfo.colorConversionLibrary == ColorConversionLibrary::SWSCALE) {
901-
int height = 0;
902-
int width = 0;
903-
std::tie(height, width) =
900+
auto frameDims =
904901
getHeightAndWidthFromOptionsOrAVFrame(streamInfo.options, *frame);
902+
int height = frameDims.height;
903+
int width = frameDims.width;
905904
if (preAllocatedOutputTensor.has_value()) {
906905
tensor = preAllocatedOutputTensor.value();
907906
auto shape = tensor.sizes();
@@ -1315,10 +1314,10 @@ void VideoDecoder::convertFrameToBufferUsingSwsScale(
13151314
enum AVPixelFormat frameFormat =
13161315
static_cast<enum AVPixelFormat>(frame->format);
13171316
StreamInfo& activeStream = streams_[streamIndex];
1318-
int outputHeight = 0;
1319-
int outputWidth = 0;
1320-
std::tie(outputHeight, outputWidth) =
1317+
auto frameDims =
13211318
getHeightAndWidthFromOptionsOrAVFrame(activeStream.options, *frame);
1319+
int outputHeight = frameDims.height;
1320+
int outputWidth = frameDims.width;
13221321
if (activeStream.swsContext.get() == nullptr) {
13231322
SwsContext* swsContext = sws_getContext(
13241323
frame->width,
@@ -1384,12 +1383,11 @@ torch::Tensor VideoDecoder::convertFrameToTensorUsingFilterGraph(
13841383
ffmpegStatus =
13851384
av_buffersink_get_frame(filterState.sinkContext, filteredFrame.get());
13861385
TORCH_CHECK_EQ(filteredFrame->format, AV_PIX_FMT_RGB24);
1387-
int height = 0;
1388-
int width = 0;
1389-
std::tie(height, width) = getHeightAndWidthFromOptionsOrAVFrame(
1386+
auto frameDims = getHeightAndWidthFromOptionsOrAVFrame(
13901387
streams_[streamIndex].options, *filteredFrame.get());
1388+
int height = frameDims.height;
1389+
int width = frameDims.width;
13911390
std::vector<int64_t> shape = {height, width, 3};
1392-
13931391
std::vector<int64_t> strides = {filteredFrame->linesize[0], 3, 1};
13941392
AVFrame* filteredFramePtr = filteredFrame.release();
13951393
auto deleter = [filteredFramePtr](void*) {
@@ -1413,18 +1411,18 @@ VideoDecoder::~VideoDecoder() {
14131411
}
14141412
}
14151413

1416-
std::tuple<int, int> getHeightAndWidthFromOptionsOrMetadata(
1414+
FrameDims getHeightAndWidthFromOptionsOrMetadata(
14171415
const VideoDecoder::VideoStreamDecoderOptions& options,
14181416
const VideoDecoder::StreamMetadata& metadata) {
1419-
return std::make_tuple(
1417+
return FrameDims(
14201418
options.height.value_or(*metadata.height),
14211419
options.width.value_or(*metadata.width));
14221420
}
14231421

1424-
std::tuple<int, int> getHeightAndWidthFromOptionsOrAVFrame(
1422+
FrameDims getHeightAndWidthFromOptionsOrAVFrame(
14251423
const VideoDecoder::VideoStreamDecoderOptions& options,
14261424
const AVFrame& avFrame) {
1427-
return std::make_tuple(
1425+
return FrameDims(
14281426
options.height.value_or(avFrame.height),
14291427
options.width.value_or(avFrame.width));
14301428
}

src/torchcodec/decoders/_core/VideoDecoder.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,11 +457,17 @@ class VideoDecoder {
457457
// it is very important to check the height and width assumptions where the
458458
// tensors memory is used/filled in order to avoid segfaults.
459459

460-
std::tuple<int, int> getHeightAndWidthFromOptionsOrMetadata(
460+
struct FrameDims {
461+
int height;
462+
int width;
463+
FrameDims(int h, int w) : height(h), width(w) {}
464+
};
465+
466+
FrameDims getHeightAndWidthFromOptionsOrMetadata(
461467
const VideoDecoder::VideoStreamDecoderOptions& options,
462468
const VideoDecoder::StreamMetadata& metadata);
463469

464-
std::tuple<int, int> getHeightAndWidthFromOptionsOrAVFrame(
470+
FrameDims getHeightAndWidthFromOptionsOrAVFrame(
465471
const VideoDecoder::VideoStreamDecoderOptions& options,
466472
const AVFrame& avFrame);
467473

0 commit comments

Comments
 (0)