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,207 @@
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 "EncodePng.h"
8
+
9
+ #include <torch/csrc/stable/library.h>
10
+ #include <torch/csrc/stable/ops.h>
11
+ #include <torch/headeronly/util/Exception.h>
12
+
13
+ #include "StableABICompat.h"
14
+
15
+ #if !TORCHCODEC_ENABLE_PNG
16
+
17
+ namespace facebook::torchcodec {
18
+
19
+ void encode_png(
20
+ [[maybe_unused]] const torch::stable::Tensor& img,
21
+ [[maybe_unused]] int64_t compression_level,
22
+ [[maybe_unused]] IOInterface& interface) {
23
+ STD_TORCH_CHECK(
24
+ false,
25
+ "encode_png: torchcodec was not compiled with libpng support. Rebuild "
26
+ "torchcodec with TORCHCODEC_BUILD_PNG=1. If you see this error in a "
27
+ "prebuilt wheel, please report it to the TorchCodec repo.");
28
+ }
29
+
30
+ } // namespace facebook::torchcodec
31
+
32
+ #else
33
+
34
+ #include <png.h>
35
+ #include <setjmp.h>
36
+
37
+ #include <cstdint>
38
+ #include <cstdio>
39
+ #include <exception>
40
+
41
+ namespace facebook::torchcodec {
42
+
43
+ namespace {
44
+
45
+ struct ErrorCtx {
46
+ char error_message[256] = "";
47
+ };
48
+
49
+ void error_callback(png_structp png_ptr, png_const_charp error_message) {
50
+ auto* error_ctx = static_cast<ErrorCtx*>(png_get_error_ptr(png_ptr));
51
+ if (error_ctx != nullptr) {
52
+ std::snprintf(
53
+ error_ctx->error_message,
54
+ sizeof(error_ctx->error_message),
55
+ "%s",
56
+ error_message);
57
+ }
58
+ png_longjmp(png_ptr, 1);
59
+ }
60
+
61
+ struct IOCtx {
62
+ IOInterface* interface;
63
+ std::exception_ptr interface_exception;
64
+ };
65
+
66
+ // libpng calls write_callback with chunks of encoded output, which we forward
67
+ // to the IOInterface. The interface->write() call may throw (e.g. a Python
68
+ // file-like raising, or a disk error), but a C++ exception must not unwind
69
+ // through libpng's C stack. So we capture it and abort encoding by
70
+ // png_longjmp-ing to the setjmp point in write_png_to_io, which rethrows it.
71
+ void write_callback(png_structp png_ptr, png_bytep data, png_size_t length) {
72
+ auto* io_ctx = static_cast<IOCtx*>(png_get_io_ptr(png_ptr));
73
+ try {
74
+ io_ctx->interface->write(
75
+ reinterpret_cast<const uint8_t*>(data), static_cast<int>(length));
76
+ return;
77
+ } catch (...) {
78
+ io_ctx->interface_exception = std::current_exception();
79
+ }
80
+ png_longjmp(png_ptr, 1);
81
+ }
82
+
83
+ void write_png_to_io(
84
+ png_structp& png_write,
85
+ png_infop& info_ptr,
86
+ ErrorCtx& error_ctx,
87
+ IOCtx& io_ctx,
88
+ const uint8_t* input_ptr,
89
+ int64_t width,
90
+ int64_t height,
91
+ int64_t num_channels,
92
+ int64_t compression_level) {
93
+ if (setjmp(png_jmpbuf(png_write)) != 0) {
94
+ png_destroy_write_struct(&png_write, &info_ptr);
95
+ // We land here on either a libpng error (via error_callback) or a failed
96
+ // write (via write_callback). The latter sets interface_exception, which we
97
+ // rethrow to surface the original exception.
98
+ if (io_ctx.interface_exception) {
99
+ std::rethrow_exception(io_ctx.interface_exception);
100
+ } else {
101
+ STD_TORCH_CHECK(false, "encode_png failed: ", error_ctx.error_message);
102
+ }
103
+ }
104
+
105
+ png_set_write_fn(png_write, &io_ctx, write_callback, /*flush_fn=*/nullptr);
106
+
107
+ const int color_type =
108
+ (num_channels == 1) ? PNG_COLOR_TYPE_GRAY : PNG_COLOR_TYPE_RGB;
109
+ png_set_IHDR(
110
+ png_write,
111
+ info_ptr,
112
+ static_cast<png_uint_32>(width),
113
+ static_cast<png_uint_32>(height),
114
+ /*bit_depth=*/8,
115
+ color_type,
116
+ PNG_INTERLACE_NONE,
117
+ PNG_COMPRESSION_TYPE_DEFAULT,
118
+ PNG_FILTER_TYPE_DEFAULT);
119
+ png_set_compression_level(png_write, static_cast<int>(compression_level));
120
+ png_write_info(png_write, info_ptr);
121
+
122
+ const int64_t stride = width * num_channels;
123
+ for (int64_t row = 0; row < height; ++row) {
124
+ png_write_row(png_write, input_ptr + row * stride);
125
+ }
126
+ png_write_end(png_write, info_ptr);
127
+ }
128
+
129
+ } // namespace
130
+
131
+ // Important: see Note [libjpeg error handling] in the jpeg decoder: everything
132
+ // applies here too. We must not throw a C++ exception through libpng's C stack
133
+ // (and callbacks), and we must not allocate anything that needs proper
134
+ // destruction in a function that defines a setjmp() point. The IOCtx
135
+ // (which owns a std::exception_ptr) therefore lives here, outside
136
+ // write_png_to_io's setjmp.
137
+ void encode_png(
138
+ const torch::stable::Tensor& img,
139
+ int64_t compression_level,
140
+ IOInterface& interface) {
141
+ STD_TORCH_CHECK(
142
+ compression_level >= 0 && compression_level <= 9,
143
+ "Compression level should be between 0 and 9, got ",
144
+ compression_level,
145
+ ".");
146
+ STD_TORCH_CHECK(
147
+ img.device().type() == kStableCPU,
148
+ "Input tensor must be on the CPU, got a tensor on ",
149
+ device_type_name(img.device().type()),
150
+ ".");
151
+ STD_TORCH_CHECK(
152
+ img.scalar_type() == kStableUInt8,
153
+ "Input tensor must have uint8 data type, got ",
154
+ torch::headeronly::toString(img.scalar_type()),
155
+ ".");
156
+ STD_TORCH_CHECK(
157
+ img.dim() == 3,
158
+ "Input tensor must be a 3-dimensional (C, H, W) tensor, got ",
159
+ img.dim(),
160
+ " dimensions.");
161
+
162
+ const int64_t num_channels = img.size(0);
163
+ const int64_t height = img.size(1);
164
+ const int64_t width = img.size(2);
165
+ STD_TORCH_CHECK(
166
+ num_channels == 1 || num_channels == 3,
167
+ "The number of channels should be 1 or 3, got ",
168
+ num_channels,
169
+ ".");
170
+
171
+ // libpng writes rows in HWC interleaved order, so permute from CHW. This
172
+ // contiguous copy is kept alive for the whole encoding below.
173
+ auto input = torch::stable::contiguous(stable_permute(img, {1, 2, 0}));
174
+
175
+ auto png_write =
176
+ png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
177
+ STD_TORCH_CHECK(
178
+ png_write != nullptr, "libpng write structure allocation failed!");
179
+ auto info_ptr = png_create_info_struct(png_write);
180
+ if (info_ptr == nullptr) {
181
+ png_destroy_write_struct(&png_write, nullptr);
182
+ STD_TORCH_CHECK(false, "libpng info structure allocation failed!");
183
+ }
184
+
185
+ ErrorCtx error_ctx;
186
+ png_set_error_fn(png_write, &error_ctx, error_callback, /*warn_fn=*/nullptr);
187
+
188
+ IOCtx io_ctx;
189
+ io_ctx.interface = &interface;
190
+
191
+ write_png_to_io(
192
+ png_write,
193
+ info_ptr,
194
+ error_ctx,
195
+ io_ctx,
196
+ input.const_data_ptr<uint8_t>(),
197
+ width,
198
+ height,
199
+ num_channels,
200
+ compression_level);
201
+
202
+ png_destroy_write_struct(&png_write, &info_ptr);
203
+ }
204
+
205
+ } // namespace facebook::torchcodec
206
+
207
+ #endif // !TORCHCODEC_ENABLE_PNG
@@ -0,0 +1,19 @@
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 "IOInterface.h"
10
+ #include "StableABICompat.h"
11
+
12
+ namespace facebook::torchcodec {
13
+
14
+ FORCE_PUBLIC_VISIBILITY void encode_png(
15
+ const torch::stable::Tensor& img,
16
+ int64_t compression_level,
17
+ IOInterface& interface);
18
+
19
+ } // namespace facebook::torchcodec