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,306 @@
|
|
|
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
|
+
|
|
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_HEIC
|
|
16
|
+
|
|
17
|
+
namespace facebook::torchcodec {
|
|
18
|
+
|
|
19
|
+
torch::stable::Tensor decode_heic(
|
|
20
|
+
[[maybe_unused]] const torch::stable::Tensor& input,
|
|
21
|
+
[[maybe_unused]] int64_t mode,
|
|
22
|
+
[[maybe_unused]] int64_t output_dtype) {
|
|
23
|
+
STD_TORCH_CHECK(
|
|
24
|
+
false,
|
|
25
|
+
"decode_heic: torchcodec was not compiled with libheif support. Rebuild "
|
|
26
|
+
"torchcodec with TORCHCODEC_BUILD_HEIC=1. If you see this error in a "
|
|
27
|
+
"prebuilt wheel, please report it to the TorchCodec repo.");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
} // namespace facebook::torchcodec
|
|
31
|
+
|
|
32
|
+
#else
|
|
33
|
+
|
|
34
|
+
#include <torch/headeronly/core/MemoryFormat.h>
|
|
35
|
+
|
|
36
|
+
#include <bit>
|
|
37
|
+
#include <cstring>
|
|
38
|
+
#include <memory>
|
|
39
|
+
#include <vector>
|
|
40
|
+
|
|
41
|
+
#include "libheif/heif.h"
|
|
42
|
+
|
|
43
|
+
#include "ImageCommon.h"
|
|
44
|
+
|
|
45
|
+
namespace facebook::torchcodec {
|
|
46
|
+
|
|
47
|
+
namespace {
|
|
48
|
+
|
|
49
|
+
struct HeifContextDeleter {
|
|
50
|
+
void operator()(heif_context* ctx) const {
|
|
51
|
+
heif_context_free(ctx);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
struct HeifImageHandleDeleter {
|
|
56
|
+
void operator()(heif_image_handle* handle) const {
|
|
57
|
+
heif_image_handle_release(handle);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
struct HeifImageDeleter {
|
|
62
|
+
void operator()(heif_image* img) const {
|
|
63
|
+
heif_image_release(img);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
using UniqueHeifContext = std::unique_ptr<heif_context, HeifContextDeleter>;
|
|
68
|
+
using UniqueHeifImageHandle =
|
|
69
|
+
std::unique_ptr<heif_image_handle, HeifImageHandleDeleter>;
|
|
70
|
+
using UniqueHeifImage = std::unique_ptr<heif_image, HeifImageDeleter>;
|
|
71
|
+
|
|
72
|
+
} // namespace
|
|
73
|
+
|
|
74
|
+
torch::stable::Tensor decode_heic(
|
|
75
|
+
const torch::stable::Tensor& input,
|
|
76
|
+
int64_t mode,
|
|
77
|
+
int64_t output_dtype) {
|
|
78
|
+
auto contig_input = validate_encoded_data(input);
|
|
79
|
+
|
|
80
|
+
UniqueHeifContext ctx(heif_context_alloc());
|
|
81
|
+
STD_TORCH_CHECK(ctx != nullptr, "Failed to allocate libheif context.");
|
|
82
|
+
|
|
83
|
+
heif_error err = heif_context_read_from_memory_without_copy(
|
|
84
|
+
ctx.get(),
|
|
85
|
+
contig_input.const_data_ptr<uint8_t>(),
|
|
86
|
+
static_cast<size_t>(contig_input.numel()),
|
|
87
|
+
/*options=*/nullptr);
|
|
88
|
+
STD_TORCH_CHECK(
|
|
89
|
+
err.code == heif_error_Ok,
|
|
90
|
+
"heif_context_read_from_memory_without_copy failed: ",
|
|
91
|
+
err.message);
|
|
92
|
+
|
|
93
|
+
// A HEIF file can hold several "top-level" images (an image sequence /
|
|
94
|
+
// burst). We decode all of them into a batched (N, C, H, W) tensor, one per
|
|
95
|
+
// top-level image, and squeeze the batch dim below when there's only one.
|
|
96
|
+
// Thumbnails and grid tiles are not top-level images, so they don't show up
|
|
97
|
+
// here.
|
|
98
|
+
int num_images = heif_context_get_number_of_top_level_images(ctx.get());
|
|
99
|
+
STD_TORCH_CHECK(
|
|
100
|
+
num_images > 0, "HEIC file should contain at least one top-level image.");
|
|
101
|
+
|
|
102
|
+
std::vector<heif_item_id> image_ids(static_cast<size_t>(num_images));
|
|
103
|
+
int num_ids = heif_context_get_list_of_top_level_image_IDs(
|
|
104
|
+
ctx.get(), image_ids.data(), num_images);
|
|
105
|
+
STD_TORCH_CHECK(
|
|
106
|
+
num_ids == num_images,
|
|
107
|
+
"Expected ",
|
|
108
|
+
num_images,
|
|
109
|
+
" top-level HEIC image IDs but got ",
|
|
110
|
+
num_ids,
|
|
111
|
+
".");
|
|
112
|
+
|
|
113
|
+
// Geometry/format is derived from the first image and every subsequent frame
|
|
114
|
+
// is required to match it (validated below), so the whole sequence fits a
|
|
115
|
+
// single output tensor.
|
|
116
|
+
torch::stable::Tensor output;
|
|
117
|
+
uint8_t* output_ptr = nullptr;
|
|
118
|
+
int64_t frame_num_bytes = 0;
|
|
119
|
+
int64_t height = 0;
|
|
120
|
+
int64_t width = 0;
|
|
121
|
+
int num_channels = 0;
|
|
122
|
+
int bit_depth = 0;
|
|
123
|
+
bool decode_16 = false;
|
|
124
|
+
heif_chroma chroma = heif_chroma_undefined;
|
|
125
|
+
|
|
126
|
+
for (int64_t i = 0; i < num_images; ++i) {
|
|
127
|
+
heif_image_handle* raw_handle = nullptr;
|
|
128
|
+
err = heif_context_get_image_handle(
|
|
129
|
+
ctx.get(), image_ids[static_cast<size_t>(i)], &raw_handle);
|
|
130
|
+
STD_TORCH_CHECK(
|
|
131
|
+
err.code == heif_error_Ok,
|
|
132
|
+
"heif_context_get_image_handle failed at frame ",
|
|
133
|
+
i,
|
|
134
|
+
": ",
|
|
135
|
+
err.message);
|
|
136
|
+
UniqueHeifImageHandle handle(raw_handle);
|
|
137
|
+
|
|
138
|
+
int64_t frame_height = heif_image_handle_get_height(handle.get());
|
|
139
|
+
int64_t frame_width = heif_image_handle_get_width(handle.get());
|
|
140
|
+
int frame_bit_depth =
|
|
141
|
+
heif_image_handle_get_luma_bits_per_pixel(handle.get());
|
|
142
|
+
STD_TORCH_CHECK(
|
|
143
|
+
frame_bit_depth > 0,
|
|
144
|
+
"Failed to get a valid bit depth from the HEIC image.");
|
|
145
|
+
bool frame_has_alpha =
|
|
146
|
+
static_cast<bool>(heif_image_handle_has_alpha_channel(handle.get()));
|
|
147
|
+
|
|
148
|
+
if (i == 0) {
|
|
149
|
+
height = frame_height;
|
|
150
|
+
width = frame_width;
|
|
151
|
+
bit_depth = frame_bit_depth;
|
|
152
|
+
bool source_gt_8bit = bit_depth > 8;
|
|
153
|
+
|
|
154
|
+
bool return_rgb =
|
|
155
|
+
should_return_rgb(static_cast<ImageReadMode>(mode), frame_has_alpha);
|
|
156
|
+
num_channels = return_rgb ? 3 : 4;
|
|
157
|
+
|
|
158
|
+
// Decode into a 16-bit container only when the source actually carries >8
|
|
159
|
+
// bits AND 16-bit output is wanted. For uint8 output we let libheif
|
|
160
|
+
// downscale a >8-bit source straight to 8-bit. We never ask libheif to
|
|
161
|
+
// *widen* an 8-bit source to 16 bits: that would be a lossy 8->10->16
|
|
162
|
+
// hop, so 8->16 is done exactly as `* 257` in Python instead.
|
|
163
|
+
bool output_16 = should_output_uint16(
|
|
164
|
+
static_cast<OutputDtype>(output_dtype), source_gt_8bit);
|
|
165
|
+
decode_16 = source_gt_8bit && output_16;
|
|
166
|
+
|
|
167
|
+
constexpr bool little_endian = std::endian::native == std::endian::little;
|
|
168
|
+
if (decode_16) {
|
|
169
|
+
if (return_rgb) {
|
|
170
|
+
chroma = little_endian ? heif_chroma_interleaved_RRGGBB_LE
|
|
171
|
+
: heif_chroma_interleaved_RRGGBB_BE;
|
|
172
|
+
} else {
|
|
173
|
+
chroma = little_endian ? heif_chroma_interleaved_RRGGBBAA_LE
|
|
174
|
+
: heif_chroma_interleaved_RRGGBBAA_BE;
|
|
175
|
+
}
|
|
176
|
+
} else {
|
|
177
|
+
chroma = return_rgb ? heif_chroma_interleaved_RGB
|
|
178
|
+
: heif_chroma_interleaved_RGBA;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// ChannelsLast so each frame's region is laid out as contiguous
|
|
182
|
+
// interleaved HWC bytes, matching what libheif hands us below.
|
|
183
|
+
output = torch::stable::empty(
|
|
184
|
+
{static_cast<int64_t>(num_images), num_channels, height, width},
|
|
185
|
+
decode_16 ? kStableUInt16 : kStableUInt8,
|
|
186
|
+
std::nullopt,
|
|
187
|
+
std::nullopt,
|
|
188
|
+
std::nullopt,
|
|
189
|
+
torch::headeronly::MemoryFormat::ChannelsLast);
|
|
190
|
+
output_ptr = static_cast<uint8_t*>(output.mutable_data_ptr());
|
|
191
|
+
frame_num_bytes = num_channels * height * width * (decode_16 ? 2 : 1);
|
|
192
|
+
} else {
|
|
193
|
+
STD_TORCH_CHECK(
|
|
194
|
+
frame_height == height && frame_width == width &&
|
|
195
|
+
frame_bit_depth == bit_depth,
|
|
196
|
+
"HEIC image sequence has frames with mismatched geometry: frame ",
|
|
197
|
+
i,
|
|
198
|
+
" is ",
|
|
199
|
+
frame_width,
|
|
200
|
+
"x",
|
|
201
|
+
frame_height,
|
|
202
|
+
" at ",
|
|
203
|
+
frame_bit_depth,
|
|
204
|
+
" bits vs ",
|
|
205
|
+
width,
|
|
206
|
+
"x",
|
|
207
|
+
height,
|
|
208
|
+
" at ",
|
|
209
|
+
bit_depth,
|
|
210
|
+
" bits for the first frame. This is not supported.");
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// libheif applies the image's 'irot'/'imir' transforms during decode by
|
|
214
|
+
// default (heif_decoding_options.ignore_transformations == false), so the
|
|
215
|
+
// output is already correctly oriented. We deliberately do NOT run our own
|
|
216
|
+
// exif_orientation_transform() afterwards, to avoid double-applying.
|
|
217
|
+
heif_image* raw_img = nullptr;
|
|
218
|
+
err = heif_decode_image(
|
|
219
|
+
handle.get(),
|
|
220
|
+
&raw_img,
|
|
221
|
+
heif_colorspace_RGB,
|
|
222
|
+
chroma,
|
|
223
|
+
/*options=*/nullptr);
|
|
224
|
+
STD_TORCH_CHECK(
|
|
225
|
+
err.code == heif_error_Ok,
|
|
226
|
+
"heif_decode_image failed at frame ",
|
|
227
|
+
i,
|
|
228
|
+
": ",
|
|
229
|
+
err.message,
|
|
230
|
+
". If this is an \"Unsupported codec\" error, the libheif found at "
|
|
231
|
+
"runtime was built/installed without a decoder for this image's codec "
|
|
232
|
+
"(typically libde265 for HEVC-coded HEIC). Install a libheif with HEVC "
|
|
233
|
+
"decode support (e.g. `conda install -c conda-forge libheif`, which "
|
|
234
|
+
"pulls libde265).");
|
|
235
|
+
UniqueHeifImage img(raw_img);
|
|
236
|
+
|
|
237
|
+
int stride = 0;
|
|
238
|
+
const uint8_t* decoded_data = heif_image_get_plane_readonly(
|
|
239
|
+
img.get(), heif_channel_interleaved, &stride);
|
|
240
|
+
STD_TORCH_CHECK(
|
|
241
|
+
decoded_data != nullptr, "Failed to get the decoded HEIC image plane.");
|
|
242
|
+
|
|
243
|
+
// `width` and `height` come from the image handle, i.e. from the
|
|
244
|
+
// container's metadata, and that's what we used to allocate the output
|
|
245
|
+
// tensor. On malformed files, the actual decoded frame may have different
|
|
246
|
+
// dimensions. We enforce that they must agree, because the copy below uses
|
|
247
|
+
// the width and height to read from the decoded frame, and would read past
|
|
248
|
+
// its end if the plane were actually smaller.
|
|
249
|
+
int64_t plane_width =
|
|
250
|
+
heif_image_get_width(img.get(), heif_channel_interleaved);
|
|
251
|
+
int64_t plane_height =
|
|
252
|
+
heif_image_get_height(img.get(), heif_channel_interleaved);
|
|
253
|
+
STD_TORCH_CHECK(
|
|
254
|
+
plane_width == width && plane_height == height,
|
|
255
|
+
"HEIC frame ",
|
|
256
|
+
i,
|
|
257
|
+
" decoded to ",
|
|
258
|
+
plane_width,
|
|
259
|
+
"x",
|
|
260
|
+
plane_height,
|
|
261
|
+
" but its header declares ",
|
|
262
|
+
width,
|
|
263
|
+
"x",
|
|
264
|
+
height,
|
|
265
|
+
". This file is malformed.");
|
|
266
|
+
|
|
267
|
+
// Copy the decoded plane into this frame's region row by row: the plane's
|
|
268
|
+
// `stride` may include per-row padding, and the buffer is owned by `img`
|
|
269
|
+
// (freed when it goes out of scope), so we can't wrap it with from_blob.
|
|
270
|
+
uint8_t* frame_ptr = output_ptr + i * frame_num_bytes;
|
|
271
|
+
int64_t row_num_bytes = width * num_channels * (decode_16 ? 2 : 1);
|
|
272
|
+
for (int64_t h = 0; h < height; ++h) {
|
|
273
|
+
std::memcpy(
|
|
274
|
+
frame_ptr + h * row_num_bytes,
|
|
275
|
+
decoded_data + h * stride,
|
|
276
|
+
static_cast<size_t>(row_num_bytes));
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
if (decode_16) {
|
|
281
|
+
// libheif writes the decoded values at their native bit depth (e.g. in
|
|
282
|
+
// [0, 1023] for 10-bit), NOT scaled to the full uint16 range, so we expand
|
|
283
|
+
// them ourselves.
|
|
284
|
+
STD_TORCH_CHECK(
|
|
285
|
+
bit_depth <= 16,
|
|
286
|
+
"Unexpected HEIC bit depth greater than 16: ",
|
|
287
|
+
bit_depth);
|
|
288
|
+
int shift = 16 - bit_depth;
|
|
289
|
+
auto* output_ptr_16 = reinterpret_cast<uint16_t*>(output_ptr);
|
|
290
|
+
int64_t num_values = num_images * height * width * num_channels;
|
|
291
|
+
for (int64_t p = 0; p < num_values; ++p) {
|
|
292
|
+
uint16_t v = output_ptr_16[p];
|
|
293
|
+
output_ptr_16[p] =
|
|
294
|
+
static_cast<uint16_t>((v << shift) | (v >> (bit_depth - shift)));
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
if (num_images == 1) {
|
|
299
|
+
output = select_row(output, 0);
|
|
300
|
+
}
|
|
301
|
+
return output;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
} // namespace facebook::torchcodec
|
|
305
|
+
|
|
306
|
+
#endif // !TORCHCODEC_ENABLE_HEIC
|
|
@@ -0,0 +1,18 @@
|
|
|
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_heic(
|
|
14
|
+
const torch::stable::Tensor& input,
|
|
15
|
+
int64_t mode,
|
|
16
|
+
int64_t output_dtype);
|
|
17
|
+
|
|
18
|
+
} // namespace facebook::torchcodec
|