This article is excerpted from the original. In this article, I will add some notes on the content of the original author, and the key points will be marked in red.

Copyright notice: This article is originally published BY CSDN blogger “HardBoy”. It follows CC 4.0 BY-SA copyright agreement. Please attach the link of original source and this statement. The original link: blog.csdn.net/fanzonghao/…

Four, video coding

4.1. Color Space

YCbCr color space and its deformation (sometimes called YUV) are the most commonly used methods for representing color images efficiently. Y is the luminance/ Luma component of the image, calculated by the following formula, and is the weighted average value of R,G and B components:

Y = kr R + kgG + kbB

Where k is the weighting factor.

Where each chromatic difference component is the difference between R,G,B values and brightness Y:

Cb = b-y (note: blue color difference)

Cr = R – Y

Cg = G – Y

Where Cb+Cr+Cg is a constant (actually an expression of Y), so only two of the values combined with the Y value can be used to calculate the original RGB value. So, we just keep the brightness and the color difference between the blue and the red, which is (Y,Cb,Cr).

YCbCr color space has one significant advantage over RGB color space. Y can be stored at the same resolution as the original picture, but Cb and Cr can be stored at a lower resolution.

In RGB format, w* H *3 bytes (note: 1 byte for each pixel) are needed to store the RGB information of each pixel for a w width and H height picture, and the pixel data of the picture is continuously arranged.

In the YUV format, take the YUV420 format as an example. For a picture of width W and height H, its brightness Y data needs W * H bytes to represent (one brightness per pixel). Cb and Cr data are four pixels in the picture sharing a Cb and Cr value. So Cb is w times h over 4 bytes, Cr is w times h over 4 bytes.

Advantages of YUV:

1. The importance of YUV notation is that its luminance signal (Y) and chromaticity signal (U, V) are independent of each other.

2. Another advantage of YUV notation is that it can use the properties of human eyes to reduce the storage capacity required for digital color images. ———————————————— Copyright notice: This article is originally published BY CSDN blogger “HardBoy” under CC 4.0 BY-SA copyright agreement. Please attach the link of the original source and this statement. The original link: blog.csdn.net/fanzonghao/…

Copyright notice: This article is originally published BY CSDN blogger “Foreign Engineer”. It follows CC 4.0 BY-SA copyright agreement. Please attach the link of original source and this statement. Original link: blog.csdn.net/huayu\_huay…

4. Yuv sampling

In YUV, the sampling rate of UV is lower than that of Y, but the visual quality will not be reduced, because the human eye is sensitive to brightness and color information, and the chromaticity signal is not stored with full resolution, so there is no need for full sampling.

Currently there are YUV444, YUV422, YUV420, YUV411 and other sampling methods.

YUV444: for every 4 points Y sampling, there are 4 points Cb and 4 points Cr, and each pixel has corresponding chroma and brightness sampling information;

YUV422: indicates that for every 4 points Y sampling, there are 2 points Cb and 2 points Cr. When the 4:2:2 signal is decoded, the “missing” chromaticity sampling is usually supplemented by a certain interpolation point algorithm through chromaticity information operation on both sides;

YUV420: Literally, 4:2:0 should be for every 4 points Y sampled, there are 2 points Cb and 0 points Cr, but this is not the case at all. In fact, 4:2:0 means that the chromaticity sample is only half the luminance sample on each horizontal scan line, and half the luminance sample on each horizontal scan line! In other words, the chroma signal has half the resolution of the brightness signal, both horizontally and vertically. For example, if the entire screen is 720*480 in size, then the brightness signal is 720*480 and the chroma signal is only 360*240. In 4:2:0, the “missing” chromaticity sample is not only supplemented by the left and right adjacent samples through interpolation point calculation, but also the chromaticity sample of the whole row is obtained by interpolation point calculation of the chromaticity sample of the upper and lower rows

YUV411: indicates that for every 4 points of Y sampling, there will be 1 point Cb and 1 point Cr, but this is not the mainstream sampling method. ———————————————— Copyright notice: This article is originally published BY CSDN blogger “Foreign Engineer”. It follows CC 4.0 BY-SA copyright agreement. Please attach the link of the original source and this statement. Original link: blog.csdn.net/huayu\_huay…

Copyright notice: This article is originally published BY CSDN blogger “Liu Changfu”. It follows CC 4.0 BY-SA copyright agreement. Please attach the original source link and this statement. The original link: blog.csdn.net/u012618915/…

Each Y corresponds to a set of UV components, that is, each pixel has a Y, U and V component. There is no downsampling, as in RGB, which is rarely used.

As shown in the figure, the circle is the UV component, the cross is the Y component, their corresponding relationship is one to one, there are many blogs in this figure, from Microsoft’s official description of YUV, I see this figure also covered for a long time, this is how to save it, think for a long time this figure has nothing to do with storage, is to represent the corresponding relationship, How to store is FOURCC, packed and planar. 4:2:2 represents a 2:1 level down sampling without a vertical down sampling. Each scan line contains 4 y’s corresponding to 2 U’s or v’s. So every two Y’s share a set of UV components



4:2:0

Represents the horizontal sampling of 2:1, and the vertical sampling of 2:1. Each of the four Y’s share a set of UV components. The four Y’s are not four on a line but four on a square.

It has two kinds of correspondence, one is Mpeg1 and the other is Mpeg2

The figure above shows the corresponding mode specified by MPEg1



The figure above shows the corresponding mode specified by MPEG2

— — — — — — — —

Copyright notice: This article is originally published BY CSDN blogger “Liu Changfu”. It follows CC 4.0 BY-SA copyright agreement. Please attach the original source link and this statement.

Original link:Blog.csdn.net/u012618915/…

Note:

In fact, every frame and pixel of the original video file uses color, and it is a mixture of RGB three colors. Because the human eye is more sensitive to brightness, chroma is not so sensitive, so you can code RGB, calculate the brightness Y, and then chroma only need to calculate the outstanding difference on the line, anyway, when decoding in the future can restore the primary color according to the color difference. By converting RGB to YUV, the size of each frame is greatly reduced.

For YUV4:2-0, converted to YUV format, a frame of all pixels sampling brightness, and it is 4 pixels per sample, the four pixels is 2 X 2 square, sampling is level 2 pixels, vertical sampling two pixels, U and V sampling number is on the horizontal and vertical half of Y, So there’s only one U component and one V component in this square.