Skip to main content

mediadecode_ffmpeg/
buffer.rs

1//! The **amputation seam**: FFmpeg's bytes leave here, copied once,
2//! as Rust-owned memory.
3//!
4//! An `AVPacket`'s payload and an `AVFrame`'s planes both live in
5//! `AVBufferRef`s — FFmpeg's own refcounted allocations. Through 0.8
6//! this crate handed those out directly, wrapped in an `FfmpegBuffer`
7//! whose `AsRef<[u8]>` pointed straight into libavcodec's memory. That
8//! type is gone. Every byte that crosses this boundary is now copied
9//! into an [`FfmpegBytes`], which is what the core's
10//! [D-seat amputation contract][law] requires: owned, `Send + Sync`,
11//! clone-is-a-refcount-bump, and with no FFmpeg lifetime riding along.
12//!
13//! What is left in this module is everything the copy still has to
14//! *judge*. A packet's payload has to be proved to lie inside the
15//! buffer that owns it before a byte of it is read, its side data has
16//! to be carried whole or refused, and its flags have to fit the
17//! portable set — so [`PacketBufferError`] and its payload structs
18//! outlive the buffer type they were written for. The bounds check in
19//! particular matters *more* now, not less: 0.8 formed a view over the
20//! claimed range, 0.9 reads it.
21//!
22//! # The one thing the amputation costs
23//!
24//! The `Arc<[u8]>` behind [`FfmpegBytes`] has no fallible constructor
25//! on stable Rust, so the copy itself aborts on allocation failure
26//! rather than returning an error.
27//! Everything that bounds *how much* can be asked for — the side-data
28//! entry and byte caps, the plane-geometry checks — is unchanged and
29//! still runs before any allocation, so a hostile stream cannot reach
30//! that abort by demanding memory; only a genuinely exhausted
31//! allocator can.
32//!
33//! [`payload_of`] is where the per-packet half of that bounding lives:
34//! every packet body this crate copies passes through it, and it
35//! refuses an over-budget claim before reading a byte. See
36//! [`crate::limits`] for the budgets and their defaults.
37//!
38//! # The funnel's accounting
39//!
40//! Every [`FfmpegBytes`] in this crate is built by
41//! [`FfmpegBytes::copy_from_slice`], [`FfmpegBytes::from_rows`] or
42//! [`FfmpegBytes::empty`], and **every one of those call sites is
43//! bounded before it allocates**. The table is kept here, beside the
44//! constructors, so that a new exit has to answer the question the
45//! existing ones already answered — the discipline is inherited by
46//! being written down where the next author will be standing.
47//!
48//! Three review rounds found bypasses that each looked like an
49//! exception: a plane path with no ceiling, an attachment whose
50//! payload was copied by `avcodec_parameters_copy` before its budget
51//! was charged, a resampler amplifying a small input into a huge
52//! output, and then `coded_side_data` — a third heap seat on the same
53//! wholesale parameter copy, where a MOV `prof` atom puts an ICC
54//! profile. None of them were exceptions. They were rows nobody had
55//! written down.
56//!
57//! The third one is why the table below has a section it did not need
58//! at first. `FfmpegBytes` is not the only place this crate copies
59//! attacker-sized bytes: `AVCodecParameters` has heap seats of its own,
60//! and the wholesale FFI copy that used to duplicate them took every
61//! one — including any this crate had never enumerated. That copy is
62//! gone; see
63//! [`bounded_clone_parameters`](crate::extras::bounded_clone_parameters),
64//! and [`CodecTicket`](crate::CodecTicket), which took the track row's
65//! copy one step further: those three seats are now owned Rust, and
66//! `extradata` lands in an `FfmpegBytes` like every other file-sized
67//! buffer here.
68//!
69//! | construction site | what it carries | what bounds it |
70//! |---|---|---|
71//! | [`payload_of`] | any packet's payload | its `budget` argument — [`PacketLimits::max_packet_bytes`](crate::PacketLimits::max_packet_bytes) for timed packets, [`DemuxLimits::max_attachment_bytes`](crate::DemuxLimits::max_attachment_bytes) for attachments — judged against the declared `size` before a byte is read |
72//! | `convert::copy_out_planes`, tight stride | one video or image plane | [`FrameLimits::max_pixels`](crate::FrameLimits::max_pixels) and [`FrameLimits::max_frame_bytes`](crate::FrameLimits::max_frame_bytes), both in a judge-pass that runs before any plane is allocated; `max_pixels` also reaches libavcodec |
73//! | `convert::copy_out_planes`, padded stride | one compacted plane, via [`FfmpegBytes::from_rows`] | the same pre-pass |
74//! | `convert::av_frame_to_audio_frame` | one audio plane | `max_frame_bytes`, checked over `plane_bytes × plane_count` before the loop |
75//! | `convert::collect_side_data` | one frame side-data entry | `SIDE_DATA_MAX_ENTRIES` (64) and `SIDE_DATA_MAX_TOTAL_BYTES` (256 KiB), plus `try_reserve_exact` |
76//! | `boundary::packet_side_data` | one packet side-data entry | the same two caps, as refusals rather than truncation |
77//! | `convert::av_subtitle_to_subtitle_frame`, text | concatenated cue text | `SUBTITLE_MAX_TEXT_BYTES_PER_RECT` (64 KiB), `SUBTITLE_MAX_TEXT_TOTAL_BYTES` (256 KiB), `SUBTITLE_MAX_RECTS` (64) |
78//! | …, bitmap | one paletted rect | `SUBTITLE_MAX_BITMAP_BYTES_PER_RECT` (16 MiB), `SUBTITLE_MAX_BITMAP_TOTAL_BYTES` (32 MiB), `SUBTITLE_MAX_RECTS` |
79//! | …, palette | an RGBA palette | structurally fixed at 256 × 4 bytes by the format |
80//! | `demuxer::extradata_payload` | a synthesized attachment (a font) | `demuxer::admit_attachments`, which charges every attachment in the file — per-attachment **and** aggregate — before the track loop allocates anything; re-checked here against the per-attachment ceiling |
81//! | `demuxer::attached_pic_payload` | a hoisted cover-art packet | the same admission pass, then `payload_of`'s budget |
82//! | `resampler::finish_output` | one converted audio plane | `FfmpegResampler::check_output_bytes`, against `max_frame_bytes`, run before the output `AVFrame` is allocated |
83//! | every [`FfmpegBytes::empty`] site | nothing | structurally zero: placeholder plane slots, a payload-less packet, a null palette, a marker side-data entry |
84//!
85//! # The other heap this crate copies
86//!
87//! `AVCodecParameters` is the same class of exposure — three heap
88//! seats, all sized by the file — so its rows belong in the same
89//! accounting. Since the track row went owned, one of those seats
90//! *is* an [`FfmpegBytes`]: [`CodecTicket`](crate::CodecTicket) holds
91//! `extradata` in one, and each `coded_side_data` payload in another.
92//!
93//! | construction site | what it carries | what bounds it |
94//! |---|---|---|
95//! | [`CodecTicket::mirror`](crate::CodecTicket::mirror), `extradata` | SPS/PPS and codec headers, into an [`FfmpegBytes`] | [`DemuxLimits::max_codec_parameter_bytes`](crate::DemuxLimits::max_codec_parameter_bytes), measured by `measure_parameters` before a byte is read |
96//! | …, `coded_side_data` | the descriptor array and each entry's payload — a MOV `prof` atom's ICC profile among them — into owned entries | the same seat, counting the array as well as the payloads |
97//! | …, `ch_layout` custom map | a channel map, into owned entries | the same seat |
98//! | [`CodecTicket::rebuild`](crate::CodecTicket::rebuild) | the same three seats, back into a fresh `AVCodecParameters` for a decoder to open from | nothing further, and nothing further is needed: it allocates exactly [`CodecTicket::footprint_bytes`](crate::CodecTicket::footprint_bytes), which is the number the mirror already admitted — no file-controlled input reaches it |
99//! | [`bounded_clone_parameters`](crate::extras::bounded_clone_parameters) | the same three seats, `AVCodecParameters` to `AVCodecParameters` | the same measurement. Off the demux road since the track row went owned; it is the decoder's own re-clone (`decoder::try_clone_parameters`), bounded by [`DecoderLimits::max_codec_parameter_bytes`](crate::DecoderLimits::max_codec_parameter_bytes) |
100//! | `demuxer::admit_streams` | nothing — it only measures | runs over **every** stream before the track loop mirrors anything, and charges the whole-file [`max_total_codec_parameter_bytes`](crate::DemuxLimits::max_total_codec_parameter_bytes) |
101//! | `decoder::build_codec_context` → `avcodec_parameters_to_context` | the same three seats, copied *into* an `AVCodecContext` | **the choke point**: measured and admitted against [`DecoderLimits::max_codec_parameter_bytes`](crate::DecoderLimits::max_codec_parameter_bytes) right there. Every decoder in this crate opens through this function — the four session `open`s, the HW probe's `build_state`, its per-backend advances, the software fallback — and none of them reaches `avcodec_parameters_to_context` any other way |
102//! | `image::FfmpegImageDecoder::decode` → `boundary::try_packet_copy` | the caller's compressed bytes, duplicated into an `AVPacket` | [`DecoderLimits::max_image_input_bytes`](crate::DecoderLimits::max_image_input_bytes), defaulting to the attachment family so the direct road is no more permissive than the demuxed one |
103//! | `boundary::ffmpeg_packet_from_{video,audio,subtitle}_packet` → `try_packet_copy` | the caller's compressed bytes, duplicated into an `AVPacket` — **the send leg** | [`DecoderLimits::max_packet_bytes`](crate::DecoderLimits::max_packet_bytes), judged before the allocation. The same seat the receive leg (`payload_of`) judges, so a byte count refused coming out of a container is refused going into a decoder |
104//! | still `pal8` palette plane | a fixed `AVPALETTE_SIZE` run | the **format**, not a seat: 256 × `AV_PIX_FMT_RGB32`, always, with no number a file gets to choose |
105//!
106//! # The rule
107//!
108//! **A carrier whose size comes from a file is bounded by a seat in
109//! [`crate::limits`]; a carrier whose size is a property of a format is
110//! bounded by that format.** There is no third kind, and a site that
111//! looks like one has not been thought about yet.
112//!
113//! And the corollary the third round bought: **no code path hands
114//! attacker-sized data to a wholesale FFI copy** — a copy that
115//! duplicates every field of a struct duplicates the fields nobody
116//! enumerated, which is a budget bypass that arrives with the next
117//! FFmpeg release rather than with the next commit.
118//!
119//! # The substrate's knobs, and where this crate stops
120//!
121//! Everything above is **tier one** of the [resource governance
122//! contract][gov]: allocations this crate makes itself, each bounded by
123//! a named seat or by a format. This table is that tier's proof.
124//!
125//! Tier two is the other half — FFmpeg's own resource knobs, set at
126//! every point libavcodec and libavformat offer one. They bound
127//! allocations this crate does not make and could not otherwise see:
128//!
129//! | knob | where it is set | what it bounds |
130//! |---|---|---|
131//! | `AVCodecContext.max_pixels` | every opened decoder | the caller's pixel limit, **verbatim**, applied by `ff_set_dimensions` to the raw dimensions. Extent, not cost: what a frame *costs* is the byte judge's question, two rows down |
132//! | the `get_format` coded-dims ask | the hardware road | the **pool's own declared extent**, asked of `avcodec_get_hw_frames_parameters` before the pool is initialised — `max_pixels` is applied to the *display* dims, which a cropped stream can make 2000x smaller |
133//! | the `get_format` byte judge | the hardware road | the **pool's** cost, priced through [`crate::footprint`] against `max_frame_bytes`. **Fails closed**: a pool that will not declare its dimensions and layout is a pool that cannot be judged, and the codec-alignment fallback that used to stand in could answer *smaller* than the pool it was standing in for |
134//! | the `get_buffer2` byte judge | every software decode | what the allocator will actually take for this frame — pictures and audio both, priced through [`crate::footprint`] against the caller's own `max_frame_bytes`, carried in the codec context's callback state |
135//! | the pre-transfer judge | every `av_hwframe_transfer_data` | the CPU destination a hardware download allocates, priced at the frames-context pool dims — folding **every** candidate format FFmpeg may pick, priceable or not, since FFmpeg does the picking |
136//! | `probesize` / `formatprobesize` | both demux entrypoints | what the format probe and stream analysis may consume |
137//! | `max_streams` | both demux entrypoints | the `AVStream` array a header can conjure |
138//! | the `AVIOContext` byte meter | the **reader** demux entrypoint | total bytes libavformat is handed, hard — past the budget the reader stops answering |
139//!
140//! Two of those knobs used to carry *translated* byte ceilings —
141//! `max_pixels` as `min(the caller's limit, bytes / 16)` and
142//! `max_samples` as `bytes / 8` — so that the byte budget could bite
143//! before libavcodec allocated. Both translations charged every stream
144//! the worst format in existence, and both over-refused ordinary media:
145//! a 1920x1080 `yuv420p` frame under a 4 MiB budget, a 6-channel `s16`
146//! frame under 64 KiB. They are gone. The byte budget is enforced by
147//! the `get_buffer2` judge, which is *itself* a pre-allocation seat —
148//! `get_buffer2` **is** the allocation — and prices the frame's real
149//! format at its real dimensions. An exact judge at the allocation
150//! beats an approximate one before it.
151//!
152//! Where a layout cannot be priced at all, these judges charge
153//! [`crate::footprint::video_frame_bytes_upper_bound`] — the same
154//! dimension alignment and per-plane overhead at the widest per-pixel
155//! rate the census finds — rather than a bare `w * h * rate`, which
156//! omits both and could land *below* the accurate path it was standing
157//! in for. A conservative fallback that can under-state is not
158//! conservative.
159//!
160//! **These are defense in depth, not a proof.** Each bounds what it was
161//! built to bound; together they cover every interposition point FFmpeg
162//! exposes, which is not the same as covering FFmpeg.
163//!
164//! ## What the demux seats cannot reach, and why they exist anyway
165//!
166//! `avformat_open_input` and `avformat_find_stream_info` build the
167//! attached picture, the extradata and the coded side data out of the
168//! file themselves. The attachment and parameter seats in the table
169//! above therefore measure this crate's *copies* of buffers libavformat
170//! has already allocated — too late, by construction, to have prevented
171//! the original.
172//!
173//! A parser cannot allocate from bytes it was never handed, so the
174//! input is bounded instead: that is what the probe knobs and the byte
175//! meter are for. What is **not** bounded is allocation *amplification*
176//! inside a parser — a container can describe, in a handful of bytes, a
177//! structure whose in-memory form is far larger, and nothing outside
178//! libavformat can observe it happen. Bounding that output is the
179//! substrate's own hardening territory; FFmpeg keeps `max_streams`,
180//! `max_index_size` and `max_picture_buffer` for it, and this crate
181//! sets the first.
182//!
183//! The hard meter also does not reach the **path** entrypoint: it needs
184//! an `AVIOContext` this crate owns, and a path is opened by
185//! libavformat's own protocol layer. The probe knobs still apply there;
186//! a caller who wants the meter on a file opens it as a reader.
187//!
188//! That gap is **tier three**, and it is named rather than hedged: see
189//! the [contract][gov] for the boundary and for the OS-level instrument
190//! a deployment needing a hard memory bound puts underneath all of
191//! this. This crate is not a hypervisor for FFmpeg, and its seats
192//! compose with that instrument rather than replacing it.
193//!
194//! [gov]: mediadecode::adapter#the-resource-governance-contract
195//!
196//! And the capstone, which is what every seat in this table is finally
197//! for: **a judge must dominate the allocator's arithmetic, not the
198//! payload's.** A budget compared against what the bytes weigh is not a
199//! budget on what will be spent — see [`crate::footprint`] for the
200//! measured gap and for the two judges that were caught paying it.
201//!
202//! And the corollary the ninth bought, which is about *whether* to
203//! carry at all rather than how much: **a payload that carries
204//! addresses instead of bytes is uncarriable.** `AV_PKT_FLAG_TRUSTED`
205//! marks one — the wrapped-`AVFrame` producers use it for a body that
206//! is an `AVFrame` pointer structure — and copying it mints a carrier
207//! that passes every property this table exists to guarantee and
208//! dangles the moment its source drops. It is refused on both legs
209//! ([`payload_of`] and the reverse builders), because either alone
210//! leaves the loop open. See [`TrustedPayload`].
211//!
212//! And the corollary the seventh bought, about the *inputs* to every
213//! guard above rather than the guards themselves: **a number a file
214//! chooses is judged or refused, never clipped.** A seat that bounds a
215//! byte product still trusts the fields the product is computed from,
216//! so a clamped sample count or channel count does not trip any budget
217//! — it produces a smaller, plausible frame that no ceiling has any
218//! reason to stop. Two of those were live on the audio path (a floored
219//! negative `nb_samples`, a channel count clipped to `u8::MAX`), and
220//! both turned a malformed header into a well-formed-looking frame,
221//! which is strictly worse than an error. The audio road now carries no
222//! lossy clamp; the one floor left, `sample_rate`, is censused at its
223//! site with the reason it is metadata and sizes nothing.
224//!
225//! [law]: mediadecode::adapter#the-d-seat-amputation-contract
226
227use std::{
228  fmt,
229  sync::{Arc, OnceLock},
230};
231
232use derive_more::{IsVariant, TryUnwrap, Unwrap};
233
234/// The bytes every packet and frame this crate produces are carried in.
235///
236/// Owned, `Send + Sync`, `'static`, and clone-is-a-refcount-bump: the
237/// core's [D-seat amputation contract][law], satisfied. Nothing inside
238/// reaches back into libavcodec.
239///
240/// # Why it is opaque
241///
242/// The obvious spelling was the bare `Arc<[u8]>` this type wraps, and
243/// 0.9.0's first cut used it. It is opaque for one reason, and the
244/// reason is not aesthetics:
245///
246/// **`Arc<[u8]>` is one storage strategy, and it is not going to be the
247/// only one.** Every exit currently allocates, copies, and frees per
248/// frame; a decode loop at 4K is asking the global allocator for eight
249/// megabytes sixty times a second and handing it back. The recorded
250/// answer is a plane pool — reusable slabs handed out at the boundary
251/// and returned when the last consumer drops them
252/// ([issue #35](https://github.com/findit-studio/mediadecode/issues/35)).
253/// A pooled slab is a different carrier with the same contract: still
254/// owned, still `Send + Sync`, still refcount-cloned, still holding no
255/// FFmpeg lifetime.
256///
257/// If the carrier were `Arc<[u8]>` in the public aliases, adding the
258/// pool would change the type of every frame and every packet in the
259/// crate — a breaking release for a change consumers cannot observe.
260/// Behind this newtype it is a new arm of a **private** enum: no
261/// signature moves, no consumer recompiles differently, and the
262/// `AsRef<[u8]>` a consumer actually programs against is unchanged.
263/// That extension point *is* this type's justification for existing.
264///
265/// The enum has exactly one arm today. It gains the second when the
266/// pool is built and not before — this codebase does not carry members
267/// nothing can produce.
268///
269/// [law]: mediadecode::adapter#the-d-seat-amputation-contract
270#[derive(Clone, Default, PartialEq, Eq, Hash)]
271pub struct FfmpegBytes(Inner);
272
273/// The storage behind [`FfmpegBytes`]. **Private, and the point.**
274///
275/// One arm today; see the type's own docs for the arm that is coming
276/// and why it can arrive without a breaking release.
277#[derive(Clone, PartialEq, Eq, Hash)]
278enum Inner {
279  /// A refcounted slice, allocated by the copy at the boundary.
280  Shared(Arc<[u8]>),
281}
282
283impl Default for Inner {
284  #[inline]
285  fn default() -> Self {
286    Self::Shared(shared_empty())
287  }
288}
289
290impl FfmpegBytes {
291  /// Copies `bytes` into a fresh carrier.
292  ///
293  /// **The copy site.** Every exit in this crate lands here or on
294  /// [`Self::empty`], so "one copy at the boundary" is a property of
295  /// one constructor rather than a promise thirty call sites keep —
296  /// and it is the one place a future pooled arm has to be taught
297  /// about.
298  ///
299  /// Public because the reverse direction needs it: a consumer
300  /// building a packet to feed back into a decoder has bytes and needs
301  /// a carrier, and the alternative is an opaque type nobody outside
302  /// this crate can construct.
303  ///
304  /// A zero-length copy lands on the shared empty allocation rather
305  /// than minting its own.
306  #[inline]
307  pub fn copy_from_slice(bytes: &[u8]) -> Self {
308    if bytes.is_empty() {
309      return Self::empty();
310    }
311    Self(Inner::Shared(Arc::from(bytes)))
312  }
313
314  /// The zero-length carrier, shared.
315  ///
316  /// Placeholder plane slots and payload-less packets are frequent — a
317  /// video frame allocates four slots and populates one to three of
318  /// them — and each would otherwise be its own `Arc` header
319  /// allocation. One empty allocation for the process, cloned by
320  /// refcount, instead.
321  #[inline]
322  pub fn empty() -> Self {
323    Self(Inner::Shared(shared_empty()))
324  }
325
326  /// Builds a carrier of `rows * row_bytes` bytes by writing each row
327  /// in turn — **one allocation, no staging buffer**.
328  ///
329  /// This is the road a padded plane takes. FFmpeg lays such a plane
330  /// out `linesize` bytes per row while only the first `row_bytes` of
331  /// each are the decoder's output, so the copy has to be row-wise and
332  /// the destination is contiguous. The obvious spelling — build a
333  /// `Vec`, then `Arc::from` it — allocates the whole plane **twice**
334  /// and copies it twice, so a 250 MiB frame peaks at 750 MiB counting
335  /// FFmpeg's own. Writing the rows straight into
336  /// `Arc::new_uninit_slice` leaves the unavoidable 2×: FFmpeg's plane
337  /// and ours.
338  ///
339  /// `row(i)` must answer a slice of exactly `row_bytes`; a shorter or
340  /// longer one is a bug in the caller's geometry and panics rather
341  /// than leaving the tail of the allocation uninitialised. That
342  /// assertion is what discharges the initialisation contract for the
343  /// `assume_init` below: the loop visits every row, each row fills its
344  /// full width, and `rows * row_bytes` is the whole allocation.
345  ///
346  /// Crate-internal: the public face is [`Self::copy_from_slice`], and
347  /// this shape only makes sense to a caller that already holds a
348  /// strided picture.
349  ///
350  /// # Panics
351  ///
352  /// If `rows * row_bytes` overflows `usize`, or if `row(i)` answers a
353  /// slice that is not `row_bytes` long. Callers reach this only after
354  /// the geometry has been validated and the total checked against
355  /// [`FrameLimits`](crate::FrameLimits), so both are unreachable from
356  /// input.
357  pub(crate) fn from_rows<'a>(
358    rows: usize,
359    row_bytes: usize,
360    mut row: impl FnMut(usize) -> &'a [u8],
361  ) -> Option<Self> {
362    let len = rows.checked_mul(row_bytes)?;
363    if len == 0 {
364      return Some(Self::empty());
365    }
366    let mut uninit = Arc::<[u8]>::new_uninit_slice(len);
367    {
368      let slots =
369        Arc::get_mut(&mut uninit).expect("the allocation was made here and has not been shared");
370      for index in 0..rows {
371        let source = row(index);
372        if source.len() != row_bytes {
373          // A length that arrives from a caller is an input, not a
374          // promise: refuse rather than copy `row_bytes` out of a
375          // shorter slice. The half-built `Arc` drops with this
376          // return, and every byte of it is still `MaybeUninit`.
377          return None;
378        }
379        let start = index * row_bytes;
380        // `MaybeUninit<u8>` has the same layout as `u8`, so the source
381        // slice can be viewed as one and copied wholesale.
382        let destination = &mut slots[start..start + row_bytes];
383        // SAFETY: `&[u8]` and `&[MaybeUninit<u8>]` have identical
384        // layout, and the cast is read-only on the source side.
385        let source: &[core::mem::MaybeUninit<u8>] = unsafe {
386          core::slice::from_raw_parts(
387            source.as_ptr().cast::<core::mem::MaybeUninit<u8>>(),
388            row_bytes,
389          )
390        };
391        destination.copy_from_slice(source);
392      }
393    }
394    // SAFETY: the loop above wrote every one of the `rows * row_bytes`
395    // slots — `rows` iterations, each filling exactly `row_bytes`
396    // consecutive bytes starting at `index * row_bytes`, with the
397    // length of each source row checked before the copy and the whole
398    // gather abandoned if one disagreed. Nothing in the allocation is
399    // left uninitialised on this road.
400    Some(Self(Inner::Shared(unsafe { uninit.assume_init() })))
401  }
402
403  /// The bytes, as a slice.
404  ///
405  /// The same answer [`AsRef::as_ref`] gives; inherent so a caller
406  /// reaching through a `&FfmpegBytes` does not have to name the trait.
407  #[inline]
408  pub fn as_slice(&self) -> &[u8] {
409    match &self.0 {
410      Inner::Shared(bytes) => bytes,
411    }
412  }
413
414  /// Number of bytes carried.
415  #[inline]
416  pub fn len(&self) -> usize {
417    self.as_slice().len()
418  }
419
420  /// `true` when this carries no bytes.
421  #[inline]
422  pub fn is_empty(&self) -> bool {
423    self.as_slice().is_empty()
424  }
425
426  /// `true` when both handles name the same allocation — a clone of
427  /// one another, rather than two copies that happen to be equal.
428  ///
429  /// The property the amputation contract is really about: `Clone` on
430  /// a message is a refcount bump. `PartialEq` answers a different
431  /// question (do these hold the same bytes), and a test that wants to
432  /// prove the clone did not copy has to ask this one.
433  #[inline]
434  pub fn ptr_eq(&self, other: &Self) -> bool {
435    match (&self.0, &other.0) {
436      (Inner::Shared(a), Inner::Shared(b)) => Arc::ptr_eq(a, b),
437    }
438  }
439}
440
441impl AsRef<[u8]> for FfmpegBytes {
442  #[inline]
443  fn as_ref(&self) -> &[u8] {
444    self.as_slice()
445  }
446}
447
448impl fmt::Debug for FfmpegBytes {
449  /// Length only, never the bytes.
450  ///
451  /// A derived `Debug` would print a decoded 4K plane one integer at a
452  /// time; this type is reached from the derived `Debug` of every
453  /// packet, frame and side-data entry in the crate, so the terse form
454  /// is the one that keeps those useful. Mirrors what `FfmpegBuffer`'s
455  /// own hand-written `Debug` did through 0.8.
456  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
457    f.debug_struct("FfmpegBytes")
458      .field("len", &self.len())
459      .finish()
460  }
461}
462
463/// The process-wide empty `Arc`, so a zero-length carrier costs a
464/// refcount bump rather than an allocation.
465fn shared_empty() -> Arc<[u8]> {
466  static EMPTY: OnceLock<Arc<[u8]>> = OnceLock::new();
467  EMPTY.get_or_init(|| Arc::from(&[][..])).clone()
468}
469
470/// Payload for [`PacketBufferError::PacketTooLarge`].
471///
472/// A packet's payload is larger than the budget in force.
473///
474/// Refused **before** the copy: 0.8 answered a claimed payload with a
475/// refcount, so an absurd `size` cost nothing; 0.9 answers it with an
476/// allocation, so the claim has to be judged first.
477#[derive(Copy, Clone, Debug, PartialEq, Eq, thiserror::Error)]
478#[error("a {bytes}-byte packet payload exceeds the {limit}-byte budget")]
479pub struct PacketTooLarge {
480  bytes: usize,
481  limit: usize,
482}
483
484impl PacketTooLarge {
485  /// Constructs a `PacketTooLarge` payload.
486  #[cfg_attr(not(tarpaulin), inline(always))]
487  pub const fn new(bytes: usize, limit: usize) -> Self {
488    Self { bytes, limit }
489  }
490  /// The payload length the packet declared.
491  #[cfg_attr(not(tarpaulin), inline(always))]
492  pub const fn bytes(&self) -> usize {
493    self.bytes
494  }
495  /// The budget in force.
496  #[cfg_attr(not(tarpaulin), inline(always))]
497  pub const fn limit(&self) -> usize {
498    self.limit
499  }
500}
501
502/// Payload for [`PacketBufferError::Bounds`].
503///
504/// The payload does not lie inside the packet's own buffer.
505/// `AVPacket` guarantees it does; a packet that says otherwise is
506/// malformed, and wrapping it would hand out a view over memory the
507/// buffer does not own.
508#[derive(Copy, Clone, Debug, PartialEq, Eq, thiserror::Error)]
509#[error("a {len}-byte payload at offset {offset} does not lie inside a {size}-byte buffer")]
510pub struct Bounds {
511  offset: usize,
512  len: usize,
513  size: usize,
514}
515
516impl Bounds {
517  /// Constructs a `Bounds` payload.
518  #[cfg_attr(not(tarpaulin), inline(always))]
519  pub const fn new(offset: usize, len: usize, size: usize) -> Self {
520    Self { offset, len, size }
521  }
522  /// Where the payload starts inside the buffer.
523  #[cfg_attr(not(tarpaulin), inline(always))]
524  pub const fn offset(&self) -> usize {
525    self.offset
526  }
527  /// The payload's length in bytes.
528  #[cfg_attr(not(tarpaulin), inline(always))]
529  pub const fn len(&self) -> usize {
530    self.len
531  }
532  /// `true` when the payload is zero bytes long.
533  #[cfg_attr(not(tarpaulin), inline(always))]
534  pub const fn is_empty(&self) -> bool {
535    self.len == 0
536  }
537  /// The buffer's own length in bytes.
538  #[cfg_attr(not(tarpaulin), inline(always))]
539  pub const fn size(&self) -> usize {
540    self.size
541  }
542}
543
544/// Payload for [`PacketBufferError::SideDataEntries`].
545///
546/// A packet declares more side-data entries than this crate will
547/// walk, or a negative count.
548///
549/// The cap bounds the work a crafted packet can demand *before* it is
550/// refused. It cannot trip on anything FFmpeg's own packet API
551/// produces: both `av_packet_new_side_data` and
552/// `av_packet_add_side_data` replace an entry of the same type, so a
553/// packet carries at most one entry per named type — forty-three in
554/// this build, and the cap tracks that number if it ever grows past
555/// the floor.
556#[derive(Copy, Clone, Debug, PartialEq, Eq, thiserror::Error)]
557#[error("a packet declaring {count} side-data entries cannot be carried (limit {cap})")]
558pub struct SideDataEntries {
559  count: i32,
560  cap: usize,
561}
562
563impl SideDataEntries {
564  /// Constructs a `SideDataEntries` payload.
565  #[cfg_attr(not(tarpaulin), inline(always))]
566  pub const fn new(count: i32, cap: usize) -> Self {
567    Self { count, cap }
568  }
569  /// The count the packet declared.
570  #[cfg_attr(not(tarpaulin), inline(always))]
571  pub const fn count(&self) -> i32 {
572    self.count
573  }
574  /// The most entries this crate will walk.
575  #[cfg_attr(not(tarpaulin), inline(always))]
576  pub const fn cap(&self) -> usize {
577    self.cap
578  }
579}
580
581/// Payload for [`PacketBufferError::SideDataArray`].
582///
583/// A packet declares side-data entries and carries no array to read
584/// them from.
585///
586/// Malformed, and named rather than read as "no side data": a null
587/// array with a positive count is the same silent loss as a truncated
588/// copy, reached through the pointer instead of the cap.
589#[derive(Copy, Clone, Debug, PartialEq, Eq, thiserror::Error)]
590#[error("a packet declaring {count} side-data entries carries no array")]
591pub struct SideDataArray {
592  count: i32,
593}
594
595impl SideDataArray {
596  /// Constructs a `SideDataArray` payload.
597  #[cfg_attr(not(tarpaulin), inline(always))]
598  pub const fn new(count: i32) -> Self {
599    Self { count }
600  }
601  /// The count the packet declared.
602  #[cfg_attr(not(tarpaulin), inline(always))]
603  pub const fn count(&self) -> i32 {
604    self.count
605  }
606}
607
608/// Payload for [`PacketBufferError::SideDataPayload`].
609#[derive(Copy, Clone, Debug, PartialEq, Eq, thiserror::Error)]
610#[error("side-data entry {index} declares {size} bytes and carries no data")]
611pub struct SideDataPayload {
612  index: usize,
613  size: usize,
614}
615
616impl SideDataPayload {
617  /// Constructs a `SideDataPayload` payload.
618  #[cfg_attr(not(tarpaulin), inline(always))]
619  pub const fn new(index: usize, size: usize) -> Self {
620    Self { index, size }
621  }
622  /// The entry's position in the packet's array.
623  #[cfg_attr(not(tarpaulin), inline(always))]
624  pub const fn index(&self) -> usize {
625    self.index
626  }
627  /// The length the entry declared.
628  #[cfg_attr(not(tarpaulin), inline(always))]
629  pub const fn size(&self) -> usize {
630    self.size
631  }
632}
633
634/// Payload for [`PacketBufferError::SideDataBytes`].
635///
636/// A packet's side data is larger than this crate will copy.
637#[derive(Copy, Clone, Debug, PartialEq, Eq, thiserror::Error)]
638#[error("{bytes} bytes of side data cannot be carried (limit {cap})")]
639pub struct SideDataBytes {
640  bytes: usize,
641  cap: usize,
642}
643
644impl SideDataBytes {
645  /// Constructs a `SideDataBytes` payload.
646  #[cfg_attr(not(tarpaulin), inline(always))]
647  pub const fn new(bytes: usize, cap: usize) -> Self {
648    Self { bytes, cap }
649  }
650  /// The total the packet's entries reached.
651  #[cfg_attr(not(tarpaulin), inline(always))]
652  pub const fn bytes(&self) -> usize {
653    self.bytes
654  }
655  /// The most bytes this crate will copy.
656  #[cfg_attr(not(tarpaulin), inline(always))]
657  pub const fn cap(&self) -> usize {
658    self.cap
659  }
660}
661
662/// Payload for [`PacketBufferError::UnrepresentableFlags`].
663///
664/// A packet carries flag bits the portable vocabulary cannot hold.
665///
666/// `mediadecode`'s `PacketFlags` is a `u8` bit set, and every packet
667/// flag FFmpeg names today lives in that byte — so this cannot fire
668/// against this build. It exists so that the day one does not, the
669/// packet is refused by name instead of arriving with a bit quietly
670/// missing: the same rule the rest of this boundary keeps.
671#[derive(Copy, Clone, Debug, PartialEq, Eq, thiserror::Error)]
672#[error("packet flags {raw:#x} do not fit the portable flag set")]
673pub struct UnrepresentableFlags {
674  raw: i32,
675}
676
677impl UnrepresentableFlags {
678  /// Constructs an `UnrepresentableFlags` payload.
679  #[cfg_attr(not(tarpaulin), inline(always))]
680  pub const fn new(raw: i32) -> Self {
681    Self { raw }
682  }
683  /// `AVPacket.flags` as FFmpeg wrote it.
684  #[cfg_attr(not(tarpaulin), inline(always))]
685  pub const fn raw(&self) -> i32 {
686    self.raw
687  }
688}
689
690/// Payload for [`PacketBufferError::SideDataAlloc`].
691///
692/// Out of memory copying a side-data entry.
693#[derive(Copy, Clone, Debug, PartialEq, Eq, thiserror::Error)]
694#[error("out of memory copying {size} bytes of side data")]
695pub struct SideDataAlloc {
696  size: usize,
697}
698
699impl SideDataAlloc {
700  /// Constructs a `SideDataAlloc` payload.
701  #[cfg_attr(not(tarpaulin), inline(always))]
702  pub const fn new(size: usize) -> Self {
703    Self { size }
704  }
705  /// The entry's length in bytes.
706  #[cfg_attr(not(tarpaulin), inline(always))]
707  pub const fn size(&self) -> usize {
708    self.size
709  }
710}
711/// Why a packet could not be carried across the boundary — its payload,
712/// or the side data that comes with it.
713///
714/// Every arm means the bytes are real and this crate could not carry
715/// them — never that there were none. "No payload" is `Ok(None)` from
716/// [`payload_of`], and keeping the two apart is the whole point of the
717/// type: a demuxer that reads a malformed packet as an empty marker
718/// drops a video packet and carries on as though the file said so. The
719/// side-data arms exist for the same reason one tier along — a packet
720/// whose side data cannot be carried whole is refused, never delivered
721/// with some of it.
722#[derive(Copy, Clone, Debug, PartialEq, Eq, thiserror::Error, IsVariant, Unwrap, TryUnwrap)]
723#[unwrap(ref, ref_mut)]
724#[try_unwrap(ref, ref_mut)]
725pub enum PacketBufferError {
726  /// The payload is larger than the budget in force. Refused before
727  /// the copy.
728  #[error(transparent)]
729  PacketTooLarge(#[from] PacketTooLarge),
730
731  /// The payload does not lie inside the packet's own buffer.
732  #[error(transparent)]
733  Bounds(#[from] Bounds),
734
735  /// A packet declares more side-data entries than this crate will
736  /// walk, or a negative count.
737  #[error(transparent)]
738  SideDataEntries(#[from] SideDataEntries),
739
740  /// A packet declares side-data entries and carries no array to read
741  /// them from.
742  #[error(transparent)]
743  SideDataArray(#[from] SideDataArray),
744
745  /// A side-data entry declares bytes it does not carry.
746  #[error(transparent)]
747  SideDataPayload(#[from] SideDataPayload),
748
749  /// A packet's side data is larger than this crate will copy.
750  #[error(transparent)]
751  SideDataBytes(#[from] SideDataBytes),
752
753  /// A packet carries flag bits the portable vocabulary cannot hold.
754  #[error(transparent)]
755  UnrepresentableFlags(#[from] UnrepresentableFlags),
756
757  /// A packet is marked `AV_PKT_FLAG_TRUSTED`, so its payload may hold
758  /// pointers rather than bytes. See [`TrustedPayload`].
759  #[error(transparent)]
760  TrustedPayload(#[from] TrustedPayload),
761
762  /// The capture itself failed — an allocation on the owned lane, a
763  /// refcount on the view lane. See [`CaptureFailed`].
764  #[error(transparent)]
765  CaptureFailed(#[from] CaptureFailed),
766
767  /// The payload's buffer is referenced by something other than the
768  /// packet it came from. See [`SharedPayload`].
769  #[error(transparent)]
770  SharedPayload(#[from] SharedPayload),
771
772  /// Out of memory copying a side-data entry.
773  #[error(transparent)]
774  SideDataAlloc(#[from] SideDataAlloc),
775}
776
777impl PacketBufferError {
778  /// Whether the demux session should **park** the packet this refusal
779  /// came from and re-attempt it on the next pull.
780  ///
781  /// Deliberately not public, and deliberately named for the decision
782  /// rather than for a property of the error. It was briefly public as
783  /// `is_transient`, which promised more than an error enum can know:
784  /// whether retrying helps depends on *what was retried*.
785  /// `SharedPayload` is permanent for a caller who keeps their other
786  /// reference and retryable the moment they drop it;
787  /// `CaptureFailed` is worth another attempt only if the packet still
788  /// exists to attempt, which on a **consuming** conversion it does
789  /// not. Only the demux loop knows both halves — it still holds the
790  /// packet, and it knows nobody else does.
791  ///
792  /// So this answers one question for one caller. An allocation that
793  /// failed says nothing about the packet, and the demux loop is
794  /// holding the bytes; everything else is a fact about the packet
795  /// itself, and parking it would answer every later pull with the same
796  /// error instead of letting the session make progress.
797  ///
798  /// **The door left open:** a public retry signal would have to know
799  /// which operation produced the error and what the caller still
800  /// holds — an operation-aware answer, not a property of this enum.
801  /// If one is ever wanted it is designed then, not approximated now.
802  #[inline]
803  pub(crate) const fn parks_in_demux(&self) -> bool {
804    matches!(self, Self::CaptureFailed(_) | Self::SideDataAlloc(_))
805  }
806}
807
808/// `AV_PKT_FLAG_TRUSTED` as the bit the portable `PacketFlags` byte
809/// carries it in.
810///
811/// The core vocabulary deliberately does not *name* this flag — it is
812/// FFmpeg's, not a portable fact about packets — but `from_bits_retain`
813/// keeps the bit, so this crate can recognise its own flag coming back
814/// without the core growing a constant for it.
815pub(crate) const TRUSTED_BIT: u8 = ffmpeg_next::ffi::AV_PKT_FLAG_TRUSTED as u8;
816
817/// Compile-time proof that the flag really does fit the byte, so the
818/// cast above cannot silently become a different bit.
819const _: () = {
820  assert!(
821    ffmpeg_next::ffi::AV_PKT_FLAG_TRUSTED > 0
822      && ffmpeg_next::ffi::AV_PKT_FLAG_TRUSTED <= u8::MAX as std::ffi::c_int,
823    "AV_PKT_FLAG_TRUSTED no longer fits the portable flag byte",
824  );
825};
826
827/// Payload for [`PacketBufferError::TrustedPayload`] and
828/// [`crate::boundary::PacketBuildError::TrustedPayload`].
829///
830/// A packet carrying `AV_PKT_FLAG_TRUSTED`, refused on both legs.
831///
832/// # Why a flag makes a payload uncarriable
833///
834/// `AV_PKT_FLAG_TRUSTED` is FFmpeg's marker for a packet whose bytes
835/// came from a source the *decoder* may treat as its own — and the
836/// wrapped-AVFrame producers use it for exactly that: the payload is
837/// not media, it is a **structure containing pointers to other live
838/// objects** (an `AVFrame` and its buffers), passed by address between
839/// components inside one FFmpeg pipeline.
840///
841/// This crate copies bytes. A pointer copied by value is not owned by
842/// the copy — and that is not a gap this crate can close, because there
843/// is no bound on what a payload's pointers might reach. So the
844/// amputation has a corollary:
845///
846/// > **A payload that carries addresses instead of bytes cannot be
847/// > carried.** Copying it produces a message that looks owned, is
848/// > `Send + Sync + 'static` by every type-level test, and dangles the
849/// > moment its source is dropped — a use-after-free reachable through
850/// > entirely safe API.
851///
852/// Refusing is not conservatism, it is the only correct answer: the
853/// contract this crate exists to keep says every byte leaving FFmpeg is
854/// copied once into memory Rust owns, and a pointer cannot be.
855///
856/// Refused at **both** legs, because either one alone leaves the loop
857/// open: copy-out ([`payload_of`]) is where such a packet would enter
858/// the graph, and the reverse builders are where a flag that survived
859/// some other route would be handed back to a decoder that trusts it.
860#[derive(Debug, Clone, Copy, PartialEq, Eq)]
861pub struct TrustedPayload {
862  len: usize,
863}
864
865impl TrustedPayload {
866  /// Constructs a `TrustedPayload` payload.
867  #[inline]
868  pub const fn new(len: usize) -> Self {
869    Self { len }
870  }
871  /// How many bytes the packet declared.
872  #[inline]
873  pub const fn len(&self) -> usize {
874    self.len
875  }
876  /// Whether the refused packet declared no bytes.
877  #[inline]
878  pub const fn is_empty(&self) -> bool {
879    self.len == 0
880  }
881}
882
883impl core::fmt::Display for TrustedPayload {
884  fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
885    write!(
886      f,
887      "packet of {} bytes carries AV_PKT_FLAG_TRUSTED; a payload that may hold \
888       pointers to other objects cannot be copied into an owned carrier",
889      self.len,
890    )
891  }
892}
893
894impl std::error::Error for TrustedPayload {}
895
896/// The payload of a raw `AVPacket`, copied out.
897///
898/// Shared by the four timed boundary conversions, the attachment
899/// conversion, and the demuxer's capture of `AVStream.attached_pic` —
900/// an `AVPacket` embedded in the stream by value, which no safe
901/// wrapper reaches. One implementation, so the empty-versus-malformed
902/// distinction cannot drift between them.
903///
904/// `Ok(None)` means the packet carries no payload at all: an empty
905/// marker, which some demuxers emit. That is a fact about the packet
906/// and is kept apart from [`PacketBufferError`], which is a failure to
907/// take a payload that *is* there.
908///
909/// A packet whose `buf` is null — a stack- or arena-allocated
910/// `AVPacket` — still reads as "no payload", exactly as it did before
911/// the amputation. It is tempting now that the bytes are copied to
912/// serve those from `data` / `size` directly, and that is precisely the
913/// case with no owning buffer to bound the read against: the claim
914/// would have to be taken on faith.
915///
916/// # Safety
917///
918/// `pkt` must be a live `*const AVPacket` for the duration of this
919/// call.
920pub(crate) unsafe fn payload_of<C: crate::FfmpegCarrier + crate::CarrierOps>(
921  pkt: *const ffmpeg_next::ffi::AVPacket,
922  budget: usize,
923  provenance: PayloadProvenance,
924) -> Result<Option<C::Buffer>, PacketBufferError> {
925  // SAFETY: `pkt` is live per the contract above; `.buf`, `.data` and
926  // `.size` are public fields on `AVPacket`, and `buf` may be null
927  // (stack-allocated packets).
928  let buf_ptr = unsafe { (*pkt).buf };
929  let data_ptr = unsafe { (*pkt).data };
930  let size_raw = unsafe { (*pkt).size };
931  // **The uncarriable-payload refusal, ahead of everything.** See
932  // [`TrustedPayload`]: this flag marks a payload that may be a
933  // structure of pointers into other live objects rather than media
934  // bytes, and copying those bytes would mint an owned-looking carrier
935  // full of addresses that dangle as soon as the source is dropped.
936  //
937  // Judged before the empty-payload answer as well as before the copy:
938  // "there is nothing to take here" is the wrong reply to a packet this
939  // crate must not take *anything* from.
940  //
941  // SAFETY: `pkt` is live per the contract; `flags` is a public `c_int`
942  // field, read as the integer it is.
943  let flags_raw = unsafe { (*pkt).flags };
944  if flags_raw & ffmpeg_next::ffi::AV_PKT_FLAG_TRUSTED != 0 {
945    return Err(PacketBufferError::TrustedPayload(TrustedPayload::new(
946      size_raw.max(0) as usize,
947    )));
948  }
949  if buf_ptr.is_null() || data_ptr.is_null() || size_raw <= 0 {
950    return Ok(None);
951  }
952  let len = size_raw as usize;
953  // **The budget, before anything is read or allocated.** Judged on
954  // the declared length rather than on what the copy turns out to
955  // cost, because the point is to refuse without paying. Ahead of the
956  // bounds check too: a forged `size` is exactly what both exist for,
957  // and the cheaper judgement goes first.
958  if len > budget {
959    return Err(PacketBufferError::PacketTooLarge(PacketTooLarge::new(
960      len, budget,
961    )));
962  }
963  // SAFETY: `buf_ptr` is a live `AVBufferRef` owned by the packet.
964  let buf_data = unsafe { (*buf_ptr).data };
965  let size = unsafe { (*buf_ptr).size };
966  if buf_data.is_null() {
967    return Err(PacketBufferError::Bounds(Bounds::new(0, len, size)));
968  }
969  // `AVPacket` guarantees `data` lies within
970  // `buf->data .. buf->data + buf->size`. Checked before the copy, not
971  // instead of it: 0.8 formed a view over the claimed range and a
972  // malformed `size` handed out a slice nobody read; 0.9 reads every
973  // byte of it, so an unchecked claim is an out-of-bounds read rather
974  // than a latent one.
975  let offset = (data_ptr as usize).wrapping_sub(buf_data as usize);
976  match offset.checked_add(len) {
977    Some(end) if end <= size => {}
978    _ => {
979      return Err(PacketBufferError::Bounds(Bounds::new(offset, len, size)));
980    }
981  }
982  // **The sharing question, asked before any byte is read.**
983  //
984  // Everything below reads the payload — the view lane by handing out a
985  // span over it, the owned lane by copying it — so a buffer somebody
986  // else references has to be classified before it is touched. What
987  // matters is not the count but **who** the other holder is, and only
988  // the caller of this function knows that: see [`PayloadProvenance`]
989  // for the dichotomy and [`PayloadProvenance::route`] for the table.
990  //
991  // Placed here rather than inside the capture so the ordering is a
992  // property of this function rather than of two lane impls: nothing
993  // between the bounds proof and this decision touches the payload.
994  //
995  // SAFETY: `buf_ptr` is a live `AVBufferRef` owned by the packet;
996  // `av_buffer_get_ref_count` only reads its atomic.
997  let references = unsafe { ffmpeg_next::ffi::av_buffer_get_ref_count(buf_ptr.cast_const()) };
998  let route = provenance.route(references != 1);
999  if route == CaptureRoute::Refuse {
1000    return Err(PacketBufferError::SharedPayload(SharedPayload::new(
1001      references,
1002    )));
1003  }
1004
1005  // **The capture, and the only step the two lanes spell differently.**
1006  // Everything above — the `TRUSTED` refusal, the empty answer, the
1007  // budget, the extent proof — is shared, which is what keeps the view
1008  // lane from having to re-earn a single one of them.
1009  //
1010  // The **packet-payload** capture, not the general one: this range is
1011  // an `AVPacket`'s payload inside that packet's own buffer, which is
1012  // the one place libavformat's trailing-padding contract applies. The
1013  // view lane records that, and the send leg is the only thing that
1014  // reads it back — see `boundary::share_or_copy`.
1015  //
1016  // SAFETY: `offset + len` was just proved to lie inside `buf_ptr`'s
1017  // own `size`, and `buf_ptr` is a live `AVBufferRef` the packet owns.
1018  let carried = match route {
1019    CaptureRoute::Capture => unsafe { C::capture_packet_payload(buf_ptr, offset, len) },
1020    CaptureRoute::Copy => {
1021      // A demux-delivered packet whose buffer libavformat also holds.
1022      // Reading it here is race-free — every other reference is
1023      // C-owned, no `ffmpeg_next::Packet` wraps one, and this crate
1024      // holds the `AVFormatContext` exclusively for the duration of
1025      // the call — but the copy is what keeps that argument confined
1026      // to *this* call instead of to the carrier's whole life.
1027      //
1028      // SAFETY: the extent was proved above and `buf_data` is
1029      // non-null.
1030      let bytes = unsafe { core::slice::from_raw_parts(buf_data.add(offset).cast_const(), len) };
1031      C::from_bytes(bytes)
1032    }
1033    // Answered before the payload was touched.
1034    CaptureRoute::Refuse => unreachable!("a refusal returns above"),
1035  };
1036  carried
1037    .map(Some)
1038    .ok_or(PacketBufferError::CaptureFailed(CaptureFailed::new(len)))
1039}
1040
1041#[cfg(test)]
1042mod tests {
1043  use super::*;
1044  use crate::limits::DEFAULT_MAX_PACKET_BYTES;
1045  use ffmpeg_next::{Packet, packet::Ref};
1046
1047  #[test]
1048  fn a_real_payload_is_copied_out_whole() {
1049    let packet = Packet::copy(&[1u8, 2, 3, 4]);
1050    // SAFETY: `packet` owns a live `AVPacket` for the call.
1051    let payload = unsafe {
1052      payload_of::<crate::Owned>(
1053        packet.as_ptr(),
1054        DEFAULT_MAX_PACKET_BYTES,
1055        PayloadProvenance::CallerSupplied,
1056      )
1057    }
1058    .expect("a well-formed packet is carriable")
1059    .expect("present");
1060    assert_eq!(payload.as_ref(), &[1, 2, 3, 4]);
1061  }
1062
1063  #[test]
1064  fn the_copy_outlives_the_packet_it_came_from() {
1065    // The whole point of the amputation: FFmpeg's allocation is gone
1066    // and the bytes are still here.
1067    let packet = Packet::copy(&[9u8, 8, 7]);
1068    // SAFETY: `packet` owns a live `AVPacket` for the call.
1069    let payload = unsafe {
1070      payload_of::<crate::Owned>(
1071        packet.as_ptr(),
1072        DEFAULT_MAX_PACKET_BYTES,
1073        PayloadProvenance::CallerSupplied,
1074      )
1075    }
1076    .expect("carriable")
1077    .expect("present");
1078    let shared = payload.clone();
1079    assert!(shared.ptr_eq(&payload), "the clone copied the bytes");
1080    drop(packet);
1081    drop(payload);
1082    assert_eq!(shared.as_ref(), &[9, 8, 7]);
1083  }
1084
1085  #[test]
1086  fn an_empty_packet_has_no_payload_rather_than_a_failure() {
1087    let packet = Packet::empty();
1088    // SAFETY: `packet` owns a live `AVPacket` for the call.
1089    assert!(
1090      unsafe {
1091        payload_of::<crate::Owned>(
1092          packet.as_ptr(),
1093          DEFAULT_MAX_PACKET_BYTES,
1094          PayloadProvenance::CallerSupplied,
1095        )
1096      }
1097      .expect("not a failure")
1098      .is_none()
1099    );
1100  }
1101
1102  #[test]
1103  fn a_payload_outside_its_own_buffer_is_refused_before_a_byte_is_read() {
1104    use ffmpeg_next::packet::Mut;
1105    let mut packet = Packet::copy(&[1u8, 2, 3, 4]);
1106    // SAFETY: `packet` owns a live `AVPacket`; `size` is a public
1107    // field. The forged claim is the read this check exists to stop.
1108    unsafe {
1109      (*packet.as_mut_ptr()).size = 1 << 20;
1110    }
1111    // SAFETY: `packet` owns a live `AVPacket` for the call.
1112    assert!(matches!(
1113      unsafe {
1114        payload_of::<crate::Owned>(
1115          packet.as_ptr(),
1116          DEFAULT_MAX_PACKET_BYTES,
1117          PayloadProvenance::CallerSupplied,
1118        )
1119      },
1120      Err(PacketBufferError::Bounds(_)),
1121    ));
1122  }
1123
1124  #[test]
1125  fn the_shared_empty_carrier_is_one_allocation() {
1126    let a = FfmpegBytes::empty();
1127    let b = FfmpegBytes::empty();
1128    assert!(a.is_empty());
1129    assert_eq!(a.len(), 0);
1130    assert!(a.ptr_eq(&b), "the empty carrier is shared, not remade");
1131    // And a zero-length copy lands on that same allocation rather than
1132    // minting its own.
1133    assert!(FfmpegBytes::copy_from_slice(&[]).ptr_eq(&a));
1134  }
1135
1136  #[test]
1137  fn copy_out_is_owned_and_shareable() {
1138    fn owned_and_shareable<T: Send + Sync + Clone + 'static>(_: &T) {}
1139    let carrier = FfmpegBytes::copy_from_slice(&[4u8, 5, 6]);
1140    owned_and_shareable(&carrier);
1141    assert_eq!(carrier.as_ref(), &[4, 5, 6]);
1142    // Terse `Debug` — the bytes never reach a log line through it.
1143    let rendered = format!("{carrier:?}");
1144    assert!(rendered.contains("len: 3"), "got {rendered}");
1145    assert!(!rendered.contains('4'), "got {rendered}");
1146  }
1147}
1148
1149/// Where the packet a payload is taken from came from — and therefore
1150/// what a second reference to its buffer can do.
1151///
1152/// The dichotomy is **delivered by libavformat** versus **handed over
1153/// by a caller**, and it is about who can *write*, not how many
1154/// references there are.
1155///
1156/// * A packet this crate's own read loop just took from
1157///   `av_read_frame` — or hoisted out of `AVStream.attached_pic` — may
1158///   well share its buffer, and every other reference to it is
1159///   libavformat's. FFmpeg writes through a buffer only after
1160///   `av_buffer_make_writable`, which *copies* when the buffer is
1161///   shared; and while this crate is reading, it holds the
1162///   `AVFormatContext` exclusively, so no libavformat code is running
1163///   at all. There is no safe-Rust `data_mut` on any of those
1164///   references, because no `ffmpeg_next::Packet` wraps them.
1165/// * A packet a **caller** hands over may share its buffer with
1166///   another `ffmpeg_next::Packet` — and that type's `data_mut` writes
1167///   in place from safe code, without consulting writability. That is
1168///   the writer the uniqueness rule exists for, and it may be on
1169///   another thread.
1170///
1171/// Spelled out as a parameter rather than assumed at the call sites,
1172/// because a blanket "refcount must be one" looked right and was twice
1173/// wrong: it refused every embedded cover picture, and then every
1174/// packet from a queue-backed subtitle demuxer, which delivers
1175/// `av_packet_ref`s of originals it keeps in its own queue.
1176#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1177pub(crate) enum PayloadProvenance {
1178  /// A packet a caller handed to a public conversion. A second
1179  /// reference may be a `Packet` with a safe `data_mut`; shared is
1180  /// refused by name.
1181  CallerSupplied,
1182  /// A packet this crate's demux loop just received from
1183  /// `av_read_frame`. Secondary references are libavformat's own.
1184  DemuxDelivered,
1185  /// The container's parked picture, whether hoisted at open or queued
1186  /// as a stream's first packet. Written once while the container was
1187  /// opened and never again.
1188  AttachedPicture,
1189}
1190
1191/// How a payload of a given provenance may be captured once its extent
1192/// is proved.
1193#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1194pub(crate) enum CaptureRoute {
1195  /// The ordinary road: the view lane takes a window, the owned lane
1196  /// copies.
1197  Capture,
1198  /// Both lanes copy. The bytes are safe to *read* — no safe-Rust
1199  /// writer exists — but no long-lived window may be opened onto a
1200  /// buffer somebody else also holds unless something stronger than
1201  /// "nobody is writing right now" is true of it.
1202  Copy,
1203  /// Refuse without reading a byte.
1204  Refuse,
1205}
1206
1207impl PayloadProvenance {
1208  /// What to do with a payload whose buffer has `shared` other
1209  /// references.
1210  ///
1211  /// | provenance | unique | shared | the argument |
1212  /// |---|---|---|---|
1213  /// | [`Self::CallerSupplied`] | capture | **refuse** | a second `Packet`'s `data_mut` writes in place from safe code, possibly on another thread |
1214  /// | [`Self::DemuxDelivered`] | capture | **copy** | the read is race-free — every other reference is C-owned and the context is held exclusively — but a *window* would outlive that exclusivity, and FFmpeg's copy-on-write discipline is a weaker guarantee than this crate wants under a long-lived span |
1215  /// | [`Self::AttachedPicture`] | capture | **capture** | `AVStream.attached_pic` is written once while the container opens and never again, so a window onto it is as stable as one onto a private buffer |
1216  ///
1217  /// The middle row is the deliberate one. Sharing there would have
1218  /// rested on "libavformat honours its own writability rules
1219  /// forever"; copying rests on "nothing can be writing while we hold
1220  /// the context", which is a fact about *this* call and needs no
1221  /// promise about anyone's future behaviour. Subtitle queues — the
1222  /// shape that produced this row — carry payloads measured in bytes,
1223  /// so the copy is not a cost worth an argument.
1224  #[inline]
1225  pub(crate) const fn route(self, shared: bool) -> CaptureRoute {
1226    match (self, shared) {
1227      (_, false) | (Self::AttachedPicture, true) => CaptureRoute::Capture,
1228      (Self::DemuxDelivered, true) => CaptureRoute::Copy,
1229      (Self::CallerSupplied, true) => CaptureRoute::Refuse,
1230    }
1231  }
1232}
1233
1234/// A packet whose payload buffer somebody else still references.
1235///
1236/// **Refused without reading a byte of it, and that is the whole
1237/// point.** A refcount above one is exactly the state in which another
1238/// handle to the same allocation may exist — `ffmpeg_next::Packet`
1239/// hands out `&mut [u8]` through `data_mut` from entirely safe code,
1240/// and a `Packet` is `Send`, so that handle may be on another thread
1241/// writing right now. Forming a `&[u8]` over those bytes is a data race
1242/// whether the bytes are then viewed *or copied*: the copy needs the
1243/// read, and the read is the race.
1244///
1245/// An earlier round answered this shape with a silent copy, reasoning
1246/// that a copy is always sound and keeps the API total. That was wrong
1247/// in the direction that matters — it traded soundness for totality.
1248/// The refcount protects the allocation's *lifetime*; it says nothing
1249/// about who may be writing into it.
1250///
1251/// The ordinary roads never see this: a packet from `av_read_frame` is
1252/// uniquely referenced, and so is one the caller cloned successfully.
1253/// What produces it is a second reference the caller may not know they
1254/// have — see `ffmpeg_next::Packet::clone`, which ignores
1255/// `av_packet_make_writable`'s return code.
1256#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
1257#[error(
1258  "packet payload buffer is shared ({references} references): its bytes cannot be read \
1259   without racing whoever else holds it"
1260)]
1261pub struct SharedPayload {
1262  references: i32,
1263}
1264
1265impl SharedPayload {
1266  /// Constructs a `SharedPayload` payload.
1267  #[inline]
1268  #[must_use]
1269  pub const fn new(references: i32) -> Self {
1270    Self { references }
1271  }
1272
1273  /// References the payload's buffer had when it was refused.
1274  #[inline]
1275  #[must_use]
1276  pub const fn references(&self) -> i32 {
1277    self.references
1278  }
1279}
1280
1281/// Payload for [`PacketBufferError::CaptureFailed`].
1282///
1283/// The proofs all passed and the carrier still could not be formed:
1284/// `av_buffer_alloc` returned null on the owned lane, or
1285/// `av_buffer_ref` did on the view lane. Distinct from
1286/// [`Bounds`] on purpose — a malformed packet and an exhausted
1287/// allocator are different facts, and 0.8 reported both as an absent
1288/// payload.
1289#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
1290#[error("could not capture a {len}-byte payload: the allocator or the refcount refused")]
1291pub struct CaptureFailed {
1292  len: usize,
1293}
1294
1295impl CaptureFailed {
1296  /// Constructs a `CaptureFailed` payload.
1297  #[inline]
1298  pub const fn new(len: usize) -> Self {
1299    Self { len }
1300  }
1301  /// Bytes the capture was for.
1302  #[inline]
1303  pub const fn len(&self) -> usize {
1304    self.len
1305  }
1306  /// Whether the refused capture was of no bytes.
1307  #[inline]
1308  pub const fn is_empty(&self) -> bool {
1309    self.len == 0
1310  }
1311}