YUV format conversion and storage

Source: Internet
Author: User
In general==========There are planar and packed modes.- Planar mode means: You have 3 separate images, one for each component,each image 8 bits/pixel. To get the real colored pixel, you have tomix the components from all planes. The resolution of planes may differ!- Packed mode means: you have all components mixed/interleaved together,so you have small "packs" of components in a single, big image.There are RGB and YUV colorspaces.- RGB: Red, Green and Blue components. Used by analog VGA monitors.- YUV: Luminance (Y) and Chrominance (U,V) components. Used by some  video systems, like PAL. Also most M(J)PEG/DCT based codecs use this.With YUV, they used to reduce the resolution of U,V planes:The most common YUV formats:FOURCC:    bpp: IEEE:      plane sizes: (w=width h=height of original image)444P       24   YUV 4:4:4  Y: w * h  U,V: w * hYUY2,UYVY  16   YUV 4:2:2  Y: w * h  U,V: (w/2) * h      [MJPEG]YV12,I420  12   YUV 4:2:0  Y: w * h  U,V: (w/2) * (h/2)  [MPEG, H.263]411P       12   YUV 4:1:1  Y: w * h  U,V: (w/4) * h      [DV-NTSC, CYUV]YVU9,IF09   9   YUV 4:1:0  Y: w * h  U,V: (w/4) * (h/4)  [Sorenson, Indeo]The YUV a:b:c naming style means: for <a> samples of Y there are <b> samplesof UV in odd lines and <c> samples of UV in even lines.conversion: (some cut'n'paste from www and maillist)RGB to YUV Conversion:    Y  =      (0.257 * R) + (0.504 * G) + (0.098 * B) + 16    Cr = V =  (0.439 * R) - (0.368 * G) - (0.071 * B) + 128    Cb = U = -(0.148 * R) - (0.291 * G) + (0.439 * B) + 128YUV to RGB Conversion:    B = 1.164(Y - 16)                  + 2.018(U - 128)    G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128)    R = 1.164(Y - 16) + 1.596(V - 128)In both these cases, you have to clamp the output values to keep them inthe [0-255] range. Rumour has it that the valid range is actually a subsetof [0-255] (I've seen an RGB range of [16-235] mentioned) but clamping thevalues into [0-255] seems to produce acceptable results to me.Julien (sorry, I can't recall his surname) suggests that there areproblems with the above formula and proposes the following instead:    Y  = 0.299R + 0.587G + 0.114B    Cb = U'= (B-Y)*0.565    Cr = V'= (R-Y)*0.713with reciprocal versions:    R = Y + 1.403V'    G = Y - 0.344U' - 0.714V'    B = Y + 1.770U'Note: This formula doesn't contain the +128 offsets of U,V values!Conclusion:Y = luminance, the weighted average of R G B components. (0=black 255=white)U = Cb = blue component (0=green 128=grey 255=blue)V = Cr = red component  (0=green 128=grey 255=red)Huh. The planar YUV modes.==========================The most misunderstood thingie...In MPlayer, we usually have 3 pointers to the Y, U and V planes, so itdoesn't matter what the order of the planes in the memory is:    for mp_image_t and libvo's draw_slice():planes[0] = Y = luminanceplanes[1] = U = Cb = blueplanes[2] = V = Cr = red    Note: planes[1] is ALWAYS U, and planes[2] is V, the FOURCC    (YV12 vs. I420) doesn't matter here! So, every codec using 3 pointers    (not only the first one) normally supports YV12 and I420 (=IYUV), too!But there are some codecs (VfW, dshow) and vo drivers (xv) ignoring the 2ndand 3rd pointer that use only a single pointer to the planar YUV image. Inthis case we must know the right order and alignment of planes in the memory!from the webartz fourcc list:YV12:  12 bpp, full sized Y plane followed by 2x2 subsampled V and U planesI420:  12 bpp, full sized Y plane followed by 2x2 subsampled U and V planesIYUV:  the same as I420YVU9:   9 bpp, full sized Y plane followed by 4x4 subsampled V and U planesHuh 2. RGB vs. BGR ?====================The 2nd most misunderstood thingie...You know, there are Intel and Motorola, and they use different byteorder.There are also others, like MIPS or Alpha, but all follow either Intelor Motorola byteorder.Unfortunately, the packed colorspaces depend on CPU byteorder. So, RGBon Intel and Motorola means different order of bytes.In MPlayer, we have constants IMGFMT_RGBxx and IMGFMT_BGRxx.Unfortunately, some codecs and vo drivers follow Intel, some follow Motorolabyteorder, so they are incompatible. We had to find a stable base, so longtime ago I've chosen OpenGL, as it's a wide-spreaded standard, and it welldefines what RGB is and what BGR is. So, MPlayer's RGB is compatible withOpenGL's GL_RGB on all platforms, and the same goes for BGR - GL_BGR.Unfortunately, most of the x86 codecs call our BGR RGB, so it sometimesconfuses developers.memory order:           namelowest address .. highest addressRGBA                    IMGFMT_RGBAARGB                    IMGFMT_ARGBBGRA                    IMGFMT_BGRAABGR                    IMGFMT_ABGRRGB                     IMGFMT_RGB24BGR                     IMGFMT_BGR24order in an int         namemost significant .. least significant bit8A8R8G8B                IMGFMT_BGR328A8B8G8R                IMGFMT_RGB325R6G5B                  IMGFMT_BGR165B6G5R                  IMGFMT_RGB161A5R5G5B                IMGFMT_BGR151A5B5G5R                IMGFMT_RGB15The following are palettized formats, the palette is in the second plane.When they come out of the swscaler (this can be different when theycome from a codec!), their palette is organized in such a waythat you actually get:3R3G2B                  IMGFMT_BGR82B3G3R                  IMGFMT_RGB81R2G1B                  IMGFMT_BGR4_CHAR1B2G1R                  IMGFMT_RGB4_CHAR1R2G1B1R2G1B            IMGFMT_BGR41B2G1R1B2G1R            IMGFMT_RGB4Depending upon the CPU being little- or big-endian, different 'in memory' and'in register' formats will be equal (LE -> BGRA == BGR32 / BE -> ARGB == BGR32)

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.