AVI封装

http://blog.chinaunix.net/uid-27091949-id-4088330.html

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;
///////////////////////////////////////////////////////////////////////////////////////////

"JUNK"
0X00          //JUNK length 长度计算方式为使下一个LIST开始于16的倍数的地址
0X00...       //length长度个字节,垃圾信息
//list0 长度计算到这里,avi头部到此结束

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
"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
第四字段 数据长度,不包含对齐的那个字节

00000000   52 49 46 46  00 00 00 00  41 56 49 20  4C 49 53 54  RIFF....AVI LIST
00000010   3C 01 00 00  68 64 72 6C  61 76 69 68  38 00 00 00  <...hdrlavih8...
00000020   00 00 00 00  00 00 00 00  00 00 00 00  10 09 00 00  ................
00000030   00 00 00 00  00 00 00 00  02 00 00 00  00 00 10 00  ................
00000040   80 07 00 00  38 04 00 00  00 00 00 00  00 00 00 00  ....8...........
00000050   00 00 00 00  00 00 00 00  4C 49 53 54  74 00 00 00  ........LISTt...
00000060   73 74 72 6C  73 74 72 68  38 00 00 00  76 69 64 73  strlstrh8...vids
00000070   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................
00000080   01 00 00 00  19 00 00 00  00 00 00 00  00 00 00 00  ................
00000090   00 00 10 00  FF FF FF FF  00 00 00 00  00 00 00 00  ................
000000A0   80 07 38 04  73 74 72 66  28 00 00 00  28 00 00 00  ..8.strf(...(...
000000B0   80 07 00 00  38 04 00 00  01 00 18 00  48 32 36 34  ....8.......H264
000000C0   00 EC 5E 00  00 00 00 00  00 00 00 00  00 00 00 00  ..^.............
000000D0   00 00 00 00  4C 49 53 54  5E 00 00 00  73 74 72 6C  ....LIST^...strl
000000E0   73 74 72 68  38 00 00 00  61 75 64 73  00 00 00 00  strh8...auds....
000000F0   00 00 00 00  00 00 00 00  00 00 00 00  01 00 00 00  ................
00000100   40 1F 00 00  00 00 00 00  00 00 00 00  00 30 00 00  @............0..
00000110   FF FF FF FF  01 00 00 00  00 00 00 00  00 00 00 00  ................
00000120   73 74 72 66  12 00 00 00  07 00 01 00  40 1F 00 00  strf........@...
00000130   40 1F 00 00  01 00 08 00  00 00 4A 55  4E 4B 0E 00  @.........JUNK..
00000140   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................
00000150   4C 49 53 54  CA 91 5B 09  6D 6F 76 69  30 30 64 63  LIST..[.movi00dc
00000160   D2 67 02 00  00 00 00 01  67 64 00 28  AD 84 05 45  .g......gd.(...E
......................................
095B9310   0D 52 D0 DB  2B 82 73 01  6E B1 5B E5  D7 0B 1A AC  .R..+.s.n.[.....
095B9320   67 10 69 64  78 31 30 B3  00 00 30 30  64 63 10 00  g.idx10...00dc..
095B9330   00 00 04 00  00 00 D2 67  02 00 30 30  64 63 10 00  .......g..00dc..
095B9340   00 00 DE 67  02 00 62 03  00 00 30 30  64 63 00 00  ...g..b...00dc..
095B9350   00 00 48 6B  02 00 B1 DF  00 00 30 30  64 63 00 00  ..Hk......00dc..
095B9360   00 00 02 4B  03 00 84 D8  00 00 30 30  64 63 00 00  ...K......00dc..
095B9370   00 00 8E 23  04 00 C5 9A  01 00 30 30  64 63 00 00  ...#......00dc..
095B9380   00 00 5C BE  05 00 2C AA  01 00 30 30  64 63 00 00  ..\...,...00dc..
095B9390   00 00 90 68  07 00 AE 7C  01 00 30 30  64 63 00 00  ...h...|..00dc..
095B93A0   00 00 46 E5  08 00 F3 78  01 00 30 30  64 63 00 00  ..F....x..00dc..
095B93B0   00 00 42 5E  0A 00 C7 18  01 00 30 30  64 63 00 00  ..B^......00dc..
095B93C0   00 00 12 77  0B 00 10 2A  01 00 30 30  64 63 00 00  ...w...*..00dc..
095B93D0   00 00 2A A1  0C 00 62 03  00 00 30 30  64 63 00 00  ..*...b...00dc..








参考文档:http://msdn.microsoft.com/en-us/library/windows/desktop/dd318189(v=vs.85).aspx
参考文档:http://www.alexander-noe.com/video/documentation/avi.pdf
Easiest way to install: Load the program group AviDemo.bpg, install the package AviPack.dpk(bpl), try the demos. Read the source of AviWriter_2.pas (in AviPack) to get help on what the procedures and properties do. **Current version: AviWriter_2 ver 1.0.0.4 Changes: Finally got On-the-fly compression working with still being able to add an audio-stream. Use property OnTheFlyCompression (default is true) Also, now more than one audio file can be added. For each wav-file a delay (ms) can be specified, which says when it'll start playing. Use method AddWaveFile. In 1.0.0.3 the delay got too short. Now it seems to work, due to really adding "silence" from the end of the previous audio file. Note: Some Codecs don't support On-the-fly compression. If OnTheFlyCompression is false, only one wave file can be added. **A list of codec-gotchas: (still unclear about the exact range of occurrance) IV50 Indeo Video 5: Both frame dimensions must be a factor of 4. DIVX DivX codec: Both frame dimensions must be a factor of 4. Gives a floating point error under certain circumstances. More and more likely that this occurs if the frames are too "different". If this happens, then there's an AV in Avifil32.dll, don't know how to prevent this. The codec compresses real movies nicely at frametimes <=60 ms, when changing the settings in its dialog box as follows: Bitrate (1st page): to >=1300 Max Keyframe interval (2nd page): to <=20. MRLE MS-RLE Use that one if you want to make avis which play transparently in a TAnimate. (Thanks Eddie Shipman) But it does not support on-the-fly compression. Whenever a codec fails to work, there's this dreaded error "A call to an operating system function failed" while writing a compressed file, or an unhandled exception. The only way to prevent it, that I see, is, to collect more peculiarities about the codecs and stop execution in case of certain combinations of settings. When queried about their capabilities, some of these guys seem to lie. Renate Schaaf renates@xmission.com
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值