# Using YUV overlay and RGB(A) Surfaces

**URL:** <https://discourse.libsdl.org/t/using-yuv-overlay-and-rgb-a-surfaces/5010>\
**Category:** SDL Development\
**Created:** [September 4, 2001, 10:02am UTC](https://discourse.libsdl.org/t/using-yuv-overlay-and-rgb-a-surfaces/5010 "2001-09-04T10:02:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xavier\_Servettaz](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/x/bcef8e/32.png) [@Xavier\_Servettaz](https://discourse.libsdl.org/u/Xavier_Servettaz)\
**Post date:** [September 4, 2001, 10:02am UTC](https://discourse.libsdl.org/t/using-yuv-overlay-and-rgb-a-surfaces/5010/1 "2001-09-04T10:02:01Z")

</div>

I’m working on the SDL output plugin for the videolan project  
([www.videolan.org](http://www.videolan.org))

I want to display the YUV overlay AND some RGB(A) surfaces on the  
screen.

the overlay contains the decoded yuv images from the mpeg file. I want  
to add on-screen messages to this images.  
(Title, icons, text …). messages are bitmap data loaded in  
SDL\_Surfaces.  
I want to display the overlay as a background and add the messages in  
the foreground.

I’ve tried this :

SDL\_LockYUVOverlay(p\_vout-\>p\_sys-\>p\_overlay);

/\* copy the data into video buffers _/  
/_ Y first _/  
memcpy(p\_vout-\>p\_sys-\>p\_overlay-\>pixels[0], //to  
p\_vout-\>p\_rendered\_pic-\>p\_y, //from  
p\_vout-\>p\_sys-\>p\_overlay-\>h \* //size = height \* length of  
a row in bytes  
p\_vout-\>p\_sys-\>p\_overlay-\>pitches[0]);  
/_ then V _/  
memcpy(…);  
/_ and U \*/

disp.w = …  
disp.h = …  
disp.x = …  
disp.y = …

/\* add a BMP message \*/  
image = SDL\_LoadBMP(“videolan.bmp”); //my test - TODO: load image only  
once

dest.x = …  
dest.y  
…

/\* Display overlay \*/  
SDL\_DisplayYUVOverlay( p\_vout-\>p\_sys-\>p\_overlay , &disp );

SDL\_UnlockSurface ( p\_vout-\>p\_sys-\>p\_display );

SDL\_BlitSurface(image, NULL, p\_vout-\>p\_sys-\>p\_display, &dest);

/\* Flip screen to display message\*/  
//SDL\_Flip(p\_vout-\>p\_sys-\>p\_display); \<----- THISLINE

SDL\_LockSurface ( p\_vout-\>p\_sys-\>p\_display );

SDL\_UnlockYUVOverlay(p\_vout-\>p\_sys-\>p\_overlay);

//–  
If I try this I only the movie (overlay)

when I uncomment THISLINE: I only see my message on a black  
background…  
(detail: what exactly happend is: I can see the first image of my movie  
(I can’t say if it’s only 1 frame or 2) then my message (bmp) on a black  
background)

How can I do to see both ?

my overlay is accelerated (hw\_overlay).

Another question:  
Is it possible to “combine” overlay with RGB surfaces to obtain  
transparency (see the movie through the surface) At last will colorkey  
work ?

Thanks–  
Xavier Servettaz  
Ps: I’m new to overlay so perhaps I missunderstood something 🙂

---

<div class="post-metadata">

**Author:** ![slouken](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/ec9cab/32.png) [@slouken](https://discourse.libsdl.org/u/slouken)\
**Post date:** [September 4, 2001, 4:52pm UTC](https://discourse.libsdl.org/t/using-yuv-overlay-and-rgb-a-surfaces/5010/2 "2001-09-04T16:52:01Z")

</div>

> I want to display the YUV overlay AND some RGB(A) surfaces on the  
> screen.

> How can I do to see both ?

> my overlay is accelerated (hw\_overlay).

You can’t. It’s a physical property of the display. You have to  
convert your surface to YUV and “blit” into the YUV surface area.  
Unfortunately I don’t have code to do this, but if somebody writes it  
I’ll definitely add it to the SDL demos page.

> Another question:  
> Is it possible to “combine” overlay with RGB surfaces to obtain  
> transparency (see the movie through the surface) At last will colorkey  
> work ?

SDL YUV overlays currently don’t support the colorkey. This will be fixed  
in the SDL 1.3 redesign.

See ya,  
-Sam Lantinga, Software Engineer, Blizzard Entertainment

---

<div class="post-metadata">

**Author:** ![Xavier\_Servettaz](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/x/bcef8e/32.png) [@Xavier\_Servettaz](https://discourse.libsdl.org/u/Xavier_Servettaz)\
**Post date:** [September 11, 2001, 11:24am UTC](https://discourse.libsdl.org/t/using-yuv-overlay-and-rgb-a-surfaces/5010/3 "2001-09-11T11:24:00Z")

</div>

Sam Lantinga wrote:

> You can’t. It’s a physical property of the display. You have to  
> convert your surface to YUV and “blit” into the YUV surface area.  
> Unfortunately I don’t have code to do this, but if somebody writes it  
> I’ll definitely add it to the SDL demos page.

Ok, I’m working on it. (only for YUV 4.2.0 (YV12 overlay format))  
But I have some difficulties with U and V comversions.

I tried a lot of equations found on the Internet, but with no success.

I even “build” my own equations, but the result is not very clean…  
Pure blue, red, green and yellow seems to be good but not gray  
(U = (Uint8)b  
V = (Uint8)(0.5r - 0.5g + 128))

Does anyone of you know whitch one I should use ?  
Are U and V signed or unsigned (Uint8 or Sint8);[-127;128] or [0,255] ?

What I’ve noticed is that the walue 127 for both U and V seems to be  
“neutral” (good for gray, white…)

> SDL YUV overlays currently don’t support the colorkey. This will be fixed  
> in the SDL 1.3 redesign.

Cool

Another question, have you any idea about calculating transparency in  
YUV format ? I meen something like alpha transparency but in YUV  
format.(YUVA)

thanks–  
Xavier

---

<div class="post-metadata">

**Author:** ![Mikko\_Rantalainen](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/m/6bbea6/32.png) [@Mikko\_Rantalainen](https://discourse.libsdl.org/u/Mikko_Rantalainen)\
**Post date:** [September 11, 2001, 3:58pm UTC](https://discourse.libsdl.org/t/using-yuv-overlay-and-rgb-a-surfaces/5010/4 "2001-09-11T15:58:01Z")

</div>

> I tried a lot of equations found on the Internet, but with no success.
> 
> I even “build” my own equations, but the result is not very clean…  
> Pure blue, red, green and yellow seems to be good but not gray  
> (U = (Uint8)b  
> V = (Uint8)(0.5r - 0.5g + 128))
> 
> Does anyone of you know whitch one I should use ?  
> Are U and V signed or unsigned (Uint8 or Sint8);[-127;128] or [0,255] ?
> 
> What I’ve noticed is that the walue 127 for both U and V seems to be  
> “neutral” (good for gray, white…)

AFAIK 128 for U & V should be pure grey. I’ve seen following equations:

conversion from YUV to RGB  
r = 1.164\*(y-16) + 1.596\*(v-128);  
g = 1.164\*(y-16) - 0.813\*(v-128) - 0.391\*(u-128);  
b = 1.164\*(y-16) + 2.018\*(u-128);

conversion from RGB to YUV  
y = 0.257_r + 0.504_g + 0.098_b + 16  
u = -0.148_r - 0.291_g + 0.439_b + 128  
v = 0.439_r - 0.368_g - 0.071\*b + 128

I’m not sure if they’re correct, but results should be quite good… You  
might want to precalculate those multiplications though. Also if you don’t  
intend to use output for tv-out it might be better not to add 16 to  
y-component IMO. Some adjusting to factors might need to be done if this  
tweak is used.

- Mikko

---

<div class="post-metadata">

**Author:** ![Dave\_Newton](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/d/bbce88/32.png) [@Dave\_Newton](https://discourse.libsdl.org/u/Dave_Newton)\
**Post date:** [September 12, 2001, 2:49am UTC](https://discourse.libsdl.org/t/using-yuv-overlay-and-rgb-a-surfaces/5010/5 "2001-09-12T02:49:01Z")

</div>

Hi,

Try this routine (yep…I know it`s slow and uses doubles) it`s for video  
compression and for that I need reasonable accuracy. You could use int`s or even convert it to use bit shifts to speed it up. It`s actually Y:Cr:Cb but  
don`t tell anyone...they`ll only start an arugment about it :).

void rgb2yuv(Uint8 r, Uint8 g, Uint8 b, Uint8\* y, Uint8\* u, Uint8\* v)  
{  
//convert RGB colourspace to YUV  
double red, green, blue, yy, cr, cb, ry, ru, rv;  
red = (double)r / 255.0;  
green = (double)g / 255.0;  
blue = (double)b / 255.0;  
yy = 0.299 \* red + 0.587 \* green + 0.114 \* blue;  
cb = blue - yy;  
cr = red - yy;  
ry = 16.0 + 219.0 \* yy;  
ru = 128.0 + 126.0 \* cb;  
rv = 128.0 + 160.0 \* cr;  
\*y = (Uint8) ry;  
\*u = (Uint8) ru;  
\*v = (Uint8) rv;  
}

Hope that helps

Dave

P.s. This is my first post as I`ve been lurking for some time......I`m using  
SDL in a project for work and I`ll post some more info when i`m allowed to  
:). Good work sam!On Tuesday 11 September 2001 7:21 pm, you wrote:

> Does anyone of you know whitch one I should use ?  
> Are U and V signed or unsigned (Uint8 or Sint8);[-127;128] or [0,255] ?
> 
> What I’ve noticed is that the walue 127 for both U and V seems to be  
> “neutral” (good for gray, white…)

---

<div class="post-metadata">

**Author:** ![Xavier\_Servettaz](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/x/bcef8e/32.png) [@Xavier\_Servettaz](https://discourse.libsdl.org/u/Xavier_Servettaz)\
**Post date:** [September 12, 2001, 8:18am UTC](https://discourse.libsdl.org/t/using-yuv-overlay-and-rgb-a-surfaces/5010/6 "2001-09-12T08:18:01Z")

</div>

Thank you guys,

I found the good equations:

y = (Uint8)(0.299_r+0.587_g+0.098_b);  
u = (Sint8)(0.558_(b - y) +127);  
v = (Sint8)(0.709\*(r - y) +127);

from [http://www.video-demystified.com/mm/tutor/ch03.pdf](http://www.video-demystified.com/mm/tutor/ch03.pdf)

(I added 127 to U and V)

I have another question:

I load a RGB BMP whitch is 233x52 into a 16BPP surface

and surface-\>pitch is 468 (2_234) and not 2_233 = 466  
then I create an overlay:

overlay = SDL\_CreateYUVOverlay(image-\>w,  
image-\>h,  
SDL\_YV12\_OVERLAY, //YUV 4.2.0  
screen);

but  
overlay-\>pitches[0] = 236 //Y why not 233 ?  
overlay-\>pitches[1] = 120 //V  
overlay-\>pitches[2] = 120 //U

So when I convert my surface I’ve got a problem because line sizes are  
not equals and my picture “slides” from line to line…

Why are sizes differents ?  
How can I correct this?–  
Xav
