Skip to content

Commit 2bc0f8c

Browse files
authored
Expose constructors in C++ (#434)
1 parent 943dc6e commit 2bc0f8c

File tree

2 files changed

+28
-33
lines changed

2 files changed

+28
-33
lines changed

src/torchcodec/decoders/_core/VideoDecoder.cpp

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,22 @@ bool VideoDecoder::SwsContextKey::operator!=(
216216
return !(*this == other);
217217
}
218218

219-
VideoDecoder::VideoDecoder() {}
219+
VideoDecoder::VideoDecoder(const std::string& videoFilePath) {
220+
AVInput input = createAVFormatContextFromFilePath(videoFilePath);
221+
formatContext_ = std::move(input.formatContext);
222+
223+
initializeDecoder();
224+
}
225+
226+
VideoDecoder::VideoDecoder(const void* buffer, size_t length) {
227+
TORCH_CHECK(buffer != nullptr, "Video buffer cannot be nullptr!");
228+
229+
AVInput input = createAVFormatContextFromBuffer(buffer, length);
230+
formatContext_ = std::move(input.formatContext);
231+
ioBytesContext_ = std::move(input.ioBytesContext);
232+
233+
initializeDecoder();
234+
}
220235

221236
void VideoDecoder::initializeDecoder() {
222237
// Some formats don't store enough info in the header so we read/decode a few
@@ -275,28 +290,14 @@ void VideoDecoder::initializeDecoder() {
275290
}
276291

277292
std::unique_ptr<VideoDecoder> VideoDecoder::createFromFilePath(
278-
const std::string& videoFilePath,
279-
const VideoDecoder::DecoderOptions& options) {
280-
AVInput input = createAVFormatContextFromFilePath(videoFilePath);
281-
std::unique_ptr<VideoDecoder> decoder(new VideoDecoder());
282-
decoder->formatContext_ = std::move(input.formatContext);
283-
decoder->options_ = options;
284-
decoder->initializeDecoder();
285-
return decoder;
293+
const std::string& videoFilePath) {
294+
return std::unique_ptr<VideoDecoder>(new VideoDecoder(videoFilePath));
286295
}
287296

288297
std::unique_ptr<VideoDecoder> VideoDecoder::createFromBuffer(
289298
const void* buffer,
290-
size_t length,
291-
const VideoDecoder::DecoderOptions& options) {
292-
TORCH_CHECK(buffer != nullptr, "Video buffer cannot be nullptr!");
293-
AVInput input = createAVFormatContextFromBuffer(buffer, length);
294-
std::unique_ptr<VideoDecoder> decoder(new VideoDecoder());
295-
decoder->formatContext_ = std::move(input.formatContext);
296-
decoder->ioBytesContext_ = std::move(input.ioBytesContext);
297-
decoder->options_ = options;
298-
decoder->initializeDecoder();
299-
return decoder;
299+
size_t length) {
300+
return std::unique_ptr<VideoDecoder>(new VideoDecoder(buffer, length));
300301
}
301302

302303
void VideoDecoder::initializeFilterGraph(

src/torchcodec/decoders/_core/VideoDecoder.h

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,27 +46,23 @@ class VideoDecoder {
4646
public:
4747
~VideoDecoder();
4848

49-
struct DecoderOptions {
50-
DecoderOptions() {}
51-
// TODO: Add options for the entire decoder here, or remove if not needed.
52-
};
53-
5449
// --------------------------------------------------------------------------
5550
// CONSTRUCTION API
5651
// --------------------------------------------------------------------------
5752

58-
// Creates a VideoDecoder with the given options for the video in file
59-
// `videoFilePath`. If it fails, returns an error status.
60-
static std::unique_ptr<VideoDecoder> createFromFilePath(
61-
const std::string& videoFilePath,
62-
const DecoderOptions& options = DecoderOptions());
53+
// Creates a VideoDecoder from the video at videoFilePath.
54+
explicit VideoDecoder(const std::string& videoFilePath);
6355

6456
// Creates a VideoDecoder from a given buffer. Note that the buffer is not
6557
// owned by the VideoDecoder.
58+
explicit VideoDecoder(const void* buffer, size_t length);
59+
60+
static std::unique_ptr<VideoDecoder> createFromFilePath(
61+
const std::string& videoFilePath);
62+
6663
static std::unique_ptr<VideoDecoder> createFromBuffer(
6764
const void* buffer,
68-
size_t length,
69-
const DecoderOptions& options = DecoderOptions());
65+
size_t length);
7066

7167
// --------------------------------------------------------------------------
7268
// VIDEO METADATA QUERY API
@@ -349,7 +345,6 @@ class VideoDecoder {
349345
SwsContextKey swsContextKey;
350346
UniqueSwsContext swsContext;
351347
};
352-
VideoDecoder();
353348
// Returns the key frame index of the presentation timestamp using FFMPEG's
354349
// index. Note that this index may be truncated for some files.
355350
int getKeyFrameIndexForPtsUsingEncoderIndex(AVStream* stream, int64_t pts)
@@ -409,7 +404,6 @@ class VideoDecoder {
409404
DecodedOutput getNextFrameOutputNoDemuxInternal(
410405
std::optional<torch::Tensor> preAllocatedOutputTensor = std::nullopt);
411406

412-
DecoderOptions options_;
413407
ContainerMetadata containerMetadata_;
414408
UniqueAVFormatContext formatContext_;
415409
std::map<int, StreamInfo> streams_;

0 commit comments

Comments
 (0)