Summary of related YUV knowledge points of Camera

foreword

        In the process of debugging the cam sensor, it is inevitable to encounter yuv-related function points. The debugging effect may need to dump the yuv image, and the preview needs to convert and display the yuv.

        Here is mainly a record of the basic concepts, classification standards, sampling and storage formats of yuv.

        This article mainly refers to blogs on the Internet, organizes and records the knowledge of yuv, and attaches reference links.

Table of contents

YUV tool

What is YUV

Sampling, classification and storage format of YUV

Classification

sampling

basic storage format

packed  YUV422

Packed  YUV420

Planer  YUV422

Planning YUV420

Semi Planer YUV422

Semi Planer YUV420

derived storage format

I420  --  YUV 420 Planer

YV12 -- YUV 420 Planar

NV12  --  YUV 420 Semi-Planar

NV21  --  YUV 420 Semi-Planar

I422  --  YUV 422 Planar

YV16 --  YUV 422 Planar

NV16  -- YUV 422 Semi-Planar

NV61  --  YUV 422 Semi-Planar

YUVY  --  YUV 422 Packed Interleaved

VYUY  --  YUV 422 Packed Interleaved

UYVY  --  YUV 422 Packed Interleaved

I444 -- YUV 444 Plannar

YV24 -- YUV 444 Plannar

NV24  --  YUV 444 Semi-Planar

NV42  --  YUV 444 Semi-Planar

YUV 444 Packed

YUV422 to RGB565

RGB to YUV

YUV to RGB fast algorithm

integer arithmetic

look-up table method

References and Links


YUV tool

        First introduce a tool that can open yuv images in various formats. It is free and easy to use, and the URL is attached.

        YUV tool link;

What is YUV

        Among the common color models, RGB is mainly used to express and display colors in electronic systems , CMYK printing four-color mode is used for color printing, and YUV is a color coding method adopted by the European television system.

        The advantages of using YUV are twofold:

  • YUV is mainly used to optimize the transmission of color video signals and is backward compatible with old black and white TVs. This feature is used in TV signals.
  • YUV is that the total size of the data is smaller than the RGB format (but with YUV444, it is 24bits like RGB888)

        For YUV subdivision, there are Y'UV , YUV , YCbCr , YPbPr and other types, among which YCbCr is mainly used for digital signals .

        YCbCr is a part of the ITU-R BT1601 proposal in the development process of the video standard of the World Digital Organization. In fact, it is a replica of YUV through Gamma. Among them, Y has the same meaning as Y in YUV, and Cb and Cr also refer to color, but the representation method is different. In the YUV family, YCbCr is the most widely used member in computer systems, and its application fields are very wide. JPEG, MPEG, and H264 all use this format. Among them, Cr reflects the difference between the red part of the RGB input signal and the brightness value of the RGB signal, and Cb reflects the difference between the blue part of the RGB input signal and the brightness value of the RGB signal, which is the so-called color difference signal.

        Among them, Y refers to the luminance component, Cb refers to the blue chroma component, and Cr refers to the red chroma component, which is a replica of the standard YUV.

        Generally speaking, YUV mostly refers to YCbCr . Hereinafter, YUV is used to refer to YCbCr .

         The color picture of the same painting, Y component, V component, U component

Sampling, classification and storage format of YUV

Classification

 

sampling

        There are many kinds of YUV sampling, commonly used are 444, 422, 420 and so on. Since the human eye is much more sensitive to Y than to U and V , sometimes multiple Y components can share a set of UVs , which can greatly save space and not lose quality.

        According to the amount of data from small to large, it is YUV 420 , YUV 422 , YUV 444 .

        Use three diagrams to visually represent the acquisition method, the black dot represents the Y component of the pixel, and the hollow circle represents the UV component of the pixel.

 

  • YUV 4:4:4 sampling, each Y corresponds to a set of UV components.

4:4:4 Formats, 24 Bits per Pixel

The total data volume size = w*h*3, does not save space

  • YUV 4:2:2 sampling, every two Y share a set of UV components.

4:2:2 Formats, 16 Bits per Pixel

The total data volume size = w*h*2, saving 1/3 space compared to yuv444

  • YUV 4:2:0 sampling, every four Y shares a set of UV components.

4:2:0 Formats, 12 Bits per Pixel

The total data volume size = w*h*1.5, saving 1/2 space compared to yuv444

basic storage format

        Storage formats can be divided into three categories:

  • Planar: The three components of YUV are stored separately, first store all Y, then all U(V), and then all V(U).
  • Packed: The three components of YUV are all interleaved, and YUV is stored sequentially like this
  • Semi Planar: Y components are stored separately, UV components are stored interleaved, all Y is stored first, and the rest of UV are stored interleaved.
    1. Combining the characteristics of the first two methods, the Y component is stored separately, and the UV is stored interleaved

 

packed  YUV422

        Every 2 consecutive Y components share a UV space, and YUV is stored interleaved.

        UYVY422(COLOR_FormatYUV422PackedPlanar)。

Packed  YUV420

        Every 4 consecutive Y components share a UV space, and yuv is stored interleaved.

Planer  YUV422

        Every 2 consecutive Y components share a UV space, first store all Y, then U(V), and finally V(U).

        YUV422P(COLOR_FormatYUV422Planar)。

Planer YUV420

        Every 4 consecutive Y components share a UV space. Store all Y first, then U(V), and finally V(U). ,

        YUV420P(COLOR_FormatYUV422Planar)。

Semi Planer YUV422

        Every 2 consecutive Y components share a UV space. Store all Y first, and then UV interleaved storage.

        YUV422SP(COLOR_FormatYUV422SemiPlanar)。

Semi Planer YUV420

        Every 4 consecutive Y components share a UV space. Store all Y first, and then UV interleaved storage.

        YUV422SP(COLOR_FormatYUV422SemiPlanar)。

derived storage format

I420  --  YUV 420 Planer

        I420 is a kind of YUV 420 Planar. The YUV components are stored separately, first Y, then U, and finally V.

Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
U U U
U U U
U U U
V V V
V V V
V V V

YV12 -- YUV 420 Planar

        YV12 is a kind of YUV 420 Planar, YUV components are stored separately, first is Y, followed by V, and finally U.

        Different from I420, YV12 is V first and then U.

Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
V V V
V V V
V V V
U U U
U U U
U U U

NV12  --  YUV 420 Semi-Planar

        NV12 is a type of YUV 420 Semi-Planar: the Y component is stored separately, and the UV components are interleaved.

        When UV is arranged, first U and then V.

Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
U V U V U V
U V U V U V
U V U V U V

NV21  --  YUV 420 Semi-Planar

        NV21 is a kind of YUV 420 Semi-Planar, the Y component is stored separately, and the UV component is stored interleaved.

        The difference with NV12 is that when UV is arranged, V first and then U.

Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
V U V U V U
V U V U V U
V U V U V U

I422  --  YUV 422 Planar

        I422 is a kind of YUV 422 Planar. The YUV components are stored separately, firstly Y, then U, and finally V.

Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
U U U U U U
U U U U U U
U U U U U U
V V V V V V
V V V V V V
V V V V V V

YV16 --  YUV 422 Planar

        YV16 is a kind of YUV 422 Planar, YUV components are stored separately, first is Y, followed by V, and finally U.

        Different from I422, YV16 is V first and then U.

Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
V V V V V V
V V V V V V
V V V V V V
U U U U U U
U U U U U U
U U U U U U

NV16  -- YUV 422 Semi-Planar

        NV16 is a kind of YUV 422 Semi-Planar, the Y component is stored separately, and the UV component is stored interleaved.

        When UV is arranged, first U and then V.

Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
U V U V U V
U V U V U V
U V U V U V
U V U V U V
U V U V U V
U V U V U V

NV61  --  YUV 422 Semi-Planar

        NV61 is a kind of YUV 422 Semi-Planar, the Y component is stored separately, and the UV component is stored interleaved.

        When UV is arranged, first V and then U.

Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
V U V U V U
V U V U V U
V U V U V U
V U V U V U
V U V U V U
V U V U V U

YUVY  --  YUV 422 Packed Interleaved

        YUVY is a type of YUV 422 Interleaved.

        In fact, Interleaved belongs to Packed, but in 422, it is more vivid to use Interleaved.

        Inside Packed, the arrangement order of YUV is YUVY, and two Ys share a set of UVs.

Y U V Y   Y U V Y   Y U V Y
Y U V Y   Y U V Y   Y U V Y
Y U V Y   Y U V Y   Y U V Y
Y U V Y   Y U V Y   Y U V Y
Y U V Y   Y U V Y   Y U V Y
Y U V Y   Y U V Y   Y U V Y

VYUY  --  YUV 422 Packed Interleaved

        VYUY is a kind of YUV 422 Interleaved.

        Inside Packed, the arrangement order of YUV is VYUY, and two Ys share a set of UVs.

V Y U Y   V Y U Y   V Y U Y
V Y U Y   V Y U Y   V Y U Y
V Y U Y   V Y U Y   V Y U Y
V Y U Y   V Y U Y   V Y U Y
V Y U Y   V Y U Y   V Y U Y
V Y U Y   V Y U Y   V Y U Y

UYVY  --  YUV 422 Packed Interleaved

        UYVY is a type of YUV 422 Interleaved.

        Inside Packed, the arrangement order of YUV is UYVY, and two Ys share a set of UVs.

U Y V Y   U Y V Y   U Y V Y
U Y V Y   U Y V Y   U Y V Y
U Y V Y   U Y V Y   U Y V Y
U Y V Y   U Y V Y   U Y V Y
U Y V Y   U Y V Y   U Y V Y
U Y V Y   U Y V Y   U Y V Y

I444 -- YUV 444 Plannar

        I444 is a kind of YUV 444 Plannar.

        YUV components are stored separately, first Y, followed by U.

Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
U U U U U U
U U U U U U
U U U U U U
U U U U U U
U U U U U U
U U U U U U
V V V V V V
V V V V V V
V V V V V V
V V V V V V
V V V V V V
V V V V V V

YV24 -- YUV 444 Plannar

        YV24 is a kind of YUV 444 Plannar, and the YUV components are stored separately, firstly Y, followed by V, and finally U.

        Different from I444, YV24 arranges V first.

Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
V V V V V V
V V V V V V
V V V V V V
V V V V V V
V V V V V V
V V V V V V
U U U U U U
U U U U U U
U U U U U U
U U U U U U
U U U U U U
U U U U U U

NV24  --  YUV 444 Semi-Planar

        NV24 is a kind of YUV 444 Semi-Planar, the Y component is stored separately, and the UV component is stored interleaved.

        When UV is arranged, it starts from U.

Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
U V U V U V U V U V U V 
U V U V U V U V U V U V 
U V U V U V U V U V U V 
U V U V U V U V U V U V 
U V U V U V U V U V U V 
U V U V U V U V U V U V 

NV42  --  YUV 444 Semi-Planar

        NV42 is a kind of YUV 444 Semi-Planar, the Y component is stored separately, and the UV component is stored interleaved.

        When UV is arranged, it starts from V.

Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
Y Y Y Y Y Y
V U V U V U V U V U V U
V U V U V U V U V U V U
V U V U V U V U V U V U
V U V U V U V U V U V U
V U V U V U V U V U V U
V U V U V U V U V U V U

YUV 444 Packed

Y U V   Y U V   Y U V   Y U V   Y U V   Y U V
Y U V   Y U V   Y U V   Y U V   Y U V   Y U V
Y U V   Y U V   Y U V   Y U V   Y U V   Y U V
Y U V   Y U V   Y U V   Y U V   Y U V   Y U V
Y U V   Y U V   Y U V   Y U V   Y U V   Y U V
Y U V   Y U V   Y U V   Y U V   Y U V   Y U V

YUV422 to RGB565

        Colors in nature are ever-changing. In order to give a quantitative measure of color, it is necessary to establish a color space model to describe a variety of colors. Since people's perception of color is a complex process of combined physiological and psychological effects, so in different In order to better and more accurately meet their respective needs in the application fields of various color space models, various color space models have emerged to quantify and describe colors. The ones we are more often exposed to include RGB / CMYK / YIQ / YUV / HSI and so on.

        For the field of digital electronic multimedia, the concept of color space that we often come into contact with is mainly RGB and YUV (in fact, these two systems contain many specific color expression methods and models, such as sRGB, Adobe RGB , YUV422, YUV420…).

        RGB describes the color according to the principle of the three-primary color plus light system, while YUV describes the color according to the principle of brightness and color difference. Even if it is only the two major color spaces of RGB and YUV, the knowledge involved is very rich and complex.

        The most commonly used YUV model in Camera Sensor is the YUV422 format, because it uses 4 bytes to describe two pixels, which is better compatible with the RGB565 model. It is beneficial to the software and hardware interface design of Camera Sensor and Camera controller .

        The YUV model is used in the PAL TV system, Y means brightness, and UV is not an abbreviation of any word .

        The YIQ model is similar to the YUV model and is used in NTSC TV systems . The I and Q components in the YIQ color space are equivalent to a 33 -degree rotation of the UV component in the YUV space.

        The YCbCr color space is a color space derived from the YUV color space, and is mainly used in digital TV systems. In the conversion from RGB to YCbCr , the input and output are both in 8 -bit binary format .

RGB to YUV

  • Y=0.30R + 0.59G + 0.11B
  • U=0.493(BY)
  • V=0.877(RY)

        From the formula, the key point we need to understand is that the UV/CbCr signal is actually the blue difference signal and the red difference signal. Furthermore, it actually indirectly represents the intensity of blue and red to a certain extent. Understand this It will be very helpful for us to understand the process of various color transformation processing.

YUV to RGB fast algorithm

        The YUV referred to here is actually YcrCb. The YUV2RGB conversion formula itself is very simple, but it involves floating-point operations. Therefore, if you want to implement a fast algorithm , the algorithm structure itself has nothing to study, mainly using integer operations or Look-up tables to speed up calculations.

        First, the conversion formula can be derived as:

  • R = Y + 1.4075 *(V-128)
  • G = Y – 0.3455 *(U –128) – 0.7169 *(V –128)
  • B = Y + 1.779 *(U – 128)

integer arithmetic

        To replace floating-point operations with integer operations, of course, we need to use shifting. We can easily get the following algorithm:

  • u = YUVdata[UPOS] - 128;
  • v = YUVdata[VPOS] - 128;

  • rdif = v + ((v * 103) >> 8); // (1+103/256 = 1.4023)*v
  • invgdif = ((u * 88) >> 8) +((v * 183) >> 8); // (u*0.3437 + v*0.7148)
  • bdif = u +( (u*198) >> 8); // (1 + 198/256 = 1.7734) *u

  • r = YUVdata[YPOS] + rdif;
  • g = YUVdata[YPOS] - invgdif;
  • b = YUVdata[YPOS] + bdif;

        To convert from RGB24 to RGB565 data, a shift and OR operation is required:

RGBdata[1] =( (r & 0xF8) | ( g >> 5) );

RGBdata[0] =( ((g & 0x1C) << 3) | ( b >> 3) );

look-up table method

        slightly.

appendix

        YUV422 to RGB565 implementation code

        C code;

References and Links

https://www.jianshu.com/p/538ee63f4c1c

https://www.jianshu.com/p/3e44c2262775

https://www.cnblogs.com/huaping-audio/archive/2009/12/27/1633624.html

http://blog.sina.com.cn/s/blog_475e9bf20100siir.html

Guess you like

Origin blog.csdn.net/FANG_YISHAO/article/details/118699482