Click here to Skip to main content
15,885,435 members
Articles / Desktop Programming / MFC
Article

CAviCap and CFrameGrabber - wrappers for AVICap Window

Rate me:
Please Sign up or sign in to vote.
4.86/5 (55 votes)
8 Dec 1999 627.1K   19K   168   126
AVICap wrappers to ease real-time video processing and single frame capture
  • Download demo project - 77 Kb
  • Download source files - 17 Kb

    Introduction

    AVICap window class provides applications with a message-based interface to access video and waveform-audio acquisition hardware and to control the process of streaming video capture to disk. Unfortunately, it has a few serious limitations:

    • All of setup operations for format, source and video standard are implemented within driver dialogs.
    • Program control is complicated and without of any warranties of true results.
    • Capture conditions are strongly related to WM_VISIBLE flag of AVICap window.
    • There is no capability of window stretching.
    • Most of capture drivers have internal buffers queue (fifo), so they can't provide satisfactory time accuracy when user application needs to grab a single frame. They simply return last frame from buffers queue.

    In short, AVICap window is really good enough to control the process of streaming video capture to disk, but in most cases useless for real-time video processing. For resolving this problem, a wrapper for AVICap windows in order to work with real-time video was written. This class was initially described on CodeGuru. After this, i've received a lot of questions about AviCap class implementation and usage. I've understood that for those programmers who are not so familiar with VfW tricks, CAviCap class is not quite understandable and hence is not so useful. I will try to answer most frequent questions about CAviCap implementation in this article. Also, a new wrapper for AVIcap window will be presented. This class has a very simple interface and intended for programmers who is just starting to play with VfW.

    Video Capture Software Components

    Avicap32.dll exports capCreateCaptureWindow function which is used for creating AVICap window. Because capture drivers are fundamentally 16-bit DLLs, Avicap32.dll does not call capture driver indirectly. It translates drivers call to 16-bit Avicap.dll & mmsystem.dll which deal with capture drivers. Windows 98 also includes a software layer that translates VfW 1.1 interfaces into WDM video capture interfaces. This translation software works only for video capture devices on external busses (USB and 1394) in Windows 98. See additional information about USB and WDM under Windows 98 at http://support.microsoft.com/support/kb/articles/Q192/1/12.ASP.

    One more way to capture gives DirectShow interface. It encapsulates standard AviCap window and also gives independent interface to WDM drivers for USB devices. See DirectShow SDK for more information.

    CAviCap class implementation.

    The CAviCap class provides the functionality of a AVICap window class, along with members for managing the window, excepting streaming operation. Internally, this class was implemented by usual for MFC way("dynamic subclass"). Most of public methods are helpers for WM_CAP_XXXX messaging, but a few methods give enhanced control for programmers. File "cavicap.h" contains all necessary functions descriptions.

    To use this class in your application:

    1. Create CAviCap class object.
    2. Call the Create member function to create an AVICap window. Note, if you will create invisible window, StartPreview method will not work, because AVICap disables control timer for hidden/minimized capture window. If you need invisible capture window, you must control the capture using GrabOneFrame member function.
    3. Call GetDriversList method to determine available drivers list.
    4. Call ConnectWithDriver method. According to m_DoQuickConnection flag, connections carries out with/without testing driver parameters. Usually, such testing can be very slow and also can "freeze" a driver not fully compatible with VfW 1.1. It seems to me, the best approach is to test driver once during software setup/installation.

      Note: Some drivers can splash Message box during testing. Also, the list of "public formats" from driver dialog can be appreciably different from list that you can get using EnumValidFrameSize method. This is a really strange fact, but, sometimes, it can be very useful.

    5. Set up required frame size and color resolution using SetFrameSize and SetBitResolution or with SetFormat methods.
    6. Install your own callback function using SetFrameCallBack if you need direct access to image data. Capture driver will call your function every time when new frame is captured in response to internal timer event or GrabOneFrame member function call. Driver passes a VIDEOHDR struct pointer to callback function. The struct member lpData point to image data buffer.
    7. Start preview using StartPreview for sequential capturing with interval defined by SetPreviewRate, or control the capture by just calling GrabOneFrame member function.

      See demo project "AviCapTest" for information about the usage of other methods.

    CFrameGrabber class implementation.

    This simple wrapper intended for single-frame capturing. Although, class does not support a lot of AVICap window functionality, such as file streaming, previewing, callback notification, it can be used for most application tasks.

    To use this class in your application:

    1. Create CFrameGrabber class object.
    2. Call the Create member function to create an AVICap window. By default, invisible child windows will be created, and connection with default capture driver will be established. Function returns "FALSE", if no capture driver found.
    3. If window is successfully created, object is ready to use.
    4. Call GetDIB member function to capture, get device-independent bitmap(DIB). It's possible to call GetImageBitsBuffer, which return image buffer pointer. In order to get a new image, call these functions after not less then 20 ms.
    5. You can determine current frame size and bits resolution with GetImageSize and GetImageBitsResolution member function.
    6. VideoFormatDialog and VideoSourceDialog allows to call standard driver dialogs.

    Demo programs

    Included are two demo programs in common workspace file. Projects were compiled and tested with MSVC++ 5.0 & 6.0. AviCapTest.exe uses CAviCap class for capturing and FrameGrabberTest.exe is based on FrameGrabber class. These demo programs connect with default capture driver installed on computer. Additionally, AviCapTest performs full testing of driver. Some of "standard" and "enhanced" commands of these classes are combined in menu. Both projects also include an examples of simple image processing.

  • License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    Written By
    Russian Federation Russian Federation
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    GeneralRe: Save as AVI file Pin
    svsatish_se24-Feb-06 3:23
    svsatish_se24-Feb-06 3:23 
    GeneralLittle question About DIB Pin
    Captnoord9-Feb-03 22:54
    Captnoord9-Feb-03 22:54 
    Generaltransmit video Pin
    ghassan19811-Feb-03 11:57
    ghassan19811-Feb-03 11:57 
    GeneralGrab specific frame using DirectX 9.0 Pin
    Anonymous20-Jan-03 12:17
    Anonymous20-Jan-03 12:17 
    Generalmulticam Pin
    itzahk14-Jan-03 20:41
    itzahk14-Jan-03 20:41 
    GeneralChanging VideoSource question Pin
    Anonymous6-Jan-03 13:30
    Anonymous6-Jan-03 13:30 
    Generalform WAV to EBU Pin
    nmanes26-Nov-02 2:31
    nmanes26-Nov-02 2:31 
    GeneralLower frame rates capturing video Pin
    paulgonc4-Nov-02 7:48
    paulgonc4-Nov-02 7:48 
    GeneralRe: Lower frame rates capturing video Pin
    Anonymous20-Nov-02 23:02
    Anonymous20-Nov-02 23:02 
    QuestionHow to handle 12 BITS Pin
    Ashok Jaiswal28-Oct-02 14:20
    Ashok Jaiswal28-Oct-02 14:20 
    AnswerRe: How to handle 12 BITS Pin
    nattu79-Feb-03 15:12
    nattu79-Feb-03 15:12 
    QuestionHow to handle 12 BITS Pin
    Jaiswal Ashok28-Oct-02 14:09
    Jaiswal Ashok28-Oct-02 14:09 
    QuestionHow to capturing frams one by one from a avi file !! Pin
    Shamal22-Oct-02 3:23
    Shamal22-Oct-02 3:23 
    GeneralVersion Visual Basic 6.0 Pin
    Maritzita16-Oct-02 11:30
    Maritzita16-Oct-02 11:30 
    GeneralRe: Version Visual Basic 6.0 Pin
    Anonymous25-Mar-03 8:55
    Anonymous25-Mar-03 8:55 
    Questionhow to fix the pixel dimension instead? Pin
    Anonymous4-Sep-02 4:01
    Anonymous4-Sep-02 4:01 
    QuestionHow can I set PAL-B as default for my capture driver ? Pin
    Hitesh Suhagiya31-Aug-02 20:44
    Hitesh Suhagiya31-Aug-02 20:44 
    QuestionHow to Reduce file size Pin
    sowmy25-Jul-02 1:56
    sowmy25-Jul-02 1:56 
    AnswerRe: How to Reduce file size Pin
    Harold28-Jul-02 12:14
    Harold28-Jul-02 12:14 
    GeneralRe: How to Reduce file size Pin
    hvibh12-Dec-02 19:14
    hvibh12-Dec-02 19:14 
    QuestionWhy AVICap window stops capturing? Pin
    Harold12-Jul-02 12:09
    Harold12-Jul-02 12:09 
    AnswerRe: Why AVICap window stops capturing? Pin
    sowmy25-Jul-02 1:24
    sowmy25-Jul-02 1:24 
    GeneralRe: Why AVICap window stops capturing? Pin
    Harold28-Jul-02 12:07
    Harold28-Jul-02 12:07 
    GeneralGot 2 errors whilst compiling in .NET Pin
    xces14-Jun-02 0:47
    xces14-Jun-02 0:47 
    GeneralRe: Got 2 errors whilst compiling in .NET Pin
    4-Jul-02 3:41
    suss4-Jul-02 3:41 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.