Original address: http://www.cnblogs.com/azraelly/archive/2013/01/01/2841269.html
1. About YUV
YUV definition: divided into three components, "Y" for luminance (luminance or luma), that is, gray value, and "U" and "V" is the chroma (chrominance or chroma), the role is to describe the color and saturation of the image, The color used to specify the pixel.
YUV Format : There are two major categories: 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 Storage : The 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.
YUV features : is also a color coding method, it will be the brightness information (Y) and color information (UV) separation, no UV information can display the full image, but is black and white, such a design to solve the color TV and black and white compatibility problems . 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 .
Figure: use three graphs to visually represent the method of collection, with a black dot to sample the Y component of the pixel, and a hollow circle to represent the UV component with that pixel point.
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 one of the storage formats 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, The YUV values of the other pixels are then followed by analogy.
(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 storage , 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 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 the YUV data is the Y component value, and the 720X480--720X480X5/4 byte is the U component 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:
Figure: YUV420SP format
Figure: yuv420p data formats such as
90-degree rotation algorithm:
Public Static voidROTATEYUV240SP (byte[] SRC,byte[] des,intWidthintheight) { intWH = width *height; //Rotate y intK =0; for(intI=0; i<width;i++) { for(intj=0; j) {Des[k]= Src[width*j +i]; K++; } } for(intI=0; i<width;i+=2) { for(intj=0; j2; 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 the size of a frame size=widthxheigthx3 bit,rgb32 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:
RGB24->YUV (I420) after the first data compression. The amount of data will be reduced by half, RGB24->YUV (YV12) a great loss. After X264 encoding, the amount of data will be 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 decoding, the data is still in YUV format, need a conversion, so that Windows driver can be processed, that is yuv2rgb24.
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)
YUY2 is 4:2:2 [Y0 U0 Y1 V0]
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. In the I420 format (ie: 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
Detailed YUV420 data format