## RGB to YUV and YUV to RGB Questions

### RGB to YUV and YUV to RGB Questions

Goal: to unpack/pack to/from YUV to RGB24, so the data is then ready for the
equations below.

According to IEEE 1394 Digital Camera Specification 1.20 YUV formats are
defined as follows:

YUV 4:4:4 >>> What do the three numbers represent A:B:C?

Pn : Pixel number / packet >>> is this pixel per packet (0,1)(0,1)(0,1)...
>>> or is this (0,1)(2,3)(4,5)...

K : Pn x n (n=0..N-1) >>> What does K represent?
(Pn x N = Total pixel number / frame) >>> What is Pn x n/N?

U-(K+0) Y-(K+0) V-(K+0) U-(K+1)
Y-(K+1) V-(K+1) U-(K+2) Y-(K+2)
V-(K+2) U-(K+3) Y-(K+3) V-(K+3)

U-(K+Pn-4) Y-(K+Pn-4) V-(K+Pn-4) U-(K+Pn-3)
Y-(K+Pn-3) V-(K+Pn-3) U-(K+Pn-2) Y-(K+Pn-2)
V-(K+Pn-2) U-(K+Pn-1) Y-(K+Pn-1) V-(K+Pn-1)

An alternate representation for the IYU2 format is a 24 bit format used in
mode 0, which is the same as YUV (4:4:4), and is according to the following
pattern:

Byte 0 1 2 3 4
5
Sample U(K+0) Y(K+0) V(K+0) U(K+1) Y(K+1) V(K+1)

Horizontal Vertical
Y Sample Period 1 1
V Sample Period 1 1
U Sample Period 1 1

What do they mean by Sample Period?

For 4:4:4 it appears to be two consecutive pixels spread over 6 bytes along
horizontal lines that wrap down (or up?) to the next video line, with no
planer separation. Is this correct?

If the format is planer how do you determine the start position of each
plane?

Some of the other camera formats:

YUV 4:2:2
YUV 4:1:1
Y (mono)
IYUV/I420 (planar 4:2:0 or 4:1:1).
YV12 (planar 4:2:0 or 4:1:1).
YUY2 (packed 4:2:2).
UYVY (packed 4:2:2)
YVYU (packed 4:2:2)
YVU9 (planar 16:1:1)

Also when a signal stream comes in from a camera, using winmm.dll interface,
how do you determine which format the stream is in?

Thanks,
Tim
http://www.yqcomputer.com/
http://www.yqcomputer.com/

--------------

Conversion equations:

Floating point:

RGB to YUV (YCbCr)
Y = (0.299 * R) + (0.587 * G) + (0.114 * B)
U' = (B - Y) * 0.564 = (-0.169 * R) - (0.331 * G) + (0.500 * B)
V' = (R - Y) * 0.713 = (0.500 * R) - (0.419 * G) - (0.081 * B)

U' = Cr
V' = Cb

Luminance Y are encoded grayscale from 0 to 255
Chrominance U, V are encoded color information between -128 and +127
similar to color/hue and tint/saturation
Values saturate at ends of range
Average U,V values over 2x2 blocks of pixels
Human eye is less sensitive to variations in color than in brightness

YUV to RGB
R = Y + (1.403 * V')
G = Y - (0.344 * U') - (0.714 * V')
B = Y + (1.770 * U')

16 <= Y <= 235 [220 steps]
16 <= U'/Cr <= 239 [235 steps]
16 <= V'/Cb <= 239 [235 steps]

Fixed point:

Y = [(9798 R + 19235G + 3736 B) / 32768]
U = [(-4784 R - 9437 G + 4221 B) / 32768] + 128
V = [(20218R - 16941G - 3277 B) / 32768] + 128

Y should be clamped at 16 to prevent sub-black, the same way as it should be
clamped at 235 to prevent white>100%. This can be done by the use of a 256
entry lookup table.

--------------

### RGB to YUV and YUV to RGB Questions

Move info at www.fourcc.org

### RGB to YUV and YUV to RGB Questions

http://www.yqcomputer.com/

Regards,

Lupo

### RGB to YUV and YUV to RGB Questions

http://www.yqcomputer.com/

Also check the specs on your DV camera, most consumer cameras only capture
either 4:1:1 or 4:2:2 YUV. You usually have to get fairly high-end before
you can get 4:4:4.