.h文件
继承三个类,两个QT的类,一个回调类,纯虚函数类,提供回调参数接口;
采用OpenGL显示画面,使用shader脚本
#pragma once
#include <QOpenGLWidget>
#include <QOpenGLFunctions>
#include <QGLShaderProgram>
#include <mutex>
#include "IVideoCall.h"
extern "C"
{
#include "libavcodec/avcodec.h"//解码
#include "libavutil/avutil.h"//工具
}
#pragma comment(lib,"avutil.lib")//工具库,包含打印错误信息函数
#pragma comment(lib,"avcodec.lib")//解码
class XVideoWidget : public QOpenGLWidget, protected QOpenGLFunctions, public IVideoCall
{
Q_OBJECT
public:
XVideoWidget(QWidget* parent);
~XVideoWidget();
//初始化视频宽高
virtual void Init(int width, int height);//重载的IVideoCall
//重绘,接收frame,不管成功与否都释放frame空间
virtual void Repaint(AVFrame* frame);//重载的IVideoCall
protected:
//重载父类的虚函数
//初始化GL
void initializeGL();
//窗口尺寸变化
void resizeGL(int w, int h);
//刷新显示
void paintGL();
private:
std::mutex mux;
//shader程序
QGLShaderProgram program;
//shader中的yuv变量地址
GLuint unis[3] = { 0 };
//openGL中的texture变量地址
GLuint texs[3] = { 0 };
//材质的内存空间
unsigned char* datas[3] = { 0 };
int width = 240;
int height = 0;
};
.cpp文件
#include "XVideoWidget.h"
#include <QDebug>
#include <QTimer>
#define A_VER 3
#define T_VER 4
FILE* fp = NULL;
//自动双引号
#define GET_STR(x) #x
//顶点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 x.mp4 -t 10 -s 240x128 -pix_fmt yuv420p out240x128.yuv
//-i是输入,-t是时长,-s是尺寸, -pix_fmt是格式
XVideoWidget::XVideoWidget(QWidget* parent)
: QOpenGLWidget(parent)
{
}
XVideoWidget::~XVideoWidget()
{
}
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-//
//重绘,接收frame
void XVideoWidget::Repaint(AVFrame* frame)
{
if (frame == NULL)
return;
mux.lock();
//保证尺寸正确
if (!datas[0] || this->width * this->height == 0 || frame->width != this->width || frame->height != this->height)
{
av_frame_free(&frame);//释放传进来的内存
mux.unlock();
return;
}
//行对齐问题
if (width == frame->linesize[0]) //无需对齐
{
memcpy(datas[0], frame->data[0], width * height);
memcpy(datas[1], frame->data[1], width * height / 4);
memcpy(datas[2], frame->data[2], width * height / 4);
}
else//行对齐问题,???
{
for (int i = 0; i < height; i++) //Y
memcpy(datas[0] + width * i, frame->data[0] + frame->linesize[0] * i, width);
for (int i = 0; i < height / 2; i++) //U
memcpy(datas[1] + width / 2 * i, frame->data[1] + frame->linesize[1] * i, width);
for (int i = 0; i < height / 2; i++) //V
memcpy(datas[2] + width / 2 * i, frame->data[2] + frame->linesize[2] * i, width);
}
mux.unlock();
av_frame_free(&frame);//释放传进来的内存
//刷新显示
update();
}
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-//
//初始化视频宽高
void XVideoWidget::Init(int width, int height)
{
mux.lock();//加锁,放置访问texs材质时清理和访问冲突
this->width = width;//重解封装类中获取视频宽高
this->height = height;
delete datas[0];
delete datas[1];
delete datas[2];
//分配材质内存空间
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
if (texs[0])//如果材质已经有了,下次先需要清理掉,再创建
{
glDeleteTextures(3, texs);
}
//创建材质
glGenTextures(3, texs);
//Y
glBindTexture(GL_TEXTURE_2D, texs[0]);
//放大缩小过滤GL_TEXTURE_MAG_FILTER GL_TEXTURE_MIN_FILTER,线性插值GL_LINEAR,临近差值效率高(GL_NEAREST),但马赛克严重
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);
mux.unlock();//解锁
}
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-//
//初始化openGL
void XVideoWidget::initializeGL()
{
qDebug() << "initializeGL ";
mux.lock();
//初始化openGL(继承QOpenGLFunctions)函数
initializeOpenGLFunctions();
//program加载shader(顶点和片元)脚本,都是shader的相关设置
//片元shader
qDebug() << program.addShaderFromSourceCode(QGLShader::Fragment, tString);
//顶点shader
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();
//传递顶点坐标,第三维默认是0
static const GLfloat ver[] = {
-1.0f,-1.0f,
1.0f,-1.0f,
-1.0f,1.0f,
1.0f,1.0f
};
//传递材质坐标,第三维默认是0
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");
mux.unlock();
}
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-//
//窗口尺寸变化
void XVideoWidget::resizeGL(int width, int heigth)
{
mux.lock();
qDebug() << "resizeGL " << width << heigth;
mux.unlock();
}
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-//
//刷新显示
void XVideoWidget::paintGL()
{
qDebug() << "paintGL ";
mux.lock();
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 unis变量关联
glUniform1i(unis[0], 0);
glActiveTexture(GL_TEXTURE0 + 1);
glBindTexture(GL_TEXTURE_2D, texs[1]);//1层绑定到Y材质
//修改材质内容(复制内存内容)
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width / 2, height / 2, GL_RED, GL_UNSIGNED_BYTE, datas[1]);
//与shader unis变量关联
glUniform1i(unis[1], 1);
glActiveTexture(GL_TEXTURE0 + 2);
glBindTexture(GL_TEXTURE_2D, texs[2]);//2层绑定到Y材质
//修改材质内容(复制内存内容)
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width / 2, height / 2, GL_RED, GL_UNSIGNED_BYTE, datas[2]);
//与shader unis变量关联
glUniform1i(unis[2], 2);
//显示
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
mux.unlock();
}
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-//
回调类
只是做了一个接口出去,提供视频流的播放函数,具体实现在XVideoWidget类
#pragma once
class AVFrame;
class IVideoCall
{
public:
virtual void Init(int width, int height) = 0;
virtual void Repaint(AVFrame* frame) = 0;
};