| 1 | // Copyright (C) 2021 The Qt Company Ltd. | 
|---|
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only | 
|---|
| 3 |  | 
|---|
| 4 | #ifndef QVIDEOTEXTUREHELPER_H | 
|---|
| 5 | #define QVIDEOTEXTUREHELPER_H | 
|---|
| 6 |  | 
|---|
| 7 | // | 
|---|
| 8 | //  W A R N I N G | 
|---|
| 9 | //  ------------- | 
|---|
| 10 | // | 
|---|
| 11 | // This file is not part of the Qt API.  It exists purely as an | 
|---|
| 12 | // implementation detail.  This header file may change from version to | 
|---|
| 13 | // version without notice, or even be removed. | 
|---|
| 14 | // | 
|---|
| 15 | // We mean it. | 
|---|
| 16 | // | 
|---|
| 17 |  | 
|---|
| 18 | #include <qvideoframeformat.h> | 
|---|
| 19 | #include <rhi/qrhi.h> | 
|---|
| 20 |  | 
|---|
| 21 | #include <QtGui/qtextlayout.h> | 
|---|
| 22 |  | 
|---|
| 23 | QT_BEGIN_NAMESPACE | 
|---|
| 24 |  | 
|---|
| 25 | class QVideoFrame; | 
|---|
| 26 | class QTextLayout; | 
|---|
| 27 | class QVideoFrameTextures; | 
|---|
| 28 | using QVideoFrameTexturesUPtr = std::unique_ptr<QVideoFrameTextures>; | 
|---|
| 29 | class QVideoFrameTexturesHandles; | 
|---|
| 30 | using QVideoFrameTexturesHandlesUPtr = std::unique_ptr<QVideoFrameTexturesHandles>; | 
|---|
| 31 |  | 
|---|
| 32 | namespace QVideoTextureHelper | 
|---|
| 33 | { | 
|---|
| 34 |  | 
|---|
| 35 | struct Q_MULTIMEDIA_EXPORT TextureDescription | 
|---|
| 36 | { | 
|---|
| 37 | static constexpr int maxPlanes = 3; | 
|---|
| 38 | struct SizeScale { | 
|---|
| 39 | int x; | 
|---|
| 40 | int y; | 
|---|
| 41 | }; | 
|---|
| 42 | using BytesRequired = int(*)(int stride, int height); | 
|---|
| 43 |  | 
|---|
| 44 | enum TextureFormat { | 
|---|
| 45 | UnknownFormat, | 
|---|
| 46 | Red_8, | 
|---|
| 47 | RG_8, | 
|---|
| 48 | RGBA_8, | 
|---|
| 49 | BGRA_8, | 
|---|
| 50 | Red_16, | 
|---|
| 51 | RG_16, | 
|---|
| 52 | }; | 
|---|
| 53 |  | 
|---|
| 54 | QRhiTexture::Format rhiTextureFormat(int plane, QRhi *rhi) const; | 
|---|
| 55 |  | 
|---|
| 56 | inline int strideForWidth(int width) const { return (width*strideFactor + 15) & ~15; } | 
|---|
| 57 | inline int bytesForSize(QSize s) const { return bytesRequired(strideForWidth(width: s.width()), s.height()); } | 
|---|
| 58 | int widthForPlane(int width, int plane) const | 
|---|
| 59 | { | 
|---|
| 60 | if (plane > nplanes) return 0; | 
|---|
| 61 | return (width + sizeScale[plane].x - 1)/sizeScale[plane].x; | 
|---|
| 62 | } | 
|---|
| 63 | int heightForPlane(int height, int plane) const | 
|---|
| 64 | { | 
|---|
| 65 | if (plane > nplanes) return 0; | 
|---|
| 66 | return (height + sizeScale[plane].y - 1)/sizeScale[plane].y; | 
|---|
| 67 | } | 
|---|
| 68 |  | 
|---|
| 69 | /** | 
|---|
| 70 | * \returns Plane scaling factors taking into account possible workarounds due to QRhi backend | 
|---|
| 71 | * capabilities. | 
|---|
| 72 | */ | 
|---|
| 73 | SizeScale rhiSizeScale(int plane, QRhi *rhi) const | 
|---|
| 74 | { | 
|---|
| 75 | if (!rhi) | 
|---|
| 76 | return sizeScale[plane]; | 
|---|
| 77 |  | 
|---|
| 78 | // NOTE: We need to handle sizing difference when packing two-component textures to RGBA8, | 
|---|
| 79 | // where width gets cut in half. | 
|---|
| 80 | // Another option would be to compare QRhiImplementation::TextureFormatInfo to expected | 
|---|
| 81 | // based on TextureDescription::Format. | 
|---|
| 82 | if (textureFormat[plane] == TextureDescription::RG_8 | 
|---|
| 83 | && rhiTextureFormat(plane, rhi) == QRhiTexture::RGBA8) | 
|---|
| 84 | return { .x: sizeScale[plane].x * 2, .y: sizeScale[plane].y }; | 
|---|
| 85 |  | 
|---|
| 86 | return sizeScale[plane]; | 
|---|
| 87 | } | 
|---|
| 88 |  | 
|---|
| 89 | QSize rhiPlaneSize(QSize frameSize, int plane, QRhi *rhi) const | 
|---|
| 90 | { | 
|---|
| 91 | SizeScale scale = rhiSizeScale(plane, rhi); | 
|---|
| 92 | return QSize(frameSize.width() / scale.x, frameSize.height() / scale.y); | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | bool hasTextureFormat(TextureFormat format) const | 
|---|
| 96 | { | 
|---|
| 97 | return std::any_of(first: textureFormat, last: textureFormat + nplanes, pred: [format](TextureFormat f) { | 
|---|
| 98 | return f == format; | 
|---|
| 99 | }); | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | int nplanes; | 
|---|
| 103 | int strideFactor; | 
|---|
| 104 | BytesRequired bytesRequired; | 
|---|
| 105 | TextureFormat textureFormat[maxPlanes]; | 
|---|
| 106 | SizeScale sizeScale[maxPlanes]; | 
|---|
| 107 | }; | 
|---|
| 108 |  | 
|---|
| 109 | Q_MULTIMEDIA_EXPORT const TextureDescription *textureDescription(QVideoFrameFormat::PixelFormat format); | 
|---|
| 110 |  | 
|---|
| 111 | Q_MULTIMEDIA_EXPORT QString vertexShaderFileName(const QVideoFrameFormat &format); | 
|---|
| 112 | Q_MULTIMEDIA_EXPORT QString | 
|---|
| 113 | fragmentShaderFileName(const QVideoFrameFormat &format, QRhi *rhi, | 
|---|
| 114 | QRhiSwapChain::Format surfaceFormat = QRhiSwapChain::SDR); | 
|---|
| 115 | Q_MULTIMEDIA_EXPORT void updateUniformData(QByteArray *dst, QRhi *rhi, | 
|---|
| 116 | const QVideoFrameFormat &format, | 
|---|
| 117 | const QVideoFrame &frame, const QMatrix4x4 &transform, | 
|---|
| 118 | float opacity, float maxNits = 100); | 
|---|
| 119 |  | 
|---|
| 120 | /** | 
|---|
| 121 | * @brief Creates plane textures from texture handles set by the specified rhi. | 
|---|
| 122 | *        The result owns the specified handles set; the QRhiTexture(s), exposed by the result | 
|---|
| 123 | *        refer to the owned handles set. | 
|---|
| 124 | *        If the specified size is empty or pixelFormat is invalid, null is returned. | 
|---|
| 125 | */ | 
|---|
| 126 | Q_MULTIMEDIA_EXPORT QVideoFrameTexturesUPtr | 
|---|
| 127 | createTexturesFromHandles(QVideoFrameTexturesHandlesUPtr handles, QRhi &rhi, | 
|---|
| 128 | QVideoFrameFormat::PixelFormat pixelFormat, QSize size); | 
|---|
| 129 |  | 
|---|
| 130 |  | 
|---|
| 131 | /** | 
|---|
| 132 | * @brief Creates plane textures from a video frame by the specified rhi. | 
|---|
| 133 | If possible, the function modifies 'oldTextures', which is the texture from the pool, | 
|---|
| 134 | and returns the specified one or a new one with any taken data. | 
|---|
| 135 | */ | 
|---|
| 136 | Q_MULTIMEDIA_EXPORT QVideoFrameTexturesUPtr createTextures(const QVideoFrame &frame, QRhi &rhi, | 
|---|
| 137 | QRhiResourceUpdateBatch &rub, | 
|---|
| 138 | QVideoFrameTexturesUPtr oldTextures); | 
|---|
| 139 |  | 
|---|
| 140 | Q_MULTIMEDIA_EXPORT void | 
|---|
| 141 | setExcludedRhiTextureFormats(QList<QRhiTexture::Format> formats); // for tests only | 
|---|
| 142 |  | 
|---|
| 143 | struct UniformData { | 
|---|
| 144 | float transformMatrix[4][4]; | 
|---|
| 145 | float colorMatrix[4][4]; | 
|---|
| 146 | float opacity; | 
|---|
| 147 | float width; | 
|---|
| 148 | float masteringWhite; | 
|---|
| 149 | float maxLum; | 
|---|
| 150 | int redOrAlphaIndex; | 
|---|
| 151 | int planeFormats[TextureDescription::maxPlanes]; | 
|---|
| 152 | }; | 
|---|
| 153 |  | 
|---|
| 154 | struct Q_MULTIMEDIA_EXPORT SubtitleLayout | 
|---|
| 155 | { | 
|---|
| 156 | QSize videoSize; | 
|---|
| 157 | QRectF bounds; | 
|---|
| 158 | QTextLayout layout; | 
|---|
| 159 |  | 
|---|
| 160 | bool update(const QSize &frameSize, QString text); | 
|---|
| 161 | void draw(QPainter *painter, const QPointF &translate) const; | 
|---|
| 162 | QImage toImage() const; | 
|---|
| 163 | }; | 
|---|
| 164 |  | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 167 | QT_END_NAMESPACE | 
|---|
| 168 |  | 
|---|
| 169 | #endif | 
|---|
| 170 |  | 
|---|