YUV

YUV is a color coding method. Y refers to Luminance and Luma, U and V to Chrominance and Chroma. Y ‘UV, YUV, YCbCr and YPbPr are actually the same thing. Due to historical relations, Y ‘UV and YUV are mainly used in color TV for analog signals, while YCbCr is used for digital video and image compression and transmission, such as MPEG and JPEG. Y ‘UV was originally designed to make color TVS compatible with black and white TVS. For black and white TV signal, there is no chroma information is (UV), so in color TV display refers to display brightness information. The Y, U and V data of a picture will be displayed separately as shown below:

Chroma sampling

Since the human eye is less sensitive to chromaticity than to brightness, the chromaticity component of the image does not need to have the same sharpness as the brightness component, so many video systems sample on the chromaticity channel with lower (relative brightness) sharpness (e.g., sampling frequency). In this way, the total bandwidth of the image signal is reduced without significantly reducing the picture quality. Chromaticity values lost due to sampling are replaced by interpolation, or by previous chromaticity values. In compressed images, 4:2:2y ‘CbCr, for example, requires only two-thirds of the bandwidth of R ‘g ‘b ‘(4:4:4), but has little impact on visual effects.

The sampling system of a video system is usually represented by a ratio of three: J: A: B (e.g. 4:2:2), which describes a conceptual region J pixels wide and two pixels high. Sometimes it is represented by a ratio of four (e.g. 4:2:2:4), which can be sequentially expressed as:

  • J: Horizontal sampling reference (width of concept area), usually 4;
  • A: the number of chroma samples in the first row of J pixels (Cr, Cb);
  • B: the number of additional chroma samples in the second row of J pixels (Cr, Cb);
  • Alpha: horizontal factor (associated with the leading value). If there is no such part, it can be ignored; if there is, it is the same as J.

YUV sampling format

To save bandwidth, most YUV formats use fewer than 24 bits per pixel on average. The main subsample formats are YCbCr 4:2:0, YCbCr 4:2:2, YCbCr 4:1:1 and YCbCr 4:4:4. The notation for YUV is called A:B:C notation:

  • 4:4:4 means complete sampling. Each Y corresponds to a set of UV components.
  • 4:2:2 represents 2:1 horizontal sampling and vertical complete sampling. Every two Y’s share a set of UV components.
  • 4:1:1 represents the horizontal sampling of 4:1, vertical complete sampling. Each of the four Y’s share a set of UV components.
  • 4:2:0 represents 2:1 horizontal sampling and 2:1 vertical sampling. Each of the four Y’s share a set of UV components.

YUV storage format

YUV storage comes in two formats:

  • Packed formats: Store Y, U, and V values in Macro Pixels arrays, similar to RGB. The Y, U and V components of each pixel point are stored continuously and alternately.
  • Planar formats: Store Y, U, and V components in different matrices. The Y component of all pixels is stored consecutively first, then the U component, and finally the V component.

Compressed format

Two common compression formats: YUYV and UYVY

YUYV/YUY2 (belonging to YUV422)

The Y component is sampled at each pixel, but its UV component is sampled at every other pixel, with each Y sharing a set of UV components. As shown in the figure, Y0 and Y1 share U0 V0 components, Y2 and Y3 share U2 V2 components, and so on.

UYVY (belonging to YUV422)

The order is opposite to YUYV, as shown in the figure, Y0 and Y1 share U0 V0 components, Y2 and Y3 share U2 V2 components, and so on.

Graphic format

The four common flat formats are YV12, I420, NV12, and NV21

YV12 (belonging to YUV420P)

Many software MPEG codecs use this format. It consists of a Y plane of size N x M, and a V plane of size N / 2 x M / 2, and a U plane.

The order of the components is Y, V, U.

I420 (belonging to YUV420P)

It’s basically the same as YV12, exceptThe order of components becomes Y, U, and V. So this format is also called YU12.

NV12, NV21 (belonging to YUV420SP)

NV12 is a format available in iOS, which stores Y components first, and then U and V are stored alternately.

NV21 is a format in Android, its storage order is to store Y component first, and then V U alternate storage.

RGB and YUV

RGB to YUV


Y = 0.299 R + 0.587 G + 0.114 B U = 0.169 R 0.331 G + 0.5 B + 128 V = 0.5 R 0.419 G 0.081 B + 128 \begin{array}{RLL} Y &= 0.299 * R + 0.587 * G + 0.114 * B \\ U &= -0.169 * r-0.331 * G + 0.5 * B + 128 \\ V &= 0.5 * R -0.419 * G – 0.081 * B + 128 \end{array}

[ Y U V ] = [ 0.299 0.587 0.114 0.169 0.331 0.5 0.5 0.419 0.081 ] [ R G B ] + [ 0 128 128 ] \begin{bmatrix} Y \\ U \\ V \end{bmatrix} = \begin{bmatrix} 0.299&0.587&0.114 \\ -0.169&-0.331&0.5 \\ 0.5& -0.419&-0.081 \end{bmatrix} \begin{bmatrix} R \ G \ B \end{bmatrix} + \begin{bmatrix} 0 \\ 128\128 \end{bmatrix}

YUV turn RGB


R = Y + 1.13983 ( V 128 ) G = Y 0.39465 ( U 128 ) 0.58060 ( V 128 ) B = Y + 2.03211 ( U 128 ) \begin{array}{RLL} R & = Y + 1.13983 * (V-128) \\ g& = y-0.39465 * (U-128) -0.58060 * (V-128) \\ b& = Y + 2.03211 * (U-128) \end{array}

[ R G B ] = [ 1 0.00093 1.401687 1 0.3437 0.71417 1 1.77216 0.00099 ] [ Y U 128 V 128 ] \begin{bmatrix} R \ G \ B \end{bmatrix} = \begin{bmatrix} 1&-0.00093&1.401687\1&-0.3437&-0.71417\1& 1.772&0.00099 \end{bmatrix} \begin{bmatrix} Y \\ u-128 \\ v-128 \end{bmatrix}

References:

Docs.microsoft.com/en-us/windo…

Docs.microsoft.com/zh-cn/previ…

En.wikipedia.org/wiki/Chroma…

en.wikipedia.org/wiki/YUV

Glumes.com/post/ffmpeg…

www.fourcc.org/yuv.php