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,
112 const QVideoFrameTexturesUPtr &videoFrameTextures)
114 auto format = frame.surfaceFormat();
115 auto pixelFormat = format.pixelFormat();
117 auto textureDesc = QVideoTextureHelper::textureDescription(pixelFormat);
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";
133 graphicsPipeline.reset(rhi->newGraphicsPipeline());
134 graphicsPipeline->setTopology(QRhiGraphicsPipeline::TriangleStrip);
136 QShader vs = ensureShader(QVideoTextureHelper::vertexShaderFileName(format));
140 QShader fs = ensureShader(QVideoTextureHelper::fragmentShaderFileName(format, rhi));
144 graphicsPipeline->setShaderStages({
145 { QRhiShaderStage::Vertex, vs },
146 { QRhiShaderStage::Fragment, fs }
150 inputLayout.setBindings({
151 { 4 *
sizeof(
float) }
153 inputLayout.setAttributes({
154 { 0, 0, QRhiVertexInputAttribute::Float2, 0 },
155 { 0, 1, QRhiVertexInputAttribute::Float2, 2 *
sizeof(
float) }
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";
171 Q_ASSERT(mappedFrame.isReadable());
172 Q_ASSERT(mappedFrame.pixelFormat() == QVideoFrameFormat::PixelFormat::Format_Jpeg);
174 QSpan<
const uchar> jpegData{
176 mappedFrame.mappedBytes(0),
181 constexpr std::array<uchar, 2> soiMarker{ uchar(0xff), uchar(0xd8) };
182 if (!ranges::equal(take(jpegData, 2), soiMarker, std::equal_to<
void>{})) {
183 qCDebug(qLcVideoFrameConverter)
184 << Q_FUNC_INFO <<
": JPEG data does not start with SOI marker";
188 constexpr std::array<uchar, 2> eoiMarker{ uchar(0xff), uchar(0xd9) };
192 if (!ranges::equal(jpegData.last(2), eoiMarker, std::equal_to<
void>{})) {
193 qCDebug(qLcVideoFrameConverter)
194 << Q_FUNC_INFO <<
": JPEG data does not end with EOI marker";
196 auto eoi_it = std::find_end(jpegData.begin(), jpegData.end(), std::begin(eoiMarker),
197 std::end(eoiMarker));
198 if (eoi_it == jpegData.end()) {
199 qCWarning(qLcVideoFrameConverter)
200 << Q_FUNC_INFO <<
": JPEG data does not contain EOI marker";
204 const size_t newSize =
std::distance(jpegData.begin(), eoi_it) +
std::size(eoiMarker);
205 jpegData = jpegData.first(newSize);
265 QMacAutoReleasePool releasePool;
268 std::unique_ptr<QRhiRenderPassDescriptor> renderPass;
269 std::unique_ptr<QRhiBuffer> vertexBuffer;
270 std::unique_ptr<QRhiBuffer> uniformBuffer;
271 std::unique_ptr<QRhiTexture> targetTexture;
272 std::unique_ptr<QRhiTextureRenderTarget> renderTarget;
273 std::unique_ptr<QRhiSampler> textureSampler;
274 std::unique_ptr<QRhiShaderResourceBindings> shaderResourceBindings;
275 std::unique_ptr<QRhiGraphicsPipeline> graphicsPipeline;
277 if (frame.size().isEmpty() || frame.pixelFormat() == QVideoFrameFormat::Format_Invalid)
280 if (frame.pixelFormat() == QVideoFrameFormat::Format_Jpeg)
281 return convertJPEG(frame, transformation);
284 return convertCPU(frame, transformation);
288 if (QHwVideoBuffer *buffer = QVideoFramePrivate::hwBuffer(frame))
289 rhi = buffer->associatedCurrentThreadRhi();
296 rhi = qEnsureThreadLocalRhi();
299 if (!rhi || rhi->isRecordingFrame())
300 return convertCPU(frame, transformation);
302 Q_ASSERT(rhi->thread()->isCurrentThread());
306 const QSize frameSize = qRotatedFrameSize(frame.size(), frame.surfaceFormat().rotation());
308 vertexBuffer.reset(rhi->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer,
sizeof(
g_quad)));
309 if (!vertexBuffer->create()) {
310 qCDebug(qLcVideoFrameConverter) <<
"Failed to create vertex buffer. Using CPU conversion.";
311 return convertCPU(frame, transformation);
314 uniformBuffer.reset(rhi->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer,
sizeof(QVideoTextureHelper::UniformData)));
315 if (!uniformBuffer->create()) {
316 qCDebug(qLcVideoFrameConverter) <<
"Failed to create uniform buffer. Using CPU conversion.";
317 return convertCPU(frame, transformation);
320 textureSampler.reset(rhi->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
321 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge));
322 if (!textureSampler->create()) {
323 qCDebug(qLcVideoFrameConverter)
324 <<
"Failed to create texture sampler. Using CPU conversion.";
325 return convertCPU(frame, transformation);
328 shaderResourceBindings.reset(rhi->newShaderResourceBindings());
330 targetTexture.reset(rhi->newTexture(QRhiTexture::RGBA8, frameSize, 1, QRhiTexture::RenderTarget));
331 if (!targetTexture->create()) {
332 qCDebug(qLcVideoFrameConverter) <<
"Failed to create target texture. Using CPU conversion.";
333 return convertCPU(frame, transformation);
336 renderTarget.reset(rhi->newTextureRenderTarget({ { targetTexture.get() } }));
337 renderPass.reset(renderTarget->newCompatibleRenderPassDescriptor());
338 renderTarget->setRenderPassDescriptor(renderPass.get());
339 if (!renderTarget->create()) {
340 qCDebug(qLcVideoFrameConverter) <<
"Failed to create render target. Using CPU conversion.";
341 return convertCPU(frame, transformation);
344 QRhiCommandBuffer *cb =
nullptr;
345 QRhi::FrameOpResult r = rhi->beginOffscreenFrame(&cb);
346 if (r != QRhi::FrameOpSuccess) {
347 qCDebug(qLcVideoFrameConverter) <<
"Failed to set up offscreen frame. Using CPU conversion.";
348 return convertCPU(frame, transformation);
351 QRhiResourceUpdateBatch *rub = rhi->nextResourceUpdateBatch();
354 rub->uploadStaticBuffer(vertexBuffer.get(),
g_quad);
356 QVideoFrame frameTmp = frame;
357 QVideoFrameTexturesUPtr texturesTmp;
358 auto videoFrameTextures = QVideoTextureHelper::createTextures(frameTmp, *rhi, *rub, texturesTmp);
359 if (!videoFrameTextures) {
360 qCDebug(qLcVideoFrameConverter) <<
"Failed obtain textures. Using CPU conversion.";
361 return convertCPU(frame, transformation);
364 if (!updateTextures(rhi, uniformBuffer, textureSampler, shaderResourceBindings,
365 graphicsPipeline, renderPass, frameTmp, videoFrameTextures)) {
366 qCDebug(qLcVideoFrameConverter) <<
"Failed to update textures. Using CPU conversion.";
367 return convertCPU(frame, transformation);
370 float xScale = transformation.mirroredHorizontallyAfterRotation ? -1.0 : 1.0;
373 if (rhi->isYUpInFramebuffer())
376 QMatrix4x4 transform;
377 transform.scale(xScale, yScale);
379 QByteArray uniformData(
sizeof(QVideoTextureHelper::UniformData), Qt::Uninitialized);
380 QVideoTextureHelper::updateUniformData(&uniformData, rhi, frame.surfaceFormat(), frame,
382 rub->updateDynamicBuffer(uniformBuffer.get(), 0, uniformData.size(), uniformData.constData());
384 cb->beginPass(renderTarget.get(), Qt::black, { 1.0f, 0 }, rub);
385 cb->setGraphicsPipeline(graphicsPipeline.get());
387 cb->setViewport({ 0, 0,
float(frameSize.width()),
float(frameSize.height()) });
388 cb->setShaderResources(shaderResourceBindings.get());
390 const quint32 vertexOffset = quint32(
sizeof(
float)) * 16 * transformation.rotationIndex();
391 const QRhiCommandBuffer::VertexInput vbufBinding(vertexBuffer.get(), vertexOffset);
392 cb->setVertexInput(0, 1, &vbufBinding);
396 QRhiReadbackResult readResult;
397 bool readCompleted =
false;
399 readResult.completed = [&readCompleted] { readCompleted =
true; };
401 rub = rhi->nextResourceUpdateBatch();
402 rub->readBackTexture(readDesc, &readResult);
406 rhi->endOffscreenFrame();
408 if (!readCompleted) {
409 qCDebug(qLcVideoFrameConverter) <<
"Failed to read back texture. Using CPU conversion.";
410 return convertCPU(frame, transformation);
413 QByteArray *imageData =
new QByteArray(readResult.data);
415 return QImage(
reinterpret_cast<
const uchar *>(imageData->constData()),
416 readResult.pixelSize.width(), readResult.pixelSize.height(),