I recently studied DirectShow-based knowledge and saw some good articles so that I could not search for them each time, so I reposted them to my blog. I have no intention of infringing the author's copyright, I only want to learn and have no other meaning. I hope the author will forgive me and apologize to you.
AVI file format (the attachment is the source code for implementing AVI file creation in C Language) AVI (abbreviation of audio video interleaved) is a format of riff (short for resource Interchange File Format, it is mostly used in audio/video capture, editing, playback, and other 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 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.
| Riff |
Size |
Type of File |
Actual data |
|
| File size value | the actual data of the riff file is usually organized in the form of list and Chunk. The list can be nested with sublists and blocks. Where,
The list structure is 'LIST' listsize listtype listdata.-- 'LIST' is a four-byte code that indicates a list. listsize occupies 4 bytes and records the size of the entire list. listtype is also a four-byte code that indicates 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 ('avi' 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' (available 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; // The value must be 'avih 'dword CB; // the size of the data structure, excluding the first 8 bytes (fcc and CB) DWORD dwmicrosecperframe; // The video frame interval (in milliseconds) DWORD dwmaxbytespersec; // The maximum data rate of the AVI file DWORD dwpaddinggranularity; // The data filling granularity DWORD dwflags; // global tag of the AVI file, such as whether or not it contains index blocks and other DWORD dwtotalframes; // total number of frames DWORD dwinitia Lframes; // specify the initial number of frames for the interaction format (0 for non-interaction format) DWORD dwstreams; // The number of streams contained in this file DWORD dwsuggestedbuffersize; // It is recommended to read the cache size of this file (which should accommodate the largest block) DWORD dwwidth; // The width of the video image (in pixels) DWORD dwheight; // The height (in pixels) of the video image DWORD dwreserved [4]; // reserved} avimainheader; then, it 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 operate: typedef struct _ avistreamheader {fourcc FCC; // It Must Be 'strh' dword cb; // the 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. for audio and video, it is the decoder DWORD dwflags; // MARK: whether to allow this stream 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 frame number DWORD dwscale for the interaction format; // The time scale DWORD dwrate used by the stream; DWORD dwstart; // The stream start time DWORD dwlength; // The stream length (unit: related to the dwscale and dwrate definitions) DWORD dwsuggestedbuffersize; // The cache size recommended for reading the 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; // specify the display 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, and then the 'strf' block, describes the specific stream format. 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; // It Must Be 'idx1' dword cb; // the size of the data structure, excluding the first 8 bytes (fcc and CB) struct _ avioldindex_entry {DWORD dwchunkid; // the four-digit DWORD dwflags that characterize the data block; // It indicates whether the data block is a key frame, whether it is a 'rec 'list or other information DWORD dwoffset; // The offset DWORD dwsize of the data block in the file; // the size of the data block} aindex []; // This is an array! Define an index for each media data block} avioldindex; Note: if an AVI file package contains an index block, it should be in the description of the primary Avi information header, that is, dwflags in the avimainheader structure contains an avif_hasindex tag. 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.