V4L is the acronym for Linux, which is the subsystem of the Linux kernel about video devices, it provides a unified interface for the video drive under Linux, allowing applications to operate different video devices using a unified API function, which greatly simplifies the development and maintenance of video systems. Since the early v4l had a lot of flaws, Bill Dirks and others redesigned it and named video-for Linux 2 (V4L2), which was first seen in the linux2.5.x version. V4L2 has better scalability and flexibility than v4l, and supports more hardware devices. Therefore in the application v4l programming actually refers to V4L2, our this series mainly v4l2, but because of the historical reason, V4L2 General compatible v4l. So many programs can use the V4L interface.
1.v4l Support Equipment
v4l2 (for Linux) can support a variety of devices, which can have several interfaces:
1. Video acquisition interface (Capture interface): The device for this application can be a high-frequency head or a camera. V4L2 's original design was applied to this function. The following is also a focus on this application.
2. Video output Interface (interface): A device that can drive a computer's peripheral video-image device, such as an output TV signal format.
3. Direct Transmission Video Overlay interface: Its main work is to send the signal from the video capture device directly to the output device, without having to go through the CPU of the system.
4. Video interval blanking signal interface (VBI interface): It allows applications to access the video signal of the transmission blanking period.
5. Radio Interface (radio interface): Used to process audio streams received from AM or FM HF head devices.
2.v4l processing Basic Process
as with general equipment processing, generally v4l processing has four processes.
2.1 Open the V4L device node.
General v4l Equipment knot call is/dev/videon. If the first v4l device is/dev/video0.
int fd = open ("/dev/video0", O_RDWR | O_nonblock);
2.2 Configure Device/query device properties
mainly through the IOCTL to operate, like V4L2 common commands have
format
int IOCTL (int __fd, unsigned long int __request, .../*args*/);
__request is v4l2 some IOCTL commands, common as follows.
VIDIOC_REQBUFS: Allocating memory Vidioc_querybuf: Converts the data cache allocated in Vidioc_reqbufs to physical addresses Vidioc_querycap: query-driven functionality Vidioc_enum_ FMT: Gets the current driver-supported video format VIDIOC_S_FMT: Sets the current-driven frequency capture format vidioc_g_fmt: reads the current-driven frequency capture format vidioc_try_fmt: Verifies the current-driven display format VIDIOC_ Cropcap: Query-driven pruning capability Vidioc_s_crop: Set the border of the video signal Vidioc_g_crop: Read the border of the video signal vidioc_qbuf: Read the data out of the cache Vidioc_dqbuf: Put the data back into the cache queue Vidioc_streamon: Start video display function Vidioc_streamoff: End video display function VIDIOC_QUERYSTD: Check the current video device support criteria, such as PAL or NTSC.
Complete IOCTL command see http://v4l2spec.bytesex.org/spec/r7624.htm
2.3 Processing v4l video data
in the v4l device, some devices are removed from the hardware, sent to the application processing, such as camera hardware to obtain video data, through the V4L interface to send video data to the application, such as display screen or save as a file.
Some devices are sent from applications to hardware processing, such as TV interfaces.
in the V4L interface, three types of applications are configured to interact with the driver, respectively
Direct Read device file mode (read/write), user pointer mode (USERPTR), and mmap mapping method.
1 Mmap Way, drive the internal data space map to the application space, the two sides directly in this space for data exchange, is the most effective method, which is also one of the most common way 2)
Directly read the device file directly by calling the read (), write () function for data reading and output, the method is generally in conjunction with select ().
3 The user pointer method first requests a buffer from the application, then passes the buffer to the driver, drives it as a buffer, and realizes the memory sharing. This method is less used.
2.4 Turn off the deviceCall Close (), and if it is a memory-mapped method, you will need to call Munmap to unlock the map before shutting down.
3.v4l Two version difference
1. header file not the same v4l use #include <linux/videodevice.h>
v4l2 use #include <linux/videodevice2.h>
2.IOCTL command number, V4L uses vidiocxxxx form, while V4L2 uses vidioc_xxxx or vidioc_g_xxxx form.
such as V4L device Properties command is Vidiocgcap, and v4l2 corresponds to Vidioc_querycap.
3. The data structure is not the same, v4l is prefixed by video_, and v4l is prefixed by v4l2_. such as device properties
v4l1--> struct video_capability video_cap
V4l2-->struct v4l2_capability
4. Detect V4L Equipment VersionIn V4l2, the stipulation must implement the Vidioc_querycap command, and V4L1, the stipulation must realize the Vidiocgcap, uses this method to judge the device the version. See the following code.
* * Author:andrew Huang <bluedrum@163.com> * DETECTD V4L2 Device Version * * * * */#include <stdio.h> #includ E <string.h> #include <errno.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/ ioctl.h> #include <fcntl.h> #include <linux/videodev2.h> #include <linux/videodev.h>/* 0-
-Not V4L device 1--v4l Device 2--V4L2 device/int test_v4l_version (int fd) {int ret = 0;
Char dummy[256];
if ( -1!= ioctl (fd,vidioc_querycap,dummy)) {ret = 2;
else if ( -1!= ioctl (fd,vidiocgcap,dummy)) {ret = 1;
return ret;
int main (int argc,char * argv[]) {char dev_name[64] = "/dev/video2";
int CAM_FD =-1;
if (argc>1) {strncpy (dev_name,argv[1],sizeof (dev_name)-1);
printf ("Open device%s\n", dev_name); CAM_FD = open (dev_name,o_rdwr|
O_nonblock);
if (cam_fd = = 1) {printf ("Open failure \ n"); Return-1; Switch (test_v4l_version (CAM_FD)) {case 0:printf ("%s:fd%d isn ' t v4l deivce\n", dev_name,cam_fd
);
return-1;
Break
Case 1:printf ("\n### video4linux device info [%s] ###\n", dev_name);
Return-2;
Break
Case 2:printf ("\n### v4l2 device info [%s] ###\n", dev_name);
Break
Close (CAM_FD);
return 0;
}
Author:
Andrew HuangBluedrum@163.com from: http://blog.chinaunix.net/uid-20587912-id-405264.html PS: Error: expected expression before ' struct ' Solution: #include <sys/ioctl.h>