Last active
August 1, 2024 01:57
-
-
Save crearo/0d50442145b63c6c288d1c1675909990 to your computer and use it in GitHub Desktop.
A fragment shader to convert NV12 to RGB.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** A fragment shader to convert NV12 to RGB. | |
* Input textures Y - is a block of size w*h. | |
* texture UV is of size w*h/2. | |
* Remember, both U and V are individually of size w/2*h/2, but they are interleaved. | |
* The layout looks like this : | |
* ---------- | |
* | | | |
* | Y | size = w*h | |
* | | | |
* |________| | |
* |UVUVUVUV|size = w/2*h/2 | |
* |UVUVUVUV|size = w/2*h/2 | |
* ---------- | |
*/ | |
precision highp float; | |
varying vec2 vTextureCoord; | |
uniform sampler2D sTextureY; | |
uniform sampler2D sTextureUV; | |
uniform float sBrightnessValue; | |
uniform float sContrastValue; | |
void main (void) { | |
float r, g, b, y, u, v; | |
// We had put the Y values of each pixel to the R,G,B components by GL_LUMINANCE, | |
// that's why we're pulling it from the R component, we could also use G or B | |
y = texture2D(sTextureY, vTextureCoord).r; | |
// We had put the U and V values of each pixel to the A and R,G,B components of the | |
// texture respectively using GL_LUMINANCE_ALPHA. Since U,V bytes are interspread | |
// in the texture, this is probably the fastest way to use them in the shader | |
u = texture2D(sTextureUV, vTextureCoord).r - 0.5; | |
v = texture2D(sTextureUV, vTextureCoord).a - 0.5; | |
// The numbers are just YUV to RGB conversion constants | |
r = y + 1.13983*v; | |
g = y - 0.39465*u - 0.58060*v; | |
b = y + 2.03211*u; | |
r = r * sContrastValue + sBrightnessValue; | |
g = g * sContrastValue + sBrightnessValue; | |
b = b * sContrastValue + sBrightnessValue; | |
// We finally set the RGB color of our pixel | |
gl_FragColor = vec4(r, g, b, 1.0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-Check width and linesize
-Check color order: ex
rgba
andbgra
....