Video storage format YUV420 NV12 NV21 i420 YV12

1) YUV Profile

YUV format has two categories: planar and packed.

For the planar YUV format, into continuous storage of all pixels Y, U subsequently stored in all pixels, followed by all the pixels of the V.

For the packed YUV format, Y of each pixel, U, V * is a continuous cross stored.

YUV, is divided into three components, "Y" represents brightness (Luminance or Luma), i.e. the gray value; and "U" and "V" is represented by chromaticity (Chrominance or Chroma), is to describe an image and color saturation, color designation for the pixel.

And we know the like RGB, YUV is a color coding method, is mainly used for a television system and the analog domain, it luminance information (Y) and color information (UV) separation, no UV information may be displayed as the complete image, just black and white, this design solves the problem of color television sets compatible with black and white TV. And, unlike the RGB YUV requirements as three separate video signals simultaneously transmitted, it transmits the YUV occupies very little bandwidth mode.

2) YUV format storage

Storage format YUV stream fact, its way of sampling is closely related to mainstream sampling in three ways, YUV4: 4: 4, YUV4: 2: 2, YUV4: 2: 0, on the detailed principles, you can learn through online other articles here I want to emphasize it is how to restore the YUV values ​​of each pixel from the code stream based on its sampling format, since only properly restore the YUV values ​​for each pixel in order to extract the YUV to RGB conversion formula by out RGB values ​​of each pixel, and displayed.

FIG using three representation intuitively acquired right, black dots represent the Y component of the pixel sampling points, a hollow circle indicates use of the UV component of the pixel.


To remember the following passage, extracted after the YUV components of each pixel will be used.

1. YUV 4: 4: 4 sampling, each corresponding to a set of UV component Y 8 + 8 + 8 = 24bits, 3 bytes.

2. YUV 4: 2: 2 sampling, each set of two UV share a Y component, a YUV accounting 8 + 4 + 4 = 16bits 2 bytes.

3. YUV 4: 2: 0 sampling, each set of four Y share a UV component representing a YUV 8 + 2 + 2 = 12bits 1.5 bytes.

3) YUV420 type

3.1) YUV420p and YUV420sp difference

Because YUV420 more commonly used here to highlight YUV420. YUV420 divided into two types: YUV420p and YUV420sp.

FIG YUV420sp format:


FIG YUV420p following data formats:


3.2) YUV420p and YUV420sp specific classification details

YUV420p: planar mode known planer, Y, U, V, respectively, and then a different plane, i.e. there are three planes.

YUV420p divided into: Their difference is stored in the order of UV just not the same.

I420: also called YU12, Android model. Memory storage order is Y, then deposit U, and finally deposit V. YYYYUUUVVV

YV12: the order is stored in memory Y, reload V, and finally deposit U. YYYVVVUUU

YUV420sp: also known as bi-planer dual or two-planer plane, Y plane, UV is stored in the same cross plane.

YUV420sp divided into: Their difference is stored in the order of UV just not the same.

NV12: IOS This is the only mode. Memory storage order is Y, then UV stored alternately. YYYYUVUVUV

NV21: Android mode. Memory storage order is Y, then deposit U, VU then stored alternately. YYYYVUVUVU

Official documents as follows:

YV12

All of the Y samples appear first in memory as an array of unsigned char values. This array is followed immediately by all of the V (Cr) samples. The stride of the V plane is half the stride of the Y plane, and the V plane contains half as many lines as the Y plane. The V plane is followed immediately by all of the U (Cb) samples, with the same stride and number of lines as the V plane (Figure 12).

Roughly means: first all the variable memory Y, followed by the memory V, V step size (i.e. width) Y is half the step size, the line V is high half of Y. V End followed later storage memory U, all of the U, and the same step height river line V, that is, the Y are half.

Figure 12:


NV12

All of the Y samples are found first in memory as an array of unsigned char values with an even number of lines. The Y plane is followed immediately by an array of unsigned char values that contains packed U (Cb) and V (Cr) samples, as shown in Figure 13. When the combined U-V array is addressed as an array of little-endian WORD values, the LSBs contain the U values, and the MSBs contain the V values. NV12 is the preferred 4:2:0 pixel format for DirectX VA. It is expected to be an intermediate-term requirement for DirectX VA accelerators supporting 4:2:0 video.

Figure 13:


Memory calculated 3.3) YUV420 of

width * hight = Y (Total)

U = Y / 4 V = Y / 4

Therefore, the length of the data in memory YUV420 is width * hight * 3/2 (i.e., a YUV 1.5 bytes), the acquired data size calculation: width * hight * 1.5 * frame * time

In the image size 720 × 488 YUV420 planar, for example,

Which is stored in the format: Total size is 720 × 480 × 3 × 1.5 bytes,

Divided into three parts: Y, U and V

Y component: (720 × 480) bytes

U (Cb) component: (720 × 480 × 1/4) bytes

V (Cr) component: (720 × 480 × 1/4) bytes

Three inner portion are stored in row-major, between the three parts is Y, U, V sequential storage.

I.e. 0--720 × 480 bytes YUV data Y component value,

720 × 480--720 × 480 × 5/4 bytes component U

720 × 480 × 5/4 --720 × 480 × 3/2 bytes is the V component.

In general, direct video data is collected to RGB24 format, a size of a size RGB24 = width × heigth × 3 Bit, RGB32 the size = width × heigth × 4, YUV standard format 4: 0 data amount: 2 is the size = width × heigth × 1.5 Bit.

After data collection RGB24 to, the need for data compression format for the first time. Color space of the image coming from the RGB2YUV. Because, X264 is necessary when performing coding standard YUV (4: 2: 0).

After the first data compression RGB24-> YUV (I420). Thus, the data amount is reduced by half, after X264 encoded, the data amount will be greatly reduced. The encoded data package transmitted in real time through the RTP. On arrival, the data out, is decoded. Upon completion of decoding, data is still YUV format, so, you also need a conversion is YUV2RGB24.

3.4) on IOS

IOS hardware decoding done all know, when you create a decoder, you need to specify PixelFormatType. IOS only supports YUV420 NV12 which is a kind of, you 420 search and found four, are as follows:

kCVPixelFormatType_420YpCbCr8Planar

kCVPixelFormatType_420YpCbCr8PlanarFullRange

kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange

kCVPixelFormatType_420YpCbCr8BiPlanarFullRange

The surface means, it can be seen, can be divided into two categories: planar (flat 420p) and BiPlanar (bi-planar).

There is also a way to distinguish between, CVPixelBufferGetPlaneCount (pixel) to obtain the number of the plane was found kCVPixelFormatType_420YpCbCr8Planar and kCVPixelFormatType_420YpCbCr8PlanarFullRange three sides, belongs to 420p, iOS does not support. KCVPixelFormatType_420YpCbCr8BiPlanarVideoRange and the two planes are kCVPixelFormatType_420YpCbCr8BiPlanarFullRange. This tangled, in the end by which one do?

I checked the official website data, explained as follows:

kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange = '420v', /* Bi-Planar Component Y'CbCr 8-bit 4:2:0, video-range (luma=[16,235] chroma=[16,240]).  baseAddr points to a big-endian CVPlanarPixelBufferInfo_YCbCrBiPlanar struct */

kCVPixelFormatType_420YpCbCr8BiPlanarFullRange  = '420f', /* Bi-Planar Component Y'CbCr 8-bit 4:2:0, full-range (luma=[0,255] chroma=[1,255]).  baseAddr points to a big-endian CVPlanarPixelBufferInfo_YCbCrBiPlanar struct */

In addition to the brightness and color sense of the scope is not the same, we did not find other different. Still tangled, then check Neurotechnology, some say there WWDC video, URL: https:? //Developer.apple.com/videos/play/wwdc2011/419/ time = 1527 (probably in the 25:30 ')

Explained as follows:


But I still do not know me, know them, please inform me.

I resolved then created using kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange and kCVPixelFormatType_420YpCbCr8BiPlanarFullRange, video playback did not find out what is not the same, the only different is calculated step size is not the same.

For example: 480 * 640

If you are: kCVPixelFormatType_420YpCbCr8BiPlanarFullRange

The Y and UV step 512 (using the non-aligned 64-byte aligned complement 0) Y line width is 640, UV line width is 320

If you are: kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange

The Y and UV step 480 (the actual length, not filled) Y line width is 640, the line width is 320 UV

I collected setPreset, so follow the instructions above (but I still do not quite understand), I finally chose the project which kCVPixelFormatType_420YpCbCr8BiPlanarFullRange.

Released eight original articles · won praise 18 · views 9425

Guess you like

Origin blog.csdn.net/weixin_38054045/article/details/104273628