Click here to Skip to main content
15,887,454 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.5K   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

 
GeneralRe: Streaming results? Pin
Geert van Horrik2-Oct-05 9:02
Geert van Horrik2-Oct-05 9:02 
GeneralRe: Streaming results? Pin
Sameer Ahmed7-Oct-05 7:47
Sameer Ahmed7-Oct-05 7:47 
GeneralRe: Streaming results? Pin
Geert van Horrik7-Oct-05 12:15
Geert van Horrik7-Oct-05 12:15 
Generalplease , i have an error in dshow.h Pin
dynamica12330-Aug-05 3:11
dynamica12330-Aug-05 3:11 
GeneralRe: please , i have an error in dshow.h Pin
Sameer Ahmed30-Aug-05 4:21
Sameer Ahmed30-Aug-05 4:21 
Generalplease , i need dshow.h Pin
dynamica12330-Jul-05 1:59
dynamica12330-Jul-05 1:59 
GeneralRe: please , i need dshow.h Pin
Sameer Ahmed30-Jul-05 8:37
Sameer Ahmed30-Jul-05 8:37 
GeneralProblem with Video Mixing Renderer 9 Pin
tl0825426-Sep-05 21:57
tl0825426-Sep-05 21:57 
Hi, i'm trying to make an aplication to blend subtitles onto a video stream. The renderer i need to use is the Video Mixing Renderer 9. The application is based on one of the examples of the DirectX SDK, called Text9. I'm using parto of the code. I've build the filter graph, with the VMR-9 as renderer. When i try to add the bitmap to the VMR's input, the program says: "Unhandled exception in executable.exe:0xc0000005:Access Violation".

This is the code:

HRESULT CText::BlendText(TCHAR *szNewText)
{

LONG cx, cy;
HRESULT hr;
CSubtitlesDlg dialog;

// Read the default video size
// hr = pWC->GetNativeVideoSize(&cx, &cy, NULL, NULL);

// Create a device context compatible with the current window
HDC hdc= GetDC(dialog.GetSafeHwnd());
HDC hdcBmp = CreateCompatibleDC(hdc);

// Write with a known font by selecting it into our HDC
HFONT hOldFont = (HFONT) SelectObject(hdcBmp, g_hFont);

// Determine the length of the string, then determine the
// dimensions (in pixels) of the character string using the
// currently selected font. These dimensions are used to create
// a bitmap below.
int nLength, nTextBmpWidth, nTextBmpHeight;
SIZE sz={0};

nLength = (int) _tcslen(szNewText);
GetTextExtentPoint32(hdcBmp, szNewText, nLength, &sz);
nTextBmpHeight = sz.cy;
nTextBmpWidth = sz.cx;

// Create a new bitmap that is compatible with the current window
HBITMAP hbm = CreateCompatibleBitmap(hdc, nTextBmpWidth, nTextBmpHeight);
ReleaseDC(dialog.GetSafeHwnd(), hdc);

// Select our bitmap into the device context and save the old one
BITMAP bm;
HBITMAP hbmOld;
GetObject(hbm, sizeof(bm), &bm);
hbmOld = (HBITMAP)SelectObject(hdcBmp, hbm);

// Set initial bitmap settings
RECT rcText;
SetRect(&rcText, 0, 0, nTextBmpWidth, nTextBmpHeight);
SetBkColor(hdcBmp, RGB(255, 255, 255)); // Pure white background
SetTextColor(hdcBmp, g_rgbColors); // Write text with requested color

// Draw the requested text string onto the bitmap
TextOut(hdcBmp, 0, 0, szNewText, nLength);

// Configure the VMR's bitmap structure
VMR9AlphaBitmap bmpInfo;
ZeroMemory(&bmpInfo, sizeof(bmpInfo));
bmpInfo.dwFlags = VMRBITMAP_HDC;
bmpInfo.hdc = hdcBmp; // DC which has selected our bitmap

// Remember the width of this new bitmap
g_nImageWidth = bm.bmWidth;

// Save the ratio of the bitmap's width to the width of the video file.
// This value is used to reposition the bitmap in composition space.
g_fBitmapCompWidth = (float)g_nImageWidth / (float)cx;

// Display the bitmap in the bottom right corner.
// rSrc specifies the source rectangle in the GDI device context
// rDest specifies the destination rectangle in composition space (0.0f to 1.0f)
bmpInfo.rDest.left = 0.0f + X_EDGE_BUFFER;
bmpInfo.rDest.right = 1.0f - X_EDGE_BUFFER;
bmpInfo.rDest.top = (float)(cy - bm.bmHeight) / (float)cy - Y_EDGE_BUFFER;
bmpInfo.rDest.bottom = 1.0f - Y_EDGE_BUFFER;
bmpInfo.rSrc = rcText;

// Transparency value 1.0 is opaque, 0.0 is transparent.
bmpInfo.fAlpha = TRANSPARENCY_VALUE;

// Set the COLORREF so that the bitmap outline will be transparent
// SetColorRef(bmpInfo);

// Give the bitmap to the VMR for display
CFilterGraph Graph;
hr = Graph.m_pBMP->SetAlphaBitmap(&bmpInfo); <----This is the point where i get the problem.

// Select the initial objects back into our device context
DeleteObject(SelectObject(hdcBmp, hbmOld));
SelectObject(hdc, hOldFont);

// Clean up resources
DeleteObject(hbm);
DeleteDC(hdcBmp);

return hr;
}

Is there anyone who knows what the problem is? Thanks in advance. Sergio.
GeneralRe: Problem with Video Mixing Renderer 9 Pin
Sameer Ahmed27-Sep-05 7:55
Sameer Ahmed27-Sep-05 7:55 
Generalcreating Video Mixing Renderer 9 Pin
Member 161799915-Jun-05 6:09
Member 161799915-Jun-05 6:09 
GeneralRe: creating Video Mixing Renderer 9 Pin
Sameer Ahmed15-Jun-05 12:15
Sameer Ahmed15-Jun-05 12:15 
QuestionMixing with Texture ??? Pin
Anonymous21-Apr-05 2:25
Anonymous21-Apr-05 2:25 
AnswerRe: Mixing with Texture ??? Pin
Sameer Ahmed21-Apr-05 5:30
Sameer Ahmed21-Apr-05 5:30 
GeneralRelease verson problem Pin
kifayatullah19-Apr-05 21:14
kifayatullah19-Apr-05 21:14 
GeneralRe: Release verson problem Pin
Sameer Ahmed19-Apr-05 22:58
Sameer Ahmed19-Apr-05 22:58 
GeneralNeed asistance Pin
Fahad N. Abbasi12-Apr-05 10:44
Fahad N. Abbasi12-Apr-05 10:44 
GeneralRe: Need asistance Pin
Sameer Ahmed16-Apr-05 14:52
Sameer Ahmed16-Apr-05 14:52 
GeneralRe: Need asistance Pin
Fahad N. Abbasi18-Apr-05 12:14
Fahad N. Abbasi18-Apr-05 12:14 
GeneralWaterMark on Stream Pin
kifayatullah1-Apr-05 20:31
kifayatullah1-Apr-05 20:31 
GeneralRe: WaterMark on Stream Pin
Sameer Ahmed16-Apr-05 14:37
Sameer Ahmed16-Apr-05 14:37 
Generalhi,i need you some help Pin
lserlohn24-Feb-05 22:31
lserlohn24-Feb-05 22:31 
GeneralRe: hi,i need you some help Pin
Sameer Ahmed24-Feb-05 22:53
Sameer Ahmed24-Feb-05 22:53 
GeneralLNK error Pin
craigbennett15-Feb-05 4:10
craigbennett15-Feb-05 4:10 
GeneralLNK error Pin
craigbennett15-Feb-05 4:06
craigbennett15-Feb-05 4:06 
GeneralRe: LNK error Pin
Sameer Ahmed15-Feb-05 6:05
Sameer Ahmed15-Feb-05 6:05 

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.