mediadecode_ffmpeg/error.rs
1use ffmpeg_next::Packet;
2
3use crate::backend::Backend;
4
5/// Crate result alias.
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Errors returned from [`crate::VideoDecoder`].
9///
10/// `Debug` is derived; the variants that wrap a payload struct
11/// (`HwDeviceInitFailed`, `AllBackendsFailed`, `FallbackFailed`)
12/// delegate their `Debug` to the payload, which is hand-written
13/// where needed because [`ffmpeg_next::Packet`] (carried by
14/// `AllBackendsFailed::unconsumed_packets` /
15/// `FallbackFailed::unconsumed_packets`) does not derive
16/// `Debug`. Those payloads summarize the packet count rather
17/// than dumping each packet's fields, which would be both noisy
18/// and useless for triage.
19#[derive(Debug, Clone, thiserror::Error)]
20pub enum Error {
21 /// An underlying FFmpeg error.
22 #[error("ffmpeg error: {0}")]
23 Ffmpeg(#[from] ffmpeg_next::Error),
24
25 /// `avcodec_find_decoder` returned null for the input codec id. The id
26 /// is reported as the raw integer (`AVCodecID` discriminant) — we do not
27 /// construct the bindgen `AVCodecID` enum from a runtime value, since
28 /// values outside our build's discriminant set would invoke UB.
29 #[error("no decoder for codec id {0}")]
30 NoCodec(u32),
31
32 /// The codec does not advertise a hardware configuration matching the
33 /// requested backend (via `avcodec_get_hw_config`).
34 #[error("codec does not support backend {0:?}")]
35 BackendUnsupportedByCodec(Backend),
36
37 /// `av_hwdevice_ctx_create` failed for the requested backend. See
38 /// [`HwDeviceInitFailed`] for the payload details. `#[from]` gives
39 /// a free `impl From<HwDeviceInitFailed> for Error`, so inner
40 /// helpers that return `Result<_, HwDeviceInitFailed>` can be
41 /// `?`-propagated into `Error` directly.
42 #[error(transparent)]
43 HwDeviceInitFailed(#[from] HwDeviceInitFailed),
44
45 /// Auto-probe exhausted every backend in the platform's order. See
46 /// [`AllBackendsFailed`] for the payload details (in particular the
47 /// `unconsumed_packets` history that callers should replay through
48 /// their own software decoder for non-seekable inputs). `#[from]`
49 /// gives a free `impl From<AllBackendsFailed> for Error`.
50 #[error(transparent)]
51 AllBackendsFailed(#[from] AllBackendsFailed),
52
53 /// Surfaced by [`crate::FfmpegVideoStreamDecoder`] when a HW->SW
54 /// fallback attempt itself fails. See [`FallbackFailed`] for the
55 /// payload details (in particular the rescued `unconsumed_packets`
56 /// the HW path had already consumed from the caller). `#[from]`
57 /// gives a free `impl From<FallbackFailed> for Error`.
58 #[error(transparent)]
59 FallbackFailed(#[from] FallbackFailed),
60}
61
62/// Payload for [`Error::HwDeviceInitFailed`].
63///
64/// `av_hwdevice_ctx_create` failed for the requested backend.
65#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
66#[error("hardware device init failed for {backend:?}: {source}")]
67pub struct HwDeviceInitFailed {
68 /// Backend that failed to initialise.
69 backend: Backend,
70 /// Underlying FFmpeg error.
71 source: ffmpeg_next::Error,
72}
73
74impl HwDeviceInitFailed {
75 /// Constructs a new [`HwDeviceInitFailed`] payload.
76 #[inline]
77 pub const fn new(backend: Backend, source: ffmpeg_next::Error) -> Self {
78 Self { backend, source }
79 }
80 /// Backend that failed to initialise.
81 #[inline]
82 pub const fn backend(&self) -> Backend {
83 self.backend
84 }
85 /// Underlying FFmpeg error.
86 #[inline]
87 pub const fn source(&self) -> &ffmpeg_next::Error {
88 &self.source
89 }
90 /// Consume the payload, returning the backend identifier and the
91 /// moved FFmpeg error so callers can take ownership without
92 /// cloning.
93 #[inline]
94 pub fn into_parts(self) -> (Backend, ffmpeg_next::Error) {
95 (self.backend, self.source)
96 }
97}
98
99/// Where in the decoder's life a [`AllBackendsFailed`] was raised.
100///
101/// The [`crate::FfmpegVideoStreamDecoder`] wrapper routes its software-fallback
102/// replay on **this explicit signal** rather than inferring origin from whether
103/// `unconsumed_packets` is empty. Both origins can carry an empty
104/// `unconsumed_packets` — a probe-era failure on the *first* packet (a
105/// side-data / byte / packet cap trip, or an `av_packet_ref` ENOMEM) has no
106/// prior history to surface, exactly like every post-commit failure — so
107/// emptiness cannot disambiguate them. Conflating the two made the wrapper
108/// treat a probe-era first-packet cap trip as post-commit: it would append a
109/// clone of the borrowed current packet to an empty replay set and skip the
110/// post-fallback `send_packet`, silently dropping that packet if the clone
111/// failed.
112#[derive(Debug, Clone, Copy, PartialEq, Eq)]
113pub enum FallbackOrigin {
114 /// Raised while the inner decoder's probe was still active (before the first
115 /// frame). `unconsumed_packets` is the probe's buffered history (possibly
116 /// empty when the failure landed on the very first packet). The wrapper
117 /// replays that history and then routes the still-unconsumed current packet
118 /// to the new software decoder itself.
119 Probe,
120 /// Raised after the probe collapsed (the committed backend failed at
121 /// runtime). `unconsumed_packets` is always empty — the probe buffer is gone
122 /// — so the wrapper does not replay: it opens a software decoder cold,
123 /// forwards only the failing call's current packet (or EOF), and resyncs at
124 /// the next keyframe, accepting a bounded, logged gap (degrade-and-continue).
125 PostCommit,
126}
127
128impl FallbackOrigin {
129 /// `true` for [`FallbackOrigin::PostCommit`].
130 #[inline]
131 pub const fn is_post_commit(self) -> bool {
132 matches!(self, FallbackOrigin::PostCommit)
133 }
134}
135
136/// Payload for [`Error::AllBackendsFailed`].
137///
138/// Auto-probe exhausted every backend in the platform's order. Empty
139/// `attempts` means the platform has no hardware backends listed in
140/// [`crate::Backend`] for the current `target_os` — callers must
141/// fall back to a software decoder of their choice.
142///
143/// `unconsumed_packets` holds the packets the decoder accepted from
144/// the caller before the probe exhausted (refcounted shallow clones
145/// of the packets fed via `send_packet`). For non-seekable inputs
146/// (live streams, pipes, network sources) the caller cannot
147/// re-demux from start, so this crate surfaces the buffered history
148/// here so the caller can feed those packets directly into a
149/// software decoder of their choice. When `AllBackendsFailed` comes
150/// from [`crate::VideoDecoder::open`] (no packets were ever sent),
151/// this vec is empty.
152///
153/// `origin` records whether the failure happened during the probe or after the
154/// committed backend collapsed at runtime — the explicit signal the wrapper
155/// routes on (see [`FallbackOrigin`]). It is never inferred from
156/// `unconsumed_packets.is_empty()`, which both origins can satisfy.
157///
158/// `Debug` is hand-written: [`ffmpeg_next::Packet`] does not derive
159/// `Debug`, so we print `[N packets]` instead of dumping per-packet
160/// bytes, which would be both noisy and useless for triage.
161#[derive(Clone, thiserror::Error)]
162#[error("all hardware backends failed; attempts: {attempts:?}")]
163pub struct AllBackendsFailed {
164 /// Per-backend errors collected during probing, in the order tried.
165 attempts: Vec<(Backend, Box<Error>)>,
166 /// Packets the decoder consumed from the caller before exhaustion.
167 /// Replay them through a software decoder for non-seekable inputs.
168 unconsumed_packets: Vec<Packet>,
169 /// Whether this was raised during the probe or post-commit. The wrapper's
170 /// fallback replay routes on this, never on `unconsumed_packets` emptiness.
171 origin: FallbackOrigin,
172}
173
174impl AllBackendsFailed {
175 /// Constructs a probe-era [`AllBackendsFailed`] payload — raised while the
176 /// inner decoder's probe is still active. `unconsumed_packets` is the probe's
177 /// buffered history (possibly empty if the failure landed on the first
178 /// packet). See [`FallbackOrigin::Probe`].
179 ///
180 /// Not `const fn`: the `Vec` arguments may carry destructors and
181 /// the const evaluator can't prove their drop safe for arbitrary
182 /// allocator state.
183 #[inline]
184 pub fn new(attempts: Vec<(Backend, Box<Error>)>, unconsumed_packets: Vec<Packet>) -> Self {
185 Self {
186 attempts,
187 unconsumed_packets,
188 origin: FallbackOrigin::Probe,
189 }
190 }
191 /// Constructs a post-commit [`AllBackendsFailed`] payload — raised after the
192 /// probe collapsed, when the committed backend failed at runtime.
193 /// `unconsumed_packets` is always empty (the probe buffer is gone); the
194 /// wrapper's retained GOP window supplies the replay set. See
195 /// [`FallbackOrigin::PostCommit`].
196 #[inline]
197 pub fn new_post_commit(attempts: Vec<(Backend, Box<Error>)>) -> Self {
198 Self {
199 attempts,
200 unconsumed_packets: Vec::new(),
201 origin: FallbackOrigin::PostCommit,
202 }
203 }
204 /// Per-backend errors collected during probing, in the order tried.
205 #[inline]
206 pub fn attempts(&self) -> &[(Backend, Box<Error>)] {
207 &self.attempts
208 }
209 /// Where this failure was raised — the explicit probe-vs-post-commit signal
210 /// the wrapper routes its fallback replay on.
211 #[inline]
212 pub const fn origin(&self) -> FallbackOrigin {
213 self.origin
214 }
215 /// Packets the decoder consumed from the caller before exhaustion.
216 /// Replay them through a software decoder for non-seekable inputs.
217 #[inline]
218 pub fn unconsumed_packets(&self) -> &[Packet] {
219 &self.unconsumed_packets
220 }
221 /// Consume the payload, returning the moved unconsumed packets so
222 /// non-seekable callers can replay them through a software decoder
223 /// without cloning.
224 #[inline]
225 pub fn into_unconsumed_packets(self) -> Vec<Packet> {
226 self.unconsumed_packets
227 }
228 /// Consume the payload, returning the moved attempts log and
229 /// unconsumed packets.
230 #[inline]
231 pub fn into_parts(self) -> (Vec<(Backend, Box<Error>)>, Vec<Packet>) {
232 (self.attempts, self.unconsumed_packets)
233 }
234}
235
236impl std::fmt::Debug for AllBackendsFailed {
237 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
238 f.debug_struct("AllBackendsFailed")
239 .field("attempts", &self.attempts)
240 // `Packet` is not `Debug`; print just the count so the error is
241 // still useful for triage without dumping per-packet bytes.
242 .field(
243 "unconsumed_packets",
244 &format_args!("[{} packets]", self.unconsumed_packets.len()),
245 )
246 .field("origin", &self.origin)
247 .finish()
248 }
249}
250
251/// Payload for [`Error::FallbackFailed`].
252///
253/// Surfaced by [`crate::FfmpegVideoStreamDecoder`] when a HW->SW
254/// fallback attempt itself fails — e.g. the SW decoder failed to
255/// open, EOF replay returned EAGAIN past the bounded retry, or the
256/// per-frame replay queue exceeded its cap. The HW decoder has
257/// already consumed `unconsumed_packets` from the caller; we
258/// surface them here so non-seekable inputs (pipes, live streams)
259/// can drive their own decoder of last resort.
260///
261/// `Debug` is hand-written for the same reason as
262/// [`AllBackendsFailed`]: [`ffmpeg_next::Packet`] does not derive
263/// `Debug`.
264#[derive(Clone, thiserror::Error)]
265#[error("HW->SW fallback failed: {source}")]
266pub struct FallbackFailed {
267 /// Underlying error that aborted the fallback transition.
268 source: Box<Error>,
269 /// Packets that the HW path had consumed but had not yet decoded
270 /// at fallback time. The caller can replay them through a
271 /// software decoder of their choice.
272 unconsumed_packets: Vec<Packet>,
273}
274
275impl FallbackFailed {
276 /// Constructs a new [`FallbackFailed`] payload.
277 ///
278 /// Not `const fn`: the `Vec` argument may carry destructors.
279 #[inline]
280 pub fn new(source: Box<Error>, unconsumed_packets: Vec<Packet>) -> Self {
281 Self {
282 source,
283 unconsumed_packets,
284 }
285 }
286 /// Underlying error that aborted the fallback transition.
287 #[inline]
288 pub fn source(&self) -> &Error {
289 &self.source
290 }
291 /// Packets that the HW path had consumed but had not yet decoded
292 /// at fallback time.
293 #[inline]
294 pub fn unconsumed_packets(&self) -> &[Packet] {
295 &self.unconsumed_packets
296 }
297 /// Consume the payload, returning the moved unconsumed packets so
298 /// non-seekable callers can replay them through a software decoder
299 /// without cloning.
300 #[inline]
301 pub fn into_unconsumed_packets(self) -> Vec<Packet> {
302 self.unconsumed_packets
303 }
304 /// Consume the payload, returning the moved source error and
305 /// unconsumed packets.
306 #[inline]
307 pub fn into_parts(self) -> (Box<Error>, Vec<Packet>) {
308 (self.source, self.unconsumed_packets)
309 }
310}
311
312impl std::fmt::Debug for FallbackFailed {
313 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
314 f.debug_struct("FallbackFailed")
315 .field("source", &self.source)
316 .field(
317 "unconsumed_packets",
318 &format_args!("[{} packets]", self.unconsumed_packets.len()),
319 )
320 .finish()
321 }
322}