V4L2 中文
Made by:鱼胖子(467350479) 个人翻译,转载申明
Chapter 2. 图像格式(Image Formats)
V4L2 API 主要是设计用于设备和应用程序进行图片数据交换的。结构 v4l2_pix_format
定义了一张图片在内存中的格式和布局。格式是通过 VIDIOC_S_FMT 协商得到的。(这里涉及
的重点是关乎视频采集和输出,对于 overlay 帧缓冲格式,您还是看看 VIDIOC_G_FBUF)。
Table 2-1. struct v4l2_pix_format
__u32
__u32
width
height
Image width in pixels.
Image height in pixels.
应用程序设置这些域是用来请求图片尺寸,驱动会返回最接近的合适值。在平面格式情形中…(不知是不
是我下规范不好怎么地,在这种列表式中总会有些地方不完整,当然我看过了,这不太影响我们的工作)
Applications set these fields to request an image size, drivers return the closest possible values. In case
of planar formats
__u32
pixelformat
The pixel format or type of compression, set by
the application. This is a little endian four
character code. V4L2 defines standard RGB
formats in Table 2-1, YUV formats in Section 2.5,
and reserved codes in Table 2-8
enum v4l2_field
field
Video images are typically interlaced.
__u32
Applications can request to capture or output only
the top or bottom field, or both fields interlaced or
sequentially stored in one buffer or alternating in
separate buffers. Drivers return the actual field
order selected. For details see Section 3.6.
bytesperline
Distance in bytes between the leftmost pixels in
two adjacent lines.
应用程序和驱动都可以设置这些域以请求在每行的末尾的填补位。然而驱动却可能予以忽略…
Both applications and drivers can set this field to request 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
bytesperline times height. When the image
consists of variable length compressed data this is
the maximum number of bytes required to hold an
image.
enum v4l2_colorspace
colorspace
This information supplements the pixelformat
__u32
and must be set by the driver, see Section 2.2.
priv
Reserved for custom (driver defined) additional
information about formats. When not used drivers
and applications must set this field to zero.
2.1. 标准图形格式(Standard Image Formats)
为了在驱动和应用程序间进行图片交换,就必须有一个双方都能辨析(统一的)的图片
数据格式。V4L2 包含了诸多格式,而本节就来谈谈 V4L2 规范中的标准图形格式。
当然,V4L2 驱动并不局限于这些个格式。相反,可以有定制的格式。在这种情况下,
当需要时应用程序就可能依赖解码器来将这种格式转换成标准类型的。但是数据依然可以以
专有(定制的)格式来进行数据的存储和重新获取。例子说,假设一个设备支持专有的压缩
格式,那么,应用程序仍然可以以这种格式进行采集和保存数据,已达到节省空间的目的。
而当要在 X Windows 端显示时,就可以用解码器进行解码并显示。
最后尽管如此,我们还是需要一些个标准格式的。只有定义了明确的标准格式,才可以
保证 V4L2 规范的完整性嘛。
(接下来的这一段主要说标准格式,是未压缩的,其在内存的布局是怎么样的,就不译
了)
在 V4L2 中,每种格式都个标识符形如 PIX_FMT_XXX,定义在 videodev.h 头文件中。这
些标识符代表了在下面列出的 4 字节代码,当然了他们和 Windows(相对的是 Linux 或者
Unix)世界里的用法是不一样的。
2.2. 色彩空间(Colorspaces)
Gamma Correction
[to do]
E’R = f(R)
E’G = f(G)
E’B = f(B)
Construction of 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-normalized color-difference signals
The color-difference signals are scaled 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
Forward Transformation
int ER, EG, EB;
/* gamma corrected RGB input [0;255] */
int Y1, Cb, Cr;
/* output [0;255] */
double r, g, b;
/* temporaries */
double y1, 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-corrected input [0;255] */
int ER, EG, EB;
/* output [0;255] */
double r, g, b;
/* temporaries */
double y1, 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)
2.3. 索引格式(Indexed Format)
在这种格式中,每个像素点都用一个 8bit 来表示,用来索引 256 色的调色板。这是特
地为视频输出 overlay 准备的。没有用来访问该调色板的 ioctl 方法,但可以且仅可以通过
Linux 的帧缓冲 API(头一回接触)来进行访问。
Table 2-3. Indexed Image Format(P42-P43)
2.4. RGB 格式(RGB Formats)
Packed RGB Formats
Name
Packed RGB formats – Packed RGB formats
Description
这些格式定义用来匹配传统 PC 的图形帧缓冲的像素格式的。每像素占据 8,16,24 或
者 32 个 bit。这就是填充像素(packed-pixel)格式,意味着每个像素点在内存中是一个挨
着一个的。
当 使 用 这 些 格 式 中 的 某 一 个 时 , 驱 动 应 该 报 告 给 色 彩 空 间 :
V4L2_COLORSPACE_SRGB。
Table 2-1. Packed RGB Image Formats
Identifier
Code
Byte 0
7654/3210
Byte 1
Byte 2
Byte 3
V4L2_PIX_FMT_RGB332
b1 b0 g2 g1 g0 r2 r1 r0
‘RGB1’
V4L2_PIX_FMT_RGB444
g3 g2 g1 g0 b3 b2 b1 b0
a3 a2 a1 a0 r3 r2 r1 r0
‘R444’
V4L2_PIX_FMT_RGB555
g2 g1 g0 r4 r3 r2 r1 r0
a b4 b3 b2 b1 b0 g4 g3
‘RGBO’
V4L2_PIX_FMT_RGB555X
a b4 b3 b2 b1 b0 g4 g3
g2 g1 g0 r4 r3 r2 r1 r0
‘RGBQ’
V4L2_PIX_FMT_RGB565
g2 g1 g0 r4 r3 r2 r1 r0
b4 b3 b2 b1 b0 g5 g4 g3
‘RGBP’
V4L2_PIX_FMT_RGB565X
b4 b3 b2 b1 b0 g5 g4 g3
g2 g1 g0 r4 r3 r2 r1 r0
‘RGBR’
V4L2_PIX_FMT_BGR24
b7 b6 b5 b4 b3 b2 b1 b0
g7 g6 g5 g4 g3 g2 g1 g0
r7 r6 r5 r4 r3 r2 r1 r0
‘BGR3’
V4L2_PIX_FMT_RGB24
r7 r6 r5 r4 r3 r2 r1 r0
g7 g6 g5 g4 g3 g2 g1 g0
b7 b6 b5 b4 b3 b2 b1 b0
‘RGB3’
V4L2_PIX_FMT_BGR32
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
‘BGR4’
V4L2_PIX_FMT_RGB32
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
‘RGB4’
一个驱动到底支持哪种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:
start + 4:
start + 8:
start + 12:
B00
G10
B20
G30
G01
R11
G21
R31
B02
G12
B22
G32
G03
R13
G23
R33
V4L2_PIX_FMT_SBGGR16 (’BA82’)
Name
V4L2_PIX_FMT_SBGGR16— Bayer RGB format
Description
这个格式类似于上面提到的 V4L2_PIX_FMT_SBGGR8,不同的在于每个像素有 16bit 深度(depth,
这...)。最不重要的 byte 存储在低地址空间(小端格式)。考虑到实际的采样精度可能低于 16bit,
比如 10 比特的吧,每个像素的值可能就是 0 到 1023 了。
Example 2-1. V4L2_PIX_FMT_SBGGR16 4 × 4 pixel image
Byte Order. Each cell is one byte.
start + 0:
B00low
B00high G01low
G01high
B02low
B02high G03low
start + 8:
G10low
G10high R11low
R11high G12low
G12high
R13low
start + 16:
B20low
B20high G21low
G21high
B22low
B22high G23low
start + 24:
G30low
G30high R31low
R31high G32low
G32high
R33low
2.5. YUV 格式(YUV Formats)
关于 YUV 格式介绍可以看看上面的 JPEG 简易文档...
Packed YUV formats
Name
Packed YUV formats ---Packed YUV formats
Description
类似于 packed RGB 格式,这些格式用 16 或者 32bit 将每个像素点的 Y,Cb 和 Cr 存储起来。
Table 2-1. Packed YUV Image Formats(P49)
V4L2_PIX_FMT_GREY (’GREY’)
Name
V4L2_PIX_FMT_GREY— Grey-scale image
Description
这是一个灰度图。这是个弱化的 YCbCr 格式,其中简化掉了 Cb 或者 Cr 数据。
Example 2-1. V4L2_PIX_FMT_GREY 4 × 4 pixel image
Byte Order. Each cell is one byte.
start + 0:
start + 4:
start + 8:
Y’00
Y’10
Y’20
Y’01
Y’11
Y’21
Y’02
Y’12
Y’22
start + 12:
V4L2_PIX_FMT_Y16 (’Y16 ’)
Y’31
Y’30
Y’32
Y’03
Y’13
Y’23
Y’33
Name
V4L2_PIX_FMT_Y16— Grey-scale image
Description
这也是个灰度图,只是其每像素点有 16bit 的深度。最不重要的 byte 存储在低地址空间(小端格式)。
考虑到实际的采样精度可能低于 16bit,比如 10 比特的吧,每个像素的值可能就是 0 到 1023 了。
Example 2-1. V4L2_PIX_FMT_Y16 4 × 4 pixel image
Byte Order. Each cell is one byte.
start + 0:
Y’00low Y’00high Y’01low Y’01high Y’02low Y’02high Y’03low Y’03high
start + 8:
Y’10low Y’10high Y’11low Y’11high Y’12low Y’12high Y’13low Y’13high
start + 16: Y’20low Y’20high Y’21low Y’21high Y’22low Y’22high Y’23low Y’23high
start + 24: Y’30low Y’30high Y’31low Y’31high Y’32low Y’32high Y’33low Y’33high
V4L2_PIX_FMT_YUYV (’YUYV’)
Name
V4L2_PIX_FMT_YUYV— Packed format with ½ horizontal chroma resolution, also known as
YUV4:2:2
Description
此格式,每4个byte来表征2个像素。也即每4个byte就有2个Y,一个Cb和一个Cr。因为人眼对色彩度
不是很敏感,所以就没必要有那么多的Cb和Cr了。V4L2_PIX_FMT_YUYV在Windows环境中是YUY2。
Example 2-1. V4L2_PIX_FMT_YUYV 4 × 4 pixel image
Byte Order. Each cell is one byte.
start + 0:
Y’00
start + 8:
Y’10
Cb00
Cb10
Y’01
Y’11
start + 16: Y’20
Cb20
Y’21
start + 24: Y’30
下面有一堆 YUV 格式,详见 P53-P59
Cb30
Y’31
Cr00
Cr10
Cr20
Cr30
Y’02
Cb01
Y’12 Cb11
Y’22
Y’32
Cb21
Cb31
Y’03
Y’13
Y’23
Y’33
Cr01
Cr11
Cr21
Cr31
2.6. 压缩格式(Compressed Formats)
Table 2-7. Compressed Image Formats
Identifier
Code
Description
V4L2_PIX_FMT_JPEG
‘JPEG’
TBD. See also
VIDIOC_G_JPEGCOMP,
VIDIOC_S_JPEGCOMP.
V4L2_PIX_FMT_MPEG
‘MPEG’
MPEG stream. The actual format is
determined by extended control
V4L2_CID_MPEG_STREAM_TYPE,
see Table 1-2.
2.7. 保留的格式标识符(Reserved Format Identifiers)
这些格式不是有本规范定义的,列出来只是为了参考和避免可能的命名冲突(你知道
Linux绝大本分是开源的,其命名方式完全由项目作者决定,没有统一的标准,所以怕撞车)。
接下来的部分主要是说你可以把你自己定制的格式上交给一个邮件列表,供大家参考...
Table 2-8. Reserved Image Formats
Identifier
Code
Description
V4L2_PIX_FMT_DV
’dvsd’
unknown
V4L2_PIX_FMT_ET61X251 ’E625’
Compressed format of the ET61X251 driver.
V4L2_PIX_FMT_HI240
’HI24’
8 bit RGB format used by the BTTV driver,
http://bytesex.org/bttv/
V4L2_PIX_FMT_HM12
’HM12’ YUV 4:2:0 format used by the IVTV driver,
http://www.ivtvdriver.org/The format is documented in the
kernel sources in the file
Documentation/video4linux/cx2341x/README.hm12
Identifier
Code
Description
V4L2_PIX_FMT_MJPEG
’MJPG’ Compressed format used by the Zoran driver
V4L2_PIX_FMT_PWC1
’PWC1’ Compressed format of the PWC driver.
V4L2_PIX_FMT_PWC2
’PWC2’ Compressed format of the PWC driver.
V4L2_PIX_FMT_SN9C10X
’S910’
Compressed format of the SN9C102 driver.
V4L2_PIX_FMT_WNVA
’WNVA’ Used by the Winnov Videum driver,
http://www.thedirks.org/winnov/
V4L2_PIX_FMT_YYUV
’YYUV’
unknown
To be continued…