Chapter 2. 图像格式(Image Formats)
v4l2 全五章下载:
v4l2中文手册 ch02
V4L2 API主要是设计用于设备和应用程序进行图片数据交换的。结构v4l2_pix_format定义了一张图片在内存中的格式和布局。格式是通过VIDIOC_S_FMT协商得到的。(这里涉及的重点是关乎视频采集和输出,对于overlay帧缓冲格式,您还是看看VIDIOC_G_FBUF)。
Table 2-1. structv4l2_pix_format
__u32 width Image width in pixels.
__u32 height Image height inpixels.
应用程序设置这些域是用来请求图片尺寸,驱动会返回最接近的合适值。在平面格式情形中…(不知是不是我下规范不好怎么地,在这种列表式中总会有些地方不完整,当然我看过了,这不太影响我们的工作)
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 by
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 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 an
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.
为了在驱动和应用程序间进行图片交换,就必须有一个双方都能辨析(统一的)的图片数据格式。V4L2包含了诸多格式,而本节就来谈谈V4L2规范中的标准图形格式。
当然,V4L2驱动并不局限于这些个格式。相反,可以有定制的格式。在这种情况下,当需要时应用程序就可能依赖解码器来将这种格式转换成标准类型的。但是数据依然可以以专有(定制的)格式来进行数据的存储和重新获取。例子说,假设一个设备支持专有的压缩格式,那么,应用程序仍然可以以这种格式进行采集和保存数据,已达到节省空间的目的。而当要在X Windows端显示时,就可以用解码器进行解码并显示。
最后尽管如此,我们还是需要一些个标准格式的。只有定义了明确的标准格式,才可以保证V4L2规范的完整性嘛。
(接下来的这一段主要说标准格式,是未压缩的,其在内存的布局是怎么样的,就不译了)
在V4L2中,每种格式都个标识符形如PIX_FMT_XXX,定义在videodev.h头文件中。这些标识符代表了在下面列出的4字节代码,当然了他们和Windows(相对的是Linux或者Unix)世界里的用法是不一样的。
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
给大伙提供个介绍RGB和YUV关系的文档(网上找的,在此感谢原作者!)
JPEG 简易文档 V1.0 - GameRes.com.htm(如果打不开直接去搜一下吧)
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 should prob. limit y1,pb,pr] */
EG= clamp (g * 255);
EB= clamp (b * 255);
Table 2-2. enum v4l2_colorspace
(详见P42)
在这种格式中,每个像素点都用一个8bit来表示,用来索引256色的调色板。这是特地为视频输出overlay准备的。没有用来访问该调色板的ioctl方法,但可以且仅可以通过Linux的帧缓冲API(头一回接触)来进行访问。
Table2-3. Indexed Image Format(P42-P43)
Packed RGB Formats
Name
Packed RGB formats – Packed RGB formats
Description
这些格式定义用来匹配传统PC的图形帧缓冲的像素格式的。每像素占据8,16,24或者32个bit。这就是填充像素(packed-pixel)格式,意味着每个像素点在内存中是一个挨着一个的。
当使用这些格式中的某一个时,驱动应该报告给色彩空间: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 |
一个驱动到底支持哪种RGB格式,你可以去LinuxTV v4l-dvb代码库下个工具测试下。可以访问http://linuxtv.org/repo/ 来获得更多信息。
V4L2_PIX_FMT_SBGGR8 (’BA81’)
Name
V4L2_PIX_FMT_SBGG8 --- Bayer RGB format
Description
这通常是数字摄像机的本征(native)格式,用来表明CCD设备上传感器的布局的。每个像素点就一个红色或者绿色或者蓝色。缺失部分必须用邻近像素来内插补充。从左到右,第一行包含一个blue和green,第二行是一个green和red。这种组合会向右向下每隔一行一列进行排布。
Example 2-1. V4L2_PIX_FMT_SBGGR8 4 × 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…