mediadecode_ffmpeg/decoder/mod.rs
1use std::{collections::VecDeque, mem::ManuallyDrop, ptr};
2
3use ffmpeg_next::{
4 Codec, Packet, Rational,
5 codec::{
6 self,
7 Context,
8 // Bring the `Mut` / `Ref` traits into scope so `Packet::as_ptr` /
9 // `Packet::as_mut_ptr` resolve. They are aliased to avoid shadowing
10 // any future `Mut`/`Ref` types we might add — `cargo clippy` would
11 // otherwise flag them as "unused" without the alias and the import
12 // can mistakenly look unused. Confirmed in use by all `packet.as_ptr()`
13 // / `packet.as_mut_ptr()` call sites in this module.
14 packet::{Mut as PacketMut, Ref as PacketRef},
15 },
16 ffi::{
17 AVBufferRef, AVCodec, AVFrame, AVHWFramesContext, AVMediaType, av_buffer_ref, av_buffer_unref,
18 av_frame_move_ref, av_frame_unref, av_hwdevice_ctx_create, av_hwframe_transfer_data,
19 av_packet_ref, avcodec_alloc_context3, avcodec_free_context, avcodec_parameters_to_context,
20 },
21 frame,
22};
23
24/// Local FFI shims: FFmpeg entry points re-declared with `c_int` where
25/// the generated bindings use a closed Rust enum.
26///
27/// Constructing `AVCodecID` / `AVPixelFormat` / `AVSampleFormat` from a
28/// runtime integer that is not in this build's discriminant set is UB —
29/// and these are open C enums that FFmpeg extends in ABI-compatible
30/// releases. Declaring the same C symbol with `c_int` sidesteps the
31/// boundary entirely: both Rust declarations resolve to the same symbol
32/// at link time, and the integer never becomes an enum on the Rust
33/// side.
34///
35/// **This is the enum class inside this crate's own code.** The
36/// dependency-API sweep closed every place *ffmpeg-next* formed an enum
37/// out of FFmpeg memory; these three are places the crate's own new
38/// code did the same thing. The census that walks the pixel-format
39/// table to price the worst format is the sharpest instance: it exists
40/// precisely to be correct about formats this build does not name, and
41/// the binding it called returned those very ids as a closed
42/// `AVPixelFormat`. Every id would have become an invalid enum value on
43/// the way *into* the pricing that was supposed to handle it.
44pub(crate) mod c_shims {
45 use libc::c_int;
46
47 use super::AVCodec;
48
49 unsafe extern "C" {
50 /// `AVCodecID` as `c_int`.
51 pub fn avcodec_find_decoder(id: c_int) -> *const AVCodec;
52
53 /// `AVCodecID` as `c_int`, answering the descriptor libavcodec keeps
54 /// for that id — or null where this build names no codec for it.
55 ///
56 /// The pointer is into `codec_descriptors[]`, a `static const` table
57 /// compiled into libavcodec, so the strings it names are live and
58 /// unwritten for the process — the contract `crate::ffi`'s bounded
59 /// reader is given. It is left **raw** at every call site: the
60 /// struct embeds an `AVCodecID` and an `AVMediaType`, and forming a
61 /// reference to it would assert two bindgen enums valid on a table
62 /// that belongs to the linked library rather than to the bindings.
63 /// See `crate::CodecId::descriptor`.
64 ///
65 /// Preferred over `avcodec_get_name`, which never answers null: for
66 /// an id it cannot place it returns the string `"unknown_codec"`,
67 /// a sentinel wearing a name's clothes. A descriptor that is absent
68 /// is `None` here, and a consumer can tell the two apart.
69 pub fn avcodec_descriptor_get(id: c_int) -> *const ffmpeg_next::ffi::AVCodecDescriptor;
70
71 /// Returns `AVPixelFormat` as `c_int` — the id of a descriptor that
72 /// may well name a format this build's bindings do not.
73 pub fn av_pix_fmt_desc_get_id(desc: *const ffmpeg_next::ffi::AVPixFmtDescriptor) -> c_int;
74
75 /// Takes `AVPixelFormat` as `c_int`, so an id straight out of
76 /// [`av_pix_fmt_desc_get_id`] can be priced without ever being an
77 /// enum.
78 pub fn av_image_get_buffer_size(
79 pix_fmt: c_int,
80 width: c_int,
81 height: c_int,
82 align: c_int,
83 ) -> c_int;
84
85 /// Takes `AVSampleFormat` as `c_int`. Kept for the footprint
86 /// sweep, which walks the format table to decide which cells
87 /// exist; production pricing goes through
88 /// `av_samples_get_buffer_size`, the allocator's own ruler.
89 #[cfg(test)]
90 pub fn av_get_bytes_per_sample(sample_fmt: c_int) -> c_int;
91
92 /// The allocator's own audio ruler, with `AVSampleFormat` as
93 /// `c_int`. `align = 0` asks for the alignment
94 /// `av_frame_get_buffer` itself uses.
95 pub fn av_samples_get_buffer_size(
96 linesize: *mut c_int,
97 nb_channels: c_int,
98 nb_samples: c_int,
99 sample_fmt: c_int,
100 align: c_int,
101 ) -> c_int;
102
103 /// Writes an `AV_PIX_FMT_NONE`-terminated list of destination
104 /// formats a transfer may produce. Declared `*mut *mut c_int` so
105 /// the list is walked as integers — a driver may well offer a
106 /// format this build's bindings do not name.
107 pub fn av_hwframe_transfer_get_formats(
108 hwframe_ctx: *mut ffmpeg_next::ffi::AVBufferRef,
109 dir: c_int,
110 formats: *mut *mut c_int,
111 flags: c_int,
112 ) -> c_int;
113 }
114}
115
116use mediadecode::{Received, Sent, decoder::ScaledOutputCapability};
117
118use crate::{
119 backend::{self, Backend},
120 error::{AllBackendsFailed, Error, HwDeviceInitFailed, Result},
121 ffi::{CallbackState, codec_supports_hwaccel, get_hw_format},
122 frame::Frame,
123};
124
125/// Hardware-accelerated video decoder.
126///
127/// Hardware-only — there is no software fallback inside this crate. If
128/// every hardware backend in the platform's probe order fails to open,
129/// `open` returns [`Error::AllBackendsFailed`] and the caller is
130/// responsible for falling back to a software decoder of their choice
131/// (e.g. `ffmpeg::decoder::Video`).
132///
133/// Mirrors `ffmpeg::decoder::Video`'s `send_packet`/`receive_frame` interface.
134/// Decoded frames are returned through [`crate::Frame`], a CPU-side wrapper
135/// whose accessors avoid the `AVPixelFormat`-enum UB that an unvalidated read
136/// of FFmpeg's raw integer pixel formats can trigger.
137///
138/// `open` does a true probe: each backend opens with a strict `get_format`
139/// callback. On the first non-transient error from a backend the decoder is
140/// torn down and the next backend in probe order is tried, with all packets
141/// seen so far replayed through it. The advance is *transactional* — the
142/// candidate backend must successfully build and accept the replayed packets
143/// before any probe state is consumed, so a failing backend in the middle of
144/// the order does not strand the caller without history. Once the first frame
145/// is delivered the probe collapses and subsequent calls go straight to the
146/// active (committed) backend.
147///
148/// The committed backend can still fail at runtime — e.g. VideoToolbox can
149/// decode a clip's first frames and then hit content its kernel can't handle
150/// (H.264 High 4:2:2 10-bit), surfacing `AVERROR_EXTERNAL`. Post-commit a
151/// non-transient, non-EOF error from the committed backend is reclassified to
152/// [`Error::AllBackendsFailed`] (see the `is_hw_decode_failure` predicate), so
153/// the [`crate::FfmpegVideoStreamDecoder`] wrapper still recognises it as a
154/// HW-path exhaustion and falls back to software. The post-commit
155/// `unconsumed_packets` is empty (the probe buffer is gone); the wrapper's
156/// rolling since-last-keyframe buffer supplies the replay set.
157pub struct VideoDecoder {
158 /// Live FFmpeg state for the currently active backend.
159 state: DecoderState,
160 /// Reusable frame buffer used for hw-side decoding before transfer / move.
161 /// Internal use only — never handed to callers.
162 hw_frame: frame::Video,
163 /// Probe state: present until the first frame is received from the active
164 /// backend, then `None`. While `Some`, packets are buffered for replay and
165 /// non-transient errors / decoder failures advance to the next backend.
166 probe: Option<ProbeState>,
167 /// CPU-side frames produced by a candidate decoder during probe replay
168 /// (when its internal queue filled and we had to drain output before the
169 /// next `send_packet`). Already transferred from the candidate's
170 /// `AVHWFramesContext` to a CPU frame, so they remain valid after the
171 /// candidate state is committed. [`Self::receive_frame`] dequeues these
172 /// FIFO before reading from `state.inner`.
173 pending_frames: VecDeque<frame::Video>,
174 /// Per-decoder byte budget for [`Self::pending_frames`] during probe
175 /// replay. Defaults to [`DEFAULT_MAX_PROBE_PENDING_BYTES`]; override via
176 /// [`Self::with_max_probe_pending_bytes`].
177 max_probe_pending_bytes: usize,
178 /// Resource ceilings for the frames this decoder produces. Fixed at
179 /// open, because [`FrameLimits::max_pixels`] is written into every
180 /// `AVCodecContext` this decoder builds — including the ones a probe
181 /// advance builds later — and a context's ceiling cannot be moved
182 /// after `avcodec_open2`.
183 frame_limits: crate::limits::DecoderLimits,
184 /// The stream's packet timebase, written into every
185 /// `AVCodecContext` this decoder opens — including the ones a probe
186 /// advance opens later, which is why it is held rather than passed.
187 ///
188 /// `None` for a standalone decoder: [`Self::open`] and its siblings
189 /// are handed codec parameters and no stream, so there is no ruler
190 /// to declare. The stream decoder one tier up has one.
191 pkt_timebase: Option<mediadecode::Timebase>,
192 /// `true` once [`Self::send_eof`] has been accepted, until
193 /// [`Self::flush`].
194 ///
195 /// **It lives here rather than in [`ProbeState`], and that move is
196 /// the point.** It used to be a probe field, so it vanished the
197 /// moment the probe collapsed — a committed decoder could not tell
198 /// whether the caller had signalled the end, and therefore could not
199 /// answer the one question [`SessionPhase`] exists to answer without
200 /// guessing. The probe machinery still reads it for replay; it simply
201 /// no longer owns it.
202 eof_sent: bool,
203 /// The GPU-side scaled-output stage — see [`crate::vtscale`]. Carries
204 /// the caller's standing [`Self::request_scaled_output`] and, on the
205 /// VideoToolbox road, the cached pixel-transfer session and fitted
206 /// frames context the CPU download reads from.
207 ///
208 /// **It rides the decoder rather than the wrapper** because the seam
209 /// it inserts itself into is here: between the decoded hardware frame
210 /// and `av_hwframe_transfer_data`, the one point at which a picture is
211 /// still a GPU surface and can still be resized without a
212 /// full-resolution round-trip through main memory.
213 ///
214 /// Declared last, so it drops **after** the [`DecoderState`] holding
215 /// the VideoToolbox device its fitted frames context was built over —
216 /// and that order is safe by the FFmpeg contract rather than by luck:
217 /// `av_hwframe_ctx_alloc` "will make a new reference for internal
218 /// use", so the device outlives the decoder that opened it for exactly
219 /// as long as this stage still holds a context over it.
220 scaled_output: crate::vtscale::ScaledOutput,
221}
222
223/// Owned FFmpeg state for one open codec context. Has its own `Drop` so we
224/// can swap it out cleanly during a probe advance via `mem::replace`.
225struct DecoderState {
226 /// Wrapped FFmpeg decoder. `ManuallyDrop` so we can sequence its drop
227 /// before freeing the callback state.
228 inner: ManuallyDrop<ffmpeg_next::decoder::Video>,
229 /// Backend driving this state.
230 backend: Backend,
231 /// Owned reference produced by `av_hwdevice_ctx_create`.
232 hw_device_ref: *mut AVBufferRef,
233 /// Owned `Box<CallbackState>` raw pointer; `AVCodecContext::opaque`
234 /// aliases it.
235 callback_state: *mut CallbackState,
236}
237
238/// Maximum number of packets we are willing to buffer for probe replay
239/// before abandoning the fallback safety net. Set high enough to absorb
240/// long B-frame GOPs and codec setup latency, low enough to bound memory
241/// against malicious / pathological streams that never produce a first
242/// frame.
243const MAX_PROBE_PACKETS: usize = 256;
244
245/// Maximum total compressed-byte size of buffered probe packets. Each
246/// `Packet` clone holds a refcounted reference to the demuxer's bitstream
247/// data — even though the clone itself is shallow, the underlying buffers
248/// stay alive until we drop them. 64 MiB is generous for normal video and
249/// gives untrusted media a hard ceiling.
250const MAX_PROBE_PACKET_BYTES: usize = 64 * 1024 * 1024;
251
252/// Hard cap on the number of side-data entries we tolerate per buffered
253/// packet. `av_packet_ref` allocates an `AVPacketSideData` descriptor and
254/// an `AVBufferRef` per entry, so a packet stuffed with many tiny or
255/// zero-sized entries can consume significant memory in descriptor /
256/// allocator overhead even after [`packet_side_data_bytes`] charges
257/// [`SIDE_DATA_ENTRY_OVERHEAD`] bytes per entry. Refusing to clone such
258/// packets short-circuits the descriptor explosion path.
259///
260/// Sized for legitimate streams (typical video packets carry 0-5 side-
261/// data entries; SEI-heavy HEVC/AV1 maybe a dozen) while comfortably
262/// rejecting weaponised input.
263///
264/// Shared with the [`crate::FfmpegVideoStreamDecoder`] rolling GOP buffer,
265/// which charges the same side-data budget so its byte cap is a true upper
266/// bound on retained memory rather than counting bare payloads.
267pub(crate) const MAX_PROBE_PACKET_SIDE_DATA_ENTRIES: usize = 64;
268
269/// Conservative per-side-data-entry overhead estimate used by both
270/// [`packet_side_data_bytes`] and the budget accounting in
271/// [`VideoDecoder::send_packet`]. Counts the `AVPacketSideData`
272/// descriptor (24 bytes per the FFmpeg 9.x bindings), the `AVBufferRef`
273/// FFmpeg allocates per entry, and a margin for malloc bookkeeping
274/// (header bytes, alignment slack). Setting it on the high side keeps
275/// the byte cap a true upper bound on retained memory; under-charging
276/// would let many tiny entries slip past the cap.
277const SIDE_DATA_ENTRY_OVERHEAD: usize = 80;
278
279/// Conservative upper-bound bytes-per-pixel multiplier used to estimate
280/// the size of a CPU frame **before** `av_hwframe_transfer_data`
281/// allocates its pixel buffers. Covers every HW download format this
282/// crate produces (worst case is `P416LE` / `P412LE` at 6 bytes/pixel
283/// for 16-bit 4:4:4 semi-planar) plus a margin for FFmpeg's per-row
284/// stride alignment (typically 32-byte aligned, ~5% extra at HD widths
285/// and below).
286///
287/// Used by [`drain_into_pending`] as a pre-transfer guard: if the
288/// product `width * height * WORST_CASE_BYTES_PER_PIXEL` would already
289/// push `pending_bytes` past `max_probe_pending_bytes`, the candidate
290/// replay refuses the frame *before* allocating. Without this, FFmpeg
291/// would perform the full HW→CPU download (potentially ~100 MiB for
292/// 8K HDR) and we would only reject the frame after RSS had already
293/// spiked. The post-transfer accounting via [`cpu_frame_bytes`] stays in
294/// place as a backstop using the frame's actual stride/format.
295///
296/// Slightly over-charges true 4:2:0 NV12 / P010 frames (which dominate
297/// real workloads) — that's the right side to err on. Callers feeding
298/// 8K+ workloads through the probe path can tune
299/// [`VideoDecoder::with_max_probe_pending_bytes`] upward to compensate.
300const WORST_CASE_BYTES_PER_PIXEL: usize = 8;
301
302/// Maximum number of CPU frames we are willing to queue from a candidate
303/// during probe replay. Each frame is a fully-allocated CPU buffer
304/// (~3 MiB for 1080p NV12, ~24 MiB for 4K P010, ~96 MiB for 8K P010), so
305/// an unbounded queue would OOM on a candidate with a shallow internal
306/// queue against a deep replay history. This cap, together with
307/// [`DEFAULT_MAX_PROBE_PENDING_BYTES`], is enforced as a hard limit during
308/// replay: once either limit is reached, probe buffering fails for the
309/// candidate (returns `ENOMEM` from `drain_into_pending`) instead of
310/// queueing additional drained frames. The probe loop then advances to
311/// the next backend or returns `Error::AllBackendsFailed` if exhausted.
312const MAX_PROBE_PENDING_FRAMES: usize = 16;
313
314/// Default byte budget for probe-replay drained frames. 256 MiB is enough
315/// for 16 frames at 4K P010 (~24 MiB each = 384 MiB worst case under the
316/// count cap), and is the cap that fires first for very high-resolution
317/// content (8K P010: ~96 MiB per frame → only ~2 frames fit).
318///
319/// Override per-decoder with [`VideoDecoder::with_max_probe_pending_bytes`]
320/// when targeting 8K+ workloads or memory-constrained environments.
321///
322/// TODO: when frames significantly exceed typical sizes, consider
323/// memmap-backed pending buffers (write transferred frames to a temp file
324/// or shared-memory segment) so the resident set stays bounded even when
325/// the byte cap is raised. Out of scope for now.
326pub const DEFAULT_MAX_PROBE_PENDING_BYTES: usize = 256 * 1024 * 1024;
327
328/// Where a decoding session is in its life — **the one derived fact the
329/// classifiers read, and the only place the latches are interpreted.**
330///
331/// Every road that must decide what an errno *means* needs the same two
332/// questions answered, and answering them ad hoc at each road is what
333/// let them disagree. `EAGAIN` means "send me more" only where more can
334/// come; `AVERROR_EOF` means "the stream is over" only where a backend
335/// has committed to producing it. Read those wrong and a caller is
336/// handed a state with no satisfying operation, or a candidate that
337/// will never produce a frame is mistaken for a finished stream.
338///
339/// The two questions are exactly the two dimensions the machinery
340/// already keeps latches for — whether an end has been recorded, and
341/// whether a backend is still on trial — so this enum is a census of
342/// those latches rather than a new idea. Deriving it lives in
343/// [`VideoDecoder::phase`] and its siblings, one per session type;
344/// nothing else reads a latch to answer a classification question.
345#[derive(Copy, Clone, Debug, PartialEq, Eq)]
346pub(crate) enum SessionPhase {
347 /// A committed backend, and no end-of-stream recorded. Both flow
348 /// signals mean what they say.
349 Streaming,
350 /// A committed backend draining its tail after a recorded end. "Send
351 /// me more" is no longer satisfiable here — the caller has nothing
352 /// left to send and the send gates refuse — so it reads as the end.
353 Draining,
354 /// A candidate backend on trial, no end recorded. It may legitimately
355 /// want more input; what it may not do is quietly end the stream on
356 /// the caller's behalf, which is the committed backend's privilege.
357 Auditioning,
358 /// A candidate on trial that has already been handed the whole
359 /// history, **end-of-stream included**.
360 ///
361 /// The arm that had no name. A candidate here has been given
362 /// everything there is and answers about a stream that is already
363 /// over: if it has produced no frame, it never will. That is a
364 /// candidate failing — the probe's business — and neither "send me
365 /// more" (nothing left to send) nor "the stream ended" (this backend
366 /// never decoded a thing) is a true reading of it.
367 AuditioningPastEnd,
368}
369
370impl SessionPhase {
371 /// Whether more input can still reach this session.
372 ///
373 /// The satisfiability question: [`Received::NeedsInput`] and
374 /// [`Sent::MustDrain`] are both instructions, and both are honest
375 /// only where the caller can carry them out.
376 pub(crate) const fn accepts_input(self) -> bool {
377 matches!(self, Self::Streaming | Self::Auditioning)
378 }
379
380 /// Whether a backend has committed, and so may speak for the stream.
381 ///
382 /// Only a committed backend's `AVERROR_EOF` is the stream's end. A
383 /// candidate's is its own: it drained to nothing without ever proving
384 /// it could decode this content.
385 pub(crate) const fn is_committed(self) -> bool {
386 matches!(self, Self::Streaming | Self::Draining)
387 }
388}
389
390/// How a funnel verdict routes. See [`VideoDecoder::verdict_routing`].
391enum VerdictRouting {
392 /// The name says this backend cannot decode this content.
393 CandidateFailed,
394 /// The name says retrying a backend cannot help — report it as it is.
395 Direct,
396 /// Nothing was named; the road's own reading decides.
397 Unnamed,
398}
399
400/// What an **unnamed** verdict means on the road that produced it — the
401/// one thing the shared routing policy cannot know for itself.
402enum BareVerdict {
403 /// A flow signal's own fault: `avcodec_send_packet` answering
404 /// `AVERROR_EOF` to a submission past the end. The probe must not
405 /// advance on it — the candidate did nothing wrong, the caller did.
406 Reported,
407 /// A real failure. While a candidate is on trial, that is the
408 /// candidate failing.
409 CandidateFailure,
410}
411
412/// What the caller should do with a routed hardware failure.
413enum HwRoute {
414 /// Hand this to the caller.
415 Report(Error),
416 /// The active candidate failed: advance the probe and retry.
417 Advance(Error),
418}
419
420/// State carried only during the probe window (before the first successful
421/// frame). Holds enough information to tear down the current decoder and
422/// retry with the next backend.
423struct ProbeState {
424 parameters: codec::Parameters,
425 codec: Codec,
426 /// Backends still to try, in order. Empty means "no more options after
427 /// the active one fails" — `advance_probe` then surfaces
428 /// [`Error::AllBackendsFailed`] so the contract is the same on
429 /// single-backend platforms (e.g. macOS) as on multi-backend ones.
430 remaining_backends: Vec<Backend>,
431 /// Packets sent so far, kept for replay through any candidate backend.
432 /// Preserved across failed candidates — only cleared when the probe
433 /// collapses on a successful first frame, or when the probe is
434 /// abandoned due to the size caps.
435 buffered_packets: Vec<Packet>,
436 /// Cumulative size (in compressed bytes) of `buffered_packets`. Tracked
437 /// incrementally so we don't have to re-sum on every send.
438 buffered_bytes: usize,
439 /// Whether `send_eof` has been called; replayed alongside packets.
440 /// Per-backend errors captured since the probe window opened. Pushed
441 /// whenever a backend's failure triggers `advance_probe` (the active
442 /// backend that just failed) or a candidate's build / replay rejects
443 /// it. Drained into [`Error::AllBackendsFailed`] when the probe
444 /// exhausts every option.
445 attempts: Vec<(Backend, Box<Error>)>,
446}
447
448// SAFETY: All raw pointers are exclusively owned by `DecoderState` and never
449// shared. `ffmpeg::decoder::Video` is itself `Send` (its `Context` carries an
450// `unsafe impl Send`). The decoder is not safe for concurrent use, hence not
451// `Sync`.
452//
453// `VideoDecoder`'s claim additionally covers what `crate::vtscale` holds on
454// Apple targets: a `VTPixelTransferSessionRef` and an `AVBufferRef` to a
455// fitted `AVHWFramesContext`. Both are exclusively owned by the stage — no
456// `Clone`, no handing out — and every use of the session goes through
457// `&mut ScaledOutput`, so the one-transfer-at-a-time protocol
458// `VTPixelTransferSessionTransferImage` expects is serialized by the borrow
459// rather than by a lock. VideoToolbox sessions are Core Foundation objects
460// with no thread affinity (unlike the UI-framework types that have one), so
461// moving one between threads is exactly as sound as moving the codec context
462// beside it. Still not `Sync`, for the same reason nothing else here is.
463unsafe impl Send for DecoderState {}
464unsafe impl Send for VideoDecoder {}
465
466impl Drop for DecoderState {
467 fn drop(&mut self) {
468 // Order matters:
469 // 1. Drop the codec context first. While it lives, FFmpeg may invoke
470 // `get_format`, which dereferences `callback_state` via `opaque`.
471 // 2. Free the callback state heap allocation.
472 // 3. Release our hw device reference (FFmpeg released its own when
473 // the codec context was freed in step 1).
474 unsafe {
475 ManuallyDrop::drop(&mut self.inner);
476 if !self.callback_state.is_null() {
477 drop(Box::from_raw(self.callback_state));
478 self.callback_state = ptr::null_mut();
479 }
480 if !self.hw_device_ref.is_null() {
481 av_buffer_unref(&mut self.hw_device_ref);
482 }
483 }
484 }
485}
486
487impl VideoDecoder {
488 /// Auto-probe hardware backends in the platform's default order.
489 ///
490 /// Each backend opens with a strict `get_format` callback. The first
491 /// backend whose `avcodec_open2` succeeds becomes active; if its first
492 /// frame is unusable (decode error, transfer failure, or a CPU-format
493 /// frame from a HW context) the decoder is torn down and the next backend
494 /// is tried — packets sent so far are replayed through the new decoder
495 /// transparently. The probe advance is transactional: the next backend
496 /// must build *and* accept the replayed history before any probe state is
497 /// consumed, so a misbehaving middle backend cannot strand the caller.
498 ///
499 /// [`Self::backend`] reflects whichever backend ultimately produced the
500 /// first frame.
501 ///
502 /// [`Error::AllBackendsFailed`] surfaces in two places, with the same
503 /// meaning ("no hardware backend can decode this stream — fall back to
504 /// software yourself"):
505 /// - From `open` itself, when no backend even opens.
506 /// - From [`Self::send_packet`] / [`Self::send_eof`] /
507 /// [`Self::receive_frame`], when the initially-opened backend fails
508 /// at decode time and every remaining backend in the probe order
509 /// either also fails or doesn't exist. On single-backend platforms
510 /// (e.g. macOS, where the order is `[VideoToolbox]`), this is the
511 /// only place a HW-only failure surfaces.
512 ///
513 /// In both cases, `attempts` carries the per-backend error log. When
514 /// the runtime path fires, `unconsumed_packets` also contains the
515 /// packets the decoder consumed from the caller before the probe
516 /// exhausted (refcounted shallow clones); for non-seekable inputs
517 /// (live streams, pipes) the caller can replay these directly into
518 /// a software decoder of their choice without re-demuxing. From the
519 /// open-time path the vec is empty since no packets have been sent.
520 ///
521 /// On `Ok`, the returned decoder **always** has an active probe
522 /// rescue safety net. If a parameters clone fails under memory
523 /// pressure before the probe state can be set up, `open` returns
524 /// `Err(Error::Ffmpeg(Other { errno: ENOMEM }))` rather than handing
525 /// back a live decoder with no fallback contract. No packets have
526 /// been sent yet, so the caller can retry or fall back to software
527 /// with the original `parameters` directly.
528 pub fn open(parameters: codec::Parameters) -> Result<Self> {
529 Self::open_with_frame_limits(parameters, crate::limits::DecoderLimits::default())
530 }
531
532 /// [`Self::open`], with the stream's packet timebase declared.
533 ///
534 /// **Prefer this one whenever the caller knows the timebase**, which
535 /// is nearly always: the packets fed to a decoder carry timestamps in
536 /// their stream's units, and `AVCodecContext.pkt_timebase` is how
537 /// libavcodec is told what those units are. Not owning the
538 /// `AVStream` does not mean not knowing its ruler.
539 ///
540 /// The value reaches every `AVCodecContext` this decoder opens,
541 /// including the ones a later hardware probe advance opens.
542 pub fn open_timed(
543 parameters: codec::Parameters,
544 timebase: mediadecode::Timebase,
545 ) -> Result<Self> {
546 Self::open_with_frame_limits_timed(
547 parameters,
548 crate::limits::DecoderLimits::default(),
549 timebase,
550 )
551 }
552
553 /// [`Self::open`], with the frame ceilings named.
554 ///
555 /// Taken at open for the reason [`Self::open_with_limits`] gives:
556 /// [`FrameLimits::max_pixels`] is written into every `AVCodecContext`
557 /// this decoder opens — including the ones a later probe advance
558 /// opens — and a context's ceiling cannot be moved after
559 /// `avcodec_open2`.
560 pub fn open_with_frame_limits(
561 parameters: codec::Parameters,
562 limits: crate::limits::DecoderLimits,
563 ) -> Result<Self> {
564 // **Explicitly untimed.** No packet timebase is declared, so
565 // `AVCodecContext.pkt_timebase` is left at libavcodec's own
566 // default and anything it derives from that field — a subtitle
567 // cue's PTS, a packet-duration fallback — is unavailable. Reach
568 // for [`Self::open_with_frame_limits_timed`] when the stream's
569 // ruler is known, which is nearly always.
570 Self::open_with_frame_limits_timed_in(parameters, limits, None)
571 }
572
573 /// [`Self::open_with_frame_limits`], with the stream's packet
574 /// timebase declared — see [`Self::open_timed`].
575 pub fn open_with_frame_limits_timed(
576 parameters: codec::Parameters,
577 limits: crate::limits::DecoderLimits,
578 timebase: mediadecode::Timebase,
579 ) -> Result<Self> {
580 Self::open_with_frame_limits_timed_in(parameters, limits, Some(timebase))
581 }
582
583 fn open_with_frame_limits_timed_in(
584 parameters: codec::Parameters,
585 limits: crate::limits::DecoderLimits,
586 pkt_timebase: Option<mediadecode::Timebase>,
587 ) -> Result<Self> {
588 let codec = find_decoder(¶meters)?;
589 let order = backend::probe_order();
590
591 let mut attempts: Vec<(Backend, Box<Error>)> = Vec::new();
592 for (i, &backend) in order.iter().enumerate() {
593 // Use the checked clone — ffmpeg-next's `Parameters::clone` does
594 // `avcodec_parameters_alloc` without a null check and ignores the
595 // return of `avcodec_parameters_copy`. Under OOM that path silently
596 // produces a Parameters with a null inner pointer.
597 let cloned_for_build =
598 match try_clone_parameters(¶meters, limits.max_codec_parameter_bytes()) {
599 Ok(p) => p,
600 Err(e) => {
601 tracing::warn!(?backend, error = %e, "hwdecode: parameters clone failed");
602 attempts.push((backend, Box::new(e)));
603 continue;
604 }
605 };
606 match Self::build_state(cloned_for_build, codec, backend, limits, pkt_timebase) {
607 Ok(state) => {
608 tracing::info!(?backend, "hwdecode: opened video decoder (probing)");
609 let remaining = order[(i + 1)..].to_vec();
610 // Deep-copy the caller's `parameters` before storing in ProbeState.
611 // `codec::Parameters` from `stream.parameters()` carries an Rc
612 // owner pointing at the demuxer; moving that Rc to a worker
613 // thread (when VideoDecoder is sent) would race with the demuxer's
614 // Rc on the original thread. The checked clone copies the bytes
615 // into a fresh allocation with `owner: None`, severing the link.
616 //
617 // We always create ProbeState — even when `remaining` is empty
618 // (single-backend platforms like macOS) — so that a first-frame
619 // failure on the only backend surfaces as
620 // `Error::AllBackendsFailed` from `receive_frame` /
621 // `send_packet` rather than as a raw FFmpeg error. That keeps
622 // the API contract the same regardless of how many HW backends
623 // the platform exposes.
624 //
625 // If the clone fails (ENOMEM), fail the **whole open call**
626 // rather than returning a live decoder with `probe: None`.
627 // Returning Ok here would let the caller send packets that the
628 // active backend consumes, and a subsequent backend failure
629 // would then surface as a raw FFmpeg error with no
630 // `unconsumed_packets` — silently breaking the rescue contract
631 // for non-seekable inputs (live streams, pipes). Dropping the
632 // already-built `state` here runs its FFmpeg cleanup, and the
633 // caller can retry / fall back to software with the original
634 // parameters in their hand (no packets were consumed yet).
635 // Seed the probe's attempt log with any backends that failed
636 // to open earlier in this loop (including
637 // `BackendUnsupportedByCodec` and parameters-clone errors).
638 // Without this, a runtime exhaustion on the active backend
639 // would surface an `AllBackendsFailed` containing only the
640 // active backend's runtime failure — losing the original
641 // open-time causes that, on multi-backend platforms (Linux,
642 // Windows), are usually the more diagnostic signal. E.g. a
643 // VAAPI-then-CUDA host where VAAPI fails to open and CUDA
644 // later fails at first-frame must report both failures in
645 // probe order, not just CUDA.
646 let probe = match try_clone_parameters(¶meters, limits.max_codec_parameter_bytes()) {
647 Ok(probe_params) => ProbeState {
648 parameters: probe_params,
649 codec,
650 remaining_backends: remaining,
651 buffered_packets: Vec::new(),
652 buffered_bytes: 0,
653 attempts: std::mem::take(&mut attempts),
654 },
655 Err(e) => {
656 tracing::warn!(
657 error = %e,
658 "hwdecode: parameters clone failed for probe state at open; \
659 failing closed instead of returning a decoder without rescue"
660 );
661 return Err(e);
662 }
663 };
664 return Ok(Self {
665 state,
666 hw_frame: alloc_av_frame().map_err(Error::Ffmpeg)?,
667 probe: Some(probe),
668 pending_frames: VecDeque::new(),
669 max_probe_pending_bytes: DEFAULT_MAX_PROBE_PENDING_BYTES,
670 frame_limits: limits,
671 pkt_timebase,
672 eof_sent: false,
673 scaled_output: crate::vtscale::ScaledOutput::new(),
674 });
675 }
676 Err(e) => {
677 tracing::warn!(?backend, error = %e, "hwdecode: backend open failed");
678 attempts.push((backend, Box::new(e)));
679 }
680 }
681 }
682 // No packets have been consumed at open time.
683 Err(Error::AllBackendsFailed(AllBackendsFailed::new(
684 attempts,
685 Vec::new(),
686 )))
687 }
688
689 /// Open the decoder with a specific backend. No probe, no fallback.
690 ///
691 /// If `backend` cannot actually decode this stream, the failure surfaces
692 /// from [`Self::receive_frame`] (the strict `get_format` callback returns
693 /// `AV_PIX_FMT_NONE`, the decoder errors out). The caller is responsible
694 /// for retrying with another hardware backend or falling back to a
695 /// software decoder of their choice (e.g. `ffmpeg::decoder::Video`).
696 pub fn open_with(parameters: codec::Parameters, backend: Backend) -> Result<Self> {
697 Self::open_with_limits(parameters, backend, crate::limits::DecoderLimits::default())
698 }
699
700 /// [`Self::open_with`], with the stream's packet timebase declared —
701 /// see [`Self::open_timed`].
702 pub fn open_with_timed(
703 parameters: codec::Parameters,
704 backend: Backend,
705 timebase: mediadecode::Timebase,
706 ) -> Result<Self> {
707 Self::open_with_limits_timed(
708 parameters,
709 backend,
710 crate::limits::DecoderLimits::default(),
711 timebase,
712 )
713 }
714
715 /// [`Self::open_with`], with the frame ceilings named.
716 ///
717 /// The limits are taken **at open**, not through a `with_*` builder,
718 /// because [`FrameLimits::max_pixels`] is written straight into the
719 /// `AVCodecContext` this call opens — that is the layer that makes
720 /// libavcodec refuse an oversized picture before allocating it, and a
721 /// context's ceiling cannot be moved after `avcodec_open2`. A builder
722 /// would have silently applied to only half the enforcement.
723 pub fn open_with_limits(
724 parameters: codec::Parameters,
725 backend: Backend,
726 limits: crate::limits::DecoderLimits,
727 ) -> Result<Self> {
728 // Explicitly untimed — see [`Self::open_with_frame_limits`].
729 Self::open_with_limits_timed_in(parameters, backend, limits, None)
730 }
731
732 /// [`Self::open_with_limits`], with the stream's packet timebase
733 /// declared — see [`Self::open_timed`].
734 pub fn open_with_limits_timed(
735 parameters: codec::Parameters,
736 backend: Backend,
737 limits: crate::limits::DecoderLimits,
738 timebase: mediadecode::Timebase,
739 ) -> Result<Self> {
740 Self::open_with_limits_timed_in(parameters, backend, limits, Some(timebase))
741 }
742
743 fn open_with_limits_timed_in(
744 parameters: codec::Parameters,
745 backend: Backend,
746 limits: crate::limits::DecoderLimits,
747 pkt_timebase: Option<mediadecode::Timebase>,
748 ) -> Result<Self> {
749 let codec = find_decoder(¶meters)?;
750 let state = Self::build_state(parameters, codec, backend, limits, pkt_timebase)?;
751 Ok(Self {
752 state,
753 hw_frame: alloc_av_frame().map_err(Error::Ffmpeg)?,
754 probe: None,
755 pending_frames: VecDeque::new(),
756 max_probe_pending_bytes: DEFAULT_MAX_PROBE_PENDING_BYTES,
757 frame_limits: limits,
758 pkt_timebase,
759 eof_sent: false,
760 scaled_output: crate::vtscale::ScaledOutput::new(),
761 })
762 }
763
764 /// Builds a decoder around a **software** `ffmpeg::decoder::Video`,
765 /// for tests that need [`VideoDecoder`]'s own send/receive arms driven
766 /// against real libavcodec.
767 ///
768 /// **Why this exists.** Those arms classify libavcodec's flow control
769 /// themselves, and the only other way to reach them is
770 /// [`VideoDecoder::open`], which needs a working hardware backend and
771 /// a sample file — so every existing lane through them is
772 /// `#[ignore]`-gated and runs nowhere. A regression that never runs is
773 /// a claim, not a check. This keeps the arms, the probe state and the
774 /// funnels exactly as production builds them and swaps only the
775 /// backend behind `state.inner`, which is the one thing a test cannot
776 /// otherwise supply.
777 ///
778 /// `auditioning` opens the probe window with **no backends left to
779 /// try**, which is what makes the candidate-failure road observable:
780 /// `advance_probe` has nowhere to advance to, so it surfaces
781 /// [`Error::AllBackendsFailed`] and a lane can tell "the probe road
782 /// was taken" from "a status was answered". Passing `false` leaves the
783 /// probe collapsed, for lanes that mean to exercise a committed
784 /// backend.
785 ///
786 /// The `backend` label is cosmetic here — it is read only for the
787 /// attempt log and [`Self::backend`], neither of which a software
788 /// decoder reaches — and `hw_device_ref` is null, which
789 /// [`DecoderState`]'s `Drop` already handles.
790 #[cfg(test)]
791 pub(crate) fn from_software_for_test(
792 parameters: codec::Parameters,
793 limits: crate::limits::DecoderLimits,
794 auditioning: bool,
795 ) -> Result<Self> {
796 let codec = find_decoder(¶meters)?;
797 let (ctx, callback_state) = build_codec_context(¶meters, limits, None)?;
798 let opened = ctx.decoder().open_as(codec).map_err(Error::Ffmpeg)?;
799 ensure_video_codec_type(&opened)?;
800 let state = DecoderState {
801 inner: ManuallyDrop::new(ffmpeg_next::decoder::Video(opened)),
802 backend: backend::probe_order()
803 .first()
804 .copied()
805 .unwrap_or(Backend::VideoToolbox),
806 hw_device_ref: ptr::null_mut(),
807 callback_state: Box::into_raw(callback_state),
808 };
809 let probe = auditioning.then(|| ProbeState {
810 parameters: try_clone_parameters(¶meters, limits.max_codec_parameter_bytes())
811 .expect("a clonable parameter set"),
812 codec,
813 remaining_backends: Vec::new(),
814 buffered_packets: Vec::new(),
815 buffered_bytes: 0,
816 attempts: Vec::new(),
817 });
818 Ok(Self {
819 state,
820 hw_frame: alloc_av_frame().map_err(Error::Ffmpeg)?,
821 probe,
822 pending_frames: VecDeque::new(),
823 max_probe_pending_bytes: DEFAULT_MAX_PROBE_PENDING_BYTES,
824 frame_limits: limits,
825 pkt_timebase: None,
826 eof_sent: false,
827 scaled_output: crate::vtscale::ScaledOutput::new(),
828 })
829 }
830
831 /// Override the byte budget for probe-replay queued frames. Defaults to
832 /// [`DEFAULT_MAX_PROBE_PENDING_BYTES`]. Use a higher value when targeting
833 /// 8K+ workloads where 16 frames at full size could exceed the default;
834 /// use a lower value in memory-constrained services to bound peak
835 /// allocation more tightly.
836 ///
837 /// Setting after the first frame has been delivered is harmless but has
838 /// no observable effect — the probe has already collapsed and the cap
839 /// only applies during replay drain.
840 ///
841 /// Returns `self` for builder-style chaining:
842 /// ```ignore
843 /// let decoder = VideoDecoder::open(params)?
844 /// .with_max_probe_pending_bytes(1024 * 1024 * 1024); // 1 GiB
845 /// ```
846 #[must_use]
847 pub fn with_max_probe_pending_bytes(mut self, bytes: usize) -> Self {
848 self.max_probe_pending_bytes = bytes;
849 self
850 }
851
852 /// The backend currently producing frames. While the probe is still in
853 /// progress (no frame received yet) this returns the optimistically
854 /// selected backend; after the first frame, it is the backend that
855 /// actually produced it. Once stable, never changes again.
856 pub fn backend(&self) -> Backend {
857 self.state.backend
858 }
859
860 /// Whether this decoder can emit pictures at a caller-requested size
861 /// instead of full coded size.
862 ///
863 /// [`ScaledOutputCapability::Supported`] on exactly one road: the
864 /// VideoToolbox backend on an Apple target, where
865 /// [`crate::vtscale`]'s `VTPixelTransferSession` sits between the
866 /// decoded hardware frame and the CPU download. Every other backend
867 /// this crate wires — [`Backend::Vaapi`], [`Backend::Cuda`],
868 /// [`Backend::D3d11va`] — answers `Unsupported`, each with its own
869 /// filed native scaling seam.
870 ///
871 /// # And it stops saying `Supported` the moment the promise breaks
872 ///
873 /// The trait's contract is that a `Supported` answer lets a caller
874 /// skip its own resampler, so a session that quietly went back to
875 /// full-size pictures under that answer would hand the caller mixed
876 /// extents with nothing to notice them by. The stage can stand down
877 /// per frame — a padded pixel buffer, a crop rectangle, side data a
878 /// resize would strand, a resolution change that turns the standing
879 /// request into an upscale, a transfer that fails — and the **first**
880 /// frame that goes out at anything other than the requested extent
881 /// flips this answer to [`ScaledOutputCapability::Unsupported`]. That
882 /// is the explicit transition a caller is told to look for: query
883 /// again and learn that resampling is theirs once more. A fresh
884 /// [`Self::request_scaled_output`] buys a fresh promise.
885 ///
886 /// A request the stream already satisfies is not a broken promise:
887 /// the picture arrives at exactly the size asked for.
888 ///
889 /// A pure query: it neither requests anything nor changes what
890 /// [`Self::receive_frame`] delivers.
891 pub fn scaled_output_capability(&self) -> ScaledOutputCapability {
892 if crate::vtscale::ScaledOutput::supported()
893 && self.state.backend.is_video_toolbox()
894 && self.scaled_output.promise_stands()
895 {
896 ScaledOutputCapability::Supported
897 } else {
898 ScaledOutputCapability::Unsupported
899 }
900 }
901
902 /// Asks this decoder to emit pictures at `size` from the next frame
903 /// on, and reports whether the request was recorded.
904 ///
905 /// # What "from the next frame on" means, exactly
906 ///
907 /// The stage is consulted per frame, on the way out of the decoder
908 /// and before the GPU→CPU download. So a request placed mid-stream
909 /// takes effect on the next picture `receive_frame` produces — never
910 /// retroactively on one already decoded and queued, and never later
911 /// than that.
912 ///
913 /// # Refusal is not an error
914 ///
915 /// [`ScaledOutputCapability::Unsupported`] comes back when this road
916 /// has no stage at all (see [`Self::scaled_output_capability`]), when
917 /// either requested extent is zero, or when the request is an
918 /// **upscale** of the stream's coded size — the stage exists to move
919 /// fewer bytes across the GPU→CPU bus, and enlarging a picture moves
920 /// more. Nothing about [`Self::send_packet`] or
921 /// [`Self::receive_frame`] can fail because of it.
922 ///
923 /// **A refusal returns the session to full coded size**, dropping any
924 /// request already standing. That is what the trait says this answer
925 /// means, and the only reading a caller can act on: told
926 /// `Unsupported` it resamples for itself, and a session that went on
927 /// quietly fitting to an older request would have it resample an
928 /// already-fitted picture. Like an acceptance, it takes effect from
929 /// the next picture; one already decoded keeps the extent it was
930 /// decoded at.
931 ///
932 /// See [`ScaledOutputCapability`] for the determinism trade a caller
933 /// takes on by acting on a `Supported` answer.
934 pub fn request_scaled_output(&mut self, size: (u32, u32)) -> ScaledOutputCapability {
935 // **The road, not the standing promise.** A session whose promise
936 // broke on an earlier request answers `Unsupported` from
937 // [`Self::scaled_output_capability`] until somebody asks again —
938 // and asking again is exactly this, so gating it on that answer
939 // would make the break permanent and unrecoverable. What is checked
940 // here is what cannot change: whether this build and this backend
941 // have a stage at all.
942 if !crate::vtscale::ScaledOutput::supported() || !self.state.backend.is_video_toolbox() {
943 return ScaledOutputCapability::Unsupported;
944 }
945 let coded = (self.width(), self.height());
946 self.scaled_output.request(size, coded)
947 }
948
949 /// Withdraws any standing scaled-output request, returning this
950 /// decoder to full coded size from the next frame.
951 ///
952 /// The refusal road that does not go through
953 /// [`Self::request_scaled_output`]: the wrapper refuses a request
954 /// placed while a decoded picture is parked, and a refusal has to
955 /// mean the same thing there as everywhere else — the session is
956 /// returning to full coded size. Without this the old request would
957 /// stay armed behind an `Unsupported` answer, and a caller acting on
958 /// that answer would resample pictures that were already fitted.
959 pub fn cancel_scaled_output(&mut self) {
960 self.scaled_output.cancel();
961 }
962
963 /// Decoder width in pixels.
964 pub fn width(&self) -> u32 {
965 self.state.inner.width()
966 }
967
968 /// Decoder height in pixels.
969 pub fn height(&self) -> u32 {
970 self.state.inner.height()
971 }
972
973 /// Codec context time base.
974 pub fn time_base(&self) -> Rational {
975 self.state.inner.time_base()
976 }
977
978 /// Frame rate from the codec context, if known.
979 pub fn frame_rate(&self) -> Option<Rational> {
980 self.state.inner.frame_rate()
981 }
982
983 /// Reclassify a post-commit runtime error from the committed HW backend
984 /// into [`Error::AllBackendsFailed`] so the [`crate::FfmpegVideoStreamDecoder`]
985 /// wrapper recognises it as a HW-path exhaustion and falls back to
986 /// software. The single attempt records the committed backend
987 /// (`self.state.backend` is the live backend post-commit) paired with the
988 /// underlying FFmpeg error. `unconsumed_packets` is empty: the probe
989 /// buffer is gone after commit, so the wrapper's rolling
990 /// since-last-keyframe buffer supplies the replay set.
991 ///
992 /// # `reason` is the funnel's verdict, and this does not mint another
993 ///
994 /// It used to call [`Self::hw_exit`] itself, which was right while it
995 /// was the *first* funnel on its road and wrong the moment it was the
996 /// second. On the receive road the verdict is minted at the top of the
997 /// arm, and `hw_exit` **consumes** the latch it reads — so a second
998 /// call finds nothing and records the raw errno libavcodec reported,
999 /// throwing away the refusal that had already been collected. A
1000 /// caller's attempt log then blamed `InvalidData` for a coded surface
1001 /// this crate declined over a configured ceiling.
1002 ///
1003 /// So the verdict is minted once, by whichever funnel is first on the
1004 /// road, and threaded from there. See the doors' invariant on
1005 /// [`software_receive`].
1006 /// Mints a verdict for a road that holds none yet, then routes it.
1007 ///
1008 /// The funnel runs **exactly once** here, which is the law the doors
1009 /// carry: see the invariant on [`software_receive`]. Roads that have
1010 /// already minted (the receive arm) call [`Self::hw_route`] straight.
1011 fn hw_failure(&self, e: ffmpeg_next::Error, bare: BareVerdict) -> HwRoute {
1012 self.hw_route(self.hw_exit(Error::Ffmpeg(e)), e, bare)
1013 }
1014
1015 /// How a funnel verdict routes, before the road's own reading of an
1016 /// unnamed one applies.
1017 ///
1018 /// **Exhaustive on purpose, and with no wildcard.** A `_ => false`
1019 /// stood here and was a hazard rather than a convenience: a future
1020 /// named verdict that *did* require a fallback would inherit the
1021 /// silence and be reported plain, which is a bug that compiles. The
1022 /// match is total over this crate's own error vocabulary, so adding an
1023 /// arm forces whoever adds it to say how it routes.
1024 fn verdict_routing(reason: &Error) -> VerdictRouting {
1025 match reason {
1026 // The hardware pool declined the coded surface. Software is not
1027 // subject to that ceiling, and neither is the next backend.
1028 Error::HwSurfaceTooLarge(_) => VerdictRouting::CandidateFailed,
1029 // Software would decode the same oversized frame and be refused
1030 // by the same ceiling; so would the next backend. A fallback here
1031 // invites an action that cannot succeed.
1032 Error::FrameBudgetExceeded(_) => VerdictRouting::Direct,
1033 // The funnel handed its fallback straight back: nothing was named,
1034 // so the errno is all there is and the road decides.
1035 Error::Ffmpeg(_) => VerdictRouting::Unnamed,
1036 // None of these can leave a funnel — `hw_exit` mints only the two
1037 // refusals above or returns its argument — and each is already a
1038 // decided fact that did not ask for a backend to be retried. They
1039 // are listed rather than swept up so a twelfth arm cannot join
1040 // them silently.
1041 Error::PacketBuild(_)
1042 | Error::ParametersTooLarge(_)
1043 // A malformed channel layout is a fact about the *stream*, not
1044 // about a backend: every backend would be handed the same
1045 // parameters and refuse them the same way, and the refusal
1046 // happens before any backend is chosen at all.
1047 | Error::MalformedChannelLayout(_)
1048 | Error::ChannelMapMissing(_)
1049 | Error::NoCodec(_)
1050 | Error::HwTransferTooLarge(_)
1051 | Error::BackendUnsupportedByCodec(_)
1052 | Error::HwDeviceInitFailed(_)
1053 | Error::AllBackendsFailed(_)
1054 | Error::FallbackFailed(_) => VerdictRouting::Direct,
1055 }
1056 }
1057
1058 /// Whether a post-commit failure means the hardware backend cannot
1059 /// decode this content, so the wrapper must open a software decoder.
1060 ///
1061 /// A named verdict outranks the raw errno in **both** directions: a
1062 /// name that says no is as binding as one that says yes, and the errno
1063 /// is consulted only where nothing was named. See
1064 /// [`Self::verdict_routing`].
1065 fn fallback_required(reason: &Error, raw: ffmpeg_next::Error) -> bool {
1066 match Self::verdict_routing(reason) {
1067 VerdictRouting::CandidateFailed => true,
1068 VerdictRouting::Direct => false,
1069 VerdictRouting::Unnamed => is_hw_decode_failure(&raw),
1070 }
1071 }
1072
1073 /// **One policy for what a funnelled hardware failure means, shared by
1074 /// every road that can produce one.**
1075 ///
1076 /// The send roads used to return their funnel's result the moment they
1077 /// had it. That was right for a flow signal and wrong for anything
1078 /// else: `hw_send` can mint [`Error::HwSurfaceTooLarge`], and returning
1079 /// it plain meant the wrapper — which opens software only on
1080 /// [`Error::AllBackendsFailed`] — simply stopped, and a probe still
1081 /// auditioning never advanced past the candidate that had just
1082 /// declined the surface.
1083 ///
1084 /// So minting and routing are one move now, and the receive road's
1085 /// policy is the policy. What differs between roads is only what an
1086 /// *unnamed* verdict means, which is why [`BareVerdict`] is a
1087 /// parameter rather than an assumption.
1088 fn hw_route(&self, reason: Error, raw: ffmpeg_next::Error, bare: BareVerdict) -> HwRoute {
1089 let candidate_failed = match Self::verdict_routing(&reason) {
1090 VerdictRouting::CandidateFailed => true,
1091 VerdictRouting::Direct => false,
1092 VerdictRouting::Unnamed => matches!(bare, BareVerdict::CandidateFailure),
1093 };
1094 if !candidate_failed {
1095 return HwRoute::Report(reason);
1096 }
1097 if self.probe.is_some() {
1098 return HwRoute::Advance(reason);
1099 }
1100 if Self::fallback_required(&reason, raw) {
1101 return HwRoute::Report(self.post_commit_hw_failure(reason));
1102 }
1103 HwRoute::Report(reason)
1104 }
1105
1106 fn post_commit_hw_failure(&self, reason: Error) -> Error {
1107 // `new_post_commit` stamps `FallbackOrigin::PostCommit`: the wrapper
1108 // routes its replay on that explicit signal, not on the (here-empty)
1109 // `unconsumed_packets`, which a probe-era first-packet cap trip also
1110 // leaves empty.
1111 Error::AllBackendsFailed(AllBackendsFailed::new_post_commit(vec![(
1112 self.state.backend,
1113 Box::new(reason),
1114 )]))
1115 }
1116
1117 /// Whether the probe rescue history is still being recorded.
1118 ///
1119 /// While this is true, [`Self::send_packet`] `av_packet_ref`s every
1120 /// accepted packet into `buffered_packets`, and a later
1121 /// [`Error::AllBackendsFailed`] hands those recordings to the caller
1122 /// as owned, mutable `Packet`s. A submission built to be dropped
1123 /// inside one call therefore does **not** stay inside that call on
1124 /// this road — which is what the view lane's send-side sharing
1125 /// assumed. The window closes at commit, when the first frame
1126 /// arrives and `probe` is taken.
1127 #[inline]
1128 pub(crate) const fn is_probing(&self) -> bool {
1129 self.probe.is_some()
1130 }
1131
1132 /// **Where this session is, derived here and nowhere else.**
1133 ///
1134 /// The two latches this reads — whether a backend is still on trial,
1135 /// and whether an end has been recorded — are the only inputs any
1136 /// classification question has ever needed. Reading them at the point
1137 /// of a decision is what let the roads disagree; reading them once,
1138 /// here, is what stops it.
1139 pub(crate) const fn phase(&self) -> SessionPhase {
1140 match (self.probe.is_some(), self.eof_sent) {
1141 (false, false) => SessionPhase::Streaming,
1142 (false, true) => SessionPhase::Draining,
1143 (true, false) => SessionPhase::Auditioning,
1144 (true, true) => SessionPhase::AuditioningPastEnd,
1145 }
1146 }
1147
1148 /// Submit a packet to the decoder.
1149 ///
1150 /// On success — and only on success — the packet is buffered for potential
1151 /// replay through a fallback backend while the probe is active. `EAGAIN`
1152 /// (the decoder needs `receive_frame` to drain output first) is
1153 /// [`Sent::MustDrain`]: nothing was consumed, so the caller drains and
1154 /// offers the same packet again. `AVERROR_EOF` is **not** back pressure
1155 /// on this face — it means this decoder was already told the stream
1156 /// ended — so it stays a fault. See [`send_status`].
1157 ///
1158 /// While the probe is active, a non-transient error (e.g. the active HW
1159 /// backend rejecting this stream's geometry on first packet) advances the
1160 /// probe to the next candidate and retries the packet there. The caller
1161 /// observes only the eventual success or, if the probe is exhausted, the
1162 /// final error.
1163 ///
1164 /// **Atomic probe rescue.** While the probe is active, the rescue
1165 /// invariant is that everything FFmpeg has consumed since open is
1166 /// reflected in `buffered_packets` (so a future
1167 /// [`Error::AllBackendsFailed`] can hand a complete replay history
1168 /// back to the caller for software fallback on a non-seekable input).
1169 /// If we cannot prove this packet is buffer-able — its side-data
1170 /// entry count exceeds [`MAX_PROBE_PACKET_SIDE_DATA_ENTRIES`], its
1171 /// bytes would push the probe past [`MAX_PROBE_PACKETS`] or
1172 /// [`MAX_PROBE_PACKET_BYTES`], or [`av_packet_ref`] fails ENOMEM —
1173 /// `send_packet` returns [`Error::AllBackendsFailed`] **without
1174 /// invoking** `state.inner.send_packet` on this packet. The caller's
1175 /// packet stays in their hand and `unconsumed_packets` carries the
1176 /// pre-existing buffered history, so they can replay
1177 /// `unconsumed_packets` plus the current packet through their
1178 /// software decoder of choice. The post-probe path (after the first
1179 /// frame, when `self.probe` is `None`) skips this pre-flight
1180 /// entirely.
1181 pub fn send_packet(&mut self, packet: &Packet) -> Result<Sent> {
1182 loop {
1183 // Re-read each iteration: a probe advance moves this session from
1184 // one phase to another underneath the loop.
1185 let phase = self.phase();
1186 // Pre-flight while probe is active: prove we can record this
1187 // packet for replay BEFORE the active decoder consumes it.
1188 // `staged_clone` carries the refcounted clone and the new
1189 // `buffered_bytes` value through the send below; we only commit
1190 // them to the probe state if FFmpeg accepts the packet.
1191 let staged_clone: Option<(Packet, usize)> = if let Some(probe) = self.probe.as_ref() {
1192 // Step 1: side-data entry count cap. Read just `side_data_elems`
1193 // (no array walk yet) so a corrupt or weaponised value cannot
1194 // drive an unbounded loop from the safe entry point.
1195 let side_count = packet_side_data_count(packet);
1196 if side_count > MAX_PROBE_PACKET_SIDE_DATA_ENTRIES {
1197 let probe = self.probe.take().expect("probe present");
1198 tracing::warn!(
1199 side_data_entries = side_count,
1200 max_side_data_entries = MAX_PROBE_PACKET_SIDE_DATA_ENTRIES,
1201 trigger = "side_data_entry_cap",
1202 "hwdecode: probe rescue exhausted before consuming packet; \
1203 returning AllBackendsFailed without invoking decoder"
1204 );
1205 return Err(Error::AllBackendsFailed(AllBackendsFailed::new(
1206 probe.attempts,
1207 probe.buffered_packets,
1208 )));
1209 }
1210 // Step 2: byte / packet count cap. `packet_side_data_bytes`
1211 // clamps its walk to MAX_PROBE_PACKET_SIDE_DATA_ENTRIES as
1212 // defense-in-depth even though the count check above already
1213 // bounded the array length.
1214 let pkt_size = packet.size().saturating_add(packet_side_data_bytes(
1215 packet,
1216 MAX_PROBE_PACKET_SIDE_DATA_ENTRIES,
1217 ));
1218 let new_count = probe.buffered_packets.len() + 1;
1219 let new_bytes = probe.buffered_bytes.saturating_add(pkt_size);
1220 if new_count > MAX_PROBE_PACKETS || new_bytes > MAX_PROBE_PACKET_BYTES {
1221 let probe = self.probe.take().expect("probe present");
1222 tracing::warn!(
1223 packets = new_count,
1224 bytes = new_bytes,
1225 side_data_entries = side_count,
1226 max_packets = MAX_PROBE_PACKETS,
1227 max_bytes = MAX_PROBE_PACKET_BYTES,
1228 trigger = "byte_or_packet_cap",
1229 "hwdecode: probe rescue exhausted before consuming packet; \
1230 returning AllBackendsFailed without invoking decoder"
1231 );
1232 return Err(Error::AllBackendsFailed(AllBackendsFailed::new(
1233 probe.attempts,
1234 probe.buffered_packets,
1235 )));
1236 }
1237 // Step 3: pre-clone before consuming. `av_packet_ref` is a
1238 // refcounted shallow clone (no payload deep-copy) but can still
1239 // ENOMEM on heavy side-data; if it does we bail rather than
1240 // consuming a packet we can't track.
1241 match try_clone_packet(packet) {
1242 Ok(c) => Some((c, new_bytes)),
1243 Err(e) => {
1244 let probe = self.probe.take().expect("probe present");
1245 tracing::warn!(
1246 error = %e,
1247 "hwdecode: packet clone failed before consuming; \
1248 returning AllBackendsFailed without invoking decoder"
1249 );
1250 return Err(Error::AllBackendsFailed(AllBackendsFailed::new(
1251 probe.attempts,
1252 probe.buffered_packets,
1253 )));
1254 }
1255 }
1256 } else {
1257 None
1258 };
1259
1260 match self.state.inner.send_packet(packet) {
1261 Ok(()) => {
1262 if let Some((cloned, new_bytes)) = staged_clone {
1263 // Probe is still Some here: the only paths that take it are
1264 // the bailouts above (which return) and `advance_probe`'s
1265 // exhaustion (which would have propagated via `?`). Commit
1266 // the clone now that FFmpeg has accepted the packet.
1267 if let Some(probe) = self.probe.as_mut() {
1268 probe.buffered_packets.push(cloned);
1269 probe.buffered_bytes = new_bytes;
1270 }
1271 }
1272 return Ok(Sent::Accepted);
1273 }
1274 // **libavcodec's send-side flow control, guarded here and read
1275 // through the funnel — the same door the software road uses.**
1276 //
1277 // The guard and the classification are two different questions
1278 // and they get two different answers. `is_transient` decides
1279 // *whether the probe may advance*: neither `EAGAIN` nor
1280 // `AVERROR_EOF` is a candidate failing, so both are taken here,
1281 // ahead of the failure road, exactly as this one guard always
1282 // took them. `send_status` then decides *which* of the two this
1283 // is — back pressure or the double-EOF fault — and that
1284 // decision is not written here at all, so this arm cannot drift
1285 // away from the road the software decoders take. The staged
1286 // clone drops; the caller drains and re-offers, and we re-clone
1287 // at the top of the loop.
1288 //
1289 // It reads the errno through [`Self::hw_send`] rather than
1290 // classifying it raw: a refusal this crate latched during the
1291 // submission — `get_format` declining a coded surface as the
1292 // decoder configures on its first packet — must be what the
1293 // caller is told, not the `EAGAIN` libavcodec reported over the
1294 // top of it.
1295 // **Mint, then route — not mint and return.** A flow signal
1296 // leaves immediately; anything else is a verdict, and a verdict
1297 // that names a declined surface has to reach the probe or the
1298 // fallback rather than exiting plain. `BareVerdict::Reported`
1299 // is the road's own reading of an *unnamed* verdict here: the
1300 // double-EOF is the caller's fault, not the candidate's, so the
1301 // probe must not advance on it.
1302 Err(e) if is_transient(&e) => match self.hw_send(e, phase) {
1303 Ok(status) => return Ok(status),
1304 Err(reason) => match self.hw_route(reason, e, BareVerdict::Reported) {
1305 HwRoute::Report(err) => return Err(err),
1306 HwRoute::Advance(err) => {
1307 self.advance_probe(err)?;
1308 continue;
1309 }
1310 },
1311 },
1312 // A real failure. Minted once and routed by the shared policy:
1313 // while a candidate is on trial this is that candidate failing,
1314 // so `advance_probe` consumes the reason into `attempts` and
1315 // either installs the next candidate or surfaces
1316 // `AllBackendsFailed`. Any staged clone drops without entering
1317 // history; the next iteration clones afresh.
1318 Err(e) => match self.hw_failure(e, BareVerdict::CandidateFailure) {
1319 HwRoute::Report(err) => return Err(err),
1320 HwRoute::Advance(err) => {
1321 self.advance_probe(err)?;
1322 continue;
1323 }
1324 },
1325 }
1326 }
1327 }
1328
1329 /// Signal end-of-stream to the decoder.
1330 ///
1331 /// Recorded for replay only if the underlying `send_eof` succeeds. While
1332 /// the probe is active, non-transient errors trigger probe advance and
1333 /// retry, matching `send_packet`'s behaviour.
1334 ///
1335 /// Answers [`Sent::MustDrain`] on `EAGAIN` — the end-of-stream was
1336 /// **not** recorded, so drain and signal again. A second EOF is a
1337 /// caller fault and stays one; see [`send_status`].
1338 pub fn send_eof(&mut self) -> Result<Sent> {
1339 loop {
1340 // Re-read each iteration: a probe advance moves this session from
1341 // one phase to another underneath the loop.
1342 let phase = self.phase();
1343 match self.state.inner.send_eof() {
1344 Ok(()) => {
1345 self.eof_sent = true;
1346 return Ok(Sent::Accepted);
1347 }
1348 // The same guard, the same door and the same routing as
1349 // `send_packet`; see the note there.
1350 Err(e) if is_transient(&e) => match self.hw_send(e, phase) {
1351 Ok(status) => return Ok(status),
1352 Err(reason) => match self.hw_route(reason, e, BareVerdict::Reported) {
1353 HwRoute::Report(err) => return Err(err),
1354 HwRoute::Advance(err) => {
1355 self.advance_probe(err)?;
1356 continue;
1357 }
1358 },
1359 },
1360 // The same shared policy; see `send_packet`.
1361 Err(e) => match self.hw_failure(e, BareVerdict::CandidateFailure) {
1362 HwRoute::Report(err) => return Err(err),
1363 HwRoute::Advance(err) => {
1364 self.advance_probe(err)?;
1365 continue;
1366 }
1367 },
1368 }
1369 }
1370 }
1371
1372 /// Receive a CPU-side decoded frame.
1373 ///
1374 /// The frame is downloaded with `av_hwframe_transfer_data` and metadata
1375 /// is copied via `av_frame_copy_props`. The caller's frame is always
1376 /// unref'd first, so reuse across resolution changes or different
1377 /// decoders is safe.
1378 ///
1379 /// While the probe window is open, *any* non-transient failure (decode
1380 /// error, transfer error, copy_props error, or a CPU-format frame from a
1381 /// HW-opened context) tears down the current decoder and advances to the
1382 /// next hardware backend in probe order, replaying buffered packets
1383 /// through it. Frames the candidate produced during replay (drained when
1384 /// `send_packet` returned EAGAIN) are queued and delivered FIFO via this
1385 /// method, so the caller never loses initial frames after a fallback.
1386 ///
1387 /// This crate is hardware-only: there is no software fallback inside the
1388 /// decoder. When every backend in the probe order has been exhausted —
1389 /// including the case of a single-backend platform whose only backend
1390 /// failed — this returns [`Error::AllBackendsFailed`] with the per-
1391 /// backend attempt log so the caller can branch into a software
1392 /// decoder of their choice.
1393 ///
1394 /// Answers the same three states `ffmpeg::decoder::Video` does, in
1395 /// the shape the trait tier publishes: [`Received::NeedsInput`] where
1396 /// libavcodec says `EAGAIN`, [`Received::Ended`] where it says `EOF`,
1397 /// and [`Received::Frame`] when `frame` was written. **The errno
1398 /// stops here** — the two flow signals never leave this crate as
1399 /// `Error::Ffmpeg`, so a caller has nothing to decode.
1400 pub fn receive_frame(&mut self, frame: &mut Frame) -> Result<Received> {
1401 // Pre-drain frames queued during probe replay. They are already CPU-side
1402 // (transferred at drain time, when the candidate's HW context was alive)
1403 // so we just move them into the caller's slot.
1404 if self.try_pop_pending(frame) {
1405 return Ok(Received::Frame);
1406 }
1407
1408 loop {
1409 // Re-read each iteration: a probe advance moves this session from
1410 // one phase to another underneath the loop.
1411 let phase = self.phase();
1412 let res = self.state.inner.receive_frame(&mut self.hw_frame);
1413 match res {
1414 Err(e) => {
1415 // **The phase decides whether this errno is a protocol state
1416 // at all, and this arm holds no opinion of its own.**
1417 //
1418 // `EAGAIN` used to short-circuit here unconditionally, which
1419 // was right for three of the four phases and quietly wrong for
1420 // the fourth: a candidate that has been replayed the whole
1421 // history *including the end* and still answers "nothing yet"
1422 // has produced zero frames and never will. Answering the
1423 // caller `NeedsInput` there asked for input nothing could
1424 // supply; answering `Ended` would have credited a backend that
1425 // never decoded a thing. It is a candidate failing, and the
1426 // classifier says so by handing it back — straight into the
1427 // probe road below, which is where candidate failures have
1428 // always gone.
1429 //
1430 // **And it reads the errno through the funnel, which is the
1431 // law this road lost and got back.** A `get_format`
1432 // declination or an allocator-judge refusal sits in the
1433 // callback state waiting to be collected; classifying the raw
1434 // errno first answers `Ended` or `NeedsInput` for a frame this
1435 // crate itself declined, and the reason dies unread. The
1436 // funnel is no longer a step to remember — [`Self::hw_receive`]
1437 // is the only way in, and the classifiers are private to this
1438 // module so no road can take a shortcut past it.
1439 let reason = match self.hw_receive(e, phase) {
1440 Ok(status) => return Ok(status),
1441 // The funnel's verdict: the latched refusal when there was
1442 // one, the original error when there was not. It travels
1443 // onward as it is — rebuilding `Error::Ffmpeg(e)` here would
1444 // throw away the collection that just happened.
1445 Err(reason) => reason,
1446 };
1447 // **The same shared policy every hardware road uses.** This
1448 // road mints its own verdict (above), so it routes rather than
1449 // minting again. `CandidateFailure` is its reading of an
1450 // unnamed verdict: a candidate that drains to `EOF` without
1451 // ever producing a frame is a candidate failing, not a stream
1452 // ending — which is why this road hands `AVERROR_EOF` to the
1453 // probe while the send roads report it.
1454 match self.hw_route(reason, e, BareVerdict::CandidateFailure) {
1455 HwRoute::Report(err) => return Err(err),
1456 HwRoute::Advance(err) => {
1457 self.advance_probe(err)?;
1458 // Probe advance may have populated `pending_frames`;
1459 // deliver one of those before reading more from the new
1460 // candidate.
1461 if self.try_pop_pending(frame) {
1462 return Ok(Received::Frame);
1463 }
1464 continue;
1465 }
1466 }
1467 }
1468 Ok(()) => {
1469 // Always attempt the HW→CPU transfer. With strict `get_format`,
1470 // libavcodec can only deliver frames in the wired-up HW format
1471 // (or fail). If a misbehaving codec ever hands us a CPU-side
1472 // frame anyway, `av_hwframe_transfer_data` returns AVERROR(EINVAL)
1473 // (neither src nor dst has an AVHWFramesContext attached) and we
1474 // route through the same error path below.
1475 // **The transfer is priced before it is paid, and a refusal
1476 // here is final.** See [`judge_hw_transfer`]: neither ceiling
1477 // hook reaches this allocation — `hwaccel->alloc_frame`
1478 // bypasses `get_buffer2` entirely, and the CPU destination is
1479 // allocated by `av_hwframe_transfer_data` outside both — so
1480 // this is the seat that bounds what the hardware road hands
1481 // back.
1482 //
1483 // Judged out here rather than inside `transfer_hw_frame`
1484 // deliberately. Errors from that function are FFmpeg's, and
1485 // the arms below reclassify them into "the hardware failed,
1486 // fall back to software". A byte ceiling is not a hardware
1487 // failure: software would decode the same oversized frame and
1488 // be refused again, so retrying it silently is exactly the
1489 // wrong answer. The named refusal returns straight to the
1490 // caller.
1491 if let Err(e) =
1492 unsafe { judge_hw_transfer(self.hw_frame.as_ptr(), self.frame_limits.frame()) }
1493 {
1494 return Err(Error::HwTransferTooLarge(e));
1495 }
1496 // **The scaled-output stage, and the one place it sits.** A
1497 // standing request turns this into a GPU resize followed by a
1498 // download of the *fitted* surface; with no request, or on any
1499 // condition the stage cannot honor, `stage` answers `None` and
1500 // the full-size hardware frame is downloaded exactly as
1501 // before. It cannot fail — see [`crate::vtscale`].
1502 //
1503 // The byte ceiling above is deliberately **not** re-priced
1504 // against the fitted size. `judge_hw_transfer` bounds what
1505 // this road may allocate, and pricing the full-size frame is
1506 // the conservative reading of that bound: a stream refused
1507 // without scaled output is refused identically with it, so
1508 // turning the stage on can never widen what the ceiling lets
1509 // through. What it saves is the bus traffic and the CPU
1510 // allocation actually paid, which is the trade #55 is about.
1511 //
1512 // **And a fitted surface that will not download is the
1513 // stage's failure, not the backend's.** The scale can succeed
1514 // and the crossing still fail — an unsupported destination
1515 // pixel format, a metadata copy that runs out of memory — and
1516 // routing that into the arms below would let an optional
1517 // bandwidth optimisation reject a VideoToolbox decode that
1518 // was working, or degrade the session to software. So the
1519 // fitted attempt is made first and separately: if it fails,
1520 // the stage latches the key off, the destination is reset,
1521 // and the original full-size frame — still live in
1522 // `hw_frame`, still the path this crate took before any of
1523 // this existed — is downloaded instead. Only *that* failing
1524 // is a hardware failure.
1525 let scaled = self
1526 .scaled_output
1527 .stage(&self.hw_frame)
1528 .map(|fitted| unsafe { transfer_hw_frame(frame, fitted) });
1529 match scaled {
1530 Some(Ok(())) => {
1531 self.probe = None;
1532 return Ok(Received::Frame);
1533 }
1534 Some(Err(e)) => {
1535 tracing::warn!(
1536 error = %e,
1537 "hwdecode: the fitted surface would not download; retiring scaled output for \
1538 this size and delivering the full-size frame instead"
1539 );
1540 self.scaled_output.latch_failure();
1541 // SAFETY: `frame` is the caller's slot; unreferencing it
1542 // discards whatever the failed transfer left behind
1543 // before the retry writes it again.
1544 unsafe { av_frame_unref(frame.as_inner_mut().as_mut_ptr()) };
1545 }
1546 None => {}
1547 }
1548 match unsafe { transfer_hw_frame(frame, &self.hw_frame) } {
1549 Ok(()) => {
1550 self.probe = None;
1551 return Ok(Received::Frame);
1552 }
1553 Err(e) => {
1554 // The same shared policy. A transfer failure is an
1555 // HW-output problem — an unsupported CPU pix_fmt surfaces
1556 // as `AVERROR(EINVAL)`, a context loss as Bug/Bug2/Unknown
1557 // — never input corruption, so while a candidate is on
1558 // trial it is that candidate failing.
1559 match self.hw_failure(e, BareVerdict::CandidateFailure) {
1560 HwRoute::Report(err) => return Err(err),
1561 HwRoute::Advance(err) => {
1562 self.advance_probe(err)?;
1563 unsafe { av_frame_unref(frame.as_inner_mut().as_mut_ptr()) };
1564 if self.try_pop_pending(frame) {
1565 return Ok(Received::Frame);
1566 }
1567 continue;
1568 }
1569 }
1570 }
1571 }
1572 }
1573 }
1574 }
1575 }
1576
1577 /// Pop one queued frame (produced by a candidate decoder during probe
1578 /// replay) into the caller's slot. Returns `true` when a frame was
1579 /// delivered, `false` when the queue was empty.
1580 fn try_pop_pending(&mut self, frame: &mut Frame) -> bool {
1581 let Some(mut buffered) = self.pending_frames.pop_front() else {
1582 return false;
1583 };
1584 // SAFETY: `buffered` is a CPU-side AVFrame we previously transferred
1585 // and pushed into the queue; both pointers are valid.
1586 unsafe {
1587 av_frame_unref(frame.as_inner_mut().as_mut_ptr());
1588 av_frame_move_ref(frame.as_inner_mut().as_mut_ptr(), buffered.as_mut_ptr());
1589 }
1590 // Probe semantics: delivering a frame collapses the probe.
1591 self.probe = None;
1592 true
1593 }
1594
1595 /// Flush internal buffers (e.g. after a seek).
1596 ///
1597 /// Discards every frame buffered by the decoder, every frame queued during
1598 /// probe replay (`pending_frames`), and the residual `hw_frame` scratch
1599 /// buffer. Probe-time replay state (buffered packets, EOF marker) is also
1600 /// cleared since post-seek packets do not align with the previously
1601 /// captured history. After a flush, the next `receive_frame` waits for new
1602 /// post-seek input.
1603 pub fn flush(&mut self) {
1604 self.state.inner.flush();
1605 // SAFETY: hw_frame is a valid AVFrame we own; av_frame_unref is a no-op
1606 // for an already-empty frame.
1607 unsafe { av_frame_unref(self.hw_frame.as_mut_ptr()) };
1608 self.pending_frames.clear();
1609 // The end belongs to the position being abandoned.
1610 self.eof_sent = false;
1611 if let Some(probe) = self.probe.as_mut() {
1612 probe.buffered_packets.clear();
1613 probe.buffered_bytes = 0;
1614 }
1615 }
1616
1617 /// Takes the coded-surface refusal the `get_format` callback left
1618 /// behind, if it left one, clearing it for the next candidate.
1619 fn take_ceiling_declination(&self) -> Option<Error> {
1620 ceiling_declination_of(self.state.callback_state)
1621 }
1622
1623 /// **The single hardware-exit funnel.** Every road that turns a
1624 /// hardware failure — or an end-of-stream that is really a refusal —
1625 /// into an `Error` goes through here, and it reads the callback's
1626 /// declination *before* anything wraps or tears down state.
1627 ///
1628 /// The reason there is a funnel at all: a `get_format` callback
1629 /// cannot return a reason, so it leaves one behind, and every exit
1630 /// that forgets to collect it hands the caller libavcodec's
1631 /// `Invalid data found when processing input` for a refusal this
1632 /// crate made — or, on the explicit-backend road, a stream that
1633 /// simply drains to EOF with nothing said at all.
1634 ///
1635 /// The lesson this encodes: R14 claimed four consumers of the
1636 /// declination and production had exactly one. Consumers added
1637 /// helper-by-helper are lost the next time the surrounding code is
1638 /// restructured; a single funnel that every exit *must* call is the
1639 /// only version of this that stays true. The per-road table in
1640 /// `decoder/tests.rs` is what checks that it did.
1641 /// The hardware road's funnel-and-classify entry — [`software_receive`]'s
1642 /// twin, and the same law: what a caller reads is what the funnel
1643 /// found, never the errno that reached it.
1644 ///
1645 /// Answers `Err` with the funnel's verdict, which is the latched
1646 /// refusal when there was one. Callers route *that* value onward
1647 /// rather than rebuilding the raw error, or the collection is undone
1648 /// the moment it is used.
1649 fn hw_receive(&self, e: ffmpeg_next::Error, phase: SessionPhase) -> Result<Received> {
1650 receive_status(self.hw_exit(Error::Ffmpeg(e)), phase)
1651 }
1652
1653 /// The send road's half of [`Self::hw_receive`].
1654 fn hw_send(&self, e: ffmpeg_next::Error, phase: SessionPhase) -> Result<Sent> {
1655 send_status(self.hw_exit(Error::Ffmpeg(e)), phase)
1656 }
1657
1658 fn hw_exit(&self, fallback: Error) -> Error {
1659 self
1660 .take_ceiling_declination()
1661 .or_else(|| frame_budget_declination_of(self.state.callback_state))
1662 .unwrap_or(fallback)
1663 }
1664
1665 /// Try the next backend in `remaining_backends`. Transactional: a
1666 /// candidate must successfully build and accept the replayed history
1667 /// before any probe state is consumed. Backends that fail to build or
1668 /// reject the replay are recorded into `probe.attempts` and the loop
1669 /// continues to the next one.
1670 ///
1671 /// `last_error` is the error that triggered this advance — i.e. the
1672 /// failure of the currently active backend on `send_packet` /
1673 /// `send_eof` / `receive_frame`. It is recorded against the active
1674 /// backend before any candidate is tried so that a final
1675 /// `AllBackendsFailed` carries the full attempt log including the
1676 /// initially-opened backend's runtime failure.
1677 ///
1678 /// Returns:
1679 /// - `Ok(())` when a candidate is installed and replay completed —
1680 /// caller should retry the operation.
1681 /// - `Err(Error::AllBackendsFailed(p))` when every remaining
1682 /// backend has been exhausted (including the just-failed active one).
1683 /// `p.attempts()` carries the per-backend failure log.
1684 /// This is what the documented `open` contract promises, surfaced at
1685 /// runtime so the caller can branch into a software fallback. On a
1686 /// single-backend platform (e.g. macOS), this fires after the only
1687 /// backend's first-frame failure; on multi-backend platforms it
1688 /// fires after the last candidate's failure.
1689 /// - `Err(_)` for other fatal conditions surfaced by probe machinery
1690 /// itself (e.g. `alloc_av_frame` ENOMEM during replay drain).
1691 fn advance_probe(&mut self, last_error: Error) -> Result<()> {
1692 // Record the failure that triggered this advance against the active
1693 // backend. If the probe was somehow already gone (shouldn't happen —
1694 // call sites guard with `self.probe.is_some()`), just propagate the
1695 // error so behaviour matches the pre-fix code path.
1696 let active_backend = self.state.backend;
1697 // **The reason the callback could not return.** Declining a format
1698 // in `get_format` surfaces from libavcodec as
1699 // `Invalid data found when processing input` — true about what it
1700 // saw, false about what happened, because the data was fine and
1701 // this crate declined it over the coded surface's size. The
1702 // callback leaves the real reason in its own state; this is where
1703 // it becomes the error the caller reads.
1704 // **Mint or no-op, and never a re-derivation.** Three of this
1705 // method's callers hand it a raw `Error::Ffmpeg` — no funnel has run
1706 // on their roads — so this is where their verdict is minted. The
1707 // receive road hands it one already minted, and this call then finds
1708 // the latch empty and returns its argument unchanged: `hw_exit`
1709 // answers with the recorded refusal when there is one and with its
1710 // fallback when there is not, so a verdict passed in comes back out.
1711 // Either way the caller's reason is what gets recorded. See the
1712 // invariant on [`software_receive`].
1713 let last_error = self.hw_exit(last_error);
1714 match self.probe.as_mut() {
1715 Some(probe) => probe.attempts.push((active_backend, Box::new(last_error))),
1716 None => return Err(last_error),
1717 }
1718
1719 // Drop frames previously queued from the backend we're now abandoning.
1720 // They came from a candidate that just failed for cause and cannot be
1721 // trusted alongside frames we may queue from the next candidate. (If
1722 // this method is called repeatedly via chained probe advances, this
1723 // also keeps `pending_frames` from accumulating frames from multiple
1724 // rejected backends.)
1725 self.pending_frames.clear();
1726 // Read before any `probe` borrow: the end is the *decoder's* fact
1727 // now, not the probe's, and a candidate must be handed it along
1728 // with the replayed history or it will sit at `EAGAIN` forever on a
1729 // stream that is already over.
1730 let eof_sent = self.eof_sent;
1731
1732 loop {
1733 // Snapshot inputs without mutating probe state. Use the checked
1734 // clone helper rather than `Parameters::clone` (which masks ENOMEM).
1735 let (next_backend, parameters, codec) = match self.probe.as_ref() {
1736 Some(probe) if !probe.remaining_backends.is_empty() => {
1737 let parameters = match try_clone_parameters(
1738 &probe.parameters,
1739 self.frame_limits.max_codec_parameter_bytes(),
1740 ) {
1741 Ok(p) => p,
1742 Err(e) => {
1743 tracing::warn!(
1744 error = %e,
1745 "hwdecode: parameters clone failed during probe advance; popping backend and trying next"
1746 );
1747 let popped = self
1748 .probe
1749 .as_mut()
1750 .expect("probe state present")
1751 .remaining_backends
1752 .remove(0);
1753 self
1754 .probe
1755 .as_mut()
1756 .expect("probe state present")
1757 .attempts
1758 .push((popped, Box::new(e)));
1759 continue;
1760 }
1761 };
1762 (probe.remaining_backends[0], parameters, probe.codec)
1763 }
1764 // No more candidates — surface the accumulated attempt log as
1765 // AllBackendsFailed so single- and multi-backend platforms have
1766 // the same contract for "every HW backend failed."
1767 //
1768 // Hand the buffered packet history back to the caller along
1769 // with the attempt log: those packets were consumed from the
1770 // caller's demuxer (and refcounted-cloned into `buffered_packets`)
1771 // before the probe exhausted, and for non-seekable inputs the
1772 // caller cannot re-demux them. Returning them here lets a
1773 // caller-side software fallback replay the same byte history
1774 // through `ffmpeg::decoder::Video` without losing initial frames.
1775 // Dropping `ProbeState` after the take frees the codec/params
1776 // refs we no longer need; only `attempts` and `buffered_packets`
1777 // are retained.
1778 _ => {
1779 let (attempts, unconsumed_packets) = self
1780 .probe
1781 .take()
1782 .map(|p| (p.attempts, p.buffered_packets))
1783 .unwrap_or_default();
1784 return Err(Error::AllBackendsFailed(AllBackendsFailed::new(
1785 attempts,
1786 unconsumed_packets,
1787 )));
1788 }
1789 };
1790
1791 let prev_backend = self.state.backend;
1792 tracing::warn!(from = ?prev_backend, to = ?next_backend, "hwdecode: advancing probe");
1793
1794 // Build candidate. On failure, record into attempts and continue
1795 // without touching the packet buffer.
1796 let mut candidate_state = match Self::build_state(
1797 parameters,
1798 codec,
1799 next_backend,
1800 self.frame_limits,
1801 self.pkt_timebase,
1802 ) {
1803 Ok(s) => s,
1804 Err(e) => {
1805 tracing::warn!(?next_backend, error = %e, "hwdecode: candidate build failed");
1806 self
1807 .probe
1808 .as_mut()
1809 .expect("probe state present")
1810 .remaining_backends
1811 .remove(0);
1812 self
1813 .probe
1814 .as_mut()
1815 .expect("probe state present")
1816 .attempts
1817 .push((next_backend, Box::new(e)));
1818 continue;
1819 }
1820 };
1821
1822 // Replay buffered history through the candidate WITHOUT installing it.
1823 // We borrow the buffer immutably; if replay fails the candidate's Drop
1824 // releases the FFmpeg state and the buffer is preserved for the next
1825 // attempt.
1826 //
1827 // EAGAIN handling: `avcodec_send_packet` may return EAGAIN when its
1828 // internal queue is full and the user is expected to drain output
1829 // first (B-frame buffering, candidate-specific queue depth, etc.).
1830 // This is normal flow — we drain frames out of the candidate, transfer
1831 // each one to a CPU frame, and stash them in `local_pending`. After
1832 // commit they move to `self.pending_frames` and are delivered FIFO
1833 // by `receive_frame`, so the caller never loses initial frames.
1834 let mut local_pending: VecDeque<frame::Video> = VecDeque::new();
1835 let mut local_pending_bytes: usize = 0;
1836 let max_pending_bytes = self.max_probe_pending_bytes;
1837 let replay_result: std::result::Result<(), ffmpeg_next::Error> = {
1838 let probe = self.probe.as_ref().expect("probe state present");
1839 let mut hw_buf = match alloc_av_frame() {
1840 Ok(f) => f,
1841 Err(e) => return Err(Error::Ffmpeg(e)),
1842 };
1843 let mut r: std::result::Result<(), ffmpeg_next::Error> = Ok(());
1844
1845 'replay: for pkt in &probe.buffered_packets {
1846 loop {
1847 match candidate_state.inner.send_packet(pkt) {
1848 Ok(()) => break,
1849 Err(e) if is_eagain(&e) => {
1850 // Drain candidate output (transferring + queueing each frame)
1851 // and retry the same packet.
1852 if let Err(de) = drain_into_pending(
1853 &mut candidate_state.inner,
1854 &mut hw_buf,
1855 &mut local_pending,
1856 &mut local_pending_bytes,
1857 max_pending_bytes,
1858 self.frame_limits.frame(),
1859 ) {
1860 r = Err(de);
1861 break 'replay;
1862 }
1863 }
1864 Err(e) => {
1865 r = Err(e);
1866 break 'replay;
1867 }
1868 }
1869 }
1870 }
1871 if r.is_ok() && eof_sent {
1872 // `avcodec_send_packet(NULL)` (which `send_eof` becomes) can
1873 // return EAGAIN with the same drain-output-first semantics as
1874 // a regular send_packet. Loop drain+retry instead of failing
1875 // the candidate on backpressure.
1876 loop {
1877 match candidate_state.inner.send_eof() {
1878 Ok(()) => break,
1879 Err(e) if is_eagain(&e) => {
1880 if let Err(de) = drain_into_pending(
1881 &mut candidate_state.inner,
1882 &mut hw_buf,
1883 &mut local_pending,
1884 &mut local_pending_bytes,
1885 max_pending_bytes,
1886 self.frame_limits.frame(),
1887 ) {
1888 r = Err(de);
1889 break;
1890 }
1891 }
1892 Err(e) => {
1893 r = Err(e);
1894 break;
1895 }
1896 }
1897 }
1898 }
1899 r
1900 };
1901
1902 if let Err(e) = replay_result {
1903 tracing::warn!(?next_backend, error = %e, "hwdecode: candidate replay failed");
1904 // **The candidate's own refusal, read before the candidate
1905 // dies.** `hw_exit` consults `self.state` — the backend that is
1906 // still active — but the error being recorded here belongs to
1907 // `candidate_state`, whose `get_format` callback is the one
1908 // that may have declined. Classifying through the wrong state
1909 // and then dropping the right one lost the reason entirely: the
1910 // attempt log recorded FFmpeg's `Invalid data found when
1911 // processing input` for a coded surface this crate refused.
1912 //
1913 // Order matters and is the whole fix — read, then drop.
1914 let recorded =
1915 ceiling_declination_of(candidate_state.callback_state).unwrap_or(Error::Ffmpeg(e));
1916 // Drop candidate explicitly so its FFI cleanup runs now. Discard any
1917 // frames we drained from this candidate — they're tied to a decoder
1918 // we're throwing away.
1919 drop(candidate_state);
1920 drop(local_pending);
1921 self
1922 .probe
1923 .as_mut()
1924 .expect("probe state present")
1925 .remaining_backends
1926 .remove(0);
1927 self
1928 .probe
1929 .as_mut()
1930 .expect("probe state present")
1931 .attempts
1932 .push((next_backend, Box::new(recorded)));
1933 continue;
1934 }
1935
1936 // Commit: install the candidate, clear residual hw_frame, queue the
1937 // drained frames for the caller, and pop the now-active backend.
1938 self.state = candidate_state;
1939 // **The scaled-output stage's session belongs to the device that
1940 // just went away.** Its fitted frames context was built over the
1941 // outgoing backend's hardware device; the caller's standing
1942 // request outlives the advance, but nothing built for the old
1943 // device may. Unreachable on the one platform where the stage has
1944 // a body — `probe_order` names a single backend on Apple targets,
1945 // so this method never reaches a commit there — and written all
1946 // the same, because "unreachable today" is not a property a
1947 // resource-owning cache should depend on.
1948 self.scaled_output.retire();
1949 unsafe { av_frame_unref(self.hw_frame.as_mut_ptr()) };
1950 self.pending_frames.append(&mut local_pending);
1951 self
1952 .probe
1953 .as_mut()
1954 .expect("probe state present")
1955 .remaining_backends
1956 .remove(0);
1957 return Ok(());
1958 }
1959 }
1960
1961 /// Build raw FFmpeg state for one hardware backend. Strict `get_format`
1962 /// (NONE on missing HW format); cross-backend fallback is the caller's job.
1963 fn build_state(
1964 parameters: codec::Parameters,
1965 codec: Codec,
1966 backend: Backend,
1967 limits: crate::limits::DecoderLimits,
1968 pkt_timebase: Option<mediadecode::Timebase>,
1969 ) -> Result<DecoderState> {
1970 // Use our checked allocator instead of Context::from_parameters, which
1971 // does not null-check avcodec_alloc_context3 and would feed a null
1972 // AVCodecContext into FFmpeg under OOM.
1973 let (mut ctx, mut state) = build_codec_context(¶meters, limits, pkt_timebase)?;
1974 let av_type = backend.av_hwdevice_type();
1975
1976 // Verify the codec advertises this hwaccel **with the exact HW pix_fmt
1977 // we're about to wire up in `get_format`**. FFmpeg's HW config table
1978 // is keyed per (device_type, pix_fmt); a codec can advertise the same
1979 // device with several HW pix_fmts, so matching only on device_type
1980 // would let probing succeed for a backend whose pix_fmt the codec
1981 // never offers — the failure would then surface deep inside the
1982 // probe/decode loop. Matching the exact pix_fmt keeps the strict
1983 // `get_format` honest and gives `open_with` a clean rejection.
1984 let hw_pix_fmt = backend.hw_pixel_format();
1985 if !codec_supports_hwaccel(unsafe { codec.as_ptr() }, av_type, hw_pix_fmt as i32) {
1986 return Err(Error::BackendUnsupportedByCodec(backend));
1987 }
1988
1989 // Create the device context.
1990 let mut hw_device_ref: *mut AVBufferRef = ptr::null_mut();
1991 // SAFETY: `hw_device_ref` is a stack ptr we hand FFmpeg to fill.
1992 let ret = unsafe {
1993 av_hwdevice_ctx_create(&mut hw_device_ref, av_type, ptr::null(), ptr::null_mut(), 0)
1994 };
1995 if ret < 0 {
1996 return Err(Error::HwDeviceInitFailed(HwDeviceInitFailed::new(
1997 backend,
1998 ffmpeg_next::Error::from(ret),
1999 )));
2000 }
2001
2002 // The state `build_codec_context` already installed in `opaque`,
2003 // told which format this backend wants. One allocation, one seat:
2004 // the budget the judge reads and the declination the funnel reads
2005 // are the same object, and `Box::into_raw` hands its ownership to
2006 // the guard below without moving it — so the pointer the context
2007 // holds stays the one that is freed.
2008 state.wanted = hw_pix_fmt;
2009 state.wanted_int = hw_pix_fmt as i32;
2010 let callback_state = Box::into_raw(state);
2011 // RAII guard: from now until the end-of-function `into_owned()`, every
2012 // early return — `av_buffer_ref` failure, `open_as` failure, codec_type
2013 // mismatch, or any future error path added between here and the
2014 // `DecoderState` construction — frees `hw_device_ref` and
2015 // `callback_state` via the guard's Drop. Without it, each error site
2016 // had to remember to clean up these two FFI-owned resources by hand;
2017 // the codec_type-mismatch branch was missed and silently leaked one
2018 // device ref + one heap allocation per bad input.
2019 let guard = PartialBuildState {
2020 hw_device_ref,
2021 callback_state,
2022 };
2023
2024 // SAFETY: ctx is a freshly-constructed AVCodecContext we own;
2025 // av_buffer_ref bumps the refcount of the device buffer for FFmpeg's
2026 // use (we keep our own ref in `hw_device_ref` for cleanup).
2027 // av_buffer_ref returns NULL on allocation failure; we must check it
2028 // before assigning, otherwise the codec context would be opened with a
2029 // HW-flagged setup but no actual device reference.
2030 let device_ref_for_ctx = unsafe { av_buffer_ref(hw_device_ref) };
2031 if device_ref_for_ctx.is_null() {
2032 // guard's Drop frees hw_device_ref (the first ref) and callback_state.
2033 return Err(Error::Ffmpeg(ffmpeg_next::Error::Other {
2034 errno: libc::ENOMEM,
2035 }));
2036 }
2037 // SAFETY: device_ref_for_ctx is a valid AVBufferRef* from av_buffer_ref;
2038 // ctx is freshly built and owned by us. After this point ctx aliases
2039 // `callback_state` via `opaque` (FFmpeg never frees opaque, so
2040 // `callback_state` ownership stays with us / the guard) and aliases
2041 // `device_ref_for_ctx` (the second ref) via `hw_device_ctx` (FFmpeg
2042 // unrefs that on codec context drop, independent of the guard's first
2043 // ref).
2044 unsafe {
2045 let raw = ctx.as_mut_ptr();
2046 (*raw).hw_device_ctx = device_ref_for_ctx;
2047 (*raw).opaque = callback_state.cast();
2048 (*raw).get_format = Some(get_hw_format);
2049 }
2050
2051 // Open the decoder. On failure `ctx`/`opened` Drop releases the codec
2052 // context (and via that the second device ref); the guard releases the
2053 // first device ref and the callback state.
2054 //
2055 // We deliberately bypass `Opened::video()` because it calls
2056 // `Context::medium()`, which reads `AVCodecContext.codec_type` as the
2057 // bindgen `AVMediaType` enum — the same UB hazard we've been
2058 // systematically removing. Instead: validate `codec_type` as a raw
2059 // `c_int` ourselves, then construct the `decoder::Video` wrapper
2060 // directly via its public tuple field.
2061 // Through the funnel's free-standing half — there is no decoder yet
2062 // to ask, and the guard frees the callback state on the way out, so
2063 // the reason has to be collected here or not at all.
2064 let opened = match ctx.decoder().open_as(codec) {
2065 Ok(opened) => opened,
2066 Err(e) => return Err(ceiling_declination_of(callback_state).unwrap_or(Error::Ffmpeg(e))),
2067 };
2068
2069 // Validate codec_type as a raw integer — never construct AVMediaType
2070 // from an unvalidated runtime value. On failure `opened`'s Drop
2071 // releases the codec context; the guard releases the first
2072 // hw_device_ref and the callback state.
2073 if let Err(e) = ensure_video_codec_type(&opened) {
2074 // Same exit, same collection: a declined format can leave the
2075 // context looking like the wrong medium.
2076 return Err(ceiling_declination_of(callback_state).unwrap_or(e));
2077 }
2078 // SAFETY of construction: `decoder::Video` is `pub struct Video(pub Opened)`.
2079 // We construct via the public field; this is the same wrapping
2080 // `Opened::video()` does on success, just without the enum read.
2081 let opened = ffmpeg_next::decoder::Video(opened);
2082
2083 // Disarm the guard and transfer ownership of both resources into the
2084 // returned DecoderState (whose own Drop handles their lifetime).
2085 let (hw_device_ref, callback_state) = guard.into_owned();
2086 Ok(DecoderState {
2087 inner: ManuallyDrop::new(opened),
2088 backend,
2089 hw_device_ref,
2090 callback_state,
2091 })
2092 }
2093}
2094
2095/// RAII guard for the partially-owned FFmpeg state that
2096/// [`VideoDecoder::build_state`] holds between the
2097/// `av_hwdevice_ctx_create` and `Box::into_raw(CallbackState)`
2098/// allocations and the final `DecoderState` construction.
2099///
2100/// If `build_state` returns `Err` for any reason in that window
2101/// (`av_buffer_ref` ENOMEM, `open_as` failure, codec_type mismatch, or
2102/// any future error path), this guard's `Drop` releases
2103/// `hw_device_ref` — the first ref returned by `av_hwdevice_ctx_create`,
2104/// distinct from the second ref FFmpeg unrefs when the codec context
2105/// drops — and the boxed `CallbackState`, which FFmpeg never touches
2106/// because `AVCodecContext::opaque` is purely user-owned.
2107///
2108/// Successful construction calls [`Self::into_owned`] to disarm the
2109/// guard and hand both pointers to the new `DecoderState`.
2110struct PartialBuildState {
2111 hw_device_ref: *mut AVBufferRef,
2112 callback_state: *mut CallbackState,
2113}
2114
2115impl PartialBuildState {
2116 /// Disarm the guard: return the owned pointers and replace the guard's
2117 /// fields with null so its Drop is a no-op.
2118 fn into_owned(mut self) -> (*mut AVBufferRef, *mut CallbackState) {
2119 let hw = std::mem::replace(&mut self.hw_device_ref, ptr::null_mut());
2120 let cb = std::mem::replace(&mut self.callback_state, ptr::null_mut());
2121 (hw, cb)
2122 }
2123}
2124
2125impl Drop for PartialBuildState {
2126 fn drop(&mut self) {
2127 // SAFETY: pointers are either freshly allocated by `build_state` (via
2128 // `av_hwdevice_ctx_create` and `Box::into_raw`) or null after
2129 // `into_owned`. Both `av_buffer_unref` and `Box::from_raw` need the
2130 // null check we apply here; both are otherwise sound on resources we
2131 // own.
2132 unsafe {
2133 if !self.hw_device_ref.is_null() {
2134 let mut hw = self.hw_device_ref;
2135 av_buffer_unref(&mut hw);
2136 }
2137 if !self.callback_state.is_null() {
2138 drop(Box::from_raw(self.callback_state));
2139 }
2140 }
2141 }
2142}
2143
2144/// Download a HW frame into a CPU [`Frame`]. Always unrefs the destination
2145/// first so reuse across resolution changes is safe.
2146///
2147/// `src` is whichever surface the scaled-output stage nominated: the
2148/// decoder's own hardware frame in the ordinary case, or the fitted
2149/// surface [`crate::vtscale`] scaled it into when a request is standing.
2150/// Both are VideoToolbox frames carrying their own `AVHWFramesContext`,
2151/// and the fitted one already carries this frame's metadata — so this
2152/// function's contract is unchanged either way, and the CPU frame's
2153/// extent is the nominated surface's.
2154///
2155/// Deliberately does **not** call `av_frame_copy_props`. That FFmpeg
2156/// helper deep-copies AVFrame side data (SEI, mastering display, ICC
2157/// profiles, dynamic HDR, etc.), the metadata dict, and bumps both
2158/// `opaque_ref` and `private_ref` on every receive — none of which
2159/// `Frame` exposes via its public accessors. On a crafted stream with
2160/// megabytes of per-frame metadata that would mean an unbounded
2161/// allocation per receive, with no caller-visible benefit. We instead
2162/// copy only the scalar fields the public API can read (today: `pts`);
2163/// pixel layout (`width`, `height`, `format`, `linesize`, `data`) is
2164/// already set by `av_hwframe_transfer_data`. If `Frame` ever grows
2165/// accessors for timing extras (`duration`, `time_base`, `pkt_dts`) or
2166/// color metadata, add those to `copy_frame_props_minimal` at the same
2167/// time.
2168unsafe fn transfer_hw_frame(
2169 dst: &mut Frame,
2170 src: &frame::Video,
2171) -> std::result::Result<(), ffmpeg_next::Error> {
2172 unsafe {
2173 av_frame_unref(dst.as_inner_mut().as_mut_ptr());
2174 let ret = av_hwframe_transfer_data(dst.as_inner_mut().as_mut_ptr(), src.as_ptr(), 0);
2175 if ret < 0 {
2176 return Err(ffmpeg_next::Error::from(ret));
2177 }
2178 // Validate the post-transfer CPU pix_fmt against the safe `Frame`
2179 // accessor's supported set. FFmpeg picks the destination format
2180 // when `dst.format == AV_PIX_FMT_NONE` on entry (which it always is
2181 // here — `av_frame_unref` clears it) by walking the result of
2182 // `av_hwframe_transfer_get_formats`. Driver/version ordering can
2183 // pick a layout outside our NV*/P0xx/P2xx/P4xx set; the call would
2184 // return success while the resulting frame is unreadable through
2185 // `Frame::row` / `Frame::as_ptr` (those return `None` for
2186 // unsupported formats). Surface the unsupported result as a
2187 // transfer failure so `receive_frame`'s probe-active path advances
2188 // to the next backend rather than collapsing on an unusable frame;
2189 // post-probe, the caller gets an `Err` they can branch into a
2190 // software fallback.
2191 let dst_raw_fmt: i32 = (*dst.as_inner_mut().as_ptr()).format;
2192 let dst_pix_fmt = crate::boundary::from_av_pixel_format(dst_raw_fmt);
2193 if !crate::frame::is_supported_cpu_pix_fmt(&dst_pix_fmt) {
2194 tracing::warn!(
2195 pix_fmt = dst_raw_fmt,
2196 "hwdecode: hw->cpu transfer produced unsupported pix_fmt; \
2197 treating as backend failure"
2198 );
2199 av_frame_unref(dst.as_inner_mut().as_mut_ptr());
2200 return Err(ffmpeg_next::Error::Other {
2201 errno: libc::EINVAL,
2202 });
2203 }
2204 if let Err(e) = copy_frame_props_minimal(dst.as_inner_mut().as_mut_ptr(), src.as_ptr()) {
2205 // Failed to propagate metadata. Reset the destination so the
2206 // partial frame doesn't leak (its pixel buffers were attached
2207 // by `av_hwframe_transfer_data` above) and surface as a
2208 // backend failure — the probe path will advance to the next
2209 // candidate; post-probe, the caller branches into SW fallback.
2210 av_frame_unref(dst.as_inner_mut().as_mut_ptr());
2211 return Err(e);
2212 }
2213 }
2214 Ok(())
2215}
2216
2217/// Copies AVFrame metadata (timestamps, color metadata, crop rect,
2218/// flags, side data, etc.) from the source HW frame to the destination
2219/// CPU frame so the post-transfer frame surfaces the same metadata a
2220/// SW-decoded frame would.
2221///
2222/// Defers to FFmpeg's `av_frame_copy_props`, which handles the per-
2223/// `side_data[i]` allocation, dict copy, and refcounted buffer
2224/// replacements internally. The cost is bounded by what the source
2225/// frame attaches — typical HDR streams carry 1–3 side-data entries
2226/// (mastering display, content light level, dolby/HDR10+ dynamic
2227/// metadata) totalling a few hundred bytes, so per-frame allocation
2228/// overhead stays negligible relative to the pixel data already
2229/// transferred via `av_hwframe_transfer_data`.
2230///
2231/// # Safety
2232/// Both pointers must be valid `AVFrame` pointers we own. We do not
2233/// form `&AVFrame` — `av_frame_copy_props` operates on raw pointers
2234/// directly.
2235/// Sum the byte sizes of every entry in `(*frame).side_data[]`.
2236/// Used by the probe replay queue's byte-cap accounting so a
2237/// frame's deep-copied side-data is charged against
2238/// `max_probe_pending_bytes` along with its pixel buffers.
2239///
2240/// # Safety
2241/// `frame` must be a live `*const AVFrame`. Reads only `nb_side_data`,
2242/// the `side_data` pointer array, and each `AVFrameSideData.size` —
2243/// no `&AVFrame` reference is formed.
2244unsafe fn sum_side_data_bytes(frame: *const AVFrame) -> usize {
2245 // Clamp `nb_side_data` to the same entry cap the copy path
2246 // enforces. Without the clamp, a decoder-controlled or
2247 // version-skew `nb_side_data` value (the bindgen field is
2248 // `c_int`, signed) could drive this walk arbitrarily long
2249 // before the cap downstream kicks in. Negative values are
2250 // pinned to zero before casting.
2251 let raw = unsafe { (*frame).nb_side_data };
2252 let arr = unsafe { (*frame).side_data };
2253 if raw <= 0 || arr.is_null() {
2254 return 0;
2255 }
2256 let count = (raw as usize).min(HW_COPY_SIDE_DATA_MAX_ENTRIES);
2257 let mut total: usize = 0;
2258 for i in 0..count {
2259 // SAFETY: `arr` points to `nb_side_data` valid `*mut AVFrameSideData`
2260 // entries per FFmpeg's contract; `i < count` is in-bounds.
2261 let entry = unsafe { *arr.add(i) };
2262 if entry.is_null() {
2263 continue;
2264 }
2265 let sz = unsafe { (*entry).size };
2266 total = total.saturating_add(sz);
2267 if total >= HW_COPY_SIDE_DATA_MAX_TOTAL_BYTES {
2268 // Already at or above the byte cap — further entries can't
2269 // change the projected-vs-cap decision the caller makes.
2270 total = HW_COPY_SIDE_DATA_MAX_TOTAL_BYTES;
2271 break;
2272 }
2273 }
2274 total
2275}
2276
2277/// Hard cap on the number of `AVFrameSideData` entries we copy from
2278/// HW source frame to CPU destination frame on the HW transfer
2279/// path. Mirrors `convert::SIDE_DATA_MAX_ENTRIES`; the public
2280/// converter re-enforces the same cap so this is defense in depth.
2281pub(crate) const HW_COPY_SIDE_DATA_MAX_ENTRIES: usize = 64;
2282/// Hard cap on the total side-data byte budget per HW transfer.
2283/// Mirrors `convert::SIDE_DATA_MAX_TOTAL_BYTES`.
2284const HW_COPY_SIDE_DATA_MAX_TOTAL_BYTES: usize = 256 * 1024;
2285
2286/// Maps a raw `AV_FRAME_DATA_*` integer to the matching bindgen
2287/// `AVFrameSideDataType` enum value when (and only when) the integer
2288/// is a known discriminant in the linked FFmpeg's bindgen output.
2289/// Returns `None` for unknown / version-skew / corrupt values —
2290/// the caller drops those entries instead of `transmute`-ing an
2291/// arbitrary integer back into the enum (which would be immediate
2292/// UB if the discriminant isn't in the enum's set).
2293///
2294/// The whitelist covers the entries safe to preserve across HW
2295/// transfer:
2296/// - HDR10 / HDR10+ / Dolby Vision / Vivid / ambient HDR metadata
2297/// - SMPTE / GOP timecodes
2298/// - ICC color profile
2299/// - A53 closed captions
2300/// - Spherical / display matrix orientation
2301/// - Stereo3D layout
2302///
2303/// Other AV_FRAME_DATA_* constants exist (motion vectors, encoder
2304/// params, RPU buffers, …) but are either decoder-internal or
2305/// rarely useful through the public mediadecode API; dropping them
2306/// is the safe default.
2307pub(crate) fn whitelisted_side_data_kind(
2308 kind_raw: i32,
2309) -> Option<ffmpeg_next::ffi::AVFrameSideDataType> {
2310 use ffmpeg_next::ffi::AVFrameSideDataType;
2311 // Each match arm compares `kind_raw` against the i32 cast of a
2312 // known constant, then returns the constant itself — we never
2313 // construct the enum from arbitrary integer bytes.
2314 let kind = match kind_raw {
2315 x if x == AVFrameSideDataType::AV_FRAME_DATA_PANSCAN as i32 => {
2316 AVFrameSideDataType::AV_FRAME_DATA_PANSCAN
2317 }
2318 x if x == AVFrameSideDataType::AV_FRAME_DATA_A53_CC as i32 => {
2319 AVFrameSideDataType::AV_FRAME_DATA_A53_CC
2320 }
2321 x if x == AVFrameSideDataType::AV_FRAME_DATA_STEREO3D as i32 => {
2322 AVFrameSideDataType::AV_FRAME_DATA_STEREO3D
2323 }
2324 x if x == AVFrameSideDataType::AV_FRAME_DATA_DISPLAYMATRIX as i32 => {
2325 AVFrameSideDataType::AV_FRAME_DATA_DISPLAYMATRIX
2326 }
2327 x if x == AVFrameSideDataType::AV_FRAME_DATA_AFD as i32 => {
2328 AVFrameSideDataType::AV_FRAME_DATA_AFD
2329 }
2330 x if x == AVFrameSideDataType::AV_FRAME_DATA_MASTERING_DISPLAY_METADATA as i32 => {
2331 AVFrameSideDataType::AV_FRAME_DATA_MASTERING_DISPLAY_METADATA
2332 }
2333 x if x == AVFrameSideDataType::AV_FRAME_DATA_GOP_TIMECODE as i32 => {
2334 AVFrameSideDataType::AV_FRAME_DATA_GOP_TIMECODE
2335 }
2336 x if x == AVFrameSideDataType::AV_FRAME_DATA_SPHERICAL as i32 => {
2337 AVFrameSideDataType::AV_FRAME_DATA_SPHERICAL
2338 }
2339 x if x == AVFrameSideDataType::AV_FRAME_DATA_CONTENT_LIGHT_LEVEL as i32 => {
2340 AVFrameSideDataType::AV_FRAME_DATA_CONTENT_LIGHT_LEVEL
2341 }
2342 x if x == AVFrameSideDataType::AV_FRAME_DATA_ICC_PROFILE as i32 => {
2343 AVFrameSideDataType::AV_FRAME_DATA_ICC_PROFILE
2344 }
2345 x if x == AVFrameSideDataType::AV_FRAME_DATA_S12M_TIMECODE as i32 => {
2346 AVFrameSideDataType::AV_FRAME_DATA_S12M_TIMECODE
2347 }
2348 x if x == AVFrameSideDataType::AV_FRAME_DATA_DYNAMIC_HDR_PLUS as i32 => {
2349 AVFrameSideDataType::AV_FRAME_DATA_DYNAMIC_HDR_PLUS
2350 }
2351 x if x == AVFrameSideDataType::AV_FRAME_DATA_REGIONS_OF_INTEREST as i32 => {
2352 AVFrameSideDataType::AV_FRAME_DATA_REGIONS_OF_INTEREST
2353 }
2354 x if x == AVFrameSideDataType::AV_FRAME_DATA_SEI_UNREGISTERED as i32 => {
2355 AVFrameSideDataType::AV_FRAME_DATA_SEI_UNREGISTERED
2356 }
2357 x if x == AVFrameSideDataType::AV_FRAME_DATA_FILM_GRAIN_PARAMS as i32 => {
2358 AVFrameSideDataType::AV_FRAME_DATA_FILM_GRAIN_PARAMS
2359 }
2360 x if x == AVFrameSideDataType::AV_FRAME_DATA_DOVI_RPU_BUFFER as i32 => {
2361 AVFrameSideDataType::AV_FRAME_DATA_DOVI_RPU_BUFFER
2362 }
2363 x if x == AVFrameSideDataType::AV_FRAME_DATA_DOVI_METADATA as i32 => {
2364 AVFrameSideDataType::AV_FRAME_DATA_DOVI_METADATA
2365 }
2366 x if x == AVFrameSideDataType::AV_FRAME_DATA_DYNAMIC_HDR_VIVID as i32 => {
2367 AVFrameSideDataType::AV_FRAME_DATA_DYNAMIC_HDR_VIVID
2368 }
2369 x if x == AVFrameSideDataType::AV_FRAME_DATA_AMBIENT_VIEWING_ENVIRONMENT as i32 => {
2370 AVFrameSideDataType::AV_FRAME_DATA_AMBIENT_VIEWING_ENVIRONMENT
2371 }
2372 _ => return None,
2373 };
2374 Some(kind)
2375}
2376
2377pub(crate) unsafe fn copy_frame_props_minimal(
2378 dst: *mut AVFrame,
2379 src: *const AVFrame,
2380) -> std::result::Result<(), ffmpeg_next::Error> {
2381 // We deliberately do NOT use `av_frame_copy_props` here, despite
2382 // its convenience. Upstream `av_frame_copy_props` deep-copies
2383 // *every* `AVFrameSideData` entry, the metadata `AVDictionary`,
2384 // and refcounted `opaque_ref` / `private_ref` buffers — all from
2385 // attacker-controlled decoder output. A crafted stream with many
2386 // multi-MiB side-data entries could drive the per-frame
2387 // allocation cost arbitrarily high (one alloc per entry, with the
2388 // entry's bytes copied via `memcpy`). The downstream
2389 // `convert::collect_side_data` cap helps the *Rust* side but the
2390 // FFmpeg-side allocations have already happened.
2391 //
2392 // Instead we copy scalar fields manually (timestamps, color
2393 // metadata, picture type, flags) and copy side-data with a hard
2394 // cap matching the converter's. Metadata dict and opaque_ref /
2395 // private_ref are intentionally NOT copied — they're rarely
2396 // populated on decoded frames and represent unbounded surfaces.
2397 use core::ptr::{addr_of, addr_of_mut, read_unaligned, write_unaligned};
2398 use ffmpeg_next::ffi::av_frame_new_side_data;
2399 unsafe {
2400 // Scalar timestamps / flags / color / SAR / crop. None of
2401 // these allocate.
2402 (*dst).pts = (*src).pts;
2403 (*dst).pkt_dts = (*src).pkt_dts;
2404 (*dst).duration = (*src).duration;
2405 (*dst).best_effort_timestamp = (*src).best_effort_timestamp;
2406 (*dst).quality = (*src).quality;
2407 (*dst).repeat_pict = (*src).repeat_pict;
2408 (*dst).flags = (*src).flags;
2409 (*dst).sample_aspect_ratio = (*src).sample_aspect_ratio;
2410 (*dst).crop_left = (*src).crop_left;
2411 (*dst).crop_top = (*src).crop_top;
2412 (*dst).crop_right = (*src).crop_right;
2413 (*dst).crop_bottom = (*src).crop_bottom;
2414 (*dst).time_base = (*src).time_base;
2415
2416 // Enum-typed fields: bit-copy raw to avoid materializing an
2417 // invalid `AVColorPrimaries` etc. on either side. `read_unaligned`
2418 // / `write_unaligned` on `i32` projections sidestep the bindgen
2419 // enum's discriminant-validity invariant.
2420 let pict_type_raw = read_unaligned(addr_of!((*src).pict_type) as *const i32);
2421 write_unaligned(addr_of_mut!((*dst).pict_type) as *mut i32, pict_type_raw);
2422 let cp_raw = read_unaligned(addr_of!((*src).color_primaries) as *const i32);
2423 write_unaligned(addr_of_mut!((*dst).color_primaries) as *mut i32, cp_raw);
2424 let trc_raw = read_unaligned(addr_of!((*src).color_trc) as *const i32);
2425 write_unaligned(addr_of_mut!((*dst).color_trc) as *mut i32, trc_raw);
2426 let cs_raw = read_unaligned(addr_of!((*src).colorspace) as *const i32);
2427 write_unaligned(addr_of_mut!((*dst).colorspace) as *mut i32, cs_raw);
2428 let cr_raw = read_unaligned(addr_of!((*src).color_range) as *const i32);
2429 write_unaligned(addr_of_mut!((*dst).color_range) as *mut i32, cr_raw);
2430 let cl_raw = read_unaligned(addr_of!((*src).chroma_location) as *const i32);
2431 write_unaligned(addr_of_mut!((*dst).chroma_location) as *mut i32, cl_raw);
2432
2433 // Side-data: bounded copy. `av_frame_new_side_data(dst, type,
2434 // size)` allocates the entry and returns a pointer to write
2435 // the payload bytes into; a null return is the OOM signal.
2436 // Callers (`transfer_hw_frame`, `drain_into_pending`) hand us
2437 // freshly-unref'd `dst` frames, so any prior side-data has
2438 // already been freed by `av_frame_unref` — we don't need to
2439 // strip dst's existing side-data here.
2440 // Read `nb_side_data` as the bindgen `c_int` and clamp non-
2441 // positive values BEFORE casting to `usize`. A negative value
2442 // (corrupt / version-skew decoder output) cast directly to
2443 // `usize` becomes a huge positive count and would walk OOB
2444 // memory below; pinning to zero up front collapses that to a
2445 // no-op. Same signed-count guard `sum_side_data_bytes` applies.
2446 let nb_side_data_raw = (*src).nb_side_data;
2447 let src_arr = (*src).side_data;
2448 if nb_side_data_raw > 0 && !src_arr.is_null() {
2449 let count_raw = nb_side_data_raw as usize;
2450 let count = count_raw.min(HW_COPY_SIDE_DATA_MAX_ENTRIES);
2451 if count_raw > HW_COPY_SIDE_DATA_MAX_ENTRIES {
2452 tracing::warn!(
2453 cap = HW_COPY_SIDE_DATA_MAX_ENTRIES,
2454 requested = count_raw,
2455 "mediadecode-ffmpeg: HW->CPU transfer side-data entry cap reached; truncating",
2456 );
2457 }
2458 let mut total_bytes: usize = 0;
2459 for i in 0..count {
2460 let entry = *src_arr.add(i);
2461 if entry.is_null() {
2462 continue;
2463 }
2464 let kind_raw = read_unaligned(addr_of!((*entry).type_) as *const i32);
2465 let size = (*entry).size;
2466 let data_ptr = (*entry).data;
2467 if size == 0 || data_ptr.is_null() {
2468 continue;
2469 }
2470 // Whitelist gate: only proceed when `kind_raw` matches a
2471 // known `AV_FRAME_DATA_*` constant the linked FFmpeg's
2472 // bindgen output knows about. Without this gate, a
2473 // version-skew or hostile decoder could write a side-data
2474 // type integer outside our bindgen's discriminant set, and
2475 // constructing the `AVFrameSideDataType` enum value (so
2476 // we could pass it to `av_frame_new_side_data`) would be
2477 // immediate UB before the call. Unknown types are dropped
2478 // with a debug-level log — the public converter's
2479 // `collect_side_data` walks the destination raw and would
2480 // also surface them as bare integers in `SideDataEntry.kind`.
2481 let Some(kind_enum) = whitelisted_side_data_kind(kind_raw) else {
2482 tracing::debug!(
2483 kind_raw,
2484 "mediadecode-ffmpeg: unknown AV_FRAME_DATA type during HW->CPU transfer; dropping",
2485 );
2486 continue;
2487 };
2488 let projected = total_bytes.saturating_add(size);
2489 if projected > HW_COPY_SIDE_DATA_MAX_TOTAL_BYTES {
2490 tracing::warn!(
2491 cap = HW_COPY_SIDE_DATA_MAX_TOTAL_BYTES,
2492 projected,
2493 "mediadecode-ffmpeg: HW->CPU transfer side-data byte cap reached; dropping rest",
2494 );
2495 break;
2496 }
2497 let new_entry = av_frame_new_side_data(dst, kind_enum, size);
2498 if new_entry.is_null() {
2499 // **OOM is reported, not absorbed.** This used to `break` and
2500 // return `Ok(())`, which published a frame carrying whatever
2501 // side data happened to fit before the allocator gave out —
2502 // silently dropping the entries behind it. Those entries are
2503 // the HDR mastering metadata, the ICC profile and the display
2504 // matrix: a picture that comes back with its colours or its
2505 // orientation quietly missing is worse than one that does not
2506 // come back, because nothing downstream can tell.
2507 //
2508 // The caller already knows what to do with an error here: it
2509 // unrefs the partial destination and either advances to the
2510 // next backend or surfaces the failure for a software retry.
2511 tracing::warn!("mediadecode-ffmpeg: av_frame_new_side_data OOM during HW->CPU transfer",);
2512 return Err(ffmpeg_next::Error::Other {
2513 errno: libc::ENOMEM,
2514 });
2515 }
2516 // SAFETY: `(*new_entry).data` is allocated for `size` bytes
2517 // per av_frame_new_side_data's contract; `data_ptr` is
2518 // valid for `size` reads per AVFrameSideData's contract.
2519 core::ptr::copy_nonoverlapping(data_ptr, (*new_entry).data, size);
2520 total_bytes = projected;
2521 }
2522 }
2523 }
2524 Ok(())
2525}
2526
2527/// `EAGAIN` and `EOF` together: "this decoder has no more output for
2528/// now", either because it wants input or because it is finished.
2529///
2530/// **What is left of a predicate that used to guard both roads.** Both
2531/// public faces classify at their boundary now — [`receive_status`] and
2532/// [`send_status`] — and the send face had to stop treating the two
2533/// alike, since `AVERROR_EOF` there is a caller fault rather than a
2534/// state. The one caller that still wants them together is
2535/// [`drain_into_pending`], the probe-replay drain: it reads a raw
2536/// `ffmpeg_next::decoder::Video` that never crosses a public seam, and
2537/// for it "wants input" and "finished" really are one answer — stop
2538/// draining, the candidate produced everything it is going to.
2539fn is_transient(e: &ffmpeg_next::Error) -> bool {
2540 is_eagain(e) || matches!(e, ffmpeg_next::Error::Eof)
2541}
2542
2543/// **The receive road's single errno gate, and it cannot be spent
2544/// without saying where the session is.**
2545///
2546/// Turns what a funnel (`software_exit` / `hw_exit`) handed back into
2547/// the trait's status vocabulary, keeping the two flow signals inside
2548/// this crate.
2549///
2550/// Takes the funnel's *output*, never libavcodec's raw error, and that
2551/// ordering is the point: the funnels collect a `get_format` or
2552/// allocator-judge refusal that the callback state is holding, and a
2553/// classifier placed in front of them would answer "needs input" for a
2554/// road that had a named refusal waiting. So every receive site funnels
2555/// first and gates second.
2556///
2557/// # Why the phase is a parameter and not a guess
2558///
2559/// The same errno means different things at different points in a
2560/// session's life, and every road that guessed guessed differently:
2561///
2562/// * `EAGAIN` is [`Received::NeedsInput`] only where more input can
2563/// arrive. Past a recorded end it is an instruction the caller cannot
2564/// carry out — the send gates refuse — so it is the end instead. And
2565/// on a candidate that has already been handed the whole history
2566/// including the end, it is neither: that candidate has produced no
2567/// frame and never will, which is a candidate failing, so it goes
2568/// back as an error for the probe machinery to act on.
2569/// * `AVERROR_EOF` is [`Received::Ended`] only from a backend that has
2570/// committed. A candidate's is its own exhaustion, not the stream's.
2571///
2572/// Making the phase an argument is what stops a road from having an
2573/// opinion about this. A classification without it does not compile.
2574fn receive_status(e: Error, phase: SessionPhase) -> Result<Received> {
2575 match &e {
2576 Error::Ffmpeg(f) if is_eagain(f) => {
2577 if phase.accepts_input() {
2578 Ok(Received::NeedsInput)
2579 } else if phase.is_committed() {
2580 // Draining. A committed backend with nothing more to give has
2581 // ended, whichever errno it chose — libavcodec is not supposed
2582 // to answer `EAGAIN` after a flush packet, but this crate has
2583 // met a codec that does (see `ImageDecodeError::NoImage`), and
2584 // the alternative is handing back a state nothing can satisfy.
2585 Ok(Received::Ended)
2586 } else {
2587 // `AuditioningPastEnd`: a candidate that has been given
2588 // everything and produced nothing. The probe road owns it.
2589 Err(e)
2590 }
2591 }
2592 Error::Ffmpeg(ffmpeg_next::Error::Eof) if phase.is_committed() => Ok(Received::Ended),
2593 _ => Err(e),
2594 }
2595}
2596
2597/// **The send road's gate, and it is deliberately narrower than its
2598/// sibling.** Only `EAGAIN` is back pressure here.
2599///
2600/// `avcodec_send_packet` answers `AVERROR_EOF` for a different fact than
2601/// `avcodec_receive_frame` does: not "the stream is over" but *"this
2602/// decoder has already been told the stream is over, and you sent
2603/// something anyway"* — a caller usage fault rather than a session
2604/// state, so it stays in `Err`. Reading it as `Accepted` would silently
2605/// drop the submission; reading it as `MustDrain` would send the caller
2606/// into a drain loop that can never make the next offer succeed.
2607///
2608/// The same line puts [`crate::ResampleError::AfterEof`] and the
2609/// WebCodecs adapter's `AfterEof` on the error side.
2610///
2611/// # The phase, here too
2612///
2613/// [`Sent::MustDrain`] is a promise — *drain, and this same offer
2614/// becomes acceptable* — and past a recorded end it is one no session
2615/// can keep. The send gates refuse there first, so this is the second
2616/// lock rather than the first; what it buys is that the classifier
2617/// itself becomes incapable of making the promise, which is the whole
2618/// point of moving the phase into the signature.
2619fn send_status(e: Error, phase: SessionPhase) -> Result<Sent> {
2620 match &e {
2621 Error::Ffmpeg(f) if is_eagain(f) && phase.accepts_input() => Ok(Sent::MustDrain),
2622 _ => Err(e),
2623 }
2624}
2625
2626/// Post-commit, a HW-only decoder's non-transient, non-EOF error means the
2627/// committed HW backend can't decode this content → fall back to SW. VT's
2628/// "hardware accelerator failed" surfaces as AVERROR_EXTERNAL; some HW
2629/// backends report unsupported geometry as InvalidData; context loss as
2630/// Bug/Bug2/Unknown. Broad-by-design (decode-all-kinds); fixtures will let us
2631/// narrow if a real backend proves a code should NOT trigger fallback.
2632///
2633/// `EAGAIN`/`EOF` are deliberately excluded by the caller, which guards on
2634/// them first — on the send roads through [`is_transient`] into
2635/// [`send_status`], and on `receive_frame` through [`is_eagain`] into
2636/// [`receive_status`], plus the probe/`hw_exit` road for `EOF`. `EAGAIN` is back pressure and `EOF` is a
2637/// genuine end-of-stream that must reach the caller as
2638/// [`Received::Ended`], never be trapped in an infinite fallback-retry
2639/// loop. `Other { errno: EINVAL }` from the HW→CPU transfer path is also
2640/// covered — an unsupported CPU output pix_fmt is a HW-output problem,
2641/// never input corruption.
2642fn is_hw_decode_failure(e: &ffmpeg_next::Error) -> bool {
2643 matches!(
2644 e,
2645 ffmpeg_next::Error::External
2646 | ffmpeg_next::Error::Bug
2647 | ffmpeg_next::Error::Bug2
2648 | ffmpeg_next::Error::Unknown
2649 | ffmpeg_next::Error::InvalidData
2650 | ffmpeg_next::Error::Other {
2651 errno: libc::EINVAL
2652 }
2653 )
2654}
2655
2656/// Reject a `codec::Parameters` whose inner `*mut AVCodecParameters` is
2657/// null. This guards the public trust boundary: ffmpeg-next can produce
2658/// such a `Parameters` under OOM (`Parameters::new()` does not check
2659/// `avcodec_parameters_alloc`), and a safe caller can legally hand one
2660/// in. Without this check, the very next `(*p.as_ptr()).field` read
2661/// would be a null deref.
2662fn ensure_parameters_non_null(parameters: &codec::Parameters) -> Result<()> {
2663 // SAFETY: as_ptr() returns the inner *const AVCodecParameters; we just
2664 // inspect the pointer value (no deref).
2665 if unsafe { parameters.as_ptr() }.is_null() {
2666 return Err(Error::Ffmpeg(ffmpeg_next::Error::Other {
2667 errno: libc::ENOMEM,
2668 }));
2669 }
2670 Ok(())
2671}
2672
2673/// Allocate a fresh `frame::Video`, checking that `av_frame_alloc` did not
2674/// return NULL. ffmpeg-next's `frame::Video::empty()` does not surface that
2675/// failure and the resulting null pointer would be UB on the next field
2676/// access; this wrapper catches it and surfaces it as `ENOMEM`.
2677fn alloc_av_frame() -> std::result::Result<frame::Video, ffmpeg_next::Error> {
2678 let inner = frame::Video::empty();
2679 // SAFETY: as_ptr() just exposes the inner pointer for inspection.
2680 if unsafe { inner.as_ptr() }.is_null() {
2681 return Err(ffmpeg_next::Error::Other {
2682 errno: libc::ENOMEM,
2683 });
2684 }
2685 Ok(inner)
2686}
2687
2688/// Build a fresh `Context` from `parameters`, checking the underlying
2689/// `avcodec_alloc_context3` for NULL before passing it to
2690/// `avcodec_parameters_to_context`. ffmpeg-next's `Context::from_parameters`
2691/// skips that check and would feed a null pointer into FFmpeg under OOM —
2692/// undefined behavior. This helper surfaces the failure as `ENOMEM` and
2693/// frees the context if `parameters_to_context` itself errors.
2694pub(crate) fn build_codec_context(
2695 parameters: &codec::Parameters,
2696 limits: crate::limits::DecoderLimits,
2697 pkt_timebase: Option<mediadecode::Timebase>,
2698) -> Result<(Context, Box<CallbackState>)> {
2699 ensure_parameters_non_null(parameters)?;
2700 // **The choke point.** `avcodec_parameters_to_context` below is a
2701 // wholesale copy *into* FFmpeg — it duplicates `extradata`, every
2702 // `coded_side_data` entry and the channel map into the context, at
2703 // whatever size the caller's parameters declare. Every road that
2704 // opens a decoder in this crate arrives here, so measuring and
2705 // admitting once, right here, is what stops a caller handing
2706 // libavcodec parameters nobody budgeted: the four session `open`s,
2707 // the HW probe's `build_state`, its per-backend advances, and the
2708 // software fallback all pass through this function and none of them
2709 // can reach `avcodec_parameters_to_context` any other way.
2710 //
2711 // The outbound clone (`extras::bounded_clone_parameters`) closed the
2712 // Rust-side copy; this closes the FFmpeg-side one. They are the same
2713 // budget.
2714 //
2715 // **Structure before size, and before FFmpeg is handed anything.**
2716 //
2717 // `measure_parameters` below prices a custom channel map from
2718 // `nb_channels` alone and never looks at `u.map`, so a layout with a
2719 // positive count and a null map passed this budget and went on to
2720 // `avcodec_parameters_to_context`, which reaches
2721 // `av_channel_layout_copy` — a `memcpy` from that null. The
2722 // non-custom orders were no better: nothing bounded `nb_channels` for
2723 // a `NATIVE` or `AMBISONIC` layout, and FFmpeg's own arithmetic over
2724 // it assumes invariants it does not check.
2725 //
2726 // Every decoder this crate opens arrives here, and the refusal is
2727 // structural rather than an allocation failure, so it carries its own
2728 // error rather than the ceiling's or the allocator's.
2729 //
2730 // SAFETY: `ensure_parameters_non_null` just proved the pointer is
2731 // live; for a custom order its map is FFmpeg's own allocation of
2732 // `nb_channels` entries, and the preflight allocates nothing.
2733 unsafe {
2734 crate::channel_layout::layout_preflight(ptr::addr_of!((*parameters.as_ptr()).ch_layout))
2735 }
2736 .map_err(
2737 |fault| match crate::demuxer::layout_fault_to_demux(0, fault) {
2738 crate::demuxer::DemuxError::ParametersLayoutShape(shape) => {
2739 Error::MalformedChannelLayout(shape)
2740 }
2741 crate::demuxer::DemuxError::ParametersChannelMap(map) => Error::ChannelMapMissing(map),
2742 // The preflight allocates nothing, so no other arm is reachable;
2743 // reporting the allocator keeps the match total without an
2744 // `unreachable!`.
2745 _ => Error::Ffmpeg(ffmpeg_next::Error::Other {
2746 errno: libc::ENOMEM,
2747 }),
2748 },
2749 )?;
2750
2751 // SAFETY: `ensure_parameters_non_null` just proved the pointer is
2752 // live; the measurement allocates nothing.
2753 let footprint = unsafe { crate::extras::measure_parameters(parameters.as_ptr()) };
2754 let declared = footprint.and_then(|f| f.total()).unwrap_or(usize::MAX);
2755 if declared > limits.max_codec_parameter_bytes() {
2756 return Err(Error::ParametersTooLarge(
2757 crate::demuxer::ParametersTooLarge::new(0, declared, limits.max_codec_parameter_bytes()),
2758 ));
2759 }
2760 // SAFETY: avcodec_alloc_context3(NULL) returns a fresh AVCodecContext
2761 // or NULL on allocation failure.
2762 let ctx_ptr = unsafe { avcodec_alloc_context3(ptr::null()) };
2763 if ctx_ptr.is_null() {
2764 return Err(Error::Ffmpeg(ffmpeg_next::Error::Other {
2765 errno: libc::ENOMEM,
2766 }));
2767 }
2768 // SAFETY: ctx_ptr is non-null and freshly allocated; parameters.as_ptr()
2769 // returns a valid AVCodecParameters pointer; the function copies bytes
2770 // out of parameters into the context.
2771 let ret = unsafe { avcodec_parameters_to_context(ctx_ptr, parameters.as_ptr()) };
2772 if ret < 0 {
2773 // SAFETY: ctx_ptr was allocated by us and never handed to anyone else.
2774 let mut p = ctx_ptr;
2775 unsafe { avcodec_free_context(&mut p) };
2776 return Err(Error::Ffmpeg(ffmpeg_next::Error::from(ret)));
2777 }
2778 // **The push-down.** The same pixel ceiling this crate checks against
2779 // a decoded frame is written into the decoder itself, so libavcodec
2780 // refuses an oversized picture *before allocating it*. Checking only
2781 // on our side would mean FFmpeg had already paid for the frame by the
2782 // time we declined to copy it — two layers, one number, and this is
2783 // the layer that matters.
2784 //
2785 // FFmpeg's own default here is `INT_MAX`, i.e. no ceiling worth the
2786 // name. `max_pixels` is a plain `int64_t` field on `AVCodecContext`
2787 // (and has been since FFmpeg 4.0), so it is set directly rather than
2788 // through `av_opt_set_int` and a stringly-typed option name.
2789 //
2790 // **And the byte ceiling, pushed down through the same field.**
2791 //
2792 // The pixel ceiling alone does not bound bytes, because a pixel is not
2793 // a fixed price: 10000x10000 is 100 Mpx — comfortably under the 256
2794 // Mpx default — and in `rgba64` it is 800 MB, well over the 512 MiB
2795 // byte ceiling. A highly compressible frame of that shape is a few KB
2796 // on disk, so nothing upstream sees it coming.
2797 //
2798 // **`max_pixels` carries the caller's number, verbatim.** It used to
2799 // carry `min(that, max_frame_bytes / worst-bytes-per-pixel)`, so the
2800 // byte ceiling could be enforced before libavcodec allocated — and
2801 // that translation charged every stream the widest format in
2802 // existence, 16 bytes a pixel. A 1920x1080 `yuv420p` frame costs
2803 // 3.14 MiB and was refused under a 4 MiB budget, at
2804 // `ff_set_dimensions`, before anything accurate had a chance to look
2805 // at it. Over-refusing ordinary video is not a conservative failure;
2806 // it is a broken decoder.
2807 //
2808 // The translation is gone because it is no longer needed: the byte
2809 // ceiling is enforced by [`judge_buffer`], which is *also* a
2810 // pre-allocation seat — `get_buffer2` is the allocator, so it runs
2811 // before the allocation and prices the frame's real format at its
2812 // real aligned dimensions. Nothing is lost on the software road by
2813 // stating the pixel limit as what it is.
2814 //
2815 // SAFETY: `ctx_ptr` is the non-null context just allocated and
2816 // populated above; `max_pixels` is a public field.
2817 unsafe {
2818 (*ctx_ptr).max_pixels = i64::try_from(limits.frame().max_pixels()).unwrap_or(i64::MAX);
2819 }
2820
2821 // **The byte ceiling's own seat, in the allocator itself.**
2822 // `max_pixels` bounds an extent; what an extent costs depends on its
2823 // format and on how the allocator aligns it — a `gray8` frame of
2824 // 65536x1 is 64 KiB by `w * h` and 2 MiB once its single row is
2825 // rounded up. No scalar compared against a pixel product can bound
2826 // that, so the byte question is asked where the answer is knowable:
2827 // in `get_buffer2`, which *is* the allocation, against the caller's
2828 // own `max_frame_bytes`.
2829 //
2830 // See [`judge_buffer`] for why this hook rather than `get_format`
2831 // (measured: `get_format` never fires for a one-shot `png` decode).
2832 //
2833 // SAFETY: `ctx_ptr` is the non-null context; `get_buffer2` is a
2834 // public function-pointer field, and `judge_buffer` delegates every
2835 // frame it accepts to the allocator libavcodec would have used.
2836 unsafe {
2837 (*ctx_ptr).get_buffer2 = Some(judge_buffer);
2838 }
2839
2840 // **The packet timebase, for every decoder opened against a stream.**
2841 //
2842 // `AVCodecContext.pkt_timebase` is documented as caller-supplied, and
2843 // libavcodec reads it: its generic subtitle path derives
2844 // `AVSubtitle.pts` and the packet-duration fallback *only* when the
2845 // field is set, so a subtitle decoder opened without it returns
2846 // timestamped cues carrying neither. It was never written here, on
2847 // any road.
2848 //
2849 // **A zero numerator is deliberately not written.** Left alone the
2850 // field is `0/1`, and `0/1` is also exactly what a container that
2851 // declared no timebase hands this crate — the two are the same
2852 // statement, so writing it would be ceremony, and treating it as a
2853 // real ruler would claim one the file never gave. See
2854 // [`crate::demuxer`]'s own note on why that value is passed through
2855 // rather than refused on the track road.
2856 if let Some(timebase) = pkt_timebase.filter(|timebase| timebase.num() > 0) {
2857 // SAFETY: `ctx_ptr` is the non-null context allocated above and
2858 // `pkt_timebase` is a public field; the value is built from a
2859 // `Timebase`, which cannot be negative or zero-denominatored.
2860 unsafe {
2861 (*ctx_ptr).pkt_timebase =
2862 ffmpeg_next::Rational::new(timebase.num(), timebase.den().get()).into();
2863 }
2864 }
2865
2866 // **`max_samples` is deliberately left alone.**
2867 //
2868 // It bounds `nb_samples * channels`, so bounding *bytes* with it
2869 // means dividing by a per-channel-sample cost — and the only sound
2870 // divisor is the widest sample format the build can emit, 8 bytes.
2871 // That charged every stream `f64` rates: a 6-channel `s16` frame
2872 // fitting a 64 KiB budget was refused, because the translation
2873 // priced it at four times its real cost.
2874 //
2875 // The audio pre-allocation story is now the same as the video one,
2876 // and it is stronger than the translation was: [`judge_buffer`] runs
2877 // in `get_buffer2`, before the planes are allocated, and prices the
2878 // frame's real sample format at its real channel count through
2879 // [`crate::footprint`] — which asks `av_samples_get_buffer_size`, the
2880 // allocator's own ruler. An exact judge at the allocation beats an
2881 // approximate one before it.
2882
2883 // **The judge's budget seat.** `judge_buffer` runs as a C callback
2884 // with nothing but the context to read, and the byte ceiling is not
2885 // recoverable from any field on it — see
2886 // [`CallbackState::max_frame_bytes`]. So the state that already
2887 // carries the `get_format` declination carries the budget too, and
2888 // every road gets one: this is the single point every decoder in the
2889 // crate is built through.
2890 //
2891 // Ownership stays with the caller, which keeps the box alive for as
2892 // long as the context. `Box` contents do not move when the box does,
2893 // so the pointer installed here stays valid across the return.
2894 let mut state = Box::new(CallbackState {
2895 wanted: ffmpeg_next::ffi::AVPixelFormat::AV_PIX_FMT_NONE,
2896 wanted_int: ffmpeg_next::ffi::AVPixelFormat::AV_PIX_FMT_NONE as i32,
2897 ceiling_declined: core::sync::atomic::AtomicBool::new(false),
2898 declined_pixels: core::sync::atomic::AtomicI64::new(0),
2899 declined_limit: core::sync::atomic::AtomicI64::new(0),
2900 max_frame_bytes: limits.frame().max_frame_bytes() as u64,
2901 frame_budget_declined: core::sync::atomic::AtomicBool::new(false),
2902 declined_frame_bytes: core::sync::atomic::AtomicU64::new(0),
2903 declined_frame_audio: core::sync::atomic::AtomicBool::new(false),
2904 });
2905 // SAFETY: `ctx_ptr` is the non-null context; `opaque` is a public
2906 // field FFmpeg never reads or frees.
2907 unsafe {
2908 (*ctx_ptr).opaque = (&raw mut *state).cast();
2909 }
2910
2911 // SAFETY: ctx_ptr is valid; passing `owner: None` means our wrapper owns
2912 // the allocation and `Context::drop` will run `avcodec_free_context`.
2913 Ok((unsafe { Context::wrap(ctx_ptr, None) }, state))
2914}
2915
2916/// Checked deep-clone of `codec::Parameters`. ffmpeg-next's
2917/// `Parameters::clone` allocates via `avcodec_parameters_alloc` without
2918/// checking for NULL and runs `avcodec_parameters_copy` without checking
2919/// the return code. On `ENOMEM` the result is a `Parameters` with a null
2920/// inner pointer, which becomes UB when later passed to FFmpeg.
2921///
2922/// This helper performs both calls explicitly, frees a partial allocation
2923/// on failure, and surfaces the AVERROR. The returned `Parameters` has
2924/// `owner: None`, severing any Rc link to the caller's demuxer (the
2925/// reason we deep-clone in the first place — see Send safety in
2926/// `VideoDecoder::open`).
2927pub(crate) fn try_clone_parameters(
2928 src: &codec::Parameters,
2929 budget: usize,
2930) -> std::result::Result<codec::Parameters, Error> {
2931 // Through the bounded clone, like every other parameter copy in this
2932 // crate — see [`crate::extras::bounded_clone_parameters`] for the
2933 // rule and why the wholesale `avcodec_parameters_copy` this used to
2934 // call is gone. This path is attacker-facing: `VideoDecoder::open`
2935 // takes whatever `stream.parameters()` hands it, straight off a
2936 // container.
2937 //
2938 // `budget` is the **active** ceiling, threaded from the session's own
2939 // `DecoderLimits` — through the initial ownership clone, the probe
2940 // state's copy, every probe advance and the software fallback. It
2941 // used to be the crate default, so a lowered ceiling did not bind
2942 // here (the clone admitted 16 MiB whatever the caller configured,
2943 // and only `build_codec_context` downstream refused) and a raised one
2944 // could not be used at all.
2945 //
2946 // The stream index is reported as 0: this helper is handed
2947 // parameters, not a stream, and inventing a coordinate it cannot
2948 // know would be worse than admitting it has none.
2949 crate::extras::bounded_clone_parameters(src, 0, budget).map_err(|e| match e {
2950 crate::demuxer::DemuxError::ParametersTooLarge(p) => Error::ParametersTooLarge(p),
2951 crate::demuxer::DemuxError::ParametersCopy(p) => Error::Ffmpeg(*p.source()),
2952 // A missing or unallocatable destination is the out-of-memory this
2953 // helper has always reported.
2954 _ => Error::Ffmpeg(ffmpeg_next::Error::Other {
2955 errno: libc::ENOMEM,
2956 }),
2957 })
2958}
2959
2960/// Checked counterpart to `Packet::clone()`. ffmpeg-next's `clone_from`
2961/// calls `av_packet_ref` and ignores the int return value; on `ENOMEM`
2962/// the destination is left empty while the caller assumes the clone
2963/// succeeded — corrupting any later replay history. This helper surfaces
2964/// the AVERROR. The result is a refcounted shallow clone — the payload
2965/// buffer is shared with `src` rather than deep-copied; the probe replay
2966/// only sends packets through `avcodec_send_packet`, which does not
2967/// require a writable buffer.
2968pub(crate) fn try_clone_packet(src: &Packet) -> std::result::Result<Packet, ffmpeg_next::Error> {
2969 let mut dst = Packet::empty();
2970 // SAFETY: dst is a freshly zero-initialized Packet (av_init_packet inside
2971 // Packet::empty); av_packet_ref initializes its data fields from src's
2972 // refcounted buffer or returns AVERROR(ENOMEM) on failure.
2973 let ret = unsafe { av_packet_ref(dst.as_mut_ptr(), src.as_ptr()) };
2974 if ret < 0 {
2975 return Err(ffmpeg_next::Error::from(ret));
2976 }
2977 Ok(dst)
2978}
2979
2980/// Sum of `AVPacket.side_data[i].size` across every entry, plus
2981/// `nb_entries * SIDE_DATA_ENTRY_OVERHEAD` (descriptor + AVBufferRef +
2982/// allocator bookkeeping per entry). `av_packet_ref` performs a deep
2983/// copy of side data via `av_packet_copy_props`, so each probe-buffered
2984/// clone retains every one of these bytes. Charging both keeps
2985/// `MAX_PROBE_PACKET_BYTES` a true upper bound — without the overhead,
2986/// many zero-size entries slip past the cap on pure descriptor cost.
2987///
2988/// Walks at most `max_entries` entries even when `side_data_elems`
2989/// reports a larger count. Defense-in-depth against a corrupt or hostile
2990/// packet whose `side_data_elems` lies about the actual array length:
2991/// the caller is expected to also reject any packet whose count exceeds
2992/// the cap (so the inflated clone is never created), but bounding the
2993/// walk here means a stale or weaponised value can never trigger an
2994/// unbounded raw-pointer scan from the safe API.
2995///
2996/// Reads only the `size` field of each `AVPacketSideData` entry — never
2997/// touches the bindgen `AVPacketSideDataType` enum, so no UB even if a
2998/// future FFmpeg adds a side-data type discriminant our build doesn't
2999/// know.
3000pub(crate) fn packet_side_data_bytes(packet: &Packet, max_entries: usize) -> usize {
3001 // SAFETY: AVPacket.side_data is `*mut AVPacketSideData` and
3002 // side_data_elems is `c_int`; both are raw struct fields safe to read.
3003 // Field projection (`.size`) does not reconstruct the enum-typed `type_`
3004 // field, so the bindgen-enum UB hazard does not apply here.
3005 unsafe {
3006 let raw = packet.as_ptr();
3007 let nel = (*raw).side_data_elems;
3008 let arr = (*raw).side_data;
3009 if arr.is_null() || nel <= 0 || max_entries == 0 {
3010 return 0;
3011 }
3012 let count = (nel as usize).min(max_entries);
3013 let mut total = count.saturating_mul(SIDE_DATA_ENTRY_OVERHEAD);
3014 for i in 0..count {
3015 let entry = arr.add(i);
3016 total = total.saturating_add((*entry).size);
3017 }
3018 total
3019 }
3020}
3021
3022/// Number of `AVPacketSideData` entries on `packet`. The probe buffer
3023/// uses this to enforce [`MAX_PROBE_PACKET_SIDE_DATA_ENTRIES`] before
3024/// cloning, so a packet whose entry count alone would dominate retained
3025/// memory is rejected up front.
3026pub(crate) fn packet_side_data_count(packet: &Packet) -> usize {
3027 // SAFETY: side_data_elems is `c_int`, safe to read; clamp negatives to 0.
3028 let nel = unsafe { (*packet.as_ptr()).side_data_elems };
3029 if nel <= 0 { 0 } else { nel as usize }
3030}
3031
3032/// Just `EAGAIN` (separate from EOF — the FFmpeg send/receive state machine
3033/// distinguishes "drain output and retry" from "stream over").
3034fn is_eagain(e: &ffmpeg_next::Error) -> bool {
3035 matches!(e, ffmpeg_next::Error::Other { errno } if *errno == ffmpeg_next::error::EAGAIN)
3036}
3037
3038/// The probe square the per-pixel cost is measured on.
3039///
3040/// 256 divides every chroma subsampling FFmpeg has **and** every
3041/// alignment libavcodec uses, so the measurement is exact: no plane is
3042/// rounded up to cover a half-sized dimension, and no row is padded to
3043/// an alignment boundary. Measured at 257 the same census reads 16.934
3044/// bytes per pixel instead of 16.000 — that 5.8% is per-*row* padding,
3045/// a term linear in height rather than in pixels, and it is not part of
3046/// the per-pixel rate.
3047pub(crate) const PROBE_PIXELS: usize = 256 * 256;
3048
3049/// Bytes a [`PROBE_PIXELS`]-pixel picture costs in the **most expensive
3050/// pixel format this build of libavcodec can describe**.
3051///
3052/// # Why the worst case and not the declared one
3053///
3054/// The first cut of this ceiling charged the format the *container*
3055/// declared, and a container's declaration is not an upper bound on
3056/// anything. It may be unset, it may be wrong, and it may be narrower
3057/// than what the decoder actually emits — a stream declaring `yuv420p`
3058/// at 1.5 bytes per pixel whose decoder outputs `rgbaf32` at 16 got a
3059/// ceiling more than ten times too generous, which is the same hole one
3060/// layer down from the one it was added to close.
3061///
3062/// So the rate is not negotiated with the file at all. Every stream is
3063/// charged the worst case, and the worst case is **measured**, not
3064/// tabulated: this build's descriptor list is walked once and each
3065/// format priced through `av_image_get_buffer_size`, the same function
3066/// `avcodec_default_get_buffer2` sizes from. A future FFmpeg that adds
3067/// a wider format is priced correctly without this crate learning its
3068/// name.
3069///
3070/// # The census, at the time of writing
3071///
3072/// 267 descriptors, 251 of them CPU formats that price (the rest are
3073/// hardware surfaces, which carry no CPU bytes and return no size). The
3074/// maximum is **16.000 bytes per pixel**, reached by eight formats —
3075/// `gbrapf32be/le`, `rgbaf32be/le`, `rgba128be/le`, `gbrap32be/le`.
3076/// Next below are the 12-byte `gbrpf32`/`rgbf32` family.
3077///
3078/// # What this trades
3079///
3080/// Over-refusal for cheap formats, and it is deliberate. At the 512 MiB
3081/// default the effective ceiling becomes ~33.55 Mpx, so 8K (33.18 Mpx)
3082/// still decodes in *any* format — including the 16-byte ones, where it
3083/// really does cost 506 MiB — but a 16K `yuv420p` frame, which would
3084/// only have cost 199 MB, is refused too. That is the honest shape of a
3085/// bound that has to hold before the format is known: the deployment
3086/// answer is to raise `max_frame_bytes`, which is exactly the knob that
3087/// says how much memory one frame may cost.
3088///
3089/// # The residual, stated
3090///
3091/// Row alignment adds at most `align x planes x height` bytes on top of
3092/// this rate — about 1 MB on an 8K frame, 0.2%, and covered by the fact
3093/// that `max_frame_bytes` is a policy number rather than a hardware
3094/// limit. It is only significant for degenerate aspect ratios (a
3095/// one-pixel-wide frame is all padding), which the *pixel* ceiling has
3096/// always been the wrong shape to bound and which this change neither
3097/// introduces nor worsens.
3098pub(crate) fn worst_bytes_per_probe() -> usize {
3099 /// The census result, taken once. `av_pix_fmt_desc_next` walks a
3100 /// static table that cannot change during the process.
3101 static WORST: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
3102 *WORST.get_or_init(|| {
3103 /// The measured maximum at the time of writing, and the floor this
3104 /// census may not fall below. A build whose census comes back
3105 /// *smaller* than the eight 16-byte formats has failed to walk the
3106 /// table, not discovered a cheaper world — take the known number
3107 /// rather than a ceiling built on a failed measurement.
3108 const KNOWN_WORST_BYTES_PER_PIXEL: usize = 16;
3109
3110 let mut worst = 0usize;
3111 let mut desc: *const ffmpeg_next::ffi::AVPixFmtDescriptor = ptr::null();
3112 loop {
3113 // SAFETY: `av_pix_fmt_desc_next` walks libavutil's own static
3114 // descriptor table, taking the previous entry (or null to start)
3115 // and returning null at the end. It traffics in descriptor
3116 // pointers, not enums, so it needs no shim.
3117 desc = unsafe { ffmpeg_next::ffi::av_pix_fmt_desc_next(desc) };
3118 if desc.is_null() {
3119 break;
3120 }
3121 // **Both of these go through the `c_int` shims**, and this is the
3122 // place it matters most: the whole point of walking the table is
3123 // to price formats this build's bindings may not name, and the
3124 // generated `av_pix_fmt_desc_get_id` hands those ids back as a
3125 // closed `AVPixelFormat`. Every future format would have become
3126 // an invalid enum value on the way into the pricing meant to
3127 // handle it — the census would have been UB on exactly its reason
3128 // for existing.
3129 //
3130 // SAFETY: `desc` is a live entry from libavutil's static table;
3131 // the id is passed straight back to libavutil as the integer it
3132 // is, and `av_image_get_buffer_size` returns a negative AVERROR
3133 // for ids it cannot size rather than misbehaving.
3134 let id = unsafe { c_shims::av_pix_fmt_desc_get_id(desc) };
3135 let size = unsafe { c_shims::av_image_get_buffer_size(id, 256, 256, 1) };
3136 if size > 0 {
3137 worst = worst.max(size as usize);
3138 }
3139 }
3140 worst.max(KNOWN_WORST_BYTES_PER_PIXEL * PROBE_PIXELS)
3141 })
3142}
3143/// `AVCodecContext.get_buffer2`: the same pixel ceiling, applied where
3144/// the **aligned** dimensions are knowable.
3145///
3146/// # The hole this closes
3147///
3148/// `max_pixels` is checked by libavcodec against the frame's *raw*
3149/// `width * height`. What it then allocates is the **aligned** shape —
3150/// `avcodec_align_dimensions2` rounds both dimensions up to whatever
3151/// the codec and the CPU want — and for degenerate aspect ratios those
3152/// are not the same number at all. Measured on this build:
3153///
3154/// | shape | raw | aligned | inflation |
3155/// |---|---|---|---|
3156/// | `gray8` 65536x1 | 65,536 px / 64 KiB | 65536x32 = 2,097,152 px / 2 MiB | **32x** |
3157/// | `gray8` 1x65536 | 65,536 px / 64 KiB | 16x65536 = 1,048,576 px / 2 MiB | 16x |
3158/// | `yuv420p` 7680x4320 | 33,177,600 px | 7680x4320 | 1.00x |
3159/// | `gray8` 1024x1024 | 1,048,576 px | 1024x1024 | 1.00x |
3160///
3161/// So a one-pixel-tall frame slips 32 times its declared cost past a
3162/// scalar compared against `w * h`, and no value of that scalar can fix
3163/// it: bounding the product cannot bound a product whose factors are
3164/// then rounded up independently. Real pictures inflate by nothing at
3165/// all, which is why the ceiling looked sound.
3166///
3167/// # Why this hook and not `get_format`
3168///
3169/// `get_format` was measured first, because it needs no allocation
3170/// decision and receives the context. It **does not fire on every
3171/// road**: on this build a one-shot `mjpeg` decode calls it once and a
3172/// `png` decode calls it *zero* times. Cover art is overwhelmingly
3173/// mjpeg or png, so half the road this ceiling exists to guard would
3174/// have been unguarded.
3175///
3176/// `get_buffer2` fired on both — it is the allocator, so every frame
3177/// libavcodec hands back comes through it, and it sees the frame's
3178/// *real* format rather than a negotiated candidate.
3179///
3180/// # No state, so no lifetime to prove
3181///
3182/// The composed-`opaque` design was not needed. This callback reads the
3183/// ceiling from `AVCodecContext.max_pixels` — the field this crate set
3184/// itself, one number, already carrying the byte ceiling converted at
3185/// the worst per-pixel rate — and applies it to the aligned dimensions.
3186/// Same scalar, same meaning, applied where alignment is knowable.
3187/// `opaque` is untouched, so the hardware path keeps it and there is no
3188/// allocation whose lifetime has to outlive a C callback.
3189///
3190/// Panic discipline is likewise structural rather than asserted: the
3191/// body allocates nothing, indexes nothing, unwraps nothing, and calls
3192/// exactly three FFmpeg functions. There is no Rust operation in it
3193/// that can panic, and an `extern "C"` function aborts rather than
3194/// unwinding into C in any case.
3195///
3196/// # Safety
3197///
3198/// Called by libavcodec with a live context and a frame whose `format`,
3199/// `width` and `height` are set. Delegates every accepted frame to
3200/// `avcodec_default_get_buffer2`, which is what libavcodec would have
3201/// called had this hook not been installed.
3202unsafe extern "C" fn judge_buffer(
3203 ctx: *mut ffmpeg_next::ffi::AVCodecContext,
3204 frame: *mut ffmpeg_next::ffi::AVFrame,
3205 flags: libc::c_int,
3206) -> libc::c_int {
3207 // SAFETY: libavcodec passes a live context and frame; both fields are
3208 // plain integers.
3209 let (width, height) = unsafe { ((*frame).width, (*frame).height) };
3210
3211 // **This seat judges cost, and only cost.**
3212 //
3213 // `max_pixels` is a *logical* limit on a picture's extent, and
3214 // libavcodec already enforces it — against the **raw** dimensions, in
3215 // `ff_set_dimensions` via `av_image_check_size2`, before any frame
3216 // exists. That is the semantics the caller asked for and the
3217 // semantics FFmpeg documents, and this callback does not restate it.
3218 //
3219 // It used to. R11 added an *aligned*-dimension comparison here
3220 // against `max_pixels`, because at the time the callback had no
3221 // accurate byte check and a degenerate shape could slip its real cost
3222 // past a raw-pixel gate — 65536x1 aligns to 65536x32, thirty-two
3223 // times the pixels. That instrument is now both **redundant** and
3224 // **wrong**:
3225 //
3226 // * redundant, because since the byte ceiling was threaded in the
3227 // footprint below prices the aligned dimensions itself, so the
3228 // degenerate shape is refused on its actual cost; and
3229 // * wrong, because `max_pixels` is `min(the caller's pixel limit,
3230 // byte ceiling / worst-bytes-per-pixel)` — so when the caller's
3231 // pixel limit was the tighter seat, alignment inflation alone
3232 // refused frames satisfying *both* requested limits. A 65536x1
3233 // `gray8` frame under `max_pixels = 65536` and a generous byte
3234 // budget fits the pixel limit exactly and costs 2 MiB, and was
3235 // refused anyway — for arithmetic the caller never asked about.
3236 //
3237 // Logical extent is libavcodec's gate on raw dimensions; allocation
3238 // cost is this one, against the caller's own `max_frame_bytes`. One
3239 // question each.
3240 //
3241 // Audio reaches here too, and used to pass unpriced entirely:
3242 // `max_samples` bounds the sample *count*, so one sample across eight
3243 // packed `f64` channels is 64 valid bytes under a 64-byte ceiling and
3244 // a 2,080-byte allocation — delivered, because the copy-out only ever
3245 // rechecks the valid bytes.
3246 //
3247 // SAFETY: `ctx` and `frame` are live; every field read is a plain
3248 // integer, and `format` stays an integer throughout.
3249 // SAFETY: `frame` is live; the field is a plain pointer.
3250 let hw_frames = unsafe { (*frame).hw_frames_ctx };
3251
3252 // A hardware frame carries no CPU bytes for this seat to price — its
3253 // pool is judged where it is declared, in the `get_format` callback —
3254 // so it is delegated rather than failed closed on an unpriceable
3255 // format.
3256 if hw_frames.is_null() {
3257 // **The caller's own number, read from the seat that carries it.**
3258 // This used to recover a byte ceiling from `AVCodecContext.max_pixels`,
3259 // and the recovery was wrong in both directions:
3260 //
3261 // * `max_pixels` is `min(pixel ceiling, byte ceiling / worst)`, so
3262 // when the *pixel* seat was the tighter of the two it stopped
3263 // encoding the byte ceiling at all — and the recovery invented a
3264 // smaller one. A 256x256 frame at 16 bytes a pixel under
3265 // `max_pixels = 65536` with a 2 MiB byte budget satisfies both of
3266 // the caller's limits, costs 1,050,624 bytes, and was judged
3267 // against 1,048,576 and refused. The claim that the conflation
3268 // was harmless in one direction was simply wrong: it omitted the
3269 // footprint's own alignment and slack, which is exactly where
3270 // those extra 2,048 bytes live.
3271 // * and for audio a pixel ceiling has no business being consulted
3272 // at all.
3273 //
3274 // The audio road briefly recovered from `max_samples` instead,
3275 // which *is* exact — but two sources of truth for one number is how
3276 // the first one went wrong. Both media read the seat now.
3277 //
3278 // SAFETY: `opaque` holds the `CallbackState` that
3279 // `build_codec_context` installed and whose owner outlives the
3280 // context. A null one means a context this crate did not build, and
3281 // is refused rather than assumed generous.
3282 let state = unsafe { (*ctx).opaque } as *const CallbackState;
3283 if state.is_null() {
3284 return -(libc::EINVAL);
3285 }
3286 // SAFETY: non-null per the check above; the field is a plain `u64`.
3287 let byte_ceiling = u128::from(unsafe { (*state).max_frame_bytes });
3288
3289 // SAFETY: `frame` is live; both are plain integer fields.
3290 let (format_raw, nb_samples) = unsafe { ((*frame).format, (*frame).nb_samples) };
3291 let priced = if width > 0 && height > 0 {
3292 crate::footprint::video_frame_bytes(format_raw, width, height)
3293 } else if nb_samples > 0 {
3294 // **The frame's layout, not the context's.** FFmpeg's
3295 // `get_buffer2` contract says the callback reads the values on
3296 // the *frame*, and `avcodec_default_get_buffer2` sizes from them
3297 // — the context's layout is whatever was last negotiated and can
3298 // differ outright. A context claiming mono against a frame
3299 // carrying 255 `dblp` channels at 130,000 samples prices about a
3300 // megabyte and allocates about 265 MB.
3301 //
3302 // Read raw and signed, per the house discipline, and refused
3303 // rather than floored: a negative count is malformed, and
3304 // flooring it to zero would price an allocation that is about to
3305 // happen at nothing.
3306 // SAFETY: `frame` is live; `ch_layout.nb_channels` is a plain
3307 // `c_int`.
3308 let channels = unsafe { (*frame).ch_layout.nb_channels };
3309 if channels <= 0 {
3310 return -(libc::EINVAL);
3311 }
3312 crate::footprint::audio_frame_bytes(format_raw, nb_samples as usize, channels as usize)
3313 } else {
3314 // Neither geometry nor samples: nothing is being allocated that
3315 // this seat can price, and nothing is claimed.
3316 Some(0)
3317 };
3318
3319 // **The refusal leaves its reason behind.** A `get_buffer2`
3320 // callback can only answer libavcodec with an errno, and
3321 // `AVERROR(EINVAL)` is also what libavcodec reports for corrupt
3322 // input — so a bare refusal here was indistinguishable from a
3323 // broken file, and only one of those is worth retrying with a
3324 // larger ceiling. The decoder funnels collect this the same way
3325 // they collect the `get_format` declination.
3326 let record = |bytes: u64| {
3327 use core::sync::atomic::Ordering;
3328 // SAFETY: `state` was proved non-null above.
3329 unsafe {
3330 (*state)
3331 .declined_frame_bytes
3332 .store(bytes, Ordering::Relaxed);
3333 (*state)
3334 .declined_frame_audio
3335 .store(width <= 0 && height <= 0, Ordering::Relaxed);
3336 (*state)
3337 .frame_budget_declined
3338 .store(true, Ordering::Release);
3339 }
3340 -(libc::EINVAL)
3341 };
3342 match priced {
3343 // Fail closed. An allocation whose size cannot be established is
3344 // not a small one — the same stance every other judge here takes.
3345 // Reported as an unbounded cost, which is what an unprovable one
3346 // is.
3347 None => return record(u64::MAX),
3348 // Nothing to buy, so nothing to refuse.
3349 Some(0) => {}
3350 // A budget of zero admits nothing, and this is the arm that used
3351 // to be a skipped guard.
3352 Some(bytes) if byte_ceiling == 0 => return record(bytes as u64),
3353 Some(bytes) if bytes as u128 > byte_ceiling => return record(bytes as u64),
3354 Some(_) => {}
3355 }
3356 }
3357
3358 // SAFETY: delegating to the allocator libavcodec would have used.
3359 unsafe { ffmpeg_next::ffi::avcodec_default_get_buffer2(ctx, frame, flags) }
3360}
3361
3362/// Prices the CPU frame `av_hwframe_transfer_data` would allocate, and
3363/// refuses it if it is over the ceiling — **before** the transfer runs.
3364///
3365/// # Why the hardware road needs its own seat
3366///
3367/// [`judge_buffer`] is not a universal choke point, and the census says
3368/// so on this machine. `ff_get_buffer` calls `hwaccel->alloc_frame`
3369/// directly and never reaches `get_buffer2` at all: a VideoToolbox
3370/// h264 decode of a 160x120 clip records **zero** `get_buffer2` calls
3371/// while producing a hardware frame. And the CPU destination of a
3372/// download is allocated by `av_hwframe_transfer_data` itself, outside
3373/// both hooks.
3374///
3375/// # What the census settled about the surface itself
3376///
3377/// `max_pixels` **does** bite before `alloc_frame`, and this was
3378/// measured rather than assumed: with `max_pixels = 100`, a 160x120
3379/// VideoToolbox h264 decode fails at `avcodec_open2` with
3380/// `Picture size 160x120 exceeds specified max pixel count 100` from
3381/// `av_image_check_size2`, zero `get_buffer2` calls and no frame. The
3382/// check lives in `ff_set_dimensions`, which every decoder runs when it
3383/// learns its dimensions and before any surface pool exists — so the
3384/// seat `max_pixels` already occupies covers the hardware surface too.
3385///
3386/// The residual on that road is the aligned-dimensions gap
3387/// [`judge_buffer`] closes for software frames, and it applies to
3388/// **driver-owned GPU memory** rather than to anything this crate
3389/// carries. What this crate does carry off the hardware road is the CPU
3390/// frame downloaded here, and that is bounded exactly, by this
3391/// function.
3392///
3393/// # How the price is taken
3394///
3395/// The destination format is not chosen by this crate: `dst.format` is
3396/// `AV_PIX_FMT_NONE` on entry and FFmpeg picks from
3397/// `av_hwframe_transfer_get_formats`. So the whole candidate list is
3398/// priced and the **worst** taken — walked as `*const c_int` through
3399/// the shim, because a driver may offer a format this build's bindings
3400/// do not name, which is the same discipline the pixel census keeps.
3401///
3402/// When the list cannot be obtained the global worst rate stands in;
3403/// over-refusing is the safe direction for a ceiling.
3404///
3405/// # Safety
3406///
3407/// `hw_frame` must be a live `*const AVFrame`.
3408unsafe fn judge_hw_transfer(
3409 hw_frame: *const ffmpeg_next::ffi::AVFrame,
3410 limits: crate::FrameLimits,
3411) -> std::result::Result<(), crate::error::HwTransferTooLarge> {
3412 // SAFETY: `hw_frame` is live per the contract; the field is a plain
3413 // pointer.
3414 let frames_ctx = unsafe { (*hw_frame).hw_frames_ctx };
3415
3416 // **The allocated extent, not the displayed one.** `AVFrame.width` /
3417 // `.height` are the *display* dims; what
3418 // `av_hwframe_transfer_data` allocates is sized from the frames
3419 // context, and on a cropped stream the two diverge by orders of
3420 // magnitude — measured on this build, an h264 stream with SPS
3421 // cropping shows 32x32 display over a 1920x1088 coded surface, a
3422 // 2040x gap. This crate already had a helper that reads the pool
3423 // dims, with a doc comment naming this exact trap; the first version
3424 // of this judge reached past it for `AVFrame.width` anyway.
3425 //
3426 // **Fail closed.** No context, no dims, or no priceable candidate
3427 // means the allocation extent cannot be proved — and an unprovable
3428 // extent is not a small one. The same stance
3429 // `estimate_transfer_bytes` takes next door, and for the same reason:
3430 // falling back to display dims here would restore precisely the hole
3431 // this judge exists to close.
3432 if frames_ctx.is_null() {
3433 // Not a hardware frame at all. `av_hwframe_transfer_data` refuses
3434 // such a source with `EINVAL` and allocates nothing, so there is no
3435 // extent to bound here — and answering "too large" would put a
3436 // ceiling's name on a completely different fault. The existing path
3437 // reports it accurately.
3438 return Ok(());
3439 }
3440 let Some((width, height)) = (unsafe { hw_frames_ctx_dimensions_raw(hw_frame) }) else {
3441 // A hardware frame whose pool extent cannot be read. The transfer
3442 // may well allocate; nothing here can say how much. Charged as
3443 // unbounded, which is what an unprovable extent is.
3444 return Err(crate::error::HwTransferTooLarge::new(
3445 usize::MAX,
3446 limits.max_frame_bytes(),
3447 ));
3448 };
3449
3450 // **Every candidate folded in, priceable or not.**
3451 //
3452 // FFmpeg picks the destination format from this list; this crate does
3453 // not get to choose. So the bound has to be the maximum over the
3454 // *whole* list — and the fold used to skip the members libavutil
3455 // would not size, updating `worst` only on priceable ones and
3456 // reaching for a fallback only when *nothing* priced. A list holding
3457 // one cheap priceable format beside one unpriceable format was
3458 // therefore judged at the cheap price, while FFmpeg remained free to
3459 // select the one that was ignored.
3460 //
3461 // An unpriceable candidate is charged
3462 // [`crate::footprint::video_frame_bytes_upper_bound`] instead: the
3463 // same dimension alignment and per-plane overhead at the widest rate,
3464 // so it dominates whatever that layout would have cost had it been
3465 // priceable.
3466 let mut worst: usize = 0;
3467 let mut judged_any = false;
3468 if !frames_ctx.is_null() {
3469 let mut list: *mut libc::c_int = ptr::null_mut();
3470 // `AV_HWFRAME_TRANSFER_DIRECTION_FROM` is 0 — passed as the integer
3471 // it is, like every other open C enum on this road.
3472 // SAFETY: `frames_ctx` is the frame's live `AVHWFramesContext`
3473 // reference; on success FFmpeg allocates a NONE-terminated list
3474 // that the caller frees.
3475 let rc = unsafe { c_shims::av_hwframe_transfer_get_formats(frames_ctx, 0, &mut list, 0) };
3476 if rc >= 0 && !list.is_null() {
3477 let none = ffmpeg_next::ffi::AVPixelFormat::AV_PIX_FMT_NONE as libc::c_int;
3478 let mut p = list;
3479 loop {
3480 // SAFETY: FFmpeg guarantees the list is NONE-terminated; reads
3481 // up to and including the sentinel are in bounds.
3482 let candidate = unsafe { ptr::read(p) };
3483 if candidate == none {
3484 break;
3485 }
3486 // **The allocator's arithmetic, not the payload's.** Pricing
3487 // `av_image_get_buffer_size` at a fixed alignment is what the
3488 // pixels weigh laid out tightly — for a 16x16 NV12 destination
3489 // that is 768 bytes against the 1,792 `av_frame_get_buffer`
3490 // really takes. See [`crate::footprint`].
3491 let cost = crate::footprint::video_frame_bytes(candidate, width, height)
3492 .or_else(|| crate::footprint::video_frame_bytes_upper_bound(width, height));
3493 match cost {
3494 Some(size) => {
3495 worst = worst.max(size);
3496 judged_any = true;
3497 }
3498 // Not even the dimension-only bound could be formed, so the
3499 // extent itself is not a picture. Nothing here will guess.
3500 None => {
3501 // SAFETY: `list` is freed exactly once, on every road out.
3502 unsafe { ffmpeg_next::ffi::av_freep(ptr::addr_of_mut!(list).cast()) };
3503 return Err(crate::error::HwTransferTooLarge::new(
3504 usize::MAX,
3505 limits.max_frame_bytes(),
3506 ));
3507 }
3508 }
3509 p = unsafe { p.add(1) };
3510 }
3511 // SAFETY: `list` was allocated by `av_hwframe_transfer_get_formats`
3512 // and is freed exactly once here.
3513 unsafe { ffmpeg_next::ffi::av_freep(ptr::addr_of_mut!(list).cast()) };
3514 }
3515 }
3516
3517 if !judged_any {
3518 // An empty list, or a query that failed: no candidate was seen at
3519 // all. Charge the dimension-only bound over the pool extent, which
3520 // is the most any format this build can emit could cost there.
3521 let Some(bound) = crate::footprint::video_frame_bytes_upper_bound(width, height) else {
3522 return Err(crate::error::HwTransferTooLarge::new(
3523 usize::MAX,
3524 limits.max_frame_bytes(),
3525 ));
3526 };
3527 worst = bound;
3528 }
3529
3530 if worst > limits.max_frame_bytes() {
3531 return Err(crate::error::HwTransferTooLarge::new(
3532 worst,
3533 limits.max_frame_bytes(),
3534 ));
3535 }
3536 Ok(())
3537}
3538/// Reads and clears the coded-surface refusal a `get_format` callback
3539/// left in its state, if it left one.
3540///
3541/// Free-standing rather than a method because the reason has to survive
3542/// on **every** hardware exit, and one of them — the open-time failure
3543/// path — runs before a decoder exists to ask.
3544fn ceiling_declination_of(state: *const CallbackState) -> Option<Error> {
3545 use core::sync::atomic::Ordering;
3546 if state.is_null() {
3547 return None;
3548 }
3549 // SAFETY: `state` is the live `CallbackState` the caller owns; it is
3550 // freed only after the codec context it belongs to.
3551 let (declined, pixels, limit) = unsafe {
3552 (
3553 (*state).ceiling_declined.swap(false, Ordering::Acquire),
3554 (*state).declined_pixels.load(Ordering::Relaxed),
3555 (*state).declined_limit.load(Ordering::Relaxed),
3556 )
3557 };
3558 declined.then(|| Error::HwSurfaceTooLarge(crate::error::HwSurfaceTooLarge::new(pixels, limit)))
3559}
3560/// The software decoders' error funnel.
3561///
3562/// Every road that turns a libavcodec decode failure into an `Error`
3563/// goes through here, so a frame the allocator judge refused comes back
3564/// named instead of as the `EINVAL` libavcodec also uses for corrupt
3565/// input. The hardware roads have their own funnel (`hw_exit`); this is
3566/// its software twin, and the discipline is the same one: **a consumer
3567/// added helper-by-helper is lost the next time the surrounding code is
3568/// restructured, so every exit calls one function.**
3569///
3570/// # Safety
3571///
3572/// `state` must be null or a live `CallbackState` the caller owns.
3573pub(crate) fn software_exit(state: *const CallbackState, e: ffmpeg_next::Error) -> Error {
3574 frame_budget_declination_of(state).unwrap_or(Error::Ffmpeg(e))
3575}
3576
3577/// **The software road's only way to read an errno — funnel and
3578/// classify in one call, because the order between them is a law and
3579/// laws that depend on remembering get broken.**
3580///
3581/// Every receive site used to write the two steps out: funnel, then
3582/// classify. The R1 report called that ordering load-bearing and
3583/// explained why — a `get_format` declination or an allocator-judge
3584/// refusal sits in the callback state waiting to be collected, and a
3585/// classifier that runs first reads the errno libavcodec reported
3586/// instead of the refusal this crate made, answering `Ended` or
3587/// `NeedsInput` for a frame that was declined. Then a restructure
3588/// reordered one road and the law was simply gone, silently, because
3589/// nothing enforced it.
3590///
3591/// So the classifiers are private to this module now and this is the
3592/// door. A caller cannot classify a raw error because it cannot reach a
3593/// classifier; the funnel is not something to remember to call first,
3594/// it is the only thing there is to call.
3595///
3596/// # The verdict is minted once and threaded
3597///
3598/// **A funnel consumes what it collects.** `take_ceiling_declination`
3599/// and `take_frame_budget_declination` both *clear* the latch they
3600/// read, because a refusal reported twice would be a refusal invented
3601/// once. That makes the verdict a one-shot value, and the rule that
3602/// follows is the whole of this invariant:
3603///
3604/// > The first funnel on a road mints the verdict. Every later step on
3605/// > that road **threads it**. A site that re-funnels, or that rebuilds
3606/// > `Error::Ffmpeg(raw)` after a funnel has run, is the bug class —
3607/// > the second call finds the latch empty and reports the errno the
3608/// > substrate happened to give over the refusal this crate made.
3609///
3610/// A raw errno may still be *read* after minting — `is_hw_decode_failure`
3611/// does, to decide whether a fallback is required — but reading it to
3612/// decide a route is not the same as reporting it. What the caller is
3613/// told is always the verdict.
3614///
3615/// # Safety
3616///
3617/// `state` must be null or a live [`CallbackState`] the caller owns.
3618pub(crate) fn software_receive(
3619 state: *const CallbackState,
3620 e: ffmpeg_next::Error,
3621 phase: SessionPhase,
3622) -> Result<Received> {
3623 receive_status(software_exit(state, e), phase)
3624}
3625
3626/// The send road's half of [`software_receive`]. Same law, same door.
3627///
3628/// # Safety
3629///
3630/// `state` must be null or a live [`CallbackState`] the caller owns.
3631pub(crate) fn software_send(
3632 state: *const CallbackState,
3633 e: ffmpeg_next::Error,
3634 phase: SessionPhase,
3635) -> Result<Sent> {
3636 send_status(software_exit(state, e), phase)
3637}
3638
3639/// Reads and clears a software frame-budget refusal left by
3640/// [`judge_buffer`], as the named error it deserves.
3641///
3642/// The software twin of [`ceiling_declination_of`]: the allocator judge
3643/// can only answer libavcodec with an errno, so the reason lives in the
3644/// callback state and every decoder funnel collects it.
3645pub(crate) fn frame_budget_declination_of(state: *const CallbackState) -> Option<Error> {
3646 crate::ffi::take_frame_budget_declination(state).map(|(bytes, limit, audio)| {
3647 Error::FrameBudgetExceeded(crate::error::FrameBudgetExceeded::new(
3648 bytes,
3649 limit,
3650 if audio {
3651 crate::error::FrameMedium::Audio
3652 } else {
3653 crate::error::FrameMedium::Video
3654 },
3655 ))
3656 })
3657}
3658
3659/// Proves an opened codec context is a **video** one without going
3660/// through `Opened::video()`.
3661///
3662/// `Opened::video()` calls `Context::medium()`, which reads
3663/// `AVCodecContext.codec_type` as the bindgen `AVMediaType` enum — a
3664/// value outside this build's discriminant set is UB the moment it is
3665/// formed, before any comparison can run. The hardware path has always
3666/// bypassed that API for this reason; this is that bypass, extracted so
3667/// the second caller reuses it instead of restating it.
3668///
3669/// The caller keeps ownership of `opened` on failure, so its `Drop`
3670/// still releases the codec context.
3671pub(crate) fn ensure_video_codec_type(opened: &codec::decoder::Opened) -> Result<()> {
3672 ensure_codec_type(opened, AVMediaType::AVMEDIA_TYPE_VIDEO)
3673}
3674
3675/// The general form: proves an opened context has the medium expected,
3676/// reading `codec_type` as the integer it is.
3677///
3678/// `Opened::{video,audio,subtitle}()` all go through
3679/// `Context::medium()`, so all three carried the same hazard and all
3680/// three now come through here.
3681pub(crate) fn ensure_codec_type(
3682 opened: &codec::decoder::Opened,
3683 expected: AVMediaType,
3684) -> Result<()> {
3685 // SAFETY: `codec_type` is bound as `AVMediaType` (`#[repr(i32)]`),
3686 // the same size and alignment as `i32`; reading the bytes as `i32`
3687 // cannot be UB whatever FFmpeg wrote there.
3688 let codec_type_int: i32 =
3689 unsafe { ptr::read(ptr::addr_of!((*opened.as_ptr()).codec_type) as *const i32) };
3690 if codec_type_int != expected as i32 {
3691 // The same error `Opened::video()` would have produced, without the
3692 // enum construction.
3693 return Err(Error::Ffmpeg(ffmpeg_next::Error::InvalidData));
3694 }
3695 Ok(())
3696}
3697
3698/// Look up the decoder for `parameters` without going through the bindgen
3699/// `AVCodecID` Rust enum. Reads the codec_id field as raw `u32` via
3700/// `addr_of!` + `ptr::read` so a value not in our build's discriminant
3701/// set never invokes UB.
3702pub(crate) fn find_decoder(parameters: &codec::Parameters) -> Result<Codec> {
3703 ensure_parameters_non_null(parameters)?;
3704 // SAFETY: parameters' inner pointer is non-null (checked above);
3705 // addr_of! projects to the codec_id field; the *const u32 cast is sound
3706 // because AVCodecID is `#[repr(u32)]` (same size and alignment as u32).
3707 // Reading as u32 cannot be UB regardless of the value FFmpeg wrote.
3708 let raw_id: u32 =
3709 unsafe { ptr::read(ptr::addr_of!((*parameters.as_ptr()).codec_id) as *const u32) };
3710
3711 // Call C `avcodec_find_decoder` via our local `c_int`-typed shim — we
3712 // never construct an `AVCodecID` enum from `raw_id`. The C function
3713 // returns NULL for unknown ids, which we surface as `Error::NoCodec`.
3714 // SAFETY: avcodec_find_decoder is a pure FFmpeg lookup; passing any
3715 // c_int is sound (returns NULL for unknown).
3716 let codec_ptr = unsafe { c_shims::avcodec_find_decoder(raw_id as libc::c_int) };
3717 if codec_ptr.is_null() {
3718 return Err(Error::NoCodec(raw_id));
3719 }
3720 // SAFETY: codec_ptr is a non-null *const AVCodec into FFmpeg's static
3721 // codec table; it lives for the duration of the program.
3722 Ok(unsafe { Codec::wrap(codec_ptr) })
3723}
3724
3725/// Drain output frames from a candidate decoder during probe replay,
3726/// transferring each one from the candidate's HW context to a fresh CPU
3727/// frame and queueing it. Returns `Ok(())` once the candidate signals
3728/// EAGAIN/EOF. The transfer happens while the candidate is still alive
3729/// (its `AVHWFramesContext` is reachable); the resulting CPU frames remain
3730/// valid after the candidate is committed because they hold their own
3731/// buffer references with no dependency on the original device context.
3732fn drain_into_pending(
3733 decoder: &mut ffmpeg_next::decoder::Video,
3734 hw_buf: &mut frame::Video,
3735 pending: &mut VecDeque<frame::Video>,
3736 pending_bytes: &mut usize,
3737 max_bytes: usize,
3738 frame_limits: crate::FrameLimits,
3739) -> std::result::Result<(), ffmpeg_next::Error> {
3740 loop {
3741 match decoder.receive_frame(hw_buf) {
3742 Ok(()) => {
3743 // Pre-transfer cap check: if we are already at or over either cap,
3744 // the candidate is producing more than we can hold. Treat as an
3745 // explicit candidate failure so `advance_probe` can try the next
3746 // backend instead of committing a stream with silently-dropped
3747 // frames in the middle.
3748 //
3749 // TODO: at very large frame sizes (8K HDR P010, > ~96 MiB each)
3750 // even a single retained frame is significant. Future direction:
3751 // memmap-backed pending frames (write to a temp file or shared
3752 // memory segment) so the resident set stays bounded even when the
3753 // byte cap is raised. Out of scope for now.
3754 if pending.len() >= MAX_PROBE_PENDING_FRAMES || *pending_bytes >= max_bytes {
3755 tracing::warn!(
3756 frames = pending.len(),
3757 bytes = *pending_bytes,
3758 max_frames = MAX_PROBE_PENDING_FRAMES,
3759 max_bytes = max_bytes,
3760 "hwdecode: probe pending cap reached; failing candidate replay"
3761 );
3762 // SAFETY: hw_buf is owned and valid; unref of an empty frame is a no-op.
3763 unsafe { av_frame_unref(hw_buf.as_mut_ptr()) };
3764 return Err(ffmpeg_next::Error::Other {
3765 errno: libc::ENOMEM,
3766 });
3767 }
3768 // Pre-transfer size guard: `av_hwframe_transfer_data` will
3769 // allocate the CPU buffer based on `hw_buf`'s dimensions. If a
3770 // single frame's worst-case footprint already pushes past the
3771 // cap, refuse the candidate **before** allocating so RSS does
3772 // not spike on a frame we'd immediately drop. Uses a width *
3773 // height * `WORST_CASE_BYTES_PER_PIXEL` upper bound; the
3774 // post-transfer accounting via `cpu_frame_bytes` below stays in
3775 // place as a backstop using the actual stride/format.
3776 let estimated_bytes = match estimate_transfer_bytes(hw_buf) {
3777 Some(b) => b,
3778 None => {
3779 // SAFETY: AVFrame.width/height are c_int reads.
3780 let (w, h) = unsafe {
3781 let raw = hw_buf.as_ptr();
3782 ((*raw).width, (*raw).height)
3783 };
3784 tracing::warn!(
3785 width = w,
3786 height = h,
3787 "hwdecode: HW frame dimensions invalid for sizing; failing candidate replay"
3788 );
3789 unsafe { av_frame_unref(hw_buf.as_mut_ptr()) };
3790 return Err(ffmpeg_next::Error::Other {
3791 errno: libc::ENOMEM,
3792 });
3793 }
3794 };
3795 let estimated_total = pending_bytes.saturating_add(estimated_bytes);
3796 if estimated_total > max_bytes {
3797 // SAFETY: AVFrame.width/height are c_int reads.
3798 let (w, h) = unsafe {
3799 let raw = hw_buf.as_ptr();
3800 ((*raw).width, (*raw).height)
3801 };
3802 tracing::warn!(
3803 pending_bytes = *pending_bytes,
3804 estimated_bytes,
3805 width = w,
3806 height = h,
3807 max_bytes = max_bytes,
3808 "hwdecode: pre-transfer size estimate exceeds cap; \
3809 refusing candidate replay before allocating CPU frame"
3810 );
3811 unsafe { av_frame_unref(hw_buf.as_mut_ptr()) };
3812 return Err(ffmpeg_next::Error::Other {
3813 errno: libc::ENOMEM,
3814 });
3815 }
3816 // **The same exact judge, on the replay road.** This site
3817 // already had a pre-transfer *estimate* (`w * h * 8`) against
3818 // the probe's own pending budget; that stays, and this adds the
3819 // frame ceiling itself, priced exactly.
3820 //
3821 // The refusal is reported through this function's existing
3822 // `ffmpeg_next::Error` channel rather than the named arm: every
3823 // error out of a probe-replay drain is collapsed by the caller
3824 // into "this candidate failed, try the next backend", so a name
3825 // has no consumer here. The reason is logged so it is not lost.
3826 // SAFETY: `hw_buf` holds a live decoded HW frame.
3827 if let Err(e) = unsafe { judge_hw_transfer(hw_buf.as_ptr(), frame_limits) } {
3828 tracing::warn!(
3829 bytes = e.bytes(),
3830 limit = e.limit(),
3831 "hwdecode: candidate's hw->cpu transfer would exceed the frame ceiling; \
3832 refusing the candidate before the download"
3833 );
3834 // SAFETY: `hw_buf` is owned and valid.
3835 unsafe { av_frame_unref(hw_buf.as_mut_ptr()) };
3836 return Err(ffmpeg_next::Error::Other {
3837 errno: libc::EINVAL,
3838 });
3839 }
3840 let mut cpu = alloc_av_frame()?;
3841 // SAFETY: hw_buf is a freshly-decoded HW frame;
3842 // `av_hwframe_transfer_data` allocates pixel buffers on `cpu`.
3843 // We use `copy_frame_props_minimal` (only `pts`) instead of
3844 // `av_frame_copy_props` for the same reason as
3845 // `transfer_hw_frame`: the public `Frame` API does not expose
3846 // side data / metadata / opaque refs, so deep-copying them per
3847 // frame is pure cost and an unbounded allocation source on
3848 // attacker-controlled streams.
3849 unsafe {
3850 let r1 = av_hwframe_transfer_data(cpu.as_mut_ptr(), hw_buf.as_ptr(), 0);
3851 if r1 < 0 {
3852 return Err(ffmpeg_next::Error::from(r1));
3853 }
3854 }
3855 // Same post-transfer pix_fmt validation as `transfer_hw_frame`.
3856 // A driver that picks a CPU format outside our supported set
3857 // would queue an unusable frame here; later, when
3858 // `try_pop_pending` hands it to the caller, `Frame::row` /
3859 // `Frame::as_ptr` would return `None`. Refuse the candidate
3860 // before the queue grows so probing advances to the next
3861 // backend instead.
3862 let cpu_raw_fmt: i32 = unsafe { (*cpu.as_ptr()).format };
3863 let cpu_pix_fmt = crate::boundary::from_av_pixel_format(cpu_raw_fmt);
3864 if !crate::frame::is_supported_cpu_pix_fmt(&cpu_pix_fmt) {
3865 tracing::warn!(
3866 pix_fmt = cpu_raw_fmt,
3867 "hwdecode: candidate produced unsupported CPU pix_fmt during \
3868 probe replay; failing candidate"
3869 );
3870 return Err(ffmpeg_next::Error::Other {
3871 errno: libc::EINVAL,
3872 });
3873 }
3874 let pixel_bytes = match cpu_frame_bytes(&cpu) {
3875 Some(b) => b,
3876 None => {
3877 // Unknown pix_fmt or vertically-flipped layout — we cannot
3878 // bound this frame's contribution against the byte cap, so up
3879 // to MAX_PROBE_PENDING_FRAMES of them could exhaust memory.
3880 // Fail the candidate so probing tries the next backend
3881 // rather than queueing untracked allocations.
3882 // SAFETY: AVFrame.format is c_int, safe to read.
3883 let pix_fmt: i32 = unsafe { (*cpu.as_ptr()).format };
3884 tracing::warn!(
3885 pix_fmt,
3886 "hwdecode: cannot size unknown CPU pix_fmt during replay; failing candidate"
3887 );
3888 // cpu drops here.
3889 return Err(ffmpeg_next::Error::Other {
3890 errno: libc::ENOMEM,
3891 });
3892 }
3893 };
3894 // Account for side-data bytes that `av_frame_copy_props`
3895 // will deep-copy from the source HW frame. HDR streams
3896 // typically carry mastering display + content light level
3897 // (~50 bytes) and dynamic HDR metadata (~few hundred bytes);
3898 // pathological side-data could otherwise quietly bypass the
3899 // pixel-data byte cap.
3900 // SAFETY: hw_buf is a valid AVFrame; we read scalar fields
3901 // and pointer arrays without forming a `&AVFrame`.
3902 let side_data_bytes = unsafe { sum_side_data_bytes(hw_buf.as_ptr()) };
3903 let new_total = pending_bytes
3904 .saturating_add(pixel_bytes)
3905 .saturating_add(side_data_bytes);
3906 if new_total > max_bytes {
3907 tracing::warn!(
3908 pending_bytes = *pending_bytes,
3909 pixel_bytes,
3910 side_data_bytes,
3911 max_bytes,
3912 "hwdecode: queueing this frame would exceed byte cap; \
3913 failing candidate replay"
3914 );
3915 // cpu drops here without ever paying a metadata deep copy.
3916 return Err(ffmpeg_next::Error::Other {
3917 errno: libc::ENOMEM,
3918 });
3919 }
3920 // Cap check passed — copy AVFrame metadata. SAFETY: cpu and
3921 // hw_buf are both valid AVFrames we own. On failure (OOM
3922 // during side-data alloc) we propagate so the probe candidate
3923 // is treated as failed rather than queueing a frame whose
3924 // metadata silently disappeared.
3925 unsafe { copy_frame_props_minimal(cpu.as_mut_ptr(), hw_buf.as_ptr()) }?;
3926 *pending_bytes = new_total;
3927 pending.push_back(cpu);
3928 }
3929 Err(e) if is_transient(&e) => return Ok(()),
3930 Err(e) => return Err(e),
3931 }
3932 }
3933}
3934
3935/// Allocated frame dimensions according to `hw_buf.hw_frames_ctx`.
3936///
3937/// Per FFmpeg's `libavutil/hwcontext.c::transfer_data_alloc`, the CPU
3938/// destination of `av_hwframe_transfer_data` is allocated using
3939/// `AVHWFramesContext.width / .height` (the *allocated* surface size of
3940/// the HW pool); only afterwards is `dst->width / dst->height` reset to
3941/// `src->width / src->height` (the *display* size). For cropped or
3942/// heavily aligned streams the allocated dims can be much larger than
3943/// the display dims (e.g. coded 8192×8192 surface with a 100×100
3944/// display crop), so any byte-cap accounting that uses display dims
3945/// undercounts by `allocated_height / display_height` and lets the
3946/// real allocation slip past the cap.
3947///
3948/// Returns `None` when no `hw_frames_ctx` is attached or the dimensions
3949/// are non-positive — the caller treats `None` as "cannot prove
3950/// allocation extent, fail the candidate."
3951fn hw_frames_ctx_dimensions(frame: &frame::Video) -> Option<(i32, i32)> {
3952 // SAFETY: `frame` owns a live `AVFrame` for the call.
3953 unsafe { hw_frames_ctx_dimensions_raw(frame.as_ptr()) }
3954}
3955
3956/// Pointer form of [`hw_frames_ctx_dimensions`], for the judges that
3957/// hold a raw `AVFrame` rather than a wrapper.
3958///
3959/// # Safety
3960///
3961/// `raw` must be a live `*const AVFrame`.
3962unsafe fn hw_frames_ctx_dimensions_raw(raw: *const AVFrame) -> Option<(i32, i32)> {
3963 // SAFETY: AVFrame.hw_frames_ctx is `*mut AVBufferRef`. When non-null,
3964 // its `data` field points to an `AVHWFramesContext`. We read `.width`
3965 // and `.height` (both `c_int`) via field projection — neither field is
3966 // enum-typed, so no bindgen-enum UB hazard.
3967 unsafe {
3968 let hw_ctx_ref = (*raw).hw_frames_ctx;
3969 if hw_ctx_ref.is_null() {
3970 return None;
3971 }
3972 let data = (*hw_ctx_ref).data;
3973 if data.is_null() {
3974 return None;
3975 }
3976 let frames_ctx = data as *const AVHWFramesContext;
3977 let w: i32 = ptr::read(ptr::addr_of!((*frames_ctx).width));
3978 let h: i32 = ptr::read(ptr::addr_of!((*frames_ctx).height));
3979 if w <= 0 || h <= 0 {
3980 return None;
3981 }
3982 Some((w, h))
3983 }
3984}
3985
3986/// Conservative upper-bound estimate of the bytes
3987/// `av_hwframe_transfer_data` will allocate when downloading `hw_buf` to
3988/// a CPU frame. Used by [`drain_into_pending`] as a pre-transfer guard
3989/// so a candidate replay can refuse a frame whose footprint would
3990/// exceed the byte budget *without* first paying the allocation.
3991///
3992/// Sizes from `hw_buf.hw_frames_ctx` (the allocated dims used by the
3993/// FFmpeg transfer path) rather than `AVFrame.width / .height` (display
3994/// dims). On a cropped stream the two can differ by orders of magnitude
3995/// and using display dims would let the real allocation slip past the
3996/// cap.
3997///
3998/// Returns `None` when `hw_frames_ctx` is missing or its width/height
3999/// are non-positive — caller treats as candidate failure since we
4000/// cannot prove the allocation extent. (A SW source frame on the probe
4001/// replay path is not expected; we don't fall back to display dims
4002/// because that's the exact attack the cap is meant to prevent.)
4003fn estimate_transfer_bytes(hw_buf: &frame::Video) -> Option<usize> {
4004 let (w, h) = hw_frames_ctx_dimensions(hw_buf)?;
4005 Some(
4006 (w as usize)
4007 .saturating_mul(h as usize)
4008 .saturating_mul(WORST_CASE_BYTES_PER_PIXEL),
4009 )
4010}
4011
4012/// Exact resident size of a CPU frame: sum of `AVFrame.buf[i].size`
4013/// across every populated buffer.
4014///
4015/// `AVBufferRef.size` is documented as "Size of data in bytes" — the
4016/// real allocated extent FFmpeg used. Reading it directly handles the
4017/// cropped/aligned case where `AVFrame.height` (display) is smaller
4018/// than the underlying allocation height (the `AVHWFramesContext`
4019/// surface size FFmpeg sized the buffer for); a `linesize *
4020/// plane_height_for(display_height)` formula would undercount in that
4021/// case.
4022///
4023/// Returns `None` only when `linesize[0]` is negative — FFmpeg's
4024/// vertically-flipped layout. The crate's safe row accessors
4025/// ([`crate::Frame::row`] / [`crate::Frame::rows`]) already reject
4026/// negative-stride frames, so queueing one during probe replay would
4027/// just delay the failure to the consumer; refusing here lets the
4028/// probe loop advance to the next backend instead.
4029fn cpu_frame_bytes(frame: &frame::Video) -> Option<usize> {
4030 // SAFETY: AVFrame.linesize is `[c_int; 8]`; AVFrame.buf is
4031 // `[*mut AVBufferRef; 8]`; AVBufferRef.size is `usize`. All are
4032 // primitive reads / pointer dereferences with no enum interpretation.
4033 unsafe {
4034 let raw = frame.as_ptr();
4035 let first_linesize = (*raw).linesize[0];
4036 // Vertically-flipped (negative linesize) is the only "unsizeable"
4037 // case we still surface as `None`; everything else can be exactly
4038 // measured from buf[i].size.
4039 if first_linesize < 0 {
4040 return None;
4041 }
4042 let mut total: usize = 0;
4043 for i in 0..(*raw).buf.len() {
4044 let buf = (*raw).buf[i];
4045 if buf.is_null() {
4046 continue;
4047 }
4048 total = total.saturating_add((*buf).size);
4049 }
4050 Some(total)
4051 }
4052}
4053
4054#[allow(dead_code)]
4055fn _assert_send() {
4056 fn check<T: Send>() {}
4057 check::<VideoDecoder>();
4058}
4059
4060#[cfg(test)]
4061mod tests;