VFW Video Collection
First, VFW overview
VFW is a software package for digital video launched by Microsoft in 1992 that enables applications to digitize video clips from traditional analog video sources via digital devices. VFW Video main idea is that in the process of playback does not require dedicated hardware, in order to solve the problem of large data volume, data need to be compressed. It introduces a file format called AVI, in which the video is not required to capture, compress and play, only specify how the video and audio is stored on the hard disk, as well as in the AVI file to alternately store video frames and matching audio data. VFW enables programmers to capture, play, and edit video clips by sending messages or setting properties. In Windows 9x systems, when users install VFW, the installer automatically installs the components needed to configure the video, such as device drivers, video compression programs, and so on.
VFW is mainly composed of the following 6 modules:
AVICap. DLL: Contains a function to perform video capture, which provides an advanced interface for I/O processing of AVI files and video and audio device drivers;
Msvideo. DLL: Contains a special set of DRAWDIB functions to handle the video operation on the screen;
Mciavi. DLL: Includes a driver for the MCI command interpreter for VFW;
Avifile. DLL: Contains higher commands provided by standard multimedia I/O functions for access. AVI file;
Compression Manager (ICM): Compiler decoder (CODEC) for managing video compression/decompression;
Audio Compression Manager ACM: provides services similar to ICM for waveform audio;
Ii. Development Steps
The AVICap window class supports real-time video stream capture and single frame capture, and provides control over the video source. Although MCI also provides a digital video service that provides a overlay command set for video overlays, these commands are primarily file-based operations that do not meet the need to read data from the video cache in real time, for a PC that uses a capture card without video overlay capability, The set of commands provided with MCI is not able to capture the video stream. While the AVICap window class has some advantages in capturing video, it can directly access the video buffer, it does not need to generate intermediate files, real-time is very strong, high efficiency. And it can be said that digital video capture into a file.
1. Create a "Capture window"
Before video capture, you need to create a "capture window" and use it as the basis for all capture and parameter setting operations. The Capture window is created by the "Capcreatecapturewindow" function of the AVICap window class, which typically has a window style of ws_child and ws_visible.
The capture window is similar to a standard space and has the following features:
Capturing the video stream and audio stream into an AVI file;
Dynamic connection or disconnection with the video and audio input;
Real-time display of the input video in overlay or preview mode;
When capturing, you can specify the file name used and copy the contents of the captured file to another file;
Set the capture rate;
Display control video source, video format, compression format dialog box;
Create, save, or load a color palette;
Copy the image and the associated palette to the Clipboard;
Saves the captured single-frame image as a file in DIB format.
2. Associating a capture window with a driver
A separately defined capture window is not working and needs to be associated with a device in order to obtain a video signal. Use the function Capdriverconnect to associate a capture window with a driver for a device.
3. Set the properties of the video device
By setting the individual member variables of the captureparams struct variable, you can control the device sampling frequency, interrupt sampling keys, state behavior, and so on. After you set the CAPTUREPARAMS structure variable, you can use the function Capcapturesetsetup setting to make it effective. You can then use Cappreviewscale, cappreviewrate to set the preview scale and number of frames, or you can work with the device's default values.
4. Open the preview
Use the function Capoverlay to choose whether to preview in overlay mode, which takes up less system resources and shows the video speed quickly. Then use Cappreview to start the preview function, then you can see the image captured by the camera on the screen.
A basic video capture program can be established with the above four steps. However, if you want to handle the video data captured from the device yourself, you will need to use the Capture window callback function, such as capturing video data in one frame or capturing the video data in a streaming way, etc.
III. four structures in the VFW video collection
1) Capstatus: Defines the current state of the capture window;
Uiimagewidth: Image width;
Uiimageheight: Image height;
Flivewindow: The active window tag, which is true if the window is displaying the picture in preview mode;
Foverlaywindow: Overlay window flag bit, if the overlay window is being used, then the change is true;
Fscale: The scale flag bit, if the input video is scaled to the size of the client area during the preview, the value is true. When a hardware overlay is used, the change of the bit is not valid.
Ptscall: The offset of the x and Y coordinates of the pixel that is displayed in the upper-left corner of the window client area.
Fusingdefaultpalette: Default palette flag bit, if the capture window is using the current default palette, the value is true.
Faudiohardware: Audio hardware flag bit, this value is true if the system has audio hardware installed.
Fcapfileexists: Capture file flag bit, if a capture file has been marked, the value bit is true.
Dwcurrentvideoframe: The number of frames processed, including the number of dropped frames, during current or recent stream capture.
Dwcurrentvideoframedropped: The number of frames dropped during the current stream capture.
DWCURRENTTIMEELAPSEDMS: The time, in milliseconds, that the program takes to start the calculation from the current stream capture.
Hpalcurrent: The handle of the current clipboard.
Fcapturingnow: Captures the identity bit, and the bit is true when the capture is in progress.
Dwreturn: Error return value, which can be changed when the application does not support the error callback function.
wnumvideoallocated: Number of video caches allocated.
wnumaudioallocated: Number of audio caches allocated.
2) Capdrivercaps: Defines the ability to capture the drive, such as incorrect video overlay capability, control video source, Video Format dialog box, etc.
Wdeviceindex: Capture the index value of the drive, change the value can be changed in 0-9.
Fhasoverly: Video overlay flag bit, if the video supports video overlay, this value is true.
Fhasdlgvideosource: Video Resource dialog Flag bit, if the device supports video selection, Control dialog box, the value is true.
Fhasdlgvideoformat: The video Format dialog flag bit, if the device supports the selection of the Video Format dialog box, the value is true.
Fhasdlgvideodisplay: The video display dialog flag bit, if the device supports replay of the video capture buffer, the bit is true.
Fcaptureinitialized: Captures the installation flag bit, if the capture drive has been successfully connected, the value is true.
Fdriversuppliespalette: Drive palette flag bit, if the drive can create a color palette, the value is true.
3) Captureparams: Contains parameters that control the video stream capture process, such as capturing frame rate, specifying keyboard or mouse keys to stop capturing, capturing time limits, and so on.
Dwrequestmicrosecperframe: The desired frame playback rate, in milliseconds, the default is 66667, which is equivalent to 15 frames per second.
Fmakeusehitoktocapture: Starts capturing the flag bit and, if true, produces an inquiry dialog before starting the capture, which defaults to false.
Wpercentdropforerror: The maximum allowable drop frame percentage can vary from 0 to 100, with the default value of 10.
Fyield:; another thread flag bit, if true, then the program restarts a thread for capturing the video stream, the default value is False. However, if you set it to true, you need to handle some potential actions in your program because the current video capture does not mask other actions.
Dwindexsize: The maximum number of index entries allowed in an AVI file.
The logical size of the Wchunkgraunlarity:avi file, in bytes. If the value is 0, it means that the dimension is increasing and useless in the Win32 program.
Wnumvideorequested: The maximum number of video caches that are allowed to be allocated.
Fcaptureaudio: The audio flag bit, if the audio stream is being captured, the value is true.
Wnumaudiorequested: The maximum number of audio caches, the default value is 10.
Vkeyabort: The virtual keyboard code that terminates the stream capture, the default value is Vk_escape.
Fabortleftmouse: Terminates the left mouse button flag bit, if the value is true, the capture terminates if the left mouse button is clicked during the stream capture, the default value is true.
Fabortrightmouse: Terminates the right mouse button flag bit.
Flimitenabled: Capture the operation time limit, if the value is true, then the time to capture the end, the default value is False.
Wtimelimit: The specific end time, only if the flimitenabled is true, the bit is valid.
FMCICONTROL:MCI device Flag bit.
Wstepcaptureaverageframes: When an image frame is created based on average sampling, the sample time of the frame is typically 5.
Dwaudiobuffersize: The size of the audio cache, if the default value is 0, the cache size is up to 0.5 seconds, or 10k bytes.
Avstreammaster: Audio and video synchronization flag.
4) Videohar: Defines the header information of the video data block, and in writing the callback function is often used for its data member lpdata (pointer to the data cache) and Dwbufferlength (the size of the data cache).
Application of four or three kinds of structural bodies
Capdrivergetcaps: Gets the status information of the drive.
Capdrivergetcaps (m_hcapwnd,&capdrivers,sizeof (capdrivercaps));
Capcapturegetsetup (m_hcapwnd,&capparams,sizeof (captureparams)) to get the status information of Captureparams.
Capcapturesetsetup (m_hcapwnd,&capparams,sizeof (Captureparams)), the newly established state information is written to the struct, and only the new state will take effect at this time.
Capgetstatus: Get Capstatus's information.
Capgetstatus (hwnd,&capstatus,sizeof (capstatus));
V. Creation of the video window
First, create a parent window handle that must be set to the member variable of the class, because the creator of the acquisition window uses that variable, and if it is set to a local variable to be destroyed outside of this function, the collection window cannot find the parent window.
M_hcapwnd = Capcreatecapturewindow (_t ("video capture"), Ws_child | ws_visible | Ws_sysmenu | ws_caption,150,150,375,270,hwndmain,0);
To create a capture window, note that the handle M_hcapwnd is best for a global variable, because the callback function will use it later, and the callback function can only be a global function. Note Ws_sysmenu can have the capture window with a close button, the state can only be added to the Capture window, and the type of the parent window will not be reflected.
Define global variables: Global variables can no longer be defined in the header file, because header files are easily caused by errors when they are contained by multiple source programs. Global variables can be defined anywhere in the source program, and can be used anywhere at the beginning of the source program.
1) can be defined in a separate H file, but only one class contains it, otherwise it will be an error.
2) Define this variable in a program that uses this variable.
3) If other programs also need to use this variable, just use the extern declaration before using it.
Functions that are defined outside the class are global functions. Global functions are defined in a CPP, and the Declaration of function prototypes in H! To use this global function for other files, just declare it before using extern. The global function does not have to be declared in the header file, other files need to use this global function, just before using the extern declaration.
Vi. Connecting to hardware Drivers
Through the function capgetdriverdescription can get the local machine multi-connection of all the video read device information. As follows:
Char achdeviceversion[80];//device version information
Char achdeviceandversion[160];//device name and version information
int uIndex;
int drivercount = 0;
for (uIndex = 0; UIndex < 9; uindex++)
{
if (Capgetdriverdescription (UIndex, (LPSTR) achdeviceandversion,sizeof (achdeviceandversion), (LPSTR) Achdeviceversion,sizeof (achdeviceversion)))
{
Strcat (Achdeviceandversion, ",");
Strcat (achdeviceandversion,achdeviceversion);
drivercount++;
}
}
You can get a description of the connected device and have drivercount determine the number of capture devices with a maximum value of 9.
Once you have identified the available video capture devices, you can use Capdriverconnect (m_hcapwnd,0) to connect the No. 0 video device, and 0 represents the driver interface for the specific video device.
Capgetdriverdescription takes priority in obtaining driver information for hot-swappable video devices, such as USB drive devices.
You can use the Capgetvideoformatsize function to achieve the format of getting and setting video:
DWORD dwsize = capgetvideoformatsize (M_hcapwnd);
Lpbitmapinfo LPBI = (lpbitmapinfo) HeapAlloc (GetProcessHeap (), heap_zero_memmory| heap_no_serialize,dwsize);//Because the LPBITMAPINFO structure has a variable length, the application needs to get the size of the struct and then allocate memory.
Capgetvideoformat (m_hcapwnd,lpbi,dwsize);
...//Set the parameters of the video format;
Capsetvideoformat (m_hcapwnd,lpbi,dwsize);
The frames that are intercepted by the driver are in the format of the above settings.
Set the preview format of the video via Capdrivergetcaps, Capdrivergetcaps (m_hcapwnd,&capdrivers,sizeof (capdrivercaps));
if (capdrivers.fhasoverlay)//Driver support overlapping format, choose overlapping format, efficiency faster than the preview mode is a bit higher.
{
Capoverlay (m_hcapwnd,true);
}
Else
{
Cappreviewscale (m_hcapwnd,true);
Cappreviewrate (m_hcapwnd,20);
Cappreview (m_hcapwnd,true);
}
Seven, callback function
Back to function is one of the most useful programming mechanisms to date, in Windows, callback functions are required for window procedures, hook procedures, asynchronous procedure calls, and callback methods are used throughout the process. People can register callback methods to get load/unload notifications, unhandled exception notifications, database/window status modification notifications, file system modification Notifications, menu options, completed asynchronous operation notifications, and more. In the VFW, there are several macro functions, such as a macro function for setting a callback function that can react after an event occurs, which is similar to the interrupt service mechanism, which is automatically entered into the corresponding callback function when the condition is met, and what functions the callback function needs to implement, which can be programmed by the developer with its own parameters. Using VFW to obtain real-time video data can usually use the callback mechanism of video processing to obtain the first address and length of the real-time data buffer and to process the image data, and also can transmit the video data directly.
The callback function must be a public function, and no member function can be bit. In MFC, the callback function is defined as a public function, that is, the function body is defined directly in the. cpp file and is not defined as a member function of a class. In the program that intends to invoke the callback function, only the body of the callback function needs to be declared, and the callback function can be registered.
Declaring and registering callback functions:
LRESULT PASCAL Framecallbackproc (HWND,LPVIDEOHDR);
Capsetcallbackonframe (M_HCAPWND,FRAMECALLBACKPROC);
To define a callback function:
LRESULT CALLBACK Framecallbackproc (Hwndhwnd,lpvideohdr lpvhdr)
Lpvhdr points to the data structure Videoheadera, which is the image of each frame, RGB format, regardless of whether the compression format is set, in this function is always not compressed raw BMP data, so add BMP file header to write files.
Lpvhdr->lpdata is stored in the bit image elements information, you must include the header file and pixel information to form a group of images
{
if (...)
{return FALSE;}
Else
Return (LRESULT) TRUE;
Note that the callback function must have a return value.
Callback functions are an important part of the VFW, and many important functions are implemented using callback functions.
Status callback: BOOL capsetcallbackonstatus (HWND,FPPROC);
Set state callback function: LRESULT PASCAL statuscallbackproc (hwndhwnd,int nid,lpstr lpstatustext);
HWND: Window handle;
NID: Status code, a status code that corresponds to a specific state change.
Lpstatustext: Text information related to the status.
Error callback: BOOL Capsetcallbackonerror (hwnd, FPPROC);
Set Error callback function: LRESULT CALLBACK caperrorcallback (hwndhwnd, int nID, LPCSTR lpsz);
The meaning of the parameter is the same as above.
Video stream back and frame callback (also the most important part of video interception):
Frame callback: BOOL Capsetcallbackonframe (hwnd, FPPROC);
Set Frame callback function: LRESULT CALLBACK capframecallback (HWND hwnd,lpvideohdr LPVHDR);
HWND: The handle value of the Intercept window;
LPVHDR: Only concerns members lpdata and Dwbufferlength. Lpvhdr->lpdata is a pointer to a video buffer within the process space, and lpvhdr->dwbufferlength is the length of the buffer, but the actual length needs to be obtained through strlen (Lpvhdr->lpdata). Lpvhdr->lpdata points to the data only to make the actual data of the image, excluding the bitmapinfo structure. If necessary, you also need to obtain the current drive captured video format Capgetvideoformat () to get the BITMAPINFO structure, and then combine the bitmapinfo and lpvhdr->lpdata data into a bitmap compressed and sent.
The Capframecallback function is called only in preview mode, before the video is transferred from the buffer to the window display. There are two conditions: 1 video frames are coming out of memory buffers (not directly from the drive buffers). In overlapping mode, the video does not pass through the buffer in the process space, but is directly sent to the window display by the drive cache, which improves the efficiency. )。 2 frames to be sent to the window to display.
Several video interception functions: Capcapturesingleframe (), Capgrabframe (), Capgrabframenostop ().
Capcapturesingleframe (): Extract a single frame image, the process is to extract the video frame data from the video driver cache, then into the memory cache, and then sent from the memory cache to the window display.
Capgrabframe (): Works in the same way as Capcapturesingleframe (), except that this function disables overlapping and preview modes when the error is changed.
Capgrabframenostop (): Unlike Capgrabframe (), it will open up a new thread to complete the function of the Capgrabframe () function. Other operations can be performed during the acquisition process.
Video Stream callback: BOOL Capsetcallbackonvideostream (hwnd, FPPROC);
Set the video stream callback function: LRESULT CALLBACK capvideostreamcallback (HWND hwnd,lpvideohdr LPVHDR);
This callback function is called when the process video buffer is available (should be full).
Other related functions: Capcapturesequence (), Capcapturesequencenofile ()
Capcapturesequence () streams the video stream from the process buffer to an AVI file (specified by Capfilesetcapturefile).
The function of Capcapturesequencenofile () is to make the memory buffer full by constantly intercepting the video so that Capvideostreamcallback is constantly being called. This function does not save the video stream to a file.
Viii. threading issues in video capture and storage
Using the function capcapturesequence (M_hcapwnd) to save the captured video stream as an image, the system will default a capture file C:\NEWFILE on the C drive. AVI, you can also customize the capture file by Capfilesetcapturefile (M_hcapwnd, filename). During the capture process, the capture process is a bit of a card due to shielding other operations, which can be solved in two ways at this time; 1 own another thread acquisition, 2 set Fyield in Captureparams to true. Because of another thread for image acquisition, it is necessary to handle some keyboard operations, such as when the Fabortleftmouse is true, when the left mouse button is clicked, the collection will stop.
Ix. save captured frames as pictures
Several image-preserving functions: Capcapturesingleframe (), Capgrabframe (), Capgrabframenostop ().
Capcapturesingleframe (): Extract a single frame image, the process is to extract the video frame data from the video driver cache, then into the memory cache, and then sent from the memory cache to the window display.
Capgrabframe (): Works in the same way as Capcapturesingleframe (), except that this function disables overlapping and preview modes when the error is changed.
Capgrabframenostop (): Unlike Capgrabframe (), it will open up a new thread to complete the function of the Capgrabframe () function. Other operations can be performed during the acquisition process.
Capfilesavedib (m_hcapwnd, filename) can save the contents of the video buffer as a file.
It is not easy to save the information in the callback function Capframecallback (HWND hwnd, LPVIDEOHDR LPVHDR) as a picture, because only the pixel information of the video is stored in Lpvhdr->lpdata. This information must be added to Bitmapfileheader and bitmapinfo two headers before it can be a real picture message.
For example:
Variable values used to obtain the information header file:
Bitmapinfo BihI;
DWORD dwsize;
dwsize = Capgetvideoformatsize (M_hcapwnd);
Capgetvideoformat (M_hcapwnd, &BIHL, dwsize);
Used to assign values to the individual variables of the file header;
Bitmapfileheader BFH;
Bfh.bftype = 0x4d42;
bfh.bfsize = sizeof (bitmapfileheader) +bihl.bmiheader.bisize + bihl.bmiHeader.biSizeImage;
bfh.bfreserved1 = Bfh.bfreserved2 = 0;
Bfh.bfoffbits = (DWORD) sizeof (Bitmapfileheader) +bihl.bmiheader.bisize;
The specific process for video capture development using VFW can be found in MSDN: Using video capture under video capture. As shown in the following:
VFW Collection Video