Video for Linux programming Overview

Source: Internet
Author: User
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 as a normal file. The camera is under/dev/video0.

2. Video programming process in video4linux
(1) Enable the video device:
(2) reading device information
(3) change the current device settings (if not necessary)
(4) video collection methods:
A. Memory ing
B. Read from the device directly
(5) process the collected videos
(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 resolution, and signal source information), including the following components:
• Name [32] // device name
• Maxwidth, maxheight, minwidth, and minheight
• Channels // Number of signal sources
• Type // capture, color, black/white, and cropping. Values such as vid_type_capture

(2) attributes of images collected by video_picture Devices
• Brightness 0 ~ 65535
• Hue
• Color
• Contrast
• Whiteness
• Depth // 24
• Palette // video_palette_rgb24

(3) video_channel attributes of each signal source
Channel // signal source number
Name
Tuners
Type video_type_ TV | ideo_type_camera
Norm Standard

(4) video_window // contains the capture area information
XX 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)
(5) video_mbuf // frame information 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
(6) Description of the buffer at the bottom layer of video_buffer
Void * basebase physical address of the buffer
Int heightheight of the frame buffer
Int widthwidth of the frame buffer
Int depthdepth of the frame buffer
Int bytesperlinenumber of bytes of memory between the start of two
Adjacent lines
The actually displayed part is generally smaller than the part described in it.
(7) video_mmap // used for MMAP

4. Key Steps
(1) Open the video:
Open ("/dev/video0", VD à FD );
Close ("/dev/video0", VD à FD) to disable the video device );
(2) read the information in video_capability.
IOCTL (vd-> FD, vidiocgcap, & (vd-> capability ))
After successful reading, you can read the VD-> capability component eg.
(3) read the information in video_picture.
IOCTL (vd-> FD, vidiocgpict, & (vd-> picture ));
(4) change the value of the video_picture component (this can be left blank)
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 (which can be left blank)
• Information in VD-> capability must be obtained first.
• For (I = 0; I <VD-> capability. channels; I ++)
•{
• VD-> channel [I]. Channel = I;
• If (IOCTL (vd-> FD, vidiocgchan, & (vd-> channel [I]) <0)
•{
• Perror ("v4l_get_channel :");
• Return-1;
•}
•}

Focus: two methods of image capturing
1. video capturing using MMAP (memory ing)
• MMAP (
) System calls enable shared memory between processes by ing 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.
• Two Different Processes A and B share the memory, which 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 information of the mapped buffer.
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. The value starts from the offset byte 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 for ADDR.
// The starting address of shared memory. Generally, it is set to 0, indicating that the address 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;
•}
(5) 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
(6) 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;
• One frame can be acquired when it is processed
• 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 obtained by VD-> map + VD-> mbuf. offsets [VD-> frame ].
• Call munmap to unbind after collection is complete
• Munmap (vd-> map, VD-> mbuf. Size)

2. 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 information to be read/written; number of characters to be read/written );
• The return value is the number of characters actually read/write.
• Int Len;
• Unsigned char * vd-> map = (unsigned char *)
Malloc (VD à capability. maxwidth * vd à capability. maxheight );
• Len = read (VD à FD, VD à VD-> map,
• VD à capability. maxwidth * vd à capability. maxheight * 3 );

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.