V4l2 is an upgraded version of v4l. It provides a set of interface specifications for Video device programs in Linux. Includes a set of data structures and underlying v4l2 driver interfaces.
1. Common structs are defined in the kernel directory include/Linux/videodev2.h.
Struct v4l2_requestbuffers // request frame buffering, corresponding to the command vidioc_reqbufs
Struct v4l2_capability // specifies the function of the Video device. The corresponding command is vidioc_querycap.
Struct v4l2_input // specifies the video input information, corresponding to the command vidioc_enuminput.
Struct v4l2_standard // specifies the video standard, such as PAL and NTSC. It corresponds to the vidioc_enumstd command.
Struct v4l2_format // frame format, corresponding to commands such as vidioc_g_fmt and vidioc_s_fmt
Struct v4l2_buffer // a frame of image cache in the driver, corresponding to the command vidioc_querybuf
Struct v4l2_crop // video signal rectangular border
V4l2_std_id // video standard
2. Common ioctl interface commands are also defined in include/Linux/videodev2.h.
Vidioc_reqbufs // allocate memory
Vidioc_querybuf // convert the data cache allocated in vidioc_reqbufs to a physical address
Vidioc_querycap // query driver Function
Vidioc_enum_fmt // obtain the video format supported by the current driver
Vidioc_s_fmt // sets the frequency capture format of the current driver.
Vidioc_g_fmt // read the current drive's frequency capture format
Vidioc_try_fmt // verify the display format of the current driver
Vidioc_cropcap // query driver pruning capability
Vidioc_s_crop // sets the rectangular border of the video signal.
Vidioc_g_crop // The rectangular border for reading Video Signals
Vidioc_qbuf // read data from the cache
Vidioc_dqbuf // put the data back into the cache queue
Vidioc_streamon // starts the video display function
Vidioc_streamoff // The function used to display the end video.
Vidioc_querystd // check the standards supported by the current video device, such as pal or NTSC.
3. Procedure
V4l2 provides many access interfaces. You can select an operation method as needed. Note that few drivers fully implement all interface functions. Therefore, you need to refer to the driver source code or carefully read the instructions for use by the driver provider.
The following describes an operation procedure for your reference.
(1) Open the Device File
Int FD = open (devicename, mode );
Devicename:/dev/video0,/dev/video1 ......
Mode: o_rdwr [| o_nonblock]
If you use the non-blocking mode to call a video device, when there is no available video data, it does not block and returns immediately.
(2) obtain the capability of the device
Struct v4l2_capability capability;
Int ret = IOCTL (FD, vidioc_querycap, & capability );
Check whether a device has any functions, such as video input.
(3) Select Video Input
Struct v4l2_input input;
...... Initialize Input
Int ret = IOCTL (FD, vidioc_querycap, & input );
A video device can have multiple video inputs. This function is optional if there is only one input.
(4) detection of supported video formats
V4l2_std_id STD;
Do {
Ret = IOCTL (FD, vidioc_querystd, & STD );
} While (ret =-1 & errno = eagain );
Switch (STD ){
Case v4l2_std_ntsc:
//......
Case v4l2_std_pal:
//......
}
(5) set the video capture format
Struct v4l2_format FMT;
FMT. type = v4l2_buf_type_video_output;
FMT. FMT. pix. pixelformat = v4l2_pix_fmt_uyvy;
FMT. FMT. pix. Height = height;
FMT. FMT. pix. width = width;
FMT. FMT. pix. Field = v4l2_field_interlaced;
Ret = IOCTL (FD, vidioc_s_fmt, & FMT );
If (RET ){
Perror ("vidioc_s_fmt \ n ");
Close (FD );
Return-1;
}
(6) apply for frame caching from the driver
Struct v4l2_requestbuffers req;
If (IOCTL (FD, vidioc_reqbufs, & req) =-1 ){
Return-1;
}
The v4l2_requestbuffers structure defines the number of caches, and the driver applies for the corresponding number of video caches accordingly. Multiple caches can be used to create a FIFO to improve video collection efficiency.
(7) obtain the information of each cache and map it to the user space.
Typedef struct videobuffer {
Void * start;
Size_t length;
} Videobuffer;
Videobuffer * buffers = calloc (req. Count, sizeof (* buffers ));
Struct v4l2_buffer Buf;
For (numbufs = 0; numbufs <Req. Count; numbufs ++) {// map all the caches
Memset (& Buf, 0, sizeof (BUF ));
Buf. type = v4l2_buf_type_video_capture;
Buf. Memory = v4l2_memory_mmap;
Buf. Index = numbufs;
If (IOCTL (FD, vidioc_querybuf, & BUF) =-1) {// obtain the cache information of the corresponding index, here, we mainly use length and offset information to complete subsequent MMAP operations.
Return-1;
}
Buffers [numbufs]. Length = Buf. length;
// Convert to relative address
Buffers [numbufs]. Start = MMAP (null, Buf. length,
Prot_read | prot_write,
Map_shared,
FD, Buf. M. offset );
If (buffers [numbufs]. Start = map_failed ){
Return-1;
}
(8) Start video collection
Int buf_type = v4l2_buf_type_video_capture;
Int ret = IOCTL (FD, vidioc_streamon, & buf_type );
(9) retrieve the sampled frame cache in the FIFO Cache
Struct v4l2_buffer Buf;
Memset (& Buf, 0, sizeof (BUF ));
Buf. type = v4l2_buf_type_video_capture;
Buf. Memory = v4l2_memory_mmap;
Buf. Index = 0; // This value is returned by the following IOCTL
If (IOCTL (FD, vidioc_dqbuf, & BUF) =-1)
{
Return-1;
}
Find the corresponding MMAP ing cache based on the returned Buf. Index and retrieve the video data.
(10) re-import the buffer that has just been processed to the end of the queue so that it can be collected cyclically.
If (IOCTL (FD, vidioc_qbuf, & BUF) =-1 ){
Return-1;
}
(11) Stop video collection
Int ret = IOCTL (FD, vidioc_streamoff, & buf_type );
(12) disable the video device
Close (FD );
Video collection methods
The operating system generally divides the memory used by the system into user space and kernel space, which are managed by applications and operating systems respectively. Applications can directly access the memory address, while the kernel space stores the code and data for the kernel to access, and users cannot directly access it. The data captured by v4l2 is initially stored in the kernel space, which means that the user cannot directly access the memory segment and must use some means to convert the address.
There are three video collection methods:
1) read and write: Read and Write Functions are directly used. This method is the easiest, But it constantly copies data in user space and kernel space, and occupies a large amount of memory in user space and kernel space, which is inefficient.
2) memory ing mode (MMAP): This is an effective method to map the memory in the device to the memory control in the application and directly process the device memory. The above MMAP function uses this method.
3) User pointer mode: the memory is allocated by applications in the user space and the address is passed to the driver in the kernel, then, the v4l2 driver directly fills the data in the memory of the user space. In this case, set the memory field to v4l2_memory_userptr in v4l2_requestbuffers.
The first method has the lowest efficiency. Both methods can improve the execution efficiency. However, for the MMAP method, the document describes the remember the buffers are allocated in physical memory, as opposed to virtual memory which can be swapped out to disk. applications shocould free the buffers as soon as possible with the munmap () function
. (When the MMAP method is used, buffers is allocated in the kernel space. In this case, these buffers cannot be exchanged to the virtual memory. Although this method does not affect the read/write efficiency, however, it always occupies the memory in the kernel space. When the system memory is limited, if a large number of processes are running at the same time, the overall performance of the system will be affected .)
Therefore, the recommended sequence for three video collection methods is userptr, MMAP, and read-write. When MMAP or userptr is used, there is a concept of a circular Buffer Queue, which contains N buffers and the video frame data collected by the driver, is stored in each buffer. Every time you use vidioc_dqbuf to retrieve a buffer, and after processing the data, you must use vidioc_qbuf to put the buffer back to the ring Buffer Queue. The ring Buffer Queue also makes these two video acquisition methods more efficient than direct
Read/write.