torchcodec 1.0.0__py3-none-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- torchcodec/__init__.py +50 -0
- torchcodec/_core/AVIOContextHolder.cpp +85 -0
- torchcodec/_core/AVIOContextHolder.h +44 -0
- torchcodec/_core/AudioCommon.cpp +101 -0
- torchcodec/_core/AudioCommon.h +48 -0
- torchcodec/_core/AudioConverter.cpp +130 -0
- torchcodec/_core/AudioConverter.h +59 -0
- torchcodec/_core/BetaCudaDeviceInterface.cpp +1635 -0
- torchcodec/_core/BetaCudaDeviceInterface.h +364 -0
- torchcodec/_core/CMakeLists.txt +661 -0
- torchcodec/_core/CUDACommon.cpp +129 -0
- torchcodec/_core/CUDACommon.h +115 -0
- torchcodec/_core/Cache.h +126 -0
- torchcodec/_core/CodecParameters.cpp +98 -0
- torchcodec/_core/CodecParameters.h +59 -0
- torchcodec/_core/ColorConverter.cpp +92 -0
- torchcodec/_core/ColorConverter.h +40 -0
- torchcodec/_core/CpuDeviceInterface.cpp +654 -0
- torchcodec/_core/CpuDeviceInterface.h +153 -0
- torchcodec/_core/CudaDeviceInterface.cpp +519 -0
- torchcodec/_core/CudaDeviceInterface.h +92 -0
- torchcodec/_core/DecodeAvif.cpp +284 -0
- torchcodec/_core/DecodeAvif.h +28 -0
- torchcodec/_core/DecodeGif.cpp +488 -0
- torchcodec/_core/DecodeGif.h +32 -0
- torchcodec/_core/DecodeHeic.cpp +306 -0
- torchcodec/_core/DecodeHeic.h +18 -0
- torchcodec/_core/DecodeJpeg.cpp +489 -0
- torchcodec/_core/DecodeJpeg.h +17 -0
- torchcodec/_core/DecodeJpegCuda.cpp +638 -0
- torchcodec/_core/DecodeJpegCuda.h +92 -0
- torchcodec/_core/DecodePng.cpp +569 -0
- torchcodec/_core/DecodePng.h +31 -0
- torchcodec/_core/DecodeWebp.cpp +238 -0
- torchcodec/_core/DecodeWebp.h +17 -0
- torchcodec/_core/Demuxer.cpp +415 -0
- torchcodec/_core/Demuxer.h +158 -0
- torchcodec/_core/DeviceInterface.cpp +152 -0
- torchcodec/_core/DeviceInterface.h +255 -0
- torchcodec/_core/EncodeJpeg.cpp +223 -0
- torchcodec/_core/EncodeJpeg.h +19 -0
- torchcodec/_core/EncodeJpegCuda.cpp +272 -0
- torchcodec/_core/EncodeJpegCuda.h +56 -0
- torchcodec/_core/EncodePng.cpp +207 -0
- torchcodec/_core/EncodePng.h +19 -0
- torchcodec/_core/Encoder.cpp +1023 -0
- torchcodec/_core/Encoder.h +144 -0
- torchcodec/_core/Exif.h +241 -0
- torchcodec/_core/FFMPEGCommon.cpp +1159 -0
- torchcodec/_core/FFMPEGCommon.h +506 -0
- torchcodec/_core/FileIO.cpp +78 -0
- torchcodec/_core/FileIO.h +34 -0
- torchcodec/_core/FileLikeIO.cpp +76 -0
- torchcodec/_core/FileLikeIO.h +68 -0
- torchcodec/_core/FilterGraph.cpp +211 -0
- torchcodec/_core/FilterGraph.h +59 -0
- torchcodec/_core/Frame.cpp +75 -0
- torchcodec/_core/Frame.h +76 -0
- torchcodec/_core/IOInterface.h +44 -0
- torchcodec/_core/ImageCommon.h +113 -0
- torchcodec/_core/Logging.cpp +42 -0
- torchcodec/_core/Logging.h +38 -0
- torchcodec/_core/Metadata.cpp +312 -0
- torchcodec/_core/Metadata.h +115 -0
- torchcodec/_core/NVCUVIDRuntimeLoader.cpp +324 -0
- torchcodec/_core/NVCUVIDRuntimeLoader.h +14 -0
- torchcodec/_core/NVDECCache.cpp +111 -0
- torchcodec/_core/NVDECCache.h +136 -0
- torchcodec/_core/NVDECCacheConfig.cpp +61 -0
- torchcodec/_core/NVDECCacheConfig.h +30 -0
- torchcodec/_core/PacketDecoder.cpp +358 -0
- torchcodec/_core/PacketDecoder.h +140 -0
- torchcodec/_core/SingleStreamDecoder.cpp +1856 -0
- torchcodec/_core/SingleStreamDecoder.h +424 -0
- torchcodec/_core/StableABICompat.h +213 -0
- torchcodec/_core/StreamOptions.h +93 -0
- torchcodec/_core/SwScale.cpp +124 -0
- torchcodec/_core/SwScale.h +67 -0
- torchcodec/_core/TensorIO.cpp +136 -0
- torchcodec/_core/TensorIO.h +53 -0
- torchcodec/_core/Transform.cpp +207 -0
- torchcodec/_core/Transform.h +125 -0
- torchcodec/_core/ValidationUtils.cpp +65 -0
- torchcodec/_core/ValidationUtils.h +29 -0
- torchcodec/_core/WavDecoder.cpp +494 -0
- torchcodec/_core/WavDecoder.h +73 -0
- torchcodec/_core/__init__.py +45 -0
- torchcodec/_core/_decoder_utils.py +258 -0
- torchcodec/_core/_ffmpeg_op_names.py +95 -0
- torchcodec/_core/_ffmpeg_ops.py +699 -0
- torchcodec/_core/_file_like.py +221 -0
- torchcodec/_core/_metadata.py +402 -0
- torchcodec/_core/color_conversion.cpp +316 -0
- torchcodec/_core/color_conversion.cu +391 -0
- torchcodec/_core/color_conversion.h +150 -0
- torchcodec/_core/custom_ops.cpp +2438 -0
- torchcodec/_core/fetch_and_expose_non_gpl_ffmpeg_libs.cmake +192 -0
- torchcodec/_core/fetch_avif_from_s3.cmake +81 -0
- torchcodec/_core/giflib/README +28 -0
- torchcodec/_core/giflib/dgif_lib.c +1313 -0
- torchcodec/_core/giflib/gif_hash.c +129 -0
- torchcodec/_core/giflib/gif_hash.h +43 -0
- torchcodec/_core/giflib/gif_lib.h +292 -0
- torchcodec/_core/giflib/gif_lib_private.h +73 -0
- torchcodec/_core/giflib/gifalloc.c +426 -0
- torchcodec/_core/giflib/openbsd-reallocarray.c +74 -0
- torchcodec/_core/heic_custom_ops.cpp +20 -0
- torchcodec/_core/image_custom_ops.cpp +137 -0
- torchcodec/_core/nvcuvid_include/cuviddec.h +1374 -0
- torchcodec/_core/nvcuvid_include/nvcuvid.h +610 -0
- torchcodec/_core/ops.py +103 -0
- torchcodec/_core/sources.bzl +113 -0
- torchcodec/_frame.py +146 -0
- torchcodec/_internally_replaced_utils.py +129 -0
- torchcodec/_logging.py +59 -0
- torchcodec/decoders/__init__.py +50 -0
- torchcodec/decoders/_audio_decoder.py +183 -0
- torchcodec/decoders/_blocks/__init__.py +48 -0
- torchcodec/decoders/_blocks/_audio_converter.py +155 -0
- torchcodec/decoders/_blocks/_codec_parameters.py +229 -0
- torchcodec/decoders/_blocks/_color_converter.py +107 -0
- torchcodec/decoders/_blocks/_demuxer.py +678 -0
- torchcodec/decoders/_blocks/_frame.py +577 -0
- torchcodec/decoders/_blocks/_helpers.py +53 -0
- torchcodec/decoders/_blocks/_packet_decoder.py +357 -0
- torchcodec/decoders/_decoder_utils.py +128 -0
- torchcodec/decoders/_image_decoders.py +1575 -0
- torchcodec/decoders/_video_decoder.py +592 -0
- torchcodec/decoders/_wav_decoder.py +131 -0
- torchcodec/encoders/__init__.py +4 -0
- torchcodec/encoders/_audio_encoder.py +167 -0
- torchcodec/encoders/_image_encoders.py +181 -0
- torchcodec/encoders/_multi_stream_encoder.py +272 -0
- torchcodec/encoders/_video_encoder.py +227 -0
- torchcodec/jpeg8.dll +0 -0
- torchcodec/libavif.dll +0 -0
- torchcodec/libpng16.dll +0 -0
- torchcodec/libsharpyuv.dll +0 -0
- torchcodec/libtorchcodec_core4.dll +0 -0
- torchcodec/libtorchcodec_core5.dll +0 -0
- torchcodec/libtorchcodec_core6.dll +0 -0
- torchcodec/libtorchcodec_core7.dll +0 -0
- torchcodec/libtorchcodec_core8.dll +0 -0
- torchcodec/libtorchcodec_core9.dll +0 -0
- torchcodec/libtorchcodec_custom_ops4.dll +0 -0
- torchcodec/libtorchcodec_custom_ops5.dll +0 -0
- torchcodec/libtorchcodec_custom_ops6.dll +0 -0
- torchcodec/libtorchcodec_custom_ops7.dll +0 -0
- torchcodec/libtorchcodec_custom_ops8.dll +0 -0
- torchcodec/libtorchcodec_custom_ops9.dll +0 -0
- torchcodec/libtorchcodec_heic.dll +0 -0
- torchcodec/libtorchcodec_image.dll +0 -0
- torchcodec/libwebp.dll +0 -0
- torchcodec/libwebpdecoder.dll +0 -0
- torchcodec/libwebpdemux.dll +0 -0
- torchcodec/libwebpmux.dll +0 -0
- torchcodec/libzstd.dll +0 -0
- torchcodec/py.typed +0 -0
- torchcodec/samplers/__init__.py +2 -0
- torchcodec/samplers/_common.py +83 -0
- torchcodec/samplers/_index_based.py +287 -0
- torchcodec/samplers/_time_based.py +360 -0
- torchcodec/share/cmake/TorchCodec/TorchCodecConfig.cmake +76 -0
- torchcodec/share/cmake/TorchCodec/ffmpeg_versions.cmake +128 -0
- torchcodec/transforms/__init__.py +12 -0
- torchcodec/transforms/_decoder_transforms.py +375 -0
- torchcodec/version.py +1 -0
- torchcodec/zlib.dll +0 -0
- torchcodec-1.0.0.dist-info/METADATA +275 -0
- torchcodec-1.0.0.dist-info/RECORD +179 -0
- torchcodec-1.0.0.dist-info/WHEEL +5 -0
- torchcodec-1.0.0.dist-info/licenses/LICENSE +28 -0
- torchcodec-1.0.0.dist-info/licenses/third_party/COPYING.dav1d +23 -0
- torchcodec-1.0.0.dist-info/licenses/third_party/LICENSE.libavif +387 -0
- torchcodec-1.0.0.dist-info/licenses/third_party/LICENSE.libjpeg-turbo +135 -0
- torchcodec-1.0.0.dist-info/licenses/third_party/LICENSE.libpng +134 -0
- torchcodec-1.0.0.dist-info/licenses/third_party/LICENSE.libwebp +30 -0
- torchcodec-1.0.0.dist-info/licenses/third_party/LICENSE.libyuv +29 -0
- torchcodec-1.0.0.dist-info/licenses/third_party/LICENSE.zlib +22 -0
|
@@ -0,0 +1,488 @@
|
|
|
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 "DecodeGif.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_GIF
|
|
16
|
+
|
|
17
|
+
namespace facebook::torchcodec {
|
|
18
|
+
|
|
19
|
+
torch::stable::Tensor decode_gif(
|
|
20
|
+
[[maybe_unused]] const torch::stable::Tensor& input,
|
|
21
|
+
[[maybe_unused]] int64_t mode) {
|
|
22
|
+
STD_TORCH_CHECK(
|
|
23
|
+
false,
|
|
24
|
+
"decode_gif: torchcodec was not compiled with GIF support. Rebuild "
|
|
25
|
+
"torchcodec with TORCHCODEC_BUILD_GIF=1. If you see this error in a "
|
|
26
|
+
"prebuilt wheel, please report it to the TorchCodec repo.");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
std::vector<int64_t> probe_gif_from_tensor(
|
|
30
|
+
[[maybe_unused]] const torch::stable::Tensor& input) {
|
|
31
|
+
STD_TORCH_CHECK(
|
|
32
|
+
false,
|
|
33
|
+
"get_image_metadata: torchcodec was not compiled with GIF support. "
|
|
34
|
+
"Rebuild torchcodec with TORCHCODEC_BUILD_GIF=1. If you see this error "
|
|
35
|
+
"in a prebuilt wheel, please report it to the TorchCodec repo.");
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
std::vector<int64_t> probe_gif_from_file(
|
|
39
|
+
[[maybe_unused]] std::string filename) {
|
|
40
|
+
STD_TORCH_CHECK(
|
|
41
|
+
false,
|
|
42
|
+
"get_image_metadata: torchcodec was not compiled with GIF support. "
|
|
43
|
+
"Rebuild torchcodec with TORCHCODEC_BUILD_GIF=1. If you see this error "
|
|
44
|
+
"in a prebuilt wheel, please report it to the TorchCodec repo.");
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
} // namespace facebook::torchcodec
|
|
48
|
+
|
|
49
|
+
#else
|
|
50
|
+
|
|
51
|
+
#include <torch/headeronly/core/MemoryFormat.h>
|
|
52
|
+
|
|
53
|
+
#include <algorithm>
|
|
54
|
+
#include <climits>
|
|
55
|
+
#include <cstdint>
|
|
56
|
+
#include <optional>
|
|
57
|
+
|
|
58
|
+
#include "Exif.h"
|
|
59
|
+
#include "FileIO.h"
|
|
60
|
+
#include "ImageCommon.h"
|
|
61
|
+
#include "giflib/gif_lib.h"
|
|
62
|
+
|
|
63
|
+
namespace facebook::torchcodec {
|
|
64
|
+
|
|
65
|
+
namespace {
|
|
66
|
+
|
|
67
|
+
struct SourceCtx {
|
|
68
|
+
const uint8_t* ptr; // current read position in the input tensor
|
|
69
|
+
size_t count; // number of bytes left to read
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
// Reader passed to DGifOpen(): giflib calls it to pull `bytes` encoded bytes
|
|
73
|
+
// into `output`. We serve them from the input tensor (see SourceCtx), advancing
|
|
74
|
+
// as we go, and return the number of bytes actually read.
|
|
75
|
+
int read_callback(GifFileType* gif_file, GifByteType* output, int bytes) {
|
|
76
|
+
// UserData was set in DGifOpen().
|
|
77
|
+
auto* source_ctx = static_cast<SourceCtx*>(gif_file->UserData);
|
|
78
|
+
size_t num_bytes_to_read =
|
|
79
|
+
std::min(static_cast<size_t>(bytes), source_ctx->count);
|
|
80
|
+
std::copy(source_ctx->ptr, source_ctx->ptr + num_bytes_to_read, output);
|
|
81
|
+
source_ctx->ptr += num_bytes_to_read;
|
|
82
|
+
source_ctx->count -= num_bytes_to_read;
|
|
83
|
+
return static_cast<int>(num_bytes_to_read);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Same as read_callback, reading from a file instead of a tensor.
|
|
87
|
+
int file_read_callback(GifFileType* gif_file, GifByteType* output, int bytes) {
|
|
88
|
+
auto* file = static_cast<FileIO*>(gif_file->UserData);
|
|
89
|
+
return std::max(file->read(output, bytes), 0);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
} // namespace
|
|
93
|
+
|
|
94
|
+
torch::stable::Tensor decode_gif(
|
|
95
|
+
const torch::stable::Tensor& input,
|
|
96
|
+
int64_t mode) {
|
|
97
|
+
// GIF decoding model (see https://giflib.sourceforge.net/whatsinagif/):
|
|
98
|
+
//
|
|
99
|
+
// A GIF is a shared canvas (the "logical screen") onto which one or more
|
|
100
|
+
// FRAMES are painted in order. Each frame is a palette image (<= 256 colors)
|
|
101
|
+
// that may be smaller than the canvas and placed at an offset, and it is
|
|
102
|
+
// composited onto the running canvas -- so frame i as displayed is the
|
|
103
|
+
// accumulation of all frames so far, not frame i's pixels alone.
|
|
104
|
+
//
|
|
105
|
+
// Two per-frame settings (both in the frame's Graphic Control Extension)
|
|
106
|
+
// drive that compositing:
|
|
107
|
+
// - TRANSPARENCY: one palette index can be marked transparent; those pixels
|
|
108
|
+
// are "not drawn", so whatever is already on the canvas shows through. It
|
|
109
|
+
// is binary (fully transparent or fully opaque).
|
|
110
|
+
// - DISPOSAL METHOD: what to do with the canvas AFTER this frame is shown, to
|
|
111
|
+
// prepare for the next one:
|
|
112
|
+
// - DISPOSE_DO_NOT leaves it (the next frame draws on top)
|
|
113
|
+
// - DISPOSE_BACKGROUND clears this frame's area to the background color
|
|
114
|
+
// (a palette index from the logical screen descriptor)
|
|
115
|
+
// - DISPOSE_PREVIOUS restores the canvas to its state before this frame.
|
|
116
|
+
//
|
|
117
|
+
// So frame i's starting canvas is decided by frame (i-1)'s disposal method,
|
|
118
|
+
// and transparency then lets frame i reveal it. Below we rebuild that canvas
|
|
119
|
+
// per frame and write each composited frame into the output tensor.
|
|
120
|
+
//
|
|
121
|
+
// LibGif API docs: https://giflib.sourceforge.net/intro.html
|
|
122
|
+
|
|
123
|
+
auto contig_input = validate_encoded_data(input);
|
|
124
|
+
|
|
125
|
+
int error = D_GIF_SUCCEEDED;
|
|
126
|
+
|
|
127
|
+
// We're using DGifOpen. The other entrypoints of libgif are DGifOpenFileName
|
|
128
|
+
// and DGifOpenFileHandle but we don't want to use those, since we need to
|
|
129
|
+
// read the encoded bytes from a tensor of encoded bytes, not from a file (for
|
|
130
|
+
// consistency with existing jpeg and png decoders). Using DGifOpen is the
|
|
131
|
+
// only way to read from a custom source, via the read_callback reader above.
|
|
132
|
+
|
|
133
|
+
// TODO: We are potentially doing an unnecessary copy of the encoded bytes:
|
|
134
|
+
// - 1 copy from file to tensor (in the Python _source_to_tensor())
|
|
135
|
+
// - 1 copy from tensor to GIFLIB buffers (in read_callback())
|
|
136
|
+
// Since we're vendoring GIFLIB we can potentially modify the calls to
|
|
137
|
+
// InternalRead() and just set the `buf` pointer to the tensor data directly.
|
|
138
|
+
// That might even save allocation of those buffers.
|
|
139
|
+
// If we do that, we'd have to make sure the buffers are never written to by
|
|
140
|
+
// GIFLIB, otherwise we'd be overriding the tensor data.
|
|
141
|
+
SourceCtx source_ctx{
|
|
142
|
+
.ptr = contig_input.const_data_ptr<uint8_t>(),
|
|
143
|
+
.count = static_cast<size_t>(contig_input.numel())};
|
|
144
|
+
GifFileType* gif_file =
|
|
145
|
+
DGifOpen(static_cast<void*>(&source_ctx), read_callback, &error);
|
|
146
|
+
|
|
147
|
+
STD_TORCH_CHECK(
|
|
148
|
+
(gif_file != nullptr) && (error == D_GIF_SUCCEEDED),
|
|
149
|
+
"DGifOpen() failed - ",
|
|
150
|
+
error);
|
|
151
|
+
|
|
152
|
+
if (DGifSlurp(gif_file) == GIF_ERROR) {
|
|
153
|
+
auto slurp_error = gif_file->Error;
|
|
154
|
+
DGifCloseFile(gif_file, &error);
|
|
155
|
+
STD_TORCH_CHECK(false, "DGifSlurp() failed - ", slurp_error);
|
|
156
|
+
}
|
|
157
|
+
auto num_images = gif_file->ImageCount;
|
|
158
|
+
|
|
159
|
+
// This check should already be done within DGifSlurp(), just to be safe.
|
|
160
|
+
STD_TORCH_CHECK(
|
|
161
|
+
num_images > 0, "GIF file should contain at least one image!");
|
|
162
|
+
|
|
163
|
+
GifColorType bg{0, 0, 0};
|
|
164
|
+
if (gif_file->SColorMap &&
|
|
165
|
+
// out of range background falls back to black
|
|
166
|
+
gif_file->SBackGroundColor < gif_file->SColorMap->ColorCount) {
|
|
167
|
+
bg = gif_file->SColorMap->Colors[gif_file->SBackGroundColor];
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
bool has_transparency = false;
|
|
171
|
+
for (int i = 0; i < num_images; ++i) {
|
|
172
|
+
GraphicsControlBlock gcb;
|
|
173
|
+
if (DGifSavedExtensionToGCB(gif_file, i, &gcb) == GIF_OK &&
|
|
174
|
+
gcb.TransparentColor != NO_TRANSPARENT_COLOR) {
|
|
175
|
+
has_transparency = true;
|
|
176
|
+
break;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// We represent transparency differently per output mode, and this is where we
|
|
181
|
+
// diverge from Pillow (see the primer above for how transparency/disposal
|
|
182
|
+
// build the canvas that transparent pixels reveal):
|
|
183
|
+
// - RGB_ALPHA, and UNCHANGED when the GIF has transparency: background/
|
|
184
|
+
// uncovered/disposed regions become alpha 0 (i.e. transparent); since GIF
|
|
185
|
+
// transparency is binary, the alpha we emit is always 0 or 255. This
|
|
186
|
+
// matches Pillow's RGBA output exactly (alpha, and RGB wherever opaque).
|
|
187
|
+
// - RGB: there is no alpha, so those regions instead get the GIF background
|
|
188
|
+
// color via fill_background(). This is spec-faithful (the background color
|
|
189
|
+
// is what uncovered pixels should show) but differs from Pillow's
|
|
190
|
+
// convert("RGB"): Pillow ignores the background color and, for a
|
|
191
|
+
// transparent pixel with nothing opaque behind it, shows the transparent
|
|
192
|
+
// index's own palette color. We deliberately do not match Pillow here. (A
|
|
193
|
+
// transparent pixel over an opaque previous frame shows that frame in both,
|
|
194
|
+
// so we only diverge on background/uncovered regions.)
|
|
195
|
+
bool emit_alpha =
|
|
196
|
+
!should_return_rgb(static_cast<ImageReadMode>(mode), has_transparency);
|
|
197
|
+
int num_output_channels = emit_alpha ? 4 : 3;
|
|
198
|
+
|
|
199
|
+
// The GIFLIB docs say that the canvas's height and width are potentially
|
|
200
|
+
// ignored by modern viewers, so to be on the safe side we set the output
|
|
201
|
+
// height to max(canvas_height, first_image_height). Same for width.
|
|
202
|
+
// https://giflib.sourceforge.net/whatsinagif/bits_and_bytes.html
|
|
203
|
+
auto output_h =
|
|
204
|
+
std::max(gif_file->SHeight, gif_file->SavedImages[0].ImageDesc.Height);
|
|
205
|
+
auto output_w =
|
|
206
|
+
std::max(gif_file->SWidth, gif_file->SavedImages[0].ImageDesc.Width);
|
|
207
|
+
|
|
208
|
+
auto output = torch::stable::empty(
|
|
209
|
+
{static_cast<int64_t>(num_images),
|
|
210
|
+
num_output_channels,
|
|
211
|
+
static_cast<int64_t>(output_h),
|
|
212
|
+
static_cast<int64_t>(output_w)},
|
|
213
|
+
kStableUInt8,
|
|
214
|
+
std::nullopt,
|
|
215
|
+
std::nullopt,
|
|
216
|
+
std::nullopt,
|
|
217
|
+
torch::headeronly::MemoryFormat::ChannelsLast);
|
|
218
|
+
auto output_a = mutable_accessor<uint8_t, 4>(output);
|
|
219
|
+
|
|
220
|
+
// Clears frame i over the rectangle [top, top + height) x [left, left +
|
|
221
|
+
// width) (clipped to the canvas). For RGB output this paints the GIF
|
|
222
|
+
// background color; for RGBA output it clears to fully transparent (matching
|
|
223
|
+
// web browsers / Pillow, which ignore the background color for transparent
|
|
224
|
+
// GIFs).
|
|
225
|
+
auto fill_background = [&](int i, int top, int left, int height, int width) {
|
|
226
|
+
for (int h = 0; h < height; ++h) {
|
|
227
|
+
const auto y = static_cast<int64_t>(top) + h;
|
|
228
|
+
if (y < 0 || y >= output_h) {
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
for (int w = 0; w < width; ++w) {
|
|
232
|
+
const auto x = static_cast<int64_t>(left) + w;
|
|
233
|
+
if (x < 0 || x >= output_w) {
|
|
234
|
+
continue;
|
|
235
|
+
}
|
|
236
|
+
if (emit_alpha) {
|
|
237
|
+
output_a[i][0][y][x] = 0;
|
|
238
|
+
output_a[i][1][y][x] = 0;
|
|
239
|
+
output_a[i][2][y][x] = 0;
|
|
240
|
+
output_a[i][3][y][x] = 0;
|
|
241
|
+
} else {
|
|
242
|
+
output_a[i][0][y][x] = bg.Red;
|
|
243
|
+
output_a[i][1][y][x] = bg.Green;
|
|
244
|
+
output_a[i][2][y][x] = bg.Blue;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
// Loop-carried canvas state (used by prepare_canvas): the previous frame's
|
|
251
|
+
// disposal method and image rectangle, plus a snapshot of the canvas taken
|
|
252
|
+
// before a DISPOSE_PREVIOUS frame is drawn (lazily allocated, shape
|
|
253
|
+
// (1, C, H, W)) so the following frame can restore it.
|
|
254
|
+
int prev_disposal = DISPOSAL_UNSPECIFIED;
|
|
255
|
+
GifImageDesc prev_desc{};
|
|
256
|
+
torch::stable::Tensor restore_point;
|
|
257
|
+
bool have_restore_point = false;
|
|
258
|
+
|
|
259
|
+
// Stage 1: set output[i] to the base canvas frame i is drawn onto, per the
|
|
260
|
+
// *previous* frame's disposal method (see the primer). DISPOSE_BACKGROUND
|
|
261
|
+
// clears only the previous frame's rectangle (keeping the rest of the
|
|
262
|
+
// accumulated canvas); DISPOSE_PREVIOUS restores the snapshot taken before
|
|
263
|
+
// that frame; the first frame starts from a fully cleared canvas. Also
|
|
264
|
+
// snapshots this frame's base if it will itself be disposed with
|
|
265
|
+
// DISPOSE_PREVIOUS.
|
|
266
|
+
auto prepare_canvas = [&](int i, const GraphicsControlBlock& gcb) {
|
|
267
|
+
if (i == 0) {
|
|
268
|
+
// Clear the whole first-frame canvas, not just the logical screen: when
|
|
269
|
+
// the first image is larger than the logical screen the output is sized
|
|
270
|
+
// to the image (output_h/output_w above), and any pixel the image leaves
|
|
271
|
+
// transparent there would otherwise be left uninitialized.
|
|
272
|
+
fill_background(0, 0, 0, output_h, output_w);
|
|
273
|
+
} else if (prev_disposal == DISPOSE_BACKGROUND) {
|
|
274
|
+
copy_frame(output, i, output, i - 1);
|
|
275
|
+
fill_background(
|
|
276
|
+
i, prev_desc.Top, prev_desc.Left, prev_desc.Height, prev_desc.Width);
|
|
277
|
+
} else if (prev_disposal == DISPOSE_PREVIOUS && have_restore_point) {
|
|
278
|
+
copy_frame(output, i, restore_point, 0);
|
|
279
|
+
} else {
|
|
280
|
+
// DISPOSE_DO_NOT or DISPOSAL_UNSPECIFIED: just copy the previous canvas.
|
|
281
|
+
copy_frame(output, i, output, i - 1);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// Save current canvas state for next frame to restore.
|
|
285
|
+
if (gcb.DisposalMode == DISPOSE_PREVIOUS) {
|
|
286
|
+
if (!have_restore_point) {
|
|
287
|
+
restore_point = torch::stable::empty(
|
|
288
|
+
{1,
|
|
289
|
+
num_output_channels,
|
|
290
|
+
static_cast<int64_t>(output_h),
|
|
291
|
+
static_cast<int64_t>(output_w)},
|
|
292
|
+
kStableUInt8);
|
|
293
|
+
have_restore_point = true;
|
|
294
|
+
}
|
|
295
|
+
copy_frame(restore_point, 0, output, i);
|
|
296
|
+
}
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
// Stage 2: draw frame i's opaque pixels on top of its prepared base canvas.
|
|
300
|
+
// Transparent pixels are skipped so the base shows through, and pixels that
|
|
301
|
+
// fall outside the output tensor (a frame larger than the first, or with a
|
|
302
|
+
// non-zero offset) are dropped.
|
|
303
|
+
auto draw_frame_over_canvas = [&](int i,
|
|
304
|
+
const SavedImage& img,
|
|
305
|
+
const GraphicsControlBlock& gcb) {
|
|
306
|
+
const GifImageDesc& desc = img.ImageDesc;
|
|
307
|
+
const ColorMapObject* cmap =
|
|
308
|
+
desc.ColorMap ? desc.ColorMap : gif_file->SColorMap;
|
|
309
|
+
STD_TORCH_CHECK(
|
|
310
|
+
cmap != nullptr,
|
|
311
|
+
"Global and local color maps are missing. This should never happen!");
|
|
312
|
+
for (int h = 0; h < desc.Height; ++h) {
|
|
313
|
+
const auto y = static_cast<int64_t>(desc.Top) + h;
|
|
314
|
+
if (y < 0 || y >= output_h) {
|
|
315
|
+
continue;
|
|
316
|
+
}
|
|
317
|
+
for (int w = 0; w < desc.Width; ++w) {
|
|
318
|
+
const auto x = static_cast<int64_t>(desc.Left) + w;
|
|
319
|
+
if (x < 0 || x >= output_w) {
|
|
320
|
+
continue;
|
|
321
|
+
}
|
|
322
|
+
auto c = img.RasterBits[h * desc.Width + w];
|
|
323
|
+
if (c == gcb.TransparentColor) {
|
|
324
|
+
continue;
|
|
325
|
+
}
|
|
326
|
+
GifColorType rgb =
|
|
327
|
+
c < cmap->ColorCount ? cmap->Colors[c] : GifColorType{0, 0, 0};
|
|
328
|
+
output_a[i][0][y][x] = rgb.Red;
|
|
329
|
+
output_a[i][1][y][x] = rgb.Green;
|
|
330
|
+
output_a[i][2][y][x] = rgb.Blue;
|
|
331
|
+
if (emit_alpha) {
|
|
332
|
+
output_a[i][3][y][x] = 255;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
for (int i = 0; i < num_images; ++i) {
|
|
339
|
+
const SavedImage& img = gif_file->SavedImages[i];
|
|
340
|
+
GraphicsControlBlock gcb;
|
|
341
|
+
DGifSavedExtensionToGCB(gif_file, i, &gcb);
|
|
342
|
+
|
|
343
|
+
prepare_canvas(i, gcb);
|
|
344
|
+
draw_frame_over_canvas(i, img, gcb);
|
|
345
|
+
|
|
346
|
+
prev_disposal = gcb.DisposalMode;
|
|
347
|
+
prev_desc = img.ImageDesc;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// Remove the batch dim if there's only one image, so a still GIF decodes to a
|
|
351
|
+
// (C, H, W) tensor like the other image decoders.
|
|
352
|
+
if (num_images == 1) {
|
|
353
|
+
output = select_row(output, 0);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
DGifCloseFile(gif_file, &error);
|
|
357
|
+
STD_TORCH_CHECK(error == D_GIF_SUCCEEDED, "DGifCloseFile() failed - ", error);
|
|
358
|
+
|
|
359
|
+
return output;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
namespace {
|
|
363
|
+
|
|
364
|
+
// Walks the records like DGifSlurp() does, but skips the image data instead of
|
|
365
|
+
// LZW-decoding it. Closes gif_file.
|
|
366
|
+
ImageProbeResult probe_and_close(GifFileType* gif_file) {
|
|
367
|
+
int error = D_GIF_SUCCEEDED;
|
|
368
|
+
|
|
369
|
+
auto fail = [&]() {
|
|
370
|
+
auto gif_error = gif_file->Error;
|
|
371
|
+
DGifCloseFile(gif_file, &error);
|
|
372
|
+
STD_TORCH_CHECK(false, "Failed to parse GIF - ", gif_error);
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
int64_t num_images = 0;
|
|
376
|
+
int64_t first_image_width = 0;
|
|
377
|
+
int64_t first_image_height = 0;
|
|
378
|
+
// Like DGifSavedExtensionToGCB(), only the first graphics control extension
|
|
379
|
+
// preceding an image applies to it.
|
|
380
|
+
bool seen_gcb_for_next_image = false;
|
|
381
|
+
bool has_transparency = false;
|
|
382
|
+
GifRecordType record_type;
|
|
383
|
+
do {
|
|
384
|
+
if (DGifGetRecordType(gif_file, &record_type) == GIF_ERROR) {
|
|
385
|
+
fail();
|
|
386
|
+
}
|
|
387
|
+
if (record_type == IMAGE_DESC_RECORD_TYPE) {
|
|
388
|
+
if (DGifGetImageDesc(gif_file) == GIF_ERROR) {
|
|
389
|
+
fail();
|
|
390
|
+
}
|
|
391
|
+
const GifImageDesc& desc = gif_file->Image;
|
|
392
|
+
if (desc.Width <= 0 || desc.Height <= 0 ||
|
|
393
|
+
desc.Width > (INT_MAX / desc.Height)) {
|
|
394
|
+
fail();
|
|
395
|
+
}
|
|
396
|
+
if (num_images == 0) {
|
|
397
|
+
first_image_width = desc.Width;
|
|
398
|
+
first_image_height = desc.Height;
|
|
399
|
+
}
|
|
400
|
+
++num_images;
|
|
401
|
+
seen_gcb_for_next_image = false;
|
|
402
|
+
|
|
403
|
+
int code_size = 0;
|
|
404
|
+
GifByteType* code_block = nullptr;
|
|
405
|
+
if (DGifGetCode(gif_file, &code_size, &code_block) == GIF_ERROR) {
|
|
406
|
+
fail();
|
|
407
|
+
}
|
|
408
|
+
while (code_block != nullptr) {
|
|
409
|
+
if (DGifGetCodeNext(gif_file, &code_block) == GIF_ERROR) {
|
|
410
|
+
fail();
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
} else if (record_type == EXTENSION_RECORD_TYPE) {
|
|
414
|
+
int ext_code = 0;
|
|
415
|
+
GifByteType* ext_data = nullptr;
|
|
416
|
+
if (DGifGetExtension(gif_file, &ext_code, &ext_data) == GIF_ERROR) {
|
|
417
|
+
fail();
|
|
418
|
+
}
|
|
419
|
+
if (ext_data != nullptr && ext_code == GRAPHICS_EXT_FUNC_CODE &&
|
|
420
|
+
!seen_gcb_for_next_image) {
|
|
421
|
+
seen_gcb_for_next_image = true;
|
|
422
|
+
GraphicsControlBlock gcb;
|
|
423
|
+
if (DGifExtensionToGCB(ext_data[0], ext_data + 1, &gcb) == GIF_OK &&
|
|
424
|
+
gcb.TransparentColor != NO_TRANSPARENT_COLOR) {
|
|
425
|
+
has_transparency = true;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
while (ext_data != nullptr) {
|
|
429
|
+
if (DGifGetExtensionNext(gif_file, &ext_data) == GIF_ERROR) {
|
|
430
|
+
fail();
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
} while (record_type != TERMINATE_RECORD_TYPE);
|
|
435
|
+
|
|
436
|
+
int64_t width = std::max<int64_t>(gif_file->SWidth, first_image_width);
|
|
437
|
+
int64_t height = std::max<int64_t>(gif_file->SHeight, first_image_height);
|
|
438
|
+
DGifCloseFile(gif_file, &error);
|
|
439
|
+
STD_TORCH_CHECK(
|
|
440
|
+
num_images > 0, "GIF file should contain at least one image!");
|
|
441
|
+
|
|
442
|
+
return {
|
|
443
|
+
width,
|
|
444
|
+
height,
|
|
445
|
+
has_transparency ? 4 : 3,
|
|
446
|
+
8,
|
|
447
|
+
num_images,
|
|
448
|
+
static_cast<int64_t>(exif_private::ExifOrientation::Unspecified)};
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
} // namespace
|
|
452
|
+
|
|
453
|
+
ImageProbeResult probe_gif_from_tensor(const torch::stable::Tensor& input) {
|
|
454
|
+
auto contig_input = validate_encoded_data(input);
|
|
455
|
+
|
|
456
|
+
int error = D_GIF_SUCCEEDED;
|
|
457
|
+
SourceCtx source_ctx{
|
|
458
|
+
.ptr = contig_input.const_data_ptr<uint8_t>(),
|
|
459
|
+
.count = static_cast<size_t>(contig_input.numel())};
|
|
460
|
+
GifFileType* gif_file =
|
|
461
|
+
DGifOpen(static_cast<void*>(&source_ctx), read_callback, &error);
|
|
462
|
+
STD_TORCH_CHECK(
|
|
463
|
+
(gif_file != nullptr) && (error == D_GIF_SUCCEEDED),
|
|
464
|
+
"DGifOpen() failed - ",
|
|
465
|
+
error);
|
|
466
|
+
return probe_and_close(gif_file);
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
// Same as probe_gif_from_tensor, but streams the file in small chunks instead
|
|
470
|
+
// of requiring all of it in memory. GIF has no frame count in its header, so
|
|
471
|
+
// the whole file is still read, but this avoids loading the whole file into a
|
|
472
|
+
// tensor first and is still faster for large GIFs. Note that for decoding we
|
|
473
|
+
// always load into a tensor because the speed difference is negligible then.
|
|
474
|
+
ImageProbeResult probe_gif_from_file(std::string filename) {
|
|
475
|
+
FileIO file(filename, FileIO::Mode::Read);
|
|
476
|
+
int error = D_GIF_SUCCEEDED;
|
|
477
|
+
GifFileType* gif_file =
|
|
478
|
+
DGifOpen(static_cast<void*>(&file), file_read_callback, &error);
|
|
479
|
+
STD_TORCH_CHECK(
|
|
480
|
+
(gif_file != nullptr) && (error == D_GIF_SUCCEEDED),
|
|
481
|
+
"DGifOpen() failed - ",
|
|
482
|
+
error);
|
|
483
|
+
return probe_and_close(gif_file);
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
} // namespace facebook::torchcodec
|
|
487
|
+
|
|
488
|
+
#endif // !TORCHCODEC_ENABLE_GIF
|
|
@@ -0,0 +1,32 @@
|
|
|
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 <string>
|
|
10
|
+
#include <vector>
|
|
11
|
+
|
|
12
|
+
#include "StableABICompat.h"
|
|
13
|
+
|
|
14
|
+
namespace facebook::torchcodec {
|
|
15
|
+
|
|
16
|
+
// Decodes a GIF to an (C, H, W) tensor (still GIF) or (N, C, H, W) tensor
|
|
17
|
+
// (animated GIF). `mode` is an ImageReadMode: only UNCHANGED, RGB and RGB_ALPHA
|
|
18
|
+
// are handled here (the grayscale modes are derived in Python). RGB produces a
|
|
19
|
+
// 3-channel tensor with transparency composited over the GIF background color;
|
|
20
|
+
// RGB_ALPHA produces a 4-channel RGBA tensor preserving transparency as alpha;
|
|
21
|
+
// UNCHANGED produces RGBA if the GIF has any transparency, else RGB.
|
|
22
|
+
FORCE_PUBLIC_VISIBILITY torch::stable::Tensor decode_gif(
|
|
23
|
+
const torch::stable::Tensor& input,
|
|
24
|
+
int64_t mode);
|
|
25
|
+
|
|
26
|
+
FORCE_PUBLIC_VISIBILITY std::vector<int64_t> probe_gif_from_tensor(
|
|
27
|
+
const torch::stable::Tensor& input);
|
|
28
|
+
|
|
29
|
+
FORCE_PUBLIC_VISIBILITY std::vector<int64_t> probe_gif_from_file(
|
|
30
|
+
std::string filename);
|
|
31
|
+
|
|
32
|
+
} // namespace facebook::torchcodec
|