CFrameGrabber – Wrapper for AVICap Window

Environment: Windows 98, Visual C++ 6

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
    videostandard 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

Here is 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 CFrameGrabber
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.

Downloads

Download demo project – 77 Kb
Download source – 17 Kb

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read