zz 结合avi视频文件头十六进制码分析AVI格式
这段时间要搞视频合成方面的东西,因此打算先弄明白avi视频的格式,介绍这方面内容的东西google一下一大堆,可是很悲剧,现在的人基本上都喜欢copy,内容基本来源于同一篇,对我没什么实质性的帮助。本来是希望通过看看一些avi合成的源码来分析avi的结构的,可是windows下的程序基本是调用directshow完成的,格式方法都封装好了。
百思不得其解之余,还只自己分析吧。
开始之前,先看看avi的文件格式图:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0123456789012345]
000000000: 52 49 46 46 DC 6C 57 09 41 56 49 20 4C 49 53 54 |RIFF.lW.AVI LIST|
000000016: CC 41 00 00 68 64 72 6C 61 76 69 68 38 00 00 00 |.A..hdrl avih8...|e
000000032: 50 C3 00 00 00 B0 04 00 00 00 00 00 10 00 00 00 |P...............| c
000000048: A8 02 00 00 00 00 00 00 01 00 00 00 00 84 03 00 |................|
000000064: 40 01 00 00 F0 00 00 00 00 00 00 00 00 00 00 00 |@...............|
000000080: 00 00 00 00 00 00 00 00 4C 49 53 54 74 40 00 00 |........LISTt@..|
000000096: 73 74 72 6C 73 74 72 68 38 00 00 00 76 69 64 73 |str l strh 8... vids|
000000112: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
000000128: 64 00 00 00 D0 07 00 00 00 00 00 00 A8 02 00 00 |d...............|
000000144: 00 84 03 00 10 27 00 00 00 00 00 00 00 00 00 00 |.....'..........|
000000160: 40 01 F0 00 73 74 72 66 28 00 00 00 28 00 00 00 |@...strf (...(...|
000000176: 40 01 00 00 F0 00 00 00 01 00 18 00 00 00 00 00 |@...............|
000000192: 00 84 03 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
000000208: 00 00 00 00 69 6E 64 78 F8 3F 00 00 04 00 00 00 |.... indx.?......|
000000224: 01 00 00 00 30 30 64 62 00 00 00 00 00 00 00 00 |....00db........|
000000240: 00 00 00 00 0C 44 00 00 00 00 00 00 00 40 00 00 |.....D.......@..|
.
.
000017408: 4C 49 53 54 38 F9 56 09 6D 6F 76 69 69 78 30 30 |LIST8.V.moviix00|
000017424: F8 3F 00 00 02 00 00 01 A8 02 00 00 30 30 64 62 |.?..........00db|
"avih"子块:
typedefstruct_avimainheader{
FOURCC fcc;//必须为‘avih’
DWORD cb;//本数据结构的大小,不包括最初的8个字节(fcc和cb两个域)structsize
DWORD dwMicroSecPerFrame;//视频帧间隔时间(以毫秒为单位)
DWORD dwMaxBytesPerSec;//这个AVI文件的最大数据率
DWORD dwPaddingGranularity;//数据填充的粒度
DWORD dwFlags;//AVI文件的全局标记,比如是否含有索引块等
DWORD dwTotalFrames;//总帧数
DWORD dwInitialFrames;//为交互格式指定初始帧数(非交互格式应该指定为0)
DWORD dwStreams;//本文件包含的流的个数
DWORD dwSuggestedBufferSize;//建议读取本文件的缓存大小(应能容纳最大的块)
DWORD dwWidth;//视频图像的宽(以像素为单位)
DWORD dwHeight;//视频图像的高(以像素为单位)
DWORD dwReserved[4];//保留
}AVIMAINHEADER;
- [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0123456789012345]
- 000000000: 52 49 46 46 DC 6C 57 09 41 56 49 20 4C 49 53 54 |RIFF.lW.AVI LIST| 【RIFF fileSize AVI LIST】
- 000000016: CC 41 00 00 68 64 72 6C 61 76 69 68 38 00 00 00 |.A..hdrlavih8...| 【listSize listType avih 结构大小】
- 000000032: 50 C3 00 00 00 B0 04 00 00 00 00 00 10 00 00 00 |P...............| 【帧间时间 最大数据率 填充粒度 全局标记
- 000000048: A8 02 00 00 00 00 00 00 01 00 00 00 00 84 03 00 |................| 【总帧数 交互帧数 流个数 建议缓存
- 000000064: 40 01 00 00 F0 00 00 00 00 00 00 00 00 00 00 00 |@...............| 【width height 保留
- 000000080: 00 00 00 00 00 00 00 00
"strh"子块:
typedefstruct_avistreamheader{
FOURCC fcc;//必须为‘strh’
DWORD cb;//本数据结构的大小,不包括最初的8个字节(fcc和cb两个域)
FOURCC fccType;//流的类型:‘auds’(音频流)、‘vids’(视频流)、
//‘mids’(MIDI流)、‘txts’(文字流)
FOURCC fccHandler;//指定流的处理者,对于音视频来说就是解码器
DWORD dwFlags;//标记:是否允许这个流输出?调色板是否变化?
WORD wPriority;//流的优先级(当有多个相同类型的流时优先级最高的为默认流)
WORD wLanguage;
DWORD dwInitialFrames;//为交互格式指定初始帧数
DWORD dwScale;//这个流使用的时间尺度
DWORD dwRate;
DWORD dwStart;//流的开始时间
DWORD dwLength;//流的长度(单位与dwScale和dwRate的定义有关)
DWORD dwSuggestedBufferSize;//读取这个流数据建议使用的缓存大小
DWORD dwQuality;//流数据的质量指标(0~10,000)
DWORD dwSampleSize;//Sample的大小
struct{
shortint left;
shortint top;
shortint right;
shortint bottom;
}rcFrame;//指定这个流(视频流或文字流)在视频主窗口中的显示位置
//视频主窗口由AVIMAINHEADER结构中的dwWidth和dwHeight决定
}AVISTREAMHEADER;
4C 49 53 54 74 40 00 00 |........LISTt@..| LIST listSize
- 000000096: 73 74 72 6C 73 74 72 68 38 00 00 00 76 69 64 73 |str lstrh8...vids| 【strl ,strh, 结构大小 ,流类型(vids)
- 000000112: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 【流的处理者dwFlags, wPriority ,wLanguage交互帧数
- 000000128: 64 00 00 00 D0 07 00 00 00 00 00 00 A8 02 00 00 |d...............|
- 000000144: 00 84 03 00 10 27 00 00 00 00 00 00 00 00 00 00 |.....'..........|
- 000000160: 40 01 F0 00
"strf"子块:
typedef struct tagBITMAPINFO
{
BITMAPINFOHEADER bmiHeader;
RGBQUAD bmiColors[1]; //颜色表
}BITMAPINFO;
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;
73 74 72 66 28 00 00 00 28 00 00 00 | @...strf(...(...| strf
000000176: 40 01 00 00 F0 00 00 00 01 00 18 00 00 00 00 00 |@...............|
000000192: 00 84 03 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
000000208: 00 00 00 00
Index子块:
typedefstruct_avioldindex{
FOURCCfcc;//必须为‘idx1’
DWORDcb;//本数据结构的大小,不包括最初的8个字节(fcc和cb两个域)
struct_avioldindex_entry{
DWORDdwChunkId;//表征本数据块的四字符码
DWORDdwFlags;//说明本数据块是不是关键帧、是不是‘rec’列表等信息
DWORDdwOffset;//本数据块在文件中的偏移量
DWORDdwSize;//本数据块的大小
}aIndex[];//这是一个数组!为每个媒体数据块都定义一个索引信息
}AVIOLDINDEX;
69 6E 64 78 F8 3F 00 00 04 00 00 00 |....indx.?......| indx
000000224: 01 00 00 00 30 30 64 62 00 00 00 00 00 00 00 00 |....00db........|
000000240: 00 00 00 00 0C 44 00 00 00 00 00 00 00 40 00 00 |.....D.......@..|
000017408: 4C 49 53 54 38 F9 56 09 6D 6F 76 69 69 78 30 30 |LIST8.V.moviix00|LISTlistSize listType indexBlock ( ix00 )
000017424: F8 3F 00 00 02 00 00 01 A8 02 00 00 30 30 64 62 |.?..........00db| .... 00db ( uncompressed video frame )