Video4linux Chinese parsing favorites
• 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.
• Camera under/dev/video0
1. Enable the video device:
2. Read Device Information
3. Change the current device settings (if necessary)
4. Two Methods for video collection: (failed) L
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;
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, and minheight
• Channels // Number of signal sources
• Type // capture, color, black/white, and cropping. Values such as vid_type_capture
2 video_picture
Various attributes of images collected by 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.
X // X coordinates in windows.
Y // coordinates in ywindows.
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
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
6. video_buffer bottom layer buffer description
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.
7. video_mmap // used for MMAP
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", VD à FD );
Close the video device ("/dev/video0", VD à FD );
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
• 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;
}
}
• The first method for capturing images: Using MMAP (memory ing)
• MMAP () system calls allow processes to implement shared 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.
• 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 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 ing to the number of bytes of the call process address space, which starts from the offset byte at the beginning of the mapped file
• Prot specifies the access permissions 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 for 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;
}
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;
• One frame can be acquired when it is processed
• Int frame; // The frame currently collected
• Int framestat [2]; // The collection is not started, waiting for the collection to end.
• 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)
• 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;
}