forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

autothreads.rs110 lines, 4.0 KiBHistoryRawDownload
1//! lane-av1threads: the decoder's default frame-worker count.
2//!
3//! `EC_AV1_THREADS` used to default to 1, so an edith decode ran on one core.
4//! It now defaults to `min(available_parallelism, `[`DEFAULT_CAP`]`)`, and
5//! `EC_AV1_THREADS=1` still forces the single-threaded decoder. Frame workers
6//! are byte-exact by the stream gates; they do not on their own reach 1080p30
7//! realtime on the bench stream (see `lanes/av1threads.report.md`).
8//!
9//! The in-frame filter bands follow the thread count: at `EC_AV1_THREADS > 1`
10//! they default to [`split`]'s filter share, and `EC_AV1_FILTER_THREADS`
11//! overrides it. `EC_AV1_THREADS=1` is fully serial. Reconstruction runs on
12//! the superblock-row wavefront at `EC_AV1_RECON_THREADS` workers, which
13//! defaults to the recon half of [`split`].
14
15/// Upper bound on the default frame-worker count.
16pub(crate) const DEFAULT_CAP: usize = 6;
17
18/// The default decode thread count: the core count, capped at
19/// [`DEFAULT_CAP`]. Never less than 1.
20pub(crate) fn default_threads() -> usize {
21 std::thread::available_parallelism()
22 .map_or(1, std::num::NonZeroUsize::get)
23 .clamp(1, DEFAULT_CAP)
24}
25
26/// The in-frame `(recon, filter)` thread split for `threads` decode threads.
27/// The recon half is the wavefront's worker count (2 to 4); the filter half
28/// is serial at 1, otherwise half the threads (1 to 3).
29pub(crate) fn split(threads: usize) -> (usize, usize) {
30 if threads <= 1 {
31 return (1, 1);
32 }
33 ((threads * 3 / 4).clamp(2, 4), (threads / 2).clamp(1, 3))
34}
35
36/// The decode thread count for an `EC_AV1_THREADS` value: the parsed value
37/// when it is a positive integer, otherwise [`default_threads`].
38///
39/// Under `cfg(test)` the unset default is 1: the gates read thread-local
40/// counters that frame workers never increment, so a threaded default would
41/// blind about 150 of them. The threaded path is pinned explicitly by
42/// [`tests::frame_workers_decode_the_same_pictures_as_one_thread`].
43pub(crate) fn decode_threads_from(value: Option<&str>) -> usize {
44 value
45 .and_then(|v| v.trim().parse::<usize>().ok())
46 .filter(|&n| n > 0)
47 .unwrap_or_else(|| if cfg!(test) { 1 } else { default_threads() })
48}
49
50/// Decodes `data` with `threads` frame workers and returns every shown
51/// picture's planes, in output order.
52#[cfg(test)]
53fn shown_planes(data: &[u8], threads: usize) -> Vec<(usize, Vec<u16>, Vec<u16>, Vec<u16>)> {
54 let mut out = Vec::new();
55 crate::stream::decode_stream_with_threads(data, threads, |picture, _, shown| {
56 if shown {
57 out.push((
58 picture.width,
59 picture.y.clone(),
60 picture.u.clone(),
61 picture.v.clone(),
62 ));
63 }
64 Ok(())
65 })
66 .expect("fixture decodes");
67 out
68}
69
70#[cfg(test)]
71mod tests {
72 use super::*;
73
74 #[test]
75 fn explicit_env_value_wins_and_zero_or_junk_falls_back() {
76 assert_eq!(decode_threads_from(Some("1")), 1);
77 assert_eq!(decode_threads_from(Some(" 3 ")), 3);
78 assert_eq!(decode_threads_from(Some("0")), 1);
79 assert_eq!(decode_threads_from(Some("many")), 1);
80 assert_eq!(decode_threads_from(None), 1);
81 }
82
83 #[test]
84 fn frame_workers_decode_the_same_pictures_as_one_thread() {
85 let fixtures: [&[u8]; 2] = [
86 include_bytes!("../fixtures/420_mixll_altref_640x480_5f.obu"),
87 include_bytes!("../fixtures/420_mixll_320x240_5f.obu"),
88 ];
89 for data in fixtures {
90 let serial = shown_planes(data, 1);
91 assert!(!serial.is_empty(), "fixture shows no frames");
92 assert_eq!(serial, shown_planes(data, 4));
93 }
94 }
95
96 #[test]
97 fn split_is_serial_at_one_thread_and_bounded_above() {
98 assert_eq!(split(1), (1, 1));
99 assert_eq!(split(2), (2, 1));
100 assert_eq!(split(4), (3, 2));
101 assert_eq!(split(6), (4, 3));
102 assert_eq!(split(64), (4, 3));
103 }
104
105 #[test]
106 fn default_is_capped_and_positive() {
107 let n = default_threads();
108 assert!((1..=DEFAULT_CAP).contains(&n));
109 }
110}