VFW programming example

Source: Internet
Author: User

VFW (video for Windows) is a software development kit for Digital Videos released by Microsoft. The core of VFW is the AVI file standard. Audio and video data frames in AVI (Audio Video Interleave) files are staggered. Around AVI files, VFW provides a complete set of application APIs for video collection, compression, decompression, playback, and editing ). Because the AVI file format has been launched earlier and has been widely used in digital video technology, VFW still has great practical value and will be further developed.

Calling VFW in the VC ++ development environment is no different from using other development kits. You just need to add the vfw32.lib file to the project, however, other software and hardware settings are required when the video capture and compression management program is enabled. VFW provides rich processing functions and macro definitions for AVI files. An AVI file is a typical data stream file that consists of video streams, audio streams, and text streams. Therefore, the processing of AVI files is mainly to process file streams.

Programming example
To capture video frames, You need to enable the videostreamcallback function.
The following functions are used to capture a video stream or the current device status:
// Capture a video stream
Capsetcallbackonvideostream;
// Get a device Error
Capsetcallbackonerror;
// Get a device status
Capsetcallbackonstatus
} Custom function 1 // defines a frame capture callback function
Capsetcallbackonframe (ghcapwnd, longint (@ videostreamcallback ));
// Associate a capture window with a device drive. The second parameter is a serial number. When multiple explicit driver programs are installed in the system, the values are 0 to the total number respectively.
Capdriverconnect (ghcapwnd, 0 );
// Set the structure variable of the device property
Capparms. dwrequestmicrosecperframe: = 40000;
Capparms. flimitenabled: = false;
Capparms. fcaptureaudio: = false; // No audio
Capparms. fmcicontrol: = false;
Capparms. fyield: = true;
Capparms. vkeyabort: = vk_escape;
Capparms. fabortleftmouse: = false;
Capparms. fabortrightmouse: = false;
// Make the settings take effect
Capcapturesetsetup (ghcapwnd, longint (@ capparms), sizeof (tcaptureparms ));
// Set the preview Ratio
Cappreviewscale (ghcapwnd, 1 );
// Sets the frame frequency during preview.
Cappreviewrate (ghcapwnd, 66 );
// If you want to capture video streams, use the function to specify not to generate files. Otherwise, the AVI file is automatically generated.
Capcapturesequencenofile (ghcapwnd );
// Specify whether the overlay mode is used. The value is 1. Otherwise, the value is 0.
Capoverlay (ghcapwnd, 1 );
// Open Preview
Cappreview (ghcapwnd, 1 );
// Stop capture
Capcaptureabort (ghcapwnd );
// Disconnect the capture window from the drive
Capdriverdisconnect (ghcapwnd );

Custom function 2 // defines the callback function for captured frames:
Function framecallback (hwnd: hwnd; lpvhdr: longint): longint; stdcall;
VaR
Datapoint: ^ byte;
Diblen, rectwidth, rectheight: integer;
Begin
// Convert the pointer obtained from the callback function
Videostr: = lpvideohdr (lpvhdr );
// Get the returned data size
Diblen: = videostr ^. dwbufferlength;
Getmem (datapoint, 64000 );
// Copy the frame data to a memory. Note that the datapoint must allocate space first.
Copymemory (datapoint, videostr ^. lpdata, diblen );
// Some other processing
......
End;
Flexible use of the avicap window callback function can meet various needs, but pay attention to the video data format captured from the video card and the length and width of the image to refer to the video card parameters. In addition, some video cards support multiple formats and image lengths and widths through settings. Therefore, when restoring an image, pay attention to the parameters of the video card used.

Programming related to video capture.
1. Define global variables:
Hwnd ghwndcap; // capture window handle
Capdrivercaps gcapdrivercaps; // video drive capability

Capstatus gcapstatus; // The status of the capture window.
2. process the wm_create message:
// Create a capture window with hwnd as the main window handle
Ghwndcap = capcreatecapturewindow (lpstr) "Capture window", ws_child | ws_visible, 0, 0,300,240, (hwnd) hwnd, (INT) 0 );
// Register three callback functions, which should be affirmed in advance
Capsetcallbackonerror (ghwndcap, (farproc) errorcallbackproc); capsetcallbackonstatus (ghwndcap, (farproc) callback); capsetcallbackonframe (ghwndcap, (farproc) callback );

Capdriverconnect (ghwndcap, 0); // connect the capture window to the drive
// Obtain the drive capability and put the relevant information in the Structure Variable gcapdrivercaps.
Capdrivergetcaps (ghwndcap, & gcapdrivercaps, sizeof (capdrivercaps ));
3. process the wm_close message:
// Cancel the three registered callback Functions
Capsetcallbackonstatus (ghwndcap, null );
Capsetcallbackonerror (ghwndcap, null );
Capsetcallbackonframe (ghwndcap, null );
Capcaptureabort (ghwndcap); // stop capture
Capdriverdisconnect (ghwndcap); // disconnect the capture window from the drive

4. process the menu item preview:
Cappreviewrate (ghwndcap, 66); // sets the display rate in preview mode.
Cappreview (ghwndcap, true); // start Preview mode
5. Process menu item overlay:
If (gcapdrivercaps. fhasoverlay) // check whether the drive has the overlay capability
Capoverlay (ghwndcap, true); // enable overlay Mode
6. Process menu item Exit:
Sendmessage (hwnd, wm_close, wparam, lparam );
7. process three menu items under setting respectively. They can control the video source, video format, and display respectively:
If (gcapdrivercaps. fhasdlgvideosource)
Capdlgvideosource (ghwndcap); // video source dialog box
If (gapdrivercaps. fhasdlgvideoformat)
Capdlgvideoformat (ghwndcap); // video format dialog box
If (capdrivercaps. fhasdlgvideodisplay)
Capdlgvideodisplay (ghwndcap); // video display dialog box
8. process the video stream menu item, which captures the video stream to A. AVI file:
Char szcapturefile [] = "mycap. Avi ";
Capfilesetcapturefile (ghwndcap, szcapturefile); // specify the capture file name
Capfilealloc (ghwndcap, (1024l * 1024l * 5); // allocate storage space for captured files
Capcapturesequence (ghwndcap); // starts to capture the video sequence
9. Process single frame menu items:
Capgrabframe (ghwndcap); // capture a single-frame image
10. Define three callback functions:
Lresult callback statuscallbackproc (hwnd, int NID, lpstr lpstatustext)
{
If (! Ghwndcap) return false;
// Obtain the status of the capture window
Capgetstatus (ghwndcap, & gcapstatus, sizeof (capstatus ));
// Update the size of the capture window
Setwindowpos (ghwndcap, null, 0, 0, gcapstatus. uiimagewidth,
Gcapstatus. uiimageheight, swp_nozorder | swp_nomove );
If (nid = 0) {// clear the old status information
Setwindowtext (ghwndcap, (lpstr) gachappname );
Return (lresult) true;
}
// Display status ID and status text
Wsprintf (gachbuffer, "status # % d: % s", NID, lpstatustext );
Setwindowtext (ghwndcap, (lpstr) gachbuffer );
Return (lresult) true;
}
Lresult callback errorcallbackproc (hwnd, int nerrid, lpstr lperrortext)
{
If (! Ghwndcap)
Return false;
If (nerrid = 0)
Return true; // clear old errors
Wsprintf (gachbuffer, "Error # % d", nerrid); // display the error ID and text
MessageBox (hwnd, lperrortext, gachbuffer, mb_ OK | mb_iconexclamation );
Return (lresult) true;
}
Lresult callback framecallbackproc (hwnd, lpvideohdr lpvhdr)

{
If (! Ghwndcap)
Return false;
// Assume FP is an open. dat file pointer.
Fwrite (FP, lpvhdr-> lpdata, lpvhdr-> dwbufferlength, 1 );
Return (lresult) true;
}
It is worth noting that # include should be added to the. cpp file, and vfw32.lib should be added to link settings.
The preceding callback function framecallbackproc writes video data directly from the buffer to a file. You can also use the memcpy function to directly copy video data to another cache. Similarly, videostreamcallbackproc can be defined. Capsetcallbackonvideostream is a little more complex than capsetcallbackonframe. During the capture process, when a new video buffer is available, the system calls the callback function it registers. By default, the capture window does not allow other applications to continue running during the capture process. To cancel this restriction, you can set the captureparms member fyield to true or create a yield callback function. To solve potential reentry problems, you can use peekmessage in yieldcallbackproc to filter out some messages, such as mouse messages.

 

Frequently Used question code

Q: I used the code on the Internet to create a small video collection program. Now we need to implement the following functions:

The following functions are available:
/*************************************** ****************************************
Function: enablepreviewvideo
Arguments: parent (input)-parent window that will display video.
X (input)-x location in parent where video will be shown.
Y (input)-Y location in parent where video will be shown.
Width (input)-width of preview window.
Height (input)-height of preview window.
Previewrate (input)-rate of preview in FPS.
Return: true success, false failed.
Description: enables Preview Video mode.
**************************************** ***************************************/
Bool cvfwimageprocessor: enablepreviewvideo (hwnd parent, int X, int y, int width, int height, int previewrate)
{
// Reset any error conditions.
Getpreviuserror (null, null, true );

Setparent (m_hwndvideo, parent );
Setwindowlong (m_hwndvideo, gwl_style, ws_child );

Setwindowpos (m_hwndvideo, null, x, y,
Width,
Height,
Swp_nozorder );
Showwindow (m_hwndvideo, sw_show );
Cappreviewrate (m_hwndvideo, previewrate );

Return cappreview (m_hwndvideo, true );
}

Now you have obtained the crect rectvideo size in the preview window, but the video is displayed in the same size as the video format setting. You can only display a part of the video in the upper-left corner of the window, without scaling the video, so the display is incomplete. Now I want to zoom in and preview the video to the set area, but do not change the actual video size, because the actual size of the captured image is still required.

A: automatically scales the video to the video window size:
Add cappreviewscale (hwnd, true) to your cappreviewrate (...).

Appendix:
Bool cappreviewscale (hwnd, F );

Parameters
Hwnd
Handle of a capture window.
F
Preview scaling flag. Specify true for this parameter to stretch preview frames to the size of the capture window or false to display them at their natural size.
Return values
Returns true if successful or false otherwise.

Remarks
Scaling preview images controls the immediate presentation of captured frames within the capture window. It has no effect on the size of the frames saved to file.

Scaling has no effect when using overlay to display video in the frame buffer.


In addition:
Set the image capture size:
Hwnd m_capwnd = capcreatecapturewindow (.......);
Bitmapinfo m_bmp Info;
Capgetvideoformat (m_capwnd, & m_bmp info, sizeof (m_bmp info ));

M_bmp info.bmiheader.biwidth and m_bmp info.bmiheader.biheight are the width and height of the video you want.

Set the size:
M_bmp info.bmiheader.biwidth = 176; // 320
M_bmp info.bmiheader.biheight = 144; // 240
Bool ret = capsetvideoformat (m_capwnd, & m_bmp info, sizeof (m_bmp info ));

 

 

 

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.