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,144 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
#include <map>
|
|
3
|
+
#include <optional>
|
|
4
|
+
#include <string>
|
|
5
|
+
#include <vector>
|
|
6
|
+
#include "AVIOContextHolder.h"
|
|
7
|
+
#include "DeviceInterface.h"
|
|
8
|
+
#include "FFMPEGCommon.h"
|
|
9
|
+
#include "StableABICompat.h"
|
|
10
|
+
#include "StreamOptions.h"
|
|
11
|
+
|
|
12
|
+
extern "C" {
|
|
13
|
+
#include <libavutil/dict.h>
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
namespace facebook::torchcodec {
|
|
17
|
+
|
|
18
|
+
/* clang-format off */
|
|
19
|
+
//
|
|
20
|
+
// Note: [Encoding loop, sample rate conversion and FIFO]
|
|
21
|
+
//
|
|
22
|
+
// The input samples are in a given format, sample rate, and number of channels.
|
|
23
|
+
// We may want to change these properties before encoding. The conversion is
|
|
24
|
+
// done in maybeConvertAudioAVFrame() and we rely on libswresample. When sample
|
|
25
|
+
// rate conversion is needed, this means two things:
|
|
26
|
+
// - swr will be storing samples in its internal buffers, which we'll need to
|
|
27
|
+
// flush at the very end of the encoding process.
|
|
28
|
+
// - the converted AVFrame we get back from maybeConvertAudioAVFrame() typically
|
|
29
|
+
// won't have the same number of samples as the original AVFrame. And that's
|
|
30
|
+
// a problem, because some encoders expect AVFrames with a specific and
|
|
31
|
+
// constant number of samples. If we were to send it as-is, we'd get an error
|
|
32
|
+
// in avcodec_send_frame(). In order to feed the encoder with AVFrames
|
|
33
|
+
// with the expected number of samples, we go through an intermediate FIFO
|
|
34
|
+
// from which we can pull the exact number of samples that we need. Note that
|
|
35
|
+
// this involves at least 2 additional copies.
|
|
36
|
+
//
|
|
37
|
+
// Unlike the old single-stream AudioEncoder where the FIFO was only used when
|
|
38
|
+
// sample rate conversion was needed with a fixed-frame-size encoder, the
|
|
39
|
+
// MultiStreamEncoder always creates a FIFO. This is because addSamples() can
|
|
40
|
+
// be called multiple times with arbitrary chunk sizes, so we always need to
|
|
41
|
+
// buffer and re-chunk samples into frame_size-sized batches.
|
|
42
|
+
//
|
|
43
|
+
/* clang-format on */
|
|
44
|
+
|
|
45
|
+
class FORCE_PUBLIC_VISIBILITY MultiStreamEncoder {
|
|
46
|
+
public:
|
|
47
|
+
~MultiStreamEncoder();
|
|
48
|
+
|
|
49
|
+
MultiStreamEncoder(const MultiStreamEncoder&) = delete;
|
|
50
|
+
MultiStreamEncoder& operator=(const MultiStreamEncoder&) = delete;
|
|
51
|
+
MultiStreamEncoder(MultiStreamEncoder&&) = delete;
|
|
52
|
+
MultiStreamEncoder& operator=(MultiStreamEncoder&&) = delete;
|
|
53
|
+
|
|
54
|
+
MultiStreamEncoder();
|
|
55
|
+
|
|
56
|
+
int add_video_stream(
|
|
57
|
+
int height,
|
|
58
|
+
int width,
|
|
59
|
+
double frame_rate,
|
|
60
|
+
std::string device = "cpu",
|
|
61
|
+
std::optional<std::string> codec = std::nullopt,
|
|
62
|
+
std::optional<std::string> pixel_format = std::nullopt,
|
|
63
|
+
std::optional<double> crf = std::nullopt,
|
|
64
|
+
std::optional<std::string> preset = std::nullopt,
|
|
65
|
+
std::optional<std::map<std::string, std::string>> extra_options =
|
|
66
|
+
std::nullopt);
|
|
67
|
+
int add_audio_stream(
|
|
68
|
+
int sample_rate,
|
|
69
|
+
int num_channels,
|
|
70
|
+
std::optional<int> bit_rate = std::nullopt,
|
|
71
|
+
std::optional<int> out_num_channels = std::nullopt,
|
|
72
|
+
std::optional<int> out_sample_rate = std::nullopt);
|
|
73
|
+
void open(std::string_view file_name);
|
|
74
|
+
void open(
|
|
75
|
+
std::string_view format_name,
|
|
76
|
+
std::unique_ptr<AVIOContextHolder> avio_context_holder);
|
|
77
|
+
void add_frames(const torch::stable::Tensor& frames, int stream_index);
|
|
78
|
+
void add_samples(const torch::stable::Tensor& samples, int stream_index);
|
|
79
|
+
void close();
|
|
80
|
+
|
|
81
|
+
private:
|
|
82
|
+
struct VideoStream {
|
|
83
|
+
int in_height = 0;
|
|
84
|
+
int in_width = 0;
|
|
85
|
+
double in_frame_rate = 0;
|
|
86
|
+
VideoStreamOptions options;
|
|
87
|
+
UniqueAVCodecContext av_codec_context;
|
|
88
|
+
AVStream* av_stream = nullptr;
|
|
89
|
+
int num_encoded_frames = 0;
|
|
90
|
+
std::unique_ptr<DeviceInterface> device_interface;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
struct AudioStream {
|
|
94
|
+
int in_sample_rate = -1;
|
|
95
|
+
int in_num_channels = -1;
|
|
96
|
+
int out_num_channels = -1;
|
|
97
|
+
int out_sample_rate = -1;
|
|
98
|
+
int frame_size = -1;
|
|
99
|
+
int64_t last_encoded_av_frame_pts = 0;
|
|
100
|
+
AudioStreamOptions options;
|
|
101
|
+
UniqueAVCodecContext av_codec_context;
|
|
102
|
+
AVStream* av_stream = nullptr;
|
|
103
|
+
UniqueSwrContext swr_context;
|
|
104
|
+
UniqueAVAudioFifo av_audio_fifo;
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
void initialize_video_stream(VideoStream& video_stream);
|
|
108
|
+
void open_streams_and_write_header();
|
|
109
|
+
void encode_video_frame(
|
|
110
|
+
AutoAVPacket& auto_av_packet,
|
|
111
|
+
const UniqueAVFrame& av_frame,
|
|
112
|
+
VideoStream& video_stream);
|
|
113
|
+
void initialize_audio_stream(AudioStream& audio_stream);
|
|
114
|
+
void encode_audio_samples(
|
|
115
|
+
const torch::stable::Tensor& samples,
|
|
116
|
+
AudioStream& audio_stream);
|
|
117
|
+
UniqueAVFrame maybe_convert_audio_av_frame(
|
|
118
|
+
const UniqueAVFrame& av_frame,
|
|
119
|
+
AudioStream& audio_stream);
|
|
120
|
+
void encode_audio_frame_through_fifo(
|
|
121
|
+
AutoAVPacket& auto_av_packet,
|
|
122
|
+
const UniqueAVFrame& av_frame,
|
|
123
|
+
AudioStream& audio_stream,
|
|
124
|
+
bool flush_fifo = false);
|
|
125
|
+
void encode_audio_frame(
|
|
126
|
+
AutoAVPacket& auto_av_packet,
|
|
127
|
+
const UniqueAVFrame& av_frame,
|
|
128
|
+
AudioStream& audio_stream);
|
|
129
|
+
void maybe_flush_swr_and_fifo(
|
|
130
|
+
AutoAVPacket& auto_av_packet,
|
|
131
|
+
AudioStream& audio_stream);
|
|
132
|
+
void flush_buffers();
|
|
133
|
+
|
|
134
|
+
UniqueEncodingAVFormatContext av_format_context_;
|
|
135
|
+
std::vector<VideoStream> video_streams_;
|
|
136
|
+
std::vector<AudioStream> audio_streams_;
|
|
137
|
+
bool header_written_ = false;
|
|
138
|
+
UniqueAVDictionary av_format_options_;
|
|
139
|
+
|
|
140
|
+
std::unique_ptr<AVIOContextHolder> avio_context_holder_;
|
|
141
|
+
bool closed_ = false;
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
} // namespace facebook::torchcodec
|
torchcodec/_core/Exif.h
ADDED
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
// @nolint (improperly imported third-party code)
|
|
2
|
+
/*M///////////////////////////////////////////////////////////////////////////////////////
|
|
3
|
+
//
|
|
4
|
+
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
|
5
|
+
//
|
|
6
|
+
// By downloading, copying, installing or using the software you agree to this
|
|
7
|
+
license.
|
|
8
|
+
// If you do not agree to this license, do not download, install,
|
|
9
|
+
// copy or use the software.
|
|
10
|
+
//
|
|
11
|
+
//
|
|
12
|
+
// License Agreement
|
|
13
|
+
// For Open Source Computer Vision Library
|
|
14
|
+
//
|
|
15
|
+
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
|
16
|
+
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
|
|
17
|
+
// Third party copyrights are property of their respective owners.
|
|
18
|
+
//
|
|
19
|
+
// Redistribution and use in source and binary forms, with or without
|
|
20
|
+
modification,
|
|
21
|
+
// are permitted provided that the following conditions are met:
|
|
22
|
+
//
|
|
23
|
+
// * Redistribution's of source code must retain the above copyright notice,
|
|
24
|
+
// this list of conditions and the following disclaimer.
|
|
25
|
+
//
|
|
26
|
+
// * Redistribution's in binary form must reproduce the above copyright
|
|
27
|
+
notice,
|
|
28
|
+
// this list of conditions and the following disclaimer in the documentation
|
|
29
|
+
// and/or other materials provided with the distribution.
|
|
30
|
+
//
|
|
31
|
+
// * The name of the copyright holders may not be used to endorse or promote
|
|
32
|
+
products
|
|
33
|
+
// derived from this software without specific prior written permission.
|
|
34
|
+
//
|
|
35
|
+
// This software is provided by the copyright holders and contributors "as is"
|
|
36
|
+
and
|
|
37
|
+
// any express or implied warranties, including, but not limited to, the implied
|
|
38
|
+
// warranties of merchantability and fitness for a particular purpose are
|
|
39
|
+
disclaimed.
|
|
40
|
+
// In no event shall the Intel Corporation or contributors be liable for any
|
|
41
|
+
direct,
|
|
42
|
+
// indirect, incidental, special, exemplary, or consequential damages
|
|
43
|
+
// (including, but not limited to, procurement of substitute goods or services;
|
|
44
|
+
// loss of use, data, or profits; or business interruption) however caused
|
|
45
|
+
// and on any theory of liability, whether in contract, strict liability,
|
|
46
|
+
// or tort (including negligence or otherwise) arising in any way out of
|
|
47
|
+
// the use of this software, even if advised of the possibility of such damage.
|
|
48
|
+
//
|
|
49
|
+
//M*/
|
|
50
|
+
#pragma once
|
|
51
|
+
// Functions in this module are taken from OpenCV
|
|
52
|
+
// https://github.com/opencv/opencv/blob/097891e311fae1d8354eb092a0fd0171e630d78c/modules/imgcodecs/src/exif.cpp
|
|
53
|
+
//
|
|
54
|
+
// Ported from torchvision's csrc/io/image/cpu/exif.h into torchcodec.
|
|
55
|
+
// Changes made:
|
|
56
|
+
// - make input ptr const.
|
|
57
|
+
// - make the exif values an enum
|
|
58
|
+
|
|
59
|
+
#include <string_view>
|
|
60
|
+
|
|
61
|
+
#include <torch/csrc/stable/ops.h>
|
|
62
|
+
#include <torch/headeronly/util/Exception.h>
|
|
63
|
+
|
|
64
|
+
#include "StableABICompat.h"
|
|
65
|
+
|
|
66
|
+
namespace facebook::torchcodec {
|
|
67
|
+
namespace exif_private {
|
|
68
|
+
|
|
69
|
+
constexpr uint16_t ENDIANNESS_INTEL = 0x49;
|
|
70
|
+
constexpr uint16_t ENDIANNESS_MOTO = 0x4d;
|
|
71
|
+
constexpr uint16_t REQ_EXIF_TAG_MARK = 0x2a;
|
|
72
|
+
constexpr uint16_t ORIENTATION_EXIF_TAG = 0x0112;
|
|
73
|
+
constexpr uint16_t INCORRECT_TAG = -1;
|
|
74
|
+
|
|
75
|
+
// EXIF orientation values as defined in JEITA CP-3451C section 4.6.4.A. Each
|
|
76
|
+
// name describes where the 0th row and 0th column of the stored pixels end up
|
|
77
|
+
// in the correctly-displayed image.
|
|
78
|
+
enum class ExifOrientation : int {
|
|
79
|
+
Unspecified = -1,
|
|
80
|
+
TopLeft = 1, // normal orientation
|
|
81
|
+
TopRight = 2, // needs horizontal flip
|
|
82
|
+
BottomRight = 3, // needs 180 rotation
|
|
83
|
+
BottomLeft = 4, // needs vertical flip
|
|
84
|
+
LeftTop = 5, // mirrored horizontal & rotate 270 CW
|
|
85
|
+
RightTop = 6, // rotate 90 CW
|
|
86
|
+
RightBottom = 7, // mirrored horizontal & rotate 90 CW
|
|
87
|
+
LeftBottom = 8, // needs 270 CW rotation
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
class ExifDataReader {
|
|
91
|
+
public:
|
|
92
|
+
ExifDataReader(const unsigned char* p, size_t s) : _ptr(p), _size(s) {}
|
|
93
|
+
|
|
94
|
+
size_t size() const {
|
|
95
|
+
return _size;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const unsigned char& operator[](size_t index) const {
|
|
99
|
+
STD_TORCH_CHECK(index < _size);
|
|
100
|
+
return _ptr[index];
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
protected:
|
|
104
|
+
const unsigned char* _ptr;
|
|
105
|
+
size_t _size;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
inline uint16_t get_endianness(const ExifDataReader& exif_data) {
|
|
109
|
+
if ((exif_data.size() < 1) ||
|
|
110
|
+
(exif_data.size() > 1 && exif_data[0] != exif_data[1])) {
|
|
111
|
+
return 0;
|
|
112
|
+
}
|
|
113
|
+
if (exif_data[0] == 'I') {
|
|
114
|
+
return ENDIANNESS_INTEL;
|
|
115
|
+
}
|
|
116
|
+
if (exif_data[0] == 'M') {
|
|
117
|
+
return ENDIANNESS_MOTO;
|
|
118
|
+
}
|
|
119
|
+
return 0;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
inline uint16_t get_uint16(
|
|
123
|
+
const ExifDataReader& exif_data,
|
|
124
|
+
uint16_t endianness,
|
|
125
|
+
const size_t offset) {
|
|
126
|
+
if (offset + 1 >= exif_data.size()) {
|
|
127
|
+
return INCORRECT_TAG;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (endianness == ENDIANNESS_INTEL) {
|
|
131
|
+
return exif_data[offset] + (exif_data[offset + 1] << 8);
|
|
132
|
+
}
|
|
133
|
+
return (exif_data[offset] << 8) + exif_data[offset + 1];
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
inline uint32_t get_uint32(
|
|
137
|
+
const ExifDataReader& exif_data,
|
|
138
|
+
uint16_t endianness,
|
|
139
|
+
const size_t offset) {
|
|
140
|
+
if (offset + 3 >= exif_data.size()) {
|
|
141
|
+
return INCORRECT_TAG;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (endianness == ENDIANNESS_INTEL) {
|
|
145
|
+
return exif_data[offset] + (exif_data[offset + 1] << 8) +
|
|
146
|
+
(exif_data[offset + 2] << 16) + (exif_data[offset + 3] << 24);
|
|
147
|
+
}
|
|
148
|
+
return (exif_data[offset] << 24) + (exif_data[offset + 1] << 16) +
|
|
149
|
+
(exif_data[offset + 2] << 8) + exif_data[offset + 3];
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
inline ExifOrientation fetch_exif_orientation(
|
|
153
|
+
const unsigned char* exif_data_ptr,
|
|
154
|
+
size_t size) {
|
|
155
|
+
STD_TORCH_CHECK(exif_data_ptr != nullptr, "exif_data_ptr cannot be null");
|
|
156
|
+
|
|
157
|
+
ExifOrientation exif_orientation = ExifOrientation::Unspecified;
|
|
158
|
+
|
|
159
|
+
// Exif binary structure looks like this
|
|
160
|
+
// First 6 bytes: [E, x, i, f, 0, 0]
|
|
161
|
+
// Endianness, 2 bytes : [M, M] or [I, I]
|
|
162
|
+
// Tag mark, 2 bytes: [0, 0x2a]
|
|
163
|
+
// Offset, 4 bytes
|
|
164
|
+
// Num entries, 2 bytes
|
|
165
|
+
// Tag entries and data, tag has 2 bytes and its data has 10 bytes
|
|
166
|
+
// For more details:
|
|
167
|
+
// http://www.media.mit.edu/pia/Research/deepview/exif.html
|
|
168
|
+
|
|
169
|
+
ExifDataReader exif_data(exif_data_ptr, size);
|
|
170
|
+
auto endianness = get_endianness(exif_data);
|
|
171
|
+
|
|
172
|
+
// Checking whether Tag Mark (0x002A) correspond to one contained in the
|
|
173
|
+
// Jpeg file
|
|
174
|
+
uint16_t tag_mark = get_uint16(exif_data, endianness, 2);
|
|
175
|
+
if (tag_mark == REQ_EXIF_TAG_MARK) {
|
|
176
|
+
auto offset = get_uint32(exif_data, endianness, 4);
|
|
177
|
+
size_t num_entry = get_uint16(exif_data, endianness, offset);
|
|
178
|
+
offset += 2; // go to start of tag fields
|
|
179
|
+
constexpr size_t tiff_field_size = 12;
|
|
180
|
+
for (size_t entry = 0; entry < num_entry; entry++) {
|
|
181
|
+
// Here we just search for orientation tag and parse it
|
|
182
|
+
auto tag_num = get_uint16(exif_data, endianness, offset);
|
|
183
|
+
if (tag_num == INCORRECT_TAG) {
|
|
184
|
+
break;
|
|
185
|
+
}
|
|
186
|
+
if (tag_num == ORIENTATION_EXIF_TAG) {
|
|
187
|
+
exif_orientation = static_cast<ExifOrientation>(
|
|
188
|
+
get_uint16(exif_data, endianness, offset + 8));
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
offset += tiff_field_size;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
return exif_orientation;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// Not from OpenCV. Like Pillow's Exif.load(), skips any number of "Exif\0\0"
|
|
198
|
+
// prefixes before the TIFF header.
|
|
199
|
+
inline ExifOrientation fetch_exif_orientation_after_prefixes(
|
|
200
|
+
const unsigned char* data,
|
|
201
|
+
size_t size) {
|
|
202
|
+
constexpr std::string_view exif_header("Exif\0\0", 6);
|
|
203
|
+
std::string_view exif(reinterpret_cast<const char*>(data), size);
|
|
204
|
+
while (exif.starts_with(exif_header)) {
|
|
205
|
+
exif.remove_prefix(exif_header.size());
|
|
206
|
+
}
|
|
207
|
+
if (exif.empty()) {
|
|
208
|
+
return ExifOrientation::Unspecified;
|
|
209
|
+
}
|
|
210
|
+
return fetch_exif_orientation(
|
|
211
|
+
reinterpret_cast<const unsigned char*>(exif.data()), exif.size());
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
inline torch::stable::Tensor exif_orientation_transform(
|
|
215
|
+
const torch::stable::Tensor& image,
|
|
216
|
+
ExifOrientation orientation) {
|
|
217
|
+
switch (orientation) {
|
|
218
|
+
case ExifOrientation::TopRight:
|
|
219
|
+
return stable_flip(image, {-1});
|
|
220
|
+
case ExifOrientation::BottomRight:
|
|
221
|
+
// 180 rotation, equivalent to flipping both horizontally and vertically.
|
|
222
|
+
return stable_flip(image, {-2, -1});
|
|
223
|
+
case ExifOrientation::BottomLeft:
|
|
224
|
+
return stable_flip(image, {-2});
|
|
225
|
+
case ExifOrientation::LeftTop:
|
|
226
|
+
return torch::stable::transpose(image, -1, -2);
|
|
227
|
+
case ExifOrientation::RightTop:
|
|
228
|
+
return stable_flip(torch::stable::transpose(image, -1, -2), {-1});
|
|
229
|
+
case ExifOrientation::RightBottom:
|
|
230
|
+
return stable_flip(torch::stable::transpose(image, -1, -2), {-2, -1});
|
|
231
|
+
case ExifOrientation::LeftBottom:
|
|
232
|
+
return stable_flip(torch::stable::transpose(image, -1, -2), {-2});
|
|
233
|
+
default:
|
|
234
|
+
// TopLeft is the normal orientation; Unspecified and any unknown value
|
|
235
|
+
// (including the invalid 0) are treated as no transform.
|
|
236
|
+
return image;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
} // namespace exif_private
|
|
241
|
+
} // namespace facebook::torchcodec
|