From 4197b1e2efab5d84441e26641206253eadad6e57 Mon Sep 17 00:00:00 2001 From: Scott Schneider Date: Mon, 27 Jan 2025 17:43:01 -0800 Subject: [PATCH] Remove createFrom functions in C++ --- src/torchcodec/decoders/_core/VideoDecoder.cpp | 15 --------------- src/torchcodec/decoders/_core/VideoDecoder.h | 7 ++----- src/torchcodec/decoders/_core/VideoDecoderOps.cpp | 10 +++++----- test/decoders/VideoDecoderTest.cpp | 13 +++++-------- 4 files changed, 12 insertions(+), 33 deletions(-) diff --git a/src/torchcodec/decoders/_core/VideoDecoder.cpp b/src/torchcodec/decoders/_core/VideoDecoder.cpp index bba8b4e4a..b773e0188 100644 --- a/src/torchcodec/decoders/_core/VideoDecoder.cpp +++ b/src/torchcodec/decoders/_core/VideoDecoder.cpp @@ -318,21 +318,6 @@ void VideoDecoder::initializeDecoder() { initialized_ = true; } -std::unique_ptr VideoDecoder::createFromFilePath( - const std::string& videoFilePath, - SeekMode seekMode) { - return std::unique_ptr( - new VideoDecoder(videoFilePath, seekMode)); -} - -std::unique_ptr VideoDecoder::createFromBuffer( - const void* buffer, - size_t length, - SeekMode seekMode) { - return std::unique_ptr( - new VideoDecoder(buffer, length, seekMode)); -} - void VideoDecoder::createFilterGraph( StreamInfo& streamInfo, int expectedOutputHeight, diff --git a/src/torchcodec/decoders/_core/VideoDecoder.h b/src/torchcodec/decoders/_core/VideoDecoder.h index 760b534df..0d4bfb1c7 100644 --- a/src/torchcodec/decoders/_core/VideoDecoder.h +++ b/src/torchcodec/decoders/_core/VideoDecoder.h @@ -29,17 +29,14 @@ class VideoDecoder { enum class SeekMode { exact, approximate }; - explicit VideoDecoder(const std::string& videoFilePath, SeekMode seekMode); - explicit VideoDecoder(const void* buffer, size_t length, SeekMode seekMode); - // Creates a VideoDecoder from the video at videoFilePath. - static std::unique_ptr createFromFilePath( + explicit VideoDecoder( const std::string& videoFilePath, SeekMode seekMode = SeekMode::exact); // Creates a VideoDecoder from a given buffer. Note that the buffer is not // owned by the VideoDecoder. - static std::unique_ptr createFromBuffer( + explicit VideoDecoder( const void* buffer, size_t length, SeekMode seekMode = SeekMode::exact); diff --git a/src/torchcodec/decoders/_core/VideoDecoderOps.cpp b/src/torchcodec/decoders/_core/VideoDecoderOps.cpp index e46a5066f..05bc903bf 100644 --- a/src/torchcodec/decoders/_core/VideoDecoderOps.cpp +++ b/src/torchcodec/decoders/_core/VideoDecoderOps.cpp @@ -117,7 +117,7 @@ at::Tensor create_from_file( } std::unique_ptr uniqueDecoder = - VideoDecoder::createFromFilePath(filenameStr, realSeek); + std::make_unique(filenameStr, realSeek); return wrapDecoderPointerToTensor(std::move(uniqueDecoder)); } @@ -134,9 +134,9 @@ at::Tensor create_from_tensor( realSeek = seekModeFromString(seek_mode.value()); } - std::unique_ptr videoDecoder = - VideoDecoder::createFromBuffer(buffer, length, realSeek); - return wrapDecoderPointerToTensor(std::move(videoDecoder)); + std::unique_ptr uniqueDecoder = + std::make_unique(buffer, length, realSeek); + return wrapDecoderPointerToTensor(std::move(uniqueDecoder)); } at::Tensor create_from_buffer( @@ -149,7 +149,7 @@ at::Tensor create_from_buffer( } std::unique_ptr uniqueDecoder = - VideoDecoder::createFromBuffer(buffer, length, realSeek); + std::make_unique(buffer, length, realSeek); return wrapDecoderPointerToTensor(std::move(uniqueDecoder)); } diff --git a/test/decoders/VideoDecoderTest.cpp b/test/decoders/VideoDecoderTest.cpp index 2c7d2d89c..c297bb7b5 100644 --- a/test/decoders/VideoDecoderTest.cpp +++ b/test/decoders/VideoDecoderTest.cpp @@ -50,10 +50,10 @@ class VideoDecoderTest : public testing::TestWithParam { content_ = outputStringStream.str(); void* buffer = content_.data(); size_t length = outputStringStream.str().length(); - return VideoDecoder::createFromBuffer( + return std::make_unique( buffer, length, VideoDecoder::SeekMode::approximate); } else { - return VideoDecoder::createFromFilePath( + return std::make_unique( filepath, VideoDecoder::SeekMode::approximate); } } @@ -94,8 +94,7 @@ TEST_P(VideoDecoderTest, ReturnsFpsAndDurationForVideoInMetadata) { TEST(VideoDecoderTest, MissingVideoFileThrowsException) { EXPECT_THROW( - VideoDecoder::createFromFilePath("/this/file/does/not/exist"), - std::invalid_argument); + VideoDecoder("/this/file/does/not/exist"), std::invalid_argument); } void dumpTensorToDisk( @@ -145,8 +144,7 @@ double computeAverageCosineSimilarity( TEST(VideoDecoderTest, RespectsWidthAndHeightFromOptions) { std::string path = getResourcePath("nasa_13013.mp4"); - std::unique_ptr decoder = - VideoDecoder::createFromFilePath(path); + std::unique_ptr decoder = std::make_unique(path); VideoDecoder::VideoStreamOptions videoStreamOptions; videoStreamOptions.width = 100; videoStreamOptions.height = 120; @@ -157,8 +155,7 @@ TEST(VideoDecoderTest, RespectsWidthAndHeightFromOptions) { TEST(VideoDecoderTest, RespectsOutputTensorDimensionOrderFromOptions) { std::string path = getResourcePath("nasa_13013.mp4"); - std::unique_ptr decoder = - VideoDecoder::createFromFilePath(path); + std::unique_ptr decoder = std::make_unique(path); VideoDecoder::VideoStreamOptions videoStreamOptions; videoStreamOptions.dimensionOrder = "NHWC"; decoder->addVideoStreamDecoder(-1, videoStreamOptions);