博客
关于我
[FFmpeg + OpenGL + OpenSL ES]OpenGL ES 渲染获取到的yuv数据 - 7
阅读量:273 次
发布时间:2019-03-01

本文共 2444 字,大约阅读时间需要 8 分钟。

在OpenGL中渲染YUV数据时,通常会使用3个纹理分别获取Y、U和V的值,然后通过公式转换为RGB颜色。这种方法让转换过程在GPU完成,效率远高于CPU。

渲染流程

YUV数据的转换涉及三个步骤:获取纹理数据、执行转换公式以及输出结果。每个步骤都需要精确的计算和优化。

获取纹理数据

在OpenGL中,使用顶点着色器和片元着色器来处理纹理数据。顶点着色器负责顶点坐标的处理,片元着色器则负责纹理的采样和转换。代码如下:

attribute vec4 av_Position;attribute vec2 af_Position;varying vec2 v_texPosition;void main() {    v_texPosition = af_Position;    gl_Position = av_Position;}

片元着色器代码:

precision mediump float;varying vec2 v_texPosition;uniform sampler2D sampler_y;uniform sampler2D sampler_u;uniform sampler2D sampler_v;void main() {    float y = texture2D(sampler_y, v_texPosition).r;    float u = texture2D(sampler_u, v_texPosition).r - 0.5;    float v = texture2D(sampler_v, v_texPosition).r - 0.5;    vec3 rgb = vec3(        y + 1.403 * v,        y - 0.344 * u - 0.714 * v,        y + 1.770 * u    );    gl_FragColor = vec4(rgb, 1.0);}

Java工程配置

在Android项目中,需要配置OpenGL渲染环境。创建opengl包,添加必要的GL类:

public class WlRender implements GLSurfaceView.Renderer {    private Context mContext;    private int program_yuv;    private int avPosition_yuv;    private int afPosition_yuv;    private int[] textureId_yuv;    private int width_yuv;    private int height_yuv;    private ByteBuffer y, u, v;    public WlRender(Context context) {        mContext = context;        // 初始化顶点和纹理坐标        // ...    }    @Override    public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) {        initRanderYUV();    }    @Override    public void onSurfaceChanged(GL10 gl10, int width, int height) {        GLES20.glViewport(0, 0, width, height);    }    @Override    public void onDrawFrame(GL10 gl10) {        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);        renderYUV();        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);    }    private void initRanderYUV() {        // 加载着色器程序        // ...        // 创建并绑纹理        GLES20.glGenTextures(3, textureId_yuv);        // 设置纹理参数        // ...    }    public void setYUVRenderData(int width, int height, byte[] y, byte[] u, byte[] v) {        this.width_yuv = width;        this.height_yuv = height;        this.y = ByteBuffer.wrap(y);        this.u = ByteBuffer.wrap(u);        this.v = ByteBuffer.wrap(v);    }    private void renderYUV() {        if (width_yuv > 0 && height_yuv > 0 && y != null && u != null && v != null) {            GLES20.glUseProgram(program_yuv);            // 启用顶点和纹理位置            // ...            // 绘制纹理            // ...        }    }}

注意事项

目前绘制存在问题,但程序能正常运行。后续需要解决这些问题,确保图像正确显示。

转载地址:http://udzo.baihongyu.com/

你可能感兴趣的文章
PCA---主成成分分析
查看>>
PCA和自动编码器:每个人都能理解的算法
查看>>
pca算法
查看>>
PCA降维demo
查看>>
SharePoint 2013 图文开发系列之定义站点模板
查看>>
PCB生产流程详解-ChatGPT4o作答
查看>>
PCB设计十条黄金法则
查看>>
SpringSecurity框架介绍
查看>>
PCI Express学习篇:Power Management(二)
查看>>
pcie握手机制_【博文连载】PCIe扫盲——Ack/Nak 机制详解(一)
查看>>
pcm转wav的方法及代码示例
查看>>
PC史上最悲剧的16次失败
查看>>
PC端恶意代码分析Lab1.1-5.1,从零基础到精通,收藏这篇就够了!
查看>>
PC端稳定性测试探索
查看>>
PC端编辑 但能在PC端模拟移动端预览的富文本编辑器
查看>>
PDB文件:每个开发人员都必须知道的
查看>>
springMVC学习(二)
查看>>
Pdfkit页眉和页脚
查看>>
PDF中的Pandoc语法突出显示不起作用
查看>>
pdf从结构新建书签_在PDF文件中怎样创建书签
查看>>