对yuv文件使用OpenGLShader播放

这篇博客介绍了如何使用OpenGL和GLSL着色器语言处理YUV视频数据,包括顶点和片元着色器的编写,以及纹理映射和材质处理。代码示例展示了从YUV文件读取数据,创建纹理,并在OpenGL窗口中进行渲染的过程。
摘要由CSDN通过智能技术生成

一、相关知识
着色器语言GLSL
1.顶点着色器是针对每一个顶点执行一次,用于确定地顶点的位置。
片元着色器是针对每个片元执行一次,用于确定每个片元的颜色
2.语法基本与C相同
3.完美的支持向量和矩阵操作
4.GLSL提供了大量的内置函数来提供丰富的扩展功能。
5.它是通过限定符操作来管理输入输出类型。

二、代码:首先创建widget,创建类并选择基类为openGLWidget。

#include "XVideo.h"
#include <QDebug>
#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 v1080.mp4 -t 10 -s 240x128 -pix_fmt yuv420p  out240x128.yuv
XVideo::XVideo(QWidget *parent) :
    QOpenGLWidget(parent)
{

}
XVideo::~XVideo()
{

}
//初始化
void XVideo::initializeGL()
{
    //初始化OPenGl(OpenGLFunctions)函数
    initializeOpenGLFunctions();
    //program加载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();

            //传递顶点和材质坐标
            //顶点
            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);

            //绑定纹理
            glBindTexture(GL_TEXTURE_2D, texs[0]);
            //放大过滤,线性插值   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);

            ///分配材质内存空间
            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("out240x128.yuv","rb");
            if (!fp)
            {
                qDebug() << "out240x128.yuv file open failed!";
            }


            //启动定时器,定时刷新
            QTimer *ti = new QTimer(this);
            connect(ti, SIGNAL(timeout()), this, SLOT(update()));
            ti->start(40);
}

void XVideo::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 uni遍历关联
        glUniform1i(unis[0], 0);


        glActiveTexture(GL_TEXTURE0+1);
        glBindTexture(GL_TEXTURE_2D, texs[1]); //1层绑定到U材质
                                               //修改材质内容(复制内存内容)
        glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width/2, height / 2, GL_RED, GL_UNSIGNED_BYTE, datas[1]);
        //与shader uni遍历关联
        glUniform1i(unis[1],1);


        glActiveTexture(GL_TEXTURE0+2);
        glBindTexture(GL_TEXTURE_2D, texs[2]); //2层绑定到V材质
                                               //修改材质内容(复制内存内容)
        glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width / 2, height / 2, GL_RED, GL_UNSIGNED_BYTE, datas[2]);
        //与shader uni遍历关联
        glUniform1i(unis[2], 2);

        glDrawArrays(GL_TRIANGLE_STRIP,0,
                     4      //顶点数量
                     );
        qDebug() << "paintGL";
}

void XVideo::resizeGL(int width, int height)
{

}

可以使用OpenGL播放本地yuv文件,具体步骤如下: 1. 创建一个继承自QOpenGLWidget的类,并在其构造函数中初始化OpenGL相关设置: ```cpp class YUVPlayer : public QOpenGLWidget, protected QOpenGLFunctions { public: YUVPlayer(QWidget *parent = nullptr); ~YUVPlayer(); protected: void initializeGL() override; void paintGL() override; void resizeGL(int w, int h) override; void cleanupGL() override; private: GLuint m_textureY; GLuint m_textureU; GLuint m_textureV; GLsizei m_videoWidth; GLsizei m_videoHeight; }; ``` ```cpp YUVPlayer::YUVPlayer(QWidget *parent) : QOpenGLWidget(parent) { // 初始化OpenGL函数 initializeOpenGLFunctions(); // 设置背景色 glClearColor(0, 0, 0, 1); // 创建纹理 glGenTextures(1, &m_textureY); glGenTextures(1, &m_textureU); glGenTextures(1, &m_textureV); } ``` 2. 在initializeGL()函数中加载yuv文件,创建纹理并绑定到对应的纹理单元上: ```cpp void YUVPlayer::initializeGL() { // 加载yuv文件 QFile file("test.yuv"); if (!file.open(QIODevice::ReadOnly)) return; QByteArray data = file.readAll(); file.close(); m_videoWidth = 640; m_videoHeight = 480; // 创建纹理 glBindTexture(GL_TEXTURE_2D, m_textureY); glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, m_videoWidth, m_videoHeight, 0, GL_RED, GL_UNSIGNED_BYTE, data.data()); glBindTexture(GL_TEXTURE_2D, m_textureU); glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, m_videoWidth / 2, m_videoHeight / 2, 0, GL_RED, GL_UNSIGNED_BYTE, data.data() + m_videoWidth * m_videoHeight); glBindTexture(GL_TEXTURE_2D, m_textureV); glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, m_videoWidth / 2, m_videoHeight / 2, 0, GL_RED, GL_UNSIGNED_BYTE, data.data() + m_videoWidth * m_videoHeight * 5 / 4); // 设置纹理过滤方式 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // 绑定纹理单元 glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, m_textureY); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, m_textureU); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, m_textureV); } ``` 3. 在paintGL()函数中绘制纹理: ```cpp void YUVPlayer::paintGL() { glClear(GL_COLOR_BUFFER_BIT); // 绑定纹理并绘制 glBindTexture(GL_TEXTURE_2D, m_textureY); glActiveTexture(GL_TEXTURE0); glBegin(GL_QUADS); glTexCoord2i(0, 0); glVertex3f(-1, -1, 0); glTexCoord2i(1, 0); glVertex3f(1, -1, 0); glTexCoord2i(1, 1); glVertex3f(1, 1, 0); glTexCoord2i(0, 1); glVertex3f(-1, 1, 0); glEnd(); glBindTexture(GL_TEXTURE_2D, m_textureU); glActiveTexture(GL_TEXTURE1); glBegin(GL_QUADS); glTexCoord2i(0, 0); glVertex3f(-1, -1, 0); glTexCoord2i(1, 0); glVertex3f(1, -1, 0); glTexCoord2i(1, 1); glVertex3f(1, 1, 0); glTexCoord2i(0, 1); glVertex3f(-1, 1, 0); glEnd(); glBindTexture(GL_TEXTURE_2D, m_textureV); glActiveTexture(GL_TEXTURE2); glBegin(GL_QUADS); glTexCoord2i(0, 0); glVertex3f(-1, -1, 0); glTexCoord2i(1, 0); glVertex3f(1, -1, 0); glTexCoord2i(1, 1); glVertex3f(1, 1, 0); glTexCoord2i(0, 1); glVertex3f(-1, 1, 0); glEnd(); } ``` 4. 在resizeGL()函数中设置视口: ```cpp void YUVPlayer::resizeGL(int w, int h) { glViewport(0, 0, w, h); } ``` 5. 在程序中创建YUVPlayer类的对象,并显示: ```cpp int main(int argc, char *argv[]) { QApplication a(argc, argv); YUVPlayer player; player.show(); return a.exec(); } ``` 这样就可以使用OpenGL播放本地yuv文件了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风赤

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值