Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose constructors in C++ #434

Merged
merged 3 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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!");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this TORCH_CHECK was almost certainly optimized out in optimized builds. Because we already used buffer, it being null at this point would be undefined behavior, and the C++ compiler is allowed to assume it won't happen. Null checks must always be before use.

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
Loading