Skip to main content

mediadecode_ffmpeg/
lib.rs

1#![doc = include_str!("../README.md")]
2#![cfg_attr(docsrs, feature(doc_cfg))]
3#![cfg_attr(docsrs, allow(unused_attributes))]
4#![deny(missing_docs)]
5// The core crate carries this same allow, for this same reason, and 0.9
6// is where this crate joined it: a signature here reads
7// `VideoPacket<VideoPacketExtra, FfmpegBytes>` because every one of those
8// three names is load-bearing — the household, the backend's extras,
9// and the owned carrier the D-seat amputation contract requires.
10// `clippy::type_complexity` counts nesting, and the fix it asks for is
11// an alias that hides exactly the word this release exists to make
12// visible. 0.8 had that alias; it was called `FfmpegBuffer`.
13#![allow(clippy::type_complexity)]
14
15mod adapter;
16mod audio;
17mod backend;
18pub mod boundary;
19mod buffer;
20mod carrier;
21pub mod channel_layout;
22mod codec_id;
23pub mod convert;
24mod decoder;
25mod demuxer;
26mod error;
27pub mod extras;
28#[cfg(test)]
29mod fault_subprocess;
30mod ffi;
31mod footprint;
32mod frame;
33mod image;
34pub mod limits;
35mod pixdesc;
36mod reader_guard;
37#[cfg(feature = "resample")]
38mod resampler;
39mod sample_format;
40mod subtitle;
41pub mod ticket;
42mod video;
43mod view;
44
45pub use adapter::Ffmpeg;
46pub use audio::{AudioDecodeError, CarrierAudioStreamDecoder};
47pub use backend::Backend;
48pub use boundary::{
49  MediaKind, PacketBuildError, SendPayloadTooLarge, SendSideDataTooLarge,
50  attachment_packet_from_ffmpeg, audio_packet_from_ffmpeg, audio_packet_from_ffmpeg_in,
51  data_packet_from_ffmpeg_in, empty_audio_frame, empty_owned_audio_frame,
52  empty_owned_subtitle_frame, empty_owned_video_frame, empty_subtitle_frame, empty_video_frame,
53  ffmpeg_packet_from_audio_packet, ffmpeg_packet_from_owned_audio_packet,
54  ffmpeg_packet_from_owned_subtitle_packet, ffmpeg_packet_from_owned_video_packet,
55  ffmpeg_packet_from_subtitle_packet, ffmpeg_packet_from_video_packet, from_av_pixel_format,
56  is_hardware_pix_fmt, owned_attachment_packet_from_ffmpeg, owned_audio_packet_from_ffmpeg_in,
57  owned_data_packet_from_ffmpeg_in, owned_subtitle_packet_from_ffmpeg_in,
58  owned_video_packet_from_ffmpeg_in, subtitle_packet_from_ffmpeg, subtitle_packet_from_ffmpeg_in,
59  video_packet_from_ffmpeg, video_packet_from_ffmpeg_in,
60};
61
62pub use buffer::{FfmpegBytes, PacketBufferError, TrustedPayload};
63pub(crate) use carrier::CarrierOps;
64pub use carrier::{FfmpegCarrier, Owned, View};
65pub use view::FfmpegBuffer;
66
67/// The demuxer, on the **view** lane — the ordinary road.
68///
69/// Packets carry [`FfmpegBuffer`] views onto libavformat's own
70/// allocations: nothing is copied, and a consumer that reads a packet
71/// and drops it never pays for bytes it did not keep. This is what a
72/// direct consumer wants, which is why it is what the bare name means.
73///
74/// Reach for [`FfmpegOwnedDemuxer`] when a packet has to **travel** —
75/// across a graph, between threads that both read it, into a cache
76/// that outlives the decoder. See [the carrier lanes][lanes] for the
77/// tradeoff table.
78///
79/// [lanes]: mediadecode::adapter#the-two-carrier-lanes
80pub type FfmpegDemuxer = demuxer::CarrierDemuxer<View>;
81
82/// The demuxer on the **owned** lane: every byte copied once at the
83/// boundary into memory Rust owns.
84///
85/// The same type as [`FfmpegDemuxer`] on the other carrier, with the
86/// same constructors — `FfmpegOwnedDemuxer::open(&path)`. Packets are
87/// `Send + Sync + 'static` and owe nothing to the session that produced
88/// them, which is what a graph needs and what a view cannot give.
89pub type FfmpegOwnedDemuxer = demuxer::CarrierDemuxer<Owned>;
90
91/// The audio decoder on the **view** lane.
92pub type FfmpegAudioStreamDecoder = audio::CarrierAudioStreamDecoder<View>;
93/// The audio decoder on the **owned** lane.
94pub type FfmpegOwnedAudioStreamDecoder = audio::CarrierAudioStreamDecoder<Owned>;
95
96/// The subtitle decoder on the **view** lane.
97pub type FfmpegSubtitleStreamDecoder = subtitle::CarrierSubtitleStreamDecoder<View>;
98/// The subtitle decoder on the **owned** lane.
99pub type FfmpegOwnedSubtitleStreamDecoder = subtitle::CarrierSubtitleStreamDecoder<Owned>;
100
101/// The still-image decoder on the **view** lane.
102pub type FfmpegImageDecoder = image::CarrierImageDecoder<View>;
103/// The still-image decoder on the **owned** lane.
104pub type FfmpegOwnedImageDecoder = image::CarrierImageDecoder<Owned>;
105
106/// The video stream decoder on the **view** lane.
107pub type FfmpegVideoStreamDecoder = video::CarrierVideoStreamDecoder<View>;
108/// The video stream decoder on the **owned** lane.
109pub type FfmpegOwnedVideoStreamDecoder = video::CarrierVideoStreamDecoder<Owned>;
110pub use channel_layout::{
111  channel_layout_description_from_ffmpeg, channel_layout_from_ffmpeg, channel_order_from_ffmpeg,
112};
113pub use codec_id::CodecId;
114pub use decoder::VideoDecoder;
115pub use demuxer::{CarrierDemuxer, DemuxError, ProbeBudgetExhausted};
116pub use error::{
117  Error, FrameBudgetExceeded, FrameMedium, HwSurfaceTooLarge, HwTransferTooLarge, Result,
118};
119pub use frame::Frame;
120pub use image::{CarrierImageDecoder, Corrupt, CorruptSource, ImageDecodeError, InputTooLarge};
121pub use limits::{
122  DEFAULT_MAX_ATTACHMENT_BYTES, DEFAULT_MAX_CODEC_PARAMETER_BYTES, DEFAULT_MAX_FRAME_BYTES,
123  DEFAULT_MAX_IMAGE_INPUT_BYTES, DEFAULT_MAX_IMAGE_SIDE_DATA_BYTES, DEFAULT_MAX_PACKET_BYTES,
124  DEFAULT_MAX_PIXELS, DEFAULT_MAX_PROBE_BYTES, DEFAULT_MAX_STREAMS,
125  DEFAULT_MAX_TOTAL_ATTACHMENT_BYTES, DEFAULT_MAX_TOTAL_CODEC_PARAMETER_BYTES, DecoderLimits,
126  DemuxLimits, FrameLimits, PacketLimits,
127};
128#[cfg(feature = "resample")]
129#[cfg_attr(docsrs, doc(cfg(feature = "resample")))]
130pub use resampler::{
131  CarrierResampler, OutputTooLarge, ResampleError, ResampleSpec, SpecEnd,
132  UnsupportedChannelCount as UnsupportedSpecChannelCount,
133};
134/// The resampler, on the **view** lane — planes shared out of its own
135/// output frame.
136///
137/// Its output frames are the resampler's, not a decoder's: it allocates
138/// one per conversion and never writes into a buffer it has handed out,
139/// so the pool-hostage warning that applies to decoder frames does not
140/// apply here. What does apply is `!Sync` and the amputation contract —
141/// use [`FfmpegOwnedResampler`] for a frame that has to travel.
142#[cfg(feature = "resample")]
143#[cfg_attr(docsrs, doc(cfg(feature = "resample")))]
144pub type FfmpegResampler = resampler::CarrierResampler<View>;
145
146/// The resampler, on the **owned** lane — every produced plane copied
147/// out of the output frame.
148#[cfg(feature = "resample")]
149#[cfg_attr(docsrs, doc(cfg(feature = "resample")))]
150pub type FfmpegOwnedResampler = resampler::CarrierResampler<Owned>;
151
152pub use sample_format::SampleFormat;
153pub use subtitle::{CarrierSubtitleStreamDecoder, SubtitleDecodeError};
154pub use ticket::{ChannelLayoutTicket, CodecTicket, CustomChannel, Ratio};
155pub use video::{CarrierVideoStreamDecoder, VideoDecodeError};
156
157// Every bare alias below binds [`FfmpegBuffer`] in the `D` seat — the
158// view lane, the ordinary road: a decoder's output read where it lands
159// and dropped. The `Owned*` family binds [`FfmpegBytes`] and is what a
160// payload takes when it has to **travel** — outlive the decoder, cross
161// into a graph, be shared across threads.
162//
163// Each spells its carrier out rather than hiding it behind a neutral
164// name. That is deliberate, and it is the one lesson 0.8's version of
165// this block failed to teach: 0.8 also called this type `FfmpegBuffer`,
166// but the D seat was *only* ever that type, so a consumer reading
167// `VideoFrame` could not tell that holding one held libavcodec's memory
168// open. It did. Naming both carriers in both families is what makes the
169// question answerable at the use site — and [`FfmpegBytes`] answers it
170// by being nothing of ours: owned, `Send + Sync`, no FFmpeg lifetime
171// attached, the core's D-seat amputation contract satisfied by a type
172// out of `alloc`.
173
174/// Compressed video packet pre-parameterized with this crate's extras
175/// and view carrier — the type [`FfmpegVideoStreamDecoder`] consumes
176/// via [`mediadecode::decoder::VideoStreamDecoder::send_packet`].
177pub type VideoPacket = mediadecode::packet::VideoPacket<extras::VideoPacketExtra, FfmpegBuffer>;
178
179/// Compressed audio packet pre-parameterized with this crate's extras
180/// and view carrier.
181pub type AudioPacket = mediadecode::packet::AudioPacket<extras::AudioPacketExtra, FfmpegBuffer>;
182
183/// Compressed subtitle packet pre-parameterized with this crate's
184/// extras and view carrier.
185pub type SubtitlePacket =
186  mediadecode::packet::SubtitlePacket<extras::SubtitlePacketExtra, FfmpegBuffer>;
187
188/// Decoded video frame pre-parameterized with this crate's pixel
189/// format / extras / view carrier.
190///
191/// Its planes are windows into the decoder's own frame buffer wherever
192/// the geometry proves they may be — see the frame row of the lane
193/// table in [`mediadecode::adapter`]. **A frame held is a pool slot
194/// held**: on a hardware or fixed-pool decoder, retaining these past
195/// the next `receive_frame` starves the decoder. Use [`OwnedVideoFrame`]
196/// when a frame has to outlive the decode loop.
197pub type VideoFrame =
198  mediadecode::frame::VideoFrame<mediadecode::PixelFormat, extras::VideoFrameExtra, FfmpegBuffer>;
199
200/// Decoded audio frame pre-parameterized with this crate's sample
201/// format / channel layout / extras / view carrier.
202///
203/// Each plane is a window over exactly the samples the decoder wrote —
204/// never the allocator's alignment padding past them.
205pub type AudioFrame = mediadecode::frame::AudioFrame<
206  SampleFormat,
207  mediaframe::audio::ChannelLayoutDescription,
208  extras::AudioFrameExtra,
209  FfmpegBuffer,
210>;
211
212/// Decoded subtitle frame pre-parameterized with this crate's
213/// extras / view carrier.
214pub type SubtitleFrame =
215  mediadecode::frame::SubtitleFrame<extras::SubtitleFrameExtra, FfmpegBuffer>;
216
217/// Decoded still image pre-parameterized with this crate's pixel
218/// format / extras / view carrier — what [`FfmpegImageDecoder`]
219/// produces.
220pub type ImageFrame =
221  mediadecode::frame::ImageFrame<mediadecode::PixelFormat, extras::ImageFrameExtra, FfmpegBuffer>;
222
223/// Timed opaque-data packet pre-parameterized with this crate's extras
224/// and view carrier.
225pub type DataPacket = mediadecode::demuxer::DataPacket<extras::DataPacketExtra, FfmpegBuffer>;
226
227/// Attachment payload pre-parameterized with this crate's extras and
228/// view carrier — a font, or the cover art [`FfmpegImageDecoder`]
229/// decodes.
230pub type AttachmentPacket =
231  mediadecode::demuxer::AttachmentPacket<extras::AttachmentPacketExtra, FfmpegBuffer>;
232
233/// The five-arm demux envelope [`FfmpegDemuxer`] delivers.
234pub type DemuxedPacket = mediadecode::demuxer::DemuxedPacket<Ffmpeg, FfmpegBuffer>;
235
236// --- The owned lane's alias family -----------------------------------
237//
238// The same shapes on the copying carrier, named explicitly because the
239// bare names mean the view lane — the ordinary road for a direct
240// consumer. Reach for these when a packet has to travel.
241//
242// Note what is **not** doubled: the `*Extra` types and `SideDataEntry`
243// stay monomorphic on both lanes. Side data has no `AVBufferRef` to
244// share — `AVPacketSideData` and `AVFrameSideData` payloads are plain
245// allocations — so both lanes copy it, and a second family of extras
246// would have been two names for one representation. The carrier
247// parameter reaches the **payload**, not the annotations.
248
249/// [`VideoPacket`] on the owned lane.
250pub type OwnedVideoPacket = mediadecode::packet::VideoPacket<extras::VideoPacketExtra, FfmpegBytes>;
251
252/// [`AudioPacket`] on the owned lane.
253pub type OwnedAudioPacket = mediadecode::packet::AudioPacket<extras::AudioPacketExtra, FfmpegBytes>;
254
255/// [`SubtitlePacket`] on the owned lane.
256pub type OwnedSubtitlePacket =
257  mediadecode::packet::SubtitlePacket<extras::SubtitlePacketExtra, FfmpegBytes>;
258
259/// [`DataPacket`] on the owned lane.
260pub type OwnedDataPacket = mediadecode::demuxer::DataPacket<extras::DataPacketExtra, FfmpegBytes>;
261
262/// [`AttachmentPacket`] on the owned lane.
263pub type OwnedAttachmentPacket =
264  mediadecode::demuxer::AttachmentPacket<extras::AttachmentPacketExtra, FfmpegBytes>;
265
266/// [`DemuxedPacket`] on the owned lane.
267pub type OwnedDemuxedPacket = mediadecode::demuxer::DemuxedPacket<Ffmpeg, FfmpegBytes>;
268
269/// [`VideoFrame`] on the owned lane — planes copied out of the
270/// decoder's buffer, so the frame outlives it and the pool slot goes
271/// straight back.
272pub type OwnedVideoFrame =
273  mediadecode::frame::VideoFrame<mediadecode::PixelFormat, extras::VideoFrameExtra, FfmpegBytes>;
274
275/// [`AudioFrame`] on the owned lane.
276pub type OwnedAudioFrame = mediadecode::frame::AudioFrame<
277  SampleFormat,
278  mediaframe::audio::ChannelLayoutDescription,
279  extras::AudioFrameExtra,
280  FfmpegBytes,
281>;
282
283/// [`SubtitleFrame`] on the owned lane.
284pub type OwnedSubtitleFrame =
285  mediadecode::frame::SubtitleFrame<extras::SubtitleFrameExtra, FfmpegBytes>;
286
287/// [`ImageFrame`] on the owned lane — what [`FfmpegOwnedImageDecoder`]
288/// produces.
289pub type OwnedImageFrame =
290  mediadecode::frame::ImageFrame<mediadecode::PixelFormat, extras::ImageFrameExtra, FfmpegBytes>;
291
292/// One row of the track table [`FfmpegDemuxer::tracks`] returns.
293///
294/// [`FfmpegDemuxer::tracks`]: mediadecode::demuxer::Demuxer::tracks
295pub type TrackInfo = mediadecode::demuxer::TrackInfo<Ffmpeg>;
296
297/// A track's per-kind codec parameters, as [`TrackInfo`] carries them.
298pub type TrackParams = mediadecode::demuxer::TrackParams<Ffmpeg>;
299
300/// Asserts a submission was taken, and answers nothing.
301///
302/// The `#[must_use]` on [`mediadecode::Sent`] is deliberate teeth: a
303/// test that submits and ignores the answer is a test that would not
304/// notice a decoder quietly asking to be drained. Most of this crate's
305/// tests submit into a session they have just emptied, where
306/// [`Sent::MustDrain`](mediadecode::Sent::MustDrain) is a real
307/// surprise — so they say so here rather than dropping it.
308#[cfg(test)]
309#[track_caller]
310pub(crate) fn accepted<E: core::fmt::Debug>(
311  status: core::result::Result<mediadecode::Sent, E>,
312  what: &str,
313) {
314  assert_eq!(
315    status.unwrap_or_else(|e| panic!("{what}: {e:?}")),
316    mediadecode::Sent::Accepted,
317    "{what}: the session asked to be drained where the test expected room",
318  );
319}