1use core::fmt;
7use std::collections::HashMap;
8use std::sync::OnceLock;
9
10use crate::event::Event;
11use crate::state::State;
12use crate::types::{BayAudioDetails, BaySignalDetails};
13use crate::wire::{BayStatus, BayUid, MxrSignalType};
14
15use super::handlers::{u16_at, u32_at};
16use super::Rx;
17
18const SVD_TABLE: &str = include_str!("../svd.csv");
20
21#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
23pub struct Svd {
24 pub id: u16,
26 pub picture_aspect: u16,
28 pub pixel_aspect: u16,
30 pub horizontal_active: u16,
32 pub horizontal_total: u16,
34 pub vertical_active: u16,
36 pub vertical_total: u16,
38 pub refresh: u16,
40 pub interlaced: bool,
42 pub multiplier: u16,
44}
45
46impl fmt::Display for Svd {
47 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48 write!(
49 f,
50 "{}x{}@{}Hz",
51 self.horizontal_active, self.vertical_active, self.refresh
52 )
53 }
54}
55
56fn table() -> &'static HashMap<u16, Svd> {
57 static TABLE: OnceLock<HashMap<u16, Svd>> = OnceLock::new();
58 TABLE.get_or_init(|| {
59 let mut map = HashMap::new();
60 for line in SVD_TABLE.lines() {
61 let fields: Vec<u16> = line
62 .trim()
63 .split(';')
64 .filter_map(|f| f.trim().parse().ok())
65 .collect();
66 let Ok(n) = <[u16; 10]>::try_from(fields.as_slice()) else {
67 continue;
68 };
69 map.insert(
70 n[0],
71 Svd {
72 id: n[0],
73 picture_aspect: n[1],
74 pixel_aspect: n[2],
75 horizontal_active: n[3],
76 horizontal_total: n[4],
77 vertical_active: n[5],
78 vertical_total: n[6],
79 refresh: n[7],
80 interlaced: n[8] == 1,
81 multiplier: n[9],
82 },
83 );
84 }
85 map
86 })
87}
88
89pub fn lookup_svd(id: u16) -> Option<Svd> {
91 table().get(&id).copied()
92}
93
94fn colour_space(v: u8) -> &'static str {
96 match v {
97 0 => "RGB",
98 1 => "4:4:4",
99 2 => "4:2:2",
100 3 => "4:2:0",
101 _ => "unknown",
102 }
103}
104
105const AV_DETAILS_SIZE: usize = 112;
117
118const STREAM_INTERLACED: u8 = 1 << 1;
120const STREAM_NON_INTEGER_CLOCK: u8 = 1 << 3;
121const STREAM_HDR: u8 = 1 << 4;
122
123const NO_SIGNAL: &str = "no signal";
126
127const SUPPORT_STREAM_VALID: u8 = 1 << 1;
129
130const SUPPORT_AUDIO_INFOFRAME: u8 = 1 << 4;
132
133const SUPPORT_AUDIO_VALID: u8 = 1 << 5;
135
136fn audio_details(audio: &[u8], support_flags: u8) -> Option<BayAudioDetails> {
138 if support_flags & SUPPORT_AUDIO_VALID == 0 {
139 return None;
140 }
141 Some(BayAudioDetails {
142 format: audio[10],
143 channels: audio[11],
144 sample_rate: u32_at(audio, 12),
145 coding: (support_flags & SUPPORT_AUDIO_INFOFRAME != 0).then(|| audio[1] >> 4),
148 })
149}
150
151pub(super) fn signal_status(state: &mut State, rx: &Rx<'_>, ev: &mut Vec<Event>) {
162 let p = rx.frame.payload();
163 if p.len() < AV_DETAILS_SIZE {
164 return;
165 }
166 let support_flags = p[2];
167 let stream_flags = p[3];
168 let stream_valid = support_flags & SUPPORT_STREAM_VALID != 0;
169
170 let bay_block = &p[100..112];
171 let port = u16_at(bay_block, 0);
172 let bay = BayUid::new(rx.sender(), port);
173 if state.bay(bay).is_none() {
174 return;
175 }
176
177 let video = &p[40..56];
178 let svd_id = u16::from(video[0]);
179 let mut frame_rate = f64::from(u16_at(video, 8));
180 if stream_flags & STREAM_NON_INTEGER_CLOCK != 0 {
181 frame_rate = (frame_rate * 1000.0 / 1001.0 * 100.0).round() / 100.0;
182 }
183
184 let signal_type = match lookup_svd(svd_id) {
185 Some(svd) if stream_valid && svd_id != 0 => {
186 let mut description = format!(
187 "{}x{} / {} / {}bpp",
188 svd.horizontal_active,
189 svd.vertical_active,
190 colour_space(video[1]),
191 video[2]
192 );
193 if stream_flags & STREAM_INTERLACED != 0 {
194 description.push_str(" interlaced");
195 }
196 if stream_flags & STREAM_HDR != 0 {
197 description.push_str(" HDR");
198 }
199 description.push_str(&format!(" / {frame_rate}Hz"));
200 description
201 }
202 _ => NO_SIGNAL.to_owned(),
208 };
209
210 let details = BaySignalDetails {
211 frame_rate,
212 tmds_clock: u32_at(video, 10),
213 status: BayStatus::from_bits(u32_at(bay_block, 2)),
214 scaling: MxrSignalType::from_wire(u16_at(bay_block, 6)),
215 clock_rate: u32_at(bay_block, 8),
216 audio: audio_details(&p[24..40], support_flags),
217 };
218
219 if let Some(bay) = state.bay_mut(bay) {
220 bay.set_signal_details(details);
221 bay.apply_signal_status(stream_valid, Some(signal_type), ev);
222 }
223}