V4L2 video collection procedure and Interface Description

Source: Internet
Author: User
Seven407blog. csdn. netseven407articledetails6401792 general operation procedure (Video device): 1. Open the device file. Intfdopen (devvideo0, O_RDWR); 2. Obtain the capability of the device to see what functions the device has, such as whether it has video input or audio input and output. VIDIOC_QUE

Seven407 http://blog.csdn.net/seven407/article/details/6401792 general procedure (Video device): 1. Open the device file. Int fd = open (/dev/video0, O_RDWR); 2. Obtain the capability of the device to see what functions the device has, such as whether it has video input or audio input/output. VIDIOC_QUE

Seven407

Http://blog.csdn.net/seven407/article/details/6401792

General operation procedure (Video device ):

1. Open the device file. Int fd = open ("/dev/video0", O_RDWR );
2. Obtain the capability of the device to see what functions the device has, such as whether it has video input or audio input and output. VIDIOC_QUERYCAP, struct v4l2_capability
3. Select video input. A video device can have multiple video inputs. VIDIOC_S_INPUT, struct v4l2_input
4. Set the video format and frame format, including PAL, NTSC, and width and height.
VIDIOC_S_STD, VIDIOC_S_FMT, struct v4l2_std_id, struct v4l2_format
5. Apply for frame buffering from the driver. Generally, there are no more than five frames. Struct v4l2_requestbuffers
6. Map the requested frame buffer to the user space, so that you can directly operate on the captured frames without having to copy them. Mmap
7. add all the requested frame buffers to the queue to store the collected data. VIDIOC_QBUF, struct v4l2_buffer
8. Start video collection. VIDIOC_STREAMON
9. Get out of the queue to get the frame buffer of the collected data and obtain the original collected data. VIDIOC_DQBUF
10. re-import the buffer to the end of the queue to collect data cyclically. VIDIOC_QBUF
11. Stop video collection. VIDIOC_STREAMOFF
12. Disable the video device. Close (fd );

Common struct (see/usr/include/linux/videodev2.h ):

Struct v4l2_requestbuffers reqbufs; // request to request a frame buffer from the driver, which contains the number of requests
Struct v4l2_capability cap; // the features of this device, such as whether it is a video input device.
Struct v4l2_input input; // Video input
Struct v4l2_standard std; // video standard, such as PAL and NTSC
Struct v4l2_format fmt; // frame format, such as width and height

Struct v4l2_buffer buf; // represents a frame in the driver
V4l2_std_id stdid; // video system, for example, V4L2_STD_PAL_ B
Struct v4l2_queryctrl query; // a type of control
Struct v4l2_control control; // specific control Value

1. User controlls is actually some attributes that users can set, such as the brightness in the video,

Video4linux extracts some of the most common settings and assigns them an ID,

You can use these IDs to view the value set by the current device, or set a new value for this device,

Some configuration packages contain many sub-settings, so they have the meaning of menu, that is, for a specific control,

When listing its properties, we find that its type includes menu, so we can take the control id as the parameter,

View its menu and its values. Of course, you can use custom control and extended control.

It seems that there is a Control ID in Camera control id that can set focus. You can take a look at this.

2. The Data format application can negotiate with the device on the communication Data, that is, you can set the Data format used by the device,

You can obtain the format of the data used by the device, or try whether a certain type of data device is supported.

VIDIOC_G_FMT and VIDIOC_S_FMT ioctls are used, while VIDIOC_TRY_FMT is used to check whether a certain setting is supported by the device,

It is only a test and does not work. We can still use VIDIOC_ENUM_FMT to list all the image formats supported by the device.

The image format, size (width, height), and other information are involved in video.
3. crapping and scaling
This is to crop, scale, and crop the data, and sample only part of the size of the image we can get,
The main parameters for cropping are the position, length, and width. The scale setting is obtained through VIDIOC_G_FMT and VIDIOC_S_FMT and
Set the length and width of the current image. View

We can assume that bounds is the largest image range that can be captured, and defrect is the largest range that our devices can get,
The ioctl of VIDIOC_CROPCAP can be used to obtain the crap-related attributes of the device v4l2_cropcap,
The bounds is the bounds, which is actually the upper limit. Each device has a default sampling range, that is, defrect,
Default rect means that it is smaller than bounds. This range is also based on VIDIOC_CROPCAP ioctl.
The obtained defrect in the v4l2_cropcap structure is represented by VIDIOC_G_CROP and VIDIOC_S_CROP.
To get and set the current crop settings of the device.

Http://blog.mcuol.com/User/GuoHuiCao/Article/44253_1.htm

In Linux, a video device is viewed as a device file. The device file is stored in the/dev directory. The complete path is named/dev/video0.

The basic process of video collection is as follows: Enable the video device, set the video device attributes, collection method, and video data processing, and disable the video device, as shown in:


1. Enable the video device

It is very easy to open a video device. In V4L2, a video device is considered as a file. Use the open function to open the device:

1. Enable the camera device in non-blocking mode
Int cameraFd;
CameraFd = open ("/dev/video0", O_RDWR | O_NONBLOCK );

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

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.

Ii. instructions on the use of common control commands for Linux video device drivers

Ioctl is used to set video device properties. ioctl has three parameters: fd, cmd, and parameter, indicating device descriptor, control command, and control command parameters.

Common Control commands supported by the Linux video Device Driver Interface V4L2 are as follows:

1. Control Command VIDIOC_ENUM_FMT

Function: Obtain the video formats supported by the current video device.

Parameter description: The video format descriptor type of V4L2 is struct v4l2_fmtdesc.

Description of returned values: when the execution is successful, the function returns 0; The. pixelformat and. description members in the struct v4l2_fmtdesc struct return the video formats supported by the current video device;

Example:

Bytes -------------------------------------------------------------------------------------------------

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', description = ''% s'}/n ",

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

(Fmt. pixelformat> 16) & 0xFF, (fmt. pixelformat> 24) & 0xFF,

Fmt. description );

}

Bytes ---------------------------------------------------------------------------------------------------------------

2. Control Command VIDIOC_QUERYCAP

Function: Query functions of video devices;

Parameter description: The parameter type is V4L2. The Capability Description type is struct v4l2_capability;

Return Value Description: When the function is successfully executed, the return value is 0. After the function is successfully executed, the functions supported by the current video device are returned in the struct v4l2_capability struct variable; for example, the video capture function V4L2_CAP_VIDEO_CAPTURE and V4L2_CAP_STREAMING are supported.

Example:

Bytes -----------------------------------------------------------------------------------------------------------

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;

}

Bytes ----------------------------------------------------------------------------------------------------------

After executing the VIDIOC_QUERYCAP command, the cap variable contains the capability information of the Video device. The program checks the device capability information in the cap to determine whether the device supports a certain function.

3. Control Command VIDIOC_S_FMT

Function: sets the video data format of a video device, such as the length, width, and image format (JPEG and YUYV formats) of the video image data );

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

Return Value Description: when the execution is successful, the return value of the function is 0;

Example:

Bytes ----------------------------------------------------------------------------------------------------------

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_YUV420;

Tv4l2_format.fmt.pix.field = V4L2_FIELD_INTERLACED;

Iret = ioctl (fd_usbcam, VIDIOC_S_FMT, & tv4l2_format );

Bytes -----------------------------------------------------------------------------------------------------------

Note: If the video device driver does not support the image format 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, therefore, in programming, after setting all the video formats, you must read the struct v4l2_format struct variable again to obtain the actual video format.

4. Control Command VIDIOC_REQBUFS

Function: Request the V4L2 driver to allocate a video buffer (apply for V4L2 video driver to allocate memory). V4L2 is the driver layer of the Video device and is located in the kernel space, therefore, the memory requested by using the VIDIOC_REQBUFS control command word is in the kernel space and cannot be accessed directly by applications. After you call the mmap memory ing function to map the kernel space memory to the user space, applications access the kernel space by accessing the user space address.

Parameter description: The Request Buffer data struct type struct v4l2_requestbuffers of the parameter type V4L2;

Return Value Description: when the execution is successful, the function returns 0; The V4L2 driver layer has allocated a video buffer;

Example:

Bytes -----------------------------------------------------------------------------------------------------------

Struct v4l2_requestbuffers tV4L2_reqbuf;

Memset (& tV4L2_reqbuf, 0, sizeof (struct v4l2_requestbuffers ));

TV4L2_reqbuf.count = 1; // Number of applied Buffers

TV4L2_reqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

TV4L2_reqbuf.memory = V4L2_MEMORY_MMAP;

Iret = ioctl (fd_usbcam, VIDIOC_REQBUFS, & tV4L2_reqbuf );

Bytes --------------------------------------------------------------------------------------------------------------

Note: VIDIOC_REQBUFS will modify the count value of tV4L2_reqbuf, and the count value of tV4L2_reqbuf will return the number of video buffers successfully applied;

5. Control Command VIDIOC_QUERYBUF

Function: Query Information about the allocated V4L2 video buffer, including the Usage Status of the video buffer, the offset address in the kernel space, and the buffer length. In application design, VIDIOC_QUERYBUF is called to obtain the video buffer information of the kernel space, and then mmap is called to map the kernel space address to the user space, in this way, the application can access the video buffer in the kernel space.

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

Description of return values: when the execution is successful, the return value of the function is 0. The struct v4l2_buffer struct variable stores information about the instruction buffer;

Generally, after VIDIOC_QUERYBUF is called in an application to obtain the kernel buffer information, mmap is called to map the kernel space address to the user space to facilitate access to the user space application.

Example:

Bytes ----------------------------------------------------------------------------------------------------------------

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; // ID of the kernel video buffer to be retrieved

Iret = ioctl (fd_usbcam, VIDIOC_QUERYBUF, & tV4L2buf );

// Map the kernel space buffer to the user space buffer.

AppBufLength = tV4L2buf. length;

AppBufStartAddr = mmap (NULL/* start anywhere */,

TV4L2buf. length,

PROT_READ | PROT_WRITE/* access privilege */,

MAP_SHARED/* recommended */,

Fd_usbcam, tV4L2buf. m. offset );

Bytes ------------------------------------------------------------------------------------------------------------------

After calling VIDIOC_QUERYBUF to obtain the buffer information of the kernel space, the above Code calls the mmap function to map the buffer of the kernel space to the user space. For usage of the mmap function, please query the relevant information;

6. Control Command VIDIOC_QBUF

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

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

Description of return values: when the operation is successful, the return value of the function is 0. After the function is successfully executed, the video buffer of the Instruction enters the video input queue. When the video device starts to take an image, the corresponding video data is saved to the corresponding video buffer in the video input queue.

Example:

Bytes -------------------------------------------------------------------------------------------------------------

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; // The number of the video buffer in the kernel space to be delivered to the video input queue;

Iret = ioctl (fd_usbcam, VIDIOC_QBUF, & tV4L2buf );

Bytes ----------------------------------------------------------------------------------------------------------

7. Control Command VIDIOC_STREAMON

Function: starts the video collection command. After the app calls VIDIOC_STREAMON to start the video collection command, the video device driver starts to collect video data, save the collected video data to the video buffer zone of the video driver.

Parameter description: The video buffer type of V4L2 is enum v4l2_buf_type;

Description of returned values: if the function is successfully executed, the returned value is 0. After the function is successfully executed, the video device driver starts to collect video data, in this case, the application generally calls the select function to determine whether a video data collection is complete. When the video device driver completes a video data collection and saves the data to the video buffer, the select function returns, the application can then read the video data; otherwise, the select function is blocked until the video data collection is complete. For more information about the use of the Select function, see.

Example:

Bytes ----------------------------------------------------------------------------------------------------------

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

Bytes ----------------------------------------------------------------------------------------------------------

8. Control Command VIDIOC_DQBUF

Function: obtains a video buffer that stores a frame of video data from the output queue of the video buffer;

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

Return Value Description: When the function is successfully executed, the return value is 0. After the function is successfully executed, the corresponding kernel video buffer contains the video data currently taken, the application can access the user space to read the video data. (Previously, the user space and kernel space memory ing has been done by calling the mmap function ).

Example:

Bytes ----------------------------------------------------------------------------------------------------------

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

Bytes ----------------------------------------------------------------------------------------------------------

9. Control Command VIDIOC_STREAMOFF

Function: Stop the video capture command. After the app calls VIDIOC _ STREAMOFF to stop the video capture command, the video device driver does not collect video data.

Parameter description: The video buffer type of V4L2 is enum v4l2_buf_type;

Description of returned values: When the function is successfully executed, the returned value is 0. After the function is successfully executed, the video device stops collecting video data.

Example:

Bytes ----------------------------------------------------------------------------------------------------------

Enum v4l2_buf_type v4l2type;

V4l2type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

Iret = ioctl (fd_usbcam, VIDIOC_STREAMOFF, & v4l2type );

Bytes -----------------------------------------------------------------------------------------------------------

The preceding section describes how to use the most common control commands for Linux video driver V4L2. You can use the preceding control commands to complete the video data collection process. For more control command instructions on V4L2, see: http://v4l2spec.bytesex.org/spec/book1.htm

I hope this article will help you understand the video driver programming in linux.

Bytes ---------------------------------------------------------------------------------------------------------------------

Author: Cao Guohui Nanjing lingembedded education embedded Linux gold medal lecturer

QQ: 1539730715 welcome to the discussion of Embedded Linux enthusiasts.

Lingembedded education technology is an authoritative embedded linxu training expert in Jiangsu, professional agent Fei Ling, and youlong Embedded ARM development board. Web: http://www.jslinux.com

Copyright statement: This article is copyrighted by the author and cannot be used for commercial purposes without permission.

Http://www.jslinux.com

Powered by Zoundry Raven

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.