video: size the NVENC VBV buffer like libx264 so keyframes stop blurring (#103) (#205)

The 100-500 kbit NVENC default squeezes every IDR of a high-resolution desktop
into a blur that sharpens over the GOP: at 3840x2160 hevc_nvenc CQ18 / 30M the
IDR frame lost 29-31 dB Y-PSNR against the frame before it, and the following
P-frames spent 15-18 Mbit/s re-sharpening a static desktop.

NVENC now takes one second of the bitrate cap within 500-8000 kbit, the rule
libx264 already uses (#8). UU_NVENC_BUFFER_MS still overrides it, so a small
value brings back the low-latency buffer. VAAPI is unchanged.
This commit is contained in:
Cr authored and GitHub committed 2026-11-09 08:37:00 +08:00
1 parent 21e9e690ae
commit d8e3025fb3
3 files changed
+19 -13

No files matched your search

+1 -1
View File
@@ -398,7 +398,7 @@ Common environment variables:
| `UU_TERMINAL_SHORTCUTS=0` | Disable the Ctrl+V paste rewrite in terminal windows |
| `UU_VIDEO_ENCODER` | `cpu`: libx264; `vaapi`: opt-in Intel/AMD; `nvenc`: force NVENC; unset or `auto`: a background probe (one synthetic IDR per candidate, 3 s each). With an NVIDIA driver the order is NVENC then CPU; without one the only candidate is CPU and the probe is skipped. A session that starts before the probe finishes uses the previous rule (NVENC if NVIDIA is present, else CPU). On a self-built `--features inproc-av` binary, a process-only failure of NVENC when the in-process engine would try it first on X11 is logged `native_unprobed` and keeps the previous NVENC choice (Wayland, portal, and KMS fall back to CPU). The skipped CPU-only probe is logged `not probed (only candidate)`. A successful probe and that CPU-only skip stay cached while NVIDIA presence matches the report. A later desktop connection whose presence differs starts one background re-probe and still uses the cached encoder until the new result is stored. A hardware-candidate failure (NVENC failed, CPU chosen) is kept for 10 minutes; the next desktop connection after that starts the same re-probe and still uses the cached encoder until the new result is stored. Explicit `cpu` / `vaapi` / `nvenc` does not start the probe. Another failure restarts the 10 minutes. `native_unprobed` is not a temporary failure. A non-`auto` `video_encoder` in `host_settings.json` wins. VAAPI stays out of auto until #33 |
| `UU_VAAPI_BUFFER_MS` | VAAPI VBV buffer in ms (10-1000, VAAPI only); raise it when keyframes are squeezed into a blur at high resolution |
| `UU_NVENC_BUFFER_MS` | NVENC VBV buffer in ms (10-1000, NVENC only, including the in-process engine); the default is 100-500 kbit, set `1000` when high-resolution keyframes blur and flicker once per GOP (#103) |
| `UU_NVENC_BUFFER_MS` | NVENC VBV buffer in ms (10-1000, NVENC only, including the in-process engine); the default matches libx264, one second of the bitrate cap within 500-8000 kbit, so high-resolution keyframes no longer blur and flicker once per GOP (#103). Set a small value (e.g. `50`) for a smaller low-latency buffer on a weak link |
| `UU_FFMPEG_ROOT` | Use FFmpeg from this prefix |
| `UU_AUDIO=0` | Disable sound; `UU_AUDIO_SOURCE` picks the PulseAudio source |
| `UU_REMOTE_ASSIST=0` | Disable remote assistance (no guest assistance before sign-in either) |
+1 -1
View File
@@ -361,7 +361,7 @@ systemctl --user restart uuremote-native.service
| `UU_TERMINAL_SHORTCUTS=0` | 关闭终端窗口里的 Ctrl+V 粘贴转换 |
| `UU_VIDEO_ENCODER` | `cpu` 强制 CPU;`vaapi` 显式启用 Intel/AMD 硬编;`nvenc` 强制 NVENC;未设置或 `auto` 时后台探测(每个候选编码一帧,限时 3 秒)。有 NVIDIA 驱动时顺序是 NVENC → CPU;没有 NVIDIA 时只有 CPU,不探测。探测未完成就建流则沿用旧规则(有 NVIDIA 用 NVENC,否则 CPU)。自行编译的 `--features inproc-av` 上,仅 X11 会先走进程内引擎:外部 FFmpeg 探测失败时记为 `native_unprobed`,沿用旧规则(NVENC);Wayland、portal、KMS 回退 CPU。只有 CPU 时跳过探测,记为 `not probed (only candidate)`。探测成功和这种只含 CPU 的跳过会缓存到 NVIDIA 是否存在发生变化为止(`/proc/driver/nvidia/version` 或 `/dev/nvidia0`)。之后的桌面连接在后台再探测一次(不挡住这一路,仍用缓存的编码器)。NVENC 探测失败、选用 CPU 时只缓存 10 分钟;到期后下一次桌面连接同样在后台再探测一次,再次失败则重新计 10 分钟。显式 `cpu` / `vaapi` / `nvenc` 不启动探测。`native_unprobed` 不算失败缓存。优先级低于 `host_settings.json` 里非 `auto` 的 `video_encoder`。VAAPI 要等 #33 解决后才会进入 auto |
| `UU_VAAPI_BUFFER_MS` | VAAPI 的 VBV 缓冲(10–1000 ms,仅 `UU_VIDEO_ENCODER=vaapi` 时生效);高分辨率下关键帧被压糊时用它换稳定画质 |
| `UU_NVENC_BUFFER_MS` | NVENC 的 VBV 缓冲(10–1000 ms,仅 NVENC 生效,含进程内引擎);默认 100–500 kbit,1440p 等高分辨率下关键帧被压糊、按 GOP 周期闪烁时设 `1000`(#103) |
| `UU_NVENC_BUFFER_MS` | NVENC 的 VBV 缓冲(10–1000 ms,仅 NVENC 生效,含进程内引擎);默认与 libx264 相同,取 1 秒码率上限并限制在 500–8000 kbit,高分辨率关键帧不再被压糊、按 GOP 周期闪烁(#103)。弱网下想要更小的低延迟缓冲可设小值(如 `50`) |
| `UU_FFMPEG_ROOT` | 使用指定目录下的 FFmpeg |
| `UU_AUDIO=0` | 关闭声音转发;`UU_AUDIO_SOURCE` 指定 PulseAudio 采集源 |
| `UU_LOGIN_WINDOW=0` | 凭证缺失时不自动弹出登录窗口 |
+17 -11
View File
@@ -795,11 +795,11 @@ async fn read_inprocess(
job.await.map_err(|e| format!("Encoder task: {e}"))?
}
/// Rate-control arguments: constant quality capped at 30M (自动), or capped at the viewer's
/// chosen bitrate. The VBV buffer stays at most 500k so a cap never adds queueing latency.
/// VBV buffer in kbit. NVENC ull and VAAPI keep the small low-latency buffer; libx264 needs room for a
/// whole IDR frame, otherwise large desktops get near-grey keyframes that sharpen over the GOP (#8).
/// Half a second of bitrate still starved repeated 0.5 s-GOP keyframes at 3296x2036/2M; one second did not.
/// VBV buffer in kbit. libx264 and NVENC need room for a whole IDR frame, otherwise large desktops get
/// keyframes quantized into a blur that sharpens over the GOP (#8 libx264, #103 NVENC). Half a second of
/// bitrate still starved repeated 0.5 s-GOP keyframes at 3296x2036/2M; one second did not. The 8000 kbit
/// cap bounds the keyframe burst: 4K hevc_nvenc CQ18 IDRs measured 0.45 MB (desktop) to 0.76 MB (photo).
/// VAAPI keeps the small low-latency buffer unless UU_VAAPI_BUFFER_MS asks for more.
fn buffer_kbits(max_kbps: u32, encoder: Encoder) -> u32 {
let env = match encoder {
Encoder::Cpu => return max_kbps.clamp(500, 8000),
@@ -807,13 +807,14 @@ fn buffer_kbits(max_kbps: u32, encoder: Encoder) -> u32 {
Encoder::Nvenc => "UU_NVENC_BUFFER_MS",
};
let requested = std::env::var(env).ok();
buffer_ms_kbits(max_kbps, requested.as_deref()).unwrap_or((max_kbps / 8).clamp(100, 500))
let default = if encoder == Encoder::Nvenc { max_kbps.clamp(500, 8000) } else { (max_kbps / 8).clamp(100, 500) };
buffer_ms_kbits(max_kbps, requested.as_deref()).unwrap_or(default)
}
/// Opt-in hardware VBV buffer: `码率上限 × 毫秒 / 1000` for `UU_VAAPI_BUFFER_MS` /
/// `UU_NVENC_BUFFER_MS` in 10-1000 ms. With the 100-500 kbit default the encoder quantizes a
/// whole desktop keyframe, which reads as a blur that sharpens over each GOP (#42 VAAPI, #103 NVENC).
/// Unset, out of range or unparsable values keep the default buffer.
/// Hardware VBV buffer override: `码率上限 × 毫秒 / 1000` for `UU_VAAPI_BUFFER_MS` /
/// `UU_NVENC_BUFFER_MS` in 10-1000 ms. With VAAPI's 100-500 kbit default the encoder quantizes a
/// whole desktop keyframe, which reads as a blur that sharpens over each GOP (#42); a small NVENC value
/// brings back the old low-latency buffer. Unset, out of range or unparsable values keep the default buffer.
fn buffer_ms_kbits(max_kbps: u32, value: Option<&str>) -> Option<u32> {
let ms = value?.trim().parse::<u32>().ok().filter(|ms| (10..=1000).contains(ms))?;
Some((max_kbps.saturating_mul(ms) / 1000).max(1))
@@ -825,6 +826,8 @@ fn buffer_for(settings: &VideoSettings, encoder: Encoder) -> u32 {
} else { buffer_kbits(settings.max_kbps().unwrap_or(30_000), encoder) }
}
/// Rate-control arguments: constant quality capped at 30M (自动), or capped at the viewer's
/// chosen bitrate.
fn rate_args(settings: &VideoSettings, encoder: Encoder) -> (String, String) {
let max_kbps = settings.max_kbps().unwrap_or(30_000);
(format!("{max_kbps}k"), format!("{}k", buffer_for(settings, encoder)))
@@ -1613,7 +1616,10 @@ mod tests {
#[test]
fn x264_buffer_fits_keyframes() {
assert_eq!(buffer_kbits(2000, Encoder::Nvenc), 250);
// NVENC follows libx264 (#103): one second of the cap within 500-8000 kbit.
assert_eq!(buffer_kbits(2000, Encoder::Nvenc), 2000);
assert_eq!(buffer_kbits(300, Encoder::Nvenc), 500);
assert_eq!(buffer_kbits(30_000, Encoder::Nvenc), 8000);
assert_eq!(buffer_kbits(2000, Encoder::Vaapi), 250);
assert_eq!(buffer_kbits(2000, Encoder::Cpu), 2000);
assert_eq!(buffer_kbits(300, Encoder::Cpu), 500);