天天看點

QT實作渲染yuv和rgb渲染yuv渲染rgb

渲染yuv

#ifndef XVIDEOWIDGET_H
#define XVIDEOWIDGET_H
​
#include <QOpenGLWidget>
#include <QOpenGLFunctions>
#include <QGLShaderProgram>
​
/**
 * @brief The XVideoWidget class 渲染YUV
 */
class XVideoWidget : public QOpenGLWidget,protected QOpenGLFunctions
{
    Q_OBJECT
​
public:
    XVideoWidget(QWidget *parent);
    ~XVideoWidget();
​
public slots:
    void slotShow(uchar *ptr,uint width,uint height); //顯示一幀Yuv圖像
​
protected:
    //重新整理顯示
    void paintGL();
​
    //初始化gl
    void initializeGL();
​
    // 視窗尺寸變化
    void resizeGL(int width, int height);
private:
    //shader程式
    QGLShaderProgram program;
​
    //shader中yuv變量位址
    GLuint unis[3] = {0};
    //openg的 texture位址
    GLuint texs[3] = {0};
​
    //材質記憶體空間
    unsigned char *datas[3] = {0};
​
    int width = 720;
    int height = 576;
};
​
#endif // XVIDEOWIDGET_H
​
           
#include "xvideowidget.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
XVideoWidget::XVideoWidget(QWidget *parent)
    : QOpenGLWidget(parent)
{
}
​
XVideoWidget::~XVideoWidget()
{
}
​
//初始化opengl
void XVideoWidget::initializeGL()
{
    qDebug() << "initializeGL";
​
    //初始化opengl (QOpenGLFunctions繼承)函數
    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);
​
    //Y
    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("/tmp/111.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 XVideoWidget::slotShow(uchar *ptr, uint width, uint height)
{
//    qDebug() << "slotShowYuv width:" << width << ", height:" << height;
    this->width = width;
    this->height = height;
    datas[0] = ptr;
    datas[1] = ptr + width*height;
    datas[2] = ptr + width*height*5/4;
    update();
}
​
//重新整理顯示
void XVideoWidget::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_TEXTURE1);
    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_TEXTURE2);
    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 XVideoWidget::resizeGL(int width, int height)
{
    qDebug() << "resizeGL "<<width<<":"<<height;
}
​
           

渲染rgb

#ifndef PAINTLABEL_H
#define PAINTLABEL_H
#include <QObject>
#include <QWidget>
​
/**
 * @brief The PaintLabel class 渲染RGB
 */
class PaintLabel : public QWidget
{
    Q_OBJECT
​
public:
    explicit PaintLabel(QWidget *parent = 0);
​
public slots:
    void slotShow(uchar *ptr, uint width, uint height);
​
protected:
    void paintEvent(QPaintEvent *event) override;
​
private:
    QImage image;
​
};
​
​
#endif // PAINTLABEL_H
​
           
#include <QPainter>
#include "paintlabel.h"
​
PaintLabel::PaintLabel(QWidget *parent) : QWidget(parent)
{
    setAttribute(Qt::WA_NoSystemBackground, true);
}
​
void PaintLabel::slotShow(uchar *ptr, uint width, uint height)
{
    image = QImage((uchar *)ptr, width, height, QImage::Format_RGB32).scaled(this->size());
    update();
}
​
void PaintLabel::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter painter;
    painter.begin(this);
    if (!image.isNull()) {
        const QPoint p = QPoint(0,0);
        painter.drawImage(p, image);
    }
    painter.end();
}
​