【发布时间】:2019-09-19 15:47:15
【问题描述】:
我知道同样的问题已经出现了很多次,我已经尝试了所有我能找到的解决方案,但没有任何效果。
我每帧接收 3 个缓冲区(每个缓冲区用于 Y、Cr、Cb)。我目前只是将它们绑定到红色通道。 Y 缓冲区是 1 字节/像素,Cr 和 Cb 缓冲区是 1/4 大小,因此它与我看到的使用 U 和 V 纹理贴图的宽度/2 和高度/2 的示例很好地联系在一起,如下所示;
int glFormat = GL20.GL_RED;
int glType = GL20.GL_UNSIGNED_BYTE;
int glInternalFormat = GL20.GL_RED;
GL20.glActiveTexture(GL20.GL_TEXTURE0);
GL20.glBindTexture(glTarget, yTexHandle);
GL20.glTexImage2D(glTarget, 0, glInternalFormat, width, height, 0, glFormat, glType, buffer);
int uvWidth = videoWidth/2;
int uvHeight = videoHeight/2;
GL20.glActiveTexture(GL20.GL_TEXTURE0+2);
GL20.glBindTexture(glTarget, cbTexHandle);
GL20.glTexImage2D(glTarget, 0, glInternalFormat, uvWidth, uvHeigth, 0, glFormat, glType, cbBuffer);
GL20.glActiveTexture(GL20.GL_TEXTURE0+1);
GL20.glBindTexture(glTarget, crTexHandle);
GL20.glTexImage2D(glTarget, 0, glInternalFormat, uvWidth, uvHeight, 0, glFormat, glType, crBuffer);
这是我的碎片着色器;
uniform sampler2D u_texture;
uniform sampler2D u_texture_cr;
uniform sampler2D u_texture_cb;
void main() {
float y = texture2D(u_texture, v_texCoords).r;
float u = texture2D(u_texture_cr, v_texCoords).r - 0.5;
float v = texture2D(u_texture_cb, v_texCoords).r - 0.5;
float r = y + 1.402 * v;
float g = y - 0.344 * u - 0.714 * v;
float b = y + 1.772 * u;
gl_FragColor = v_color * vec4(r, g, b, 1.0);
}
虽然它可能不是最好的转换算法,但我已经尝试了所有我能找到的替代方案,它看起来总是大致相同,非常绿色和非常粉红色。
我认为问题在于缓冲区本身,或者它们如何绑定到 GL,而不是片段着色器本身。我曾尝试切换 u 和 v,甚至尝试将 u 用于所有内容,将 v 用于所有内容,结果始终相同,因此当 u 和 v 缓冲区到达着色器时,它们似乎是不正确的。
我已经打印出 Cb 和 Cr 缓冲区的片段,以了解它们的值是什么,这是一个示例;
Cr: -124 Cb: 110
Cr: -126 Cb: 109
Cr: -127 Cb: 107
Cr: -128 Cb: 106
Cr: 127 Cb: 104
Cr: 127 Cb: 101
Cr: 127 Cb: 99
注意这是一个 Java ByteBuffer。我曾尝试使用 glType 作为 GL_BYTE 而不是 GL_UNSIGNED_BYTE,但它看起来更糟。我也尝试过使用 GL_ALPHA 或 GL_LUMINANCE 作为格式的 alpha 通道,GL_LUMINANCE 看起来略有不同,但输出仍然大致相同。
此外,我从中获取这些帧的软件包能够转换为 RGBA 帧,这非常有效,但这是一个昂贵的过程(~30ms 与 ~2ms 相比)。它也是一种本地方法,我找不到它的来源,所以我不知道它在后台做什么,但我想这证明当我得到缓冲区时它们是正确的。
更新
我尝试按照 MoDJ 的建议在着色器中实现伽玛和饱和度,使用该答案中的this (BT709 conversions)。输出仍然几乎相同。着色器导致了这个;
uniform sampler2D u_texture;
uniform sampler2D u_texture_cr;
uniform sampler2D u_texture_cb;
const float yScale = 255.0 / (235.0 - 16.0); //(BT709_YMax-BT709_YMin)
const float uvScale = 255.0 / (240.0 - 16.0); //(BT709_UVMax-BT709_UVMin)
float BT709_nonLinearNormToLinear(float normV) {
if (normV < 0.081) {
normV *= (1.0 / 4.5);
} else {
float a = 0.099;
float gamma = 1.0 / 0.45;
normV = (normV + a) * (1.0 / (1.0 + a));
normV = pow(normV, gamma);
}
return normV;
}
void main() {
float y = texture2D(u_texture, v_texCoords).r;
float u = texture2D(u_texture_cr, v_texCoords).r - 0.5;
float v = texture2D(u_texture_cb, v_texCoords).r - 0.5;
y = y - 16.0/255.0;
float r = y*yScale + v*uvScale*1.5748;
float g = y*yScale - u*uvScale*1.8556*0.101 - v*uvScale*1.5748*0.2973;
float b = y*yScale + u*uvScale*1.8556;
r = clamp(r, 0.0, 1.0);
g = clamp(g, 0.0, 1.0);
b = clamp(b, 0.0, 1.0);
r = BT709_nonLinearNormToLinear(r);
g = BT709_nonLinearNormToLinear(g);
b = BT709_nonLinearNormToLinear(b);
【问题讨论】:
-
您不能只将有符号整数数据解释为 UNORM 格式。
-
抱歉我不确定我是否关注?
-
你有很多问题,非线性采样,不处理伽马,看起来你没有对输出 RGB 值进行饱和。我建议您从一个完整的示例开始,并将该逻辑调整为您的 OpenGL 代码,请参阅此 SO 答案:stackoverflow.com/questions/53911662/…
-
缓冲区包含有符号整数,这不是我所期望的。我认为缓冲区应该使用
unsigned char,因为视频的值通常在 0-255 之间,有时在 16-240 之间。如果缓冲区是正确的,那么您在打印值时会隐式转换为有符号整数。
标签: java opengl-es opengl-es-2.0 fragment-shader