AVI.cpp
资源名称:视频会议系统.rar [点击查看]
上传用户:popouu88
上传日期:2013-02-11
资源大小:2894k
文件大小:5k
源码类别:
IP电话/视频会议
开发平台:
Visual C++
- // AVI.cpp: implementation of the CAVI class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "....stdafx.h"
- #include "AVI.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CAVI::CAVI( )
- {
- this->m_avi_file = NULL;
- this->m_avi_stream = NULL;
- this->read_frame = 0;
- this->avi_buffer = NULL;
- this->m_bMode = 0;
- this->write_frame = 0;
- this->time_interval = 0;
- this->time_now = 0;
- memset( &m_avi_stream_info , 0 , sizeof( AVISTREAMINFO ) );
- memset( &m_avi_bmp_info , 0 , sizeof( BITMAPINFOHEADER ) );
- }
- CAVI::~CAVI( )
- {
- this->CloseAVI( );
- }
- BOOL CAVI::OpenAVI( LPCSTR filename ,DWORD openmode )
- {
- HRESULT hr;
- if( openmode == AVI_WRITE )
- { //线程内打开出错
- hr = ::AVIFileOpen( &this->m_avi_file , filename , OF_CREATE | OF_WRITE , NULL );
- //打开文件失败
- if( hr != AVIERR_OK || this->m_avi_file == NULL )
- return !this->CloseAVI( );
- }
- else if( openmode == AVI_READ )
- {
- hr = ::AVIFileOpen( &this->m_avi_file , filename , OF_SHARE_DENY_NONE | OF_READ , NULL );
- //打开文件失败
- if( hr != AVIERR_OK || this->m_avi_file == NULL )
- return !this->CloseAVI( );
- //创建流
- hr = ::AVIFileGetStream( this->m_avi_file , &this->m_avi_stream , streamtypeVIDEO , 0 );
- if( hr != AVIERR_OK )
- return ! this->CloseAVI( );
- //读取avi图像信息
- memset( &this->m_avi_stream_info , 0 ,sizeof( this->m_avi_stream_info ) );
- hr = ::AVIStreamInfo( this->m_avi_stream , &this->m_avi_stream_info , sizeof( AVISTREAMINFO ) );
- if( hr != AVIERR_OK )
- return !this->CloseAVI( );
- //读取位图信息
- long size=sizeof( BITMAPINFOHEADER );
- hr = ::AVIStreamReadFormat( this->m_avi_stream , 0 , &this->m_avi_bmp_info , &size );
- if( hr != AVIERR_OK )
- return !this->CloseAVI( );
- //申请内存
- this->avi_buffer = new char[ this->m_avi_bmp_info.biSizeImage ];
- ASSERT( this->avi_buffer != NULL );
- this->read_frame = 0;
- }
- this->m_bMode = openmode;
- return TRUE;
- }
- BOOL CAVI::CloseAVI(void)
- {
- this->m_bMode = 0;
- if( this->m_avi_stream )
- {
- ::AVIStreamRelease( this->m_avi_stream );
- this->m_avi_stream = NULL;
- }
- if( this->m_avi_file )
- {
- ::AVIFileRelease( this->m_avi_file );
- this->m_avi_file=NULL;
- }
- if( this->avi_buffer )
- {
- delete [ ]this->avi_buffer;
- this->avi_buffer = NULL;
- }
- this->codec.ReleaseEncode( );
- this->read_frame = 0;
- this->write_frame = 0;
- this->time_interval = 0;
- this->time_now = 0;
- return TRUE;
- }
- BOOL CAVI::SetAVIInfo( BITMAPINFOHEADER header , DWORD rate )
- {
- HRESULT hr;
- if( this->m_bMode != CAVI::AVI_WRITE || this->m_avi_file == NULL)
- return FALSE;
- memcpy( &this->m_avi_bmp_info , &header , sizeof( BITMAPINFOHEADER ) );
- //设置流信息
- memset( &this->m_avi_stream_info , 0 ,sizeof( AVISTREAMINFO ) );
- this->m_avi_stream_info.fccType = streamtypeVIDEO;//视频流类型
- this->m_avi_stream_info.fccHandler = header.biCompression; //压缩方式
- this->m_avi_stream_info.dwScale = 1; // 对于视频,取值为 1
- this->m_avi_stream_info.dwRate = rate; // 帧速率
- this->m_avi_stream_info.dwSuggestedBufferSize = header.biSizeImage; // 推荐的图像大小
- SetRect( &this->m_avi_stream_info.rcFrame , 0 , 0 , header.biWidth , header.biHeight );// 图形矩形大小
- // 创建视频流
- hr = AVIFileCreateStream( this->m_avi_file , &this->m_avi_stream , &this->m_avi_stream_info );
- if( hr != AVIERR_OK )
- return !this->CloseAVI( );
- //写流格式
- hr = ::AVIStreamSetFormat( this->m_avi_stream , 0 , &this->m_avi_bmp_info ,sizeof( BITMAPINFOHEADER ) );
- if( hr != AVIERR_OK )
- return ! this->CloseAVI( );
- //当前写入帧数
- this->write_frame = 0;
- this->time_interval = int ( 1000.0 / rate + 0.5 );
- this->time_now = ::timeGetTime( );
- //初始化压缩器
- this->codec.InitEncode( header );
- return TRUE;
- }
- BOOL CAVI::GetAVIInfo( BITMAPINFOHEADER * header , DWORD * rate )
- {
- if( this->m_bMode != CAVI::AVI_READ || this->m_avi_file == NULL)
- return FALSE;
- memcpy( header ,&this->m_avi_bmp_info , sizeof( BITMAPINFOHEADER ) );
- *rate = this->m_avi_stream_info.dwRate / this->m_avi_stream_info.dwScale;
- return TRUE;
- }
- BOOL CAVI::AddFrame( void * data , UINT size )
- {
- DWORD now = ::timeGetTime( );
- if( this->m_bMode != CAVI::AVI_WRITE || this->m_avi_file == NULL || now - this->time_now < this->time_interval )
- return FALSE;
- this->time_now = now;
- if( size == 0 )
- size = this->m_avi_bmp_info.biSizeImage;
- data = this->codec.Encode( data , ( int * )&size );
- if( data && size )
- return AVIERR_OK == ::AVIStreamWrite( this->m_avi_stream , this->write_frame ++ , 1 , data , size , AVIIF_KEYFRAME , NULL , NULL );
- return false;
- }
- BOOL CAVI::GetFrame( void ** data , UINT * size , int index )
- {
- if( this->m_bMode != CAVI::AVI_READ )
- return false;
- if(index != -1 ) this->read_frame = index;
- *data = NULL;
- HRESULT hr = ::AVIStreamRead( this->m_avi_stream , this->read_frame ++ , 1 , this->avi_buffer , this->m_avi_bmp_info.biSizeImage , ( long * )size , 0 );
- if( hr == AVIERR_OK )
- *data = this->avi_buffer;
- return *data != NULL;
- }