AVI File Format Parsing + AVI File Parsing Tool

Source: Internet
Author: User

AVI File Parsing tool: http://download.csdn.net/detail/zjq634359531/7556659

AVI (short for audio video interleaved) is a type of riff (short for resource Interchange File Format) file format. It is mostly used in audio/video capturing, editing, and playback applications. Normally, an AVI file can contain multiple different types of media streams (in typical cases, there is an audio stream and a video stream ), however, AVI files containing a single audio stream or a single video stream are also valid. Avi is the most basic and commonly used media file format in windows.

First, we will introduce the riff file format. The riff file uses the four-character code fourcc (four-character code) to characterize the data type, such as 'riff', 'av', and 'LIST. Note: In Windows, the byte sequence is little-Endian. Therefore, the actual DWORD Value of a four-character code 'abcd' should be 0x64636261. In addition, it is legal that the four-digit code contains spaces like 'av.

The riff file first contains a 3.31 File Header structure.

Figure 3.31 riff file structure

The first four bytes are a four-byte code 'riff', which indicates that this is a riff file, followed by four bytes to indicate the size of the riff file; then there is a four-digit code indicating the specific type of the file (such as avi and wave), and finally the actual data. Note that the calculation method of the file size value is: actual Data Length + 4 (size of the file type field); that is, the file size value does not include the size of the 'riff' and "file size" fields.

In the actual data of the riff file, the list and chunk are usually used for organization. The list can be nested with sublists and blocks. Here, the list structure is: 'LIST' listsize listtype listdata -- 'LIST' is a four-byte code, indicating that this is a list; listsize occupies 4 bytes and records the size of the entire list; listtype is also a four-digit code, indicating the specific type of the List; listdata is the actual list data. Note that the listsize value is calculated as follows: the actual length of the list data + 4 (the size of the listtype field); that is, the listsize value does not include the size of the 'LIST' and listsize fields. Next, let's look at the block structure: ckid cksize ckdata -- ckid is a four-byte code that represents the block type. cksize occupies 4 bytes and records the size of the entire block. ckdata is the actual block data. Note that the cksize value refers to the actual block data length, excluding the size of the ckid and cksize fields. (Note: In the following content, a list is represented in the form of list (listtype (listdata), and a block is represented in ckid (ckdata, for example, the elements in [optional element] are represented as optional .)

Next we will introduce the AVI file format. The AVI file type is represented by a four-character code 'avi. The structure of the entire AVI file is: a riff header + two lists (one for describing the media stream format, one for saving media stream data) + an optional index block. The structure of the AVI file is roughly as follows:

Riff ('av'
List ('hdrl'
'Avih '(Master Avi information header data)
List ('strl'
'Strh' (Stream header information data)
'Strf' (Stream format information data)
['Strd' (optional additional header information data)]
['Strn' (name of an optional stream)]
...
)
...
)
List ('movi'
{Subchunk | list ('rec'
Subchunk1
Subchunk2
...
)
...
}
...
)
['Idx1' (optional Avi index block data)]
)

First, Riff ('av '...) Represents the AVI file type. The first list required by the AVI file is the 'hdrl' list, which describes the format information of each stream in the AVI file (each media data in the AVI file is called a stream ). The 'hdrl' list contains a series of blocks and sublists. The first is an 'avih 'block, which is used to record the global information of an AVI file, such as the number of streams, width and height of the video image, you can use an avimainheader data structure to operate:

Typedef struct _ avimainheader {
Fourcc FCC; // It Must Be 'avih'
Dword cb; // size of the data structure, excluding the first 8 bytes (fcc and CB)
DWORD dwmicrosecperframe; // video frame interval (in milliseconds)
DWORD dwmaxbytespersec; // maximum data rate of the AVI file
DWORD dwpaddinggranularity; // data filling Granularity
DWORD dwflags; // global tag of the AVI file, such as whether the index block is contained
DWORD dwtotalframes; // total number of frames
DWORD dwinitialframes; // specify the initial number of frames for the interactive format (the non-interactive format should be set to 0)
DWORD dwstreams; // Number of streams contained in this file
DWORD dwsuggestedbuffersize; // we recommend that you read the cache size of this file (the maximum block size should be supported)
DWORD dwwidth; // video image width (in pixels)
DWORD dwheight; // video image height (in pixels)
DWORD dwreserved [4]; // Reserved
} Avimainheader;

Then, there is one or more 'strl' sublists. (The number of streams in the file. Here we will list the number of 'strl' sublists .) Each 'strl' sublist contains at least one 'strh' block and one 'strf' block, while the 'strd' block (save some configuration information required by the decoder) and 'strn' blocks (Save the stream name) are optional. The first is the 'strh' block, which is used to describe the header information of the stream. You can use an avistreamheader data structure to perform operations:

Typedef struct _ avistreamheader {
Fourcc FCC; // The value must be 'strh'
Dword cb; // size of the data structure, excluding the first 8 bytes (fcc and CB)
Fourcc fcctype; // stream type: 'auds '(audio stream), 'vids' (Video Stream ),
// 'Mids '(MIDI stream) and 'txts' (Text Stream)
Fourcc fcchandler; // specifies the stream processor, which is a decoder for audio and video.
DWORD dwflags; // flag: can this stream be output? Does the palette change?
Word wpriority; // stream priority (when multiple streams of the same type have the highest priority, the default stream is used)
Word wlanguage;
DWORD dwinitialframes; // specify the initial number of frames for the interaction format
DWORD dwscale; // The time scale used by the stream
DWORD dwrate;
DWORD dwstart; // stream Start Time
DWORD dwlength; // The stream length. The unit is related to the dwscale and dwrate definitions)
DWORD dwsuggestedbuffersize; // The cache size recommended for reading this stream data
DWORD dwquality; // streaming data quality indicator (0 ~ 10,000)
DWORD dwsamplesize; // sample size
Struct {
Short int left;
Short int top;
Short int right;
Short int bottom;
} Rcframe; // specifies the position of the stream (video stream or text stream) in the main video window.
// The main video window is determined by dwwidth and dwheight in the avimainheader structure.
} Avistreamheader;

 

Then the 'strf' block is used to describe the specific format of the stream. For video streams, A bitmapinfo data structure is used for description. For audio streams, A waveformatex data structure is used for description.

 

After all streams in the AVI file are described in the 'strl' sublist (note: the sequence in which the 'strl' sublist appears corresponds to the ID of the media stream, for example, the first 'strl' sublist describes the first stream (Stream 0), the second 'strl' sublist describes the second stream (Stream 1), and so on ), the 'hdrl' list task is completed, followed by the second list required by the AVI file-'movi' list, stores Real Media Stream Data (video image frame data or audio sample data ). So how can we organize this data? Data blocks can be directly embedded in the 'movi' list, or several data blocks can be grouped into a 'rec 'list and then organized into the 'movi' list. (Note: when reading the content of an AVI file, we recommend that you read all data blocks in the 'rec 'list at a time .) However, when an AVI file contains multiple streams, what are the differences between data blocks and data blocks? Therefore, a data block uses a four-byte code to represent its type. This four-byte code consists of two types and two stream numbers. The standard type code is defined as follows: 'db' (non-compressed video frame), 'dc' (compressed video frame), and 'pc' (use a new color palette) and 'wb '(audio reduction video ). For example, if the first stream 0 is audio, the four-digit code representing the audio data block is '00wb ', and the second stream (Stream 1) is video, the four-digit code representing the video data block is '00db' or '00dc '. For video data, a new palette can be defined in the middle of the Avi Data Sequence. Each changed palette data block is characterized by 'xxpc, the new palette is defined using a Data Structure avipalchange. (Note: If the color of a stream may change midway through, you should include an avisf_video_palchanges mark in the description of the stream format, that is, dwflags of the avistreamheader structure .) In addition, the data block of a text stream can use arbitrary type code table.

 

Finally, the index block that follows the 'hdrl' list and 'movi' list is an optional index block for the AVI file. This index block is used to index each media block in the AVI file and record their offset in the file (which may be relative to the 'movi' list or start with the AVI file ). The index block is characterized by a four-character code 'idx1'. The index information is defined by avioldindex using a data structure.

Typedef struct _ avioldindex {
Fourcc FCC; // The value must be 'idx1'
Dword cb; // size of the data structure, excluding the first 8 bytes (fcc and CB)
Struct _ avioldindex_entry {
DWORD dwchunkid; // the four-digit code representing the data block
DWORD dwflags; // indicates whether the data block is a key frame or a 'rec 'list.
DWORD dwoffset; // the offset of the data block in the file
DWORD dwsize; // size of the data block
} Aindex []; // This is an array! Define an index for each media Block
} Avioldindex;

 

NOTE: If an AVI file package contains an index block, an avif_hasindex mark should be included in the description of the master Avi information header, that is, dwflags of the avimainheader structure.

 

There is also a special data block, which is characterized by a four-character code 'junk'. It is used for internal data queues (filling), and the application should ignore the actual meaning of these data blocks.

 

Prompt: The above introduction to the AVI file format does not include the opendml AVI file format extensions developed by the opendml Avi M-JPEG file format team. For more information about this extension, see "opendml AVI file format extensions" (This article can be found on the Internet ).

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.