AVI Creation

Creating an AVI file is a three step process. The first step is calling fg_avimake() to open a new AVI file and specify its resolution, frame rate, and compression details. If fg_avimake() is successful, you next call fg_aviframe() to write each AVI frame, passing it an uncompressed 256-color or true color bitmap containing that frame's image data. Finally, use fg_avidone() to close the AVI file, just as you do when playing an AVI. We'll now describe these steps in more detail.

The fg_avimake() function's first two parameters specify the AVI file name and context descriptor array, just like fg_aviopen(). For fg_avimake(), however, the context descriptor only needs to be 24 bytes, not 48. The third fg_avimake() parameter is the four-character code (FourCC) for the codec that will be used to compress the video data. It can instead be zero for uncompressed frames, or -1 to display a dialog box to select compression options at run time. Here are the FourCC values for some of the more popular codecs supplied with Video for Windows:

Codec name

FourCC

Hex equivalent

Intel Indeo 3.2

IV32

49563332

Microsoft Video 1

MSVC

4D535643

Microsoft Run Length Encoding

MRLE

4D524C45

Radius Cinepak

CVID

43564944

The fourth and fifth fg_avimake() parameters specify the image width and height in pixels. The sixth parameter is the suggested AVI color depth in bits per pixel; fg_avimake() will use this color depth unless the selected codec does not support it. The next parameter defines the compression quality, between 0 and 10,000. A value of 10,000 indicates lossless compression, with lower values resulting in increased degrees of lossy compression. The quality value is ignored if creating an uncompressed AVI, or if we select compression options through the dialog box. The final fg_avimake() parameter is the video rate in frames per second. The fg_avimake() function returns 0 if the AVI file was created successfully, -1 if there was an error creating the AVI file, -2 if there was an error creating the AVI video stream, or -3 if the user pressed Cancel on the compression options dialog box.

The code shown here creates an empty 320x240 AVI file called INTRO.AVI, compressed with the MSVC codec:

C/C++:

BYTE ContextAVI[24];
fg_avimake("INTRO.AVI",ContextAVI,0x4D535643,320,240,8,10000,30);

Delphi:

ContextAVI : array [1..24] of byte;
fg_avimake('INTRO.AVI'+chr(0),ContextAVI,$4D535643,320,240,8,
   10000,30);

Visual Basic:

Dim ContextAVI(24) As Byte
Call fg_avimake("INTRO.AVI", ContextAVI(0), &H4D535643, 320, 240,
   8, 10000, 30)

We specify the FourCC for the MSVC codec as 4D535643 hex. Because this codec supports 256-color compression and we specify a color depth of 8 bits per pixel, INTRO.AVI will be a 256-color AVI file. We also specify lossless compression with a quality value of 10000, and a playback rate of 30 frames per second. In a real program, we would probably want to check the fg_avimake() return value to make sure the file was created successfully.

Once we've created an empty AVI file with fg_avimake(), we're ready to write each frame of image data. To do this, we use fg_aviframe(), which writes a single frame to an AVI file. Its first parameter is the 24-byte context descriptor returned by fg_avimake(), and its second parameter is a bitmap containing the uncompressed image data for that frame. The bitmap width and height must be the same as specified in the fg_avimake() call for this context descriptor. If creating a 256-color AVI, the bitmap must be a 256-color bitmap. If creating a high color or true color AVI, it must be a 24-bit direct color bitmap. The fg_aviframe() function returns 0 if the frame was written successfully, and -1 if not.

<< Prev

Next >>

Contents
Fastgraph Home Page

 

copyright 2001 Ted Gruber Software, Inc.