为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

avi-api

2011-11-11 11页 doc 52KB 101阅读

用户头像

is_359887

暂无简介

举报
avi-apiAVI有关函数 2010-12-17 08:50 http://blog.ednchina.com/opencv2008/195506/message.aspx AVI文件和BMP文件在结构上似乎很相近 1.AVIFileInfo() The AVIFileInfo function obtains information about an AVI file. STDAPI AVIFileInfo(   PAVIFILE pfile,  //  Handle of an open AVI file.   AVIFILE...
avi-api
AVI有关函数 2010-12-17 08:50 http://blog.ednchina.com/opencv2008/195506/message.aspx AVI文件和BMP文件在结构上似乎很相近 1.AVIFileInfo() The AVIFileInfo function obtains information about an AVI file. STDAPI AVIFileInfo(   PAVIFILE pfile,  //  Handle of an open AVI file.   AVIFILEINFO * pfi,  //Address of the structure used to return file information.   LONG lSize         // Size, in bytes, of the structure. );   2.结构 AVIFILEINFO The AVIFILEINFO structure contains global information for an entire AVI file. typedef struct {     DWORD dwMaxBytesPerSec;     DWORD dwFlags;     DWORD dwCaps;     DWORD dwStreams;     DWORD dwSuggestedBufferSize;     DWORD dwWidth;     DWORD dwHeight;     DWORD dwScale;     DWORD dwRate;     DWORD dwLength;     DWORD dwEditCount;     char  szFileType[64]; } AVIFILEINFO; AVISTREAMINFO The AVISTREAMINFO structure contains information for a single stream. typedef struct {     DWORD fccType;     DWORD fccHandler;     DWORD dwFlags;     DWORD dwCaps;     WORD  wPriority;     WORD  wLanguage;     DWORD dwScale;     DWORD dwRate;     DWORD dwStart;     DWORD dwLength;     DWORD dwInitialFrames;     DWORD dwSuggestedBufferSize;     DWORD dwQuality;     DWORD dwSampleSize;     RECT  rcFrame; //Dimensions of the video destination rectangle.                    //The values represent the coordinates of upper left corner,                    // the height, and the width of the rectangle.     DWORD dwEditCount;     DWORD dwFormatChangeCount;     char  szName[64]; } AVISTREAMINFO;    AVICOMPRESSOPTIONS The AVICOMPRESSOPTIONS structure contains information about a stream and how it is compressed and saved. This structure passes data to the AVIMakeCompressedStream function (or the AVISave function, which uses AVIMakeCompressedStream). typedef struct {     DWORD  fccType;     DWORD  fccHandler;     DWORD  dwKeyFrameEvery;     DWORD  dwQuality;     DWORD  dwBytesPerSecond;     DWORD  dwFlags;     LPVOID lpFormat;     DWORD  cbFormat;     LPVOID lpParms;     DWORD  cbParms;     DWORD  dwInterleaveEvery; } AVICOMPRESSOPTIONS; 3.AVIFileInit在使用其他AVI函数前,使用它初始化AVI函数库 The AVIFileInit function initializes the AVIFile library. The AVIFile library maintains a count of the number of times it is initialized, but not the number of times it was released. Use the AVIFileExit function to release the AVIFile library and decrement the reference count. Call AVIFileInit before using any other AVIFile functions. This function supersedes the obsolete AVIStreamInit function. 4. AVIFileOpen The AVIFileOpen function opens an AVI file and returns the address of a file interface used to access it. The AVIFile library maintains a count of the number of times a file is opened, but not the number of times it was released. Use the AVIFileRelease function to release the file and decrement the count. STDAPI AVIFileOpen(   PAVIFILE * ppfile,       LPCTSTR szFile,          UINT mode,               CLSID * pclsidHandler  ); AVIFileCreateStream The AVIFileCreateStream function creates a new stream in an existing file and creates an interface to the new stream. STDAPI AVIFileCreateStream(   PAVIFILE pfile,        PAVISTREAM * ppavi,    AVISTREAMINFO * psi  ); This function starts a reference count for the new stream. AVIMakeCompressedStream The AVIMakeCompressedStream function creates a compressed stream from an uncompressed stream and a compression filter, and returns the address of a pointer to the compressed stream. This function supports audio and video compression. STDAPI AVIMakeCompressedStream(   PAVISTREAM * ppsCompressed,  //Address to contain the compressed stream pointer.   PAVISTREAM psSource,           AVICOMPRESSOPTIONS * lpOptions, //   Address of a structure that identifies the type of compression to use and the options to apply   CLSID * pclsidHandler    //Address of a class identifier used to create the stream ); AVISaveOptions The AVISaveOptions function retrieves the save options for a file and returns them in a buffer. BOOL AVISaveOptions(   HWND hwnd,        //vHandle of the parent window for the Compression Options dialog box.     UINT uiFlags,     //Flags for displaying the Compression Options dialog box.   int nStreams,   //                     PAVISTREAM * ppavi,  //Address of an array of stream interface pointers                LPAVICOMPRESSOPTIONS * plpOptions  //Address of an array of pointers to AVICOMPRESSOPTIONS                                      //structures. These structures hold the compression options                                      //set by the dialog box. The nStreams parameter indicates                                      //the number of pointers in the array. ); AVIStreamSetFormat  //设置视频流格式 The AVIStreamSetFormat function sets the format of a stream at the specified position. STDAPI AVIStreamSetFormat(   PAVISTREAM pavi,  //Handle of an open stream.   LONG lPos,        //Position in the stream to receive the format.   LPVOID lpFormat,  //Position in the stream to receive the format.   LONG cbFormat     //Size, in bytes, of the block of memory referenced by lpFormat. ); AVIStreamWrite The AVIStreamWrite function writes data to a stream. STDAPI AVIStreamWrite(   PAVISTREAM pavi,  //Handle of an open stream.         LONG lStart,         //First sample to write    LONG lSamples,         //Number of samples to write   LPVOID lpBuffer,//Address of a buffer containing the data to write   LONG cbBuffer,         //Size of the buffer referenced by lpBuffer.   DWORD dwFlags,           LONG * plSampWritten,  //Address to contain the number of samples written. This can be set to NULL.   LONG * plBytesWritten  //Address to contain the number of bytes written. This can be set to NULL. );     界面更新 CCmdUI does not have a base class. The CCmdUI class is used only within an ON_UPDATE_COMMAND_UI handler in a CCmdTarget-derived class. When a user of your application pulls down a menu, each menu item needs to know whether it should be displayed as enabled or disabled. The target of a menu command provides this information by implementing  an ON_UPDATE_COMMAND_UI handler. Use ClassWizard to browse the command user-intetrface objects in your  application and create a message-map entry and function prototype for each handler. When the menu is pulled down, the framework searches for and calls each ON_UPDATE_COMMAND_UI handler, each handler calls CCmdUI member functions such as Enable and Check, and the framework then appropriately displays each menu item. A menu item can be replaced with a control-bar button or other command user-interface objec without changing the code within the ON_UPDATE_COMMAND_UI handler.   When it routes an update command to its handler, the framework passes the handler a pointer to a CCmdUI object (or to an object of a CCmdUI-derived class). This object represents the menu item  or toolbar button or other user-interface object that generated the command. The update handler   calls member functions of the CCmdUI structure through the pointer to update the user-interface    object. For example, here is an update handler for the Clear All menu item: void CMyClass::OnUpdateToolsMyTool( CCmdUI* pCmdUI ) {     if( ToolAvailable() )         pCmdUI->Enable( TRUE ); } This handler calls the Enable member function of an object with access to the menu item. Enable makes the item available for use. CCmdUI::Enable virtual void Enable( BOOL bOn = TRUE ); TRUE to enable the item, FALSE to disable it. Remarks: Call this member function to enable or disable the user-interface item for this command. CCmdUI::SetCheck virtual void SetCheck( int nCheck = 1 ); Specifies the check state to set. If 0, unchecks; if 1, checks; and if 2, sets indeterminate. Remarks: Call this member function to set the user-interface item for this command to the appropriate check state. This member function works for menu items and toolbar buttons. The indeterminate state applies only to toolbar buttons. memset Sets buffers to a specified character. void *memset( void *dest,//Pointer to destination              int c,      //Character to set              size_t count              ); returns the value of dest. AVI应用实例 AVI文件格式 Microsoft 公司的Audio-Video Interleaved(AVI) 文件格式是一个RIFF文件说明, 可以用于捕捉、编辑及回放音频/视频序列。AVI文件可以包含多个不同类型的数据流,包 括音频流、视频流等,有的还有控制路径(Control track ) 或MIDI 路径等附加数据流。 另外,AVI标准是一种软件辅助的数字化视频压缩技术,使用它可以快速地从数字存储设备 中解压缩图像。本文将仅对视频数据流作一介绍。 AVI 文件使用的是AVI RIFF格式,AVI RIFF格式由字串"AVI"标识,所有的AVI文件都 包含两个必需的LIST块,这些块定义了流和数据流的格式。AVI文件可能还包含一个索引 块,这个任选块给出了文件中这些数据的地址,如下所示: RIFF ( ‘AVI' LIST (‘hdrl' ‘avih'() LIST (‘strl' ‘strh' () ‘strf' () ‘strd' () ‘strn' () ... ... ) ... ... ) LIST ( ‘movi' (subchunk | LIST (‘rec'   subchunk1   subchunk2   ... ...   ) ... ... ) ... ... ) [‘idxl' ] ) 其中LIST块的索引块都是RIFF ‘AVI’的子块,‘AVI’块标识了此文件是一个AVIR IFF文件。LIST块‘hdrl’定义了数据的格式,它是必须出现的LIST块中的第一块;LIST块 ‘movi’包含了‘AVI’序列的数据,是必须出现的LIST块中的第二块;‘idxl’是可选的 索引块。AVI文件中的这三项必须按适当的次序排列,下面分别介绍。 1. LIST块 ‘hdrl’ (1) 主AVI头标LIST 文件是以主头标(main header)开始的。在AVI文件中,头标用‘avih' 字串标识。头 标定义了有关此文件的一般信息,比如文件中的流块数、AVI序列的宽、高等。主头标用 以下数据结构定义: typedef struct { DWORD dwTotalFrames; //总帧数 DWORD dwStreams; //总流数 DWORD dwWidth; DWORD dwHeight; //视频图像的宽、高 ... ... } MainAVIHeader; (2) ‘strl’流头块 主头标后紧跟的是一个或多个‘strl’块(每个数据流都需要一个‘strl'块),这些 块包含了文件中有关流的信息。每一个‘strl’块都需要一个流头和流格式块。流头块 由字串‘strl’标识,流格式块用字串‘strf’标识。除了流头和流格式块,‘strl’块 还可能有流数据块,流数据块使用‘strd’字串标识。AVIStreamHeader结构说明了流中 包含的数据类型,例如视频或音频等,其定义如下: typedef struct { FOURCC fccType; FOURCC fccHandler; ... ... } AVIStreamHeader; 在说明数据类型时使用字串定义的方式,如果流包含视频数据,则把fccType定义为‘ vids’;如果包含音频数据,则为‘auds’。另外,fccHandler域包含了一个长度为4个字 符的代码,该字串描述对数据所用的压缩及解码程序。 2. LIST块‘movi’ 在头标信息之后是‘movi’LIST块,它包含了流中实际的数据块,即声音和图像数据 本身。这些数据块能直接驻留在LIST块‘movi’中,也可以被驻留到‘rec’块中。‘re c’块是指那些从磁盘上依次读取的组块。像任何RIFF块一样,数据块也包含了一个字串 以定义块的类型。标识每个块的字串(4个字符)由流的个数以及一个用来定义块中被压缩 信息类型的字串(2个字符)组成。例如,一个波形块(waveform chunk)被定义为"wb",如果 这波形块与第二个LIST块‘hdrl’流描述一致,则它将有一个字串"01wb"。 由于所有的格式信息都在头标中,所以在这些数据块中的音频数据就不包含关于格式 的信息,音频数据有以下格式的形式(格式中的"##"表示流标识符): WAVE Bytes ‘##wb' BYTE  abBytes[] 视频数据可以是压缩或非压缩的DIB形式,将与BITMAPINFO结构相关的biCompressio n域的值置为BI_RGB,则表明视频数据是非压缩的DIB格式。未被压缩的DIB数据块包含RG B视频数据,这些块用字串‘db’标识,被压缩的DIB数据块用字串‘ dc '标识。这些块中 都不包含关于DIB的头标信息。非压缩的DIB数据块有以下形式: DIB Bits "##db" BYTE abBits[] 压缩的DIB数据块有以下形式: Compressd DIB "##dc" BYTE abBits[] 通过‘strl’块的顺序关系,一个AVI播放器把LIST ‘hdrl' 块中的流头(‘strl’ )和LIST ‘movi’块中的数据联系起来,第一个‘strl’块对应于第0个流,第二个‘str l’块对应于第1个流,依次类推。例如,如果第一个‘strl’块描述的是Wave Audio数据 ,则该波形数据包含在第0个流中。 3. 索引块‘idxl’ 在LIST‘movi’块之后,有一个索引块,索引块包含了一个数据块的列表及他们在文 件中的位置。它提供了在AVI文件中随机存取数据的有效方法,使得一个应用程序可以不 必扫描整个AVI文件而随机定位到某个特定的音频/视频序列。索引块用四个字串‘idxl ’来标识。 AVI文件处理流程 常用的AVI相关操作包括读、写及显示,其流程大致如下: @@0569300.JPG;图1@@ 笔者采用面向对象的程序设计思想,编制了一个CAvi类,其行为涵盖了关于AVI文件的 大部分操作。类的定义如下: class Cavi { public:   CAvi(LPCSTR fileName ); //Constructon Function.   ~CAvi(); //Destruction Function. public:   LPCSTR AviFileName;   //AVI FileName   int Height, Width; //AVI 视频图像的宽和高   LONG lSize; //AVI 视频图像的帧数   PAVIFILE gpfile; //AVI 文件指针   PAVISTREAM gapavi; //AVI 流指针   PGETFRAME gapgf;  //AVI GetFrame 对象   AVISTREAMINFO avis;  //AVI 流头信息   BYTE abFormat[1024];  //AVI 流格式信息 public:   int hAviVideoOpen(LPCSTR szFile);  //打开AVI文件  int hAviVideoReadOneFrame(Your Data Pointer to AVI Video Data,LONG lCu rFrame);  //读取某帧数据   void hFreeAvi();   //释放 AVI 文件   void hAviVideoDrawOneFrame(LONG lCurFrame); }; CAvi::CAvi(LPCSTR fileName) { hAviVideoOpen(fileName); }; CAvi::~CAvi() { hFreeAvi(); }; int CAvi:: hAviVideoOpen(LPCSTR szFile) { ... ... AVIFileInit(); //初始化AVIFile库 AVIFileOpen(&gpfile,AviFileName,0,0L); //获取文件指针 if ( AVIFileGetStream(gpfile,& gapavi, streamtypeVIDEO,0)!=0) { //Error}  //获取流指针 AVIStreamInfo(gapavi,&avis,sizeof(avis)); //获取流头信息 AVIStreamReadFormat(gapavi,0,&abFormat,&sizeof(avis)); //获取流格式 Height = (LPBITMATINFOHEADER)abFormat->biHeight; Width = (LPBITMATINFOHEADER)abFormat->biWidth; //获取宽和高 if(avis.fccType == StreamtypeVIDEO) apgf = AVIStreamGetFrameOpen(gapavi,NULL);  //获取一个GetFrame对象,预备读数据 ... ... } int CAvi::hAviVideoReadOneFrame(YourDataPointerto AVIVideoData, LONG lCu rFrame) { ... ... if(avis.fcctype==StreamtypeVIDEO && !gapgf )   lpbi = (LPBITMAPINFOHEADER)abFormat; if(lCurrent>AVIStreamStart(gapavi))   lpbi = (LPBITMAPINFOHEADER)AVIStreamGetFrame (gapgf,lCurrent); else { //error. } BYTE imageData = (BYTE huge *)(lpbi+1); for(int row = Height-1; row>=0; row++)   for(int col = =;colMWidth;col++) { //将视频数据读入自己的内存区中。} ... ... } void CAvi::hFreeAvi() { AVIStreamGetFrameClose(gapgf); //关闭帧 AVIStreamRelease(gapavi); //释放帧 AVIFileRelease(gpfile); //释放AVI文件 AVIFileExit();   //退出AVIFile库 } void CAvi::hAviVideoDrawOneFrame(LONG lCurFrame) { HDRAWDIB h = DrawDibOpen(); CAvi::hAviVideoDrawOneFrame(lCurFrame) ; DrawDibDraw(... ...); DrawDibClose( h ); }; 几点注意 首先,在编制AVI应用程序时,通常采用两种:其一是利用Video for Windows软件 ,它提供了关于AVI文件的读、写以及绘制操作的函数库,程序设计人员可以在自己的项 目中加入相应的静态库,如AviFile.Lib、Vfw.lib等,并在调用AVI库函数时增加相应函数 声明;另一种方法是利用Visual C++自带的AVI函数库。由于Microsoft 公司已将AVI函数 库嵌入Visual C++ 4.x平台中,程序员只要将VFW32.lib加入项目中,就可以方便地编制3 2位的AVI程序。两者相比,后者更方便,程序运行速度更快。 其次,AVIFileInit函数用来初始化AVI函数库,是使用AVI函数库而调用的第一个函数 ,它将AVI库引用的次数加一。相应地,AVIFileExit将AVI库引用的次数减一,如果该次数 为零,则释放AVI库,它是使用AVI函数库而调用的最后一个函数。注意,这两个函数必须成 对调用,否则AVI文件将被破坏,或造成死机。如果将AVIFileInit函数放在CAvi类的构造 函数中,将AVIFileExit放在CAvi类的析构函数中,则当同时处理多个AVI文件,即多个CAv i类对象时,很容易造成AVIFileInit和AVIFileExit调用的不匹配。基于此,笔者建议,在 整个应用程序的最前面调用AVIFileInit函数,初始化AVI库;在整个应用程序的最后面调 用AVIFileExit函数,释放AVI函数库。这样,即使处理多个AVI文件,也不会出问题。 最后,AVI文件中的视频数据可以是8、16、24、32位,这和一般的BMP文件是相似的, 如24位的数据格式是:R、G、B分量各占8位。读者应具体问题具体分析。
/
本文档为【avi-api】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
热门搜索

历史搜索

    清空历史搜索