歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

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:

Copyright © Linux教程網 All Rights Reserved