From 2bc0f8cddaee61c2b5bf6a055f356b3493218427 Mon Sep 17 00:00:00 2001 From: Scott Schneider Date: Fri, 13 Dec 2024 20:54:45 -0500 Subject: [PATCH] Expose constructors in C++ (#434) --- .../decoders/_core/VideoDecoder.cpp | 39 ++++++++++--------- src/torchcodec/decoders/_core/VideoDecoder.h | 22 ++++------- 2 files changed, 28 insertions(+), 33 deletions(-) diff --git a/src/torchcodec/decoders/_core/VideoDecoder.cpp b/src/torchcodec/decoders/_core/VideoDecoder.cpp index ae951b91..4c98980c 100644 --- a/src/torchcodec/decoders/_core/VideoDecoder.cpp +++ b/src/torchcodec/decoders/_core/VideoDecoder.cpp @@ -216,7 +216,22 @@ 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 +290,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..fcd1b17c 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 // -------------------------------------------------------------------------- - // 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()); + // 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); + + static std::unique_ptr createFromFilePath( + const std::string& videoFilePath); + 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_;