Click here to Skip to main content
15,886,837 members
Articles / Multimedia / DirectX
Article

Using the DirectShow Video Mixing Renderer 9 filter

Rate me:
Please Sign up or sign in to vote.
4.58/5 (30 votes)
1 Feb 20052 min read 788.1K   11.4K   93   114
This article describes how to dynamically mix two video files (.mpeg, .mpg, .avi and .dat). Mixing involves alpha-blending and stretching/shrinking and positioning of the two video streams, individually, using DirectShow's VMR9 filter.

Sample Image - DirectShowVMR91.jpg

Introduction

This article shows the steps involved in creating and configuring DirectShow’s Video Mixing Renderer Filter 9 (VMR9). The two video streams, one on top of the other, are rendered on a single surface. This surface, in our case, is a PictureBox control. Each stream's alpha value, position and height/width can be adjusted at runtime.

How VMR9 is different

The following diagrams show the difference between rendering two videos with VMR9 and without VMR9.

Without VMR9

Rendering without VMR9

We notice that simply rendering two videos will result in two separate Video Renderers, which means that the videos are being played on two separate surfaces.

With VMR9

Rendering with VMR9

In this case, the VMR9 filter directs both video streams into its own input pins. This means there is only one renderer, and thus a single rendering surface for both video streams.

The Working

To enhance reusability and readability factors, the functionality of the VMR9 filter has been encapsulated inside a class named myVMR9.

The myVMR9 class

This class has the following private data members:

  • VMR9NormalizedRect *r;
  • IVMRWindowlessControl9 *pWC;
  • IVMRMixerControl9 *pMix;
  • IGraphBuilder *pGB;
  • IBaseFilter *pVmr;
  • IVMRFilterConfig9 *pConfig;
  • IMediaControl *pMC;
  • IMediaSeeking *pMS;

The constructor

The constructor receives a PictureBox's coordinates of type System::Drawing::Rectangle, along with its handler of type HWND. These two attributes are used by VMR9 for rendering purposes.

public: myVMR9(System::Drawing::Rectangle rect, HWND hwnd)
{
    // initialize video coordinates with normal values
    r = new VMR9NormalizedRect;
    r->left = 0;
    r->top = 0;
    r->right = 1;
    r->bottom = 1;

    pWC = NULL;
    pMix = NULL;
    pGB = NULL;
    pVmr = NULL;
    pConfig = NULL;
    pMC = NULL;
    pMS = NULL;
    // create an instance of the Filter Graph Manager
    CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, 
        IID_IGraphBuilder, (void **)&pGB);
    // create an instance of the VMR9 filter
    CoCreateInstance(CLSID_VideoMixingRenderer9, NULL, CLSCTX_INPROC,
        IID_IBaseFilter, (void**)&pVmr);
    // add the VMR9 filter to the Graph Manager
    pGB->AddFilter(pVmr, L"Video");    
    // get a pointer to the IVMRFilterConfig9 interface
    pVmr->QueryInterface(IID_IVMRFilterConfig9, (void**)&pConfig);
    // make sure VMR9 is in windowless mode
    pConfig->SetRenderingMode(VMR9Mode_Windowless);
    // get a pointer to the IVMRWindowlessControl9 interface 
    pVmr->QueryInterface(IID_IVMRWindowlessControl9, (void**)&pWC);
    // explicitly convert System::Drawing::Rectangle type to RECT type
    RECT rcDest = {0};
    rcDest.bottom = rect.Bottom;
    rcDest.left = rect.Left;
    rcDest.right = rect.Right;
    rcDest.top = rect.Top;

    // set destination rectangle for the video
    pWC->SetVideoPosition(NULL, &rcDest);

    // specify the container window that the video should be clipped to    
    pWC->SetVideoClippingWindow(hwnd);
    // IVMRMixerControl manipulates video streams
    pVmr->QueryInterface(IID_IVMRMixerControl9, (void**)&pMix);
    // IMediaSeeking seeks to a position in the video stream
    pGB->QueryInterface(IID_IMediaSeeking, (void **)&pMS);
    // IMediaControl controls flow of data through the graph
    pGB->QueryInterface(IID_IMediaControl, (void **)&pMC);
}

The methods

HRESULT play()
{
    pMC->Run(); return
    S_OK;
}

HRESULT pause()
{
    pMC->Pause();
    return S_OK;
}

HRESULT stop()
{
    LONGLONG pos = 0;
    pMC->Stop();
    pMS->SetPositions(&pos, AM_SEEKING_AbsolutePositioning, 
                      NULL,AM_SEEKING_NoPositioning);
    pMC->Pause();
    return S_OK;
}

HRESULT close()
{
    // make sure resources are freed
    SAFE_RELEASE(pWC);
    SAFE_RELEASE(pMix);
    SAFE_RELEASE(pGB);
    SAFE_RELEASE(pVmr);
    SAFE_RELEASE(pConfig);
    SAFE_RELEASE(pMC);
    SAFE_RELEASE(pMS);
    return S_OK;
}

HRESULT setAlpha(DWORD stream, float alpha)
{
    // set alpha of specified video stream
    pMix->SetAlpha(stream, alpha);
    return S_OK;
}

HRESULT setX(DWORD stream, float x)
{
    // video displacement along x-axis
    r->right = x + (r->right - r->left);
    r->left = x;
    pMix->SetOutputRect(stream, r);
    return S_OK;
}

HRESULT setY(DWORD stream, float y)
{
    // video displacement along y-axis
    r->bottom = y + (r->bottom - r->top);
    r->top = y;
    pMix->SetOutputRect(stream, r);
    return S_OK;
}

HRESULT setW(DWORD stream, float w)
{
    // video stretching/shrinking along x-axis
    r->right = r->left + w;
    pMix->SetOutputRect(stream, r);
    return S_OK;
}

HRESULT setH(DWORD stream, float h)
{
    // video stretching/shrinking along y-axis
    r->bottom = r->top + h;
    pMix->SetOutputRect(stream, r);
    return S_OK;
}

HRESULT renderFiles(String* file1, String* file2)
{
    // convert String type to LPCSTR type and render the videos
    LPCTSTR lFile;
    lFile = 
      static_cast<LPCTSTR>(const_cast<void*>(static_cast<const void*>
      (System::Runtime::InteropServices::Marshal::StringToHGlobalAuto(file1))));
    pGB->RenderFile((LPCWSTR)lFile, NULL);
    lFile = 
      static_cast<LPCTSTR>(const_cast<void*>(static_cast<const void*>
      (System::Runtime::InteropServices::Marshal::StringToHGlobalAuto(file2))));
    pGB->RenderFile((LPCWSTR)lFile, NULL);
    System::Runtime::InteropServices::Marshal::FreeHGlobal
      (static_cast<IntPtr>(const_cast<void*>
      (static_cast<const void*>(lFile))));
    pMC->StopWhenReady();
    return S_OK;
}

Now that the VMR9's functionality has been separated from the GUI, Button and TrackBar handlers can simply create a pointer to a myVMR9 object and call the required methods.

Sample screenshot

Additional Information

  • The second video stream opened is on top of the first one, i.e., file-2 video is rendered on top of file-1 video. Therefore, if the first video's alpha value is a 100% and the second video's alpha value is 50%, then both videos will be equally (50%) visible.
  • It should be noted that the values of Width and Height of trackbars can run into negative values. So when a video stream's width is -100%, it is laterally inverted. Similarly, when a video stream's height is -100%, the video is upside down.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalgreenish lines on video using VMR9 Pin
shovit25-Oct-06 20:29
shovit25-Oct-06 20:29 
GeneralFull Screen [modified] Pin
alanmark5-Oct-06 4:02
alanmark5-Oct-06 4:02 
Questionenumerate pins Pin
Fariha Atta1-Oct-06 17:57
Fariha Atta1-Oct-06 17:57 
AnswerRe: enumerate pins Pin
Sameer Ahmed2-Oct-06 11:08
Sameer Ahmed2-Oct-06 11:08 
GeneralSound Pin
Tharkunius26-Sep-06 23:38
Tharkunius26-Sep-06 23:38 
GeneralRe: Sound Pin
Sameer Ahmed2-Oct-06 10:29
Sameer Ahmed2-Oct-06 10:29 
QuestionUrgent !!! No errors in code but still cant see the video :-( [modified] Pin
~Jabeen~3-Sep-06 21:21
~Jabeen~3-Sep-06 21:21 
Assalamoalikum!

i have tried your code as the way it is

It gives no errors but i cant see the video ... now i am stuck Confused | :confused:

My scenario is that i have a preview mode and a capture mode

I have implemented "preview mode" through VMR in the preview window.

It works fine through it.

Now about the problem i am facing i have a child window that starts showing the same preview on button click in it( say "capture window").

I had implemented this scenario through GRABBER but i want to do it through VMR although i am running an instance of appilication in the same application and on the same dialog


Through your code i got no errors through out i mean till
m_hr=pMC->Run();
i get no errors still i cant preview the video

more over i am getting the rectangle of the child window as

<br />
RECT rcDest;<br />
::GetClientRect (hWnd,&rcDest);<br />



still i cant preview it Frown | :( how to proceed???





Thanks and regards



if you want to look through Code then its like

<br />
/*<br />
<br />
m_DlghWnd: the handle of the dialog which have both child windows that is preview (for which i have written the code through VMR) and the capture window which will show the image stream on button click<br />
<br />
<br />
hWnd: it is the handle of the child window in which i want to start the preview stream<br />
<br />
<br />
Requirement: Both windows should be previewed after the button click <br />
<br />
*/<br />
<br />
HRESULT CCameraPreview::TestVideo(HWND m_DlghWnd,HWND hWnd)<br />
{<br />
<br />
	r = new VMR9NormalizedRect;<br />
    r->left = 0;<br />
    r->top = 0;<br />
    r->right = 1;<br />
    r->bottom = 1;<br />
<br />
    pWC = NULL;<br />
    pMix = NULL;<br />
    pGB = NULL;<br />
    pVmr = NULL;<br />
    pConfig = NULL;<br />
    pMC = NULL;<br />
    pMS = NULL;<br />
<br />
    // create an instance of the Filter Graph Manager<br />
    m_hr=CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, <br />
        IID_IGraphBuilder, (void **)&pGB);<br />
    // create an instance of the VMR9 filter<br />
    m_hr=CoCreateInstance(CLSID_VideoMixingRenderer9, NULL, CLSCTX_INPROC,<br />
        IID_IBaseFilter, (void**)&pVmr);<br />
    // add the VMR9 filter to the Graph Manager<br />
    m_hr=pGB->AddFilter(pVmr, L"Video");    <br />
    // get a pointer to the IVMRFilterConfig9 interface<br />
    m_hr=pVmr->QueryInterface(IID_IVMRFilterConfig9, (void**)&pConfig);<br />
    // make sure VMR9 is in windowless mode<br />
    m_hr=pConfig->SetRenderingMode(VMR9Mode_Windowless);<br />
    // get a pointer to the IVMRWindowlessControl9 interface <br />
    m_hr=pVmr->QueryInterface(IID_IVMRWindowlessControl9, (void**)&pWC);<br />
    // explicitly convert System::Drawing::Rectangle type to RECT type<br />
<br />
	<br />
	RECT rcDest;<br />
	::GetClientRect (hWnd,&rcDest);<br />
	<br />
<br />
      // set destination rectangle for the video<br />
    m_hr=pWC->SetVideoPosition(NULL, &rcDest);<br />
<br />
    // specify the container window that the video should be clipped to    <br />
    m_hr=pWC->SetVideoClippingWindow(hWnd);<br />
    // IVMRMixerControl manipulates video streams<br />
    m_hr=pVmr->QueryInterface(IID_IVMRMixerControl9, (void**)&pMix);<br />
    // IMediaSeeking seeks to a position in the video stream<br />
    m_hr=pGB->QueryInterface(IID_IMediaSeeking, (void **)&pMS);<br />
    // IMediaControl controls flow of data through the graph<br />
    m_hr=pGB->QueryInterface(IID_IMediaControl, (void **)&pMC);<br />
	m_hr=pMC->Run();<br />
   return m_hr;<br />




-- modified at 3:45 Monday 4th September, 2006


Jabeen

AnswerRe: Urgent !!! No errors in code but still cant see the video :-( Pin
Sameer Ahmed5-Sep-06 1:58
Sameer Ahmed5-Sep-06 1:58 
Questionvideo mixing problem [modified] Pin
Fariha Atta17-Aug-06 16:06
Fariha Atta17-Aug-06 16:06 
AnswerRe: video mixing problem [modified] Pin
Sameer Ahmed17-Aug-06 18:22
Sameer Ahmed17-Aug-06 18:22 
GeneralRe: video mixing problem Pin
Fariha Atta18-Aug-06 5:04
Fariha Atta18-Aug-06 5:04 
GeneralRe: vmr9 Pin
Sameer Ahmed15-Aug-06 23:55
Sameer Ahmed15-Aug-06 23:55 
Questionproblem in implementing a filter graph thru Filter Graph Manager Pin
Fariha Atta28-Jul-06 16:22
Fariha Atta28-Jul-06 16:22 
AnswerRe: problem in implementing a filter graph thru Filter Graph Manager Pin
Sameer Ahmed29-Jul-06 1:36
Sameer Ahmed29-Jul-06 1:36 
GeneralFull screen switching mode Pin
gabrilestrimtu9-Jun-06 7:45
gabrilestrimtu9-Jun-06 7:45 
GeneralRe: Full screen switching mode Pin
jimerino18-Apr-07 7:35
jimerino18-Apr-07 7:35 
Generalsir,i got some error complied in .net Pin
chenhuasheng8-May-06 18:44
chenhuasheng8-May-06 18:44 
GeneralText Mixing Pin
jeykumars27-Apr-06 23:11
jeykumars27-Apr-06 23:11 
GeneralRe: Text Mixing Pin
Sameer Ahmed28-Apr-06 2:06
Sameer Ahmed28-Apr-06 2:06 
GeneralRe: AoA Sameer! Pin
Sameer Ahmed29-Jul-06 1:49
Sameer Ahmed29-Jul-06 1:49 
GeneralGreat stuff Pin
RYoung4-Mar-06 7:36
RYoung4-Mar-06 7:36 
GeneralRe: Great stuff Pin
Sameer Ahmed4-Mar-06 8:14
Sameer Ahmed4-Mar-06 8:14 
Generaloops Pin
GraceNetwork23-Feb-06 6:35
GraceNetwork23-Feb-06 6:35 
Questionoutput render video to file? Pin
GraceNetwork22-Feb-06 14:10
GraceNetwork22-Feb-06 14:10 
GeneralI am getting erros trying to compile/ Pin
GraceNetwork22-Feb-06 14:04
GraceNetwork22-Feb-06 14:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.