Repository navigation
Expand file tree
/
Copy pathXVideoWidget.cpp
More file actions
189 lines (162 loc) · 5.98 KB
/
Copy pathXVideoWidget.cpp
File metadata and controls
189 lines (162 loc) · 5.98 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
#include "XVideoWidget.h"
#include <QDebug>
#include <QCoreApplication>
#include <iostream>
#include <QTimer>
// 自动加双引号
#define GET_STR(x) #x
#define A_VER 3
#define T_VER 4
FILE *fp = NULL;
// 定点shader
const char *vString = GET_STR(
attribute vec4 vertexIn;
attribute vec2 textureIn;
varying vec2 textureOut;
void main(void) {
gl_Position = vertexIn;
textureOut = textureIn;
}
);
// 片元shader
const char *tString = GET_STR(
varying vec2 textureOut;
uniform sampler2D tex_y;
uniform sampler2D tex_u;
uniform sampler2D tex_v;
void main(void) {
vec3 yuv;
vec3 rgb;
yuv.x = texture2D(tex_y, textureOut).r;
yuv.y = texture2D(tex_u, textureOut).r - 0.5;
yuv.z = texture2D(tex_v, textureOut).r - 0.5;
rgb = mat3(1.0,1.0,1.0,
0.0,-0.39465,2.03211,
1.13983,-0.58060,0.0) * yuv;
gl_FragColor = vec4(rgb, 1.0);
}
);
// 准备yuv数据
// ffmpeg -i xxx.mp4 -t 10 -s 240x128 -pix_fmt yuv420p out240x128.yuv
XVideoWidget::XVideoWidget(QWidget *parent) : QOpenGLWidget(parent)
{
}
XVideoWidget::~XVideoWidget() {
}
// 初始化gl
void XVideoWidget::initializeGL() {
qDebug() << "initializeGL";
//获取程序当前运行目录
QString fileName = QCoreApplication::applicationDirPath();
qDebug() << "fileName " << fileName;
// 初始化opengl 函数
initializeOpenGLFunctions();
glClearColor(1.0, 1.0, 1.0,1.0);
std::cout << glGetString(GL_VERSION) << std::endl;
// 加载shader脚本 (定点和片元)脚本
qDebug() << program.addShaderFromSourceCode(QGLShader::Fragment, tString);
qDebug() << program.addShaderFromSourceCode(QGLShader::Vertex, vString);
// 设置定点坐标的变量
program.bindAttributeLocation("vertexIn", A_VER);
// 设置材质坐标
program.bindAttributeLocation("textureIn", T_VER);
// 编译shader
qDebug() << "program.link()" << program.link();
qDebug() << "program.bind()" << program.bind();
// 专递顶点和材质坐标
// 顶点
static const GLfloat ver[] = {
-1.0f, -1.0f,
1.0f, -1.0f,
-1.0f, 1.0f,
1.0f, 1.0f
};
// 材质
static const GLfloat tex[] = {
0.0f, 1.0f,
1.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f
};
// 顶点
glVertexAttribPointer(A_VER, 2, GL_FLOAT, 0, 0, ver);
glEnableVertexAttribArray(A_VER);
// 材质
glVertexAttribPointer(T_VER, 2, GL_FLOAT, 0, 0, tex);
glEnableVertexAttribArray(T_VER);
// 从shader获取材质
unis[0] = program.uniformLocation("tex_y");
unis[1] = program.uniformLocation("tex_u");
unis[2] = program.uniformLocation("tex_v");
// 创建材质
glGenTextures(3, texs);
// Y
glBindTexture(GL_TEXTURE_2D, texs[0]);
// 放大过滤,线性插值
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// 创建材质显卡空间
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, 0);
// U
glBindTexture(GL_TEXTURE_2D, texs[1]);
// 放大过滤,线性插值
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// 创建材质显卡空间
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, width / 2, height / 2, 0, GL_RED, GL_UNSIGNED_BYTE, 0);
// V
glBindTexture(GL_TEXTURE_2D, texs[2]);
// 放大过滤,线性插值
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// 创建材质显卡空间
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, width / 2, height / 2, 0, GL_RED, GL_UNSIGNED_BYTE, 0);
// 分配材质内存空间
datas[0] = new unsigned char[width * height]; // Y
datas[1] = new unsigned char[width * height / 4]; // U
datas[2] = new unsigned char[width * height / 4]; // V
// fp = fopen("/Users/amglfk/Desktop/lesson_code/src/out240x128.yuv", "rb");
// if ( !fp ) {
// qDebug() << "out240x128.yuv file open failed!";
// }
// qDebug() << " fp = " << fp;
// // 启动定时器
// QTimer *ti = new QTimer(this);
// connect(ti, SIGNAL(timeout()), this, SLOT(update()));
// ti->start(40);
}
// 刷新显示
void XVideoWidget::paintGL() {
qDebug() << "paintGL";
// if ( feof(fp) ) {
// fseek(fp, 0, SEEK_SET);
// }
// fread(datas[0], 1, width * height, fp);
// fread(datas[1], 1, width * height / 4, fp);
// fread(datas[2], 1, width * height / 4, fp);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texs[0]); // 0层绑定到Y材质
// 修改材质内容 (复制内存内容)
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RED, GL_UNSIGNED_BYTE, datas[0]);
// 与shader关联起来
glUniform1i(unis[0], 0);
glActiveTexture(GL_TEXTURE0 + 1);
glBindTexture(GL_TEXTURE_2D, texs[1]); // 0层绑定到U材质
// 修改材质内容 (复制内存内容)
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width / 2, height / 2, GL_RED, GL_UNSIGNED_BYTE, datas[1]);
// 与shader关联起来
glUniform1i(unis[1], 1);
glActiveTexture(GL_TEXTURE0 + 2);
glBindTexture(GL_TEXTURE_2D, texs[2]); // 0层绑定到V材质
// 修改材质内容 (复制内存内容)
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width / 2, height / 2, GL_RED, GL_UNSIGNED_BYTE, datas[2]);
// 与shader关联起来
glUniform1i(unis[2], 2);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
// 窗口尺寸变化
void XVideoWidget::resizeGL(int width, int height) {
qDebug() << "resizeGL";
qDebug() << "width = " << width;
qDebug() << "height = " << height;
}