-
Notifications
You must be signed in to change notification settings - Fork 0
/
videoplaywidget.cpp
201 lines (194 loc) · 6.55 KB
/
videoplaywidget.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
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
#include "videoplaywidget.h"
#define GET_STR(x) #x
// 顶点shader
const char* vertexStr = GET_STR(
attribute vec4 vertexIn;
attribute vec2 textureIn;
varying vec2 textureOut;
void main(void)
{
gl_Position = vertexIn;
textureOut = textureIn;
}
);
// 片元shader
const char* fragmentStr = 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);
}
);
VideoPlayWidget::VideoPlayWidget(QWidget* parent)
: QOpenGLWidget(parent)
{
}
VideoPlayWidget::~VideoPlayWidget() {}
void VideoPlayWidget::Init(int width, int height)
{
lock.lock();
this->width = width;
this->height = height;
delete pixels[0];
delete pixels[1];
delete pixels[2];
// 分配材质内存空间
pixels[0] = new unsigned char[width * height];
pixels[1] = new unsigned char[width * height / 4];
pixels[2] = new unsigned char[width * height / 4];
if (textures[0])
{
glDeleteTextures(3, textures);
}
// 创建材质
glGenTextures(3, textures);
// Y
glBindTexture(GL_TEXTURE_2D, textures[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, nullptr);
// U
glBindTexture(GL_TEXTURE_2D, textures[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, nullptr);
// V
glBindTexture(GL_TEXTURE_2D, textures[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, nullptr);
lock.unlock();
}
void VideoPlayWidget::Repaint(AVFrame* frame)
{
if (!frame)
{
return;
}
lock.lock();
if (!pixels[0] || this->width * this->height == 0 || frame->width != this->width || frame->height != this->height)
{
av_frame_free(&frame);
lock.unlock();
return;
}
if (width == frame->linesize[0])
{
memcpy(pixels[0], frame->data[0], width * height);
memcpy(pixels[1], frame->data[1], width * height / 4);
memcpy(pixels[2], frame->data[2], width * height / 4);
}
else
{
// 行对齐问题
for (int i = 0; i < height; i++)
{
memcpy(pixels[0] + width * i, frame->data[0] + frame->linesize[0] * i, width);
}
for (int i = 0; i < height / 2; i++)
{
memcpy(pixels[1] + width / 2 * i, frame->data[1] + frame->linesize[1] * i, width / 2);
}
for (int i = 0; i < height / 2; i++)
{
memcpy(pixels[2] + width / 2 * i, frame->data[2] + frame->linesize[2] * i, width / 2);
}
}
lock.unlock();
av_frame_free(&frame);
update();
}
void VideoPlayWidget::initializeGL()
{
qDebug() << QString::fromLocal8Bit("initializeGL执行");
lock.lock();
// 初始化OpenGL
initializeOpenGLFunctions();
// 加载shader脚本(顶点和片元)
shaderProgram.addShaderFromSourceCode(QGLShader::Vertex, vertexStr);
shaderProgram.addShaderFromSourceCode(QGLShader::Fragment, fragmentStr);
// 设置顶点坐标
shaderProgram.bindAttributeLocation("vertexIn", 3);
// 设置材质坐标
shaderProgram.bindAttributeLocation("textureIn", 4);
qDebug() << QString::fromLocal8Bit("shaderProgram.link()") << shaderProgram.link();
qDebug() << QString::fromLocal8Bit("shaderProgram.bind()") << shaderProgram.bind();
//传递顶点和材质坐标
// 顶点坐标
static const GLfloat vertexLocation[] =
{
-1.0f, -1.0f,
1.0f, -1.0f,
-1.0f, 1.0f,
1.0f, 1.0f
};
// 材质坐标
static const GLfloat textureLocation[] =
{
0.0f, 1.0f,
1.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f
};
// 顶点
glVertexAttribPointer(3, 2, GL_FLOAT, 0, 0, vertexLocation);
glEnableVertexAttribArray(3);
// 材质
glVertexAttribPointer(4, 2, GL_FLOAT, 0, 0, textureLocation);
glEnableVertexAttribArray(4);
// 从shader获取材质
uniformLocations[0] = shaderProgram.uniformLocation("tex_y");
uniformLocations[1] = shaderProgram.uniformLocation("tex_u");
uniformLocations[2] = shaderProgram.uniformLocation("tex_v");
lock.unlock();
}
void VideoPlayWidget::resizeGL(int w, int h)
{
lock.lock();
qDebug() << QString::fromLocal8Bit("重绘") << w << "x" << h;
lock.unlock();
}
void VideoPlayWidget::paintGL()
{
lock.lock();
glActiveTexture(GL_TEXTURE0);
// 0层绑定到Y材质
glBindTexture(GL_TEXTURE_2D, textures[0]);
// 修改材质内容
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RED, GL_UNSIGNED_BYTE, pixels[0]);
// 与shader uniform关联
glUniform1i(uniformLocations[0], 0);
glActiveTexture(GL_TEXTURE0 + 1);
// 1层绑定到U材质
glBindTexture(GL_TEXTURE_2D, textures[1]);
// 修改材质内容
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width / 2, height / 2, GL_RED, GL_UNSIGNED_BYTE, pixels[1]);
// 与shader uniform关联
glUniform1i(uniformLocations[1], 1);
glActiveTexture(GL_TEXTURE0 + 2);
// 2层绑定到V材质
glBindTexture(GL_TEXTURE_2D, textures[2]);
// 修改材质内容
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width / 2, height / 2, GL_RED, GL_UNSIGNED_BYTE, pixels[2]);
// 与shader uniform关联
glUniform1i(uniformLocations[2], 2);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
lock.unlock();
}