Constant stuttering while streaming 4K @ 60 FPS on AMD Radeon RX 6700 XT #398
-
There is something that puzzles me in recent months. I experience regular stuttering every ~4 seconds with 4K 60 FPS streams. 2K streams stutter as well but it's less noticeable. The framerate oscillates from ~57 to 60 similar to #193 but this one seems to be a completely different issue. My setup is:
Moonlight reports 0.0% frame loss, 1-2 ms of network latency and 2-3 ms of decoding time, so this is definitely not a network or client issue. I did some benchmarking of FFmpeg/VAAPI encoder performance, in particular measured the execution time of As we can see, the GPU usually spends 10-13 ms to encode most of frames. However, there are many peaks above 35 ms that cause frame skipping and constant stuttering. Moreover, all these peaks occur exactly every 256 frames, i.e. about 4.3 seconds at 60 FPS! The number 256 is so magical that it can't be a coincidence. There must be some settings/constants in the underlying encoder implementation that cause peaks to appear so regularly. BTW streaming at 1080p is not that different. Sure, the stream runs much smoother and the frame encoding time is always below 16 ms but all the peaks are still there. I managed to find only a silly workaround so far. Before starting the stream, I open a small H.264 video in Haruna (a media player based on mpv), minimize Haruna window and keep it playing in the background. Then I run Sunshine and start streaming as usual. This magically fixes all stuttering issues as shown in the graph below: This looks much better! No more annoying peaks every 4 seconds. The media player should use VAAPI hardware decoding otherwise this hack won't work. It seems like even the slightest usage of the hardware decoder somehow forces the hardware encoder to produce much better results. All of this makes me think the issue is not related to Sunshine or FFmpeg but is buried somewhere deeper in libva or amdgpu. All in all, this is just a hack that might stop working someday. Any ideas what could cause the stuttering and how to properly fix it? |
Beta Was this translation helpful? Give feedback.
Replies: 0 comments 57 replies
-
I wonder if this would help improve things? LizardByte/Sunshine#1043 Note, it's quite outdated... so it might take a little bit of modification to bring it into current nightly. |
Beta Was this translation helpful? Give feedback.
-
The following FFmpeg ticket might be potentially related: https://trac.ffmpeg.org/ticket/8377 I'm also using kmsgrab + h264_vaapi / hevc_vaapi. Need to check. |
Beta Was this translation helpful? Give feedback.
-
I can reproduce the same exact problem. Also using an RX 6700 XT. Here a similar plot of I cannot reproduce your "hacky" fix. In the middle graph, I'm playing a video with MPV, it just make I also have have 251 frames normal frames and 5 slow frames. It's really annoying and make sunshine hardly usable on my machine. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Hi @kodemeister and @cupown , I poured some more hours into yesterday, until I had a breakthrough. Ffmpeg can actually pass some additional flags to AMD's VAAPI by (mis)using With most flag combination that I tried there was always that annoying spike pattern. But with the This might also be of interest to @psyke83. It might also be interesting to know how old your firmware versions are. While researching various AMD issues I found one issue that was partly related to an old firmware. Mine is rather old since I'm still on Ubuntu 22.04 and waiting for 24.04's release (kernel and mesa are rather recent, but firmware is often neglected as it turns out). It would be nice to have reproducer from the command line using only ffmpeg. I'm still suspecting that under some circumstances sunshine/ffmpeg/vaapi is emitting keyframes even if configured not to. But I did not find the part in |
Beta Was this translation helpful? Give feedback.
-
AFAIK, AMF is only used by the amdgpu-pro driver on Linux, but Sunshine is not coded to support enumeration of the AMF codecs on Linux, so it won't be straightforward to test (or perhaps worthwhile at all). I know that @kodemeister does not believe this to be a bandwidth issue, but can someone report on the behaviour with the CQP rate control method clamped to the lowest quality possible? For example: diff --git a/src/video.cpp b/src/video.cpp
index 6c1938c2..aa620c91 100644
--- a/src/video.cpp
+++ b/src/video.cpp
@@ -809,6 +809,9 @@ namespace video {
{ "low_power"s, 1 },
{ "async_depth"s, 1 },
{ "idr_interval"s, std::numeric_limits<int>::max() },
+ { "qmin"s, 50 },
+ { "qmax"s, 51 },
+ { "rc_mode"s, "cqp"s },
},
// SDR-specific options
{},
@@ -828,6 +831,9 @@ namespace video {
{ "async_depth"s, 1 },
{ "sei"s, 0 },
{ "idr_interval"s, std::numeric_limits<int>::max() },
+ { "qmin"s, 50 },
+ { "qmax"s, 51 },
+ { "rc_mode"s, "cqp"s },
},
// SDR-specific options
{},
@@ -847,6 +853,9 @@ namespace video {
{ "async_depth"s, 1 },
{ "sei"s, 0 },
{ "idr_interval"s, std::numeric_limits<int>::max() },
+ { "qmin"s, 50 },
+ { "qmax"s, 51 },
+ { "rc_mode"s, "cqp"s },
},
// SDR-specific options
{}, It might also be worth incrementing the quality slowly (by reducing qmin/qmax) to see if CQP behaves better at equivalent quality levels to your regular bandwidth target -- but keep in mind that CQP is not suitable for enforcing a specific bandwidth limit as needed by Sunshine. |
Beta Was this translation helpful? Give feedback.
-
Reported the issue upstream: https://gitlab.freedesktop.org/drm/amd/-/issues/3336 Thanks everyone for your valuable comments and feedback! 🙇 |
Beta Was this translation helpful? Give feedback.
-
The fixes sadly don't work for me. On one terminal I run: While on the other: I see spikes jumping from 2ms to 15ms in a periodic fashion. I attached my csv here. Anything missing when doing the workaround? |
Beta Was this translation helpful? Give feedback.
-
I took some notes on how to install the patched mesa on Ubuntu (22.04): |
Beta Was this translation helpful? Give feedback.
-
A patch for low latency encode was merged in mesa: https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/30039 Enable it with |
Beta Was this translation helpful? Give feedback.
-
That's great news! Thanks for the heads up.
…On July 16, 2024 8:49:37 PM GMT+02:00, cupown ***@***.***> wrote:
A patch for low latency encode was merged in mesa: https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/30039
Enable it with `AMD_DEBUG=lowlatencyenc`
--
Reply to this email directly or view it on GitHub:
https://github.com/LizardByte/Sunshine/discussions/2193#discussioncomment-10065908
You are receiving this because you were mentioned.
Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Closing this discussion as resolved. Sunshine now automatically enables low-latency mode on AMD GPUs (#3115). Mesa 24.2 or newer is required. The fix is already available in nightly and should be included to the next Sunshine release. Thanks everyone who helped with this annoying issue! 🙇 |
Beta Was this translation helpful? Give feedback.
Closing this discussion as resolved. Sunshine now automatically enables low-latency mode on AMD GPUs (#3115). Mesa 24.2 or newer is required. The fix is already available in nightly and should be included to the next Sunshine release. Thanks everyone who helped with this annoying issue! 🙇