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