2

I'm writing an animation app in C#/WinForms (see this question). Basically, the animation in my application is smooth but shows tearing effects; when I take the same animation and render it to an AVI file and play it with Windows Media Player, the animation shows no tearing effects at all. I know WMP is not changing the frame rate because the animation is synchronized with music.

I assume WMP uses DirectX or some other technology that is aware of the monitor's refresh rate and scanline position etc., but I always assumed that programming to the refresh rate would constrain the frame rate. Obviously this isn't the case with WMP.

Does anyone know anything about how WMP (or other video players) renders video internally? I've searched but I can't seem to find any details about this.

Community
  • 1
  • 1
MusiGenesis
  • 74,184
  • 40
  • 190
  • 334
  • 1
    "I know WMP is not changing the frame rate because the animation is synchronized with music." - I could be wrong, but I don't believe this is a correct statement. Frame rate does not have anything to do with synchronization with the music. A higher frame rate means a smoother animation. Not the speed at which the animation executes. – JasCav Mar 16 '10 at 16:46
  • 1
    It may be possible that the conversion to AVI format has resampled the video framerate to something WMP can play without artifacts. – Skizz Mar 16 '10 at 16:48
  • @Skizz: WMP will just render the video with vertical synchronization which prevents tearing. The framerate of the video is irrelevant there. – Joey Mar 16 '10 at 16:50
  • What technology are you using that IS generating artifacts? – Nate Mar 16 '10 at 16:50
  • @Jason: to clarify, I meant that if, say, my animation had a frame rate of 19.33333 or something, and WMP was actually playing it at a frame rate of 20 (to keep in sync with my 60Hz monitor), then the animation would slowly drift out of sync with the music (which is also added to the AVI file). Because this drift doesn't happen, I know WMP is animating at 19.33333 frames per second. – MusiGenesis Mar 16 '10 at 16:58
  • @Skizz: I thought about that, but I took stills from the AVI and compared them to the original Bitmap cells, and they matched exactly. – MusiGenesis Mar 16 '10 at 17:00
  • @Nate: I'm just BitBlt-ing BitMaps to the screen, driven by a multimedia timer (`timeSetEvent` in `winmm.dll`). Since this isn't aware of the monitor state in any way, it's very smooth but has tearing effects. – MusiGenesis Mar 16 '10 at 17:01

2 Answers2

5

It's been a while since I did any DirectX programming, so this may be out of date.

From what I remember, with DirectX you could set up a flipping chain of buffers, usually three buffers: the buffer being displayed, the buffer to be displayed and the buffer being written to. On an update, DirectX will wait for a V-sync before updating the displayed buffer. Now, this will cause a discrepency between the displayed image and the image that should be displayed, but this will be, at most, one refresh, about 1/60th of a second, so you're unlikely to notice.

Some ASCII art to show what I mean:

|-|-|-|-|-|-|-|-|-|-|-|-|-|-|  - screen refresh
|----|----|----|----|----|---  - animation 
|-----|---|-----|---|-----|--  - displayed
Skizz
  • 69,698
  • 10
  • 71
  • 108
  • Crap, it looks like this is what I'll have to do. I was hoping I could do something simpler, like only use DirectX to tell me when the monitor was in the vertical blank period, and postpone my rendering until the VBP. Unless I'm misunderestimating this, this would eliminate tearing at the cost of occasional little hiccups in motion. – MusiGenesis Mar 16 '10 at 17:34
1

Are you painting each frame of your animation to a memory bitmap first, and then blitting the bitmap to your window? If not, this might be the solution for you.

(this is, of course, in addition to double-buffering)

Dmitry Brant
  • 7,612
  • 2
  • 29
  • 47
  • 2
    I was going to put a disclaimer on the question referencing double-buffering, along with setting control styles, overriding OnPaintBackground and all the other stuff you have to do to avoid flicker, in the hopes of convincing people that I really am having a tearing problem. :) – MusiGenesis Mar 16 '10 at 17:14