The Microsoft AVI file format is a RIFF file specification used with applications that capture, edit, and play back audio-video sequences. In general, AVI files contain multiple streams of different types of data. Most AVI sequences use both audio and video streams. A simple variation for an AVI sequence uses video data and does not require an audio stream.
以下是具体封装:
"RIFF" //
0X00 //文件总长度,可设置为0
"AVI " //最后有个空格
"LIST" //list0
0X00 //list0 length,不可以置0
"hdrl" //
"avih" //fcc
0X38 //cb 长度,56字节
0Xa2c2 //dwMicroSecPerFrame 每帧微秒值,这个值可以通过 10^6 / fps求得
0X00 //dwMaxBytesPerSec (可设置为0)
0X00 //dwPaddingGranularity (可设置为0)
0X0910 //dwFlags (AVIF_HASINDEX | AVIF_ISINTERLEAVED | AVIF_TRUSTCKTYPE)
0X00 //dwTotalFrames 可设置为0
0X00 //dwInitialFrames 可设置为0
0X02 //dwStreams 流的个数,如果是一个视频流一个音频流,此处就是2,
0X100000//dwSuggestedBufferSize 1024 * 1024
0X00 //dwWidth 视频宽,不可设置为0
0X00 //dwHeight 视频高, 不可设置为0
0X00 //dwReserved[0] 设置为0
0X00 //dwReserved[1] 设置为0
0X00 //dwReserved[2] 设置为0
0X00 //dwReserved[3] 设置为0
///////////////////////////////////////////////////////////////////////////////////////////////
typedef struct _avimainheader {
FOURCC fcc;
DWORD cb;
DWORD dwMicroSecPerFrame;
DWORD dwMaxBytesPerSec;
DWORD dwPaddingGranularity;
DWORD dwFlags;
DWORD dwTotalFrames;
DWORD dwInitialFrames;
DWORD dwStreams;
DWORD dwSuggestedBufferSize;
DWORD dwWidth;
DWORD dwHeight;
DWORD dwReserved[4];
} AVIMAINHEADER;
///////////////////////////////////////////////////////////////////////////////////////////////
"LIST" //list1
0X00 //list1 length 不可设置为0
"strl"
"strh" //fcc
0X38 //cb 流头部长度
"vids" //fccType
0X00 //fccHandler 此处可设置为0
0X00 //dwFlags 此处可设置为0
0X00 //wPriority AND wLanguage 可设置为0
0X00 //dwInitialFrames 可设置为0
0X01 //dwScale
0X19 //dwRate (dwRate / dwScale = fps)
0X00 //dwStart 可设置为0
0X00 //dwLength 可设置为0
0X100000//dwSuggestedBufferSize 1024 * 1024
0XFFFFFFFF//dwQuality
0X00 //dwSampleSize 可设置为0
0X00 //left AND top 设置为0
0X00 //Height << 16 | Width 不可设置为0
/////////////////////////////////////////////////////////////////////////////////////////////
typedef struct _avistreamheader {
FOURCC fcc;
DWORD cb;
FOURCC fccType;
FOURCC fccHandler;
DWORD dwFlags;
WORD wPriority;
WORD wLanguage;
DWORD dwInitialFrames;
DWORD dwScale;
DWORD dwRate;
DWORD dwStart;
DWORD dwLength;
DWORD dwSuggestedBufferSize;
DWORD dwQuality;
DWORD dwSampleSize;
struct {
short int left;
short int top;
short int right;
short int bottom;
} rcFrame;
} AVISTREAMHEADER;
/////////////////////////////////////////////////////////////////////////////////////////////
"strf"
0X28 //
0X28 //biSize
0X00 //biWidth 不可设置为0
0X00 //biHeight 不可设置为0
0X180001//biBitCount << 16 | biPlanes (biBitCount表示一个像素用几位表示,此处24位,biPlanes设备位面,为1)
"H264" //biCompression For compressed video and YUV formats, this member is a FOURCC code, specified as a //DWORD in little-endian order.For example, YUYV video has the FOURCC 'VYUY' or 0x56595559. //For more information see FOURCC Codes.
0X00 //biSizeImage = biWidth * biHeight * biBitCount / 8 不可设置为0
0X00 //biXPelsPerMeter 可设置为0
0X00 //biYPelsPerMeter 可设置为0
0X00 //biClrUsed 可设置为0
0X00 //biClrImportant 可设置为0
//list1长度计算到这里
///////////////////////////////////////////////////////////////////////////////////////////////
typedef struct tagBITMAPINFOHEADER {
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPINFOHEADER;
////////////////////////////////////////////////////////////////////////////////////////////////
"LIST" //list2
0x00 //list2 length 不可设置为0
"strl"
"strh" //fcc
0X38 //cb 流头部长度
"auds" //fccType
0X00 //fccHandler 此处可设置为0
0X00 //dwFlags 此处可设置为0
0X00 //wPriority AND wLanguage 可设置为0
0X00 //dwInitialFrames 可设置为0
0X01 //dwScale
0X00 //dwRate (dwRate / dwScale = audio bps/8)
0X00 //dwStart 可设置为0
0X00 //dwLength 可设置为0
0X3000 //dwSuggestedBufferSize 1024 * 12
0XFFFFFFFF //dwQuality
0X01 //dwSampleSize 可设置为0
0X00 //left AND top 设置为0
0X00 //right AND bottom 设置为0
////////////////////////////////////////////////////////////////////////////////////////////////////////////
typedef struct _avistreamheader {
FOURCC fcc;
DWORD cb;
FOURCC fccType;
FOURCC fccHandler;
DWORD dwFlags;
WORD wPriority;
WORD wLanguage;
DWORD dwInitialFrames;
DWORD dwScale;
DWORD dwRate;
DWORD dwStart;
DWORD dwLength;
DWORD dwSuggestedBufferSize;
DWORD dwQuality;
DWORD dwSampleSize;
struct {
short int left;
short int top;
short int right;
short int bottom;
} rcFrame;
} AVISTREAMHEADER;
////////////////////////////////////////////////////////////////////////////////////////////////////////////
"strf" //
0X12 //sizeof(WAVEFORMATEX)
0X10007 //nChannels << 16 | wFormatTag
//nChannels : Number of channels in the waveform-audio data.
//Monaural data uses one channel and stereo data uses two channels.
//wFormatTag : 0X07:AV_CODEC_ID_PCM_MULAW 0X55:AV_CODEC_ID_MP3
0X00 //nSamplesPerSec 采样率,不可设置为0
0X00 //nAvgBytesPerSec 字节率,即bps/8 不可设置为0
0X80001 //wBitsPerSample << 16 | nBlockAlign
//wBitsPerSample:Bits per sample for the wFormatTag format type.
//If wFormatTag is WAVE_FORMAT_PCM,
//then wBitsPerSample should be equal to 8 or 16.
//nBlockAlign:Block alignment, in bytes.
//The block alignment is the minimum atomic unit of data for the
//wFormatTag format type. If wFormatTag is WAVE_FORMAT_PCM,
//nBlockAlign must equal (nChannels × wBitsPerSample) / 8.
0X00 //cbSize 设置为0,注意,这个字段只有16位
//list2长度计算到这里
///////////////////////////////////////////////////////////////////////////////////////////
typedef struct {
WORD wFormatTag;
WORD nChannels;
DWORD nSamplesPerSec;
DWORD nAvgBytesPerSec;
WORD nBlockAlign;
WORD wBitsPerSample;
WORD cbSize;
} WAVEFORMATEX;
///////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
"LIST"
0X00 //list3 length 如果忽略这个长度,即置0,Movie Player,VLC等可以播放,Media Player不能播放
"movi"
"00dc" Length1 Video_Data (if Length % 2 == 1, we should add a byte 0x00 to the end of data for align)
"00wb" Length2 Audio_Data (if Length % 2 == 1, we should add a byte 0x00 to the end of data for align)
"00wb" Length3 Audio_Data
"00dc" Length4 Video_Data
. . .
. . .
. . .
"00dc" Lengthn Video_Data
//list3长度计算到这里,avi数据信息到此结束
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
"idx1"
0X00 //index length 不可设置为0
"00dc" 0X10000000 0X04 Length1
"00wb" 0X10000000 0Xxx Length2
"00wb" 0X10000000 0Xxx Length3
"00dc" 0X00000000 0Xxx Length4
第一字段 指定音视频
第二字段 关键帧 音频都置0X10000000,视频I帧置0X10000000,其他帧置0X00000000
第三字段 指定从"movi"开始的偏移 第一个指向"00dc" Length Video_Data (if Length % 2 == 1, we should add a byte 0x00 to the end of data for align)
第二个指向"00wb" Length Audio_Data (if Length % 2 == 1, we should add a byte 0x00 to the end of data for align)
第三个指向"00wb" Length Audio_Data
第四个指向"00dc" Length Video_Data
第四字段 数据长度,不包含对齐的那个字节