V4l2-video driver model in Linux

Source: Internet
Author: User

On the dm6446 platform, we generally use the monavista operating system for program control on the GPP end. As an embedded Linux
Similar to Linux, the video driver v4l2 (video for Linux two) is also used for video collection and output. This article briefly describes how to use v4l2.

Generally, video collection involves the following process:

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 );
Blocking Mode and non-blocking mode
The application can enable the video device in blocking or non-blocking mode. If the video device is called in non-blocking mode, the driver will still cache (dqbuff) even if no information is captured) to the application.

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.

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:
//......
}
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;
};
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];
};
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;
}
}

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 is
The code and data that can be accessed by the kernel. 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 location
Address.

There are three video collection methods: Read and Write; Memory ing and user pointer modes.

The read and write Methods continuously copy data in user space and kernel space, occupying a large amount of user memory space and reducing efficiency.

Memory ing method: 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.

User pointer mode: Memory segments are allocated by the application itself. In this case, set the memory field to v4l2_memory_userptr in v4l2_requestbuffers.

V4l2 has a data cache that stores the number of cached data in Req. Count. Data Cache uses the FIFO method. When an application calls the cached data, the cache queue first collects
The video data is cached and sent out, and a new video data is collected. 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 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;
}

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.