Qt 6.x
The Qt SDK
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#include <private/qtmultimediaglobal_p.h>
7#include <qstring.h>
8#include <optional>
9
10extern "C" {
11#include <libavformat/avformat.h>
12#include <libavcodec/avcodec.h>
13#include <libswresample/swresample.h>
14#include <libavutil/avutil.h>
15#include <libswscale/swscale.h>
16}
17
18#define QT_FFMPEG_OLD_CHANNEL_LAYOUT (LIBAVCODEC_VERSION_INT < AV_VERSION_INT(59,24,100))
19#define QT_FFMPEG_HAS_VULKAN \
20 (LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(58, 91, 100)) // since ffmpeg n4.3
21#define QT_FFMPEG_HAS_FRAME_TIME_BASE \
22 (LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59, 18, 100)) // since ffmpeg n5.0
23#define QT_FFMPEG_HAS_FRAME_DURATION \
24 (LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(60, 3, 100)) // since ffmpeg n6.0
25
27
28namespace QFFmpeg
29{
30
31inline std::optional<qint64> mul(qint64 a, AVRational b)
32{
33 return b.den != 0 ? (a * b.num + b.den / 2) / b.den : std::optional<qint64>{};
34}
35
36inline std::optional<qreal> mul(qreal a, AVRational b)
37{
38 return b.den != 0 ? a * qreal(b.num) / qreal(b.den) : std::optional<qreal>{};
39}
40
41inline std::optional<qint64> timeStampMs(qint64 ts, AVRational base)
42{
43 return mul(1'000 * ts, base);
44}
45
46inline std::optional<qint64> timeStampUs(qint64 ts, AVRational base)
47{
48 return mul(1'000'000 * ts, base);
49}
50
51inline std::optional<float> toFloat(AVRational r)
52{
53 return r.den != 0 ? float(r.num) / float(r.den) : std::optional<float>{};
54}
55
56inline QString err2str(int errnum)
57{
58 char buffer[AV_ERROR_MAX_STRING_SIZE + 1] = {};
59 av_make_error_string(buffer, AV_ERROR_MAX_STRING_SIZE, errnum);
61}
62
63inline void setAVFrameTime(AVFrame &frame, int64_t pts, const AVRational &timeBase)
64{
65 frame.pts = pts;
66#if QT_FFMPEG_HAS_FRAME_TIME_BASE
67 frame.time_base = timeBase;
68#else
69 Q_UNUSED(timeBase);
70#endif
71}
72
73inline void getAVFrameTime(const AVFrame &frame, int64_t &pts, AVRational &timeBase)
74{
75 pts = frame.pts;
76#if QT_FFMPEG_HAS_FRAME_TIME_BASE
77 timeBase = frame.time_base;
78#else
79 timeBase = { 0, 1 };
80#endif
81}
82
83inline int64_t getAVFrameDuration(const AVFrame &frame)
84{
85#if QT_FFMPEG_HAS_FRAME_DURATION
86 return frame.duration;
87#else
89 return 0;
90#endif
91}
92
94{
95 AVDictionary *opts = nullptr;
96
97 operator AVDictionary **() { return &opts; }
98
100 {
101 if (opts)
102 av_dict_free(&opts);
103 }
104};
105
106template<typename FunctionType, FunctionType F>
108{
109 template<typename T>
110 void operator()(T *object) const
111 {
112 if (object)
113 F(&object);
114 }
115};
116
117using AVFrameUPtr = std::unique_ptr<AVFrame, AVDeleter<decltype(&av_frame_free), &av_frame_free>>;
118
120{
121 return AVFrameUPtr(av_frame_alloc());
122}
123
125 std::unique_ptr<AVPacket, AVDeleter<decltype(&av_packet_free), &av_packet_free>>;
126
128 std::unique_ptr<AVCodecContext,
129 AVDeleter<decltype(&avcodec_free_context), &avcodec_free_context>>;
130
132 std::unique_ptr<AVBufferRef, AVDeleter<decltype(&av_buffer_unref), &av_buffer_unref>>;
133
134using AVHWFramesConstraintsUPtr = std::unique_ptr<
135 AVHWFramesConstraints,
136 AVDeleter<decltype(&av_hwframe_constraints_free), &av_hwframe_constraints_free>>;
137
139using AVScore = int;
140constexpr AVScore BestAVScore = std::numeric_limits<AVScore>::max();
142constexpr AVScore NotSuitableAVScore = std::numeric_limits<AVScore>::min();
144
145const AVCodec *findAVDecoder(AVCodecID codecId,
146 const std::optional<AVHWDeviceType> &deviceType = {},
147 const std::optional<PixelOrSampleFormat> &format = {});
148
149const AVCodec *findAVEncoder(AVCodecID codecId,
150 const std::optional<AVHWDeviceType> &deviceType = {},
151 const std::optional<PixelOrSampleFormat> &format = {});
152
153const AVCodec *findAVEncoder(AVCodecID codecId,
154 const std::function<AVScore(const AVCodec *)> &scoresGetter);
155
157
158template<typename Format>
159bool hasAVFormat(const Format *fmts, Format format)
160{
161 return findAVFormat(fmts, [format](Format f) { return f == format; }) != Format(-1);
162}
163
164template<typename Format, typename Predicate>
165Format findAVFormat(const Format *fmts, const Predicate &predicate)
166{
167 auto scoresGetter = [&predicate](Format fmt) {
169 };
170 return findBestAVFormat(fmts, scoresGetter).first;
171}
172
173template<typename Format, typename CalculateScore>
174std::pair<Format, AVScore> findBestAVFormat(const Format *fmts,
175 const CalculateScore &calculateScore)
176{
177 std::pair result(Format(-1), NotSuitableAVScore);
178 if (fmts) {
179 for (; *fmts != -1 && result.second != BestAVScore; ++fmts) {
180 const auto score = calculateScore(*fmts);
181 if (score > result.second)
182 result = std::pair(*fmts, score);
183 }
184 }
185
186 return result;
187}
188
189bool isHwPixelFormat(AVPixelFormat format);
190
191inline bool isSwPixelFormat(AVPixelFormat format)
192{
193 return !isHwPixelFormat(format);
194}
195
196AVPixelFormat pixelFormatForHwDevice(AVHWDeviceType deviceType);
197
198#ifdef Q_OS_DARWIN
199bool isCVFormatSupported(uint32_t format);
200
201std::string cvFormatToString(uint32_t format);
202
203#endif
204}
205
207
208#endif
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
static QString fromLocal8Bit(QByteArrayView ba)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:5788
Format
Definition ddsheader.h:14
const auto predicate
AVFrameUPtr makeAVFrame()
Definition qffmpeg_p.h:119
constexpr AVScore BestAVScore
Definition qffmpeg_p.h:140
void getAVFrameTime(const AVFrame &frame, int64_t &pts, AVRational &timeBase)
Definition qffmpeg_p.h:73
const AVCodec * findAVEncoder(AVCodecID codecId, const std::optional< AVHWDeviceType > &deviceType, const std::optional< PixelOrSampleFormat > &format)
Definition qffmpeg.cpp:276
std::pair< Format, AVScore > findBestAVFormat(const Format *fmts, const CalculateScore &calculateScore)
Definition qffmpeg_p.h:174
std::unique_ptr< AVHWFramesConstraints, AVDeleter< decltype(&av_hwframe_constraints_free), &av_hwframe_constraints_free > > AVHWFramesConstraintsUPtr
Definition qffmpeg_p.h:136
int64_t getAVFrameDuration(const AVFrame &frame)
Definition qffmpeg_p.h:83
std::unique_ptr< AVCodecContext, AVDeleter< decltype(&avcodec_free_context), &avcodec_free_context > > AVCodecContextUPtr
Definition qffmpeg_p.h:129
bool isHwPixelFormat(AVPixelFormat format)
Definition qffmpeg.cpp:299
std::unique_ptr< AVBufferRef, AVDeleter< decltype(&av_buffer_unref), &av_buffer_unref > > AVBufferUPtr
Definition qffmpeg_p.h:132
QString err2str(int errnum)
Definition qffmpeg_p.h:56
bool isAVFormatSupported(const AVCodec *codec, PixelOrSampleFormat format)
Definition qffmpeg.cpp:288
const AVCodec * findAVDecoder(AVCodecID codecId, const std::optional< AVHWDeviceType > &deviceType, const std::optional< PixelOrSampleFormat > &format)
Definition qffmpeg.cpp:270
void setAVFrameTime(AVFrame &frame, int64_t pts, const AVRational &timeBase)
Definition qffmpeg_p.h:63
std::unique_ptr< AVPacket, AVDeleter< decltype(&av_packet_free), &av_packet_free > > AVPacketUPtr
Definition qffmpeg_p.h:125
bool isSwPixelFormat(AVPixelFormat format)
Definition qffmpeg_p.h:191
constexpr AVScore DefaultAVScore
Definition qffmpeg_p.h:141
bool hasAVFormat(const Format *fmts, Format format)
Definition qffmpeg_p.h:159
int PixelOrSampleFormat
Definition qffmpeg_p.h:138
Format findAVFormat(const Format *fmts, const Predicate &predicate)
Definition qffmpeg_p.h:165
int AVScore
Definition qffmpeg_p.h:139
constexpr AVScore NotSuitableAVScore
Definition qffmpeg_p.h:142
std::optional< qint64 > timeStampMs(qint64 ts, AVRational base)
Definition qffmpeg_p.h:41
AVPixelFormat pixelFormatForHwDevice(AVHWDeviceType deviceType)
Definition qffmpeg.cpp:305
std::optional< qint64 > timeStampUs(qint64 ts, AVRational base)
Definition qffmpeg_p.h:46
std::optional< float > toFloat(AVRational r)
Definition qffmpeg_p.h:51
constexpr AVScore MinAVScore
Definition qffmpeg_p.h:143
std::unique_ptr< AVFrame, AVDeleter< decltype(&av_frame_free), &av_frame_free > > AVFrameUPtr
Definition qffmpeg_p.h:117
std::optional< qint64 > mul(qint64 a, AVRational b)
Definition qffmpeg_p.h:31
Combined button and popup list for selecting options.
QMediaFormat::AudioCodec codec
static AVCodecID codecId(QMediaFormat::VideoCodec codec)
GLboolean GLboolean GLboolean b
GLboolean GLboolean GLboolean GLboolean a
[7]
GLboolean r
[2]
GLfloat GLfloat f
GLenum GLuint buffer
GLint GLsizei GLsizei GLenum format
GLuint64EXT * result
[6]
#define Q_UNUSED(x)
long long qint64
Definition qtypes.h:55
double qreal
Definition qtypes.h:92
QVideoFrameFormat::PixelFormat fmt
static QInputDevice::DeviceType deviceType(const UINT cursorType)
QFrame frame
[0]
void operator()(T *object) const
Definition qffmpeg_p.h:110