| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | use std::ffi::{CString, c_char, c_void}; |
| 13 | use std::path::{Path, PathBuf}; |
| 14 | use std::sync::OnceLock; |
| 15 | |
| 16 | use libloading::Library; |
| 17 | |
| 18 | const LIB_NAME: &str = "libengine_hw.so"; |
| 19 | |
| 20 | |
| 21 | #[repr(C)] |
| 22 | #[derive(Default)] |
| 23 | pub struct VhMeta { |
| 24 | pub width: u32, |
| 25 | pub height: u32, |
| 26 | pub frame_rate: f64, |
| 27 | pub frame_count: u32, |
| 28 | } |
| 29 | |
| 30 | |
| 31 | |
| 32 | #[repr(C)] |
| 33 | pub struct VhFrame { |
| 34 | pub y: *const u8, |
| 35 | pub u: *const u8, |
| 36 | pub v: *const u8, |
| 37 | pub y_stride: usize, |
| 38 | pub u_stride: usize, |
| 39 | pub v_stride: usize, |
| 40 | pub width: u32, |
| 41 | pub height: u32, |
| 42 | } |
| 43 | |
| 44 | impl Default for VhFrame { |
| 45 | fn default() -> Self { |
| 46 | Self { |
| 47 | y: std::ptr::null(), |
| 48 | u: std::ptr::null(), |
| 49 | v: std::ptr::null(), |
| 50 | y_stride: 0, |
| 51 | u_stride: 0, |
| 52 | v_stride: 0, |
| 53 | width: 0, |
| 54 | height: 0, |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | struct Plugin { |
| 60 | open_at: unsafe extern "C" fn(*const c_char, u32) -> *mut c_void, |
| 61 | meta: unsafe extern "C" fn(*mut c_void, *mut VhMeta) -> i32, |
| 62 | next_frame: unsafe extern "C" fn(*mut c_void, *mut VhFrame) -> i32, |
| 63 | close: unsafe extern "C" fn(*mut c_void), |
| 64 | |
| 65 | _lib: Library, |
| 66 | } |
| 67 | |
| 68 | fn plugin() -> Option<&'static Plugin> { |
| 69 | static PLUGIN: OnceLock<Option<Plugin>> = OnceLock::new(); |
| 70 | PLUGIN.get_or_init(load).as_ref() |
| 71 | } |
| 72 | |
| 73 | |
| 74 | |
| 75 | fn candidates() -> impl Iterator<Item = PathBuf> { |
| 76 | std::env::current_exe() |
| 77 | .ok() |
| 78 | .and_then(|exe| exe.parent().map(|dir| dir.join(LIB_NAME))) |
| 79 | .into_iter() |
| 80 | .chain(std::iter::once(PathBuf::from(LIB_NAME))) |
| 81 | } |
| 82 | |
| 83 | fn load() -> Option<Plugin> { |
| 84 | for candidate in candidates() { |
| 85 | |
| 86 | |
| 87 | |
| 88 | let lib = match unsafe { Library::new(candidate) } { |
| 89 | Ok(lib) => lib, |
| 90 | Err(_) => continue, |
| 91 | }; |
| 92 | let plugin = unsafe { |
| 93 | (|| { |
| 94 | Some(Plugin { |
| 95 | open_at: *lib.get(b"vh_open_at\0").ok()?, |
| 96 | meta: *lib.get(b"vh_meta\0").ok()?, |
| 97 | next_frame: *lib.get(b"vh_next_frame\0").ok()?, |
| 98 | close: *lib.get(b"vh_close\0").ok()?, |
| 99 | _lib: lib, |
| 100 | }) |
| 101 | })() |
| 102 | }; |
| 103 | if plugin.is_some() { |
| 104 | return plugin; |
| 105 | } |
| 106 | } |
| 107 | None |
| 108 | } |
| 109 | |
| 110 | |
| 111 | pub struct HwSession { |
| 112 | plugin: &'static Plugin, |
| 113 | handle: *mut c_void, |
| 114 | } |
| 115 | |
| 116 | impl HwSession { |
| 117 | |
| 118 | |
| 119 | pub fn open(path: &Path) -> Option<Self> { |
| 120 | Self::open_at(path, 0) |
| 121 | } |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | pub fn open_at(path: &Path, start_frame: u32) -> Option<Self> { |
| 127 | let plugin = plugin()?; |
| 128 | let c_path = CString::new(path.as_os_str().as_encoded_bytes()).ok()?; |
| 129 | |
| 130 | let target_sample = start_frame.saturating_add(1); |
| 131 | |
| 132 | let handle = unsafe { (plugin.open_at)(c_path.as_ptr(), target_sample) }; |
| 133 | if handle.is_null() { |
| 134 | return None; |
| 135 | } |
| 136 | Some(Self { plugin, handle }) |
| 137 | } |
| 138 | |
| 139 | pub fn meta(&self) -> Option<VhMeta> { |
| 140 | let mut meta = VhMeta::default(); |
| 141 | |
| 142 | if unsafe { (self.plugin.meta)(self.handle, &mut meta) } < 0 { |
| 143 | return None; |
| 144 | } |
| 145 | Some(meta) |
| 146 | } |
| 147 | |
| 148 | |
| 149 | |
| 150 | pub fn next_frame(&mut self) -> crate::Result<Option<(&[u8], &[u8], &[u8], u32, u32)>> { |
| 151 | let mut frame = VhFrame::default(); |
| 152 | |
| 153 | |
| 154 | match unsafe { (self.plugin.next_frame)(self.handle, &mut frame) } { |
| 155 | 0 => return Ok(None), |
| 156 | 1 => {} |
| 157 | code => return Err(format!("hardware decode failed (code {code})").into()), |
| 158 | } |
| 159 | let (w, h) = (frame.width as usize, frame.height as usize); |
| 160 | let (cw, ch) = (w.div_ceil(2), h.div_ceil(2)); |
| 161 | if frame.y.is_null() |
| 162 | || frame.u.is_null() |
| 163 | || frame.v.is_null() |
| 164 | || frame.y_stride != w |
| 165 | || frame.u_stride != cw |
| 166 | || frame.v_stride != cw |
| 167 | { |
| 168 | return Err("hardware plugin returned a non-packed I420 frame".into()); |
| 169 | } |
| 170 | |
| 171 | |
| 172 | unsafe { |
| 173 | Ok(Some(( |
| 174 | std::slice::from_raw_parts(frame.y, w * h), |
| 175 | std::slice::from_raw_parts(frame.u, cw * ch), |
| 176 | std::slice::from_raw_parts(frame.v, cw * ch), |
| 177 | frame.width, |
| 178 | frame.height, |
| 179 | ))) |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | impl Drop for HwSession { |
| 185 | fn drop(&mut self) { |
| 186 | |
| 187 | unsafe { (self.plugin.close)(self.handle) } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | |
| 192 | |
| 193 | unsafe impl Send for HwSession {} |
| 194 | |
| 195 | struct EncPlugin { |
| 196 | open: extern "C" fn(u32, u32, u32, u32, u64) -> *mut c_void, |
| 197 | frame: |
| 198 | unsafe extern "C" fn(*mut c_void, *const VhFrame, i32, *mut *const u8, *mut usize) -> i32, |
| 199 | drain: unsafe extern "C" fn(*mut c_void, *mut *const u8, *mut usize) -> i32, |
| 200 | close: unsafe extern "C" fn(*mut c_void), |
| 201 | |
| 202 | _lib: Library, |
| 203 | } |
| 204 | |
| 205 | fn enc_plugin() -> Option<&'static EncPlugin> { |
| 206 | static PLUGIN: OnceLock<Option<EncPlugin>> = OnceLock::new(); |
| 207 | PLUGIN.get_or_init(load_enc).as_ref() |
| 208 | } |
| 209 | |
| 210 | fn load_enc() -> Option<EncPlugin> { |
| 211 | for candidate in candidates() { |
| 212 | |
| 213 | |
| 214 | let lib = match unsafe { Library::new(candidate) } { |
| 215 | Ok(lib) => lib, |
| 216 | Err(_) => continue, |
| 217 | }; |
| 218 | let plugin = unsafe { |
| 219 | (|| { |
| 220 | Some(EncPlugin { |
| 221 | open: *lib.get(b"vh_enc_open\0").ok()?, |
| 222 | frame: *lib.get(b"vh_enc_frame\0").ok()?, |
| 223 | drain: *lib.get(b"vh_enc_drain\0").ok()?, |
| 224 | close: *lib.get(b"vh_enc_close\0").ok()?, |
| 225 | _lib: lib, |
| 226 | }) |
| 227 | })() |
| 228 | }; |
| 229 | if plugin.is_some() { |
| 230 | return plugin; |
| 231 | } |
| 232 | } |
| 233 | None |
| 234 | } |
| 235 | |
| 236 | |
| 237 | pub struct HwEncoder { |
| 238 | plugin: &'static EncPlugin, |
| 239 | handle: *mut c_void, |
| 240 | } |
| 241 | |
| 242 | impl HwEncoder { |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | pub fn open(width: u32, height: u32, fps_num: u32, fps_den: u32, bitrate: u64) -> Option<Self> { |
| 249 | let plugin = enc_plugin()?; |
| 250 | let handle = (plugin.open)(width, height, fps_num, fps_den, bitrate); |
| 251 | if handle.is_null() { |
| 252 | return None; |
| 253 | } |
| 254 | Some(Self { plugin, handle }) |
| 255 | } |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | pub fn encode( |
| 261 | &mut self, |
| 262 | y: &[u8], |
| 263 | u: &[u8], |
| 264 | v: &[u8], |
| 265 | width: u32, |
| 266 | height: u32, |
| 267 | force_key: bool, |
| 268 | ) -> crate::Result<Option<&[u8]>> { |
| 269 | let (w, h) = (width as usize, height as usize); |
| 270 | let (cw, ch) = (w.div_ceil(2), h.div_ceil(2)); |
| 271 | if y.len() < w * h || u.len() < cw * ch || v.len() < cw * ch { |
| 272 | return Err("encode input is not a packed I420 frame".into()); |
| 273 | } |
| 274 | let frame = VhFrame { |
| 275 | y: y.as_ptr(), |
| 276 | u: u.as_ptr(), |
| 277 | v: v.as_ptr(), |
| 278 | y_stride: w, |
| 279 | u_stride: cw, |
| 280 | v_stride: cw, |
| 281 | width, |
| 282 | height, |
| 283 | }; |
| 284 | |
| 285 | |
| 286 | self.take(|plugin, handle, out, out_len| unsafe { |
| 287 | (plugin.frame)(handle, &frame, force_key as i32, out, out_len) |
| 288 | }) |
| 289 | } |
| 290 | |
| 291 | |
| 292 | |
| 293 | pub fn drain(&mut self) -> crate::Result<Option<&[u8]>> { |
| 294 | |
| 295 | self.take(|plugin, handle, out, out_len| unsafe { (plugin.drain)(handle, out, out_len) }) |
| 296 | } |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | fn take( |
| 302 | &mut self, |
| 303 | call: impl FnOnce(&EncPlugin, *mut c_void, *mut *const u8, *mut usize) -> i32, |
| 304 | ) -> crate::Result<Option<&[u8]>> { |
| 305 | let mut ptr: *const u8 = std::ptr::null(); |
| 306 | let mut len: usize = 0; |
| 307 | match call(self.plugin, self.handle, &mut ptr, &mut len) { |
| 308 | 0 => return Ok(None), |
| 309 | 1 => {} |
| 310 | code => return Err(format!("hardware encode failed (code {code})").into()), |
| 311 | } |
| 312 | if ptr.is_null() || len == 0 { |
| 313 | return Err("hardware encoder returned an empty access unit".into()); |
| 314 | } |
| 315 | |
| 316 | |
| 317 | Ok(Some(unsafe { std::slice::from_raw_parts(ptr, len) })) |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | impl Drop for HwEncoder { |
| 322 | fn drop(&mut self) { |
| 323 | |
| 324 | unsafe { (self.plugin.close)(self.handle) } |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | |
| 329 | unsafe impl Send for HwEncoder {} |