Linux v4l2 camera video capture __linux

Source: Internet
Author: User

One, what is Video4linux
Video4linux (referred to as V4L), Linux is about the video device kernel driver, now has VIDEO4LINUX2, has not joined the Linux kernel, the use of their own download patches. In Linux, the video device is a device file, you can read and write like a normal file, the camera under/dev/videon, n may be 0,1,2,3 ... General 0.

Another, recommend to play from the camera collected raw data player rawplayer, just want to save the data collected to file ***.yuv OK.

Second, v4l2 capture video Flow

1. Open the device file. int Fd=open ("/dev/video0″,o_rdwr");
2. Get the capability of the equipment to see what function 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 input. Vidioc_s_input,struct V4l2_input
4. Set the format and frame of the video, including PAL,NTSC, the format of the frame including width and height.
Vidioc_s_std,vidioc_s_fmt,struct v4l2_std_id,struct V4l2_format
5. To drive the application frame buffer, generally no more than 5. struct V4l2_requestbuffers
6. Map the application to the frame buffer to the user space, so you can directly manipulate the captured frames, without having to replicate. Mmap
7. All the frame buffers applied to the queue, in order to store the collected data. Vidioc_qbuf,struct V4l2_buffer
8. Start the video collection. Vidioc_streamon
9. Out of the queue to obtain the collected data frame buffer to obtain the original acquisition data. Vidioc_dqbuf
10. The buffer will be back into the end of the queue so that it can be recycled. Vidioc_qbuf
11. Stop the Video collection. Vidioc_streamoff
12. Turn off the video device. Close (FD);
Iii. common structural bodies (see/USR/INCLUDE/LINUX/VIDEODEV2.H):

struct V4l2_requestbuffers reqbufs;//the request to drive the frame buffer, which contains the number of applications
struct V4l2_capability cap;//The function of this device, such as whether it is a video input device
struct V4l2_input input; Video input
struct V4l2_standard std;//video format, such as PAL,NTSC
struct V4l2_format fmt;//frame format, such as width, height, etc.

struct V4l2_buffer buf;//represents a frame in the driver
v4l2_std_id stdid;//video format, for example: V4l2_std_pal_b
Control of struct V4l2_queryctrl query;//query
struct V4l2_control control;//The value of the specific control

The following specific description of the development process (online search, also in the study)

Turn on the video device

In V4l2, the video device is considered a file. Open this device using the Open function:

Open camera device in non-blocking mode

int camerafd;

CAMERAFD = Open ("/dev/video0″, O_RDWR | O_nonblock, 0);

If you open the camera device in blocking mode, the code above becomes:

CAMERAFD = Open ("/dev/video0″, O_rdwr, 0");

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.

Setting properties and Collecting methods

After you turn on the video device, you can set the properties of the video device, such as cropping, scaling, and so on. This step is optional. In Linux programming, the IOCTL function is generally used to manage the I/O channel of a device:

extern int ioctl (int __fd, unsigned long int __request, ...) __throw;

__FD: The ID of the device, such as the CAMERAFD returned after the video channel was opened with the Open function;

__request: A specific command marker.

In V4L2 development, the following command markers are typically used:

VIDIOC_REQBUFS: Allocating memory
Vidioc_querybuf: Converts the data cache allocated in Vidioc_reqbufs to physical addresses
Vidioc_querycap: Query-driven features
VIDIOC_ENUM_FMT: Get current driver supported video formats
VIDIOC_S_FMT: Set the current drive frequency capture format
VIDIOC_G_FMT: Read the current drive frequency capture format
VIDIOC_TRY_FMT: Verifying the current-driven display format
Vidioc_cropcap: Query-driven pruning capabilities
Vidioc_s_crop: Set the border of the video signal
Vidioc_g_crop: Read the border of the video signal
VIDIOC_QBUF: Read the data out of the cache
VIDIOC_DQBUF: Put the data back into the cache queue
Vidioc_streamon: Start video display function
Vidioc_streamoff: End Video Display function
VIDIOC_QUERYSTD: Check the current video device support criteria, such as PAL or NTSC.
Some of these IO calls are required and some are optional.

Check the current video device support criteria

In Asia, a PAL (720X576) standard camera is generally used, whereas in Europe, NTSC (720X480) is used to detect the use of VIDIOC_QUERYSTD:

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 testing the standard 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 structure is defined as follows:

struct V4l2_format

{

Enum V4l2_buf_type type; Data flow type, 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; Wide, must be a multiple of 16

__u32 height; High, must be a multiple of 16

__u32 PixelFormat; Video data storage type, such as//yuv4:2:2 or RGB

enum V4l2_field field;

__u32 Bytesperline;

__u32 Sizeimage;

Enum V4l2_colorspace ColorSpace;

__u32 Priv;

};

Allocating memory

Next you can allocate memory for video capture:

struct V4l2_requestbuffers req;

if (IOCTL (FD, VIDIOC_REQBUFS, &req) = =-1) {

return-1;

}

The v4l2_requestbuffers definition is as follows:

struct V4l2_requestbuffers

{

__u32 count; Number of caches, which means how many photos are kept in the cache queue

Enum V4l2_buf_type type; Data flow type, must always be v4l2_buf_type_video_capture

Enum V4l2_memory memory; V4l2_memory_mmap or V4l2_memory_userptr

__u32 reserved[2];

};

Get and record the cached physical space

Using Vidioc_reqbufs, we get the Req.count cache, the next step is to get the cached address by invoking the VIDIOC_QUERYBUF command, and then using the Mmap function to convert to an absolute address in the application, and finally putting this cache into 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 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 in cache queue

if (IOCTL (FD, VIDIOC_QBUF, &buf) = =-1) {

return-1;

}

}

Four, about video capture mode

The operating system generally divides the memory used by the system into user space and kernel space, respectively, by application management and operating system management. The application can access the address of the memory directly, while kernel space holds the code and data for the kernel to access, and the user cannot access it directly. The data captured by V4L2 is initially stored in kernel space, which means that the user cannot access the memory directly, and must use some means to convert the address.

A total of three kinds of video capture methods: Using read, write mode, memory mapping and user pointer mode.

Read, write way: In user space and kernel space constantly copy data, occupy a large number of user memory space, efficiency is not high.

Memory mapping: It is an effective way to map the memory in the device to the memory controls in the application and directly process the device memory. The Mmap function above is to use this method.

User pointer mode: the memory fragment is allocated by the application itself. This requires that the MEMORY field be set to V4l2_memory_userptr in the v4l2_requestbuffers.

Process acquisition Data

V4L2 has a data cache that holds Req.count number of cached data. The data cache is FIFO, and when the application invokes the cached data, the cache queue sends the first captured video data cache and collects a 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 cache

if (IOCTL (CAMERAFD, vidioc_dqbuf, &buf) = = 1)

{

return-1;

}

............ Video Processing algorithm

Back into the cache queue

if (IOCTL (CAMERAFD, vidioc_qbuf, &buf) = = 1) {

return-1;

}

Turn off the video device

Use the close function to turn off a video device

Close (CAMERAFD)

Reproduced 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.