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,489 @@
|
|
|
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 "DecodeJpeg.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_JPEG
|
|
16
|
+
|
|
17
|
+
namespace facebook::torchcodec {
|
|
18
|
+
|
|
19
|
+
torch::stable::Tensor decode_jpeg(
|
|
20
|
+
[[maybe_unused]] const torch::stable::Tensor& input,
|
|
21
|
+
[[maybe_unused]] int64_t mode) {
|
|
22
|
+
STD_TORCH_CHECK(
|
|
23
|
+
false,
|
|
24
|
+
"decode_jpeg: torchcodec was not compiled with libjpeg support. Rebuild "
|
|
25
|
+
"torchcodec with TORCHCODEC_BUILD_JPEG=1. If you see this error in a "
|
|
26
|
+
"prebuilt wheel, please report it to the TorchCodec repo.");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
} // namespace facebook::torchcodec
|
|
30
|
+
|
|
31
|
+
#else
|
|
32
|
+
|
|
33
|
+
#include <jpeglib.h>
|
|
34
|
+
#include <setjmp.h>
|
|
35
|
+
|
|
36
|
+
#include <algorithm>
|
|
37
|
+
#include <cstdint>
|
|
38
|
+
#include <cstring>
|
|
39
|
+
#include <tuple>
|
|
40
|
+
|
|
41
|
+
#include "Exif.h"
|
|
42
|
+
#include "ImageCommon.h"
|
|
43
|
+
|
|
44
|
+
namespace facebook::torchcodec {
|
|
45
|
+
|
|
46
|
+
using namespace exif_private;
|
|
47
|
+
|
|
48
|
+
namespace {
|
|
49
|
+
|
|
50
|
+
// Main libjpeg docs:
|
|
51
|
+
// https://github.com/libjpeg-turbo/libjpeg-turbo/blob/main/doc/libjpeg.txt
|
|
52
|
+
|
|
53
|
+
constexpr uint16_t EXIF_APP1 = 0xe1;
|
|
54
|
+
|
|
55
|
+
// Helpers to convert a CMYK line to RGB or grayscale, since libjpeg doesn't
|
|
56
|
+
// handle that directly.
|
|
57
|
+
using CmykLineConverterFn = void (*)(int, const uint8_t*, uint8_t*);
|
|
58
|
+
|
|
59
|
+
struct CMYKHelper {
|
|
60
|
+
uint8_t* cmyk_line_ptr = nullptr;
|
|
61
|
+
// This is either convert_line_cmyk_to_rgb or convert_line_cmyk_to_gray
|
|
62
|
+
CmykLineConverterFn convert_fn = nullptr;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
inline uint8_t cmyk_to_rgb(uint8_t k, uint8_t cmy) {
|
|
66
|
+
// Inspired from Pillow:
|
|
67
|
+
// https://github.com/python-pillow/Pillow/blob/07623d1a7cc65206a5355fba2ae256550bfcaba6/src/libImaging/Convert.c#L568-L569
|
|
68
|
+
int v = k * cmy + 128;
|
|
69
|
+
v = ((v >> 8) + v) >> 8;
|
|
70
|
+
return std::clamp(k - v, 0, 255);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
void convert_line_cmyk_to_rgb(
|
|
74
|
+
int width,
|
|
75
|
+
const uint8_t* cmyk_line_ptr,
|
|
76
|
+
uint8_t* output_ptr) {
|
|
77
|
+
for (int i = 0; i < width; ++i) {
|
|
78
|
+
int c = cmyk_line_ptr[i * 4 + 0];
|
|
79
|
+
int m = cmyk_line_ptr[i * 4 + 1];
|
|
80
|
+
int y = cmyk_line_ptr[i * 4 + 2];
|
|
81
|
+
int k = cmyk_line_ptr[i * 4 + 3];
|
|
82
|
+
|
|
83
|
+
output_ptr[i * 3 + 0] = cmyk_to_rgb(k, 255 - c);
|
|
84
|
+
output_ptr[i * 3 + 1] = cmyk_to_rgb(k, 255 - m);
|
|
85
|
+
output_ptr[i * 3 + 2] = cmyk_to_rgb(k, 255 - y);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
inline uint8_t rgb_to_gray(int r, int g, int b) {
|
|
90
|
+
// Inspired from Pillow:
|
|
91
|
+
// https://github.com/python-pillow/Pillow/blob/07623d1a7cc65206a5355fba2ae256550bfcaba6/src/libImaging/Convert.c#L226
|
|
92
|
+
return (r * 19595 + g * 38470 + b * 7471 + 0x8000) >> 16;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
void convert_line_cmyk_to_gray(
|
|
96
|
+
int width,
|
|
97
|
+
const uint8_t* cmyk_line_ptr,
|
|
98
|
+
uint8_t* output_ptr) {
|
|
99
|
+
for (int i = 0; i < width; ++i) {
|
|
100
|
+
int c = cmyk_line_ptr[i * 4 + 0];
|
|
101
|
+
int m = cmyk_line_ptr[i * 4 + 1];
|
|
102
|
+
int y = cmyk_line_ptr[i * 4 + 2];
|
|
103
|
+
int k = cmyk_line_ptr[i * 4 + 3];
|
|
104
|
+
|
|
105
|
+
int r = cmyk_to_rgb(k, 255 - c);
|
|
106
|
+
int g = cmyk_to_rgb(k, 255 - m);
|
|
107
|
+
int b = cmyk_to_rgb(k, 255 - y);
|
|
108
|
+
|
|
109
|
+
output_ptr[i] = rgb_to_gray(r, g, b);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
ExifOrientation fetch_jpeg_exif_orientation(j_decompress_ptr jpeg_ctx) {
|
|
114
|
+
STD_TORCH_CHECK(jpeg_ctx != nullptr, "jpeg_ctx cannot be null");
|
|
115
|
+
|
|
116
|
+
// APP1 segments can also hold other payloads, e.g. XMP, so we look for the
|
|
117
|
+
// first one starting with the "Exif\0\0" header.
|
|
118
|
+
constexpr size_t exif_header_size = 6;
|
|
119
|
+
jpeg_saved_marker_ptr exif_marker = jpeg_ctx->marker_list;
|
|
120
|
+
while (exif_marker != nullptr) {
|
|
121
|
+
if (exif_marker->marker == EXIF_APP1 &&
|
|
122
|
+
exif_marker->data_length > exif_header_size &&
|
|
123
|
+
std::memcmp(exif_marker->data, "Exif\0\0", exif_header_size) == 0) {
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
exif_marker = exif_marker->next;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (exif_marker == nullptr) {
|
|
130
|
+
return ExifOrientation::Unspecified;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return fetch_exif_orientation_after_prefixes(
|
|
134
|
+
exif_marker->data, exif_marker->data_length);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Error context that gets passed by libjpeg to its callbacks
|
|
138
|
+
// (error_exit_cb, fill_input_buffer_cb, etc.).
|
|
139
|
+
// libjpeg doesn't actually pass this ErrorCtx, it passes the jpeg_ctx object
|
|
140
|
+
// which has an `err` field. This `err` field is *still* not an ErrorCtx
|
|
141
|
+
// object, it's the jpeg_error_mgr base field, but we can cast it back to
|
|
142
|
+
// ErrorCtx in the callbacks because the struct and its first field share the
|
|
143
|
+
// same address.
|
|
144
|
+
struct ErrorCtx {
|
|
145
|
+
jpeg_error_mgr base;
|
|
146
|
+
char last_error_message[JMSG_LENGTH_MAX];
|
|
147
|
+
jmp_buf setjmp_buffer;
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
// Callback called by libjpeg when an error occurs.
|
|
151
|
+
void error_exit_cb(j_common_ptr jpeg_ctx) {
|
|
152
|
+
auto error_ctx = reinterpret_cast<ErrorCtx*>(jpeg_ctx->err);
|
|
153
|
+
error_ctx->base.format_message(jpeg_ctx, error_ctx->last_error_message);
|
|
154
|
+
longjmp(error_ctx->setjmp_buffer, 1);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// Callback called by libjpeg whenever it runs out of input data. We treat this
|
|
158
|
+
// as an error.
|
|
159
|
+
// Should we ever want to support truncated JPEGs, we could instead return a
|
|
160
|
+
// fake EOI.
|
|
161
|
+
boolean fill_input_buffer_cb(j_decompress_ptr jpeg_ctx) {
|
|
162
|
+
auto error_ctx = reinterpret_cast<ErrorCtx*>(jpeg_ctx->err);
|
|
163
|
+
strcpy(error_ctx->last_error_message, "Image is incomplete or truncated.");
|
|
164
|
+
longjmp(error_ctx->setjmp_buffer, 1);
|
|
165
|
+
return TRUE; // never reached, but keeps compiler happy
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// Callback called when libjpeg wants to skip num_bytes worth of data.
|
|
169
|
+
void skip_input_data_cb(j_decompress_ptr jpeg_ctx, long num_bytes) {
|
|
170
|
+
if (num_bytes <= 0) {
|
|
171
|
+
return; // libjpeg docs say to ignore non-positive values
|
|
172
|
+
}
|
|
173
|
+
if (jpeg_ctx->src->bytes_in_buffer < static_cast<size_t>(num_bytes)) {
|
|
174
|
+
// libjpeg requested to skip more data than is available. This path isn't
|
|
175
|
+
// exercized in our tests.
|
|
176
|
+
// In TorchVision this would return a fake EOI, but that's inconsistent with
|
|
177
|
+
// our fill_input_buffer_cb, which treats truncated JPEGs as an error.
|
|
178
|
+
// So we error here too.
|
|
179
|
+
auto error_ctx = reinterpret_cast<ErrorCtx*>(jpeg_ctx->err);
|
|
180
|
+
strcpy(
|
|
181
|
+
error_ctx->last_error_message,
|
|
182
|
+
"Skipped over more data than is available in the input buffer.");
|
|
183
|
+
longjmp(error_ctx->setjmp_buffer, 1);
|
|
184
|
+
} else {
|
|
185
|
+
jpeg_ctx->src->next_input_byte += num_bytes;
|
|
186
|
+
jpeg_ctx->src->bytes_in_buffer -= num_bytes;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
void init_source_cb(j_decompress_ptr) {}
|
|
191
|
+
|
|
192
|
+
void term_source_cb(j_decompress_ptr) {}
|
|
193
|
+
|
|
194
|
+
// Returns {num_output_channels, cmyk_to_rgb_or_gray}.
|
|
195
|
+
// jpeg_ctx.output_height and jpeg_ctx.output_width are available after this
|
|
196
|
+
// function returns.
|
|
197
|
+
std::tuple<int, bool> read_header_and_start(
|
|
198
|
+
jpeg_decompress_struct& jpeg_ctx,
|
|
199
|
+
ErrorCtx& error_ctx,
|
|
200
|
+
const uint8_t* input_ptr,
|
|
201
|
+
const size_t input_len,
|
|
202
|
+
ImageReadMode mode) {
|
|
203
|
+
if (setjmp(error_ctx.setjmp_buffer)) {
|
|
204
|
+
// See Note [libjpeg error handling]
|
|
205
|
+
jpeg_destroy_decompress(&jpeg_ctx);
|
|
206
|
+
STD_TORCH_CHECK(false, error_ctx.last_error_message);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
jpeg_create_decompress(&jpeg_ctx);
|
|
210
|
+
|
|
211
|
+
jpeg_ctx.src = static_cast<jpeg_source_mgr*>(jpeg_ctx.mem->alloc_small(
|
|
212
|
+
reinterpret_cast<j_common_ptr>(&jpeg_ctx),
|
|
213
|
+
JPOOL_PERMANENT,
|
|
214
|
+
sizeof(jpeg_source_mgr)));
|
|
215
|
+
jpeg_ctx.src->init_source = init_source_cb;
|
|
216
|
+
jpeg_ctx.src->fill_input_buffer = fill_input_buffer_cb;
|
|
217
|
+
jpeg_ctx.src->skip_input_data = skip_input_data_cb;
|
|
218
|
+
jpeg_ctx.src->resync_to_restart = jpeg_resync_to_restart; // default
|
|
219
|
+
jpeg_ctx.src->term_source = term_source_cb;
|
|
220
|
+
jpeg_ctx.src->bytes_in_buffer = input_len;
|
|
221
|
+
jpeg_ctx.src->next_input_byte = input_ptr;
|
|
222
|
+
|
|
223
|
+
// Tells libjpeg to save APP1 markers (EXIF) in memory for later retrieval.
|
|
224
|
+
jpeg_save_markers(&jpeg_ctx, EXIF_APP1, 0xffff);
|
|
225
|
+
|
|
226
|
+
jpeg_read_header(&jpeg_ctx, TRUE);
|
|
227
|
+
|
|
228
|
+
// libjpeg natively decodes to UNCHANGED, GRAY or RGB. The alpha modes
|
|
229
|
+
// (GRAY_ALPHA, RGB_ALPHA) are emulated in Python at the single
|
|
230
|
+
// _decode_to_mode() callsite (see _image_decoders.py), which requests a
|
|
231
|
+
// native mode here and appends an alpha channel. The cpp decode_jpeg is not a
|
|
232
|
+
// public API and is only ever called from there, so the default branch is
|
|
233
|
+
// unreachable.
|
|
234
|
+
int num_output_channels = -1;
|
|
235
|
+
switch (mode) {
|
|
236
|
+
case ImageReadMode::UNCHANGED:
|
|
237
|
+
num_output_channels = jpeg_ctx.num_components;
|
|
238
|
+
break;
|
|
239
|
+
case ImageReadMode::GRAY:
|
|
240
|
+
num_output_channels = 1;
|
|
241
|
+
break;
|
|
242
|
+
case ImageReadMode::RGB:
|
|
243
|
+
num_output_channels = 3;
|
|
244
|
+
break;
|
|
245
|
+
default:
|
|
246
|
+
jpeg_destroy_decompress(&jpeg_ctx);
|
|
247
|
+
STD_TORCH_CHECK(
|
|
248
|
+
false,
|
|
249
|
+
"Reached an unexpected code path while decoding a JPEG file to mode ",
|
|
250
|
+
static_cast<int64_t>(mode),
|
|
251
|
+
". This should never happen, please report a bug to the TorchCodec repo.");
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// libjpeg can't convert CMYK/YCCK straight to gray or RGB, so for those modes
|
|
255
|
+
// we keep libjpeg's JCS_CMYK default as the output color-space, and convert
|
|
256
|
+
// the lines ourselves (see the decode_rows loop). Similar to:
|
|
257
|
+
// https://github.com/tensorflow/tensorflow/blob/86871065265b04e0db8ca360c046421efb2bdeb4/tensorflow/core/lib/jpeg/jpeg_mem.cc#L284-L313
|
|
258
|
+
bool cmyk_to_rgb_or_gray = (jpeg_ctx.jpeg_color_space == JCS_CMYK ||
|
|
259
|
+
jpeg_ctx.jpeg_color_space == JCS_YCCK) &&
|
|
260
|
+
(mode == ImageReadMode::GRAY || mode == ImageReadMode::RGB);
|
|
261
|
+
|
|
262
|
+
// For other sources, ask libjpeg to convert to the requested color space.
|
|
263
|
+
if (mode != ImageReadMode::UNCHANGED && !cmyk_to_rgb_or_gray) {
|
|
264
|
+
if (mode == ImageReadMode::GRAY) {
|
|
265
|
+
jpeg_ctx.out_color_space = JCS_GRAYSCALE;
|
|
266
|
+
} else {
|
|
267
|
+
STD_TORCH_CHECK(mode == ImageReadMode::RGB, "Should never reach here.");
|
|
268
|
+
jpeg_ctx.out_color_space = JCS_RGB;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
jpeg_start_decompress(&jpeg_ctx);
|
|
272
|
+
return {num_output_channels, cmyk_to_rgb_or_gray};
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// The actual decoding loop, row by row.
|
|
276
|
+
void decode_rows(
|
|
277
|
+
jpeg_decompress_struct& jpeg_ctx,
|
|
278
|
+
ErrorCtx& error_ctx,
|
|
279
|
+
uint8_t* output_ptr,
|
|
280
|
+
int64_t stride,
|
|
281
|
+
CMYKHelper& cmyk_helper) {
|
|
282
|
+
if (setjmp(error_ctx.setjmp_buffer)) {
|
|
283
|
+
// See Note [libjpeg error handling]
|
|
284
|
+
jpeg_destroy_decompress(&jpeg_ctx);
|
|
285
|
+
STD_TORCH_CHECK(false, error_ctx.last_error_message);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
while (jpeg_ctx.output_scanline < jpeg_ctx.output_height) {
|
|
289
|
+
if (cmyk_helper.cmyk_line_ptr != nullptr &&
|
|
290
|
+
cmyk_helper.convert_fn != nullptr) {
|
|
291
|
+
jpeg_read_scanlines(
|
|
292
|
+
&jpeg_ctx, &cmyk_helper.cmyk_line_ptr, /*max_lines=*/1);
|
|
293
|
+
cmyk_helper.convert_fn(
|
|
294
|
+
jpeg_ctx.output_width, cmyk_helper.cmyk_line_ptr, output_ptr);
|
|
295
|
+
} else {
|
|
296
|
+
jpeg_read_scanlines(&jpeg_ctx, &output_ptr, /*max_lines=*/1);
|
|
297
|
+
}
|
|
298
|
+
output_ptr += stride;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
jpeg_finish_decompress(&jpeg_ctx);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
} // namespace
|
|
305
|
+
|
|
306
|
+
/* clang-format off */
|
|
307
|
+
//
|
|
308
|
+
// Note [libjpeg error handling]
|
|
309
|
+
//
|
|
310
|
+
// The structure of our code is:
|
|
311
|
+
//
|
|
312
|
+
// ```
|
|
313
|
+
// decode_jpeg():
|
|
314
|
+
// torch::stable Tensor output;
|
|
315
|
+
// read_header_and_start():
|
|
316
|
+
// setjmp() {STD_TORCH_CHECK(false)}
|
|
317
|
+
// libjpeg call stack that can trigger a callback, where we longjmp back
|
|
318
|
+
// to the setjmp() above
|
|
319
|
+
// decode_rows():
|
|
320
|
+
// setjmp() {STD_TORCH_CHECK(false)}
|
|
321
|
+
// libjpeg call stack that can trigger a callback, where we longjmp back
|
|
322
|
+
// to the setjmp() above
|
|
323
|
+
// ```
|
|
324
|
+
//
|
|
325
|
+
// There's a reason for this structure: it is important that the `output` tensor
|
|
326
|
+
// is declared in a function that DOES NOT define a setjmp() (same for any other
|
|
327
|
+
// object that needs a non-trivial destructor). We previously had both the
|
|
328
|
+
// setjmp() and the tensor declaration within decode_jpeg(), with the
|
|
329
|
+
// read_header_and_start() and decode_rows() inlined there, and we would get a
|
|
330
|
+
// segault in the test_truncated_jpeg_raises() test:
|
|
331
|
+
//
|
|
332
|
+
// ```
|
|
333
|
+
// decode_jpeg(): (bad, UB territory)
|
|
334
|
+
// torch::stable Tensor output;
|
|
335
|
+
// setjmp() {STD_TORCH_CHECK(false)}
|
|
336
|
+
// libjpeg call stack that can trigger a callback, where we longjmp back
|
|
337
|
+
// to the setjmp() above
|
|
338
|
+
// ```
|
|
339
|
+
//
|
|
340
|
+
// Let's [try to] explain why. First, a bit of background on setjmp and longjmp:
|
|
341
|
+
// they're the closest that C can get to exceptions. You define a setjmp() point
|
|
342
|
+
// where you handle any error, and you get to that setjmp block by calling
|
|
343
|
+
// longjmp(). It's a glorified GOTO that restores [part of] the stack to the
|
|
344
|
+
// point of the setjmp() call:
|
|
345
|
+
//
|
|
346
|
+
// ```C
|
|
347
|
+
// jmp_buf setjmp_buffer;
|
|
348
|
+
// if (setjmp(setjmp_buffer) != 0) {
|
|
349
|
+
// error path: exit gracefully, handle error, etc.
|
|
350
|
+
// }
|
|
351
|
+
//
|
|
352
|
+
// // Some code down the line:
|
|
353
|
+
// longjmp(setjmp_buffer, /*err=*/ 1); <-- jumps back to the setjmp point
|
|
354
|
+
// ```
|
|
355
|
+
//
|
|
356
|
+
// The setjmp_buffer works such that you don't enter the setjmp block on the
|
|
357
|
+
// first pass, but you do enter it when you longjmp back to it.
|
|
358
|
+
//
|
|
359
|
+
// libjpeg recommends using setjmp/longjmp but doesn't *force* us to: libjpeg
|
|
360
|
+
// just triggers callbacks on errors, like error_exit_cb(), and it's up to us to
|
|
361
|
+
// handle the error there. But we do use setjmp/longjmp because we can't
|
|
362
|
+
// directly throw exceptions within the callback itself (more on that later)
|
|
363
|
+
//
|
|
364
|
+
// Now, there's an important rule (C11 7.13.2.1):
|
|
365
|
+
// > the values of objects of automatic storage duration that are local to the
|
|
366
|
+
// function containing the invocation of the corresponding setjmp macro that do
|
|
367
|
+
// not have volatile-qualified type and have been changed between the setjmp
|
|
368
|
+
// invocation and longjmp call are indeterminate.
|
|
369
|
+
//
|
|
370
|
+
// In other words, if we do what we had before:
|
|
371
|
+
//
|
|
372
|
+
// ```
|
|
373
|
+
// decode_jpeg(): (bad, UB territory)
|
|
374
|
+
// torch::stable Tensor output; // not a volatile
|
|
375
|
+
//
|
|
376
|
+
// // setjump() in the same function as the tensor declaration
|
|
377
|
+
// setjmp() {STD_TORCH_CHECK(false)}
|
|
378
|
+
//
|
|
379
|
+
// modify output in the call stack
|
|
380
|
+
//
|
|
381
|
+
// libjpeg call stack that can trigger a callback, where we longjmp back
|
|
382
|
+
// to the setjmp() above
|
|
383
|
+
// ```
|
|
384
|
+
//
|
|
385
|
+
// then we have the `output` tensor which is:
|
|
386
|
+
// - not a volatile
|
|
387
|
+
// - in the same function that defines the setjmp()
|
|
388
|
+
// - modified
|
|
389
|
+
//
|
|
390
|
+
// which means `output` has indeterminate value within the setjmp() block:
|
|
391
|
+
// bummer, that's where its destructor is needed, and that's why we segfault.
|
|
392
|
+
// You won't always segfault BTW, it depends on the compiler optimization level
|
|
393
|
+
// and other factors, but it's still UB.
|
|
394
|
+
//
|
|
395
|
+
// So, from there we have a few solutions:
|
|
396
|
+
//
|
|
397
|
+
// 1. Make the `output` tensor a volatile. This works, but making the
|
|
398
|
+
// torch::stable::Tensor volatile isn't directly possible. We have to have a
|
|
399
|
+
// volatile `torch::stable::Tensor*` pointer, and we lose RAII semantics.
|
|
400
|
+
// Meh.
|
|
401
|
+
// 2. Not use setjmp/longjmp and just throw STD_TORCH_CHECK() directly in the
|
|
402
|
+
// callbacks, i.e. `void error_exit_cb(...) { STD_TORCH_CHECK(false, "error"); }`.
|
|
403
|
+
// The issue here is that this callback is invoked from libjpeg C code. And
|
|
404
|
+
// for the exception to propagate up the stack, where the user may want to
|
|
405
|
+
// catch it within a try block, the stack must 'unwind' the exception through
|
|
406
|
+
// C code. Whether C code can unwind C++ exceptions depends on how that C
|
|
407
|
+
// code was compiled (there are compiler-specific flags). Usually, it does,
|
|
408
|
+
// but we have no control over how libjpeg was built. It's just easier and
|
|
409
|
+
// safer not to assume anything.
|
|
410
|
+
// 3. Don't declare `output` in the same function as the setjmp(). That's what
|
|
411
|
+
// we do. That's why `decode_jpeg()` declares output and then calls
|
|
412
|
+
// read_header_and_start() and decode_rows() where the setjmp points are
|
|
413
|
+
// defined. Critically, it means that neither `read_header_and_start()` nor
|
|
414
|
+
// `decore_rows()` should be declaring anything that needs proper destruction
|
|
415
|
+
// within the setjmp/longjmp context.
|
|
416
|
+
//
|
|
417
|
+
/* clang-format on */
|
|
418
|
+
|
|
419
|
+
torch::stable::Tensor decode_jpeg(
|
|
420
|
+
const torch::stable::Tensor& input,
|
|
421
|
+
int64_t mode) {
|
|
422
|
+
auto contig_input = validate_encoded_data(input);
|
|
423
|
+
|
|
424
|
+
torch::stable::Tensor output;
|
|
425
|
+
torch::stable::Tensor cmyk_line_tensor;
|
|
426
|
+
|
|
427
|
+
jpeg_decompress_struct jpeg_ctx;
|
|
428
|
+
ErrorCtx error_ctx;
|
|
429
|
+
jpeg_ctx.err = jpeg_std_error(&error_ctx.base);
|
|
430
|
+
error_ctx.base.error_exit = error_exit_cb;
|
|
431
|
+
|
|
432
|
+
auto [num_output_channels, cmyk_to_rgb_or_gray] = read_header_and_start(
|
|
433
|
+
jpeg_ctx,
|
|
434
|
+
error_ctx,
|
|
435
|
+
contig_input.const_data_ptr<uint8_t>(),
|
|
436
|
+
contig_input.numel(),
|
|
437
|
+
static_cast<ImageReadMode>(mode));
|
|
438
|
+
|
|
439
|
+
// Note: this must be called before jpeg_finish_decompress(), otherwise we get
|
|
440
|
+
// garbage exif data.
|
|
441
|
+
ExifOrientation exif_orientation = fetch_jpeg_exif_orientation(&jpeg_ctx);
|
|
442
|
+
|
|
443
|
+
// We want output to be channels last
|
|
444
|
+
int64_t stride =
|
|
445
|
+
static_cast<int64_t>(jpeg_ctx.output_width) * num_output_channels;
|
|
446
|
+
output = torch::stable::empty(
|
|
447
|
+
{int64_t(jpeg_ctx.output_height),
|
|
448
|
+
int64_t(jpeg_ctx.output_width),
|
|
449
|
+
num_output_channels},
|
|
450
|
+
kStableUInt8);
|
|
451
|
+
|
|
452
|
+
auto output_ptr = output.mutable_data_ptr<uint8_t>();
|
|
453
|
+
|
|
454
|
+
CMYKHelper cmyk_helper = {nullptr, nullptr};
|
|
455
|
+
if (cmyk_to_rgb_or_gray) {
|
|
456
|
+
cmyk_line_tensor =
|
|
457
|
+
torch::stable::empty({int64_t(jpeg_ctx.output_width), 4}, kStableUInt8);
|
|
458
|
+
cmyk_helper.cmyk_line_ptr = cmyk_line_tensor.mutable_data_ptr<uint8_t>();
|
|
459
|
+
if (num_output_channels == 3) {
|
|
460
|
+
cmyk_helper.convert_fn = convert_line_cmyk_to_rgb;
|
|
461
|
+
} else if (num_output_channels == 1) {
|
|
462
|
+
cmyk_helper.convert_fn = convert_line_cmyk_to_gray;
|
|
463
|
+
} else {
|
|
464
|
+
STD_TORCH_CHECK(
|
|
465
|
+
false,
|
|
466
|
+
"Should never reach here, this is a bug in TorchCodec, please report");
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
decode_rows(jpeg_ctx, error_ctx, output_ptr, stride, cmyk_helper);
|
|
471
|
+
|
|
472
|
+
// Flip raw CMYK samples so our output matches PIL.
|
|
473
|
+
if (static_cast<ImageReadMode>(mode) == ImageReadMode::UNCHANGED &&
|
|
474
|
+
(jpeg_ctx.jpeg_color_space == JCS_CMYK ||
|
|
475
|
+
jpeg_ctx.jpeg_color_space == JCS_YCCK)) {
|
|
476
|
+
int64_t num_bytes = output.numel();
|
|
477
|
+
for (int64_t i = 0; i < num_bytes; ++i) {
|
|
478
|
+
output_ptr[i] = static_cast<uint8_t>(255 - output_ptr[i]);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
jpeg_destroy_decompress(&jpeg_ctx);
|
|
483
|
+
return exif_orientation_transform(
|
|
484
|
+
stable_permute(output, {2, 0, 1}), exif_orientation);
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
} // namespace facebook::torchcodec
|
|
488
|
+
|
|
489
|
+
#endif // !TORCHCODEC_ENABLE_JPEG
|
|
@@ -0,0 +1,17 @@
|
|
|
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 "StableABICompat.h"
|
|
10
|
+
|
|
11
|
+
namespace facebook::torchcodec {
|
|
12
|
+
|
|
13
|
+
FORCE_PUBLIC_VISIBILITY torch::stable::Tensor decode_jpeg(
|
|
14
|
+
const torch::stable::Tensor& input,
|
|
15
|
+
int64_t mode);
|
|
16
|
+
|
|
17
|
+
} // namespace facebook::torchcodec
|