Application and Implementation Based on V4L2 Video Capture and Cache Mechanism

Abstract: V4L is Linux's application interface for video devices. V4L2 is an upgraded version that fixes many design flaws in the first version. However, the conventional read/write function provided by it does not satisfy the high-speed transmission of large data volume, so the introduction of caching technology into the field of video acquisition can improve the throughput of the system. A dual-frame memory-mapped video capture mechanism is proposed. Since the data copying action is not required and the read/write time limit is reduced, the video capture performance can be improved. The experimental results show that the dual-frame memory mapping mechanism is fast and efficient in video acquisition, and the expected experimental results are achieved.
Keywords: V4L2; Linux; video capture; memory mapping

0 Introduction V4L (video for linux) is an application programming interface (API) for video devices developed by Alan Cox that began to appear in Linux 2.1. In the x version of the kernel, functions such as image acquisition, AM/FM broadcast, and image encoding and decoding can be implemented. However, due to its shortcomings in scalability and flexibility, it was gradually replaced by the upgraded version of V4L designed by Bill Dirks, V4L2, which started on Linux 2.5. Integrated in the x version of the kernel, in the read/write of video device data, the application can obtain image data in the kernel space through read/write method or memory mapping. The read/write method copies data between kernel space and user space, while memory mapping allows applications to directly access device memory, reducing data copies from kernel mode to user mode, thereby significantly increasing system throughput. The application and implementation of the caching mechanism in video capture is discussed below.

1 V4L2 video capture framework V4L2 uses a layered architecture, the application interface is the upper layer, and the lower layer is the driver of the video device, the general research field is to write the upper application, through the programming interface to control the video device to complete the corresponding Operation and function. The video capture program developed with V4L2 is device-independent, and any video capture device that supports V4L2 can port such programs, thus improving the portability of the video capture program.
When the video device is connected to the host, the driver first registers a character device with a major device number of 81, which is the unique identifier for the hardware. The driver uses the major device number to identify the hardware, while the system kernel uses the major device number to combine the device with the corresponding driver, and simultaneously loads the driver's member functions, minor device numbers, and other related information to make the device work properly. . Use the functions in Table 1 to access the V4L2 device or directly in the application. The specific functions are shown in Table 1.
a.JPG

Among them, the ioctl function is very powerful, it can manage the device's I / O channel, set the video format and frame format, and also provide the function of querying the current device properties, the main ioctl command is shown in Table 2.
b.JPG

These function prototypes are generally defined in include/linux/videodev2. h or videodev. h.
The specific process of video capture is described as follows:
(1) Open the device. Open the device file via the open() function and return the file descriptor.
(2) Initialize the device. First, query the device attribute through VIDIOC_QUERYCAP to determine whether the device is a legitimate video capture device, and determine which functions it supports. Then set the format of the image through VIDIOC_S_FMT, such as the size of the image; use VIDIOC_REQBUFS and malloc() respectively. Kernel space and user space allocate memory buffers; finally, memory mapping is done through the mmap() function.
(3) Image acquisition cycle. First, the empty buffer is moved into the pending queue through VIDIOC_QBUF, ready to receive image data; then the full buffer is moved out of the processed queue through VIDIOC_QBUF to display and process the image; finally, the acquisition is started and stopped by VIDIOC_STREAMON and VIDIOC_STREAMOFF.
(4) Turn off the device. Close the device file with the close() function.

2 Double frame buffer data transmission In video acquisition, first create two image buffers in the kernel space, and continuously store the collected images in a buffer. When the application needs an image, the driver does not do the copy operation, but establishes the mapping of the kernel buffer to the user space, that is, using the mmap() function to access the returned pointer, which is equivalent to accessing the image in the kernel. Buffer. Since no additional copying operations are required, the efficiency is greatly improved, and the image acquisition process is as shown in FIG.

c.JPG
The specific instructions are as follows:
(1) The program first uses VIDIOC_REQBUFS to request the image buffer from the driver. The v412_requestbuffers structure contains the type and quantity of the required buffer, but the driver has the right to determine the last number returned, so the program still needs to use the buffer returned by the system. The number, here the program returns 2 buffers.
(2) Since there are two buffers, calling mmap() twice establishes the correspondence between user space and kernel space buffer, and then reading the pointer returned by mmap() is equivalent to reading the image buffer.
(3) At this time, the driver still cannot read the image buffer. The VIDIOC_QBUF ioctl is called twice to add the buffer to the acquisition sequence inside the driver, and the acquired images are stored in these buffers.
(4) After calling the VIDIOC_STREAM ioctl, the driver starts to capture the image and place the image in the buffer.
(5) Although there are already images stored in the buffer, it is very necessary to read a buffer directly, because the buffer is still in the image acquisition sequence of the driver, it is possible to read half of the driver. This buffer is used to store the new image, and (5) in Figure 1 is the last call to VIDIOC_STREAMOFF to stop image acquisition. At this point, the driver will automatically remove all buffers from the image acquisition sequence, so no manual is required. Calling VIDIOC_DQBUF, then using munmap() to clear all memory maps results in inconsistent images. Therefore, before reading the buffer, call the VIDIOC_DQBUG ioctl to inform the driver not to use this buffer. In this stage, each buffer is usually read in the order shown in Figure 2.
d.JPG


3 Experimental results After the video acquisition program is completed, the kernel needs to be inserted into the module, and then the module is compiled by the make modules. The frame rate of the video capture without the double frame buffer technology is 6 f/s, which can be achieved by applying the method of this experiment. 8.3 f / s, video capture results screenshot shown in Figure 3:
E.jpg


4 Conclusion Video capture is the foundation of current technologies such as distance learning, remote diagnosis, video surveillance and video conferencing. In today's real-time requirements are getting higher and higher, and work performance is becoming more and more prominent. The dual frame buffer mapping mechanism can achieve high speed in data acquisition to meet general application requirements. However, there are still two restrictions on using memory mapping. : First, the stream-oriented device can not use memory mapping, such as serial port; second, the starting address and size of the mapping area must be an integer multiple of PAGE_SIZE, that is, the memory mapping is operated in units of PAGE_SIZE. These are all issues that need to be addressed in later research.

This article refers to the address: http://

Mobile phone battery is rechargeable Lithium Ion Battery for NOKIA,SAMSUNG and other phone brands. With its rechargeable ability and high energy desity, lithium ion battery is extensively applied in mobile phone and cell phone. With rich experience and advanced technology, our company supply battery for domestic and oversea clients. Please feel free to contact us!

Mobile Phone Battery For Samsung

Mobile Phone Battery For Samsung,Mobile Battery For Samsung,Phone Battery,Mobile Phone Battery

Ji'an Powercome New Energy Technology Co., Ltd. , http://www.expowercome.com