Skip to main content

History: DirectShowVMR9 Plugin

Source of version: 7 (current)

Copy to clipboard
            {IMG(src="img/wiki_up/DirectShowVMR9_Plugin_ScreenShot.jpg",thumb="y",rel="box",width="200",imalign="right",align="right",stylebox="border",alt="DirectShowVMR9_Plugin_ScreenShot.jpg")}{IMG}
__DSVMR9 Plugin__ - DirectShow using VMR9 in Ogre
__Author__: Stefano "''Pagghiu''" Cristiano (spagghiu@yahoo.it)

The code is not very clean, I'm forcing myself to release in hope
this will be useful to someone.

Follow the thread [http://www.ogre3d.org/phpBB2/viewtopic.php?p=261994|in the forums]

{maketoc}

!!FFDShow Problems
Remember prevent [http://sourceforge.net/projects/ffdshow|FFDShow] from inserting its filters in the graph when using VMR9 as it causes some strange problems with videos.

!!Downloads and History
* 0.1: [http://pagghiu.ulisse.info/tmp/ogre/DSVMR9_0.1.zip|DSVMR9_0.1.zip] initial relase
* 0.2: [http://pagghiu.ulisse.info/tmp/ogre/DSVMR9_0.2.zip|DSVMR9_0.2.zip] manteniance release
** Now searches for files in DEFAULT_RESOURCE_GROUP_NAME. Needs to be generalized to load from custom resource groups?
** Now creates textures and dshow vmr9 streamer only if the material is actually loaded and not only parsed (see OgreDSVMR9Texture::UpdateTexture() ). Need to find a better way of doing this however...
** Improved handling of some strange camera pixel formats I've been struggling with
** Fixed various bugs and general cleanup of code

!!License
This editor is available under the same licensing scheme as the OGRE
engine i.e. the GNU Lesser Public License (LGPL).

If you use it please drop me a note just to let me be happy about it ;D

I'm not responsible for your crazy ways of using this code.

!!Introduction
This sample implements a link between Video Mixing Renderer 9, an user
mode OS component available since Windows Xp SP2 that allows to render
directshow video content directly into a Direct3D 9 Texture.
This makes use of some hardware accelerating capabilities available
on modern video card for video decoding/deinterlacing.
This limits the usage of plugin only with the Direct3D9 rendersystem,
but that's it,is not possibile to use VMR9 in opengl.
Obviously you need very good reason to use this plugin instead of
other available for ogre (Theora,etc.) as you have to stick with
a fixed rendersystem (D3d) and platform (windows).

!!Features
* Stream images from arbitrary number of .avi files
* Stream images from arbitrary number of webcams
* Hardware accelerated de-interlacing and decoding
* Pixel format conversion for some formats

!!Usage
You can use this plugin as an external texture source and specify
various options:
* __filename__: filename of the video to be played (.avi or anything supported by an installed directshow filter)
* __camera_number__: number of camera 
* __use_vmr9__: [true/false] whether to use vrm9 or stick with standard classic directshow "grab to dynamic texture" approach (to disable vrm9 in case of problems)
* __use_yuvmixing__: [true/false] whether to use accelerated yuv mixing (to disable hw accel mixing in case of problems)
* __capture__: [true/false] set this to false if you don't need to software process frames captured from camera or video file. If you set this to true, is probably smarter disabling also vrm9 setting user_vrm9 to false.



when capture is true you can register a callback like this
{CODE(wrap="1", colors="c++")}    OgreDSVMR9Controller* WebcamCtrl;
....
    WebcamCtrl->getTextureByMaterialName("Plane/Background")->addCaptureFrameListerner(m_listener);
....

    void newFrame(OgreDSVMR9* webcam)
    {
        OgreDSVMR9::LockStruct myLock;
        if(!webcam->Lock(&myLock))
            return;

        webcam->Unlock();
    }

...
    WebcamCtrl->getTextureByMaterialName("Plane/Background")->removeCaptureListener(m_listener);{CODE}

with LockStruct a struct that has everything you need to software process the grabbed grame (maybe can be refactored using Ogre::pixelbox?)
{CODE(wrap="1", colors="c++")}
        struct LockStruct
        {
            BYTE*    buffer;
            DWORD    bufferSize;
            int        width;
            int        height;
            bool    interlaced;
            PixelFormat pixelFormat;
        };{CODE}
!!Examples


{CODE(wrap="1", colors="c++")}material Plane/Background
{
    technique
    {
        pass
        {
            cull_hardware none
            cull_software none

            depth_write off
            depth_check off

            lighting off

            texture_unit
            {
                texture_source dsvmr9_video
                {
                        camera_number 0 //Defines which webcam to use.
                        use_vmr9 true
                        use_yuvmixing true
                        capture false
                }
            }
        }
    }
}{CODE}

{CODE(wrap="1", colors="c++")}
material Ogre/Skin
{
    technique
    {
        pass
        {
            ambient 0.7 0.7 0.7
            cull_hardware none

            texture_unit
            {
                //texture GreenSkin.jpg
                texture_source dsvmr9_video
                {
                        use_vmr9 true
                        use_yuvmixing true
                        capture false
                        filename PANASONIC_TRAVE_1.avi

                }
                tex_address_mode mirror
            }
        }
    }
}{CODE}

!!Notes on source code
Sources that interact with DirectShow and VMR9 are in a library
called 'DSVMR9' and gets compiled as a static .lib.
This library is linked to the ogre plugin "DSVMR9_Ogre" that provides
transparent access to VMR9.

This ogre plugin interface has been developed following the code shown in
[http://www.ogre3d.org/phpBB2/viewtopic.php?t=16572]

To make VMR9 work obviously you can't use the OpenGL renderer.
the Library can also be configured to act just like a normal directshow
grabbing plugin (changing a flag called mbUseVMR9 in OgreDSVMR9.cpp but
in this case is better use other plugins)

!!Compiling
I've used Microsoft Visual Studio 2005 Professional SP1

!!!DSVMR9_Ogre
To compile "DSVMR9_Ogre" plugin you need 
* Ogre :D (i've used version 1.4.5)
* DSVMR9 Libray (comes already compiled in Code\src\DSVMR9\lib)
* DirectX SDK Headers (you can use any recent version)



!!!DSVMR9
If you need to recompile DSVMR9,the procedure is a bit more involved as you need:

* Windows SDK: 

download on msdn [http://www.microsoft.com/downloads/details.aspx?FamilyId=0BAF2B35-C656-4969-ACE8-E4C0C0716ADB&displaylang=en]
You must build the DirectShow Base classes (in \WindowsSDK\Samples\Multimedia\DirectShow\BaseClasses)

* DXSDK: 

The DirectX SDK available [http://msdn.microsoft.com/directx/sdk/]

* DXSDK Old version: 

you need some header of an old version of the dxsdk to successfully compile the directshow base classes...
maybe this is fixed in the new Windows SDK for vista because they are missing,but i've not checked it.

the headers are:

* d3d.h
* d3dcaps.h
* d3drm.h
* d3drmdef.h
* d3drmobj.h
* d3drmwin.h
* d3dtypes.h
* dxtrans.h

!!Directory structure
The directory structure of this package is

* Bin: binaries 
* Code
** Src
*** DSVMR9: the library that uses directshow
*** Ogre
**** Plugins
***** DSVMR9: the ogre plugin
**** Samples
***** DSVMR9_Ogre: a sample
* Dependencies: on my system I like to have all dependencies here :)

!!Sample
You can just run the demo in bin\Demo_DSVMR9Ogre.exe
If everything works, you will see three panels.
The first, big one, is connected to first camera available to the
sistem.
The other two panels are connected to a second camera and to a 
streaming .avi video in xvid format.
If you have FFDShow installed be sure to prevent him from inserting
its filters into the dshow graph.
I tested the camera code only with my Sony Handycam Firewire camera 
(DCR HC96E) and a simple Logitech Quickcam.
It may crash on other system/configuration.