Skip to content

Commit

Permalink
Expose constructors in C++ (#434)
Browse files Browse the repository at this point in the history
  • Loading branch information
scotts authored Dec 14, 2024
1 parent 943dc6e commit 2bc0f8c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 33 deletions.
39 changes: 20 additions & 19 deletions src/torchcodec/decoders/_core/VideoDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -275,28 +290,14 @@ void VideoDecoder::initializeDecoder() {
}

std::unique_ptr<VideoDecoder> VideoDecoder::createFromFilePath(
const std::string& videoFilePath,
const VideoDecoder::DecoderOptions& options) {
AVInput input = createAVFormatContextFromFilePath(videoFilePath);
std::unique_ptr<VideoDecoder> decoder(new VideoDecoder());
decoder->formatContext_ = std::move(input.formatContext);
decoder->options_ = options;
decoder->initializeDecoder();
return decoder;
const std::string& videoFilePath) {
return std::unique_ptr<VideoDecoder>(new VideoDecoder(videoFilePath));
}

std::unique_ptr<VideoDecoder> 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<VideoDecoder> 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<VideoDecoder>(new VideoDecoder(buffer, length));
}

void VideoDecoder::initializeFilterGraph(
Expand Down
22 changes: 8 additions & 14 deletions src/torchcodec/decoders/_core/VideoDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<VideoDecoder> 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<VideoDecoder> createFromFilePath(
const std::string& videoFilePath);

static std::unique_ptr<VideoDecoder> createFromBuffer(
const void* buffer,
size_t length,
const DecoderOptions& options = DecoderOptions());
size_t length);

// --------------------------------------------------------------------------
// VIDEO METADATA QUERY API
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -409,7 +404,6 @@ class VideoDecoder {
DecodedOutput getNextFrameOutputNoDemuxInternal(
std::optional<torch::Tensor> preAllocatedOutputTensor = std::nullopt);

DecoderOptions options_;
ContainerMetadata containerMetadata_;
UniqueAVFormatContext formatContext_;
std::map<int, StreamInfo> streams_;
Expand Down

0 comments on commit 2bc0f8c

Please sign in to comment.