AVI Files
AVI Files
AVI Files
The Microsoft Audio/Video Interleaved (AVI) file format is a RIFF file specification used
with applications that capture, edit, and playback audio/video sequences. In general, AVI
files contain multiple streams of different types of data. Most AVI sequences will use both
audio and video streams. A simple variation for an AVI sequence uses video data and
does not require an audio stream. Specialized AVI sequences might include a control track
or MIDI track as an additional data stream. The control track could control external
devices such as an MCI videodisc player. The MIDI track could play background music
for the sequence. While a specialized sequence requires a specialized control program to
take advantage of all its capabilities, applications that can read and play AVI sequences
can still read and play an AVI sequence in a specialized file. (These applications ignore
the non-AVI data in the specialized file.) This chapter primarily describes AVI files
containing only audio and video data.
This chapter covers the following topics:
The required chunks of an AVI file
The optional chunks of an AVI file
Developing routines to write AVI files
For additional information about RIFF files, see the Microsoft Windows Multimedia
Programmer’s Guide and Microsoft Windows Multimedia Programmer’s Reference.
For additional information about installable compressors and decompressors, see chapter
10, “Video Compression and Decompression Drivers.”
The LIST chunks and the index chunk are subchunks of the RIFF “AVI ” chunk. The
“AVI ” chunk identifies the file as an AVI RIFF file. The LIST “hdrl” chunk defines the
format of the data and is the first required list chunk. The LIST “movi” chunk contains the
data for the AVI sequence and is the second required list chunk. The “idx1” chunk is the
optional index chunk. AVI files must keep these three components in the proper sequence.
The LIST “hdrl” and LIST “movi” chunks use subchunks for their data. The following
example shows the AVI RIFF form expanded with the chunks needed to complete the
LIST “hdrl” and LIST “movi” chunks:
RIFF ('AVI '
LIST ('hdrl'
'avih'(<Main AVI Header>)
LIST ('strl'
'strh'(<Stream header>)
'strf'(<Stream format>)
'strd'(additional header data)
.
.
.
)
.
.
.
)
02/10/93
AVI Files 錯誤! 僅限主文件。-3
LIST ('movi'
{SubChunk | LIST('rec '
SubChunk1
SubChunk2
.
.
.
)
.
.
.
}
.
.
.
)
['idx1'<AVIIndex>]
)
The following sections describe the chunks contained in the LIST “hdrl” and LIST “movi”
chunks as well as the “idx1” chunk.
02/10/93
錯誤! 僅限主文件。-4 Video for Windows Programmer's Guide
typedef struct {
DWORD dwMicroSecPerFrame;
DWORD dwMaxBytesPerSec;
DWORD dwReserved1;
DWORD dwFlags;
DWORD dwTotalFrames;
DWORD dwInitialFrames;
DWORD dwStreams;
DWORD dwSuggestedBufferSize;
DWORD dwWidth;
DWORD dwHeight;
DWORD dwScale;
DWORD dwRate;
DWORD dwStart;
DWORD dwLength;
} MainAVIHeader;
The dwMicroSecPerFrame field specifies the period between video frames. This value
indicates the overall timing for the file.
The dwMaxBytesPerSec field specifies the approximate maximum data rate of the file.
This value indicates the number of bytes per second the system must handle to present an
AVI sequence as specified by the other parameters contained in the main header and
stream header chunks.
The dwFlags field contains any flags for the file. The following flags are defined:
AVIF_HASINDEX
Indicates the AVI file has an “idx1” chunk.
AVIF_MUSTUSEINDEX
Indicates the index should be used to determine the order of presentation of the data.
AVIF_ISINTERLEAVED
Indicates the AVI file is interleaved.
AVIF_WASCAPTUREFILE
Indicates the AVI file is a specially allocated file used for capturing real-time video.
AVIF_COPYRIGHTED
Indicates the AVI file contains copyrighted data.
02/10/93
AVI Files 錯誤! 僅限主文件。-5
data. For more information on interleaved files, see “Special Information for Interleaved
Files.”
The AVIF_WASCAPTUREFILE flag indicates the AVI file is a specially allocated file
used for capturing real-time video. Typically, capture files have been defragmented by
user so video capture data can be efficiently streamed into the file. If this flag is set, an
application should warn the user before writing over the file with this flag.
The AVIF_COPYRIGHTED flag indicates the AVI file contains copyrighted data. When
this flag is set, applications should not let users duplicate the file or the data in the file.
The dwTotalFrames field of the main header specifies the total number of frames of data
in file.
The dwInitialFrames is used for interleaved files. If you are creating interleaved files,
specify the number of frames in the file prior to the initial frame of the AVI sequence in
this field.
The dwStreams field specifies the number of streams in the file. For example, a file with
audio and video has 2 streams.
The dwSuggestedBufferSize field specifies the suggested buffer size for reading the file.
Generally, this size should be large enough to contain the largest chunk in the file. If set to
zero, or if it is too small, the playback software will have to reallocate memory during
playback which will reduce performance. For an interleaved file, the buffer size should be
large enough to read an entire record and not just a chunk.
The dwWidth and dwHeight fields specify the width and height of the AVI file in pixels.
The dwScale and dwRate fields are used to specify the general time scale that the file will
use. In addition to this time scale, each stream can have its own time scale. The time scale
in samples per second is determined by dividing dwRate by dwScale.
The dwStart and dwLength fields specify the starting time of the AVI file and the length
of the file. The units are defined by dwRate and dwScale. The dwStart field is usually
set to zero.
02/10/93
錯誤! 僅限主文件。-6 Video for Windows Programmer's Guide
The stream header has the following data structure defined for it:
typedef struct {
FOURCC fccType;
FOURCC fccHandler;
DWORD dwFlags;
DWORD dwReserved1;
DWORD dwInitialFrames;
DWORD dwScale;
DWORD dwRate;
DWORD dwStart;
DWORD dwLength;
DWORD dwSuggestedBufferSize;
DWORD dwQuality;
DWORD dwSampleSize;
} AVIStreamHeader;
The stream header specifies the type of data the stream contains, such as audio or video,
by means of a four-character code. The fccType field is set to “vids” if the stream it
specifies contains video data. It is set to “auds” if it contains audio data.
The fccHandler field contains a four-character code describing the installable compressor
or decompressor used with the data.
The dwFlags field contains any flags for the data stream. The AVISF_DISABLED flag
indicates that the stream data should be rendered only when explicitly enabled by the user.
The AVISF_VIDEO_PALCHANGES flag indicates palette changes are embedded in the
file.
The dwInitialFrames is used for interleaved files. If you are creating interleaved files,
specify the number of frames in the file prior to the initial frame of the AVI sequence in
this field.
The remaining fields describe the playback characteristics of the stream. These factors
include the playback rate (dwScale and dwRate), the starting time of the sequence
(dwStart), the length of the sequence (dwLength), the size of the playback buffer
(dwSuggestedBuffer), an indicator of the data quality (dwQuality), and sample size
(dwSampleSize). See the reference section for more information on these fields.
Some of the fields in the stream header structure are also present in the main header
structure. The data in the main header structure applies to the whole file while the data in
the stream header structure applies only to a stream.
A stream format (“strf”) chunk must follow a stream header (“strh”) chunk. The stream
format chunk describes the format of the data in the stream. For video streams, the
information in this chunk is a BITMAPINFO structure (including palette information if
appropriate). For audio streams, the information in this chunk is a WAVEFORMATEX or
PCMWAVEFORMAT structure. (The WAVEFORMATEX structure is an extended
version of the WAVEFORMAT structure.) For more information on this structure, see the
New Multimedia Data Types and Data Techniques Standards Update.
The “strl” chunk might also contain a stream data (“strd”) chunk. If used, this chunk
follows the stream format chunk. The format and content of this chunk is defined by
installable compression or decompression drivers. Typically, drivers use this information
02/10/93
AVI Files 錯誤! 僅限主文件。-7
for configuration. Applications that read and write RIFF files do not need to decode this
information. They transfer this data to and from a driver as a memory block.
An AVI player associates the stream headers in the LIST “hdrl” chunk with the stream
data in the LIST “movi” chunk by using the order of the “strl” chunks. The first “strl”
chunk applies to stream 0, the second applies to stream 1, and so forth. For example, if the
first “strl” chunk describes the wave audio data, the wave audio data is contained in
stream 0. Similarly, if the second “strl” chunk describes video data, then the video data is
contained in stream 1.
02/10/93
錯誤! 僅限主文件。-8 Video for Windows Programmer's Guide
The data chunk for a compressed DIB has the following form:
Compressed DIB '##dc'
BYTE abBits[];
Video data chunks can also define new palette entries used to update the palette during an
AVI sequence. These chunks are identified with a two-character code of “pc” (pc is an
abbreviation for palette change). The following data structure is defined palette
information:
typedef struct {
BYTE bFirstEntry;
BYTE bNumEntries;
WORD wFlags;
PALETTEENTRY peNew;
} AVIPALCHANGE;
The bFirstEntry field defines the first entry to change and the bNumEntries field
specifies the number of entries to change. The peNew field contains the new color entries.
If you include palette changes in a video stream, set the AVITF_VIDEO_PALCHANGES
flag in the dwFlags field of the stream header. This flag indicates that this video stream
contains palette changes and warns the playback software that it will need to animate the
palette.
The ckid, dwFlags, dwChunkOffset, and dwChunkLength entries are repeated in the
AVI file for each data chunk indexed. If the file is interleaved, the index will also have
these entries for each “rec” chunk. The “rec” entries should have the AVIIF_LIST flag set
and the list type in the ckid field.
The ckid field identifies the data chunk. This field uses four-character codes for
identifying the chunk.
The dwFlags field specifies any flags for the data. The AVIIF_KEYFRAME flag
indicates key frames in the video sequence. Key frames do not need previous video
information to be decompressed. The AVIIF_NOTIME flag indicates a chunk does not
02/10/93
AVI Files 錯誤! 僅限主文件。-9
affect the timing of a video stream. For example, changing palette entries indicated by a
palette chunk should occur between displaying video frames. Thus, if an application needs
to determine the length of a video sequence, it should not use chunks with the
AVIIF_NOTIME flag. In this case, it would ignore a palette chunk. The AVIIF_LIST flag
indicates the current chunk is a LIST chunk. Use the ckid field to identify the type of
LIST chunk.
The dwChunkOffset and dwChunkLength fields specify the position of the chunk and
the length of the chunk. The dwChunkOffset field specifies the position of the chunk in
the file relative to the 'movi' list. The dwChunkLength field specifies the length of the
chunk excluding the eight bytes for the RIFF header.
If you include an index in the RIFF file, set the AVIF_HASINDEX in the dwFlags field
of the AVI header. (This header is identified by “avih” chunk ID.) This flag indicates that
the file has an index.
As with any other RIFF files, all applications that read AVI files should ignore the non-
AVI chunks that it does not recognize. Applications that read and write AVI files should
preserve the non-AVI chunks when they save files they have loaded.
02/10/93
錯誤! 僅限主文件。-10 Video for Windows Programmer's Guide
mmioOpen
Opens a file for reading or writing, and returns a handle to the open file.
mmioCreateChunk
Creates a new chunk in a RIFF file.
mmioWrite
Writes a specified number of bytes to an open file.
mmioAscend
Ascends out of a RIFF file chunk to the next chunk in the file.
mmioClose
Closes an open file.
In addition to these functions, you can use mmioFOURCC to convert four individual
characters into a four-character code. For more information on these functions and macros,
see the Microsoft Windows Multimedia Programmer’s Guide and Microsoft Windows
Multimedia Programmer’s Reference.
02/10/93
AVI Files 錯誤! 僅限主文件。-11
Note:
The AVIFMT.H file contains macro definitions for creating the two- and four-character
codes described in this chapter. It also defines the aviTWOCC and
TWOCCFromFOURCC macros. These macros create two-character codes from
individual characters or from four-character codes.
Unlike many other RIFF files, AVI files use many nested chunks and subchunks. This
makes them more complicated than most RIFF files. Use the following tables as a
checklist to help you decide when to create a chunk, when to write data to a chunk, and
when to ascend from a chunk. The tables do not include information about writing non-
AVI data chunks to the file. The information in the chunk column of the table mirrors the
example in the “AVI RIFF Form” section presented previously.
02/10/93
錯誤! 僅限主文件。-12 Video for Windows Programmer's Guide
02/10/93
AVI Files 錯誤! 僅限主文件。-13
You can write the data as an individual chunk or as part of a “rec ” chunk. Like the LIST
“movi” chunk, you will not ascend from a “rec ” chunk until you write all of its subchunks.
Creating the “idx1” Chunk and Ascending From the “AVI ” Chunk
The optional index chunk is written as a complete chunk. After you have completed this
chunk, you can ascend from the “AVI ” chunk and close the file.
02/10/93
錯誤! 僅限主文件。-14 Video for Windows Programmer's Guide
AVIINDEXENTRY
The AVI file index consists of an array of AVIINDEXENTRY structures contained
within an 'idx1' chunk at the end of an AVI file. This chunk follows the main LIST 'movi'
chunk which contains the actual data.
typedef struct {
DWORD ckid;
DWORD dwFlags;
DWORD dwChunkOffset;
DWORD dwChunkLength;
} AVIINDEXENTRY;
Fields
The AVIINDEXENTRY structure has the following fields:
ckid
Specifies a four-character code corresponding to the chunk ID of a data chunk in the
file.
dwFlags
Specifies any applicable flags. The flags in the low-order word are reserved for AVI,
while those in the high-order word can be used for stream- and
compressor/decompressor-specific information.
The following values are currently defined:
AVIIF_LIST
Indicates the specified chunk is a 'LIST' chunk, and the ckid field contains the list
type of the chunk.
AVIIF_KEYFRAME
Indicates this chunk is a key frame. Key frames do not require additional preceding
chunks to be properly decoded.
02/10/93
AVI Files 錯誤! 僅限主文件。-15
AVIIF_FIRSTPART
Indicates this chunk needs the frames following it to be used; it cannot stand alone.
AVIIF_LASTPART
Indicates this chunk needs the frames preceding it to be used; it cannot stand alone.
AVIIF_NOTIME
Indicates this chunk should have no effect on timing or calculating time values
based on the number of chunks. For example, palette change chunks in a video
stream should have this flag set, so that they are not counted as taking up a frame’s
worth of time.
dwChunkOffset
Specifies the position in the file of the specified chunk. The position value includes the
eight byte RIFF header.
dwChunkLength
Specifies the length of the specified chunk. The length value does not include the eight
byte RIFF header.
AVIPALCHANGE
The AVIPALCHANGE structure is used in video streams containing palettized data to
indicate the palette should change for subsequent video data.
typedef struct {
BYTE bFirstEntry;
BYTE bNumEntries;
WORD wFlags;
PALETTEENTRY peNew;
} AVIPALCHANGE;
Fields
The AVIPALCHANGE structure has the following fields:
bFirstEntry
Specifies the first palette entry to change.
bNumEntries
Specifies the number of entries to change.
wFlags
Reserved. (This should be set to 0.)
peNew
Specifies an array of new palette entries.
02/10/93
錯誤! 僅限主文件。-16 Video for Windows Programmer's Guide
AVIStreamHeader
The AVIStreamHeader structure contains header information for a single stream of an
file. It is contained within an 'strh' chunk within a LIST 'strl' chunk that is itself contained
within the LIST 'hdrl' chunk at the beginning of an AVI RIFF file.
typedef struct {
FOURCC fccType;
FOURCC fccHandler;
DWORD dwFlags;
DWORD dwReserved1;
DWORD dwInitialFrames;
DWORD dwScale;
DWORD dwRate;
DWORD dwStart;
DWORD dwLength;
DWORD dwSuggestedBufferSize;
DWORD dwQuality;
DWORD dwSampleSize;
} AVIStreamHeader;
Fields
The AVIStreamHeader structure has the following fields:
fccType
Contains a four-character code which specifies the type of data contained in the stream.
The following values are currently defined for AVI data:
'vids'
Indicates the stream contains video data. The stream format chunk contains a
BITMAPINFO structure which can include palette information.
'auds'
Indicates the stream contains video data. The stream format chunk contains a
WAVEFORMAT or PCMWAVEFORMAT structure.
Other four-character codes can identify non-AVI data.
fccHandler
Optionally, contains a four-character code that identifies a specific data handler. The
data handler is the preferred handler for the stream.
dwFlags
Specifies any applicable flags. The bits in the high-order word of these flags are
specific to the type of data contained in the stream. The following flags are currently
defined:
AVISF_DISABLED
Indicates this stream should not be enabled by default.
AVISF_VIDEO_PALCHANGES
Indicates this video stream contains palette changes. This flag warns the playback
software that it will need to animate the palette.
dwReserved1
Reserved. (Should be set to 0.)
02/10/93
AVI Files 錯誤! 僅限主文件。-17
dwInitialFrames
Specifies how far audio data is skewed ahead of the video frames in interleaved files.
Typically, this is about 0.75 seconds.
dwScale
This field is used together with dwRate to specify the time scale that this stream will
use.
Dividing dwRate by dwScale gives the number of samples per second.
For video streams, this rate should be the frame rate.
For audio streams, this rate should correspond to the time needed for nBlockAlign
bytes of audio, which for PCM audio simply reduces to the sample rate.
dwRate
See dwScale.
dwStart
Specifies the starting time of the AVI file. The units are defined by the dwRate and
dwScale fields in the main file header. Normally, this is zero, but it can specify a delay
time for a stream which does not start concurrently with the file.
Note: The 1.0 release of the AVI tools does not support a non-zero starting time.
dwLength
Specifies the length of this stream. The units are defined by the dwRate and dwScale
fields of the stream’s header.
dwSuggestedBufferSize
Suggests how large a buffer should be used to read this stream. Typically, this contains
a value corresponding to the largest chunk present in the stream. Using the correct
buffer size makes playback more efficient. Use zero if you do not know the correct
buffer size.
dwQuality
Specifies an indicator of the quality of the data in the stream. Quality is represented as
a number between 0 and 10000. For compressed data, this typically represent the value
of the quality parameter passed to the compression software. If set to -1, drivers use the
default quality value.
dwSampleSize
Specifies the size of a single sample of data. This is set to zero if the samples can vary
in size. If this number is non-zero, then multiple samples of data can be grouped into a
single chunk within the file. If it is zero, each sample of data (such as a video frame)
must be in a separate chunk.
For video streams, this number is typically zero, although it can be non-zero if all video
frames are the same size.
For audio streams, this number should be the same as the nBlockAlign field of the
WAVEFORMAT structure describing the audio.
02/10/93
錯誤! 僅限主文件。-18 Video for Windows Programmer's Guide
MainAVIHeader
The MainAVIHeader structure contains global information for the entire AVI file. It is
contained within an 'avih' chunk within the LIST 'hdrl' chunk at the beginning of an AVI
RIFF file.
typedef struct {
DWORD dwMicroSecPerFrame;
DWORD dwMaxBytesPerSec;
DWORD dwReserved1;
DWORD dwFlags;
DWORD dwTotalFrames;
DWORD dwInitialFrames;
DWORD dwStreams;
DWORD dwSuggestedBufferSize;
DWORD dwWidth;
DWORD dwHeight;
DWORD dwScale;
DWORD dwRate;
DWORD dwStart;
DWORD dwLength;
} MainAVIHeader;
Fields
The MainAVIHeader structure has the following fields:
dwMicroSecPerFrame
Specifies the number of microseconds between frames.
dwMaxBytesPerSec
Specifies the approximate maximum data rate of file.
dwReserved1
Reserved. (This field should be set to 0.)
dwFlags
Specifies any applicable flags. The following flags are defined:
AVIF_HASINDEX
Indicates the AVI file has an 'idx1' chunk containing an index at the end of the file.
For good performance, all AVI files should contain an index.
AVIF_MUSTUSEINDEX
Indicates that the index, rather than the physical ordering of the chunks in the file,
should be used to determine the order of presentation of the data. For example, this
could be used for creating a list frames for editing.
AVIF_ISINTERLEAVED
Indicates the AVI file is interleaved.
AVIF_WASCAPTUREFILE
Indicates the AVI file is a specially allocated file used for capturing real-time video.
Applications should warn the user before writing over a file with this flag set
because the user probably defragmented this file.
02/10/93
AVI Files 錯誤! 僅限主文件。-19
AVIF_COPYRIGHTED
Indicates the AVI file contains copyrighted data and software. When this flag is
used, software should not permit the data to be duplicated.
dwTotalFrames
Specifies the number of frames of data in file.
dwInitialFrames
Specifies the initial frame for interleaved files. Non-interleaved files should specify
zero.
dwStreams
Specifies the number of streams in the file. For example, a file with audio and video
has 2 streams.
dwSuggestedBufferSize
Specifies the suggested buffer size for reading the file. Generally, this size should be
large enough to contain the largest chunk in the file. If set to zero, or if it is too small,
the playback software will have to reallocate memory during playback which will
reduce performance.
For an interleaved file, this buffer size should be large enough to read an entire record
and not just a chunk.
dwWidth
Specifies the width of the AVI file in pixels.
dwHeight
Specifies the height of the AVI file in pixels.
dwScale
This field is used with dwRate to specify the time scale that the file as a whole will use.
In addition, each stream can have its own time scale.
Dividing dwRate by dwScale gives the number of samples per second.
dwRate
See dwScale.
dwStart
Specifies the starting time of the AVI file. The units are defined by dwRate and
dwScale. This field is usually set to zero.
dwLength
Specifies the length of the AVI file. The units are defined by dwRate and dwScale.
This length is returned by MCIAVI when using the frames time format.
02/10/93