forked from wang-bin/QtAV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
169 lines (161 loc) · 5.42 KB
/
main.cpp
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
/******************************************************************************
Simple Player: this file is part of QtAV examples
Copyright (C) 2012-2016 Wang Bin <[email protected]>
* This file is part of QtAV (from 2016)
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 <QtAV>
#include <QtAV/ConvolutionShader.h>
#include <QtAV/VideoShaderObject.h>
#include <QtCore/qmath.h>
#include <QApplication>
#include <QtCore/QScopedPointer>
#include <QWidget>
using namespace QtAV;
#define GLSL(x) #x ""
class MyShader : public VideoShaderObject
{
public:
MyShader() { startTimer(50);}
protected:
void timerEvent(QTimerEvent*) {
static int t = 0;
const float b = float(50 - t%100)/50.0;
const float c = float(50 - 4*t%100)/50.0;
++t;
setProperty("bs", QVariant::fromValue(QVector<float>() << b << c));
}
private:
const char* userShaderHeader(QOpenGLShader::ShaderType type) const {
if (type == QOpenGLShader::Vertex)
return 0;
return GLSL(uniform vec2 bs;);
}
const char* userPostProcess() const {
return GLSL(
float lr = 0.3086;
float lg = 0.6094;
float lb = 0.0820;
float s = bs.g + 1.0;
float is = 1.0-s;
float ilr = is * lr;
float ilg = is * lg;
float ilb = is * lb;
mat4 m = mat4(
ilr+s, ilg , ilb , 0.0,
ilr , ilg+s, ilb , 0.0,
ilr , ilg , ilb+s, 0.0,
0.0 , 0.0 , 0.0 , 1.0);
gl_FragColor = m*gl_FragColor+bs.r;);
}
};
class MediumBlurShader : public ConvolutionShader
{
public:
MediumBlurShader() {setKernelRadius(2);}
protected:
virtual const float* kernel() const {
const float v = 1.0/(float)kernelSize(); //radius 1
static QVector<float> k;
k.resize(kernelSize());
k.fill(v);
return k.constData();
}
};
class WaveShader : public QObject, public VideoShader {
public:
WaveShader(QObject *parent = 0) : QObject(parent)
, t(0)
, A(0.06)
, omega(5)
{
startTimer(20);
}
protected:
void timerEvent(QTimerEvent*) {
t+=2.0*M_PI/50.0;
}
private:
const char* userShaderHeader(QOpenGLShader::ShaderType type) const {
if (type == QOpenGLShader::Vertex)
return 0;
return GLSL(
uniform float u_omega;
uniform float u_A;
uniform float u_t;
);
}
const char* userSample() const {
return GLSL(
vec4 sample2d(sampler2D tex, vec2 pos, int p)
{
vec2 pulse = sin(u_t - u_omega * pos);
vec2 coord = pos + u_A*vec2(pulse.x, -pulse.x);
return texture(tex, coord);
});
}
bool setUserUniformValues() {
// return false; // enable this line to call setUserUniformValue(Uniform&)
program()->setUniformValue("u_A", A);
program()->setUniformValue("u_omega", omega);
program()->setUniformValue("u_t", t);
return true;
}
// setUserUniformValues() must return false to call setUserUniformValue(Uniform&)
void setUserUniformValue(Uniform &u) {
if (u.name == "u_A") {
u.set(A);
} else if (u.name == "u_omega") {
u.set(omega);
} else if (u.name == "u_t") {
u.set(t);
}
}
private:
float t;
float A;
float omega;
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
if (a.arguments().size() < 2) {
qDebug("./shader file");
return 0;
}
VideoOutput vo[4];
QScopedPointer<VideoShader> shaders[4];
AVPlayer player;
struct {
QByteArray title;
VideoShader *shader;
} shader_list[] = {
{"No shader", NULL},
{"Wave effect shader", new WaveShader()},
{"Blur shader", new MediumBlurShader()},
{"Brightness+Saturation. (VideoShaderObject dynamic properties)", new MyShader()}
};
vo[0].widget()->move(0, 0);
for (int i = 0; i < 4; ++i) {
if (!vo[i].opengl())
qFatal("No opengl in the renderer");
player.addVideoRenderer(&vo[i]);
vo[i].widget()->setWindowTitle(shader_list[i].title);
vo[i].widget()->show();
vo[i].widget()->resize(500, 300);
vo[i].widget()->move(vo[0].widget()->x() + (i%2)*vo[0].widget()->width(), vo[0].widget()->y() + (i/2)*vo[0].widget()->height());
vo[i].opengl()->setUserShader(shader_list[i].shader);
shaders[i].reset(shader_list[i].shader);
}
player.play(a.arguments().at(1));
return a.exec();
}