oximedia-virtual 0.1.2

Virtual production and LED wall tools for OxiMedia
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//! Genlock synchronization for virtual production.
//!
//! Provides sync signal generation, lock detection, and phase alignment
//! for synchronizing LED walls, cameras, and rendering engines.

#![allow(dead_code)]
#![allow(clippy::cast_precision_loss)]

use std::time::Duration;

/// Type of genlock sync signal.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SyncSignalType {
    /// Black burst (PAL/NTSC analog composite sync).
    BlackBurst,
    /// Tri-level sync (HD reference).
    TriLevel,
    /// SDI embedded sync.
    SdiEmbedded,
    /// Word clock (audio-derived sync).
    WordClock,
    /// GPS-derived PPS (pulse-per-second).
    Gps,
}

impl SyncSignalType {
    /// Returns a human-readable name for the signal type.
    #[must_use]
    pub fn description(&self) -> &'static str {
        match self {
            Self::BlackBurst => "Black Burst (analog composite)",
            Self::TriLevel => "Tri-Level Sync (HD reference)",
            Self::SdiEmbedded => "SDI Embedded Sync",
            Self::WordClock => "Word Clock",
            Self::Gps => "GPS PPS",
        }
    }
}

/// Current lock state of a genlock signal.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LockState {
    /// No signal detected.
    NoSignal,
    /// Signal detected but not yet locked.
    Acquiring,
    /// Locked to the reference signal.
    Locked,
    /// Previously locked; signal lost.
    Lost,
}

impl LockState {
    /// Returns `true` if the genlock is currently locked.
    #[must_use]
    pub fn is_locked(&self) -> bool {
        matches!(self, Self::Locked)
    }
}

/// Phase alignment information relative to the reference signal.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct PhaseAlignment {
    /// Phase offset in degrees [−180.0, 180.0].
    pub offset_degrees: f32,
    /// Frequency error in parts-per-million.
    pub frequency_error_ppm: f32,
    /// Whether the alignment is within acceptable tolerances.
    pub aligned: bool,
}

impl PhaseAlignment {
    /// Create a phase alignment measurement.
    #[must_use]
    pub fn new(offset_degrees: f32, frequency_error_ppm: f32, tolerance_degrees: f32) -> Self {
        let aligned =
            offset_degrees.abs() <= tolerance_degrees && frequency_error_ppm.abs() <= 10.0;
        Self {
            offset_degrees,
            frequency_error_ppm,
            aligned,
        }
    }

    /// Phase offset expressed as a fraction of a full cycle [−0.5, 0.5].
    #[must_use]
    pub fn phase_fraction(&self) -> f32 {
        self.offset_degrees / 360.0
    }

    /// Phase offset expressed in microseconds for a given frame rate.
    #[must_use]
    pub fn offset_microseconds(&self, fps: f32) -> f32 {
        if fps <= 0.0 {
            return 0.0;
        }
        let frame_period_us = 1_000_000.0 / fps;
        (self.offset_degrees / 360.0) * frame_period_us
    }
}

/// Configuration for a genlock generator.
#[derive(Debug, Clone)]
pub struct GenlockGeneratorConfig {
    /// Type of sync signal to generate.
    pub signal_type: SyncSignalType,
    /// Output frame rate numerator.
    pub fps_num: u32,
    /// Output frame rate denominator.
    pub fps_den: u32,
    /// Phase offset to apply to the generated signal in degrees.
    pub phase_offset_deg: f32,
    /// Whether to enable frame-accurate jitter measurement.
    pub measure_jitter: bool,
}

impl GenlockGeneratorConfig {
    /// Create a default HD configuration (29.97 fps tri-level).
    #[must_use]
    pub fn hd_default() -> Self {
        Self {
            signal_type: SyncSignalType::TriLevel,
            fps_num: 30000,
            fps_den: 1001,
            phase_offset_deg: 0.0,
            measure_jitter: true,
        }
    }

    /// Create a cinema configuration (24 fps tri-level).
    #[must_use]
    pub fn cinema() -> Self {
        Self {
            signal_type: SyncSignalType::TriLevel,
            fps_num: 24,
            fps_den: 1,
            phase_offset_deg: 0.0,
            measure_jitter: true,
        }
    }

    /// Effective frame rate as f64.
    #[must_use]
    pub fn fps(&self) -> f64 {
        f64::from(self.fps_num) / f64::from(self.fps_den)
    }

    /// Frame period as a `Duration`.
    #[must_use]
    pub fn frame_period(&self) -> Duration {
        let nanos = (1_000_000_000.0 / self.fps()) as u64;
        Duration::from_nanos(nanos)
    }
}

impl Default for GenlockGeneratorConfig {
    fn default() -> Self {
        Self::hd_default()
    }
}

/// Genlock generator that emits sync pulses at a configured frame rate.
#[allow(dead_code)]
pub struct GenlockGenerator {
    config: GenlockGeneratorConfig,
    /// Total number of sync pulses emitted.
    pulse_count: u64,
    /// Accumulated jitter in nanoseconds (if measurement enabled).
    jitter_ns_accumulated: f64,
}

impl GenlockGenerator {
    /// Create a new genlock generator.
    #[must_use]
    pub fn new(config: GenlockGeneratorConfig) -> Self {
        Self {
            config,
            pulse_count: 0,
            jitter_ns_accumulated: 0.0,
        }
    }

    /// Simulate emitting a single sync pulse, recording optional jitter.
    pub fn emit_pulse(&mut self, jitter_ns: f64) {
        self.pulse_count += 1;
        if self.config.measure_jitter {
            self.jitter_ns_accumulated += jitter_ns.abs();
        }
    }

    /// Number of pulses emitted.
    #[must_use]
    pub fn pulse_count(&self) -> u64 {
        self.pulse_count
    }

    /// Average jitter in nanoseconds (0 if not measuring).
    #[must_use]
    pub fn average_jitter_ns(&self) -> f64 {
        if !self.config.measure_jitter || self.pulse_count == 0 {
            return 0.0;
        }
        self.jitter_ns_accumulated / self.pulse_count as f64
    }

    /// Configuration reference.
    #[must_use]
    pub fn config(&self) -> &GenlockGeneratorConfig {
        &self.config
    }
}

/// Detects lock state by monitoring incoming sync pulses.
#[allow(dead_code)]
pub struct LockDetector {
    /// Expected frame period.
    expected_period: Duration,
    /// Maximum allowed deviation to stay locked.
    tolerance_ns: u64,
    /// Current lock state.
    state: LockState,
    /// Consecutive in-tolerance pulses required to declare lock.
    lock_threshold: u32,
    /// Current run of in-tolerance pulses.
    consecutive_in_tolerance: u32,
}

impl LockDetector {
    /// Create a lock detector for a given expected period.
    #[must_use]
    pub fn new(expected_period: Duration, tolerance_ns: u64, lock_threshold: u32) -> Self {
        Self {
            expected_period,
            tolerance_ns,
            state: LockState::NoSignal,
            lock_threshold,
            consecutive_in_tolerance: 0,
        }
    }

    /// Process an incoming pulse with the given measured period.
    pub fn process_pulse(&mut self, measured_period: Duration) {
        let expected_ns = self.expected_period.as_nanos() as i64;
        let measured_ns = measured_period.as_nanos() as i64;
        let deviation = (measured_ns - expected_ns).unsigned_abs();

        if deviation <= self.tolerance_ns {
            self.consecutive_in_tolerance += 1;
            if self.consecutive_in_tolerance >= self.lock_threshold {
                self.state = LockState::Locked;
            } else {
                self.state = LockState::Acquiring;
            }
        } else {
            self.consecutive_in_tolerance = 0;
            self.state = match self.state {
                LockState::Locked => LockState::Lost,
                _ => LockState::Acquiring,
            };
        }
    }

    /// Current lock state.
    #[must_use]
    pub fn state(&self) -> LockState {
        self.state
    }

    /// Whether currently locked.
    #[must_use]
    pub fn is_locked(&self) -> bool {
        self.state.is_locked()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_sync_signal_description() {
        assert_eq!(
            SyncSignalType::TriLevel.description(),
            "Tri-Level Sync (HD reference)"
        );
        assert_eq!(
            SyncSignalType::BlackBurst.description(),
            "Black Burst (analog composite)"
        );
    }

    #[test]
    fn test_lock_state_is_locked() {
        assert!(LockState::Locked.is_locked());
        assert!(!LockState::NoSignal.is_locked());
        assert!(!LockState::Acquiring.is_locked());
        assert!(!LockState::Lost.is_locked());
    }

    #[test]
    fn test_phase_alignment_aligned_within_tolerance() {
        let pa = PhaseAlignment::new(1.0, 0.5, 5.0);
        assert!(pa.aligned);
    }

    #[test]
    fn test_phase_alignment_not_aligned_offset_exceeds_tolerance() {
        let pa = PhaseAlignment::new(10.0, 0.0, 5.0);
        assert!(!pa.aligned);
    }

    #[test]
    fn test_phase_alignment_not_aligned_frequency_error() {
        let pa = PhaseAlignment::new(0.0, 15.0, 5.0);
        assert!(!pa.aligned);
    }

    #[test]
    fn test_phase_alignment_phase_fraction() {
        let pa = PhaseAlignment::new(90.0, 0.0, 180.0);
        assert!((pa.phase_fraction() - 0.25).abs() < 1e-5);
    }

    #[test]
    fn test_phase_alignment_offset_microseconds_24fps() {
        // 90 degrees at 24fps: frame period = 1_000_000/24 ≈ 41666.7 us; quarter = ~10416.7 us
        let pa = PhaseAlignment::new(90.0, 0.0, 180.0);
        let us = pa.offset_microseconds(24.0);
        let expected = (1_000_000.0_f32 / 24.0) * 0.25;
        assert!((us - expected).abs() < 1.0);
    }

    #[test]
    fn test_phase_alignment_offset_microseconds_zero_fps() {
        let pa = PhaseAlignment::new(90.0, 0.0, 180.0);
        assert_eq!(pa.offset_microseconds(0.0), 0.0);
    }

    #[test]
    fn test_genlock_generator_config_fps() {
        let config = GenlockGeneratorConfig::hd_default();
        // 30000 / 1001 ≈ 29.97
        assert!((config.fps() - 29.97).abs() < 0.01);
    }

    #[test]
    fn test_genlock_generator_config_cinema_fps() {
        let config = GenlockGeneratorConfig::cinema();
        assert!((config.fps() - 24.0).abs() < 1e-10);
    }

    #[test]
    fn test_genlock_generator_config_frame_period() {
        let config = GenlockGeneratorConfig::cinema();
        let period = config.frame_period();
        // 24fps → ~41.667ms
        assert!((period.as_secs_f64() - 1.0 / 24.0).abs() < 0.001);
    }

    #[test]
    fn test_genlock_generator_emit_pulse() {
        let mut gen = GenlockGenerator::new(GenlockGeneratorConfig::default());
        gen.emit_pulse(100.0);
        gen.emit_pulse(200.0);
        assert_eq!(gen.pulse_count(), 2);
        // Average jitter should be (100+200)/2 = 150
        assert!((gen.average_jitter_ns() - 150.0).abs() < 1e-5);
    }

    #[test]
    fn test_genlock_generator_no_jitter_measurement() {
        let mut config = GenlockGeneratorConfig::default();
        config.measure_jitter = false;
        let mut gen = GenlockGenerator::new(config);
        gen.emit_pulse(500.0);
        assert_eq!(gen.average_jitter_ns(), 0.0);
    }

    #[test]
    fn test_lock_detector_acquires_then_locks() {
        let period = Duration::from_nanos(41_666_667); // ~24fps
        let mut detector = LockDetector::new(period, 10_000, 3);
        assert_eq!(detector.state(), LockState::NoSignal);

        detector.process_pulse(period);
        assert_eq!(detector.state(), LockState::Acquiring);

        detector.process_pulse(period);
        assert_eq!(detector.state(), LockState::Acquiring);

        detector.process_pulse(period);
        assert_eq!(detector.state(), LockState::Locked);
        assert!(detector.is_locked());
    }

    #[test]
    fn test_lock_detector_loses_lock_on_deviation() {
        let period = Duration::from_nanos(41_666_667);
        let mut detector = LockDetector::new(period, 10_000, 1);
        detector.process_pulse(period);
        assert!(detector.is_locked());

        // Send a pulse with large deviation
        detector.process_pulse(Duration::from_nanos(period.as_nanos() as u64 + 100_000));
        assert_eq!(detector.state(), LockState::Lost);
    }
}