Stutter after refresh rate change (2 Viewers)

nyt

Retired Team Member
  • Premium Supporter
  • October 15, 2009
    199
    48
    Home Country
    United States of America United States of America
    setframeduration and modifypreviousframeduration do not work.

    DwmSetPresentParameters looks promising, but finding a c# example and structure for the arguments is not happening
     

    nyt

    Retired Team Member
  • Premium Supporter
  • October 15, 2009
    199
    48
    Home Country
    United States of America United States of America
    Check this Vista Aero ToolStrip on Non-Client Area - CodeProject

    The source code contains a c# import/ interop class for the whole dwm library :D

    Thanks for that.


    Dwm.DWM_PRESENT_PARAMETERS pparams = new Dwm.DWM_PRESENT_PARAMETERS();
    pparams.cbSize = Marshal.SizeOf(typeof(Dwm.DWM_PRESENT_PARAMETERS));
    pparams.fQueue = 1;
    pparams.cBuffer = 4;
    pparams.fUseSourceRate = 0;
    pparams.cRefreshesPerFrame = 1;
    pparams.eSampling = Dwm.DWM_SOURCE_FRAME_SAMPLING.DWM_SOURCE_FRAME_SAMPLING_POINT;

    Dwm.DwmSetPresentParameters(GUIGraphicsContext.DX9Device.PresentationParameters.DeviceWindowHandle, ref pparams);

    This seems to work.

    I'll post a full patch after more testing.

    MP calculates time between frames right? The reason I set fUseSourceRate to 0 is because setting the rate this way might cause funny issues.

    It does work though, but doing this just doesn't seem right.

    pparams.fUseSourceRate = 1;
    pparams.rateSource.uiNumerator = (int)(refreshRate * 1000);
    pparams.rateSource.uiDenominator = 1000;

    ...

    hm, setting the buffer causes jutter on scene changes briefly.

    still trying to tweak this to work 100%, but very very close.

    I can't quite get it perfect. There is still some jerkiness coming up in playback no matter what I do. Not as bad as the stutter, but it's there =[

    If I win-tab during playback (without this code in place) I get perfectly smooth playback. I'll keep at it until I can reproduce that with code.
     

    nyt

    Retired Team Member
  • Premium Supporter
  • October 15, 2009
    199
    48
    Home Country
    United States of America United States of America
    ...
    nailed it, patch/dll incoming.

    you're missing a few crucial things that will cause jerky playback. I've been testing this on 24p, 25p, and 60p media, and finally nailed this down.

    I'll show you in a second =] cleaning up the mess i made testing.

    edit: this is really frustrating. It was playing smooth on everything I tested, then I restarted and it wasn't. so close.

    edit again: it seems to be holding from settings from the last played video. need to work that out.

    If I start mp, play 25p video, its smooth as silk. If i play 24p, then 25p, there are frames being dropped.

    I'm not entirely sure this refresh rate change is to blame for the behavior I'm seeing.

    Start MP, play 24p video, play 25p video (frames drop).
    Start MP, play 25p video, plays perfect. stop, play again, still plays perfect.


    This is from 24p -> 60p(mp screen) -> 25p(50hz)
    17-01-2010 14:32:55.240 [2078]Dropping frame, behind 18.71 ms, last sleep time 6.37 ms.
    17-01-2010 14:32:55.279 [2078]Dropping frame, behind 15.96 ms, last sleep time 6.37 ms.
    17-01-2010 14:32:55.321 [2078]Dropping frame, behind 18.27 ms, last sleep time 6.37 ms.
    17-01-2010 14:32:55.363 [2078]Dropping frame, behind 17.60 ms, last sleep time 6.37 ms.
    17-01-2010 14:32:55.371 [2060]Scheduling sample from the past (7.38 ms, last call to NotifyWorker: 7.38 ms)
    17-01-2010 14:32:55.372 [2078]Dropping frame, behind 7.53 ms, last sleep time 0.00 ms.
    17-01-2010 14:32:55.414 [2060]Scheduling sample from the past (11.24 ms, last call to NotifyWorker: 7.69 ms)
    17-01-2010 14:32:55.414 [2078]Dropping frame, behind 11.35 ms, last sleep time 0.00 ms.
    17-01-2010 14:32:55.454 [2060]Scheduling sample from the past (13.02 ms, last call to NotifyWorker: 6.19 ms)
    17-01-2010 14:32:55.454 [2078]Dropping frame, behind 13.12 ms, last sleep time 0.00 ms.
    17-01-2010 14:32:55.496 [2060]Scheduling sample from the past (17.54 ms, last call to NotifyWorker: 6.06 ms)
    17-01-2010 14:32:55.496 [2078]Dropping frame, behind 17.65 ms, last sleep time 0.00 ms.
    17-01-2010 14:32:55.499 [2078]Dropping frame, behind 0.06 ms, last sleep time 0.00 ms.
    17-01-2010 14:32:55.580 [2060]Scheduling sample from the past (30.64 ms, last call to NotifyWorker: 6.23 ms)
    17-01-2010 14:32:55.580 [2078]Dropping frame, behind 30.75 ms, last sleep time 0.00 ms.
    17-01-2010 14:32:55.583 [2060]Scheduling sample from the past (13.23 ms, last call to NotifyWorker: 8.95 ms)
    17-01-2010 14:32:55.583 [2078]Dropping frame, behind 13.33 ms, last sleep time 0.00 ms.

    Index: RefreshRateChanger.cs
    ===================================================================
    --- RefreshRateChanger.cs (revision 24760)
    +++ RefreshRateChanger.cs (working copy)
    @@ -104,6 +104,36 @@
    }


    + [StructLayout(LayoutKind.Sequential)]
    + public struct DWM_PRESENT_PARAMETERS
    + {
    + public int cbSize;
    + public int fQueue;
    + public long cRefreshStart;
    + public int cBuffer;
    + public int fUseSourceRate;
    + public UNSIGNED_RATIO rateSource;
    + public int cRefreshesPerFrame;
    + public DWM_SOURCE_FRAME_SAMPLING eSampling;
    + }
    +
    +
    + public enum DWM_SOURCE_FRAME_SAMPLING
    + {
    + DWM_SOURCE_FRAME_SAMPLING_POINT,
    + DWM_SOURCE_FRAME_SAMPLING_COVERAGE,
    + DWM_SOURCE_FRAME_SAMPLING_LAST
    + }
    +
    +
    + [StructLayout(LayoutKind.Sequential)]
    + public struct UNSIGNED_RATIO
    + {
    + public int uiNumerator;
    + public int uiDenominator;
    + }
    +
    +
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public class DISPLAY_DEVICE
    {
    @@ -184,6 +214,15 @@
    [In] ChangeDisplaySettings_Flags dwFlags, [In] IntPtr lParam);


    + [DllImport("dwmapi.dll")]
    + public static extern int DwmSetPresentParameters(IntPtr hwnd, ref DWM_PRESENT_PARAMETERS pPresentParams);
    + [DllImport("dwmapi.dll")]
    + public static extern int DwmSetDxFrameDuration(IntPtr hwnd, int cRefreshes);
    + [DllImport("dwmapi.dll")]
    + public static extern int DwmEnableMMCSS(int fEnableMMCSS);
    + [DllImport("dwmapi.dll")]
    + public static extern int DwmIsCompositionEnabled(ref int pfEnabled);
    +
    public static void Win32_SetRefreshRate(uint monitorIndex, uint refreshRate)
    {
    Win32.DISPLAY_DEVICE displayDevice = new Win32.DISPLAY_DEVICE();
    @@ -209,6 +248,7 @@

    public static void CycleRefreshRate(uint monitorIndex, double refreshRate)
    {
    +
    if (Environment.OSVersion.Version.Major == 6 && Environment.OSVersion.Version.Minor == 1)
    // Are we running Windows 7?
    {
    @@ -227,7 +267,41 @@
    {
    Win32_SetRefreshRate(monitorIndex, (uint)refreshRate);
    }
    + FixDwm();
    +
    }
    + public static void DisableDwmQueue()
    + {
    + Log.Debug("DisableDwmQueue: disabling");
    + DWM_PRESENT_PARAMETERS presentationParams = new DWM_PRESENT_PARAMETERS();
    + presentationParams.cbSize = Marshal.SizeOf(typeof(DWM_PRESENT_PARAMETERS));
    + presentationParams.fQueue = 0;
    +
    + DwmSetPresentParameters(GUIGraphicsContext.DX9Device.PresentationParameters.DeviceWindowHandle, ref presentationParams);
    + }
    +
    + public static void FixDwm()
    + {
    + int dwmEnabled = 0;
    + DwmIsCompositionEnabled(ref dwmEnabled);
    +
    + if (dwmEnabled > 0)
    + {
    + Log.Debug("CycleRefresh: DWM Detected, adjusting presentation parameters");
    +
    + DWM_PRESENT_PARAMETERS presentationParams = new DWM_PRESENT_PARAMETERS();
    + presentationParams.cbSize = Marshal.SizeOf(typeof(DWM_PRESENT_PARAMETERS));
    +
    + presentationParams.fQueue = 1;
    + presentationParams.cBuffer = 2;
    + presentationParams.fUseSourceRate = 0;
    + presentationParams.cRefreshesPerFrame = 1;
    + presentationParams.eSampling = DWM_SOURCE_FRAME_SAMPLING.DWM_SOURCE_FRAME_SAMPLING_POINT;
    +
    + DwmSetPresentParameters(GUIGraphicsContext.DX9Device.PresentationParameters.DeviceWindowHandle, ref presentationParams);
    +
    + }
    + }
    }

    internal class RefreshRateSetting
    @@ -319,6 +393,13 @@

    private static void RefreshRateShowNotification(string msg, bool waitForFullscreen)
    {
    + // Thread.Sleep(10000);
    +
    + Win32.DWM_PRESENT_PARAMETERS presentationParams = new Win32.DWM_PRESENT_PARAMETERS();
    + presentationParams.fQueue = 0;
    + // Win32.DwmSetPresentParameters(GUIGraphicsContext.DX9Device.PresentationParameters.DeviceWindowHandle, ref presentationParams);
    +
    +
    if (GUIGraphicsContext.IsFullScreenVideo == waitForFullscreen)
    {
    GUIMessage guiMsg = new GUIMessage(GUIMessage.MessageType.GUI_MSG_REFRESHRATE_CHANGED, 0, 0, 0, 0, 0, null);
    @@ -657,6 +738,7 @@
    // defaults the refreshrate
    public static void AdaptRefreshRate()
    {
    + Win32.DisableDwmQueue();
    if (_refreshrateChangePending || _refreshrateChangeCurrentRR == 0)
    {
    return;
     

    Scythe42

    Retired Team Member
  • Premium Supporter
  • June 20, 2009
    2,065
    2,703
    51
    Berlin
    Home Country
    Germany Germany
    Can you post the patch as an attachment please. After copy and paste I cannot apply it to the source...
     

    nyt

    Retired Team Member
  • Premium Supporter
  • October 15, 2009
    199
    48
    Home Country
    United States of America United States of America
    patch as attachment.

    something thats strange:

    DWM_TIMING_INFO timingInfo = new DWM_TIMING_INFO();
    timingInfo.cbSize = Marshal.SizeOf(typeof (DWM_TIMING_INFO));
    DwmGetCompositionTimingInfo(GUIGraphicsContext.form.Handle, ref timingInfo);
    Log.Debug("TIMING INFO {0}, {1}, {2}, {3}, {4}, {5}, {6}", timingInfo.cFrameCurrent, timingInfo.cRefresh, timingInfo.cRefreshConfirmed, timingInfo.cRefreshFrame, timingInfo.qpcCompose, timingInfo.rateCompose.uiNumerator, timingInfo.rateRefresh.uiNumerator);


    2010-01-17 14:54:38.633400 [Debug][MPMain]: TIMING INFO 0, 0, 0, 0, 0, 0, 0

    edit: no idea why this is returning all 0's.
     

    Attachments

    • test.patch
      30.6 KB

    Scythe42

    Retired Team Member
  • Premium Supporter
  • June 20, 2009
    2,065
    2,703
    51
    Berlin
    Home Country
    Germany Germany
    Attached is nyt's patch compiled into the core.dll. Please test if it works or breaks anything else. Again thx to nyt for all the work he put into this.

    I also included an experimental dshowhelper.dll from another thread: [other] Problems with dropping frames with EVR - MediaPortal Forum

    Please test first the core.dll for the stutter after refresh rate change. Please try to reproduce the issue as good as you can to make sure the patch works. If it works well, I'll commit the change to the SVN for inclusion in RC1.

    Once the stutter after RR change is gone - and only then - try the dshowhelper.dll as well and test if playback is smoother and the number of dropped frames is lower compared to before.
     

    nyt

    Retired Team Member
  • Premium Supporter
  • October 15, 2009
    199
    48
    Home Country
    United States of America United States of America
    I just tested a clean build of MP, and I found the following:

    RefreshRateChanger enabled
    Play 24 -> choppy, hit win-tab -> smooth, play 25p -> choppy, seek -> choppy, hit win-tab still choppy, seek -> smooth

    I think something isn't right somewhere else.

    I'm going to dig around in the EVR code when I get some time. I believe that's where the roof of this may be stemming from.
     

    Scythe42

    Retired Team Member
  • Premium Supporter
  • June 20, 2009
    2,065
    2,703
    51
    Berlin
    Home Country
    Germany Germany
    I just tested a clean build of MP, and I found the following:

    RefreshRateChanger enabled
    Play 24 -> choppy, hit win-tab -> smooth, play 25p -> choppy, seek -> choppy, hit win-tab still choppy, seek -> smooth

    I think something isn't right somewhere else.

    I'm going to dig around in the EVR code when I get some time. I believe that's where the roof of this may be stemming from.
    When taking a look at the EVR code, take a look at this thread first: [other] Problems with dropping frames with EVR - MediaPortal Forum.

    A lot of stuff is already discussed over there. It's independent of the refresh rate thing which - at least on my system - is fixed with your patch as far as I could test up to now. But I only had the symptom on every first playback of a media file anyway and then never again.
     

    Users who are viewing this thread

    Top Bottom