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 } }
348 QRhiSwapChain::Format surfaceFormat)
351 switch (format.pixelFormat()) {
352 case QVideoFrameFormat::Format_Y8:
353 shaderFile = QStringLiteral(
"y");
355 case QVideoFrameFormat::Format_Y16:
356 shaderFile = QStringLiteral(
"y16");
358 case QVideoFrameFormat::Format_AYUV:
359 case QVideoFrameFormat::Format_AYUV_Premultiplied:
360 shaderFile = QStringLiteral(
"ayuv");
362 case QVideoFrameFormat::Format_ARGB8888:
363 case QVideoFrameFormat::Format_ARGB8888_Premultiplied:
364 case QVideoFrameFormat::Format_XRGB8888:
365 shaderFile = QStringLiteral(
"argb");
367 case QVideoFrameFormat::Format_ABGR8888:
368 case QVideoFrameFormat::Format_XBGR8888:
369 shaderFile = QStringLiteral(
"abgr");
371 case QVideoFrameFormat::Format_Jpeg:
372 shaderFile = QStringLiteral(
"bgra");
374 case QVideoFrameFormat::Format_RGBA8888:
375 case QVideoFrameFormat::Format_RGBX8888:
376 shaderFile = QStringLiteral(
"rgba");
378 case QVideoFrameFormat::Format_BGRA8888:
379 case QVideoFrameFormat::Format_BGRA8888_Premultiplied:
380 case QVideoFrameFormat::Format_BGRX8888:
384 shaderFile = !isRhiTextureFormatSupported(rhi, QRhiTexture::BGRA8)
385 ? QStringLiteral(
"bgra")
386 : QStringLiteral(
"rgba");
388 case QVideoFrameFormat::Format_YUV420P:
389 case QVideoFrameFormat::Format_YUV422P:
390 case QVideoFrameFormat::Format_IMC3:
391 shaderFile = QStringLiteral(
"yuv_triplanar");
393 case QVideoFrameFormat::Format_YUV420P10:
394 shaderFile = QStringLiteral(
"yuv_triplanar_p10");
396 case QVideoFrameFormat::Format_YV12:
397 case QVideoFrameFormat::Format_IMC1:
398 shaderFile = QStringLiteral(
"yvu_triplanar");
400 case QVideoFrameFormat::Format_IMC2:
401 shaderFile = QStringLiteral(
"imc2");
403 case QVideoFrameFormat::Format_IMC4:
404 shaderFile = QStringLiteral(
"imc4");
406 case QVideoFrameFormat::Format_UYVY:
407 shaderFile = QStringLiteral(
"uyvy");
409 case QVideoFrameFormat::Format_YUYV:
410 shaderFile = QStringLiteral(
"yuyv");
412 case QVideoFrameFormat::Format_P010:
413 case QVideoFrameFormat::Format_P016:
415 if (format.colorTransfer() == QVideoFrameFormat::ColorTransfer_ST2084) {
416 shaderFile = QStringLiteral(
"nv12_bt2020_pq");
419 if (format.colorTransfer() == QVideoFrameFormat::ColorTransfer_STD_B67) {
420 shaderFile = QStringLiteral(
"nv12_bt2020_hlg");
423 shaderFile = QStringLiteral(
"p016");
425 case QVideoFrameFormat::Format_NV12:
426 shaderFile = QStringLiteral(
"nv12");
428 case QVideoFrameFormat::Format_NV21:
429 shaderFile = QStringLiteral(
"nv21");
431 case QVideoFrameFormat::Format_SamplerExternalOES:
433 shaderFile = QStringLiteral(
"externalsampler");
436 case QVideoFrameFormat::Format_SamplerRect:
438 shaderFile = QStringLiteral(
"rectsampler_bgra");
442 case QVideoFrameFormat::Format_Invalid:
447 if (shaderFile.isEmpty())
450 shaderFile.prepend(u":/qt-project.org/multimedia/shaders/");
452 if (surfaceFormat == QRhiSwapChain::HDRExtendedSrgbLinear)
453 shaderFile.append(u"_linear");
455 shaderFile.append(u".frag.qsb");
458 QFile::exists(shaderFile), Q_FUNC_INFO,
459 QStringLiteral(
"Shader file %1 does not exist").arg(shaderFile).toLatin1().constData());
460 qCDebug(qLcVideoTextureHelper) <<
"fragmentShaderFileName returns" << shaderFile;
498 auto colorSpace = format.colorSpace();
499 if (colorSpace == QVideoFrameFormat::ColorSpace_Undefined) {
500 if (format.frameHeight() > 576)
502 colorSpace = QVideoFrameFormat::ColorSpace_BT709;
505 colorSpace = QVideoFrameFormat::ColorSpace_BT601;
507 switch (colorSpace) {
508 case QVideoFrameFormat::ColorSpace_AdobeRgb:
510 1.0f, 0.000f, 1.402f, -0.701f,
511 1.0f, -0.344f, -0.714f, 0.529f,
512 1.0f, 1.772f, 0.000f, -0.886f,
513 0.0f, 0.000f, 0.000f, 1.000f
516 case QVideoFrameFormat::ColorSpace_BT709:
517 if (format.colorRange() == QVideoFrameFormat::ColorRange_Full)
519 1.0f, 0.0f, 1.5748f, -0.790488f,
520 1.0f, -0.187324f, -0.468124f, 0.329010f,
521 1.0f, 1.855600f, 0.0f, -0.931439f,
522 0.0f, 0.0f, 0.0f, 1.0f
525 1.1644f, 0.0000f, 1.7927f, -0.9729f,
526 1.1644f, -0.2132f, -0.5329f, 0.3015f,
527 1.1644f, 2.1124f, 0.0000f, -1.1334f,
528 0.0000f, 0.0000f, 0.0000f, 1.0000f
530 case QVideoFrameFormat::ColorSpace_BT2020:
531 if (format.colorRange() == QVideoFrameFormat::ColorRange_Full)
533 1.f, 0.0000f, 1.4746f, -0.7402f,
534 1.f, -0.1646f, -0.5714f, 0.3694f,
535 1.f, 1.8814f, 0.000f, -0.9445f,
536 0.0f, 0.0000f, 0.000f, 1.0000f
539 1.1644f, 0.000f, 1.6787f, -0.9157f,
540 1.1644f, -0.1874f, -0.6504f, 0.3475f,
541 1.1644f, 2.1418f, 0.0000f, -1.1483f,
542 0.0000f, 0.0000f, 0.0000f, 1.0000f
544 case QVideoFrameFormat::ColorSpace_BT601:
547 if (format.colorRange() == QVideoFrameFormat::ColorRange_Full)
549 1.f, 0.000f, 1.772f, -0.886f,
550 1.f, -0.1646f, -0.57135f, 0.36795f,
551 1.f, 1.42f, 0.000f, -0.71f,
552 0.0f, 0.000f, 0.000f, 1.0000f
555 1.164f, 0.000f, 1.596f, -0.8708f,
556 1.164f, -0.392f, -0.813f, 0.5296f,
557 1.164f, 2.017f, 0.000f, -1.0810f,
558 0.000f, 0.000f, 0.000f, 1.0000f
599 const QVideoFrame &frame,
const QMatrix4x4 &transform,
float opacity,
607 switch (format.pixelFormat()) {
608 case QVideoFrameFormat::Format_Invalid:
611 case QVideoFrameFormat::Format_ARGB8888:
612 case QVideoFrameFormat::Format_ARGB8888_Premultiplied:
613 case QVideoFrameFormat::Format_XRGB8888:
614 case QVideoFrameFormat::Format_BGRA8888:
615 case QVideoFrameFormat::Format_BGRA8888_Premultiplied:
616 case QVideoFrameFormat::Format_BGRX8888:
617 case QVideoFrameFormat::Format_ABGR8888:
618 case QVideoFrameFormat::Format_XBGR8888:
619 case QVideoFrameFormat::Format_RGBA8888:
620 case QVideoFrameFormat::Format_RGBX8888: {
621 if (format.colorRange() == QVideoFrameFormat::ColorRange_Video) {
622 constexpr float scale = 255.0f / 219.0f;
623 constexpr float offset = -16.0f / 219.0f;
626 scale, 0.f, 0.f, offset,
627 0.f, scale, 0.f, offset,
628 0.f, 0.f, scale, offset,
637 case QVideoFrameFormat::Format_Jpeg:
638 case QVideoFrameFormat::Format_Y8:
639 case QVideoFrameFormat::Format_Y16:
641 case QVideoFrameFormat::Format_IMC1:
642 case QVideoFrameFormat::Format_IMC2:
643 case QVideoFrameFormat::Format_IMC3:
644 case QVideoFrameFormat::Format_IMC4:
645 case QVideoFrameFormat::Format_AYUV:
646 case QVideoFrameFormat::Format_AYUV_Premultiplied:
647 case QVideoFrameFormat::Format_YUV420P:
648 case QVideoFrameFormat::Format_YUV420P10:
649 case QVideoFrameFormat::Format_YUV422P:
650 case QVideoFrameFormat::Format_YV12:
651 case QVideoFrameFormat::Format_UYVY:
652 case QVideoFrameFormat::Format_YUYV:
653 case QVideoFrameFormat::Format_NV12:
654 case QVideoFrameFormat::Format_NV21:
655 case QVideoFrameFormat::Format_P010:
656 case QVideoFrameFormat::Format_P016:
657 cmat = colorMatrix(format);
659 case QVideoFrameFormat::Format_SamplerExternalOES:
661 if (
auto hwBuffer = QVideoFramePrivate::hwBuffer(frame))
662 cmat = hwBuffer->externalTextureMatrix();
664 case QVideoFrameFormat::Format_SamplerRect:
670 const QSize videoSize = frame.size();
671 cmat.scale(videoSize.width(), videoSize.height());
680 switch (format.colorTransfer()) {
681 case QVideoFrameFormat::ColorTransfer_ST2084:
684 case QVideoFrameFormat::ColorTransfer_STD_B67:
694 auto ud =
reinterpret_cast<
UniformData*>(dst->data());
695 memcpy(ud->transformMatrix, transform.constData(),
sizeof(ud->transformMatrix));
696 memcpy(ud->colorMatrix, cmat.constData(),
sizeof(ud->transformMatrix));
697 ud->opacity = opacity;
698 ud->width =
float(format.frameWidth());
699 ud->masteringWhite = fromLinear(
float(format.maxLuminance())/100.f);
700 ud->maxLum = fromLinear(
float(maxNits)/100.f);
701 const TextureDescription* desc = textureDescription(format.pixelFormat());
704 using FallbackPolicy = QVideoTextureHelper::TextureDescription::FallbackPolicy;
705 auto fallbackPolicy = isDmaBuf
706 ? FallbackPolicy::Disable
707 : FallbackPolicy::Enable;
712 const bool useRedComponent =
713 !desc->hasTextureFormat(TextureDescription::Red_8)
714 || isRhiTextureFormatSupported(rhi, QRhiTexture::R8)
715 || rhi->isFeatureSupported(QRhi::RedOrAlpha8IsRed)
717 ud->redOrAlphaIndex = useRedComponent ? 0 : 3;
719 for (
int plane = 0; plane < desc->nplanes; ++plane)
720 ud->planeFormats[plane] = desc->rhiTextureFormat(plane, rhi, fallbackPolicy);
730 QRhiResourceUpdateBatch &rub,
int plane,
731 std::unique_ptr<QRhiTexture> &tex)
733 Q_ASSERT(frame.isMapped());
736 QVideoFrameFormat::PixelFormat pixelFormat = fmt.pixelFormat();
737 QSize size = fmt.frameSize();
739 const TextureDescription &texDesc = descriptions[pixelFormat];
740 QSize planeSize = texDesc.rhiPlaneSize(size, plane, &rhi);
742 bool needsRebuild = !tex || tex->pixelSize() != planeSize || tex->format() != texDesc.rhiTextureFormat(plane, &rhi);
744 tex.reset(rhi.newTexture(texDesc.rhiTextureFormat(plane, &rhi), planeSize, 1, {}));
746 qWarning(
"Failed to create new texture (size %dx%d)", planeSize.width(), planeSize.height());
752 tex->setFormat(texDesc.rhiTextureFormat(plane, &rhi));
753 tex->setPixelSize(planeSize);
754 if (!tex->create()) {
755 qWarning(
"Failed to create texture (size %dx%d)", planeSize.width(), planeSize.height());
764 if (pixelFormat == QVideoFrameFormat::Format_Jpeg) {
765 Q_ASSERT(plane == 0);
771 const QVideoFrameFormat surfaceFormat = frame.surfaceFormat();
773 const bool hasSurfaceTransform = surfaceFormat.isMirrored()
774 || surfaceFormat.scanLineDirection() == QVideoFrameFormat::BottomToTop
775 || surfaceFormat.rotation() != QtVideo::Rotation::None;
777 if (hasSurfaceTransform)
778 image = qImageFromVideoFrame(frame, VideoTransformation{});
780 image = frame.toImage();
782 image.convertTo(QImage::Format_ARGB32);
783 subresDesc.setImage(image);
787 subresDesc.setData(QByteArray::fromRawData(
788 reinterpret_cast<
const char *>(frame.bits(plane)), frame.mappedBytes(plane)));
789 subresDesc.setDataStride(frame.bytesPerLine(plane));
795 rub.uploadTexture(tex.get(), desc);
802 QVideoFrameFormat::PixelFormat pixelFormat, QSize size,
int plane)
804 const TextureDescription &texDesc = descriptions[pixelFormat];
805 QSize planeSize = texDesc.rhiPlaneSize(size, plane, &rhi);
807 QRhiTexture::Flags textureFlags = {};
808 if (pixelFormat == QVideoFrameFormat::Format_SamplerExternalOES) {
810 if (rhi.backend() == QRhi::OpenGLES2)
811 textureFlags |= QRhiTexture::ExternalOES;
814 if (pixelFormat == QVideoFrameFormat::Format_SamplerRect) {
816 if (rhi.backend() == QRhi::OpenGLES2)
817 textureFlags |= QRhiTexture::TextureRectangleGL;
821 if (quint64 handle = texturesSet.textureHandle(rhi, plane); handle) {
822 std::unique_ptr<QRhiTexture> tex(rhi.newTexture(texDesc.rhiTextureFormat(plane, &rhi), planeSize, 1, textureFlags));
823 if (tex->createFrom({handle, 0}))
826 qWarning(
"Failed to initialize QRhiTexture wrapper for native texture object %llu",handle);
870 QRhiResourceUpdateBatch &rub,
871 QVideoFrameTexturesUPtr &oldTextures)
873 qCDebug(qLcVideoTextureHelper) <<
"createTexturesFromMemory, pixelFormat:" << frame.pixelFormat();
874 if (!frame.map(QVideoFrame::ReadOnly)) {
875 qWarning() <<
"Cannot map a video frame in ReadOnly mode!";
879 auto unmapFrameGuard = qScopeGuard([&frame] { frame.unmap(); });
881 const TextureDescription &texDesc = descriptions[frame.surfaceFormat().pixelFormat()];
885 std::unique_ptr<QVideoFrameTexturesFromMemory> textures(canReuseTextures ?
889 RhiTextureArray& textureArray = textures->textureArray();
890 bool shouldKeepMapping =
false;
891 for (quint8 plane = 0; plane < texDesc.nplanes; ++plane) {
892 const auto result = updateTextureWithMap(frame, rhi, rub, plane, textureArray[plane]);
897 shouldKeepMapping =
true;
901 textures->setMappedFrame(shouldKeepMapping ?
std::move(frame) : QVideoFrame());
907 QRhiResourceUpdateBatch &rub,
908 QVideoFrameTexturesUPtr &oldTextures)
910 if (!frame.isValid())
913 auto setSourceFrame = [&frame](QVideoFrameTexturesUPtr result) {
914 result->setSourceFrame(frame);
918 if (QHwVideoBuffer *hwBuffer = QVideoFramePrivate::hwBuffer(frame)) {
919 if (
auto textures = hwBuffer->mapTextures(rhi, oldTextures))
920 return setSourceFrame(
std::move(textures));
923 if (
auto textures = createTexturesArray<QVideoFrameTexturesFromRhiTextureArray>(
924 rhi, *hwBuffer, format.pixelFormat(), format.frameSize()))
925 return setSourceFrame(
std::move(textures));
928 if (
auto textures = createTexturesFromMemory(frame, rhi, rub, oldTextures))
929 return setSourceFrame(
std::move(textures));
\inmodule QtGuiPrivate \inheaderfile rhi/qrhi.h
QRhiTexture::Format resolvedRhiTextureFormat(QRhiTexture::Format format, QRhi *rhi)
Returns a QRhiTexture::Format taking into account rhi capabilities and explicitly excluded formats us...
QVideoFrameTexturesUPtr createTextures(const QVideoFrame &frame, QRhi &rhi, QRhiResourceUpdateBatch &rub, QVideoFrameTexturesUPtr &oldTextures)
Creates plane textures from a video frame by the specified rhi. If possible, the function modifies 'o...
QVideoFrameTexturesUPtr createTexturesFromHandles(QVideoFrameTexturesHandlesUPtr texturesSet, QRhi &rhi, QVideoFrameFormat::PixelFormat pixelFormat, QSize size)
Creates plane textures from texture handles set by the specified rhi. The result owns the specified h...
QString fragmentShaderFileName(const QVideoFrameFormat &format, QRhi *rhi, QRhiSwapChain::Format surfaceFormat)
static std::unique_ptr< QRhiTexture > createTextureFromHandle(QVideoFrameTexturesHandles &texturesSet, QRhi &rhi, QVideoFrameFormat::PixelFormat pixelFormat, QSize size, int plane)
void updateUniformData(QByteArray *dst, QRhi *rhi, const QVideoFrameFormat &format, const QVideoFrame &frame, const QMatrix4x4 &transform, float opacity, float maxNits)
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)