PPP
PPP

Reputation: 1850

Where did this YUV420P to RGB shader conversion come from?

I'm trying to understand these calculations for YUV420P to RGB conversion on an OpenGL fragment shader. On https://en.wikipedia.org/wiki/YUV there are lots of calculations but none of them look like the one below. Why take 0.0625 and 0.5 and 0.5 in the first part? And where did the second part come from?

yuv.r = texture(tex_y, TexCoord).r - 0.0625;
yuv.g = texture(tex_u, TexCoord).r - 0.5;
yuv.b = texture(tex_v, TexCoord).r - 0.5;

rgba.r = yuv.r + 1.596 * yuv.b
rgba.g = yuv.r - 0.813 * yuv.b - 0.391 * yuv.g;
rgba.b = yuv.r + 2.018 * yuv.g;

It may be an special color conversion for some specific YUV color scheme but I couldn't find anything on the internet.

Upvotes: 2

Views: 1214

Answers (1)

Rabbid76
Rabbid76

Reputation: 211116

Why take [...] 0.5 and 0.5 in the first part?

U and V are stored in the green and blue color channel of the texture. The values in the color channels are stored in the range [0.0, 1.0]. For the computations the values have to be in mapped to the range [-0.5, 0.5]:

yuv.g = texture(tex_u, TexCoord).r - 0.5;
yuv.b = texture(tex_v, TexCoord).r - 0.5;

Subtracting 0.0625 from the red color channel is just an optimization. Thus, It does not have to be subtracted separately in each expression later.

The algorithm is the same as in How to convert RGB -> YUV -> RGB (both ways) or various books.

Upvotes: 4

Related Questions