Video Capture programming (Liu Tao Guo Ge Yang Yusen)

Source: Internet
Author: User
In multimedia applications such as desktop video conferencing and videophone, obtaining digital videos is a key prerequisite. Before video for Windows (VFW) appeared, it was extremely complicated to capture digital videos. Microsoft Visual C ++ has been supporting video for Windows (VFW) Since version 4.0, which makes video capture programming very convenient. For multimedia application development, the most popular materials on the market are: mCi (media control interface ), this article focuses on how to use the avicap window class provided by Visual C ++ to capture videos, as well as the concepts and key issues involved.

1. Introduction to video for Windows

VFW is a software package for digital video released by Microsoft in 1992. It enables applicationsProgramDigitize and play video clips from traditional simulated 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:

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.

Iv. Conclusion

The avicap window class provided by Visual C ++ provides great convenience for capturing digital video streams and related operations. The callback function can be flexibly written to meet the needs of real-time video transmission, for example, an application can directly obtain a digital video from the buffer and compress and encode it before transmitting it to a remote computer in real time. The author uses avicap for video capture in the videophone system on the telephone network. This method can also be used in other multimedia conferencing systems, such as ISDN and LAN conferencing systems. (Nanjing Institute of Posts and Telecommunications 70 # mailbox Liu Tao Guo Ge Yang Yusen 210003)

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.