diff --git a/src/qtmultimediaquicktools/qsgvideonode_yuv.cpp b/src/qtmultimediaquicktools/qsgvideonode_yuv.cpp index 9eff9dbd68fb1d91d725b40a36d7c06254c076db..43cd5a2b244bd610f0c01a031763417f6339d878 100644 --- a/src/qtmultimediaquicktools/qsgvideonode_yuv.cpp +++ b/src/qtmultimediaquicktools/qsgvideonode_yuv.cpp @@ -44,6 +44,28 @@ #include <QtGui/QOpenGLFunctions> #include <QtGui/QOpenGLShaderProgram> +#ifndef GL_RED +#define GL_RED 0x1903 +#endif +#ifndef GL_GREEN +#define GL_GREEN 0x1904 +#endif +#ifndef GL_RG +#define GL_RG 0x8227 +#endif +#ifndef GL_TEXTURE_SWIZZLE_R +#define GL_TEXTURE_SWIZZLE_R 0x8E42 +#endif +#ifndef GL_TEXTURE_SWIZZLE_G +#define GL_TEXTURE_SWIZZLE_G 0x8E43 +#endif +#ifndef GL_TEXTURE_SWIZZLE_B +#define GL_TEXTURE_SWIZZLE_B 0x8E44 +#endif +#ifndef GL_TEXTURE_SWIZZLE_A +#define GL_TEXTURE_SWIZZLE_A 0x8E45 +#endif + QT_BEGIN_NAMESPACE QList<QVideoFrame::PixelFormat> QSGVideoNodeFactory_YUV::supportedPixelFormats( @@ -335,6 +357,8 @@ QSGVideoMaterial_YUV::~QSGVideoMaterial_YUV() void QSGVideoMaterial_YUV::bind() { QOpenGLFunctions *functions = QOpenGLContext::currentContext()->functions(); + QSurfaceFormat::OpenGLContextProfile profile = QOpenGLContext::currentContext()->format().profile(); + QMutexLocker lock(&m_frameMutex); if (m_frame.isValid()) { if (m_frame.map(QAbstractVideoBuffer::ReadOnly)) { @@ -350,12 +374,16 @@ void QSGVideoMaterial_YUV::bind() } GLint previousAlignment; + const GLenum texFormat1 = (profile == QSurfaceFormat::CoreProfile) ? GL_RED : GL_LUMINANCE; + const GLenum texFormat2 = (profile == QSurfaceFormat::CoreProfile) ? GL_RG : GL_LUMINANCE_ALPHA; + functions->glGetIntegerv(GL_UNPACK_ALIGNMENT, &previousAlignment); functions->glPixelStorei(GL_UNPACK_ALIGNMENT, 1); if (m_format.pixelFormat() == QVideoFrame::Format_UYVY || m_format.pixelFormat() == QVideoFrame::Format_YUYV) { int fw = m_frame.width(); + m_planeWidth[0] = fw; // In YUYV texture the UV plane appears with the 1/2 of image and Y width. m_planeWidth[1] = fw / 2; @@ -367,7 +395,7 @@ void QSGVideoMaterial_YUV::bind() bindTexture(m_textureIds[1], m_planeWidth[1], m_frame.height(), m_frame.bits(), GL_RGBA); functions->glActiveTexture(GL_TEXTURE0); // Finish with 0 as default texture unit // Either red (YUYV) or alpha (UYVY) values are used as source of Y - bindTexture(m_textureIds[0], m_planeWidth[0], m_frame.height(), m_frame.bits(), GL_LUMINANCE_ALPHA); + bindTexture(m_textureIds[0], m_planeWidth[0], m_frame.height(), m_frame.bits(), texFormat2); } else if (m_format.pixelFormat() == QVideoFrame::Format_NV12 || m_format.pixelFormat() == QVideoFrame::Format_NV21) { const int y = 0; @@ -376,9 +404,9 @@ void QSGVideoMaterial_YUV::bind() m_planeWidth[0] = m_planeWidth[1] = qreal(fw) / m_frame.bytesPerLine(y); functions->glActiveTexture(GL_TEXTURE1); - bindTexture(m_textureIds[1], m_frame.bytesPerLine(uv) / 2, fh / 2, m_frame.bits(uv), GL_LUMINANCE_ALPHA); + bindTexture(m_textureIds[1], m_frame.bytesPerLine(uv) / 2, fh / 2, m_frame.bits(uv), texFormat2); functions->glActiveTexture(GL_TEXTURE0); // Finish with 0 as default texture unit - bindTexture(m_textureIds[0], m_frame.bytesPerLine(y), fh, m_frame.bits(y), GL_LUMINANCE); + bindTexture(m_textureIds[0], m_frame.bytesPerLine(y), fh, m_frame.bits(y), texFormat1); } else { // YUV420P || YV12 const int y = 0; @@ -389,11 +417,11 @@ void QSGVideoMaterial_YUV::bind() m_planeWidth[1] = m_planeWidth[2] = qreal(fw) / (2 * m_frame.bytesPerLine(u)); functions->glActiveTexture(GL_TEXTURE1); - bindTexture(m_textureIds[1], m_frame.bytesPerLine(u), fh / 2, m_frame.bits(u), GL_LUMINANCE); + bindTexture(m_textureIds[1], m_frame.bytesPerLine(u), fh / 2, m_frame.bits(u), texFormat1); functions->glActiveTexture(GL_TEXTURE2); - bindTexture(m_textureIds[2], m_frame.bytesPerLine(v), fh / 2, m_frame.bits(v), GL_LUMINANCE); + bindTexture(m_textureIds[2], m_frame.bytesPerLine(v), fh / 2, m_frame.bits(v), texFormat1); functions->glActiveTexture(GL_TEXTURE0); // Finish with 0 as default texture unit - bindTexture(m_textureIds[0], m_frame.bytesPerLine(y), fh, m_frame.bits(y), GL_LUMINANCE); + bindTexture(m_textureIds[0], m_frame.bytesPerLine(y), fh, m_frame.bits(y), texFormat1); } functions->glPixelStorei(GL_UNPACK_ALIGNMENT, previousAlignment); @@ -415,6 +443,13 @@ void QSGVideoMaterial_YUV::bindTexture(int id, int w, int h, const uchar *bits, QOpenGLFunctions *functions = QOpenGLContext::currentContext()->functions(); functions->glBindTexture(GL_TEXTURE_2D, id); functions->glTexImage2D(GL_TEXTURE_2D, 0, format, w, h, 0, format, GL_UNSIGNED_BYTE, bits); + // replacement for GL_LUMINANCE_ALPHA in core profile + if (format == GL_RG) { + functions->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_RED); + functions->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_RED); + functions->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_RED); + functions->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_GREEN); + } functions->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); functions->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); functions->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); diff --git a/src/qtmultimediaquicktools/qtmultimediaquicktools.qrc b/src/qtmultimediaquicktools/qtmultimediaquicktools.qrc index d5efb1f3e51abaa4f7b7dd909cf6d50cba7b5d8a..b8180e31f3cd25b055660d48b536244652da57c0 100644 --- a/src/qtmultimediaquicktools/qtmultimediaquicktools.qrc +++ b/src/qtmultimediaquicktools/qtmultimediaquicktools.qrc @@ -11,5 +11,17 @@ <file>shaders/triplanaryuvvideo.vert</file> <file>shaders/uyvyvideo.frag</file> <file>shaders/yuyvvideo.frag</file> + + <file>shaders/monoplanarvideo_core.vert</file> + <file>shaders/rgbvideo_core.frag</file> + <file>shaders/rgbvideo_swizzle_core.frag</file> + <file>shaders/rgbvideo_padded_core.vert</file> + <file>shaders/biplanaryuvvideo_core.frag</file> + <file>shaders/biplanaryuvvideo_core.vert</file> + <file>shaders/biplanaryuvvideo_swizzle_core.frag</file> + <file>shaders/triplanaryuvvideo_core.frag</file> + <file>shaders/triplanaryuvvideo_core.vert</file> + <file>shaders/uyvyvideo_core.frag</file> + <file>shaders/yuyvvideo_core.frag</file> </qresource> </RCC> diff --git a/src/qtmultimediaquicktools/shaders/biplanaryuvvideo_core.frag b/src/qtmultimediaquicktools/shaders/biplanaryuvvideo_core.frag new file mode 100644 index 0000000000000000000000000000000000000000..1261782f8c489772f5e120f5880f08475caf6894 --- /dev/null +++ b/src/qtmultimediaquicktools/shaders/biplanaryuvvideo_core.frag @@ -0,0 +1,16 @@ +#version 150 core +uniform sampler2D plane1Texture; +uniform sampler2D plane2Texture; +uniform mat4 colorMatrix; +uniform float opacity; +in vec2 plane1TexCoord; +in vec2 plane2TexCoord; +out vec4 fragColor; + +void main() +{ + float Y = texture(plane1Texture, plane1TexCoord).r; + vec2 UV = texture(plane2Texture, plane2TexCoord).ra; + vec4 color = vec4(Y, UV.x, UV.y, 1.); + fragColor = colorMatrix * color * opacity; +} diff --git a/src/qtmultimediaquicktools/shaders/biplanaryuvvideo_core.vert b/src/qtmultimediaquicktools/shaders/biplanaryuvvideo_core.vert new file mode 100644 index 0000000000000000000000000000000000000000..1c162785e1998792b9c7e013d19b28996d6fd513 --- /dev/null +++ b/src/qtmultimediaquicktools/shaders/biplanaryuvvideo_core.vert @@ -0,0 +1,14 @@ +#version 150 core +uniform mat4 qt_Matrix; +uniform float plane1Width; +uniform float plane2Width; +in vec4 qt_VertexPosition; +in vec2 qt_VertexTexCoord; +out vec2 plane1TexCoord; +out vec2 plane2TexCoord; + +void main() { + plane1TexCoord = qt_VertexTexCoord * vec2(plane1Width, 1); + plane2TexCoord = qt_VertexTexCoord * vec2(plane2Width, 1); + gl_Position = qt_Matrix * qt_VertexPosition; +} diff --git a/src/qtmultimediaquicktools/shaders/biplanaryuvvideo_swizzle_core.frag b/src/qtmultimediaquicktools/shaders/biplanaryuvvideo_swizzle_core.frag new file mode 100644 index 0000000000000000000000000000000000000000..48ef3fa2c791583a6959410e0889f6e0c0e95452 --- /dev/null +++ b/src/qtmultimediaquicktools/shaders/biplanaryuvvideo_swizzle_core.frag @@ -0,0 +1,16 @@ +#version 150 core +uniform sampler2D plane1Texture; +uniform sampler2D plane2Texture; +uniform mat4 colorMatrix; +uniform float opacity; +in vec2 plane1TexCoord; +in vec2 plane2TexCoord; +out vec4 fragColor; + +void main() +{ + float Y = texture(plane1Texture, plane1TexCoord).r; + vec2 UV = texture(plane2Texture, plane2TexCoord).ar; + vec4 color = vec4(Y, UV.x, UV.y, 1.); + fragColor = colorMatrix * color * opacity; +} diff --git a/src/qtmultimediaquicktools/shaders/monoplanarvideo_core.vert b/src/qtmultimediaquicktools/shaders/monoplanarvideo_core.vert new file mode 100644 index 0000000000000000000000000000000000000000..23b9bfae2c5abbf2f41a98475582e776f0634a6f --- /dev/null +++ b/src/qtmultimediaquicktools/shaders/monoplanarvideo_core.vert @@ -0,0 +1,10 @@ +#version 150 core +uniform mat4 qt_Matrix; +in vec4 qt_VertexPosition; +in vec2 qt_VertexTexCoord; +out vec2 qt_TexCoord; + +void main() { + qt_TexCoord = qt_VertexTexCoord; + gl_Position = qt_Matrix * qt_VertexPosition; +} diff --git a/src/qtmultimediaquicktools/shaders/rgbvideo_core.frag b/src/qtmultimediaquicktools/shaders/rgbvideo_core.frag new file mode 100644 index 0000000000000000000000000000000000000000..ac41c108d9d41b10bbba419fd417b74b142cdd9e --- /dev/null +++ b/src/qtmultimediaquicktools/shaders/rgbvideo_core.frag @@ -0,0 +1,10 @@ +#version 150 core +uniform sampler2D rgbTexture; +uniform float opacity; +in vec2 qt_TexCoord; +out vec4 fragColor; + +void main() +{ + fragColor = texture(rgbTexture, qt_TexCoord) * opacity; +} diff --git a/src/qtmultimediaquicktools/shaders/rgbvideo_padded_core.vert b/src/qtmultimediaquicktools/shaders/rgbvideo_padded_core.vert new file mode 100644 index 0000000000000000000000000000000000000000..d4fa8ad7685e8794bd551a435f5ce07872c868c8 --- /dev/null +++ b/src/qtmultimediaquicktools/shaders/rgbvideo_padded_core.vert @@ -0,0 +1,11 @@ +#version 150 core +uniform mat4 qt_Matrix; +uniform float width; +in vec4 qt_VertexPosition; +in vec2 qt_VertexTexCoord; +out vec2 qt_TexCoord; + +void main() { + qt_TexCoord = qt_VertexTexCoord * vec2(width, 1); + gl_Position = qt_Matrix * qt_VertexPosition; +} diff --git a/src/qtmultimediaquicktools/shaders/rgbvideo_swizzle_core.frag b/src/qtmultimediaquicktools/shaders/rgbvideo_swizzle_core.frag new file mode 100644 index 0000000000000000000000000000000000000000..e952124d12afcc8bb06bef3d3012f10311a6bfef --- /dev/null +++ b/src/qtmultimediaquicktools/shaders/rgbvideo_swizzle_core.frag @@ -0,0 +1,12 @@ +#version 150 core +uniform sampler2D rgbTexture; +uniform float opacity; +uniform bool hasAlpha; +in vec2 qt_TexCoord; +out vec4 fragColor; + +void main() +{ + vec4 v = texture(rgbTexture, qt_TexCoord); + fragColor = vec4(v.bgr, hasAlpha ? v.a : 1.0) * opacity; +} diff --git a/src/qtmultimediaquicktools/shaders/triplanaryuvvideo_core.frag b/src/qtmultimediaquicktools/shaders/triplanaryuvvideo_core.frag new file mode 100644 index 0000000000000000000000000000000000000000..ad81083f5dbe4ae240739ec5776a3bbe516f5c2f --- /dev/null +++ b/src/qtmultimediaquicktools/shaders/triplanaryuvvideo_core.frag @@ -0,0 +1,19 @@ +#version 150 core +uniform sampler2D plane1Texture; +uniform sampler2D plane2Texture; +uniform sampler2D plane3Texture; +uniform mat4 colorMatrix; +uniform float opacity; +in vec2 plane1TexCoord; +in vec2 plane2TexCoord; +in vec2 plane3TexCoord; +out vec4 fragColor; + +void main() +{ + float Y = texture(plane1Texture, plane1TexCoord).r; + float U = texture(plane2Texture, plane2TexCoord).r; + float V = texture(plane3Texture, plane3TexCoord).r; + vec4 color = vec4(Y, U, V, 1.); + fragColor = colorMatrix * color * opacity; +} diff --git a/src/qtmultimediaquicktools/shaders/triplanaryuvvideo_core.vert b/src/qtmultimediaquicktools/shaders/triplanaryuvvideo_core.vert new file mode 100644 index 0000000000000000000000000000000000000000..ebf3604e1b48865ab7f877decf458081d1816c96 --- /dev/null +++ b/src/qtmultimediaquicktools/shaders/triplanaryuvvideo_core.vert @@ -0,0 +1,17 @@ +#version 150 core +uniform mat4 qt_Matrix; +uniform float plane1Width; +uniform float plane2Width; +uniform float plane3Width; +in vec4 qt_VertexPosition; +in vec2 qt_VertexTexCoord; +out vec2 plane1TexCoord; +out vec2 plane2TexCoord; +out vec2 plane3TexCoord; + +void main() { + plane1TexCoord = qt_VertexTexCoord * vec2(plane1Width, 1); + plane2TexCoord = qt_VertexTexCoord * vec2(plane2Width, 1); + plane3TexCoord = qt_VertexTexCoord * vec2(plane3Width, 1); + gl_Position = qt_Matrix * qt_VertexPosition; +} diff --git a/src/qtmultimediaquicktools/shaders/uyvyvideo_core.frag b/src/qtmultimediaquicktools/shaders/uyvyvideo_core.frag new file mode 100644 index 0000000000000000000000000000000000000000..75c7de5a6a0d103309e14bf24d6a17d0b57540a6 --- /dev/null +++ b/src/qtmultimediaquicktools/shaders/uyvyvideo_core.frag @@ -0,0 +1,13 @@ +#version 150 core +uniform sampler2D yTexture; // Y component passed as GL_LUMINANCE_ALPHA, in uyvy Y = a +uniform sampler2D uvTexture; // UV component passed as RGBA macropixel, in uyvy U = r, V = b +uniform mat4 colorMatrix; +uniform float opacity; +in vec2 qt_TexCoord; +out vec4 fragColor; + +void main() +{ + vec3 YUV = vec3(texture(yTexture, qt_TexCoord).a, texture2D(uvTexture, qt_TexCoord).rb); + fragColor = colorMatrix * vec4(YUV, 1.0) * opacity; +} diff --git a/src/qtmultimediaquicktools/shaders/yuyvvideo_core.frag b/src/qtmultimediaquicktools/shaders/yuyvvideo_core.frag new file mode 100644 index 0000000000000000000000000000000000000000..010c4a5f273ae48bf783aecfc4e016afe50f16f8 --- /dev/null +++ b/src/qtmultimediaquicktools/shaders/yuyvvideo_core.frag @@ -0,0 +1,13 @@ +#version 150 core +uniform sampler2D yTexture; // Y component passed as GL_LUMINANCE_ALPHA, in yuyv Y = r +uniform sampler2D uvTexture; // UV component passed as RGBA macropixel, in uyvy U = g, V = a +uniform mat4 colorMatrix; +uniform float opacity; +in vec2 qt_TexCoord; +out vec4 fragColor; + +void main() +{ + vec3 YUV = vec3(texture(yTexture, qt_TexCoord).r, texture2D(uvTexture, qt_TexCoord).ga); + fragColor = colorMatrix * vec4(YUV, 1.0) * opacity; +}