博客
关于我
[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/

你可能感兴趣的文章
postfix在邮件服务器中的使用
查看>>
PostGIS 3.1.2软件安装详细教程(地图工具篇.8)
查看>>
PostGIS中获取所有EPSG的编码以及对应Proj4字符串
查看>>
SpringBoot中集成海康威视SDK实现布防报警数据上传/交通违章图片上传并在linux上部署(附示例代码资源)
查看>>
PostGIS在Windows上的下载与安装
查看>>
Qt开发——网络编程之UDP客户端
查看>>
postgis数据库优化_postgresql 性能优化
查看>>
postgis求面积、交集等相关函数
查看>>
postgis相关函数
查看>>
Postgres Docker版本安装mysql_fdw 插件
查看>>
Postgres invalid command \N数据恢复处理
查看>>
Postgres like 模糊查询匹配集合
查看>>
Postgres 自定义函数内实现 in 操作符的递归查询
查看>>
Postgres 返回当前时间前后指定天数的集合
查看>>
postgres--vacuum
查看>>
postgres--wal
查看>>
postgres--流复制
查看>>
postgres10配置huge_pages
查看>>
PostgreSQL 10.0 preview sharding增强 - pushdown 增强
查看>>
PostgreSQL 10.0 preview 变化 - pg_xlog,pg_clog,pg_log目录更名为pg_wal,pg_xact,log
查看>>