Linux v4l2 camera video Acquisition

Source: Internet
Author: User

This article from: http://blog.csdn.net/jiang_dlut/archive/2010/09/07/5868636.aspx

 

1. What is video4linux?
Video4linux (v4l for short) is the kernel driver for video devices in Linux. Currently, video4linux2 has been added to the Linux kernel. You need to download the patch yourself. In Linux, a video device is a device file that can be read and written just like a common file. Under/dev/videon, the number of N may be 0, 1, 2, 3... generally 0.

In addition, we recommend that you use rawplayer to play back the raw data collected from the camera. You only need to save the collected data to the file **. YUV.


Ii. Video capture process in v4l2

1. open the device file. Int FD = open ("/dev/video0", o_rdwr);
2. obtain the capability of a device and check whether the device has any functions, such as video input or audio input/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 standard and frame format, including PAL, NTSC, and width and height.
vidioc_s_std, vidioc_s_fmt, struct v4l2_std_id, and 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 collected 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 get 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);
3. 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; // query Control
Struct v4l2_control control; // specific Control Value

The following describes the development process (I am looking for it online, are you still learning)

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 modeCodeChanged:

// Camerafd = open ("/dev/video0", o_rdwr, 0 );

Blocking Mode and non-blocking mode

ApplicationProgramYou can enable the video device in blocking or non-blocking mode. If you call the video device in non-blocking mode, the driver will still cache the video device (dqbuff) even if no information is captured) to the application.

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.

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:

<! -- [If! Supportlinebreaknewline] -->
<! -- [Endif] -->

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;

}

}

Iv. 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 of this segment and must use some means to change the address.

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

Read and Write: data is constantly copied in user space and kernel space, which occupies a large amount of user memory space and is inefficient.

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.

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 ProcessingAlgorithm

// Re-import to the cache queue

If (IOCTL (camerafd, vidioc_qbuf, & BUF) =-1 ){

Return-1;

}

Disable video devices

Use the close function to close a video device.

Close (camerafd)

Reprinted from: http://blog.chinaunix.net/u3/108006/showart_2284666.html

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.