Skip to main content

yscv_video_mpp/
lib.rs

1//! Rockchip MPP (Media Process Platform) hardware H.264/HEVC encoder.
2//!
3//! Resolves `librockchip_mpp.so` at runtime via `dlopen` — the binary
4//! compiles on every platform; HW encoding only activates on Rockchip
5//! devices where the library is present.
6//!
7//! # Why this exists
8//!
9//! Software H.264 encode of a 720p I-frame takes 20–30 ms on RK3588's
10//! A76 cores. That alone exceeds the 10 ms FPV budget. Rockchip's MPP
11//! does the same encode in 2–3 ms on dedicated VPU hardware, plus
12//! supports zero-copy input directly from DMA-BUF or `MB_BLK` (the same
13//! handle type used by `RknnBackend::wrap_mb_blk`).
14//!
15//! # Coverage
16//!
17//! - `mpp_create` / `mpp_init` / `mpp_destroy` — context lifecycle
18//! - `mpp_packet_*` — encoded NAL output
19//! - `mpp_frame_*` — raw input wrapping
20//! - `mpi.encode_put_frame` / `encode_get_packet` — synchronous encode
21//! - `mpp_buffer_get_mpp_buffer` — extract MB_BLK for zero-copy input
22//!
23//! # Safety
24//!
25//! All `unsafe` is confined to FFI call sites. Library symbols obtained
26//! via `dlopen`/`dlsym` are checked non-null. RAII Drop handlers free
27//! every MPP resource (encoder ctx, frames, packets) deterministically.
28
29#![cfg_attr(not(feature = "mpp"), allow(dead_code))]
30#![allow(unsafe_code)]
31
32mod error;
33
34#[cfg(feature = "mpp")]
35mod encoder;
36#[cfg(feature = "mpp")]
37mod ffi;
38
39pub use error::{MppError, MppResult};
40
41#[cfg(feature = "mpp")]
42pub use encoder::{MppEncoderConfig, MppH264Encoder};
43
44/// Check whether `librockchip_mpp.so` is loadable on this host.
45///
46/// On non-Linux platforms or non-feature-enabled builds, returns `false`.
47pub fn mpp_available() -> bool {
48    #[cfg(all(feature = "mpp", target_os = "linux"))]
49    {
50        ffi::probe_library()
51    }
52    #[cfg(not(all(feature = "mpp", target_os = "linux")))]
53    {
54        false
55    }
56}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    #[test]
63    fn mpp_unavailable_on_dev_host() {
64        // On macOS/Linux without librockchip_mpp.so, must report false.
65        // Real Rockchip hardware is the only place this returns true.
66        let _ = mpp_available();
67    }
68}