Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
FFmpeg adapter for the mediadecode abstraction
layer, built on top of
ffmpeg-next.
Implements mediadecode's VideoAdapter / AudioAdapter /
SubtitleAdapter / ImageAdapter traits, the matching push-style
*StreamDecoder traits, the one-shot ImageDecoder, and Demuxer.
Every byte a frame or packet carries is copied once, at the FFmpeg
boundary, into an FfmpegBytes — mediadecode 0.9's carrier
amputation contract. A delivered frame is owned, Send + Sync, and
cheap to clone (a refcount bump); it holds nothing of libavcodec's
open, so it can cross a channel, be read from several threads, and
outlive the decoder that produced it. Through 0.8 the planes were
refcounted views into AVBufferRef behind an FfmpegBuffer type,
which meant every consumer inherited an FFmpeg lifetime it could not
see. That type is gone.
FfmpegVideoStreamDecoder mirrors the send_packet / receive_frame
shape of ffmpeg::decoder::Video, auto-probes the host's HW backends,
and falls through to a software decoder when none takes the stream
before its first picture. Audio and
subtitles use parallel FfmpegAudioStreamDecoder /
FfmpegSubtitleStreamDecoder types.
Backends
FfmpegVideoStreamDecoder::open walks this probe order, opening the
first backend that accepts the stream:
| Target | Probe order |
|---|---|
| macOS / iOS / tvOS | VideoToolbox → software |
| Linux | VAAPI → CUDA → software |
| Windows | D3D11VA → CUDA → software |
| other | software |
Output frames are CPU-side, downloaded with av_hwframe_transfer_data
(NV12 for 8-bit, P010/P012/P016/P210/P212/P216/P410/P412/P416 for
10/12/16-bit). Pixel-format conversion is intentionally out of scope
— downstream
colconv handles it.
The probe keeps every packet it consumes until the first picture comes
out. When no backend takes the stream by then, open (that is,
DecodePath::Auto) replays those packets into the software decoder, so
nothing is lost; DecodePath::AnyHardware and a DecodePath::Hardware
pin report VideoDecodeError::Decode(Error::AllBackendsFailed(p))
instead, carrying them (p.unconsumed_packets() /
p.into_unconsumed_packets()) — so non-seekable callers (live streams,
pipes, network sources) can replay them through a software decoder of
their own without re-demuxing.
After the first picture nothing changes the road, on any path, and
nothing is classified. A decoder failure, VideoDecodeError::Decode, is
that picture's own error, reported as the decoder minted it, and nothing
of it is remembered, so the next call reaches libavcodec. FFmpeg has no
reliable signal that a hardware session is gone (AVERROR_EXTERNAL, for
one, also answers a single picture), and whether a hardware session
recovers is FFmpeg's, not this crate's. FFmpeg 9.0.1 does not always
recover one. After a VideoToolbox restart that fails, every picture
fails the same way until a new parameter set re-arms the restart, and
flush does not change that: it drops the pictures and references the
codec holds, and it rebuilds no hardware session. VideoDecoder's
documentation cites the FFmpeg lines.
A VideoDecodeError::Convert is the wrapper's own failure, not the
decoder's: FfmpegVideoStreamDecoder could not convert a decoded
picture into a frame (a frame ceiling, a pixel format or plane layout it
cannot carry, an allocation). One that failed on an allocation parks the
picture: the next receive_frame converts it again, and until one
delivers it, send_packet and send_eof answer Sent::MustDrain
without reaching libavcodec. That is the wrapper's back pressure, not a
failure.
So a caller that sees a hardware session's decoder failures persist
rebuilds: it opens a session on DecodePath::Software from the same
parameters and feeds it forward. What the new session can be fed depends
on the road the failure came from:
send_packet: the failure names the packet in hand, and the new session is given that packet.receive_frame: the failure may concern a packet accepted earlier. FFmpeg decouples input from output and may hold several pictures (libavcodec/avcodec.h90–139 in FFmpeg 9.0.1), so no packet is named: the new session is given the next packet, and the caller accepts the gap.send_eof: the end has no packet to give a new session. What the hardware session still held is recoverable only from packets kept from before, or by a seek.
When to rebuild is the caller's policy, not the decoder's, because it is
the caller that sees the failures on the three roads, the packets' key
flags and what it has delivered. What a policy counts is the hardware
session's decoder failures; a Convert error is reported, not counted.
The usage example below carries the simplest one.
Usage
A file's video track, decoded under the simplest policy for a hardware
session the caller stops trusting. It counts the hardware session's
decoder failures on all three roads, and only a delivered picture ends
the count. At FAILURES_BEFORE_SOFTWARE failures in a row it opens a
session on DecodePath::Software from the same parameters and feeds it
forward: the packet in hand when the failure came from send_packet,
otherwise what comes next, the next packet or the end. It keeps no
packets and replays nothing, so every picture the new session delivers
is delivered as it is. A Convert error is reported, not counted: the
example returns it, as it returns every error it does not handle.
use ffmpeg_next as ffmpeg;
use ;
use ;
use ;
type BoxError = ;
/// This caller's threshold, not the decoder's: how many decoder failures
/// in a row, with no picture delivered between them, it takes from a
/// hardware session before it rebuilds on software.
const FAILURES_BEFORE_SOFTWARE: u32 = 3;
/// A video track decoded under this caller's policy for a hardware
/// session it stops trusting.
///
/// The policy feeds forward and does nothing else: it keeps no packets,
/// replays nothing and matches no pictures. That costs the pictures from
/// a failure to the next keyframe: a new session holds no reference
/// pictures, so libavcodec drops or conceals what comes before one, and
/// what the hardware session still held goes with it. A caller that
/// cannot afford that gap keeps the packets since the last clean keyframe
/// and replays them with a picture identity of its own; that bookkeeping
/// is the caller's design and is not shown here, because a doc example
/// cannot carry it correctly.
Audio and subtitle decoding share the shape — see
examples/decode_via_trait.rs and
tests/audio_subtitle_via_trait.rs
for end-to-end demuxer-driven runs that cover all three streams.
Public surface map
- Decoders:
FfmpegVideoStreamDecoder,FfmpegAudioStreamDecoder,FfmpegSubtitleStreamDecoder. Plus their error types:VideoDecodeError,AudioDecodeError,SubtitleDecodeError. - Decode path:
DecodePathandFfmpegVideoStreamDecoder::open_as—Auto(whatopendoes: probe hardware, fall back to software before the first picture),AnyHardware(the same probe, never software),Software, or the pinHardware(Backend). After the first picture no path changes its decoder, and a decoder failure is the picture's own error; when to stop trusting a hardware session is the caller's policy.is_hardware()/is_software()stay the live reading of where a session is. - Demuxer:
FfmpegDemuxer—mediadecode'sDemuxeroverlibavformat, opened from a path (open) or from anyRead + Seekbyte source through a customAVIOContext(open_reader). PlusDemuxError, andformat()→ContainerFormat— libavformat's own identification of the bytes ("matroska,webm","mov,mp4,m4a,3gp,3g2,mj2"), read from the container rather than guessed from a path. - Codec identity:
CodecId— the number FFmpeg keys on, withname()/long_name()reading libavcodec's own descriptor table for the word beside it ("h264","aac"), so a stored track row can cross into a typed codec vocabulary. The number stays the identity; an id libavcodec has no descriptor for has no name, rather than a"unknown_codec"sentinel. - Resampler (
resamplefeature, on by default):FfmpegResampler—mediadecode'sAudioResampleroverswresample, built from two explicitResampleSpecs (the source read off a track or off the opened decoder, the target the caller's). PlusResampleError, which carries faults and theSourceChangedmid-stream refusal — "needs more input" and "the tail is finished" aremediadecode::Receivedstates out ofreceive_frame, not error variants. Disabling the feature drops the type and thelibswresamplelink along with it. FfmpegImageDecoder: the one-shotImageDecoder— cover art in,ImageFrameout. Opened from an attachment track's codec parameters, which the demuxer's cover-art reclassification retains in full. The picture's EXIF orientation comes back typed onImageFrameExtra, read off the display matrix libavcodec emits for it.- Type aliases:
VideoPacket,AudioPacket,SubtitlePacket,DataPacket,AttachmentPacket,DemuxedPacket,VideoFrame,AudioFrame,SubtitleFrame,ImageFrame,TrackInfo,TrackParams— themediadecodegeneric types pre-parameterized with this crate's adapter / carrier / extras, so you don't have to spell them out. - Carrier:
FfmpegBytes— owned,Send + Sync,AsRef<[u8]>, cloning by refcount. Opaque over anArc<[u8]>, because the storage gains a pooled strategy later (#35) and a consumer must not have to recompile for it. Construct one withFfmpegBytes::copy_from_slice/::emptywhen feeding a packet back into a decoder. - Boundary helpers:
video_packet_from_ffmpeg,audio_packet_from_ffmpeg,subtitle_packet_from_ffmpeg— convert a borrowedffmpeg::Packetinto the matchingmediadecodepacket, copying the compressed payload out. Their*_insiblings (video_packet_from_ffmpeg_in,audio_packet_from_ffmpeg_in,subtitle_packet_from_ffmpeg_in,data_packet_from_ffmpeg_in) take the stream's timebase, so the producedTimestampsays what its ticks mean instead of carrying the 1/1 placeholder anAVPacketalone leaves you with.attachment_packet_from_ffmpegwraps a cover-art packet, which has no timestamps to carry. Every one isunsafe: a borrow of anffmpeg_nextpacket cannot vouch that its payload was ever written (Packet::newleaves it unwritten), so the caller does — a packetav_read_framefilled always was. The demuxer,FfmpegDemuxer, converts its own packets and needs nounsafe. - Empty-frame builders:
empty_video_frame,empty_audio_frame,empty_subtitle_frame— well-formed destinations forreceive_frame.
Running tests and benches
The fixture-gated tests and the benchmark expect real media files,
named by five environment variables. The unit tests run
unconditionally; the gated ones are #[ignore]d, so --ignored
is what opts into them.
| Variable | Feeds |
|---|---|
HWDECODE_SAMPLE_VIDEO |
tests/decode.rs, tests/hw_smoke.rs, benches/decode.rs, and the three decoder::tests backend cases |
MEDIADECODE_SAMPLE_VIDEO |
tests/decode_via_trait.rs |
MEDIADECODE_SAMPLE_AUDIO |
the audio-through-trait case (any container with an audio track) |
MEDIADECODE_SAMPLE_SUBTITLE |
the subtitle-through-trait case (needs a container that really carries a subtitle track) |
MEDIADECODE_FX3_SAMPLE |
the Sony FX3 H.264 High 4:2:2 10-bit probe-era HW→SW fallback case |
HWDECODE_SAMPLE_VIDEO=/path/to/clip.mp4
HWDECODE_SAMPLE_VIDEO=/path/to/clip.mp4
# The whole fixture-gated set at once.
HWDECODE_SAMPLE_VIDEO=/path/to/clip.mp4 \
MEDIADECODE_SAMPLE_VIDEO=/path/to/clip.mp4 \
MEDIADECODE_SAMPLE_AUDIO=/path/to/clip.mp4 \
MEDIADECODE_SAMPLE_SUBTITLE=/path/to/subtitled.mkv \
MEDIADECODE_FX3_SAMPLE=/path/to/12_sony_fx3_xavc.mp4 \
A variable left unset is not always a quiet skip: the FX3 case prints
a notice and returns, but the trait cases panic on a missing path
once --ignored has opted into them.
Build requirements
- A system FFmpeg ≥ 5.1 linkable via
pkg-config(we referenceAV_PIX_FMT_P212LE/AV_PIX_FMT_P412LE, which were added in 5.1). Tested against 9.0. Verify withffmpeg -hwaccelsthat your build has the backends you expect compiled in (e.g.videotoolboxon macOS,vaapi/cudaon Linux,d3d11va/cudaon Windows). - Rust ≥ 1.95, edition 2024.
License
mediadecode-ffmpeg is under the terms of both the MIT license and the
Apache License (Version 2.0).
See LICENSE-APACHE, LICENSE-MIT for details.
Copyright (c) 2026 FinDIT Studio authors.