YUV Format Learning: Fill YUV444 and Yuyv, Yvyu, UYVY, Vyuy

Source: Internet
Author: User


More than two years ago, a function was written to generate the Uyvy format. Remember when our department 4, 5 people in the "small black House" in the development of a new platform, I want to "kick ah" a chip to achieve the display of the screen, the format is Uyvy, because there is no actual image, so he wrote a. Although we make great effort to realize the video display, menu function, but unfortunately not used.

The YUV444 format is not compressed and occupies a space of with*height*3, which is the same as the RGB footprint. It is also convenient to convert, but many encoders do not seem to support this format. Maybe it's a great connection to the space it occupies.

Yuyv, Yvyu, UYVY, Vyuy, are all YUV422 packaging formats-that is, in memory, Y, U, and V are sorted next to each other. Their names represent the order of Y, U, v. Like Yuyv, y, u, y, V, y, u, y, v. When you fill in these formats, it's easy and easy.

Another common format is YUV420. Starting from this article, will focus on the format of the YUV conversion of the text, but did not study a lot, like the specific process of sampling is not well understood. Here is a picture, you can intuitively understand the appearance of YUV422, YUV420.

Back to the topic, this code is basically modified from a function written two years ago. The contents of the fill are different color bars. First the code, as follows:

void Init_yuv_buf (Yuv_type TYPE, unsigned char* buf, int width, int height) {unsigned char *src = BUF;

    int I, J; /* unsigned int rainbow_rgb[] = {0xFF0000, 0xff6100, 0xffff00, 0x00FF00, 0x00ffff, 0x0000FF, 0xa020f0
    , 0x000000, 0xFFFFFF, 0xf4a460}; *////from the array converted to unsigned int rainbow_yuv[] = {0x4c54ff, 0x8534d6, 0xe10094, 0x952b15, 0xb2ab00, 0x

    1dff6b, 0x5dd2af, 0xbb1654, 0x9c4bc5, 0XB450AD};
    unsigned char *p_y = src;
    unsigned char *p_u = src+width*height;

    unsigned char *p_v = src+2*width*height;
    int slice = HEIGHT/10;
        for (i = 0; i < height; i++)//h {int index = I/slice;
        unsigned char y = (Rainbow_yuv[index] & 0xff0000) >> 16;
        unsigned char u = (Rainbow_yuv[index] & 0x00ff00) >> 8;
        unsigned char v = (Rainbow_yuv[index] & 0X0000FF);
     if (type = = fmt_yuv444) {for (j=0;j<width;j++)//W {           *p_y++ = y;
                *p_u++ = u;
            *p_v++ = v; }} else {for (j=0; j<width*2; j+=4)//W {if (type = = Fmt_yuyv) {src[i*width*2+j+0] = y;//Y0 src[i*width*2+j+1] = u ; U src[i*width*2+j+2] = y; Y1 src[i*width*2+j+3] = v;  V} if (type = = Fmt_yvyu) {src[i*width*2+j+0] = y; Y0 src[i*width*2+j+1] = v; V src[i*width*2+j+2] = y; Y1 src[i*width*2+j+3] = u;  U} else if (type = = Fmt_uyvy) {src[i*width*2+j+0] = u; U src[i*width*2+j+1] = y; Y0 src[i*width*2+j+2] = v; V src[i*width*2+j+3] = y;
          Y1      } else if (type = = Fmt_vyuy) {src[i*width*2+j+0] = V;//V Src[i*width*2+j+1] = y; Y0 src[i*width*2+j+2] = u; U src[i*width*2+j+3] = y; Y1}}}}

Where RAINBOW_YUV is converted from the value of the RAINBOW_RGB array. Because I'm sure the color is using RGB space. The conversion functions used are as follows:

int rgb2ycbcr (unsigned int rgbColor, int* Y, int* Cb, int* Cr)
{
    unsigned char R, G, B;
    int y, CB, CR;
    R = (rgbcolor&0x00ff0000) >>;
    g = (rgbcolor&0x0000ff00) >> 8;
    b = RgbColor & 0xFF;

    y = (int) (0.299 * r + 0.587 * g + 0.114 * b);
    CB = (int) ( -0.16874 * r-0.33126 * g + 0.50000 * b + +);
    if (CB < 0)
        cb = 0;
    CR = (int) (0.50000 * r-0.41869 * g-0.08131 * b + N);
    if (CR < 0)
        cr = 0;

    *y = Y;
    *CB = Cb;
    *CR = Cr;

    return 0;
}

For YUV444, Y, U, v occupy a space of width*height, so at the time of filling, we will specify 3 components of the pointer, and then fill in turn.

For the four formats of YUYV, Yvyu, UYVY, Vyuy, which are actually YUV422 acquisition space, 2 y corresponds to 1 u and one U. Y, U, and V3 components are continuously stored, and according to the order, 4 formats are obtained. As you can see from the code, the 3 components are adjusted according to the different format macro definitions.

In short, the code of this article is not technical content, but for my study, still helpful, let's write it.


Li Yu 2015.8.5 Night

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.