Chapter 2. image formats)
Download the full five chapters of v4l2:
V4l2 Chinese manual ch02
V4l2 APIs are mainly designed to exchange image data between devices and applications. The structure v4l2_pix_format defines the format and layout of an image in the memory. The format is obtained through vidioc_s_fmt negotiation. (The focus here is on video capture and output. For the overlay frame buffer format, check vidioc_g_fbuf ).
Table 2-1. structv4l2_pix_format
_ U32 width Image Width in pixels.
_ U32 height image height inpixels.
The application sets these fields to request the image size, and the driver returns the nearest value. In the case of plane format... (I don't know if my rules are not good. In this sort of style, there will always be some incomplete information. Of course, I have read it, which does not affect our work)
Applicationsset these fields to request an image size, Drivers return the closest possiblevalues. In case of planar formats
_ U32 pixelformat the pixel format or type of compression, set
Theapplication. This is a little endian four
Charactercode. v4l2 defines standard RGB
Formatsin Table 2-1, YUV formats in section 2.5,
Andreserved codes in Table 2-8
Enum v4l2_field field video images are typically interlaced.
Applicationscan request to capture or output only
Thetop or bottom field, or both fields interlaced or
Sequentiallystored in one buffer or alternating in
Separatebuffers. Drivers return the actual field
Orderselected. For details see section 3.6.
_ U32 bytesperline distance in bytes between theleftmost pixels in
Twoadjacent lines.
Both applications and drivers can set these domains to fill the request at the end of each line. However, the driver may be ignored...
Both applications and drivers can set this field torequest Padding Bytes at the end of each line. Drivers however may ignore
_ U32 sizeimage size in bytes of the buffer to hold a complete
Image, set by the driver. Usually this is
Bytesperlinetimes height. When the image
Consistsof Variable Length compressed data this is
Themaximum number of bytes required to hold
Image.
Enum v4l2_colorspace colorspace thisinformation supplements the pixelformat
Andmust be set by the driver,See section2.2.
_ U32 priv reserved for custom (driver defined) Additional
Informationabout formats. When not used drivers
Andapplications must set this field to zero.
2.1. Standard image formats)
In order to exchange images between the driver and the application, there must be an image data format that both parties can analyze (Unified. V4l2 contains many formats. This section describes the standard format in v4l2 specifications.
Of course, the v4l2 driver is not limited to these formats. On the contrary, you can customize the format. In this case, the application may rely on the decoder to convert the format to a standard type when needed. However, data can still be stored and retrieved in a proprietary (custom) format. For example, if a device supports a proprietary compression format, the application can still collect and store data in this format, which has saved space. When you want to display data on X Windows, you can use the decoder to decode and display the data.
Finally, we still need some standard formats. The integrity of the v4l2 specification can be guaranteed only when a clear standard format is defined.
(The following section mainly refers to the standard format, which is uncompressed. What is the layout of the memory)
In v4l2, each format has an identifier, such as pix_fmt_xxx, which is defined in the videodev. h header file. These identifiers represent the 4-Byte Code listed below. Of course they are used differently in the Windows (relative to Linux or UNIX) World.
2.2. colorspaces)
Gammacorrection
[To do]
E 'r = f (r)
E 'G = f (g)
E 'B = F (B)
Constructionof luminance and color-difference Signals
[To do]
E 'y = coeffr e 'r + coeffg e 'G + coeffb e' B
(E 'r-e' y) = E' R-coeffr e 'r-coeffg e 'G-coeffb e' B
(E 'B-e' y) = E' B-coeffr e 'r-coeffg e 'G-coeffb e' B
Re-normalizedcolor-difference Signals
The color-difference signals arescaled back to unity range [-0.5; + 0.5]:
KB = 0.5/(1-coeffb)
KR = 0.5/(1-coeffr)
PB = KB (E 'B-e' y) = 0.5 (coeffr/coeffb) e'r + 0.5 (coeffg/coeffb) e'g + 0.5 e' B
PR = Kr (E 'r-e 'y) = 0.5 e 'r + 0.5 (coeffg/coeffr) e 'G + 0.5 (coeffb/coeffr) e 'B
Quantization
[To do]
Y' = (Lum. Levels-1) · E' y + Lum. offset
CB = (chrom. Levels-1) · Pb + chrom. offset
Cr = (chrom. Levels-1) · PR + chrom. offset
I would like to provide you with a document on the relationship between RGB and YUV (I am looking for it online. Thanks to the original author !)
JPEG simple document V1.0-gameres.com.htm (if not, search for it)
Example 2-1. ITU-R Rec. bt.601 Color Conversion
Forwardtransformation
Inter, eg, Eb;/* gamma correctedrgb input [0; 255] */
Inty1, CB, CR;/* output [0, 255] */
Doubler, G, B;/* temporaries */
Doubley1, Pb, PR;
Int
Clamp (Double X)
{
Int r = x;/* round to nearest */
If (r <0) return 0;
Else if (r> 255) return 255;
Else return R;
}
R = ER/255.0;
G = EG/255.0;
B = Eb/255.0;
Y1 = 0.299 * r + 0.587 * g + 0.114 * B;
PB =-0.169 * r-0.331 * g + 0.5 * B;
PR = 0.5 * r-0.419 * g-0.081 * B;
Y1 = clamp (219 * Y1 + 16 );
CB = clamp (224 * Pb + 128 );
Cr = clamp (224 * PR + 128 );
/* Or shorter */
Y1 = 0.299 * er + 0.587 * EG + 0.114 * Eb;
Y1 = clamp (219/255 .0) * Y1 + 16 );
CB = clamp (224/255 .0)/(2-2*0.114) * (EB-Y1) + 128 );
Cr = clamp (224/255 .0)/(2-2*0.299) * (Er-Y1) + 128 );
Inverse Transformation
Int Y1, CB, CR;/* gamma pre-correctedinput [0; 255] */
Int er, eg, Eb;/* output [0; 255] */
Double R, G, B;/* temporaries */
Doubley1, Pb, PR;
Int
Clamp (Double X)
{
Int r = x;/* round to nearest */
If (r <0) return 0;
Else if (r> 255) return 255;
Else return R;
}
Y1 = (255/219 .0) * (Y1-16 );
PB = (255/224 .0) * (CB-128 );
PR = (255/224 .0) * (Cr-128 );
R = 1.0 * Y1 + 0 * Pb + 1.402 * PR;
G = 1.0 * Y1-0.344 * pb-0.714 * PR;
B = 1.0 * Y1 + 1.772 * Pb + 0 * PR;
ER = clamp (R * 255);/* [OK? One shoshould prob. Limit Y1, Pb, PR] */
Eg = clamp (G * 255 );
EB = clamp (B * 255 );
Table 2-2. Enum v4l2_colorspace
(For details, see p42)
2.3. Index format (indexed format)
In this format, each pixel is represented by an 8bit, which is used to index the 256 color palette. This is specially prepared for the video output overlay. There is no IOCTL method used to access the color palette, but it can be accessed only through the frame buffer API of Linux (first touch.
Table2-3. Indexed image format (P42-P43)
2.4. RGB format (RGB formats)
Packed RGB formats
Name
Packed RGB formats-packed RGB formats
Description
These format definitions are used to match the pixel format of the traditional PC's image frame buffer. Each pixel occupies 8, 16, 24, or 32 bits. This is the packed-pixel format, which means that each pixel is in the memory.
When one of these formats is used, the driver should report the color space to v4l2_colorspace_srgb.
Table2-1. Packed RGB image formats
Identifier Code |
Byte 0 7654/3210 |
Byte 1 |
Byte 2 |
Byte 3 |
V4l2_pix_fmt_rgb332 'Rgb1' |
B1 B0 G2 G1 G0 R2 R1 R0 |
|
|
|
V4l2_pix_fmt_rgb444 'R444' |
G3 G2 G1 G0 B3 B2 B1 B0 |
A3 A2 A1 A0 R3 R2 R1 R0 |
|
|
V4l2_pix_fmt_rgb555 'Rgbo' |
G2 G1 G0 R4 R3 R2 R1 R0 |
A B4 B3 B2 B1 B0 G4 G3 |
|
|
V4l2_pix_fmt_rgb555x 'Rgbq' |
A B4 B3 B2 B1 B0 G4 G3 |
G2 G1 G0 R4 R3 R2 R1 R0 |
|
|
V4l2_pix_fmt_rgb565 'Rgbp' |
G2 G1 G0 R4 R3 R2 R1 R0 |
B4 B3 B2 B1 B0 G5 G4 G3 |
|
|
V4l2_pix_fmt_rgb565x 'Rgbr' |
B4 B3 B2 B1 B0 G5 G4 G3 |
G2 G1 G0 R4 R3 R2 R1 R0 |
|
|
V4l2_pix_fmt_bgr24 'Bgr3' |
B7 B6 B5 B4 B3 B2 B1 B0 |
G7 G6 G5 G4 G3 G2 G1 G0 |
R7 R6 R5 R4 R3 R2 R1 R0 |
|
V4l2_pix_fmt_rgb24 'Rgb3' |
R7 R6 R5 R4 R3 R2 R1 R0 |
G7 G6 G5 G4 G3 G2 G1 G0 |
B7 B6 B5 B4 B3 B2 B1 B0 |
|
V4l2_pix_fmt_bgr32 'Bgr4' |
B7 B6 B5 B4 B3 B2 B1 B0 |
G7 G6 G5 G4 G3 G2 G1 G0 |
R7 R6 R5 R4 R3 R2 R1 R0 |
A7 A6 A5 A4 A3 A2 A1 A0 |
V4l2_pix_fmt_rgb32 'Rgb4' |
R7 R6 R5 R4 R3 R2 R1 R0 |
G7 G6 G5 G4 G3 G2 G1 G0 |
B7 B6 B5 B4 B3 B2 B1 B0 |
A7 A6 A5 A4 A3 A2 A1 A0 |
Which RGB format does a driver support? You can test it with the next tool in the linuxtv v4l-dvb code library. You can access http://linuxtv.org/repo/ for more information.
V4l2_pix_fmt_sbggr8 ('ba81 ')
Name
V4l2_pix_fmt_sbgg8 --- Bayer RGB format
Description
This is usually the native format of the digital camera, used to indicate the layout of the sensor on the CCD device. Each pixel is red, green, or blue. The missing part must be interpolated and supplemented by neighboring pixels. From left to right, the first line contains a blue and green, and the second line is a green and red. This combination will arrange the columns in each row to the right.
Example 2-1. v4l2_pix_fmt_sbggr84 × 4 pixel image
Byte order.Each cell is one bytew.
Start + 0: B00 g01 B02 g03
Start + 4: G10 R11 G12 R13
Start + 8: B20 G21 B22 G23
Start + 12: g30 R31 G32 r33
V4l2_pix_fmt_sbggr16 ('ba82 ')
Name
V4l2_pix_fmt_sbggr16-Bayer RGB format
Description
Tobe continued...