1 | // Copyright (C) 2021 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | #ifndef QFFMPEG_P_H |
4 | #define QFFMPEG_P_H |
5 | |
6 | // |
7 | // W A R N I N G |
8 | // ------------- |
9 | // |
10 | // This file is not part of the Qt API. It exists purely as an |
11 | // implementation detail. This header file may change from version to |
12 | // version without notice, or even be removed. |
13 | // |
14 | // We mean it. |
15 | // |
16 | |
17 | #include <QtFFmpegMediaPluginImpl/private/qffmpegdefs_p.h> |
18 | #include <QtFFmpegMediaPluginImpl/private/qffmpegcodec_p.h> |
19 | #include <QtFFmpegMediaPluginImpl/private/qffmpegavaudioformat_p.h> |
20 | #include <QtMultimedia/qvideoframeformat.h> |
21 | |
22 | #include <qstring.h> |
23 | #include <optional> |
24 | |
25 | inline bool operator==(const AVRational &lhs, const AVRational &rhs) |
26 | { |
27 | return lhs.num == rhs.num && lhs.den == rhs.den; |
28 | } |
29 | |
30 | inline bool operator!=(const AVRational &lhs, const AVRational &rhs) |
31 | { |
32 | return !(lhs == rhs); |
33 | } |
34 | |
35 | QT_BEGIN_NAMESPACE |
36 | |
37 | namespace QFFmpeg |
38 | { |
39 | |
40 | inline std::optional<qint64> mul(qint64 a, AVRational b) |
41 | { |
42 | if (b.den == 0) |
43 | return {}; |
44 | |
45 | auto multiplyAndRound = [](qint64 a, AVRational b) { // |
46 | return (a * b.num + b.den / 2) / b.den; |
47 | }; |
48 | |
49 | if (a < 0) |
50 | return -multiplyAndRound(-a, b); |
51 | else |
52 | return multiplyAndRound(a, b); |
53 | } |
54 | |
55 | inline std::optional<qreal> mul(qreal a, AVRational b) |
56 | { |
57 | return b.den != 0 ? a * qreal(b.num) / qreal(b.den) : std::optional<qreal>{}; |
58 | } |
59 | |
60 | inline std::optional<qint64> timeStampMs(qint64 ts, AVRational base) |
61 | { |
62 | return mul(a: 1'000 * ts, b: base); |
63 | } |
64 | |
65 | inline std::optional<qint64> timeStampUs(qint64 ts, AVRational base) |
66 | { |
67 | return mul(a: 1'000'000 * ts, b: base); |
68 | } |
69 | |
70 | inline std::optional<float> toFloat(AVRational r) |
71 | { |
72 | return r.den != 0 ? float(r.num) / float(r.den) : std::optional<float>{}; |
73 | } |
74 | |
75 | inline QString err2str(int errnum) |
76 | { |
77 | char buffer[AV_ERROR_MAX_STRING_SIZE + 1] = {}; |
78 | av_make_error_string(errbuf: buffer, AV_ERROR_MAX_STRING_SIZE, errnum); |
79 | return QString::fromLocal8Bit(ba: buffer); |
80 | } |
81 | |
82 | inline void setAVFrameTime(AVFrame &frame, int64_t pts, const AVRational &timeBase) |
83 | { |
84 | frame.pts = pts; |
85 | #if QT_FFMPEG_HAS_FRAME_TIME_BASE |
86 | frame.time_base = timeBase; |
87 | #else |
88 | Q_UNUSED(timeBase); |
89 | #endif |
90 | } |
91 | |
92 | inline void getAVFrameTime(const AVFrame &frame, int64_t &pts, AVRational &timeBase) |
93 | { |
94 | pts = frame.pts; |
95 | #if QT_FFMPEG_HAS_FRAME_TIME_BASE |
96 | timeBase = frame.time_base; |
97 | #else |
98 | timeBase = { .num: 0, .den: 1 }; |
99 | #endif |
100 | } |
101 | |
102 | inline int64_t getAVFrameDuration(const AVFrame &frame) |
103 | { |
104 | #if QT_FFMPEG_HAS_FRAME_DURATION |
105 | return frame.duration; |
106 | #else |
107 | return frame.pkt_duration; |
108 | #endif |
109 | } |
110 | |
111 | struct AVDictionaryHolder |
112 | { |
113 | AVDictionary *opts = nullptr; |
114 | |
115 | operator AVDictionary **() { return &opts; } |
116 | |
117 | AVDictionaryHolder() = default; |
118 | |
119 | Q_DISABLE_COPY(AVDictionaryHolder) |
120 | |
121 | AVDictionaryHolder(AVDictionaryHolder &&other) noexcept |
122 | : opts(std::exchange(obj&: other.opts, new_val: nullptr)) |
123 | { |
124 | } |
125 | |
126 | ~AVDictionaryHolder() |
127 | { |
128 | if (opts) |
129 | av_dict_free(m: &opts); |
130 | } |
131 | }; |
132 | |
133 | template<typename FunctionType, FunctionType F> |
134 | struct AVDeleter |
135 | { |
136 | template <typename T, std::invoke_result_t<FunctionType, T **> * = nullptr> |
137 | void operator()(T *object) const |
138 | { |
139 | if (object) |
140 | F(&object); |
141 | } |
142 | |
143 | template <typename T, std::invoke_result_t<FunctionType, T *> * = nullptr> |
144 | void operator()(T *object) const |
145 | { |
146 | F(object); |
147 | } |
148 | }; |
149 | |
150 | using AVFrameUPtr = std::unique_ptr<AVFrame, AVDeleter<decltype(&av_frame_free), &av_frame_free>>; |
151 | |
152 | inline AVFrameUPtr makeAVFrame() |
153 | { |
154 | return AVFrameUPtr(av_frame_alloc()); |
155 | } |
156 | |
157 | using AVPacketUPtr = |
158 | std::unique_ptr<AVPacket, AVDeleter<decltype(&av_packet_free), &av_packet_free>>; |
159 | |
160 | using AVCodecContextUPtr = |
161 | std::unique_ptr<AVCodecContext, |
162 | AVDeleter<decltype(&avcodec_free_context), &avcodec_free_context>>; |
163 | |
164 | using AVBufferUPtr = |
165 | std::unique_ptr<AVBufferRef, AVDeleter<decltype(&av_buffer_unref), &av_buffer_unref>>; |
166 | |
167 | using AVHWFramesConstraintsUPtr = std::unique_ptr< |
168 | AVHWFramesConstraints, |
169 | AVDeleter<decltype(&av_hwframe_constraints_free), &av_hwframe_constraints_free>>; |
170 | |
171 | using SwrContextUPtr = std::unique_ptr<SwrContext, AVDeleter<decltype(&swr_free), &swr_free>>; |
172 | |
173 | using SwsContextUPtr = |
174 | std::unique_ptr<SwsContext, AVDeleter<decltype(&sws_freeContext), &sws_freeContext>>; |
175 | |
176 | bool isAVFormatSupported(const Codec &codec, PixelOrSampleFormat format); |
177 | |
178 | // Returns true if the range contains the value, false otherwise |
179 | template <typename Value> |
180 | bool hasValue(QSpan<const Value> range, Value value) |
181 | { |
182 | return std::find(range.begin(), range.end(), value) != range.end(); |
183 | } |
184 | |
185 | // Search for the first element in the range that satisfies the predicate |
186 | // The predicate is evaluated for each value in the range until it returns |
187 | // true, and the corresponding value is returned. If no match is found, |
188 | // std::nullopt is returned. |
189 | template <typename Value, typename Predicate> |
190 | std::optional<Value> findIf(QSpan<const Value> range, const Predicate &predicate) |
191 | { |
192 | const auto value = std::find_if(range.begin(), range.end(), predicate); |
193 | if (value == range.end()) |
194 | return {}; |
195 | return *value; |
196 | } |
197 | |
198 | // Search the codec's pixel formats for a format that matches the predicate. |
199 | // If no pixel format is found, repeat the search through the pixel formats |
200 | // of all the codec's hardware configs. If no matching pixel format is found, |
201 | // std::nullopt is returned. The predicate is evaluated once for each pixel |
202 | // format until the predicate returns true. |
203 | template <typename Predicate> |
204 | std::optional<AVPixelFormat> findAVPixelFormat(const Codec &codec, const Predicate &predicate) |
205 | { |
206 | if (codec.type() != AVMEDIA_TYPE_VIDEO) |
207 | return {}; |
208 | |
209 | const auto pixelFormats = codec.pixelFormats(); |
210 | |
211 | if (const auto format = findIf(pixelFormats, predicate)) |
212 | return format; |
213 | |
214 | // No matching pixel format was found. Check the pixel format |
215 | // of the codec's hardware config. |
216 | for (const AVCodecHWConfig *const config : codec.hwConfigs()) { |
217 | const AVPixelFormat format = config->pix_fmt; |
218 | |
219 | if (format == AV_PIX_FMT_NONE) |
220 | continue; |
221 | |
222 | if (predicate(format)) |
223 | return format; |
224 | } |
225 | return {}; |
226 | } |
227 | |
228 | // Evaluate the function for each of the codec's pixel formats and each of |
229 | // the pixel formats supported by the codec's hardware configs. |
230 | template <typename Function> |
231 | void forEachAVPixelFormat(const Codec &codec, const Function &function) |
232 | { |
233 | findAVPixelFormat(codec, [&function](AVPixelFormat format) { |
234 | function(format); |
235 | return false; // Evaluate the function for all pixel formats |
236 | }); |
237 | } |
238 | |
239 | template <typename ValueT, typename ScoreT = AVScore> |
240 | struct ValueAndScore |
241 | { |
242 | std::optional<ValueT> value; |
243 | ScoreT score = std::numeric_limits<ScoreT>::min(); |
244 | }; |
245 | |
246 | template <typename Value, typename CalculateScore, |
247 | typename ScoreType = std::invoke_result_t<CalculateScore, Value>> |
248 | ValueAndScore<Value, ScoreType> findBestAVValueWithScore(QSpan<const Value> values, |
249 | const CalculateScore &calculateScore) |
250 | { |
251 | static_assert(std::is_invocable_v<CalculateScore, Value>); |
252 | |
253 | ValueAndScore<Value, ScoreType> result; |
254 | for (const Value &val : values) { |
255 | const ScoreType score = calculateScore(val); |
256 | if (score > result.score) |
257 | result = { val, score }; // Note: Optional is only set if score > Limits::min() |
258 | |
259 | if (result.score == std::numeric_limits<ScoreType>::max()) |
260 | break; |
261 | } |
262 | |
263 | return result; |
264 | } |
265 | |
266 | template <typename Value, typename CalculateScore> |
267 | std::optional<Value> findBestAVValue(QSpan<const Value> values, |
268 | const CalculateScore &calculateScore) |
269 | { |
270 | return findBestAVValueWithScore(values, calculateScore).value; |
271 | } |
272 | |
273 | bool isHwPixelFormat(AVPixelFormat format); |
274 | |
275 | inline bool isSwPixelFormat(AVPixelFormat format) |
276 | { |
277 | return !isHwPixelFormat(format); |
278 | } |
279 | |
280 | void applyExperimentalCodecOptions(const Codec &codec, AVDictionary **opts); |
281 | |
282 | AVPixelFormat pixelFormatForHwDevice(AVHWDeviceType deviceType); |
283 | |
284 | AVPacketSideData *addStreamSideData(AVStream *stream, AVPacketSideData sideData); |
285 | |
286 | const AVPacketSideData *streamSideData(const AVStream *stream, AVPacketSideDataType type); |
287 | |
288 | SwrContextUPtr createResampleContext(const AVAudioFormat &inputFormat, |
289 | const AVAudioFormat &outputFormat); |
290 | |
291 | QVideoFrameFormat::ColorTransfer fromAvColorTransfer(AVColorTransferCharacteristic colorTrc); |
292 | |
293 | AVColorTransferCharacteristic toAvColorTransfer(QVideoFrameFormat::ColorTransfer colorTrc); |
294 | |
295 | QVideoFrameFormat::ColorSpace fromAvColorSpace(AVColorSpace colorSpace); |
296 | |
297 | AVColorSpace toAvColorSpace(QVideoFrameFormat::ColorSpace colorSpace); |
298 | |
299 | QVideoFrameFormat::ColorRange fromAvColorRange(AVColorRange colorRange); |
300 | |
301 | AVColorRange toAvColorRange(QVideoFrameFormat::ColorRange colorRange); |
302 | |
303 | AVHWDeviceContext *avFrameDeviceContext(const AVFrame *frame); |
304 | |
305 | SwsContextUPtr createSwsContext(const QSize &srcSize, AVPixelFormat srcPixFmt, const QSize &dstSize, |
306 | AVPixelFormat dstPixFmt, int conversionType = SWS_BICUBIC); |
307 | |
308 | #ifdef Q_OS_DARWIN |
309 | bool isCVFormatSupported(uint32_t format); |
310 | |
311 | std::string cvFormatToString(uint32_t format); |
312 | |
313 | #endif |
314 | } // namespace QFFmpeg |
315 | |
316 | QDebug operator<<(QDebug, const AVRational &); |
317 | |
318 | #if QT_FFMPEG_HAS_AV_CHANNEL_LAYOUT |
319 | QDebug operator<<(QDebug, const AVChannelLayout &); |
320 | #endif |
321 | |
322 | QT_END_NAMESPACE |
323 | |
324 | #endif |
325 | |