How to Implement video capture in Windows

Source: Internet
Author: User
For video capture in windows, Microsoft provides two SDK libraries: avicap and DirectShow. 1. Introduction to video for Windows

VFW is a software package for Digital Videos released by Microsoft in 1992. It allows applications to digitize and play video clips from traditional analog video sources. A key idea of VFW is that dedicated hardware is not required during playback. To solve the problem of large data volumes in digital videos, data must be compressed. It introduces a file standard named Avi, which does not specify how to capture, compress, and play a video. It only specifies how the video and audio should be stored on the hard disk, the AVI file is used to store video frames and audio data that match the frames. VFW provides programmers with advanced programming tools for. vbx and avicap window classes, enabling them to capture, play, and edit video clips by sending messages or setting properties. Now you don't have to install VFW. Windows95 itself includes video for windows1.1. When you install Windows, the installer automatically installs the components required to configure the video, such as device drivers and video compression programs.

VFW consists of the following six modules:

(1) avicap. dll: contains the video capture function, which provides an Advanced Interface for AVI file I/O and video and audio device drivers;

(2) msvideo. dll: a set of special drawdib functions are used to process video operations on the screen;

(3) mciavi. DRV: This driver includes an interpreter for the MCI command of VFW;

(4) avifile. dll: supports accessing the. AVI file by higher commands provided by standard multimedia I/O (mmio) functions;

(5) compression manager (ICM): manages the codec for video compression and decompression );

(6) audio compression manager ACM: Provides services similar to those of ICM. The difference is that ACM is suitable for Waveform audio.

Visual c ++ provides similar libraries in vfw32.lib, msacm32.lib, and winmm. lib to support VFW. In particular, it provides a powerful, simple, and mciwnd-like window avicap. Avicap provides a simple message-based interface for applications to access the video and waveform audio hardware, and control the video stream captured to the hard disk.

Ii. avicap programming Overview

Avicap supports real-time video stream capturing and single-frame capturing, and provides control over the video source. Although MCI also provides digital video services, such as display. the AVI file video provides the avivideo command set and overlay command set for video overlay. However, these commands are mainly file-based operations, it cannot meet the requirement of retrieving data directly from the video cache in real time. For a PC that uses a capture card without video overlay capabilities, the command set provided by MCI cannot capture video streams. Avicap has some advantages in video capturing. It can directly access the video buffer without generating intermediate files, which is highly real-time and efficient. It can also capture digital videos to files.

Before capturing a video, you must create a capture window. All the capture operations and settings are based on it. Windows created using the avicap window class (created using the capcreatecapturewindow function) are called "Capture windows". The window style is generally ws_child and ws_visible. In concept, the capture window is similar to standard control (such as buttons and list boxes ). The capture window has the following functions:

(1) capturing a video stream and audio stream into an AVI file;

(2) dynamically connect to or disconnect from the video and audio input devices;

(3) displays the input video stream in real time in overlay or preview mode;

(4) When capturing, you can specify the file name used and copy the content of the captured file to another file;

(5) set the capture rate;

(6) display a dialog box that controls the video source, video format, and video compression;

(7) Create, save, or load a color palette;

(8) copy the image and related color palette to the clipboard;

(9) Save a captured single-frame image as a file in Dib format.

Here we need to explain the two modes that avicap provides when displaying videos:

(A) Preview mode: This mode uses CPU resources. Video Frames are first transmitted from the captured hardware to the system memory, and then displayed in the capture window using the GDI function. Physically, this mode needs to be displayed on the monitor through vga.

(B) Overlay mode: This mode uses hardware overlay to display videos without passing through the vgacard, the overlay Video hardware combines the VGA output signal with its own output signal to form a combined signal that is displayed on the computer monitor. Only some video capture cards have video overlapping capabilities.

In addition to the nine functions of the capture window, the callback function provided by avicap can be flexibly written to meet some special requirements, for example, the macro capturesequencenofile can be used together with the callback function registered with capsetcallbackonvideostream to allow the application to directly use video and audio data. This can be used in the Video Conferencing Application to obtain video frames, the callback function transmits the captured image to a remote computer. The application can use a capture window to register the callback function (written by the user and called by the System) so that it can notify the application to respond in the following situations:

(1) The status of the capture window changes;

(2) error;

(3) Video Frame and audio cache can be used;

(4) Other applications are in the yield position during the capture process.

Like programming with common sdks, video capture programming also uses the structures, macros, messages, and functions that involve video capture. It makes it easy for programmers to call corresponding macros to complete all the functions that can be completed by sending avicap window messages. For example, if sendmessage (hwndcap, wm_cap_driver_connect, 0, 0l) and capdriverconnect (hwndcap, 0) act the same way, the created capture window is connected to the video input device.

When programming with avicap, you should be familiar with the structure related to video capture. Next, we will give a brief introduction to the four commonly used structures, the first three structures have corresponding functions to set and obtain the information contained in the structure:

(1) capstatus: defines the current status of the capture window, such as width and height;

(2) capdrivercaps: defines the capture drive capability, such as video overlay capability, video source control, and video format dialog box;

(3) captureparms: Contains parameters for controlling the video stream capture process, such as frame rate capture, specifying the keyboard or mouse key to terminate the capture, and the capture time limit;

(4) videohdr: defines the header information of the video data block. When writing a callback function, it often uses its data member lpdata (pointer to the data cache) and dwbufferlength (size of the data cache ).
3. avicap programming example

The following uses a simple application as an example to describe the use of avicap. This program displays and captures the input video stream in real time, and demonstrates the need for a video capture card and camera. Menu item 1 is shown in. The menu item display can display images in preview or overlay mode. The menu item setting can set the capture by displaying the video source, video format, and video display dialog boxes provided by avicap, the image in Figure 4 is set and displayed in preview mode according to the dialog box in figure 2 and Figure 3; menu item capture can capture video streams or single-frame images to a specified file.

Figure 1 menu items

Figure 2 video format dialog box

Figure 3 video source dialog box

Figure 4 figure 2 and Figure 3 show a frame

Due to space limitations, the following section only describes programming related to video capture.

1. Define global variables:

Code:

Hwnd ghwndcap; // capture window handle

Capdrivercaps gcapdrivercaps; // video drive capability

Capstatus gcapstatus; // The status of the capture window.2. process the wm_create message:

Code:

// 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)StatusCallbackProc); capSetCallbackOnFrame(ghWndCap, (FARPROC)FrameCallbackProc);

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:

Code:

// 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 drive4. process the menu item preview:

Code:

Cappreviewrate (ghwndcap, 66); // sets the display rate in preview mode.

Cappreview (ghwndcap, true); // start Preview mode5. Process menu item overlay:

Code:

If (gcapdrivercaps. fhasoverlay) // check whether the drive has the overlay capability

Capoverlay (ghwndcap, true); // enable overlay Mode6. Process menu item Exit:

Code:

  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:

Code:

  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 box8. process the video stream menu item, which captures the video stream to A. AVI file:

Code:

  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 sequence9. Process single frame menu items:

Code:

Capgrabframe (ghwndcap); // capture a single-frame image10. Define three callback functions:

Code:

  LRESULT CALLBACK StatusCallbackProc(HWND 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 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 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.

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.