kodecs-vaapi 0.3.2

VA-API H.264 and H.265 hardware encode and decode, forked from discord/cros-libva and discord/cros-codecs, with libva loaded at runtime
Documentation
// Copyright 2022 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

//! This module implements the bindgen C FFI bindings for use within this crate

#![allow(
	missing_docs,
	clippy::useless_transmute,
	clippy::too_many_arguments,
	non_upper_case_globals,
	non_snake_case,
	non_camel_case_types,
	dead_code,
	rustdoc::all,
	reason = "generated by bindgen from the vendored libva headers"
)]

// Generated from the vendored headers by `bindgen.sh` and checked in, so a
// build needs neither bindgen nor libclang.
include!("bindings/libva.rs");

use std::sync::OnceLock;

/// The dlopen'd libva symbol table, resolved once on first use. The result is
/// cached (success or failure) so a missing libva is probed at most once.
static VA: OnceLock<Result<Va, String>> = OnceLock::new();

/// Loads libva (via `libva-drm.so.2`) on first call and returns the symbol table.
///
/// `libva-drm.so.2` is linked against `libva.so.2`, so a single dlopen exposes
/// both `vaGetDisplayDRM` and the core `va*` symbols (dlsym on the handle searches
/// its dependencies). Returns `Err` if the library is absent, so a caller on a
/// libva-less host can fall back to another encoder instead of aborting.
pub(crate) fn load() -> Result<&'static Va, &'static str> {
	VA.get_or_init(|| unsafe { Va::new("libva-drm.so.2") }.map_err(|e| e.to_string()))
		.as_ref()
		.map_err(String::as_str)
}

/// Accessor for the loaded libva symbols.
///
/// Panics if libva has not loaded. Every entry point that reaches libva goes
/// through a [`crate::Display`], whose constructor calls [`load`] and bails out on
/// failure, so by the time any other `va*` call runs the library is present.
pub(crate) fn va() -> &'static Va {
	load().expect("libva not loaded; open a Display first")
}