forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiB0563cbfTahinli2 months ago824 commits

caps.rs143 lines, 5.8 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, HEVC and AV1 both
108 // ways, VP9 decode only, and the 10-bit profiles on three of the four.
109 // HEVC is an encode seat here as of the plugin's
110 // `vh_enc_hevc_open_colour` --
111 // the 10-bit note beside it stays a *decode* claim, that seat coding
112 // 8-bit Main whatever the source was.
113 let radeonsi = VhCaps {
114 decode: CAP_H264 | CAP_HEVC | CAP_VP9 | CAP_AV1,
115 encode: CAP_H264 | CAP_HEVC | CAP_AV1,
116 decode_10bit: CAP_HEVC | CAP_VP9 | CAP_AV1,
117 };
118 assert_eq!(
119 hw_line(Some(radeonsi)),
120 "H.264 dec+enc · HEVC dec+enc (10-bit) · VP9 dec (10-bit) · AV1 dec+enc (10-bit)"
121 );
122 // A codec the driver has neither seat for is left out rather than
123 // listed as a refusal: this line is what the machine *can* do.
124 let h264_only = VhCaps {
125 decode: CAP_H264,
126 ..Default::default()
127 };
128 assert_eq!(hw_line(Some(h264_only)), "H.264 dec");
129 }
130
131 #[test]
132 fn the_software_line_names_every_encoder_this_build_carries() {
133 let line = software();
134 for name in ["rusty_h264", "oxideav-h265 intra", "rav1e", "rusty_aac"] {
135 assert!(line.contains(name), "{name} missing from {line}");
136 }
137 // Decode-only crates must not read as encoders, and the seat words are
138 // the only thing saying which is which.
139 assert!(line.contains("AC-3 dec (oxideav-ac3)"));
140 assert!(line.contains("H.264 dec+enc (rusty_h264)"));
141 assert!(line.contains("HEVC enc (oxideav-h265 intra)"));
142 }
143}