There are two main types of YUV format: planar and packed.
For the planar YUV format, the y of all pixels is stored successively, followed by the U of all pixels, followed by the V of all pixels.
For the packed YUV format, the y,u,v of each pixel point is a continuous cross-storage.
YUV, divided into three components, "Y" for luminance (luminance or luma), that is, gray values, and "U" and "V" is the chroma (chrominance or chroma), the role is to describe the image color and saturation, to specify the color of the pixel.
Similar to our well-known RGB, YUV is also a color coding method, mainly used in television systems and analog video field, it separates the luminance information (Y) from the color information (UV), no UV information can display the full image, but black and white, This kind of design solves the problem of compatibility between color TV and black and white well. In addition, YUV does not require three independent video signals to be transmitted simultaneously as RGB, so it uses a very small bandwidth in YUV mode.
YUV Code Stream storage format is closely related to the way of sampling, the mainstream sampling method has three kinds, yuv4:4:4,yuv4:2:2,yuv4:2:0, about its detailed principles, can be learned through other articles on the Internet, What I want to emphasize here is how to restore the YUV value of each pixel from the stream according to its sampling format, because only the YUV value of each pixel is correctly restored, the RGB value of each pixel can be extracted by the conversion formula of YUV and RGB, and then displayed.
Use three graphs to visually represent the method of collection, using black dots to sample the Y component of the pixel, and a hollow circle to represent the UV component with that pixel point.
Remember the following paragraph, which will be used to extract the YUV component of each pixel.
- YUV 4:4:4 sampling, each y corresponds to a set of UV components.
- YUV 4:2:2 sampling, with a set of UV components per two Y.
- YUV 4:2:0 sampling, with a set of UV components per four Y.
2. Storage mode
Below I use the form of figure to give the common YUV stream storage, and after the storage method is attached to sampling the YUV data of each pixel point, wherein the meaning of Cb, cr equals U, V.
(1) Yuvy format (belongs to YUV422)
YUYV is a storage format for YUV422 sampling, the adjacent two Y share its adjacent two CB, CR, analysis, for the Pixel y ' 00, y ' 01, its CB, CR values are Cb00, Cr00, and other pixels of the YUV value in order and so on.
(2) UYVY format (belongs to YUV422)The UYVY format is also one of the storage formats for YUV422 sampling, except that unlike YUYV, the UV arrangement is different, and the method of restoring the YUV value of each pixel point is the same as above.
(3) yuv422p (belongs to YUV422)YUV422P is also a kind of YUV422, it is a plane mode, that is, the flat pattern, not to interleave YUV data storage, but first store all the Y components, and then store all the U (Cb) components, and finally store all the V (Cr) components, as shown in. The YUV value extraction method of each pixel point is also the most basic extraction method following the YUV422 format, that is, two y share a UV. For example, for Pixel y ' 00, y ' 01, its CB, CR values are Cb00, Cr00.
(4) yv12,yu12 format (belongs to YUV420)
YU12 and YV12 belong to the YUV420 format and are also a plane mode, which packages y, U, v components separately and stores them sequentially. The YUV data extraction of each pixel point follows the extraction method of the YUV420 format, that is, 4 y components share a set of Uvs. Note that in, y ' 00, y ' 01, y ' 10, y ' 11 share Cr00, Cb00, and so on.
(5) NV12, NV21 (belonging to YUV420)
NV12 and NV21 belong to the YUV420 format, which is a two-plane pattern, where Y and UV are divided into two plane, but Uvs (CBCR) are interleaved and not divided into three plane. The extraction method is similar to the previous one, that is, y ' 00, y ' 01, y ' 10, y ' 11 share Cr00, Cb00
YUV420 planar data, take 720x488 size image YUV420 Planar as an example,
Its storage format is: A total size of (720x480x3>>1) bytes,
Divided into three parts: Y,u and V
Y component: (720x480) bytes
U (CB) component: (720x480>>2) bytes
V (CR) component: (720x480>>2) bytes
Three parts are internally row-first storage, and three-part is y,u,v sequential storage.
That is, the 0--720x480 byte of YUV data is the Y component value,
720x480--720x480x5/4 bytes is the U component
The 720X480X5/4--720X480X3/2 byte is the V component.
4:2: 2 and 4:2:0 conversion:
The simplest way:
Yuv4:2:2---> yuv4:2:0 y are unchanged, the U and V signal values are sampled in rows (vertically) in a interlaced way. yuv4:2:0---> Yuv4:2:2 y, each line of the U and V signal values is copied one copy to form two consecutive lines of data.
In YUV420, one pixel corresponds to a Y, and a 4x4 small square corresponds to a U and V. For all YUV420 images, their Y-value arrangement is identical, because only the image of Y is the grayscale image. YUV420SP and yuv420p data formats their UV arrangement is completely different in principle. 420p It is the first to store the U, then store V, that is, UV they are continuous. and 420sp It is UV, UV such alternating storage. (see) with the above theory, I can accurately calculate the size of a YUV420 stored in memory. Width * hight =y (sum) U = Y/4 V = Y/4
So the length of the YUV420 data in memory is width * hight * 3/2,
Consider a YUV image with a resolution of 8x4, which is formatted as:
yuv420sp formats such as
yuv420p data formats such as
90-degree rotation algorithm:
public static void Rotateyuv240sp (byte[] src,byte[] des,int width,int height)
{
int WH = width * height;
Rotate Y
int k = 0;
for (int i=0;i<width;i++) {
for (int j=0;j{
Des[k] = src[width*j + i];
k++;
}
}
for (int i=0;i<width;i+=2) {
for (int j=0;j{
Des[k] = src[wh+ width*j + i];
DES[K+1]=SRC[WH + width*j + i+1];
k+=2;
}
}
}
The difference between YV12 and I420 in general, the directly captured video data is RGB24 format, RGB24 a frame of size size=widthxheigthx3 bit,rgb32 of SIZE=WIDTHXHEIGTHX 4, if it is I420 (i.e. YUV standard format 4:2:0) The amount of data is size=widthxheigthx1.5 Bit. After collecting the RGB24 data, it is necessary to compress the data in this format for the first time. The color space of the image will be rgb2yuv by. Because the X264 requires a standard YUV (4:2:0) when coding. However, it is important to note that although YV12 is also (4:2:0), YV12 and I420 are different, there are some differences in storage space. as follows: YV12: Brightness (Row x column) + U (row x column/4) + V (row x column/4)
I420: Brightness (Row x column) + V (row x column/4) + U (row x column/4)
It can be seen that YV12 and I420 are basically the same, that is, the UV order is different.
Continue our topic after the first Data compression RGB24->YUV (I420). In this way, the amount of data will be reduced by half, why? Oh, this is too basic, I will not write more. Similarly, if it is RGB24->YUV (YV12), it is also reduced by half. However, although all are half, if is YV12 words effect has very big loss. Then, after X264 encoding, the amount of data is greatly reduced. The encoded data is packaged for real-time delivery via RTP. When the destination is reached, the data is taken out and decoded. After the decoding is complete, the data is still in YUV format, so a conversion is required, so that the Windows driver can handle the YUV2RGB24.
YUY2 is 4:2:2 [Y0 U0 Y1 V0]
The difference between yuv420p and YUV420 is different in storage format yuv420p:yyyyyyyy uuuuuuuu vvvvv yuv420:yuv YUV YUV
Yuv420p,y,u,v three components are in flat format and are divided into I420 and YV12. The I420 format differs from the YV12 format in the position of the U plane and the V plane. In the I420 format, the U plane is immediately following the Y plane, then the V plane (i.e., YUV), but YV12 is the opposite (i.e.: YVU).
YUV420SP, y component planar format, UV packaging format, i.e. NV12. NV12 is similar to NV21, where U and V are staggered, unlike UV sequences.
i420:yyyyyyyy UU VV =>yuv420p
yv12:yyyyyyyy VV UU =>yuv420p
NV12:YYYYYYYY Uvuv =>YUV420SP
NV21:YYYYYYYY Vuvu =>YUV420SP
Transferred from: http://www.cnblogs.com/azraelly/archive/2013/01/01/2841269.html
"Turn" text detailed YUV420 data format