分类: WINDOWS
2007-01-25 17:29:57
最近在做一个“运动物体分割”的课题,在课题中要处理视频文件,首先考虑最简单的视频格式:AVI格式,由于AVI格式是微软推出的,所以在Windows下操作比较容易,这里我用DirectShow读写AVI视频文件,并把所有操作封装成一个类:AVICodec。下面是类的定义:
class AVICodec
{
public:
AVICodec(){size = -1;}
AVICodec(LPCWSTR avifile);
void release();
int init_getframe();
char* getframe(unsigned int index);
int getFrame(unsigned int index);
int getFrameEnd();
void play();
private:
IGraphBuilder *pGraph;
IMediaControl *pControl;
IMediaEvent *pEvent;
IMediaDet *pDet;AM_MEDIA_TYPE amMediaType;
public:
BITMAPINFOHEADER *pbih;
double fps, length;
unsigned int nFrame;
long size;
bool is_begin;
char * pBuffer;
private:
char * buf;
};
这个类目前实现两个功能:
下面给出代码:
AVICodec::AVICodec(LPCWSTR avifile)//类初始化,avifile是文件名
{
pGraph = NULL;
pControl = NULL;
pEvent = NULL;
size = -1;
is_begin = false;
buf = NULL;
HRESULT hr = CoInitialize(NULL);
if(FAILED(hr)){
cout<<"initialize COM Library failed"<exit(0);
}
hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
IID_IGraphBuilder, (void **)&pGraph);
if(FAILED(hr)){
cout<<"could not create Filter Graph Manager"<exit(0);
}
hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);
hr = pGraph->RenderFile(avifile, NULL);
if(FAILED(hr)){
cout<<"open AVI file failed"<exit(0);
}
hr = CoCreateInstance(CLSID_MediaDet, NULL, CLSCTX_INPROC, IID_IMediaDet,
(void **)&pDet);
if(FAILED(hr)){
cout<<"initialize MediaDet Failed"<exit(0);
}
hr = pDet->put_Filename(BSTR(avifile));
if(FAILED(hr)){
cout<<"MediaDet put file failed"<exit(0);
}
}int AVICodec::init_getframe()//抓图的初始化,得到帧数,帧频等参数
{
long lStreams;
HRESULT hr = pDet->get_OutputStreams(&lStreams);
if(FAILED(hr)){
cout<<"Failed : pDet->get_OutputStreams()"<return -1;
}
hr = pDet->put_CurrentStream(0);
if(FAILED(hr)){
cout<<"Failed : pDet->put_CurrentStream()"<return -1;
}
hr = pDet->get_StreamMediaType(&amMediaType);
if(FAILED(hr)){
cout<<"Failed : pDet->get_StreamMediaType()"<}
pbih = &((VIDEOINFOHEADER *)amMediaType.pbFormat)->bmiHeader;
if(FAILED(pDet->get_FrameRate(&fps)) || FAILED(pDet->get_StreamLength(&length)))
{
cout<<"Failed : pDet->get_FrameRate() || pDet->get_StreamLength()"<return -1;
}
nFrame = (unsigned int)(length * fps);
return 1;
}int AVICodec::getFrame(unsigned int index)//抓取index帧的图像,存放在buf中
{
if(index>nFrame){
cout<<"exceed max frame index"<return -1;
}
HRESULT hr;
double position = (double)(index*1.0/fps);
hr = pDet->GetBitmapBits(position,&size,NULL,pbih->biWidth,pbih->biHeight);
if(FAILED(hr)){
cout<<"Failed : pDet->GetBitmapBits()"<return -1;
}
if(is_begin==false){
buf = new char[size];
memset(buf,0,size);
is_begin = true;
}
//memset(buf,0,size);
hr = pDet->GetBitmapBits(position, NULL, buf, pbih->biWidth, pbih->biHeight);
BITMAPINFO *binfo = (BITMAPINFO *)buf;
pBuffer = buf + ((BITMAPINFO *)buf)->bmiHeader.biSize;
if(FAILED(hr)){
cout<<"Failed : pDet->GetBitmapBits()"<return -1;
}
return 1;
}int AVICodec::getFrameEnd()//抓图后释放内存
{
is_begin = false;
delete[] buf;
return 1;
}void AVICodec::play()//播放AVI文件,文件名在类初始化时给定
{
HRESULT hr = pControl->Run();
if(SUCCEEDED(hr)){
long evCode;
pEvent->WaitForCompletion(INFINITE,&evCode);
}
}void AVICodec::release()//运行完释放资源
{
pControl->Release();
pEvent->Release();
pControl->Release();
pDet->Release();
CoUninitialize();
}
目前这个类还在充实中,后续的代码会不断更新。
如果程序中有什么问题,还望指教