DirectX: Add a custom filter (traversal filter in graph) to the graph Auto-link, filtergraph

Source: Internet
Author: User

DirectX: Add a custom filter (traversal filter in graph) to the graph Auto-link, filtergraph

In the filter test program for video playback provided to the customer, the positive manual connection method (http://blog.csdn.net/mao0514/article/details/40535791) is adopted because of different video compression formats, the disadvantage of this method is that you cannot play any video file in time. Now, you can add your own filter to the automatically connected graph:

The process is as follows:

1. Build an automatic graph connection;

2. Search for render filter in graph;

3. Search for the upper-level connection m_n_out_pin of the input m_r_in_pin on renderf ilter.

4. Disconnect the render pin from the upper-level pin.

5. Add a custom filter to obtain the input and output values m_my_in_pin and m_my_out_pin.

6. Connect pin: m_n_out_pin --> m_my_in_pin, m_my_out_pin-> m_r_in_pin

7. run

To be improved: the current program does not judge the input type after finding Render filtre. Currently, only RGB32 input is supported. If yuv is another filter

IGraphBuilder * pigb = NULL; IMediaControl * pimc = NULL; IMediaEventEx * pimex = NULL; IVideoWindow * pivw = NULL; IMediaSeeking * pims = NULL; Limit * g_pCaptureBuilder = NULL; // enumerate all filters HRESULT FindFilterInterface (IGraphBuilder * pGraph, // Pointer to the filter graph Manager. REFGUID iid, // IID of the interface to retrieve. void ** ppUnk) // es the interface pointer. {if (! PGraph |! PpUnk) return E_POINTER; HRESULT hr = E_FAIL; IEnumFilters * pEnum = NULL; IBaseFilter * pF = NULL; if (FAILED (pGraph-> EnumFilters (& pEnum ))) {return E_FAIL;} // Query every filter for the interface. while (S_ OK = pEnum-> Next (1, & pF, 0) {hr = pF-> QueryInterface (iid, ppUnk); pF-> Release (); if (SUCCEEDED (hr) {FILTER_INFO * fin = new FILTER_INFO (); hr = pF-> QueryFilterInfo (fin); // wchar ---> cha R char * m_char; int len = WideCharToMultiByte (CP_ACP, 0, fin-> achName, wcslen (fin-> achName), NULL, 0, NULL, NULL ); m_char = new char [len + 1]; WideCharToMultiByte (CP_ACP, 0, fin-> achName, wcslen (fin-> achName), m_char, len, NULL, NULL ); m_char [len] = '\ 0'; MessageBox (NULL, m_char, "", NULL); // display the name of the currently queried filter if (strstr (m_char, "Render ")! = NULL) {// enumerate the filters in the graph and find the Render filter break;} //} pEnum-> Release (); return hr ;} void CTestDlg: OnButton1 () {HRESULT hr; // construct graphCoInitialize (NULL); hr = CoCreateInstance (CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, struct, (void **) & pigb ); hr = CoCreateInstance (response, NULL, CLSCTX_INPROC, response, (void **) & g_pCaptureBuilder); pigb-> QueryInterface (IID_IMediaControl, (void **) & pimc ); pigb-> QueryInterface (IID_IMediaEventEx, (void **) & pimex); pigb-> QueryInterface (IID_IMediaSeeking, (void **) & pims); pigb-> QueryInterface (IID_IVideoWindow, (void **) & pivw); hr = g_pCaptureBuilder-> SetFiltergraph (pigb); // construct graph // open the video file jpg, bmp, avi, mpg ...... /////////////////// // CString strFilename; // CFileDialog openBox (TRUE, NULL, "", OFN_HIDEREADONLY, "bmp (*. bmp) | *. bmp | jpg (*. jpg) | *. jpg | avi (*. avi) | *. avi | mpg (*. mpg) | *. mpg | ALLFiles (*. *) | *. * | ", NULL); openBox. m_ofn.lpstrTitle = "Open Data File"; INT_PTR nResult = openBox. doModal (); // if the file is opened, prepare to play the video if (nResult = IDOK) {strFilename = openBox. getPathName () ;}else {return;} strFilename. releaseBuffer (); // cstring to LPCSTR USES_CONVERSION; LPWSTR pwStr = new wchar_t [strFilename. getLength () + 1]; wcscpy (pwStr, T2W (LPCTSTR) strFilename )); //////////////////////////////////////// /// // hr = pigb-> RenderFile (pwStr, NULL); // # if 1 // find the Render filter and obtain the Input pin IBaseFilter * pRenderFilter of the Render filter; hr = FindFilterInterface (pigb, IID_IBaseFilter, (void **) & pRenderFilter); hr = pigb-> AddFilter (pRenderFilter, L "ffdshow Video Render"); IPin * pIn_Render = 0; hr = g_pCaptureBuilder-> FindPin (pRenderFilter, PINDIR_INPUT, NULL, NULL, FALSE, 0, & pIn_Render); if (FAILED (hr) {MessageBox ("err10 ");} # endif # if 1 // query the previous filter IEnumFilters * pEnum = NULL in Render; IPin * pPinNext = 0; hr = pigb-> EnumFilters (& pEnum ); // IBaseFilter * pF; // Pointer to some filter. IBaseFilter * pUpstream = NULL; PIN_DIRECTION ThisPinDir; hr = pIn_Render-> QueryDirection (& ThisPinDir); if (SUCCEEDED (hr) {if (ThisPinDir = PINDIR_INPUT) {hr = pIn_Render-> ConnectedTo (& pPinNext); if (SUCCEEDED (hr) {// Get the filter that owns that pin. PIN_INFO PinInfo; hr = pPinNext-> QueryPinInfo (& PinInfo); pPinNext-> Release (); if (FAILED (hr) | (PinInfo. pFilter = NULL) {MessageBox ("err GetNextFilter3");} pUpstream = PinInfo. pFilter; // Client must release .} else {MessageBox ("err GetNextFilter4") ;}} else {MessageBox ("err GetNextFilter1") ;}} else {MessageBox ("err GetNextFilter2 ");} # endif # if 1 //// Add the custom Special filter ///////////////////////// /// // IBaseFilter * peffect; // --- 871E-AB91661A4EF7 const GUID CLSID_EFFECT = {0x8b498501, 0x1218, 0x11cf, {0xad, 0xc4, 0x0, 0xa0, 0xd1, 0x0, 0x4, 0x1b }}; hr = CoCreateInstance (CLSID_EFFECT, NULL, CLSCTX_ALL, IID_IBaseFilter, (void **) & peffect); hr = pigb-> AddFilter (peffect, L "Image effect"); IPin * pIn_peffect = 0; IPin * pOut_peffect = 0; hr = g_pCaptureBuilder-> FindPin (peffect, PINDIR_INPUT, NULL, NULL, FALSE, 0, & pIn_peffect); if (FAILED (hr) {MessageBox ("err11");} hr = g_pCaptureBuilder-> FindPin (peffect, PINDIR_OUTPUT, NULL, NULL, FALSE, 0, & pOut_peffect); if (FAILED (hr) {MessageBox ("err12");} peffect-> Release (); # endif // Disconnect to obtain the automatic connection pPinNext-> Disconnect (); pIn_Render-> Disconnect (); # if 1 // reconnect hr = pigb-> Connect (pPinNext, pIn_peffect); if (FAILED (hr) {MessageBox ("err19");} hr = pigb-> Connect (pOut_peffect, pIn_Render); if (FAILED (hr )) {MessageBox ("err20") ;}# endif // set the display area HWND m_hwndScreen = m_Screen.GetSafeHwnd (); RECT rc; hr = pivw-> put_Owner (OAHWND) m_hwndScreen ); hr = pivw-> put_WindowStyle (WS_CHILD | WS_CLIPCHILDREN); m_Screen.GetClientRect (& rc); hr = pivw-> SetWindowPosition (0, 0, (rc. right), (rc. bottom); // play hr = pimc-> Run ();}





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.