V4L2 Linux驱动简介

笔者最近有机会写了一个摄像头的驱动,是“One laptop per child”项目的中摄像头专用的。这个驱动使用了为此目的而设计的内核API:the Video4Linux2 API。在写这个驱动的过程中,笔者发现了一个惊人的问题:这个API的文档工作做得并不是很好,而用户层的文档则写的,实际上,相当不错。为了补救现在的状况,LWN将在未来的内个月里写一系列文章,告诉大家如何写V4L2接口的驱动。V4L2有一段历史了。大约在1998的秋天,灵感第一次出现在Bill Dirks的眼中。经过长足的发展,它于2002年11月,内核发布2.5.46时,融入了内核主干之中。然而直到今天,仍有一部分内核驱动不支持新的API,这种新旧API的转换工作仍在进行。同时,V4L2 API也在发展,并在2.6.18版本中进行了一些重大的改变。支持V4L2的应用依旧相对较少。

V4L2在设计时,是要支持很多广泛的设备的,它们之中只有一部分在本质上是真正的视频设备:

•video capture interface(影像捕获接口)从调谐器或是摄像头上获取视频数据。对很多人来讲,影像捕获(video capture)是V4L2的基本应用。由于笔者在这方面的经验是强项,这一系列文章也趋于强调捕获API,但V4L2不止这些。

•video output interface(视频输出接口)允许应用使用PC的外设,让其提供视频图像。有可能是通过电视信号的形式。
•捕获接口还有一个变体,存在于videooverlay interface(视频覆盖接口)之中。它的工作是方便视频显示设备直接从捕获设备上获取数据。视频数据直接从捕获设备传到显示设备,无需经过CPU。

•VBI interfaces(Vertical blanking interval interface,垂直消隐接口)提供垂直消隐期的数据接入。这个接口包括raw和sliced两种接口,其分别在于硬件中处理的VBI数据量。

•radio interface (广播接口)用于从AM或FM调谐器中获得音频数据。
也可能出现其它种类的设备。V4L2API中还有一些关于编译码和效果设备的stub,他们都用来转换视频数据流。然而这块的东西尚未完成确定,更不说应用了。还有“teletext”和”radio data system”的接口,他们目前在V4L1 API中实现。他们没有移动到V4L2的API中来,而且目前也没有这方面的计划。

视频驱动与其他驱动不同之处,在于它的配置方式多种多样。因此大部分V4L2驱动都有一些特定的代码,好让应用可以知道给定的设备有什么功能,并配置设备,使其按期望的方式工作。V4L2的API定义了几十个回调函数,用来配置如调谐频率、窗口和裁剪、帧速率、视频压缩、图像参数(亮度、对比度…)、视频标准、视频格式等参数。这一系列文章的很大部分都要用来考察这些配置的过程。

然后,还有一个小任务,就是有效地在视频频率下进行I/O操作。V4L2定义了三种方法来在用户空间和外设之间移动视频数据,其中有些会比较复杂。视频I/O和视频缓冲层,将会分成两篇文章来写,它们是用来处量一些共性的任务的.

原文:


Your editor has recently had the opportunity to write a Linux driver for acamera device - the camera which will be packaged with the One Laptop PerChild system, in particular. This driver works with the internal kernelAPI designed for such purposes: the Video4Linux2 API. In the process ofwriting this code, your editor made the shocking discovery that, in fact,this API is not particularly well documented - though the user-space sideis, instead,quitewell documented indeed. In an attempt to remedy the situation somewhat, LWN will, over the coming months, publish a series ofarticles describing how to write drivers for the V4L2 interface.

V4L2 has a long history - the first gleam came into Bill Dirks's eye backaround August of 1998. Development proceeded for years, and the V4L2 APIwas finally merged into the mainline in November, 2002, when2.5.46 was released. To thisday, however, quite a few Linux drivers do not support the newer API; theconversion process is an ongoing task. Meanwhile, the V4L2 API continuesto evolve, with some major changes being made in 2.6.18. Applicationswhich work with V4L2 remain relatively scarce.

V4L2 is designed to support a wide variety of devices, only some of whichare truly "video" in nature:

The video capture interface grabs video data from a tuner or camera device. For many, video capture will be the primary application for V4L2. Since your editor's experience is strongest in this area, this series will tend to emphasize the capture API, but there is more to V4L2 than that.
The video output interface allows applications to drive peripherals which can provide video images - perhaps in the form of a television signal - outside of the computer.
A variant of the capture interface can be found in thevideo overlay interface, whose job is to facilitate the direct display of video data from a capture device. Video data moves directly from the capture device to the display, without passing through the system's CPU.
The VBI interfaces provide access to data transmitted during the video blanking interval. There are two of them, the "raw" and "sliced" interfaces, which differ in the amount of processing of the VBI data performed in hardware.
The radio interface provides access to audio streams from AM and FM tuner devices.
Other types of devices are possible. The V4L2 API has some stubs for"codec" and "effect" devices, both of which perform transformations onvideo data streams. Those areas have not yet been completely specified,however, much less implemented. There are also the "teletext" and "radiodata system" interfaces currently implemented in the older V4L1 API; thosehave not been moved to V4L2 and there do not appear to be any immediateplans to do so.

Video devices differ from many others in the vast number of ways in whichthey can be configured. As a result, much of a V4L2 driver implements codewhich enables applications to discover a given device's capabilities and toconfigure that device to operate in the desired manner. The V4L2 APIdefines several dozen callbacks for the configuration of parameters liketuner frequencies, windowing and cropping, frame rates, video compression,image parameters (brightness, contrast, ...), video standards, videoformats, etc. Much of this series will be devoted to looking at how thisconfiguration process happens.

Then, there is the small task of actually performing I/O at video rates inan efficient manner. The V4L2 API defines three different ways of movingvideo data between user space and the peripheral, some of which can be onthe complex side. Separate articles will look at video I/O and thevideo-buf layer which has been provided to handle common tasks.

Subsequent articles will appear every few weeks, and will be added to thelist below:

相关推荐