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,426 @@
1
+ // @nolint (improperly imported third-party code)
2
+ /*****************************************************************************
3
+
4
+ GIF construction tools
5
+
6
+ ****************************************************************************/
7
+ // SPDX-License-Identifier: MIT
8
+ // SPDX-FileCopyrightText: Copyright (C) Eric S. Raymond <esr@thyrsus.com>
9
+
10
+ #include <stdio.h>
11
+ #include <stdlib.h>
12
+ #include <string.h>
13
+
14
+ #include "gif_lib.h"
15
+ #include "gif_lib_private.h"
16
+
17
+ #define MAX(x, y) (((x) > (y)) ? (x) : (y))
18
+
19
+ /******************************************************************************
20
+ Miscellaneous utility functions
21
+ ******************************************************************************/
22
+
23
+ /* return smallest bitfield size n will fit in */
24
+ int GifBitSize(int n) {
25
+ int i;
26
+
27
+ for (i = 1; i <= 8; i++) {
28
+ if ((1 << i) >= n) {
29
+ break;
30
+ }
31
+ }
32
+ return (i);
33
+ }
34
+
35
+ /******************************************************************************
36
+ Color map object functions
37
+ ******************************************************************************/
38
+
39
+ /*
40
+ * Allocate a color map of given size; initialize with contents of
41
+ * ColorMap if that pointer is non-NULL.
42
+ */
43
+ ColorMapObject *GifMakeMapObject(int ColorCount, const GifColorType *ColorMap) {
44
+ ColorMapObject *Object;
45
+
46
+ /*** FIXME: Our ColorCount has to be a power of two. Is it necessary to
47
+ * make the user know that or should we automatically round up instead?
48
+ */
49
+ if (ColorCount != (1 << GifBitSize(ColorCount))) {
50
+ return ((ColorMapObject *)NULL);
51
+ }
52
+
53
+ Object = (ColorMapObject *)malloc(sizeof(ColorMapObject));
54
+ if (Object == (ColorMapObject *)NULL) {
55
+ return ((ColorMapObject *)NULL);
56
+ }
57
+
58
+ Object->Colors =
59
+ (GifColorType *)calloc(ColorCount, sizeof(GifColorType));
60
+ if (Object->Colors == (GifColorType *)NULL) {
61
+ free(Object);
62
+ return ((ColorMapObject *)NULL);
63
+ }
64
+
65
+ Object->ColorCount = ColorCount;
66
+ Object->BitsPerPixel = GifBitSize(ColorCount);
67
+ Object->SortFlag = false;
68
+
69
+ if (ColorMap != NULL) {
70
+ memcpy((char *)Object->Colors, (char *)ColorMap,
71
+ ColorCount * sizeof(GifColorType));
72
+ }
73
+
74
+ return (Object);
75
+ }
76
+
77
+ /*******************************************************************************
78
+ Free a color map object
79
+ *******************************************************************************/
80
+ void GifFreeMapObject(ColorMapObject *Object) {
81
+ if (Object != NULL) {
82
+ (void)free(Object->Colors);
83
+ (void)free(Object);
84
+ }
85
+ }
86
+
87
+ #ifdef DEBUG
88
+ void DumpColorMap(ColorMapObject *Object, FILE *fp) {
89
+ if (Object != NULL) {
90
+ int i, j, Len = Object->ColorCount;
91
+
92
+ for (i = 0; i < Len; i += 4) {
93
+ for (j = 0; j < 4 && j < Len; j++) {
94
+ (void)fprintf(fp, "%3d: %02x %02x %02x ",
95
+ i + j, Object->Colors[i + j].Red,
96
+ Object->Colors[i + j].Green,
97
+ Object->Colors[i + j].Blue);
98
+ }
99
+ (void)fprintf(fp, "\n");
100
+ }
101
+ }
102
+ }
103
+ #endif /* DEBUG */
104
+
105
+ /*******************************************************************************
106
+ Compute the union of two given color maps and return it. If result can't
107
+ fit into 256 colors, NULL is returned, the allocated union otherwise.
108
+ ColorIn1 is copied as is to ColorUnion, while colors from ColorIn2 are
109
+ copied iff they didn't exist before. ColorTransIn2 maps the old
110
+ ColorIn2 into the ColorUnion color map table./
111
+ *******************************************************************************/
112
+ ColorMapObject *GifUnionColorMap(const ColorMapObject *ColorIn1,
113
+ const ColorMapObject *ColorIn2,
114
+ GifPixelType ColorTransIn2[]) {
115
+ int i, j, CrntSlot, RoundUpTo, NewGifBitSize;
116
+ ColorMapObject *ColorUnion;
117
+
118
+ /*
119
+ * We don't worry about duplicates within either color map; if
120
+ * the caller wants to resolve those, he can perform unions
121
+ * with an empty color map.
122
+ */
123
+
124
+ /* Allocate table which will hold the result for sure. */
125
+ ColorUnion = GifMakeMapObject(
126
+ MAX(ColorIn1->ColorCount, ColorIn2->ColorCount) * 2, NULL);
127
+
128
+ if (ColorUnion == NULL) {
129
+ return (NULL);
130
+ }
131
+
132
+ /*
133
+ * Copy ColorIn1 to ColorUnion.
134
+ */
135
+ for (i = 0; i < ColorIn1->ColorCount; i++) {
136
+ ColorUnion->Colors[i] = ColorIn1->Colors[i];
137
+ }
138
+ CrntSlot = ColorIn1->ColorCount;
139
+
140
+ /*
141
+ * Potentially obnoxious hack:
142
+ *
143
+ * Back CrntSlot down past all contiguous {0, 0, 0} slots at the end
144
+ * of table 1. This is very useful if your display is limited to
145
+ * 16 colors.
146
+ */
147
+ while (ColorIn1->Colors[CrntSlot - 1].Red == 0 &&
148
+ ColorIn1->Colors[CrntSlot - 1].Green == 0 &&
149
+ ColorIn1->Colors[CrntSlot - 1].Blue == 0) {
150
+ CrntSlot--;
151
+ }
152
+
153
+ /* Copy ColorIn2 to ColorUnion (use old colors if they exist): */
154
+ for (i = 0; i < ColorIn2->ColorCount && CrntSlot <= 256; i++) {
155
+ /* Let's see if this color already exists: */
156
+ for (j = 0; j < ColorIn1->ColorCount; j++) {
157
+ if (memcmp(&ColorIn1->Colors[j], &ColorIn2->Colors[i],
158
+ sizeof(GifColorType)) == 0) {
159
+ break;
160
+ }
161
+ }
162
+
163
+ if (j < ColorIn1->ColorCount) {
164
+ ColorTransIn2[i] = j; /* color exists in Color1 */
165
+ } else {
166
+ /* Color is new - copy it to a new slot: */
167
+ ColorUnion->Colors[CrntSlot] = ColorIn2->Colors[i];
168
+ ColorTransIn2[i] = CrntSlot++;
169
+ }
170
+ }
171
+
172
+ if (CrntSlot > 256) {
173
+ GifFreeMapObject(ColorUnion);
174
+ return ((ColorMapObject *)NULL);
175
+ }
176
+
177
+ NewGifBitSize = GifBitSize(CrntSlot);
178
+ RoundUpTo = (1 << NewGifBitSize);
179
+
180
+ if (RoundUpTo != ColorUnion->ColorCount) {
181
+ GifColorType *Map = ColorUnion->Colors;
182
+
183
+ /*
184
+ * Zero out slots up to next power of 2.
185
+ * We know these slots exist because of the way ColorUnion's
186
+ * start dimension was computed.
187
+ */
188
+ for (j = CrntSlot; j < RoundUpTo; j++) {
189
+ Map[j].Red = Map[j].Green = Map[j].Blue = 0;
190
+ }
191
+
192
+ /* perhaps we can shrink the map? */
193
+ if (RoundUpTo < ColorUnion->ColorCount) {
194
+ GifColorType *new_map = (GifColorType *)reallocarray(
195
+ Map, RoundUpTo, sizeof(GifColorType));
196
+ if (new_map == NULL) {
197
+ GifFreeMapObject(ColorUnion);
198
+ return ((ColorMapObject *)NULL);
199
+ }
200
+ ColorUnion->Colors = new_map;
201
+ }
202
+ }
203
+
204
+ ColorUnion->ColorCount = RoundUpTo;
205
+ ColorUnion->BitsPerPixel = NewGifBitSize;
206
+
207
+ return (ColorUnion);
208
+ }
209
+
210
+ /*******************************************************************************
211
+ Apply a given color translation to the raster bits of an image
212
+ *******************************************************************************/
213
+ void GifApplyTranslation(SavedImage *Image, const GifPixelType Translation[]) {
214
+ int i;
215
+ int RasterSize =
216
+ Image->ImageDesc.Height * Image->ImageDesc.Width;
217
+
218
+ for (i = 0; i < RasterSize; i++) {
219
+ Image->RasterBits[i] = Translation[Image->RasterBits[i]];
220
+ }
221
+ }
222
+
223
+ /******************************************************************************
224
+ Extension record functions
225
+ ******************************************************************************/
226
+ int GifAddExtensionBlock(int *ExtensionBlockCount,
227
+ ExtensionBlock **ExtensionBlocks, int Function,
228
+ unsigned int Len, unsigned char ExtData[]) {
229
+ ExtensionBlock *ep;
230
+
231
+ if (*ExtensionBlocks == NULL) {
232
+ *ExtensionBlocks =
233
+ (ExtensionBlock *)malloc(sizeof(ExtensionBlock));
234
+ } else {
235
+ ExtensionBlock *ep_new = (ExtensionBlock *)reallocarray(
236
+ *ExtensionBlocks, (*ExtensionBlockCount + 1),
237
+ sizeof(ExtensionBlock));
238
+ if (ep_new == NULL) {
239
+ return (GIF_ERROR);
240
+ }
241
+ *ExtensionBlocks = ep_new;
242
+ }
243
+
244
+ if (*ExtensionBlocks == NULL) {
245
+ return (GIF_ERROR);
246
+ }
247
+
248
+ ep = &(*ExtensionBlocks)[(*ExtensionBlockCount)++];
249
+
250
+ ep->Function = Function;
251
+ ep->ByteCount = Len;
252
+ ep->Bytes = (GifByteType *)malloc(ep->ByteCount);
253
+ if (ep->Bytes == NULL) {
254
+ return (GIF_ERROR);
255
+ }
256
+
257
+ if (ExtData != NULL) {
258
+ memcpy(ep->Bytes, ExtData, Len);
259
+ }
260
+
261
+ return (GIF_OK);
262
+ }
263
+
264
+ void GifFreeExtensions(int *ExtensionBlockCount,
265
+ ExtensionBlock **ExtensionBlocks) {
266
+ ExtensionBlock *ep;
267
+
268
+ if (*ExtensionBlocks == NULL) {
269
+ return;
270
+ }
271
+
272
+ for (ep = *ExtensionBlocks;
273
+ ep < (*ExtensionBlocks + *ExtensionBlockCount); ep++) {
274
+ (void)free((char *)ep->Bytes);
275
+ }
276
+ (void)free((char *)*ExtensionBlocks);
277
+ *ExtensionBlocks = NULL;
278
+ *ExtensionBlockCount = 0;
279
+ }
280
+
281
+ /******************************************************************************
282
+ Image block allocation functions
283
+ ******************************************************************************/
284
+
285
+ /* Private Function:
286
+ * Frees the last image in the GifFile->SavedImages array
287
+ */
288
+ void FreeLastSavedImage(GifFileType *GifFile) {
289
+ SavedImage *sp;
290
+
291
+ if ((GifFile == NULL) || (GifFile->SavedImages == NULL)) {
292
+ return;
293
+ }
294
+
295
+ /* Remove one SavedImage from the GifFile */
296
+ GifFile->ImageCount--;
297
+ sp = &GifFile->SavedImages[GifFile->ImageCount];
298
+
299
+ /* Deallocate its Colormap */
300
+ if (sp->ImageDesc.ColorMap != NULL) {
301
+ GifFreeMapObject(sp->ImageDesc.ColorMap);
302
+ sp->ImageDesc.ColorMap = NULL;
303
+ }
304
+
305
+ /* Deallocate the image data */
306
+ if (sp->RasterBits != NULL) {
307
+ free((char *)sp->RasterBits);
308
+ }
309
+
310
+ /* Deallocate any extensions */
311
+ GifFreeExtensions(&sp->ExtensionBlockCount, &sp->ExtensionBlocks);
312
+
313
+ /*** FIXME: We could realloc the GifFile->SavedImages structure but is
314
+ * there a point to it? Saves some memory but we'd have to do it every
315
+ * time. If this is used in GifFreeSavedImages then it would be
316
+ * inefficient (The whole array is going to be deallocated.) If we just
317
+ * use it when we want to free the last Image it's convenient to do it
318
+ * here.
319
+ */
320
+ }
321
+
322
+ /*
323
+ * Append an image block to the SavedImages array
324
+ */
325
+ SavedImage *GifMakeSavedImage(GifFileType *GifFile,
326
+ const SavedImage *CopyFrom) {
327
+ // cppcheck-suppress ctunullpointer
328
+ if (GifFile->SavedImages == NULL) {
329
+ GifFile->SavedImages = (SavedImage *)malloc(sizeof(SavedImage));
330
+ } else {
331
+ SavedImage *newSavedImages = (SavedImage *)reallocarray(
332
+ GifFile->SavedImages, (GifFile->ImageCount + 1),
333
+ sizeof(SavedImage));
334
+ if (newSavedImages == NULL) {
335
+ return ((SavedImage *)NULL);
336
+ }
337
+ GifFile->SavedImages = newSavedImages;
338
+ }
339
+ if (GifFile->SavedImages == NULL) {
340
+ return ((SavedImage *)NULL);
341
+ } else {
342
+ SavedImage *sp = &GifFile->SavedImages[GifFile->ImageCount++];
343
+
344
+ if (CopyFrom != NULL) {
345
+ memcpy((char *)sp, CopyFrom, sizeof(SavedImage));
346
+
347
+ /*
348
+ * Make our own allocated copies of the heap fields in
349
+ * the copied record. This guards against potential
350
+ * aliasing problems.
351
+ */
352
+
353
+ /* first, the local color map */
354
+ if (CopyFrom->ImageDesc.ColorMap != NULL) {
355
+ sp->ImageDesc.ColorMap = GifMakeMapObject(
356
+ CopyFrom->ImageDesc.ColorMap->ColorCount,
357
+ CopyFrom->ImageDesc.ColorMap->Colors);
358
+ if (sp->ImageDesc.ColorMap == NULL) {
359
+ FreeLastSavedImage(GifFile);
360
+ return (SavedImage *)(NULL);
361
+ }
362
+ }
363
+
364
+ /* next, the raster */
365
+ sp->RasterBits = (unsigned char *)reallocarray(
366
+ NULL,
367
+ (CopyFrom->ImageDesc.Height *
368
+ CopyFrom->ImageDesc.Width),
369
+ sizeof(GifPixelType));
370
+ if (sp->RasterBits == NULL) {
371
+ FreeLastSavedImage(GifFile);
372
+ return (SavedImage *)(NULL);
373
+ }
374
+ memcpy(sp->RasterBits, CopyFrom->RasterBits,
375
+ sizeof(GifPixelType) *
376
+ CopyFrom->ImageDesc.Height *
377
+ CopyFrom->ImageDesc.Width);
378
+
379
+ /* finally, the extension blocks */
380
+ if (CopyFrom->ExtensionBlocks != NULL) {
381
+ sp->ExtensionBlocks =
382
+ (ExtensionBlock *)reallocarray(
383
+ NULL, CopyFrom->ExtensionBlockCount,
384
+ sizeof(ExtensionBlock));
385
+ if (sp->ExtensionBlocks == NULL) {
386
+ FreeLastSavedImage(GifFile);
387
+ return (SavedImage *)(NULL);
388
+ }
389
+ memcpy(sp->ExtensionBlocks,
390
+ CopyFrom->ExtensionBlocks,
391
+ sizeof(ExtensionBlock) *
392
+ CopyFrom->ExtensionBlockCount);
393
+ }
394
+ } else {
395
+ memset((char *)sp, '\0', sizeof(SavedImage));
396
+ }
397
+
398
+ return (sp);
399
+ }
400
+ }
401
+
402
+ void GifFreeSavedImages(GifFileType *GifFile) {
403
+ SavedImage *sp;
404
+
405
+ if ((GifFile == NULL) || (GifFile->SavedImages == NULL)) {
406
+ return;
407
+ }
408
+ for (sp = GifFile->SavedImages;
409
+ sp < GifFile->SavedImages + GifFile->ImageCount; sp++) {
410
+ if (sp->ImageDesc.ColorMap != NULL) {
411
+ GifFreeMapObject(sp->ImageDesc.ColorMap);
412
+ sp->ImageDesc.ColorMap = NULL;
413
+ }
414
+
415
+ if (sp->RasterBits != NULL) {
416
+ free((char *)sp->RasterBits);
417
+ }
418
+
419
+ GifFreeExtensions(&sp->ExtensionBlockCount,
420
+ &sp->ExtensionBlocks);
421
+ }
422
+ free((char *)GifFile->SavedImages);
423
+ GifFile->SavedImages = NULL;
424
+ }
425
+
426
+ /* end */
@@ -0,0 +1,74 @@
1
+ // @nolint (improperly imported third-party code)
2
+ /*
3
+ * SPDX-FileCopyrightText: Copyright (C) 2008 Otto Moerbeek <otto@drijf.net>
4
+ * SPDX-License-Identifier: MIT
5
+ */
6
+
7
+ #include <errno.h>
8
+ #include <stdint.h>
9
+ #include <stdlib.h>
10
+ #include <sys/types.h>
11
+
12
+ #ifndef SIZE_MAX
13
+ #define SIZE_MAX UINTPTR_MAX
14
+ #endif
15
+
16
+ /*
17
+ * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
18
+ * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
19
+ */
20
+ #define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))
21
+
22
+ void *openbsd_reallocarray(void *optr, size_t nmemb, size_t size) {
23
+ if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
24
+ nmemb > 0 && SIZE_MAX / nmemb < size) {
25
+ errno = ENOMEM;
26
+ return NULL;
27
+ }
28
+ /*
29
+ * Head off variations in realloc behavior on different
30
+ * platforms (reported by MarkR <mrogers6@users.sf.net>)
31
+ *
32
+ * The behaviour of reallocarray is implementation-defined if
33
+ * nmemb or size is zero. It can return NULL or non-NULL
34
+ * depending on the platform.
35
+ * https://www.securecoding.cert.org/confluence/display/c/MEM04-C.Beware+of+zero-lengthallocations
36
+ *
37
+ * Here are some extracts from realloc man pages on different platforms.
38
+ *
39
+ * void realloc( void memblock, size_t size );
40
+ *
41
+ * Windows:
42
+ *
43
+ * If there is not enough available memory to expand the block
44
+ * to the given size, the original block is left unchanged,
45
+ * and NULL is returned. If size is zero, then the block
46
+ * pointed to by memblock is freed; the return value is NULL,
47
+ * and memblock is left pointing at a freed block.
48
+ *
49
+ * OpenBSD:
50
+ *
51
+ * If size or nmemb is equal to 0, a unique pointer to an
52
+ * access protected, zero sized object is returned. Access via
53
+ * this pointer will generate a SIGSEGV exception.
54
+ *
55
+ * Linux:
56
+ *
57
+ * If size was equal to 0, either NULL or a pointer suitable
58
+ * to be passed to free() is returned.
59
+ *
60
+ * OS X:
61
+ *
62
+ * If size is zero and ptr is not NULL, a new, minimum sized
63
+ * object is allocated and the original object is freed.
64
+ *
65
+ * It looks like images with zero width or height can trigger
66
+ * this, and fuzzing behaviour will differ by platform, so
67
+ * fuzzing on one platform may not detect zero-size allocation
68
+ * problems on other platforms.
69
+ */
70
+ if (size == 0 || nmemb == 0) {
71
+ return NULL;
72
+ }
73
+ return realloc(optr, size * nmemb);
74
+ }
@@ -0,0 +1,20 @@
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 "DecodeHeic.h"
8
+ #include "StableABICompat.h"
9
+
10
+ namespace facebook::torchcodec {
11
+
12
+ STABLE_TORCH_LIBRARY_FRAGMENT(torchcodec_ns, m) {
13
+ m.def("decode_heic(Tensor input, int mode, int output_dtype=0) -> Tensor");
14
+ }
15
+
16
+ STABLE_TORCH_LIBRARY_IMPL(torchcodec_ns, CPU, m) {
17
+ m.impl("decode_heic", TORCH_BOX(&decode_heic));
18
+ }
19
+
20
+ } // namespace facebook::torchcodec
@@ -0,0 +1,137 @@
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
+ // PyTorch custom-op registration for the image decoders. This lives in the
8
+ // libtorchcodec_image library, which is FFmpeg-free and loaded in its own
9
+ // RTLD_LOCAL symbol group (see load_torchcodec_shared_libraries): that keeps
10
+ // our bundled image codec libs (libjpeg/libpng/libwebp) isolated from the codec
11
+ // libs pulled in by the user's FFmpeg, so they can't collide.
12
+
13
+ #include <memory>
14
+ #include <string>
15
+
16
+ #include "DecodeAvif.h"
17
+ #include "DecodeGif.h"
18
+ #include "DecodeJpeg.h"
19
+ #include "DecodeJpegCuda.h"
20
+ #include "DecodePng.h"
21
+ #include "DecodeWebp.h"
22
+ #include "EncodeJpeg.h"
23
+ #include "EncodeJpegCuda.h"
24
+ #include "EncodePng.h"
25
+ #include "FileIO.h"
26
+ #include "FileLikeIO.h"
27
+ #include "IOInterface.h"
28
+ #include "StableABICompat.h"
29
+
30
+ namespace facebook::torchcodec {
31
+
32
+ namespace {
33
+
34
+ void encode_png_to_file(
35
+ const torch::stable::Tensor& img,
36
+ std::string filename,
37
+ int64_t compression_level) {
38
+ FileIO interface(filename, FileIO::Mode::Write);
39
+ encode_png(img, compression_level, interface);
40
+ }
41
+
42
+ void encode_png_to_file_like(
43
+ const torch::stable::Tensor& img,
44
+ int64_t file_like_context,
45
+ int64_t compression_level) {
46
+ auto interface = adopt_file_like_context(file_like_context);
47
+ encode_png(img, compression_level, *interface);
48
+ }
49
+
50
+ void encode_jpeg_dispatch(
51
+ const torch::stable::Tensor& img,
52
+ int64_t quality,
53
+ IOInterface& interface) {
54
+ if (img.device().is_cuda()) {
55
+ encode_jpeg_cuda(img, quality, interface);
56
+ } else {
57
+ encode_jpeg(img, quality, interface);
58
+ }
59
+ }
60
+
61
+ void encode_jpeg_to_file(
62
+ const torch::stable::Tensor& img,
63
+ std::string filename,
64
+ int64_t quality) {
65
+ FileIO interface(filename, FileIO::Mode::Write);
66
+ encode_jpeg_dispatch(img, quality, interface);
67
+ }
68
+
69
+ void encode_jpeg_to_file_like(
70
+ const torch::stable::Tensor& img,
71
+ int64_t file_like_context,
72
+ int64_t quality) {
73
+ auto interface = adopt_file_like_context(file_like_context);
74
+ encode_jpeg_dispatch(img, quality, *interface);
75
+ }
76
+
77
+ } // namespace
78
+
79
+ STABLE_TORCH_LIBRARY_FRAGMENT(torchcodec_ns, m) {
80
+ m.def("decode_jpeg(Tensor input, int mode) -> Tensor");
81
+ m.def("decode_png(Tensor input, int mode, int output_dtype=0) -> Tensor");
82
+ m.def("decode_webp(Tensor input, int mode) -> Tensor");
83
+ m.def("decode_gif(Tensor input, int mode) -> Tensor");
84
+ m.def(
85
+ "decode_avif(Tensor input, int mode, int output_dtype=0, int num_threads=1) -> Tensor");
86
+ m.def(
87
+ "decode_jpegs_cuda(Tensor[] encoded_images, int mode, Device device) -> Tensor[]");
88
+ m.def(
89
+ "encode_png_to_file(Tensor img, str filename, int compression_level) -> ()");
90
+ m.def(
91
+ "encode_png_to_file_like(Tensor img, int file_like_context, int compression_level) -> ()");
92
+ m.def("encode_jpeg_to_file(Tensor img, str filename, int quality) -> ()");
93
+ m.def(
94
+ "encode_jpeg_to_file_like(Tensor img, int file_like_context, int quality) -> ()");
95
+ m.def("encode_jpeg_to_tensor_cuda(Tensor img, int quality) -> Tensor");
96
+ m.def("probe_gif_from_tensor(Tensor input) -> int[]");
97
+ m.def("probe_avif_from_tensor(Tensor input) -> int[]");
98
+ m.def("probe_avif_from_file(str filename) -> int[]");
99
+ m.def("probe_gif_from_file(str filename) -> int[]");
100
+ m.def("png_skip_idat_chunks_from_tensor(Tensor input, int offset) -> int");
101
+ m.def("png_skip_idat_chunks_from_file(str filename, int offset) -> int");
102
+ }
103
+
104
+ STABLE_TORCH_LIBRARY_IMPL(torchcodec_ns, CPU, m) {
105
+ m.impl("decode_jpeg", TORCH_BOX(&decode_jpeg));
106
+ m.impl("decode_png", TORCH_BOX(&decode_png));
107
+ m.impl("decode_webp", TORCH_BOX(&decode_webp));
108
+ m.impl("decode_gif", TORCH_BOX(&decode_gif));
109
+ m.impl("decode_avif", TORCH_BOX(&decode_avif));
110
+ m.impl("encode_png_to_file", TORCH_BOX(&encode_png_to_file));
111
+ m.impl("encode_png_to_file_like", TORCH_BOX(&encode_png_to_file_like));
112
+ m.impl("probe_gif_from_tensor", TORCH_BOX(&probe_gif_from_tensor));
113
+ m.impl("probe_avif_from_tensor", TORCH_BOX(&probe_avif_from_tensor));
114
+ m.impl(
115
+ "png_skip_idat_chunks_from_tensor",
116
+ TORCH_BOX(&png_skip_idat_chunks_from_tensor));
117
+ }
118
+
119
+ STABLE_TORCH_LIBRARY_IMPL(torchcodec_ns, BackendSelect, m) {
120
+ m.impl("probe_avif_from_file", TORCH_BOX(&probe_avif_from_file));
121
+ m.impl("probe_gif_from_file", TORCH_BOX(&probe_gif_from_file));
122
+ m.impl(
123
+ "png_skip_idat_chunks_from_file",
124
+ TORCH_BOX(&png_skip_idat_chunks_from_file));
125
+ }
126
+
127
+ // The JPEG encoders are registered as CompositeExplicitAutograd (rather than
128
+ // CPU) because they accept both CPU and CUDA tensors and dispatch on the device
129
+ // internally (see encode_jpeg_dispatch).
130
+ STABLE_TORCH_LIBRARY_IMPL(torchcodec_ns, CompositeExplicitAutograd, m) {
131
+ m.impl("decode_jpegs_cuda", TORCH_BOX(&decode_jpegs_cuda));
132
+ m.impl("encode_jpeg_to_file", TORCH_BOX(&encode_jpeg_to_file));
133
+ m.impl("encode_jpeg_to_file_like", TORCH_BOX(&encode_jpeg_to_file_like));
134
+ m.impl("encode_jpeg_to_tensor_cuda", TORCH_BOX(&encode_jpeg_to_tensor_cuda));
135
+ }
136
+
137
+ } // namespace facebook::torchcodec