-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVideoView.cpp
More file actions
353 lines (280 loc) · 9.28 KB
/
VideoView.cpp
File metadata and controls
353 lines (280 loc) · 9.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
Vidiot id a VIDeo Input Output Transformer With a Touch of Functionality
Copyright (C) 2016 Delicode Ltd
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "VideoView.h"
#include "VideoPlayer.h"
char glsl_color[] = {
"#version 150\n"
"uniform sampler2D tex;\n"
"in vec2 TexCoord;\n"
"out vec4 Out;\n"
"void main()\n"
"{\n"
"Out = texture(tex, TexCoord);\n"
"Out.a = 1.0;\n"
"}\n"
};
char glsl_quad_vert[] = {
"#version 150\n"
"in vec2 vertex;\n"
"in vec2 tcoord;\n"
"out vec2 TexCoord;\n"
"uniform vec2 scale;\n"
"void main( void ) {\n"
" gl_Position = vec4(scale*vertex, 0.0, 1.0);\n"
" TexCoord = tcoord;\n"
"}"
};
bool FboDrawer::ensureBuffers()
{
if(init_glbuffers) {
init_glbuffers = false;
if(!verts.isCreated()) {
GLER
float quad_verts[] = {
-1.f, 1.f,
1.f, 1.f,
-1.f, -1.f,
1.f, -1.f
};
if (!verts.create()) {
GLER
qDebug() << "failed creating vert buffer";
return false;
}
GLER
verts.setUsagePattern(QOpenGLBuffer::StaticDraw);
GLER
if (!verts.bind()) {
GLER
qDebug() << "failed binding verts buffer";
return false;
}
GLER
verts.allocate(quad_verts, 2*4*sizeof(float));
GLER
verts.release();
GLER
}
if(!tcoords.isCreated()) {
float quad_tcoords[] = {
0.f, 0.f,
1.f, 0.f,
0.f, 1.f,
1.f, 1.f
};
if (!tcoords.create()){
qDebug() << "failed creating tcoords buffer";
return false;
}
tcoords.setUsagePattern(QOpenGLBuffer::StaticDraw);
if (!tcoords.bind()) {
qDebug() << "failed binding tcoords buffer";
return false;
}
tcoords.allocate(quad_tcoords, 2*4*sizeof(float));
tcoords.release();
}
glfunc->glGenVertexArrays(1, &vaoQuad);
GLER
glfunc->glBindVertexArray(vaoQuad);
GLER
verts.bind();
GLER
glfunc->glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
GLER
glfunc->glEnableVertexAttribArray(0);
GLER
tcoords.bind();
GLER
glfunc->glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
GLER
glfunc->glEnableVertexAttribArray(1);
GLER
glfunc->glBindVertexArray(0);
GLER
}
return true;
}
void FboDrawer::drawFBO(QOpenGLFramebufferObject *fbo, QSize screen)
{
static bool gl_error_reported = false;
if(!screen_prog)
{
screen_prog = new QOpenGLShaderProgram();
}
GLenum glerr = glGetError();
if(glerr != GL_NO_ERROR) {
if(!gl_error_reported)
qDebug() << "Error while trying to draw live view: " + QString::number(glerr);
//return;
}
gl_error_reported = false;
if (!ensureBuffers()) {
qDebug() << "failed ensuring buffers";
return;
}
GLER
if(!screen_prog->isLinked()) {
qDebug() << QString("Linking ") + screen_prog->objectName();
screen_prog->removeAllShaders();
QString frag = QString(glsl_color);
if(!screen_prog->addShaderFromSourceCode(QOpenGLShader::Fragment, frag))
qDebug() << screen_prog->objectName() + " fragment compile: " + screen_prog->log();
QString vert = QString(glsl_quad_vert);
if(!screen_prog->addShaderFromSourceCode(QOpenGLShader::Vertex, vert))
qDebug() << screen_prog->objectName() + " vertex compile: " + screen_prog->log();
screen_prog->bindAttributeLocation("vertex", 0);
screen_prog->bindAttributeLocation("tcoord", 1);
if(!screen_prog->link())
qDebug() << screen_prog->objectName() + " link: " + screen_prog->log();
}
GLER
if(!screen_prog->isLinked())
return;
GLER
glfunc->glBindVertexArray(vaoQuad);
GLER
QOpenGLContext::currentContext()->functions()->glBindTexture(GL_TEXTURE_2D, fbo->texture());
float fbo_aspect = (float)fbo->width()/(float)fbo->height();
float screen_aspect = (float)screen.width()/(float)screen.height();
float scalex = 1;
float scaley = 1;
if(fbo_aspect < screen_aspect)
scalex = fbo_aspect/screen_aspect;
else
scaley = screen_aspect/fbo_aspect;
if(screen_prog->bind()) {
screen_prog->setUniformValue("tex", 0);
screen_prog->setUniformValue("scale", QVector2D(scalex, scaley));
QOpenGLContext::currentContext()->functions()->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
screen_prog->release();
}
GLER
glfunc->glBindVertexArray(0);
GLER
}
void FboDrawer::drawTexture(unsigned int texid, int width, int height, bool flip, QSize screen)
{
static bool gl_error_reported = false;
if(!screen_prog)
screen_prog = new QOpenGLShaderProgram();
GLenum glerr = glGetError();
if(glerr != GL_NO_ERROR) {
if(!gl_error_reported)
qDebug() << "Error while trying to draw live view: " + QString::number(glerr);
//return;
}
gl_error_reported = false;
if (!ensureBuffers()) {
qDebug() << "failed ensuring buffers";
return;
}
GLER
if(!screen_prog->isLinked()) {
qDebug() << QString("Linking ") + screen_prog->objectName();
screen_prog->removeAllShaders();
QString frag = QString(glsl_color);
if(!screen_prog->addShaderFromSourceCode(QOpenGLShader::Fragment, frag))
qDebug() << screen_prog->objectName() + " fragment compile: " + screen_prog->log();
QString vert = QString(glsl_quad_vert);
if(!screen_prog->addShaderFromSourceCode(QOpenGLShader::Vertex, vert))
qDebug() << screen_prog->objectName() + " vertex compile: " + screen_prog->log();
screen_prog->bindAttributeLocation("vertex", 0);
screen_prog->bindAttributeLocation("tcoord", 1);
if(!screen_prog->link())
qDebug() << screen_prog->objectName() + " link: " + screen_prog->log();
}
GLER
if(!screen_prog->isLinked())
return;
GLER
glfunc->glBindVertexArray(vaoQuad);
GLER
QOpenGLContext::currentContext()->functions()->glBindTexture(GL_TEXTURE_2D, texid);
float fbo_aspect = (float)width/(float)height;
float screen_aspect = (float)screen.width()/(float)screen.height();
float scalex = 1;
float scaley = 1;
if(fbo_aspect < screen_aspect)
scalex = fbo_aspect/screen_aspect;
else
scaley = screen_aspect/fbo_aspect;
if(flip == false)
scaley *= -1.f;
scalex = 1.f;
scaley = 1.f;
if(screen_prog->bind()) {
screen_prog->setUniformValue("tex", 0);
screen_prog->setUniformValue("scale", QVector2D(scalex, scaley));
QOpenGLContext::currentContext()->functions()->glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
screen_prog->release();
}
GLER
glfunc->glBindVertexArray(0);
GLER
}
class TextureInFboRenderer : public QQuickFramebufferObject::Renderer
{
public:
TextureInFboRenderer() {
texid = 0;
width = 0;
height = 0;
flip = true;
}
void render() {
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
GLenum er = glGetError();
if(er != GL_NO_ERROR)
qDebug() << "GLerror1" << er;
drawer.drawTexture(texid, width, height, flip, framebufferObject()->size());
}
void synchronize(QQuickFramebufferObject *item) {
VideoView *vv = static_cast<VideoView *>(item);
texid = vv->texid;
width = vv->texwidth;
height = vv->texheight;
flip = vv->texflip;
}
QOpenGLFramebufferObject *createFramebufferObject(const QSize &size) {
QOpenGLFramebufferObjectFormat fmt;
return new QOpenGLFramebufferObject(size, fmt);
}
protected:
private:
FboDrawer drawer;
unsigned int texid;
int width, height;
bool flip;
};
QQuickFramebufferObject::Renderer *VideoView::createRenderer() const
{
return new TextureInFboRenderer();
}
void VideoView::setMainView(QQuickView *v)
{
main_view = v;
}
void VideoView::receiveSourcesList(QStringList sourcesList)
{
if (main_view)
main_view->rootContext()->setContextProperty("sourcelist", sourcesList);
}
void VideoView::receiveResolutionList(QStringList list)
{
if(main_view)
main_view->rootContext()->setContextProperty("resolutionlist", list);
}