Master the usage of Dump Filter: Collect the program code of mpg, Avi and ASF

Source: Internet
Author: User

 

Http://www.360doc.com/content/08/0705/23/29694_1401883.shtml this web site has source code

 

The following program code demonstrates how to use a VW card to collect mpg, Avi, or ASF files. This program uses DirectShow source filter on the VW Card and related filter to collect files based on DirectShow. Since Avi, asfformat files are MPEG-4 videos here, for simplicity, mpg files are also MPEG-4 videos, that is, the MPEG-4 embedded MPEG-2 raised by the previous article.

After the program is running, first create a filter graph, which only contains VW Source Filter and corresponding settings, in this instance set the parameters: PAL composite video input, MPEG-4 program encoding, the video bit rate is 1 m BPS, the resolution is 720x576, the audio bit rate is 192 k bps, 44.1k sampling frequency, MPEG-1 L2 Audio Encoding (AVI file audio encoding is PCM ). Then, complete the corresponding filte graph according to the selected output file format.

1. Create a basic filter graph

// We create a filter graph including source filter only.
// For every usage, we add other usaful filter into the Graph
// And Remove useless filters.
Hresult ccap4dlg: craetebasecapturegraph ()
{
Hresult hr;
Ibasefilter * psrc = NULL;

If (m_pgraph)
{
Safe_release (m_pgraph );
}

_ Try {
// Create a filter graph
Check_hr (hR = cocreateinstance (clsid_filtergraph, null, clsctx_inproc ,/
Iid_igraphbuilder, (void **) & m_pgraph ));
// Create a source filter instance and add it to the filter graph.
Check_hr (hR = cocreateinstance (clsid_vwsource, null, clsctx_inproc, iid_ibasefilter, (void **) & psrc ));
Check_hr (hR = m_pgraph-> addfilter (psrc, l "Source Filter "));

Isrccontrol * p_isrc;

Check_hr (hR = psrc-> QueryInterface (iid_isrccontrol, (void **) & p_isrc); // obtain the interface pointer
P_isrc-> setaudioencodeformat (1); // MPEG-1 L2
P_isrc-> setdevicenumber (0); // specify the operation card number 0
P_isrc-> setstreamtype (3); // specify the MPEG-4 Program
P_isrc-> setvideosource (vw_composite); // compound Input
P_isrc-> setvideostandard (vw_src_pal); // pal
P_isrc-> setvideoresolution (vw_video_resolution_full_720); // 720x576
P_isrc-> setvideobitrate (1000000); // 1 m BPS video bit rate
P_isrc-> setaudiofrequency (vw_audio_freq_44100); // 44.1khz audio sampling
P_isrc-> setaudiobitrate (vw_audio_bitrate_192); // 192 K audio bit rate
P_isrc-> setparameteractive ();
Safe_release (p_isrc );

}__ Finally {
Safe_release (psrc );

If (failed (HR )){
Safe_release (m_pgraph );
}
}
# Ifdef _ debug
If (succeeded (HR ))
Addgraphtorot (m_pgraph, & m_dwgraphregister );
# Endif
Return hr;
}

2. Create a filter graph for MPG file collection
Creating a filter graph for collecting MPG files is simple. You only need to add the Dump Filter and connect it to the source filter.
// Remove all filters before t source filter, then add Dump Filter

HRESULT CCap4Dlg::UpdateCaptureGraphForMpg (const char * filename)
{
HRESULT hr = E_FAIL;
IBaseFilter * pSrc = NULL;
IBaseFilter * pDump = NULL;
IFileSinkFilter *pSink = NULL;
WCHAR wFile[512];
IPin * pSrcPin;
IPin * pDstPin;

#ifdef _DEBUG
if (m_dwGraphRegister) {
RemoveGraphFromRot(m_dwGraphRegister);
m_dwGraphRegister = 0;
}
#endif
__try {
CHECK_HR (hr = m_pGraph->FindFilterByName (L"Source Filter", &pSrc));
TearDownGraph (pSrc);

// Create and add Dump Filter
CHECK_HR( hr = CoCreateInstance(CLSID_Dump,NULL,CLSCTX_INPROC,IID_IBaseFilter,(void**)&pDump));
CHECK_HR( hr = m_pGraph->AddFilter(pDump, L"Dump"));

CHECK_HR( hr = pDump->QueryInterface (IID_IFileSinkFilter, (void **)&pSink));
MultiByteToWideChar(CP_ACP, 0, filename, -1, wFile, NUMELMS(wFile));
pSink->SetFileName (wFile, NULL);

// Connect Source to Dump
pSrcPin = GetOutPin (pSrc, 0);
pDstPin = GetInPin (pDump, 0);
CHECK_HR( hr = m_pGraph->Connect (pSrcPin, pDstPin));
}__finally {
SAFE_RELEASE(pSink);
SAFE_RELEASE(pDump);
SAFE_RELEASE(pSrc);
}

#ifdef _DEBUG
if (SUCCEEDED(hr))
AddGraphToRot(m_pGraph, &m_dwGraphRegister);
#endif
return hr;
}

Filter graph used to collect MPG files, such

 

3. Create a filter graph for collecting AVI Files

 

Creating the filter graph for collecting AVI files is a little more complicated. We directly connect them to the avi mux filter by using the video and audio output pin of VW source video. Because PCM audio encoding is used, we want to connect an MPEG audio decoder filter in VW Source Filter pin2 to Avi MUX. The pin0 of the VW source filter, that is, the main thread that is implemented on the pin of the output MPEG multiplexing stream, we cannot leave it empty here, so we use an nullrenderer filter to connect to this pin. Connect Avi MUX to file witer filter.

// Remove all filters except source filter, then add AVI Mux filter
HRESULT CCap4Dlg::UpdateCaptureGraphForAvi (const char * filename)
{
HRESULT hr = E_FAIL;
IBaseFilter * pSrc = NULL;
IBaseFilter * pFileWriter = NULL;
IBaseFilter * pMux = NULL;
IBaseFilter * pAudDec = NULL;
IBaseFilter * pNullRnd = NULL;
IFileSinkFilter *pSink = NULL;
WCHAR wFile[512];
IPin * pSrcPin;
IPin * pDstPin;

#ifdef _DEBUG
if (m_dwGraphRegister) {
RemoveGraphFromRot(m_dwGraphRegister);
m_dwGraphRegister = 0;
}
#endif
__try {
CHECK_HR (hr = m_pGraph->FindFilterByName (L"Source Filter", &pSrc));
TearDownGraph (pSrc);

CHECK_HR( hr = CoCreateInstance(CLSID_NullRenderer, NULL,CLSCTX_INPROC,IID_IBaseFilter,(void**)&pNullRnd));
CHECK_HR( hr = m_pGraph->AddFilter(pNullRnd, L"Null Render"));
pSrcPin = GetOutPin (pSrc, 0);
pDstPin = GetInPin (pNullRnd, 0);
CHECK_HR( hr = m_pGraph->Connect (pSrcPin, pDstPin));

CHECK_HR( hr = CoCreateInstance(CLSID_AviDest, NULL,CLSCTX_INPROC,IID_IBaseFilter,(void**)&pMux));
CHECK_HR( hr = m_pGraph->AddFilter(pMux, L"AVI Mux"));

CHECK_HR( hr = CoCreateInstance(CLSID_CMpegAudioCodec,NULL,CLSCTX_INPROC,IID_IBaseFilter,(void**)&pAudDec));
CHECK_HR( hr = m_pGraph->AddFilter(pAudDec, L"Audio Decoder"));

CHECK_HR( hr = CoCreateInstance(CLSID_FileWriter, NULL,CLSCTX_INPROC,IID_IBaseFilter,(void**)&pFileWriter));
CHECK_HR( hr = m_pGraph->AddFilter(pFileWriter, L"File Writer"));

CHECK_HR( hr = pFileWriter->QueryInterface (IID_IFileSinkFilter, (void **)&pSink));
MultiByteToWideChar(CP_ACP, 0,filename, -1, wFile, NUMELMS(wFile));
pSink->SetFileName (wFile, NULL);

pSrcPin = GetOutPin (pSrc, 1);
pDstPin = GetInPin (pMux, 0);
CHECK_HR( hr = m_pGraph->Connect (pSrcPin, pDstPin));

pSrcPin = GetOutPin (pSrc, 2);
pDstPin = GetInPin (pAudDec, 0);
CHECK_HR( hr = m_pGraph->Connect (pSrcPin, pDstPin));

pSrcPin = GetOutPin (pAudDec, 0);
pDstPin = GetInPin (pMux, 1);
CHECK_HR( hr = m_pGraph->Connect (pSrcPin, pDstPin));

pSrcPin = GetOutPin (pMux, 0);
pDstPin = GetInPin (pFileWriter, 0);
CHECK_HR( hr = m_pGraph->Connect (pSrcPin, pDstPin));

}__finally {
SAFE_RELEASE(pMux);
SAFE_RELEASE(pNullRnd);
SAFE_RELEASE(pFileWriter);
SAFE_RELEASE(pSink);
SAFE_RELEASE(pAudDec);
SAFE_RELEASE(pSrc);
}
#ifdef _DEBUG
if (SUCCEEDED(hr))
AddGraphToRot(m_pGraph, &m_dwGraphRegister);
#endif
return hr;
}

The created filter graph is as follows:

 

4. Create a filter graph for ASF file collection.
The filter graph used to create an ASF file is similar to the filter graph used to collect an AVI file. However, ASF writer contains file storage, so file writer or dump is not required to output a filter.

// Remove all filters except source filter, then add ASF Mux filter
HRESULT CCap4Dlg::UpdateCaptureGraphForAsf (const char * filename)
{
HRESULT hr = E_FAIL;
IBaseFilter * pSrc = NULL;
IBaseFilter * pMux = NULL;
IBaseFilter * pNullRnd = NULL;
IFileSinkFilter *pSink = NULL;
WCHAR wFile[512];
IPin * pSrcPin;
IPin * pDstPin;

#ifdef _DEBUG
if (m_dwGraphRegister) {
RemoveGraphFromRot(m_dwGraphRegister);
m_dwGraphRegister = 0;
}
#endif
__try {
CHECK_HR (hr = m_pGraph->FindFilterByName (L"Source Filter", &pSrc));
TearDownGraph (pSrc);

CHECK_HR( hr = CoCreateInstance(CLSID_NullRenderer, NULL,CLSCTX_INPROC,IID_IBaseFilter, (void**)&pNullRnd));
CHECK_HR( hr = m_pGraph->AddFilter(pNullRnd, L"Null Render"));
pSrcPin = GetOutPin (pSrc, 0);
pDstPin = GetInPin (pNullRnd, 0);
CHECK_HR( hr = m_pGraph->Connect (pSrcPin, pDstPin));

CHECK_HR( hr = CoCreateInstance(CLSID_ASFWRITER, NULL,CLSCTX_INPROC,IID_IBaseFilter,(void**)&pMux));
CHECK_HR( hr = m_pGraph->AddFilter(pMux, L"ASF Mux"));
CHECK_HR( hr = pMux->QueryInterface (IID_IFileSinkFilter, (void **)&pSink));
MultiByteToWideChar(CP_ACP, 0,filename, -1, wFile, NUMELMS(wFile));
pSink->SetFileName (wFile, NULL);

pSrcPin = GetOutPin (pSrc, 1);
pDstPin = GetInPin (pMux, 0);
CHECK_HR( hr = m_pGraph->Connect (pSrcPin, pDstPin));

pSrcPin = GetOutPin (pSrc, 2);
pDstPin = GetInPin (pMux, 1);
CHECK_HR( hr = m_pGraph->Connect (pSrcPin, pDstPin));
}__finally {
SAFE_RELEASE(pMux);
SAFE_RELEASE(pNullRnd);
SAFE_RELEASE(pSink);
SAFE_RELEASE(pSrc);
}
#ifdef _DEBUG
if (SUCCEEDED(hr))
AddGraphToRot(m_pGraph, &m_dwGraphRegister);
#endif
return hr;
}

The created filter graph is as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.