torchcodec 1.0.0__py3-none-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- torchcodec/__init__.py +50 -0
- torchcodec/_core/AVIOContextHolder.cpp +85 -0
- torchcodec/_core/AVIOContextHolder.h +44 -0
- torchcodec/_core/AudioCommon.cpp +101 -0
- torchcodec/_core/AudioCommon.h +48 -0
- torchcodec/_core/AudioConverter.cpp +130 -0
- torchcodec/_core/AudioConverter.h +59 -0
- torchcodec/_core/BetaCudaDeviceInterface.cpp +1635 -0
- torchcodec/_core/BetaCudaDeviceInterface.h +364 -0
- torchcodec/_core/CMakeLists.txt +661 -0
- torchcodec/_core/CUDACommon.cpp +129 -0
- torchcodec/_core/CUDACommon.h +115 -0
- torchcodec/_core/Cache.h +126 -0
- torchcodec/_core/CodecParameters.cpp +98 -0
- torchcodec/_core/CodecParameters.h +59 -0
- torchcodec/_core/ColorConverter.cpp +92 -0
- torchcodec/_core/ColorConverter.h +40 -0
- torchcodec/_core/CpuDeviceInterface.cpp +654 -0
- torchcodec/_core/CpuDeviceInterface.h +153 -0
- torchcodec/_core/CudaDeviceInterface.cpp +519 -0
- torchcodec/_core/CudaDeviceInterface.h +92 -0
- torchcodec/_core/DecodeAvif.cpp +284 -0
- torchcodec/_core/DecodeAvif.h +28 -0
- torchcodec/_core/DecodeGif.cpp +488 -0
- torchcodec/_core/DecodeGif.h +32 -0
- torchcodec/_core/DecodeHeic.cpp +306 -0
- torchcodec/_core/DecodeHeic.h +18 -0
- torchcodec/_core/DecodeJpeg.cpp +489 -0
- torchcodec/_core/DecodeJpeg.h +17 -0
- torchcodec/_core/DecodeJpegCuda.cpp +638 -0
- torchcodec/_core/DecodeJpegCuda.h +92 -0
- torchcodec/_core/DecodePng.cpp +569 -0
- torchcodec/_core/DecodePng.h +31 -0
- torchcodec/_core/DecodeWebp.cpp +238 -0
- torchcodec/_core/DecodeWebp.h +17 -0
- torchcodec/_core/Demuxer.cpp +415 -0
- torchcodec/_core/Demuxer.h +158 -0
- torchcodec/_core/DeviceInterface.cpp +152 -0
- torchcodec/_core/DeviceInterface.h +255 -0
- torchcodec/_core/EncodeJpeg.cpp +223 -0
- torchcodec/_core/EncodeJpeg.h +19 -0
- torchcodec/_core/EncodeJpegCuda.cpp +272 -0
- torchcodec/_core/EncodeJpegCuda.h +56 -0
- torchcodec/_core/EncodePng.cpp +207 -0
- torchcodec/_core/EncodePng.h +19 -0
- torchcodec/_core/Encoder.cpp +1023 -0
- torchcodec/_core/Encoder.h +144 -0
- torchcodec/_core/Exif.h +241 -0
- torchcodec/_core/FFMPEGCommon.cpp +1159 -0
- torchcodec/_core/FFMPEGCommon.h +506 -0
- torchcodec/_core/FileIO.cpp +78 -0
- torchcodec/_core/FileIO.h +34 -0
- torchcodec/_core/FileLikeIO.cpp +76 -0
- torchcodec/_core/FileLikeIO.h +68 -0
- torchcodec/_core/FilterGraph.cpp +211 -0
- torchcodec/_core/FilterGraph.h +59 -0
- torchcodec/_core/Frame.cpp +75 -0
- torchcodec/_core/Frame.h +76 -0
- torchcodec/_core/IOInterface.h +44 -0
- torchcodec/_core/ImageCommon.h +113 -0
- torchcodec/_core/Logging.cpp +42 -0
- torchcodec/_core/Logging.h +38 -0
- torchcodec/_core/Metadata.cpp +312 -0
- torchcodec/_core/Metadata.h +115 -0
- torchcodec/_core/NVCUVIDRuntimeLoader.cpp +324 -0
- torchcodec/_core/NVCUVIDRuntimeLoader.h +14 -0
- torchcodec/_core/NVDECCache.cpp +111 -0
- torchcodec/_core/NVDECCache.h +136 -0
- torchcodec/_core/NVDECCacheConfig.cpp +61 -0
- torchcodec/_core/NVDECCacheConfig.h +30 -0
- torchcodec/_core/PacketDecoder.cpp +358 -0
- torchcodec/_core/PacketDecoder.h +140 -0
- torchcodec/_core/SingleStreamDecoder.cpp +1856 -0
- torchcodec/_core/SingleStreamDecoder.h +424 -0
- torchcodec/_core/StableABICompat.h +213 -0
- torchcodec/_core/StreamOptions.h +93 -0
- torchcodec/_core/SwScale.cpp +124 -0
- torchcodec/_core/SwScale.h +67 -0
- torchcodec/_core/TensorIO.cpp +136 -0
- torchcodec/_core/TensorIO.h +53 -0
- torchcodec/_core/Transform.cpp +207 -0
- torchcodec/_core/Transform.h +125 -0
- torchcodec/_core/ValidationUtils.cpp +65 -0
- torchcodec/_core/ValidationUtils.h +29 -0
- torchcodec/_core/WavDecoder.cpp +494 -0
- torchcodec/_core/WavDecoder.h +73 -0
- torchcodec/_core/__init__.py +45 -0
- torchcodec/_core/_decoder_utils.py +258 -0
- torchcodec/_core/_ffmpeg_op_names.py +95 -0
- torchcodec/_core/_ffmpeg_ops.py +699 -0
- torchcodec/_core/_file_like.py +221 -0
- torchcodec/_core/_metadata.py +402 -0
- torchcodec/_core/color_conversion.cpp +316 -0
- torchcodec/_core/color_conversion.cu +391 -0
- torchcodec/_core/color_conversion.h +150 -0
- torchcodec/_core/custom_ops.cpp +2438 -0
- torchcodec/_core/fetch_and_expose_non_gpl_ffmpeg_libs.cmake +192 -0
- torchcodec/_core/fetch_avif_from_s3.cmake +81 -0
- torchcodec/_core/giflib/README +28 -0
- torchcodec/_core/giflib/dgif_lib.c +1313 -0
- torchcodec/_core/giflib/gif_hash.c +129 -0
- torchcodec/_core/giflib/gif_hash.h +43 -0
- torchcodec/_core/giflib/gif_lib.h +292 -0
- torchcodec/_core/giflib/gif_lib_private.h +73 -0
- torchcodec/_core/giflib/gifalloc.c +426 -0
- torchcodec/_core/giflib/openbsd-reallocarray.c +74 -0
- torchcodec/_core/heic_custom_ops.cpp +20 -0
- torchcodec/_core/image_custom_ops.cpp +137 -0
- torchcodec/_core/nvcuvid_include/cuviddec.h +1374 -0
- torchcodec/_core/nvcuvid_include/nvcuvid.h +610 -0
- torchcodec/_core/ops.py +103 -0
- torchcodec/_core/sources.bzl +113 -0
- torchcodec/_frame.py +146 -0
- torchcodec/_internally_replaced_utils.py +129 -0
- torchcodec/_logging.py +59 -0
- torchcodec/decoders/__init__.py +50 -0
- torchcodec/decoders/_audio_decoder.py +183 -0
- torchcodec/decoders/_blocks/__init__.py +48 -0
- torchcodec/decoders/_blocks/_audio_converter.py +155 -0
- torchcodec/decoders/_blocks/_codec_parameters.py +229 -0
- torchcodec/decoders/_blocks/_color_converter.py +107 -0
- torchcodec/decoders/_blocks/_demuxer.py +678 -0
- torchcodec/decoders/_blocks/_frame.py +577 -0
- torchcodec/decoders/_blocks/_helpers.py +53 -0
- torchcodec/decoders/_blocks/_packet_decoder.py +357 -0
- torchcodec/decoders/_decoder_utils.py +128 -0
- torchcodec/decoders/_image_decoders.py +1575 -0
- torchcodec/decoders/_video_decoder.py +592 -0
- torchcodec/decoders/_wav_decoder.py +131 -0
- torchcodec/encoders/__init__.py +4 -0
- torchcodec/encoders/_audio_encoder.py +167 -0
- torchcodec/encoders/_image_encoders.py +181 -0
- torchcodec/encoders/_multi_stream_encoder.py +272 -0
- torchcodec/encoders/_video_encoder.py +227 -0
- torchcodec/jpeg8.dll +0 -0
- torchcodec/libavif.dll +0 -0
- torchcodec/libpng16.dll +0 -0
- torchcodec/libsharpyuv.dll +0 -0
- torchcodec/libtorchcodec_core4.dll +0 -0
- torchcodec/libtorchcodec_core5.dll +0 -0
- torchcodec/libtorchcodec_core6.dll +0 -0
- torchcodec/libtorchcodec_core7.dll +0 -0
- torchcodec/libtorchcodec_core8.dll +0 -0
- torchcodec/libtorchcodec_core9.dll +0 -0
- torchcodec/libtorchcodec_custom_ops4.dll +0 -0
- torchcodec/libtorchcodec_custom_ops5.dll +0 -0
- torchcodec/libtorchcodec_custom_ops6.dll +0 -0
- torchcodec/libtorchcodec_custom_ops7.dll +0 -0
- torchcodec/libtorchcodec_custom_ops8.dll +0 -0
- torchcodec/libtorchcodec_custom_ops9.dll +0 -0
- torchcodec/libtorchcodec_heic.dll +0 -0
- torchcodec/libtorchcodec_image.dll +0 -0
- torchcodec/libwebp.dll +0 -0
- torchcodec/libwebpdecoder.dll +0 -0
- torchcodec/libwebpdemux.dll +0 -0
- torchcodec/libwebpmux.dll +0 -0
- torchcodec/libzstd.dll +0 -0
- torchcodec/py.typed +0 -0
- torchcodec/samplers/__init__.py +2 -0
- torchcodec/samplers/_common.py +83 -0
- torchcodec/samplers/_index_based.py +287 -0
- torchcodec/samplers/_time_based.py +360 -0
- torchcodec/share/cmake/TorchCodec/TorchCodecConfig.cmake +76 -0
- torchcodec/share/cmake/TorchCodec/ffmpeg_versions.cmake +128 -0
- torchcodec/transforms/__init__.py +12 -0
- torchcodec/transforms/_decoder_transforms.py +375 -0
- torchcodec/version.py +1 -0
- torchcodec/zlib.dll +0 -0
- torchcodec-1.0.0.dist-info/METADATA +275 -0
- torchcodec-1.0.0.dist-info/RECORD +179 -0
- torchcodec-1.0.0.dist-info/WHEEL +5 -0
- torchcodec-1.0.0.dist-info/licenses/LICENSE +28 -0
- torchcodec-1.0.0.dist-info/licenses/third_party/COPYING.dav1d +23 -0
- torchcodec-1.0.0.dist-info/licenses/third_party/LICENSE.libavif +387 -0
- torchcodec-1.0.0.dist-info/licenses/third_party/LICENSE.libjpeg-turbo +135 -0
- torchcodec-1.0.0.dist-info/licenses/third_party/LICENSE.libpng +134 -0
- torchcodec-1.0.0.dist-info/licenses/third_party/LICENSE.libwebp +30 -0
- torchcodec-1.0.0.dist-info/licenses/third_party/LICENSE.libyuv +29 -0
- torchcodec-1.0.0.dist-info/licenses/third_party/LICENSE.zlib +22 -0
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
// All rights reserved.
|
|
3
|
+
//
|
|
4
|
+
// This source code is licensed under the BSD-style license found in the
|
|
5
|
+
// LICENSE file in the root directory of this source tree.
|
|
6
|
+
|
|
7
|
+
#pragma once
|
|
8
|
+
|
|
9
|
+
#include <memory>
|
|
10
|
+
|
|
11
|
+
#include "IOInterface.h"
|
|
12
|
+
|
|
13
|
+
namespace facebook::torchcodec {
|
|
14
|
+
|
|
15
|
+
// C callbacks into a Python file-like object. Built on the Python side with
|
|
16
|
+
// ctypes, so that no torchcodec library depends on the CPython C API. See
|
|
17
|
+
// Note [File-like objects through ctypes] in torchcodec/_core/_file_like.py,
|
|
18
|
+
// which also mirrors this layout. ctypes callbacks attach the calling thread to
|
|
19
|
+
// the interpreter and take the GIL themselves, so they're safe to call from ops
|
|
20
|
+
// that run with the GIL released.
|
|
21
|
+
//
|
|
22
|
+
// read/write/seek return kFileLikeCallbackError if the Python call raised;
|
|
23
|
+
// get_error then writes the Python error message into buf (truncated to
|
|
24
|
+
// buf_len) and returns the number of bytes written. release() drops the Python
|
|
25
|
+
// side's reference to the file-like object and must be called exactly once.
|
|
26
|
+
struct FileLikeCallbacks {
|
|
27
|
+
// Python id() of the _FileLikeContext holding the file-like object on the
|
|
28
|
+
// Python side. Never dereferenced: it's only passed back to every callback,
|
|
29
|
+
// so that the shared Python callbacks know which file-like object to use.
|
|
30
|
+
int64_t id_;
|
|
31
|
+
int64_t (*read)(int64_t id_, uint8_t* buf, int64_t size);
|
|
32
|
+
int64_t (*write)(int64_t id_, const uint8_t* buf, int64_t size);
|
|
33
|
+
int64_t (*seek)(int64_t id_, int64_t offset, int64_t whence);
|
|
34
|
+
void (*release)(int64_t id_);
|
|
35
|
+
int64_t (*get_error)(int64_t id_, char* buf, int64_t buf_len);
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
inline constexpr int64_t kFileLikeCallbackError = INT64_MIN;
|
|
39
|
+
|
|
40
|
+
// FFmpeg-free bridge to a Python file-like object. Wrap it in an
|
|
41
|
+
// AVIOContextHolder to feed FFmpeg, or use it directly (e.g. the image
|
|
42
|
+
// encoders write straight through it).
|
|
43
|
+
class FileLikeIO : public IOInterface {
|
|
44
|
+
public:
|
|
45
|
+
explicit FileLikeIO(const FileLikeCallbacks& callbacks);
|
|
46
|
+
~FileLikeIO() override;
|
|
47
|
+
|
|
48
|
+
FileLikeIO(const FileLikeIO&) = delete;
|
|
49
|
+
FileLikeIO& operator=(const FileLikeIO&) = delete;
|
|
50
|
+
|
|
51
|
+
int read(uint8_t* buf, int size) override;
|
|
52
|
+
int write(const uint8_t* buf, int size) override;
|
|
53
|
+
int64_t seek(int64_t offset, int whence) override;
|
|
54
|
+
int64_t get_size() override;
|
|
55
|
+
|
|
56
|
+
private:
|
|
57
|
+
void check_callback_result(int64_t result, const char* method);
|
|
58
|
+
|
|
59
|
+
FileLikeCallbacks callbacks_;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
// file_like_context is the address of a FileLikeCallbacks struct, as returned
|
|
63
|
+
// by create_file_like_context() on the Python side. Call this before doing
|
|
64
|
+
// anything else in an op, so that release() is guaranteed to run even if the
|
|
65
|
+
// op fails.
|
|
66
|
+
std::unique_ptr<IOInterface> adopt_file_like_context(int64_t file_like_context);
|
|
67
|
+
|
|
68
|
+
} // namespace facebook::torchcodec
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
// All rights reserved.
|
|
3
|
+
//
|
|
4
|
+
// This source code is licensed under the BSD-style license found in the
|
|
5
|
+
// LICENSE file in the root directory of this source tree.
|
|
6
|
+
|
|
7
|
+
#include "FilterGraph.h"
|
|
8
|
+
#include "FFMPEGCommon.h"
|
|
9
|
+
#include "StableABICompat.h"
|
|
10
|
+
|
|
11
|
+
extern "C" {
|
|
12
|
+
#include <libavfilter/buffersink.h>
|
|
13
|
+
#include <libavfilter/buffersrc.h>
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
namespace facebook::torchcodec {
|
|
17
|
+
|
|
18
|
+
FiltersConfig::FiltersConfig(
|
|
19
|
+
int input_width,
|
|
20
|
+
int input_height,
|
|
21
|
+
AVPixelFormat input_format,
|
|
22
|
+
AVRational input_aspect_ratio,
|
|
23
|
+
int output_width,
|
|
24
|
+
int output_height,
|
|
25
|
+
AVPixelFormat output_format,
|
|
26
|
+
const std::string& filtergraph_str,
|
|
27
|
+
AVRational time_base,
|
|
28
|
+
AVBufferRef* hw_frames_ctx)
|
|
29
|
+
: input_width(input_width),
|
|
30
|
+
input_height(input_height),
|
|
31
|
+
input_format(input_format),
|
|
32
|
+
input_aspect_ratio(input_aspect_ratio),
|
|
33
|
+
output_width(output_width),
|
|
34
|
+
output_height(output_height),
|
|
35
|
+
output_format(output_format),
|
|
36
|
+
filtergraph_str(filtergraph_str),
|
|
37
|
+
time_base(time_base),
|
|
38
|
+
hw_frames_ctx(hw_frames_ctx) {}
|
|
39
|
+
|
|
40
|
+
bool operator==(const AVRational& lhs, const AVRational& rhs) {
|
|
41
|
+
return lhs.num == rhs.num && lhs.den == rhs.den;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
bool FiltersConfig::operator==(const FiltersConfig& other) const {
|
|
45
|
+
return input_width == other.input_width &&
|
|
46
|
+
input_height == other.input_height &&
|
|
47
|
+
input_format == other.input_format &&
|
|
48
|
+
output_width == other.output_width &&
|
|
49
|
+
output_height == other.output_height &&
|
|
50
|
+
output_format == other.output_format &&
|
|
51
|
+
filtergraph_str == other.filtergraph_str &&
|
|
52
|
+
time_base == other.time_base &&
|
|
53
|
+
hw_frames_ctx.get() == other.hw_frames_ctx.get();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
bool FiltersConfig::operator!=(const FiltersConfig& other) const {
|
|
57
|
+
return !(*this == other);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
FilterGraph::FilterGraph(
|
|
61
|
+
const FiltersConfig& filters_config,
|
|
62
|
+
const VideoStreamOptions& video_stream_options) {
|
|
63
|
+
filter_graph_.reset(avfilter_graph_alloc());
|
|
64
|
+
STD_TORCH_CHECK(
|
|
65
|
+
filter_graph_.get() != nullptr, "Failed to allocate filter graph");
|
|
66
|
+
|
|
67
|
+
if (video_stream_options.ffmpeg_thread_count.has_value()) {
|
|
68
|
+
filter_graph_->nb_threads =
|
|
69
|
+
video_stream_options.ffmpeg_thread_count.value();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Configure the source context.
|
|
73
|
+
const AVFilter* buffer_src = avfilter_get_by_name("buffer");
|
|
74
|
+
UniqueAVBufferSrcParameters src_params(av_buffersrc_parameters_alloc());
|
|
75
|
+
STD_TORCH_CHECK(src_params, "Failed to allocate buffersrc params");
|
|
76
|
+
|
|
77
|
+
src_params->format = filters_config.input_format;
|
|
78
|
+
src_params->width = filters_config.input_width;
|
|
79
|
+
src_params->height = filters_config.input_height;
|
|
80
|
+
src_params->sample_aspect_ratio = filters_config.input_aspect_ratio;
|
|
81
|
+
src_params->time_base = filters_config.time_base;
|
|
82
|
+
if (filters_config.hw_frames_ctx) {
|
|
83
|
+
src_params->hw_frames_ctx =
|
|
84
|
+
av_buffer_ref(filters_config.hw_frames_ctx.get());
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
source_context_ =
|
|
88
|
+
avfilter_graph_alloc_filter(filter_graph_.get(), buffer_src, "in");
|
|
89
|
+
STD_TORCH_CHECK(source_context_, "Failed to allocate filter graph");
|
|
90
|
+
|
|
91
|
+
int status = av_buffersrc_parameters_set(source_context_, src_params.get());
|
|
92
|
+
STD_TORCH_CHECK(
|
|
93
|
+
status >= 0,
|
|
94
|
+
"Failed to create filter graph: ",
|
|
95
|
+
get_ffmpeg_error_string_from_error_code(status));
|
|
96
|
+
|
|
97
|
+
status = avfilter_init_str(source_context_, nullptr);
|
|
98
|
+
STD_TORCH_CHECK(
|
|
99
|
+
status >= 0,
|
|
100
|
+
"Failed to create filter graph : ",
|
|
101
|
+
get_ffmpeg_error_string_from_error_code(status));
|
|
102
|
+
|
|
103
|
+
// Configure the sink context.
|
|
104
|
+
const AVFilter* buffer_sink = avfilter_get_by_name("buffersink");
|
|
105
|
+
STD_TORCH_CHECK(buffer_sink != nullptr, "Failed to get buffersink filter.");
|
|
106
|
+
|
|
107
|
+
sink_context_ = create_av_filter_context_with_options(
|
|
108
|
+
filter_graph_.get(), buffer_sink, filters_config.output_format);
|
|
109
|
+
STD_TORCH_CHECK(
|
|
110
|
+
sink_context_ != nullptr, "Failed to create and configure buffersink");
|
|
111
|
+
|
|
112
|
+
// Create the filtergraph nodes based on the source and sink contexts.
|
|
113
|
+
UniqueAVFilterInOut outputs(avfilter_inout_alloc());
|
|
114
|
+
outputs->name = av_strdup("in");
|
|
115
|
+
outputs->filter_ctx = source_context_;
|
|
116
|
+
outputs->pad_idx = 0;
|
|
117
|
+
outputs->next = nullptr;
|
|
118
|
+
|
|
119
|
+
UniqueAVFilterInOut inputs(avfilter_inout_alloc());
|
|
120
|
+
inputs->name = av_strdup("out");
|
|
121
|
+
inputs->filter_ctx = sink_context_;
|
|
122
|
+
inputs->pad_idx = 0;
|
|
123
|
+
inputs->next = nullptr;
|
|
124
|
+
|
|
125
|
+
// Create the filtergraph specified by the filtergraph string in the context
|
|
126
|
+
// of the inputs and outputs. Note the dance we have to do with release and
|
|
127
|
+
// resetting the output and input nodes because FFmpeg modifies them in place.
|
|
128
|
+
AVFilterInOut* outputs_tmp = outputs.release();
|
|
129
|
+
AVFilterInOut* inputs_tmp = inputs.release();
|
|
130
|
+
status = avfilter_graph_parse_ptr(
|
|
131
|
+
filter_graph_.get(),
|
|
132
|
+
filters_config.filtergraph_str.c_str(),
|
|
133
|
+
&inputs_tmp,
|
|
134
|
+
&outputs_tmp,
|
|
135
|
+
nullptr);
|
|
136
|
+
outputs.reset(outputs_tmp);
|
|
137
|
+
inputs.reset(inputs_tmp);
|
|
138
|
+
STD_TORCH_CHECK(
|
|
139
|
+
status >= 0,
|
|
140
|
+
"Failed to parse filter description: ",
|
|
141
|
+
get_ffmpeg_error_string_from_error_code(status),
|
|
142
|
+
", provided filters: " + filters_config.filtergraph_str);
|
|
143
|
+
|
|
144
|
+
// Check filtergraph validity and configure links and formats.
|
|
145
|
+
status = avfilter_graph_config(filter_graph_.get(), nullptr);
|
|
146
|
+
STD_TORCH_CHECK(
|
|
147
|
+
status >= 0,
|
|
148
|
+
"Failed to configure filter graph: ",
|
|
149
|
+
get_ffmpeg_error_string_from_error_code(status),
|
|
150
|
+
", provided filters: " + filters_config.filtergraph_str);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
namespace {
|
|
154
|
+
|
|
155
|
+
bool is_reserved(AVColorPrimaries primaries) {
|
|
156
|
+
return primaries == AVCOL_PRI_RESERVED0 || primaries == AVCOL_PRI_RESERVED ||
|
|
157
|
+
av_color_primaries_name(primaries) == nullptr;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
bool is_reserved(AVColorTransferCharacteristic trc) {
|
|
161
|
+
return trc == AVCOL_TRC_RESERVED0 || trc == AVCOL_TRC_RESERVED ||
|
|
162
|
+
av_color_transfer_name(trc) == nullptr;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
bool is_reserved(AVColorSpace colorspace) {
|
|
166
|
+
return colorspace == AVCOL_SPC_RESERVED ||
|
|
167
|
+
av_color_space_name(colorspace) == nullptr;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
} // namespace
|
|
171
|
+
|
|
172
|
+
UniqueAVFrame FilterGraph::convert(const AVFrame& av_frame) {
|
|
173
|
+
// From FFmpeg >= 8, swscale (used by the scale filter) rejects frames with
|
|
174
|
+
// reserved color properties. FFmpeg < 8 ignores them. A 'reserved' value is
|
|
175
|
+
// argulably a bad encoding. We just preserve the FFmpeg < 8 behavior by
|
|
176
|
+
// setting them to unspecified, which ignores them.
|
|
177
|
+
const AVFrame* input_frame = &av_frame;
|
|
178
|
+
UniqueAVFrame sanitized_frame;
|
|
179
|
+
if (is_reserved(av_frame.color_primaries) ||
|
|
180
|
+
is_reserved(av_frame.color_trc) || is_reserved(av_frame.colorspace)) {
|
|
181
|
+
sanitized_frame.reset(av_frame_clone(&av_frame));
|
|
182
|
+
STD_TORCH_CHECK(sanitized_frame != nullptr, "Failed to clone frame");
|
|
183
|
+
if (is_reserved(sanitized_frame->color_primaries)) {
|
|
184
|
+
sanitized_frame->color_primaries = AVCOL_PRI_UNSPECIFIED;
|
|
185
|
+
}
|
|
186
|
+
if (is_reserved(sanitized_frame->color_trc)) {
|
|
187
|
+
sanitized_frame->color_trc = AVCOL_TRC_UNSPECIFIED;
|
|
188
|
+
}
|
|
189
|
+
if (is_reserved(sanitized_frame->colorspace)) {
|
|
190
|
+
sanitized_frame->colorspace = AVCOL_SPC_UNSPECIFIED;
|
|
191
|
+
}
|
|
192
|
+
input_frame = sanitized_frame.get();
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
int status = av_buffersrc_write_frame(source_context_, input_frame);
|
|
196
|
+
STD_TORCH_CHECK(
|
|
197
|
+
status >= AVSUCCESS,
|
|
198
|
+
"Failed to add frame to buffer source context: ",
|
|
199
|
+
get_ffmpeg_error_string_from_error_code(status));
|
|
200
|
+
|
|
201
|
+
UniqueAVFrame filtered_av_frame(av_frame_alloc());
|
|
202
|
+
status = av_buffersink_get_frame(sink_context_, filtered_av_frame.get());
|
|
203
|
+
STD_TORCH_CHECK(
|
|
204
|
+
status >= AVSUCCESS,
|
|
205
|
+
"Failed to get frame from buffer sink context: ",
|
|
206
|
+
get_ffmpeg_error_string_from_error_code(status));
|
|
207
|
+
|
|
208
|
+
return filtered_av_frame;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
} // namespace facebook::torchcodec
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
// All rights reserved.
|
|
3
|
+
//
|
|
4
|
+
// This source code is licensed under the BSD-style license found in the
|
|
5
|
+
// LICENSE file in the root directory of this source tree.
|
|
6
|
+
|
|
7
|
+
#pragma once
|
|
8
|
+
|
|
9
|
+
#include "FFMPEGCommon.h"
|
|
10
|
+
#include "StreamOptions.h"
|
|
11
|
+
|
|
12
|
+
namespace facebook::torchcodec {
|
|
13
|
+
|
|
14
|
+
struct FiltersConfig {
|
|
15
|
+
int input_width = 0;
|
|
16
|
+
int input_height = 0;
|
|
17
|
+
AVPixelFormat input_format = AV_PIX_FMT_NONE;
|
|
18
|
+
AVRational input_aspect_ratio = {0, 0};
|
|
19
|
+
int output_width = 0;
|
|
20
|
+
int output_height = 0;
|
|
21
|
+
AVPixelFormat output_format = AV_PIX_FMT_NONE;
|
|
22
|
+
std::string filtergraph_str;
|
|
23
|
+
AVRational time_base = {0, 0};
|
|
24
|
+
UniqueAVBufferRef hw_frames_ctx;
|
|
25
|
+
|
|
26
|
+
FiltersConfig() = default;
|
|
27
|
+
FiltersConfig(FiltersConfig&&) = default;
|
|
28
|
+
FiltersConfig& operator=(FiltersConfig&&) = default;
|
|
29
|
+
FiltersConfig(
|
|
30
|
+
int input_width,
|
|
31
|
+
int input_height,
|
|
32
|
+
AVPixelFormat input_format,
|
|
33
|
+
AVRational input_aspect_ratio,
|
|
34
|
+
int output_width,
|
|
35
|
+
int output_height,
|
|
36
|
+
AVPixelFormat output_format,
|
|
37
|
+
const std::string& filtergraph_str,
|
|
38
|
+
AVRational time_base,
|
|
39
|
+
AVBufferRef* hw_frames_ctx = nullptr);
|
|
40
|
+
|
|
41
|
+
bool operator==(const FiltersConfig&) const;
|
|
42
|
+
bool operator!=(const FiltersConfig&) const;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
class FilterGraph {
|
|
46
|
+
public:
|
|
47
|
+
FilterGraph(
|
|
48
|
+
const FiltersConfig& filters_config,
|
|
49
|
+
const VideoStreamOptions& video_stream_options);
|
|
50
|
+
|
|
51
|
+
UniqueAVFrame convert(const AVFrame& av_frame);
|
|
52
|
+
|
|
53
|
+
private:
|
|
54
|
+
UniqueAVFilterGraph filter_graph_;
|
|
55
|
+
AVFilterContext* source_context_ = nullptr;
|
|
56
|
+
AVFilterContext* sink_context_ = nullptr;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
} // namespace facebook::torchcodec
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
// All rights reserved.
|
|
3
|
+
//
|
|
4
|
+
// This source code is licensed under the BSD-style license found in the
|
|
5
|
+
// LICENSE file in the root directory of this source tree.
|
|
6
|
+
|
|
7
|
+
#include "Frame.h"
|
|
8
|
+
#include "StableABICompat.h"
|
|
9
|
+
|
|
10
|
+
namespace facebook::torchcodec {
|
|
11
|
+
|
|
12
|
+
FrameDims::FrameDims(int height, int width) : height(height), width(width) {
|
|
13
|
+
STD_TORCH_CHECK(height > 0, "FrameDims.height must be > 0, got: ", height);
|
|
14
|
+
STD_TORCH_CHECK(width > 0, "FrameDims.width must be > 0, got: ", width);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
FrameBatchOutput::FrameBatchOutput(
|
|
18
|
+
int64_t num_frames,
|
|
19
|
+
const FrameDims& output_dims,
|
|
20
|
+
const StableDevice& device,
|
|
21
|
+
OutputDtype output_dtype)
|
|
22
|
+
: pts_seconds(torch::stable::empty({num_frames}, kStableFloat64)),
|
|
23
|
+
duration_seconds(torch::stable::empty({num_frames}, kStableFloat64)) {
|
|
24
|
+
data =
|
|
25
|
+
allocate_empty_hwc_tensor(output_dims, device, output_dtype, num_frames);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
torch::stable::Tensor allocate_empty_hwc_tensor(
|
|
29
|
+
const FrameDims& frame_dims,
|
|
30
|
+
const StableDevice& device,
|
|
31
|
+
OutputDtype output_dtype,
|
|
32
|
+
std::optional<int> num_frames) {
|
|
33
|
+
STD_TORCH_CHECK(
|
|
34
|
+
frame_dims.height > 0, "height must be > 0, got: ", frame_dims.height);
|
|
35
|
+
STD_TORCH_CHECK(
|
|
36
|
+
frame_dims.width > 0, "width must be > 0, got: ", frame_dims.width);
|
|
37
|
+
auto dtype =
|
|
38
|
+
output_dtype == OutputDtype::FLOAT32 ? kStableUInt16 : kStableUInt8;
|
|
39
|
+
if (num_frames.has_value()) {
|
|
40
|
+
auto num_frames_value = num_frames.value();
|
|
41
|
+
STD_TORCH_CHECK(
|
|
42
|
+
num_frames_value >= 0,
|
|
43
|
+
"numFrames must be >= 0, got: ",
|
|
44
|
+
num_frames_value);
|
|
45
|
+
return torch::stable::empty(
|
|
46
|
+
{num_frames_value, frame_dims.height, frame_dims.width, 3},
|
|
47
|
+
dtype,
|
|
48
|
+
std::nullopt,
|
|
49
|
+
device);
|
|
50
|
+
} else {
|
|
51
|
+
return torch::stable::empty(
|
|
52
|
+
{frame_dims.height, frame_dims.width, 3}, dtype, std::nullopt, device);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
torch::stable::Tensor convert_to_output_dtype(
|
|
57
|
+
const torch::stable::Tensor& tensor,
|
|
58
|
+
OutputDtype output_dtype) {
|
|
59
|
+
bool is_uint16 = tensor.scalar_type() == kStableUInt16;
|
|
60
|
+
|
|
61
|
+
if (output_dtype == OutputDtype::FLOAT32) {
|
|
62
|
+
double max_val = is_uint16 ? 65535.0 : 255.0;
|
|
63
|
+
return stable_div(torch::stable::to(tensor, kStableFloat32), max_val);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (!is_uint16) {
|
|
67
|
+
return tensor;
|
|
68
|
+
}
|
|
69
|
+
// uint16 -> uint8. 257 is 65535 / 255.
|
|
70
|
+
return torch::stable::to(
|
|
71
|
+
stable_div(torch::stable::to(tensor, kStableFloat32), 257.0),
|
|
72
|
+
kStableUInt8);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
} // namespace facebook::torchcodec
|
torchcodec/_core/Frame.h
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
// All rights reserved.
|
|
3
|
+
//
|
|
4
|
+
// This source code is licensed under the BSD-style license found in the
|
|
5
|
+
// LICENSE file in the root directory of this source tree.
|
|
6
|
+
|
|
7
|
+
#pragma once
|
|
8
|
+
|
|
9
|
+
#include "StableABICompat.h"
|
|
10
|
+
#include "StreamOptions.h"
|
|
11
|
+
|
|
12
|
+
namespace facebook::torchcodec {
|
|
13
|
+
|
|
14
|
+
struct FrameDims {
|
|
15
|
+
int height = 0;
|
|
16
|
+
int width = 0;
|
|
17
|
+
|
|
18
|
+
FrameDims() = default;
|
|
19
|
+
|
|
20
|
+
FrameDims(int h, int w);
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
// All public video decoding entry points return either a FrameOutput or a
|
|
24
|
+
// FrameBatchOutput.
|
|
25
|
+
// They are the equivalent of the user-facing Frame and FrameBatch classes in
|
|
26
|
+
// Python. They contain RGB decoded frames along with some associated data
|
|
27
|
+
// like PTS and duration.
|
|
28
|
+
// FrameOutput is also relevant for audio decoding, typically as the output of
|
|
29
|
+
// getNextFrame(), or as a temporary output variable.
|
|
30
|
+
struct FrameOutput {
|
|
31
|
+
// data shape is:
|
|
32
|
+
// - 3D (C, H, W) or (H, W, C) for videos
|
|
33
|
+
// - 2D (numChannels, numSamples) for audio
|
|
34
|
+
torch::stable::Tensor data;
|
|
35
|
+
double pts_seconds;
|
|
36
|
+
double duration_seconds;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
struct FrameBatchOutput {
|
|
40
|
+
torch::stable::Tensor data; // 4D: of shape NCHW or NHWC.
|
|
41
|
+
torch::stable::Tensor pts_seconds; // 1D of shape (N,)
|
|
42
|
+
torch::stable::Tensor duration_seconds; // 1D of shape (N,)
|
|
43
|
+
|
|
44
|
+
FrameBatchOutput(
|
|
45
|
+
int64_t num_frames,
|
|
46
|
+
const FrameDims& output_dims,
|
|
47
|
+
const StableDevice& device,
|
|
48
|
+
OutputDtype output_dtype);
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
struct AudioFramesOutput {
|
|
52
|
+
torch::stable::Tensor data; // shape is (numChannels, numSamples)
|
|
53
|
+
double pts_seconds;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
// --------------------------------------------------------------------------
|
|
57
|
+
// FRAME TENSOR ALLOCATION APIs
|
|
58
|
+
// --------------------------------------------------------------------------
|
|
59
|
+
|
|
60
|
+
// Note [Frame Tensor allocation]
|
|
61
|
+
//
|
|
62
|
+
// We always allocate [N]HWC tensors. The low-level decoding functions all
|
|
63
|
+
// assume HWC tensors, since this is what FFmpeg natively handles. It's up to
|
|
64
|
+
// the high-level decoding entry-points to permute that back to CHW, by calling
|
|
65
|
+
// maybePermuteHWC2CHW().
|
|
66
|
+
torch::stable::Tensor allocate_empty_hwc_tensor(
|
|
67
|
+
const FrameDims& frame_dims,
|
|
68
|
+
const StableDevice& device,
|
|
69
|
+
OutputDtype output_dtype,
|
|
70
|
+
std::optional<int> num_frames = std::nullopt);
|
|
71
|
+
|
|
72
|
+
torch::stable::Tensor convert_to_output_dtype(
|
|
73
|
+
const torch::stable::Tensor& tensor,
|
|
74
|
+
OutputDtype output_dtype);
|
|
75
|
+
|
|
76
|
+
} // namespace facebook::torchcodec
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
// All rights reserved.
|
|
3
|
+
//
|
|
4
|
+
// This source code is licensed under the BSD-style license found in the
|
|
5
|
+
// LICENSE file in the root directory of this source tree.
|
|
6
|
+
|
|
7
|
+
#pragma once
|
|
8
|
+
|
|
9
|
+
#include <cstdint>
|
|
10
|
+
#include "StableABICompat.h"
|
|
11
|
+
|
|
12
|
+
namespace facebook::torchcodec {
|
|
13
|
+
|
|
14
|
+
// Abstract io handler to read and write stuff. Implementations provide raw
|
|
15
|
+
// read/write/seek/get_size primitives over some backing store (an in-memory
|
|
16
|
+
// tensor, a file on disk, a Python file-like object, ...).
|
|
17
|
+
//
|
|
18
|
+
// Two kinds of consumers use it:
|
|
19
|
+
// - FFmpeg code wraps an IOInterface in an AVIOContextHolder, which adapts it
|
|
20
|
+
// into an FFmpeg AVIOContext.
|
|
21
|
+
// - Non-FFmpeg code (the image encoders, WavDecoder) calls the primitives
|
|
22
|
+
// directly.
|
|
23
|
+
class FORCE_PUBLIC_VISIBILITY IOInterface {
|
|
24
|
+
public:
|
|
25
|
+
virtual ~IOInterface() = default;
|
|
26
|
+
|
|
27
|
+
virtual int read(uint8_t* /*buf*/, int /*size*/) {
|
|
28
|
+
STD_TORCH_CHECK(false, "read() is not supported by this IOInterface");
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
virtual int write(const uint8_t* /*buf*/, int /*size*/) {
|
|
32
|
+
STD_TORCH_CHECK(false, "write() is not supported by this IOInterface");
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
virtual int64_t seek(int64_t /*offset*/, int /*whence*/) {
|
|
36
|
+
STD_TORCH_CHECK(false, "seek() is not supported by this IOInterface");
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
virtual int64_t get_size() {
|
|
40
|
+
STD_TORCH_CHECK(false, "get_size() is not supported by this IOInterface");
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
} // namespace facebook::torchcodec
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
// All rights reserved.
|
|
3
|
+
//
|
|
4
|
+
// This source code is licensed under the BSD-style license found in the
|
|
5
|
+
// LICENSE file in the root directory of this source tree.
|
|
6
|
+
|
|
7
|
+
#pragma once
|
|
8
|
+
|
|
9
|
+
#include <torch/csrc/stable/ops.h>
|
|
10
|
+
#include <torch/headeronly/util/Exception.h>
|
|
11
|
+
|
|
12
|
+
#include <vector>
|
|
13
|
+
|
|
14
|
+
#include "StableABICompat.h"
|
|
15
|
+
|
|
16
|
+
namespace facebook::torchcodec {
|
|
17
|
+
|
|
18
|
+
// Must be kept in-sync with the Python ImageReadMode enum in
|
|
19
|
+
// torchcodec/decoders/_image_decoders.py (and matching torchvision's
|
|
20
|
+
// ImageReadMode).
|
|
21
|
+
enum class ImageReadMode : int64_t {
|
|
22
|
+
UNCHANGED = 0,
|
|
23
|
+
GRAY = 1,
|
|
24
|
+
GRAY_ALPHA = 2,
|
|
25
|
+
RGB = 3,
|
|
26
|
+
RGB_ALPHA = 4,
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
// Requested output dtype. Must be kept in-sync with the codes in
|
|
30
|
+
// _validate_output_dtype in torchcodec/decoders/_image_decoders.py. Only
|
|
31
|
+
// relevant for the decoders whose source can carry more than 8 bits per channel
|
|
32
|
+
// (PNG, AVIF, HEIC); AUTO keeps the source's native precision (16-bit for
|
|
33
|
+
// >8-bit sources, 8-bit otherwise).
|
|
34
|
+
enum class OutputDtype : int64_t {
|
|
35
|
+
UINT8 = 0,
|
|
36
|
+
UINT16 = 1,
|
|
37
|
+
AUTO = 2,
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// Whether a decoder should output 8bit or 16bit data based on the user's
|
|
41
|
+
// requeted dtype, and the source. This is assumed to be called on a decoder
|
|
42
|
+
// that supports >8bit sources.
|
|
43
|
+
inline bool should_output_uint16(
|
|
44
|
+
OutputDtype output_dtype,
|
|
45
|
+
bool source_gt_8bit) {
|
|
46
|
+
switch (output_dtype) {
|
|
47
|
+
case OutputDtype::UINT8:
|
|
48
|
+
return false;
|
|
49
|
+
case OutputDtype::UINT16:
|
|
50
|
+
return true;
|
|
51
|
+
case OutputDtype::AUTO:
|
|
52
|
+
return source_gt_8bit;
|
|
53
|
+
default:
|
|
54
|
+
STD_TORCH_CHECK(
|
|
55
|
+
false,
|
|
56
|
+
"Unexpected output_dtype ",
|
|
57
|
+
static_cast<int64_t>(output_dtype),
|
|
58
|
+
". This should never happen, please report a bug to the TorchCodec repo.");
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Whether a decoder should produce a 3-channel RGB tensor (true) or a 4-channel
|
|
63
|
+
// RGBA one (false) for the given read mode. `has_alpha` is whether the source
|
|
64
|
+
// actually carries transparency. Only RGB, RGB_ALPHA and UNCHANGED are handled:
|
|
65
|
+
// the grayscale modes are emulated in Python (see _decode_to_mode() in
|
|
66
|
+
// _image_decoders.py), which requests RGB/RGBA from the C++ decoders and
|
|
67
|
+
// converts, so the default branch below is unreachable in practice. Shared by
|
|
68
|
+
// the decoders whose native output is RGB/RGBA (e.g. webp, gif).
|
|
69
|
+
inline bool should_return_rgb(ImageReadMode mode, bool has_alpha) {
|
|
70
|
+
switch (mode) {
|
|
71
|
+
case ImageReadMode::RGB:
|
|
72
|
+
return true;
|
|
73
|
+
case ImageReadMode::RGB_ALPHA:
|
|
74
|
+
return false;
|
|
75
|
+
case ImageReadMode::UNCHANGED:
|
|
76
|
+
return !has_alpha;
|
|
77
|
+
default:
|
|
78
|
+
STD_TORCH_CHECK(
|
|
79
|
+
false,
|
|
80
|
+
"Reached an unexpected code path while decoding an image to mode ",
|
|
81
|
+
static_cast<int64_t>(mode),
|
|
82
|
+
". This should never happen, please report a bug to the TorchCodec repo.");
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// The probe_* functions return {width, height, num_channels, bit_depth,
|
|
87
|
+
// num_frames, orientation}: see ImageMetadata in decoders/_image_decoders.py.
|
|
88
|
+
using ImageProbeResult = std::vector<int64_t>;
|
|
89
|
+
|
|
90
|
+
// Validates that `input` is a non-empty, 1-dimensional, uint8 CPU tensor of
|
|
91
|
+
// encoded image bytes, and returns a contiguous version of it.
|
|
92
|
+
inline torch::stable::Tensor validate_encoded_data(
|
|
93
|
+
const torch::stable::Tensor& input) {
|
|
94
|
+
STD_TORCH_CHECK(
|
|
95
|
+
input.device().type() == kStableCPU,
|
|
96
|
+
"Input tensor must be on the CPU, got a tensor on ",
|
|
97
|
+
device_type_name(input.device().type()),
|
|
98
|
+
".");
|
|
99
|
+
STD_TORCH_CHECK(
|
|
100
|
+
input.scalar_type() == kStableUInt8,
|
|
101
|
+
"Input tensor must have uint8 data type, got ",
|
|
102
|
+
torch::headeronly::toString(input.scalar_type()));
|
|
103
|
+
STD_TORCH_CHECK(
|
|
104
|
+
input.dim() == 1 && input.numel() > 0,
|
|
105
|
+
"Input tensor must be 1-dimensional and non-empty, got ",
|
|
106
|
+
input.dim(),
|
|
107
|
+
" dims and ",
|
|
108
|
+
input.numel(),
|
|
109
|
+
" numels.");
|
|
110
|
+
return torch::stable::contiguous(input);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
} // namespace facebook::torchcodec
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
// All rights reserved.
|
|
3
|
+
//
|
|
4
|
+
// This source code is licensed under the BSD-style license found in the
|
|
5
|
+
// LICENSE file in the root directory of this source tree.
|
|
6
|
+
|
|
7
|
+
#include "Logging.h"
|
|
8
|
+
|
|
9
|
+
#include <atomic>
|
|
10
|
+
#include <cstdarg>
|
|
11
|
+
#include <cstdio>
|
|
12
|
+
#include <filesystem>
|
|
13
|
+
|
|
14
|
+
namespace facebook::torchcodec {
|
|
15
|
+
|
|
16
|
+
static std::atomic<int> g_log_level{static_cast<int>(LogLevel::OFF)};
|
|
17
|
+
|
|
18
|
+
void set_cpp_log_level(LogLevel level) {
|
|
19
|
+
g_log_level.store(static_cast<int>(level), std::memory_order_relaxed);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
LogLevel get_log_level() {
|
|
23
|
+
return static_cast<LogLevel>(g_log_level.load(std::memory_order_relaxed));
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
namespace internal {
|
|
27
|
+
|
|
28
|
+
void log(const char* file, int line, const char* fmt, ...) {
|
|
29
|
+
auto basename = std::filesystem::path(file).filename().string();
|
|
30
|
+
|
|
31
|
+
std::fprintf(stderr, "[torchcodec %s:%d] ", basename.c_str(), line);
|
|
32
|
+
|
|
33
|
+
va_list args;
|
|
34
|
+
va_start(args, fmt);
|
|
35
|
+
std::vfprintf(stderr, fmt, args);
|
|
36
|
+
va_end(args);
|
|
37
|
+
|
|
38
|
+
std::fprintf(stderr, "\n");
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
} // namespace internal
|
|
42
|
+
} // namespace facebook::torchcodec
|