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,699 @@
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
+ # FFmpeg-dependent ops, thin Python wrappers, and torch.compile abstract impls.
8
+ #
9
+ # This module is split out from ``ops.py`` and imported by it when FFmpeg is
10
+ # available.
11
+
12
+ import ctypes
13
+ import io
14
+ import json
15
+ import warnings
16
+
17
+ import torch
18
+ from torch.library import get_ctx, register_fake
19
+ from torchcodec._core._ffmpeg_op_names import FFMPEG_OP_NAMES
20
+ from torchcodec._core._file_like import create_file_like_context
21
+
22
+ __all__ = sorted(FFMPEG_OP_NAMES)
23
+
24
+
25
+ def _disallow_in_graph_when_compiling(op):
26
+ # We use disallow_in_graph because PyTorch does constant propagation of
27
+ # factory functions.
28
+ # Whether it's still needed is unclear and goes back to the time where we
29
+ # assumed torch.compile support would be needed. It... may not be.
30
+ # In any case, this is protected in a wrapper because we only want to import
31
+ # torch._dynamo if the user explicitly asks for compile support: importing
32
+ # torch._dynamo imports triton, which in turns re-enables the GIL on
33
+ # free-threaded Python.
34
+ def wrapper(*args, **kwargs):
35
+ if torch.compiler.is_compiling():
36
+ torch._dynamo.disallow_in_graph(op)
37
+ return op(*args, **kwargs)
38
+
39
+ return wrapper
40
+
41
+
42
+ create_from_file = _disallow_in_graph_when_compiling(
43
+ torch.ops.torchcodec_ns.create_from_file.default
44
+ )
45
+ create_from_tensor = _disallow_in_graph_when_compiling(
46
+ torch.ops.torchcodec_ns.create_from_tensor.default
47
+ )
48
+ _create_from_file_like = _disallow_in_graph_when_compiling(
49
+ torch.ops.torchcodec_ns._create_from_file_like.default
50
+ )
51
+ _add_video_stream_raw = torch.ops.torchcodec_ns.add_video_stream.default
52
+ _add_video_stream = torch.ops.torchcodec_ns._add_video_stream.default
53
+
54
+
55
+ def add_video_stream(
56
+ decoder: torch.Tensor,
57
+ *,
58
+ num_threads: int | None = None,
59
+ dimension_order: str | None = None,
60
+ stream_index: int | None = None,
61
+ device: str = "cpu",
62
+ device_variant: str = "default",
63
+ transform_specs: str = "",
64
+ custom_frame_mappings: (
65
+ tuple[torch.Tensor, torch.Tensor, torch.Tensor] | None
66
+ ) = None,
67
+ output_dtype: str = "uint8",
68
+ ) -> None:
69
+ custom_frame_mappings_pts: torch.Tensor | None = None
70
+ custom_frame_mappings_keyframe_indices: torch.Tensor | None = None
71
+ custom_frame_mappings_duration: torch.Tensor | None = None
72
+ if custom_frame_mappings is not None:
73
+ (
74
+ custom_frame_mappings_pts,
75
+ custom_frame_mappings_keyframe_indices,
76
+ custom_frame_mappings_duration,
77
+ ) = custom_frame_mappings
78
+ _add_video_stream_raw(
79
+ decoder,
80
+ num_threads=num_threads,
81
+ dimension_order=dimension_order,
82
+ stream_index=stream_index,
83
+ device=device,
84
+ device_variant=device_variant,
85
+ transform_specs=transform_specs,
86
+ custom_frame_mappings_pts=custom_frame_mappings_pts,
87
+ custom_frame_mappings_duration=custom_frame_mappings_duration,
88
+ custom_frame_mappings_keyframe_indices=custom_frame_mappings_keyframe_indices,
89
+ output_dtype=output_dtype,
90
+ )
91
+
92
+
93
+ add_audio_stream = torch.ops.torchcodec_ns.add_audio_stream.default
94
+ seek_to_pts = torch.ops.torchcodec_ns.seek_to_pts.default
95
+ get_next_frame = torch.ops.torchcodec_ns.get_next_frame.default
96
+ get_frame_at_pts = torch.ops.torchcodec_ns.get_frame_at_pts.default
97
+ get_frame_at_index = torch.ops.torchcodec_ns.get_frame_at_index.default
98
+ _get_frames_at_indices_tensor_input = (
99
+ torch.ops.torchcodec_ns.get_frames_at_indices.default
100
+ )
101
+ _get_frames_by_pts_tensor_input = torch.ops.torchcodec_ns.get_frames_by_pts.default
102
+ get_frames_in_range = torch.ops.torchcodec_ns.get_frames_in_range.default
103
+ get_frames_by_pts_in_range = torch.ops.torchcodec_ns.get_frames_by_pts_in_range.default
104
+ get_frames_by_pts_in_range_audio = (
105
+ torch.ops.torchcodec_ns.get_frames_by_pts_in_range_audio.default
106
+ )
107
+ get_json_metadata = torch.ops.torchcodec_ns.get_json_metadata.default
108
+
109
+ _blocks_create_demuxer_from_file = (
110
+ torch.ops.torchcodec_ns._blocks_create_demuxer_from_file.default
111
+ )
112
+ _blocks_create_demuxer_from_tensor = (
113
+ torch.ops.torchcodec_ns._blocks_create_demuxer_from_tensor.default
114
+ )
115
+ _blocks_create_demuxer_from_file_like_context = (
116
+ torch.ops.torchcodec_ns._blocks_create_demuxer_from_file_like.default
117
+ )
118
+ _blocks_demuxer_add_stream = torch.ops.torchcodec_ns._blocks_demuxer_add_stream.default
119
+ _blocks_demuxer_get_audio_video_stream_indices = (
120
+ torch.ops.torchcodec_ns._blocks_demuxer_get_audio_video_stream_indices.default
121
+ )
122
+ _blocks_demuxer_next_packet = (
123
+ torch.ops.torchcodec_ns._blocks_demuxer_next_packet.default
124
+ )
125
+ _blocks_demuxer_seek = torch.ops.torchcodec_ns._blocks_demuxer_seek.default
126
+ _blocks_demuxer_scan = torch.ops.torchcodec_ns._blocks_demuxer_scan.default
127
+ _blocks_demuxer_get_codec_parameters = (
128
+ torch.ops.torchcodec_ns._blocks_demuxer_get_codec_parameters.default
129
+ )
130
+ _blocks_create_packet_decoder = (
131
+ torch.ops.torchcodec_ns._blocks_create_packet_decoder.default
132
+ )
133
+ _blocks_create_packet = torch.ops.torchcodec_ns._blocks_create_packet.default
134
+ _blocks_packet_get_fields = torch.ops.torchcodec_ns._blocks_packet_get_fields.default
135
+ _blocks_packet_decoder_send_packet = (
136
+ torch.ops.torchcodec_ns._blocks_packet_decoder_send_packet.default
137
+ )
138
+ _blocks_packet_decoder_send_eof = (
139
+ torch.ops.torchcodec_ns._blocks_packet_decoder_send_eof.default
140
+ )
141
+ _blocks_packet_decoder_reset = (
142
+ torch.ops.torchcodec_ns._blocks_packet_decoder_reset.default
143
+ )
144
+ _blocks_packet_decoder_receive_frame = (
145
+ torch.ops.torchcodec_ns._blocks_packet_decoder_receive_frame.default
146
+ )
147
+ _blocks_audio_packet_decoder_receive_frame = (
148
+ torch.ops.torchcodec_ns._blocks_audio_packet_decoder_receive_frame.default
149
+ )
150
+ _blocks_create_color_converter = (
151
+ torch.ops.torchcodec_ns._blocks_create_color_converter.default
152
+ )
153
+ _blocks_convert_frame = torch.ops.torchcodec_ns._blocks_convert_frame.default
154
+ _blocks_create_audio_converter = (
155
+ torch.ops.torchcodec_ns._blocks_create_audio_converter.default
156
+ )
157
+ _blocks_audio_converter_convert = (
158
+ torch.ops.torchcodec_ns._blocks_audio_converter_convert.default
159
+ )
160
+ _blocks_audio_converter_drain = (
161
+ torch.ops.torchcodec_ns._blocks_audio_converter_drain.default
162
+ )
163
+ _blocks_audio_converter_reset = (
164
+ torch.ops.torchcodec_ns._blocks_audio_converter_reset.default
165
+ )
166
+ _blocks_frame_metadata = torch.ops.torchcodec_ns._blocks_frame_metadata.default
167
+ _blocks_get_ffmpeg_enum_name = (
168
+ torch.ops.torchcodec_ns._blocks_get_ffmpeg_enum_name.default
169
+ )
170
+ _blocks_frame_planes = torch.ops.torchcodec_ns._blocks_frame_planes.default
171
+
172
+ _test_frame_pts_equality = torch.ops.torchcodec_ns._test_frame_pts_equality.default
173
+ _get_key_frame_indices = torch.ops.torchcodec_ns._get_key_frame_indices.default
174
+ scan_all_streams_to_update_metadata = (
175
+ torch.ops.torchcodec_ns.scan_all_streams_to_update_metadata.default
176
+ )
177
+ _get_json_ffmpeg_library_versions = (
178
+ torch.ops.torchcodec_ns._get_json_ffmpeg_library_versions.default
179
+ )
180
+ _get_backend_details = torch.ops.torchcodec_ns._get_backend_details.default
181
+ create_streaming_encoder = _disallow_in_graph_when_compiling(
182
+ torch.ops.torchcodec_ns.create_streaming_encoder.default
183
+ )
184
+ streaming_encoder_close = torch.ops.torchcodec_ns.streaming_encoder_close.default
185
+ streaming_encoder_add_video_stream = (
186
+ torch.ops.torchcodec_ns.streaming_encoder_add_video_stream.default
187
+ )
188
+ streaming_encoder_add_audio_stream = (
189
+ torch.ops.torchcodec_ns.streaming_encoder_add_audio_stream.default
190
+ )
191
+ streaming_encoder_open_file = (
192
+ torch.ops.torchcodec_ns.streaming_encoder_open_file.default
193
+ )
194
+ _streaming_encoder_open_file_like = (
195
+ torch.ops.torchcodec_ns.streaming_encoder_open_file_like.default
196
+ )
197
+ streaming_encoder_add_frames = (
198
+ torch.ops.torchcodec_ns.streaming_encoder_add_frames.default
199
+ )
200
+ streaming_encoder_add_samples = (
201
+ torch.ops.torchcodec_ns.streaming_encoder_add_samples.default
202
+ )
203
+ set_nvdec_cache_capacity = torch.ops.torchcodec_ns.set_nvdec_cache_capacity.default
204
+ get_nvdec_cache_capacity = torch.ops.torchcodec_ns.get_nvdec_cache_capacity.default
205
+ _get_nvdec_cache_size = torch.ops.torchcodec_ns._get_nvdec_cache_size.default
206
+ _set_cpp_log_level = torch.ops.torchcodec_ns._set_cpp_log_level.default
207
+ _get_log_level = torch.ops.torchcodec_ns._get_log_level.default
208
+ create_wav_decoder_from_file = (
209
+ torch.ops.torchcodec_ns.create_wav_decoder_from_file.default
210
+ )
211
+ create_wav_decoder_from_tensor = (
212
+ torch.ops.torchcodec_ns.create_wav_decoder_from_tensor.default
213
+ )
214
+ _create_wav_decoder_from_file_like = (
215
+ torch.ops.torchcodec_ns._create_wav_decoder_from_file_like.default
216
+ )
217
+ get_wav_samples_in_range = torch.ops.torchcodec_ns.get_wav_samples_in_range.default
218
+
219
+
220
+ # =============================
221
+ # Functions not related to custom ops, but similar implementation to c++ ops
222
+ # =============================
223
+ def _bytes_to_tensor(data: bytes) -> torch.Tensor:
224
+ with warnings.catch_warnings():
225
+ # Ignore warning stating that the underlying buffer is non-writable.
226
+ warnings.filterwarnings("ignore", category=UserWarning)
227
+ return torch.frombuffer(data, dtype=torch.uint8)
228
+
229
+
230
+ def _json_tensor_to_str(tensor: torch.Tensor) -> str:
231
+ # See Note [Returning text from ops] in custom_ops.cpp.
232
+ return ctypes.string_at(tensor.data_ptr(), tensor.numel()).decode()
233
+
234
+
235
+ def _blocks_demuxer_container_json_metadata(demuxer: torch.Tensor) -> str:
236
+ return _json_tensor_to_str(
237
+ torch.ops.torchcodec_ns._blocks_demuxer_container_json_metadata.default(demuxer)
238
+ )
239
+
240
+
241
+ def _blocks_demuxer_stream_json_metadata(
242
+ demuxer: torch.Tensor, stream_index: int
243
+ ) -> str:
244
+ return _json_tensor_to_str(
245
+ torch.ops.torchcodec_ns._blocks_demuxer_stream_json_metadata.default(
246
+ demuxer, stream_index
247
+ )
248
+ )
249
+
250
+
251
+ def _get_container_json_metadata(decoder: torch.Tensor) -> str:
252
+ return _json_tensor_to_str(
253
+ torch.ops.torchcodec_ns.get_container_json_metadata.default(decoder)
254
+ )
255
+
256
+
257
+ def _get_stream_json_metadata(decoder: torch.Tensor, stream_index: int) -> str:
258
+ return _json_tensor_to_str(
259
+ torch.ops.torchcodec_ns.get_stream_json_metadata.default(decoder, stream_index)
260
+ )
261
+
262
+
263
+ def get_wav_metadata_from_decoder(decoder: torch.Tensor) -> str:
264
+ return _json_tensor_to_str(
265
+ torch.ops.torchcodec_ns.get_wav_metadata_from_decoder.default(decoder)
266
+ )
267
+
268
+
269
+ def create_from_bytes(video_bytes: bytes, seek_mode: str | None = None) -> torch.Tensor:
270
+ return create_from_tensor(_bytes_to_tensor(video_bytes), seek_mode)
271
+
272
+
273
+ def create_from_file_like(
274
+ file_like: io.RawIOBase | io.BufferedReader, seek_mode: str | None = None
275
+ ) -> torch.Tensor:
276
+ return _create_from_file_like(
277
+ create_file_like_context(file_like, False), # False means not for writing
278
+ seek_mode,
279
+ )
280
+
281
+
282
+ def _blocks_create_demuxer_from_bytes(video_bytes: bytes) -> torch.Tensor:
283
+ return _blocks_create_demuxer_from_tensor(_bytes_to_tensor(video_bytes))
284
+
285
+
286
+ def _blocks_create_demuxer_from_file_like(
287
+ file_like: io.RawIOBase | io.BufferedReader,
288
+ ) -> torch.Tensor:
289
+ return _blocks_create_demuxer_from_file_like_context(
290
+ create_file_like_context(file_like, False), # False means not for writing
291
+ )
292
+
293
+
294
+ def create_wav_decoder_from_bytes(wav_bytes: bytes) -> torch.Tensor:
295
+ return create_wav_decoder_from_tensor(_bytes_to_tensor(wav_bytes))
296
+
297
+
298
+ def create_wav_decoder_from_file_like(
299
+ file_like: io.RawIOBase | io.BufferedReader,
300
+ ) -> torch.Tensor:
301
+ return _create_wav_decoder_from_file_like(
302
+ create_file_like_context(file_like, False), # False means not for writing
303
+ )
304
+
305
+
306
+ def streaming_encoder_open_file_like(
307
+ encoder: torch.Tensor,
308
+ format: str,
309
+ file_like: io.RawIOBase | io.BufferedIOBase,
310
+ ) -> None:
311
+ _streaming_encoder_open_file_like(
312
+ encoder,
313
+ format,
314
+ create_file_like_context(file_like, True),
315
+ )
316
+
317
+
318
+ def get_frames_at_indices(
319
+ decoder: torch.Tensor, *, frame_indices: torch.Tensor | list[int]
320
+ ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
321
+ if isinstance(frame_indices, torch.Tensor):
322
+ # Ensure indices is the correct dtype (int64)
323
+ frame_indices = frame_indices.to(torch.int64)
324
+ else:
325
+ # Convert list to tensor for dispatch
326
+ frame_indices = torch.tensor(frame_indices)
327
+ return _get_frames_at_indices_tensor_input(decoder, frame_indices=frame_indices)
328
+
329
+
330
+ def get_frames_by_pts(
331
+ decoder: torch.Tensor, *, timestamps: torch.Tensor | list[float]
332
+ ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
333
+ if isinstance(timestamps, torch.Tensor):
334
+ # Ensure indices is the correct dtype (float64)
335
+ timestamps = timestamps.to(torch.float64)
336
+ else:
337
+ # Convert list to tensor for dispatch
338
+ try:
339
+ timestamps = torch.tensor(timestamps, dtype=torch.float64)
340
+ except Exception as e:
341
+ raise ValueError("Couldn't convert timestamps input to a tensor") from e
342
+ return _get_frames_by_pts_tensor_input(decoder, timestamps=timestamps)
343
+
344
+
345
+ # ==============================
346
+ # Abstract impl for the operators. Needed by torch.compile.
347
+ # ==============================
348
+ @register_fake("torchcodec_ns::create_from_file")
349
+ def create_from_file_abstract(filename: str, seek_mode: str | None) -> torch.Tensor:
350
+ return torch.empty([], dtype=torch.long)
351
+
352
+
353
+ @register_fake("torchcodec_ns::_create_from_file_like")
354
+ def _create_from_file_like_abstract(
355
+ file_like: int, seek_mode: str | None
356
+ ) -> torch.Tensor:
357
+ return torch.empty([], dtype=torch.long)
358
+
359
+
360
+ @register_fake("torchcodec_ns::create_from_tensor")
361
+ def create_from_tensor_abstract(
362
+ video_tensor: torch.Tensor, seek_mode: str | None
363
+ ) -> torch.Tensor:
364
+ return torch.empty([], dtype=torch.long)
365
+
366
+
367
+ @register_fake("torchcodec_ns::_add_video_stream")
368
+ def _add_video_stream_abstract(
369
+ decoder: torch.Tensor,
370
+ *,
371
+ num_threads: int | None = None,
372
+ dimension_order: str | None = None,
373
+ stream_index: int | None = None,
374
+ device: str = "cpu",
375
+ device_variant: str = "default",
376
+ transform_specs: str = "",
377
+ custom_frame_mappings_pts: torch.Tensor | None = None,
378
+ custom_frame_mappings_duration: torch.Tensor | None = None,
379
+ custom_frame_mappings_keyframe_indices: torch.Tensor | None = None,
380
+ color_conversion_library: str | None = None,
381
+ output_dtype: str = "uint8",
382
+ ) -> None:
383
+ return
384
+
385
+
386
+ @register_fake("torchcodec_ns::add_video_stream")
387
+ def add_video_stream_abstract(
388
+ decoder: torch.Tensor,
389
+ *,
390
+ num_threads: int | None = None,
391
+ dimension_order: str | None = None,
392
+ stream_index: int | None = None,
393
+ device: str = "cpu",
394
+ device_variant: str = "default",
395
+ transform_specs: str = "",
396
+ custom_frame_mappings_pts: torch.Tensor | None = None,
397
+ custom_frame_mappings_duration: torch.Tensor | None = None,
398
+ custom_frame_mappings_keyframe_indices: torch.Tensor | None = None,
399
+ output_dtype: str = "uint8",
400
+ ) -> None:
401
+ return
402
+
403
+
404
+ @register_fake("torchcodec_ns::add_audio_stream")
405
+ def add_audio_stream_abstract(
406
+ decoder: torch.Tensor,
407
+ *,
408
+ stream_index: int | None = None,
409
+ sample_rate: int | None = None,
410
+ num_channels: int | None = None,
411
+ ) -> None:
412
+ return
413
+
414
+
415
+ @register_fake("torchcodec_ns::seek_to_pts")
416
+ def seek_abstract(decoder: torch.Tensor, seconds: float) -> None:
417
+ return
418
+
419
+
420
+ @register_fake("torchcodec_ns::get_next_frame")
421
+ def get_next_frame_abstract(
422
+ decoder: torch.Tensor,
423
+ ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
424
+ # Images are 3 dimensions: height, width, channels.
425
+ # The exact permutation depends on the constructor options passed in.
426
+ image_size = [get_ctx().new_dynamic_size() for _ in range(3)]
427
+ return (
428
+ torch.empty(image_size),
429
+ torch.empty([], dtype=torch.float),
430
+ torch.empty([], dtype=torch.float),
431
+ )
432
+
433
+
434
+ @register_fake("torchcodec_ns::get_frame_at_pts")
435
+ def get_frame_at_pts_abstract(
436
+ decoder: torch.Tensor, seconds: float
437
+ ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
438
+ image_size = [get_ctx().new_dynamic_size() for _ in range(3)]
439
+ return (
440
+ torch.empty(image_size),
441
+ torch.empty([], dtype=torch.float),
442
+ torch.empty([], dtype=torch.float),
443
+ )
444
+
445
+
446
+ @register_fake("torchcodec_ns::get_frames_by_pts")
447
+ def get_frames_by_pts_abstract(
448
+ decoder: torch.Tensor,
449
+ *,
450
+ timestamps: torch.Tensor | list[float],
451
+ ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
452
+ image_size = [get_ctx().new_dynamic_size() for _ in range(4)]
453
+ return (
454
+ torch.empty(image_size),
455
+ torch.empty([], dtype=torch.float),
456
+ torch.empty([], dtype=torch.float),
457
+ )
458
+
459
+
460
+ @register_fake("torchcodec_ns::get_frame_at_index")
461
+ def get_frame_at_index_abstract(
462
+ decoder: torch.Tensor, *, frame_index: int
463
+ ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
464
+ image_size = [get_ctx().new_dynamic_size() for _ in range(3)]
465
+ return (
466
+ torch.empty(image_size),
467
+ torch.empty([], dtype=torch.float),
468
+ torch.empty([], dtype=torch.float),
469
+ )
470
+
471
+
472
+ @register_fake("torchcodec_ns::get_frames_at_indices")
473
+ def get_frames_at_indices_abstract(
474
+ decoder: torch.Tensor, *, frame_indices: torch.Tensor | list[int]
475
+ ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
476
+ image_size = [get_ctx().new_dynamic_size() for _ in range(4)]
477
+ return (
478
+ torch.empty(image_size),
479
+ torch.empty([], dtype=torch.float),
480
+ torch.empty([], dtype=torch.float),
481
+ )
482
+
483
+
484
+ @register_fake("torchcodec_ns::get_frames_in_range")
485
+ def get_frames_in_range_abstract(
486
+ decoder: torch.Tensor,
487
+ *,
488
+ start: int,
489
+ stop: int,
490
+ step: int | None = None,
491
+ ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
492
+ image_size = [get_ctx().new_dynamic_size() for _ in range(4)]
493
+ return (
494
+ torch.empty(image_size),
495
+ torch.empty([], dtype=torch.float),
496
+ torch.empty([], dtype=torch.float),
497
+ )
498
+
499
+
500
+ @register_fake("torchcodec_ns::get_frames_by_pts_in_range")
501
+ def get_frames_by_pts_in_range_abstract(
502
+ decoder: torch.Tensor,
503
+ *,
504
+ start_seconds: float,
505
+ stop_seconds: float,
506
+ fps: float | None = None,
507
+ ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
508
+ image_size = [get_ctx().new_dynamic_size() for _ in range(4)]
509
+ return (
510
+ torch.empty(image_size),
511
+ torch.empty([], dtype=torch.float),
512
+ torch.empty([], dtype=torch.float),
513
+ )
514
+
515
+
516
+ @register_fake("torchcodec_ns::get_frames_by_pts_in_range_audio")
517
+ def get_frames_by_pts_in_range_audio_abstract(
518
+ decoder: torch.Tensor,
519
+ *,
520
+ start_seconds: float,
521
+ stop_seconds: float | None = None,
522
+ ) -> tuple[torch.Tensor, torch.Tensor]:
523
+ image_size = [get_ctx().new_dynamic_size() for _ in range(4)]
524
+ return (torch.empty(image_size), torch.empty([], dtype=torch.float))
525
+
526
+
527
+ @register_fake("torchcodec_ns::_get_key_frame_indices")
528
+ def get_key_frame_indices_abstract(decoder: torch.Tensor) -> torch.Tensor:
529
+ return torch.empty([], dtype=torch.int)
530
+
531
+
532
+ @register_fake("torchcodec_ns::get_json_metadata")
533
+ def get_json_metadata_abstract(decoder: torch.Tensor) -> str:
534
+ return ""
535
+
536
+
537
+ @register_fake("torchcodec_ns::get_container_json_metadata")
538
+ def get_container_json_metadata_abstract(decoder: torch.Tensor) -> torch.Tensor:
539
+ return torch.empty(get_ctx().new_dynamic_size(), dtype=torch.uint8)
540
+
541
+
542
+ @register_fake("torchcodec_ns::get_stream_json_metadata")
543
+ def get_stream_json_metadata_abstract(
544
+ decoder: torch.Tensor, stream_idx: int
545
+ ) -> torch.Tensor:
546
+ return torch.empty(get_ctx().new_dynamic_size(), dtype=torch.uint8)
547
+
548
+
549
+ @register_fake("torchcodec_ns::_test_frame_pts_equality")
550
+ def _test_frame_pts_equality_abstract(
551
+ decoder: torch.Tensor,
552
+ *,
553
+ frame_index: int,
554
+ pts_seconds_to_test: float,
555
+ ) -> bool:
556
+ return False
557
+
558
+
559
+ @register_fake("torchcodec_ns::_get_json_ffmpeg_library_versions")
560
+ def _get_json_ffmpeg_library_versions_abstract() -> str:
561
+ return ""
562
+
563
+
564
+ @register_fake("torchcodec_ns::scan_all_streams_to_update_metadata")
565
+ def scan_all_streams_to_update_metadata_abstract(decoder: torch.Tensor) -> None:
566
+ return
567
+
568
+
569
+ def get_ffmpeg_library_versions():
570
+ versions_json = _get_json_ffmpeg_library_versions()
571
+ return json.loads(versions_json)
572
+
573
+
574
+ @register_fake("torchcodec_ns::_get_backend_details")
575
+ def _get_backend_details_abstract(decoder: torch.Tensor) -> str:
576
+ return ""
577
+
578
+
579
+ @register_fake("torchcodec_ns::create_streaming_encoder")
580
+ def _create_streaming_encoder_abstract() -> torch.Tensor:
581
+ return torch.empty([], dtype=torch.long)
582
+
583
+
584
+ @register_fake("torchcodec_ns::streaming_encoder_close")
585
+ def streaming_encoder_close_abstract(encoder: torch.Tensor) -> None:
586
+ return
587
+
588
+
589
+ @register_fake("torchcodec_ns::streaming_encoder_add_video_stream")
590
+ def streaming_encoder_add_video_stream_abstract(
591
+ encoder: torch.Tensor,
592
+ height: int,
593
+ width: int,
594
+ frame_rate: float,
595
+ device: str = "cpu",
596
+ codec: str | None = None,
597
+ pixel_format: str | None = None,
598
+ crf: float | None = None,
599
+ preset: str | None = None,
600
+ extra_options: list[str] | None = None,
601
+ ) -> int:
602
+ return 0
603
+
604
+
605
+ @register_fake("torchcodec_ns::streaming_encoder_add_audio_stream")
606
+ def streaming_encoder_add_audio_stream_abstract(
607
+ encoder: torch.Tensor,
608
+ sample_rate: int,
609
+ num_channels: int,
610
+ bit_rate: int | None = None,
611
+ output_num_channels: int | None = None,
612
+ output_sample_rate: int | None = None,
613
+ ) -> int:
614
+ return 0
615
+
616
+
617
+ @register_fake("torchcodec_ns::streaming_encoder_open_file")
618
+ def streaming_encoder_open_file_abstract(encoder: torch.Tensor, filename: str) -> None:
619
+ return
620
+
621
+
622
+ @register_fake("torchcodec_ns::streaming_encoder_open_file_like")
623
+ def streaming_encoder_open_file_like_abstract(
624
+ encoder: torch.Tensor, format: str, file_like_context: int
625
+ ) -> None:
626
+ return
627
+
628
+
629
+ @register_fake("torchcodec_ns::streaming_encoder_add_frames")
630
+ def streaming_encoder_add_frames_abstract(
631
+ encoder: torch.Tensor, frames: torch.Tensor, stream_index: int
632
+ ) -> None:
633
+ return
634
+
635
+
636
+ @register_fake("torchcodec_ns::streaming_encoder_add_samples")
637
+ def streaming_encoder_add_samples_abstract(
638
+ encoder: torch.Tensor, samples: torch.Tensor, stream_index: int
639
+ ) -> None:
640
+ return
641
+
642
+
643
+ @register_fake("torchcodec_ns::set_nvdec_cache_capacity")
644
+ def set_nvdec_cache_capacity_abstract(capacity: int) -> None:
645
+ return
646
+
647
+
648
+ @register_fake("torchcodec_ns::get_nvdec_cache_capacity")
649
+ def get_nvdec_cache_capacity_abstract() -> int:
650
+ return 0
651
+
652
+
653
+ @register_fake("torchcodec_ns::_get_nvdec_cache_size")
654
+ def _get_nvdec_cache_size_abstract(device_index: int) -> int:
655
+ return 0
656
+
657
+
658
+ @register_fake("torchcodec_ns::_set_cpp_log_level")
659
+ def _set_cpp_log_level_abstract(level: int) -> None:
660
+ return
661
+
662
+
663
+ @register_fake("torchcodec_ns::_get_log_level")
664
+ def _get_log_level_abstract() -> int:
665
+ return 0
666
+
667
+
668
+ @register_fake("torchcodec_ns::create_wav_decoder_from_file")
669
+ def create_wav_decoder_from_file_abstract(filename: str) -> torch.Tensor:
670
+ return torch.empty([], dtype=torch.long)
671
+
672
+
673
+ @register_fake("torchcodec_ns::create_wav_decoder_from_tensor")
674
+ def create_wav_decoder_from_tensor_abstract(data: torch.Tensor) -> torch.Tensor:
675
+ return torch.empty([], dtype=torch.long)
676
+
677
+
678
+ @register_fake("torchcodec_ns::_create_wav_decoder_from_file_like")
679
+ def _create_wav_decoder_from_file_like_abstract(
680
+ file_like_context: int,
681
+ ) -> torch.Tensor:
682
+ return torch.empty([], dtype=torch.long)
683
+
684
+
685
+ @register_fake("torchcodec_ns::get_wav_samples_in_range")
686
+ def get_wav_samples_in_range_abstract(
687
+ decoder: torch.Tensor, start_seconds: float, stop_seconds: float | None
688
+ ) -> tuple[torch.Tensor, torch.Tensor]:
689
+ sample_size = [
690
+ get_ctx().new_dynamic_size() for _ in range(2)
691
+ ] # [channels, samples]
692
+ frames = torch.empty(sample_size)
693
+ pts = torch.empty([], dtype=torch.float64)
694
+ return frames, pts
695
+
696
+
697
+ @register_fake("torchcodec_ns::get_wav_metadata_from_decoder")
698
+ def get_wav_metadata_from_decoder_abstract(decoder: torch.Tensor) -> torch.Tensor:
699
+ return torch.empty(get_ctx().new_dynamic_size(), dtype=torch.uint8)