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,38 @@
|
|
|
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
|
+
namespace facebook::torchcodec {
|
|
10
|
+
|
|
11
|
+
// Keep in sync with _LogLevel in torchcodec/_logging.py.
|
|
12
|
+
enum class LogLevel : int {
|
|
13
|
+
OFF = 0,
|
|
14
|
+
ALL = 1,
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
void set_cpp_log_level(LogLevel level);
|
|
18
|
+
|
|
19
|
+
// getLogLevel doesn't have "Cpp" in the name because it's the source of
|
|
20
|
+
// truth for both Python and C++ log levels.
|
|
21
|
+
LogLevel get_log_level();
|
|
22
|
+
|
|
23
|
+
namespace internal {
|
|
24
|
+
void log(const char* file, int line, const char* fmt, ...)
|
|
25
|
+
#ifndef _WIN32
|
|
26
|
+
__attribute__((format(printf, 3, 4)))
|
|
27
|
+
#endif
|
|
28
|
+
;
|
|
29
|
+
} // namespace internal
|
|
30
|
+
} // namespace facebook::torchcodec
|
|
31
|
+
|
|
32
|
+
#define TC_LOG(...) \
|
|
33
|
+
do { \
|
|
34
|
+
if (::facebook::torchcodec::get_log_level() != \
|
|
35
|
+
::facebook::torchcodec::LogLevel::OFF) { \
|
|
36
|
+
::facebook::torchcodec::internal::log(__FILE__, __LINE__, __VA_ARGS__); \
|
|
37
|
+
} \
|
|
38
|
+
} while (0)
|
|
@@ -0,0 +1,312 @@
|
|
|
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 "Metadata.h"
|
|
8
|
+
|
|
9
|
+
#include <algorithm>
|
|
10
|
+
|
|
11
|
+
#include "FFMPEGCommon.h"
|
|
12
|
+
#include "StableABICompat.h"
|
|
13
|
+
#include "Transform.h"
|
|
14
|
+
|
|
15
|
+
extern "C" {
|
|
16
|
+
#include <libavutil/pixdesc.h>
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
namespace facebook::torchcodec {
|
|
20
|
+
|
|
21
|
+
namespace {
|
|
22
|
+
int get_best_stream_index(AVFormatContext* format_context, AVMediaType type) {
|
|
23
|
+
AVCodecOnlyUseForCallingAVFindBestStream av_codec = nullptr;
|
|
24
|
+
return av_find_best_stream(format_context, type, -1, -1, &av_codec, 0);
|
|
25
|
+
}
|
|
26
|
+
} // namespace
|
|
27
|
+
|
|
28
|
+
// Builds the container and per-stream metadata that the header describes.
|
|
29
|
+
// Shared by SingleStreamDecoder and by the Demuxer building block, so that both
|
|
30
|
+
// report exactly the same thing.
|
|
31
|
+
ContainerMetadata get_container_metadata_from_format_context(
|
|
32
|
+
AVFormatContext* format_context) {
|
|
33
|
+
ContainerMetadata metadata;
|
|
34
|
+
|
|
35
|
+
if (format_context->duration > 0) {
|
|
36
|
+
AVRational default_time_base{1, AV_TIME_BASE};
|
|
37
|
+
metadata.duration_seconds_from_header =
|
|
38
|
+
pts_to_seconds(format_context->duration, default_time_base);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (format_context->bit_rate > 0) {
|
|
42
|
+
metadata.bit_rate = format_context->bit_rate;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
int best_video_stream =
|
|
46
|
+
get_best_stream_index(format_context, AVMEDIA_TYPE_VIDEO);
|
|
47
|
+
if (best_video_stream >= 0) {
|
|
48
|
+
metadata.best_video_stream_index = best_video_stream;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
int best_audio_stream =
|
|
52
|
+
get_best_stream_index(format_context, AVMEDIA_TYPE_AUDIO);
|
|
53
|
+
if (best_audio_stream >= 0) {
|
|
54
|
+
metadata.best_audio_stream_index = best_audio_stream;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
for (unsigned int i = 0; i < format_context->nb_streams; i++) {
|
|
58
|
+
AVStream* av_stream = format_context->streams[i];
|
|
59
|
+
StreamMetadata stream_metadata;
|
|
60
|
+
|
|
61
|
+
STD_TORCH_CHECK(
|
|
62
|
+
static_cast<int>(i) == av_stream->index,
|
|
63
|
+
"Our stream index, " + std::to_string(i) +
|
|
64
|
+
", does not match AVStream's index, " +
|
|
65
|
+
std::to_string(av_stream->index) + ".");
|
|
66
|
+
stream_metadata.stream_index = i;
|
|
67
|
+
stream_metadata.codec_name =
|
|
68
|
+
avcodec_get_name(av_stream->codecpar->codec_id);
|
|
69
|
+
stream_metadata.media_type = av_stream->codecpar->codec_type;
|
|
70
|
+
stream_metadata.bit_rate = av_stream->codecpar->bit_rate;
|
|
71
|
+
if (av_stream->time_base.num > 0 && av_stream->time_base.den > 0) {
|
|
72
|
+
stream_metadata.time_base = av_stream->time_base;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
int64_t frame_count = av_stream->nb_frames;
|
|
76
|
+
if (frame_count > 0) {
|
|
77
|
+
stream_metadata.num_frames_from_header = frame_count;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (av_stream->duration > 0 && av_stream->time_base.den > 0) {
|
|
81
|
+
stream_metadata.duration_seconds_from_header =
|
|
82
|
+
pts_to_seconds(av_stream->duration, av_stream->time_base);
|
|
83
|
+
}
|
|
84
|
+
if (av_stream->start_time != AV_NOPTS_VALUE) {
|
|
85
|
+
stream_metadata.begin_stream_seconds_from_header =
|
|
86
|
+
pts_to_seconds(av_stream->start_time, av_stream->time_base);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (av_stream->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
|
|
90
|
+
double fps = av_q2d(av_stream->r_frame_rate);
|
|
91
|
+
if (fps > 0) {
|
|
92
|
+
stream_metadata.average_fps_from_header = fps;
|
|
93
|
+
}
|
|
94
|
+
stream_metadata.rotation = get_rotation_from_stream(av_stream);
|
|
95
|
+
|
|
96
|
+
// Report post-rotation dimensions: swap width/height for 90 or -90
|
|
97
|
+
// degree rotations so metadata matches what the decoder returns.
|
|
98
|
+
int width = av_stream->codecpar->width;
|
|
99
|
+
int height = av_stream->codecpar->height;
|
|
100
|
+
Rotation rotation = rotation_from_degrees(stream_metadata.rotation);
|
|
101
|
+
// 90° rotations swap dimensions
|
|
102
|
+
if (rotation == Rotation::CCW90 || rotation == Rotation::CW90) {
|
|
103
|
+
std::swap(width, height);
|
|
104
|
+
}
|
|
105
|
+
stream_metadata.post_rotation_width = width;
|
|
106
|
+
stream_metadata.post_rotation_height = height;
|
|
107
|
+
|
|
108
|
+
stream_metadata.sample_aspect_ratio =
|
|
109
|
+
av_stream->codecpar->sample_aspect_ratio;
|
|
110
|
+
|
|
111
|
+
if (av_stream->codecpar->color_primaries != AVCOL_PRI_UNSPECIFIED) {
|
|
112
|
+
stream_metadata.color_primaries = av_stream->codecpar->color_primaries;
|
|
113
|
+
}
|
|
114
|
+
if (av_stream->codecpar->color_space != AVCOL_SPC_UNSPECIFIED) {
|
|
115
|
+
stream_metadata.color_space = av_stream->codecpar->color_space;
|
|
116
|
+
}
|
|
117
|
+
if (av_stream->codecpar->color_trc != AVCOL_TRC_UNSPECIFIED) {
|
|
118
|
+
stream_metadata.color_transfer_characteristic =
|
|
119
|
+
av_stream->codecpar->color_trc;
|
|
120
|
+
}
|
|
121
|
+
AVPixelFormat pixel_format =
|
|
122
|
+
static_cast<AVPixelFormat>(av_stream->codecpar->format);
|
|
123
|
+
// If the AVPixelFormat is not recognized, we get back nullptr. We have
|
|
124
|
+
// to make sure we don't initialize a std::string with nullptr. There's
|
|
125
|
+
// nothing to do on the else branch because we're already using an
|
|
126
|
+
// optional; it'll just remain empty.
|
|
127
|
+
const char* raw_pixel_format = av_get_pix_fmt_name(pixel_format);
|
|
128
|
+
if (raw_pixel_format != nullptr) {
|
|
129
|
+
stream_metadata.pixel_format = std::string(raw_pixel_format);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
metadata.num_video_streams++;
|
|
133
|
+
} else if (av_stream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
|
|
134
|
+
AVSampleFormat format =
|
|
135
|
+
static_cast<AVSampleFormat>(av_stream->codecpar->format);
|
|
136
|
+
stream_metadata.sample_rate =
|
|
137
|
+
static_cast<int64_t>(av_stream->codecpar->sample_rate);
|
|
138
|
+
stream_metadata.num_channels =
|
|
139
|
+
static_cast<int64_t>(get_num_channels(av_stream->codecpar));
|
|
140
|
+
|
|
141
|
+
// If the AVSampleFormat is not recognized, we get back nullptr. We have
|
|
142
|
+
// to make sure we don't initialize a std::string with nullptr. There's
|
|
143
|
+
// nothing to do on the else branch because we're already using an
|
|
144
|
+
// optional; it'll just remain empty.
|
|
145
|
+
const char* raw_sample_format = av_get_sample_fmt_name(format);
|
|
146
|
+
if (raw_sample_format != nullptr) {
|
|
147
|
+
stream_metadata.sample_format = std::string(raw_sample_format);
|
|
148
|
+
}
|
|
149
|
+
metadata.num_audio_streams++;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
stream_metadata.duration_seconds_from_container =
|
|
153
|
+
metadata.duration_seconds_from_header;
|
|
154
|
+
|
|
155
|
+
metadata.all_stream_metadata.push_back(stream_metadata);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return metadata;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
std::optional<double> StreamMetadata::get_duration_seconds(
|
|
162
|
+
SeekMode seek_mode) const {
|
|
163
|
+
switch (seek_mode) {
|
|
164
|
+
case SeekMode::custom_frame_mappings:
|
|
165
|
+
case SeekMode::exact:
|
|
166
|
+
STD_TORCH_CHECK(
|
|
167
|
+
end_stream_pts_seconds_from_content.has_value() &&
|
|
168
|
+
begin_stream_pts_seconds_from_content.has_value(),
|
|
169
|
+
"Missing beginStreamPtsSecondsFromContent or endStreamPtsSecondsFromContent");
|
|
170
|
+
return end_stream_pts_seconds_from_content.value() -
|
|
171
|
+
begin_stream_pts_seconds_from_content.value();
|
|
172
|
+
case SeekMode::approximate:
|
|
173
|
+
if (duration_seconds_from_header.has_value()) {
|
|
174
|
+
return duration_seconds_from_header.value();
|
|
175
|
+
}
|
|
176
|
+
if (num_frames_from_header.has_value() &&
|
|
177
|
+
average_fps_from_header.has_value() &&
|
|
178
|
+
average_fps_from_header.value() != 0.0) {
|
|
179
|
+
return static_cast<double>(num_frames_from_header.value()) /
|
|
180
|
+
average_fps_from_header.value();
|
|
181
|
+
}
|
|
182
|
+
if (duration_seconds_from_container.has_value()) {
|
|
183
|
+
return duration_seconds_from_container.value();
|
|
184
|
+
}
|
|
185
|
+
return std::nullopt;
|
|
186
|
+
default:
|
|
187
|
+
STD_TORCH_CHECK(false, "Unknown SeekMode");
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
double StreamMetadata::get_begin_stream_seconds(SeekMode seek_mode) const {
|
|
192
|
+
switch (seek_mode) {
|
|
193
|
+
case SeekMode::custom_frame_mappings:
|
|
194
|
+
case SeekMode::exact:
|
|
195
|
+
STD_TORCH_CHECK(
|
|
196
|
+
begin_stream_pts_seconds_from_content.has_value(),
|
|
197
|
+
"Missing beginStreamPtsSecondsFromContent");
|
|
198
|
+
return begin_stream_pts_seconds_from_content.value();
|
|
199
|
+
case SeekMode::approximate:
|
|
200
|
+
if (begin_stream_seconds_from_header.has_value()) {
|
|
201
|
+
return begin_stream_seconds_from_header.value();
|
|
202
|
+
}
|
|
203
|
+
return 0.0;
|
|
204
|
+
default:
|
|
205
|
+
STD_TORCH_CHECK(false, "Unknown SeekMode");
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
std::optional<double> StreamMetadata::get_end_stream_seconds(
|
|
210
|
+
SeekMode seek_mode) const {
|
|
211
|
+
switch (seek_mode) {
|
|
212
|
+
case SeekMode::custom_frame_mappings:
|
|
213
|
+
case SeekMode::exact:
|
|
214
|
+
STD_TORCH_CHECK(
|
|
215
|
+
end_stream_pts_seconds_from_content.has_value(),
|
|
216
|
+
"Missing endStreamPtsSecondsFromContent");
|
|
217
|
+
return end_stream_pts_seconds_from_content.value();
|
|
218
|
+
case SeekMode::approximate: {
|
|
219
|
+
auto dur = get_duration_seconds(seek_mode);
|
|
220
|
+
if (dur.has_value()) {
|
|
221
|
+
return get_begin_stream_seconds(seek_mode) + dur.value();
|
|
222
|
+
}
|
|
223
|
+
return std::nullopt;
|
|
224
|
+
}
|
|
225
|
+
default:
|
|
226
|
+
STD_TORCH_CHECK(false, "Unknown SeekMode");
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
std::optional<int64_t> StreamMetadata::get_num_frames(
|
|
231
|
+
SeekMode seek_mode) const {
|
|
232
|
+
switch (seek_mode) {
|
|
233
|
+
case SeekMode::custom_frame_mappings:
|
|
234
|
+
case SeekMode::exact:
|
|
235
|
+
STD_TORCH_CHECK(
|
|
236
|
+
num_frames_from_content.has_value(), "Missing numFramesFromContent");
|
|
237
|
+
return num_frames_from_content.value();
|
|
238
|
+
case SeekMode::approximate: {
|
|
239
|
+
auto duration_seconds = get_duration_seconds(seek_mode);
|
|
240
|
+
if (num_frames_from_header.has_value()) {
|
|
241
|
+
return num_frames_from_header.value();
|
|
242
|
+
}
|
|
243
|
+
if (average_fps_from_header.has_value() && duration_seconds.has_value()) {
|
|
244
|
+
return static_cast<int64_t>(
|
|
245
|
+
average_fps_from_header.value() * duration_seconds.value());
|
|
246
|
+
}
|
|
247
|
+
return std::nullopt;
|
|
248
|
+
}
|
|
249
|
+
default:
|
|
250
|
+
STD_TORCH_CHECK(false, "Unknown SeekMode");
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
std::optional<double> StreamMetadata::get_average_fps(
|
|
255
|
+
SeekMode seek_mode) const {
|
|
256
|
+
switch (seek_mode) {
|
|
257
|
+
case SeekMode::custom_frame_mappings:
|
|
258
|
+
case SeekMode::exact: {
|
|
259
|
+
auto num_frames = get_num_frames(seek_mode);
|
|
260
|
+
if (num_frames.has_value() &&
|
|
261
|
+
begin_stream_pts_seconds_from_content.has_value() &&
|
|
262
|
+
end_stream_pts_seconds_from_content.has_value()) {
|
|
263
|
+
double duration = end_stream_pts_seconds_from_content.value() -
|
|
264
|
+
begin_stream_pts_seconds_from_content.value();
|
|
265
|
+
if (duration != 0.0) {
|
|
266
|
+
return static_cast<double>(num_frames.value()) / duration;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
return average_fps_from_header;
|
|
270
|
+
}
|
|
271
|
+
case SeekMode::approximate:
|
|
272
|
+
return average_fps_from_header;
|
|
273
|
+
default:
|
|
274
|
+
STD_TORCH_CHECK(false, "Unknown SeekMode");
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
std::optional<std::string> StreamMetadata::get_color_primaries_name() const {
|
|
279
|
+
if (!color_primaries.has_value()) {
|
|
280
|
+
return std::nullopt;
|
|
281
|
+
}
|
|
282
|
+
const char* name = av_color_primaries_name(*color_primaries);
|
|
283
|
+
if (name == nullptr) {
|
|
284
|
+
return std::nullopt;
|
|
285
|
+
}
|
|
286
|
+
return std::string(name);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
std::optional<std::string> StreamMetadata::get_color_space_name() const {
|
|
290
|
+
if (!color_space.has_value()) {
|
|
291
|
+
return std::nullopt;
|
|
292
|
+
}
|
|
293
|
+
const char* name = av_color_space_name(*color_space);
|
|
294
|
+
if (name == nullptr) {
|
|
295
|
+
return std::nullopt;
|
|
296
|
+
}
|
|
297
|
+
return std::string(name);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
std::optional<std::string>
|
|
301
|
+
StreamMetadata::get_color_transfer_characteristic_name() const {
|
|
302
|
+
if (!color_transfer_characteristic.has_value()) {
|
|
303
|
+
return std::nullopt;
|
|
304
|
+
}
|
|
305
|
+
const char* name = av_color_transfer_name(*color_transfer_characteristic);
|
|
306
|
+
if (name == nullptr) {
|
|
307
|
+
return std::nullopt;
|
|
308
|
+
}
|
|
309
|
+
return std::string(name);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
} // namespace facebook::torchcodec
|
|
@@ -0,0 +1,115 @@
|
|
|
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 <optional>
|
|
10
|
+
#include <string>
|
|
11
|
+
#include <vector>
|
|
12
|
+
|
|
13
|
+
extern "C" {
|
|
14
|
+
#include <libavcodec/avcodec.h>
|
|
15
|
+
#include <libavformat/avformat.h>
|
|
16
|
+
#include <libavutil/avutil.h>
|
|
17
|
+
#include <libavutil/pixfmt.h>
|
|
18
|
+
#include <libavutil/rational.h>
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
namespace facebook::torchcodec {
|
|
22
|
+
|
|
23
|
+
enum class SeekMode { exact, approximate, custom_frame_mappings };
|
|
24
|
+
|
|
25
|
+
struct StreamMetadata {
|
|
26
|
+
// Common (video and audio) fields derived from the AVStream.
|
|
27
|
+
int stream_index = -1;
|
|
28
|
+
|
|
29
|
+
// See this link for what various values are available:
|
|
30
|
+
// https://ffmpeg.org/doxygen/trunk/group__lavu__misc.html#ga9a84bba4713dfced21a1a56163be1f48
|
|
31
|
+
AVMediaType media_type = AVMEDIA_TYPE_UNKNOWN;
|
|
32
|
+
|
|
33
|
+
std::optional<AVCodecID> codec_id;
|
|
34
|
+
std::optional<std::string> codec_name;
|
|
35
|
+
// The unit of the stream's packet timestamps.
|
|
36
|
+
std::optional<AVRational> time_base;
|
|
37
|
+
std::optional<double> duration_seconds_from_header;
|
|
38
|
+
std::optional<double> begin_stream_seconds_from_header;
|
|
39
|
+
std::optional<int64_t> num_frames_from_header;
|
|
40
|
+
std::optional<int64_t> num_key_frames;
|
|
41
|
+
std::optional<double> average_fps_from_header;
|
|
42
|
+
std::optional<double> bit_rate;
|
|
43
|
+
|
|
44
|
+
// Used as fallback in approximate mode when stream duration is unavailable.
|
|
45
|
+
std::optional<double> duration_seconds_from_container;
|
|
46
|
+
|
|
47
|
+
// More accurate duration, obtained by scanning the file.
|
|
48
|
+
// These presentation timestamps are in time base.
|
|
49
|
+
std::optional<int64_t> begin_stream_pts_from_content;
|
|
50
|
+
std::optional<int64_t> end_stream_pts_from_content;
|
|
51
|
+
|
|
52
|
+
// These presentation timestamps are in seconds.
|
|
53
|
+
std::optional<double> begin_stream_pts_seconds_from_content;
|
|
54
|
+
std::optional<double> end_stream_pts_seconds_from_content;
|
|
55
|
+
|
|
56
|
+
// This can be useful for index-based seeking.
|
|
57
|
+
std::optional<int64_t> num_frames_from_content;
|
|
58
|
+
|
|
59
|
+
// Video-only fields
|
|
60
|
+
// Post-rotation dimensions
|
|
61
|
+
std::optional<int> post_rotation_width;
|
|
62
|
+
std::optional<int> post_rotation_height;
|
|
63
|
+
std::optional<AVRational> sample_aspect_ratio;
|
|
64
|
+
// Rotation angle in degrees from display matrix, in the range [-180, 180].
|
|
65
|
+
std::optional<double> rotation;
|
|
66
|
+
std::optional<AVColorPrimaries> color_primaries;
|
|
67
|
+
std::optional<AVColorSpace> color_space;
|
|
68
|
+
std::optional<AVColorTransferCharacteristic> color_transfer_characteristic;
|
|
69
|
+
// The pixel format of the encoded video, e.g. "yuv420p".
|
|
70
|
+
std::optional<std::string> pixel_format;
|
|
71
|
+
|
|
72
|
+
// Audio-only fields
|
|
73
|
+
std::optional<int64_t> sample_rate;
|
|
74
|
+
std::optional<int64_t> num_channels;
|
|
75
|
+
std::optional<std::string> sample_format;
|
|
76
|
+
|
|
77
|
+
// Computed methods with fallback logic
|
|
78
|
+
std::optional<double> get_duration_seconds(SeekMode seek_mode) const;
|
|
79
|
+
double get_begin_stream_seconds(SeekMode seek_mode) const;
|
|
80
|
+
std::optional<double> get_end_stream_seconds(SeekMode seek_mode) const;
|
|
81
|
+
std::optional<int64_t> get_num_frames(SeekMode seek_mode) const;
|
|
82
|
+
std::optional<double> get_average_fps(SeekMode seek_mode) const;
|
|
83
|
+
|
|
84
|
+
// Color metadata name accessors. These return nullopt if the field is unset
|
|
85
|
+
// or if FFmpeg returns NULL for the name.
|
|
86
|
+
std::optional<std::string> get_color_primaries_name() const;
|
|
87
|
+
std::optional<std::string> get_color_space_name() const;
|
|
88
|
+
std::optional<std::string> get_color_transfer_characteristic_name() const;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
struct ContainerMetadata {
|
|
92
|
+
std::vector<StreamMetadata> all_stream_metadata;
|
|
93
|
+
int num_audio_streams = 0;
|
|
94
|
+
int num_video_streams = 0;
|
|
95
|
+
|
|
96
|
+
// Note that this is the container-level duration, which is usually the max
|
|
97
|
+
// of all stream durations available in the container.
|
|
98
|
+
std::optional<double> duration_seconds_from_header;
|
|
99
|
+
|
|
100
|
+
// Total BitRate level information at the container level in bit/s
|
|
101
|
+
std::optional<double> bit_rate;
|
|
102
|
+
|
|
103
|
+
// If set, this is the index to the default audio stream.
|
|
104
|
+
std::optional<int> best_audio_stream_index;
|
|
105
|
+
|
|
106
|
+
// If set, this is the index to the default video stream.
|
|
107
|
+
std::optional<int> best_video_stream_index;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
// Builds the container and per-stream metadata that the header describes, from
|
|
111
|
+
// an already-opened and probed AVFormatContext.
|
|
112
|
+
ContainerMetadata get_container_metadata_from_format_context(
|
|
113
|
+
AVFormatContext* format_context);
|
|
114
|
+
|
|
115
|
+
} // namespace facebook::torchcodec
|