forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

error.rs130 lines, 4.3 KiBHistoryRawDownload
1//! Typed errors. Nothing in this crate panics on a driver or bitstream fault.
2//!
3//! That is a hard requirement rather than a style preference: edith loads the
4//! VA-API path as a `dlopen`ed plugin behind `catch_unwind`, and its
5//! fall-back-to-software guarantee is only as good as this crate's promise to
6//! return instead of unwinding. Every `VAStatus` becomes an [`Error::Va`],
7//! every malformed bitstream an [`Error::Stream`], and every capability this
8//! GPU does not have an [`Error::Unsupported`] naming what and why.
9
10use std::fmt;
11
12/// Result alias for this crate.
13pub type Result<T> = std::result::Result<T, Error>;
14
15/// Everything that can go wrong on the hardware path.
16#[derive(Debug)]
17#[non_exhaustive]
18pub enum Error {
19 /// A libva call failed, or the driver contradicted its own API.
20 #[cfg(target_os = "linux")]
21 Va(ec_va::Error),
22 /// The bitstream could not be parsed.
23 Stream(ec_core::Error),
24 /// A capability this GPU or this build genuinely does not have.
25 Unsupported {
26 /// What was asked for, e.g. `"VP9 Profile 1 decode"`.
27 what: String,
28 /// Why it cannot be served, e.g. `"driver reports no VLD entrypoint"`.
29 why: String,
30 },
31 /// The caller asked for something inconsistent, e.g. feeding H.264 to a
32 /// decoder configured for AV1, or encoding a frame of the wrong size.
33 Config(String),
34 /// A Vulkan Video call failed or the Vulkan path refused the request.
35 Vk(String),
36 /// The GPU was reset (a hung ring, a lost context) while this session was
37 /// alive, or a session was asked for during the grace period after one.
38 /// The session is dead; use software. See [`crate::reset`].
39 HwReset(String),
40}
41
42impl Error {
43 /// Build an [`Error::Unsupported`].
44 pub fn unsupported(what: impl Into<String>, why: impl Into<String>) -> Error {
45 Error::Unsupported {
46 what: what.into(),
47 why: why.into(),
48 }
49 }
50
51 /// Build an [`Error::Config`].
52 pub fn config(what: impl Into<String>) -> Error {
53 Error::Config(what.into())
54 }
55
56 /// The decoder was poisoned by a hung or lost GPU context.
57 pub fn poisoned(what: impl Into<String>) -> Error {
58 Error::Config(format!("hardware decoder poisoned: {}", what.into()))
59 }
60
61 /// Build an [`Error::HwReset`].
62 pub fn hw_reset(what: impl Into<String>) -> Error {
63 Error::HwReset(what.into())
64 }
65
66 /// Whether this is an [`Error::HwReset`].
67 pub fn is_hw_reset(&self) -> bool {
68 matches!(self, Error::HwReset(_))
69 }
70
71 /// The raw `VAStatus`, when this came from a libva call.
72 pub fn va_status(&self) -> Option<i32> {
73 match self {
74 #[cfg(target_os = "linux")]
75 Error::Va(e) => e.status(),
76 _ => None,
77 }
78 }
79}
80
81#[cfg(target_os = "linux")]
82impl From<ec_va::Error> for Error {
83 fn from(e: ec_va::Error) -> Error {
84 Error::Va(e)
85 }
86}
87
88impl From<ec_vk::Error> for Error {
89 fn from(e: ec_vk::Error) -> Error {
90 match e {
91 ec_vk::Error::DeviceLost(call) => {
92 crate::reset::report_gpu_reset();
93 Error::hw_reset(format!("Vulkan device lost in {call}"))
94 }
95 ec_vk::Error::Unsupported(why) => Error::unsupported("Vulkan Video", why),
96 other => Error::Vk(other.to_string()),
97 }
98 }
99}
100
101impl From<ec_core::Error> for Error {
102 fn from(e: ec_core::Error) -> Error {
103 Error::Stream(e)
104 }
105}
106
107impl fmt::Display for Error {
108 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
109 match self {
110 #[cfg(target_os = "linux")]
111 Error::Va(e) => write!(f, "VA-API: {e}"),
112 Error::Stream(e) => write!(f, "bitstream: {e}"),
113 Error::Unsupported { what, why } => write!(f, "{what} is not supported: {why}"),
114 Error::Config(what) => write!(f, "{what}"),
115 Error::Vk(what) => write!(f, "Vulkan Video: {what}"),
116 Error::HwReset(what) => write!(f, "GPU reset: {what}"),
117 }
118 }
119}
120
121impl std::error::Error for Error {
122 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
123 match self {
124 #[cfg(target_os = "linux")]
125 Error::Va(e) => Some(e),
126 Error::Stream(e) => Some(e),
127 _ => None,
128 }
129 }
130}