forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

lib.rs128 lines, 4.6 KiBHistoryRawDownload
1//! Native VP9 decoder: all four profiles, 8/10/12-bit, and every coded
2//! subsampling (4:2:0, 4:2:2, 4:4:0, 4:4:4).
3//!
4//! The crate implements the VP9 bitstream specification directly
5//! ([spec]); libvpx's output is used only as a test oracle
6//! (sample-exactness witnesses against ffmpeg's libvpx-based decoder),
7//! never as code linked at runtime. Every module documents the spec
8//! section it implements. Inter frames decode through motion compensation,
9//! residual reconstruction and the loop filter; intra-only frames and
10//! scaled references are supported too.
11//!
12//! Planes are stored as `u16` for every depth ([`Sample`]); kernels take the
13//! frame's `bd` and clamp with `clip_pixel_bd`, so one decode walk serves
14//! every depth and subsampling. The only depth-dependent bitstream element is
15//! the CAT6 coefficient token (14 bits/`vp9_cat6_prob` at 8-bit, 16 bits/
16//! `vp9_cat6_prob_high12 + 2` at 10-bit, 18 bits/`vp9_cat6_prob_high12` at
17//! 12-bit).
18//!
19//! [spec]: https://www.webmproject.org/docs/vp9/
20//!
21//! # Unsafe
22//!
23//! This crate contains no `unsafe` code.
24
25#![forbid(unsafe_code)]
26#![warn(missing_docs)]
27// Spec-transcription lints: the DSP, loop-filter and mode-info kernels mirror
28// libvpx argument lists and index-driven pseudo-code, kept as written so each
29// line stays checkable against the spec. Byte-exact paths are not restructured.
30#![allow(
31 clippy::too_many_arguments,
32 clippy::type_complexity,
33 clippy::needless_range_loop,
34 clippy::neg_multiply,
35 clippy::collapsible_if,
36 clippy::ptr_arg
37)]
38
39pub(crate) mod adapt;
40pub mod bool;
41pub mod decode;
42pub mod header;
43pub(crate) mod inter;
44pub mod intra;
45#[doc(hidden)]
46pub mod knobs;
47pub mod loopfilter;
48pub(crate) mod mc;
49pub mod modes;
50pub mod prof;
51mod stack_budget;
52pub mod stream;
53pub mod tables;
54pub mod tokens;
55pub mod transform;
56
57pub use ec_core::{Error, Result};
58
59/// A reconstructed sample. Planes are stored as `u16` for EVERY bit depth
60/// (8-bit values occupy the low byte), so profile-0 and profile-2 frames share
61/// one representation and one decode walk; the kernels take the frame's
62/// `bd` and clamp with [`clip_pixel_bd`].
63pub type Sample = u16;
64
65/// `clip_pixel_highbd` (`vpx_dsp/vpx_dsp_common.h`): clamp to `[0, (1<<bd)-1]`.
66/// For `bd == 8` this is libvpx's `clip_pixel` exactly.
67#[inline]
68pub(crate) fn clip_pixel_bd(v: i32, bd: u8) -> Sample {
69 v.clamp(0, (1i32 << bd) - 1) as Sample
70}
71
72/// `ROUND_POWER_OF_TWO(v, n)` (`vpx_dsp/txfm_common.h`, arithmetic shift).
73#[inline]
74pub(crate) fn round_pow2(v: i32, n: i32) -> i32 {
75 (v + (1 << (n - 1))) >> n
76}
77
78/// Whether `EC_VP9_TRACE` decision tracing is enabled, resolved once per
79/// process. An `var_os` per bool read would lock std's ENV lock on every
80/// coefficient; the hot path is a single relaxed atomic load.
81pub(crate) fn trace_enabled() -> bool {
82 use std::sync::atomic::{AtomicU8, Ordering};
83 static STATE: AtomicU8 = AtomicU8::new(0);
84 match STATE.load(Ordering::Relaxed) {
85 0 => {
86 let on = std::env::var_os("EC_VP9_TRACE").is_some();
87 STATE.store(if on { 2 } else { 1 }, Ordering::Relaxed);
88 on
89 }
90 1 => false,
91 _ => true,
92 }
93}
94
95/// Cached `getenv` probe: `EC_VP9_*` gates are consulted per block, per MV
96/// component and per switchable-interp read, and `std::env::var_os` locks std's
97/// ENV lock, so each gate resolves once per process and the hot path is a
98/// single relaxed atomic load (see `trace_enabled`).
99macro_rules! cached_gate {
100 ($fn_name:ident, $env:literal) => {
101 pub(crate) fn $fn_name() -> bool {
102 use std::sync::atomic::{AtomicU8, Ordering};
103 static STATE: AtomicU8 = AtomicU8::new(0);
104 match STATE.load(Ordering::Relaxed) {
105 0 => {
106 let on = std::env::var_os($env).is_some();
107 STATE.store(if on { 2 } else { 1 }, Ordering::Relaxed);
108 on
109 }
110 1 => false,
111 _ => true,
112 }
113 }
114 };
115}
116
117// Per-block dump gate for the inter-syntax lane (scratch harnesses);
118// a candidate-list dump (`mode == NEWMV` only);
119// a search-position cell + grid-store dump; and a post-compressed-header
120// MV probability table dump. (Plain comments: a doc comment on a macro
121// invocation is an "unused doc comment" warning.)
122cached_gate!(interdump_enabled, "EC_VP9_INTERDUMP");
123cached_gate!(mvdbg_enabled, "EC_VP9_MVDGB");
124cached_gate!(mvdbg2_enabled, "EC_VP9_MVDGB2");
125cached_gate!(mvdump_enabled, "EC_VP9_MVDUMP");
126// [scratch] per-transform-block token state (the inter pixel lane).
127cached_gate!(tokdbg_enabled, "EC_VP9_TOKDGB");
128cached_gate!(lfmasksy_enabled, "EC_VP9_LFMASKSY");