1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//! Native, from-scratch VP9 encoder (no libvpx, no C bindings) — the codec that
//! rides inside [`crate::webm`] for a fully dependency-free `.webm`.
//!
//! Pure Rust so it runs everywhere threers does, native **and** `wasm32`. Like
//! the [`hevc`](crate::hevc) effort, VP9 is built as a ladder of
//! independently-testable layers, each gated behind the one below being
//! bit-exact.
//!
//! # Why VP9 (and WebM)
//!
//! WebM is the only broadly browser-native *video* container that carries an
//! alpha channel (VP9 + a second VP9 stream in `BlockAdditional`). The existing
//! `threers::video` path reaches it through the system `ffmpeg`/`libvpx`, which
//! can't run in the browser. A native encoder removes that dependency — at the
//! cost of reimplementing a large codec.
//!
//! # Build order (milestones)
//!
//! 1. **Boolean entropy coder** — *done*: [`bool_coder`], the binary range coder
//! every compressed syntax element uses (RFC 6386 §7), verified by an
//! encode↔decode roundtrip. This is VP9's analog of HEVC's CABAC engine.
//! 2. **Container** — *done*: [`crate::webm`] muxes coded frames into a
//! playable `.webm` (validated end-to-end against ffmpeg in `tests/webm.rs`).
//! 3. **Frame headers + gray keyframe** — *done & externally verified*:
//! [`keyframe`] emits a profile-0 intra keyframe (uncompressed + compressed
//! header, one 64×64 `PARTITION_NONE` / `skip` / `DC_PRED` superblock). With
//! no neighbors DC is mid-gray (`128`); ffmpeg decodes it losslessly in the
//! YUV domain (see `tests/webm.rs`).
//! 4. **Muxing** — *done*: [`crate::webm::encode_gray_webm`] pairs the
//! keyframe encoder with the WebM muxer for a dependency-free `.webm`.
//! 5. **Real intra (prediction-only)** — *done*: [`encoder`] partitions to 8×8
//! with SAD-picked DC/V/H modes (skip-only path retained for zero residual).
//! 6. **Residual** — *done & externally verified*: 4×4 hybrid DCT/ADST + quant +
//! coefficient tokens ([`transform`], [`quant`], [`tokens`]). DC/V/H modes
//! with matching TX types; frame sizes that are multiples of 8 (partial edge
//! superblocks included). ffmpeg decodes to our recon bit-for-bit
//! (see `tests/webm.rs`).
//! 7. **Alpha** — *done & externally verified*: WebM `AlphaMode` + alpha VP9 in
//! `BlockAdditional` ([`crate::webm`]). ffmpeg/`libvpx-vp9` decodes
//! colour + alpha to our recons (see `tests/webm.rs`).
//! 8. **Inter** — *in progress*: [`inter`] motion search, LAST/GOLDEN/ALTREF refs,
//! compound LAST/GOLDEN+ALTREF, square + HORZ/VERT partitions through 64×64,
//! switchable EIGHTTAP/SMOOTH/SHARP MC ([`mc`]), SAD ME ([`me`]),
//! `TX_MODE_SELECT` with max/max−1 TX pick, multi-column tiles (≥512 px),
//! segmentation (map + `SEG_LVL_ALT_Q` on the right half), and in-loop
//! deblocking ([`loopfilter`], level 10 with default mode/ref deltas).
//!
//! Layers 1–7 are complete; layer 8 covers multi-ref motion (including compound),
//! rectangular partitions, selectable transforms up to 32×32, switchable
//! interpolation, tile columns, segmentation, and loop filter (ffmpeg-verified
//! in `tests/webm.rs`).
// These modules transcribe libvpx / libde265 reference code, and the point of
// doing that is that a reader can lay the two side by side. So the shape of the
// original survives here: loops that index by hand because the spec numbers its
// arrays, argument lists as long as the C function's, branches left distinct
// where the spec distinguishes cases that happen to compute the same thing, and
// constants grouped the way the bitstream tables print them. Idiomatic Rust
// would read better and would no longer be checkable against the reference.
pub use BoolEncoder;
pub use ;
pub use ;
pub use encode_intra_gray;
/// VP9 forbids a compressed frame from ending with `0b110xxxxx` — that pattern
/// marks a superframe index. Append a padding `0` when needed (libvpx / spec).
pub