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,1159 @@
|
|
|
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 "FFMPEGCommon.h"
|
|
8
|
+
|
|
9
|
+
#include <cstdio>
|
|
10
|
+
#include <cstring>
|
|
11
|
+
|
|
12
|
+
#include "StableABICompat.h"
|
|
13
|
+
|
|
14
|
+
extern "C" {
|
|
15
|
+
#include <libavfilter/avfilter.h>
|
|
16
|
+
#include <libavfilter/buffersink.h>
|
|
17
|
+
#include <libavutil/pixdesc.h>
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
namespace facebook::torchcodec {
|
|
21
|
+
|
|
22
|
+
// The AVPixelFormat describing an NVDEC surface for a given source bit depth.
|
|
23
|
+
// bit_depth is the source's, and only matters for the 16-bit containers.
|
|
24
|
+
// FFmpeg < 6 has no P012LE. P016LE describes the same samples just as
|
|
25
|
+
// validly: they're msb-aligned, so a 12-bit surface is a 16-bit one with
|
|
26
|
+
// 4 zeroed low bits.
|
|
27
|
+
AVPixelFormat nvdec_pix_fmt(NvdecSurface surface, int bit_depth) {
|
|
28
|
+
switch (surface) {
|
|
29
|
+
case NvdecSurface::NV12:
|
|
30
|
+
return AV_PIX_FMT_NV12;
|
|
31
|
+
case NvdecSurface::YUV444:
|
|
32
|
+
return AV_PIX_FMT_YUV444P;
|
|
33
|
+
case NvdecSurface::YUV444_16Bit:
|
|
34
|
+
return AV_PIX_FMT_YUV444P16LE;
|
|
35
|
+
case NvdecSurface::P016:
|
|
36
|
+
if (bit_depth == 10) {
|
|
37
|
+
return AV_PIX_FMT_P010LE;
|
|
38
|
+
}
|
|
39
|
+
#if FFMPEG_HAS_P012
|
|
40
|
+
if (bit_depth == 12) {
|
|
41
|
+
return AV_PIX_FMT_P012LE;
|
|
42
|
+
}
|
|
43
|
+
#endif
|
|
44
|
+
return AV_PIX_FMT_P016LE;
|
|
45
|
+
}
|
|
46
|
+
return AV_PIX_FMT_NV12;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
bool is_nvdec_16bit_pix_fmt(int format) {
|
|
50
|
+
return format == AV_PIX_FMT_P010LE || format == AV_PIX_FMT_P016LE ||
|
|
51
|
+
#if FFMPEG_HAS_P012
|
|
52
|
+
format == AV_PIX_FMT_P012LE ||
|
|
53
|
+
#endif
|
|
54
|
+
false;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
OutputDtype resolve_output_dtype(
|
|
58
|
+
OutputDtypeConfig output_dtype_config,
|
|
59
|
+
AVPixelFormat pix_fmt) {
|
|
60
|
+
switch (output_dtype_config) {
|
|
61
|
+
case OutputDtypeConfig::UINT8:
|
|
62
|
+
return OutputDtype::UINT8;
|
|
63
|
+
case OutputDtypeConfig::FLOAT32:
|
|
64
|
+
return OutputDtype::FLOAT32;
|
|
65
|
+
case OutputDtypeConfig::AUTO: {
|
|
66
|
+
// TODO_HDR: This is basically our heuristic that defines how we identify
|
|
67
|
+
// HDR videos, we might want to refine it.
|
|
68
|
+
const AVPixFmtDescriptor* desc = av_pix_fmt_desc_get(pix_fmt);
|
|
69
|
+
return (desc != nullptr && desc->comp[0].depth > 8) ? OutputDtype::FLOAT32
|
|
70
|
+
: OutputDtype::UINT8;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return OutputDtype::UINT8;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
AutoAVPacket::AutoAVPacket() : av_packet_(av_packet_alloc()) {
|
|
77
|
+
STD_TORCH_CHECK(av_packet_ != nullptr, "Couldn't allocate avPacket.");
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
AutoAVPacket::~AutoAVPacket() {
|
|
81
|
+
av_packet_free(&av_packet_);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
ReferenceAVPacket::ReferenceAVPacket(AutoAVPacket& shared)
|
|
85
|
+
: av_packet_(shared.av_packet_) {}
|
|
86
|
+
|
|
87
|
+
ReferenceAVPacket::~ReferenceAVPacket() {
|
|
88
|
+
av_packet_unref(av_packet_);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
AVPacket* ReferenceAVPacket::get() {
|
|
92
|
+
return av_packet_;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
AVPacket& ReferenceAVPacket::operator*() {
|
|
96
|
+
return *av_packet_;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
AVPacket* ReferenceAVPacket::operator->() {
|
|
100
|
+
return av_packet_;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
AVCodecOnlyUseForCallingAVFindBestStream
|
|
104
|
+
make_av_codec_only_use_for_calling_av_find_best_stream(const AVCodec* codec) {
|
|
105
|
+
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(59, 18, 100)
|
|
106
|
+
return const_cast<AVCodec*>(codec);
|
|
107
|
+
#else
|
|
108
|
+
return codec;
|
|
109
|
+
#endif
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
std::string get_ffmpeg_error_string_from_error_code(int error_code) {
|
|
113
|
+
char error_buffer[AV_ERROR_MAX_STRING_SIZE] = {0};
|
|
114
|
+
av_strerror(error_code, error_buffer, AV_ERROR_MAX_STRING_SIZE);
|
|
115
|
+
return std::string(error_buffer);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
int64_t get_duration(const AVFrame& av_frame) {
|
|
119
|
+
#if FFMPEG_HAS_FRAME_DURATION
|
|
120
|
+
return av_frame.duration;
|
|
121
|
+
#else
|
|
122
|
+
return av_frame.pkt_duration;
|
|
123
|
+
#endif
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Some videos aren't properly encoded and do not specify pts values for
|
|
127
|
+
// packets, and thus for frames. Unset values correspond to INT64_MIN. When that
|
|
128
|
+
// happens, we fall back to the dts value which hopefully exists and is correct.
|
|
129
|
+
// Accessing AVFrames' and AVPackets' pts values should **always** go through
|
|
130
|
+
// these helpers.
|
|
131
|
+
int64_t get_pts_or_dts(ReferenceAVPacket& packet) {
|
|
132
|
+
return packet->pts == INT64_MIN ? packet->dts : packet->pts;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
int64_t get_pts_or_dts(const AVFrame& av_frame) {
|
|
136
|
+
return av_frame.pts == INT64_MIN ? av_frame.pkt_dts : av_frame.pts;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
void set_duration(AVFrame& av_frame, int64_t duration) {
|
|
140
|
+
#if FFMPEG_HAS_FRAME_DURATION
|
|
141
|
+
av_frame.duration = duration;
|
|
142
|
+
#else
|
|
143
|
+
av_frame.pkt_duration = duration;
|
|
144
|
+
#endif
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
void AudioPtsExtrapolator::process(AVFrame& av_frame) {
|
|
148
|
+
if (av_frame.sample_rate != sample_rate_) {
|
|
149
|
+
// next_frame_start_ was counted at the old rate. A rate change within a
|
|
150
|
+
// packet is far-fetched enough that we don't bother converting it.
|
|
151
|
+
sample_rate_ = av_frame.sample_rate;
|
|
152
|
+
next_frame_start_ = INT64_MIN;
|
|
153
|
+
}
|
|
154
|
+
if (sample_rate_ <= 0) {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
AVRational samples_time_base = {1, sample_rate_};
|
|
158
|
+
|
|
159
|
+
set_duration(
|
|
160
|
+
av_frame,
|
|
161
|
+
av_rescale_q(av_frame.nb_samples, samples_time_base, time_base_));
|
|
162
|
+
|
|
163
|
+
int64_t pts = get_pts_or_dts(av_frame);
|
|
164
|
+
if (pts != INT64_MIN) {
|
|
165
|
+
// Real pts: nothing to fix, just remember where this frame ends.
|
|
166
|
+
next_frame_start_ =
|
|
167
|
+
av_rescale_q(pts, time_base_, samples_time_base) + av_frame.nb_samples;
|
|
168
|
+
} else if (next_frame_start_ != INT64_MIN) {
|
|
169
|
+
// No pts: this frame starts where the previous one ended.
|
|
170
|
+
// TODO: log when we guess a pts like this, so users can tell their
|
|
171
|
+
// timestamps were extrapolated rather than read from the file.
|
|
172
|
+
av_frame.pts =
|
|
173
|
+
av_rescale_q(next_frame_start_, samples_time_base, time_base_);
|
|
174
|
+
next_frame_start_ += av_frame.nb_samples;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const int* get_supported_sample_rates(const AVCodec& av_codec) {
|
|
179
|
+
const int* supported_sample_rates = nullptr;
|
|
180
|
+
#if FFMPEG_HAS_SUPPORTED_CONFIG
|
|
181
|
+
int num_sample_rates = 0;
|
|
182
|
+
int ret = avcodec_get_supported_config(
|
|
183
|
+
nullptr,
|
|
184
|
+
&av_codec,
|
|
185
|
+
AV_CODEC_CONFIG_SAMPLE_RATE,
|
|
186
|
+
0,
|
|
187
|
+
reinterpret_cast<const void**>(&supported_sample_rates),
|
|
188
|
+
&num_sample_rates);
|
|
189
|
+
if (ret < 0 || supported_sample_rates == nullptr) {
|
|
190
|
+
// Return nullptr to skip validation in validateSampleRate.
|
|
191
|
+
return nullptr;
|
|
192
|
+
}
|
|
193
|
+
#else
|
|
194
|
+
supported_sample_rates = av_codec.supported_samplerates;
|
|
195
|
+
#endif
|
|
196
|
+
return supported_sample_rates;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const AVPixelFormat* get_supported_pixel_formats(const AVCodec& av_codec) {
|
|
200
|
+
const AVPixelFormat* supported_pixel_formats = nullptr;
|
|
201
|
+
#if FFMPEG_HAS_SUPPORTED_CONFIG
|
|
202
|
+
int num_pixel_formats = 0;
|
|
203
|
+
int ret = avcodec_get_supported_config(
|
|
204
|
+
nullptr,
|
|
205
|
+
&av_codec,
|
|
206
|
+
AV_CODEC_CONFIG_PIX_FORMAT,
|
|
207
|
+
0,
|
|
208
|
+
reinterpret_cast<const void**>(&supported_pixel_formats),
|
|
209
|
+
&num_pixel_formats);
|
|
210
|
+
if (ret < 0 || supported_pixel_formats == nullptr) {
|
|
211
|
+
STD_TORCH_CHECK(
|
|
212
|
+
false, "Couldn't get supported pixel formats from encoder.");
|
|
213
|
+
}
|
|
214
|
+
#else
|
|
215
|
+
supported_pixel_formats = av_codec.pix_fmts;
|
|
216
|
+
#endif
|
|
217
|
+
return supported_pixel_formats;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const AVSampleFormat* get_supported_output_sample_formats(
|
|
221
|
+
const AVCodec& av_codec) {
|
|
222
|
+
const AVSampleFormat* supported_sample_formats = nullptr;
|
|
223
|
+
#if FFMPEG_HAS_SUPPORTED_CONFIG
|
|
224
|
+
int num_sample_formats = 0;
|
|
225
|
+
int ret = avcodec_get_supported_config(
|
|
226
|
+
nullptr,
|
|
227
|
+
&av_codec,
|
|
228
|
+
AV_CODEC_CONFIG_SAMPLE_FORMAT,
|
|
229
|
+
0,
|
|
230
|
+
reinterpret_cast<const void**>(&supported_sample_formats),
|
|
231
|
+
&num_sample_formats);
|
|
232
|
+
if (ret < 0 || supported_sample_formats == nullptr) {
|
|
233
|
+
// Return nullptr to use default output format in
|
|
234
|
+
// findBestOutputSampleFormat.
|
|
235
|
+
return nullptr;
|
|
236
|
+
}
|
|
237
|
+
#else
|
|
238
|
+
supported_sample_formats = av_codec.sample_fmts;
|
|
239
|
+
#endif
|
|
240
|
+
return supported_sample_formats;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
#if !FFMPEG_HAS_CH_LAYOUT
|
|
244
|
+
// FFmpeg 4 leaves channel_layout unset (0) on some decoded frames even though
|
|
245
|
+
// .channels is correct. Everything we feed to swresample needs a real layout,
|
|
246
|
+
// so fall back to the default layout for that channel count.
|
|
247
|
+
int64_t get_channel_layout(const AVFrame& av_frame) {
|
|
248
|
+
if (av_frame.channel_layout != 0 || av_frame.channels <= 0) {
|
|
249
|
+
return static_cast<int64_t>(av_frame.channel_layout);
|
|
250
|
+
}
|
|
251
|
+
return av_get_default_channel_layout(av_frame.channels);
|
|
252
|
+
}
|
|
253
|
+
#endif
|
|
254
|
+
|
|
255
|
+
int get_num_channels(const AVFrame& av_frame) {
|
|
256
|
+
#if FFMPEG_HAS_CH_LAYOUT
|
|
257
|
+
return av_frame.ch_layout.nb_channels;
|
|
258
|
+
#else
|
|
259
|
+
int num_channels = av_get_channel_layout_nb_channels(av_frame.channel_layout);
|
|
260
|
+
if (num_channels == 0 && av_frame.channels > 0) {
|
|
261
|
+
num_channels = av_frame.channels;
|
|
262
|
+
}
|
|
263
|
+
return num_channels;
|
|
264
|
+
#endif
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
int get_num_channels(const SharedAVCodecContext& av_codec_context) {
|
|
268
|
+
#if FFMPEG_HAS_CH_LAYOUT
|
|
269
|
+
return av_codec_context->ch_layout.nb_channels;
|
|
270
|
+
#else
|
|
271
|
+
return av_codec_context->channels;
|
|
272
|
+
#endif
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
int get_num_channels(const AVCodecParameters* codecpar) {
|
|
276
|
+
STD_TORCH_CHECK(codecpar != nullptr, "codecpar is null");
|
|
277
|
+
#if FFMPEG_HAS_CH_LAYOUT
|
|
278
|
+
return codecpar->ch_layout.nb_channels;
|
|
279
|
+
#else
|
|
280
|
+
return codecpar->channels;
|
|
281
|
+
#endif
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
void set_default_channel_layout(
|
|
285
|
+
UniqueAVCodecContext& av_codec_context,
|
|
286
|
+
int num_channels) {
|
|
287
|
+
#if FFMPEG_HAS_CH_LAYOUT
|
|
288
|
+
AVChannelLayout channel_layout;
|
|
289
|
+
av_channel_layout_default(&channel_layout, num_channels);
|
|
290
|
+
av_codec_context->ch_layout = channel_layout;
|
|
291
|
+
#else
|
|
292
|
+
uint64_t channel_layout = av_get_default_channel_layout(num_channels);
|
|
293
|
+
av_codec_context->channel_layout = channel_layout;
|
|
294
|
+
av_codec_context->channels = num_channels;
|
|
295
|
+
#endif
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
void set_default_channel_layout(AVFrame& av_frame, int num_channels) {
|
|
299
|
+
#if FFMPEG_HAS_CH_LAYOUT
|
|
300
|
+
AVChannelLayout channel_layout;
|
|
301
|
+
av_channel_layout_default(&channel_layout, num_channels);
|
|
302
|
+
av_frame.ch_layout = channel_layout;
|
|
303
|
+
#else
|
|
304
|
+
uint64_t channel_layout = av_get_default_channel_layout(num_channels);
|
|
305
|
+
av_frame.channel_layout = channel_layout;
|
|
306
|
+
av_frame.channels = num_channels;
|
|
307
|
+
#endif
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
void validate_num_channels(const AVCodec& av_codec, int num_channels) {
|
|
311
|
+
#if FFMPEG_HAS_SUPPORTED_CONFIG
|
|
312
|
+
std::stringstream supported_num_channels;
|
|
313
|
+
const AVChannelLayout* supported_layouts = nullptr;
|
|
314
|
+
int num_layouts = 0;
|
|
315
|
+
int ret = avcodec_get_supported_config(
|
|
316
|
+
nullptr,
|
|
317
|
+
&av_codec,
|
|
318
|
+
AV_CODEC_CONFIG_CHANNEL_LAYOUT,
|
|
319
|
+
0,
|
|
320
|
+
reinterpret_cast<const void**>(&supported_layouts),
|
|
321
|
+
&num_layouts);
|
|
322
|
+
if (ret < 0 || supported_layouts == nullptr) {
|
|
323
|
+
// If we can't validate, we must assume it'll be fine. If not, FFmpeg will
|
|
324
|
+
// eventually raise.
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
for (int i = 0; i < num_layouts; ++i) {
|
|
328
|
+
if (i > 0) {
|
|
329
|
+
supported_num_channels << ", ";
|
|
330
|
+
}
|
|
331
|
+
supported_num_channels << supported_layouts[i].nb_channels;
|
|
332
|
+
if (num_channels == supported_layouts[i].nb_channels) {
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
#elif FFMPEG_HAS_CH_LAYOUT
|
|
337
|
+
if (av_codec.ch_layouts == nullptr) {
|
|
338
|
+
// If we can't validate, we must assume it'll be fine. If not, FFmpeg will
|
|
339
|
+
// eventually raise.
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
// FFmpeg doc indicate that the ch_layouts array is terminated by a zeroed
|
|
343
|
+
// layout, so checking for nb_channels == 0 should indicate its end.
|
|
344
|
+
for (auto i = 0; av_codec.ch_layouts[i].nb_channels != 0; ++i) {
|
|
345
|
+
if (num_channels == av_codec.ch_layouts[i].nb_channels) {
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
// At this point it seems that the encoder doesn't support the requested
|
|
350
|
+
// number of channels, so we error out.
|
|
351
|
+
std::stringstream supported_num_channels;
|
|
352
|
+
for (auto i = 0; av_codec.ch_layouts[i].nb_channels != 0; ++i) {
|
|
353
|
+
if (i > 0) {
|
|
354
|
+
supported_num_channels << ", ";
|
|
355
|
+
}
|
|
356
|
+
supported_num_channels << av_codec.ch_layouts[i].nb_channels;
|
|
357
|
+
}
|
|
358
|
+
#else // FFmpeg <= 4
|
|
359
|
+
if (av_codec.channel_layouts == nullptr) {
|
|
360
|
+
// can't validate, same as above.
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
for (auto i = 0; av_codec.channel_layouts[i] != 0; ++i) {
|
|
364
|
+
if (num_channels ==
|
|
365
|
+
av_get_channel_layout_nb_channels(av_codec.channel_layouts[i])) {
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
// At this point it seems that the encoder doesn't support the requested
|
|
370
|
+
// number of channels, so we error out.
|
|
371
|
+
std::stringstream supported_num_channels;
|
|
372
|
+
for (auto i = 0; av_codec.channel_layouts[i] != 0; ++i) {
|
|
373
|
+
if (i > 0) {
|
|
374
|
+
supported_num_channels << ", ";
|
|
375
|
+
}
|
|
376
|
+
supported_num_channels << av_get_channel_layout_nb_channels(
|
|
377
|
+
av_codec.channel_layouts[i]);
|
|
378
|
+
}
|
|
379
|
+
#endif
|
|
380
|
+
STD_TORCH_CHECK(
|
|
381
|
+
false,
|
|
382
|
+
"Desired number of channels (",
|
|
383
|
+
num_channels,
|
|
384
|
+
") is not supported by the ",
|
|
385
|
+
"encoder. Supported number of channels are: ",
|
|
386
|
+
supported_num_channels.str(),
|
|
387
|
+
".");
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
namespace {
|
|
391
|
+
#if FFMPEG_HAS_CH_LAYOUT
|
|
392
|
+
|
|
393
|
+
// Returns:
|
|
394
|
+
// - the src_av_frame's channel layout if src_av_frame has out_num_channels
|
|
395
|
+
// - the default channel layout with out_num_channels otherwise.
|
|
396
|
+
AVChannelLayout get_output_channel_layout(
|
|
397
|
+
int out_num_channels,
|
|
398
|
+
const AVFrame& src_av_frame) {
|
|
399
|
+
AVChannelLayout out_layout;
|
|
400
|
+
if (out_num_channels == get_num_channels(src_av_frame)) {
|
|
401
|
+
out_layout = src_av_frame.ch_layout;
|
|
402
|
+
} else {
|
|
403
|
+
av_channel_layout_default(&out_layout, out_num_channels);
|
|
404
|
+
}
|
|
405
|
+
return out_layout;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
#else
|
|
409
|
+
|
|
410
|
+
// Same as above
|
|
411
|
+
int64_t get_output_channel_layout(
|
|
412
|
+
int out_num_channels,
|
|
413
|
+
const AVFrame& src_av_frame) {
|
|
414
|
+
int64_t out_layout;
|
|
415
|
+
if (out_num_channels == get_num_channels(src_av_frame)) {
|
|
416
|
+
out_layout = get_channel_layout(src_av_frame);
|
|
417
|
+
} else {
|
|
418
|
+
out_layout = av_get_default_channel_layout(out_num_channels);
|
|
419
|
+
}
|
|
420
|
+
return out_layout;
|
|
421
|
+
}
|
|
422
|
+
#endif
|
|
423
|
+
|
|
424
|
+
// Returns av_frame's data planes, advanced by num_samples_to_skip samples:
|
|
425
|
+
// one plane per channel for planar formats, a single interleaved one
|
|
426
|
+
// otherwise.
|
|
427
|
+
std::vector<const uint8_t*> maybe_skip_samples(
|
|
428
|
+
const AVFrame& av_frame,
|
|
429
|
+
int num_samples_to_skip) {
|
|
430
|
+
auto sample_format = static_cast<AVSampleFormat>(av_frame.format);
|
|
431
|
+
bool is_planar = av_sample_fmt_is_planar(sample_format);
|
|
432
|
+
int num_planes = is_planar ? get_num_channels(av_frame) : 1;
|
|
433
|
+
int byte_offset = num_samples_to_skip *
|
|
434
|
+
av_get_bytes_per_sample(sample_format) *
|
|
435
|
+
(is_planar ? 1 : get_num_channels(av_frame));
|
|
436
|
+
|
|
437
|
+
std::vector<const uint8_t*> planes(num_planes);
|
|
438
|
+
for (int plane = 0; plane < num_planes; ++plane) {
|
|
439
|
+
// We use extended_data instead of data to support more than 8 channels:
|
|
440
|
+
// data only holds AV_NUM_DATA_POINTERS (8) pointers.
|
|
441
|
+
// https://ffmpeg.org/doxygen/trunk/structAVFrame.html#afca04d808393822625e09b5ba91c6756
|
|
442
|
+
planes[plane] = av_frame.extended_data[plane] + byte_offset;
|
|
443
|
+
}
|
|
444
|
+
return planes;
|
|
445
|
+
}
|
|
446
|
+
} // namespace
|
|
447
|
+
|
|
448
|
+
// Sets dst_av_frame' channel layout to get_output_channel_layout(): see doc
|
|
449
|
+
// above
|
|
450
|
+
void set_channel_layout(
|
|
451
|
+
AVFrame& dst_av_frame,
|
|
452
|
+
const AVFrame& src_av_frame,
|
|
453
|
+
int out_num_channels) {
|
|
454
|
+
#if FFMPEG_HAS_CH_LAYOUT
|
|
455
|
+
AVChannelLayout out_layout =
|
|
456
|
+
get_output_channel_layout(out_num_channels, src_av_frame);
|
|
457
|
+
auto status = av_channel_layout_copy(&dst_av_frame.ch_layout, &out_layout);
|
|
458
|
+
STD_TORCH_CHECK(
|
|
459
|
+
status == AVSUCCESS,
|
|
460
|
+
"Couldn't copy channel layout to av_frame: ",
|
|
461
|
+
get_ffmpeg_error_string_from_error_code(status));
|
|
462
|
+
#else
|
|
463
|
+
dst_av_frame.channel_layout =
|
|
464
|
+
get_output_channel_layout(out_num_channels, src_av_frame);
|
|
465
|
+
dst_av_frame.channels = out_num_channels;
|
|
466
|
+
#endif
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
UniqueAVFrame allocate_av_frame(
|
|
470
|
+
int num_samples,
|
|
471
|
+
int sample_rate,
|
|
472
|
+
int num_channels,
|
|
473
|
+
AVSampleFormat sample_format) {
|
|
474
|
+
auto av_frame = UniqueAVFrame(av_frame_alloc());
|
|
475
|
+
STD_TORCH_CHECK(av_frame != nullptr, "Couldn't allocate AVFrame.");
|
|
476
|
+
|
|
477
|
+
av_frame->nb_samples = num_samples;
|
|
478
|
+
av_frame->sample_rate = sample_rate;
|
|
479
|
+
set_default_channel_layout(*av_frame, num_channels);
|
|
480
|
+
av_frame->format = sample_format;
|
|
481
|
+
auto status = av_frame_get_buffer(av_frame.get(), 0);
|
|
482
|
+
|
|
483
|
+
STD_TORCH_CHECK(
|
|
484
|
+
status == AVSUCCESS,
|
|
485
|
+
"Couldn't allocate av_frame's buffers: ",
|
|
486
|
+
get_ffmpeg_error_string_from_error_code(status));
|
|
487
|
+
|
|
488
|
+
status = av_frame_make_writable(av_frame.get());
|
|
489
|
+
STD_TORCH_CHECK(
|
|
490
|
+
status == AVSUCCESS,
|
|
491
|
+
"Couldn't make AVFrame writable: ",
|
|
492
|
+
get_ffmpeg_error_string_from_error_code(status));
|
|
493
|
+
return av_frame;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
namespace {
|
|
497
|
+
SwrContext* create_swr_context_from_layouts(
|
|
498
|
+
AVSampleFormat src_sample_format,
|
|
499
|
+
AVSampleFormat out_sample_format,
|
|
500
|
+
int src_sample_rate,
|
|
501
|
+
int out_sample_rate,
|
|
502
|
+
const SwrChannelLayout& src_layout,
|
|
503
|
+
const SwrChannelLayout& out_layout) {
|
|
504
|
+
SwrContext* swr_context = nullptr;
|
|
505
|
+
int status = AVSUCCESS;
|
|
506
|
+
#if FFMPEG_HAS_CH_LAYOUT
|
|
507
|
+
status = swr_alloc_set_opts2(
|
|
508
|
+
&swr_context,
|
|
509
|
+
// swr_alloc_set_opts2() only became const-correct in FFmpeg 6
|
|
510
|
+
// (libswresample 4.12): before that it asks for non-const layouts that
|
|
511
|
+
// it doesn't modify.
|
|
512
|
+
const_cast<AVChannelLayout*>(&out_layout),
|
|
513
|
+
out_sample_format,
|
|
514
|
+
out_sample_rate,
|
|
515
|
+
const_cast<AVChannelLayout*>(&src_layout),
|
|
516
|
+
src_sample_format,
|
|
517
|
+
src_sample_rate,
|
|
518
|
+
0,
|
|
519
|
+
nullptr);
|
|
520
|
+
|
|
521
|
+
STD_TORCH_CHECK(
|
|
522
|
+
status == AVSUCCESS,
|
|
523
|
+
"Couldn't create SwrContext: ",
|
|
524
|
+
get_ffmpeg_error_string_from_error_code(status));
|
|
525
|
+
#else
|
|
526
|
+
swr_context = swr_alloc_set_opts(
|
|
527
|
+
nullptr,
|
|
528
|
+
out_layout,
|
|
529
|
+
out_sample_format,
|
|
530
|
+
out_sample_rate,
|
|
531
|
+
src_layout,
|
|
532
|
+
src_sample_format,
|
|
533
|
+
src_sample_rate,
|
|
534
|
+
0,
|
|
535
|
+
nullptr);
|
|
536
|
+
#endif
|
|
537
|
+
|
|
538
|
+
STD_TORCH_CHECK(swr_context != nullptr, "Couldn't create swr_context");
|
|
539
|
+
status = swr_init(swr_context);
|
|
540
|
+
STD_TORCH_CHECK(
|
|
541
|
+
status == AVSUCCESS,
|
|
542
|
+
"Couldn't initialize SwrContext: ",
|
|
543
|
+
get_ffmpeg_error_string_from_error_code(status),
|
|
544
|
+
". If the error says 'Invalid argument', it's likely that you are using "
|
|
545
|
+
"a buggy FFmpeg version. FFmpeg4 is known to fail here in some "
|
|
546
|
+
"valid scenarios. Try to upgrade FFmpeg?");
|
|
547
|
+
return swr_context;
|
|
548
|
+
}
|
|
549
|
+
} // namespace
|
|
550
|
+
|
|
551
|
+
SwrContext* create_swr_context(
|
|
552
|
+
AVSampleFormat src_sample_format,
|
|
553
|
+
AVSampleFormat out_sample_format,
|
|
554
|
+
int src_sample_rate,
|
|
555
|
+
int out_sample_rate,
|
|
556
|
+
const AVFrame& src_av_frame,
|
|
557
|
+
int out_num_channels) {
|
|
558
|
+
return create_swr_context_from_layouts(
|
|
559
|
+
src_sample_format,
|
|
560
|
+
out_sample_format,
|
|
561
|
+
src_sample_rate,
|
|
562
|
+
out_sample_rate,
|
|
563
|
+
#if FFMPEG_HAS_CH_LAYOUT
|
|
564
|
+
src_av_frame.ch_layout,
|
|
565
|
+
#else
|
|
566
|
+
get_channel_layout(src_av_frame),
|
|
567
|
+
#endif
|
|
568
|
+
get_output_channel_layout(out_num_channels, src_av_frame));
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
SwrContext* create_swr_context(
|
|
572
|
+
AVSampleFormat src_sample_format,
|
|
573
|
+
AVSampleFormat out_sample_format,
|
|
574
|
+
int src_sample_rate,
|
|
575
|
+
int out_sample_rate,
|
|
576
|
+
int src_num_channels,
|
|
577
|
+
int out_num_channels) {
|
|
578
|
+
#if FFMPEG_HAS_CH_LAYOUT
|
|
579
|
+
AVChannelLayout src_layout;
|
|
580
|
+
AVChannelLayout out_layout;
|
|
581
|
+
av_channel_layout_default(&src_layout, src_num_channels);
|
|
582
|
+
av_channel_layout_default(&out_layout, out_num_channels);
|
|
583
|
+
#else
|
|
584
|
+
int64_t src_layout = av_get_default_channel_layout(src_num_channels);
|
|
585
|
+
int64_t out_layout = av_get_default_channel_layout(out_num_channels);
|
|
586
|
+
#endif
|
|
587
|
+
return create_swr_context_from_layouts(
|
|
588
|
+
src_sample_format,
|
|
589
|
+
out_sample_format,
|
|
590
|
+
src_sample_rate,
|
|
591
|
+
out_sample_rate,
|
|
592
|
+
src_layout,
|
|
593
|
+
out_layout);
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
// TODO Other places use the built-in swr_get_out_samples. We should align.
|
|
597
|
+
int64_t get_swr_output_num_samples_bound(
|
|
598
|
+
const UniqueSwrContext& swr_context,
|
|
599
|
+
int num_src_samples,
|
|
600
|
+
int src_sample_rate,
|
|
601
|
+
int out_sample_rate) {
|
|
602
|
+
if (src_sample_rate == out_sample_rate) {
|
|
603
|
+
return num_src_samples;
|
|
604
|
+
}
|
|
605
|
+
// Note that this is an upper bound on the number of output samples.
|
|
606
|
+
// `swr_convert()` will likely not produce that many when sample rate
|
|
607
|
+
// conversion is needed: it buffers the last few, because those require
|
|
608
|
+
// future samples. That's why callers must narrow to what it actually
|
|
609
|
+
// returned. We could also use `swr_get_out_samples()`, but empirically
|
|
610
|
+
// `av_rescale_rnd()` gives a tighter bound.
|
|
611
|
+
return av_rescale_rnd(
|
|
612
|
+
swr_get_delay(swr_context.get(), src_sample_rate) + num_src_samples,
|
|
613
|
+
out_sample_rate,
|
|
614
|
+
src_sample_rate,
|
|
615
|
+
AV_ROUND_UP);
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
AVFilterContext* create_av_filter_context_with_options(
|
|
619
|
+
AVFilterGraph* filter_graph,
|
|
620
|
+
const AVFilter* buffer,
|
|
621
|
+
const enum AVPixelFormat output_format) {
|
|
622
|
+
AVFilterContext* av_filter_context = nullptr;
|
|
623
|
+
const char* filter_name = "out";
|
|
624
|
+
|
|
625
|
+
enum AVPixelFormat pix_fmts[] = {output_format, AV_PIX_FMT_NONE};
|
|
626
|
+
|
|
627
|
+
// av_opt_set_int_list was replaced by av_opt_set_array() in FFmpeg 8.
|
|
628
|
+
#if LIBAVUTIL_VERSION_MAJOR >= 60 // FFmpeg >= 8
|
|
629
|
+
// Output options like pixel_formats must be set before filter init
|
|
630
|
+
av_filter_context =
|
|
631
|
+
avfilter_graph_alloc_filter(filter_graph, buffer, filter_name);
|
|
632
|
+
STD_TORCH_CHECK(
|
|
633
|
+
av_filter_context != nullptr,
|
|
634
|
+
"Failed to allocate buffer filter context.");
|
|
635
|
+
|
|
636
|
+
// When setting pix_fmts, only the first element is used, so nb_elems = 1
|
|
637
|
+
// AV_PIX_FMT_NONE acts as a terminator for the array in av_opt_set_int_list
|
|
638
|
+
int status = av_opt_set_array(
|
|
639
|
+
av_filter_context,
|
|
640
|
+
"pixel_formats",
|
|
641
|
+
AV_OPT_SEARCH_CHILDREN,
|
|
642
|
+
0, // start_elem
|
|
643
|
+
1, // nb_elems
|
|
644
|
+
AV_OPT_TYPE_PIXEL_FMT,
|
|
645
|
+
pix_fmts);
|
|
646
|
+
STD_TORCH_CHECK(
|
|
647
|
+
status >= 0,
|
|
648
|
+
"Failed to set pixel format for buffer filter: ",
|
|
649
|
+
get_ffmpeg_error_string_from_error_code(status));
|
|
650
|
+
|
|
651
|
+
status = avfilter_init_str(av_filter_context, nullptr);
|
|
652
|
+
STD_TORCH_CHECK(
|
|
653
|
+
status >= 0,
|
|
654
|
+
"Failed to initialize buffer filter: ",
|
|
655
|
+
get_ffmpeg_error_string_from_error_code(status));
|
|
656
|
+
#else // FFmpeg <= 7
|
|
657
|
+
// For older FFmpeg versions, create filter and then set options
|
|
658
|
+
int status = avfilter_graph_create_filter(
|
|
659
|
+
&av_filter_context, buffer, filter_name, nullptr, nullptr, filter_graph);
|
|
660
|
+
STD_TORCH_CHECK(
|
|
661
|
+
status >= 0,
|
|
662
|
+
"Failed to create buffer filter: ",
|
|
663
|
+
get_ffmpeg_error_string_from_error_code(status));
|
|
664
|
+
|
|
665
|
+
status = av_opt_set_int_list(
|
|
666
|
+
av_filter_context,
|
|
667
|
+
"pix_fmts",
|
|
668
|
+
pix_fmts,
|
|
669
|
+
AV_PIX_FMT_NONE,
|
|
670
|
+
AV_OPT_SEARCH_CHILDREN);
|
|
671
|
+
STD_TORCH_CHECK(
|
|
672
|
+
status >= 0,
|
|
673
|
+
"Failed to set pixel formats for buffer filter: ",
|
|
674
|
+
get_ffmpeg_error_string_from_error_code(status));
|
|
675
|
+
#endif
|
|
676
|
+
|
|
677
|
+
return av_filter_context;
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
UniqueAVFrame convert_audio_av_frame_samples(
|
|
681
|
+
const UniqueSwrContext& swr_context,
|
|
682
|
+
const AVFrame& src_av_frame,
|
|
683
|
+
AVSampleFormat out_sample_format,
|
|
684
|
+
int out_sample_rate,
|
|
685
|
+
int out_num_channels,
|
|
686
|
+
int num_samples_to_skip) {
|
|
687
|
+
UniqueAVFrame converted_av_frame(av_frame_alloc());
|
|
688
|
+
STD_TORCH_CHECK(
|
|
689
|
+
converted_av_frame,
|
|
690
|
+
"Could not allocate frame for sample format conversion.");
|
|
691
|
+
|
|
692
|
+
converted_av_frame->pts = src_av_frame.pts;
|
|
693
|
+
converted_av_frame->format = static_cast<int>(out_sample_format);
|
|
694
|
+
|
|
695
|
+
int num_src_samples = src_av_frame.nb_samples - num_samples_to_skip;
|
|
696
|
+
STD_TORCH_CHECK(
|
|
697
|
+
num_src_samples >= 0,
|
|
698
|
+
"Trying to skip more samples than the frame contains.");
|
|
699
|
+
std::vector<const uint8_t*> src_planes =
|
|
700
|
+
maybe_skip_samples(src_av_frame, num_samples_to_skip);
|
|
701
|
+
|
|
702
|
+
converted_av_frame->sample_rate = out_sample_rate;
|
|
703
|
+
// A bound, not the real count, which is why we reset nb_samples after the
|
|
704
|
+
// call to swr_convert() below.
|
|
705
|
+
converted_av_frame
|
|
706
|
+
->nb_samples = static_cast<int>(get_swr_output_num_samples_bound(
|
|
707
|
+
swr_context, num_src_samples, src_av_frame.sample_rate, out_sample_rate));
|
|
708
|
+
|
|
709
|
+
set_channel_layout(*converted_av_frame, src_av_frame, out_num_channels);
|
|
710
|
+
|
|
711
|
+
if (converted_av_frame->nb_samples == 0) {
|
|
712
|
+
return converted_av_frame;
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
auto status = av_frame_get_buffer(converted_av_frame.get(), 0);
|
|
716
|
+
STD_TORCH_CHECK(
|
|
717
|
+
status == AVSUCCESS,
|
|
718
|
+
"Could not allocate frame buffers for sample format conversion: ",
|
|
719
|
+
get_ffmpeg_error_string_from_error_code(status));
|
|
720
|
+
|
|
721
|
+
auto num_converted_samples = swr_convert(
|
|
722
|
+
swr_context.get(),
|
|
723
|
+
converted_av_frame->extended_data,
|
|
724
|
+
converted_av_frame->nb_samples,
|
|
725
|
+
src_planes.data(),
|
|
726
|
+
num_src_samples);
|
|
727
|
+
// numConvertedSamples can be 0 if we're downsampling by a great factor and
|
|
728
|
+
// the first frame doesn't contain a lot of samples. It should be handled
|
|
729
|
+
// properly by the caller.
|
|
730
|
+
STD_TORCH_CHECK(
|
|
731
|
+
num_converted_samples >= 0,
|
|
732
|
+
"Error in swr_convert: ",
|
|
733
|
+
get_ffmpeg_error_string_from_error_code(num_converted_samples));
|
|
734
|
+
|
|
735
|
+
// See comment above about nb_samples
|
|
736
|
+
converted_av_frame->nb_samples = num_converted_samples;
|
|
737
|
+
|
|
738
|
+
return converted_av_frame;
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
void set_ffmpeg_log_level() {
|
|
742
|
+
auto log_level = AV_LOG_QUIET;
|
|
743
|
+
const char* log_level_env_ptr = std::getenv("TORCHCODEC_FFMPEG_LOG_LEVEL");
|
|
744
|
+
if (log_level_env_ptr != nullptr) {
|
|
745
|
+
std::string log_level_env(log_level_env_ptr);
|
|
746
|
+
if (log_level_env == "QUIET") {
|
|
747
|
+
log_level = AV_LOG_QUIET;
|
|
748
|
+
} else if (log_level_env == "PANIC") {
|
|
749
|
+
log_level = AV_LOG_PANIC;
|
|
750
|
+
} else if (log_level_env == "FATAL") {
|
|
751
|
+
log_level = AV_LOG_FATAL;
|
|
752
|
+
} else if (log_level_env == "ERROR") {
|
|
753
|
+
log_level = AV_LOG_ERROR;
|
|
754
|
+
} else if (log_level_env == "WARNING") {
|
|
755
|
+
log_level = AV_LOG_WARNING;
|
|
756
|
+
} else if (log_level_env == "INFO") {
|
|
757
|
+
log_level = AV_LOG_INFO;
|
|
758
|
+
} else if (log_level_env == "VERBOSE") {
|
|
759
|
+
log_level = AV_LOG_VERBOSE;
|
|
760
|
+
} else if (log_level_env == "DEBUG") {
|
|
761
|
+
log_level = AV_LOG_DEBUG;
|
|
762
|
+
} else if (log_level_env == "TRACE") {
|
|
763
|
+
log_level = AV_LOG_TRACE;
|
|
764
|
+
} else {
|
|
765
|
+
STD_TORCH_CHECK(
|
|
766
|
+
false,
|
|
767
|
+
"Invalid TORCHCODEC_FFMPEG_LOG_LEVEL: ",
|
|
768
|
+
log_level_env,
|
|
769
|
+
". Use e.g. 'QUIET', 'PANIC', 'VERBOSE', etc.");
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
av_log_set_level(log_level);
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
void forbid_nested_protocols(AVFormatContext* format_context) {
|
|
776
|
+
// See _assert_local_file_and_file_like_agree
|
|
777
|
+
// We call this explicitly so that the file-like behavior matches the default
|
|
778
|
+
// behavior of FFmpeg on local files.
|
|
779
|
+
int status = av_opt_set(format_context, "protocol_whitelist", "", 0);
|
|
780
|
+
STD_TORCH_CHECK(
|
|
781
|
+
status == 0,
|
|
782
|
+
"Failed to set protocol whitelist: ",
|
|
783
|
+
get_ffmpeg_error_string_from_error_code(status));
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
AVIOContext* avio_alloc_context(
|
|
787
|
+
uint8_t* buffer,
|
|
788
|
+
int buffer_size,
|
|
789
|
+
int write_flag,
|
|
790
|
+
void* opaque,
|
|
791
|
+
AVIOReadFunction read_packet,
|
|
792
|
+
AVIOWriteFunction write_packet,
|
|
793
|
+
AVIOSeekFunction seek) {
|
|
794
|
+
// Qualified with :: to call FFmpeg's avio_alloc_context, not this same-named
|
|
795
|
+
// wrapper in the facebook::torchcodec namespace (which would recurse).
|
|
796
|
+
return ::avio_alloc_context(
|
|
797
|
+
buffer,
|
|
798
|
+
buffer_size,
|
|
799
|
+
write_flag,
|
|
800
|
+
opaque,
|
|
801
|
+
read_packet,
|
|
802
|
+
// The buf parameter of the write function is not const before FFmpeg 7.
|
|
803
|
+
#if LIBAVFILTER_VERSION_MAJOR >= 10 // FFmpeg >= 7
|
|
804
|
+
write_packet,
|
|
805
|
+
#else
|
|
806
|
+
reinterpret_cast<AVIOWriteFunctionOld>(write_packet),
|
|
807
|
+
#endif
|
|
808
|
+
seek);
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
double pts_to_seconds(int64_t pts, const AVRational& time_base) {
|
|
812
|
+
// To perform the multiplication before the division, av_q2d is not used
|
|
813
|
+
return static_cast<double>(pts) * time_base.num / time_base.den;
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
int64_t seconds_to_closest_pts(double seconds, const AVRational& time_base) {
|
|
817
|
+
return static_cast<int64_t>(
|
|
818
|
+
std::round(seconds * time_base.den / time_base.num));
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
int64_t compute_safe_duration(
|
|
822
|
+
const AVRational& frame_rate,
|
|
823
|
+
const AVRational& time_base) {
|
|
824
|
+
if (frame_rate.num <= 0 || frame_rate.den <= 0 || time_base.num <= 0 ||
|
|
825
|
+
time_base.den <= 0) {
|
|
826
|
+
return 0;
|
|
827
|
+
} else {
|
|
828
|
+
return (static_cast<int64_t>(frame_rate.den) * time_base.den) /
|
|
829
|
+
(static_cast<int64_t>(time_base.num) * frame_rate.num);
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
const int32_t* get_display_matrix_from_stream(const AVStream* av_stream) {
|
|
834
|
+
// av_stream_get_side_data() was deprecated in FFmpeg 6.0, but its replacement
|
|
835
|
+
// (av_packet_side_data_get() + codecpar->coded_side_data) is only available
|
|
836
|
+
// from FFmpeg 6.1. We need some #pragma magic to silence the deprecation
|
|
837
|
+
// warning which our compile chain would otherwise treat as an error.
|
|
838
|
+
if (av_stream == nullptr) {
|
|
839
|
+
return nullptr;
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
const int32_t* display_matrix = nullptr;
|
|
843
|
+
|
|
844
|
+
// FFmpeg >= 6.1: Use codecpar->coded_side_data
|
|
845
|
+
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(60, 31, 100)
|
|
846
|
+
const AVPacketSideData* side_data = av_packet_side_data_get(
|
|
847
|
+
av_stream->codecpar->coded_side_data,
|
|
848
|
+
av_stream->codecpar->nb_coded_side_data,
|
|
849
|
+
AV_PKT_DATA_DISPLAYMATRIX);
|
|
850
|
+
if (side_data != nullptr) {
|
|
851
|
+
display_matrix = reinterpret_cast<const int32_t*>(side_data->data);
|
|
852
|
+
}
|
|
853
|
+
#elif LIBAVFORMAT_VERSION_MAJOR >= 60 // FFmpeg 6.0
|
|
854
|
+
// FFmpeg 6.0: Use av_stream_get_side_data (deprecated but still available)
|
|
855
|
+
// Suppress deprecation warning for this specific call
|
|
856
|
+
#pragma GCC diagnostic push
|
|
857
|
+
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
|
858
|
+
size_t side_data_size = 0;
|
|
859
|
+
const uint8_t* side_data = av_stream_get_side_data(
|
|
860
|
+
av_stream, AV_PKT_DATA_DISPLAYMATRIX, &side_data_size);
|
|
861
|
+
#pragma GCC diagnostic pop
|
|
862
|
+
if (side_data != nullptr) {
|
|
863
|
+
display_matrix = reinterpret_cast<const int32_t*>(side_data);
|
|
864
|
+
}
|
|
865
|
+
#else
|
|
866
|
+
// FFmpeg < 6: Use av_stream_get_side_data.
|
|
867
|
+
// The size parameter type changed from int* (FFmpeg 4) to size_t* (FFmpeg 5)
|
|
868
|
+
#if LIBAVFORMAT_VERSION_MAJOR >= 59 // FFmpeg 5
|
|
869
|
+
size_t side_data_size = 0;
|
|
870
|
+
#else // FFmpeg 4
|
|
871
|
+
int side_data_size = 0;
|
|
872
|
+
#endif
|
|
873
|
+
const uint8_t* side_data = av_stream_get_side_data(
|
|
874
|
+
av_stream, AV_PKT_DATA_DISPLAYMATRIX, &side_data_size);
|
|
875
|
+
if (side_data != nullptr) {
|
|
876
|
+
display_matrix = reinterpret_cast<const int32_t*>(side_data);
|
|
877
|
+
}
|
|
878
|
+
#endif
|
|
879
|
+
|
|
880
|
+
return display_matrix;
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
std::optional<double> get_rotation_from_display_matrix(
|
|
884
|
+
const int32_t* display_matrix) {
|
|
885
|
+
if (display_matrix == nullptr) {
|
|
886
|
+
return std::nullopt;
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
// av_display_rotation_get returns the rotation angle in degrees needed to
|
|
890
|
+
// rotate the video counter-clockwise to make it upright.
|
|
891
|
+
// Returns NaN if the matrix is invalid.
|
|
892
|
+
double rotation = av_display_rotation_get(display_matrix);
|
|
893
|
+
|
|
894
|
+
// Check for invalid matrix
|
|
895
|
+
if (std::isnan(rotation)) {
|
|
896
|
+
return std::nullopt;
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
return rotation;
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
std::optional<double> get_rotation_from_stream(const AVStream* av_stream) {
|
|
903
|
+
return get_rotation_from_display_matrix(
|
|
904
|
+
get_display_matrix_from_stream(av_stream));
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
std::optional<double> get_rotation_from_frame(const AVFrame& av_frame) {
|
|
908
|
+
const AVFrameSideData* side_data =
|
|
909
|
+
av_frame_get_side_data(&av_frame, AV_FRAME_DATA_DISPLAYMATRIX);
|
|
910
|
+
if (side_data == nullptr) {
|
|
911
|
+
return std::nullopt;
|
|
912
|
+
}
|
|
913
|
+
return get_rotation_from_display_matrix(
|
|
914
|
+
reinterpret_cast<const int32_t*>(side_data->data));
|
|
915
|
+
}
|
|
916
|
+
|
|
917
|
+
AVRational get_framerate([[maybe_unused]] const AVCodecParameters& codecpar) {
|
|
918
|
+
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(60, 11, 100)
|
|
919
|
+
return codecpar.framerate;
|
|
920
|
+
#else
|
|
921
|
+
return {0, 1};
|
|
922
|
+
#endif
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
std::optional<std::string> get_channel_layout_name(
|
|
926
|
+
const AVCodecParameters& codecpar) {
|
|
927
|
+
#if FFMPEG_HAS_CH_LAYOUT
|
|
928
|
+
if (codecpar.ch_layout.nb_channels == 0) {
|
|
929
|
+
return std::nullopt;
|
|
930
|
+
}
|
|
931
|
+
std::vector<char> buffer(64);
|
|
932
|
+
// Returns the size the description needs, which may be more than we gave.
|
|
933
|
+
int size = av_channel_layout_describe(
|
|
934
|
+
&codecpar.ch_layout, buffer.data(), buffer.size());
|
|
935
|
+
if (size <= 0) {
|
|
936
|
+
return std::nullopt;
|
|
937
|
+
}
|
|
938
|
+
if (static_cast<size_t>(size) > buffer.size()) {
|
|
939
|
+
buffer.resize(size);
|
|
940
|
+
av_channel_layout_describe(
|
|
941
|
+
&codecpar.ch_layout, buffer.data(), buffer.size());
|
|
942
|
+
}
|
|
943
|
+
return std::string(buffer.data());
|
|
944
|
+
#else
|
|
945
|
+
if (codecpar.channels == 0) {
|
|
946
|
+
return std::nullopt;
|
|
947
|
+
}
|
|
948
|
+
char buffer[256];
|
|
949
|
+
av_get_channel_layout_string(
|
|
950
|
+
buffer, sizeof(buffer), codecpar.channels, codecpar.channel_layout);
|
|
951
|
+
return std::string(buffer);
|
|
952
|
+
#endif
|
|
953
|
+
}
|
|
954
|
+
|
|
955
|
+
const char* get_field_order_name(AVFieldOrder field_order) {
|
|
956
|
+
switch (field_order) {
|
|
957
|
+
case AV_FIELD_PROGRESSIVE:
|
|
958
|
+
return "progressive";
|
|
959
|
+
case AV_FIELD_TT:
|
|
960
|
+
return "tt";
|
|
961
|
+
case AV_FIELD_BB:
|
|
962
|
+
return "bb";
|
|
963
|
+
case AV_FIELD_TB:
|
|
964
|
+
return "tb";
|
|
965
|
+
case AV_FIELD_BT:
|
|
966
|
+
return "bt";
|
|
967
|
+
default:
|
|
968
|
+
return nullptr;
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
void set_framerate(
|
|
973
|
+
[[maybe_unused]] AVCodecParameters& codecpar,
|
|
974
|
+
[[maybe_unused]] const AVRational& framerate) {
|
|
975
|
+
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(60, 11, 100)
|
|
976
|
+
codecpar.framerate = framerate;
|
|
977
|
+
#else
|
|
978
|
+
STD_TORCH_CHECK(false, "framerate requires FFmpeg 6.1 or above.");
|
|
979
|
+
#endif
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
void set_channel_layout(AVCodecParameters& codecpar, const std::string& name) {
|
|
983
|
+
#if FFMPEG_HAS_CH_LAYOUT
|
|
984
|
+
STD_TORCH_CHECK(
|
|
985
|
+
av_channel_layout_from_string(&codecpar.ch_layout, name.c_str()) == 0,
|
|
986
|
+
"Unknown channel_layout: ",
|
|
987
|
+
name);
|
|
988
|
+
#else
|
|
989
|
+
uint64_t layout = av_get_channel_layout(name.c_str());
|
|
990
|
+
if (layout != 0) {
|
|
991
|
+
codecpar.channel_layout = layout;
|
|
992
|
+
codecpar.channels = av_get_channel_layout_nb_channels(layout);
|
|
993
|
+
return;
|
|
994
|
+
}
|
|
995
|
+
// What av_get_channel_layout_string() describes a layout-less channel count
|
|
996
|
+
// as, and av_get_channel_layout() doesn't parse back.
|
|
997
|
+
int num_channels = 0;
|
|
998
|
+
char trailing = '\0';
|
|
999
|
+
STD_TORCH_CHECK(
|
|
1000
|
+
std::sscanf(name.c_str(), "%d channels%c", &num_channels, &trailing) ==
|
|
1001
|
+
1 &&
|
|
1002
|
+
num_channels > 0,
|
|
1003
|
+
"Unknown channel_layout: ",
|
|
1004
|
+
name);
|
|
1005
|
+
codecpar.channel_layout = 0;
|
|
1006
|
+
codecpar.channels = num_channels;
|
|
1007
|
+
#endif
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
AVFieldOrder get_field_order_from_name(const std::string& name) {
|
|
1011
|
+
for (AVFieldOrder field_order :
|
|
1012
|
+
{AV_FIELD_PROGRESSIVE,
|
|
1013
|
+
AV_FIELD_TT,
|
|
1014
|
+
AV_FIELD_BB,
|
|
1015
|
+
AV_FIELD_TB,
|
|
1016
|
+
AV_FIELD_BT}) {
|
|
1017
|
+
if (name == get_field_order_name(field_order)) {
|
|
1018
|
+
return field_order;
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
1021
|
+
STD_TORCH_CHECK(false, "Unknown field_order: ", name);
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
AVPacketSideDataType get_packet_side_data_type_from_name(
|
|
1025
|
+
const std::string& name) {
|
|
1026
|
+
for (int type = 0; type < AV_PKT_DATA_NB; ++type) {
|
|
1027
|
+
const char* candidate =
|
|
1028
|
+
av_packet_side_data_name(static_cast<AVPacketSideDataType>(type));
|
|
1029
|
+
if (candidate != nullptr && name == candidate) {
|
|
1030
|
+
return static_cast<AVPacketSideDataType>(type);
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
STD_TORCH_CHECK(false, "Unknown side data type: ", name);
|
|
1034
|
+
}
|
|
1035
|
+
|
|
1036
|
+
void set_display_matrix_on_frame(
|
|
1037
|
+
AVFrame& av_frame,
|
|
1038
|
+
const int32_t* display_matrix) {
|
|
1039
|
+
// The frame may already carry a display matrix of its own: FFmpeg propagates
|
|
1040
|
+
// the container's from 6.1 on, and the H.264/HEVC decoders derive one from a
|
|
1041
|
+
// display-orientation SEI. Since av_frame_new_side_data() appends rather than
|
|
1042
|
+
// replaces, and av_frame_get_side_data() returns the first match, not
|
|
1043
|
+
// clearing first would leave whatever the decoder attached in charge and make
|
|
1044
|
+
// ours dead weight - so which matrix wins would depend on the FFmpeg version
|
|
1045
|
+
// and the codec. Clearing keeps it always the container's, which is the one
|
|
1046
|
+
// the SingleStreamDecoder applies.
|
|
1047
|
+
//
|
|
1048
|
+
// Note that no test covers this: for our assets the decoder's matrix, when
|
|
1049
|
+
// there is one, is the container's, so dropping this line changes nothing
|
|
1050
|
+
// observable. It would take a stream whose SEI disagrees with its container.
|
|
1051
|
+
av_frame_remove_side_data(&av_frame, AV_FRAME_DATA_DISPLAYMATRIX);
|
|
1052
|
+
if (display_matrix == nullptr) {
|
|
1053
|
+
return;
|
|
1054
|
+
}
|
|
1055
|
+
|
|
1056
|
+
constexpr size_t kDisplayMatrixSize = 9 * sizeof(int32_t);
|
|
1057
|
+
AVFrameSideData* side_data = av_frame_new_side_data(
|
|
1058
|
+
&av_frame, AV_FRAME_DATA_DISPLAYMATRIX, kDisplayMatrixSize);
|
|
1059
|
+
STD_TORCH_CHECK(
|
|
1060
|
+
side_data != nullptr, "Failed to allocate display matrix side data");
|
|
1061
|
+
std::memcpy(side_data->data, display_matrix, kDisplayMatrixSize);
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
bool SwsConfig::operator==(const SwsConfig& other) const {
|
|
1065
|
+
return input_width == other.input_width &&
|
|
1066
|
+
input_height == other.input_height &&
|
|
1067
|
+
input_format == other.input_format &&
|
|
1068
|
+
input_colorspace == other.input_colorspace &&
|
|
1069
|
+
input_color_range == other.input_color_range &&
|
|
1070
|
+
output_width == other.output_width &&
|
|
1071
|
+
output_height == other.output_height &&
|
|
1072
|
+
output_format == other.output_format &&
|
|
1073
|
+
output_color_range == other.output_color_range;
|
|
1074
|
+
}
|
|
1075
|
+
|
|
1076
|
+
bool SwsConfig::operator!=(const SwsConfig& other) const {
|
|
1077
|
+
return !(*this == other);
|
|
1078
|
+
}
|
|
1079
|
+
|
|
1080
|
+
namespace {
|
|
1081
|
+
#if FFMPEG_SWS_COLORSPACE_DETAILS_FAILS_ON_YUV_TO_YUV
|
|
1082
|
+
// Matches swscale's own isYUV() || isGray(): everything that isn't RGB,
|
|
1083
|
+
// palettised or a hardware format.
|
|
1084
|
+
bool is_yuv_or_gray(AVPixelFormat format) {
|
|
1085
|
+
const AVPixFmtDescriptor* desc = av_pix_fmt_desc_get(format);
|
|
1086
|
+
return desc != nullptr &&
|
|
1087
|
+
!(desc->flags &
|
|
1088
|
+
(AV_PIX_FMT_FLAG_RGB | AV_PIX_FMT_FLAG_PAL | AV_PIX_FMT_FLAG_HWACCEL));
|
|
1089
|
+
}
|
|
1090
|
+
|
|
1091
|
+
void validate_sws_set_colorspace_details(int ret, const SwsConfig& sws_config) {
|
|
1092
|
+
bool yuv_or_gray_to_yuv_or_gray = is_yuv_or_gray(sws_config.input_format) &&
|
|
1093
|
+
is_yuv_or_gray(sws_config.output_format);
|
|
1094
|
+
STD_TORCH_CHECK(
|
|
1095
|
+
ret != -1 || yuv_or_gray_to_yuv_or_gray,
|
|
1096
|
+
"sws_setColorspaceDetails returned -1");
|
|
1097
|
+
}
|
|
1098
|
+
#else
|
|
1099
|
+
void validate_sws_set_colorspace_details(int ret, const SwsConfig&) {
|
|
1100
|
+
STD_TORCH_CHECK(ret != -1, "sws_setColorspaceDetails returned -1");
|
|
1101
|
+
}
|
|
1102
|
+
#endif
|
|
1103
|
+
} // namespace
|
|
1104
|
+
|
|
1105
|
+
UniqueSwsContext create_sws_context(
|
|
1106
|
+
const SwsConfig& sws_config,
|
|
1107
|
+
int sws_flags) {
|
|
1108
|
+
SwsContext* sws_context = sws_getContext(
|
|
1109
|
+
sws_config.input_width,
|
|
1110
|
+
sws_config.input_height,
|
|
1111
|
+
sws_config.input_format,
|
|
1112
|
+
sws_config.output_width,
|
|
1113
|
+
sws_config.output_height,
|
|
1114
|
+
sws_config.output_format,
|
|
1115
|
+
sws_flags,
|
|
1116
|
+
nullptr,
|
|
1117
|
+
nullptr,
|
|
1118
|
+
nullptr);
|
|
1119
|
+
STD_TORCH_CHECK(sws_context, "sws_getContext() returned nullptr");
|
|
1120
|
+
|
|
1121
|
+
int* inv_table = nullptr;
|
|
1122
|
+
int* table = nullptr;
|
|
1123
|
+
int src_range, dst_range, brightness, contrast, saturation;
|
|
1124
|
+
int ret = sws_getColorspaceDetails(
|
|
1125
|
+
sws_context,
|
|
1126
|
+
&inv_table,
|
|
1127
|
+
&src_range,
|
|
1128
|
+
&table,
|
|
1129
|
+
&dst_range,
|
|
1130
|
+
&brightness,
|
|
1131
|
+
&contrast,
|
|
1132
|
+
&saturation);
|
|
1133
|
+
STD_TORCH_CHECK(ret != -1, "sws_getColorspaceDetails returned -1");
|
|
1134
|
+
|
|
1135
|
+
// swscale spells a range as an int: 1 is full (jpeg), 0 is limited.
|
|
1136
|
+
if (sws_config.input_color_range != AVCOL_RANGE_UNSPECIFIED) {
|
|
1137
|
+
src_range = sws_config.input_color_range == AVCOL_RANGE_JPEG;
|
|
1138
|
+
}
|
|
1139
|
+
if (sws_config.output_color_range != AVCOL_RANGE_UNSPECIFIED) {
|
|
1140
|
+
dst_range = sws_config.output_color_range == AVCOL_RANGE_JPEG;
|
|
1141
|
+
}
|
|
1142
|
+
|
|
1143
|
+
const int* colorspace_table =
|
|
1144
|
+
sws_getCoefficients(sws_config.input_colorspace);
|
|
1145
|
+
ret = sws_setColorspaceDetails(
|
|
1146
|
+
sws_context,
|
|
1147
|
+
colorspace_table,
|
|
1148
|
+
src_range,
|
|
1149
|
+
colorspace_table,
|
|
1150
|
+
dst_range,
|
|
1151
|
+
brightness,
|
|
1152
|
+
contrast,
|
|
1153
|
+
saturation);
|
|
1154
|
+
validate_sws_set_colorspace_details(ret, sws_config);
|
|
1155
|
+
|
|
1156
|
+
return UniqueSwsContext(sws_context);
|
|
1157
|
+
}
|
|
1158
|
+
|
|
1159
|
+
} // namespace facebook::torchcodec
|