⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 video for linux.htm

📁 LINUX内核编程的一些程序例子
💻 HTM
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0061)http://www.geocities.com/marco_corvi/games/lkpe/v4l/index.htm -->
<HTML><HEAD><TITLE>Video for Linux</TITLE>
<META http-equiv=Content-Type content="text/html; charset=windows-1252"><LINK 
href="Video for Linux_file/style.css" rel=stylesheet>
<META content="MSHTML 6.00.2800.1170" name=GENERATOR></HEAD>
<BODY>
<H2>Video for Linux</H2>
<P>
<DIV>References: <BR>A. Cox,<I>Video4Linux Programming</I>, 2000, available on 
<A href="http://www.kernelnewbies.org/" 
target=_top>http://www.kernelnewbies.org/</A> <BR>
<P></P></DIV>
<DIV>Video for Linux (V4L) is an abstract layer that presents a common interface 
for media devices, such as cameras, tvs, audios, and tuners. It sits in the 
<TT>drivers/media/video</TT> subdirectory of the kernel tree. </DIV>
<DIV>V4L is compiled in the kernel with the <CODE>CONFIG_VIDEO_DEV</CODE> 
option. <TT>proc</TT> filesystem support is further added with the 
<CODE>CONFIG_VIDEO_PROC_FS</TT> option. </DIV>
<DIV>The main structure is <TT>video_device</TT> define in the header file 
<TT>include/linux/videodev.h</TT>. It contains 
<UL>
  <LI><TT>owner</TT>, the module that own this structure; 
  <LI><TT>name</TT>, the video device name (32 char max); 
  <LI><TT>type</TT>, the video device type, which can be one of 
  VFL_TYPE_GRABBER, VFL_TYPE_VBI, VFL_TYPE_RADIO, VFL_TYPE_VTX. This field is 
  used by V4L to choose the driver's minor from the proper range. 
  <LI><TT>hardware</TT>, the hardware type. This too is not used by V4L; 
  <LI><TT>priv</TT>, a pointer to private data; 
  <LI><TT>busy</TT>, a guard to ensure that the driver is open only by one 
  process at a time; 
  <LI><TT>minor</TT>, the minor number assigned by V4L; 
  <LI><TT>devfs_handle</TT>, the handle in <TT>devfs</TT> returned by 
  <TT>devfs_register()</TT>. </LI></UL>as well as the pointers to the methods used 
by V4L to dispatch the user application calls: <TT>open()</TT>, 
<TT>close()</TT>, <TT>read()</TT>, <TT>write()</TT>, <TT>poll()</TT>, 
<TT>mmap()</TT>, <TT>ioctl</TT>, and <TT>initialize</TT>. This last one is 
called by V4L at initialization after it has finished with its own 
initializations. </DIV>
<DIV>A driver that want to export its functionalities to userland via V4L must 
call <TT>video_register_device()</TT>, which takes three args: a pointer to the 
driver's <TT>video_device</TT>, the type of the driver, and the requested minor 
(-1 for no request). When the driver wants to detach from V4L it calls 
<TT>video_unregister_device()</TT> with the pointer to its <TT>video_device</TT> 
structure. By registering with V4L the driver exposes to user processes only the 
function <TT>video_fops</TT>, ie, open(), release(), poll(), read(), write(), 
ioctl(), and mmap(). llseek() is no_llseek(). The others are forwarding to the 
driver's related functions. </DIV>
<DIV>The most complex API is probably <TT>ioctl</TT>. 
<UL>
  <LI><TT>VIDIOCGCAP</TT> is used to get the driver's capabilities. The 
  capability includes a <TT>name</TT>, the interface <TT>type</TT> 
  (VID_TYPE_CAPTURE, VID_TYPE_TUNER, etc.), the number of audio/tv 
  <TT>channels</TT>, the number of <TT>audios</TT> devices if appropriate, and 
  the maximum and minimum width and height. 
  <LI>Capture cards that put the data on a frame buffer must be told the address 
  of the buffer, its size and how it is organized. The ioctl calls are 
  <TT>VIDIOCGFBUF</TT> and <TT>VIDIOCSFBUF</TT>, and the structure used is 
  <TT>video_buffer</TT> which contains a <TT>base</TT> address pointer, 
  <TT>width</TT>, <TT>height</TT>, <TT>depth</TT> and the number of 
  <TT>bytesperline</TT>. 
  <LI>The capture window is ruled by <TT>VIDIOCGWIN</TT> and 
  <TT>VIDIOCSWIN</TT>. The struct <TT>video_window</TT> contains the window 
  position coordinates <TT>x</TT> and <TT>y</TT>, the <TT>width</TT> and 
  <TT>height</TT>, the <TT>chromakey</TT>, additional capture <TT>flags</TT>, a 
  list of <TT>clips</TT> (clipping rectangles) and their number 
  (<TT>clipcount</TT>). 
  <LI>Overlay capture is used to instruct the hardware to capture the frames 
  onto the frame buffer. The device must be of type VID_TYPE_OVERLAY, and 
  initialized for overlay, with <TT></TT>, and the refresh area can be clipped 
  with a list of rectangles (where the hardware does not refresh), or with a 
  mask which uses a chromakey, a colour that indicates no refresh. In the first 
  case the video_device must declare VID_TYPE_CLIPPING in the flags. In the 
  second it must be of type VID_TYPE_CHROMAKEY. If the video capture goes 
  directly into video memory the device is of type VID_TYPE_FRAMERAM. Before the 
  driver can overlay the images the video window must be set with 
  <TT>VIDIOCSWIN</TT>. There is also an ioctl that gets the video window 
  informations. Overlay capturing is activated or deactivated by the ioctl 
  <TT>VIDIOCCAPTURE</TT> with arg 1 or 0 respectively. 
  <LI>When there are several channels, it is possible to select one with 
  <TT>VIDIOCSCHAN</TT> and <TT>VIDIOCGCHAN</TT>. See the struct 
  <TT>video_channel</TT>. 
  <LI>Image properties are described by the <TT>video_picture</TT> structure: 
  <TT>brightness</TT>, <TT>hue</TT>, <TT>colour</TT>, <TT>contrast</TT>, 
  <TT>whiteness</TT>, <TT>depth</TT>, and <TT>palette</TT>. This last one can be 
  for example VIDEO_PALETTE_GREY, VIDEO_PALETTE_RGB24, VIDEO_PALETTE_RGB32. 
  Several other palettes are defined. 
  <LI>Tuners are described by the structure <TT>video_tuner</TT> and controlled 
  with <TT>VIDIOCGTUNER</TT>, <TT>VIDIOCSTUNER</TT>. 
  <LI>Audio inputs are described by the structure <TT>video_audio</TT> and 
  controlled with <TT>VIDIOCGAUDIO</TT>, <TT>VIDIOCSAUDIO</TT>. </LI></UL></DIV>
<DIV>Reading is possibble with the system call <TT>read()</TT>. V4L calls the 
driver's read() if there is one, otherwise returns an error (EINVAL), The video 
device may support also the <TT>write()</TT> system call. If not V4L return 0 on 
write(). </DIV>
<DIV>Another way to read images from the device is through the <TT>mmap</TT> 
interface, which must first be set up with the <TT>mmap()</TT> system call. Next 
the application gets the size of the memory buffer and offset of each frame in 
the buffer with the ioctl VIDIOCMBUF call. The application issues acquisition 
commands with the ioctl VIDIOCMCAPTURE call, specifying the requested frame 
index. This call starts the acquisition and returns immediately. The application 
waits for the completion of the acquisition with the ioctl VIDIOCSYNC. Therefore 
the sequence of calls is something like <BR><PRE>   int index = 0;
   int newindex;
   ioctl( fd, VIDIOCMCAPTURE, &amp;index);
   while ( 1 ) {
     newindex = (index+1) % FRAME_NUMBER;
     ioctl( fd, VIDIOCMCAPTURE, &amp;index);
     ioctl( fd, VIDIOCSYNC, &amp;index);
     // process frame "index"
     index = newindex;
   }
</PRE><BR></DIV>
<P><B>Exercise</B><BR>
<DIV>Write a memory-based video camera driver that registers with V4L subsystem. 
Check that the memory-based video camera operates properly, by writing a 
graphical program that displays the frames. <BR>Here is a solution for the 
driver: <A 
href="http://www.geocities.com/marco_corvi/games/lkpe/v4l/mmvideo.c">mmvideo.c</A> 
<A 
href="http://www.geocities.com/marco_corvi/games/lkpe/v4l/mmvideo_h.txt">mmvideo.h</A> 
and the <A 
href="http://www.geocities.com/marco_corvi/games/lkpe/v4l/Makefile.txt">Makefile</A>. 
</DIV>
<DIV>Finally here is a simple application that loads an image onto the camera 
memory, and displays the frames generated by the camera: <A 
href="http://www.geocities.com/marco_corvi/games/lkpe/v4l/mmvideotest_cpp.txt">mmvideotest.cpp</A>. 
It needs a few helper classes, <A 
href="http://www.geocities.com/marco_corvi/games/lkpe/v4l/mmvideoutil.tgz">mmvideoutil.tgz</A> 
(the image comes from the source <TT>Documentation</TT>) and libjpeg and libX11, 
which you probably already have. Check <TT>/proc/video/mmvideo</TT> while the 
application is running. Enjoy! </DIV>
<P><BR clear=all><FONT size=-1>Marco Corvi - 2003</FONT> <!-- text below generated by server. PLEASE REMOVE --></OBJECT></LAYER>
<DIV></DIV></SPAN></STYLE></NOSCRIPT></TABLE></SCRIPT></APPLET>
<SCRIPT 
language=JavaScript>var PUpage="76001084"; var PUprop="geocities"; </SCRIPT>

<SCRIPT language=JavaScript src="Video for Linux_file/pu5geo.js"></SCRIPT>

<SCRIPT language=JavaScript src="Video for Linux_file/ygIELib9.js"></SCRIPT>

<SCRIPT language=JavaScript>var yviContents='http://us.toto.geo.yahoo.com/toto?s=76001084&l=NE&b=1&t=1057746848';yviR='us';yfiEA(0);</SCRIPT>

<SCRIPT language=JavaScript src="Video for Linux_file/mc.js"></SCRIPT>

<SCRIPT language=JavaScript src="Video for Linux_file/geov2.js"></SCRIPT>

<SCRIPT language=javascript>geovisit();</SCRIPT>
<NOSCRIPT><IMG height=1 alt=setstats src="Video for Linux_file/visit.gif" 
width=1 border=0></NOSCRIPT> <IMG height=1 alt=1 
src="Video for Linux_file/serv.gif" width=1> <!-- w34.geo.scd.yahoo.com compressed/chunked Wed Jul  9 03:34:08 PDT 2003 --></CODE></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -