V4L2较V4L有较大的改动,并已成为2.6的标准接口,函盖video\dvb\FM...,多数驱动都在向V4l2迁移。更好地了解V4L2先从应用入手,然后再深入到内核中结合物理设备/接口的规范实现相应的驱动。本文先就V4L2在视频捕捉或camera方面的应用框架。 V4L2采用流水线的
V4L2较V4L有较大的改动,并已成为2.6的标准接口,函盖video\dvb\FM...,多数驱动都在向V4l2迁移。更好地了解V4L2先从应 用入手,然后再深入到内核中结合物理设备/接口的规范实现相应的驱动。本文先就V4L2在视频捕捉或camera方面的应用框架。<br><br>V4L2采用流水线的方式,操作更简单直观,基本遵循打开视频设备、设置格式、处理数据、关闭设备,更多的具体操作通过ioctl函数来实现。<br><br>在V4L2中,视频设备被看做一个文件。使用open函数打开这个设备:
<span>// 用非阻塞模式打开摄像头设备<br></span><span>int </span><span>cameraFd</span>;<br><span>cameraFd </span>= <span>open</span>(<span>"/dev/video0"</span>, <span>O_RDWR </span>| <span>O_NONBLOCK</span>, 0);<br><span>// 如果用阻塞模式打开摄像头设备,上述代码变为:<br>//cameraFd = open("/dev/video0", O_RDWR, 0);</span>
应用程序能够使用阻塞模式或非阻塞模式打开视频设备,如果使用非阻塞模式调用视频设备,即使尚未捕获到信息,驱动依旧会把缓存(DQBUFF)里的东西返回给应用程序。
<br>打开视频设备后,可以设置该视频设备的属性,例如裁剪、缩放等。这一步是可选的。在Linux编程中,一般使用ioctl函数来对设备的I/O通道进行管理:
<span> int </span><span>ioctl </span>(<span>int </span><span>__fd</span>, <span>unsigned long int </span><span>__request</span>, .../*args*/) ;在进行V4L2开发中,常用的命令标志符如下(some are optional): <br>
在亚洲,一般使用PAL(720X576)制式的摄像头,而欧洲一般使用NTSC(720X480),使用VIDIOC_QUERYSTD来检测:
<span>v4l2_std_id std</span>;<br><span>do </span>{<br> <span>ret </span>= <span>ioctl</span>(<span>fd</span>, <span>VIDIOC_QUERYSTD</span>, &<span>std</span>);<br>} <span>while </span>(<span>ret </span>== -1 && <span>errno </span>== <span>EAGAIN</span>);<br><span>switch </span>(<span>std</span>) {<br> <span>case </span><span>V4L2_STD_NTSC</span>:<br> <span>//……<br> </span><span>case </span><span>V4L2_STD_PAL</span>:<br> <span>//……<br></span>}<br><br>
当检测完视频设备支持的标准后,还需要设定视频捕获格式,结构如下:
struct v4l2_format fmt;<br><span>memset </span>( &<span>fmt</span>, 0, <span>sizeof</span>(<span>fmt</span>) );<br><span>fmt</span>.<span>type </span>= <span>V4L2_BUF_TYPE_VIDEO_CAPTURE</span>;<br><span>fmt</span>.<span>fmt</span>.<span>pix</span>.<span>width </span>= <span>720</span>;<br><span>fmt</span>.<span>fmt</span>.<span>pix</span>.<span>height </span>= <span>576</span>;<br><span>fmt</span>.<span>fmt</span>.<span>pix</span>.<span>pixelformat </span>= <span>V4L2_PIX_FMT_YUYV</span>;<br><span>fmt</span>.<span>fmt</span>.<span>pix</span>.<span>field </span>= <span>V4L2_FIELD_INTERLACED</span>;<br><span>if </span>(<span>ioctl</span>(<span>fd</span>, <span>VIDIOC_S_FMT</span>, &<span>fmt</span>) == -1) {<br> <span>return </span>-1;<br>}<br><br>v4l2_format结构如下:<br><span>struct </span><span>v4l2_format<br></span>{<br> <span>enum </span><span>v4l2_buf_type type</span>; <span>// 数据流类型,必须永远是V4L2_BUF_TYPE_VIDEO_CAPTURE </span><br> <span>union<br> </span>{<br> <span>struct </span><span>v4l2_pix_format pix</span>; <span><br> </span><span>struct </span><span>v4l2_window win</span>; <span><br> </span><span>struct </span><span>v4l2_vbi_format vbi</span>; <span><br> </span><span>__u8 raw_data</span>[200]; <span><br> </span>} <span>fmt</span>;<br>};<br><span>struct </span><span>v4l2_pix_format<br></span>{<br> <span>__u32 width</span>; <span>// 宽,必须是16的倍数</span><br> <span>__u32 height</span>; <span>// 高,必须是16的倍数</span><br> <span>__u32 pixelformat</span>; <span>// 视频数据存储类型,例如是YUV4:2:2还是RGB</span><br> <span>enum </span><span>v4l2_field field</span>;<br> <span>__u32 bytesperline</span>; <span><br> </span><span>__u32 sizeimage</span>;<br> <span>enum </span><span>v4l2_colorspace colorspace</span>;<br> <span>__u32 priv</span>; <span><br></span>};<br>
<font>2.3 分配内存</font>
接下来可以为视频捕获分配内存:
<span>struct </span><span>v4l2_requestbuffers req</span>;<br><span>if </span>(<span>ioctl</span>(<span>fd</span>, <span>VIDIOC_REQBUFS</span>, &<span>req</span>) == -1) {<br> <span>return </span>-1;<br>}<br><br>v4l2_requestbuffers 结构如下:<br><span>struct </span><span>v4l2_requestbuffers<br></span>{<br> <span>__u32 count</span>; <span>// 缓存数量,也就是说在缓存队列里保持多少张照片</span><br> <span>enum </span><span>v4l2_buf_type type</span>; <span>// 数据流类型,必须永远是V4L2_BUF_TYPE_VIDEO_CAPTURE </span><br> <span>enum </span><span>v4l2_memory memory</span>; <span>// V4L2_MEMORY_MMAP 或 V4L2_MEMORY_USERPTR</span><br> <span>__u32 reserved</span>[2];<br>};<br>
使用VIDIOC_REQBUFS,我们获取了req.count个缓存,下一步通过调用VIDIOC_QUERYBUF命令来获取这些缓存的地址,然后使用mmap函数转换成应用程序中的绝对地址,最后把这段缓存放入缓存队列:
<span>typedef struct </span><span>VideoBuffer </span>{<br> <span>void </span>*<span>start</span>;<br> <span>size_t length</span>;<br>} <span>VideoBuffer</span>;<br><br><span>VideoBuffer</span>* <span>buffers </span>= <span>calloc</span>( <span>req</span>.<span>count</span>, <span>sizeof</span>(*<span>buffers</span>) );<br><span>struct </span><span>v4l2_buffer buf</span>;<br><br><span>for </span>(<span>numBufs </span>= 0; <span>numBufs </span>req.<span>count</span>; <span>numBufs</span>++) {<br> <span>memset</span>( &<span>buf</span>, 0, <span>sizeof</span>(<span>buf</span>) );<br> <span>buf</span>.<span>type </span>= <span>V4L2_BUF_TYPE_VIDEO_CAPTURE</span>;<br> <span>buf</span>.<span>memory </span>= <span>V4L2_MEMORY_MMAP</span>;<br> <span>buf</span>.<span>index </span>= <span>numBufs</span>;<br> <span>// 读取缓存</span><br> <span>if </span>(<span>ioctl</span>(<span>fd</span>, <span>VIDIOC_QUERYBUF</span>, &<span>buf</span>) == -1) {<br> <span>return </span>-1;<br> }<br><br> <span>buffers</span>[<span>numBufs</span>].<span>length </span>= <span>buf</span>.<span>length</span>;<br><span> // 转换成相对地址</span><br> <span>buffers</span>[<span>numBufs</span>].<span>start </span>= <span>mmap</span>(<span>NULL</span>, <span>buf</span>.<span>length</span>, <span>PROT_READ </span>| <span>PROT_WRITE</span>,<br> <span>MAP_SHARED</span>,<span>fd</span>, <span>buf</span>.<span>m</span>.<span>offset</span>);<br><br> <span>if </span>(<span>buffers</span>[<span>numBufs</span>].<span>start </span>== <span>MAP_FAILED</span>) {<br> <span>return </span>-1;<br> }<br><br> <span>// 放入缓存队列</span><br> <span>if </span>(<span>ioctl</span>(<span>fd</span>, <span>VIDIOC_QBUF</span>, &<span>buf</span>) == -1) {<br> <span>return </span>-1;<br> }<br>}<br><br>
操作系统一般把系统使用的内存划分成用户空间和内核空间,分别由应用程序管理和操作系统管理。应用程序可以直接访问内存的地址,而内核空间存放的是 供内核访问的代码和数据,用户不能直接访问。v4l2捕获的数据,最初是存放在内核空间的,这意味着用户不能直接访问该段内存,必须通过某些手段来转换地 址。
一共有三种视频采集方式:使用read、write方式;内存映射方式和用户指针模式。
read、write方式,在用户空间和内核空间不断拷贝数据,占用了大量用户内存空间,效率不高。
内存映射方式:把设备里的内存映射到应用程序中的内存控件,直接处理设备内存,这是一种有效的方式。上面的mmap函数就是使用这种方式。
用户指针模式:内存片段由应用程序自己分配。这点需要在v4l2_requestbuffers里将memory字段设置成V4L2_MEMORY_USERPTR。
<span>struct </span><span>v4l2_buffer buf</span>;<br><span>memset</span>(&<span>buf</span>,0,<span>sizeof</span>(<span>buf</span>));<br><span>buf</span>.<span>type</span>=<span>V4L2_BUF_TYPE_VIDEO_CAPTURE</span>;<br><span>buf</span>.<span>memory</span>=<span>V4L2_MEMORY_MMAP</span>;<br><span>buf</span>.<span>index</span>=0;<br><br><span>//读取缓存</span><br><span>if </span>(<span>ioctl</span>(<span>cameraFd</span>, <span>VIDIOC_DQBUF</span>, &<span>buf</span>) == -1)<br>{<br> <span>return </span>-1;<br>}<br><span>//…………视频处理算法</span><br><span>//重新放入缓存队列<br></span><span>if </span>(<span>ioctl</span>(<span>cameraFd</span>, <span>VIDIOC_QBUF</span>, &<span>buf</span>) == -1) {<br><br> <span>return </span>-1;<br>}<br>
<br>
使用close函数关闭一个视频设备
<span>close</span>(<span>cameraFd</span>)
如果使用mmap,最后还需要使用munmap方法。
====EOF===============