forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBcc667e6Tahinli3 months ago285 commits

caps.rs141 lines, 5.7 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
15use crate::hw::{CAP_AV1, CAP_H264, CAP_HEVC, CAP_VP9, VhCaps};
16
17/// The codecs a hardware line can name, in the order it lists them.
18const HW_CODECS: [(u32, &str); 4] = [
19 (CAP_H264, "H.264"),
20 (CAP_HEVC, "HEVC"),
21 (CAP_VP9, "VP9"),
22 (CAP_AV1, "AV1"),
23];
24
25/// The software half: the codec, the crate that carries it here, and which
26/// halves of the job that crate does. Named crates rather than a bare "SW",
27/// because the answer to "why is my HEVC file so large" is `oxideav-h265
28/// intra`, and the row that says so is the row that answers it.
29const SW_CODECS: [(&str, &str, bool, bool); 10] = [
30 ("H.264", "rusty_h264", true, true),
31 ("HEVC", "oxideav-h265 intra", false, true),
32 ("AV1", "rav1e", false, true),
33 ("AAC", "rusty_aac", true, true),
34 ("MP3", "symphonia / rusty_mp3", true, true),
35 ("FLAC", "symphonia / flacenc", true, true),
36 ("PCM", "symphonia / hound", true, true),
37 ("AC-3", "oxideav-ac3", true, false),
38 ("Vorbis", "symphonia / rusty_vorbis", true, true),
39 ("ALAC", "symphonia", true, false),
40];
41
42/// The hardware line as a front-end shows it: one measurement per process,
43/// costing the plugin's VA-API init (~90 ms) the first time. Ask it off a
44/// render thread.
45pub fn hardware() -> String {
46 hw_line(crate::hw::caps())
47}
48
49/// The software line. Pure and constant -- a caller may ask it per repaint.
50pub fn software() -> String {
51 SW_CODECS
52 .iter()
53 .map(|&(name, krate, decodes, encodes)| {
54 format!("{name} {} ({krate})", seats(decodes, encodes))
55 })
56 .collect::<Vec<_>>()
57 .join(" · ")
58}
59
60/// What the plugin answered, in words. Separate from [`hardware`] so the
61/// wording can be tested against a machine this test suite does not have.
62fn hw_line(caps: Option<VhCaps>) -> String {
63 let Some(caps) = caps else {
64 return "none — no plugin, no driver, or a plugin too old to say".to_string();
65 };
66 let listed: Vec<String> = HW_CODECS
67 .iter()
68 .filter(|&&(bit, _)| caps.decode & bit != 0 || caps.encode & bit != 0)
69 .map(|&(bit, name)| {
70 let seats = seats(caps.decode & bit != 0, caps.encode & bit != 0);
71 match caps.decode_10bit & bit != 0 {
72 true => format!("{name} {seats} (10-bit)"),
73 false => format!("{name} {seats}"),
74 }
75 })
76 .collect();
77 match listed.is_empty() {
78 // A driver answered and takes none of the codecs this build carries:
79 // not the same thing as no driver, and worth saying so.
80 true => "none — the driver takes none of these codecs".to_string(),
81 false => listed.join(" · "),
82 }
83}
84
85/// Which halves of the job one seat does. Never both empty: a codec with
86/// neither is not listed at all.
87fn seats(decodes: bool, encodes: bool) -> &'static str {
88 match (decodes, encodes) {
89 (true, true) => "dec+enc",
90 (true, false) => "dec",
91 _ => "enc",
92 }
93}
94
95#[cfg(test)]
96mod tests {
97 use super::*;
98
99 #[test]
100 fn the_hardware_line_says_what_the_plugin_answered_and_nothing_more() {
101 // No answer at all -- no plugin, no driver, or one older than the
102 // symbol -- is one line, and never an empty listing that would read as
103 // "still loading".
104 assert!(hw_line(None).starts_with("none —"));
105 // A driver that answers with nothing is its own sentence.
106 assert!(hw_line(Some(VhCaps::default())).starts_with("none —"));
107 // This project's own GPU as `vainfo` lists it: H.264 both ways, HEVC
108 // and VP9 decode with their 10-bit profiles, AV1 both ways. HEVC has a
109 // VA encode entrypoint here and is *not* an encode seat: the plugin has
110 // no HEVC encoder, so the line must not offer one.
111 let radeonsi = VhCaps {
112 decode: CAP_H264 | CAP_HEVC | CAP_VP9 | CAP_AV1,
113 encode: CAP_H264 | CAP_AV1,
114 decode_10bit: CAP_HEVC | CAP_VP9 | CAP_AV1,
115 };
116 assert_eq!(
117 hw_line(Some(radeonsi)),
118 "H.264 dec+enc · HEVC dec (10-bit) · VP9 dec (10-bit) · AV1 dec+enc (10-bit)"
119 );
120 // A codec the driver has neither seat for is left out rather than
121 // listed as a refusal: this line is what the machine *can* do.
122 let h264_only = VhCaps {
123 decode: CAP_H264,
124 ..Default::default()
125 };
126 assert_eq!(hw_line(Some(h264_only)), "H.264 dec");
127 }
128
129 #[test]
130 fn the_software_line_names_every_encoder_this_build_carries() {
131 let line = software();
132 for name in ["rusty_h264", "oxideav-h265 intra", "rav1e", "rusty_aac"] {
133 assert!(line.contains(name), "{name} missing from {line}");
134 }
135 // Decode-only crates must not read as encoders, and the seat words are
136 // the only thing saying which is which.
137 assert!(line.contains("AC-3 dec (oxideav-ac3)"));
138 assert!(line.contains("H.264 dec+enc (rusty_h264)"));
139 assert!(line.contains("HEVC enc (oxideav-h265 intra)"));
140 }
141}