Expand description
Native, dependency-free H.264, HEVC, VP9 and AV1 decoding — the software
twin of videotoolbox_decode::VideoToolboxDecoder.
VideoToolbox only exists on Apple platforms, and on the web there is no
system decoder a wasm32 build can reach synchronously at all. This is
the same job done in pure Rust: no OS calls, no threads, no I/O, so it
builds and runs everywhere the rest of crate does.
The decoders need the decode feature; an app that only encodes does not
compile them. The bit reader and the H.264 and
HEVC parameter-set parsers (bits, h264::params, hevc::params) are
here either way, because the MP4 muxer reads a stream’s SPS to describe
its colour.
NativeDecoder has the same shape as the VideoToolbox session, so code
written against one works with the other: open it from a demuxed track,
push samples in decode order, and take pictures back by presentation
time.
use threers_codec::decode::NativeDecoder;
use threers_codec::mp4_demux::demux_video;
let file = std::fs::read("clip.mp4").unwrap();
let track = demux_video(&file).unwrap();
let mut dec = NativeDecoder::new(&track).unwrap();
for s in &track.samples {
let bytes = &file[s.offset as usize..][..s.size as usize];
dec.push(bytes, s.pts, s.dts, s.duration as i64).unwrap();
if let Some(bgra) = dec.take(s.pts) {
let _ = bgra; // width * height * 4
}
}NativeDecoder::take_frame returns the planes instead of BGRA, at the
stream’s own bit depth — what a renderer uploading YUV textures wants,
and what the conformance tests compare against a reference decoder
sample for sample.
What each decoder covers is in its module docs: h264, hevc, vp9
and av1.
A stream using something outside that is refused with an error naming
the feature, not decoded into garbage.
Re-exports§
pub use h264::H264Decoder;decodepub use hevc::HevcDecoder;decodepub use picture::ChromaFormat;decodepub use picture::ColorInfo;decodepub use picture::DecodedFrame;decodepub use picture::Matrix;decodepub use picture::PackedFrame;decodepub use picture::Picture;decodepub use picture::Plane;decodepub use platform::Backend;decodepub use platform::Decoder;decodepub use reader::demux;decodepub use reader::CodedSample;decodepub use reader::Demuxed;decodepub use reader::VideoReader;decode
Modules§
- av1
decode - AV1 decoder: a port of dav1d 1.5.1 (single-threaded C paths), the decoder the AV1 conformance suite and libaom agree with bit for bit.
- bits
- Reading side of the bitstream layer: an MSB-first bit reader with Exp-Golomb, and NAL unit splitting and unescaping.
- cabac
decode - The CABAC arithmetic decoding engine (H.264 §9.3.3.2, H.265 §9.3.4.3).
- h264
- A from-scratch H.264 / AVC decoder.
- hevc
- A from-scratch HEVC / H.265 decoder.
- picture
- Sample planes and the decoded frame the decoders hand back.
- platform
decode - The best decoder this build has: VideoToolbox on macOS (with the
videotoolboxfeature),NativeDecodereverywhere else. - reader
decode - A video file’s pictures one at a time, in display order, for playback.
- vp9
decode - VP9 decoder: a port of ffmpeg 7.1’s native
vp9decoder (vp9.c,vp9block.c,vp9recon.c,vp9mvs.c,vp9prob.c,vp9lpf.c,vp9dsp_template.c), single-threaded path, every profile (8/10/12-bit; 4:2:0, 4:2:2, 4:4:0, 4:4:4, RGB), reference scaling, show-existing and intra-only frames, and superframes. - vp9_
packet - VP9 packet-level parsing that demuxers need without the decoder (the
decoder re-exports these as
vp9::{packet_shows, split_superframe}). - webcodecs
decode - What a platform decoder needs from a
Demuxedtrack that the container does not spell out: the browser’s WebCodecsVideoDecodertakes a codec string (avc1.640028,hvc1.1.6.L120.90,av01.0.08M.08,vp09.00.41.08) and the configuration record as itsdescription, and each chunk marked key or delta.
Structs§
- Native
Decoder decode - A software decoder, fed one coded frame at a time — the same interface as
videotoolbox_decode::VideoToolboxDecoder.
Functions§
- decode_
annexb decode - Decode a whole Annex B elementary stream (start-code delimited, as
.264and.265files are) and return its pictures in display order. - decode_
video decode - Decode every picture of a video file, whatever holds it: MP4/MOV (H.264, HEVC, AV1), WebM/Matroska (VP9, AV1), IVF (VP9, AV1), or an Annex B H.264/HEVC elementary stream, told apart by its parameter sets.
- decode_
video_ in_ order decode decode_video, with frames in display order: by time, and as they arrive where the stream has none. Only the frames that could still be overtaken are held, so memory does not grow with the stream.