Sunday, March 1, 2009

AVI File Format - Data Structures

Basic data structures

There are 2 types of atoms in AVI files:


typedef struct
DWORD dwSize
BYTE data[dwSize] // contains headers or video/audio data


typedef struct
DWORD dwList
DWORD dwSize
BYTE data[dwSize-4] // contains Lists and Chunks

A chunk containing video, audio or subtitle data uses a dwFourCC containing 2 hexadecimaldigits specifying the stream number and 2 letters specifying the data type (dc = video, wb= audio, tx = text). The values dwFourCC and dwSize have the same meaning in both ofthe structures:

dwFourCC describes the type of the chunk (for example 'hdrl' for 'header list'), and dwSizecontains the size of the chunk or list, including the rst byte after the dwSize value. In thecase of Lists, this includes the 4 bytes taken by dwFourCC!

The value of dwList can be 'RIFF' ('RIFF-List') or 'LIST' ('List').

AVI file types

Basicly, there are 3 types of AVI les:

AVI 1.0 The original, old AVI le.

Open-DML An extension to the AVI file format. Version 1.02 has been specified by28/02/1996. The most important improvements are:
- almost unlimited file size (much more than what NTFS allows, for example)
- overhead reduced by 33%

• Hybride-Files: Open-DML les that contain an additional Legacy Index for compat-ibility reasons. This is not an "official" word for those files, but it is describing thatfile type pretty well. Hybride files containing only one RIFF List can be treated aseither file type.

This document describes the subset of Open-DML 1.02 file format features, as well as some additions ('hacks'), which work in common players if the proper (freely available) filters are installed. Features that work in Open-DML, but not in AVI 1.0, will be indicated to be Open-DML only.

Layout of an AVI file

A RIFF-List where dwFourCC = 'AVI ' shall be called a 'RIFF-AVI-List', a RIFF-List where dwFourCC = 'AVIX' shall be called a 'RIFF-AVIX-List'.

Every AVI file has the following layout:

RIFF AVI // mandatory
{ RIFF AVIX } // only for Open-DML files

Unlike what a uint32 suggests, the limit for the size of those lists is not 4 GB, but

• for AVI 1.0: size(RIFF-AVI) < 2 GB
• for Open-DML:
- size(RIFF-AVI) < 1 GB (!!) (assumed to be 2 GB by some muxing applications, like VirtualDub!)
- size(RIFF-AVIX) < 2 GB

As Windows XP insists on reading the entire first RIFF AVI list if no Legacy Index (seepage 12) is found, and as that Legacy Index causes overhead, it is recommended to create RIFF-AVI-Lists as small as possible.

MainAVIHeader (avih)

This structure is defined as follows:

typedef struct
DWORD dwMicroSecPerFrame; // frame display rate (or 0)
DWORD dwMaxBytesPerSec; // max. transfer rate
DWORD dwPaddingGranularity; // pad to multiples of this// size;

DWORD dwFlags; // the ever-present flags
DWORD dwTotalFrames; // # frames in file
DWORD dwInitialFrames;
DWORD dwStreams;
DWORD dwSuggestedBufferSize;

DWORD dwWidth;
DWORD dwHeight;

DWORD dwReserved[4];

} MainAVIHeader;

Unfortunately, those values do NOT have the meaning they seem to have when looking at their names.

• dwMicroSecPerFrame
Contains the duration of one video frame in microseconds. This value can be ignored(see stream header), but shall be written correctly by any AVI writer. Important: Some broken programs, like AVI Frate, write the frame rate value in the stream header, but not dwMicroSecPerFrame. Thus, dwMicroSecPerFrame should not be considered reliable!

• dwMaxBytesPerSec
Highest occuring data rate within the file. That value is of no importance either. Itsreliability should not be overrated.

• dwPadding
Granularity File is padded to a multiple of this

• dwFlags
See below

• dwTotalFrames
Contains the number of video frames in the RIFF-AVI list (it should NOT containthe total number of frames in the entire file if there are RIFF-AVIX-Lists. Some tools claiming to handle AVI files even assume this, but it definitely violates the Open-DML file format specification. Such applications are broken.) As some AVI file muxers write bad values here, this value should not be considered reliable.

• dwInitialFrames
Ignore that

• dwStreams
Number of streams in the file

• dwSuggestedBufferSize
Size of buffer required to hold chunks of the file. The reliability of this value should not be overrated.

• dwWidth
Width of video stream

• dwHeight
Height of video stream

Available Flags for MainAVIHeader::dwFlags

The file has an index9

The order in which the video and audio chunks must be replayed is determined by the index and may differ from the order in which those chunks occur in the file.

The streams are properly interleaved into each other

The file was captured. The interleave might be weird.

Ignore it

This flag indicates that the keyframe flags in the index are reliable. If this flag is not set in an Open-DML file, the keyframe flags could be defective without technically rendering the file invalid.

No comments:

Post a Comment