Problem with compression of a DirectShow video filter
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Problem with compression of a DirectShow video filter

  1. #1
    Join Date
    Jan 2011
    Posts
    4

    Question Problem with compression of a DirectShow video filter

    Hi all, I'm having problems in setting compression parameters for a directshow video filter for a video and audio grabbed from a usb webcam.

    The code I am using (adapted from various websites on the internet) is the following (removed error checking to improve readability):

    Code:
    	//... various code before
    	hr=m_pGraph->AddFilter(m_pVidDeviceFilter,L"Vid Capture Filter");
    	hr=m_pGraph->AddFilter(m_pAudDeviceFilter,L"Aud Capture Filter");
    	
    	hr = CoCreateInstance(CLSID_WMAsfWriter,NULL,CLSCTX_INPROC_SERVER, 
                          IID_IBaseFilter, (void **) &m_pWMASFWritter); 
    
    	
    
    	//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    	// !!! Try to add compression from now on !!!!
    
    	DWORD dwBitRate = 2670000; // I tried almost everything here
    	DWORD dwQuality = 50; // in range[0-100]
    	DWORD dwSecPerKey = 5000;
    
    	// Initialize a new Profile Manager
    	IWMProfileManager* pIPM = 0;
    	WMCreateProfileManager(&pIPM);
    
    	// Create a new empty profile
    	IWMProfile* pIProf = 0;
    	pIPM->CreateEmptyProfile(WMT_VER_9_0,&pIProf);
    	pIProf->SetName(L"Window Media Video 9 Profile");
    	pIProf->SetDescription (L"Window Media Video 9 Custom Encoding");
    
    	// Create a new video stream
    	IWMStreamConfig* pVideoStream = 0;
    	pIProf->CreateNewStream(WMMEDIATYPE_Video,&pVideoStream);
    
    	pVideoStream->SetStreamName(L"videostream");;
    	pVideoStream->SetConnectionName(L"video");
    	pVideoStream->SetBitrate(662144); // This causes problems
    	pVideoStream->SetBufferWindow(3000);
    	pVideoStream->SetStreamNumber(1);
    
    	IWMVideoMediaProps* pVideoMediaProps = 0;
    
    	// Get this Interface from pVideoStream...
    	hr = pVideoStream->QueryInterface(IID_IWMMediaProps, (void**)&pVideoMediaProps);
    
    	WM_MEDIA_TYPE sMediaType;
    	WMVIDEOINFOHEADER sVideoInfoHeader;
    	memset(&sMediaType, 0, sizeof(sMediaType));
    	sMediaType.majortype = WMMEDIATYPE_Video;
    	sMediaType.subtype = WMMEDIASUBTYPE_MPEG2_VIDEO;
    	sMediaType.bFixedSizeSamples = FALSE;
    	sMediaType.bTemporalCompression = TRUE;
    	sMediaType.formattype = WMFORMAT_VideoInfo;
    	sMediaType.cbFormat = sizeof(WMVIDEOINFOHEADER);
    	sMediaType.pbFormat = (BYTE *)&sVideoInfoHeader;
    
    	// Setup the Video Info Header
    	memset(&sVideoInfoHeader, 0, sizeof(sVideoInfoHeader));
    	sVideoInfoHeader.rcSource.left = 0;
    	sVideoInfoHeader.rcSource.top = 0;
    	sVideoInfoHeader.rcSource.right = 352;
    	sVideoInfoHeader.rcSource.bottom = 240;
    	sVideoInfoHeader.rcTarget.left = 0;
    	sVideoInfoHeader.rcTarget.top = 0;
    	sVideoInfoHeader.rcTarget.right = 352;
    	sVideoInfoHeader.rcTarget.bottom = 240;
    	sVideoInfoHeader.dwBitRate = 262144;
    	sVideoInfoHeader.AvgTimePerFrame = 333667; // (1/29.97) in 100-ns units
    
    	sVideoInfoHeader.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    	sVideoInfoHeader.bmiHeader.biWidth = 352;
    	sVideoInfoHeader.bmiHeader.biHeight = 240;
    	sVideoInfoHeader.bmiHeader.biPlanes = 1;
    	sVideoInfoHeader.bmiHeader.biBitCount = 24;
    	sVideoInfoHeader.bmiHeader.biCompression = MAKEFOURCC(0xe4,0x36,0xeb,0x83);
    	sVideoInfoHeader.bmiHeader.biSizeImage = 0;
    	sVideoInfoHeader.bmiHeader.biXPelsPerMeter = 0;
    	sVideoInfoHeader.bmiHeader.biYPelsPerMeter = 0;
    	sVideoInfoHeader.bmiHeader.biClrUsed = 0;
    	sVideoInfoHeader.bmiHeader.biClrImportant = 0;
    
    	// Set the VideoMediaProps for this Stream
    	pVideoMediaProps->SetMediaType(&sMediaType);
    	// pVideoMediaProps->SetMaxKeyFrameSpacing(1000000);
    	// pVideoMediaProps->SetQuality(100);
    	pVideoMediaProps->Release();
    
    
    	hr = pIProf->AddStream(pVideoStream); ////// hr IS ALWAYS NS_E_INVALID_STREAM HERE!! PROBLEM!
    
    	if (hr == NS_E_INVALID_STREAM)
    	{
    
    		// Here the problem!
    		
    
    	}
    
    	// Set the profile for the writer
    	CComQIPtr<IConfigAsfWriter2> pConfigWriter;
    	pConfigWriter = m_pWMASFWritter;
    
    	hr = pConfigWriter->SetParam(AM_CONFIGASFWRITER_PARAM_DONTCOMPRESS, FALSE, 0);
    	hr = pConfigWriter->ConfigureFilterUsingProfile(pIProf);
    
    
    
    	//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    	// !!! End of compression code... continue with streaming/networking code.. !!!
    	
    	hr=m_pGraph->AddFilter(m_pWMASFWritter,L"ASF Writter");
    	
    	hr = m_pWMASFWritter->QueryInterface( IID_IFileSinkFilter, (void**)&m_pFileSinkFilter );
    
    	hr = m_pFileSinkFilter->SetFileName(L"C:\\test.wmv", NULL);
    	
    	//.... etc....
    Can someone point me in the right direction please?

  2. #2
    Join Date
    Jan 2011
    Posts
    4

    Re: Problem with compression of a DirectShow video filter

    Please ignore this post, discussion is updated here: http://www.codeguru.com/forum/showthread.php?t=507553

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center