Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
framegenerator.cpp
Go to the documentation of this file.
1
// Copyright (C) 2024 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4
#
include
"framegenerator_p.h"
5
6
#
include
<
QtMultimedia
/
private
/
qplatformmediaintegration_p
.
h
>
7
#
include
<
QtMultimedia
/
private
/
qmemoryvideobuffer_p
.
h
>
8
#
include
<
QtMultimedia
/
private
/
qvideoframe_p
.
h
>
9
10
#
include
<
QtCore
/
qbuffer
.
h
>
11
12
QT_BEGIN_NAMESPACE
13
14
void
VideoGenerator
::setPattern(ImagePattern pattern)
15
{
16
m_pattern = pattern;
17
}
18
19
void
VideoGenerator
::
setFrameCount
(
int
count)
20
{
21
m_maxFrameCount = count;
22
}
23
24
void
VideoGenerator
::
setSize
(QSize size)
25
{
26
m_size = size;
27
}
28
29
void
VideoGenerator
::
setPixelFormat
(QVideoFrameFormat::PixelFormat pixelFormat)
30
{
31
m_pixelFormat = pixelFormat;
32
}
33
34
void
VideoGenerator
::
setFrameRate
(
double
rate)
35
{
36
m_frameRate = rate;
37
}
38
39
void
VideoGenerator
::
setPeriod
(
std
::chrono::microseconds period)
40
{
41
m_period = period;
42
}
43
44
void
VideoGenerator
::
setPresentationRotation
(QtVideo::Rotation rotation)
45
{
46
m_presentationRotation = rotation;
47
}
48
49
void
VideoGenerator
::
setPresentationMirrored
(
bool
mirror)
50
{
51
m_presentationMirrored = mirror;
52
}
53
void
VideoGenerator
::
setEndTimeReporting
(
bool
endTimeIsReported)
54
{
55
m_endTimeIsReported = endTimeIsReported;
56
}
57
58
void
VideoGenerator
::
setColors
(std::array<QColor, 5> newColors)
59
{
60
m_colors = newColors;
61
}
62
63
void
VideoGenerator
::
emitEmptyFrameOnStop
()
64
{
65
m_emitEmptyFrameOnStop =
true
;
66
}
67
68
static
void
fillColoredSquares
(QImage& image)
69
{
70
QList<QColor> colors = { Qt::red, Qt::green, Qt::blue, Qt::yellow };
71
const
int
width = image.width();
72
const
int
height = image.height();
73
74
for
(
int
j = 0; j < height; ++j) {
75
for
(
int
i = 0; i < width; ++i) {
76
const
int
colorX = i < width / 2 ? 0 : 1;
77
const
int
colorY = j < height / 2 ? 0 : 1;
78
const
int
colorIndex = colorX + 2 * colorY;
79
image.setPixel(i, j, colors[colorIndex].rgb());
80
}
81
}
82
}
83
84
static
QVideoFrame
createJpegFrame
(
const
QImage &image, QSize size)
85
{
86
QByteArray jpegData;
87
QBuffer buffer(&jpegData);
88
if
(!buffer.open(QIODevice::WriteOnly))
89
return
{};
90
91
// Maximum quality, so that the comparison against the source image stays meaningful
92
if
(!image.save(&buffer,
"JPG"
, 100))
93
return
{};
94
95
// JPEG frames hold the compressed data in a single plane without a line stride
96
auto
videoBuffer = std::make_unique<QMemoryVideoBuffer>(std::move(jpegData), -1);
97
return
QVideoFramePrivate::createFrame(std::move(videoBuffer),
98
QVideoFrameFormat(size, QVideoFrameFormat::Format_Jpeg));
99
}
100
101
QVideoFrame
VideoGenerator
::
createFrame
()
102
{
103
using
namespace
std
::chrono;
104
105
QImage image(m_size, QImage::Format_ARGB32);
106
switch
(m_pattern) {
107
case
ImagePattern::SingleColor:
108
image.fill(m_colors[m_frameIndex % m_colors.size()]);
109
break
;
110
case
ImagePattern::ColoredSquares:
111
fillColoredSquares(image);
112
break
;
113
}
114
115
QVideoFrame frame;
116
if
(m_pixelFormat == QVideoFrameFormat::Format_Jpeg) {
117
frame = createJpegFrame(image, m_size);
118
}
else
{
119
QVideoFrame rgbFrame(image);
120
QVideoFrameFormat outputFormat{ m_size, m_pixelFormat };
121
frame = QPlatformMediaIntegration::instance()->convertVideoFrame(rgbFrame, outputFormat);
122
}
123
124
if
(m_frameRate)
125
frame.setStreamFrameRate(*m_frameRate);
126
127
if
(m_period) {
128
frame.setStartTime(m_period->count() * m_frameIndex);
129
if
(m_endTimeIsReported)
130
frame.setEndTime(m_period->count() * (m_frameIndex + 1));
131
}
132
133
if
(m_presentationRotation)
134
frame.setRotation(*m_presentationRotation);
135
136
if
(m_presentationMirrored)
137
frame.setMirrored(*m_presentationMirrored);
138
139
return
frame;
140
}
141
142
void
VideoGenerator
::nextFrame()
143
{
144
if
(m_frameIndex == m_maxFrameCount) {
145
Q_EMIT done();
146
if
(m_emitEmptyFrameOnStop)
147
Q_EMIT frameCreated({});
148
return
;
149
}
150
151
const
QVideoFrame frame = createFrame();
152
Q_EMIT frameCreated(frame);
153
++m_frameIndex;
154
}
155
156
QT_END_NAMESPACE
157
158
#
include
"moc_framegenerator_p.cpp"
VideoGenerator
Definition
framegenerator_p.h:33
VideoGenerator::setEndTimeReporting
void setEndTimeReporting(bool endTimeIsReported)
Definition
framegenerator.cpp:53
VideoGenerator::createFrame
QVideoFrame createFrame()
Definition
framegenerator.cpp:101
VideoGenerator::setSize
void setSize(QSize size)
Definition
framegenerator.cpp:24
VideoGenerator::emitEmptyFrameOnStop
void emitEmptyFrameOnStop()
Definition
framegenerator.cpp:63
VideoGenerator::setPresentationMirrored
void setPresentationMirrored(bool mirror)
Definition
framegenerator.cpp:49
VideoGenerator::setColors
void setColors(std::array< QColor, 5 > colors)
Definition
framegenerator.cpp:58
VideoGenerator::setFrameCount
void setFrameCount(int count)
Definition
framegenerator.cpp:19
VideoGenerator::setPeriod
void setPeriod(std::chrono::microseconds period)
Definition
framegenerator.cpp:39
VideoGenerator::setFrameRate
void setFrameRate(double rate)
Definition
framegenerator.cpp:34
VideoGenerator::setPresentationRotation
void setPresentationRotation(QtVideo::Rotation rotation)
Definition
framegenerator.cpp:44
VideoGenerator::setPixelFormat
void setPixelFormat(QVideoFrameFormat::PixelFormat pixelFormat)
Definition
framegenerator.cpp:29
fillColoredSquares
static void fillColoredSquares(QImage &image)
Definition
framegenerator.cpp:68
createJpegFrame
static QVideoFrame createJpegFrame(const QImage &image, QSize size)
Definition
framegenerator.cpp:84
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
std
[33]
Definition
src_corelib_tools_qhash.cpp:421
qtmultimedia
src
multimediatestlib
framegenerator.cpp
Generated on
for Qt by
1.18.0