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