From 563c808836e11520b82ca3b6f4edd11f5a56af7a Mon Sep 17 00:00:00 2001 From: mofeng-git Date: Fri, 17 Jul 2026 23:25:04 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=AE=8C=E5=96=84=20libyuv=20=E9=9D=99?= =?UTF-8?q?=E6=80=81=E5=BA=93=E6=9E=84=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- res/vcpkg/libyuv/build.rs | 71 ++++++++++++++++++++++++------------- res/vcpkg/libyuv/src/lib.rs | 33 +++++++++++------ 2 files changed, 68 insertions(+), 36 deletions(-) diff --git a/res/vcpkg/libyuv/build.rs b/res/vcpkg/libyuv/build.rs index 3d7f8ffe..33205e2c 100644 --- a/res/vcpkg/libyuv/build.rs +++ b/res/vcpkg/libyuv/build.rs @@ -86,6 +86,21 @@ fn generate_bindings(cpp_dir: &Path) { } fn link_libyuv() { + println!("cargo:rerun-if-env-changed=ONE_KVM_LIBS_PATH"); + println!("cargo:rerun-if-env-changed=LIBYUV_STATIC"); + + // An explicit library root must take precedence over host discovery. + if env::var("ONE_KVM_LIBS_PATH") + .ok() + .is_some_and(|path| !path.trim().is_empty()) + { + if link_system() { + return; + } + + panic!("libyuv not found under ONE_KVM_LIBS_PATH"); + } + // Try vcpkg first if let Some(vcpkg_installed) = vcpkg_installed_root() { if link_vcpkg(vcpkg_installed) { @@ -232,17 +247,21 @@ fn link_system() -> bool { // Build custom library paths based on target architecture: // 1. Check ONE_KVM_LIBS_PATH environment variable (explicit override) // 2. Fall back to architecture-based detection - let custom_lib_path = if let Ok(path) = env::var("ONE_KVM_LIBS_PATH") { - format!("{}/lib", path) - } else { - match target_arch.as_str() { - "x86_64" => "/usr/local/lib", - "aarch64" => "/usr/aarch64-linux-gnu/lib", - "arm" => "/usr/arm-linux-gnueabihf/lib", - _ => "", - } - .to_string() - }; + let explicit_lib_root = env::var("ONE_KVM_LIBS_PATH") + .ok() + .filter(|path| !path.trim().is_empty()); + let custom_lib_path = explicit_lib_root + .as_ref() + .map(|path| format!("{}/lib", path)) + .unwrap_or_else(|| { + match target_arch.as_str() { + "x86_64" => "/usr/local/lib", + "aarch64" => "/usr/aarch64-linux-gnu/lib", + "arm" => "/usr/arm-linux-gnueabihf/lib", + _ => "", + } + .to_string() + }); // Try common system library paths (custom paths first) let mut lib_paths: Vec = Vec::new(); @@ -252,20 +271,22 @@ fn link_system() -> bool { lib_paths.push(custom_lib_path); } - // Then standard paths - lib_paths.extend( - [ - "/usr/local/lib", // Custom builds - "/usr/local/lib64", - "/usr/lib", - "/usr/lib64", - "/usr/lib/x86_64-linux-gnu", // Debian/Ubuntu x86_64 - "/usr/lib/aarch64-linux-gnu", // Debian/Ubuntu ARM64 - "/usr/lib/arm-linux-gnueabihf", // Debian/Ubuntu ARMv7 - ] - .iter() - .map(|s| s.to_string()), - ); + // An explicit root is strict: do not silently fall back to host libraries. + if explicit_lib_root.is_none() { + lib_paths.extend( + [ + "/usr/local/lib", // Custom builds + "/usr/local/lib64", + "/usr/lib", + "/usr/lib64", + "/usr/lib/x86_64-linux-gnu", // Debian/Ubuntu x86_64 + "/usr/lib/aarch64-linux-gnu", // Debian/Ubuntu ARM64 + "/usr/lib/arm-linux-gnueabihf", // Debian/Ubuntu ARMv7 + ] + .iter() + .map(|s| s.to_string()), + ); + } for path in &lib_paths { let lib_path = Path::new(path); diff --git a/res/vcpkg/libyuv/src/lib.rs b/res/vcpkg/libyuv/src/lib.rs index 896a2eab..327bb9e3 100644 --- a/res/vcpkg/libyuv/src/lib.rs +++ b/res/vcpkg/libyuv/src/lib.rs @@ -1088,17 +1088,17 @@ pub fn bgr24_to_nv12(src: &[u8], dst: &mut [u8], width: i32, height: i32) -> Res #[cfg(not(windows))] { - let y_size = w * h; - call_yuv!(RGB24ToNV12( - src.as_ptr(), - width * 3, - dst.as_mut_ptr(), - width, - dst[y_size..].as_mut_ptr(), - width, - width, - height, - )) + let y_size = w * h; + call_yuv!(RGB24ToNV12( + src.as_ptr(), + width * 3, + dst.as_mut_ptr(), + width, + dst[y_size..].as_mut_ptr(), + width, + width, + height, + )) } } @@ -1391,4 +1391,15 @@ mod tests { assert_eq!(converter.dimensions(), (8, 8)); assert_eq!(converter.nv12_buffer().len(), nv12_size(8, 8)); } + + #[test] + fn test_bgr24_to_nv12() { + let src = [0u8; 2 * 2 * 3]; + let mut dst = [0xffu8; 2 * 2 * 3 / 2]; + + bgr24_to_nv12(&src, &mut dst, 2, 2).unwrap(); + + assert_eq!(&dst[..4], &[16, 16, 16, 16]); + assert_eq!(&dst[4..], &[128, 128]); + } }