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");
446 QFile::exists(shaderFile), Q_FUNC_INFO,
447 QStringLiteral(
"Shader file %1 does not exist").arg(shaderFile).toLatin1().constData());
448 qCDebug(qLcVideoTextureHelper) <<
"fragmentShaderFileName returns" << shaderFile;
486 auto colorSpace = format.colorSpace();
487 if (colorSpace == QVideoFrameFormat::ColorSpace_Undefined) {
488 if (format.frameHeight() > 576)
490 colorSpace = QVideoFrameFormat::ColorSpace_BT709;
493 colorSpace = QVideoFrameFormat::ColorSpace_BT601;
495 switch (colorSpace) {
496 case QVideoFrameFormat::ColorSpace_AdobeRgb:
498 1.0f, 0.000f, 1.402f, -0.701f,
499 1.0f, -0.344f, -0.714f, 0.529f,
500 1.0f, 1.772f, 0.000f, -0.886f,
501 0.0f, 0.000f, 0.000f, 1.000f
504 case QVideoFrameFormat::ColorSpace_BT709:
505 if (format.colorRange() == QVideoFrameFormat::ColorRange_Full)
507 1.0f, 0.0f, 1.5748f, -0.790488f,
508 1.0f, -0.187324f, -0.468124f, 0.329010f,
509 1.0f, 1.855600f, 0.0f, -0.931439f,
510 0.0f, 0.0f, 0.0f, 1.0f
513 1.1644f, 0.0000f, 1.7927f, -0.9729f,
514 1.1644f, -0.2132f, -0.5329f, 0.3015f,
515 1.1644f, 2.1124f, 0.0000f, -1.1334f,
516 0.0000f, 0.0000f, 0.0000f, 1.0000f
518 case QVideoFrameFormat::ColorSpace_BT2020:
519 if (format.colorRange() == QVideoFrameFormat::ColorRange_Full)
521 1.f, 0.0000f, 1.4746f, -0.7402f,
522 1.f, -0.1646f, -0.5714f, 0.3694f,
523 1.f, 1.8814f, 0.000f, -0.9445f,
524 0.0f, 0.0000f, 0.000f, 1.0000f
527 1.1644f, 0.000f, 1.6787f, -0.9157f,
528 1.1644f, -0.1874f, -0.6504f, 0.3475f,
529 1.1644f, 2.1418f, 0.0000f, -1.1483f,
530 0.0000f, 0.0000f, 0.0000f, 1.0000f
532 case QVideoFrameFormat::ColorSpace_BT601:
535 if (format.colorRange() == QVideoFrameFormat::ColorRange_Full)
537 1.f, 0.000f, 1.772f, -0.886f,
538 1.f, -0.1646f, -0.57135f, 0.36795f,
539 1.f, 1.42f, 0.000f, -0.71f,
540 0.0f, 0.000f, 0.000f, 1.0000f
543 1.164f, 0.000f, 1.596f, -0.8708f,
544 1.164f, -0.392f, -0.813f, 0.5296f,
545 1.164f, 2.017f, 0.000f, -1.0810f,
546 0.000f, 0.000f, 0.000f, 1.0000f
587 const QVideoFrame &frame,
const QMatrix4x4 &transform,
float opacity,
595 switch (format.pixelFormat()) {
596 case QVideoFrameFormat::Format_Invalid:
599 case QVideoFrameFormat::Format_ARGB8888:
600 case QVideoFrameFormat::Format_ARGB8888_Premultiplied:
601 case QVideoFrameFormat::Format_XRGB8888:
602 case QVideoFrameFormat::Format_BGRA8888:
603 case QVideoFrameFormat::Format_BGRA8888_Premultiplied:
604 case QVideoFrameFormat::Format_BGRX8888:
605 case QVideoFrameFormat::Format_ABGR8888:
606 case QVideoFrameFormat::Format_XBGR8888:
607 case QVideoFrameFormat::Format_RGBA8888:
608 case QVideoFrameFormat::Format_RGBX8888: {
609 if (format.colorRange() == QVideoFrameFormat::ColorRange_Video) {
610 constexpr float scale = 255.0f / 219.0f;
611 constexpr float offset = -16.0f / 219.0f;
614 scale, 0.f, 0.f, offset,
615 0.f, scale, 0.f, offset,
616 0.f, 0.f, scale, offset,
625 case QVideoFrameFormat::Format_Jpeg:
626 case QVideoFrameFormat::Format_Y8:
627 case QVideoFrameFormat::Format_Y16:
629 case QVideoFrameFormat::Format_IMC1:
630 case QVideoFrameFormat::Format_IMC2:
631 case QVideoFrameFormat::Format_IMC3:
632 case QVideoFrameFormat::Format_IMC4:
633 case QVideoFrameFormat::Format_AYUV:
634 case QVideoFrameFormat::Format_AYUV_Premultiplied:
635 case QVideoFrameFormat::Format_YUV420P:
636 case QVideoFrameFormat::Format_YUV420P10:
637 case QVideoFrameFormat::Format_YUV422P:
638 case QVideoFrameFormat::Format_YV12:
639 case QVideoFrameFormat::Format_UYVY:
640 case QVideoFrameFormat::Format_YUYV:
641 case QVideoFrameFormat::Format_NV12:
642 case QVideoFrameFormat::Format_NV21:
643 case QVideoFrameFormat::Format_P010:
644 case QVideoFrameFormat::Format_P016:
645 cmat = colorMatrix(format);
647 case QVideoFrameFormat::Format_SamplerExternalOES:
649 if (
auto hwBuffer = QVideoFramePrivate::hwBuffer(frame))
650 cmat = hwBuffer->externalTextureMatrix();
652 case QVideoFrameFormat::Format_SamplerRect:
658 const QSize videoSize = frame.size();
659 cmat.scale(videoSize.width(), videoSize.height());
668 switch (format.colorTransfer()) {
669 case QVideoFrameFormat::ColorTransfer_ST2084:
672 case QVideoFrameFormat::ColorTransfer_STD_B67:
682 auto ud =
reinterpret_cast<
UniformData*>(dst->data());
683 memcpy(ud->transformMatrix, transform.constData(),
sizeof(ud->transformMatrix));
684 memcpy(ud->colorMatrix, cmat.constData(),
sizeof(ud->transformMatrix));
685 ud->opacity = opacity;
686 ud->width =
float(format.frameWidth());
687 ud->masteringWhite = fromLinear(
float(format.maxLuminance())/100.f);
688 ud->maxLum = fromLinear(
float(maxNits)/100.f);
689 const TextureDescription* desc = textureDescription(format.pixelFormat());
692 using FallbackPolicy = QVideoTextureHelper::TextureDescription::FallbackPolicy;
693 auto fallbackPolicy = isDmaBuf
694 ? FallbackPolicy::Disable
695 : FallbackPolicy::Enable;
700 const bool useRedComponent =
701 !desc->hasTextureFormat(TextureDescription::Red_8)
702 || isRhiTextureFormatSupported(rhi, QRhiTexture::R8)
703 || rhi->isFeatureSupported(QRhi::RedOrAlpha8IsRed)
705 ud->redOrAlphaIndex = useRedComponent ? 0 : 3;
707 for (
int plane = 0; plane < desc->nplanes; ++plane)
708 ud->planeFormats[plane] = desc->rhiTextureFormat(plane, rhi, fallbackPolicy);
718 QRhiResourceUpdateBatch &rub,
int plane,
719 std::unique_ptr<QRhiTexture> &tex)
721 Q_ASSERT(frame.isMapped());
724 QVideoFrameFormat::PixelFormat pixelFormat = fmt.pixelFormat();
725 QSize size = fmt.frameSize();
727 const TextureDescription &texDesc = descriptions[pixelFormat];
728 QSize planeSize = texDesc.rhiPlaneSize(size, plane, &rhi);
730 bool needsRebuild = !tex || tex->pixelSize() != planeSize || tex->format() != texDesc.rhiTextureFormat(plane, &rhi);
732 tex.reset(rhi.newTexture(texDesc.rhiTextureFormat(plane, &rhi), planeSize, 1, {}));
734 qWarning(
"Failed to create new texture (size %dx%d)", planeSize.width(), planeSize.height());
740 tex->setFormat(texDesc.rhiTextureFormat(plane, &rhi));
741 tex->setPixelSize(planeSize);
742 if (!tex->create()) {
743 qWarning(
"Failed to create texture (size %dx%d)", planeSize.width(), planeSize.height());
752 if (pixelFormat == QVideoFrameFormat::Format_Jpeg) {
753 Q_ASSERT(plane == 0);
759 const QVideoFrameFormat surfaceFormat = frame.surfaceFormat();
761 const bool hasSurfaceTransform = surfaceFormat.isMirrored()
762 || surfaceFormat.scanLineDirection() == QVideoFrameFormat::BottomToTop
763 || surfaceFormat.rotation() != QtVideo::Rotation::None;
765 if (hasSurfaceTransform)
766 image = qImageFromVideoFrame(frame, VideoTransformation{});
768 image = frame.toImage();
770 image.convertTo(QImage::Format_ARGB32);
771 subresDesc.setImage(image);
775 subresDesc.setData(QByteArray::fromRawData(
776 reinterpret_cast<
const char *>(frame.bits(plane)), frame.mappedBytes(plane)));
777 subresDesc.setDataStride(frame.bytesPerLine(plane));
783 rub.uploadTexture(tex.get(), desc);
790 QVideoFrameFormat::PixelFormat pixelFormat, QSize size,
int plane)
792 const TextureDescription &texDesc = descriptions[pixelFormat];
793 QSize planeSize = texDesc.rhiPlaneSize(size, plane, &rhi);
795 QRhiTexture::Flags textureFlags = {};
796 if (pixelFormat == QVideoFrameFormat::Format_SamplerExternalOES) {
798 if (rhi.backend() == QRhi::OpenGLES2)
799 textureFlags |= QRhiTexture::ExternalOES;
802 if (pixelFormat == QVideoFrameFormat::Format_SamplerRect) {
804 if (rhi.backend() == QRhi::OpenGLES2)
805 textureFlags |= QRhiTexture::TextureRectangleGL;
809 if (quint64 handle = texturesSet.textureHandle(rhi, plane); handle) {
810 std::unique_ptr<QRhiTexture> tex(rhi.newTexture(texDesc.rhiTextureFormat(plane, &rhi), planeSize, 1, textureFlags));
811 if (tex->createFrom({handle, 0}))
814 qWarning(
"Failed to initialize QRhiTexture wrapper for native texture object %llu",handle);
858 QRhiResourceUpdateBatch &rub,
859 QVideoFrameTexturesUPtr &oldTextures)
861 qCDebug(qLcVideoTextureHelper) <<
"createTexturesFromMemory, pixelFormat:" << frame.pixelFormat();
862 if (!frame.map(QVideoFrame::ReadOnly)) {
863 qWarning() <<
"Cannot map a video frame in ReadOnly mode!";
867 auto unmapFrameGuard = qScopeGuard([&frame] { frame.unmap(); });
869 const TextureDescription &texDesc = descriptions[frame.surfaceFormat().pixelFormat()];
873 std::unique_ptr<QVideoFrameTexturesFromMemory> textures(canReuseTextures ?
877 RhiTextureArray& textureArray = textures->textureArray();
878 bool shouldKeepMapping =
false;
879 for (quint8 plane = 0; plane < texDesc.nplanes; ++plane) {
880 const auto result = updateTextureWithMap(frame, rhi, rub, plane, textureArray[plane]);
885 shouldKeepMapping =
true;
889 textures->setMappedFrame(shouldKeepMapping ?
std::move(frame) : QVideoFrame());
894QVideoFrameTexturesUPtr createTextures(
const QVideoFrame &frame, QRhi &rhi,
895 QRhiResourceUpdateBatch &rub,
896 QVideoFrameTexturesUPtr &oldTextures)
898 if (!frame.isValid())
901 auto setSourceFrame = [&frame](QVideoFrameTexturesUPtr result) {
902 result->setSourceFrame(frame);
906 if (QHwVideoBuffer *hwBuffer = QVideoFramePrivate::hwBuffer(frame)) {
907 if (
auto textures = hwBuffer->mapTextures(rhi, oldTextures))
908 return setSourceFrame(
std::move(textures));
911 if (
auto textures = createTexturesArray<QVideoFrameTexturesFromRhiTextureArray>(
912 rhi, *hwBuffer, format.pixelFormat(), format.frameSize()))
913 return setSourceFrame(
std::move(textures));
916 if (
auto textures = createTexturesFromMemory(frame, rhi, rub, oldTextures))
917 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)