diff --git a/AGENTS.md b/AGENTS.md index 572e751..5cbcfce 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -161,7 +161,7 @@ every 60 s. Diagnostic subcommands: `doctor` (paste-safe report: session, selected backend, engine plugins, uinput, encoders, default codec, sign-in/service state; booleans and names only), `login [--no-gui]`, `unlock-check` (password on stdin, prints only accepted/rejected), -`desktop-probe [new.png]`, `desktop-video-probe` (`UU_PROBE_FPS`, `UU_PROBE_FRAMES`, `UU_PROBE_QUALITY` 1-5 = viewer bitrate preset, 5 auto; `UU_PROBE_INPUT` fixed video/image sequence, `UU_PROBE_LOOP=1` repeats), `wayland-input-probe`, `clipboard-acceptance`, +`desktop-probe [new.png]`, `desktop-video-probe` (`UU_PROBE_FPS`, `UU_PROBE_FRAMES`, `UU_PROBE_QUALITY` 1-5 = viewer bitrate preset, 5 auto; `UU_PROBE_CHROMA=444` 真彩; `UU_PROBE_INPUT` fixed video/image sequence, `UU_PROBE_LOOP=1` repeats), `wayland-input-probe`, `clipboard-acceptance`, `portal-acceptance`, `help`. Unknown arguments exit with code 2. Builds up to v0.1.0 did **not** reject them and started a second full host server instead, so check that a subcommand exists in the binary you run. @@ -184,7 +184,7 @@ started a second full host server instead, so check that a subcommand exists in 5 Auto, 6 Custom. Resolution: 16/17 pixel size or 5/6, 10 resolution_type (3 = follow remote). Field 7 `chorma_format` (ChromaFormat 1 = 4:2:0, 3 = 4:4:4) is sent when 真彩 is toggled; ff_capture_setting versions are cumulative (3 CHROMA_SAMPLING, 4 CUSTOM_BITRATE, 5 DPI_SCALE, 6 HDR). 4:4:4 = NVENC `high444p` / x264 `high444` - with `yuv444p`; VAAPI and the in-process NVENC engine stay 4:2:0. + with `yuv444p`; the in-process NVENC engine uses High 4:4:4 / HEVC FRExt (AQ off); VAAPI stays 4:2:0. Reply `RpcResponse{2 CaptureSettingResponse{}}` (no errors = applied) and send Message 25 CaptureSettingSync (field 3 = the chroma actually encoded). - ConnectOptions field 4 `decoder_cap_list` = DecoderCap {1 fps, 2 codec_type (1 H264, 2 H265), 3 width, 4 height, diff --git a/README.en.md b/README.en.md index b439acd..876a251 100644 --- a/README.en.md +++ b/README.en.md @@ -117,7 +117,7 @@ Switching reuses one video track and pauses input until the new source sends a f | Feature | Status | |---|---| | Switching between multiple monitors | Experimental on native Hyprland Wayland; see above | -| True color (4:4:4) | Supported (v0.1.15): the default FFmpeg pipeline with NVENC / libx264 encodes H.264 High 4:4:4; VAAPI and `inproc-av` stay 4:2:0 and report that to the client | +| True color (4:4:4) | Supported (v0.1.15): the default FFmpeg pipeline with NVENC / libx264 encodes H.264 High 4:4:4; `inproc-av` native NVENC encodes H.264 High 4:4:4 / HEVC FRExt; VAAPI stays 4:2:0 and reports that to the client | | HDR | Not implemented | | Super screen / virtual display | Not implemented | | Privacy screen on the host | Not implemented | diff --git a/README.md b/README.md index 9610396..f4a3fad 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,7 @@ GNOME Portal 的控制通道不再等待授权窗口或第一帧画面:先通 | 功能 | 状态 | |---|---| | 多显示器切换 | Hyprland 原生 Wayland 后端实验性支持,见上文 | -| 真彩(4:4:4) | 支持(v0.1.15):NVENC / libx264 的默认 FFmpeg 管线输出 H.264 High 4:4:4;VAAPI 和 `inproc-av` 管线仍为 4:2:0,并如实回报给客户端 | +| 真彩(4:4:4) | 支持(v0.1.15):NVENC / libx264 的默认 FFmpeg 管线输出 H.264 High 4:4:4;`inproc-av` 原生 NVENC 输出 H.264 High 4:4:4 / HEVC FRExt;VAAPI 管线仍为 4:2:0,并如实回报给客户端 | | HDR | 未实现 | | 超级屏 / 虚拟显示器 | 未实现 | | 被控端防窥模式 | 未实现 | diff --git a/benches/nvenc_pipeline_bench.rs b/benches/nvenc_pipeline_bench.rs index 3b74bf7..462082d 100644 --- a/benches/nvenc_pipeline_bench.rs +++ b/benches/nvenc_pipeline_bench.rs @@ -76,6 +76,7 @@ fn main() { max_bps: i64, buffer_bits: i64, is_hevc: i32, + chroma_444: i32, } let cfg = NvencConfigFFI { @@ -88,6 +89,7 @@ fn main() { max_bps: 30_000_000, buffer_bits: 500_000, is_hevc: 1, // H.265 / HEVC + chroma_444: i32::from(std::env::var("UU_BENCH_CHROMA").as_deref() == Ok("444")), }; println!("[2] In-process NVENC H.265 encoding"); diff --git a/c_engine/nvenc_engine.c b/c_engine/nvenc_engine.c index edcd5da..3a849c1 100644 --- a/c_engine/nvenc_engine.c +++ b/c_engine/nvenc_engine.c @@ -233,23 +233,38 @@ NvencEngine* nvenc_create(const NvencConfig* config) { enc_cfg.rcParams.maxBitRate = (uint32_t)(config->max_bps > 0 ? config->max_bps : 15000000); enc_cfg.rcParams.vbvBufferSize = (uint32_t)(config->buffer_bits > 0 ? config->buffer_bits : (config->max_bps > 0 ? config->max_bps / 10 : 1500000)); enc_cfg.rcParams.targetQuality = (uint8_t)(config->cq > 0 ? config->cq : 28); - enc_cfg.rcParams.enableAQ = 1; - // Temporal AQ only where the hardware/codec reports it NV_ENC_CAPS_PARAM caps_param; memset(&caps_param, 0, sizeof(caps_param)); caps_param.version = NV_ENC_CAPS_PARAM_VER; - caps_param.capsToQuery = NV_ENC_CAPS_SUPPORT_TEMPORAL_AQ; int caps_val = 0; - enc_cfg.rcParams.enableTemporalAQ = - engine->fn.nvEncGetEncodeCaps(engine->encoder, codec_guid, &caps_param, &caps_val) == NV_ENC_SUCCESS && caps_val > 0; + if (config->chroma_444) { + // GPUs without 4:4:4 encode fail here; the caller falls back to FFmpeg/libx264, which keep 4:4:4. + caps_param.capsToQuery = NV_ENC_CAPS_SUPPORT_YUV444_ENCODE; + if (engine->fn.nvEncGetEncodeCaps(engine->encoder, codec_guid, &caps_param, &caps_val) != NV_ENC_SUCCESS || caps_val <= 0) { + fprintf(stderr, "[nvenc] 4:4:4 encoding not supported for this codec\n"); + goto fail; + } + // NVENC rejects AQ / temporal AQ in 4:4:4 mode. + enc_cfg.rcParams.enableAQ = 0; + enc_cfg.rcParams.enableTemporalAQ = 0; + } else { + enc_cfg.rcParams.enableAQ = 1; + // Temporal AQ only where the hardware/codec reports it + caps_param.capsToQuery = NV_ENC_CAPS_SUPPORT_TEMPORAL_AQ; + caps_val = 0; + enc_cfg.rcParams.enableTemporalAQ = + engine->fn.nvEncGetEncodeCaps(engine->encoder, codec_guid, &caps_param, &caps_val) == NV_ENC_SUCCESS && caps_val > 0; + } if (engine->is_hevc) { - enc_cfg.profileGUID = NV_ENC_HEVC_PROFILE_MAIN_GUID; + enc_cfg.profileGUID = config->chroma_444 ? NV_ENC_HEVC_PROFILE_FREXT_GUID : NV_ENC_HEVC_PROFILE_MAIN_GUID; + enc_cfg.encodeCodecConfig.hevcConfig.chromaFormatIDC = config->chroma_444 ? 3 : 1; enc_cfg.encodeCodecConfig.hevcConfig.idrPeriod = enc_cfg.gopLength; enc_cfg.encodeCodecConfig.hevcConfig.repeatSPSPPS = 1; enc_cfg.encodeCodecConfig.hevcConfig.outputAUD = 1; } else { - enc_cfg.profileGUID = NV_ENC_H264_PROFILE_BASELINE_GUID; + enc_cfg.profileGUID = config->chroma_444 ? NV_ENC_H264_PROFILE_HIGH_444_GUID : NV_ENC_H264_PROFILE_BASELINE_GUID; + enc_cfg.encodeCodecConfig.h264Config.chromaFormatIDC = config->chroma_444 ? 3 : 1; enc_cfg.encodeCodecConfig.h264Config.idrPeriod = enc_cfg.gopLength; enc_cfg.encodeCodecConfig.h264Config.repeatSPSPPS = 1; enc_cfg.encodeCodecConfig.h264Config.outputAUD = 1; diff --git a/c_engine/nvenc_engine.h b/c_engine/nvenc_engine.h index 4fbe43c..7c57ccc 100644 --- a/c_engine/nvenc_engine.h +++ b/c_engine/nvenc_engine.h @@ -19,7 +19,8 @@ typedef struct { int64_t target_bps; int64_t max_bps; int64_t buffer_bits; - int is_hevc; // 1 = H.265, 0 = H.264 + int is_hevc; // 1 = H.265, 0 = H.264 + int chroma_444; // 1 = 4:4:4 (H.264 High 4:4:4 / HEVC FRExt), 0 = 4:2:0 } NvencConfig; // Initialize and open an NVENC encoding session. Returns NULL on error. diff --git a/src/inproc_encoder.rs b/src/inproc_encoder.rs index 4cc168b..8c48837 100644 --- a/src/inproc_encoder.rs +++ b/src/inproc_encoder.rs @@ -26,6 +26,7 @@ struct NvencConfigFFI { max_bps: i64, buffer_bits: i64, is_hevc: libc::c_int, + chroma_444: libc::c_int, } extern "C" { @@ -70,6 +71,8 @@ pub struct Config { pub max_bps: i64, pub buffer_bits: i64, pub codec: crate::capture_setting::VideoCodec, + /// ChromaFormat (capture_setting::CHROMA_444 = 4:4:4). + pub chroma: u64, } pub fn disabled() -> bool { @@ -145,14 +148,16 @@ pub fn run( 0 }; + let chroma_444 = libc::c_int::from(config.chroma == crate::capture_setting::CHROMA_444); log::info!( - "Initializing Native In-Process NVENC: {}x{}, {} fps, GOP {}, CQ {}, HEVC={}", + "Initializing Native In-Process NVENC: {}x{}, {} fps, GOP {}, CQ {}, HEVC={}, 4:4:4={}", width, height, config.fps, config.gop, config.cq, - is_hevc == 1 + is_hevc == 1, + chroma_444 == 1 ); let ffi_cfg = NvencConfigFFI { @@ -165,6 +170,7 @@ pub fn run( max_bps: config.max_bps, buffer_bits: config.buffer_bits, is_hevc, + chroma_444, }; let mut capture = DamageCapture::open(&config.display) diff --git a/src/video.rs b/src/video.rs index 76b15e8..8ebe7eb 100644 --- a/src/video.rs +++ b/src/video.rs @@ -198,11 +198,9 @@ impl Encoder { fn nvidia_present() -> bool { std::path::Path::new("/proc/driver/nvidia/version").exists() || std::path::Path::new("/dev/nvidia0").exists() } -/// 真彩 (4:4:4) needs the FFmpeg process pipeline with NVENC or libx264 (h264_vaapi and the -/// in-process encoder are 4:2:0 only). +/// 真彩 (4:4:4): NVENC (in-process or FFmpeg) and libx264; h264_vaapi is 4:2:0 only. +/// A GPU without 4:4:4 NVENC falls back to libx264, which keeps 4:4:4. pub fn full_chroma_available() -> bool { - #[cfg(feature = "inproc-av")] - if crate::inproc_encoder::selected(Encoder::requested()) { return false; } Encoder::requested() != Encoder::Vaapi } @@ -618,7 +616,7 @@ async fn read_encoder_with( display: display.clone(), input: size, output: settings.output, fps: u32::from(settings.fps), gop: gop_frames(settings.fps, true) as u32, cq: settings.cq() as u32, target_bps: settings.max_kbps().map_or(16_000, |k| i64::from(k) * 3 / 4) * 1000, - max_bps: max_kbps * 1000, buffer_bits: i64::from(buffer_for(&settings, encoder)) * 1000, codec: settings.codec, + max_bps: max_kbps * 1000, buffer_bits: i64::from(buffer_for(&settings, encoder)) * 1000, codec: settings.codec, chroma: settings.chroma, }; log::info!("Desktop encoder: display={} source={}x{} output={:?} fps={} cq={} maxrate={}k encoder={} pipeline=in-process", display,size.0,size.1,settings.output,settings.fps,config.cq,max_kbps,encoder.name_for(settings.codec)); @@ -793,7 +791,10 @@ fn probe_settings() -> Result { None=>crate::capture_setting::QUALITY_AUTO, Some(v)=>v.parse::().ok().filter(|q|(1..=5).contains(q)).ok_or("UU_PROBE_QUALITY must be 1-5")?, }; - Ok(VideoSettings {fps,quality,codec:probe_codec(),..VideoSettings::default()}) + let mut settings=VideoSettings {fps,quality,codec:probe_codec(),..VideoSettings::default()}; + // UU_PROBE_CHROMA=444 requests 真彩 like the viewer does (only where full_chroma_available()). + if std::env::var("UU_PROBE_CHROMA").as_deref()==Ok("444") && full_chroma_available() { settings.chroma=crate::capture_setting::CHROMA_444; } + Ok(settings) } /// The probe follows `UU_VIDEO_CODEC` so the HEVC path can be measured at all (H.264 otherwise),