Video device Programming Based on video4linux

Source: Internet
Author: User


1. What is video4linux?
Video4linux (v4l for short) is the kernel driver for video devices in Linux.
Video4linux2 is now available and has not been added to the Linux kernel. You need to download the patch by yourself.
In Linux, a video device is a device file that can be read and written just like a common file.
The camera is under/dev/video0.

2. Video programming process in video4linux
1. Enable the video device:
2. Read Device Information
3. Change the current device settings (if necessary)
4. Two Methods for video collection: (failed)
1. Memory ing
2. Read from the device directly
5. process the collected video (not done)
6. Disable the video device.

Data structure defined for the program
Typedef struct v4l_struct
{
Int FD;
Struct video_capability capability;
Struct video_channel [4];
Struct video_picture picture;
Struct video_window window;
Struct video_capture capture;
Struct video_buffer buffer;
Struct video_mmap MMAP;
Struct video_mbuf mbuf;
Unsigned char * map;
Int frame;
Int framestat [2];
} VD;
3. Data Structures Supported by video4linux and their usage
1. video_capability
Contains the basic information of the device (device name, supported maximum and minimum resolutions, and signal source information)
Contained components:
Name [32] // device name
Maxwidth, maxheight, minwidth, minheight
Channels // Number of signal sources
Type // capture, color, black/white, cropping, etc. Values such as vid_type_capture

Video_picture
Various attributes of images collected by devices
Brightness 0 ~ 65535
Hue
Color
Contrast
Whiteness
Depth // 24
Palette // video_palette_rgb24

Video_channel attributes of various signal sources
Channel // signal source number
Name
Tuners
Type video_type_ TV | ideo_type_camera
Norm Standard

Video_window // contains the capture area information.
X coordinates in windows.
Y x coordinates in windows.
Width the width of the image capture.
Height the height of the image capture.
Chromakey a host order rgb32 value for the chroma key.
Flags additional capture flags.
Clips a list of clipping rectangles. (set only)
Clipcount the number of clipping rectangles. (set only)

Video_mbuf
Information of frames mapped using MMAP
Size // the size of each frame
Frames // The maximum number of supported Frames
Offsets // The offset of each frame to the base address
Description of the buffer at the bottom layer of video_buffer
Void * basebase physical address of the buffer
Int height of the frame buffer
Int widthwidth of the frame buffer
Int depthdepth of the frame buffer
Int bytesperline number of bytes of memory between the start of two adjacent lines
The actually displayed part is generally smaller than the part described in it.
Video_mmap // used for MMAP

4. Key Steps
Work done in the initialization phase
Int IOCTL (int fd, IND cmd ,...) Abbreviation of Input Output Control
It is used to "talk" with the device ".
If the driver supports ioctl, you can use the ioctl function in your program to control the I/O channel of the device.
FD: The file descriptor of the device. CMD: the control command of the user program on the device. The ellipsis is generally a parameter indicating the type length.
1. Open the video:
Open ("/dev/video0", vdfd );
Close the video device ("/dev/video0", vdfd );
2. Read the information in video_capability.
IOCTL (vd-> FD, vidiocgcap, & (vd-> capability ))
After successful reading, you can read the VD-> capability component eg.
Printf ("maxwidth = % d" VD-> capability. maxwidth );

3. Read the information in video_picture.
IOCTL (vd-> FD, vidiocgpict, & (vd-> picture ));

4. Change the value of the video_picture component.
First, assign a new value to the component, and then call vidiocspict.
Eg.
VD-> picture. Color = 65535;
If (IOCTL (vd-> FD, vidiocspict, & (vd-> picture) <0)
{
Perror ("vidiocspict ");
Return-1;
}

5. initialize the channel
You must first obtain information in VD-> capability.
For (I = 0; I <> capability. channels; I ++)
{
VD-> channel [I]. Channel = I;
If (IOCTL (vd-> FD, vidiocgchan, & (vd-> channel [I]) <0)
{
Perror ("v4l_get_channel :");
Return-1;
}
}

The first method of image capturing: video capturing using MMAP (memory ing)
MMAP () system calls allow processes to share memory by ing to the same common file. After a common file is mapped to the process address space, the process can access the file like accessing the common memory without calling read (), write (), and other operations.
The shared memory of two different processes A and B means that the same physical memory is mapped to the process address space of process a and process B. Process A can instantly view the updates to data in the shared memory of process B, and vice versa.
One obvious advantage of using shared memory communication is high efficiency, because the process can directly read and write the memory without any data copying.
1. Set picture attributes
2. initialize video_mbuf to obtain the mapped buffer information.
IOCTL (vd-> FD, vidiocgmbuf, & (vd-> mbuf ))
3. You can modify the current settings of video_mmap and frame status.
Eg. VD-> MMAP. format = video_palette_rgb24
VD-> framestat [0] = VD-> framestat [1] = 0; VD-> frame = 0;
4. Bind MMAP to video_mbuf.
Void * MMAP (void * ADDR, size_t Len, int Prot, int flags, int FD, off_t offset)
Len // The number of bytes mapped to the address space of the calling process. It starts from offset at the beginning of the mapped file.
Prot // specify the access permission for the shared memory: prot_read (readable), prot_write (writable), prot_exec (executable)
One of flags // map_shared map_private is required.
// Map _ fixed is not recommended
ADDR // The starting address of the shared memory. Generally, it is set to 0, indicating that it is allocated by the system.
The returned value of MMAP () is the starting address actually allocated by the system.
If (vd-> map = (unsigned char *) MMAP (0, VD-> mbuf. size, prot_read | prot_write, map_shared, VD-> FD, 0) <0)
{
Perror ("v4l_mmap MMAP :");
Return-1;
}

4. vidiocmcapture for video capturing in MMAP Mode
IOCTL (vd-> FD, vidiocmcapture, & (vd-> MMAP ));
If the call is successful, the screenshot of the first frame is not blocked,
Whether to leave the interception to vidiocsync for determination

5. Call vidiocsync to wait for the end of a frame capture.
If (IOCTL (vd-> FD, vidiocsync, & frame) <0)
{
Perror ("v4l_sync: vidiocsync ");
Return-1;
}
If yes, the screenshot is completed. You can start the next vidiocmcapture operation.
Frame is the sequence number of the currently captured frame.

About double buffering:
Video_bmuf bmuf. Frames = 2;
When one frame is processed, another frame can be collected.
Int frame; // The frame currently collected
Int framestat [2]; // The collection is not started when the collection ends. | wait until the collection ends.
The frame address is composed
VD-> map + VD-> mbuf. offsets [VD-> frame] to obtain

Call munmap to unbind after collection ends
Munmap (vd-> map, VD-> mbuf. Size)

Summary of the MMAP process:
1. Obtain Image Information
2. initialize video_mbuf vidiocgmbuf
3. video_mbuf binds MMAP () to MMAP ()
4. You can modify the current settings of video_mmap and frame status.
Eg. VD-> MMAP. format = video_palette_rgb24
VD-> framestat [0] = VD-> framestat [1] = 0; VD-> frame = 0;
Loop:
{
If (framestat [frame number] = 0)
Vidiocmcapture (); // start interception, set framestat [frame number] = 1
If (framestat [frame number] = 1)
Vidiocsync (); // wait until the screenshot is completed, set framestat [frame number] = 0
Process the collected frames. // The frame address is VD-> map + VD-> mbuf. offsets [VD-> frame].
Frame number = frame number ^ 1;
}

The second method of video capturing is to directly read the device.
Attributes of the buffer size and image must be set by the user in advance.
Call read ();
Int read (file descriptor to be accessed; pointer to the information to be read/written; number of characters to be read/written );
The return value is the number of characters actually read/write.
Eg
Int Len;
Unsigned char * vd-> map = (unsigned char *) malloc (vdcapability. maxwidth * vdcapability. maxheight );
Len = read (vdfd, VD-> map,
Vdcapability. maxwidth * vdcapability. maxheight * 3 );

Programming Problems
The program is compiled, the device can be opened, and the device information can be read and modified.
An error occurred while intercepting the video.
1. MMAP Method
After necessary initialization, call
IOCTL (vd-> FD, vidiocmcapture, & (vd-> MMAP); failed
Error message: invalid argument
If subsequent IOCTL (vd-> FD, vidiocsync, & frame) is executed, the same error message is displayed.
I downloaded a program from the Internet, saying that the video capture card was used to achieve video frequency interception, and the same error information was displayed after compilation and operation.
Guess the camera does not support the MMAP method?

2. Read () method:
Reading data may be incorrect.
Set the frame size to 176*144*3 (color) = 76038 bytes.
Calling Len = read () can only read 38016 bytes, Which is exactly half of the expected value. I still don't know what the data is.
The application and code called gqcam are stored on the Internet in read () mode. Only half of the windows have images and the other half have black screens, it is estimated that the data read is only half the size of a frame. I don't know why.

Reference original code:
Gqcam-0.9
Effectv-0.3.9
References:
Video4linux programming Alan Cox
Discussion on video streaming Chen junhong
Video4linux kernel api reference

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.