Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qvideoframeconverter.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 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// Qt-Security score:critical reason:data-parser
4
11#include "qcachedvalue_p.h"
12
13#include <QtCore/qcoreapplication.h>
14#include <QtCore/qsize.h>
15#include <QtCore/qhash.h>
16#include <QtCore/qfile.h>
17#include <QtGui/qimage.h>
18#include <QtCore/qloggingcategory.h>
19
20#include <QtMultimedia/private/qaudio_qspan_support_p.h>
21#include <QtMultimedia/private/qmultimedia_ranges_p.h>
22#include <QtMultimedia/private/qvideotexturehelper_p.h>
23
24#include <rhi/qrhi.h>
25
26#ifdef Q_OS_DARWIN
27#include <QtCore/private/qcore_mac_p.h>
28#endif
29
31
32namespace ranges = QtMultimediaPrivate::ranges;
33
34Q_STATIC_LOGGING_CATEGORY(qLcVideoFrameConverter, "qt.multimedia.video.frameconverter")
35
36// clang-format off
37static constexpr float g_quad[] = {
38 // Rotation 0 CW
39 1.f, -1.f, 1.f, 1.f,
40 1.f, 1.f, 1.f, 0.f,
41 -1.f, -1.f, 0.f, 1.f,
42 -1.f, 1.f, 0.f, 0.f,
43 // Rotation 90 CW
44 1.f, -1.f, 1.f, 0.f,
45 1.f, 1.f, 0.f, 0.f,
46 -1.f, -1.f, 1.f, 1.f,
47 -1.f, 1.f, 0.f, 1.f,
48 // Rotation 180 CW
49 1.f, -1.f, 0.f, 0.f,
50 1.f, 1.f, 0.f, 1.f,
51 -1.f, -1.f, 1.f, 0.f,
52 -1.f, 1.f, 1.f, 1.f,
53 // Rotation 270 CW
54 1.f, -1.f, 0.f, 1.f,
55 1.f, 1.f, 1.f, 1.f,
56 -1.f, -1.f, 0.f, 0.f,
57 -1.f, 1.f, 1.f, 0.f,
58};
59// clang-format on
60
61static bool pixelFormatHasAlpha(QVideoFrameFormat::PixelFormat format)
62{
63 switch (format) {
64 case QVideoFrameFormat::Format_ARGB8888:
65 case QVideoFrameFormat::Format_ARGB8888_Premultiplied:
66 case QVideoFrameFormat::Format_BGRA8888:
67 case QVideoFrameFormat::Format_BGRA8888_Premultiplied:
68 case QVideoFrameFormat::Format_ABGR8888:
69 case QVideoFrameFormat::Format_RGBA8888:
70 case QVideoFrameFormat::Format_AYUV:
71 case QVideoFrameFormat::Format_AYUV_Premultiplied:
72 return true;
73 default:
74 return false;
75 }
76};
77
78static QShader ensureShader(const QString &name)
79{
80 static QCachedValueMap<QString, QShader> shaderCache;
81
82 return shaderCache.ensure(name, [&name]() {
83 QFile f(name);
84 return f.open(QIODevice::ReadOnly) ? QShader::fromSerialized(f.readAll()) : QShader();
85 });
86}
87
88static void rasterTransform(QImage &image, VideoTransformation transformation)
89{
90 QTransform t;
91 if (transformation.rotation != QtVideo::Rotation::None)
92 t.rotate(qreal(transformation.rotation));
93 if (transformation.mirroredHorizontallyAfterRotation)
94 t.scale(-1., 1);
95 if (!t.isIdentity())
96 image = image.transformed(t);
97}
98
99static void imageCleanupHandler(void *info)
100{
101 QByteArray *imageData = reinterpret_cast<QByteArray *>(info);
102 delete imageData;
103}
104
105static bool updateTextures(QRhi *rhi,
106 std::unique_ptr<QRhiBuffer> &uniformBuffer,
107 std::unique_ptr<QRhiSampler> &textureSampler,
108 std::unique_ptr<QRhiShaderResourceBindings> &shaderResourceBindings,
109 std::unique_ptr<QRhiGraphicsPipeline> &graphicsPipeline,
110 std::unique_ptr<QRhiRenderPassDescriptor> &renderPass,
111 QVideoFrame &frame,
112 const QVideoFrameTexturesUPtr &videoFrameTextures)
113{
114 auto format = frame.surfaceFormat();
115 auto pixelFormat = format.pixelFormat();
116
117 auto textureDesc = QVideoTextureHelper::textureDescription(pixelFormat);
118
119 QRhiShaderResourceBinding bindings[4];
120 auto *b = bindings;
121 *b++ = QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage,
122 uniformBuffer.get());
123 for (int i = 0; i < textureDesc->nplanes; ++i)
124 *b++ = QRhiShaderResourceBinding::sampledTexture(i + 1, QRhiShaderResourceBinding::FragmentStage,
125 videoFrameTextures->texture(i), textureSampler.get());
126 shaderResourceBindings->setBindings(bindings, b);
127 if (!shaderResourceBindings->create()) {
128 qCDebug(qLcVideoFrameConverter)
129 << Q_FUNC_INFO << ": failed to create shader resource bindings";
130 return false;
131 }
132
133 graphicsPipeline.reset(rhi->newGraphicsPipeline());
134 graphicsPipeline->setTopology(QRhiGraphicsPipeline::TriangleStrip);
135
136 QShader vs = ensureShader(QVideoTextureHelper::vertexShaderFileName(format));
137 if (!vs.isValid())
138 return false;
139
140 QShader fs = ensureShader(QVideoTextureHelper::fragmentShaderFileName(format, rhi));
141 if (!fs.isValid())
142 return false;
143
144 graphicsPipeline->setShaderStages({
145 { QRhiShaderStage::Vertex, vs },
146 { QRhiShaderStage::Fragment, fs }
147 });
148
149 QRhiVertexInputLayout inputLayout;
150 inputLayout.setBindings({
151 { 4 * sizeof(float) }
152 });
153 inputLayout.setAttributes({
154 { 0, 0, QRhiVertexInputAttribute::Float2, 0 },
155 { 0, 1, QRhiVertexInputAttribute::Float2, 2 * sizeof(float) }
156 });
157
158 graphicsPipeline->setVertexInputLayout(inputLayout);
159 graphicsPipeline->setShaderResourceBindings(shaderResourceBindings.get());
160 graphicsPipeline->setRenderPassDescriptor(renderPass.get());
161 if (!graphicsPipeline->create()) {
162 qCDebug(qLcVideoFrameConverter) << Q_FUNC_INFO << ": failed to create graphics pipeline";
163 return false;
164 }
165
166 return true;
167}
168
169static QImage convertJPEG(const QVideoFrame &frame, const VideoTransformation &transform)
170{
171 QVideoFrame varFrame = frame;
172 if (!varFrame.map(QVideoFrame::ReadOnly)) {
173 qCDebug(qLcVideoFrameConverter) << Q_FUNC_INFO << ": frame mapping failed";
174 return {};
175 }
176
177 auto unmap = std::optional(QScopeGuard([&] {
178 varFrame.unmap();
179 }));
180
181 QSpan<uchar> jpegData{
182 varFrame.bits(0),
183 varFrame.mappedBytes(0),
184 };
185
186 using namespace QtMultimediaPrivate;
187
188 constexpr std::array<uchar, 2> soiMarker{ uchar(0xff), uchar(0xd8) };
189 if (!ranges::equal(take(jpegData, 2), soiMarker, std::equal_to<void>{})) {
190 qCDebug(qLcVideoFrameConverter)
191 << Q_FUNC_INFO << ": JPEG data does not start with SOI marker";
192 return QImage{};
193 }
194
195 constexpr std::array<uchar, 2> eoiMarker{ uchar(0xff), uchar(0xd9) };
196
197 // some JPEG cameras contain extra data after the JPEG marker. If so, we drop it to make
198 // libjpeg happy.
199 if (!ranges::equal(jpegData.last(2), eoiMarker, std::equal_to<void>{})) {
200 qCDebug(qLcVideoFrameConverter)
201 << Q_FUNC_INFO << ": JPEG data does not end with EOI marker";
202
203 auto eoi_it = std::find_end(jpegData.begin(), jpegData.end(), std::begin(eoiMarker),
204 std::end(eoiMarker));
205 if (eoi_it == jpegData.end()) {
206 qCWarning(qLcVideoFrameConverter)
207 << Q_FUNC_INFO << ": JPEG data does not contain EOI marker";
208 return QImage{};
209 };
210
211 const size_t newSize = std::distance(jpegData.begin(), eoi_it) + std::size(eoiMarker);
212 jpegData = jpegData.first(newSize);
213 }
214
215 QImage image = QImage::fromData(jpegData, "JPG");
216 unmap = std::nullopt; // Release unmap guard
217 rasterTransform(image, transform);
218 return image;
219}
220
221static QImage convertCPU(const QVideoFrame &frame, const VideoTransformation &transform)
222{
223 VideoFrameConvertFunc convert = qConverterForFormat(frame.pixelFormat());
224 if (!convert) {
225 qCDebug(qLcVideoFrameConverter) << Q_FUNC_INFO << ": unsupported pixel format" << frame.pixelFormat();
226 return {};
227 } else {
228 QVideoFrame varFrame = frame;
229 if (!varFrame.map(QVideoFrame::ReadOnly)) {
230 qCDebug(qLcVideoFrameConverter) << Q_FUNC_INFO << ": frame mapping failed";
231 return {};
232 }
233 auto format = pixelFormatHasAlpha(varFrame.pixelFormat()) ? QImage::Format_ARGB32_Premultiplied : QImage::Format_RGB32;
234 QImage image = QImage(varFrame.width(), varFrame.height(), format);
235 convert(varFrame, image.bits());
236 varFrame.unmap();
237 rasterTransform(image, transform);
238 return image;
239 }
240}
241
242QImage qImageFromVideoFrame(const QVideoFrame &frame, bool forceCpu)
243{
244 // by default, surface transformation is applied, as full transformation is used for presentation only
245 return qImageFromVideoFrame(frame, qNormalizedSurfaceTransformation(frame.surfaceFormat()),
246 forceCpu);
247}
248
249QImage qImageFromVideoFrame(const QVideoFrame &frame, const VideoTransformation &transformation,
250 bool forceCpu)
251{
252#ifdef Q_OS_DARWIN
253 QMacAutoReleasePool releasePool;
254#endif
255
256 std::unique_ptr<QRhiRenderPassDescriptor> renderPass;
257 std::unique_ptr<QRhiBuffer> vertexBuffer;
258 std::unique_ptr<QRhiBuffer> uniformBuffer;
259 std::unique_ptr<QRhiTexture> targetTexture;
260 std::unique_ptr<QRhiTextureRenderTarget> renderTarget;
261 std::unique_ptr<QRhiSampler> textureSampler;
262 std::unique_ptr<QRhiShaderResourceBindings> shaderResourceBindings;
263 std::unique_ptr<QRhiGraphicsPipeline> graphicsPipeline;
264
265 if (frame.size().isEmpty() || frame.pixelFormat() == QVideoFrameFormat::Format_Invalid)
266 return {};
267
268 if (frame.pixelFormat() == QVideoFrameFormat::Format_Jpeg)
269 return convertJPEG(frame, transformation);
270
271 if (forceCpu) // For test purposes
272 return convertCPU(frame, transformation);
273
274 QRhi *rhi = nullptr;
275
276 if (QHwVideoBuffer *buffer = QVideoFramePrivate::hwBuffer(frame))
277 rhi = buffer->associatedCurrentThreadRhi();
278
279 if (!rhi) {
280 // TODO: if a case with more the one available preferred backends appears,
281 // e.g. vulkan vs opengl, then we should:
282 // 1. Implement QHwVideoBuffer::preferredRhiBackend
283 // 2. Implement a map backend=>rhi inside qEnsureThreadLocalRhi
284 rhi = qEnsureThreadLocalRhi(/*buffer->preferredRhiBackend()*/);
285 }
286
287 if (!rhi || rhi->isRecordingFrame())
288 return convertCPU(frame, transformation);
289
290 Q_ASSERT(rhi->thread()->isCurrentThread());
291
292 // Do conversion using shaders
293
294 const QSize frameSize = qRotatedFrameSize(frame.size(), frame.surfaceFormat().rotation());
295
296 vertexBuffer.reset(rhi->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(g_quad)));
297 if (!vertexBuffer->create()) {
298 qCDebug(qLcVideoFrameConverter) << "Failed to create vertex buffer. Using CPU conversion.";
299 return convertCPU(frame, transformation);
300 }
301
302 uniformBuffer.reset(rhi->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, sizeof(QVideoTextureHelper::UniformData)));
303 if (!uniformBuffer->create()) {
304 qCDebug(qLcVideoFrameConverter) << "Failed to create uniform buffer. Using CPU conversion.";
305 return convertCPU(frame, transformation);
306 }
307
308 textureSampler.reset(rhi->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
309 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge));
310 if (!textureSampler->create()) {
311 qCDebug(qLcVideoFrameConverter)
312 << "Failed to create texture sampler. Using CPU conversion.";
313 return convertCPU(frame, transformation);
314 }
315
316 shaderResourceBindings.reset(rhi->newShaderResourceBindings());
317
318 targetTexture.reset(rhi->newTexture(QRhiTexture::RGBA8, frameSize, 1, QRhiTexture::RenderTarget));
319 if (!targetTexture->create()) {
320 qCDebug(qLcVideoFrameConverter) << "Failed to create target texture. Using CPU conversion.";
321 return convertCPU(frame, transformation);
322 }
323
324 renderTarget.reset(rhi->newTextureRenderTarget({ { targetTexture.get() } }));
325 renderPass.reset(renderTarget->newCompatibleRenderPassDescriptor());
326 renderTarget->setRenderPassDescriptor(renderPass.get());
327 if (!renderTarget->create()) {
328 qCDebug(qLcVideoFrameConverter) << "Failed to create render target. Using CPU conversion.";
329 return convertCPU(frame, transformation);
330 }
331
332 QRhiCommandBuffer *cb = nullptr;
333 QRhi::FrameOpResult r = rhi->beginOffscreenFrame(&cb);
334 if (r != QRhi::FrameOpSuccess) {
335 qCDebug(qLcVideoFrameConverter) << "Failed to set up offscreen frame. Using CPU conversion.";
336 return convertCPU(frame, transformation);
337 }
338
339 QRhiResourceUpdateBatch *rub = rhi->nextResourceUpdateBatch();
340 Q_ASSERT(rub);
341
342 rub->uploadStaticBuffer(vertexBuffer.get(), g_quad);
343
344 QVideoFrame frameTmp = frame;
345 QVideoFrameTexturesUPtr texturesTmp;
346 auto videoFrameTextures = QVideoTextureHelper::createTextures(frameTmp, *rhi, *rub, texturesTmp);
347 if (!videoFrameTextures) {
348 qCDebug(qLcVideoFrameConverter) << "Failed obtain textures. Using CPU conversion.";
349 return convertCPU(frame, transformation);
350 }
351
352 if (!updateTextures(rhi, uniformBuffer, textureSampler, shaderResourceBindings,
353 graphicsPipeline, renderPass, frameTmp, videoFrameTextures)) {
354 qCDebug(qLcVideoFrameConverter) << "Failed to update textures. Using CPU conversion.";
355 return convertCPU(frame, transformation);
356 }
357
358 float xScale = transformation.mirroredHorizontallyAfterRotation ? -1.0 : 1.0;
359 float yScale = 1.f;
360
361 if (rhi->isYUpInFramebuffer())
362 yScale = -yScale;
363
364 QMatrix4x4 transform;
365 transform.scale(xScale, yScale);
366
367 QByteArray uniformData(sizeof(QVideoTextureHelper::UniformData), Qt::Uninitialized);
368 QVideoTextureHelper::updateUniformData(&uniformData, rhi, frame.surfaceFormat(), frame,
369 transform, 1.f);
370 rub->updateDynamicBuffer(uniformBuffer.get(), 0, uniformData.size(), uniformData.constData());
371
372 cb->beginPass(renderTarget.get(), Qt::black, { 1.0f, 0 }, rub);
373 cb->setGraphicsPipeline(graphicsPipeline.get());
374
375 cb->setViewport({ 0, 0, float(frameSize.width()), float(frameSize.height()) });
376 cb->setShaderResources(shaderResourceBindings.get());
377
378 const quint32 vertexOffset = quint32(sizeof(float)) * 16 * transformation.rotationIndex();
379 const QRhiCommandBuffer::VertexInput vbufBinding(vertexBuffer.get(), vertexOffset);
380 cb->setVertexInput(0, 1, &vbufBinding);
381 cb->draw(4);
382
383 QRhiReadbackDescription readDesc(targetTexture.get());
384 QRhiReadbackResult readResult;
385 bool readCompleted = false;
386
387 readResult.completed = [&readCompleted] { readCompleted = true; };
388
389 rub = rhi->nextResourceUpdateBatch();
390 rub->readBackTexture(readDesc, &readResult);
391
392 cb->endPass(rub);
393
394 rhi->endOffscreenFrame();
395
396 if (!readCompleted) {
397 qCDebug(qLcVideoFrameConverter) << "Failed to read back texture. Using CPU conversion.";
398 return convertCPU(frame, transformation);
399 }
400
401 QByteArray *imageData = new QByteArray(readResult.data);
402
403 return QImage(reinterpret_cast<const uchar *>(imageData->constData()),
404 readResult.pixelSize.width(), readResult.pixelSize.height(),
405 QImage::Format_RGBA8888_Premultiplied, imageCleanupHandler, imageData);
406}
407
408QImage videoFramePlaneAsImage(QVideoFrame &frame, int plane, QImage::Format targetFormat,
409 QSize targetSize)
410{
411 if (plane >= frame.planeCount())
412 return {};
413
414 if (!frame.map(QVideoFrame::ReadOnly)) {
415 qWarning() << "Cannot map a video frame in ReadOnly mode!";
416 return {};
417 }
418
419 auto frameHandle = QVideoFramePrivate::handle(frame);
420
421 // With incrementing the reference counter, we share the mapped QVideoFrame
422 // with the target QImage. The function imageCleanupFunction is going to adopt
423 // the frameHandle by QVideoFrame and dereference it upon the destruction.
424 frameHandle->ref.ref();
425
426 auto imageCleanupFunction = [](void *data) {
427 QVideoFrame frame = reinterpret_cast<QVideoFramePrivate *>(data)->adoptThisByVideoFrame();
428 Q_ASSERT(frame.isMapped());
429 frame.unmap();
430 };
431
432 const auto bytesPerLine = frame.bytesPerLine(plane);
433 const auto height =
434 bytesPerLine ? qMin(targetSize.height(), frame.mappedBytes(plane) / bytesPerLine) : 0;
435
436 return QImage(reinterpret_cast<const uchar *>(frame.bits(plane)), targetSize.width(), height,
437 bytesPerLine, targetFormat, imageCleanupFunction, frameHandle);
438}
439
440QT_END_NAMESPACE
\inmodule QtGui
Definition qimage.h:38
\inmodule QtGuiPrivate \inheaderfile rhi/qrhi.h
Definition qrhi.h:787
\inmodule QtGuiPrivate \inheaderfile rhi/qrhi.h
Definition qrhi.h:441
\inmodule QtGuiPrivate \inheaderfile rhi/qrhi.h
Definition qrhi.h:323
\inmodule QtGui
Definition qshader.h:81
static QVideoFramePrivate * handle(QVideoFrame &frame)
Combined button and popup list for selecting options.
QT_BEGIN_NAMESPACE Q_STATIC_LOGGING_CATEGORY(lcSynthesizedIterableAccess, "qt.iterable.synthesized", QtWarningMsg)
static bool updateTextures(QRhi *rhi, std::unique_ptr< QRhiBuffer > &uniformBuffer, std::unique_ptr< QRhiSampler > &textureSampler, std::unique_ptr< QRhiShaderResourceBindings > &shaderResourceBindings, std::unique_ptr< QRhiGraphicsPipeline > &graphicsPipeline, std::unique_ptr< QRhiRenderPassDescriptor > &renderPass, QVideoFrame &frame, const QVideoFrameTexturesUPtr &videoFrameTextures)
static QImage convertJPEG(const QVideoFrame &frame, const VideoTransformation &transform)
static QT_BEGIN_NAMESPACE constexpr float g_quad[]
QImage qImageFromVideoFrame(const QVideoFrame &frame, const VideoTransformation &transformation, bool forceCpu)
QImage qImageFromVideoFrame(const QVideoFrame &frame, bool forceCpu)
static QShader ensureShader(const QString &name)
static bool pixelFormatHasAlpha(QVideoFrameFormat::PixelFormat format)
static QImage convertCPU(const QVideoFrame &frame, const VideoTransformation &transform)
static void rasterTransform(QImage &image, VideoTransformation transformation)
QImage videoFramePlaneAsImage(QVideoFrame &frame, int plane, QImage::Format targetFormat, QSize targetSize)
Maps the video frame and returns an image having a shared ownership for the video frame and referenci...
static void imageCleanupHandler(void *info)