33 [](
int,
int) {
return 0; },
35 { { 1, 1 }, { 1, 1 }, { 1, 1 } }
41 { { 1, 1 }, { 1, 1 }, { 1, 1 } }
47 { { 1, 1 }, { 1, 1 }, { 1, 1 } }
53 { { 1, 1 }, { 1, 1 }, { 1, 1 } }
59 { { 1, 1 }, { 1, 1 }, { 1, 1 } }
65 { { 1, 1 }, { 1, 1 }, { 1, 1 } }
71 { { 1, 1 }, { 1, 1 }, { 1, 1 } }
77 { { 1, 1 }, { 1, 1 }, { 1, 1 } }
83 { { 1, 1 }, { 1, 1 }, { 1, 1 } }
89 { { 1, 1 }, { 1, 1 }, { 1, 1 } }
95 { { 1, 1 }, { 1, 1 }, { 1, 1 } }
101 { { 1, 1 }, { 1, 1 }, { 1, 1 } }
107 { { 1, 1 }, { 1, 1 }, { 1, 1 } }
113 { { 1, 1 }, { 2, 2 }, { 2, 2 } }
119 { { 1, 1 }, { 2, 1 }, { 2, 1 } }
125 { { 1, 1 }, { 2, 2 }, { 2, 2 } }
131 { { 2, 1 }, { 1, 1 }, { 1, 1 } }
137 { { 2, 1 }, { 1, 1 }, { 1, 1 } }
143 { { 1, 1 }, { 2, 2 }, { 1, 1 } }
149 { { 1, 1 }, { 2, 2 }, { 1, 1 } }
156 h += 2*(((
h/2) + 15) & ~15);
160 { { 1, 1 }, { 2, 2 }, { 2, 2 } }
166 { { 1, 1 }, { 1, 2 }, { 1, 1 } }
173 h += 2*(((
h/2) + 15) & ~15);
177 { { 1, 1 }, { 2, 2 }, { 2, 2 } }
183 { { 1, 1 }, { 1, 2 }, { 1, 1 } }
189 { { 1, 1 }, { 1, 1 }, { 1, 1 } }
195 { { 1, 1 }, { 1, 1 }, { 1, 1 } }
201 { { 1, 1 }, { 2, 2 }, { 1, 1 } }
207 { { 1, 1 }, { 2, 2 }, { 1, 1 } }
212 [](
int,
int) {
return 0; },
214 { { 1, 1 }, { 1, 1 }, { 1, 1 } }
220 { { 1, 1 }, { 1, 1 }, { 1, 1 } }
225 [](
int,
int) {
return 0; },
227 { { 1, 1 }, { 1, 1 }, { 1, 1 } }
233 { { 1, 1 }, { 2, 2 }, { 2, 2 } }
342QString fragmentShaderFileName(
const QVideoFrameFormat &format, QRhi *,
343 QRhiSwapChain::Format surfaceFormat)
346 switch (format.pixelFormat()) {
347 case QVideoFrameFormat::Format_Y8:
348 shaderFile = QStringLiteral(
"y");
350 case QVideoFrameFormat::Format_Y16:
351 shaderFile = QStringLiteral(
"y16");
353 case QVideoFrameFormat::Format_AYUV:
354 case QVideoFrameFormat::Format_AYUV_Premultiplied:
355 shaderFile = QStringLiteral(
"ayuv");
357 case QVideoFrameFormat::Format_ARGB8888:
358 case QVideoFrameFormat::Format_ARGB8888_Premultiplied:
359 case QVideoFrameFormat::Format_XRGB8888:
360 shaderFile = QStringLiteral(
"argb");
362 case QVideoFrameFormat::Format_ABGR8888:
363 case QVideoFrameFormat::Format_XBGR8888:
364 shaderFile = QStringLiteral(
"abgr");
366 case QVideoFrameFormat::Format_Jpeg:
367 shaderFile = QStringLiteral(
"bgra");
369 case QVideoFrameFormat::Format_RGBA8888:
370 case QVideoFrameFormat::Format_RGBX8888:
371 case QVideoFrameFormat::Format_BGRA8888:
372 case QVideoFrameFormat::Format_BGRA8888_Premultiplied:
373 case QVideoFrameFormat::Format_BGRX8888:
374 shaderFile = QStringLiteral(
"rgba");
376 case QVideoFrameFormat::Format_YUV420P:
377 case QVideoFrameFormat::Format_YUV422P:
378 case QVideoFrameFormat::Format_IMC3:
379 shaderFile = QStringLiteral(
"yuv_triplanar");
381 case QVideoFrameFormat::Format_YUV420P10:
382 shaderFile = QStringLiteral(
"yuv_triplanar_p10");
384 case QVideoFrameFormat::Format_YV12:
385 case QVideoFrameFormat::Format_IMC1:
386 shaderFile = QStringLiteral(
"yvu_triplanar");
388 case QVideoFrameFormat::Format_IMC2:
389 shaderFile = QStringLiteral(
"imc2");
391 case QVideoFrameFormat::Format_IMC4:
392 shaderFile = QStringLiteral(
"imc4");
394 case QVideoFrameFormat::Format_UYVY:
395 shaderFile = QStringLiteral(
"uyvy");
397 case QVideoFrameFormat::Format_YUYV:
398 shaderFile = QStringLiteral(
"yuyv");
400 case QVideoFrameFormat::Format_P010:
401 case QVideoFrameFormat::Format_P016:
403 if (format.colorTransfer() == QVideoFrameFormat::ColorTransfer_ST2084) {
404 shaderFile = QStringLiteral(
"nv12_bt2020_pq");
407 if (format.colorTransfer() == QVideoFrameFormat::ColorTransfer_STD_B67) {
408 shaderFile = QStringLiteral(
"nv12_bt2020_hlg");
411 shaderFile = QStringLiteral(
"p016");
413 case QVideoFrameFormat::Format_NV12:
414 shaderFile = QStringLiteral(
"nv12");
416 case QVideoFrameFormat::Format_NV21:
417 shaderFile = QStringLiteral(
"nv21");
419 case QVideoFrameFormat::Format_SamplerExternalOES:
421 shaderFile = QStringLiteral(
"externalsampler");
424 case QVideoFrameFormat::Format_SamplerRect:
426 shaderFile = QStringLiteral(
"rectsampler_bgra");
430 case QVideoFrameFormat::Format_Invalid:
435 if (shaderFile.isEmpty())
438 shaderFile.prepend(u":/qt-project.org/multimedia/shaders/");
440 if (surfaceFormat == QRhiSwapChain::HDRExtendedSrgbLinear)
441 shaderFile.append(u"_linear");
443 shaderFile.append(u".frag.qsb");
445 Q_ASSERT_X(QFile::exists(shaderFile), Q_FUNC_INFO,
446 QStringLiteral(
"Shader file %1 does not exist").arg(shaderFile).toLatin1());
447 qCDebug(qLcVideoTextureHelper) <<
"fragmentShaderFileName returns" << shaderFile;
485 auto colorSpace = format.colorSpace();
486 if (colorSpace == QVideoFrameFormat::ColorSpace_Undefined) {
487 if (format.frameHeight() > 576)
489 colorSpace = QVideoFrameFormat::ColorSpace_BT709;
492 colorSpace = QVideoFrameFormat::ColorSpace_BT601;
494 switch (colorSpace) {
495 case QVideoFrameFormat::ColorSpace_AdobeRgb:
497 1.0f, 0.000f, 1.402f, -0.701f,
498 1.0f, -0.344f, -0.714f, 0.529f,
499 1.0f, 1.772f, 0.000f, -0.886f,
500 0.0f, 0.000f, 0.000f, 1.000f
503 case QVideoFrameFormat::ColorSpace_BT709:
504 if (format.colorRange() == QVideoFrameFormat::ColorRange_Full)
506 1.0f, 0.0f, 1.5748f, -0.790488f,
507 1.0f, -0.187324f, -0.468124f, 0.329010f,
508 1.0f, 1.855600f, 0.0f, -0.931439f,
509 0.0f, 0.0f, 0.0f, 1.0f
512 1.1644f, 0.0000f, 1.7927f, -0.9729f,
513 1.1644f, -0.2132f, -0.5329f, 0.3015f,
514 1.1644f, 2.1124f, 0.0000f, -1.1334f,
515 0.0000f, 0.0000f, 0.0000f, 1.0000f
517 case QVideoFrameFormat::ColorSpace_BT2020:
518 if (format.colorRange() == QVideoFrameFormat::ColorRange_Full)
520 1.f, 0.0000f, 1.4746f, -0.7402f,
521 1.f, -0.1646f, -0.5714f, 0.3694f,
522 1.f, 1.8814f, 0.000f, -0.9445f,
523 0.0f, 0.0000f, 0.000f, 1.0000f
526 1.1644f, 0.000f, 1.6787f, -0.9157f,
527 1.1644f, -0.1874f, -0.6504f, 0.3475f,
528 1.1644f, 2.1418f, 0.0000f, -1.1483f,
529 0.0000f, 0.0000f, 0.0000f, 1.0000f
531 case QVideoFrameFormat::ColorSpace_BT601:
534 if (format.colorRange() == QVideoFrameFormat::ColorRange_Full)
536 1.f, 0.000f, 1.772f, -0.886f,
537 1.f, -0.1646f, -0.57135f, 0.36795f,
538 1.f, 1.42f, 0.000f, -0.71f,
539 0.0f, 0.000f, 0.000f, 1.0000f
542 1.164f, 0.000f, 1.596f, -0.8708f,
543 1.164f, -0.392f, -0.813f, 0.5296f,
544 1.164f, 2.017f, 0.000f, -1.0810f,
545 0.000f, 0.000f, 0.000f, 1.0000f
586 const QVideoFrame &frame,
const QMatrix4x4 &transform,
float opacity,
594 switch (format.pixelFormat()) {
595 case QVideoFrameFormat::Format_Invalid:
598 case QVideoFrameFormat::Format_ARGB8888:
599 case QVideoFrameFormat::Format_ARGB8888_Premultiplied:
600 case QVideoFrameFormat::Format_XRGB8888:
601 case QVideoFrameFormat::Format_BGRA8888:
602 case QVideoFrameFormat::Format_BGRA8888_Premultiplied:
603 case QVideoFrameFormat::Format_BGRX8888:
604 case QVideoFrameFormat::Format_ABGR8888:
605 case QVideoFrameFormat::Format_XBGR8888:
606 case QVideoFrameFormat::Format_RGBA8888:
607 case QVideoFrameFormat::Format_RGBX8888: {
608 if (format.colorRange() == QVideoFrameFormat::ColorRange_Video) {
609 constexpr float scale = 255.0f / 219.0f;
610 constexpr float offset = -16.0f / 219.0f;
613 scale, 0.f, 0.f, offset,
614 0.f, scale, 0.f, offset,
615 0.f, 0.f, scale, offset,
624 case QVideoFrameFormat::Format_Jpeg:
625 case QVideoFrameFormat::Format_Y8:
626 case QVideoFrameFormat::Format_Y16:
628 case QVideoFrameFormat::Format_IMC1:
629 case QVideoFrameFormat::Format_IMC2:
630 case QVideoFrameFormat::Format_IMC3:
631 case QVideoFrameFormat::Format_IMC4:
632 case QVideoFrameFormat::Format_AYUV:
633 case QVideoFrameFormat::Format_AYUV_Premultiplied:
634 case QVideoFrameFormat::Format_YUV420P:
635 case QVideoFrameFormat::Format_YUV420P10:
636 case QVideoFrameFormat::Format_YUV422P:
637 case QVideoFrameFormat::Format_YV12:
638 case QVideoFrameFormat::Format_UYVY:
639 case QVideoFrameFormat::Format_YUYV:
640 case QVideoFrameFormat::Format_NV12:
641 case QVideoFrameFormat::Format_NV21:
642 case QVideoFrameFormat::Format_P010:
643 case QVideoFrameFormat::Format_P016:
644 cmat = colorMatrix(format);
646 case QVideoFrameFormat::Format_SamplerExternalOES:
648 if (
auto hwBuffer = QVideoFramePrivate::hwBuffer(frame))
649 cmat = hwBuffer->externalTextureMatrix();
651 case QVideoFrameFormat::Format_SamplerRect:
657 const QSize videoSize = frame.size();
658 cmat.scale(videoSize.width(), videoSize.height());
667 switch (format.colorTransfer()) {
668 case QVideoFrameFormat::ColorTransfer_ST2084:
671 case QVideoFrameFormat::ColorTransfer_STD_B67:
681 auto ud =
reinterpret_cast<
UniformData*>(dst->data());
682 memcpy(ud->transformMatrix, transform.constData(),
sizeof(ud->transformMatrix));
683 memcpy(ud->colorMatrix, cmat.constData(),
sizeof(ud->transformMatrix));
684 ud->opacity = opacity;
685 ud->width =
float(format.frameWidth());
686 ud->masteringWhite = fromLinear(
float(format.maxLuminance())/100.f);
687 ud->maxLum = fromLinear(
float(maxNits)/100.f);
688 const TextureDescription* desc = textureDescription(format.pixelFormat());
691 using FallbackPolicy = QVideoTextureHelper::TextureDescription::FallbackPolicy;
692 auto fallbackPolicy = isDmaBuf
693 ? FallbackPolicy::Disable
694 : FallbackPolicy::Enable;
699 const bool useRedComponent =
700 !desc->hasTextureFormat(TextureDescription::Red_8)
701 || isRhiTextureFormatSupported(rhi, QRhiTexture::R8)
702 || rhi->isFeatureSupported(QRhi::RedOrAlpha8IsRed)
704 ud->redOrAlphaIndex = useRedComponent ? 0 : 3;
706 for (
int plane = 0; plane < desc->nplanes; ++plane)
707 ud->planeFormats[plane] = desc->rhiTextureFormat(plane, rhi, fallbackPolicy);
717 QRhiResourceUpdateBatch &rub,
int plane,
718 std::unique_ptr<QRhiTexture> &tex)
720 Q_ASSERT(frame.isMapped());
723 QVideoFrameFormat::PixelFormat pixelFormat = fmt.pixelFormat();
724 QSize size = fmt.frameSize();
726 const TextureDescription &texDesc = descriptions[pixelFormat];
727 QSize planeSize = texDesc.rhiPlaneSize(size, plane, &rhi);
729 bool needsRebuild = !tex || tex->pixelSize() != planeSize || tex->format() != texDesc.rhiTextureFormat(plane, &rhi);
731 tex.reset(rhi.newTexture(texDesc.rhiTextureFormat(plane, &rhi), planeSize, 1, {}));
733 qWarning(
"Failed to create new texture (size %dx%d)", planeSize.width(), planeSize.height());
739 tex->setFormat(texDesc.rhiTextureFormat(plane, &rhi));
740 tex->setPixelSize(planeSize);
741 if (!tex->create()) {
742 qWarning(
"Failed to create texture (size %dx%d)", planeSize.width(), planeSize.height());
751 if (pixelFormat == QVideoFrameFormat::Format_Jpeg) {
752 Q_ASSERT(plane == 0);
758 const QVideoFrameFormat surfaceFormat = frame.surfaceFormat();
760 const bool hasSurfaceTransform = surfaceFormat.isMirrored()
761 || surfaceFormat.scanLineDirection() == QVideoFrameFormat::BottomToTop
762 || surfaceFormat.rotation() != QtVideo::Rotation::None;
764 if (hasSurfaceTransform)
765 image = qImageFromVideoFrame(frame, VideoTransformation{});
767 image = frame.toImage();
769 image.convertTo(QImage::Format_ARGB32);
770 subresDesc.setImage(image);
774 subresDesc.setData(QByteArray::fromRawData(
775 reinterpret_cast<
const char *>(frame.bits(plane)), frame.mappedBytes(plane)));
776 subresDesc.setDataStride(frame.bytesPerLine(plane));
782 rub.uploadTexture(tex.get(), desc);
789 QVideoFrameFormat::PixelFormat pixelFormat, QSize size,
int plane)
791 const TextureDescription &texDesc = descriptions[pixelFormat];
792 QSize planeSize = texDesc.rhiPlaneSize(size, plane, &rhi);
794 QRhiTexture::Flags textureFlags = {};
795 if (pixelFormat == QVideoFrameFormat::Format_SamplerExternalOES) {
797 if (rhi.backend() == QRhi::OpenGLES2)
798 textureFlags |= QRhiTexture::ExternalOES;
801 if (pixelFormat == QVideoFrameFormat::Format_SamplerRect) {
803 if (rhi.backend() == QRhi::OpenGLES2)
804 textureFlags |= QRhiTexture::TextureRectangleGL;
808 if (quint64 handle = texturesSet.textureHandle(rhi, plane); handle) {
809 std::unique_ptr<QRhiTexture> tex(rhi.newTexture(texDesc.rhiTextureFormat(plane, &rhi), planeSize, 1, textureFlags));
810 if (tex->createFrom({handle, 0}))
813 qWarning(
"Failed to initialize QRhiTexture wrapper for native texture object %llu",handle);
857 QRhiResourceUpdateBatch &rub,
858 QVideoFrameTexturesUPtr &oldTextures)
860 qCDebug(qLcVideoTextureHelper) <<
"createTexturesFromMemory, pixelFormat:" << frame.pixelFormat();
861 if (!frame.map(QVideoFrame::ReadOnly)) {
862 qWarning() <<
"Cannot map a video frame in ReadOnly mode!";
866 auto unmapFrameGuard = qScopeGuard([&frame] { frame.unmap(); });
868 const TextureDescription &texDesc = descriptions[frame.surfaceFormat().pixelFormat()];
872 std::unique_ptr<QVideoFrameTexturesFromMemory> textures(canReuseTextures ?
876 RhiTextureArray& textureArray = textures->textureArray();
877 bool shouldKeepMapping =
false;
878 for (quint8 plane = 0; plane < texDesc.nplanes; ++plane) {
879 const auto result = updateTextureWithMap(frame, rhi, rub, plane, textureArray[plane]);
884 shouldKeepMapping =
true;
888 textures->setMappedFrame(shouldKeepMapping ?
std::move(frame) : QVideoFrame());
893QVideoFrameTexturesUPtr createTextures(
const QVideoFrame &frame, QRhi &rhi,
894 QRhiResourceUpdateBatch &rub,
895 QVideoFrameTexturesUPtr &oldTextures)
897 if (!frame.isValid())
900 auto setSourceFrame = [&frame](QVideoFrameTexturesUPtr result) {
901 result->setSourceFrame(frame);
905 if (QHwVideoBuffer *hwBuffer = QVideoFramePrivate::hwBuffer(frame)) {
906 if (
auto textures = hwBuffer->mapTextures(rhi, oldTextures))
907 return setSourceFrame(
std::move(textures));
910 if (
auto textures = createTexturesArray<QVideoFrameTexturesFromRhiTextureArray>(
911 rhi, *hwBuffer, format.pixelFormat(), format.frameSize()))
912 return setSourceFrame(
std::move(textures));
915 if (
auto textures = createTexturesFromMemory(frame, rhi, rub, oldTextures))
916 return setSourceFrame(
std::move(textures));
\inmodule QtGuiPrivate \inheaderfile rhi/qrhi.h
static std::unique_ptr< QRhiTexture > createTextureFromHandle(QVideoFrameTexturesHandles &texturesSet, QRhi &rhi, QVideoFrameFormat::PixelFormat pixelFormat, QSize size, int plane)
static QVideoFrameTexturesUPtr createTexturesFromMemory(QVideoFrame frame, QRhi &rhi, QRhiResourceUpdateBatch &rub, QVideoFrameTexturesUPtr &oldTextures)
static UpdateTextureWithMapResult updateTextureWithMap(const QVideoFrame &frame, QRhi &rhi, QRhiResourceUpdateBatch &rub, int plane, std::unique_ptr< QRhiTexture > &tex)
static QVideoFrameTexturesUPtr createTexturesArray(QRhi &rhi, QVideoFrameTexturesHandles &texturesSet, QVideoFrameFormat::PixelFormat pixelFormat, QSize size, Args &&...args)
Q_MULTIMEDIA_EXPORT void updateUniformData(QByteArray *dst, QRhi *rhi, const QVideoFrameFormat &format, const QVideoFrame &frame, const QMatrix4x4 &transform, float opacity, float maxNits=100)