Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qffmpeg_p.h
Go to the documentation of this file.
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 "qffmpegdefs_p.h"
19#include <QtMultimedia/qvideoframeformat.h>
20
21#include <qstring.h>
22#include <optional>
23
24inline bool operator==(const AVRational &lhs, const AVRational &rhs)
25{
26 return lhs.num == rhs.num && lhs.den == rhs.den;
27}
28
29inline bool operator!=(const AVRational &lhs, const AVRational &rhs)
30{
31 return !(lhs == rhs);
32}
33
35
36namespace QFFmpeg
37{
38
39inline std::optional<qint64> mul(qint64 a, AVRational b)
40{
41 return b.den != 0 ? (a * b.num + b.den / 2) / b.den : std::optional<qint64>{};
42}
43
44inline std::optional<qreal> mul(qreal a, AVRational b)
45{
46 return b.den != 0 ? a * qreal(b.num) / qreal(b.den) : std::optional<qreal>{};
47}
48
49inline std::optional<qint64> timeStampMs(qint64 ts, AVRational base)
50{
51 return mul(1'000 * ts, base);
52}
53
54inline std::optional<qint64> timeStampUs(qint64 ts, AVRational base)
55{
56 return mul(1'000'000 * ts, base);
57}
58
59inline std::optional<float> toFloat(AVRational r)
60{
61 return r.den != 0 ? float(r.num) / float(r.den) : std::optional<float>{};
62}
63
64inline QString err2str(int errnum)
65{
66 char buffer[AV_ERROR_MAX_STRING_SIZE + 1] = {};
67 av_make_error_string(buffer, AV_ERROR_MAX_STRING_SIZE, errnum);
69}
70
71inline void setAVFrameTime(AVFrame &frame, int64_t pts, const AVRational &timeBase)
72{
73 frame.pts = pts;
74#if QT_FFMPEG_HAS_FRAME_TIME_BASE
75 frame.time_base = timeBase;
76#else
77 Q_UNUSED(timeBase);
78#endif
79}
80
81inline void getAVFrameTime(const AVFrame &frame, int64_t &pts, AVRational &timeBase)
82{
83 pts = frame.pts;
84#if QT_FFMPEG_HAS_FRAME_TIME_BASE
85 timeBase = frame.time_base;
86#else
87 timeBase = { 0, 1 };
88#endif
89}
90
91inline int64_t getAVFrameDuration(const AVFrame &frame)
92{
93#if QT_FFMPEG_HAS_FRAME_DURATION
94 return frame.duration;
95#else
97 return 0;
98#endif
99}
100
102{
103 AVDictionary *opts = nullptr;
104
105 operator AVDictionary **() { return &opts; }
106
108
109 Q_DISABLE_COPY(AVDictionaryHolder)
110
112 : opts(std::exchange(other.opts, nullptr))
113 {
114 }
115
117 {
118 if (opts)
119 av_dict_free(&opts);
120 }
121};
122
123template<typename FunctionType, FunctionType F>
125{
126 template<typename T>
127 void operator()(T *object) const
128 {
129 if (object)
130 F(&object);
131 }
132};
133
134using AVFrameUPtr = std::unique_ptr<AVFrame, AVDeleter<decltype(&av_frame_free), &av_frame_free>>;
135
137{
138 return AVFrameUPtr(av_frame_alloc());
139}
140
142 std::unique_ptr<AVPacket, AVDeleter<decltype(&av_packet_free), &av_packet_free>>;
143
145 std::unique_ptr<AVCodecContext,
146 AVDeleter<decltype(&avcodec_free_context), &avcodec_free_context>>;
147
149 std::unique_ptr<AVBufferRef, AVDeleter<decltype(&av_buffer_unref), &av_buffer_unref>>;
150
151using AVHWFramesConstraintsUPtr = std::unique_ptr<
152 AVHWFramesConstraints,
153 AVDeleter<decltype(&av_hwframe_constraints_free), &av_hwframe_constraints_free>>;
154
155using SwrContextUPtr = std::unique_ptr<SwrContext, AVDeleter<decltype(&swr_free), &swr_free>>;
156
158using AVScore = int;
159constexpr AVScore BestAVScore = std::numeric_limits<AVScore>::max();
161constexpr AVScore NotSuitableAVScore = std::numeric_limits<AVScore>::min();
163
164const AVCodec *findAVDecoder(AVCodecID codecId,
165 const std::optional<AVHWDeviceType> &deviceType = {},
166 const std::optional<PixelOrSampleFormat> &format = {});
167
168const AVCodec *findAVEncoder(AVCodecID codecId,
169 const std::optional<AVHWDeviceType> &deviceType = {},
170 const std::optional<PixelOrSampleFormat> &format = {});
171
172const AVCodec *findAVEncoder(AVCodecID codecId,
173 const std::function<AVScore(const AVCodec *)> &scoresGetter);
174
176
177template<typename Format>
178bool hasAVFormat(const Format *fmts, Format format)
179{
180 return findAVFormat(fmts, [format](Format f) { return f == format; }) != Format(-1);
181}
182
183template<typename Format, typename Predicate>
184Format findAVFormat(const Format *fmts, const Predicate &predicate)
185{
186 auto scoresGetter = [&predicate](Format fmt) {
188 };
189 return findBestAVFormat(fmts, scoresGetter).first;
190}
191
192template <typename Value, typename CalculateScore>
193auto findBestAVValue(const Value *values, const CalculateScore &calculateScore,
194 Value invalidValue = {})
195{
196 using Limits = std::numeric_limits<decltype(calculateScore(*values))>;
197 std::pair result(invalidValue, Limits::min());
198 if (values) {
199 for (; *values != invalidValue && result.second != Limits::max(); ++values) {
200 const auto score = calculateScore(*values);
201 if (score > result.second)
202 result = { *values, score };
203 }
204 }
205
206 return result;
207}
208
209template <typename Format, typename CalculateScore>
210std::pair<Format, AVScore> findBestAVFormat(const Format *fmts,
211 const CalculateScore &calculateScore)
212{
213 static_assert(std::is_same_v<Format, AVSampleFormat> || std::is_same_v<Format, AVPixelFormat>,
214 "The input value is not AV format, use findBestAVValue instead.");
215 return findBestAVValue(fmts, calculateScore, Format(-1));
216}
217
218bool isHwPixelFormat(AVPixelFormat format);
219
220inline bool isSwPixelFormat(AVPixelFormat format)
221{
222 return !isHwPixelFormat(format);
223}
224
225bool isAVCodecExperimental(const AVCodec *codec);
226
227void applyExperimentalCodecOptions(const AVCodec *codec, AVDictionary** opts);
228
229AVPixelFormat pixelFormatForHwDevice(AVHWDeviceType deviceType);
230
231AVPacketSideData *addStreamSideData(AVStream *stream, AVPacketSideData sideData);
232
233const AVPacketSideData *streamSideData(const AVStream *stream, AVPacketSideDataType type);
234
235SwrContextUPtr createResampleContext(const AVAudioFormat &inputFormat,
236 const AVAudioFormat &outputFormat);
237
238QVideoFrameFormat::ColorTransfer fromAvColorTransfer(AVColorTransferCharacteristic colorTrc);
239
240#ifdef Q_OS_DARWIN
241bool isCVFormatSupported(uint32_t format);
242
243std::string cvFormatToString(uint32_t format);
244
245#endif
246}
247
248QDebug operator<<(QDebug, const AVRational &);
249
251
252#endif
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
static QString fromLocal8Bit(QByteArrayView ba)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:5949
ColorTransfer
\value ColorTransfer_Unknown The color transfer function is unknown.
Format
Definition ddsheader.h:14
const auto predicate
std::unique_ptr< AVHWFramesConstraints, AVDeleter< decltype(&av_hwframe_constraints_free), &av_hwframe_constraints_free > > AVHWFramesConstraintsUPtr
Definition qffmpeg_p.h:151
AVFrameUPtr makeAVFrame()
Definition qffmpeg_p.h:136
constexpr AVScore BestAVScore
Definition qffmpeg_p.h:159
void getAVFrameTime(const AVFrame &frame, int64_t &pts, AVRational &timeBase)
Definition qffmpeg_p.h:81
const AVCodec * findAVEncoder(AVCodecID codecId, const std::optional< AVHWDeviceType > &deviceType, const std::optional< PixelOrSampleFormat > &format)
Definition qffmpeg.cpp:433
std::pair< Format, AVScore > findBestAVFormat(const Format *fmts, const CalculateScore &calculateScore)
Definition qffmpeg_p.h:210
int64_t getAVFrameDuration(const AVFrame &frame)
Definition qffmpeg_p.h:91
bool isHwPixelFormat(AVPixelFormat format)
Definition qffmpeg.cpp:456
bool isAVCodecExperimental(const AVCodec *codec)
Definition qffmpeg.cpp:462
AVPacketSideData * addStreamSideData(AVStream *stream, AVPacketSideData sideData)
Definition qffmpeg.cpp:512
int PixelOrSampleFormat
Definition qffmpeg_p.h:157
QString err2str(int errnum)
Definition qffmpeg_p.h:64
bool isAVFormatSupported(const AVCodec *codec, PixelOrSampleFormat format)
Definition qffmpeg.cpp:445
const AVCodec * findAVDecoder(AVCodecID codecId, const std::optional< AVHWDeviceType > &deviceType, const std::optional< PixelOrSampleFormat > &format)
Definition qffmpeg.cpp:427
void setAVFrameTime(AVFrame &frame, int64_t pts, const AVRational &timeBase)
Definition qffmpeg_p.h:71
int AVScore
Definition qffmpeg_p.h:158
bool isSwPixelFormat(AVPixelFormat format)
Definition qffmpeg_p.h:220
constexpr AVScore DefaultAVScore
Definition qffmpeg_p.h:160
std::unique_ptr< AVCodecContext, AVDeleter< decltype(&avcodec_free_context), &avcodec_free_context > > AVCodecContextUPtr
Definition qffmpeg_p.h:144
bool hasAVFormat(const Format *fmts, Format format)
Definition qffmpeg_p.h:178
std::unique_ptr< AVFrame, AVDeleter< decltype(&av_frame_free), &av_frame_free > > AVFrameUPtr
Definition qffmpeg_p.h:134
std::unique_ptr< AVBufferRef, AVDeleter< decltype(&av_buffer_unref), &av_buffer_unref > > AVBufferUPtr
Definition qffmpeg_p.h:148
Format findAVFormat(const Format *fmts, const Predicate &predicate)
Definition qffmpeg_p.h:184
QVideoFrameFormat::ColorTransfer fromAvColorTransfer(AVColorTransferCharacteristic colorTrc)
Definition qffmpeg.cpp:590
void applyExperimentalCodecOptions(const AVCodec *codec, AVDictionary **opts)
Definition qffmpeg.cpp:467
constexpr AVScore NotSuitableAVScore
Definition qffmpeg_p.h:161
std::optional< qint64 > timeStampMs(qint64 ts, AVRational base)
Definition qffmpeg_p.h:49
SwrContextUPtr createResampleContext(const AVAudioFormat &inputFormat, const AVAudioFormat &outputFormat)
Definition qffmpeg.cpp:553
AVPixelFormat pixelFormatForHwDevice(AVHWDeviceType deviceType)
Definition qffmpeg.cpp:476
std::optional< qint64 > timeStampUs(qint64 ts, AVRational base)
Definition qffmpeg_p.h:54
std::unique_ptr< SwrContext, AVDeleter< decltype(&swr_free), &swr_free > > SwrContextUPtr
Definition qffmpeg_p.h:155
const AVPacketSideData * streamSideData(const AVStream *stream, AVPacketSideDataType type)
Definition qffmpeg.cpp:538
std::unique_ptr< AVPacket, AVDeleter< decltype(&av_packet_free), &av_packet_free > > AVPacketUPtr
Definition qffmpeg_p.h:141
std::optional< float > toFloat(AVRational r)
Definition qffmpeg_p.h:59
constexpr AVScore MinAVScore
Definition qffmpeg_p.h:162
std::optional< qint64 > mul(qint64 a, AVRational b)
Definition qffmpeg_p.h:39
auto findBestAVValue(const Value *values, const CalculateScore &calculateScore, Value invalidValue={})
Definition qffmpeg_p.h:193
Combined button and popup list for selecting options.
EGLStreamKHR stream
bool operator==(const AVRational &lhs, const AVRational &rhs)
Definition qffmpeg_p.h:24
QDebug operator<<(QDebug, const AVRational &)
Definition qffmpeg.cpp:636
bool operator!=(const AVRational &lhs, const AVRational &rhs)
Definition qffmpeg_p.h:29
QMediaFormat::AudioCodec codec
static AVCodecID codecId(QMediaFormat::VideoCodec codec)
GLenum GLsizei GLsizei GLint * values
[15]
GLboolean GLboolean GLboolean b
GLboolean GLboolean GLboolean GLboolean a
[7]
GLboolean r
[2]
GLfloat GLfloat f
GLenum GLuint buffer
GLenum type
GLint GLsizei GLsizei GLenum format
GLuint64EXT * result
[6]
#define Q_UNUSED(x)
long long qint64
Definition qtypes.h:60
double qreal
Definition qtypes.h:187
static const uint base
Definition qurlidna.cpp:20
QVideoFrameFormat::PixelFormat fmt
static QInputDevice::DeviceType deviceType(const UINT cursorType)
QSharedPointer< T > other(t)
[5]
QFrame frame
[0]
void operator()(T *object) const
Definition qffmpeg_p.h:127