XNA Creators Club Online
Page 1 of 1 (7 items)
Sort Posts: Previous Next

Decoding a video in XNA (.dll)

Last post 7/29/2008 12:53 AM by The ZMan. 6 replies.
  • 7/24/2008 8:06 AM

    Decoding a video in XNA (.dll)

    Recently I have been experimenting with C++/Cli and, well, one thing has led to another...

    Because I never like to throw away code, I give you:

     

    mpeg2_xna.zip, a native (safe) C# port of the free MPEG2 video decoder you can find on mpeg.org (x86 & xbox binaries)

     

    -- The backstory --

    I started this as an experiment to investigate how C++/Cli works under the hood, and to see if the resulting .net code could be semi-automatically ported to C#. I thought I'd start with something which seemed simple, yet useful and more than just hello world. Hence the MPEG2 decoder. Long story short, there really isn't any way to autoconvert C++/Cli to C#, but by the time I resigned myself to this, the hacked code was so close that manually porting the missing bits was trivial. All in all this represents is about 6 hours of mucking about on my part, including the C++/Cli stuff.

     

    -- other notes --

    I removed a couple of the larger extensions included in the original code, so it won't work for all videos. (I couldn't find one that didn't work)

    This is a video decoder, not an audio decoder.

    I make absolutely no guarentees it's even going to work. It could quite possibly cause your computer to explode.

    Performance wise, you should *just* be able to decode a 640x480 video @ 30fps on a high end single-core CPU. This may sound a bit average, but this is simply because the original implementation is not at all optimised. In fact, the C# decoder runs ~10% FASTER than the original pure C decoder (without pointers!). So on the 360, you will need to look at breaking larger videos into blocks and doing some good-old multithreading (which should be safe.. I think).

    (Speaking of the 360, I haven't actually been able to test the 360 .dll, as my 360 is currently getting the red rung out of it.)

    Also know there are mpeg licensing requirements if you plan to sell a game that uses this .dll. I think these only come into force after your first $$$$ sales though.

     

    Anyway. Enjoy.

    Xen: Graphics API for XNA
    www.codeplex.com/xen
  • 7/24/2008 9:40 AM In reply to

    Re: Decoding a video in XNA (.dll)

    Interesting project, I'll definitely give this a go when I get home (if I can find any MPEG2 videos that is :)).
  • 7/27/2008 12:42 AM In reply to

    Re: Decoding a video in XNA (.dll)

    Gave this a go today. Got it working in Windows fairly easily once I found an MPEG2 video to try it on.http://www.bernclare.com/Free.htm. The "DVD Quality" one however didn't work properly (the one above it did).

    I was getting about 107fps on a 350x260 (ish) clip, and about 9fps on the 360. There was a reduction in speed (down from about 120fps) because I had to translate the RGB (8/8/8) format the decoder spat out into a format that I could get into a GPU Texture, in this case RGBA (32), so that required a bit of array copy jiggery-pokery. The factor of 10 slowdown is about what I'm seeing with all my code on PC vs 360, so I don't think there is any specific going on that is hurting it on the 360, it's just a little slow. Nice work though if you could speed it up some more.

  • 7/27/2008 4:55 AM In reply to

    Re: Decoding a video in XNA (.dll)

    Very nice project!  FMV for the Xbox would be great for a lot of people, especially if the video files can remain compressed during deployment.

     

    StatusUnknown:
    Performance wise, you should *just* be able to decode a 640x480 video @ 30fps on a high end single-core CPU. This may sound a bit average, but this is simply because the original implementation is not at all optimised. In fact, the C# decoder runs ~10% FASTER than the original pure C decoder (without pointers!). So on the 360, you will need to look at breaking larger videos into blocks and doing some good-old multithreading (which should be safe.. I think).

    Keep in mind that the mpeg.org implementation is essentially a reference implementation and makes no attempt to be fast or take advantage of any platform/architecture features.  If you want to take a look at MPEG decoding code that is optimized for performance, take a look at libmpeg2.  It has full support for x86/PPC vector instructions, though you're not going to be able to take advantage of that in pure C#.  It may, however, give you some pointers on how to optimize your decoder.
    Microsoft DirectX/XNA MVP
  • 7/28/2008 6:51 PM In reply to

    Re: Decoding a video in XNA (.dll)

    nice! very impressive
    Joel Martinez - XNA MVP
    Blog: http://codecube.net
    XNA Unit Testing: Scurvy Test
  • 7/29/2008 12:39 AM In reply to

    Re: Decoding a video in XNA (.dll)

    Thanks for the feedback. I've been ill the last while so I hadn't had a chance to reply.

    Adam Miles:

    Gave this a go today. Got it working in Windows fairly easily once I found an MPEG2 video to try it on.http://www.bernclare.com/Free.htm. The "DVD Quality" one however didn't work properly (the one above it did).

    I was getting about 107fps on a 350x260 (ish) clip, and about 9fps on the 360. There was a reduction in speed (down from about 120fps) because I had to translate the RGB (8/8/8) format the decoder spat out into a format that I could get into a GPU Texture, in this case RGBA (32), so that required a bit of array copy jiggery-pokery. The factor of 10 slowdown is about what I'm seeing with all my code on PC vs 360, so I don't think there is any specific going on that is hurting it on the 360, it's just a little slow. Nice work though if you could speed it up some more.

    I didn't quite expect it to be *that* horribly slow on the 360...

    Unfortunately, I wasn't planning to support the library. It wasn't an experiment and I was quite happy with it as it stands, although the slowness is troubling.

    In the next few days I'll be happy to put the full code up on codeplex, if anyone is interested. However as I say, I'm not actually looking to optimize it. Once my original experiment failed, I did the minimum to get it going so I could share it, and that is good enough for me :-)

    I suppose with the right threading work, a 400x300 clip could be broken up and played back at 30fps, but thats still a stretch... and would look pretty rough :-)

    Xen: Graphics API for XNA
    www.codeplex.com/xen
  • 7/29/2008 12:53 AM In reply to

    Re: Decoding a video in XNA (.dll)

    I tried to do some research about decoding video on the GPU last year sometime and didn't find very much that was useful (or was just too plain complicated and acedemic for my old man brain to follow)... but thats one other way to take this.
    Play Kissy Poo - a game for 4 year olds on Xbox and windows
    The ZBuffer
    News and information for XNA
      Follow The Zman on twitter, Email me
        Please read the forum FAQs - Bug/Feature reporting
          Don't forget to mark good answers and good playtest feedback when you see it!!!
Page 1 of 1 (7 items) Previous Next
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/
DCSIMG