Linux-based V4L2 video architecture driver Authoring (reprint)

Source: Internet
Author: User

Transferred from: http://www.linuxidc.com/Linux/2011-03/33022.htm

In fact, I have just started to do not know how to write the driver, what do not understand, only know I need to do the project in the process of learning, so, I found myself a about to write a video capture monitoring project to do Linux, and then last semester just when listening to Brother said, with the Dean to do the project, did not have to do, So directly out of the Doctor's team, took refuge in the dean's door, hehe, speaking here is not really I see the wind so camel, but Always let me do the single-chip machine, I am bored, he also know that I have to learn the embedded, so, finally, I think about it, since can not help him to do the project, that left no meaning, his own go (hehe, but now seems to continue to help the doctor to do the project), so find the Dean to do the tutor ... into his lab. Finish the crap, serve the dishes (alas, who is my dish ah)

In fact, learning is a process, the first most difficult to accept, after the easy ....

First, before you learn v4l2, you have to have a webcam, or you don't have to play.

In addition, it is best to assign yourself a plan, a targeted study, set the time, so that learning has a sense of urgency

V4L2 architecture is not very difficult to get started, high-level I have not qualified to say that when I first started to look at the time, but also feel super difficult ah, because there is no grasp of the system, the whole did not know, so I spent two days a day to study the articles and procedures

The program is the most classic of the two articles:

Http://www.linuxidc.com/Linux/2011-03/33020.htm

Http://www.linuxidc.com/Linux/2011-03/33021.htm

Basic knowledge, I will directly post it,//after I added

V4L2 Video acquisition programming based on Linux video driver interface 44253105764


Linux system, the video device is treated as a device file, the device files are stored in the/dev directory, the full path of the device file name is:/dev/video0.

The basic steps of the video capture process are as follows: Turn on the video device, set the video device properties and capture mode, video data processing, and turn off the video device as shown in:

First, open the video device

Opening the video device is very simple, in v4l2, the video device is considered a file. Use the Open function to turn on this device:

1. Turn on the camera device in non-blocking mode
int camerafd;
CAMERAFD = open ("/dev/video0", O_RDWR | O_nonblock);

2. If the camera device is turned on with blocking mode, the code above becomes:
CAMERAFD = open ("/dev/video0", O_RDWR);

About blocking mode and non-blocking mode

The application can open the video device using blocking mode or non-blocking mode, and if the video device is called using nonblocking mode, the driver will still return the contents of the cache (Dqbuff) to the application even if the information has not been captured.

Second, the Linux video device driver commonly used control command instructions

Set the video device properties to be set by the IOCTL, the IOCTL has three parameters, namely FD, CMD, and parameter, which represent the device descriptor, Control command, and Control command parameters.

The common control commands supported by the Linux Video device driver interface v4l2 are as follows:

1. Control command VIDIOC_ENUM_FMT//enum What do you mean? Check it out for yourself.

Function: Gets the video format supported by the current video device.

Parameter description: Video format descriptor type v4l2 with parameter type struct V4L2_FMTDESC

Return value Description: When execution succeeds, the function return value is 0;struct v4l2_fmtdesc the. PixelFormat and. Description members in the struct body return the video formats supported by the current video device;

Examples of Use:

-------------------------------------------------------------------------------------------------

struct V4L2_FMTDESC fmt;

memset (&fmt, 0, sizeof (FMT));

Fmt.index = 0;

Fmt.type = v4l2_buf_type_video_capture;

while (ret = ioctl (dev, vidioc_enum_fmt, &fmt) = = 0) {

fmt.index++;

printf ("{pixelformat = '%c%c%c%c ', description = '%s '}\n",

Fmt.pixelformat & 0xFF, (Fmt.pixelformat >> 8) & 0xFF,

(Fmt.pixelformat >>) & 0xFF, (Fmt.pixelformat >>) & 0xFF,

Fmt.description);

}

-------------------------------------------------------------------------------------------------------

2. Control command Vidioc_querycap What do//query and caps mean?

Function: Query the function of video device;

Parameter description: The capability description type of the parameter type v4l2 struct v4l2_capability;

Return value Description: When execution succeeds, the function returns a value of 0, after the function succeeds, the struct v4l2_capability returns the functions supported by the current video device in the struct-body variable, for example, the support video capture function V4l2_cap_video_capture, v4l2_ Cap_streaming and so on.

Examples of Use:

-------------------------------------------------------------------------------------------------------

struct v4l2_capability cap;

Iret = IOCTL (Fd_usbcam, Vidioc_querycap, &cap);

if (Iret < 0)

{

printf ("Get VIDIEO capability Error,error Code:%d \ n", errno);

return;

}

------------------------------------------------------------------------------------------------------

After executing the vidioc_querycap command, the CAP variable contains the capability information for the video device, which determines whether the device supports a feature by examining the device capability information in the CAP.

3. Control command VIDIOC_S_FMT//tell you directly that S is the set meaning

Function: Set video data format of video device, such as setting the length and width of video image data, image format (JPEG, yuyv format);

Parameter description: Video data format type v4l2 of parameter type struct V4l2_format;

Return value Description: When execution succeeds, the function returns a value of 0;

Examples of Use:

----------------------------------------------------------------------------------------------------------

struct V4l2_format Tv4l2_format;

Tv4l2_format.type = v4l2_buf_type_video_capture;

Tv4l2_format.fmt.pix.width = Img_width;

Tv4l2_format.fmt.pix.height = Img_height;

Tv4l2_format.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;

Tv4l2_format.fmt.pix.field = v4l2_field_interlaced;

Iret = IOCTL (Fd_usbcam, VIDIOC_S_FMT, &tv4l2_format);

-----------------------------------------------------------------------------------------------------------

Note: If the video device driver does not support the image format that you set, the video driver will re-modify the value of the struct v4l2_format struct variable to the image format supported by the video device, so in program design, after all the video formats are set, to get the actual video format, re-read struct V4l2_format struct-body variable.

4. Control command VIDIOC_REQBUFS//I don't ask, you ask yourself.

Function: Request V4L2 driver to allocate video buffer (request V4L2 video Drive allocated memory), V4L2 is the driving layer of video device, located in kernel space, so the memory requested by VIDIOC_REQBUFS Control command Word is located in kernel space, the application cannot be accessed directly, After the kernel space memory is mapped to the user space by calling the MMAP memory-mapped function, the application accesses the kernel space by accessing the user-space address.

Parameter description: Request for parameter type V4L2 buffer data structure body type struct v4l2_requestbuffers;

Return value Description: When execution succeeds, the function return value assigns the video buffer to the 0;V4L2 drive layer;

Examples of Use:

-----------------------------------------------------------------------------------------------------

struct V4l2_requestbuffers tv4l2_reqbuf;

memset (&tv4l2_reqbuf, 0, sizeof (struct v4l2_requestbuffers));

Tv4l2_reqbuf.count = 1; Number of application buffers

Tv4l2_reqbuf.type = v4l2_buf_type_video_capture;

Tv4l2_reqbuf.memory = V4l2_memory_mmap;

Iret = IOCTL (Fd_usbcam, Vidioc_reqbufs, &tv4l2_reqbuf);

-----------------------------------------------------------------------------------------------------

Note: Vidioc_reqbufs modifies the count value of Tv4l2_reqbuf, and the count value of Tv4l2_reqbuf returns the number of video buffers that actually requested success;

5. Control command Vidioc_querybuf

Function: Query the information about the V4L2 video buffer that has been allocated, including the usage state of the video buffer, the offset address in the kernel space, the buffer length, and so on. In the application design, the VIDIOC_QUERYBUF is acquired to obtain the video buffer information of the kernel space, and then the function is called to map the kernel space address to the user space, so that the application can access the video buffer located in the kernel space.

Parameter description: The parameter type is V4L2 buffer data structure type struct v4l2_buffer;

Return value Description: When executed successfully, the function return value is 0;struct v4l2_buffer the information about the buffer that holds the instruction in the struct variable;

In general, the application calls Vidioc_querybuf to obtain the kernel buffer information, and then calls the MMAP function to map the kernel-space address to the user space to facilitate user-space application access.

Examples of Use:

-------------------------------------------------------------------------------------------------------

struct V4l2_buffer tv4l2buf;

memset (&tv4l2buf, 0, sizeof (struct v4l2_buffer));

Tv4l2buf.type = v4l2_buf_type_video_capture;

Tv4l2buf.memory = V4l2_memory_mmap;

Tv4l2buf.index = i; To get the information number of the kernel video buffer

Iret = IOCTL (Fd_usbcam, Vidioc_querybuf, &tv4l2buf);

Mapping kernel space buffers to user space buffers

Appbuflength = Tv4l2buf.length;

APPBUFSTARTADDR = mmap (NULL/* Start anywhere */,

Tv4l2buf.length,

Prot_read | Prot_write/* Access privilege */,

map_shared/* Recommended */,

Fd_usbcam, Tv4l2buf.m.offset);

-------------------------------------------------------------------------------------------------------

The above code, after calling Vidioc_querybuf to obtain the buffer information of the kernel space, then call the MMAP function to map the kernel space buffer to the user space; for the use of mmap function, readers should inquire about the relevant information;

6. Control command Vidioc_qbuf

Function: Put an empty video buffer into the video buffer input queue;

Parameter description: The parameter type is V4L2 buffer data structure type struct v4l2_buffer;

Return value Description: When the execution succeeds, the function return value is 0, after the function executes successfully, the instruction (specified) video buffer enters the video input queue, when the video device is started to take the image, the corresponding video data is saved to the video input queue corresponding video buffer.

Examples of Use:

-------------------------------------------------------------------------------------------------------

struct V4l2_buffer tv4l2buf;

memset (&tv4l2buf, 0, sizeof (struct v4l2_buffer));

Tv4l2buf.type = v4l2_buf_type_video_capture;

Tv4l2buf.memory = V4l2_memory_mmap;

Tv4l2buf.index = i; Instruction (specified) the number of the kernel space video buffer to be served to the video input queue;

Iret = IOCTL (Fd_usbcam, Vidioc_qbuf, &tv4l2buf);

----------------------------------------------------------------------------------------------------

7. Control command Vidioc_streamon

Function: Start the video capture command, the application calls Vidioc_streamon to start the Video capture command, the video device driver starts capturing video data and saves the captured video data to the video-driven video buffer.

Parameter description: The video buffer type of the parameter type v4l2 enum V4l2_buf_type;

Return value Description: When the execution succeeds, the function returns a value of 0, after the function succeeds, the video device driver starts to collect the video data, at this time the application generally calls the Select function to determine whether a frame video data acquisition is complete, when the video device driver completes a frame video acquisition and saves in the video buffer, The Select function returns, and the application can then read the video data, otherwise the SELECT function blocks until the video data collection is complete. For the use of the Select function, please refer to the relevant information.

Examples of Use:

----------------------------------------------------------------------------------------------------------

Enum V4l2_buf_type v4l2type = v4l2_buf_type_video_capture;

Fd_set FDS;

struct Timeval TV;

Iret = IOCTL (Fd_usbcam, Vidioc_streamon, &v4l2type);

Fd_zero (&fds);

Fd_set (Fd_usbcam, &fds);

Tv.tv_sec = 2; /* Timeout. */

tv.tv_usec = 0;

Iret = Select (fd_usbcam+ 1, &fds, NULL, NULL, &TV);

----------------------------------------------------------------------------------------------------------

8. Control command VIDIOC_DQBUF//second d is the meaning of the deletion

Function: Obtain a video buffer with one frame of video data from the output queue of the video buffer;

Parameter description: The parameter type is V4L2 buffer data structure type struct v4l2_buffer;

Return value Description: When the execution succeeds, the function return value is 0, after the function executes successfully, the corresponding kernel video buffer holds the video data that is currently taken, the application can read the video data by accessing the user space. (The memory map of user space and kernel space has been done by calling function Mmap earlier).

Examples of Use:

----------------------------------------------------------------------------------------------------------

struct V4l2_buffer tv4l2buf;

memset (&tv4l2buf, 0, sizeof (struct v4l2_buffer));

Tv4l2buf.type = v4l2_buf_type_video_capture;

Tv4l2buf.memory = V4l2_memory_mmap;

Iret = IOCTL (Fd_usbcam, Vidioc_dqbuf, &tv4l2buf);

Sasoritattoo Note: The VIDIOC_DQBUF command results in the buffering frame information removed from the queue to this tvl2buf. The role of the v4l2_buffer structure is equivalent to the application of the buffer frame agent, to find the buffer frame must first ask it, through it to contact the buffer frame, played the role of the Middle Bridge.

----------------------------------------------------------------------------------------------------------

9. Control command Vidioc_streamoff

Function: Stop video capture command, application call VIDIOC_ streamoff stop video capture command, video device driver is not collecting video data.

Parameter description: The video buffer type of the parameter type v4l2 enum V4l2_buf_type;

Return value Description: When execution succeeds, the function returns a value of 0 and the video device stops capturing the video data after the function is successfully executed.

Examples of Use:

----------------------------------------------------------------------------------------------------------

Enum V4l2_buf_type V4l2type;

V4l2type = v4l2_buf_type_video_capture;

Iret = IOCTL (Fd_usbcam, Vidioc_streamoff, &v4l2type);

-----------------------------------------------------------------------------------------------------------

The above is the Linux video device driver v4l2 most commonly used control command instructions, through the use of the above control command, you can complete a video data acquisition process. V4L2 more Control command instructions please refer to: http://v4l2spec.bytesex.org/spec/book1.htm

I hope this article is helpful for you to understand the video driver programming under Linux.

I'm a split line, you can't see me ********************************************

There's really nothing to talk about, that is, you do not go around, these are the essence, I at that time, I have seen at least 15 times, not that you take it up to study a 15 times, but in the middle school, learning, in the use of learning, good things have to slowly product, the taste is coming out, this process is the process of growth. When you write a program that can actually capture an image, you've completed a half of Long march.


I uploaded a time ago is also based on the V4L2 video architecture of the acquisition program, is written by someone else, I commented, the above can go to the next look, in addition, in the course of learning, I also looked for a lot of this from the Internet master's thesis, now look back, that can also be called Master Thesis?? No content, pure is blind nonsense, things estimate have not done out, on their own in there, some copied others copy of the wrong do not know,, fortunately I never thought to take an examination, now go to college I do not feel boring also postgraduate?? Ridiculous, ridiculous ...

Linux-based V4L2 video architecture driver Authoring (reprint)

Related Article

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.