summaryrefslogtreecommitdiffstats
path: root/src/multimediatestlib/framegenerator.cpp
blob: d63933fb20a858caf72414e7eab0605fdc028457 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include "framegenerator_p.h"
#include <QtCore/qdebug.h>

#include <private/qplatformmediaintegration_p.h>

QT_BEGIN_NAMESPACE

void VideoGenerator::setPattern(ImagePattern pattern)
{
    m_pattern = pattern;
}

void VideoGenerator::setFrameCount(int count)
{
    m_maxFrameCount = count;
}

void VideoGenerator::setSize(QSize size)
{
    m_size = size;
}

void VideoGenerator::setPixelFormat(QVideoFrameFormat::PixelFormat pixelFormat)
{
    m_pixelFormat = pixelFormat;
}

void VideoGenerator::setFrameRate(double rate)
{
    m_frameRate = rate;
}

void VideoGenerator::setPeriod(std::chrono::milliseconds period)
{
    m_period = period;
}

void VideoGenerator::setPresentationRotation(QtVideo::Rotation rotation)
{
    m_presentationRotation = rotation;
}

void VideoGenerator::setPresentationMirrored(bool mirror)
{
    m_presentationMirrored = mirror;
}

void VideoGenerator::emitEmptyFrameOnStop()
{
    m_emitEmptyFrameOnStop = true;
}

static void fillColoredSquares(QImage& image)
{
    QList<QColor> colors = { Qt::red, Qt::green, Qt::blue, Qt::yellow };
    const int width = image.width();
    const int height = image.height();

    for (int j = 0; j < height; ++j) {
        for (int i = 0; i < width; ++i) {
            const int colorX = i < width / 2 ? 0 : 1;
            const int colorY = j < height / 2 ? 0 : 1;
            const int colorIndex = colorX + 2 * colorY;
            image.setPixel(i, j, colors[colorIndex].rgb());
        }
    }
}

QVideoFrame VideoGenerator::createFrame()
{
    using namespace std::chrono;

    QImage image(m_size, QImage::Format_ARGB32);
    switch (m_pattern) {
    case ImagePattern::SingleColor:
        image.fill(colors[m_frameIndex % colors.size()]);
        break;
    case ImagePattern::ColoredSquares:
        fillColoredSquares(image);
        break;
    }

    QVideoFrame rgbFrame(image);
    QVideoFrameFormat outputFormat { m_size, m_pixelFormat };
    QVideoFrame frame =
            QPlatformMediaIntegration::instance()->convertVideoFrame(rgbFrame, outputFormat);

    if (m_frameRate)
        frame.setStreamFrameRate(*m_frameRate);

    if (m_period) {
        frame.setStartTime(duration_cast<microseconds>(*m_period).count() * m_frameIndex);
        frame.setEndTime(duration_cast<microseconds>(*m_period).count() * (m_frameIndex + 1));
    }

    if (m_presentationRotation)
        frame.setRotation(*m_presentationRotation);

    if (m_presentationMirrored)
        frame.setMirrored(*m_presentationMirrored);

    return frame;
}

void VideoGenerator::nextFrame()
{
    if (m_frameIndex == m_maxFrameCount) {
        emit done();
        if (m_emitEmptyFrameOnStop)
            emit frameCreated({});
        return;
    }

    const QVideoFrame frame = createFrame();
    emit frameCreated(frame);
    ++m_frameIndex;
}

QT_END_NAMESPACE

#include "moc_framegenerator_p.cpp"