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.
Files changed (179) hide show
  1. torchcodec/__init__.py +50 -0
  2. torchcodec/_core/AVIOContextHolder.cpp +85 -0
  3. torchcodec/_core/AVIOContextHolder.h +44 -0
  4. torchcodec/_core/AudioCommon.cpp +101 -0
  5. torchcodec/_core/AudioCommon.h +48 -0
  6. torchcodec/_core/AudioConverter.cpp +130 -0
  7. torchcodec/_core/AudioConverter.h +59 -0
  8. torchcodec/_core/BetaCudaDeviceInterface.cpp +1635 -0
  9. torchcodec/_core/BetaCudaDeviceInterface.h +364 -0
  10. torchcodec/_core/CMakeLists.txt +661 -0
  11. torchcodec/_core/CUDACommon.cpp +129 -0
  12. torchcodec/_core/CUDACommon.h +115 -0
  13. torchcodec/_core/Cache.h +126 -0
  14. torchcodec/_core/CodecParameters.cpp +98 -0
  15. torchcodec/_core/CodecParameters.h +59 -0
  16. torchcodec/_core/ColorConverter.cpp +92 -0
  17. torchcodec/_core/ColorConverter.h +40 -0
  18. torchcodec/_core/CpuDeviceInterface.cpp +654 -0
  19. torchcodec/_core/CpuDeviceInterface.h +153 -0
  20. torchcodec/_core/CudaDeviceInterface.cpp +519 -0
  21. torchcodec/_core/CudaDeviceInterface.h +92 -0
  22. torchcodec/_core/DecodeAvif.cpp +284 -0
  23. torchcodec/_core/DecodeAvif.h +28 -0
  24. torchcodec/_core/DecodeGif.cpp +488 -0
  25. torchcodec/_core/DecodeGif.h +32 -0
  26. torchcodec/_core/DecodeHeic.cpp +306 -0
  27. torchcodec/_core/DecodeHeic.h +18 -0
  28. torchcodec/_core/DecodeJpeg.cpp +489 -0
  29. torchcodec/_core/DecodeJpeg.h +17 -0
  30. torchcodec/_core/DecodeJpegCuda.cpp +638 -0
  31. torchcodec/_core/DecodeJpegCuda.h +92 -0
  32. torchcodec/_core/DecodePng.cpp +569 -0
  33. torchcodec/_core/DecodePng.h +31 -0
  34. torchcodec/_core/DecodeWebp.cpp +238 -0
  35. torchcodec/_core/DecodeWebp.h +17 -0
  36. torchcodec/_core/Demuxer.cpp +415 -0
  37. torchcodec/_core/Demuxer.h +158 -0
  38. torchcodec/_core/DeviceInterface.cpp +152 -0
  39. torchcodec/_core/DeviceInterface.h +255 -0
  40. torchcodec/_core/EncodeJpeg.cpp +223 -0
  41. torchcodec/_core/EncodeJpeg.h +19 -0
  42. torchcodec/_core/EncodeJpegCuda.cpp +272 -0
  43. torchcodec/_core/EncodeJpegCuda.h +56 -0
  44. torchcodec/_core/EncodePng.cpp +207 -0
  45. torchcodec/_core/EncodePng.h +19 -0
  46. torchcodec/_core/Encoder.cpp +1023 -0
  47. torchcodec/_core/Encoder.h +144 -0
  48. torchcodec/_core/Exif.h +241 -0
  49. torchcodec/_core/FFMPEGCommon.cpp +1159 -0
  50. torchcodec/_core/FFMPEGCommon.h +506 -0
  51. torchcodec/_core/FileIO.cpp +78 -0
  52. torchcodec/_core/FileIO.h +34 -0
  53. torchcodec/_core/FileLikeIO.cpp +76 -0
  54. torchcodec/_core/FileLikeIO.h +68 -0
  55. torchcodec/_core/FilterGraph.cpp +211 -0
  56. torchcodec/_core/FilterGraph.h +59 -0
  57. torchcodec/_core/Frame.cpp +75 -0
  58. torchcodec/_core/Frame.h +76 -0
  59. torchcodec/_core/IOInterface.h +44 -0
  60. torchcodec/_core/ImageCommon.h +113 -0
  61. torchcodec/_core/Logging.cpp +42 -0
  62. torchcodec/_core/Logging.h +38 -0
  63. torchcodec/_core/Metadata.cpp +312 -0
  64. torchcodec/_core/Metadata.h +115 -0
  65. torchcodec/_core/NVCUVIDRuntimeLoader.cpp +324 -0
  66. torchcodec/_core/NVCUVIDRuntimeLoader.h +14 -0
  67. torchcodec/_core/NVDECCache.cpp +111 -0
  68. torchcodec/_core/NVDECCache.h +136 -0
  69. torchcodec/_core/NVDECCacheConfig.cpp +61 -0
  70. torchcodec/_core/NVDECCacheConfig.h +30 -0
  71. torchcodec/_core/PacketDecoder.cpp +358 -0
  72. torchcodec/_core/PacketDecoder.h +140 -0
  73. torchcodec/_core/SingleStreamDecoder.cpp +1856 -0
  74. torchcodec/_core/SingleStreamDecoder.h +424 -0
  75. torchcodec/_core/StableABICompat.h +213 -0
  76. torchcodec/_core/StreamOptions.h +93 -0
  77. torchcodec/_core/SwScale.cpp +124 -0
  78. torchcodec/_core/SwScale.h +67 -0
  79. torchcodec/_core/TensorIO.cpp +136 -0
  80. torchcodec/_core/TensorIO.h +53 -0
  81. torchcodec/_core/Transform.cpp +207 -0
  82. torchcodec/_core/Transform.h +125 -0
  83. torchcodec/_core/ValidationUtils.cpp +65 -0
  84. torchcodec/_core/ValidationUtils.h +29 -0
  85. torchcodec/_core/WavDecoder.cpp +494 -0
  86. torchcodec/_core/WavDecoder.h +73 -0
  87. torchcodec/_core/__init__.py +45 -0
  88. torchcodec/_core/_decoder_utils.py +258 -0
  89. torchcodec/_core/_ffmpeg_op_names.py +95 -0
  90. torchcodec/_core/_ffmpeg_ops.py +699 -0
  91. torchcodec/_core/_file_like.py +221 -0
  92. torchcodec/_core/_metadata.py +402 -0
  93. torchcodec/_core/color_conversion.cpp +316 -0
  94. torchcodec/_core/color_conversion.cu +391 -0
  95. torchcodec/_core/color_conversion.h +150 -0
  96. torchcodec/_core/custom_ops.cpp +2438 -0
  97. torchcodec/_core/fetch_and_expose_non_gpl_ffmpeg_libs.cmake +192 -0
  98. torchcodec/_core/fetch_avif_from_s3.cmake +81 -0
  99. torchcodec/_core/giflib/README +28 -0
  100. torchcodec/_core/giflib/dgif_lib.c +1313 -0
  101. torchcodec/_core/giflib/gif_hash.c +129 -0
  102. torchcodec/_core/giflib/gif_hash.h +43 -0
  103. torchcodec/_core/giflib/gif_lib.h +292 -0
  104. torchcodec/_core/giflib/gif_lib_private.h +73 -0
  105. torchcodec/_core/giflib/gifalloc.c +426 -0
  106. torchcodec/_core/giflib/openbsd-reallocarray.c +74 -0
  107. torchcodec/_core/heic_custom_ops.cpp +20 -0
  108. torchcodec/_core/image_custom_ops.cpp +137 -0
  109. torchcodec/_core/nvcuvid_include/cuviddec.h +1374 -0
  110. torchcodec/_core/nvcuvid_include/nvcuvid.h +610 -0
  111. torchcodec/_core/ops.py +103 -0
  112. torchcodec/_core/sources.bzl +113 -0
  113. torchcodec/_frame.py +146 -0
  114. torchcodec/_internally_replaced_utils.py +129 -0
  115. torchcodec/_logging.py +59 -0
  116. torchcodec/decoders/__init__.py +50 -0
  117. torchcodec/decoders/_audio_decoder.py +183 -0
  118. torchcodec/decoders/_blocks/__init__.py +48 -0
  119. torchcodec/decoders/_blocks/_audio_converter.py +155 -0
  120. torchcodec/decoders/_blocks/_codec_parameters.py +229 -0
  121. torchcodec/decoders/_blocks/_color_converter.py +107 -0
  122. torchcodec/decoders/_blocks/_demuxer.py +678 -0
  123. torchcodec/decoders/_blocks/_frame.py +577 -0
  124. torchcodec/decoders/_blocks/_helpers.py +53 -0
  125. torchcodec/decoders/_blocks/_packet_decoder.py +357 -0
  126. torchcodec/decoders/_decoder_utils.py +128 -0
  127. torchcodec/decoders/_image_decoders.py +1575 -0
  128. torchcodec/decoders/_video_decoder.py +592 -0
  129. torchcodec/decoders/_wav_decoder.py +131 -0
  130. torchcodec/encoders/__init__.py +4 -0
  131. torchcodec/encoders/_audio_encoder.py +167 -0
  132. torchcodec/encoders/_image_encoders.py +181 -0
  133. torchcodec/encoders/_multi_stream_encoder.py +272 -0
  134. torchcodec/encoders/_video_encoder.py +227 -0
  135. torchcodec/jpeg8.dll +0 -0
  136. torchcodec/libavif.dll +0 -0
  137. torchcodec/libpng16.dll +0 -0
  138. torchcodec/libsharpyuv.dll +0 -0
  139. torchcodec/libtorchcodec_core4.dll +0 -0
  140. torchcodec/libtorchcodec_core5.dll +0 -0
  141. torchcodec/libtorchcodec_core6.dll +0 -0
  142. torchcodec/libtorchcodec_core7.dll +0 -0
  143. torchcodec/libtorchcodec_core8.dll +0 -0
  144. torchcodec/libtorchcodec_core9.dll +0 -0
  145. torchcodec/libtorchcodec_custom_ops4.dll +0 -0
  146. torchcodec/libtorchcodec_custom_ops5.dll +0 -0
  147. torchcodec/libtorchcodec_custom_ops6.dll +0 -0
  148. torchcodec/libtorchcodec_custom_ops7.dll +0 -0
  149. torchcodec/libtorchcodec_custom_ops8.dll +0 -0
  150. torchcodec/libtorchcodec_custom_ops9.dll +0 -0
  151. torchcodec/libtorchcodec_heic.dll +0 -0
  152. torchcodec/libtorchcodec_image.dll +0 -0
  153. torchcodec/libwebp.dll +0 -0
  154. torchcodec/libwebpdecoder.dll +0 -0
  155. torchcodec/libwebpdemux.dll +0 -0
  156. torchcodec/libwebpmux.dll +0 -0
  157. torchcodec/libzstd.dll +0 -0
  158. torchcodec/py.typed +0 -0
  159. torchcodec/samplers/__init__.py +2 -0
  160. torchcodec/samplers/_common.py +83 -0
  161. torchcodec/samplers/_index_based.py +287 -0
  162. torchcodec/samplers/_time_based.py +360 -0
  163. torchcodec/share/cmake/TorchCodec/TorchCodecConfig.cmake +76 -0
  164. torchcodec/share/cmake/TorchCodec/ffmpeg_versions.cmake +128 -0
  165. torchcodec/transforms/__init__.py +12 -0
  166. torchcodec/transforms/_decoder_transforms.py +375 -0
  167. torchcodec/version.py +1 -0
  168. torchcodec/zlib.dll +0 -0
  169. torchcodec-1.0.0.dist-info/METADATA +275 -0
  170. torchcodec-1.0.0.dist-info/RECORD +179 -0
  171. torchcodec-1.0.0.dist-info/WHEEL +5 -0
  172. torchcodec-1.0.0.dist-info/licenses/LICENSE +28 -0
  173. torchcodec-1.0.0.dist-info/licenses/third_party/COPYING.dav1d +23 -0
  174. torchcodec-1.0.0.dist-info/licenses/third_party/LICENSE.libavif +387 -0
  175. torchcodec-1.0.0.dist-info/licenses/third_party/LICENSE.libjpeg-turbo +135 -0
  176. torchcodec-1.0.0.dist-info/licenses/third_party/LICENSE.libpng +134 -0
  177. torchcodec-1.0.0.dist-info/licenses/third_party/LICENSE.libwebp +30 -0
  178. torchcodec-1.0.0.dist-info/licenses/third_party/LICENSE.libyuv +29 -0
  179. torchcodec-1.0.0.dist-info/licenses/third_party/LICENSE.zlib +22 -0
@@ -0,0 +1,324 @@
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
+ #ifdef FBCODE_CAFFE2
8
+ #include "StableABICompat.h"
9
+
10
+ // No need to do anything on fbcode. NVCUVID is available there, we can take a
11
+ // hard dependency on it.
12
+ // The FBCODE_CAFFE2 macro is defined in the upstream fbcode build of torch, so
13
+ // we can rely on it, that's what torch does too.
14
+
15
+ namespace facebook::torchcodec {
16
+ bool load_nvcuvid_library() {
17
+ return true;
18
+ }
19
+ } // namespace facebook::torchcodec
20
+ #else
21
+
22
+ #include "NVCUVIDRuntimeLoader.h"
23
+ #include "StableABICompat.h"
24
+
25
+ #include "nvcuvid_include/cuviddec.h"
26
+ #include "nvcuvid_include/nvcuvid.h"
27
+
28
+ #include <cstdio>
29
+ #include <mutex>
30
+ #include "StableABICompat.h"
31
+
32
+ #if defined(WIN64) || defined(_WIN64)
33
+ #include <windows.h>
34
+ typedef HMODULE tHandle;
35
+ #else
36
+ #include <dlfcn.h>
37
+ typedef void* tHandle;
38
+ #endif
39
+
40
+ namespace facebook::torchcodec {
41
+
42
+ /* clang-format off */
43
+ // This file defines the logic to load the NVCUVID library **at runtime**,
44
+ // along with the corresponding NVCUVID functions that we'll need.
45
+ //
46
+ // We do this because we *do not want* to link (statically or dynamically)
47
+ // against libnvcuvid.so: it is not always available on the users machine! If we
48
+ // were to link against libnvcuvid.so, that would mean that our
49
+ // libtorchcodec_coreN.so would try to look for it when loaded at import time.
50
+ // And if it's not on the users machine, that causes `import torchcodec` to
51
+ // fail. Source: that's what we did, and we got user reports.
52
+ //
53
+ // So, we don't link against libnvcuvid.so. But we still want to call its
54
+ // functions. So here's how it's done, we'll use cuvidCreateVideoParser as an
55
+ // example, but it works the same for all. We are largely following the
56
+ // instructions from the NVCUVID docs:
57
+ // https://docs.nvidia.com/video-technologies/video-codec-sdk/13.0/nvdec-video-decoder-api-prog-guide/index.html#dynamic-loading-nvidia-components
58
+ //
59
+ // This:
60
+ // typedef CUresult CUDAAPI tcuvidCreateVideoParser(CUvideoparser*, CUVIDPARSERPARAMS*);
61
+ // defines tcuvidCreateVideoParser, which is the *type* of a *function*.
62
+ // We define such a function of that type just below with:
63
+ // static tcuvidCreateVideoParser* dl_cuvidCreateVideoParser = nullptr;
64
+ // "dl" is for "dynamically loaded. For now dl_cuvidCreateVideoParser is
65
+ // nullptr, but later it will be a proper function [pointer] that can be called
66
+ // with dl_cuvidCreateVideoParser(...);
67
+ //
68
+ // For that to happen we need to call load_nvcuvid_library(): in there, we first
69
+ // dlopen(libnvcuvid.so) which loads the .so somewhere in memory. Then we call
70
+ // dlsym(...), which binds dl_cuvidCreateVideoParser to its actual address: it
71
+ // literally sets the value of the dl_cuvidCreateVideoParser pointer to the
72
+ // address of the actual code section. If all went well, by now, we can safely
73
+ // call dl_cuvidCreateVideoParser(...);
74
+ // All of that happens at runtime *after* import time, when the first instance
75
+ // of the NVDEC CUDA interface is created, i.e. only when the user requests
76
+ // CUDA decoding.
77
+ //
78
+ // At the bottom of this file we have an `extern "C"` section with function
79
+ // definitions like:
80
+ //
81
+ // CUresult CUDAAPI cuvidCreateVideoParser(
82
+ // CUvideoparser* videoParser,
83
+ // CUVIDPARSERPARAMS* parserParams) {...}
84
+ //
85
+ // These are the actual functions that are compiled against and called by the
86
+ // NVDEC CUDA interface code. Crucially, these functions signature match exactly
87
+ // the NVCUVID functions (as defined in cuviddec.h). Inside of
88
+ // cuvidCreateVideoParser(...) we simply call the dl_cuvidCreateVideoParser
89
+ // function [pointer] that we dynamically loaded earlier.
90
+ //
91
+ // At runtime, within the NVDEC CUDA interface code we have a fallback mechanism
92
+ // to switch back to the CPU backend if any of the NVCUVID functions are not
93
+ // available, or if libnvcuvid.so itself couldn't be found. This is what FFmpeg
94
+ // does too.
95
+
96
+
97
+ // Function pointers types
98
+ typedef CUresult CUDAAPI tcuvidCreateVideoParser(CUvideoparser*, CUVIDPARSERPARAMS*);
99
+ typedef CUresult CUDAAPI tcuvidParseVideoData(CUvideoparser, CUVIDSOURCEDATAPACKET*);
100
+ typedef CUresult CUDAAPI tcuvidDestroyVideoParser(CUvideoparser);
101
+ typedef CUresult CUDAAPI tcuvidGetDecoderCaps(CUVIDDECODECAPS*);
102
+ typedef CUresult CUDAAPI tcuvidCreateDecoder(CUvideodecoder*, CUVIDDECODECREATEINFO*);
103
+ typedef CUresult CUDAAPI tcuvidDestroyDecoder(CUvideodecoder);
104
+ typedef CUresult CUDAAPI tcuvidDecodePicture(CUvideodecoder, CUVIDPICPARAMS*);
105
+ typedef CUresult CUDAAPI tcuvidMapVideoFrame(CUvideodecoder, int, unsigned int*, unsigned int*, CUVIDPROCPARAMS*);
106
+ typedef CUresult CUDAAPI tcuvidUnmapVideoFrame(CUvideodecoder, unsigned int);
107
+ typedef CUresult CUDAAPI tcuvidMapVideoFrame64(CUvideodecoder, int, unsigned long long*, unsigned int*, CUVIDPROCPARAMS*);
108
+ typedef CUresult CUDAAPI tcuvidUnmapVideoFrame64(CUvideodecoder, unsigned long long);
109
+ /* clang-format on */
110
+
111
+ // Global function pointers - will be dynamically loaded
112
+ static tcuvidCreateVideoParser* dl_cuvidCreateVideoParser = nullptr;
113
+ static tcuvidParseVideoData* dl_cuvidParseVideoData = nullptr;
114
+ static tcuvidDestroyVideoParser* dl_cuvidDestroyVideoParser = nullptr;
115
+ static tcuvidGetDecoderCaps* dl_cuvidGetDecoderCaps = nullptr;
116
+ static tcuvidCreateDecoder* dl_cuvidCreateDecoder = nullptr;
117
+ static tcuvidDestroyDecoder* dl_cuvidDestroyDecoder = nullptr;
118
+ static tcuvidDecodePicture* dl_cuvidDecodePicture = nullptr;
119
+ static tcuvidMapVideoFrame* dl_cuvidMapVideoFrame = nullptr;
120
+ static tcuvidUnmapVideoFrame* dl_cuvidUnmapVideoFrame = nullptr;
121
+ static tcuvidMapVideoFrame64* dl_cuvidMapVideoFrame64 = nullptr;
122
+ static tcuvidUnmapVideoFrame64* dl_cuvidUnmapVideoFrame64 = nullptr;
123
+
124
+ static tHandle g_nvcuvid_handle = nullptr;
125
+ static std::mutex g_nvcuvid_mutex;
126
+
127
+ bool is_loaded() {
128
+ return (
129
+ g_nvcuvid_handle && dl_cuvidCreateVideoParser && dl_cuvidParseVideoData &&
130
+ dl_cuvidDestroyVideoParser && dl_cuvidGetDecoderCaps &&
131
+ dl_cuvidCreateDecoder && dl_cuvidDestroyDecoder &&
132
+ dl_cuvidDecodePicture && dl_cuvidMapVideoFrame &&
133
+ dl_cuvidUnmapVideoFrame && dl_cuvidMapVideoFrame64 &&
134
+ dl_cuvidUnmapVideoFrame64);
135
+ }
136
+
137
+ template <typename T>
138
+ T* bind_function(const char* function_name) {
139
+ #if defined(WIN64) || defined(_WIN64)
140
+ return reinterpret_cast<T*>(GetProcAddress(g_nvcuvid_handle, function_name));
141
+ #else
142
+ return reinterpret_cast<T*>(dlsym(g_nvcuvid_handle, function_name));
143
+ #endif
144
+ }
145
+
146
+ bool load_library() {
147
+ // Helper that just calls dlopen or equivalent on Windows. In a separate
148
+ // function because of the #ifdef uglyness.
149
+ #if defined(WIN64) || defined(_WIN64)
150
+ #ifdef UNICODE
151
+ static LPCWSTR nvcuvidDll = L"nvcuvid.dll";
152
+ #else
153
+ static LPCSTR nvcuvidDll = "nvcuvid.dll";
154
+ #endif
155
+ g_nvcuvid_handle = LoadLibrary(nvcuvidDll);
156
+ if (g_nvcuvid_handle == nullptr) {
157
+ return false;
158
+ }
159
+ #else
160
+ g_nvcuvid_handle = dlopen("libnvcuvid.so", RTLD_NOW);
161
+ if (g_nvcuvid_handle == nullptr) {
162
+ g_nvcuvid_handle = dlopen("libnvcuvid.so.1", RTLD_NOW);
163
+ }
164
+ if (g_nvcuvid_handle == nullptr) {
165
+ return false;
166
+ }
167
+ #endif
168
+
169
+ return true;
170
+ }
171
+
172
+ bool load_nvcuvid_library() {
173
+ // Loads NVCUVID library and all required function pointers.
174
+ // Returns true on success, false on failure.
175
+ std::lock_guard<std::mutex> lock(g_nvcuvid_mutex);
176
+
177
+ if (is_loaded()) {
178
+ return true;
179
+ }
180
+
181
+ if (!load_library()) {
182
+ return false;
183
+ }
184
+
185
+ // Load all function pointers. They'll be set to nullptr if not found.
186
+ dl_cuvidCreateVideoParser =
187
+ bind_function<tcuvidCreateVideoParser>("cuvidCreateVideoParser");
188
+ dl_cuvidParseVideoData =
189
+ bind_function<tcuvidParseVideoData>("cuvidParseVideoData");
190
+ dl_cuvidDestroyVideoParser =
191
+ bind_function<tcuvidDestroyVideoParser>("cuvidDestroyVideoParser");
192
+ dl_cuvidGetDecoderCaps =
193
+ bind_function<tcuvidGetDecoderCaps>("cuvidGetDecoderCaps");
194
+ dl_cuvidCreateDecoder =
195
+ bind_function<tcuvidCreateDecoder>("cuvidCreateDecoder");
196
+ dl_cuvidDestroyDecoder =
197
+ bind_function<tcuvidDestroyDecoder>("cuvidDestroyDecoder");
198
+ dl_cuvidDecodePicture =
199
+ bind_function<tcuvidDecodePicture>("cuvidDecodePicture");
200
+ dl_cuvidMapVideoFrame =
201
+ bind_function<tcuvidMapVideoFrame>("cuvidMapVideoFrame");
202
+ dl_cuvidUnmapVideoFrame =
203
+ bind_function<tcuvidUnmapVideoFrame>("cuvidUnmapVideoFrame");
204
+ dl_cuvidMapVideoFrame64 =
205
+ bind_function<tcuvidMapVideoFrame64>("cuvidMapVideoFrame64");
206
+ dl_cuvidUnmapVideoFrame64 =
207
+ bind_function<tcuvidUnmapVideoFrame64>("cuvidUnmapVideoFrame64");
208
+
209
+ return is_loaded();
210
+ }
211
+
212
+ } // namespace facebook::torchcodec
213
+
214
+ extern "C" {
215
+
216
+ CUresult CUDAAPI cuvidCreateVideoParser(
217
+ CUvideoparser* video_parser,
218
+ CUVIDPARSERPARAMS* parser_params) {
219
+ STD_TORCH_CHECK(
220
+ facebook::torchcodec::dl_cuvidCreateVideoParser,
221
+ "cuvidCreateVideoParser called but NVCUVID not loaded!");
222
+ return facebook::torchcodec::dl_cuvidCreateVideoParser(
223
+ video_parser, parser_params);
224
+ }
225
+
226
+ CUresult CUDAAPI cuvidParseVideoData(
227
+ CUvideoparser video_parser,
228
+ CUVIDSOURCEDATAPACKET* cuvid_packet) {
229
+ STD_TORCH_CHECK(
230
+ facebook::torchcodec::dl_cuvidParseVideoData,
231
+ "cuvidParseVideoData called but NVCUVID not loaded!");
232
+ return facebook::torchcodec::dl_cuvidParseVideoData(
233
+ video_parser, cuvid_packet);
234
+ }
235
+
236
+ CUresult CUDAAPI cuvidDestroyVideoParser(CUvideoparser video_parser) {
237
+ STD_TORCH_CHECK(
238
+ facebook::torchcodec::dl_cuvidDestroyVideoParser,
239
+ "cuvidDestroyVideoParser called but NVCUVID not loaded!");
240
+ return facebook::torchcodec::dl_cuvidDestroyVideoParser(video_parser);
241
+ }
242
+
243
+ CUresult CUDAAPI cuvidGetDecoderCaps(CUVIDDECODECAPS* caps) {
244
+ STD_TORCH_CHECK(
245
+ facebook::torchcodec::dl_cuvidGetDecoderCaps,
246
+ "cuvidGetDecoderCaps called but NVCUVID not loaded!");
247
+ return facebook::torchcodec::dl_cuvidGetDecoderCaps(caps);
248
+ }
249
+
250
+ CUresult CUDAAPI cuvidCreateDecoder(
251
+ CUvideodecoder* decoder,
252
+ CUVIDDECODECREATEINFO* decoder_params) {
253
+ STD_TORCH_CHECK(
254
+ facebook::torchcodec::dl_cuvidCreateDecoder,
255
+ "cuvidCreateDecoder called but NVCUVID not loaded!");
256
+ return facebook::torchcodec::dl_cuvidCreateDecoder(decoder, decoder_params);
257
+ }
258
+
259
+ CUresult CUDAAPI cuvidDestroyDecoder(CUvideodecoder decoder) {
260
+ STD_TORCH_CHECK(
261
+ facebook::torchcodec::dl_cuvidDestroyDecoder,
262
+ "cuvidDestroyDecoder called but NVCUVID not loaded!");
263
+ return facebook::torchcodec::dl_cuvidDestroyDecoder(decoder);
264
+ }
265
+
266
+ CUresult CUDAAPI
267
+ cuvidDecodePicture(CUvideodecoder decoder, CUVIDPICPARAMS* pic_params) {
268
+ STD_TORCH_CHECK(
269
+ facebook::torchcodec::dl_cuvidDecodePicture,
270
+ "cuvidDecodePicture called but NVCUVID not loaded!");
271
+ return facebook::torchcodec::dl_cuvidDecodePicture(decoder, pic_params);
272
+ }
273
+
274
+ #if !defined(__CUVID_DEVPTR64) || defined(__CUVID_INTERNAL)
275
+ // We need to protect the definition of the 32bit versions under the above
276
+ // conditions (see cuviddec.h). Defining them unconditionally would cause
277
+ // conflict compilation errors when cuviddec.h redefines those to the 64bit
278
+ // versions.
279
+ CUresult CUDAAPI cuvidMapVideoFrame(
280
+ CUvideodecoder decoder,
281
+ int pix_index,
282
+ unsigned int* frame_ptr,
283
+ unsigned int* pitch,
284
+ CUVIDPROCPARAMS* proc_params) {
285
+ STD_TORCH_CHECK(
286
+ facebook::torchcodec::dl_cuvidMapVideoFrame,
287
+ "cuvidMapVideoFrame called but NVCUVID not loaded!");
288
+ return facebook::torchcodec::dl_cuvidMapVideoFrame(
289
+ decoder, pix_index, frame_ptr, pitch, proc_params);
290
+ }
291
+
292
+ CUresult CUDAAPI
293
+ cuvidUnmapVideoFrame(CUvideodecoder decoder, unsigned int frame_ptr) {
294
+ STD_TORCH_CHECK(
295
+ facebook::torchcodec::dl_cuvidUnmapVideoFrame,
296
+ "cuvidUnmapVideoFrame called but NVCUVID not loaded!");
297
+ return facebook::torchcodec::dl_cuvidUnmapVideoFrame(decoder, frame_ptr);
298
+ }
299
+ #endif
300
+
301
+ CUresult CUDAAPI cuvidMapVideoFrame64(
302
+ CUvideodecoder decoder,
303
+ int pix_index,
304
+ unsigned long long* frame_ptr,
305
+ unsigned int* pitch,
306
+ CUVIDPROCPARAMS* proc_params) {
307
+ STD_TORCH_CHECK(
308
+ facebook::torchcodec::dl_cuvidMapVideoFrame64,
309
+ "cuvidMapVideoFrame64 called but NVCUVID not loaded!");
310
+ return facebook::torchcodec::dl_cuvidMapVideoFrame64(
311
+ decoder, pix_index, frame_ptr, pitch, proc_params);
312
+ }
313
+
314
+ CUresult CUDAAPI
315
+ cuvidUnmapVideoFrame64(CUvideodecoder decoder, unsigned long long frame_ptr) {
316
+ STD_TORCH_CHECK(
317
+ facebook::torchcodec::dl_cuvidUnmapVideoFrame64,
318
+ "cuvidUnmapVideoFrame64 called but NVCUVID not loaded!");
319
+ return facebook::torchcodec::dl_cuvidUnmapVideoFrame64(decoder, frame_ptr);
320
+ }
321
+
322
+ } // extern "C"
323
+
324
+ #endif // FBCODE_CAFFE2
@@ -0,0 +1,14 @@
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
+ namespace facebook::torchcodec {
10
+
11
+ // See note in corresponding cpp file
12
+ bool load_nvcuvid_library();
13
+
14
+ } // namespace facebook::torchcodec
@@ -0,0 +1,111 @@
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 <mutex>
8
+
9
+ #include "CUDACommon.h"
10
+ #include "FFMPEGCommon.h"
11
+ #include "NVDECCache.h"
12
+ #include "NVDECCacheConfig.h"
13
+
14
+ #include <cuda_runtime.h> // For cudaGetDevice
15
+
16
+ extern "C" {
17
+ #include <libavutil/hwcontext_cuda.h>
18
+ #include <libavutil/pixdesc.h>
19
+ }
20
+
21
+ namespace facebook::torchcodec {
22
+
23
+ NVDECCache* NVDECCache::get_cache_instances() {
24
+ // Intentionally leaked to avoid calling into CUDA/NVCUVID during static
25
+ // destruction, when the CUDA runtime may already be torn down.
26
+ static NVDECCache* cache_instances = new NVDECCache[MAX_CUDA_GPUS];
27
+ return cache_instances;
28
+ }
29
+
30
+ NVDECCache& NVDECCache::get_cache(const StableDevice& device) {
31
+ return get_cache_instances()[get_device_index(device)];
32
+ }
33
+
34
+ UniqueCUvideodecoder NVDECCache::get_decoder(
35
+ CUVIDEOFORMAT* video_format,
36
+ cudaVideoSurfaceFormat surface_format) {
37
+ CacheKey key(video_format, surface_format);
38
+ std::lock_guard<std::mutex> lock(cache_lock_);
39
+
40
+ // Find an entry with matching key
41
+ auto it = cache_.find(key);
42
+ if (it != cache_.end()) {
43
+ auto decoder = std::move(it->second.decoder);
44
+ cache_.erase(it);
45
+ return decoder;
46
+ }
47
+
48
+ return nullptr;
49
+ }
50
+
51
+ // Evicts the least-recently-used entry from cache_.
52
+ // Caller must hold cacheLock_!!!
53
+ void NVDECCache::evict_lru_entry() {
54
+ if (cache_.empty()) {
55
+ return;
56
+ }
57
+ auto victim = cache_.begin();
58
+ for (auto it = cache_.begin(); it != cache_.end(); ++it) {
59
+ if (it->second.last_used < victim->second.last_used) {
60
+ victim = it;
61
+ }
62
+ }
63
+ cache_.erase(victim);
64
+ }
65
+
66
+ void NVDECCache::return_decoder(
67
+ CUVIDEOFORMAT* video_format,
68
+ cudaVideoSurfaceFormat surface_format,
69
+ UniqueCUvideodecoder decoder) {
70
+ STD_TORCH_CHECK(decoder != nullptr, "decoder must not be null");
71
+
72
+ CacheKey key(video_format, surface_format);
73
+ std::lock_guard<std::mutex> lock(cache_lock_);
74
+
75
+ int capacity = get_nvdec_cache_capacity();
76
+ if (capacity <= 0) {
77
+ return;
78
+ }
79
+
80
+ // Evict least recently used entries until under capacity.
81
+ // This search is O(capacity), which is supposed to be small,
82
+ // so linear vs constant search overhead is expected to be negligible.
83
+ while (cache_.size() >= static_cast<size_t>(capacity)) {
84
+ evict_lru_entry();
85
+ }
86
+
87
+ // Add the decoder back to cache
88
+ cache_.emplace(key, CacheEntry(std::move(decoder), last_used_counter_++));
89
+
90
+ STD_TORCH_CHECK(
91
+ cache_.size() <= static_cast<size_t>(capacity),
92
+ "Cache size exceeded capacity, please report a bug");
93
+ }
94
+
95
+ void NVDECCache::evict_excess_entries_across_devices(int capacity) {
96
+ NVDECCache* instances = get_cache_instances();
97
+ for (int i = 0; i < MAX_CUDA_GPUS; ++i) {
98
+ std::lock_guard<std::mutex> lock(instances[i].cache_lock_);
99
+ while (instances[i].cache_.size() > static_cast<size_t>(capacity)) {
100
+ instances[i].evict_lru_entry();
101
+ }
102
+ }
103
+ }
104
+
105
+ int NVDECCache::get_cache_size_for_device(int device_index) {
106
+ NVDECCache* instances = get_cache_instances();
107
+ std::lock_guard<std::mutex> lock(instances[device_index].cache_lock_);
108
+ return static_cast<int>(instances[device_index].cache_.size());
109
+ }
110
+
111
+ } // namespace facebook::torchcodec
@@ -0,0 +1,136 @@
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 <map>
10
+ #include <memory>
11
+ #include <mutex>
12
+
13
+ #include <cuda.h>
14
+
15
+ #include "NVCUVIDRuntimeLoader.h"
16
+ #include "NVDECCacheConfig.h"
17
+ #include "StableABICompat.h"
18
+ #include "nvcuvid_include/cuviddec.h"
19
+ #include "nvcuvid_include/nvcuvid.h"
20
+
21
+ namespace facebook::torchcodec {
22
+
23
+ // This file implements a cache for NVDEC decoders.
24
+ // TODONVDEC P3: Consider merging this with Cache.h. The main difference is that
25
+ // this NVDEC Cache involves a cache key (the decoder parameters).
26
+
27
+ struct CUvideoDecoderDeleter {
28
+ void operator()(CUvideodecoder* decoder_ptr) const {
29
+ if (decoder_ptr && *decoder_ptr) {
30
+ cuvidDestroyDecoder(*decoder_ptr);
31
+ delete decoder_ptr;
32
+ }
33
+ }
34
+ };
35
+
36
+ using UniqueCUvideodecoder =
37
+ std::unique_ptr<CUvideodecoder, CUvideoDecoderDeleter>;
38
+
39
+ struct CacheEntry {
40
+ UniqueCUvideodecoder decoder;
41
+ uint64_t last_used; // LRU timestamp
42
+
43
+ CacheEntry(UniqueCUvideodecoder dec, uint64_t ts)
44
+ : decoder(std::move(dec)), last_used(ts) {}
45
+ };
46
+
47
+ // A per-device LRU cache for NVDEC decoders. There is one instance of this
48
+ // class per GPU device, and it is accessed through the static getCache()
49
+ // method. The cache supports multiple decoders with the same parameters.
50
+ class NVDECCache {
51
+ public:
52
+ static NVDECCache& get_cache(const StableDevice& device);
53
+
54
+ // Get decoder from cache - returns nullptr if none available.
55
+ UniqueCUvideodecoder get_decoder(
56
+ CUVIDEOFORMAT* video_format,
57
+ cudaVideoSurfaceFormat surface_format);
58
+
59
+ // Return decoder to cache using LRU eviction.
60
+ void return_decoder(
61
+ CUVIDEOFORMAT* video_format,
62
+ cudaVideoSurfaceFormat surface_format,
63
+ UniqueCUvideodecoder decoder);
64
+
65
+ // Iterates all per-device cache instances and evicts LRU entries until each
66
+ // cache's size is at most capacity. Called from setNVDECCacheCapacity().
67
+ static void evict_excess_entries_across_devices(int capacity);
68
+
69
+ // Returns the number of entries in the cache for a given device index.
70
+ static int get_cache_size_for_device(int device_index);
71
+
72
+ private:
73
+ // Cache key struct: a decoder can be reused and taken from the cache only if
74
+ // all these parameters match. We deliberately store the coded dimensions, not
75
+ // the true displayed dimensions, to optimize cache hits. See [NVDEC surface
76
+ // dimensions and cropping] for more details.
77
+ struct CacheKey {
78
+ cudaVideoCodec codec_type;
79
+ uint32_t coded_width;
80
+ uint32_t coded_height;
81
+ cudaVideoChromaFormat chroma_format;
82
+ uint32_t bit_depth_luma_minus8;
83
+ uint8_t num_decode_surfaces;
84
+ cudaVideoSurfaceFormat output_surface_format;
85
+
86
+ CacheKey() = delete;
87
+
88
+ explicit CacheKey(
89
+ CUVIDEOFORMAT* video_format,
90
+ cudaVideoSurfaceFormat surface_fmt) {
91
+ STD_TORCH_CHECK(video_format != nullptr, "videoFormat must not be null");
92
+ codec_type = video_format->codec;
93
+ coded_width = video_format->coded_width;
94
+ coded_height = video_format->coded_height;
95
+ chroma_format = video_format->chroma_format;
96
+ bit_depth_luma_minus8 = video_format->bit_depth_luma_minus8;
97
+ num_decode_surfaces = video_format->min_num_decode_surfaces;
98
+ output_surface_format = surface_fmt;
99
+ }
100
+
101
+ CacheKey(const CacheKey&) = default;
102
+ CacheKey& operator=(const CacheKey&) = default;
103
+
104
+ bool operator<(const CacheKey& other) const {
105
+ return std::tie(
106
+ codec_type,
107
+ coded_width,
108
+ coded_height,
109
+ chroma_format,
110
+ bit_depth_luma_minus8,
111
+ num_decode_surfaces,
112
+ output_surface_format) <
113
+ std::tie(
114
+ other.codec_type,
115
+ other.coded_width,
116
+ other.coded_height,
117
+ other.chroma_format,
118
+ other.bit_depth_luma_minus8,
119
+ other.num_decode_surfaces,
120
+ other.output_surface_format);
121
+ }
122
+ };
123
+
124
+ NVDECCache() = default;
125
+ ~NVDECCache() = default;
126
+
127
+ void evict_lru_entry();
128
+
129
+ static NVDECCache* get_cache_instances();
130
+
131
+ std::multimap<CacheKey, CacheEntry> cache_;
132
+ std::mutex cache_lock_;
133
+ uint64_t last_used_counter_ = 0;
134
+ };
135
+
136
+ } // namespace facebook::torchcodec
@@ -0,0 +1,61 @@
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 "NVDECCacheConfig.h"
8
+ #include "StableABICompat.h"
9
+
10
+ #include <atomic>
11
+ #include <mutex>
12
+
13
+ #ifdef USE_CUDA
14
+ #include "CUDACommon.h"
15
+ #include "NVDECCache.h"
16
+ #endif
17
+
18
+ namespace facebook::torchcodec {
19
+
20
+ static std::atomic<int> g_nvdecCacheCapacity{DEFAULT_NVDEC_CACHE_CAPACITY};
21
+ // This mutex serializes setNVDECCacheCapacity() calls so that the atomic store
22
+ // and the subsequent cache eviction happen as one unit. getNVDECCacheCapacity()
23
+ // intentionally reads the atomic without this mutex: callers like
24
+ // returnDecoder() may briefly see a stale value during an ongoing
25
+ // setNVDECCacheCapacity(), which is acceptable because the worst case is a
26
+ // single decoder being added back to the cache after eviction. That entry will
27
+ // be consumed by a subsequent getDecoder() call or evicted by a future
28
+ // returnDecoder() or setNVDECCacheCapacity() call.
29
+ static std::mutex g_nvdecCacheCapacityMutex;
30
+
31
+ void set_nvdec_cache_capacity(int capacity) {
32
+ STD_TORCH_CHECK(
33
+ capacity >= 0,
34
+ "NVDEC cache capacity must be non-negative, got ",
35
+ capacity);
36
+ std::lock_guard<std::mutex> lock(g_nvdecCacheCapacityMutex);
37
+ g_nvdecCacheCapacity.store(capacity);
38
+ #ifdef USE_CUDA
39
+ NVDECCache::evict_excess_entries_across_devices(capacity);
40
+ #endif
41
+ }
42
+
43
+ int get_nvdec_cache_capacity() {
44
+ return g_nvdecCacheCapacity.load();
45
+ }
46
+
47
+ int get_nvdec_cache_size([[maybe_unused]] int device_index) {
48
+ #ifdef USE_CUDA
49
+ STD_TORCH_CHECK(
50
+ device_index >= 0 && device_index < MAX_CUDA_GPUS,
51
+ "device_index must be between 0 and ",
52
+ MAX_CUDA_GPUS - 1,
53
+ ", got ",
54
+ device_index);
55
+ return NVDECCache::get_cache_size_for_device(device_index);
56
+ #else
57
+ return 0;
58
+ #endif
59
+ }
60
+
61
+ } // namespace facebook::torchcodec
@@ -0,0 +1,30 @@
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
+ // This header is intentionally CUDA-free so it can be included from
10
+ // custom_ops.cpp which is compiled without CUDA headers.
11
+
12
+ namespace facebook::torchcodec {
13
+
14
+ // Default capacity of the per-device NVDEC decoder cache.
15
+ // capacity == maximum number of cached instances allowed.
16
+ constexpr int DEFAULT_NVDEC_CACHE_CAPACITY = 20;
17
+
18
+ // Set the capacity of the per-device NVDEC decoder cache.
19
+ // capacity must be non-negative.
20
+ void set_nvdec_cache_capacity(int capacity);
21
+
22
+ // Get the current capacity of the per-device NVDEC decoder cache.
23
+ int get_nvdec_cache_capacity();
24
+
25
+ // Get the current number of entries in the NVDEC decoder cache for a device.
26
+ // This is currently only used for tests, and not publicly exposed.
27
+ // TODO expose it?
28
+ int get_nvdec_cache_size(int device_index);
29
+
30
+ } // namespace facebook::torchcodec