• OpenGL播放yuv视频


    //data指向yuv420p数据
    - (void)playVideoData:(void *)data
    {
        [EAGLContext setCurrentContext:_glContext];
        if (!_textureY)
        {
            glGenTextures(1, &_textureY);
            glGenTextures(1, &_textureU);
            glGenTextures(1, &_textureV);
        }
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, _textureY);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RED_EXT, _videoW, _videoH, 0, GL_RED_EXT, GL_UNSIGNED_BYTE, data);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        
        
        glActiveTexture(GL_TEXTURE1);
        glBindTexture(GL_TEXTURE_2D, _textureU);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RED_EXT, _videoW/2, _videoH/2, 0, GL_RED_EXT, GL_UNSIGNED_BYTE, data + _videoW * _videoH);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        
        glActiveTexture(GL_TEXTURE2);
        glBindTexture(GL_TEXTURE_2D, _textureV);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RED_EXT, _videoW/2, _videoH/2, 0, GL_RED_EXT, GL_UNSIGNED_BYTE, data + _videoW * _videoH * 5 / 4);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        
        [self render];
    }

    - (void)render
    {
        [EAGLContext setCurrentContext:_glContext];
        glViewport(0, 0, 320, 320);
        glClearColor(0.0, 0.6, 0.0, 1.0);
        
        static c*****t GLfloat squareVertices[] = {
            -1.0f, -1.0f,
            1.0f, -1.0f,
            -1.0f,  1.0f,
            1.0f,  1.0f,
        };
        
        static c*****t GLfloat coordVertices[] = {
            0.0f, 1.0f,
            1.0f, 1.0f,
            0.0f,  0.0f,
            1.0f,  0.0f,
        };
    //    GLuint vertices;
    //    glGenBuffers(1, &vertices);
    //    glBindBuffer(GL_ARRAY_BUFFER, vertices);
    //    glBufferData(GL_ARRAY_BUFFER, sizeof(squareVertices), squareVertices, GL_STATIC_DRAW);
        
        glClear(GL_COLOR_BUFFER_BIT);
        
        glUseProgram(_program);
        
        // Update uniform value
        //glUniform1f(uniforms[UNIFORM_TRANSLATE], 0.0f);
        GLuint textureUniformY = glGetUniformLocation(_program, "SamplerY");
        GLuint textureUniformU = glGetUniformLocation(_program, "SamplerU");
        GLuint textureUniformV = glGetUniformLocation(_program, "SamplerV");
        
        // Update attribute values
        glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, 0, 0, squareVertices);
        glEnableVertexAttribArray(ATTRIB_VERTEX);
        
        glVertexAttribPointer(ATTRIB_TEXTURE, 2, GL_FLOAT, 0, 0, coordVertices);
        glEnableVertexAttribArray(ATTRIB_TEXTURE);
        
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, _textureY);
        glUniform1i(textureUniformY, 0);
        
        glActiveTexture(GL_TEXTURE1);
        glBindTexture(GL_TEXTURE_2D, _textureU);
        glUniform1i(textureUniformU, 1);
        
        glActiveTexture(GL_TEXTURE2);
        glBindTexture(GL_TEXTURE_2D, _textureV);
        glUniform1i(textureUniformV, 2);
        
        // Draw
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
        
        //glFlush();
        [_glContext presentRenderbuffer:GL_RENDERBUFFER];
    }

    //Shader.vsh
    attribute vec4 position; // 1
    //uniform float translate;
    attribute vec2 TexCoordIn; // New
    varying vec2 TexCoordOut; // New

    void main(void)
    {
        gl_Position = position; // 6
        TexCoordOut = TexCoordIn;
    }

    //Shader.fsh
    varying lowp vec2 TexCoordOut;

    uniform sampler2D SamplerY;
    uniform sampler2D SamplerU;
    uniform sampler2D SamplerV;

    void main(void)
    {
        mediump vec3 yuv;
        lowp vec3 rgb;
        
        yuv.x = texture2D(SamplerY, TexCoordOut).r;
        yuv.y = texture2D(SamplerU, TexCoordOut).r - 0.5;
        yuv.z = texture2D(SamplerV, TexCoordOut).r - 0.5;

        rgb = mat3( 1,       1,         1,
                    0,       -0.39465,  2.03211,
                    1.13983, -0.58060,  0) * yuv;
        
        gl_FragColor = vec4(rgb, 1);

    }
  • 相关阅读:
    阅读笔记——字节跳动公司的“字节范儿”
    阅读笔记——今日头条的人才机制
    对老师的评价及个人建议
    个人加分项
    阅读笔记——字节跳动创始人张一鸣的发展经历
    观影数据集之大数据分析
    阅读笔记
    谷粒商城分布式高级(八)—— 缓存SpringCache
    谷粒商城分布式高级(二)—— ElasticSearch全文检索
    谷粒商城分布式高级(五)—— 性能压测(压力测试 & 性能监控 & nginx动静分离 )
  • 原文地址:https://www.cnblogs.com/weinyzhou/p/4983472.html
Copyright © 2020-2023  润新知