天天看点

Direcshow中视频捕捉和参数设置报告

direcshow中视频捕捉和参数设置报告

1视频捕捉graph的构建

一个能够捕捉音频或者视频的graph图都称之为捕捉graph图。捕捉graph图比一般的文件回放graph图要复杂许多,dshow提供了一个capture graph builder com组件使得捕捉graph图的生成更加简单。capture graph builder提供了一个icapturegraphbuilder2接口,这个接口提供了一些方法用来构建和控制捕捉graph。

首先创建一个capture graph builder对象和一个graph manger对象,然后用filter graph manager 作参数,调用icapturegraphbuilder2::setfiltergraph来初始化capture graph builder。看下面的代码把

hresultinitcapturegraphbuilder(

igraphbuilder**ppgraph, // receives the pointer.

icapturegraphbuilder2**ppbuild // receives the pointer.

)

{

if (!ppgraph|| !ppbuild)

returne_pointer;

}

igraphbuilder*pgraph = null;

icapturegraphbuilder2*pbuild = null;

 // create the capture graph builder.

  hresult hr = cocreateinstance(clsid_capturegraphbuilder2, null,

  clsctx_inproc_server,iid_icapturegraphbuilder2, (void**)&pgraph);

  if (succeeded(hr))

  {

  // create the filter graph manager.

  hr = cocreateinstance(clsid_filtergraph, 0, clsctx_inproc_server,

  iid_igraphbuilder, (void**)&pgraph);

  // initialize the capture graph builder.

  pbuild->setfiltergraph(pgraph);

 // return both interface pointers to thecaller.

  *ppbuild = pbuild;

  *ppgraph = pgraph; // the caller must releaseboth interfaces.

  return s_ok;

  }

  else

pbuild->release();

 return hr; // failed

pin的种类

捕捉filter一般都有两个或多个输出pin,他们输出的媒体类型都一样,比如预览pin和捕捉pin,因此根据媒体类型就不能很好的区别这些pin。此时就要根据pin的功能来区别每个pin了,每个pin都有一个guid,称为pin的种类。

如果想仔细的了解pin的种类,请看后面的相关内容workingwith pin categories。对于大多数的应用来说,icapturegraphbuilder2提供了一些函数可以自动确定pin的种类。

预览pin和捕捉pin

视频捕捉filter都提供了预览和捕捉的输出pin,预览pin用来将视频流在屏幕上显示,捕捉pin用来将视频流写入文件。

预览pin和输出pin有下面的区别:

1 为了保证捕捉pin对视频桢流量,预览pin必要的时候可以停止。

2 经过捕捉pin的视频桢都有时间戳,但是预览pin的视频流没有时间戳。

预览pin的视频流之所以没有时间戳的原因在于filter图表管理器在视频流里加一个很小的latency,如果捕捉时间被认为就是render时间的话,视频renderfilter就认为视频流有一个小小的延迟,如果此时render filter试图连续播放的时候,就会丢桢。去掉时间戳就保证了视频桢来了就可以播放,不用等待,也不丢桢。

video port pin

video port是一个介于视频设备(tv)和视频卡之间的硬件设备。同过video port,视频数据可以直接发送到图像卡上,通过硬件的覆盖,视频可以直接在屏幕显示出来。video port就是连接两个设备的。

使用video port的最大好处是,不用cpu的任何工作,视频流直接写入内存中。如果捕捉设备使用了video port,捕捉filter就用一个video port pin代替预览pin。

预览pin的种类guid为pin_category_preview

捕捉pin的种类guid为pin_category_capture

video port pin的种类guid为pin_category_videoport

一个捕捉filter至少有一个capturepin,另外,它可能有一个预览pin 和一个videoport pin

,或者两者都没有,也许filter有很多的capturepin,和预览pin,每一个pin都代表一种媒体类型,因此一个filter可以有一个视频capturepin,视频预览pin,音频捕捉pin,音频预览pin。

为了创建可以预览视频的graph,可以调用下面的代码

icapturegraphbuilder2*pbuild; // capture graph builder

// initializepbuild (not shown).

ibasefilter*pcap; // video capture filter.

 /* initialize pcap and add it to the filtergraph (not shown). */

hr =pbuild->renderstream(&pin_category_preview, &mediatype_video,   pcap, null, null);

1)        将视频流保存到avi文件

下面的图表显示了graph图

图2

avi mux filter接收从capture pin过来的视频流,然后将其打包成avi流。音频流也可以连接到avi mux filter上,这样mux filter就将视频流和视频流合成avi流。file writer将avi流写入到文件中。

可以像下面这样构建graph图

ibasefilter*pmux;

hr =pbuild->setoutputfilename(

  &mediasubtype_avi, // specifies avi forthe target file.

  l"c:\\example.avi", // file name.

  &pmux, // receives a pointer to the mux.

  null); // (optional) receives a pointer tothe file sink.

第一个参数表明文件的类型,这里表明是avi,第二个参数是制定文件的名称。对于avi文件,setoutputfilename函数会创建一个avi mux filter 和一个 file writer filter ,并且将两个filter添加到graph图中,在这个函数中,通过file writer filter 请求ifilesinkfilter接口,然后调用ifilesinkfilter::setfilename方法,设置文件的名称。然后将两个filter连接起来。第三个参数返回一个指向 avi mux的指针,同时,它也通过第四个参数返回一个ifilesinkfilter参数,如果你不需要这个参数,你可以将这个参数设置成null。

然后,你应该调用下面的函数将capture filter 和avi mux连接起来。

hr =pbuild->renderstream(

  &pin_category_capture, // pin category.

  &mediatype_video, // media type.

  pcap, // capture filter.

  null, // intermediate filter (optional).

  pmux); // mux or file sink filter.

  // release the mux filter.

  pmux->release();

第5个参数就是使用的上面函数返回的pmux指针。

当捕捉音频的时候,媒体类型要设置为mediatype_audio,如果你从两个不同的设备捕捉视频和音频,你最好将音频设置成主流,这样可以防止两个数据流间drift,因为avi mux filter为同步音频,会调整视频的播放速度的。为了设置master 流,调用iconfigavimux::setmasterstream方法,可以采用如下的代码:

iconfigavimux*pconfigmux = null;

  hr =pmux->queryinterface(iid_iconfigavimux, (void**)&pconfigmux);

  pconfigmux->setmasterstream(1);

  pconfigmux->release();

setmasterstream的参数指的是数据流的数目,这个是由调用renderstream的次序决定的。例如,如果你调用renderstream首先用于视频流,然后是音频,那么视频流就是0,音频流就是1。

添加编码filter

  ibasefilter *pencoder; /* create the encoderfilter (not shown). */

  // add it to the filter graph.

  pgraph->addfilter(pencoder,l"encoder);

/* callsetoutputfilename as shown previously. */

// render thestream.

&pin_category_capture,

 &mediatype_video,

pcap,pencoder, pmux);

  pencoder->release();

2)        将视频流保存成wmv格式的文件

为了将视频流保存成并编码成windows media video (wmv)格式的文件,将capture pin连到wm asf writer filter。

图3

构建graph图最简单的方法就是将在icapturegraphbuilder2::setoutputfilename方法中指定mediasubtype_asf的filter。如下

 ibasefilter* pasfwriter = 0;

  hr = pbuild->setoutputfilename(

  &mediasubtype_asf, // create a windowsmedia file.

  l"c:\\vidcap.wmv", // file name.

  &pasfwriter, // receives a pointer to thefilter.

  null); // receives an ifilesinkfilterinterface pointer (optional).

参数mediasubtype_asf 告诉graph builder,要使用wm asf writer作为文件接收器,于是,pbuild 就创建这个filter,将其添加到graph图中,然后调用ifilesinkfilter::setfilename来设置输出文件的名字。第三个参数用来返回一个asf writer指针,第四个参数用来返回文件的指针。

在将任何pin连接到wm asf writer之前,一定要对wm asf writer进行一下设置,你可以同过wm asf writer的iconfigasfwriter接口指针来进行设置。

 iconfigasfwriter *pconfig = 0;

  hr = pasfwriter->queryinterface(iid_iconfigasfwriter,(void**)&pconfig);

  // configure the asf writer filter.

  pconfig->release();

然后调用icapturegraphbuilder2::renderstream将capture filter 和 asf writer连接起来。

 hr = pbuild->renderstream(

  &pin_category_capture, // capture pin.

  &mediatype_video, // video. usemediatype_audio for audio.

  pcap, // pointer to the capture filter.

  0,

  pasfwriter); // pointer to the sink filter(asf writer).

3)        保存成自定义的文件格式

如果你想将文件保存成自己的格式,你必须有自己的 file writer。看下面的代码

 ibasefilter *pmux = 0;

  ifilesinkfilter *psink = 0;

  &clsid_mycustommuxfilter, //自己开发的filter

  l"c:\\vidcap.avi", &pmux,&psink);

4)        如何将视频流保存进多个文件

当你将视频流保存进一个文件后,如果你想开始保存第二个文件,这时,你应该首先将graph停止,然后通过ifilesinkfilter::setfilename改变 file writer 的文件名称。注意,ifilesinkfilter指针你可以在setoutputfilename时通过第四个参数返回的。

看看保存多个文件的代码吧

 ibasefilter *pmux;同步的synchronization

  ifilesinkfilter *psink

  hr =pbuild->setoutputfilename(&mediasubtype_avi,l"c:\\yourfilename.avi",

  &pmux, &psink);

  hr = pbuild->renderstream(&pin_category_capture,&mediatype_video,   pcap, null,pmux);

 if (succeeded(hr))

 {

  pcontrol->run();

  /* wait awhile, then stop the graph. */

  pcontrol->stop();

  // change the file name and run the graphagain.

  psink->setfilename(l"yourfilename02.avi",0);

 pmux->release();

 psink->release();

5)        组合视频的捕捉和预览

如果想组建一个既可以预览视频,又可以将视频保存成文件的graph,只需要两次调用icapturegraphbuilder2::renderstream即可。代码如下:

 // render the preview stream to the videorenderer.

  hr =pbuild->renderstream(&pin_category_preview, &mediatype_video,   pcap, null, null);

// render thecapture stream to the mux.

hr =pbuild->renderstream(&pin_category_capture, &mediatype_video,   pcap, null, pmux);

在上面的代码中,graph builder 其实隐藏了下面的细节。

1 如果capture filter既有preview pin 也有captrue pin,那么renderstream仅仅将两个pin和render filter接起来。如下图

图4

2如果caprture filter只有一个capture pin,那么capture graph builder就采用一个smart tee filter将视频流分流,graph图如下

图5

filter图表管理器可以通过imediacontrol接口控制整个graph的运行,停止和暂停。但是当一个graph有捕捉和预览两个数据流的时候,如果我们想单独的控制其中的一个数据流话,我们可以通过icapturegraphbuilder2::controlstream。

下面讲一下如何来单独控制捕捉和预览数据流。

1 控制捕捉视频流

下面的代码,让捕捉数据流在graph开始运行1秒后开始,允运行4秒后结束。

// control the video capture stream.

reference_time rtstart = 1000 0000, rtstop = 50000000;

const word wstartcookie = 1, wstopcookie = 2; //arbitrary values.  hr = pbuild->controlstream(

&pin_category_capture, // pin category.

&mediatype_video, // media type.

pcap, // capture filter.

&rtstart, &rtstop, // start and stop times.

wstartcookie, wstopcookie // values for the start andstop events.

  );

pcontrol->run();

第一个参数表明需要控制的数据流,一般采用的是pin种类guid,

第二个参数表明了媒体类型。

第三个参数指明了捕捉的filter。如果想要控制graph图中的所有捕捉filter,第二个和第三个参数都要设置成null。

第四和第五个参数表明了流开始和结束的时间,这是一个相对于graph开始的时间。

只有你调用imediacontrol::run以后,这个函数才有作用。如果graph正在运行,这个设置立即生效。

最后的两个参数用来设置当数据流停止,开始能够得到的事件通知。对于任何一个运用此方法的数据流,graph当流开始的时候,会发送ec_stream_control_started通知,在流结束的时候,要发送ec_stream_control_stopped通知。wstartcookie和wstopcookie是作为第二个参数的。

看看事件通知处理过程吧

 while (hr = pevent->getevent(&evcode,&param1, &param2, 0), succeeded(hr))

  switch (evcode)

  case ec_stream_control_started:

  // param2 == wstartcookie

  break;

 case ec_stream_control_stopped:

  // param2 == wstopcookie

  pevent->freeeventparams(evcode, param1,param2);

controlstream还定义了一些特定的值来表示开始和停止的时间。

maxlonglong从不开始,只有在graph停止的时候才停止

null, 立即开始和停止

例如,下面的代码立即停止捕捉流。

 pbuild->controlstream(&pin_category_capture,&mediatype_video, pcap,  0, 0, //start and stop times.

  wstartcookie, wstopcookie);

2控制预览视频流

只要给controlstream第一个参数设置成pin_category_preview就可以控制预览pin,整个函数的使用和控制捕捉流一样,但是唯一区别是在这里你没法设置开始和结束时间了,因为预览的视频流没有时间戳,因此你必须使用null或者maxlonglong。例子

 use null to start the preview stream:

 pbuild->controlstream(&pin_category_preview,&mediatype_video, pcap,  null, //start now.

  0, // (don't care.)

  use maxlonglong to stop the preview stream:

 pbuild->controlstream(&pin_category_preview,&mediatype_video, pcap,  0, // (don'tcare.)

  maxlonglong, // stop now.

3关于数据流的控制

pin的缺省的行为是传递sample,例如,如果你对pin_category_capture使用了controlstream,但是对于pin_category_preview没有使用该函数,因此,当你run graph的时候,preview 流会立即运行起来,而capture 流则要等到你设置的时间运行。

1显示vfw驱动的视频设备对话框

如果视频捕捉设备采用的仍然是vfw方式的驱动程序,则必须支持下面三个对话框,用来设置视频设备。

1 videosource

用来选择视频输入设备并且调整设备的设置,比如亮度和对比度。

2videoformat

用来设置桢的大小和位

3videodisplay

用来设置视频的显示参数

为了显示上面的三个对话框,你可以do the following

1停止graph。

2向捕捉filter请求iamvfwcapturedialogs接口,如果成功,表明设备支持vfw驱动。

3调用iamvfwcapturedialogs::hasdialog来检查驱动程序是否支持你请求的对话框,如果支持,返回s_ok,否则返回s_false。注意不要用succeded宏。

4如果驱动支持该对话框,调用iamvfwcapturedialogs::showdialog显示该对话框。

5 重新运行graph

代码如下

 pcontrol->stop(); // stop the graph.

 // query the capture filter for theiamvfwcapturedialogs interface. iamvfwcapturedialogs *pvfw = 0;

 hr =pcap->queryinterface(iid_iamvfwcapturedialogs, (void**)&pvfw);  if (succeeded(hr))

  // check if the device supports this dialogbox.

  if (s_ok ==pvfw->hasdialog(vfwcapturedialog_source))

  // show the dialog box.

  hr =pvfw->showdialog(vfwcapturedialog_source, hwndparent);

2 调整视频的质量

wdm驱动的设备支持一些属性可以用来调整视频的质量,比如亮度,对比度,饱和度,等要向调整视频的质量,do the following

1 从捕捉filter上请求iamvideoprocamp接口

2 对于你想调整的任何一个属性,调用iamvideoprocamp::getrange可以返回这个属性赋值的范围,缺省值,最小的增量值。iamvideoprocamp::get返回当前正在使用的值。videoprocampproperty枚举每个属性定义的标志。

3调用iamvideoprocamp::set来设置这个属性值。设置属性的时候,不用停止graph。

看看下面的代码是如何调整视频的质量的

hwnd htrackbar;// handle to the trackbar control.

// initializehtrackbar (not shown).

// query thecapture filter for the iamvideoprocamp interface.

  iamvideoprocamp *pprocamp = 0;

  hr =pcap->queryinterface(iid_iamvideoprocamp, (void**)&pprocamp);

  if (failed(hr))

  // the device does not supportiamvideoprocamp, so disable the control.

  enablewindow(htrackbar, false);

  long min, max, step, default, flags, val;

  // getthe range and default value.

  hr = m_pprocamp->getrange(videoprocamp_brightness,&min, &max, &step,

  &default, &flags);

  // get the current value.

  hr =m_pprocamp->get(videoprocamp_brightness, &val, &flags);

  // set the trackbar range and position.

  sendmessage(htrackbar, tbm_setrange, true,makelong(min, max));

  sendmessage(htrackbar, tbm_setpos, true,val);

  enablewindow(htrackbar, true);

  // this property is not supported, so disablethe control.

3调整视频输出格式

我们知道视频流可以有多种输出格式,一个设备可以支持16-bit rgb, 32-bit rgb, and yuyv,在每一种格式下,设备还可以调整视频桢的大小。

在wdm驱动设备上,iamstreamconfig 接口用来报告设备输出视频的格式的,vfw设备,可以采用对话框的方式来设置,参见前面的内容。

捕捉filter的捕捉pin和预览pin都支持iamstreamconfig 接口,可以通过icapturegraphbuilder2::findinterface获得iamstreamconfig接口。

 iamstreamconfig *pconfig = null;

  hr = pbuild->findinterface(

  &pin_category_preview, // preview pin.

  0, // any media type.

  iid_iamstreamconfig, (void**)&pconfig);

设备还支持一系列的媒体类型,对于每一个媒体类型,设备都要支持一系列的属性,比如,桢的大小,图像如何缩放,桢率的范围等。

通过iamstreamconfig::getnumberofcapabilities获得设备所支持的媒体类型的数量。这个方法返回两个值,一个是媒体类型的数量,二是属性所需结构的大小。

这个结构的大小很重要,因为这个方法是用于视频和音频的,视频采用的是video_stream_config_caps结构,音频用audio_stream_config_caps结构。

通过函数iamstreamconfig::getstreamcaps来枚举媒体类型,要给这个函数传递一个序号作为参数,这个函数返回媒体类型和相应的属性结构体。看代码把

 int icount = 0, isize = 0;

 hr =pconfig->getnumberofcapabilities(&icount, &isize);

// check thesize to make sure we pass in the correct structure.

  if (isize == sizeof(video_stream_config_caps)

  // use the video capabilities structure.

  for (int iformat = 0; iformat < icount;iformat++)

  video_stream_config_caps scc;

  am_media_type *pmtconfig;

  hr = pconfig->getstreamcaps(iformat,&pmtconfig, (byte*)&scc);

  /* examine the format, and possibly use it.*/

  // delete the media type when you are done.

  hr = pconfig->setformat(pmtconfig);//重新设置视频格式

  deletemediatype(pmtconfig);

你可以调用iamstreamconfig::setformat设置新的媒体类型

hr =pconfig->setformat(pmtconfig);

如果pin没有连接,当连接的时候就试图用新的格式,如果pin已经在连接了,它就会用的新的媒体格式重新连接。在任何一种情况下,下游的filter都有可能拒绝新的媒体格式。

在setformat前你可以修改video_stream_config_caps结构来重新设置媒体类型。

例如:

如果getstreamcaps返回的是24-bit rgb format,桢的大小是320 x 240 像素,你可以通过检查媒体类型的major type,subtpye,和format等值

 if ((pmtconfig.majortype == mediatype_video)&&

  (pmtconfig.subtype == mediasubtype_rgb24)&&

  (pmtconfig.formattype == format_videoinfo)&&

  (pmtconfig.cbformat >= sizeof(videoinfoheader)) &&

  (pmtconfig.pbformat != null))

  videoinfoheader *pvih =(videoinfoheader*)pmtconfig.pbformat;

  // pvih contains the detailed formatinformation.

  long lwidth = pvih->bmiheader.biwidth;

  long lheight = pvih->bmiheader.biheight;

video_stream_config_caps结构里包含了该媒体类型的视频长度和宽度的最大值和最小值,还有递增的幅度值,就是每次调整视频size的幅度,例如,设备可能返回如下的值

minoutputsize:160 x 120

maxoutputsize:320 x 240

outputgranularityx:8 pixels (horizontal step size)

outputgranularityy:8 pixels (vertical step size)

这样你可以在(160, 168, 176, ... 304, 312, 320) 范围内设置宽度,在 (120, 128, 136, ... 104, 112,120).设置高度值,

图6

如果想设置新的值,直接修改在getstreamcaps函数中返回的值即可,

 pvih->bmiheader.biwidth = 160;

  pvih->bmiheader.biheight = 120;

  pvih->bmiheader.bisizeimage =dibsize(pvih->bmiheader);

然后将媒体类型传递给setformat函数,就可修改视频格式了。

如果用户将一个graph正在使用的即插即用型的设备从系统中去掉,filter图表管理器就会发送一个ec_device_lost事件通知,如果该设备又可以使用了,filter图表管理器就发送另外的一个ec_device_lost通知,但是先前组建的捕捉filter graph图就没法用了,用户必须重新组建graph图。

当系统中有新的设备添加时,dshow是不会发送任何通知的,所以,应用程序如果想要知道系统中何时添加新的设备,应用程序可以监控wm_devicechange消息。

从设备中获取静态图片,我们一般推荐使用windows image acquisition (wia) apis。当然,你也可以用dshow来获取图片。

在graph运行的时候利用iamvideocontrol::setmode来触发静态的pin。代码如下

 pcontrol->run(); // run the graph.

  iamvideocontrol *pamvidcontrol = null;

  hr = pcap->queryinterface(iid_iamvideocontrol,(void**)&pamvidcontrol);

  // find the still pin.

  ipin *ppin = 0;

  hr = pbuild->findpin(pcap, pindir_output,&pin_category_still, 0,  false, 0,&ppin);

  hr = pamvidcontrol->setmode(ppin, videocontrolflag_trigger);

  ppin->release();

  pamvidcontrol->release();

首先向capture filter 请求iamvideocontol,如果支持该接口,就调用icapturegraphbuilder2::findpin请求指向静止pin 的指针,然后调用pin的put_mode方法。

根据不同的摄像头,你可能静态pin连接前要render 该pin。

捕捉静态图片常用的filter是sample grabber filter,sample grabber使用了一个用户定义的回调汗水来处理图片。关于这个filter的详细用法,参见using the sample grabber.。

下面的例子假设静态pin传递的是没有压缩的rgb图片。首先定义一个类,从isamplegrabbercb继承。

 // class to hold the callback function for thesample grabber filter.  classsamplegrabbercallback : public isamplegrabbercb

  // implementation is described later.

// globalinstance of the class.

samplegrabbercallbackg_stillcapcb;

然后将捕捉filter的静态pin连接到sample grabber,将sample grabber连接到null renderer filter。null renderer仅仅是将她接收到的sample丢弃掉。实际的工作都是在回调函数里进行,连接null renderer 仅仅是为了给sample grabber's 输出pin上连接点东西。具体见下面的代码

  // add the sample grabber filter to thegraph.

  ibasefilter *psg_filter;

  hr = cocreateinstance(clsid_samplegrabber,null, clsctx_inproc_server,

  iid_ibasefilter, (void**)&psg_filter);

  hr = pgraph->addfilter(psg_filter,l"samplegrab");

// add thenull renderer filter to the graph.

ibasefilter*pnull;

hr =cocreateinstance(clsid_nullrendere, null, clsctx_inproc_server,  iid_ibasefilter, (void**)&pnull);

hr =pgraph->addfilter(psg_filter, l"nullrender");

然后通过renderstream将still pin ,sample grabber ,null renderer连接起来

  &pin_category_still, // connect this pin...

  &mediatype_video, // with this media type...

  pcap, // on this filter ...

  psg_filter, // to the sample grabber ...

  pnull); // ... and finally to the nullrenderer.

然后调用isamplegrabber指针,来通过这个指针可以分配内存。

  // configure the sample grabber.

 isamplegrabber *psg;

 hr =psg_filter->queryinterface(iid_isamplegrabber, (void**)&psg);

 psg->setoneshot(false);

  psg->setbuffersamples(true);

设置你的回调对象

 psg->setcallback(&g_stillcapcb, 0); //0 = use the samplecb callback   method

获取静态pin和sample grabber之间连接所用的媒体类型

 // store the media type for later use.

  am_media_type g_stillmediatype;

  hr =psg->getconnectedmediatype(&g_stillmediatype);

  psg->release();

媒体类型包含一个bitmapinfoheader结构来定义图片的格式,在程序退出前一定要释放媒体类型

 // on exit, remember to release the mediatype.

  freemediatype(g_stillmediatype);

看看下面的回调类吧。这个类从isamplegrabber接口派生,但是它没有保持引用计数,因为应用程序在堆上创建这个对象,在整个graph的生存周期它都存在。

所有的工作都在buffercb函数里完成,当有一个新的sample到来的时候,这个函数就会被sample grabber调用到。在下面的例子里,bitmap被写入到一个文件中

classsamplegrabbercallback : public isamplegrabbercb

  public:

  // fake referance counting.

  stdmethodimp_(ulong) addref() { return 1; }

  stdmethodimp_(ulong) release() { return 2; }

 stdmethodimp queryinterface(refiid riid, void**ppvobject)

  if (null == ppvobject) return e_pointer;

  if (riid == __uuidof(iunknown))

  *ppvobject =static_cast<iunknown*>(this);

  if (riid == __uuidof(isamplegrabbercb))

  *ppvobject =static_cast<isamplegrabbercb*>(this);

  return e_notimpl;

 stdmethodimp samplecb(double time,imediasample *psample)

 stdmethodimp buffercb(double time, byte*pbuffer, long bufferlen)

  if ((g_stillmediatype.majortype !=mediatype_video) ||

  (g_stillmediatype.formattype !=format_videoinfo) ||

  (g_stillmediatype.cbformat <sizeof(videoinfoheader)) ||

  (g_stillmediatype.pbformat == null))

  return vfw_e_invalidmediatype;

 }

 handle hf =createfile("c:\\example.bmp", generic_write,

 file_share_write, null, create_always, 0,null);

 if (hf == invalid_handle_value)

  return e_fail;

 long cbbitmapinfosize =g_stillmediatype.cbformat - size_preheader;

videoinfoheader*pvideoheader = (videoinfoheader*)g_stillmediatype.pbformat;

 bitmapfileheader bfh;

zeromemory(&bfh,sizeof(bfh));

bfh.bftype ='mb'; // little-endian for "mb".

bfh.bfsize =sizeof( bfh ) + bufferlen + cbbitmapinfosize;

bfh.bfoffbits= sizeof( bitmapfileheader ) + cbbitmapinfosize;

// write thefile header.

dworddwwritten = 0;

writefile(hf, &bfh, sizeof( bfh ), &dwwritten, null );

writefile(hf,header(pvideoheader), cbbitmapinfosize, &dwwritten, null);

writefile(hf, pbuffer, bufferlen, &dwwritten, null );

closehandle(hf );

return s_ok;

};

Direcshow中视频捕捉和参数设置报告