From 25150a6d16d5cbed594a78612bf63efbdb84055c Mon Sep 17 00:00:00 2001 From: Scott Schneider Date: Fri, 13 Dec 2024 17:20:27 -0800 Subject: [PATCH 1/3] Expose constructors in C++ --- .../decoders/_core/VideoDecoder.cpp | 36 +++++++++---------- src/torchcodec/decoders/_core/VideoDecoder.h | 16 +++------ 2 files changed, 22 insertions(+), 30 deletions(-) diff --git a/src/torchcodec/decoders/_core/VideoDecoder.cpp b/src/torchcodec/decoders/_core/VideoDecoder.cpp index ae951b91..dec3077e 100644 --- a/src/torchcodec/decoders/_core/VideoDecoder.cpp +++ b/src/torchcodec/decoders/_core/VideoDecoder.cpp @@ -216,7 +216,19 @@ bool VideoDecoder::SwsContextKey::operator!=( return !(*this == other); } -VideoDecoder::VideoDecoder() {} +VideoDecoder::VideoDecoder(const std::string& videoFilePath) { + AVInput input = createAVFormatContextFromFilePath(videoFilePath); + formatContext_ = std::move(input.formatContext); + initializeDecoder(); +} + +VideoDecoder::VideoDecoder(const void* buffer, size_t length) { + TORCH_CHECK(buffer != nullptr, "Video buffer cannot be nullptr!"); + AVInput input = createAVFormatContextFromBuffer(buffer, length); + formatContext_ = std::move(input.formatContext); + ioBytesContext_ = std::move(input.ioBytesContext); + initializeDecoder(); +} void VideoDecoder::initializeDecoder() { // Some formats don't store enough info in the header so we read/decode a few @@ -275,28 +287,14 @@ void VideoDecoder::initializeDecoder() { } std::unique_ptr VideoDecoder::createFromFilePath( - const std::string& videoFilePath, - const VideoDecoder::DecoderOptions& options) { - AVInput input = createAVFormatContextFromFilePath(videoFilePath); - std::unique_ptr decoder(new VideoDecoder()); - decoder->formatContext_ = std::move(input.formatContext); - decoder->options_ = options; - decoder->initializeDecoder(); - return decoder; + const std::string& videoFilePath) { + return std::unique_ptr(new VideoDecoder(videoFilePath)); } std::unique_ptr VideoDecoder::createFromBuffer( const void* buffer, - size_t length, - const VideoDecoder::DecoderOptions& options) { - TORCH_CHECK(buffer != nullptr, "Video buffer cannot be nullptr!"); - AVInput input = createAVFormatContextFromBuffer(buffer, length); - std::unique_ptr decoder(new VideoDecoder()); - decoder->formatContext_ = std::move(input.formatContext); - decoder->ioBytesContext_ = std::move(input.ioBytesContext); - decoder->options_ = options; - decoder->initializeDecoder(); - return decoder; + size_t length) { + return std::unique_ptr(new VideoDecoder(buffer, length)); } void VideoDecoder::initializeFilterGraph( diff --git a/src/torchcodec/decoders/_core/VideoDecoder.h b/src/torchcodec/decoders/_core/VideoDecoder.h index 1da745b3..7ac5c819 100644 --- a/src/torchcodec/decoders/_core/VideoDecoder.h +++ b/src/torchcodec/decoders/_core/VideoDecoder.h @@ -46,27 +46,23 @@ class VideoDecoder { public: ~VideoDecoder(); - struct DecoderOptions { - DecoderOptions() {} - // TODO: Add options for the entire decoder here, or remove if not needed. - }; - // -------------------------------------------------------------------------- // CONSTRUCTION API // -------------------------------------------------------------------------- + explicit VideoDecoder(const std::string& videoFilePath); + explicit VideoDecoder(const void* buffer, size_t length); + // Creates a VideoDecoder with the given options for the video in file // `videoFilePath`. If it fails, returns an error status. static std::unique_ptr createFromFilePath( - const std::string& videoFilePath, - const DecoderOptions& options = DecoderOptions()); + const std::string& videoFilePath); // Creates a VideoDecoder from a given buffer. Note that the buffer is not // owned by the VideoDecoder. static std::unique_ptr createFromBuffer( const void* buffer, - size_t length, - const DecoderOptions& options = DecoderOptions()); + size_t length); // -------------------------------------------------------------------------- // VIDEO METADATA QUERY API @@ -349,7 +345,6 @@ class VideoDecoder { SwsContextKey swsContextKey; UniqueSwsContext swsContext; }; - VideoDecoder(); // Returns the key frame index of the presentation timestamp using FFMPEG's // index. Note that this index may be truncated for some files. int getKeyFrameIndexForPtsUsingEncoderIndex(AVStream* stream, int64_t pts) @@ -409,7 +404,6 @@ class VideoDecoder { DecodedOutput getNextFrameOutputNoDemuxInternal( std::optional preAllocatedOutputTensor = std::nullopt); - DecoderOptions options_; ContainerMetadata containerMetadata_; UniqueAVFormatContext formatContext_; std::map streams_; From 0c402548c82bc9874c5c495333bbb04a700aa19d Mon Sep 17 00:00:00 2001 From: Scott Schneider Date: Fri, 13 Dec 2024 17:32:03 -0800 Subject: [PATCH 2/3] Update comments --- src/torchcodec/decoders/_core/VideoDecoder.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/torchcodec/decoders/_core/VideoDecoder.h b/src/torchcodec/decoders/_core/VideoDecoder.h index 7ac5c819..fcd1b17c 100644 --- a/src/torchcodec/decoders/_core/VideoDecoder.h +++ b/src/torchcodec/decoders/_core/VideoDecoder.h @@ -50,16 +50,16 @@ class VideoDecoder { // CONSTRUCTION API // -------------------------------------------------------------------------- + // Creates a VideoDecoder from the video at videoFilePath. explicit VideoDecoder(const std::string& videoFilePath); + + // Creates a VideoDecoder from a given buffer. Note that the buffer is not + // owned by the VideoDecoder. explicit VideoDecoder(const void* buffer, size_t length); - // Creates a VideoDecoder with the given options for the video in file - // `videoFilePath`. If it fails, returns an error status. static std::unique_ptr createFromFilePath( const std::string& videoFilePath); - // Creates a VideoDecoder from a given buffer. Note that the buffer is not - // owned by the VideoDecoder. static std::unique_ptr createFromBuffer( const void* buffer, size_t length); From 0cd56f6dffe0c54d0587e15be3d4b4b4ee40b83e Mon Sep 17 00:00:00 2001 From: Scott Schneider Date: Fri, 13 Dec 2024 17:33:22 -0800 Subject: [PATCH 3/3] Formatting --- src/torchcodec/decoders/_core/VideoDecoder.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/torchcodec/decoders/_core/VideoDecoder.cpp b/src/torchcodec/decoders/_core/VideoDecoder.cpp index dec3077e..4c98980c 100644 --- a/src/torchcodec/decoders/_core/VideoDecoder.cpp +++ b/src/torchcodec/decoders/_core/VideoDecoder.cpp @@ -219,14 +219,17 @@ bool VideoDecoder::SwsContextKey::operator!=( VideoDecoder::VideoDecoder(const std::string& videoFilePath) { AVInput input = createAVFormatContextFromFilePath(videoFilePath); formatContext_ = std::move(input.formatContext); + initializeDecoder(); } VideoDecoder::VideoDecoder(const void* buffer, size_t length) { TORCH_CHECK(buffer != nullptr, "Video buffer cannot be nullptr!"); + AVInput input = createAVFormatContextFromBuffer(buffer, length); formatContext_ = std::move(input.formatContext); ioBytesContext_ = std::move(input.ioBytesContext); + initializeDecoder(); }