//! Typed errors. Nothing in this crate panics on a driver or bitstream fault. //! //! That is a hard requirement rather than a style preference: edith loads the //! VA-API path as a `dlopen`ed plugin behind `catch_unwind`, and its //! fall-back-to-software guarantee is only as good as this crate's promise to //! return instead of unwinding. Every `VAStatus` becomes an [`Error::Va`], //! every malformed bitstream an [`Error::Stream`], and every capability this //! GPU does not have an [`Error::Unsupported`] naming what and why. use std::fmt; /// Result alias for this crate. pub type Result = std::result::Result; /// Everything that can go wrong on the hardware path. #[derive(Debug)] #[non_exhaustive] pub enum Error { /// A libva call failed, or the driver contradicted its own API. #[cfg(target_os = "linux")] Va(ec_va::Error), /// The bitstream could not be parsed. Stream(ec_core::Error), /// A capability this GPU or this build genuinely does not have. Unsupported { /// What was asked for, e.g. `"VP9 Profile 1 decode"`. what: String, /// Why it cannot be served, e.g. `"driver reports no VLD entrypoint"`. why: String, }, /// The caller asked for something inconsistent, e.g. feeding H.264 to a /// decoder configured for AV1, or encoding a frame of the wrong size. Config(String), /// A Vulkan Video call failed or the Vulkan path refused the request. Vk(String), /// The GPU was reset (a hung ring, a lost context) while this session was /// alive, or a session was asked for during the grace period after one. /// The session is dead; use software. See [`crate::reset`]. HwReset(String), } impl Error { /// Build an [`Error::Unsupported`]. pub fn unsupported(what: impl Into, why: impl Into) -> Error { Error::Unsupported { what: what.into(), why: why.into(), } } /// Build an [`Error::Config`]. pub fn config(what: impl Into) -> Error { Error::Config(what.into()) } /// The decoder was poisoned by a hung or lost GPU context. pub fn poisoned(what: impl Into) -> Error { Error::Config(format!("hardware decoder poisoned: {}", what.into())) } /// Build an [`Error::HwReset`]. pub fn hw_reset(what: impl Into) -> Error { Error::HwReset(what.into()) } /// Whether this is an [`Error::HwReset`]. pub fn is_hw_reset(&self) -> bool { matches!(self, Error::HwReset(_)) } /// The raw `VAStatus`, when this came from a libva call. pub fn va_status(&self) -> Option { match self { #[cfg(target_os = "linux")] Error::Va(e) => e.status(), _ => None, } } } #[cfg(target_os = "linux")] impl From for Error { fn from(e: ec_va::Error) -> Error { Error::Va(e) } } impl From for Error { fn from(e: ec_vk::Error) -> Error { match e { ec_vk::Error::DeviceLost(call) => { crate::reset::report_gpu_reset(); Error::hw_reset(format!("Vulkan device lost in {call}")) } ec_vk::Error::Unsupported(why) => Error::unsupported("Vulkan Video", why), other => Error::Vk(other.to_string()), } } } impl From for Error { fn from(e: ec_core::Error) -> Error { Error::Stream(e) } } impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { #[cfg(target_os = "linux")] Error::Va(e) => write!(f, "VA-API: {e}"), Error::Stream(e) => write!(f, "bitstream: {e}"), Error::Unsupported { what, why } => write!(f, "{what} is not supported: {why}"), Error::Config(what) => write!(f, "{what}"), Error::Vk(what) => write!(f, "Vulkan Video: {what}"), Error::HwReset(what) => write!(f, "GPU reset: {what}"), } } } impl std::error::Error for Error { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { match self { #[cfg(target_os = "linux")] Error::Va(e) => Some(e), Error::Stream(e) => Some(e), _ => None, } } }