110 std::unique_ptr<QRhiBuffer> &uniformBuffer,
111 std::unique_ptr<QRhiSampler> &textureSampler,
112 std::unique_ptr<QRhiShaderResourceBindings> &shaderResourceBindings,
113 std::unique_ptr<QRhiGraphicsPipeline> &graphicsPipeline,
114 std::unique_ptr<QRhiRenderPassDescriptor> &renderPass,
116 const QVideoFrameTexturesUPtr &videoFrameTextures)
118 auto format = frame.surfaceFormat();
119 auto pixelFormat = format.pixelFormat();
121 auto textureDesc = QVideoTextureHelper::textureDescription(pixelFormat);
125 *b++ = QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage,
126 uniformBuffer.get());
127 for (
int i = 0; i < textureDesc->nplanes; ++i)
128 *b++ = QRhiShaderResourceBinding::sampledTexture(i + 1, QRhiShaderResourceBinding::FragmentStage,
129 videoFrameTextures->texture(i), textureSampler.get());
130 shaderResourceBindings->setBindings(bindings, b);
131 if (!shaderResourceBindings->create()) {
132 qCDebug(qLcVideoFrameConverter)
133 << Q_FUNC_INFO <<
": failed to create shader resource bindings";
137 graphicsPipeline.reset(rhi->newGraphicsPipeline());
138 graphicsPipeline->setTopology(QRhiGraphicsPipeline::TriangleStrip);
140 QShader vs = ensureShader(QVideoTextureHelper::vertexShaderFileName(format));
144 QShader fs = ensureShader(QVideoTextureHelper::fragmentShaderFileName(format, rhi));
148 graphicsPipeline->setShaderStages({
149 { QRhiShaderStage::Vertex, vs },
150 { QRhiShaderStage::Fragment, fs }
154 inputLayout.setBindings({
155 { 4 *
sizeof(
float) }
157 inputLayout.setAttributes({
158 { 0, 0, QRhiVertexInputAttribute::Float2, 0 },
159 { 0, 1, QRhiVertexInputAttribute::Float2, 2 *
sizeof(
float) }
162 graphicsPipeline->setVertexInputLayout(inputLayout);
163 graphicsPipeline->setShaderResourceBindings(shaderResourceBindings.get());
164 graphicsPipeline->setRenderPassDescriptor(renderPass.get());
165 if (!graphicsPipeline->create()) {
166 qCDebug(qLcVideoFrameConverter) << Q_FUNC_INFO <<
": failed to create graphics pipeline";
175 Q_ASSERT(mappedFrame.isReadable());
176 Q_ASSERT(mappedFrame.pixelFormat() == QVideoFrameFormat::PixelFormat::Format_Jpeg);
178 QSpan<
const uchar> jpegData{
180 mappedFrame.mappedBytes(0),
185 constexpr std::array<uchar, 2> soiMarker{ uchar(0xff), uchar(0xd8) };
186 if (!ranges::equal(take(jpegData, 2), soiMarker, std::equal_to<
void>{})) {
187 qCDebug(qLcVideoFrameConverter)
188 << Q_FUNC_INFO <<
": JPEG data does not start with SOI marker";
192 constexpr std::array<uchar, 2> eoiMarker{ uchar(0xff), uchar(0xd9) };
196 if (!ranges::equal(jpegData.last(2), eoiMarker, std::equal_to<
void>{})) {
197 qCDebug(qLcVideoFrameConverter)
198 << Q_FUNC_INFO <<
": JPEG data does not end with EOI marker";
200 auto eoi_it = std::find_end(jpegData.begin(), jpegData.end(), std::begin(eoiMarker),
201 std::end(eoiMarker));
202 if (eoi_it == jpegData.end()) {
203 qCWarning(qLcVideoFrameConverter)
204 << Q_FUNC_INFO <<
": JPEG data does not contain EOI marker";
208 const size_t newSize =
std::distance(jpegData.begin(), eoi_it) +
std::size(eoiMarker);
209 jpegData = jpegData.first(newSize);
269 QMacAutoReleasePool releasePool;
272 std::unique_ptr<QRhiRenderPassDescriptor> renderPass;
273 std::unique_ptr<QRhiBuffer> vertexBuffer;
274 std::unique_ptr<QRhiBuffer> uniformBuffer;
275 std::unique_ptr<QRhiTexture> targetTexture;
276 std::unique_ptr<QRhiTextureRenderTarget> renderTarget;
277 std::unique_ptr<QRhiSampler> textureSampler;
278 std::unique_ptr<QRhiShaderResourceBindings> shaderResourceBindings;
279 std::unique_ptr<QRhiGraphicsPipeline> graphicsPipeline;
281 if (frame.size().isEmpty() || frame.pixelFormat() == QVideoFrameFormat::Format_Invalid)
284 if (frame.pixelFormat() == QVideoFrameFormat::Format_Jpeg)
285 return convertJPEG(frame, transformation);
288 return convertCPU(frame, transformation);
292 if (QHwVideoBuffer *buffer = QVideoFramePrivate::hwBuffer(frame))
293 rhi = buffer->associatedCurrentThreadRhi();
300 rhi = qEnsureThreadLocalRhi();
303 if (!rhi || rhi->isRecordingFrame())
304 return convertCPU(frame, transformation);
306 Q_ASSERT(rhi->thread()->isCurrentThread());
310 const QSize frameSize = qRotatedFrameSize(frame.size(), frame.surfaceFormat().rotation());
312 vertexBuffer.reset(rhi->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer,
sizeof(
g_quad)));
313 if (!vertexBuffer->create()) {
314 qCDebug(qLcVideoFrameConverter) <<
"Failed to create vertex buffer. Using CPU conversion.";
315 return convertCPU(frame, transformation);
318 uniformBuffer.reset(rhi->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer,
sizeof(QVideoTextureHelper::UniformData)));
319 if (!uniformBuffer->create()) {
320 qCDebug(qLcVideoFrameConverter) <<
"Failed to create uniform buffer. Using CPU conversion.";
321 return convertCPU(frame, transformation);
324 textureSampler.reset(rhi->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
325 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge));
326 if (!textureSampler->create()) {
327 qCDebug(qLcVideoFrameConverter)
328 <<
"Failed to create texture sampler. Using CPU conversion.";
329 return convertCPU(frame, transformation);
332 shaderResourceBindings.reset(rhi->newShaderResourceBindings());
334 targetTexture.reset(rhi->newTexture(QRhiTexture::RGBA8, frameSize, 1, QRhiTexture::RenderTarget));
335 if (!targetTexture->create()) {
336 qCDebug(qLcVideoFrameConverter) <<
"Failed to create target texture. Using CPU conversion.";
337 return convertCPU(frame, transformation);
340 renderTarget.reset(rhi->newTextureRenderTarget({ { targetTexture.get() } }));
341 renderPass.reset(renderTarget->newCompatibleRenderPassDescriptor());
342 renderTarget->setRenderPassDescriptor(renderPass.get());
343 if (!renderTarget->create()) {
344 qCDebug(qLcVideoFrameConverter) <<
"Failed to create render target. Using CPU conversion.";
345 return convertCPU(frame, transformation);
348 QRhiCommandBuffer *cb =
nullptr;
349 QRhi::FrameOpResult r = rhi->beginOffscreenFrame(&cb);
350 if (r != QRhi::FrameOpSuccess) {
351 qCDebug(qLcVideoFrameConverter) <<
"Failed to set up offscreen frame. Using CPU conversion.";
352 return convertCPU(frame, transformation);
355 QRhiResourceUpdateBatch *rub = rhi->nextResourceUpdateBatch();
358 rub->uploadStaticBuffer(vertexBuffer.get(),
g_quad);
360 QVideoFrame frameTmp = frame;
361 QVideoFrameTexturesUPtr texturesTmp;
362 auto videoFrameTextures = QVideoTextureHelper::createTextures(frameTmp, *rhi, *rub, texturesTmp);
363 if (!videoFrameTextures) {
364 qCDebug(qLcVideoFrameConverter) <<
"Failed obtain textures. Using CPU conversion.";
365 return convertCPU(frame, transformation);
368 if (!updateTextures(rhi, uniformBuffer, textureSampler, shaderResourceBindings,
369 graphicsPipeline, renderPass, frameTmp, videoFrameTextures)) {
370 qCDebug(qLcVideoFrameConverter) <<
"Failed to update textures. Using CPU conversion.";
371 return convertCPU(frame, transformation);
374 float xScale = transformation.mirroredHorizontallyAfterRotation ? -1.0 : 1.0;
381 const bool flipForFramebufferReadback = rhi->isYUpInFramebuffer();
382 const bool flipForClipSpaceConvention = !rhi->isYUpInNDC();
383 float yScale = (flipForFramebufferReadback != flipForClipSpaceConvention) ? -1.f : 1.f;
385 QMatrix4x4 transform;
386 transform.scale(xScale, yScale);
388 QByteArray uniformData(
sizeof(QVideoTextureHelper::UniformData), Qt::Uninitialized);
389 QVideoTextureHelper::updateUniformData(&uniformData, rhi, frame.surfaceFormat(), frame,
391 rub->updateDynamicBuffer(uniformBuffer.get(), 0, uniformData.size(), uniformData.constData());
393 cb->beginPass(renderTarget.get(), Qt::black, { 1.0f, 0 }, rub);
394 cb->setGraphicsPipeline(graphicsPipeline.get());
396 cb->setViewport({ 0, 0,
float(frameSize.width()),
float(frameSize.height()) });
397 cb->setShaderResources(shaderResourceBindings.get());
399 const quint32 vertexOffset = quint32(
sizeof(
float)) * 16 * transformation.rotationIndex();
400 const QRhiCommandBuffer::VertexInput vbufBinding(vertexBuffer.get(), vertexOffset);
401 cb->setVertexInput(0, 1, &vbufBinding);
406 bool readCompleted =
false;
408 readResult.completed = [&readCompleted] { readCompleted =
true; };
410 rub = rhi->nextResourceUpdateBatch();
411 rub->readBackTexture(readDesc, &readResult);
415 rhi->endOffscreenFrame();
417 if (!readCompleted) {
418 qCDebug(qLcVideoFrameConverter) <<
"Failed to read back texture. Using CPU conversion.";
419 return convertCPU(frame, transformation);
422 QByteArray *imageData =
new QByteArray(readResult.data);
424 return QImage(
reinterpret_cast<
const uchar *>(imageData->constData()),
425 readResult.pixelSize.width(), readResult.pixelSize.height(),