Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
96 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

Contents
 Introduction
 Files
 Streams
 Processing
 Cleaning Up

 Printable version
 Discuss this article
 in the forums


Reference

Project Settings

You’ll need to link to winmm.lib, and vfw32.lib to use the AVIFile functions. They should be included in Borland as well. They’re included with MSVC++ 6.0, but should also be available for versions as low as 4.0. Newer versions of the libraries are also part of the latest Platform SDK release from Microsoft.

You can use these functions with any kind of project: C, C++, MFC, Win32, console app, Direct Draw, Direct3D, etc.

Initialization

AviFileInit();

Opening the AVI File

The AVIFileOpen function only takes a string for the filename, as opposed to a file handle. This means that you’ll be unable to embed an AVI file into a proprietary WAD format and load it directly from within while using this API, unless you can figure out a trick. It’s possible to encode the file format and/or change the extension to protect your copyright for a game’s release.

PAVIFILE pAviFile; if(AVIFileOpen(&pAviFile, “filename.avi”, OF_READ, NULL)) // error

Getting the File’s Info

AVIFILEINFO info; AVIFileInfo(pAviFile, &info, sizeof(info));

The info structure contains some extra stuff you might use later on, but nothing spectacular or essential for our purpose so I won’t go into that here.

Finding Audio and Video Streams

An AVI file may have any number of streams of any type. Usually they’re just audio and video streams. It’s possible to open all of the streams and then query what type they are later. Usually a program ignores streams it doesn’t recognize or need. This allows you to innovate the AVI file format, while retaining compatibility with other programs.

I’ll use preallocated arrays to contain only the audio and video streams in the file. Ordinarily I would recommend a linked list, but such implementation details are out of this document’s context, not to mention most AVI files will have only one audio and video stream anyway.

PAVISTREAM pAudio[MAX_AUDIO_STREAMS], pVideo[MAX_VIDEO_STREAMS]; int nNumAudioStreams=0, nNumVideoStreams=0;

The loops to open each stream are pretty strait forward. I explicitly specify what type of stream to load, either streamtypeAUDIO or streamtypeVIDEO. I ignore any other stream that might be in the file, like streamtypeTEXT or streamtypeMIDI. To load any stream type available, specify zero for the streamtype.

do { if(AVIFileGetStream(pAviFile, &pAudio[nNumAudioStreams], streamtypeAUDIO, nNumAudioStreams)) break; } while(++nNumAudioStreams < MAX_AUDIO_STREAMS); do { if(AVIFileGetStream(pAviFile, &pVideo[nNumVideoStreams], streamtypeVIDEO, nNumVideoStreams)) break; } while(++nNumVideoStreams < MAX_VIDEO_STREAMS);

Now we have neat arrays of audio and video streams, and we know the number contained in each. Processing them will consist of looping through these streams, so here forward I simply refer to the current stream as pStream. Note that we haven’t actually loaded anything yet, we’ve merely obtained a handle to the data that’s in the file. This allows us to play potentially massive AVI files without a significant memory impact.



Next : Streams