stukenov/rav2d
About
Memory-safe AV2 video decoder in Rust, ported from dav2d. Bit-exact with dav2d on the full conformance corpus (8- and 10-bit, filters on and off) and with avmdec on the avmenc vectors. NEON asm for MC and intra via FFI.
Creative & MediaMeasured star growth
- in 24h
- 0
- in 7 days
- Collecting history
- in 15 days
- Collecting history
- in 1 month
- Collecting history
Net star changes over overlapping periods, not daily totals. Missing measurements are left blank. Stars measure attention, not software quality.
README.md
Originalrav2d
rav2d is a memory-safe AV2 video decoder in Rust, ported from dav2d (the C reference decoder, an AV2 fork of dav1d).
The entire C decode path has been ported to Rust and is bit-exact with dav2d: every coding-order frame of every shipped conformance clip matches byte-for-byte, with in-loop filters off and on, for both 8-bit and 10-bit streams. On top of that, 34 of the 39 vectors encoded with the AOM reference encoder (
tests/data/avmenc) match the md5 ofavmdec, the AV2 reference decoder; the other five are listed below. 850 library (845 on x86) + 25 conformance + 16 CLI tests pass, on the NEON and the all-scalar path alike.
Status
Bit-exact against the dav2d C reference (verified by an FFI oracle that decodes the same bitstream with both decoders and byte-compares every plane of every frame):
| Capability | Status |
|---|---|
| Intra (DC/directional/smooth/paeth, CfL, MHCCP, MRL, DIP, palette, IntraBC) | ✅ bit-exact |
| Inter (single-ref, compound, warp-affine, OBMC, interintra, BAWP) | ✅ bit-exact |
| TIP (block-level + whole-frame), OPFL optical-flow refinement | ✅ bit-exact |
| In-loop filters (deblock, CDEF, CCSO, Wiener / PC-Wiener / GDF) | ✅ bit-exact |
| Segmentation, delta-Q, lossless (WHT) | ✅ bit-exact |
| Film grain synthesis | ✅ bit-exact |
| High bit depth — 10-bit | ✅ bit-exact |
| High bit depth — 12-bit | ⚠️ code path present, not yet verified against a vector |
| Scaled references (svc/resize) | ✅ bit-exact for single-reference prediction; compound and TIP from a scaled reference run the same ported kernels but no vector covers them yet |
| Multiple tiles | ✅ tile columns and rows, all filters, matching avmdec (dav2d cannot decode them); ⚠️ disable_loopfilters_across_tiles is ignored |
| Assembly DSP dispatch (aarch64 NEON via FFI) | ✅ 8-bit motion compensation + intra H/V/smooth; 10-bit streams run the Rust path |
| Assembly DSP dispatch (x86) | ❌ none; x86 runs Rust, with the portable wide SIMD loops (loop restoration, transform passes, blends) at the SSE2 baseline |
| Multithreading | ⚠️ output copy and film grain only; parsing, reconstruction and filters are single-threaded |
The full corpus (bit_exact_full_clip_sweep), the filtered corpus (bit_exact_full_clip_filtered_sweep), the 10-bit vectors (bit_exact_hbd_sweep), film grain (bit_exact_filmgrain_applied), scaled references (bit_exact_scaled_ref_sweep) and the coverage vectors (bit_exact_coverage_sweep, 4:0:0 and 4:2:0) are all enforced as tests.
The avmenc vectors (tools/vectors/, built from avm av2-normative with generate.sh) are checked against avmdec's md5 and, frame by frame, against dav2d (avmenc_vectors_bit_exact). Where dav2d disagrees with avm, rav2d follows avm and the vector is checked against avmdec only. Still pending: 4:0:0, 4:2:2, 4:4:4 and resize, where dav2d does not match avmdec either and the port has no guide, and tiles with disable_loopfilters_across_tiles, which no filter honours yet (avmenc_pending_report prints what they still get wrong).
One shipped vector is deliberately not a bit-exact gate: cov-multitile-416x240.obu, whose keyframe entropy stream is malformed in a way dav2d cannot decode deterministically either — its own output differs between thread configurations. rav2d must decode it without panicking, which coverage_decode_no_panic enforces.
Why Rust?
Video decoders parse untrusted bitstreams from the internet — a prime target for memory-corruption exploits. Historical CVEs in C decoders (libvpx, dav1d, ffmpeg) are overwhelmingly buffer overflows, use-after-free, and integer overflows in parsing code.
rav2d eliminates these bug classes at compile time while reusing dav2d's hand-written SIMD for the hottest pixel kernels:
| dav2d (C) | rav2d (Rust) | |
|---|---|---|
| Bitstream parsing | C (unsafe) | Rust (bounds-checked) |
| Decode orchestration | C (unsafe) | Rust (safe, typed) |
| Filter pipeline | C (unsafe) | Rust (bounds-checked) |
| DSP kernels | Assembly + C | Assembly via FFI (where AV2-valid) + Rust |
| Type safety | Weak (enums as ints) | Strong (enum variants, pattern matching) |
Quick Start
cargo add rav2d
use rav2d::{Decoder, Settings, Data, Rav2dError};
let mut decoder = Decoder::open(&Settings::default()).unwrap();
let obu_data: Vec<u8> = std::fs::read("input.obu").unwrap();
decoder.send_data(Some(Data::wrap(obu_data))).unwrap();
loop {
match decoder.get_picture() {
Ok(pic) => {
// Planes come out as ordinary slices — no `unsafe` in consumer code.
let (w, h) = pic.plane_dimensions(0).unwrap(); // luma size in samples
for row in pic.plane_rows_u8(0) { /* row: &[u8], w samples, no padding */ }
// 10/12-bit frames: pic.plane_rows_u16(0) -> &[u16]
}
Err(Rav2dError::Again) => break, // need more data
Err(e) => panic!("{e}"),
}
}
CLI
Prebuilt binaries for Linux, macOS and Windows are attached to each GitHub release; with a Rust toolchain, cargo install rav2d-cli works too.
rav2d in.obu -o out.y4m # decode to Y4M (.yuv and .md5 by extension)
rav2d in.ivf -o - | ffplay - # watch it
rav2d in.obu --verify in.obu.md5 # check against avmdec's md5
rav2d in.obu --info # what is in the stream
See the CLI README for every option.
Performance
rav2d ports the C logic to Rust; hand-written assembly stays via FFI. On aarch64 the motion-compensation kernels and four intra-prediction modes dispatch to dav2d's NEON (run RAV2D_NEON_OFF=all to force the scalar Rust path). All other DSP families run Rust. When those kernels were wired, the rest of dav2d's assembly was still AV1-era and not bit-exact for AV2. Upstream has since added AV2 assembly — AVX2 for most DSP families on x86, and NEON for CDEF, deblock, loop restoration and film grain on aarch64 — which rav2d does not use yet; wiring it in, gated by the conformance oracle like the MC kernels, is the main performance task left.
Where that leaves throughput, single-threaded, against C+SIMD dav2d:
| rav2d / dav2d | |
|---|---|
| In-loop filters on (what playback actually does) | 0.14–0.44× |
| Filters off | 0.03–0.25× |
The filtered figure is the one that matters, and it is the one portable-SIMD work has been moving (see simd.md). Two things stand between here and parity: the decode core is still single-threaded, and most of the assembly dav2d now has is not dispatched to.
DYLD_LIBRARY_PATH=dav2d/build/src cargo bench -p rav2d # prints a rav2d-vs-dav2d table
Crate Structure
| Crate | Description |
|---|---|
rav2d |
Main decoder library — safe Rust API |
rav2d-sys |
Raw FFI bindings to dav2d (bindgen), used only by the test suite as the conformance oracle; not published |
rav2d-cli |
Command-line decoder: IVF or raw OBU to Y4M, YUV or MD5 |
Note:
rav2ditself is pure Rust and does not link the C library — the NEON assembly it dispatches to is vendored undercrates/rav2d/vendor/dav2d-asm, socargo add rav2dworks with no submodule and no C toolchain.rav2d-sysis the conformance oracle: it bindsdav2d.hso the test suite can decode the same bitstream with both decoders and compare. It is a path-only dev-dependency, dropped from the published manifest entirely, and CI keeps it that way with acargo publish --dry-runjob that runs against a checkout with the submodule removed.
Building
Prerequisites
Using the library needs only Rust 1.88+ (edition 2024). Running the conformance suite needs the C reference too:
- meson + ninja, to build the
dav2dsubmodule - LLVM/clang, for bindgen
Build & test
git submodule update --init --recursive
# 1. Build the dav2d C reference (used for linking + the conformance oracle)
cd dav2d && meson setup build && ninja -C build && cd ..
# 2. Build + test rav2d
cargo build --workspace
DYLD_LIBRARY_PATH=dav2d/build/src cargo test -p rav2d # macOS
LD_LIBRARY_PATH=dav2d/build/src cargo test -p rav2d # Linux
# Force the all-scalar path (no NEON):
RAV2D_NEON_OFF=all DYLD_LIBRARY_PATH=dav2d/build/src cargo test -p rav2d
Conformance
crates/rav2d/tests/conformance.rs is an FFI oracle: it decodes each clip with both rav2d and the dav2d C library and asserts byte-equal output. Test clips live in crates/rav2d/tests/data — the 8-bit vectors under media/ came from dav2d, which has since stopped bundling them, and the rest were staged here. The dav2d submodule is kept pristine — it is the source of truth for the port.
A second, independent gate compares against avmdec, the AOM reference decoder: every vector under tests/data/media ships the md5 avmdec produces for it, and bit_exact_avm_reference_md5 hashes rav2d's visible frames, in display order, against it. Matching dav2d cannot catch a bug faithfully ported from the C — both agree and the gate stays green — so this one does not go through dav2d at all.
Approach
Following the rav1d strategy:
- Assembly stays via FFI (reused, not rewritten) — where the AV2 fork's asm is actually AV2-valid.
- All C decoder logic is ported to Rust, validated bit-exact against dav2d at every step.
- Data tables are extracted from C and validated via FFI comparison.
Safety
- The public API hands out pixels as ordinary slices (
plane_rows_u8,plane_row_u16, …), so decoding a frame and reading it needs nounsafein calling code. Raw pointers remain available viaplane_ptr/plane_stride_bytesfor FFI. PicAllocatoris anunsafe trait: supplying the decoder's pixel buffers is the one place a caller can undermine those safe accessors, so the contract is stated and opted into explicitly.- All
unsafe impl Send/Syncdocumented with SAFETY comments. - Enum transmutes replaced with validated
from_raw()helpers + debug assertions. #![warn(unsafe_op_in_unsafe_fn)]crate-wide.- Remaining
unsafeis concentrated in FFI calls, the NEON dispatch, and performance-critical inner loops.
Fuzzing and sanitizers
Bit-exactness proves the decoder computes the right picture; it says nothing about what the unsafe pixel paths do on input no encoder would produce. Two workflows cover that, both nightly and on demand:
fuzzruns thedecodeanddecode_settingscargo-fuzz targets.decode_settingsderives the decoder configuration from the input, so grain, header-only parsing, invisible-frame output and the per-filter combinations get fuzzed too. The corpus is seeded from the conformance vectors and carried between runs, and every crash it has found is replayed byfuzz_regression_corpus_no_panicin ordinary CI.sanitizersruns the unit tests, the regression corpus and the conformance sweeps under AddressSanitizer (with leak detection where the C reference is not involved), and the multi-threaded decode under ThreadSanitizer.
cd crates/rav2d/fuzz
./seed-corpus.sh decode
cargo +nightly fuzz run decode -- -max_total_time=900
Development
rav2d was ported from dav2d with heavy use of AI coding tools (Claude Code). This is disclosed here rather than left implicit in the commit history.
The methodology is built so correctness does not depend on trusting the tooling: every ported step is checked bit-exact against the dav2d C reference by the FFI oracle described in Conformance, and the whole test suite gates the result. Assembly is reused via FFI, not regenerated. Where the port and the C reference disagree, the C reference wins.
Related Projects
License
BSD 2-Clause, same as dav2d.
Explore related projects
Loading…