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,92 @@
|
|
|
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 "CUDACommon.h"
|
|
10
|
+
#include "DeviceInterface.h"
|
|
11
|
+
#include "FilterGraph.h"
|
|
12
|
+
#include "color_conversion.h"
|
|
13
|
+
|
|
14
|
+
namespace facebook::torchcodec {
|
|
15
|
+
|
|
16
|
+
class CudaDeviceInterface : public DeviceInterface {
|
|
17
|
+
public:
|
|
18
|
+
// Pixel format used for encoding on CUDA devices.
|
|
19
|
+
static constexpr AVPixelFormat CUDA_ENCODING_PIXEL_FORMAT = AV_PIX_FMT_NV12;
|
|
20
|
+
|
|
21
|
+
CudaDeviceInterface(const StableDevice& device);
|
|
22
|
+
|
|
23
|
+
virtual ~CudaDeviceInterface();
|
|
24
|
+
|
|
25
|
+
std::optional<const AVCodec*> find_codec(
|
|
26
|
+
const AVCodecID& codec_id,
|
|
27
|
+
bool is_decoder = true) override;
|
|
28
|
+
|
|
29
|
+
void initialize(const SharedAVCodecContext& codec_context) override;
|
|
30
|
+
|
|
31
|
+
using DeviceInterface::initialize_video_decoding;
|
|
32
|
+
void initialize_video_decoding(
|
|
33
|
+
const CodecParameters& codec_parameters,
|
|
34
|
+
const VideoStreamOptions& video_stream_options) override;
|
|
35
|
+
|
|
36
|
+
void initialize_color_conversion(
|
|
37
|
+
const VideoStreamOptions& video_stream_options,
|
|
38
|
+
const std::vector<std::unique_ptr<Transform>>& transforms,
|
|
39
|
+
const std::optional<FrameDims>& resized_output_dims) override;
|
|
40
|
+
|
|
41
|
+
void register_hardware_device_with_codec(
|
|
42
|
+
AVCodecContext* codec_context) override;
|
|
43
|
+
|
|
44
|
+
void convert_av_frame_to_frame_output(
|
|
45
|
+
const AVFrame& av_frame,
|
|
46
|
+
FrameOutput& frame_output,
|
|
47
|
+
std::optional<torch::stable::Tensor> pre_allocated_output_tensor)
|
|
48
|
+
override;
|
|
49
|
+
|
|
50
|
+
std::string get_details() override;
|
|
51
|
+
|
|
52
|
+
UniqueAVFrame convert_tensor_to_av_frame_for_encoding(
|
|
53
|
+
const torch::stable::Tensor& tensor,
|
|
54
|
+
int frame_index,
|
|
55
|
+
AVCodecContext* codec_context) override;
|
|
56
|
+
|
|
57
|
+
AVPixelFormat get_encoding_pixel_format(
|
|
58
|
+
const AVCodec& av_codec,
|
|
59
|
+
const std::optional<std::string>& user_pixel_format) const override;
|
|
60
|
+
|
|
61
|
+
void setup_hardware_frame_context_for_encoding(
|
|
62
|
+
AVCodecContext* codec_context) override;
|
|
63
|
+
|
|
64
|
+
private:
|
|
65
|
+
// Our CUDA decoding code assumes NV12 format. In order to handle other
|
|
66
|
+
// kinds of input, we need to convert them to NV12. Our current implementation
|
|
67
|
+
// does this using filtergraph. Returns a null frame when no conversion is
|
|
68
|
+
// needed, i.e. when the input frame can be used as-is.
|
|
69
|
+
UniqueAVFrame maybe_convert_av_frame_to_nv12_or_rgb24(
|
|
70
|
+
const AVFrame& av_frame);
|
|
71
|
+
|
|
72
|
+
// We sometimes encounter frames that cannot be decoded on the CUDA device.
|
|
73
|
+
// Rather than erroring out, we decode them on the CPU.
|
|
74
|
+
std::unique_ptr<DeviceInterface> cpu_interface_;
|
|
75
|
+
|
|
76
|
+
VideoStreamOptions video_stream_options_;
|
|
77
|
+
AVRational time_base_;
|
|
78
|
+
|
|
79
|
+
UniqueAVBufferRef hardware_device_ctx_;
|
|
80
|
+
|
|
81
|
+
// This filtergraph instance is only used for NV12 format conversion in
|
|
82
|
+
// maybeConvertAVFrameToNV12().
|
|
83
|
+
std::unique_ptr<FiltersConfig> nv12_conversion_config_;
|
|
84
|
+
std::unique_ptr<FilterGraph> nv12_conversion_;
|
|
85
|
+
|
|
86
|
+
bool using_cpu_fallback_ = false;
|
|
87
|
+
bool has_decoded_frame_ = false;
|
|
88
|
+
|
|
89
|
+
CachedColorMatrix cached_color_matrix_;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
} // namespace facebook::torchcodec
|
|
@@ -0,0 +1,284 @@
|
|
|
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 "DecodeAvif.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_AVIF
|
|
16
|
+
|
|
17
|
+
namespace facebook::torchcodec {
|
|
18
|
+
|
|
19
|
+
torch::stable::Tensor decode_avif(
|
|
20
|
+
[[maybe_unused]] const torch::stable::Tensor& input,
|
|
21
|
+
[[maybe_unused]] int64_t mode,
|
|
22
|
+
[[maybe_unused]] int64_t output_dtype,
|
|
23
|
+
[[maybe_unused]] int64_t num_threads) {
|
|
24
|
+
STD_TORCH_CHECK(
|
|
25
|
+
false,
|
|
26
|
+
"decode_avif: torchcodec was not compiled with libavif support. Rebuild "
|
|
27
|
+
"torchcodec with TORCHCODEC_BUILD_AVIF=1. If you see this error in a "
|
|
28
|
+
"prebuilt wheel, please report it to the TorchCodec repo.");
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
std::vector<int64_t> probe_avif_from_tensor(
|
|
32
|
+
[[maybe_unused]] const torch::stable::Tensor& input) {
|
|
33
|
+
STD_TORCH_CHECK(
|
|
34
|
+
false,
|
|
35
|
+
"get_image_metadata: torchcodec was not compiled with libavif support. "
|
|
36
|
+
"Rebuild torchcodec with TORCHCODEC_BUILD_AVIF=1. If you see this error "
|
|
37
|
+
"in a prebuilt wheel, please report it to the TorchCodec repo.");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
std::vector<int64_t> probe_avif_from_file(
|
|
41
|
+
[[maybe_unused]] std::string filename) {
|
|
42
|
+
STD_TORCH_CHECK(
|
|
43
|
+
false,
|
|
44
|
+
"get_image_metadata: torchcodec was not compiled with libavif support. "
|
|
45
|
+
"Rebuild torchcodec with TORCHCODEC_BUILD_AVIF=1. If you see this error "
|
|
46
|
+
"in a prebuilt wheel, please report it to the TorchCodec repo.");
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
} // namespace facebook::torchcodec
|
|
50
|
+
|
|
51
|
+
#else
|
|
52
|
+
|
|
53
|
+
#include <torch/headeronly/core/MemoryFormat.h>
|
|
54
|
+
|
|
55
|
+
#include <cstring>
|
|
56
|
+
#include <memory>
|
|
57
|
+
|
|
58
|
+
#include "avif/avif.h"
|
|
59
|
+
|
|
60
|
+
#include "Exif.h"
|
|
61
|
+
#include "ImageCommon.h"
|
|
62
|
+
|
|
63
|
+
namespace facebook::torchcodec {
|
|
64
|
+
|
|
65
|
+
using namespace exif_private;
|
|
66
|
+
|
|
67
|
+
namespace {
|
|
68
|
+
|
|
69
|
+
// This normally comes from avif_cxx.h, but that header isn't always present
|
|
70
|
+
// when installing libavif, so we define the deleter ourselves.
|
|
71
|
+
struct AvifDecoderDeleter {
|
|
72
|
+
void operator()(avifDecoder* decoder) const {
|
|
73
|
+
avifDecoderDestroy(decoder);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
using DecoderPtr = std::unique_ptr<avifDecoder, AvifDecoderDeleter>;
|
|
78
|
+
|
|
79
|
+
// AVIF stores orientation as separate 'irot' and 'imir' transforms rather than
|
|
80
|
+
// a single EXIF orientation value. We map them to the equivalent EXIF
|
|
81
|
+
// orientation so we can reuse exif_orientation_transform() and stay consistent
|
|
82
|
+
// with the other decoders. This is a port of libavif's own
|
|
83
|
+
// avifImageIrotImirToExifOrientation().
|
|
84
|
+
ExifOrientation avif_exif_orientation(const avifImage* image) {
|
|
85
|
+
bool has_irot = (image->transformFlags & AVIF_TRANSFORM_IROT) != 0;
|
|
86
|
+
bool has_imir = (image->transformFlags & AVIF_TRANSFORM_IMIR) != 0;
|
|
87
|
+
uint8_t angle = has_irot ? image->irot.angle : 0;
|
|
88
|
+
uint8_t axis = image->imir.axis;
|
|
89
|
+
|
|
90
|
+
using E = ExifOrientation;
|
|
91
|
+
switch (angle) {
|
|
92
|
+
case 0:
|
|
93
|
+
return !has_imir ? E::TopLeft : (axis == 0 ? E::BottomLeft : E::TopRight);
|
|
94
|
+
case 1:
|
|
95
|
+
return !has_imir ? E::LeftBottom
|
|
96
|
+
: (axis == 0 ? E::LeftTop : E::RightBottom);
|
|
97
|
+
case 2:
|
|
98
|
+
return !has_imir ? E::BottomRight
|
|
99
|
+
: (axis == 0 ? E::TopRight : E::BottomLeft);
|
|
100
|
+
default: // angle == 3
|
|
101
|
+
return !has_imir ? E::RightTop
|
|
102
|
+
: (axis == 0 ? E::RightBottom : E::LeftTop);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
} // namespace
|
|
107
|
+
|
|
108
|
+
torch::stable::Tensor decode_avif(
|
|
109
|
+
const torch::stable::Tensor& input,
|
|
110
|
+
int64_t mode,
|
|
111
|
+
int64_t output_dtype,
|
|
112
|
+
int64_t num_threads) {
|
|
113
|
+
// Based on
|
|
114
|
+
// https://github.com/AOMediaCodec/libavif/blob/main/examples/avif_example_decode_memory.c
|
|
115
|
+
auto contig_input = validate_encoded_data(input);
|
|
116
|
+
STD_TORCH_CHECK(
|
|
117
|
+
num_threads >= 1, "num_threads must be >= 1, got ", num_threads);
|
|
118
|
+
|
|
119
|
+
DecoderPtr decoder(avifDecoderCreate());
|
|
120
|
+
STD_TORCH_CHECK(decoder != nullptr, "Failed to create avif decoder.");
|
|
121
|
+
|
|
122
|
+
decoder->maxThreads = static_cast<int>(num_threads);
|
|
123
|
+
|
|
124
|
+
auto result = avifDecoderSetIOMemory(
|
|
125
|
+
decoder.get(),
|
|
126
|
+
contig_input.const_data_ptr<uint8_t>(),
|
|
127
|
+
contig_input.numel());
|
|
128
|
+
STD_TORCH_CHECK(
|
|
129
|
+
result == AVIF_RESULT_OK,
|
|
130
|
+
"avifDecoderSetIOMemory failed: ",
|
|
131
|
+
avifResultToString(result));
|
|
132
|
+
|
|
133
|
+
result = avifDecoderParse(decoder.get());
|
|
134
|
+
STD_TORCH_CHECK(
|
|
135
|
+
result == AVIF_RESULT_OK,
|
|
136
|
+
"avifDecoderParse failed: ",
|
|
137
|
+
avifResultToString(result));
|
|
138
|
+
|
|
139
|
+
// irot/imir are populated during parse and are constant across the sequence.
|
|
140
|
+
ExifOrientation exif_orientation = avif_exif_orientation(decoder->image);
|
|
141
|
+
|
|
142
|
+
// We detect animation via imageSequenceTrackPresent (the signal that
|
|
143
|
+
// specifically means "image sequence"), not imageCount. imageCount can also
|
|
144
|
+
// exceed 1 for a *progressive* still images.
|
|
145
|
+
int64_t num_frames =
|
|
146
|
+
decoder->imageSequenceTrackPresent ? decoder->imageCount : 1;
|
|
147
|
+
|
|
148
|
+
// alphaPresent is valid after parse and constant across the sequence.
|
|
149
|
+
auto return_rgb = should_return_rgb(
|
|
150
|
+
static_cast<ImageReadMode>(mode),
|
|
151
|
+
static_cast<bool>(decoder->alphaPresent));
|
|
152
|
+
int num_channels = return_rgb ? 3 : 4;
|
|
153
|
+
|
|
154
|
+
bool output_16 = should_output_uint16(
|
|
155
|
+
static_cast<OutputDtype>(output_dtype), decoder->image->depth > 8);
|
|
156
|
+
|
|
157
|
+
torch::stable::Tensor output;
|
|
158
|
+
uint8_t* output_ptr = nullptr;
|
|
159
|
+
int64_t frame_num_bytes = 0;
|
|
160
|
+
|
|
161
|
+
for (int64_t i = 0; i < num_frames; ++i) {
|
|
162
|
+
result = avifDecoderNextImage(decoder.get());
|
|
163
|
+
STD_TORCH_CHECK(
|
|
164
|
+
result == AVIF_RESULT_OK,
|
|
165
|
+
"avifDecoderNextImage failed at frame ",
|
|
166
|
+
i,
|
|
167
|
+
": ",
|
|
168
|
+
avifResultToString(result));
|
|
169
|
+
|
|
170
|
+
avifRGBImage rgb;
|
|
171
|
+
std::memset(&rgb, 0, sizeof(rgb));
|
|
172
|
+
avifRGBImageSetDefaults(&rgb, decoder->image);
|
|
173
|
+
|
|
174
|
+
rgb.depth = output_16 ? 16 : 8;
|
|
175
|
+
rgb.format = return_rgb ? AVIF_RGB_FORMAT_RGB : AVIF_RGB_FORMAT_RGBA;
|
|
176
|
+
rgb.ignoreAlpha = return_rgb ? AVIF_TRUE : AVIF_FALSE;
|
|
177
|
+
|
|
178
|
+
if (i == 0) {
|
|
179
|
+
output = torch::stable::empty(
|
|
180
|
+
{num_frames,
|
|
181
|
+
num_channels,
|
|
182
|
+
static_cast<int64_t>(rgb.height),
|
|
183
|
+
static_cast<int64_t>(rgb.width)},
|
|
184
|
+
output_16 ? kStableUInt16 : kStableUInt8,
|
|
185
|
+
std::nullopt,
|
|
186
|
+
std::nullopt,
|
|
187
|
+
std::nullopt,
|
|
188
|
+
torch::headeronly::MemoryFormat::ChannelsLast);
|
|
189
|
+
output_ptr = static_cast<uint8_t*>(output.mutable_data_ptr());
|
|
190
|
+
frame_num_bytes = static_cast<int64_t>(num_channels) * rgb.height *
|
|
191
|
+
rgb.width * (output_16 ? 2 : 1);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
rgb.pixels = output_ptr + i * frame_num_bytes;
|
|
195
|
+
rgb.rowBytes = rgb.width * avifRGBImagePixelSize(&rgb);
|
|
196
|
+
|
|
197
|
+
result = avifImageYUVToRGB(decoder->image, &rgb);
|
|
198
|
+
STD_TORCH_CHECK(
|
|
199
|
+
result == AVIF_RESULT_OK,
|
|
200
|
+
"avifImageYUVToRGB failed at frame ",
|
|
201
|
+
i,
|
|
202
|
+
": ",
|
|
203
|
+
avifResultToString(result));
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
output = exif_orientation_transform(output, exif_orientation);
|
|
207
|
+
|
|
208
|
+
if (num_frames == 1) {
|
|
209
|
+
output = select_row(output, 0);
|
|
210
|
+
}
|
|
211
|
+
return output;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
namespace {
|
|
215
|
+
|
|
216
|
+
ImageProbeResult parse_and_probe(avifDecoder* decoder) {
|
|
217
|
+
auto result = avifDecoderParse(decoder);
|
|
218
|
+
STD_TORCH_CHECK(
|
|
219
|
+
result == AVIF_RESULT_OK,
|
|
220
|
+
"avifDecoderParse failed: ",
|
|
221
|
+
avifResultToString(result));
|
|
222
|
+
|
|
223
|
+
int64_t width = decoder->image->width;
|
|
224
|
+
int64_t height = decoder->image->height;
|
|
225
|
+
ExifOrientation exif_orientation = avif_exif_orientation(decoder->image);
|
|
226
|
+
if (exif_orientation == ExifOrientation::LeftTop ||
|
|
227
|
+
exif_orientation == ExifOrientation::RightTop ||
|
|
228
|
+
exif_orientation == ExifOrientation::RightBottom ||
|
|
229
|
+
exif_orientation == ExifOrientation::LeftBottom) {
|
|
230
|
+
std::swap(width, height);
|
|
231
|
+
}
|
|
232
|
+
bool has_orientation = (decoder->image->transformFlags &
|
|
233
|
+
(AVIF_TRANSFORM_IROT | AVIF_TRANSFORM_IMIR)) != 0;
|
|
234
|
+
// Same frame count logic as decode_avif().
|
|
235
|
+
int64_t num_frames =
|
|
236
|
+
decoder->imageSequenceTrackPresent ? decoder->imageCount : 1;
|
|
237
|
+
return {
|
|
238
|
+
width,
|
|
239
|
+
height,
|
|
240
|
+
decoder->alphaPresent ? 4 : 3,
|
|
241
|
+
static_cast<int64_t>(decoder->image->depth),
|
|
242
|
+
num_frames,
|
|
243
|
+
static_cast<int64_t>(
|
|
244
|
+
has_orientation ? exif_orientation : ExifOrientation::Unspecified)};
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
} // namespace
|
|
248
|
+
|
|
249
|
+
ImageProbeResult probe_avif_from_tensor(const torch::stable::Tensor& input) {
|
|
250
|
+
auto contig_input = validate_encoded_data(input);
|
|
251
|
+
|
|
252
|
+
DecoderPtr decoder(avifDecoderCreate());
|
|
253
|
+
STD_TORCH_CHECK(decoder != nullptr, "Failed to create avif decoder.");
|
|
254
|
+
|
|
255
|
+
auto result = avifDecoderSetIOMemory(
|
|
256
|
+
decoder.get(),
|
|
257
|
+
contig_input.const_data_ptr<uint8_t>(),
|
|
258
|
+
contig_input.numel());
|
|
259
|
+
STD_TORCH_CHECK(
|
|
260
|
+
result == AVIF_RESULT_OK,
|
|
261
|
+
"avifDecoderSetIOMemory failed: ",
|
|
262
|
+
avifResultToString(result));
|
|
263
|
+
return parse_and_probe(decoder.get());
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// Same as probe_avif_from_tensor, but only reads the parts of the file that
|
|
267
|
+
// libavif needs, instead of requiring all of it in memory. Similarly to what we
|
|
268
|
+
// do with the gif probe, this avoids loading the whole file into a tensor which
|
|
269
|
+
// matters for large files.
|
|
270
|
+
ImageProbeResult probe_avif_from_file(std::string filename) {
|
|
271
|
+
DecoderPtr decoder(avifDecoderCreate());
|
|
272
|
+
STD_TORCH_CHECK(decoder != nullptr, "Failed to create avif decoder.");
|
|
273
|
+
|
|
274
|
+
auto result = avifDecoderSetIOFile(decoder.get(), filename.c_str());
|
|
275
|
+
STD_TORCH_CHECK(
|
|
276
|
+
result == AVIF_RESULT_OK,
|
|
277
|
+
"avifDecoderSetIOFile failed: ",
|
|
278
|
+
avifResultToString(result));
|
|
279
|
+
return parse_and_probe(decoder.get());
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
} // namespace facebook::torchcodec
|
|
283
|
+
|
|
284
|
+
#endif // !TORCHCODEC_ENABLE_AVIF
|
|
@@ -0,0 +1,28 @@
|
|
|
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
|
+
FORCE_PUBLIC_VISIBILITY torch::stable::Tensor decode_avif(
|
|
17
|
+
const torch::stable::Tensor& input,
|
|
18
|
+
int64_t mode,
|
|
19
|
+
int64_t output_dtype,
|
|
20
|
+
int64_t num_threads);
|
|
21
|
+
|
|
22
|
+
FORCE_PUBLIC_VISIBILITY std::vector<int64_t> probe_avif_from_tensor(
|
|
23
|
+
const torch::stable::Tensor& input);
|
|
24
|
+
|
|
25
|
+
FORCE_PUBLIC_VISIBILITY std::vector<int64_t> probe_avif_from_file(
|
|
26
|
+
std::string filename);
|
|
27
|
+
|
|
28
|
+
} // namespace facebook::torchcodec
|