forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB86fb0f3Tahinli7 days ago3927 commits

error.rs109 lines, 3.5 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 Va(ec_va::Error),
21 /// The bitstream could not be parsed.
22 Stream(ec_core::Error),
23 /// A capability this GPU or this build genuinely does not have.
24 Unsupported {
25 /// What was asked for, e.g. `"VP9 Profile 1 decode"`.
26 what: String,
27 /// Why it cannot be served, e.g. `"driver reports no VLD entrypoint"`.
28 why: String,
29 },
30 /// The caller asked for something inconsistent, e.g. feeding H.264 to a
31 /// decoder configured for AV1, or encoding a frame of the wrong size.
32 Config(String),
33 /// The GPU was reset (a hung ring, a lost context) while this session was
34 /// alive, or a session was asked for during the grace period after one.
35 /// The session is dead; use software. See [`crate::reset`].
36 HwReset(String),
37}
38
39impl Error {
40 /// Build an [`Error::Unsupported`].
41 pub fn unsupported(what: impl Into<String>, why: impl Into<String>) -> Error {
42 Error::Unsupported {
43 what: what.into(),
44 why: why.into(),
45 }
46 }
47
48 /// Build an [`Error::Config`].
49 pub fn config(what: impl Into<String>) -> Error {
50 Error::Config(what.into())
51 }
52
53 /// The decoder was poisoned by a hung or lost GPU context.
54 pub fn poisoned(what: impl Into<String>) -> Error {
55 Error::Config(format!("hardware decoder poisoned: {}", what.into()))
56 }
57
58 /// Build an [`Error::HwReset`].
59 pub fn hw_reset(what: impl Into<String>) -> Error {
60 Error::HwReset(what.into())
61 }
62
63 /// Whether this is an [`Error::HwReset`].
64 pub fn is_hw_reset(&self) -> bool {
65 matches!(self, Error::HwReset(_))
66 }
67
68 /// The raw `VAStatus`, when this came from a libva call.
69 pub fn va_status(&self) -> Option<i32> {
70 match self {
71 Error::Va(e) => e.status(),
72 _ => None,
73 }
74 }
75}
76
77impl From<ec_va::Error> for Error {
78 fn from(e: ec_va::Error) -> Error {
79 Error::Va(e)
80 }
81}
82
83impl From<ec_core::Error> for Error {
84 fn from(e: ec_core::Error) -> Error {
85 Error::Stream(e)
86 }
87}
88
89impl fmt::Display for Error {
90 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
91 match self {
92 Error::Va(e) => write!(f, "VA-API: {e}"),
93 Error::Stream(e) => write!(f, "bitstream: {e}"),
94 Error::Unsupported { what, why } => write!(f, "{what} is not supported: {why}"),
95 Error::Config(what) => write!(f, "{what}"),
96 Error::HwReset(what) => write!(f, "GPU reset: {what}"),
97 }
98 }
99}
100
101impl std::error::Error for Error {
102 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
103 match self {
104 Error::Va(e) => Some(e),
105 Error::Stream(e) => Some(e),
106 _ => None,
107 }
108 }
109}