Linux-based V4L2 video architecture driver Authoring __linux

Source: Internet
Author: User
Tags set time

The recent study of Mjpg-streamer, found this article, feel good, took over.

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

In fact, learning is a process, at the beginning of the most difficult to accept, after the easy ....

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

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

V4L2 architecture is not very difficult, advanced I have not qualified to say, I just started to see when, but also feel super difficult Ah, because did not grasp the system, the whole did not know, so I spent two days every day to study that several articles and procedures

These two articles are the most classic:

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

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

Basic knowledge, I will directly posted out,//after I added the

V4L2 Video acquisition programming based on Linux video driver interface 44253105764


In Linux, the video device is treated as a device file, the device file is stored in the/dev directory, and the full path device file name is:/dev/video0.

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

First, open the video device

Turning on the video device is very simple, in v4l2, the video device is considered a file. Open this device using the Open function:

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

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

About blocking mode and non-blocking mode

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

Two, Linux video device driver commonly used control command instructions

Set the video device properties by IOCTL to set, IOCTL has three parameters, respectively, FD, CMD, and parameter, representing 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 it means. Find out for yourself.

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

Parameter description: The type of the video format descriptor for the parameter type is V4L2 struct V4L2_FMTDESC

Return value Description: When execution succeeds, the function returns the value of the. PixelFormat and. Description members in the 0;STRUCT V4L2_FMTDESC structure to return the video format supported by the current video device;

Use examples:

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

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//query and cap What do you mean?

Function: Query the function of video equipment;

Parameter description: The parameter type is V4L2 ability description type struct v4l2_capability;

Return value Description: When execution succeeds, the function returns a value of 0; After the function has executed successfully, struct v4l2_capability the function supported by the current video device in the struct variable; for example, support for video capture function V4l2_cap_video_capture, v4l2_ Cap_streaming and so on.

Use examples:

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

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 the Vidioc_querycap command is executed, the CAP variable contains the ability 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//direct tell you that S is the meaning of set

Function: Set up video data format, such as set video image data long, wide, image format (JPEG, yuyv format);

Parameter description: The parameter type is V4L2 video data format type struct V4l2_format;

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

Use examples:

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

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 you have set, the video driver will change the value of the struct V4L2_FORMAT structure variable to the image format supported by the video device, so in the program design, after setting all the video formats, to get the actual video format, To re-read the struct v4l2_format struct variable.

4. Control command VIDIOC_REQBUFS//I'm not asking, you ask yourself.

Function: Request V4L2 driver to allocate video buffer (Request V4L2 Video Drive Allocate memory), V4L2 is the drive layer of video device, is located in kernel space, so the memory that controls the command word request through VIDIOC_REQBUFS is located in kernel space, the application cannot be accessed directly, After the kernel space memory is mapped to user space by invoking the mmap memory mapping function, the application accesses the kernel space by accessing the user space address.

Parameter description: The parameter type is V4L2 the application 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;

Use examples:

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

struct V4l2_requestbuffers tv4l2_reqbuf;

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

Tv4l2_reqbuf.count = 1; Number of request 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 tv4l2_reqbuf count value, and the Tv4l2_reqbuf count value returns the number of video buffers in which the actual application is successful;


5. Control command Vidioc_querybuf

Function: Query for information about the V4L2 video buffers that have been allocated, including the usage status of the video buffer, the offset address in the kernel space, the buffer length, and so on. In application design, VIDIOC_QUERYBUF is used to obtain the video buffer information of kernel space, then call function mmap Map the kernel space address to user space so that the application can access the video buffer in kernel space.

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

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

In general, the call to Vidioc_querybuf in the application obtains kernel buffer information, then calls the MMAP function to map the kernel space address to user space, which facilitates the access of the user space application.

Use examples:

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

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 obtain 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);

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

After the above code obtains the kernel space buffer information by the call Vidioc_querybuf, then calls the MMAP function to map the kernel space buffer to the user space; On the use of the MMAP function, please inquire 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 execution succeeds, the function returns a value of 0; After the function succeeds, the instruction (specified) video buffer enters the video input queue, and the corresponding video data is saved to the corresponding video buffer in the video input queue when the video device is taken to take the image.

Use examples:

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

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; Directive (specifies) the number of kernel space video buffers to be dropped into 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 start the video capture command, the video device driver starts to collect the video data, and saves the collected video data to the video-driven video buffer.

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

Return value Description: When execution succeeds, the function returns a value of 0; After the function executes successfully, the video device driver starts to collect the video data, at this time the application generally by the call select function to judge whether a frame of video data collection completes, when the video device drives completes the video data collection and saves to the video buffer, The Select function returns, the application can then read the video data, or the Select function blocks until the video data collection completes. The use of the Select function invites the reader to refer to the relevant information.

Use examples:

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

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 deletion

Function: To obtain a video buffer which has saved a 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 execution succeeds, the function returns a value of 0; When the function succeeds, the corresponding kernel video buffer holds the video data currently captured, and the application can read the video data by accessing the user space. (The memory mapping of user space and kernel space has been done previously by calling function Mmap).

Use examples:

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

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 result passes the buffer frame information removed from the queue to this tvl2buf. V4l2_buffer structure function is equivalent to the application of the buffer frame agent, to find the buffer frame to ask it first, through which 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 does not collect video data.

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

Return value Description: The function returns a value of 0 when execution succeeds, and the video device stops capturing the video data after the function has been successfully executed.

Use examples:

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

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 collection process. V4L2 more control command use 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.

There's nothing to talk about, that's what you're not going through, these are the essence, I at that time every kind of see at least 15 times above it, not that you take it up to study a 15 times, but in the middle school, learning, in the use of and learn, good things have to slowly product, this flavor came out, this process is the process of growth. When you write a program that can actually capture an image, you've finished a little of the Long march.

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.