forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBb02f499Tahinli1 month ago1000 commits

caps.rs171 lines, 7.0 KiBHistoryRawDownload
1//! What this editor can really decode and encode, in one place and in two
2//! halves.
3//!
4//! The *software* half is what this binary was built with: a table, because
5//! "is `rav1e` linked into this build" is a question the build already answered
6//! and probing for it would only be a slower way of reading the manifest.
7//!
8//! The *hardware* half is a different answer on every machine and is therefore
9//! never a table: [`crate::hw::caps`] asks the plugin, the plugin asks the
10//! driver, and what comes back is the intersection of the GPU's entrypoints
11//! with the codecs the plugin implements. No plugin, an older plugin or no
12//! driver all read the same way -- "software only" -- because that is what they
13//! mean for the file a user is about to write.
14//!
15//! VP8 is deliberately on neither line: the plugin decodes it on its own
16//! native `ec-vp8`, always present, and reports `CAP_VP8` for it -- the
17//! plugin's seat, never the GPU's. [`HW_CODECS`] does not carry the bit,
18//! because this file's one job is not presenting a seat as a GPU feat that
19//! no GPU has.
20
21use crate::hw::{CAP_AV1, CAP_H264, CAP_HEVC, CAP_VP9, VhCaps};
22
23/// The codecs a hardware line can name, in the order it lists them.
24const HW_CODECS: [(u32, &str); 4] = [
25 (CAP_H264, "H.264"),
26 (CAP_HEVC, "HEVC"),
27 (CAP_VP9, "VP9"),
28 (CAP_AV1, "AV1"),
29];
30
31/// The software half: the codec, the crate that carries it here, and which
32/// halves of the job that crate does. Named crates rather than a bare "SW",
33/// because the answer to "why is my HEVC file so large" is `ec-h265 intra` (the intra-only setting),
34/// and the row that says so is the row that answers it.
35const SW_CODECS: [(&str, &str, bool, bool); 10] = [
36 ("H.264", "ec-h264", true, true),
37 ("HEVC", "ec-h265 GOP", false, true),
38 ("AV1", "rav1e", false, true),
39 // Decode is split by width: symphonia carries stereo and narrower, `ec-aac`
40 // everything wider; the encoder is `ec-aac` as well.
41 ("AAC", "symphonia / ec-aac / ec-aac", true, true),
42 ("MP3", "symphonia / ec-mp3", true, true),
43 ("FLAC", "symphonia / ec-flac", true, true),
44 ("PCM", "symphonia / ec-riff", true, true),
45 ("AC-3", "ec-ac3", true, false),
46 ("Vorbis", "ec-vorbis", true, true),
47 ("ALAC", "symphonia", true, false),
48];
49
50/// The hardware line as a front-end shows it: one measurement per process,
51/// costing the plugin's VA-API init (~90 ms) the first time. Ask it off a
52/// render thread.
53pub fn hardware() -> String {
54 hw_line(crate::hw::caps())
55}
56
57/// The software line. Pure and constant -- a caller may ask it per repaint.
58pub fn software() -> String {
59 SW_CODECS
60 .iter()
61 .map(|&(name, krate, decodes, encodes)| {
62 format!("{name} {} ({krate})", seats(decodes, encodes))
63 })
64 .collect::<Vec<_>>()
65 .join(" · ")
66}
67
68/// What the plugin answered, in words. Separate from [`hardware`] so the
69/// wording can be tested against a machine this test suite does not have.
70fn hw_line(caps: Option<VhCaps>) -> String {
71 let Some(caps) = caps else {
72 return "none — no plugin, no driver, or a plugin too old to say".to_string();
73 };
74 let listed: Vec<String> = HW_CODECS
75 .iter()
76 .filter(|&&(bit, _)| caps.decode & bit != 0 || caps.encode & bit != 0)
77 .map(|&(bit, name)| {
78 let seats = seats(caps.decode & bit != 0, caps.encode & bit != 0);
79 match caps.decode_10bit & bit != 0 {
80 true => format!("{name} {seats} (10-bit)"),
81 false => format!("{name} {seats}"),
82 }
83 })
84 .collect();
85 match listed.is_empty() {
86 // A driver answered and takes none of the codecs this build carries:
87 // not the same thing as no driver, and worth saying so.
88 true => "none — the driver takes none of these codecs".to_string(),
89 false => listed.join(" · "),
90 }
91}
92
93/// Which halves of the job one seat does. Never both empty: a codec with
94/// neither is not listed at all.
95fn seats(decodes: bool, encodes: bool) -> &'static str {
96 match (decodes, encodes) {
97 (true, true) => "dec+enc",
98 (true, false) => "dec",
99 _ => "enc",
100 }
101}
102
103#[cfg(test)]
104mod tests {
105 use super::*;
106 use crate::hw::CAP_VP8;
107
108 #[test]
109 fn the_hardware_line_says_what_the_plugin_answered_and_nothing_more() {
110 // No answer at all -- no plugin, no driver, or one older than the
111 // symbol -- is one line, and never an empty listing that would read as
112 // "still loading".
113 assert!(hw_line(None).starts_with("none —"));
114 // A driver that answers with nothing is its own sentence.
115 assert!(hw_line(Some(VhCaps::default())).starts_with("none —"));
116 // This project's own GPU as `vainfo` lists it: H.264, HEVC and AV1 both
117 // ways, VP9 decode only, and the 10-bit profiles on three of the four.
118 // HEVC is an encode seat here as of the plugin's
119 // `vh_enc_hevc_open_colour` --
120 // the 10-bit note beside it stays a *decode* claim, that seat coding
121 // 8-bit Main whatever the source was.
122 let radeonsi = VhCaps {
123 decode: CAP_H264 | CAP_HEVC | CAP_VP9 | CAP_AV1,
124 encode: CAP_H264 | CAP_HEVC | CAP_AV1,
125 decode_10bit: CAP_HEVC | CAP_VP9 | CAP_AV1,
126 };
127 assert_eq!(
128 hw_line(Some(radeonsi)),
129 "H.264 dec+enc · HEVC dec+enc (10-bit) · VP9 dec (10-bit) · AV1 dec+enc (10-bit)"
130 );
131 // A codec the driver has neither seat for is left out rather than
132 // listed as a refusal: this line is what the machine *can* do.
133 let h264_only = VhCaps {
134 decode: CAP_H264,
135 ..Default::default()
136 };
137 assert_eq!(hw_line(Some(h264_only)), "H.264 dec");
138 }
139
140 /// The plugin's VP8 bit is its own native decoder (`ec-vp8`), lit
141 /// unconditionally. However lit the mask is, the hardware line never
142 /// reads it as a GPU seat -- and a mask holding nothing *but* it still
143 /// reads as the nothing a driver without seats would answer.
144 #[test]
145 fn the_plugin_s_software_vp8_never_reads_as_a_gpu_seat() {
146 let vpx_only = VhCaps {
147 decode: CAP_VP8,
148 ..Default::default()
149 };
150 assert!(hw_line(Some(vpx_only)).starts_with("none —"));
151 let with_gpu = VhCaps {
152 decode: CAP_H264 | CAP_VP8,
153 encode: CAP_H264,
154 ..Default::default()
155 };
156 assert_eq!(hw_line(Some(with_gpu)), "H.264 dec+enc");
157 }
158
159 #[test]
160 fn the_software_line_names_every_encoder_this_build_carries() {
161 let line = software();
162 for name in ["ec-h264", "ec-h265 GOP", "rav1e", "ec-aac"] {
163 assert!(line.contains(name), "{name} missing from {line}");
164 }
165 // Decode-only crates must not read as encoders, and the seat words are
166 // the only thing saying which is which.
167 assert!(line.contains("AC-3 dec (ec-ac3)"));
168 assert!(line.contains("H.264 dec+enc (ec-h264)"));
169 assert!(line.contains("HEVC enc (ec-h265 GOP)"));
170 }
171}