AVI file format

zhaozj2021-02-11  216

Tips: AVI file format ---- excerpt from "DirectShow Practice Featured in" Author: Lu Qiming

AVI (Audio Video Interleaved Abbreviation) is an exact file format for Riff (Resource Interchange File Format) file format, mostly used in audio and video capture, editing, playback. Typically, an AVI file can contain multiple different types of media streams (typically have an audio stream and a video stream), but the AVI file containing a single audio stream or a single video stream is also legal. AVI can be the most basic and most common media file format on the Windows operating system.

First introduce the Riff file format. Riff files use four character code Fourcc (Four-Character Code) to characterize data type, such as 'Riff', 'Avi', 'List', etc. Note that the byte order used by the Windows operating system is Little-Endian, so a four-character code 'abcd' actually DWORD value should be 0x64636261. In addition, it is also legal in four-character code like 'avi'.

The RIFF file first contains a file header structure as shown in Figure 3.31.

Figure 3.31 RIFF file structure

The first four bytes are a four-character code 'riff', indicating that this is a Riff file; followed by 4 bytes back to indicate the size of this Riff file; then a four-character code explanation file specific Type (such as AVI, Wave, etc.); the end is actual data. Note that the calculation method of the file size value is: the actual data length 4 (the size of the file type field); that is, the value of the file size does not include the size of the 'Riff' domain and the "file size" domain itself.

Among the actual data of the RIFF file, the form of list (LIST) and blocks is also used to organize. The list can be nested with sub-lists and blocks. Among them, the structure of the list is: 'list' is a four-character code, indicating that this is a list; ListSize takes up 4 bytes, records the size of the entire list; ListType is also a four-character code, Represents the specific type of this list; ListData is the actual list data. Note that the ListSize value is calculated: the actual list data length 4 (the size of the ListType field); that is, the ListSize value does not include the size of the 'List' domain and the ListSize domain itself. Take another structure: ckid cksize ckdata --ckid is a four-character code representing the block type; CKSIZE occupies 4 bytes, 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 without including the size of the CKID domain and the Cksize domain itself. (Note: In the following, a list will be represented in the form of List (ListType (ListData), indicating a block in the form of CKID (CKDATA), such as [optional element] in the brackets in brackets Options.)

Next, 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 (a media stream format, one for saving media stream data) an optional index block. The expansion structure of the AVI file is approximately as follows:

Riff ('avi' list ('hdrl' 'avih' (main AVI information header data) list ('strl' '' strH '(stream header information data)' STRF '(stream format information data) [' strd "( Optional additional header information)] ['strn' (the name of the optional stream)] ...) ...) List ('Movi' {Subchunk | List ('REC' Subchunk1 Subchunk2 ...) ...} ...) ['IDX1' (optional AVI Index Block Data)] First, Riff ('Avi' ...) characterizes the AVI file type. Then the first list of the AVI files - 'hdrl' list, used to describe the format information of each stream in the AVI file (each of the media data in the AVI file is called a stream). The 'HDRL' list nested a series of blocks and sub-lists - first is a 'avih' block for recording global information of the AVI file, such as the number of streams, the wide and higher video images, can use an AvimainHeader data structure operating:

Typedef struct _avimainhead; // must be 'avih' dword cb; // This data structure size, excluding the initial 8 bytes (FCC and CB two domains) DWORD dwMICROSECPERFRAME; // video frame interval (In milliseconds) DWORD dwMaxBytespersec; // This avi file's maximum data rate dword dwpaddinggranularity; // Data padding granular DWORD DWFLAGS; // Avi file global mark, such as whether DWord dwtotAlframes, etc. of the index block; // The number of frames DWORD dwinitialframes; / / Specify the number of initial frames (Non-interaction format should be specified as 0) DWORD DWSTREAMS; / / The number of streams containing this file is recommended to read the cache size of this file ( It should be able to accommodate the largest block) DWORD DWWIDTH; // Video image width (in pixel) DWORD dwheight; // Video image high (in pixel) DWORD DWRESERVED [4]; // Reserved} AvimainHeader;

Then, it is one or more 'strl' sub-lists. (How many flows are in the file, here there should be how many 'strl' sub-lists.) Each 'Strl' sub-list contains at least one 'strH' block and a 'STRF' block, and 'strd' block (save compilation) Some configuration information required by the decoder) and the 'STRN' block (the name of the save stream) is optional. The first is the 'strH' block for explaining the header information of this stream, you can use an AvistreamHeader data structure to operate: typedef struct _avistreamheader {fourcc fcc; // must be 'strH' dword CB; // This data structure size, Does not include the initial 8 bytes (FCC and CB two domains) Fourcc fcctype; // stream type: 'Auds' (audio stream), 'VIDS' (video stream), // 'mids' (MIDI stream) , 'Txts' (text flow) Fourcc fcchandler; // Specify the process of the stream, for the audio and video is the decoder DWORD DWFLAGS; // Tag: Is this stream output? Is the palette change? Word WPRIORITY; / / The priority (when there are multiple types of flows, the highest priority is the default flow) Word WLANGUAGE; DWORD dwinitialframes; / / Specify the initial frame number DWORD DWSCALE for the interactive format; // This stream use Time Scale DWORD DWRATE; DWORD DWSTART; / / Start Time DWORD DWLENGTH; / / The length of the stream (the definition of DWSCALE and DWRATE) DWORD dwsuggestedBuffersize; // Read this streaming data Recommended Cache size DWQQuality ; // stream data quality indicator (0 ~ 10,000) DWORD DWSAMPLESIZE; // Sample Size Struct {Short Int Left; Short Int Top; Short Int Int Right; Short Int Bottom;} rcframe; // Specify this stream (video stream) Or text flow) The display position // video main window in the video main window is determined by DWWIDTH and DWHEIGHT in the AvimainHeader structure} AvistreamHeader;

It is then the 'STRF' block for explaining the specific format of the stream. If it is a video stream, use a BitmapInfo data structure; if it is a audio stream, use a WAVEFormatex data structure to describe it.

When all streams in the AVI file use a 'strl' sub-list description (Note: The order of the 'Strl' sub-list is corresponding to the number of media streams, such as the first 'Strl' sub-list description The first stream (stream 0), the second 'Strl' sub-list description is the second stream (stream 1), in this type), the task of the 'HDRL' list is completed, followed by AVI The second list of files must be used - 'Movi' list, used to save real media stream data (video image frame data or audio sampling data, etc.). So how do you organize this data? You can embed the data block directly in the 'Movi' list, or several data blocks can be subjected to a 'REC' list and then edit the 'MOVI' list. (Note: When reading the content of the AVI file, it is recommended to read all the data blocks in a 'REC' list once.) However, when there is a plurality of streams in the AVI file, between data blocks and data blocks How to distinguish it? Since the data block uses a four-character code to characterize its type, this quadruption consists of two bytes of type code and 2 bytes of flow codes. The standard type code is defined as follows: 'DB' (non-compressed video frame), 'DC' (compressed video frame), 'PC' (modified new palette), 'WB' (True video). For example, the first stream (stream 0) is an audio, then the four-character code of the audio data block is '00wb'; the second stream (stream 1) is a video, the four-character code of the video data block is '00db' Or '00dc'. For video data, a new palette can also be defined in the middle of the AVI data sequence, and each changed palette data block is characterized by 'xxpc', and the new palette uses a data structure AvipalChange to define. (Note: If a stream can change, it should be in this stream format, that is, the DWFlags of the AvistreamHeader structure contains an avisf_video_palchanges tag.) In addition, text flow data blocks can use casual type code characterization . Finally, followed by the 'HDRL' list and the 'Movi' list, that is, the AVI file optional index block. This index block is indexed for each media block in the AVI file and records the offset they in the file (possibly relative to the 'MOVI' list, may also begin with respect to the AVI file). Index block uses a four-character code 'IDX1' to characterize, index information uses a data structure to defiberindex definition.

Typedef struct _avioldindex {fourcc fcc; // must be 'idx1' DWORD CB; // This data structure size, does not include the initial 8 bytes (FCC and CB two domains) struct _avioldindex_entry {dWord dwchunkid; // characterization The four-character code DWORD dwflags of this data block DWORD DWFLAGS; / / Description This data block is not a key frame, is it a 'REC' list and other information DWORD dwoffset; // This data block is in the file DWSize DWSize DWSize; // The size of the 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, you should include an avif_haasindex tag in the DWFLAGS of the AvimainHeader structure in the description of the primary AVI information header. There is also a special data block, characterized by a four-character code 'junk', which is used for teams (filled) for internal data, and the application should ignore the actual significance of these data blocks.

Tip: The above-mentioned introduction to the AVI file format does not include the contents of the OpenDML AVI file format extension to the OpenDML AVI M-JPEG file format group. Readers who want to know more about the extension, please refer to the "OpenDML AVI File Format Extensions" (this article can be searched on the Internet).

转载请注明原文地址:https://www.9cbs.com/read-4248.html

New Post(0)