有效绘制字节数组流以在 Android 中显示的选项

Cre*_*tar 5 android opengl-es surfaceview textureview

简单来说,我需要做的就是在Android中显示视频帧的实时流(每帧都是YUV420格式)。我有一个回调函数,我在其中接收单个帧作为字节数组。看起来像这样的东西:

public void onFrameReceived(byte[] frame, int height, int width, int format) {
    // display this frame to surfaceview/textureview.
}
Run Code Online (Sandbox Code Playgroud)

一个可行但缓慢的选择是将字节数组转换为位图并绘制到 SurfaceView 上的画布上。将来,我希望能够改变该帧的亮度、对比度等,因此希望我可以使用 OpenGL-ES 来实现同样的目的。我还有哪些其他选择可以有效地做到这一点?

Camera请记住,与或类的实现不同MediaPlayer,我无法将输出定向到表面视图/纹理视图,camera.setPreviewTexture(surfaceTexture);因为我使用 C 中的 Gstreamer 接收单个帧。

WLG*_*Gfx 2

我在我的项目中使用 ffmpeg,但是渲染 YUV 帧的原理对于您来说应该是相同的。

例如,如果帧的尺寸为 756 x 576,则 Y 帧将是该尺寸。U 框架和 V 框架的宽度和高度是 Y 框架的一半,因此您必须确保考虑到尺寸差异。

我不知道相机 API,但我从 DVB 源获得的帧有宽度,而且每条线都有步幅。在帧中每行末尾添加额外像素。以防万一您的纹理坐标相同,请在计算纹理坐标时考虑到这一点。

调整纹理坐标以考虑宽度和步幅(线条大小):

float u = 1.0f / buffer->y_linesize * buffer->wid; // adjust texture coord for edge
Run Code Online (Sandbox Code Playgroud)

我使用的顶点着色器采用从 0.0 到 1.0 的屏幕坐标,但您可以更改这些坐标以适应。它还接受纹理坐标和颜色输入。我使用了颜色输入,以便我可以添加淡入淡出等。

顶点着色器:

#ifdef GL_ES
precision mediump float;
const float c1 = 1.0;
const float c2 = 2.0;
#else
const float c1 = 1.0f;
const float c2 = 2.0f;
#endif

attribute vec4 a_vertex;
attribute vec2 a_texcoord;
attribute vec4 a_colorin;
varying vec2 v_texcoord;
varying vec4 v_colorout;



void main(void)
{
    v_texcoord = a_texcoord;
    v_colorout = a_colorin;

    float x = a_vertex.x * c2 - c1;
    float y = -(a_vertex.y * c2 - c1);

    gl_Position = vec4(x, y, a_vertex.z, c1);
}
Run Code Online (Sandbox Code Playgroud)

片段着色器采用三个统一纹理,每个 Y、U 和 V 帧一个,并转换为 RGB。这也乘以从顶点着色器传入的颜色:

#ifdef GL_ES
precision mediump float;
#endif

uniform sampler2D u_texturey;
uniform sampler2D u_textureu;
uniform sampler2D u_texturev;
varying vec2 v_texcoord;
varying vec4 v_colorout;

void main(void)
{
    float y = texture2D(u_texturey, v_texcoord).r;
    float u = texture2D(u_textureu, v_texcoord).r - 0.5;
    float v = texture2D(u_texturev, v_texcoord).r - 0.5;
    vec4 rgb = vec4(y + 1.403 * v,
                    y - 0.344 * u - 0.714 * v,
                    y + 1.770 * u,
                    1.0);
    gl_FragColor = rgb * v_colorout;
}
Run Code Online (Sandbox Code Playgroud)

使用的顶点位于:

float   x, y, z;    // coords
float   s, t;       // texture coords
uint8_t r, g, b, a; // colour and alpha
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!

编辑:

对于 NV12 格式,您仍然可以使用片段着色器,尽管我自己没有尝试过。它将交错的 UV 作为亮度 Alpha 通道或类似通道。

请参阅此处,了解一个人如何回答这个问题:/sf/answers/1571981981/