Click here to Skip to main content
15,860,972 members
Articles / Desktop Programming / MFC
Article

Wrapper class for playing video with the VMR9

Rate me:
Please Sign up or sign in to vote.
4.67/5 (40 votes)
10 Feb 2003CPOL5 min read 362.4K   10.5K   116   82
Class for playing and mixing video files using DirectX9 Video Mixing Renderer.

Image 1

Introduction

This article demonstrates a simple wrapper class for playing video files with the new DirectShow Video Mixing Renderer 9.

In DirectX9, multimedia applications can use a new video renderer to display decoded frames, but this renderer is not the default renderer for compatibility issue. On Windows XP, the default renderer is the VMR7, but on older Windows version it's the Video Renderer. The main difference is performance and overlay mixing capabilities: as older renderer use different versions of DirectDraw API (even older API for the Video Renderer), the VMR9 is based on DirectX Graphics, so it uses the Direct3D capability of your 3D video card. The result is an improved performance on recent 3D cards, better support of overlay mixing, compatibility with all Windows versions that support DirectX9, and some new capability such as de-interlacing and ProcAmp support (contrast, saturation, etc.).

So the new VMR9 looks great but it's not the default renderer, regardless of Windows version... We've to build it manually, and this is why I wrote this class.

What you need...

To run the demo, DirectX9 runtime has to be installed on your system, which must have a Direct3D compatible display adapter. To build, DirectX9 SDK must be installed on your system. The source code was created under Visual C++ 6 SP5.

All the tests I've made is on my WinXP box, running an ATI Radeon M7 (kind of Radeon 7500). As I can't check real compatibility with many other Windows releases and video cards, please try with your system and put a word in the forum.

Using the code

A first look

All of the DirectShow graph management and VMR routines are included in one class : CVMR9Graph.

class CVMR9Graph  
{
    // Constructor / destructor
public:
    CVMR9Graph();
    CVMR9Graph(HWND MediaWindow, 
             int NumberOfStream = 4);
    virtual ~CVMR9Graph();

    // Methods
public:
    // Graph configuration
    void SetNumberOfLayer(int nNumberOfLayer);
    BOOL SetMediaWindow(HWND MediaWindow);
    BOOL SetMediaFile(const char* pszFileName, 
                             int nLayer = 0);
    BOOL PreserveAspectRatio(BOOL bPreserve = TRUE);
    IBaseFilter* AddFilter(const char* pszName, 
                             const GUID& clsid);

    // Graph control
    BOOL PlayGraph();
    BOOL StopGraph();
    BOOL ResetGraph();
    IMediaEvent* GetPtrMediaEvent();
    IMediaControl* GetPtrMediaControl();
    IMediaSeeking* GetPtrMediaSeeking();
    IBasicAudio* GetPtrBasicAudio();


    // Layer control
    BOOL GetVideoRect(LPRECT pRect);
    int GetAlphaLayer(int nLayer);
    BOOL SetAlphaLayer(int nLayer, int nAlpha);
    DWORD GetLayerZOrder(int nLayer);
    BOOL SetLayerZOrder(int nLayer, DWORD dwZOrder);
    BOOL SetLayerRect(int nLayer, RECT layerRect);

    // Bitmap control
    BOOL SetBitmap(const char* pszBitmapFileName, 
      int nAlpha, COLORREF cTransColor, RECT bitmapRect);
    BOOL SetBitmapParams(int nAlpha, 
      COLORREF cTransColor, RECT bitmapRect);

    // Reflected from window
    BOOL Repaint();
    BOOL Resize();

    // helper
    LPCTSTR GetLastError();

protected:
    // [...]
};

For convenience, header and implementation files contains the DirectShow includes, Direct3D includes, and pragma directives for lib.

Step 1 : Building a simple player

Building a very simple video player is quite easy:

  1. Include VMR9Graph.h and VMR9Graph.cpp in your project,
  2. Add an instance of CVMR9Graph in your application,
  3. Provide a window for video playback,
  4. Call CVMR9Graph::SetMediaWindow(hMyVideoPlaybackHandle) to set the video playback window,
  5. Call CVMR9Graph::SetMediaFile(0, pszPathToMyFile) to set the video file to render,
  6. Call CVMR9Graph::RunGraph() to play video.

At this point video playback works but the video wasn't resized with your window...

Step 2 : Forwarding events

Your application have to tell to the graph when the video has to repaint or size:

  • Create a handler for WM_SIZE message and make a call to CVMR9Graph::Resize(),
  • Create a handler for WM_PAINT message and make a call to CVMR9Graph::Repaint()

You can notice that video playback preserves aspect ratio by default. You can change this by a call to CVMR9Graph::PreserveAspectRatio(FALSE).

Ok, that looks much better... time to play with video mixing.

Step 3: Mixing video

Multiple file playback was handled by layers. Each layer plays a video and supports several properties such as ordering, alpha blending, size and position. The video produced by multiple layers is called a composition, and takes the size of the biggest media.

Each layer you insert is identified by it's layer index; CVMR9Graph lets you play with 10 layers, with it's default value to 4 layers (VMR9 default).

The following example loads 2 video files and sets an alpha value of 50% to the first:

// load media files
myGraph.SetMediaFile(0, "C:\\Video1.avi");
myGraph.SetMediaFile(1, "C:\\Video2.mpg");

// set alpha value to video1
myGraph.SetAlphaLayer(0, 50);

Alpha value can be set in real time, as show in the demo app.

Note 1: I have not been able to mix two DivX files, but only one DivX and other codecs such as MPEG... Don't know why... perhaps a hardware lack, since DirectShow samples seems to have the same troubles.

Note 2: The CVMR9Graph adds only one sound renderer in the graph, so only the first video stream has sound. You can add another sound renderer with a call to CVMR9Graph::AddFilter(_T"Another Sound Renderer", CLSID_DSoundRender).

Looks cool on a recent computers... Can we add more?

Step 4 : Setting an overlay bitmap

Overlay bitmap in CVMR9Graph is loaded in a Direct3D surface. The bitmap can be in GIF, JPEG, PNG, BMP, DIB, TGA, or DDS format.

To set an overlay bitmap, call CVMR9Graph::SetBitmap(), with the following parameters :

  • a bitmap file path,
  • an alpha value (overlay bitmap is always topmost in composition),
  • a color key for bitmap transparency,
  • and the bitmap size and position (keep in mind that video/bitmap size is relative to composition size).

Overlay bitmap is a cool feature that can be used to:

  • display a small overlay indicator,
  • create a mask for video display (perhaps someone will try it with a window region. This can be fun).

Last step: Go further away

To keep the class simple, playback control is minimal, but you can do more by getting some DirectShow COM interfaces:

  • IMediaEvent: gives the state and event of the graph. CVMR9Graph automatically sends a WM_MEDIA_NOTIF message to your video playback window; when this occurs, call IMediaEvent::GetEvent() to get the event type.
  • IMediaControl: provides control over the playback, such as pause.
  • IMediaSeeking: provides control over playback rate and position.
  • IBasicAudio: provides control over the sound renderer, such as volume and balance.

Note: After the use of an interface, you have to release it by a call to TheInterface::Release().

Known issue

When the graph is running, a call to CVMR9Graph::Stop() or CVMR9Graph::SetMediaFile() can be done, but it seems that in some cases composition can't run correctly after, particularly with bitmap media files...

A call to CVMR9Graph::ResetGraph() cleans up the graph and constructs a new fresh instance.

When resizing video window in demo app, there is some flickering... That's because it's a MFC window with the standard OnEraseBackgnd() implementation. Microsoft guidelines clearly indicate to bypass standard background paint.

// TODO : some improvements

The IVMRMonitorConfig9 interface is retrieved but not used. Multi monitor can be great.

There's no support for de-interlacing or ProcAmp control (ProcAmp don't work on my current ATI display drivers... Grrr)

There's no support for dynamic overlay bitmap... As a Direct3D surface can be locked and modified by a device context handle or even directly, this can be quite simple...don't know why I din't code it... probably a small lack of sleep.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) G. LABOURE
France France
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionSource filter named "SRC00" Pin
sdancer7531-May-13 0:31
sdancer7531-May-13 0:31 
QuestionRead Webcam Pin
Member 1001729924-May-13 4:27
Member 1001729924-May-13 4:27 
Questionmedia seeking Pin
pranav_306-Dec-11 0:04
pranav_306-Dec-11 0:04 
Generalfull screen Pin
Ekypro22-Jan-11 10:53
Ekypro22-Jan-11 10:53 
QuestionHow to loop a video? Pin
yaooo21-Jun-10 19:16
yaooo21-Jun-10 19:16 
AnswerReset problem Pin
Member 148185420-Jun-08 3:01
Member 148185420-Jun-08 3:01 
QuestionReset function does not close the file Pin
Yuna792-May-08 23:08
Yuna792-May-08 23:08 
QuestionHow can we implement this in c# Pin
Programmer3-Dec-07 17:53
Programmer3-Dec-07 17:53 
Questionreal time video? Pin
captainc/c++27-Nov-07 20:50
captainc/c++27-Nov-07 20:50 
GeneralVB6 Pin
Xristos20003-Sep-07 10:12
Xristos20003-Sep-07 10:12 
GeneralRunning with WMP11 Pin
Trollslayer24-Aug-07 23:01
mentorTrollslayer24-Aug-07 23:01 
QuestionVB6 or VB.NET? Pin
Xristos200021-Aug-07 23:10
Xristos200021-Aug-07 23:10 
Generalplay video on 2 surfaces Pin
froid2426-Apr-07 2:38
froid2426-Apr-07 2:38 
Generaldraw over video Pin
froid2426-Apr-07 2:34
froid2426-Apr-07 2:34 
GeneralRe: draw over video Pin
KarstenK26-Apr-07 3:25
mveKarstenK26-Apr-07 3:25 
GeneralRe: draw over video Pin
froid2426-Apr-07 10:02
froid2426-Apr-07 10:02 
GeneralRe: draw over video Pin
KarstenK26-Apr-07 20:20
mveKarstenK26-Apr-07 20:20 
Questionusing IMediaSeeking Pin
superjoe313-Apr-07 10:56
superjoe313-Apr-07 10:56 
AnswerRe: using IMediaSeeking Pin
froid241-May-07 22:32
froid241-May-07 22:32 
GeneralRe: using IMediaSeeking [modified] Pin
exp2lnt4-Nov-07 23:38
exp2lnt4-Nov-07 23:38 
GeneralStreamed videos mms protocol Pin
Scope25-Feb-07 13:45
Scope25-Feb-07 13:45 
GeneralRendering to texture Pin
sodan22-Feb-07 7:33
sodan22-Feb-07 7:33 
Hi there

Nice code! Could you prove functions to render a movie, onto a supplied texture ?

thanx,
Søren

GeneralUsing the VMR project Pin
slinks55524-Jan-07 12:57
slinks55524-Jan-07 12:57 
Generalnoob...compile error.. Pin
danomano100xtc23-Jan-07 7:33
danomano100xtc23-Jan-07 7:33 
GeneralRe: noob...compile error.. Pin
bigbadmarcus5-Jul-07 8:47
bigbadmarcus5-Jul-07 8:47 

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.