HRESULT ISampleCaptureGraphBuilder::RenderToMPEG2Demux( IPin *pPin, const REGPINMEDIUM& pinMedium, IEnumMoniker *pEncoders ) { // // The pin has a special medium, // there shold be an encoder with the same // medium // SmartPtr< IBaseFilter > pEncoder; HRESULT hr = FindEncoder( pEncoders, pinMedium, &pEncoder ); if( FAILED( hr ) ) { return hr; } hr = graph_->AddFilter( pEncoder, L"Encoder" ); if( FAILED( hr ) ) { return hr; } hr = ConnectPin( pPin, pEncoder ); if( FAILED( hr ) ) { hr = graph_->RemoveFilter( pEncoder ); return hr; } // // the video pin was rendered to the same // ( hardware? ) encoder with the same mediu // hr = ConnectEncoderToMPEG2Demux( pEncoder, pinMedium ); if( FAILED( hr ) ) { hr = graph_->RemoveFilter( pEncoder ); return hr; } pEncoder_ = pEncoder; return S_OK; }
void EncodeTransform::Init(int width, int height) { VTUNE_TASK(g_pDomain, "Encoder Init"); mStreamHeight = height; mStreamWidth = width; mCompressedBuffer = new DWORD[(width*height) / 2]; HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); if (SUCCEEDED(hr) || (FAILED(hr) && hr == RPC_E_CHANGED_MODE)) { hr = FindEncoder(MFVideoFormat_H264); if (FAILED(hr)) { std::cout << "failed to find/Create specified encoder mft" << std::endl; } // set the media output info hr = SetOutputMediaType(); if (FAILED(hr)) { std::cout << "failed to SetOutputMediaType " << std::endl; } // set the media input info hr = SetInputMediaType(); if (FAILED(hr)) { std::cout << "failed to SetInputMediaType" << std::endl; } // query media input stream info hr = QueryInputStreamInfo(); if (FAILED(hr)) { std::cout << "failed to QueryInputStreamInfo" << std::endl; } { //used to be NUM_PIXELS_YUY2 * 4 HRESULT hr = MFCreateMemoryBuffer((mStreamWidth*mStreamHeight) * 2, &mpInputBuffer); if (FAILED(hr)) { std::cout << "Failed to MFCreateMemoryBuffer" << std::endl; } hr = MFCreateSample(&pSampleProcIn); if (FAILED(hr)) { std::cout << "Failed to MFCreateSample" << std::endl; } hr = pSampleProcIn->AddBuffer(mpInputBuffer); if (FAILED(hr)) { std::cout << "Failed to AddBuffer to sample" << std::endl; } } { //used to be NUM_PIXELS_YUY2 * 4 hr = MFCreateMemoryBuffer(mStreamWidth * mStreamWidth * 2, &mpEncodedBuffer); if (FAILED(hr)) { std::cout << "Failed to MFCreateMemoryBuffer" << std::endl; } hr = MFCreateSample(&pSampleProcOut); if (FAILED(hr)) { std::cout << "Failed to MFCreateSample" << std::endl; } hr = pSampleProcOut->AddBuffer(mpEncodedBuffer); if (FAILED(hr)) { std::cout << "Failed to AddBuffer to sample" << std::endl; } } } }
HRESULT ISampleCaptureGraphBuilder::ConnectAudioPinToMultiplexer( IPin *pPin, IBaseFilter *pMultiplexer) { if( !pPin || !pMultiplexer ) { return E_INVALIDARG; } REGPINMEDIUM pinMedium; HRESULT hr = GetMedium( pPin, pinMedium ); if( FAILED( hr ) ) { return hr; } SmartPtr<IBaseFilter> pEncoder; SmartPtr<IEnumMoniker> pEncoders; if( FALSE == ::IsEqualGUID( pinMedium.clsMedium, GUID_NULL ) ) { // // search through encoders category; identify // the encoder using the medium // hr = GetEncodersByCategory( &pEncoders ); if( FAILED( hr ) ) { return hr; } hr = FindEncoder( pEncoders, pinMedium, &pEncoder ); if( SUCCEEDED( hr ) ) { hr = graph_->AddFilter( pEncoder, L"Audio Encoder" ); if( SUCCEEDED( hr ) && SUCCEEDED( ConnectPin( pPin, pEncoder ) ) && SUCCEEDED( ConnectFilters( pEncoder, pMultiplexer ) ) ) { return S_OK; } } pEncoders = NULL; hr = GetEncodersByEnumerating( pPin, pinMedium, &pEncoders ); if( FAILED( hr ) ) { return hr; } hr = FindEncoder( pEncoders, pinMedium, &pEncoder ); if( SUCCEEDED( hr ) ) { hr = graph_->AddFilter( pEncoder, L"Audio Encoder" ); if( SUCCEEDED( hr ) && SUCCEEDED( ConnectPin( pPin, pEncoder ) ) && SUCCEEDED( ConnectFilters( pEncoder, pMultiplexer ) ) ) { return S_OK; } } return E_FAIL; } // // Search throgh the codec category // hr = GetEncodersByCategory( &pEncoders ); if( FAILED( hr ) ) { return hr; } SmartPtr<IBaseFilter> pFilter; SmartPtr<IMoniker> pMoniker; ULONG fetched; while( pFilter.Release(), pMoniker.Release(), S_OK == pEncoders->Next( 1, &pMoniker, &fetched ) ) { hr = pMoniker->BindToObject( 0, 0, IID_IBaseFilter, reinterpret_cast<void **>( &pFilter ) ); if( FAILED( hr ) ) { continue; } hr = graph_->AddFilter( pFilter, L"Audio Encoder" ); if( FAILED( hr ) ) { continue; } hr = ConnectPin( pPin, pFilter ); if( FAILED( hr ) ) { graph_->RemoveFilter( pFilter ); continue; } hr = ConnectFilters( pFilter, pMultiplexer ); if( SUCCEEDED( hr ) ) { return S_OK; } graph_->RemoveFilter( pFilter ); } return E_FAIL; }