那位牛人知道AVI 文件的结构是如何组织的?一般常用的组织方式时,每个结构占多大字节?

解决方案 »

  1.   

    AVI文件就是我们所说的多媒体文件,所谓的AVI图像就是视频图像,该文件是一个RIFF说明文件,它用于获取、编辑、演示音频、视频序列。一般的AVI文件包含音频流和视频流,有的特殊的AVI还包含一个控制路径或MIDI路径作为附加的数据流。  现在播放AVI文件的软件很多,但大多无法从AVI视频文件中读取一帧图像并生成BMP格式的文件。笔者在使用AVI文件开发项目过程中对AVI文件的操作积累了一些经验,对于如何实现从AVI视频流中获取任意帧的图像数据并存储成BMP文件,其中最关键的是要从AVI文件中获取具体某一帧的图像数据,为此我利用Windows提供的API函数实现了自定义的CAvi类,用于操作AVI文件。  在使用API函数操作AVI文件时,一定要注意用AVIFileInit()来初始化AVI库,程序结束时用AVIFileExit()释放AVI库,否则API函数无法使用。现以操作包含真彩色图像的AVI文件为例,给出Cavi类的部分函数的具体实现,其中CaviCreate()函数用于读取AVI文件信息并初始化Cavi类的成员,例如根据AVI文件信息定义每帧图像的宽、高、每帧图像的信息头结构等等;函数AviRead(int mFrame)用于从AVI文件中读取第mFrame帧。实现代码显示如下://Cavi类头文件定义;
    class CAvi file://AVI类,处理AVI文件
    {
     public:
     int cy;//图象高
     int cx;//图象宽
     file://long m_maxFrame;
     BYTE *pData;//寸储图象数据
     BITMAPINFO *m_pBMI;//位图文件信息头
     PAVISTREAM pavi;//AVI流
     PAVIFILE pfile;//AVI文件指针
     AVIFILEINFO * pfi; file://AVI信息
     BOOL AviRead(int mFrame);//读AVI文件的第mFrame帧
     CAvi();//标准构造函数
     CAviCreate(CString &string);//用文件名初始化AVI类的成员
     virtual ~CAvi();
    }; 
    //Cavi类文件实现部分;
    CAvi::CAvi()
    { AVIFileInit();//初始化AVI库
     cx=0;//定义图象宽、高、等成员
     cy=0;
     m_pBMI=NULL;
     pData=NULL;
     file://m_maxFrame=0;
     pfi=NULL;
    }
    CAvi::~CAvi()//析构、释放指针
    {
     // AVIFileClose(pfile);
     AVIFileExit();
     if(pData!=NULL)
      delete pData;
      pData=NULL; if(m_pBMI!=NULL)
      delete m_pBMI;
      m_pBMI=NULL;
      if(pfi!=NULL)
       delete pfi;
       pfi=NULL;
    }
      

  2.   

    CAvi::CAviCreate(CString &string)//读文件初始化该类

     HRESULT hr;
     pfi=new AVIFILEINFO;
     hr = AVIFileOpen(&pfile, // returned file pointer
     string, // file name
     OF_READ, // mode to open file with
     NULL);
     hr= AVIFileInfo(pfile, file://获取AVI信息,放入pfi中
     pfi, 
     sizeof(AVIFILEINFO) 
    );
    cx=pfi->dwWidth;//图象宽、高
    cy=pfi->dwHeight;
    hr=AVIFileGetStream(//将AVI变成视频流
    pfile, 
    &pavi, 
    streamtypeVIDEO, 
    0//LONG lParam 
    );
    m_pBMI=new BITMAPINFO;//定义BMP信息头
    m_pBMI->bmiHeader.biBitCount=24;
    m_pBMI->bmiHeader.biClrImportant=0;
    m_pBMI->bmiHeader.biClrUsed=0;
    m_pBMI->bmiHeader.biCompression=BI_RGB;
    m_pBMI->bmiHeader.biHeight=cy;
    m_pBMI->bmiHeader.biWidth=cx;
    m_pBMI->bmiHeader.biPlanes=1;
    m_pBMI->bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
    m_pBMI->bmiHeader.biXPelsPerMeter=0;
    m_pBMI->bmiHeader.biYPelsPerMeter=0;
    m_pBMI->bmiHeader.biSizeImage=cx*cy*3;
    pData=(BYTE*)new char[cx*cy*3];//根据AVI中BMP图象的信息定义缓冲区
    }
    BOOL CAvi::AviRead(int mFrame)//将AVI文件的M帧数据读入PData缓冲区
    {
    HRESULT hr;
    hr= AVIStreamRead( pavi, 
    mFrame, 
    1, 
    pData, 
    cx*cy*3, 
    NULL, 
    NULL
    );
    if(hr==0)
    return TRUE;
    else
    return FALSE;
    }
     
      上述Cavi类实现部分所涉及到的API函数可以参考微软提供的MSDN。Cavi类中的pData缓冲区存放AVI文件中的具体某一帧图像数据,同时Cavi类的m_pBMI为BMP图像文件信息结构,这时可以根据图像的大小定义BMP图像文件头结构
      

  3.   

    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; 
     
    Members
    dwMaxBytesPerSec 
    Approximate maximum data rate of the AVI file. 
    dwFlags 
    Applicable flags. The following flags are defined: 
    AVIFILEINFO_HASINDEX 
    The AVI file has an index at the end of the file. For good performance, all AVI files should contain an index. 
    AVIFILEINFO_MUSTUSEINDEX 
    The file index contains the playback order for the chunks in the file. Use the index rather than the physical ordering of the chunks when playing back the data. This could be used for creating a list of frames for editing. 
    AVIFILEINFO_ISINTERLEAVED 
    The AVI file is interleaved. 
    AVIFILEINFO_WASCAPTUREFILE 
    The AVI file is a specially allocated file used for capturing real-time video. Applications should warn the user before writing over a file with this flag set because the user probably defragmented this file. 
    AVIFILEINFO_COPYRIGHTED 
    The AVI file contains copyrighted data and software. When this flag is used, software should not permit the data to be duplicated. 
    dwCaps 
    Capability flags. The following flags are defined: 
    AVIFILECAPS_CANREAD 
    An application can open the AVI file with with the read privilege. 
    AVIFILECAPS_CANWRITE 
    An application can open the AVI file with the write privilege. 
    AVIFILECAPS_ALLKEYFRAMES 
    Every frame in the AVI file is a key frame. 
    AVIFILECAPS_NOCOMPRESSION 
    The AVI file does not use a compression method. 
    dwStreams 
    Number of streams in the file. For example, a file with audio and video has at least two streams. 
    dwSuggestedBufferSize 
    Suggested buffer size, in bytes, for reading the file. Generally, this size should be large enough to contain the largest chunk in the file. For an interleaved file, this size should be large enough to read an entire record, not just a chunk. 
    If the buffer size is too small or is set to zero, the playback software will have to reallocate memory during playback, reducing performance. dwWidth 
    Width, in pixels, of the AVI file. 
    dwHeight 
    Height, in pixels, of the AVI file. 
    dwScale 
    Time scale applicable for the entire file. Dividing dwRate by dwScale gives the number of samples per second. 
    Any stream can define its own time scale to supersede the file time scale. dwRate 
    Rate in an integer format. To obtain the rate in samples per second, divide this value by the value in dwScale. 
    dwLength 
    Length of the AVI file. The units are defined by dwRate and dwScale. 
    dwEditCount 
    Number of streams that have been added to or deleted from the AVI file. 
    szFileType 
    Null-terminated string containing descriptive information for the file type. 
    到MSDN中找,多的是