Click here to Skip to main content
13,288,520 members (79,995 online)
Click here to Skip to main content
Add your own
alternative version


93 bookmarked
Posted 3 Jan 2005

Using the DirectShow Video Mixing Renderer 9 filter

, 1 Feb 2005
Rate this:
Please Sign up or sign in to vote.
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


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
    // 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; = rect.Top;

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

    // specify the container window that the video should be clipped to    
    // 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

HRESULT pause()
    return S_OK;

HRESULT stop()
    LONGLONG pos = 0;
    pMS->SetPositions(&pos, AM_SEEKING_AbsolutePositioning, 
    return S_OK;

HRESULT close()
    // make sure resources are freed
    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*>
    pGB->RenderFile((LPCWSTR)lFile, NULL);
    lFile = 
      static_cast<LPCTSTR>(const_cast<void*>(static_cast<const void*>
    pGB->RenderFile((LPCWSTR)lFile, NULL);
      (static_cast<const void*>(lFile))));
    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.


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


About the Author

Sameer Ahmed
Software Developer
Pakistan Pakistan
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralRe: video mixing problem Pin
fariha atta18-Aug-06 6:04
memberfariha atta18-Aug-06 6:04 
GeneralRe: vmr9 Pin
Sameer Ahmed16-Aug-06 0:55
memberSameer Ahmed16-Aug-06 0:55 
Questionproblem in implementing a filter graph thru Filter Graph Manager Pin
fariha atta28-Jul-06 17:22
memberfariha atta28-Jul-06 17:22 
AnswerRe: problem in implementing a filter graph thru Filter Graph Manager Pin
Sameer Ahmed29-Jul-06 2:36
memberSameer Ahmed29-Jul-06 2:36 
GeneralFull screen switching mode Pin
gabrilestrimtu9-Jun-06 8:45
membergabrilestrimtu9-Jun-06 8:45 
GeneralRe: Full screen switching mode Pin
jimerino18-Apr-07 8:35
memberjimerino18-Apr-07 8:35 
Generalsir,i got some error complied in .net Pin
chenhuasheng8-May-06 19:44
memberchenhuasheng8-May-06 19:44 
GeneralText Mixing Pin
jeykumars28-Apr-06 0:11
memberjeykumars28-Apr-06 0:11 
GeneralRe: Text Mixing Pin
Sameer Ahmed28-Apr-06 3:06
memberSameer Ahmed28-Apr-06 3:06 
GeneralRe: AoA Sameer! Pin
Sameer Ahmed29-Jul-06 2:49
memberSameer Ahmed29-Jul-06 2:49 
GeneralGreat stuff Pin
RYoung4-Mar-06 8:36
memberRYoung4-Mar-06 8:36 
GeneralRe: Great stuff Pin
Sameer Ahmed4-Mar-06 9:14
memberSameer Ahmed4-Mar-06 9:14 
Generaloops Pin
GraceNetwork23-Feb-06 7:35
memberGraceNetwork23-Feb-06 7:35 
Questionoutput render video to file? Pin
GraceNetwork22-Feb-06 15:10
memberGraceNetwork22-Feb-06 15:10 
Oh, I forgot. After I am done I would like to capture the rendered video to a file.

Do the VMR7 or VMR9 have output pins. Or does it require geting the DDraw surface and pull the video from there.

If so how do you keep the audio sinked.

I want to be able to edit just a section one of the embedded video streams, without having to reprocess the entire video.

So I need to capture, cut, reinsert and splice.

check out the liitle people att

GeneralI am getting erros trying to compile/ Pin
GraceNetwork22-Feb-06 15:04
memberGraceNetwork22-Feb-06 15:04 
QuestionVideo looping Pin
el3gans26-Jan-06 6:19
memberel3gans26-Jan-06 6:19 
QuestionVideo synchronized ? Pin
tomprout23-Nov-05 7:35
membertomprout23-Nov-05 7:35 
AnswerRe: Video synchronized ? Pin
Sameer Ahmed3-Dec-05 0:33
memberSameer Ahmed3-Dec-05 0:33 
QuestionStreaming results? Pin
Geert van Horrik1-Oct-05 23:28
memberGeert van Horrik1-Oct-05 23:28 
AnswerRe: Streaming results? [modified] Pin
Sameer Ahmed2-Oct-05 7:04
memberSameer Ahmed2-Oct-05 7:04 
GeneralRe: Streaming results? Pin
Geert van Horrik2-Oct-05 10:02
memberGeert van Horrik2-Oct-05 10:02 
GeneralRe: Streaming results? Pin
Sameer Ahmed7-Oct-05 8:47
memberSameer Ahmed7-Oct-05 8:47 
GeneralRe: Streaming results? Pin
Geert van Horrik7-Oct-05 13:15
memberGeert van Horrik7-Oct-05 13:15 
Generalplease , i have an error in dshow.h Pin
dynamica12330-Aug-05 4:11
memberdynamica12330-Aug-05 4:11 
GeneralRe: please , i have an error in dshow.h Pin
Sameer Ahmed30-Aug-05 5:21
memberSameer Ahmed30-Aug-05 5:21 
Generalplease , i need dshow.h Pin
dynamica12330-Jul-05 2:59
memberdynamica12330-Jul-05 2:59 
GeneralRe: please , i need dshow.h Pin
Sameer Ahmed30-Jul-05 9:37
memberSameer Ahmed30-Jul-05 9:37 
GeneralProblem with Video Mixing Renderer 9 Pin
tl0825426-Sep-05 22:57
membertl0825426-Sep-05 22:57 
GeneralRe: Problem with Video Mixing Renderer 9 Pin
Sameer Ahmed27-Sep-05 8:55
memberSameer Ahmed27-Sep-05 8:55 
Generalcreating Video Mixing Renderer 9 Pin
veligeti15-Jun-05 7:09
memberveligeti15-Jun-05 7:09 
GeneralRe: creating Video Mixing Renderer 9 Pin
Sameer Ahmed15-Jun-05 13:15
memberSameer Ahmed15-Jun-05 13:15 
QuestionMixing with Texture ??? Pin
Anonymous21-Apr-05 3:25
sussAnonymous21-Apr-05 3:25 
AnswerRe: Mixing with Texture ??? Pin
Sameer Ahmed21-Apr-05 6:30
memberSameer Ahmed21-Apr-05 6:30 
GeneralRelease verson problem Pin
kifayatullah19-Apr-05 22:14
susskifayatullah19-Apr-05 22:14 
GeneralRe: Release verson problem Pin
Sameer Ahmed19-Apr-05 23:58
memberSameer Ahmed19-Apr-05 23:58 
GeneralNeed asistance Pin
Fahad N. Abbasi12-Apr-05 11:44
memberFahad N. Abbasi12-Apr-05 11:44 
GeneralRe: Need asistance Pin
Sameer Ahmed16-Apr-05 15:52
memberSameer Ahmed16-Apr-05 15:52 
GeneralRe: Need asistance Pin
Fahad N. Abbasi18-Apr-05 13:14
memberFahad N. Abbasi18-Apr-05 13:14 
GeneralWaterMark on Stream Pin
kifayatullah1-Apr-05 21:31
susskifayatullah1-Apr-05 21:31 
GeneralRe: WaterMark on Stream Pin
Sameer Ahmed16-Apr-05 15:37
memberSameer Ahmed16-Apr-05 15:37 
Generalhi,i need you some help Pin
lserlohn24-Feb-05 23:31
memberlserlohn24-Feb-05 23:31 
GeneralRe: hi,i need you some help Pin
Sameer Ahmed24-Feb-05 23:53
memberSameer Ahmed24-Feb-05 23:53 
GeneralLNK error Pin
craigbennett15-Feb-05 5:10
membercraigbennett15-Feb-05 5:10 
GeneralLNK error Pin
craigbennett15-Feb-05 5:06
membercraigbennett15-Feb-05 5:06 
GeneralRe: LNK error Pin
Sameer Ahmed15-Feb-05 7:05
memberSameer Ahmed15-Feb-05 7:05 
Generalcould not instantiate Pin
eligetiv12-Feb-05 19:16
membereligetiv12-Feb-05 19:16 
GeneralRe: could not instantiate Pin
Sameer Ahmed12-Feb-05 23:34
memberSameer Ahmed12-Feb-05 23:34 
GeneralMulti-Screen Expansion Pin
fwsouthern14-Jan-05 10:45
memberfwsouthern14-Jan-05 10:45 
GeneralRe: Multi-Screen Expansion Pin
Sameer Ahmed14-Jan-05 22:46
memberSameer Ahmed14-Jan-05 22:46 
GeneralRe: Multi-Screen Expansion Pin
fwsouthern15-Jan-05 9:20
memberfwsouthern15-Jan-05 9:20 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 1 Feb 2005
Article Copyright 2005 by Sameer Ahmed
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid