forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

knobs.rs69 lines, 2.2 KiBHistoryRawDownload
1//! Debug switches the VP9 decoder reads from the environment, with a
2//! per-thread override that tests set instead of editing the process
3//! environment (which is `unsafe` in Rust 2024).
4//!
5//! The decoder reads these on the thread that calls `decode`: tile workers
6//! and the loop-filter threads never read them. An override set on a test's
7//! own thread therefore covers exactly that test's decodes. With no override
8//! set, each reader answers from the environment exactly as it always has.
9
10use std::cell::Cell;
11use std::thread::LocalKey;
12
13thread_local! {
14 static SKIP_LF: Cell<Option<bool>> = const { Cell::new(None) };
15 static FORCE_TAIL: Cell<Option<usize>> = const { Cell::new(None) };
16}
17
18/// Restores the value a knob had before the guard was created, when dropped.
19#[doc(hidden)]
20#[must_use = "the override ends as soon as this guard is dropped"]
21pub struct Override {
22 restore: Option<Box<dyn FnOnce()>>,
23}
24
25impl Drop for Override {
26 fn drop(&mut self) {
27 if let Some(restore) = self.restore.take() {
28 restore();
29 }
30 }
31}
32
33fn force<T: Copy + 'static>(slot: &'static LocalKey<Cell<T>>, value: T) -> Override {
34 let prev = slot.with(|c| c.replace(value));
35 Override {
36 restore: Some(Box::new(move || slot.with(|c| c.set(prev)))),
37 }
38}
39
40/// `EC_VP9_SKIP_LF`: when set, the loop-filter post-pass is skipped.
41pub(crate) fn skip_lf() -> bool {
42 SKIP_LF
43 .with(Cell::get)
44 .unwrap_or_else(|| std::env::var_os("EC_VP9_SKIP_LF").is_some())
45}
46
47/// `EC_VP9_FORCE_TAIL`: overrides the keyframe tail start offset, if set and
48/// parseable.
49pub(crate) fn force_tail() -> Option<usize> {
50 FORCE_TAIL.with(Cell::get).or_else(|| {
51 std::env::var("EC_VP9_FORCE_TAIL")
52 .ok()
53 .and_then(|s| s.parse().ok())
54 })
55}
56
57/// Forces `EC_VP9_SKIP_LF` on (`Some(true)`), off (`Some(false)`) or back to
58/// the environment (`None`) on this thread, until the guard drops.
59#[doc(hidden)]
60pub fn set_skip_lf(on: Option<bool>) -> Override {
61 force(&SKIP_LF, on)
62}
63
64/// Forces `EC_VP9_FORCE_TAIL` to `tail` on this thread, until the guard drops.
65/// `None` returns the knob to the environment.
66#[doc(hidden)]
67pub fn set_force_tail(tail: Option<usize>) -> Override {
68 force(&FORCE_TAIL, tail)
69}