YUV Data YUY2 to I420

Source: Internet
Author: User

/*

The main sampling formats are YCbCr 4:2:0, YCbCr 4:2:2, YCbCr 4:1:1, and YCbCr 4:4:4.
Where YCbCr 4:1:1 is more commonly used, meaning: each point holds a 8bit luminance value (that is, the Y value),
Each 2x2 point holds a Cr and CB value, and the image does not change much in the human eye.
So, the original use of RGB (R,g,b are 8bit unsigned) model, a point requires 8x3=24 bits (such as the first figure),
(after full sampling, YUV still accounts for 8bit). After sampling by 4:1:1, now on average only 8+ (8/4) + (8/4) =12bits (4 points, 8*4 (Y) +8 (U) +8 (V) =48bits) are required,
The average of each point is 12bits (as in the second figure). This compresses the image data by half.

Above only gives a theoretical example, in the actual data storage is likely to be different, the following gives several specific storage forms:

(1) YUV 4:4:4

YUV Three channel sampling rate is the same, so in the generated image, the three component information of each pixel is complete (usually 8 bits per component),
After 8-bit quantization, the uncompressed pixel occupies 3 bytes.

The following four pixels are: [Y0 U0 V0] [Y1 U1 V1] [Y2 U2 V2] [Y3 U3 V3]

The stored stream is: Y0 U0 V0 Y1 U1 V1 Y2 U2 V2 Y3 U3 V3

(2) YUV 4:2:2

The sampling rate of each chromatic channel is half that of the luminance channel, so the chromaticity sampling rate in the horizontal direction is only half 4:4:4.
For uncompressed 8-bit quantization images, a macro pixel consisting of two pixels adjacent to each other horizontally takes up 4 bytes of memory.

The following four pixels are: [Y0 U0 V0] [Y1 U1 V1] [Y2 U2 V2] [Y3 U3 V3]

The stored stream is: Y0 U0 Y1 V1 Y2 U2 Y3 V3

Mapped pixel points: [Y0 U0 V1] [Y1 U0 V1] [Y2 U2 V3] [Y3 U2 V3]

(3) YUV 4:1:1

The chroma sampling of the 4:1:1 is a 4:1 sampling of the chroma in the horizontal direction. This is still acceptable for low-end users and consumer products.
For video with uncompressed 8-bit quantization, a macro pixel consisting of 4 pixels adjacent to each other horizontally takes up 6 bytes of memory.

The following four pixels are: [Y0 U0 V0] [Y1 U1 V1] [Y2 U2 V2] [Y3 U3 V3]

The stored stream is: Y0 U0 Y1 Y2 V2 Y3

Mapped pixel points: [Y0 U0 V2] [Y1 U0 V2] [Y2 U0 V2] [Y3 U0 V2]

(4) yuv4:2:0

4:2:0 does not mean that there is only Y,CB and no CR component. It means that for each line of scanning, only one chroma component is stored at a sampling rate of 2:1.
Adjacent scan lines store different chroma components, that is, if one line is 4:2:0, the next line is 4:0:2, and the next line is 4:2:0 ...
And so on For each chroma component, the sampling rate for both horizontal and vertical directions is 2:1, so the sampling rate of chromaticity is 4:1.
For video with uncompressed 8-bit quantization, a macro pixel of each pixel adjacent to a 2x2 2 rows and 2 columns takes up 6 bytes of memory.

The following eight pixels are: [Y0 U0 V0] [Y1 U1 V1] [Y2 U2 V2] [Y3 U3 V3]

[Y5 U5 V5] [Y6 U6 V6] [Y7u7 V7] [Y8 U8 V8]

The stored stream is: Y0 U0 Y1 Y2 U2 Y3

Y5 V5 Y6 Y7 V7 Y8

Mapped pixel points are: [Y0 U0 V5] [Y1 U0 V5] [Y2 U2 V7] [Y3 U2 V7]

[Y5 U0 V5] [Y6 U0 V5] [Y7u2 V7] [Y8 U2 V7]

Transferred from: http://blog.csdn.net/yiheng_l/article/details/3789586
*/

/*
To put it simply, the YUV format has two types of layouts on storage: packed and plannar.
The way to packed is to pack up the neighboring pixels. For example, the horizontal direction of 2 pixels into a DWORD.
The Planner method is the opposite. The Y component and the UV component are completely separated to save.
YUY2 and YV12 are the most commonly used two delegates.
YUY2 is the packed way. Horizontal two pixels are packaged into a DWORD, and the UV sample rate is only half of Y,
This conforms to the human visual characteristic to be able to compress the data effectively, the concrete layout is [Y0, U0,y1,v0]. This format is common to MPEG1 decoders.
YV12 is commonly found in the decoder, which belongs to the Plannar mode.
For a MXN-sized video, the data layout is [Y:m x N] [U:M/2 x N/2] [V:M/2 x N/2].
This means that the UV sample rate is only half of y in both horizontal and vertical directions.


*/

/*
Save format for YUYV and YUY2 formats
+--------+--------+--------+--------+--------
| Y1 | U | Y2 | X 2 ....
+--------+--------+--------+--------+--------
1 Byte 2Byte 3Byte 4Byte
This format, every 4 bytes is a group. Each group holds 2 pixels of data, that is, two pixels in a row using the same Uvs.

*/

Transferred from: http://www.cnblogs.com/cplusplus/archive/2012/04/17/2453315.html

1 #defineuint8_t BYTE2     voidyuy2toi420 (intInwidth,intInheight, uint8_t *psrc, uint8_t *pDest)3     {4         intI, J;5         //First, specify the overall layout of the I420 data6uint8_t *u = pDest + (Inwidth *inheight);7uint8_t *v = U + (inwidth * inheight)/4;8 9          for(i =0; I < inheight/2; i++)Ten         { One             /*the strategy is to take two contiguous rows inside the outer loop*/     Auint8_t *src_l1 = PSRC + inwidth*2*2*i;//because of the reason of 4:2:2, so occupy memory, quite a pixel accounted for 2 bytes, 2 pixels to synthesize 4 bytes -uint8_t *src_l2 = src_l1 + inwidth*2;//YUY2 the next line of even lines -uint8_t *y_l1 = pDest + inwidth*2*i;//even rows theuint8_t *y_l2 = y_l1 + inwidth;//next line of even rows -              for(j =0; J < inwidth/2; J + +)//Inner Loop -             { -                 //Pels in one go//crafting two pixels at a time +                 //even lines, take full pixels; Y,u,v; The next line of the even line, taking only y -*y_l1++ = src_l1[0];//Y +*u++ = src_l1[1];//U A*y_l1++ = src_l1[2];//Y at*v++ = src_l1[3];//V -                 //this is only for Y . -*y_l2++ = src_l2[0]; -*y_l2++ = src_l2[2]; -                 //yuy2,4 pixels to a set of -SRC_L1 + =4; inSrc_l2 + =4; -             } to         } +}

YUV Data YUY2 to I420

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.