多视频渲染过程中的彩色图片

时间:2014-06-24 12:22:50

标签: c++ linux qt opengl glsl

我目前正致力于将两个不同的视频流同时渲染到两个不同的OpenGL纹理。我使用QAbstractVideoSurface的实现来准备视频的每一帧,然后将其传递给我的OpenGL绘制方法。每帧以YUV编码到达,因此为了获得RGB值,我使用GLSL。

问题如下:每次我尝试绘制多个视频时,第一个视频正常播放,但另一个视频与频道有些混淆。

我的顶点着色器:

#version 420
attribute vec2 position;
attribute vec2 texcoord;
uniform mat4 modelViewProjectionMatrix;
varying vec2 v_texcoord;

void main()
{
    gl_Position = modelViewProjectionMatrix * vec4(position, 0, 1);
    v_texcoord = texcoord.xy;
}

我的片段着色器:

#version 420
varying vec2 v_texcoord;
uniform sampler2D s_texture_y;
uniform sampler2D s_texture_u;
uniform sampler2D s_texture_v;
uniform float s_texture_alpha;

void main(void)
{
    highp float y = texture2D(s_texture_y, v_texcoord).r;
    highp float u = texture2D(s_texture_u, v_texcoord).r - 0.5;
    highp float v = texture2D(s_texture_v, v_texcoord).r - 0.5;
    highp float r = y + 1.402 * v;
    highp float g = y - 0.344 * u - 0.714 * v;
    highp float b = y + 1.772 * u;
    gl_FragColor = vec4(r, g, b, s_texture_alpha);
}

结果如下图所示:

RGB/YUV issue

有时它是正确的,有时情况更糟,但第一个视频一直播放正确。

在对通道进行了一些愚弄后,我发现有时u变量在片段着色器中获得与v相同的值。我的纹理绑定如下:

uniformSamplers[0] = functions.glGetUniformLocation(program, "s_texture_y");
uniformSamplers[1] = functions.glGetUniformLocation(program, "s_texture_u");
uniformSamplers[2] = functions.glGetUniformLocation(program, "s_texture_v");

glActiveTexture(GL_TEXTURE0 + 0);
glBindTexture(GL_TEXTURE_2D, textRef);
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, image_width,  image_height, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, (GLvoid*)(image));
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_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);
functions.glUniform1i(uniformSamplers[0], 0);

glActiveTexture(GL_TEXTURE0 + 1);
glBindTexture(GL_TEXTURE_2D, textRefU);
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, image_width / 2,  image_height / 2, 0, GL_LUMINANCE,
             GL_UNSIGNED_BYTE, (GLvoid*)(image + image_width * image_height));
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_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);
functions.glUniform1i(uniformSamplers[1], 1);

glActiveTexture(GL_TEXTURE0 + 2);
glBindTexture(GL_TEXTURE_2D, textRefV);
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, image_width / 2,  image_height / 2, 0, GL_LUMINANCE,
             GL_UNSIGNED_BYTE, (GLvoid*)(image + image_height * image_width + (image_width / 2) * (image_height / 2)));
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_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);
functions.glUniform1i(uniformSamplers[2], 2);

1 个答案:

答案 0 :(得分:1)

问题非常严重,我专注于着色器,并花了几天时间试图找出那里的问题。然而问题出在纹理参考初始化中。所有三个引用都具有相同的值,因此我每次都覆盖相同的纹理。将所有三个值放在不同的值上,它可以完美地运行。