Detailed explanation of YUV420 data format

Original address: http://www.cnblogs.com/azraelly/archive/2013/01/01/2841269.html

1. Introduction to YUV

YUV definition: divided into three components, "Y" represents the brightness (Luminance or Luma), that is, the gray value; while "U" and "V" represent the chroma (Chrominance or Chroma), the role is to describe Image color and saturation, used to specify the color of the pixel.

YUV format : There are two categories: planar and packed.
For the planar YUV format, the Y of all pixels is stored consecutively, 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 are stored continuously.

YUV storage : The format is actually closely related to its sampling method. There are three mainstream sampling methods, YUV4:4:4, YUV4:2:2, YUV4:2:0. For its detailed principle, you can learn about it through other articles on the Internet, here What I want to emphasize is how to restore the YUV value of each pixel from the code stream according to its sampling format, because only by correctly restoring the YUV value of each pixel can each pixel be extracted through the YUV and RGB conversion formula. The RGB value of the pixel is then displayed. 

YUV features : It is also a color coding method, which separates brightness information (Y) from color information (UV). Without UV information, a complete image can be displayed, but in black and white. This design solves the problem of color Compatibility between TV and black and white TV . Moreover, YUV does not require three independent video signals to be transmitted at the same time like RGB, so transmission in YUV takes up very little bandwidth .

 Figure: Use three figures to visually represent the acquisition method. The black dot represents the Y component of the pixel that is sampled, and the hollow circle represents the UV component of the pixel.

2. Storage method

    Below I give the common storage method of YUV code stream in the form of a picture, and append the method of sampling the YUV data of each pixel point after the storage method, among which, the meanings of Cb and Cr are equivalent to U and V.

(1) YUVY format (belonging to YUV422)

YUYV is one of the storage formats of YUV422 sampling. Two adjacent Ys share their two adjacent Cb and Cr. Analysis, for the pixel points Y'00 and Y'01, the value of its Cb and Cr Both are Cb00, Cr00, and the YUV values ​​of other pixels are deduced in turn.
 
(2) UYVY format (belongs to YUV422)
The UYVY format is also one of the storage formats for YUV422 sampling, but the difference from YUYV is that the UV is arranged in a different order. The method of restoring the YUV value of each pixel is the same as above.
 
(3) YUV422P (belonging to YUV422)
YUV422P is also a type of YUV422. It is a Plane mode, that is, a plane mode. It does not store YUV data interleaved, but stores all Y components first, then stores all U (Cb) components, and finally stores all the The V(Cr) component, as shown above. The YUV value extraction method of each pixel is also the most basic extraction method that follows the YUV422 format, that is, two Ys share one UV. For example, for the pixel points Y'00 and Y'01, the values ​​of Cb and Cr are both Cb00 and Cr00.
(4) YV12, YU12 format (belonging to YUV420)

YU12 and YV12 belong to the YUV420 format, which is also a Plane mode. The Y, U, and V components are packaged separately and stored in sequence. The YUV data extraction of each pixel follows the extraction method of the YUV420 format, that is, the four Y components share a set of UVs. Note that in the above figure, Y'00, Y'01, Y'10, and Y'11 share Cr00 and Cb00, and so on.

(5) NV12, NV21 (belonging to YUV420)

NV12和NV21属于YUV420格式,是一种two-plane模式,即Y和UV分为两个Plane,但是UV(CbCr)为交错存储,而不是分为三个plane。其提取方式与上一种类似,即Y'00、Y'01、Y'10、Y'11共用Cr00、Cb00

YUV420 planar数据存储, 以720×488大小图象YUV420 planar为例,

其存储格式是: 共大小为(720×480×3>>1)字节,

分为三个部分: Y分量:       (720×480)个字节   U(Cb)分量:  (720×480>>2)个字节     V(Cr)分量:   (720×480>>2)个字节

三个部分内部均是行优先存储,三个部分之间是Y,U,V 顺序存储。

即YUV数据的0--720×480字节是Y分量值,    720×480--720×480×5/4字节是U分量    720×480×5/4 --720×480×3/2字节是V分量。

4 :2: 2 和4:2:0 转换:

最简单的方式:

YUV4:2:2 ---> YUV4:2:0  Y不变,将U和V信号值在行(垂直方向)在进行一次隔行抽样。 YUV4:2:0 ---> YUV4:2:2  Y不变,将U和V信号值的每一行分别拷贝一份形成连续两行数据。

在YUV420中,一个像素点对应一个Y,一个4X4的小方块对应一个U和V。对于所有 YUV420图像,它们的Y值排列是完全相同的,因为只有Y的图像就是灰度图像。YUV420sp与YUV420p的数据格式它们的UV排列在原理上是完 全不同的。420p它是先把U存放完后,再存放V,也就是说UV它们是连续的。而420sp它是UV、UV这样交替存放的。(见下图) 有了上面的理论,我就可以准确的计算出一个YUV420在内存中存放的大小。 width * hight =Y(总和) U = Y / 4   V = Y / 4

所以YUV420 数据在内存中的长度是 width * hight * 3 / 2,

 

假设一个分辨率为8X4的YUV图像,它们的格式如下图:

图:YUV420sp格式

 

图:YUV420p数据格式如下图

旋转90度的算法:

复制代码
public static void rotateYUV240SP(byte[] src,byte[] des,int width,int height)
 {
  int wh = width * height;
  //旋转Y
  int k = 0;
  for(int i=0;i<width;i++) {
   for(int j=0;j<height;j++)
   {
               des[k] = src[width*j + i];   
         k++;
   }
  }
  for(int i=0;i<width;i+=2) {
   for(int j=0;j<height/2;j++)
   { 
               des[k] = src[wh+ width*j + i]; 
               des[k+1]=src[wh + width*j + i+1];
         k+=2;
   }
  }
 }
复制代码

YV12和I420的区别        

一般来说,直接采集到的视频数据是RGB24的格式,RGB24一帧的大小size=width×heigth×3 Bit,RGB32的size=width×heigth×4,如果是I420(即YUV标准格式4:2:0)的数据量是 size=width×heigth×1.5 Bit。       在采集到RGB24数据后,需要对这个格式的数据进行第一次压缩。即将图像的颜色空间由RGB2YUV。因为,X264在进行编码的时候需要标准的 YUV(4:2:0)。但是这里需要注意的是,虽然YV12也是(4:2:0),但是YV12和I420的却是不同的,在存储空间上面有些区别。如下:
经过第一次数据压缩后RGB24->YUV(I420)。数据量将减少一半, RGB24->YUV(YV12)有很大损失。经过X264编码后,数据量将大大减少。将编码后的数据打包,通过RTP实时传送。到达目的地后,将数据取出,进行解码。完成解码后,数据仍然是YUV格式的,需要一次转换,这样windows的驱动才可以处理,就是YUV2RGB24。

YV12 : 亮度(行×列) + U(行×列/4) + V(行×列/4)
I420 : 亮度(行×列) + V(行×列/4) + U(行×列/4)
YUY2  是 4:2:2  [Y0 U0 Y1 V0]
yuv420p:yyyyyyyy uuuuuuuu vvvvv
yuv420: yuv yuv yuv

 

YUV420P,Y,U,V三个分量都是平面格式,分为I420和YV12。在I420格式(即:YUV);但YV12则是相反(即:YVU)。
YUV420SP, Y分量平面格式,UV打包格式, 即NV12。 NV12与NV21类似,U 和 V 交错排列,不同在于UV顺序。
I420: YYYYYYYY UU VV    =>YUV420P
YV12: YYYYYYYY VV UU    =>YUV420P
NV12: YYYYYYYY UVUV     =>YUV420SP
NV21: YYYYYYYY VUVU     =>YUV420SP

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324743221&siteId=291194637