V4l2 driver transplantation and application (2)

Source: Internet
Author: User

II. Application of v4l2

The following describes the v4l2-driven application process.

1. Basic Process of video collection

Generally, video collection involves the following process:

 

 

2. Enable the video device

In v4l2, a video device is considered as a file. Use the OPEN function to open the device:

// Enable the camera device in non-blocking mode
Int camerafd;
Camerafd = open ("/dev/video0", o_rdwr | o_nonblock, 0 );
// If you enable the camera device in blocking mode, the code above becomes:
// Camerafd = open ("/dev/video0", o_rdwr, 0 );

About Blocking Mode and non-blocking mode: The application can enable the video device in blocking mode or non-blocking mode. If the video device is called in non-blocking mode, even if the information has not been captured, the driver will still return the items in the cache (dqbuff) to the application.

3. Set attributes and collection methods

After a video device is enabled, you can set the attributes of the Video device, such as cropping and scaling. This step is optional. In Linux programming, IOCTL functions are generally used to manage the I/O channels of devices:

Extern int IOCTL (INT _ FD, unsigned long int _ Request,...) _ Throw;

_ FD: The device ID. For example, the camerafd returned after the video channel is opened using the open function;

_ Request: a specific command identifier.

During v4l2 development, the following command identifier is generally used:

Vidioc_reqbufs: Memory Allocation

Vidioc_querybuf: converts the data cache allocated in vidioc_reqbufs to a physical address.

Vidioc_querycap: Query driver Function

Vidioc_enum_fmt: obtains the video formats supported by the current driver.

Vidioc_s_fmt: sets the frequency capture format of the current driver.

Vidioc_g_fmt: reads 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: Specifies the border of the video signal.

Vidioc_g_crop: border for reading Video Signals

Vidioc_qbuf: reads data from the cache

Vidioc_dqbuf: puts the data back into the cache queue

Vidioc_streamon: video display function

Vidioc_streamoff: function for ending video display

Vidioc_querystd: checks the standards supported by the current video device, such as pal or NTSC.

Some of these Io calls are required and some are optional.

4. Check the standards supported by the current video device

In Asia, Pal (720x576) cameras are generally used, while NTSC (720x480) is generally used in Europe, and vidioc_querystd is used for detection:

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

After detecting the standards supported by the video device, you also need to set the video capture format:

Struct v4l2_format FMT;
Memset (& FMT, 0, sizeof (FMT ));
FMT. type = v4l2_buf_type_video_capture;
FMT. FMT. pix. width = 720;
FMT. FMT. pix. Height = 576;
FMT. FMT. pix. pixelformat = v4l2_pix_fmt_yuyv;
FMT. FMT. pix. Field = v4l2_field_interlaced;
If (IOCTL (FD, vidioc_s_fmt, & FMT) =-1 ){
Return-1;
}

The v4l2_format struct is defined as follows:

Struct v4l2_format
{
Enum v4l2_buf_type; // data stream type, which must always be v4l2_buf_type_video_capture
Union
{
Struct v4l2_pix_format pix;
Struct v4l2_window win;
Struct v4l2_vbi_format VBI;
_ U8 raw_data [200];
} FMT;
};
Struct v4l2_pix_format
{
_ U32 width; // width, which must be a multiple of 16
_ U32 height; // height, which must be a multiple of 16
_ U32 pixelformat; // video data storage type, for example, yuv4: 2: 2 or RGB
Enum v4l2_field field;
_ U32 bytesperline;
_ U32 sizeimage;
Enum v4l2_colorspace colorspace;
_ U32 priv;
};

6. allocate memory

Next, you can allocate memory for Video Capture:

Struct v4l2_requestbuffers req;
If (IOCTL (FD, vidioc_reqbufs, & req) =-1 ){
Return-1;
}

V4l2_requestbuffers is defined as follows:

Struct v4l2_requestbuffers
{
_ U32 count; // The number of cached images, that is, the number of images in the cache queue.
Enum v4l2_buf_type; // data stream type, which must always be v4l2_buf_type_video_capture
Enum v4l2_memory memory; // v4l2_memory_mmap or v4l2_memory_userptr
_ U32 reserved [2];
};

7. obtain and record the cached physical space

With vidioc_reqbufs, we get req. count caches. Next, call the vidioc_querybuf command to obtain the cached addresses. Then, use the MMAP function to convert them to the absolute address in the application. Finally, put the cached address in the cache queue:

 

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 ++ ){
Memset (& Buf, 0, sizeof (BUF ));
Buf. type = v4l2_buf_type_video_capture;
Buf. Memory = v4l2_memory_mmap;
Buf. Index = numbufs;
// Read the cache
If (IOCTL (FD, vidioc_querybuf, & BUF) =-1 ){
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;
}
// Put it into the cache queue
If (IOCTL (FD, vidioc_qbuf, & BUF) =-1 ){
Return-1;
}
}

8. 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
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.

9. process collected data

V4l2 has a data cache that stores the number of cached data in Req. Count. The data cache uses the FIFO mode. When an application calls the cached data, the cache queue caches and sends the first collected video data and then collects a new video data. This process requires two IOCTL commands, vidioc_dqbuf and vidioc_qbuf:

Struct v4l2_buffer Buf;
Memset (& Buf, 0, sizeof (BUF ));
Buf. type = v4l2_buf_type_video_capture;
Buf. Memory = v4l2_memory_mmap;
Buf. Index = 0;

// Read the cache
If (IOCTL (camerafd, vidioc_dqbuf, & BUF) =-1)
{
Return-1;
}
//............ Video Processing Algorithm
// Re-import to the cache queue
If (IOCTL (camerafd, vidioc_qbuf, & BUF) =-1 ){
Return-1;
}

10. Disable the video device.

Use the close function to close a video device.

Close (camerafd)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.