/******************************Public*Routine******************************\ * AddVideoMixingRendererToFG * \**************************************************************************/ HRESULT CMovie::AddVideoMixingRendererToFG() { IBaseFilter* pBF = NULL; HRESULT hRes = CoCreateInstance(CLSID_VideoMixingRenderer9, NULL, CLSCTX_INPROC, IID_IBaseFilter, (LPVOID *)&pBF); if(SUCCEEDED(hRes)) { hRes = m_Fg->AddFilter(pBF, L"Video Mixing Renderer 9"); if(SUCCEEDED(hRes)) { // Test VMRConfig, VMRMonitorConfig IVMRFilterConfig9* pConfig; HRESULT hRes2 = pBF->QueryInterface(IID_IVMRFilterConfig9, (LPVOID *)&pConfig); if(SUCCEEDED(hRes2)) { hRes2 = pConfig->SetNumberOfStreams(2); hRes2 = pConfig->SetRenderingMode(VMR9Mode_Windowless); //hRes2 = pConfig->SetRenderingPrefs(RenderPrefs_AllowOverlays); pConfig->Release(); } IVMRMonitorConfig9* pMonitorConfig; HRESULT hRes3 = pBF->QueryInterface(IID_IVMRMonitorConfig9, (LPVOID *)&pMonitorConfig); if(SUCCEEDED(hRes3)) { UINT iCurrentMonitor; HRESULT hr4 = pMonitorConfig->GetMonitor(&iCurrentMonitor); pMonitorConfig->Release(); } hRes = pBF->QueryInterface(IID_IVMRWindowlessControl9, (LPVOID *)&m_Wc); } } if(pBF) { pBF->Release(); } if(SUCCEEDED(hRes)) { HRESULT hr = m_Wc->SetVideoClippingWindow(m_hwndApp); hr = m_Wc->SetAspectRatioMode(VMR_ARMODE_LETTER_BOX); } else { if(m_Wc) { m_Wc->Release(); m_Wc = NULL; } } return hRes; }
void Video::ConfigureVMR9() { HRESULT hr; IVMRFilterConfig9* pVMRConfig; ASSERT( SUCCEEDED( hr = m_pVMR9->QueryInterface( IID_IVMRFilterConfig9, (LPVOID*) &pVMRConfig ) ), "Failed to retrieve VMR9 config!" ); pVMRConfig->SetRenderingMode( VMR9Mode_Renderless ); pVMRConfig->SetNumberOfStreams( 1 ); pVMRConfig->Release(); ASSERT( SUCCEEDED( hr = m_pVMR9->QueryInterface( IID_IVMRSurfaceAllocatorNotify9, (LPVOID*) &m_pVMRAllocatorNotify ) ), "Failed to retrieve VMR9 surface allocator!" ); AdviseNotify( m_pVMRAllocatorNotify ); ASSERT( SUCCEEDED( hr = m_pVMRAllocatorNotify->AdviseSurfaceAllocator( 0x1234, this ) ), "Failed to replace the allocator/renderer for VRM9!" ); // Use our class as interface to allocator/presenter }
/******************************Public*Routine******************************\ * SetRenderingMode * \**************************************************************************/ static HRESULT SetRenderingMode( IBaseFilter* pBaseFilter, VMR9Mode mode ) { // Test VMRConfig, VMRMonitorConfig IVMRFilterConfig9* pConfig; HRESULT hr = pBaseFilter->QueryInterface(IID_IVMRFilterConfig9, (LPVOID *)&pConfig); if(SUCCEEDED(hr)) { hr = pConfig->SetRenderingMode(mode); hr = pConfig->SetRenderingPrefs(RenderPrefs_ForceOverlays|RenderPrefs_AllowOverlays); pConfig->Release(); } return hr; }
/******************************Public*Routine******************************\ * BuildAndRender * * 位图源 \**************************************************************************/ HRESULT CVMR9Subgraph::BuildAndRender(UINT id,UINT imgtp,LPRECT rcSrc,DWORD ColorKey, IMultiVMR9Wizard* pWizard ) { HRESULT hr = S_OK; if(!pWizard)return E_FAIL; if(m_pGraph) return E_FAIL; IVMRFilterConfig9 * pConfig = 0; IGraphBuilder * pGb= 0; IDirect3DSurface9 * pTexturePrivSurf = NULL; IDirect3DTexture9 * pTexture = 0; IBaseFilter * pVMR9 = 0; VMR9AllocationInfo info; info.dwFlags = VMR9AllocFlag_OffscreenSurface; info.dwHeight = rcSrc->bottom - rcSrc->top; info.dwWidth = rcSrc->bottom - rcSrc->left; info.Format = (D3DFORMAT)imgtp; info.MinBuffers = 1; info.Pool = D3DPOOL_SYSTEMMEM; info.szAspectRatio.cx = 0;//rcDest->right - rcDest->left; info.szAspectRatio.cy =0;// rcDest->bottom - rcDest->top; info.szNativeSize.cy = rcSrc->right - rcSrc->left; info.szNativeSize.cy = rcSrc->bottom - rcSrc->top; // USES_CONVERSION; m_GraphType = Image_DIB; try { // create graph hr = CoCreateInstance( CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IFilterGraph, (void**)&(m_pGraph) ); if( FAILED(hr))throw hr; if(!m_pGraph) throw E_OUTOFMEMORY; // create and add VMR9 hr = CoCreateInstance( CLSID_VideoMixingRenderer9, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void**)&(pVMR9) ); if( FAILED(hr))throw hr; hr = m_pGraph->AddFilter( pVMR9, L"VMR9"); if( FAILED(hr))throw hr; // configure VMR9 hr = pVMR9->QueryInterface( IID_IVMRFilterConfig9, (void**)&(pConfig) ); if( FAILED(hr))throw hr; // if wizard is provided, set VMR to the renderless code and attach to the wizard if( pWizard ) { // set VMR to the renderless mode hr = pConfig->SetRenderingMode( VMR9Mode_Renderless ); if( FAILED(hr))throw hr; hr = pWizard->Attach( pVMR9, D3DFMT_A8R8G8B8,& m_dwID); if( FAILED(hr))throw hr; DWORD num = 1; hr = pWizard->InitializeDevice(m_dwID,&info,&num); if( FAILED(hr))throw hr; hr = pWizard->GetTexture(m_dwID,&pTexture); if( FAILED(hr))throw hr; hr = pTexture->GetSurfaceLevel(0,&pTexturePrivSurf); if( FAILED(hr))throw hr; //CDC::GetCurrentBitmap(imgtp hr = D3DXLoadSurfaceFromResource( pTexturePrivSurf, NULL, 0, GetModuleHandle(NULL), MAKEINTRESOURCE(id), rcSrc, D3DX_DEFAULT, ColorKey, 0 ); if( FAILED(hr))throw hr; if(!pTexturePrivSurf)throw E_OUTOFMEMORY; } // try to render media source hr = m_pGraph->QueryInterface( IID_IGraphBuilder, (void**)&(pGb) ); if( FAILED(hr))throw hr; // pWizard->InitializeDevice(); // ok, all is rendered, now get MediaControl, MediaSeeking and continue hr = m_pGraph->QueryInterface( IID_IMediaControl, (void**)&(m_pMc) ); if( FAILED(hr))throw hr; hr = m_pGraph->QueryInterface( IID_IMediaSeeking, (void**)&(m_pMs) ); if( FAILED(hr))throw hr; } catch(HRESULT hr1) { pWizard->Detach( m_dwID ); hr = hr1; } RELEASE(pVMR9); RELEASE(pConfig ); RELEASE(pGb ); RELEASE(pTexturePrivSurf ); RELEASE(pTexture ); //AddCaptureFilter(pWizard); return hr; }
/******************************Public*Routine******************************\ * BuildAndRender * * 采集卡源 \**************************************************************************/ HRESULT CVMR9Subgraph::BuildAndRender(IBaseFilter* pCap , GUID VidType ,UINT Width,UINT Height,int nFPS,IMultiVMR9Wizard* pWizard ,BOOL bUsingColorSpace,SourceConnectProc ConnectProc) { HRESULT hr = S_OK; if(!pWizard)return E_FAIL; if(!pCap)return E_FAIL; if(m_pGraph) return E_FAIL; IVMRFilterConfig9 * pConfig = NULL; IGraphBuilder * pGb= NULL; IBaseFilter * pColorSpace = 0; IBaseFilter * pVMR9 = 0; ICaptureGraphBuilder2 * pBuild = NULL; //IBaseFilter* pCap = NULL; IPin * pPin = NULL; IAMStreamConfig *pStrCfig = 0; AM_MEDIA_TYPE * mmt = 0; //m_DeviceId = DeviceId; m_GraphType = Capture_Device; m_ConnectProc = ConnectProc; CMediaHelper Helper; // create graph try { hr = CoCreateInstance( CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IFilterGraph, (void**)&(m_pGraph) ); if( FAILED(hr))throw hr;if(!m_pGraph)throw E_OUTOFMEMORY; //-- hr = m_pGraph->QueryInterface( IID_IGraphBuilder, (void**)&(pGb) ); if( FAILED(hr))throw hr;if(!pGb)throw E_OUTOFMEMORY; //-- hr = CoCreateInstance(CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC, IID_ICaptureGraphBuilder2, (void **)&pBuild); if( FAILED(hr))throw hr;if(!pBuild)throw E_OUTOFMEMORY; //-- hr =pBuild->SetFiltergraph(pGb); if( FAILED(hr))throw hr; //-- // create and add VMR9 hr = CoCreateInstance( CLSID_VideoMixingRenderer9, NULL, CLSCTX_INPROC, IID_IBaseFilter, (void**)&(pVMR9) ); if( FAILED(hr))throw hr;if(! pVMR9)throw E_OUTOFMEMORY; //-- hr = m_pGraph->AddFilter( pVMR9, L"VMR9"); if( FAILED(hr))throw hr; //-- hr = pVMR9->QueryInterface( IID_IVMRFilterConfig9, (void**)&(pConfig) ); if( FAILED(hr))throw hr; //-- // set VMR to the renderless mode hr = pConfig->SetRenderingMode( VMR9Mode_Renderless ); //-- hr = pWizard->Attach( pVMR9,D3DFMT_UNKNOWN, &m_dwID ); if( FAILED(hr))throw hr; if(bUsingColorSpace){ hr = CoCreateInstance( CLSID_Colour, NULL, CLSCTX_INPROC, IID_IBaseFilter, (void**)&(pColorSpace) ); if( FAILED(hr))throw hr;if( !pColorSpace)throw E_OUTOFMEMORY; //-- hr = m_pGraph->AddFilter( pColorSpace, L"ColorSpace"); if( FAILED(hr))throw hr; } ////连接采集卡 //hr = Helper.GetVidCapDevice(DeviceId,&pCap); //if( FAILED(hr))throw hr; //if(!pCap)throw E_OUTOFMEMORY; hr = m_pGraph->AddFilter(pCap,L"Capture"); if( FAILED(hr))throw hr; if(m_ConnectProc) { hr = m_ConnectProc(m_dwID,m_pGraph,pCap,pVMR9); } else{//使用默认连接方法 hr = pBuild->FindInterface(&PIN_CATEGORY_CAPTURE,&MEDIATYPE_Interleaved,pCap,IID_IAMStreamConfig,(void **)&pStrCfig); if( FAILED( hr) ){ hr = pBuild->FindInterface(&PIN_CATEGORY_CAPTURE,&MEDIATYPE_Video,pCap,IID_IAMStreamConfig,(void **)&pStrCfig); if (FAILED(hr ))throw hr; } hr = pStrCfig->GetFormat(&mmt); if(mmt->formattype == FORMAT_VideoInfo){ if (FAILED(hr ))throw hr; if(!mmt) throw E_OUTOFMEMORY; VIDEOINFO *pvi = (VIDEOINFO *) mmt->pbFormat; pvi->AvgTimePerFrame = UNITS/nFPS; pvi->bmiHeader.biWidth = Width; pvi->bmiHeader.biHeight = Height; mmt->subtype = VidType; // hr=pStrCfig->SetFormat(mmt); //重新设置参数 if( FAILED(hr))throw hr; } /*hr = pBuild->FindPin( pCap, PINDIR_OUTPUT, &PIN_CATEGORY_CAPTURE,&MEDIATYPE_Interleaved, TRUE, 0, &pPin); if( FAILED(hr)){ hr = pBuild->FindPin( pCap, PINDIR_OUTPUT, &PIN_CATEGORY_CAPTURE,&MEDIATYPE_Video, TRUE, 0, &pPin); if (FAILED(hr ))throw hr; } if(!pPin) throw E_OUTOFMEMORY;*/ hr = pBuild->RenderStream(&PIN_CATEGORY_CAPTURE, &MEDIATYPE_Interleaved,pCap,pColorSpace,pVMR9); if( FAILED(hr)){ hr = pBuild->RenderStream(&PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video,pCap,pColorSpace,pVMR9); if (FAILED(hr ))throw hr; } } if( FAILED(hr))throw hr; // ok, all is rendered, now get MediaControl, MediaSeeking and continue hr = m_pGraph->QueryInterface( IID_IMediaControl, (void**)&(m_pMc) ); if( FAILED(hr))throw hr; hr = m_pGraph->QueryInterface( IID_IMediaSeeking, (void**)&(m_pMs) ); if( FAILED(hr))throw hr; } catch(HRESULT hr1) { pWizard->Detach( m_dwID ); hr = hr1; } RELEASE( pConfig ); RELEASE( pGb ); RELEASE( pColorSpace ); RELEASE( pBuild ); RELEASE( pStrCfig ); RELEASE( pVMR9); Helper.DeleteMediaType(mmt); return hr; }
/******************************Public*Routine******************************\ * BuildAndRender * * 视频文件源 \**************************************************************************/ HRESULT CVMR9Subgraph::BuildAndRender( LPCWSTR lpPath , IMultiVMR9Wizard* pWizard ,SourceConnectProc ConnectProc) { HRESULT hr = S_OK; if(!pWizard)return E_FAIL; if(m_pGraph) return E_FAIL; m_ConnectProc = ConnectProc; if( !lpPath ) { return E_POINTER; } IBaseFilter * pVMR9 = 0; IBaseFilter * pFileScr = 0; IVMRFilterConfig9 * pConfig = 0; IGraphBuilder * pGb = 0; ICaptureGraphBuilder2 * pBuild = NULL; IBaseFilter *pAviSplit = NULL; IPin*pPin = 0; USES_CONVERSION; m_GraphType = Media_File; try { // first, check that file exists if( INVALID_FILE_ATTRIBUTES == GetFileAttributes( lpPath))throw VFW_E_NOT_FOUND; // create graph hr = CoCreateInstance( CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IFilterGraph, (void**)&(m_pGraph) ); if( FAILED(hr))throw hr; // create and add VMR9 hr = CoCreateInstance( CLSID_VideoMixingRenderer9, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void**)&(pVMR9) ); if( FAILED(hr))throw hr; hr = m_pGraph->AddFilter( pVMR9, L"VMR9"); if( FAILED(hr))throw hr; // configure VMR9 hr = pVMR9->QueryInterface( IID_IVMRFilterConfig9, (void**)&(pConfig) ); if( FAILED(hr))throw hr; // if wizard is provided, set VMR to the renderless code and attach to the wizard if( pWizard ) { // set VMR to the renderless mode hr = pConfig->SetRenderingMode( VMR9Mode_Renderless ); if( FAILED(hr))throw hr; hr = pWizard->Attach( pVMR9, D3DFMT_UNKNOWN,&m_dwID ); if( FAILED(hr))throw hr; } // try to render media source hr = m_pGraph->QueryInterface( IID_IGraphBuilder, (void**)&(pGb) ); if( FAILED(hr))throw hr; hr = CoCreateInstance(CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC, IID_ICaptureGraphBuilder2, (void **)&pBuild); if( FAILED(hr))throw hr;if(!pBuild)throw E_OUTOFMEMORY; //-- hr = pBuild->SetFiltergraph(pGb); if( FAILED(hr))throw hr; hr = pGb->AddSourceFilter( lpPath, L"Source", &pFileScr); if( FAILED(hr))throw hr; if(m_ConnectProc) { hr = m_ConnectProc(m_dwID,m_pGraph,pFileScr,pVMR9); if( FAILED(hr))throw hr; }else { hr = CoCreateInstance(CLSID_AviSplitter, 0, CLSCTX_INPROC_SERVER,IID_IBaseFilter, reinterpret_cast<void**>(&pAviSplit)); if( FAILED(hr))throw hr; hr = m_pGraph->AddFilter(pAviSplit, L"AVI Splitter"); if( FAILED(hr))throw hr; hr = pBuild->RenderStream(0, 0,pFileScr,pAviSplit,pVMR9); if( FAILED(hr))throw hr; } // ok, all is rendered, now get MediaControl, MediaSeeking and continue hr = m_pGraph->QueryInterface( IID_IMediaControl, (void**)&(m_pMc) ); if( FAILED(hr))throw hr; hr = m_pGraph->QueryInterface( IID_IMediaSeeking, (void**)&(m_pMs) ); if( FAILED(hr))throw hr; } catch(HRESULT hr1) { pWizard->Detach( m_dwID ); hr = hr1; } RELEASE(pConfig); RELEASE(pGb); RELEASE(pVMR9); RELEASE(pFileScr); RELEASE(pAviSplit); RELEASE(pBuild); RELEASE(pPin); //AddCaptureFilter(pWizard); return hr; }