forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB5c8eff8Tahinli2 months ago2949 commits

error.rs89 lines, 2.7 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}
34
35impl Error {
36 /// Build an [`Error::Unsupported`].
37 pub fn unsupported(what: impl Into<String>, why: impl Into<String>) -> Error {
38 Error::Unsupported {
39 what: what.into(),
40 why: why.into(),
41 }
42 }
43
44 /// Build an [`Error::Config`].
45 pub fn config(what: impl Into<String>) -> Error {
46 Error::Config(what.into())
47 }
48
49 /// The raw `VAStatus`, when this came from a libva call.
50 pub fn va_status(&self) -> Option<i32> {
51 match self {
52 Error::Va(e) => e.status(),
53 _ => None,
54 }
55 }
56}
57
58impl From<ec_va::Error> for Error {
59 fn from(e: ec_va::Error) -> Error {
60 Error::Va(e)
61 }
62}
63
64impl From<ec_core::Error> for Error {
65 fn from(e: ec_core::Error) -> Error {
66 Error::Stream(e)
67 }
68}
69
70impl fmt::Display for Error {
71 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72 match self {
73 Error::Va(e) => write!(f, "VA-API: {e}"),
74 Error::Stream(e) => write!(f, "bitstream: {e}"),
75 Error::Unsupported { what, why } => write!(f, "{what} is not supported: {why}"),
76 Error::Config(what) => write!(f, "{what}"),
77 }
78 }
79}
80
81impl std::error::Error for Error {
82 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
83 match self {
84 Error::Va(e) => Some(e),
85 Error::Stream(e) => Some(e),
86 _ => None,
87 }
88 }
89}