forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB70849e0Tahinli2 months ago3122 commits

lib.rs112 lines, 4.1 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#![deny(unsafe_code)]
26#![warn(missing_docs)]
27
28pub mod bool;
29pub mod decode;
30pub mod header;
31pub(crate) mod inter;
32pub mod intra;
33pub mod loopfilter;
34pub(crate) mod mc;
35pub mod modes;
36pub mod stream;
37pub mod tables;
38pub mod tokens;
39pub mod transform;
40
41pub use ec_core::{Error, Result};
42
43/// A reconstructed sample. Planes are stored as `u16` for EVERY bit depth
44/// (8-bit values occupy the low byte), so profile-0 and profile-2 frames share
45/// one representation and one decode walk; the kernels take the frame's
46/// `bd` and clamp with [`clip_pixel_bd`].
47pub type Sample = u16;
48
49/// `clip_pixel_highbd` (`vpx_dsp/vpx_dsp_common.h`): clamp to `[0, (1<<bd)-1]`.
50/// For `bd == 8` this is libvpx's `clip_pixel` exactly.
51#[inline]
52pub(crate) fn clip_pixel_bd(v: i32, bd: u8) -> Sample {
53 v.clamp(0, (1i32 << bd) - 1) as Sample
54}
55
56/// `ROUND_POWER_OF_TWO(v, n)` (`vpx_dsp/txfm_common.h`, arithmetic shift).
57#[inline]
58pub(crate) fn round_pow2(v: i32, n: i32) -> i32 {
59 (v + (1 << (n - 1))) >> n
60}
61
62/// Whether `EC_VP9_TRACE` decision tracing is enabled, resolved once per
63/// process. An `var_os` per bool read would lock std's ENV lock on every
64/// coefficient; the hot path is a single relaxed atomic load.
65pub(crate) fn trace_enabled() -> bool {
66 use std::sync::atomic::{AtomicU8, Ordering};
67 static STATE: AtomicU8 = AtomicU8::new(0);
68 match STATE.load(Ordering::Relaxed) {
69 0 => {
70 let on = std::env::var_os("EC_VP9_TRACE").is_some();
71 STATE.store(if on { 2 } else { 1 }, Ordering::Relaxed);
72 on
73 }
74 1 => false,
75 _ => true,
76 }
77}
78
79/// Cached `getenv` probe: `EC_VP9_*` gates are consulted per block, per MV
80/// component and per switchable-interp read, and `std::env::var_os` locks std's
81/// ENV lock, so each gate resolves once per process and the hot path is a
82/// single relaxed atomic load (see `trace_enabled`).
83macro_rules! cached_gate {
84 ($fn_name:ident, $env:literal) => {
85 pub(crate) fn $fn_name() -> bool {
86 use std::sync::atomic::{AtomicU8, Ordering};
87 static STATE: AtomicU8 = AtomicU8::new(0);
88 match STATE.load(Ordering::Relaxed) {
89 0 => {
90 let on = std::env::var_os($env).is_some();
91 STATE.store(if on { 2 } else { 1 }, Ordering::Relaxed);
92 on
93 }
94 1 => false,
95 _ => true,
96 }
97 }
98 };
99}
100
101// Per-block dump gate for the inter-syntax lane (scratch harnesses);
102// a candidate-list dump (`mode == NEWMV` only);
103// a search-position cell + grid-store dump; and a post-compressed-header
104// MV probability table dump. (Plain comments: a doc comment on a macro
105// invocation is an "unused doc comment" warning.)
106cached_gate!(interdump_enabled, "EC_VP9_INTERDUMP");
107cached_gate!(mvdbg_enabled, "EC_VP9_MVDGB");
108cached_gate!(mvdbg2_enabled, "EC_VP9_MVDGB2");
109cached_gate!(mvdump_enabled, "EC_VP9_MVDUMP");
110// [scratch] per-transform-block token state (the inter pixel lane).
111cached_gate!(tokdbg_enabled, "EC_VP9_TOKDGB");
112cached_gate!(lfmasksy_enabled, "EC_VP9_LFMASKSY");