105 std::unique_ptr<QRhiBuffer> &uniformBuffer,
106 std::unique_ptr<QRhiSampler> &textureSampler,
107 std::unique_ptr<QRhiShaderResourceBindings> &shaderResourceBindings,
108 std::unique_ptr<QRhiGraphicsPipeline> &graphicsPipeline,
109 std::unique_ptr<QRhiRenderPassDescriptor> &renderPass,
111 const QVideoFrameTexturesUPtr &videoFrameTextures)
113 auto format = frame.surfaceFormat();
114 auto pixelFormat = format.pixelFormat();
116 auto textureDesc = QVideoTextureHelper::textureDescription(pixelFormat);
120 *b++ = QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage,
121 uniformBuffer.get());
122 for (
int i = 0; i < textureDesc->nplanes; ++i)
123 *b++ = QRhiShaderResourceBinding::sampledTexture(i + 1, QRhiShaderResourceBinding::FragmentStage,
124 videoFrameTextures->texture(i), textureSampler.get());
125 shaderResourceBindings->setBindings(bindings, b);
126 if (!shaderResourceBindings->create()) {
127 qCDebug(qLcVideoFrameConverter)
128 << Q_FUNC_INFO <<
": failed to create shader resource bindings";
132 graphicsPipeline.reset(rhi->newGraphicsPipeline());
133 graphicsPipeline->setTopology(QRhiGraphicsPipeline::TriangleStrip);
135 QShader vs = ensureShader(QVideoTextureHelper::vertexShaderFileName(format));
139 QShader fs = ensureShader(QVideoTextureHelper::fragmentShaderFileName(format, rhi));
143 graphicsPipeline->setShaderStages({
144 { QRhiShaderStage::Vertex, vs },
145 { QRhiShaderStage::Fragment, fs }
149 inputLayout.setBindings({
150 { 4 *
sizeof(
float) }
152 inputLayout.setAttributes({
153 { 0, 0, QRhiVertexInputAttribute::Float2, 0 },
154 { 0, 1, QRhiVertexInputAttribute::Float2, 2 *
sizeof(
float) }
157 graphicsPipeline->setVertexInputLayout(inputLayout);
158 graphicsPipeline->setShaderResourceBindings(shaderResourceBindings.get());
159 graphicsPipeline->setRenderPassDescriptor(renderPass.get());
160 if (!graphicsPipeline->create()) {
161 qCDebug(qLcVideoFrameConverter) << Q_FUNC_INFO <<
": failed to create graphics pipeline";
170 QVideoFrame varFrame = frame;
171 if (!varFrame.map(QVideoFrame::ReadOnly)) {
172 qCDebug(qLcVideoFrameConverter) << Q_FUNC_INFO <<
": frame mapping failed";
176 auto unmap =
std::optional(QScopeGuard([&] {
180 QSpan<uchar> jpegData{
182 varFrame.mappedBytes(0),
185 constexpr std::array<uchar, 2> soiMarker{ uchar(0xff), uchar(0xd8) };
186 if (!ranges::equal(jpegData.first(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);
212 QImage image = QImage::fromData(jpegData,
"JPG");
213 unmap =
std::nullopt;
250 QMacAutoReleasePool releasePool;
253 std::unique_ptr<QRhiRenderPassDescriptor> renderPass;
254 std::unique_ptr<QRhiBuffer> vertexBuffer;
255 std::unique_ptr<QRhiBuffer> uniformBuffer;
256 std::unique_ptr<QRhiTexture> targetTexture;
257 std::unique_ptr<QRhiTextureRenderTarget> renderTarget;
258 std::unique_ptr<QRhiSampler> textureSampler;
259 std::unique_ptr<QRhiShaderResourceBindings> shaderResourceBindings;
260 std::unique_ptr<QRhiGraphicsPipeline> graphicsPipeline;
262 if (frame.size().isEmpty() || frame.pixelFormat() == QVideoFrameFormat::Format_Invalid)
265 if (frame.pixelFormat() == QVideoFrameFormat::Format_Jpeg)
266 return convertJPEG(frame, transformation);
269 return convertCPU(frame, transformation);
273 if (QHwVideoBuffer *buffer = QVideoFramePrivate::hwBuffer(frame))
274 rhi = buffer->associatedCurrentThreadRhi();
281 rhi = qEnsureThreadLocalRhi();
284 if (!rhi || rhi->isRecordingFrame())
285 return convertCPU(frame, transformation);
287 Q_ASSERT(rhi->thread()->isCurrentThread());
291 const QSize frameSize = qRotatedFrameSize(frame.size(), frame.surfaceFormat().rotation());
293 vertexBuffer.reset(rhi->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer,
sizeof(g_quad)));
294 if (!vertexBuffer->create()) {
295 qCDebug(qLcVideoFrameConverter) <<
"Failed to create vertex buffer. Using CPU conversion.";
296 return convertCPU(frame, transformation);
299 uniformBuffer.reset(rhi->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer,
sizeof(QVideoTextureHelper::UniformData)));
300 if (!uniformBuffer->create()) {
301 qCDebug(qLcVideoFrameConverter) <<
"Failed to create uniform buffer. Using CPU conversion.";
302 return convertCPU(frame, transformation);
305 textureSampler.reset(rhi->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
306 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge));
307 if (!textureSampler->create()) {
308 qCDebug(qLcVideoFrameConverter)
309 <<
"Failed to create texture sampler. Using CPU conversion.";
310 return convertCPU(frame, transformation);
313 shaderResourceBindings.reset(rhi->newShaderResourceBindings());
315 targetTexture.reset(rhi->newTexture(QRhiTexture::RGBA8, frameSize, 1, QRhiTexture::RenderTarget));
316 if (!targetTexture->create()) {
317 qCDebug(qLcVideoFrameConverter) <<
"Failed to create target texture. Using CPU conversion.";
318 return convertCPU(frame, transformation);
321 renderTarget.reset(rhi->newTextureRenderTarget({ { targetTexture.get() } }));
322 renderPass.reset(renderTarget->newCompatibleRenderPassDescriptor());
323 renderTarget->setRenderPassDescriptor(renderPass.get());
324 if (!renderTarget->create()) {
325 qCDebug(qLcVideoFrameConverter) <<
"Failed to create render target. Using CPU conversion.";
326 return convertCPU(frame, transformation);
329 QRhiCommandBuffer *cb =
nullptr;
330 QRhi::FrameOpResult r = rhi->beginOffscreenFrame(&cb);
331 if (r != QRhi::FrameOpSuccess) {
332 qCDebug(qLcVideoFrameConverter) <<
"Failed to set up offscreen frame. Using CPU conversion.";
333 return convertCPU(frame, transformation);
336 QRhiResourceUpdateBatch *rub = rhi->nextResourceUpdateBatch();
339 rub->uploadStaticBuffer(vertexBuffer.get(),
g_quad);
341 QVideoFrame frameTmp = frame;
342 QVideoFrameTexturesUPtr texturesTmp;
343 auto videoFrameTextures = QVideoTextureHelper::createTextures(frameTmp, *rhi, *rub, texturesTmp);
344 if (!videoFrameTextures) {
345 qCDebug(qLcVideoFrameConverter) <<
"Failed obtain textures. Using CPU conversion.";
346 return convertCPU(frame, transformation);
349 if (!updateTextures(rhi, uniformBuffer, textureSampler, shaderResourceBindings,
350 graphicsPipeline, renderPass, frameTmp, videoFrameTextures)) {
351 qCDebug(qLcVideoFrameConverter) <<
"Failed to update textures. Using CPU conversion.";
352 return convertCPU(frame, transformation);
355 float xScale = transformation.mirroredHorizontallyAfterRotation ? -1.0 : 1.0;
358 if (rhi->isYUpInFramebuffer())
361 QMatrix4x4 transform;
362 transform.scale(xScale, yScale);
364 QByteArray uniformData(
sizeof(QVideoTextureHelper::UniformData), Qt::Uninitialized);
365 QVideoTextureHelper::updateUniformData(&uniformData, rhi, frame.surfaceFormat(), frame,
367 rub->updateDynamicBuffer(uniformBuffer.get(), 0, uniformData.size(), uniformData.constData());
369 cb->beginPass(renderTarget.get(), Qt::black, { 1.0f, 0 }, rub);
370 cb->setGraphicsPipeline(graphicsPipeline.get());
372 cb->setViewport({ 0, 0,
float(frameSize.width()),
float(frameSize.height()) });
373 cb->setShaderResources(shaderResourceBindings.get());
375 const quint32 vertexOffset = quint32(
sizeof(
float)) * 16 * transformation.rotationIndex();
376 const QRhiCommandBuffer::VertexInput vbufBinding(vertexBuffer.get(), vertexOffset);
377 cb->setVertexInput(0, 1, &vbufBinding);
381 QRhiReadbackResult readResult;
382 bool readCompleted =
false;
384 readResult.completed = [&readCompleted] { readCompleted =
true; };
386 rub = rhi->nextResourceUpdateBatch();
387 rub->readBackTexture(readDesc, &readResult);
391 rhi->endOffscreenFrame();
393 if (!readCompleted) {
394 qCDebug(qLcVideoFrameConverter) <<
"Failed to read back texture. Using CPU conversion.";
395 return convertCPU(frame, transformation);
398 QByteArray *imageData =
new QByteArray(readResult.data);
400 return QImage(
reinterpret_cast<
const uchar *>(imageData->constData()),
401 readResult.pixelSize.width(), readResult.pixelSize.height(),