diff --git a/src/timings.rs b/src/timings.rs index f78cf65..69d374c 100644 --- a/src/timings.rs +++ b/src/timings.rs @@ -1,10 +1,22 @@ +//! Detailed timing presets, ported from CRU's `DetailedResolutionClass`. +//! +//! The arithmetic deliberately mirrors CRU's integer math (pixel clock in +//! 10 kHz EDID DTD units, refresh in millihertz, vertical geometry in +//! full-frame lines) so a preset produces the same timing CRU would. + +mod tables; + use crate::models::TimingDescriptor; +use tables::{AUTOMATIC_CRT, AUTOMATIC_HDTV, AUTOMATIC_PC, NATIVE, TableTiming}; #[derive(Debug, Clone, Copy, PartialEq)] pub struct CvtRequest { pub width: u16, + /// Frame height; for interlaced modes this is both fields together. pub height: u16, + /// Field rate for interlaced modes, frame rate otherwise. pub refresh_hz: f64, + pub interlaced: bool, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -17,10 +29,16 @@ pub enum TimingPreset { NativeHdtv, Exact, ExactReduced, + ExactCrt, + CvtStandard, + CvtRbStandard, + CvtRb2Standard, + GtfStandard, + VerticalTotal, } impl TimingPreset { - pub const ALL: [Self; 8] = [ + pub const ALL: [Self; 14] = [ Self::Manual, Self::AutomaticPc, Self::AutomaticHdtv, @@ -29,18 +47,30 @@ impl TimingPreset { Self::NativeHdtv, Self::Exact, Self::ExactReduced, + Self::ExactCrt, + Self::CvtStandard, + Self::CvtRbStandard, + Self::CvtRb2Standard, + Self::GtfStandard, + Self::VerticalTotal, ]; pub fn label(self) -> &'static str { match self { Self::Manual => "Manual", - Self::AutomaticPc => "Automatic (PC)", - Self::AutomaticHdtv => "Automatic (HDTV)", - Self::AutomaticCrt => "Automatic (CRT)", - Self::NativePc => "Native (PC)", - Self::NativeHdtv => "Native (HDTV)", + Self::AutomaticPc => "Automatic PC", + Self::AutomaticHdtv => "Automatic HDTV", + Self::AutomaticCrt => "Automatic CRT", + Self::NativePc => "Native PC", + Self::NativeHdtv => "Native HDTV", Self::Exact => "Exact", Self::ExactReduced => "Exact reduced", + Self::ExactCrt => "Exact CRT", + Self::CvtStandard => "CVT standard", + Self::CvtRbStandard => "CVT-RB standard", + Self::CvtRb2Standard => "CVT-RB2 standard", + Self::GtfStandard => "GTF standard", + Self::VerticalTotal => "Vertical total calculator (QFT)", } } @@ -54,308 +84,966 @@ impl TimingPreset { } } +/// Computes the timing a preset yields for the requested size and refresh. +/// +/// `current` supplies what CRU keeps from the existing timing: everything for +/// Manual, the sync polarity for Exact/Exact reduced, and the horizontal +/// timing, vertical front porch/sync and pixel clock for the vertical total +/// calculator. pub fn timing_for_preset( preset: TimingPreset, request: CvtRequest, current: Option<&TimingDescriptor>, -) -> TimingDescriptor { - match preset { - TimingPreset::Manual => current +) -> Result { + if preset == TimingPreset::Manual { + return current .cloned() - .unwrap_or_else(|| cvt_reduced_blanking(request)), - TimingPreset::AutomaticPc | TimingPreset::NativePc => cvt_reduced_blanking(request), - TimingPreset::AutomaticHdtv | TimingPreset::NativeHdtv => hdtv_timing(request), - TimingPreset::AutomaticCrt => crt_timing(request), - TimingPreset::Exact => exact_timing(request, current), - TimingPreset::ExactReduced => { - let mut timing = cvt_reduced_blanking(request); - timing.pixel_clock_khz = exact_pixel_clock_khz( - u32::from(timing.h_total()), - u32::from(timing.v_total()), - request.refresh_hz, - ); - timing - } + .ok_or("manual timing needs an existing timing"); } -} -/// Produces a practical CVT reduced-blanking style timing. -/// -/// The constants match the common CVT-RB v1 porch shape: 160 horizontal -/// blanking pixels, 48 pixel front porch, 32 pixel sync, 3 line vertical front -/// porch, and a minimum 460 us vertical blanking interval. -pub fn cvt_reduced_blanking(request: CvtRequest) -> TimingDescriptor { - const CELL_GRANULARITY: u16 = 8; - const H_BLANK: u16 = 160; - const H_FRONT_PORCH: u16 = 48; - const H_SYNC_WIDTH: u16 = 32; - const V_FRONT_PORCH: u16 = 3; - const MIN_V_BACK_PORCH: u16 = 6; - const RB_MIN_VBLANK_US: f64 = 460.0; - const CLOCK_STEP_KHZ: u32 = 250; - - let h_active = round_down_to(request.width, CELL_GRANULARITY); - let v_active = request.height; - let refresh_hz = request.refresh_hz.max(1.0); - let v_sync_width = vertical_sync_width(h_active, v_active); - let h_front_porch: u16 = H_FRONT_PORCH; - let h_sync_width: u16 = H_SYNC_WIDTH; - let h_blanking: u16 = H_BLANK; - let v_front_porch: u16 = V_FRONT_PORCH; - let h_total = u32::from(h_active) + u32::from(h_blanking); - let estimated_h_period_us = - ((1_000_000.0 / refresh_hz) - RB_MIN_VBLANK_US) / f64::from(v_active); - let min_vbi_lines = V_FRONT_PORCH + v_sync_width + MIN_V_BACK_PORCH; - let rb_vbi_lines = - ((RB_MIN_VBLANK_US / estimated_h_period_us).floor() as u16).saturating_add(1); - let v_blanking = rb_vbi_lines.max(min_vbi_lines); - let v_total = u32::from(v_active) + u32::from(v_blanking); - let pixel_clock_khz = round_down_to_step( - f64::from(h_total) * f64::from(v_total) * refresh_hz / 1000.0, - CLOCK_STEP_KHZ, - ); - - TimingDescriptor { - pixel_clock_khz, - h_active, - h_blanking, - h_front_porch, - h_sync_width, - h_back_porch: h_blanking - h_front_porch - h_sync_width, - v_active, - v_blanking, - v_front_porch, - v_sync_width, - v_back_porch: v_blanking - v_front_porch - v_sync_width, - h_sync_positive: true, - v_sync_positive: false, - interlaced: false, + let mut calc = Calc::new(request, current)?; + let ok = match preset { + TimingPreset::Manual => unreachable!(), + TimingPreset::AutomaticPc => calc.automatic_pc(), + TimingPreset::AutomaticHdtv => calc.automatic_hdtv(), + TimingPreset::AutomaticCrt => calc.automatic_crt(), + TimingPreset::NativePc => calc.native(Calc::automatic_pc, (true, false)), + TimingPreset::NativeHdtv => calc.native(Calc::automatic_hdtv, (true, true)), + TimingPreset::Exact => calc.exact(), + TimingPreset::ExactReduced => calc.exact_reduced(), + TimingPreset::ExactCrt => calc.exact_crt(), + TimingPreset::CvtStandard => calc.standard(Calc::cvt, (false, true)), + TimingPreset::CvtRbStandard => calc.standard(Calc::cvt_rb, (true, false)), + TimingPreset::CvtRb2Standard => calc.standard(Calc::cvt_rb2, (true, false)), + TimingPreset::GtfStandard => calc.standard(Calc::gtf, (false, true)), + TimingPreset::VerticalTotal => { + let current = current.ok_or("vertical total calculator needs an existing timing")?; + calc.vertical_total(current) + } + }; + if !ok { + return Err("preset cannot produce a timing for this size and refresh"); } + calc.descriptor() } -fn hdtv_timing(request: CvtRequest) -> TimingDescriptor { - let h_active = round_down_to(request.width, 8); - let v_active = request.height; - let (h_front_porch, h_sync_width, h_back_porch, v_front_porch, v_sync_width, v_back_porch) = - if h_active >= 1920 || v_active >= 1080 { - (88, 44, 148, 4, 5, 36) - } else if h_active >= 1280 || v_active >= 720 { - (110, 40, 220, 5, 5, 20) - } else { - (16, 62, 60, 9, 6, 30) - }; - detailed_from_parts( - h_active, - v_active, - h_front_porch, - h_sync_width, - h_back_porch, - v_front_porch, - v_sync_width, - v_back_porch, - request.refresh_hz, - true, - true, - false, - ) +/// CVT reduced blanking (v1) as CRU's "CVT-RB standard" preset computes it. +pub fn cvt_reduced_blanking(request: CvtRequest) -> Result { + timing_for_preset(TimingPreset::CvtRbStandard, request, None) } -fn crt_timing(request: CvtRequest) -> TimingDescriptor { - let h_active = round_down_to(request.width, 8); - let v_active = request.height; - let h_blanking = round_down_to((h_active / 4).max(160), 8); - let h_sync_width = round_down_to((h_blanking / 3).max(32), 8); - let h_front_porch = round_down_to((h_blanking / 6).max(16), 8); - let h_back_porch = h_blanking.saturating_sub(h_front_porch + h_sync_width); - let v_front_porch = 3; - let v_sync_width = vertical_sync_width(h_active, v_active); - let v_back_porch = 30; - - detailed_from_parts( - h_active, - v_active, - h_front_porch, - h_sync_width, - h_back_porch, - v_front_porch, - v_sync_width, - v_back_porch, - request.refresh_hz, - false, - false, - false, - ) -} +// CRU sentinels for "no value". +const BLANK: i64 = i32::MIN as i64; +const INVALID: i64 = BLANK + 1; +const DECIMAL_BLANK: i64 = i64::MIN; -fn exact_timing(request: CvtRequest, current: Option<&TimingDescriptor>) -> TimingDescriptor { - let base = current - .cloned() - .unwrap_or_else(|| cvt_reduced_blanking(request)); - let mut timing = TimingDescriptor { - h_active: request.width, - v_active: request.height, - ..base - }; - timing.h_blanking = timing - .h_front_porch - .saturating_add(timing.h_sync_width) - .saturating_add(timing.h_back_porch); - timing.v_blanking = timing - .v_front_porch - .saturating_add(timing.v_sync_width) - .saturating_add(timing.v_back_porch); - timing.pixel_clock_khz = exact_pixel_clock_khz( - u32::from(timing.h_total()), - u32::from(timing.v_total()), - request.refresh_hz / if timing.interlaced { 2.0 } else { 1.0 }, - ); - timing +/// Pixel clock units per MHz for EDID DTDs (10 kHz). +const PCLOCK_PRECISION: i64 = 100; +/// CVT/GTF pixel clock step: 0.25 MHz. +const PCLOCK_MULTIPLE: i64 = PCLOCK_PRECISION / 4; + +// GTF/CVT blanking formula parameters. +const C_PRIME: i64 = (40 - 20) * 128 / 256 + 20; +const M_PRIME: i64 = 600 * 128 / 256; + +// Minimum vertical blanking times in picoseconds. +const MIN_TIME_CVT: i64 = 550_000_000; +const MIN_TIME_CVT_RB: i64 = 460_000_000; +const MIN_TIME_CVT_RB2: i64 = 460_000_000; +const MIN_TIME_GTF: i64 = 550_000_000; + +/// `{min, max, vsync}` against `v_active * 4000 / h_active` (aspect ±2%). +const ASPECT_V_SYNC: [(i64, i64, i64); 5] = [ + (2205, 2295, 5), // 16:9 + (2352, 2448, 7), // 15:9 + (2450, 2550, 6), // 16:10 + (2940, 3060, 4), // 4:3 + (3136, 3264, 7), // 5:4 +]; + +/// Field limits. CRU validates presets against the EDID DTD limits and +/// guards arithmetic with the wider DisplayID limits ("supported"). +struct Limits { + max_h_active: i64, + max_h_front: i64, + max_h_sync: i64, + max_h_blank: i64, + max_v_active: i64, + max_v_front: i64, + max_v_sync: i64, + max_v_blank: i64, + max_pclock: i64, } -#[allow(clippy::too_many_arguments)] -fn detailed_from_parts( - h_active: u16, - v_active: u16, - h_front_porch: u16, - h_sync_width: u16, - h_back_porch: u16, - v_front_porch: u16, - v_sync_width: u16, - v_back_porch: u16, - refresh_hz: f64, - h_sync_positive: bool, - v_sync_positive: bool, +const DTD: Limits = Limits { + max_h_active: 4095, + max_h_front: 1023, + max_h_sync: 1023, + max_h_blank: 4095, + max_v_active: 4095, + max_v_front: 63, + max_v_sync: 63, + max_v_blank: 4095, + max_pclock: 65535, +}; + +const SUPPORTED: Limits = Limits { + max_h_active: 65536, + max_h_front: 32768, + max_h_sync: 65536, + max_h_blank: 65536, + max_v_active: 65536, + max_v_front: 32768, + max_v_sync: 65536, + max_v_blank: 65536, + max_pclock: 16_777_216, +}; + +/// Mirror of CRU's `DetailedResolutionClass` calculation state. +struct Calc { + h_active: i64, + h_front: i64, + h_sync: i64, + h_back: i64, + h_blank: i64, + h_total: i64, + v_active: i64, + v_front: i64, + v_sync: i64, + v_back: i64, + v_blank: i64, + v_total: i64, + /// Requested refresh in mHz. + v_rate: i64, + pclock: i64, + actual_pclock: i64, + actual_v_rate: i64, interlaced: bool, -) -> TimingDescriptor { - let h_blanking = h_front_porch + h_sync_width + h_back_porch; - let v_blanking = v_front_porch + v_sync_width + v_back_porch; - let pixel_clock_khz = exact_pixel_clock_khz( - u32::from(h_active) + u32::from(h_blanking), - u32::from(v_active) + u32::from(v_blanking), - refresh_hz, - ); - - TimingDescriptor { - pixel_clock_khz, - h_active, - h_blanking, - h_front_porch, - h_sync_width, - h_back_porch, - v_active, - v_blanking, - v_front_porch, - v_sync_width, - v_back_porch, - h_sync_positive, - v_sync_positive, - interlaced, - } + h_polarity: bool, + v_polarity: bool, + reset_h_polarity: bool, + reset_v_polarity: bool, + vertical_total_mode: bool, } -fn exact_pixel_clock_khz(h_total: u32, v_total: u32, refresh_hz: f64) -> u32 { - (f64::from(h_total) * f64::from(v_total) * refresh_hz / 1000.0).round() as u32 +fn positive(value: i64) -> bool { + value != BLANK && value != INVALID && value >= 0 } -fn round_down_to(value: u16, granularity: u16) -> u16 { - value / granularity * granularity -} +impl Calc { + fn new(request: CvtRequest, current: Option<&TimingDescriptor>) -> Result { + let refresh_millihz = request.refresh_hz * 1000.0; + if !refresh_millihz.is_finite() || refresh_millihz < 0.0 || refresh_millihz > 1e15 { + return Err("invalid refresh"); + } + let (reset_h_polarity, reset_v_polarity) = current + .map(|timing| (timing.h_sync_positive, timing.v_sync_positive)) + .unwrap_or((true, false)); + Ok(Self { + h_active: i64::from(request.width), + h_front: BLANK, + h_sync: BLANK, + h_back: BLANK, + h_blank: BLANK, + h_total: BLANK, + v_active: i64::from(request.height), + v_front: BLANK, + v_sync: BLANK, + v_back: BLANK, + v_blank: BLANK, + v_total: BLANK, + v_rate: refresh_millihz.round() as i64, + pclock: DECIMAL_BLANK, + actual_pclock: DECIMAL_BLANK, + actual_v_rate: DECIMAL_BLANK, + interlaced: request.interlaced, + h_polarity: reset_h_polarity, + v_polarity: reset_v_polarity, + reset_h_polarity, + reset_v_polarity, + vertical_total_mode: false, + }) + } -fn round_down_to_step(value: f64, step: u32) -> u32 { - ((value / f64::from(step)).floor() * f64::from(step)) as u32 -} + fn descriptor(&self) -> Result { + let field = |value: i64| u16::try_from(value).map_err(|_| "timing exceeds EDID limits"); + let pixel_clock_khz = self + .actual_pclock + .checked_mul(1000 / PCLOCK_PRECISION) + .and_then(|value| u32::try_from(value).ok()) + .ok_or("pixel clock exceeds EDID limits")?; + Ok(TimingDescriptor { + pixel_clock_khz, + h_active: field(self.h_active)?, + h_blanking: field(self.h_blank)?, + h_front_porch: field(self.h_front)?, + h_sync_width: field(self.h_sync)?, + h_back_porch: field(self.h_back)?, + v_active: field(self.v_active)?, + v_blanking: field(self.v_blank)?, + v_front_porch: field(self.v_front)?, + v_sync_width: field(self.v_sync)?, + v_back_porch: field(self.v_back)?, + h_sync_positive: self.h_polarity, + v_sync_positive: self.v_polarity, + interlaced: self.interlaced, + }) + } -fn vertical_sync_width(width: u16, height: u16) -> u16 { - if has_aspect(width, height, 4, 3) { - 4 - } else if has_aspect(width, height, 16, 9) { - 5 - } else if has_aspect(width, height, 16, 10) { - 6 - } else if has_aspect(width, height, 5, 4) || has_aspect(width, height, 15, 9) { - 7 - } else { - 10 + fn fields(&self) -> i64 { + i64::from(self.interlaced) + 1 } -} -fn has_aspect(width: u16, height: u16, aspect_width: u16, aspect_height: u16) -> bool { - u32::from(width) * u32::from(aspect_height) == u32::from(height) * u32::from(aspect_width) -} + fn il(&self) -> i64 { + i64::from(self.interlaced) + } -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn rb_timing_has_expected_totals() { - let timing = cvt_reduced_blanking(CvtRequest { - width: 1920, - height: 1080, - refresh_hz: 60.0, - }); + /// Vertical values as stored in a DTD (per field when interlaced). + fn value(&self, value: i64) -> i64 { + if self.interlaced && positive(value) { + value / 2 + } else { + value + } + } - assert_eq!(timing.h_active, 1920); - assert_eq!(timing.h_blanking, 160); - assert_eq!(timing.v_blanking, 31); - assert_eq!(timing.v_sync_width, 5); - assert_eq!(timing.pixel_clock_khz, 138_500); - } - - #[test] - fn extreme_requests_do_not_overflow_before_validation() { - for preset in TimingPreset::ALL { - for refresh_hz in [60.0, 2173.9, 1e100] { - let timing = timing_for_preset( - preset, - CvtRequest { - width: u16::MAX, - height: u16::MAX, - refresh_hz, - }, - None, - ); - assert!(!crate::validation::validate_timing(&timing).is_empty()); - } + // --- Range checks ------------------------------------------------------- + + fn h_active_in(&self, l: &Limits) -> bool { + (1..=l.max_h_active).contains(&self.h_active) + } + fn h_front_in(&self, l: &Limits) -> bool { + (1..=l.max_h_front).contains(&self.h_front) + } + fn h_sync_in(&self, l: &Limits) -> bool { + (1..=l.max_h_sync).contains(&self.h_sync) + } + // CRU clamps against the DTD validity of the neighbouring fields even + // when checking the wider limits; the minimum is 1 for both. + fn clamp_h_front(&self) -> i64 { + if self.h_front_in(&DTD) { + self.h_front + } else { + 1 + } + } + fn clamp_h_sync(&self) -> i64 { + if self.h_sync_in(&DTD) { self.h_sync } else { 1 } + } + fn h_back_in(&self, l: &Limits) -> bool { + self.h_back >= 0 + && self.h_back <= l.max_h_blank - self.clamp_h_front() - self.clamp_h_sync() + } + fn h_blank_in(&self, l: &Limits) -> bool { + self.h_blank >= self.clamp_h_front() + self.clamp_h_sync() && self.h_blank <= l.max_h_blank + } + fn h_total_in(&self, l: &Limits) -> bool { + let valid_active = self.h_active_in(&DTD); + let min_active = if valid_active { self.h_active } else { 1 }; + let max_active = if valid_active { + self.h_active + } else { + l.max_h_active + }; + self.h_total >= min_active + self.clamp_h_front() + self.clamp_h_sync() + && self.h_total <= max_active + l.max_h_blank + } + fn v_active_in(&self, l: &Limits) -> bool { + (1..=l.max_v_active).contains(&self.value(self.v_active)) + } + fn v_front_in(&self, l: &Limits) -> bool { + (1..=l.max_v_front).contains(&self.value(self.v_front)) + } + fn v_sync_in(&self, l: &Limits) -> bool { + (1..=l.max_v_sync).contains(&self.value(self.v_sync)) + } + fn clamp_v_front(&self) -> i64 { + if self.v_front_in(&DTD) { + self.value(self.v_front) + } else { + 1 + } + } + fn clamp_v_sync(&self) -> i64 { + if self.v_sync_in(&DTD) { + self.value(self.v_sync) + } else { + 1 + } + } + fn v_back_in(&self, l: &Limits) -> bool { + let value = self.value(self.v_back); + value >= 0 && value <= l.max_v_blank - self.clamp_v_front() - self.clamp_v_sync() + } + fn v_blank_in(&self, l: &Limits) -> bool { + let value = self.value(self.v_blank); + value >= self.clamp_v_front() + self.clamp_v_sync() && value <= l.max_v_blank + } + fn v_total_bounds(&self, l: &Limits) -> (i64, i64) { + let valid_active = self.v_active_in(&DTD); + let active = self.value(self.v_active); + let min_active = if valid_active { active } else { 1 }; + let max_active = if valid_active { active } else { l.max_v_active }; + ( + min_active + self.clamp_v_front() + self.clamp_v_sync(), + max_active + l.max_v_blank, + ) + } + fn v_total_in(&self, l: &Limits) -> bool { + let (min, max) = self.v_total_bounds(l); + (min..=max).contains(&self.value(self.v_total)) + } + fn pclock_in(value: i64, l: &Limits) -> bool { + (1..=l.max_pclock).contains(&value) + } + + fn all_in(&self, l: &Limits) -> bool { + self.h_active_in(l) + && self.h_front_in(l) + && self.h_sync_in(l) + && self.h_back_in(l) + && self.h_blank_in(l) + && self.h_total_in(l) + && self.v_active_in(l) + && self.v_front_in(l) + && self.v_sync_in(l) + && self.v_back_in(l) + && self.v_blank_in(l) + && self.v_total_in(l) + && Self::pclock_in(self.actual_pclock, l) + } + + fn is_valid(&self) -> bool { + self.all_in(&DTD) + } + + fn is_supported(&self) -> bool { + self.all_in(&SUPPORTED) + } + + fn v_rate_supported(&self) -> bool { + if self.vertical_total_mode { + self.v_rate > 0 + } else { + self.v_rate >= 0 + } + } + + fn inputs_supported(&self) -> bool { + self.h_active_in(&SUPPORTED) && self.v_active_in(&SUPPORTED) && self.v_rate_supported() + } + + // --- Derived geometry --------------------------------------------------- + + fn calculate_h_blank(&mut self) { + self.h_blank = if self.h_front_in(&SUPPORTED) + && self.h_sync_in(&SUPPORTED) + && self.h_back_in(&SUPPORTED) + { + self.h_front + self.h_sync + self.h_back + } else { + BLANK + }; + if !self.h_blank_in(&SUPPORTED) { + self.h_blank = BLANK; } } - #[test] - fn exact_preset_preserves_current_blanking_and_updates_clock() { - let current = cvt_reduced_blanking(CvtRequest { - width: 1280, - height: 1080, - refresh_hz: 60.0, + fn calculate_h_total(&mut self) { + self.h_total = if self.h_active_in(&SUPPORTED) + && self.h_front_in(&SUPPORTED) + && self.h_sync_in(&SUPPORTED) + && self.h_back_in(&SUPPORTED) + { + self.h_active + self.h_front + self.h_sync + self.h_back + } else { + BLANK + }; + if !self.h_total_in(&SUPPORTED) { + self.h_total = BLANK; + } + } + + fn calculate_h_back_from_h_total(&mut self) { + self.h_back = if self.h_total_in(&SUPPORTED) + && self.h_active_in(&SUPPORTED) + && self.h_front_in(&SUPPORTED) + && self.h_sync_in(&SUPPORTED) + { + self.h_total - self.h_active - self.h_front - self.h_sync + } else { + BLANK + }; + if !self.h_back_in(&SUPPORTED) { + self.h_back = BLANK; + } + } + + fn calculate_v_blank(&mut self) { + self.v_blank = if self.v_front_in(&SUPPORTED) + && self.v_sync_in(&SUPPORTED) + && self.v_back_in(&SUPPORTED) + { + self.v_front + self.v_sync + self.v_back + } else { + BLANK + }; + if !self.v_blank_in(&SUPPORTED) { + self.v_blank = BLANK; + } + } + + fn calculate_v_total(&mut self) { + self.v_total = if self.v_active_in(&SUPPORTED) + && self.v_front_in(&SUPPORTED) + && self.v_sync_in(&SUPPORTED) + && self.v_back_in(&SUPPORTED) + { + self.v_active + self.v_front + self.v_sync + self.v_back + } else { + BLANK + }; + if !self.v_total_in(&SUPPORTED) { + self.v_total = BLANK; + } + } + + fn calculate_v_back_from_v_total(&mut self) { + self.v_back = if self.v_total_in(&SUPPORTED) + && self.v_active_in(&SUPPORTED) + && self.v_front_in(&SUPPORTED) + && self.v_sync_in(&SUPPORTED) + { + self.v_total - self.v_active - self.v_front - self.v_sync + } else { + BLANK + }; + if !self.v_back_in(&SUPPORTED) { + self.v_back = BLANK; + } + } + + fn calculate_totals_from_porches(&mut self) { + self.calculate_h_blank(); + self.calculate_h_total(); + self.calculate_v_blank(); + self.calculate_v_total(); + } + + fn calculate_porches_from_totals(&mut self) { + self.calculate_h_back_from_h_total(); + self.calculate_h_blank(); + self.calculate_v_back_from_v_total(); + self.calculate_v_blank(); + } + + // --- Rates and clocks --------------------------------------------------- + + fn v_rate_divisor(&self) -> i64 { + 1_000_000_000 * self.fields() / PCLOCK_PRECISION + } + + fn v_rate_limit(&self, subtract: i64) -> i64 { + (i64::MAX - subtract) / self.h_total / self.v_total + } + + fn clock_inputs_supported(&self) -> bool { + self.v_rate_supported() && self.h_total_in(&SUPPORTED) && self.v_total_in(&SUPPORTED) + } + + fn finish_pclock(&mut self, value: i64) -> bool { + self.actual_pclock = if Self::pclock_in(value, &SUPPORTED) { + value + } else { + DECIMAL_BLANK + }; + self.actual_pclock != DECIMAL_BLANK + } + + /// Pixel clock rounded up so the refresh is at least the requested one. + fn calculate_pclock_from_v_rate(&mut self) -> bool { + if !self.clock_inputs_supported() { + self.actual_pclock = DECIMAL_BLANK; + return false; + } + if self.v_rate == 0 { + self.actual_pclock = 1; + return true; + } + let divisor = self.v_rate_divisor(); + if self.v_rate > self.v_rate_limit(divisor - 1) { + self.actual_pclock = DECIMAL_BLANK; + return false; + } + self.finish_pclock((self.v_rate * self.h_total * self.v_total + (divisor - 1)) / divisor) + } + + fn calculate_actual_v_rate(&mut self) { + self.actual_v_rate = if Self::pclock_in(self.actual_pclock, &SUPPORTED) + && self.h_total_in(&SUPPORTED) + && self.v_total_in(&SUPPORTED) + { + self.actual_pclock * self.v_rate_divisor() / self.h_total / self.v_total + } else { + DECIMAL_BLANK + }; + } + + fn calculate_pclock_for_cvt(&mut self) -> bool { + if !self.clock_inputs_supported() { + self.actual_pclock = DECIMAL_BLANK; + return false; + } + let h_period = self.h_period(MIN_TIME_CVT, self.v_front_cvt()); + if h_period <= 0 { + self.actual_pclock = DECIMAL_BLANK; + return false; + } + let multiplier = 1_000_000 * PCLOCK_PRECISION; + self.finish_pclock(self.h_total * multiplier / h_period / PCLOCK_MULTIPLE * PCLOCK_MULTIPLE) + } + + fn calculate_pclock_for_rb(&mut self, multiple: i64) -> bool { + if !self.clock_inputs_supported() || self.v_rate > self.v_rate_limit(0) { + self.actual_pclock = DECIMAL_BLANK; + return false; + } + let value = self.v_rate * self.h_total * self.v_total / self.v_rate_divisor(); + self.finish_pclock(value / multiple * multiple) + } + + fn calculate_pclock_for_gtf(&mut self) -> bool { + if !self.clock_inputs_supported() { + self.actual_pclock = DECIMAL_BLANK; + return false; + } + let divisor = self.v_rate_divisor(); + if self.v_rate > self.v_rate_limit(divisor / 2) { + self.actual_pclock = DECIMAL_BLANK; + return false; + } + self.finish_pclock((self.v_rate * self.h_total * self.v_total + divisor / 2) / divisor) + } + + fn finish_rates(&mut self) { + self.calculate_actual_v_rate(); + self.pclock = self.actual_pclock; + } + + // --- CVT / GTF formula pieces -------------------------------------------- + + /// Estimated line period in picoseconds (scaled by 1000 for precision). + fn h_period_with(&self, min_time: i64, lines: i64) -> i64 { + if !self.inputs_supported() { + return 0; + } + if self.v_rate == 0 { + return min_time; + } + let fields = self.fields(); + (1_000_000_000_000_000 * fields / self.v_rate - min_time * fields) / lines + } + + fn h_period(&self, min_time: i64, v_front: i64) -> i64 { + self.h_period_with(min_time, self.v_active + v_front + self.il()) + } + + fn h_period_rb(&self, min_time: i64) -> i64 { + self.h_period_with(min_time, self.v_active) + } + + fn duty_cycle_blank(&self, h_period: i64, clamp: bool, round: i64) -> i64 { + let mut duty = C_PRIME * 1_000_000_000 - M_PRIME * h_period; + if clamp && duty < 20_000_000_000 { + duty = 20_000_000_000; + } + // i128: GTF's unclamped duty cycle can be hugely negative. + let blank = + i128::from(self.h_active) * i128::from(duty) / (100_000_000_000 - i128::from(duty)); + let blank = (blank + i128::from(round)) / 16 * 16; + i64::try_from(blank).unwrap_or(BLANK) + } + + fn h_blank_cvt(&self) -> i64 { + if !self.inputs_supported() { + return BLANK; + } + let h_period = self.h_period(MIN_TIME_CVT, self.v_front_cvt()); + if h_period <= 0 { + return BLANK; + } + self.duty_cycle_blank(h_period, true, 0) + } + + fn h_blank_gtf(&self) -> i64 { + if !self.inputs_supported() { + return BLANK; + } + let h_period = self.h_period(MIN_TIME_GTF, self.v_front_gtf()); + if h_period <= 0 { + return BLANK; + } + self.duty_cycle_blank(h_period, false, 8) + } + + fn v_front_cvt(&self) -> i64 { + 3 * self.fields() + } + + fn v_front_gtf(&self) -> i64 { + self.fields() + } + + fn v_sync_cvt(&self) -> i64 { + let aspect = self.v_active * 4000 / self.h_active; + let lines = ASPECT_V_SYNC + .iter() + .find(|(min, max, _)| (*min..=*max).contains(&aspect)) + .map_or(10, |(_, _, lines)| *lines); + lines * self.fields() + } + + fn v_back_cvt(&self) -> i64 { + let h_period = self.h_period(MIN_TIME_CVT, self.v_front_cvt()); + if h_period <= 0 { + return BLANK; + } + let sync_and_back = MIN_TIME_CVT / h_period + 1; + let v_back = sync_and_back * self.fields() + self.il() - self.v_sync_cvt(); + v_back.max(6 * self.fields() + self.il()) + } + + fn v_back_cvt_rb(&self) -> i64 { + let h_period = self.h_period_rb(MIN_TIME_CVT_RB); + if h_period <= 0 { + return BLANK; + } + let v_blank = MIN_TIME_CVT_RB / h_period + 1; + let v_back = v_blank * self.fields() + self.il() - self.v_front_cvt() - self.v_sync_cvt(); + v_back.max(6 * self.fields() + self.il()) + } + + fn v_back_cvt_rb2(&self) -> i64 { + 6 * self.fields() + self.il() + } + + fn v_front_cvt_rb2(&self) -> i64 { + let h_period = self.h_period_rb(MIN_TIME_CVT_RB2); + if h_period <= 0 { + return BLANK; + } + let v_blank = MIN_TIME_CVT_RB2 / h_period + 1; + let v_front = + v_blank * self.fields() + self.il() - 8 * self.fields() - self.v_back_cvt_rb2(); + v_front.max(self.fields()) + } + + fn v_back_gtf(&self) -> i64 { + let h_period = self.h_period(MIN_TIME_GTF, self.v_front_gtf()); + if h_period <= 0 { + return BLANK; + } + let sync_and_back = (MIN_TIME_GTF * 2 / h_period + 1) / 2; + sync_and_back * self.fields() + self.il() - 3 * self.fields() + } + + // --- Formula timings ---------------------------------------------------- + + fn cvt(&mut self) -> bool { + (self.h_polarity, self.v_polarity) = (false, true); + if !self.inputs_supported() { + return false; + } + let h_blank = self.h_blank_cvt(); + let h_sync = (self.h_active + h_blank) / 100 * 8; + let h_back = h_blank / 2; + self.h_front = h_back - h_sync; + self.h_sync = h_sync; + self.h_back = h_back; + self.v_front = self.v_front_cvt(); + self.v_sync = self.v_sync_cvt(); + self.v_back = self.v_back_cvt(); + self.calculate_totals_from_porches(); + self.calculate_pclock_for_cvt(); + self.finish_rates(); + self.is_supported() + } + + fn cvt_rb(&mut self) -> bool { + (self.h_polarity, self.v_polarity) = (true, false); + if !self.inputs_supported() { + return false; + } + (self.h_front, self.h_sync, self.h_back) = (48, 32, 80); + self.v_front = self.v_front_cvt(); + self.v_sync = self.v_sync_cvt(); + self.v_back = self.v_back_cvt_rb(); + self.calculate_totals_from_porches(); + self.calculate_pclock_for_rb(PCLOCK_MULTIPLE); + self.finish_rates(); + self.is_supported() + } + + fn cvt_rb2(&mut self) -> bool { + (self.h_polarity, self.v_polarity) = (true, false); + if !self.inputs_supported() { + return false; + } + (self.h_front, self.h_sync, self.h_back) = (8, 32, 40); + self.v_front = self.v_front_cvt_rb2(); + self.v_sync = 8 * self.fields(); + self.v_back = self.v_back_cvt_rb2(); + self.calculate_totals_from_porches(); + self.calculate_pclock_for_rb(1); + self.finish_rates(); + self.is_supported() + } + + fn gtf(&mut self) -> bool { + (self.h_polarity, self.v_polarity) = (false, true); + if !self.inputs_supported() { + return false; + } + let h_blank = self.h_blank_gtf(); + let h_sync = (self.h_active + h_blank + 50) / 100 * 8; + let h_back = h_blank / 2; + self.h_front = h_back - h_sync; + self.h_sync = h_sync; + self.h_back = h_back; + self.v_front = self.v_front_gtf(); + self.v_sync = 3 * self.fields(); + self.v_back = self.v_back_gtf(); + self.calculate_totals_from_porches(); + self.calculate_pclock_for_gtf(); + self.finish_rates(); + self.is_supported() + } + + /// CRU's "* standard" presets: run the formula, then rerun it at the + /// refresh the first pass actually achieved and derive the clock from it. + fn standard(&mut self, formula: fn(&mut Self) -> bool, polarity: (bool, bool)) -> bool { + (self.h_polarity, self.v_polarity) = polarity; + if !self.inputs_supported() { + return false; + } + let old_v_rate = self.v_rate; + formula(self); + self.calculate_pclock_from_v_rate(); + self.calculate_actual_v_rate(); + self.v_rate = self.actual_v_rate; + formula(self); + self.calculate_pclock_from_v_rate(); + self.calculate_actual_v_rate(); + self.v_rate = old_v_rate; + self.pclock = self.actual_pclock; + self.is_supported() + } + + // --- Table-driven presets ----------------------------------------------- + + fn automatic( + &mut self, + table: &[TableTiming], + polarity: (bool, bool), + fallback: fn(&mut Self) -> bool, + ) -> bool { + (self.h_polarity, self.v_polarity) = polarity; + if !self.inputs_supported() { + return false; + } + let entry = table.iter().find(|entry| { + entry.h_active == self.h_active + && entry.v_active == self.v_active + && entry.interlaced == self.interlaced + && (entry.min_v_rate..=entry.max_v_rate).contains(&self.v_rate) }); - let timing = timing_for_preset( - TimingPreset::Exact, - CvtRequest { - width: 1280, - height: 1080, - refresh_hz: 100.0, - }, - Some(¤t), - ); - - assert_eq!(timing.h_front_porch, current.h_front_porch); - assert_eq!(timing.h_back_porch, current.h_back_porch); - assert!((timing.refresh_hz().unwrap() - 100.0).abs() < 0.001); - } - - #[test] - fn exact_reduced_uses_reduced_blanking_shape() { - let timing = timing_for_preset( - TimingPreset::ExactReduced, - CvtRequest { - width: 1920, - height: 1080, - refresh_hz: 120.0, - }, - None, - ); - - assert_eq!(timing.h_blanking, 160); - assert_eq!(timing.h_front_porch, 48); - assert!((timing.refresh_hz().unwrap() - 120.0).abs() < 0.001); + if let Some(entry) = entry { + [self.h_front, self.h_sync, self.h_back] = entry.h; + [self.v_front, self.v_sync, self.v_back] = entry.v; + (self.h_polarity, self.v_polarity) = (entry.h_polarity, entry.v_polarity); + self.calculate_totals_from_porches(); + self.calculate_pclock_from_v_rate(); + self.finish_rates(); + // Keep the table timing unless only its porches break DTD limits. + if self.is_valid() + || !self.h_active_in(&DTD) + || !self.v_active_in(&DTD) + || !Self::pclock_in(self.actual_pclock, &DTD) + { + return self.is_supported(); + } + } + fallback(self) + } + + fn automatic_pc(&mut self) -> bool { + self.automatic(AUTOMATIC_PC, (true, false), |calc| { + calc.standard(Self::cvt_rb, (true, false)) + }) + } + + fn automatic_hdtv(&mut self) -> bool { + self.automatic(AUTOMATIC_HDTV, (true, true), |calc| { + calc.standard(Self::cvt_rb, (true, false)) + }) + } + + fn automatic_crt(&mut self) -> bool { + self.automatic(AUTOMATIC_CRT, (false, true), |calc| { + calc.standard(Self::cvt, (false, true)) + }) + } + + /// Porches from the automatic preset at the panel's native refresh, + /// with the clock rescaled to the requested refresh. + fn native(&mut self, automatic: fn(&mut Self) -> bool, polarity: (bool, bool)) -> bool { + (self.h_polarity, self.v_polarity) = polarity; + if !self.inputs_supported() { + return false; + } + let native_rate = NATIVE + .iter() + .find(|(h, v, interlaced, _)| { + *h == self.h_active && *v == self.v_active && *interlaced == self.interlaced + }) + .map_or(60_000, |(_, _, _, rate)| *rate); + let old_v_rate = self.v_rate; + self.v_rate = native_rate; + automatic(self); + self.v_rate = old_v_rate; + self.calculate_pclock_from_v_rate(); + self.finish_rates(); + self.is_supported() + } + + // --- Exact presets ------------------------------------------------------ + + /// Totals rounded so the requested refresh is hit exactly. + fn round_v_total(&self, step: i64) -> i64 { + let lines = self.v_active + self.v_front + self.v_sync; + if self.interlaced { + (lines + step) / (step * 2) * (step * 2) + step + } else { + (lines + step) / step * step + } + } + + fn exact_h_total(&self) -> i64 { + let step = if self.interlaced { 160 } else { 80 }; + (self.h_active + self.h_front + self.h_sync + step) / step * step + } + + fn exact(&mut self) -> bool { + (self.h_polarity, self.v_polarity) = (self.reset_h_polarity, self.reset_v_polarity); + if !self.inputs_supported() { + return false; + } + (self.h_front, self.h_sync) = (48, 32); + self.h_total = self.exact_h_total(); + self.v_front = self.v_front_cvt(); + self.v_sync = self.v_sync_cvt(); + self.v_total = self.round_v_total(125); + self.calculate_porches_from_totals(); + self.calculate_pclock_from_v_rate(); + self.finish_rates(); + self.is_supported() + } + + fn exact_reduced_v_total(&self) -> i64 { + if self.h_total % 400 == 0 || self.v_rate % 5000 == 0 { + self.round_v_total(25) + } else { + self.round_v_total(125) + } + } + + fn exact_reduced(&mut self) -> bool { + (self.h_polarity, self.v_polarity) = (self.reset_h_polarity, self.reset_v_polarity); + if !self.inputs_supported() { + return false; + } + (self.h_front, self.h_sync) = (48, 32); + self.h_total = self.exact_h_total(); + self.v_front = self.v_front_cvt(); + self.v_sync = self.v_sync_cvt(); + self.v_total = self.exact_reduced_v_total(); + self.calculate_porches_from_totals(); + self.calculate_pclock_from_v_rate(); + + // Narrower front porch for single-link DVI sized modes above 165 MHz. + if self.h_active * self.v_active <= 2048 * 1200 + && self.actual_pclock > 165 * PCLOCK_PRECISION + && !self.interlaced + { + (self.h_front, self.h_sync) = (24, 32); + self.h_total = self.exact_h_total(); + self.v_front = self.v_front_cvt(); + self.v_sync = self.v_sync_cvt(); + self.v_total = self.exact_reduced_v_total(); + self.calculate_porches_from_totals(); + self.calculate_pclock_from_v_rate(); + } + + self.finish_rates(); + self.is_supported() + } + + fn exact_crt(&mut self) -> bool { + (self.h_polarity, self.v_polarity) = (self.reset_h_polarity, self.reset_v_polarity); + if !self.inputs_supported() { + return false; + } + self.standard(Self::cvt, (false, true)); + if self.interlaced { + self.h_total = (self.h_total + 159) / 160 * 160; + self.v_total = (self.v_total + 124) / 250 * 250 + 125; + } else { + self.h_total = (self.h_total + 79) / 80 * 80; + self.v_total = (self.v_total + 124) / 125 * 125; + } + self.calculate_porches_from_totals(); + self.calculate_pclock_from_v_rate(); + self.finish_rates(); + self.is_supported() + } + + // --- Vertical total calculator ------------------------------------------ + + /// Keeps the horizontal timing and pixel clock and stretches the vertical + /// back porch until the requested refresh is reached (quick frame + /// transport). + fn vertical_total(&mut self, current: &TimingDescriptor) -> bool { + self.vertical_total_mode = true; + self.h_front = i64::from(current.h_front_porch); + self.h_sync = i64::from(current.h_sync_width); + self.h_back = i64::from(current.h_back_porch); + self.v_front = i64::from(current.v_front_porch); + self.v_sync = i64::from(current.v_sync_width); + self.pclock = i64::from(current.pixel_clock_khz) * PCLOCK_PRECISION / 1000; + self.calculate_h_blank(); + self.calculate_h_total(); + + self.v_total = BLANK; + if Self::pclock_in(self.pclock, &SUPPORTED) + && self.h_total_in(&SUPPORTED) + && self.v_rate_supported() + { + let mut value = self.pclock * self.v_rate_divisor() / self.h_total / self.v_rate; + if self.interlaced && value % 2 == 0 { + value -= 1; + } + let (min, max) = self.v_total_bounds(&SUPPORTED); + if (min..=max).contains(&self.value(value)) { + self.v_total = value; + } + } + self.calculate_v_back_from_v_total(); + self.calculate_v_blank(); + self.calculate_pclock_from_v_rate(); + self.finish_rates(); + self.is_supported() } } + +#[cfg(test)] +mod tests; diff --git a/src/timings/tables.rs b/src/timings/tables.rs new file mode 100644 index 0000000..c2c132f --- /dev/null +++ b/src/timings/tables.rs @@ -0,0 +1,1693 @@ +//! Timing tables ported verbatim from CRU's `DetailedResolutionClass.cpp`. +//! +//! Each row is (active size, interlaced, refresh window in mHz, horizontal +//! front/sync/back porch, vertical front/sync/back porch in frame lines, +//! sync polarities). + +pub(super) struct TableTiming { + pub h_active: i64, + pub v_active: i64, + pub interlaced: bool, + pub min_v_rate: i64, + pub max_v_rate: i64, + pub h: [i64; 3], + pub v: [i64; 3], + pub h_polarity: bool, + pub v_polarity: bool, +} + +#[allow(clippy::too_many_arguments)] +const fn t( + h_active: i64, + v_active: i64, + interlaced: bool, + min_v_rate: i64, + max_v_rate: i64, + h: [i64; 3], + v: [i64; 3], + h_polarity: bool, + v_polarity: bool, +) -> TableTiming { + TableTiming { + h_active, + v_active, + interlaced, + min_v_rate, + max_v_rate, + h, + v, + h_polarity, + v_polarity, + } +} + +pub(super) const AUTOMATIC_PC: &[TableTiming] = &[ + t( + 1920, + 1080, + false, + 59500, + 60500, + [88, 44, 148], + [4, 5, 36], + true, + true, + ), // 1920x1080 @ 60 Hz + t( + 1920, + 1080, + false, + 49500, + 50500, + [528, 44, 148], + [4, 5, 36], + true, + true, + ), // 1920x1080 @ 50 Hz + t( + 1920, + 1080, + false, + 47500, + 48500, + [638, 44, 148], + [4, 5, 36], + true, + true, + ), // 1920x1080 @ 48 Hz + t( + 1920, + 1080, + false, + 29500, + 30500, + [88, 44, 148], + [4, 5, 36], + true, + true, + ), // 1920x1080 @ 30 Hz + t( + 1920, + 1080, + false, + 24500, + 25500, + [528, 44, 148], + [4, 5, 36], + true, + true, + ), // 1920x1080 @ 25 Hz + t( + 1920, + 1080, + false, + 23500, + 24500, + [638, 44, 148], + [4, 5, 36], + true, + true, + ), // 1920x1080 @ 24 Hz + t( + 1920, + 1080, + true, + 59500, + 60500, + [88, 44, 148], + [4, 10, 31], + true, + true, + ), // 1920x1080i @ 60 Hz + t( + 1920, + 1080, + true, + 49500, + 50500, + [528, 44, 148], + [4, 10, 31], + true, + true, + ), // 1920x1080i @ 50 Hz + t( + 1600, + 900, + false, + 59500, + 60500, + [24, 80, 96], + [1, 3, 96], + true, + true, + ), // 1600x900 @ 60 Hz + t( + 1366, + 768, + false, + 59500, + 60500, + [70, 143, 213], + [3, 3, 24], + true, + true, + ), // 1366x768 @ 60 Hz + t( + 1360, + 768, + false, + 59500, + 60500, + [64, 112, 256], + [3, 6, 18], + true, + true, + ), // 1360x768 @ 60 Hz + t( + 1280, + 720, + false, + 59500, + 60500, + [110, 40, 220], + [5, 5, 20], + true, + true, + ), // 1280x720 @ 60 Hz + t( + 1280, + 720, + false, + 49500, + 50500, + [440, 40, 220], + [5, 5, 20], + true, + true, + ), // 1280x720 @ 50 Hz + t( + 1280, + 720, + false, + 47500, + 48500, + [960, 40, 220], + [5, 5, 20], + true, + true, + ), // 1280x720 @ 48 Hz + t( + 1280, + 720, + false, + 29500, + 30500, + [1760, 40, 220], + [5, 5, 20], + true, + true, + ), // 1280x720 @ 30 Hz + t( + 1280, + 720, + false, + 24500, + 25500, + [2420, 40, 220], + [5, 5, 20], + true, + true, + ), // 1280x720 @ 25 Hz + t( + 1280, + 720, + false, + 23500, + 24500, + [1760, 40, 220], + [5, 5, 20], + true, + true, + ), // 1280x720 @ 24 Hz + t( + 1440, + 576, + true, + 49500, + 50500, + [24, 126, 138], + [4, 6, 39], + false, + false, + ), // 1440x576i @ 50 Hz + t( + 1440, + 480, + true, + 59500, + 60500, + [38, 124, 114], + [8, 6, 31], + false, + false, + ), // 1440x480i @ 60 Hz + t( + 720, + 576, + false, + 49500, + 50500, + [12, 64, 68], + [5, 5, 39], + false, + false, + ), // 720x576 @ 50 Hz + t( + 720, + 480, + false, + 59500, + 60500, + [16, 62, 60], + [9, 6, 30], + false, + false, + ), // 720x480 @ 60 Hz + t( + 640, + 480, + false, + 59500, + 60500, + [16, 96, 48], + [10, 2, 33], + false, + false, + ), // 640x480 @ 60 Hz +]; + +pub(super) const AUTOMATIC_HDTV: &[TableTiming] = &[ + t( + 10240, + 4320, + false, + 119500, + 120500, + [288, 176, 296], + [16, 20, 144], + true, + true, + ), // 10240x4320 @ 120 Hz + t( + 10240, + 4320, + false, + 99500, + 100500, + [2192, 176, 592], + [16, 20, 144], + true, + true, + ), // 10240x4320 @ 100 Hz + t( + 10240, + 4320, + false, + 59500, + 60500, + [288, 176, 296], + [16, 20, 144], + true, + true, + ), // 10240x4320 @ 60 Hz + t( + 10240, + 4320, + false, + 49500, + 50500, + [2492, 176, 592], + [16, 20, 44], + true, + true, + ), // 10240x4320 @ 50 Hz + t( + 10240, + 4320, + false, + 47500, + 48500, + [1492, 176, 592], + [16, 20, 594], + true, + true, + ), // 10240x4320 @ 48 Hz + t( + 10240, + 4320, + false, + 29500, + 30500, + [288, 176, 296], + [16, 20, 144], + true, + true, + ), // 10240x4320 @ 30 Hz + t( + 10240, + 4320, + false, + 24500, + 25500, + [2492, 176, 592], + [16, 20, 44], + true, + true, + ), // 10240x4320 @ 25 Hz + t( + 10240, + 4320, + false, + 23500, + 24500, + [1492, 176, 592], + [16, 20, 594], + true, + true, + ), // 10240x4320 @ 24 Hz + t( + 7680, + 4320, + false, + 119500, + 120500, + [352, 176, 592], + [16, 20, 144], + true, + true, + ), // 7680x4320 @ 120 Hz + t( + 7680, + 4320, + false, + 99500, + 100500, + [2112, 176, 592], + [16, 20, 144], + true, + true, + ), // 7680x4320 @ 100 Hz + t( + 7680, + 4320, + false, + 59500, + 60500, + [552, 176, 592], + [16, 20, 44], + true, + true, + ), // 7680x4320 @ 60 Hz + t( + 7680, + 4320, + false, + 49500, + 50500, + [2352, 176, 592], + [16, 20, 44], + true, + true, + ), // 7680x4320 @ 50 Hz + t( + 7680, + 4320, + false, + 47500, + 48500, + [2552, 176, 592], + [16, 20, 144], + true, + true, + ), // 7680x4320 @ 48 Hz + t( + 7680, + 4320, + false, + 29500, + 30500, + [552, 176, 592], + [16, 20, 44], + true, + true, + ), // 7680x4320 @ 30 Hz + t( + 7680, + 4320, + false, + 24500, + 25500, + [2352, 176, 592], + [16, 20, 44], + true, + true, + ), // 7680x4320 @ 25 Hz + t( + 7680, + 4320, + false, + 23500, + 24500, + [2552, 176, 592], + [16, 20, 144], + true, + true, + ), // 7680x4320 @ 24 Hz + t( + 5120, + 2160, + false, + 119500, + 120500, + [164, 88, 128], + [8, 10, 72], + true, + true, + ), // 5120x2160 @ 120 Hz + t( + 5120, + 2160, + false, + 99500, + 100500, + [1096, 88, 296], + [8, 10, 72], + true, + true, + ), // 5120x2160 @ 100 Hz + t( + 5120, + 2160, + false, + 59500, + 60500, + [164, 88, 128], + [8, 10, 72], + true, + true, + ), // 5120x2160 @ 60 Hz + t( + 5120, + 2160, + false, + 49500, + 50500, + [1096, 88, 296], + [8, 10, 72], + true, + true, + ), // 5120x2160 @ 50 Hz + t( + 5120, + 2160, + false, + 47500, + 48500, + [746, 88, 296], + [8, 10, 297], + true, + true, + ), // 5120x2160 @ 48 Hz + t( + 5120, + 2160, + false, + 29500, + 30500, + [664, 88, 128], + [8, 10, 22], + true, + true, + ), // 5120x2160 @ 30 Hz + t( + 5120, + 2160, + false, + 24500, + 25500, + [1696, 88, 296], + [8, 10, 22], + true, + true, + ), // 5120x2160 @ 25 Hz + t( + 5120, + 2160, + false, + 23500, + 24500, + [1996, 88, 296], + [8, 10, 22], + true, + true, + ), // 5120x2160 @ 24 Hz + t( + 4096, + 2160, + false, + 119500, + 120500, + [88, 88, 128], + [8, 10, 72], + true, + true, + ), // 4096x2160 @ 120 Hz + t( + 4096, + 2160, + false, + 99500, + 100500, + [800, 88, 296], + [8, 10, 72], + true, + true, + ), // 4096x2160 @ 100 Hz + t( + 4096, + 2160, + false, + 59500, + 60500, + [88, 88, 128], + [8, 10, 72], + true, + true, + ), // 4096x2160 @ 60 Hz + t( + 4096, + 2160, + false, + 49500, + 50500, + [968, 88, 128], + [8, 10, 72], + true, + true, + ), // 4096x2160 @ 50 Hz + t( + 4096, + 2160, + false, + 47500, + 48500, + [1020, 88, 296], + [8, 10, 72], + true, + true, + ), // 4096x2160 @ 48 Hz + t( + 4096, + 2160, + false, + 29500, + 30500, + [88, 88, 128], + [8, 10, 72], + true, + true, + ), // 4096x2160 @ 30 Hz + t( + 4096, + 2160, + false, + 24500, + 25500, + [968, 88, 128], + [8, 10, 72], + true, + true, + ), // 4096x2160 @ 25 Hz + t( + 4096, + 2160, + false, + 23500, + 24500, + [1020, 88, 296], + [8, 10, 72], + true, + true, + ), // 4096x2160 @ 24 Hz + t( + 3840, + 2160, + false, + 119500, + 120500, + [176, 88, 296], + [8, 10, 72], + true, + true, + ), // 3840x2160 @ 120 Hz + t( + 3840, + 2160, + false, + 99500, + 100500, + [1056, 88, 296], + [8, 10, 72], + true, + true, + ), // 3840x2160 @ 100 Hz + t( + 3840, + 2160, + false, + 59500, + 60500, + [176, 88, 296], + [8, 10, 72], + true, + true, + ), // 3840x2160 @ 60 Hz + t( + 3840, + 2160, + false, + 49500, + 50500, + [1056, 88, 296], + [8, 10, 72], + true, + true, + ), // 3840x2160 @ 50 Hz + t( + 3840, + 2160, + false, + 47500, + 48500, + [1276, 88, 296], + [8, 10, 72], + true, + true, + ), // 3840x2160 @ 48 Hz + t( + 3840, + 2160, + false, + 29500, + 30500, + [176, 88, 296], + [8, 10, 72], + true, + true, + ), // 3840x2160 @ 30 Hz + t( + 3840, + 2160, + false, + 24500, + 25500, + [1056, 88, 296], + [8, 10, 72], + true, + true, + ), // 3840x2160 @ 25 Hz + t( + 3840, + 2160, + false, + 23500, + 24500, + [1276, 88, 296], + [8, 10, 72], + true, + true, + ), // 3840x2160 @ 24 Hz + t( + 2560, + 1080, + false, + 119500, + 120500, + [548, 44, 148], + [4, 5, 161], + true, + true, + ), // 2560x1080 @ 120 Hz + t( + 2560, + 1080, + false, + 99500, + 100500, + [218, 44, 148], + [4, 5, 161], + true, + true, + ), // 2560x1080 @ 100 Hz + t( + 2560, + 1080, + false, + 59500, + 60500, + [248, 44, 148], + [4, 5, 11], + true, + true, + ), // 2560x1080 @ 60 Hz + t( + 2560, + 1080, + false, + 49500, + 50500, + [548, 44, 148], + [4, 5, 36], + true, + true, + ), // 2560x1080 @ 50 Hz + t( + 2560, + 1080, + false, + 47500, + 48500, + [998, 44, 148], + [4, 5, 11], + true, + true, + ), // 2560x1080 @ 48 Hz + t( + 2560, + 1080, + false, + 29500, + 30500, + [768, 44, 148], + [4, 5, 36], + true, + true, + ), // 2560x1080 @ 30 Hz + t( + 2560, + 1080, + false, + 24500, + 25500, + [448, 44, 148], + [4, 5, 36], + true, + true, + ), // 2560x1080 @ 25 Hz + t( + 2560, + 1080, + false, + 23500, + 24500, + [998, 44, 148], + [4, 5, 11], + true, + true, + ), // 2560x1080 @ 24 Hz + t( + 1920, + 1080, + false, + 119500, + 120500, + [88, 44, 148], + [4, 5, 36], + true, + true, + ), // 1920x1080 @ 120 Hz + t( + 1920, + 1080, + false, + 99500, + 100500, + [528, 44, 148], + [4, 5, 36], + true, + true, + ), // 1920x1080 @ 100 Hz + t( + 1920, + 1080, + false, + 59500, + 60500, + [88, 44, 148], + [4, 5, 36], + true, + true, + ), // 1920x1080 @ 60 Hz + t( + 1920, + 1080, + false, + 49500, + 50500, + [528, 44, 148], + [4, 5, 36], + true, + true, + ), // 1920x1080 @ 50 Hz + t( + 1920, + 1080, + false, + 47500, + 48500, + [638, 44, 148], + [4, 5, 36], + true, + true, + ), // 1920x1080 @ 48 Hz + t( + 1920, + 1080, + false, + 29500, + 30500, + [88, 44, 148], + [4, 5, 36], + true, + true, + ), // 1920x1080 @ 30 Hz + t( + 1920, + 1080, + false, + 24500, + 25500, + [528, 44, 148], + [4, 5, 36], + true, + true, + ), // 1920x1080 @ 25 Hz + t( + 1920, + 1080, + false, + 23500, + 24500, + [638, 44, 148], + [4, 5, 36], + true, + true, + ), // 1920x1080 @ 24 Hz + t( + 1920, + 1080, + true, + 119500, + 120500, + [88, 44, 148], + [4, 10, 31], + true, + true, + ), // 1920x1080i @ 120 Hz + t( + 1920, + 1080, + true, + 99500, + 100500, + [528, 44, 148], + [4, 10, 31], + true, + true, + ), // 1920x1080i @ 100 Hz + t( + 1920, + 1080, + true, + 59500, + 60500, + [88, 44, 148], + [4, 10, 31], + true, + true, + ), // 1920x1080i @ 60 Hz + t( + 1920, + 1080, + true, + 49500, + 50500, + [528, 44, 148], + [4, 10, 31], + true, + true, + ), // 1920x1080i @ 50 Hz + t( + 1680, + 720, + false, + 119500, + 120500, + [60, 40, 220], + [5, 5, 95], + true, + true, + ), // 1680x720 @ 120 Hz + t( + 1680, + 720, + false, + 99500, + 100500, + [60, 40, 220], + [5, 5, 95], + true, + true, + ), // 1680x720 @ 100 Hz + t( + 1680, + 720, + false, + 59500, + 60500, + [260, 40, 220], + [5, 5, 20], + true, + true, + ), // 1680x720 @ 60 Hz + t( + 1680, + 720, + false, + 49500, + 50500, + [260, 40, 220], + [5, 5, 20], + true, + true, + ), // 1680x720 @ 50 Hz + t( + 1680, + 720, + false, + 47500, + 48500, + [810, 40, 220], + [5, 5, 20], + true, + true, + ), // 1680x720 @ 48 Hz + t( + 1680, + 720, + false, + 29500, + 30500, + [700, 40, 220], + [5, 5, 20], + true, + true, + ), // 1680x720 @ 30 Hz + t( + 1680, + 720, + false, + 24500, + 25500, + [1228, 40, 220], + [5, 5, 20], + true, + true, + ), // 1680x720 @ 25 Hz + t( + 1680, + 720, + false, + 23500, + 24500, + [1360, 40, 220], + [5, 5, 20], + true, + true, + ), // 1680x720 @ 24 Hz + t( + 1600, + 900, + false, + 59500, + 60500, + [24, 80, 96], + [1, 3, 96], + true, + true, + ), // 1600x900 @ 60 Hz + t( + 1366, + 768, + false, + 59500, + 60500, + [70, 143, 213], + [3, 3, 24], + true, + true, + ), // 1366x768 @ 60 Hz + t( + 1360, + 768, + false, + 59500, + 60500, + [64, 112, 256], + [3, 6, 18], + true, + true, + ), // 1360x768 @ 60 Hz + t( + 1280, + 720, + false, + 119500, + 120500, + [110, 40, 220], + [5, 5, 20], + true, + true, + ), // 1280x720 @ 120 Hz + t( + 1280, + 720, + false, + 99500, + 100500, + [440, 40, 220], + [5, 5, 20], + true, + true, + ), // 1280x720 @ 100 Hz + t( + 1280, + 720, + false, + 59500, + 60500, + [110, 40, 220], + [5, 5, 20], + true, + true, + ), // 1280x720 @ 60 Hz + t( + 1280, + 720, + false, + 49500, + 50500, + [440, 40, 220], + [5, 5, 20], + true, + true, + ), // 1280x720 @ 50 Hz + t( + 1280, + 720, + false, + 47500, + 48500, + [960, 40, 220], + [5, 5, 20], + true, + true, + ), // 1280x720 @ 48 Hz + t( + 1280, + 720, + false, + 29500, + 30500, + [1760, 40, 220], + [5, 5, 20], + true, + true, + ), // 1280x720 @ 30 Hz + t( + 1280, + 720, + false, + 24500, + 25500, + [2420, 40, 220], + [5, 5, 20], + true, + true, + ), // 1280x720 @ 25 Hz + t( + 1280, + 720, + false, + 23500, + 24500, + [1760, 40, 220], + [5, 5, 20], + true, + true, + ), // 1280x720 @ 24 Hz + t( + 1440, + 576, + true, + 199500, + 200500, + [24, 126, 138], + [4, 6, 39], + false, + false, + ), // 1440x576i @ 200 Hz + t( + 1440, + 576, + true, + 99500, + 100500, + [24, 126, 138], + [4, 6, 39], + false, + false, + ), // 1440x576i @ 100 Hz + t( + 1440, + 576, + true, + 49500, + 50500, + [24, 126, 138], + [4, 6, 39], + false, + false, + ), // 1440x576i @ 50 Hz + t( + 1440, + 480, + true, + 239500, + 240500, + [38, 124, 114], + [8, 6, 31], + false, + false, + ), // 1440x480i @ 240 Hz + t( + 1440, + 480, + true, + 119500, + 120500, + [38, 124, 114], + [8, 6, 31], + false, + false, + ), // 1440x480i @ 120 Hz + t( + 1440, + 480, + true, + 59500, + 60500, + [38, 124, 114], + [8, 6, 31], + false, + false, + ), // 1440x480i @ 60 Hz + t( + 720, + 576, + false, + 199500, + 200500, + [12, 64, 68], + [5, 5, 39], + false, + false, + ), // 720x576 @ 200 Hz + t( + 720, + 576, + false, + 99500, + 100500, + [12, 64, 68], + [5, 5, 39], + false, + false, + ), // 720x576 @ 100 Hz + t( + 720, + 576, + false, + 49500, + 50500, + [12, 64, 68], + [5, 5, 39], + false, + false, + ), // 720x576 @ 50 Hz + t( + 720, + 480, + false, + 239500, + 240500, + [16, 62, 60], + [9, 6, 30], + false, + false, + ), // 720x480 @ 240 Hz + t( + 720, + 480, + false, + 119500, + 120500, + [16, 62, 60], + [9, 6, 30], + false, + false, + ), // 720x480 @ 120 Hz + t( + 720, + 480, + false, + 59500, + 60500, + [16, 62, 60], + [9, 6, 30], + false, + false, + ), // 720x480 @ 60 Hz + t( + 640, + 480, + false, + 59500, + 60500, + [16, 96, 48], + [10, 2, 33], + false, + false, + ), // 640x480 @ 60 Hz +]; + +pub(super) const AUTOMATIC_CRT: &[TableTiming] = &[ + t( + 1920, + 1440, + false, + 74500, + 75500, + [144, 224, 352], + [1, 3, 56], + false, + true, + ), // 1920x1440 @ 75 Hz + t( + 1920, + 1440, + false, + 59500, + 60500, + [128, 208, 344], + [1, 3, 56], + false, + true, + ), // 1920x1440 @ 60 Hz + t( + 1856, + 1392, + false, + 74500, + 75500, + [128, 224, 352], + [1, 3, 104], + false, + true, + ), // 1856x1392 @ 75 Hz + t( + 1856, + 1392, + false, + 59500, + 60500, + [96, 224, 352], + [1, 3, 43], + false, + true, + ), // 1856x1392 @ 60 Hz + t( + 1792, + 1344, + false, + 74500, + 75500, + [96, 216, 352], + [1, 3, 69], + false, + true, + ), // 1792x1344 @ 75 Hz + t( + 1792, + 1344, + false, + 59500, + 60500, + [128, 200, 328], + [1, 3, 46], + false, + true, + ), // 1792x1344 @ 60 Hz + t( + 1600, + 1200, + false, + 84500, + 85500, + [64, 192, 304], + [1, 3, 46], + true, + true, + ), // 1600x1200 @ 85 Hz + t( + 1600, + 1200, + false, + 74500, + 75500, + [64, 192, 304], + [1, 3, 46], + true, + true, + ), // 1600x1200 @ 75 Hz + t( + 1600, + 1200, + false, + 69500, + 70500, + [64, 192, 304], + [1, 3, 46], + true, + true, + ), // 1600x1200 @ 70 Hz + t( + 1600, + 1200, + false, + 64500, + 65500, + [64, 192, 304], + [1, 3, 46], + true, + true, + ), // 1600x1200 @ 65 Hz + t( + 1600, + 1200, + false, + 59500, + 60500, + [64, 192, 304], + [1, 3, 46], + true, + true, + ), // 1600x1200 @ 60 Hz + t( + 1280, + 1024, + false, + 84500, + 85500, + [64, 160, 224], + [1, 3, 44], + true, + true, + ), // 1280x1024 @ 85 Hz + t( + 1280, + 1024, + false, + 74500, + 75500, + [16, 144, 248], + [1, 3, 38], + true, + true, + ), // 1280x1024 @ 75 Hz + t( + 1280, + 1024, + false, + 59500, + 60500, + [48, 112, 248], + [1, 3, 38], + true, + true, + ), // 1280x1024 @ 60 Hz + t( + 1280, + 960, + false, + 84500, + 85500, + [64, 160, 224], + [1, 3, 47], + true, + true, + ), // 1280x960 @ 85 Hz + t( + 1280, + 960, + false, + 59500, + 60500, + [96, 112, 312], + [1, 3, 36], + true, + true, + ), // 1280x960 @ 60 Hz + t( + 1152, + 864, + false, + 74500, + 75500, + [64, 128, 256], + [1, 3, 32], + true, + true, + ), // 1152x864 @ 75 Hz + t( + 1024, + 768, + false, + 84500, + 85500, + [48, 96, 208], + [1, 3, 36], + true, + true, + ), // 1024x768 @ 85 Hz + t( + 1024, + 768, + false, + 74500, + 75500, + [16, 96, 176], + [1, 3, 28], + true, + true, + ), // 1024x768 @ 75 Hz + t( + 1024, + 768, + false, + 69500, + 70500, + [24, 136, 144], + [3, 6, 29], + false, + false, + ), // 1024x768 @ 70 Hz + t( + 1024, + 768, + false, + 59500, + 60500, + [24, 136, 160], + [3, 6, 29], + false, + false, + ), // 1024x768 @ 60 Hz + t( + 800, + 600, + false, + 84500, + 85500, + [32, 64, 152], + [1, 3, 27], + true, + true, + ), // 800x600 @ 85 Hz + t( + 800, + 600, + false, + 74500, + 75500, + [16, 80, 160], + [1, 3, 21], + true, + true, + ), // 800x600 @ 75 Hz + t( + 800, + 600, + false, + 71500, + 72500, + [56, 120, 64], + [37, 6, 23], + true, + true, + ), // 800x600 @ 72 Hz + t( + 800, + 600, + false, + 59500, + 60500, + [40, 128, 88], + [1, 4, 23], + true, + true, + ), // 800x600 @ 60 Hz + t( + 800, + 600, + false, + 55500, + 56500, + [24, 72, 128], + [1, 2, 22], + true, + true, + ), // 800x600 @ 56 Hz + t( + 1440, + 576, + true, + 49500, + 50500, + [24, 126, 138], + [4, 6, 39], + false, + false, + ), // 1440x576i @ 50 Hz + t( + 1440, + 480, + true, + 59500, + 60500, + [38, 124, 114], + [8, 6, 31], + false, + false, + ), // 1440x480i @ 60 Hz + t( + 720, + 576, + false, + 49500, + 50500, + [12, 64, 68], + [5, 5, 39], + false, + false, + ), // 720x576 @ 50 Hz + t( + 720, + 480, + false, + 59500, + 60500, + [16, 62, 60], + [9, 6, 30], + false, + false, + ), // 720x480 @ 60 Hz + t( + 640, + 480, + false, + 84500, + 85500, + [56, 56, 80], + [1, 3, 25], + false, + false, + ), // 640x480 @ 85 Hz + t( + 640, + 480, + false, + 74500, + 75500, + [16, 64, 120], + [1, 3, 16], + false, + false, + ), // 640x480 @ 75 Hz + t( + 640, + 480, + false, + 71500, + 72500, + [24, 40, 128], + [9, 3, 28], + false, + false, + ), // 640x480 @ 72 Hz + t( + 640, + 480, + false, + 59500, + 60500, + [16, 96, 48], + [10, 2, 33], + false, + false, + ), // 640x480 @ 60 Hz +]; + +pub(super) const NATIVE: &[(i64, i64, bool, i64)] = &[ + (1920, 1080, false, 60000), // 1920x1080 @ 60 Hz + (1920, 1080, true, 60000), // 1920x1080i @ 60 Hz + (1600, 900, false, 60000), // 1600x900 @ 60 Hz + (1366, 768, false, 59789), // 1366x768 @ 60 Hz + (1360, 768, false, 60015), // 1360x768 @ 60 Hz + (1280, 720, false, 60000), // 1280x720 @ 60 Hz + (1440, 576, true, 50000), // 1440x576i @ 50 Hz + (1440, 480, true, 59940), // 1440x480i @ 60 Hz + (720, 576, false, 50000), // 720x576 @ 50 Hz + (720, 480, false, 59940), // 720x480 @ 60 Hz + (640, 480, false, 60000), // 640x480 @ 60 Hz +]; diff --git a/src/timings/tests.rs b/src/timings/tests.rs new file mode 100644 index 0000000..e9fe581 --- /dev/null +++ b/src/timings/tests.rs @@ -0,0 +1,741 @@ +use super::*; + +fn request(width: u16, height: u16, refresh_hz: f64, interlaced: bool) -> CvtRequest { + CvtRequest { + width, + height, + refresh_hz, + interlaced, + } +} + +/// CEA 1080p60 with H+ V-, the "current" timing used for the CRU references. +fn current_1080p() -> TimingDescriptor { + TimingDescriptor { + pixel_clock_khz: 148_500, + h_active: 1920, + h_blanking: 280, + h_front_porch: 88, + h_sync_width: 44, + h_back_porch: 148, + v_active: 1080, + v_blanking: 45, + v_front_porch: 4, + v_sync_width: 5, + v_back_porch: 36, + h_sync_positive: true, + v_sync_positive: false, + interlaced: false, + } +} + +/// Outputs of CRU's own `DetailedResolutionClass` (compiled from the CRU +/// source) for each preset, with `current_1080p` as the existing timing: +/// (preset index, interlaced, width, height, refresh mHz, [h front, sync, +/// back], [v front, sync, back], pixel clock in 10 kHz, H+, V+). +#[allow(clippy::type_complexity)] +const CRU_REFERENCE: &[( + usize, + bool, + u16, + u16, + u32, + [u16; 3], + [u16; 3], + u32, + bool, + bool, +)] = &[ + ( + 1, + false, + 1920, + 1080, + 60000, + [88, 44, 148], + [4, 5, 36], + 14850, + true, + true, + ), + ( + 1, + false, + 1920, + 1080, + 144000, + [48, 32, 80], + [3, 5, 69], + 34655, + true, + false, + ), + ( + 1, + false, + 1280, + 720, + 25000, + [48, 32, 80], + [3, 5, 6], + 2643, + true, + false, + ), + ( + 1, + true, + 1920, + 1080, + 50000, + [528, 44, 148], + [4, 10, 31], + 7425, + true, + true, + ), + ( + 1, + false, + 2560, + 1440, + 165000, + [48, 32, 80], + [3, 5, 111], + 69968, + true, + false, + ), + ( + 2, + false, + 1920, + 1080, + 50000, + [528, 44, 148], + [4, 5, 36], + 14850, + true, + true, + ), + ( + 2, + false, + 3840, + 2160, + 60000, + [176, 88, 296], + [8, 10, 72], + 59400, + true, + true, + ), + ( + 2, + false, + 3840, + 2160, + 120000, + [176, 88, 296], + [8, 10, 72], + 118800, + true, + true, + ), + ( + 2, + false, + 7680, + 4320, + 60000, + [552, 176, 592], + [16, 20, 44], + 237600, + true, + true, + ), + ( + 2, + true, + 1440, + 480, + 59940, + [38, 124, 114], + [8, 6, 31], + 2700, + false, + false, + ), + ( + 2, + false, + 1366, + 768, + 60000, + [70, 143, 213], + [3, 3, 24], + 8581, + true, + true, + ), + ( + 3, + false, + 1024, + 768, + 85000, + [48, 96, 208], + [1, 3, 36], + 9451, + true, + true, + ), + ( + 3, + false, + 1280, + 1024, + 60000, + [48, 112, 248], + [1, 3, 38], + 10797, + true, + true, + ), + ( + 3, + false, + 1600, + 1200, + 100000, + [128, 176, 304], + [3, 4, 67], + 28130, + false, + true, + ), + ( + 3, + true, + 1024, + 768, + 60000, + [32, 96, 128], + [6, 8, 21], + 3084, + false, + true, + ), + ( + 4, + false, + 1920, + 1080, + 75000, + [88, 44, 148], + [4, 5, 36], + 18563, + true, + true, + ), + ( + 4, + false, + 1366, + 768, + 60000, + [70, 143, 213], + [3, 3, 24], + 8581, + true, + true, + ), + ( + 4, + false, + 2560, + 1440, + 144000, + [48, 32, 80], + [3, 5, 33], + 58008, + true, + false, + ), + ( + 5, + false, + 1920, + 1080, + 100000, + [88, 44, 148], + [4, 5, 36], + 24750, + true, + true, + ), + ( + 5, + false, + 3840, + 2160, + 120000, + [176, 88, 296], + [8, 10, 72], + 118800, + true, + true, + ), + ( + 6, + false, + 2560, + 1440, + 165000, + [48, 32, 80], + [3, 5, 52], + 67320, + true, + false, + ), + ( + 6, + true, + 1920, + 1080, + 50000, + [48, 32, 80], + [6, 10, 29], + 5850, + true, + false, + ), + ( + 6, + false, + 1920, + 1080, + 23976, + [48, 32, 80], + [3, 5, 37], + 5611, + true, + false, + ), + ( + 7, + false, + 2560, + 1440, + 165000, + [48, 32, 80], + [3, 5, 2], + 65076, + true, + false, + ), + ( + 7, + false, + 1920, + 1080, + 144000, + [24, 32, 24], + [3, 5, 12], + 31680, + true, + false, + ), + ( + 7, + false, + 1280, + 1024, + 75000, + [48, 32, 80], + [3, 7, 16], + 11340, + true, + false, + ), + ( + 8, + false, + 1920, + 1080, + 85000, + [144, 208, 368], + [3, 5, 162], + 28050, + false, + true, + ), + ( + 8, + true, + 1920, + 1080, + 60000, + [64, 192, 384], + [6, 10, 29], + 8640, + false, + true, + ), + ( + 9, + false, + 1920, + 1080, + 60000, + [128, 200, 328], + [3, 5, 32], + 17311, + false, + true, + ), + ( + 9, + false, + 1024, + 768, + 85000, + [72, 104, 176], + [3, 4, 34], + 9463, + false, + true, + ), + ( + 9, + true, + 1920, + 1080, + 60000, + [64, 192, 256], + [6, 10, 29], + 8208, + false, + true, + ), + ( + 10, + false, + 1920, + 1080, + 60000, + [48, 32, 80], + [3, 5, 23], + 13866, + true, + false, + ), + ( + 10, + false, + 3840, + 2160, + 60000, + [48, 32, 80], + [3, 5, 54], + 53328, + true, + false, + ), + ( + 10, + false, + 2560, + 1440, + 240000, + [48, 32, 80], + [3, 5, 171], + 105689, + true, + false, + ), + ( + 11, + false, + 1920, + 1080, + 60000, + [8, 32, 40], + [17, 8, 6], + 13332, + true, + false, + ), + ( + 11, + false, + 3840, + 2160, + 144000, + [8, 32, 40], + [140, 8, 6], + 130621, + true, + false, + ), + ( + 11, + true, + 1920, + 1080, + 60000, + [8, 32, 40], + [4, 16, 13], + 6678, + true, + false, + ), + ( + 12, + false, + 1920, + 1080, + 60000, + [120, 208, 328], + [1, 3, 34], + 17280, + false, + true, + ), + ( + 12, + false, + 1280, + 1024, + 75000, + [88, 136, 224], + [1, 3, 41], + 13855, + false, + true, + ), + ( + 13, + false, + 1920, + 1080, + 48000, + [88, 44, 148], + [4, 5, 317], + 14848, + true, + false, + ), + ( + 13, + false, + 1920, + 1080, + 30000, + [88, 44, 148], + [4, 5, 1161], + 14850, + true, + false, + ), +]; + +#[test] +fn presets_match_cru_reference_outputs() { + for &(index, interlaced, width, height, millihz, h, v, pclock, h_pos, v_pos) in CRU_REFERENCE { + let preset = TimingPreset::ALL[index]; + let timing = timing_for_preset( + preset, + request(width, height, f64::from(millihz) / 1000.0, interlaced), + Some(¤t_1080p()), + ) + .unwrap_or_else(|error| panic!("{preset:?} {width}x{height}@{millihz}: {error}")); + let context = format!( + "{preset:?} {width}x{height}{} @{millihz}", + if interlaced { "i" } else { "" } + ); + + assert_eq!( + (timing.h_active, timing.v_active), + (width, height), + "{context}" + ); + assert_eq!( + [ + timing.h_front_porch, + timing.h_sync_width, + timing.h_back_porch + ], + h, + "{context}" + ); + assert_eq!( + [ + timing.v_front_porch, + timing.v_sync_width, + timing.v_back_porch + ], + v, + "{context}" + ); + assert_eq!(timing.h_blanking, h.iter().sum::(), "{context}"); + assert_eq!(timing.v_blanking, v.iter().sum::(), "{context}"); + assert_eq!(timing.pixel_clock_khz, pclock * 10, "{context}"); + assert_eq!( + (timing.h_sync_positive, timing.v_sync_positive), + (h_pos, v_pos), + "{context}" + ); + assert_eq!(timing.interlaced, interlaced, "{context}"); + } +} + +#[test] +fn preset_list_matches_cru_order() { + let labels: Vec<_> = TimingPreset::ALL + .iter() + .map(|preset| preset.label()) + .collect(); + assert_eq!( + labels, + [ + "Manual", + "Automatic PC", + "Automatic HDTV", + "Automatic CRT", + "Native PC", + "Native HDTV", + "Exact", + "Exact reduced", + "Exact CRT", + "CVT standard", + "CVT-RB standard", + "CVT-RB2 standard", + "GTF standard", + "Vertical total calculator (QFT)", + ] + ); + assert_eq!(TimingPreset::Manual.cycle(-1), TimingPreset::VerticalTotal); +} + +#[test] +fn automatic_tables_reproduce_their_dtd_valid_rows() { + for (preset, table) in [ + (TimingPreset::AutomaticPc, AUTOMATIC_PC), + (TimingPreset::AutomaticHdtv, AUTOMATIC_HDTV), + (TimingPreset::AutomaticCrt, AUTOMATIC_CRT), + ] { + for row in table { + let millihz = (row.min_v_rate + row.max_v_rate) / 2; + let timing = timing_for_preset( + preset, + request( + row.h_active as u16, + row.v_active as u16, + millihz as f64 / 1000.0, + row.interlaced, + ), + None, + ) + .unwrap(); + let porches = [ + timing.h_front_porch, + timing.h_sync_width, + timing.h_back_porch, + timing.v_front_porch, + timing.v_sync_width, + timing.v_back_porch, + ] + .map(i64::from); + let row_porches = [row.h[0], row.h[1], row.h[2], row.v[0], row.v[1], row.v[2]]; + let fits_dtd = row.h.iter().all(|&value| value <= 1023) + && row.h.iter().sum::() <= 4095 + && row.v[0] / i64::from(1 + u8::from(row.interlaced)) <= 63; + if fits_dtd { + assert_eq!( + porches, row_porches, + "{preset:?} {}x{}", + row.h_active, row.v_active + ); + assert!((timing.refresh_hz().unwrap() * 1000.0 - millihz as f64).abs() < 50.0); + } else if row.h_active <= 4095 && table_clock_10khz(row, millihz) <= 65535 { + // CRU falls back to CVT-RB when the table porches cannot be + // encoded but the active area and clock can. Otherwise it + // keeps the table timing for validation to report. + assert_ne!( + porches, row_porches, + "{preset:?} {}x{}", + row.h_active, row.v_active + ); + } + } + } +} + +fn table_clock_10khz(row: &TableTiming, millihz: i64) -> i64 { + let h_total = row.h_active + row.h.iter().sum::(); + let v_total = row.v_active + row.v.iter().sum::(); + let divisor = 10_000_000 * (1 + i64::from(row.interlaced)); + (millihz * h_total * v_total + divisor - 1) / divisor +} + +#[test] +fn interlaced_presets_produce_encodable_half_lines() { + for preset in TimingPreset::ALL.into_iter().skip(1).take(12) { + let timing = timing_for_preset( + preset, + request(1920, 1080, 60.0, true), + Some(¤t_1080p()), + ) + .unwrap(); + assert!(timing.interlaced); + assert!( + timing.edid_vertical_fields().is_ok(), + "{preset:?}: {timing:?}" + ); + assert!( + (timing.refresh_hz().unwrap() - 60.0).abs() < 0.1, + "{preset:?}" + ); + } +} + +#[test] +fn vertical_total_keeps_pixel_clock_and_needs_a_timing() { + let timing = timing_for_preset( + TimingPreset::VerticalTotal, + request(1920, 1080, 30.0, false), + Some(¤t_1080p()), + ) + .unwrap(); + assert_eq!(timing.pixel_clock_khz, 148_500); + assert_eq!(timing.v_total(), 2250); + assert!( + timing_for_preset( + TimingPreset::VerticalTotal, + request(1920, 1080, 30.0, false), + None + ) + .is_err() + ); +} + +#[test] +fn extreme_requests_fail_or_validate_without_overflow() { + for preset in TimingPreset::ALL.into_iter().skip(1) { + for (width, height) in [(1, 1), (u16::MAX, u16::MAX), (u16::MAX, 1), (1, u16::MAX)] { + for interlaced in [false, true] { + for refresh_hz in [0.0, 0.001, 60.0, 2173.9, 1e9, 1e100, -1.0, f64::NAN] { + if let Ok(timing) = timing_for_preset( + preset, + request(width, height, refresh_hz, interlaced), + Some(¤t_1080p()), + ) && width == u16::MAX + { + assert!(!crate::validation::validate_timing(&timing).is_empty()); + } + } + } + } + } +} diff --git a/src/tui/mod.rs b/src/tui/mod.rs index ec9e14a..b4e2552 100644 --- a/src/tui/mod.rs +++ b/src/tui/mod.rs @@ -211,7 +211,9 @@ impl App { width: 2560, height: 1440, refresh_hz: 144.0, - }), + interlaced: false, + }) + .expect("CVT-RB timing for 2560x1440@144 is valid"), detailed_clipboard: None, detailed_editor: None, standard_editor: None, diff --git a/src/tui/state.rs b/src/tui/state.rs index 7f69290..5db2387 100644 --- a/src/tui/state.rs +++ b/src/tui/state.rs @@ -255,7 +255,7 @@ impl DetailedResolutionEditor { } let request = self.preset_request()?; - Ok(timing_for_preset(self.preset, request, Some(&manual))) + timing_for_preset(self.preset, request, Some(&manual)) } pub(super) fn manual_timing(&self) -> Result { @@ -320,6 +320,7 @@ impl DetailedResolutionEditor { width, height, refresh_hz, + interlaced: self.interlaced, }) } @@ -692,7 +693,9 @@ impl DetailedResolutionEditor { return; }; let current = self.manual_timing().ok(); - let timing = timing_for_preset(self.preset, request, current.as_ref()); + let Ok(timing) = timing_for_preset(self.preset, request, current.as_ref()) else { + return; + }; self.set_timing_fields(&timing); self.refresh_hint.set(format!("{:.2}", request.refresh_hz)); } @@ -1290,7 +1293,9 @@ mod tests { width: 1920, height: 1080, refresh_hz: 60.0, - }); + interlaced: false, + }) + .unwrap(); let mut editor = DetailedResolutionEditor::from_timing(timing, EditorMode::Add); editor.active_field = EditorField::Refresh; editor.input_mut(EditorField::Refresh).set("60".to_string()); @@ -1305,7 +1310,9 @@ mod tests { width: 1920, height: 1080, refresh_hz: 60.0, - }); + interlaced: false, + }) + .unwrap(); let mut editor = DetailedResolutionEditor::from_timing(timing, EditorMode::Add); for field in [ EditorField::Width,