Fix "conversion from 'double' to 'float'" warnings on MSVC2010.
[qt:qtmultimedia.git] / src / imports / multimedia / qsgvideonode_i420.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.  For licensing terms and
14 ** conditions see http://qt.digia.com/licensing.  For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights.  These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file.  Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 #include "qsgvideonode_i420.h"
42 #include <QtCore/qmutex.h>
43 #include <QtQuick/qsgtexturematerial.h>
44 #include <QtQuick/qsgmaterial.h>
45 #include <QtGui/QOpenGLContext>
46 #include <QtGui/QOpenGLFunctions>
47 #include <QtGui/QOpenGLShaderProgram>
48
49 QList<QVideoFrame::PixelFormat> QSGVideoNodeFactory_I420::supportedPixelFormats(
50                                         QAbstractVideoBuffer::HandleType handleType) const
51 {
52     QList<QVideoFrame::PixelFormat> formats;
53
54     if (handleType == QAbstractVideoBuffer::NoHandle)
55         formats << QVideoFrame::Format_YUV420P << QVideoFrame::Format_YV12;
56
57     return formats;
58 }
59
60 QSGVideoNode *QSGVideoNodeFactory_I420::createNode(const QVideoSurfaceFormat &format)
61 {
62     if (supportedPixelFormats(format.handleType()).contains(format.pixelFormat()))
63         return new QSGVideoNode_I420(format);
64
65     return 0;
66 }
67
68
69 class QSGVideoMaterialShader_YUV420 : public QSGMaterialShader
70 {
71 public:
72     void updateState(const RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial);
73
74     virtual char const *const *attributeNames() const {
75         static const char *names[] = {
76             "qt_VertexPosition",
77             "qt_VertexTexCoord",
78             0
79         };
80         return names;
81     }
82
83 protected:
84
85     virtual const char *vertexShader() const {
86         const char *shader =
87         "uniform highp mat4 qt_Matrix;                      \n"
88         "attribute highp vec4 qt_VertexPosition;            \n"
89         "attribute highp vec2 qt_VertexTexCoord;            \n"
90         "varying highp vec2 qt_TexCoord;                    \n"
91         "void main() {                                      \n"
92         "    qt_TexCoord = qt_VertexTexCoord;               \n"
93         "    gl_Position = qt_Matrix * qt_VertexPosition;   \n"
94         "}";
95         return shader;
96     }
97
98     virtual const char *fragmentShader() const {
99         static const char *shader =
100         "uniform sampler2D yTexture;"
101         "uniform sampler2D uTexture;"
102         "uniform sampler2D vTexture;"
103         "uniform mediump mat4 colorMatrix;"
104         "uniform lowp float opacity;"
105         ""
106         "varying highp vec2 qt_TexCoord;"
107         ""
108         "void main()"
109         "{"
110         "    mediump float Y = texture2D(yTexture, qt_TexCoord).r;"
111         "    mediump float U = texture2D(uTexture, qt_TexCoord).r;"
112         "    mediump float V = texture2D(vTexture, qt_TexCoord).r;"
113         "    mediump vec4 color = vec4(Y, U, V, 1.);"
114         "    gl_FragColor = colorMatrix * color * opacity;"
115         "}";
116         return shader;
117     }
118
119     virtual void initialize() {
120         m_id_matrix = program()->uniformLocation("qt_Matrix");
121         m_id_yTexture = program()->uniformLocation("yTexture");
122         m_id_uTexture = program()->uniformLocation("uTexture");
123         m_id_vTexture = program()->uniformLocation("vTexture");
124         m_id_colorMatrix = program()->uniformLocation("colorMatrix");
125         m_id_opacity = program()->uniformLocation("opacity");
126     }
127
128     int m_id_matrix;
129     int m_id_yTexture;
130     int m_id_uTexture;
131     int m_id_vTexture;
132     int m_id_colorMatrix;
133     int m_id_opacity;
134 };
135
136
137 class QSGVideoMaterial_YUV420 : public QSGMaterial
138 {
139 public:
140     QSGVideoMaterial_YUV420(const QVideoSurfaceFormat &format);
141     ~QSGVideoMaterial_YUV420();
142
143     virtual QSGMaterialType *type() const {
144         static QSGMaterialType theType;
145         return &theType;
146     }
147
148     virtual QSGMaterialShader *createShader() const {
149         return new QSGVideoMaterialShader_YUV420;
150     }
151
152     virtual int compare(const QSGMaterial *other) const {
153         const QSGVideoMaterial_YUV420 *m = static_cast<const QSGVideoMaterial_YUV420 *>(other);
154         int d = m_textureIds[0] - m->m_textureIds[0];
155         if (d)
156             return d;
157         else if ((d = m_textureIds[1] - m->m_textureIds[1]) != 0)
158             return d;
159         else
160             return m_textureIds[2] - m->m_textureIds[2];
161     }
162
163     void updateBlending() {
164         setFlag(Blending, qFuzzyCompare(m_opacity, qreal(1.0)) ? false : true);
165     }
166
167     void setCurrentFrame(const QVideoFrame &frame) {
168         QMutexLocker lock(&m_frameMutex);
169         m_frame = frame;
170     }
171
172     void bind();
173     void bindTexture(int id, int w, int h, const uchar *bits);
174
175     QVideoSurfaceFormat m_format;
176     QSize m_textureSize;
177
178     static const uint Num_Texture_IDs = 3;
179     GLuint m_textureIds[Num_Texture_IDs];
180
181     qreal m_opacity;
182     QMatrix4x4 m_colorMatrix;
183
184     QVideoFrame m_frame;
185     QMutex m_frameMutex;
186 };
187
188 QSGVideoMaterial_YUV420::QSGVideoMaterial_YUV420(const QVideoSurfaceFormat &format) :
189     m_format(format),
190     m_opacity(1.0)
191 {
192     memset(m_textureIds, 0, sizeof(m_textureIds));
193
194     switch (format.yCbCrColorSpace()) {
195     case QVideoSurfaceFormat::YCbCr_JPEG:
196         m_colorMatrix = QMatrix4x4(
197                     1.0f,  0.000f,  1.402f, -0.701f,
198                     1.0f, -0.344f, -0.714f,  0.529f,
199                     1.0f,  1.772f,  0.000f, -0.886f,
200                     0.0f,  0.000f,  0.000f,  1.0000f);
201         break;
202     case QVideoSurfaceFormat::YCbCr_BT709:
203     case QVideoSurfaceFormat::YCbCr_xvYCC709:
204         m_colorMatrix = QMatrix4x4(
205                     1.164f,  0.000f,  1.793f, -0.5727f,
206                     1.164f, -0.534f, -0.213f,  0.3007f,
207                     1.164f,  2.115f,  0.000f, -1.1302f,
208                     0.0f,    0.000f,  0.000f,  1.0000f);
209         break;
210     default: //BT 601:
211         m_colorMatrix = QMatrix4x4(
212                     1.164f,  0.000f,  1.596f, -0.8708f,
213                     1.164f, -0.392f, -0.813f,  0.5296f,
214                     1.164f,  2.017f,  0.000f, -1.081f,
215                     0.0f,    0.000f,  0.000f,  1.0000f);
216     }
217
218     setFlag(Blending, false);
219 }
220
221 QSGVideoMaterial_YUV420::~QSGVideoMaterial_YUV420()
222 {
223     if (!m_textureSize.isEmpty())
224         glDeleteTextures(Num_Texture_IDs, m_textureIds);
225 }
226
227 void QSGVideoMaterial_YUV420::bind()
228 {
229     QOpenGLFunctions *functions = QOpenGLContext::currentContext()->functions();
230
231     QMutexLocker lock(&m_frameMutex);
232     if (m_frame.isValid()) {
233         if (m_frame.map(QAbstractVideoBuffer::ReadOnly)) {
234             int fw = m_frame.width();
235             int fh = m_frame.height();
236
237             // Frame has changed size, recreate textures...
238             if (m_textureSize != m_frame.size()) {
239                 if (!m_textureSize.isEmpty())
240                     glDeleteTextures(Num_Texture_IDs, m_textureIds);
241                 glGenTextures(Num_Texture_IDs, m_textureIds);
242                 m_textureSize = m_frame.size();
243             }
244
245             const uchar *bits = m_frame.bits();
246             int bpl = m_frame.bytesPerLine();
247             int bpl2 = (bpl / 2 + 3) & ~3;
248             int offsetU = bpl * fh;
249             int offsetV = bpl * fh + bpl2 * fh / 2;
250
251             if (m_frame.pixelFormat() == QVideoFrame::Format_YV12)
252                 qSwap(offsetU, offsetV);
253
254
255             functions->glActiveTexture(GL_TEXTURE1);
256             bindTexture(m_textureIds[1], fw/2, fh / 2, bits + offsetU);
257             functions->glActiveTexture(GL_TEXTURE2);
258             bindTexture(m_textureIds[2], fw/2, fh / 2, bits + offsetV);
259             functions->glActiveTexture(GL_TEXTURE0); // Finish with 0 as default texture unit
260             bindTexture(m_textureIds[0], fw, fh, bits);
261
262             m_frame.unmap();
263         }
264
265         m_frame = QVideoFrame();
266     } else {
267         functions->glActiveTexture(GL_TEXTURE1);
268         glBindTexture(GL_TEXTURE_2D, m_textureIds[1]);
269         functions->glActiveTexture(GL_TEXTURE2);
270         glBindTexture(GL_TEXTURE_2D, m_textureIds[2]);
271         functions->glActiveTexture(GL_TEXTURE0); // Finish with 0 as default texture unit
272         glBindTexture(GL_TEXTURE_2D, m_textureIds[0]);
273     }
274 }
275
276 void QSGVideoMaterial_YUV420::bindTexture(int id, int w, int h, const uchar *bits)
277 {
278     glBindTexture(GL_TEXTURE_2D, id);
279     glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, w, h, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, bits);
280     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
281     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
282     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
283     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
284 }
285
286 QSGVideoNode_I420::QSGVideoNode_I420(const QVideoSurfaceFormat &format) :
287     m_format(format)
288 {
289     setFlag(QSGNode::OwnsMaterial);
290     m_material = new QSGVideoMaterial_YUV420(format);
291     setMaterial(m_material);
292 }
293
294 QSGVideoNode_I420::~QSGVideoNode_I420()
295 {
296 }
297
298 void QSGVideoNode_I420::setCurrentFrame(const QVideoFrame &frame)
299 {
300     m_material->setCurrentFrame(frame);
301     markDirty(DirtyMaterial);
302 }
303
304
305 void QSGVideoMaterialShader_YUV420::updateState(const RenderState &state,
306                                                 QSGMaterial *newMaterial,
307                                                 QSGMaterial *oldMaterial)
308 {
309     Q_UNUSED(oldMaterial);
310
311     QSGVideoMaterial_YUV420 *mat = static_cast<QSGVideoMaterial_YUV420 *>(newMaterial);
312     program()->setUniformValue(m_id_yTexture, 0);
313     program()->setUniformValue(m_id_uTexture, 1);
314     program()->setUniformValue(m_id_vTexture, 2);
315
316     mat->bind();
317
318     program()->setUniformValue(m_id_colorMatrix, mat->m_colorMatrix);
319     if (state.isOpacityDirty()) {
320         mat->m_opacity = state.opacity();
321         program()->setUniformValue(m_id_opacity, GLfloat(mat->m_opacity));
322     }
323
324     if (state.isMatrixDirty())
325         program()->setUniformValue(m_id_matrix, state.combinedMatrix());
326 }