Skip to main content

Module decode

Module decode 

Source
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;decode
pub use hevc::HevcDecoder;decode
pub use picture::ChromaFormat;decode
pub use picture::ColorInfo;decode
pub use picture::DecodedFrame;decode
pub use picture::Matrix;decode
pub use picture::PackedFrame;decode
pub use picture::Picture;decode
pub use picture::Plane;decode
pub use platform::Backend;decode
pub use platform::Decoder;decode
pub use reader::demux;decode
pub use reader::CodedSample;decode
pub use reader::Demuxed;decode
pub use reader::VideoReader;decode

Modules§

av1decode
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.
cabacdecode
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.
platformdecode
The best decoder this build has: VideoToolbox on macOS (with the videotoolbox feature), NativeDecoder everywhere else.
readerdecode
A video file’s pictures one at a time, in display order, for playback.
vp9decode
VP9 decoder: a port of ffmpeg 7.1’s native vp9 decoder (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}).
webcodecsdecode
What a platform decoder needs from a Demuxed track that the container does not spell out: the browser’s WebCodecs VideoDecoder takes a codec string (avc1.640028, hvc1.1.6.L120.90, av01.0.08M.08, vp09.00.41.08) and the configuration record as its description, and each chunk marked key or delta.

Structs§

NativeDecoderdecode
A software decoder, fed one coded frame at a time — the same interface as videotoolbox_decode::VideoToolboxDecoder.

Functions§

decode_annexbdecode
Decode a whole Annex B elementary stream (start-code delimited, as .264 and .265 files are) and return its pictures in display order.
decode_videodecode
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_orderdecode
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.