AVI file Format _avi

Source: Internet
Author: User

Small knowledge: AVI file format----excerpted from "DirectShow Practical Selection" by the Author: Lu Qiming

AVI (Audio Video Interleaved) is a file format for riff (Resource Interchange File format), which is used in audio-visual capture, editing, playback, and other applications. Typically, an AVI file can contain several different types of media streams (typically with an audio stream and a video stream), but an AVI file that contains a single audio stream or a single video stream is also legal. Avi can be regarded as the most basic and most commonly used media file format on Windows operating systems.


Let's introduce the riff file format first. The RIFF file uses four character code FOURCC (Four-character code) to characterize the data type, such as ' RIFF ', ' AVI ', ' LIST ', and so on. Note that the byte order used by the Windows operating system is Little-endian, so a four character code ' ABCD ' actual DWORD value should be 0x64636261. In addition, the four character code like ' AVI ' contains spaces is also legal.


The riff file first contains a file header structure like Figure 3.31.

Figure 3.31 Riff file structure

The first 4 bytes are a four character code ' RIFF ', indicates that this is a RIFF file, followed by the size of the RIFF file in 4 bytes followed by a four character code description of the specific type of file (such as AVI, wave, etc.), and finally the actual data. Note that the file size value is calculated by: The actual data length + 4 (the size of the file Type field); That is, the file size value does not include the size of the ' RIFF ' field and the file size field itself.


In the actual data of the riff file, the form of lists (list) and blocks (Chunk) is often used to organize. The list can be nested with a list of subforms and blocks. Where the structure of the list is: ' list ' listsize listtype listdata--' List ' is a four character code that indicates that this is a list; Listsize occupies 4 bytes, recording the size of the entire list; ListType is also a four character code that represents the specific type of this list; Listdata is the actual list data. Note that the Listsize value is computed by the actual list data length + 4 (the size of the ListType field), which means that the Listsize value does not include the size of the ' list ' field and the listsize domain itself. Then look at the structure of the BLOCK: Ckid Cksize Ckdata--ckid is a four character code that represents the block type; the Cksize occupies 4 bytes, recording the size of the entire block, and Ckdata to the actual block data. Note that the Cksize value refers to the actual block data length, excluding the size of the Ckid domain and the cksize domain itself. (Note: In the following section, a list is represented in the form of ListType (Listdata), representing a block in the form of Ckid (Ckdata), as the elements in parentheses in [optional element] are represented as optional. )


Next, the AVI file format is introduced. AVI file type is represented by a four character code ' AVI '. The entire AVI file is structured as follows: A RIFF header + two list (one to describe the media stream format, one to hold the media stream data) + an optional index block. The expanded structure of AVI files is as follows:

RIFF (' AVI '
LIST (' HDRL '
' Avih ' (main AVI information header data)
LIST (' Strl '
' STRH ' (header information data of the stream)
' STRF ' (format information data of the stream)
[' STRD ' (optional additional header information data)]
[' Strn ' (name of the alternative stream)]
...
)
...
)
LIST (' Movi '
{Subchunk | LIST (' Rec '
SubChunk1
SubChunk2
...
)
...
}
...
)
[' Idx1 ' (optional AVI index block data)]
)

First of all, RIFF (' AVI ' ...) The type of AVI file is characterized. Then is the AVI file must first list--' HDRL ' list, used to describe the various streams in AVI file format information (AVI file every channel of media data is called a stream). The ' HDRL ' list is nested with a series of blocks and sub lists--first, a ' AVIH ' block, which is used to record the global information of an AVI file, which is more than the number of flows, the width and height of the video image, and can be manipulated using a AVIMAINHEADER data structure:

typedef struct _AVIMAINHEADER {
FOURCC FCC; Must be ' AVIH '
DWORD CB; The size of this data structure, excluding the initial 8 bytes (FCC and CB two domains)
DWORD Dwmicrosecperframe; Video frame interval (in milliseconds)
DWORD dwmaxbytespersec; Maximum data rate for this AVI file
DWORD dwpaddinggranularity; Granularity of data fills
DWORD dwflags; The global tag of an AVI file, such as whether it contains an index block, etc.
DWORD Dwtotalframes; Total number of frames
DWORD Dwinitialframes; Specifies the initial frame number for the interactive format (non-interactive format should be specified as 0)
DWORD Dwstreams; The number of streams contained in this file
DWORD dwsuggestedbuffersize; It is recommended to read the cache size of this file (should accommodate the largest block)
DWORD Dwwidth; Width of video image (in pixels)
DWORD Dwheight; High video image (in pixels)
DWORD Dwreserved[4]; Keep
} Avimainheader;

Then, there is one or more ' Strl ' sub lists. (How many streams are in the file, and here's how many ' Strl ' sub lists.) Each ' Strl ' sub list contains at least one ' STRH ' block and one ' strf ' block, while the ' STRD ' block (some configuration information needed to save the codec) and the ' STRN ' block (the name of the saved stream) are optional. The first is the ' STRH ' block, which describes the header information for this stream and can be manipulated using a AVISTREAMHEADER data structure:

typedef struct _AVISTREAMHEADER {
FOURCC FCC; Must be ' STRH '
DWORD CB; The size of this data structure, excluding the initial 8 bytes (FCC and CB two domains)
FOURCC Fcctype; Type of stream: ' AUDs ' (audio stream), ' vids ' (video stream),
' Mids ' (MIDI stream), ' txts ' (text flow)
FOURCC Fcchandler; Specifies the processor for the stream, which is the decoder for audio and video
DWORD dwflags; Tag: Whether this stream is allowed to output. Whether the palette changes.
WORD wpriority; The priority of the stream (the default stream is the highest priority when there are multiple streams of the same type)
WORD Wlanguage;
DWORD Dwinitialframes; Specify the initial number of frames for an interactive format
DWORD Dwscale; The time scale used by this stream
DWORD dwrate;
DWORD Dwstart; Start time of the stream
DWORD dwlength; The length of the stream (the unit is related to the definition of Dwscale and dwrate)
DWORD dwsuggestedbuffersize; Read the recommended cache size for this streaming data
DWORD dwquality; Quality metrics for streaming data (0 ~ 10,000)
DWORD dwsamplesize; Size of sample
struct {
short int left;
short int top;
short int right;
short int bottom;
} rcframe; Specify where this stream (video stream or text flow) is displayed in the main video window
The main video window is determined by the dwwidth and dwheight in the avimainheader structure.
} Avistreamheader;

The ' STRF ' block is then used to illustrate the specific format of the stream. If it is a video stream, a BITMAPINFO data structure is used to describe it, and if it is an audio stream, a WAVEFORMATEX data structure is used to describe it.


When all the streams in an AVI file use a ' Strl ' sub list to illustrate (note: The order in which the ' Strl ' sub list appears corresponds to the number of the media stream, such as the first ' Strl ' sub-list that describes the first stream (stream 0), and the second ' The Strl ' sub list describes the second stream (stream 1), and so on), the ' HDRL ' list of tasks is completed, followed by the AVI file is the necessary second list-the ' movi ' list, used to save real media stream data (video image frame data or audio sampling data, etc.). So, how do you organize the data? You can embed a block of data directly into the ' movi ' list, or you can divide a few pieces of data into a ' rec ' list and then compose it into the ' movi ' list. (Note: When reading the contents of an AVI file, it is recommended that all blocks of data in a ' rec ' list be read one at a time.) However, when an AVI file contains multiple streams, how does the data block differ from the data block? The data block then uses a four character code to characterize its type, which consists of a 2-byte type code and a 2-byte stream number. The standard type codes are defined as follows: ' DB ' (uncompressed video frame), ' DC ' (compressed video frame), ' PC ' (new color palette), ' WB ' (sound shrink video). For example, the first stream (stream 0) is audio, the four character code representing the audio block is ' 00WB ', and the second stream (stream 1) is the video, the four character code representing the video block is ' 00db ' or ' 00dc '. For video data, in the middle of the AVI data sequence can also define a new color palette, each changed palette data block with ' xxpc ' to characterize the new palette using a data structure avipalchange to define. (Note: If a stream's palette may change midway, it should include a avisf_video_palchanges tag in the description of the stream format, which is the dwflags of the avistreamheader structure.) In addition, text flow data blocks can be characterized by arbitrary type codes.


Finally, immediately following the ' HDRL ' list and the ' movi ' list is an optional index block for AVI files. This index block is indexed for each media block in the AVI file and records their offset in the file (possibly relative to the ' movi ' list or the beginning of the AVI file). The index block uses a four character code ' idx1 ' to characterize the index information using a data structure to avioldindex the definition.

typedef struct _AVIOLDINDEX {
FOURCC FCC; Must be ' idx1 '
DWORD CB; The size of this data structure, excluding the initial 8 bytes (FCC and CB two domains)
struct _avioldindex_entry {
DWORD Dwchunkid; Four character codes representing this block of data
DWORD dwflags; Description of this block is not a key frame, is not a ' rec ' list and other information
DWORD Dwoffset; The offset of this block of data in the file
DWORD dwsize; Size of this data block
} aindex[]; This is an array. Define an index information for each media data block
} Avioldindex;

Note: If an AVI file contains an index block, it should include a avif_hasindex tag in the description of the main AVI header, which is the dwflags of the avimainheader structure.


There is also a special block of data that is represented by a four character code ' JUNK ', which is used for the alignment of internal data (padding), and the application should ignore the actual meaning of these blocks.

Hint: The above about AVI file format Introduction, does not include opendml AVI m-jpeg file format Group developed OPENDML AVI file format extension part of the content. For readers who want to know more about this extension, please refer to the "Opendml AVI File Format Extensions" (this article is available for search on the Internet).


from:http://blog.csdn.net/happydeer/article/details/8775

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.