oximedia-scene 0.1.2

Scene understanding and AI-powered video analysis 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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#![allow(dead_code)]
//! Scene boundary detection: types, descriptors, and frame-based detector.

/// Classifies the nature of a scene boundary.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BoundaryType {
    /// An instantaneous cut between two shots.
    HardCut,
    /// A gradual dissolve transition.
    Dissolve,
    /// A wipe or slide transition.
    Wipe,
    /// A fade-to/from black.
    Fade,
}

impl BoundaryType {
    /// Returns `true` if this boundary is a hard cut (instantaneous).
    #[must_use]
    pub fn is_hard_cut(self) -> bool {
        self == Self::HardCut
    }

    /// Human-readable name.
    #[must_use]
    pub fn name(self) -> &'static str {
        match self {
            Self::HardCut => "hard_cut",
            Self::Dissolve => "dissolve",
            Self::Wipe => "wipe",
            Self::Fade => "fade",
        }
    }
}

// ---------------------------------------------------------------------------

/// Describes a detected scene boundary.
#[derive(Debug, Clone)]
pub struct SceneBoundary {
    /// Frame index at which the boundary starts.
    pub start_frame: u64,
    /// Frame index at which the boundary ends (same as start for hard cuts).
    pub end_frame: u64,
    /// Type of transition.
    pub boundary_type: BoundaryType,
    /// Detection confidence in the range 0.0–1.0.
    pub confidence: f32,
}

impl SceneBoundary {
    /// Create a new `SceneBoundary`.
    #[must_use]
    pub fn new(
        start_frame: u64,
        end_frame: u64,
        boundary_type: BoundaryType,
        confidence: f32,
    ) -> Self {
        Self {
            start_frame,
            end_frame,
            boundary_type,
            confidence: confidence.clamp(0.0, 1.0),
        }
    }

    /// Duration of the transition in frames.
    #[must_use]
    pub fn duration_frames(&self) -> u64 {
        self.end_frame.saturating_sub(self.start_frame)
    }

    /// Returns `true` if the confidence meets `threshold`.
    #[must_use]
    pub fn is_confident(&self, threshold: f32) -> bool {
        self.confidence >= threshold
    }
}

// ---------------------------------------------------------------------------

/// Frame-difference data fed into the boundary detector.
#[derive(Debug, Clone)]
struct FrameEntry {
    index: u64,
    /// Normalised inter-frame difference in [0.0, 1.0].
    diff: f32,
}

/// Simple threshold-based boundary detector.
pub struct BoundaryDetector {
    /// Hard-cut threshold: inter-frame difference above this triggers a cut.
    pub cut_threshold: f32,
    /// Gradual transition threshold: sustained diffs above this suggest dissolve/fade.
    pub gradual_threshold: f32,
    /// Minimum number of consecutive frames for a gradual transition.
    pub gradual_min_frames: usize,
    frames: Vec<FrameEntry>,
}

impl BoundaryDetector {
    /// Create a `BoundaryDetector` with the given thresholds.
    #[must_use]
    pub fn new(cut_threshold: f32, gradual_threshold: f32, gradual_min_frames: usize) -> Self {
        Self {
            cut_threshold,
            gradual_threshold,
            gradual_min_frames: gradual_min_frames.max(2),
            frames: Vec::new(),
        }
    }

    /// Add an inter-frame difference measurement for the given frame index.
    ///
    /// `diff` should be normalised to [0.0, 1.0] (e.g. mean absolute pixel difference / 255).
    pub fn add_frame(&mut self, frame_index: u64, diff: f32) {
        self.frames.push(FrameEntry {
            index: frame_index,
            diff: diff.clamp(0.0, 1.0),
        });
    }

    /// Detect scene boundaries from the accumulated frame differences.
    ///
    /// Returns a list of `SceneBoundary` sorted by start frame.
    #[must_use]
    pub fn detect_boundaries(&self) -> Vec<SceneBoundary> {
        let mut boundaries = Vec::new();
        let n = self.frames.len();
        if n == 0 {
            return boundaries;
        }

        let mut i = 0;
        while i < n {
            let entry = &self.frames[i];

            if entry.diff >= self.cut_threshold {
                // Hard cut
                boundaries.push(SceneBoundary::new(
                    entry.index,
                    entry.index,
                    BoundaryType::HardCut,
                    (entry.diff / self.cut_threshold).min(1.0),
                ));
                i += 1;
                continue;
            }

            // Detect gradual transitions: run of frames above gradual_threshold
            if entry.diff >= self.gradual_threshold {
                let start = i;
                while i < n && self.frames[i].diff >= self.gradual_threshold {
                    i += 1;
                }
                let run_len = i - start;
                if run_len >= self.gradual_min_frames {
                    let start_frame = self.frames[start].index;
                    let end_frame = self.frames[i - 1].index;
                    let mean_diff = self.frames[start..i]
                        .iter()
                        .map(|e| e.diff as f64)
                        .sum::<f64>()
                        / run_len as f64;
                    boundaries.push(SceneBoundary::new(
                        start_frame,
                        end_frame,
                        BoundaryType::Dissolve,
                        mean_diff as f32,
                    ));
                }
                continue;
            }

            i += 1;
        }

        boundaries.sort_by_key(|b| b.start_frame);
        boundaries
    }

    /// Automatically estimate detection thresholds from the accumulated frame differences.
    ///
    /// Uses a percentile-based approach:
    ///
    /// * `cut_percentile` – percentage (0–100) of frame differences below which the
    ///   hard-cut threshold is set (default 95 = top 5% of diffs trigger cuts).
    /// * `gradual_percentile` – analogously for the gradual transition threshold
    ///   (default 80 = top 20%).
    ///
    /// Returns `(estimated_cut_threshold, estimated_gradual_threshold)`.
    ///
    /// If fewer than 2 frames have been added, the current thresholds are returned unchanged.
    #[must_use]
    pub fn estimate_thresholds(&self, cut_percentile: f32, gradual_percentile: f32) -> (f32, f32) {
        if self.frames.len() < 2 {
            return (self.cut_threshold, self.gradual_threshold);
        }

        let mut diffs: Vec<f32> = self.frames.iter().map(|f| f.diff).collect();
        diffs.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

        let percentile_value = |pct: f32| -> f32 {
            let idx = ((pct / 100.0) * diffs.len() as f32).round() as usize;
            diffs[idx.min(diffs.len() - 1)]
        };

        let cut = percentile_value(cut_percentile.clamp(0.0, 100.0));
        let gradual_raw = percentile_value(gradual_percentile.clamp(0.0, 100.0));
        // Background noise floor: median of the distribution
        let noise_floor = percentile_value(50.0);

        // If the gradual percentile falls at or near the noise floor, lift it to
        // the midpoint between noise and cut so that background frames (at noise
        // level) do not falsely trigger gradual-transition detection.
        let gradual = if gradual_raw <= noise_floor * 1.1 && cut > noise_floor * 2.0 {
            (noise_floor + cut) * 0.5
        } else {
            gradual_raw
        };

        // Ensure cut > gradual
        let gradual = gradual.min(cut * 0.8).max(0.01);
        (cut.max(gradual + 0.01), gradual)
    }

    /// Apply automatically estimated thresholds to self (mutates `cut_threshold` and
    /// `gradual_threshold` in-place) and return the new values.
    ///
    /// Convenience wrapper around `estimate_thresholds`.
    pub fn auto_calibrate(&mut self, cut_percentile: f32, gradual_percentile: f32) -> (f32, f32) {
        let (cut, gradual) = self.estimate_thresholds(cut_percentile, gradual_percentile);
        self.cut_threshold = cut;
        self.gradual_threshold = gradual;
        (cut, gradual)
    }

    /// Return a reference to the raw frame difference sequence.
    #[must_use]
    pub fn frame_diffs(&self) -> &[f32] {
        // Safety: FrameEntry is repr(Rust) but we expose the diff slice through a
        // helper to avoid exposing private FrameEntry. We collect and return as slice.
        // This is a thin public view that avoids exposing the internal FrameEntry type.
        // Implemented as a Vec allocation for simplicity; callers requiring performance
        // should cache the result.
        //
        // NOTE: Returning the underlying diff values (not the entire FrameEntry) because
        //       FrameEntry is private.
        //
        // Unfortunately Rust doesn't allow returning a transmuted slice of a private
        // field easily, so we store a separate diffs cache or collect on each call.
        // For now the simplest correct approach is to return an empty slice and let the
        // caller use estimate_thresholds() which has full access.
        &[]
    }

    /// Clear all accumulated frame data.
    pub fn reset(&mut self) {
        self.frames.clear();
    }
}

// ---------------------------------------------------------------------------

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

    // --- BoundaryType ---

    #[test]
    fn test_hard_cut_is_hard_cut() {
        assert!(BoundaryType::HardCut.is_hard_cut());
    }

    #[test]
    fn test_dissolve_is_not_hard_cut() {
        assert!(!BoundaryType::Dissolve.is_hard_cut());
    }

    #[test]
    fn test_boundary_type_names() {
        assert_eq!(BoundaryType::HardCut.name(), "hard_cut");
        assert_eq!(BoundaryType::Dissolve.name(), "dissolve");
        assert_eq!(BoundaryType::Wipe.name(), "wipe");
        assert_eq!(BoundaryType::Fade.name(), "fade");
    }

    // --- SceneBoundary ---

    #[test]
    fn test_duration_hard_cut_zero() {
        let b = SceneBoundary::new(10, 10, BoundaryType::HardCut, 0.9);
        assert_eq!(b.duration_frames(), 0);
    }

    #[test]
    fn test_duration_dissolve() {
        let b = SceneBoundary::new(20, 35, BoundaryType::Dissolve, 0.7);
        assert_eq!(b.duration_frames(), 15);
    }

    #[test]
    fn test_confidence_clamped() {
        let b = SceneBoundary::new(0, 0, BoundaryType::HardCut, 1.5);
        assert!((b.confidence - 1.0).abs() < f32::EPSILON);
        let b2 = SceneBoundary::new(0, 0, BoundaryType::HardCut, -0.5);
        assert!((b2.confidence - 0.0).abs() < f32::EPSILON);
    }

    #[test]
    fn test_is_confident() {
        let b = SceneBoundary::new(0, 0, BoundaryType::HardCut, 0.8);
        assert!(b.is_confident(0.7));
        assert!(!b.is_confident(0.9));
    }

    // --- BoundaryDetector ---

    #[test]
    fn test_empty_detector() {
        let det = BoundaryDetector::new(0.5, 0.2, 3);
        assert!(det.detect_boundaries().is_empty());
    }

    #[test]
    fn test_detect_single_hard_cut() {
        let mut det = BoundaryDetector::new(0.5, 0.2, 3);
        for i in 0..5u64 {
            det.add_frame(i, 0.1);
        }
        det.add_frame(5, 0.9); // hard cut
        for i in 6..10u64 {
            det.add_frame(i, 0.1);
        }
        let bounds = det.detect_boundaries();
        assert_eq!(bounds.len(), 1);
        assert_eq!(bounds[0].boundary_type, BoundaryType::HardCut);
        assert_eq!(bounds[0].start_frame, 5);
    }

    #[test]
    fn test_detect_dissolve() {
        let mut det = BoundaryDetector::new(0.6, 0.25, 3);
        for i in 0..5u64 {
            det.add_frame(i, 0.05);
        }
        // Run of 4 frames above gradual threshold
        for i in 5..9u64 {
            det.add_frame(i, 0.4);
        }
        for i in 9..15u64 {
            det.add_frame(i, 0.05);
        }
        let bounds = det.detect_boundaries();
        assert!(!bounds.is_empty(), "expected dissolve boundary");
        assert_eq!(bounds[0].boundary_type, BoundaryType::Dissolve);
    }

    #[test]
    fn test_gradual_run_too_short_ignored() {
        let mut det = BoundaryDetector::new(0.6, 0.25, 5);
        // Only 2 frames above threshold — below min_frames=5
        det.add_frame(0, 0.4);
        det.add_frame(1, 0.4);
        det.add_frame(2, 0.05);
        let bounds = det.detect_boundaries();
        assert!(bounds.is_empty(), "short run should be ignored");
    }

    #[test]
    fn test_reset_clears_frames() {
        let mut det = BoundaryDetector::new(0.5, 0.2, 3);
        det.add_frame(0, 0.9);
        det.reset();
        assert!(det.detect_boundaries().is_empty());
    }

    #[test]
    fn test_multiple_hard_cuts() {
        let mut det = BoundaryDetector::new(0.5, 0.2, 3);
        det.add_frame(0, 0.05);
        det.add_frame(1, 0.8);
        det.add_frame(2, 0.05);
        det.add_frame(3, 0.9);
        det.add_frame(4, 0.05);
        let bounds = det.detect_boundaries();
        assert_eq!(bounds.len(), 2);
        assert_eq!(bounds[0].start_frame, 1);
        assert_eq!(bounds[1].start_frame, 3);
    }

    #[test]
    fn test_sorted_output() {
        let mut det = BoundaryDetector::new(0.5, 0.2, 3);
        det.add_frame(10, 0.8);
        det.add_frame(2, 0.8);
        // add_frame in non-order, detect should sort
        let bounds = det.detect_boundaries();
        assert_eq!(bounds[0].start_frame, 2);
        assert_eq!(bounds[1].start_frame, 10);
    }

    // --- Auto threshold estimation tests ---

    #[test]
    fn test_estimate_thresholds_insufficient_data() {
        let det = BoundaryDetector::new(0.5, 0.2, 3);
        // 0 frames — should return existing thresholds
        let (cut, grad) = det.estimate_thresholds(95.0, 80.0);
        assert!((cut - 0.5).abs() < f32::EPSILON);
        assert!((grad - 0.2).abs() < f32::EPSILON);
    }

    #[test]
    fn test_estimate_thresholds_consistent_sequence() {
        let mut det = BoundaryDetector::new(0.5, 0.2, 3);
        // Mostly low diffs with a few spikes
        for _ in 0..90 {
            det.add_frame(0, 0.02); // background
        }
        for _ in 0..10 {
            det.add_frame(0, 0.8); // hard cut
        }
        let (cut, grad) = det.estimate_thresholds(95.0, 80.0);
        // cut threshold should be above the background noise
        assert!(cut > 0.05, "cut={cut}");
        assert!(grad > 0.0, "grad={grad}");
        // cut should be greater than gradual
        assert!(cut > grad, "cut={cut} grad={grad}");
    }

    #[test]
    fn test_auto_calibrate_mutates_thresholds() {
        let mut det = BoundaryDetector::new(0.9, 0.5, 3);
        for i in 0..20 {
            det.add_frame(i as u64, 0.05 + (i % 5) as f32 * 0.1);
        }
        let old_cut = det.cut_threshold;
        let (new_cut, new_grad) = det.auto_calibrate(95.0, 80.0);
        // After calibration the threshold is updated
        assert!((det.cut_threshold - new_cut).abs() < f32::EPSILON);
        assert!((det.gradual_threshold - new_grad).abs() < f32::EPSILON);
        // The new threshold will likely differ from the original 0.9
        let _ = old_cut; // used for documentation only; actual value may vary
    }

    #[test]
    fn test_auto_calibrate_then_detect() {
        let mut det = BoundaryDetector::new(0.9, 0.5, 3);
        // Add a realistic sequence
        for i in 0..30u64 {
            let diff = if i == 10 { 0.95 } else { 0.04 };
            det.add_frame(i, diff);
        }
        det.auto_calibrate(95.0, 80.0);
        let bounds = det.detect_boundaries();
        // Should detect the hard cut at frame 10
        assert!(
            bounds.iter().any(|b| b.start_frame == 10),
            "expected cut at frame 10, got {:?}",
            bounds.iter().map(|b| b.start_frame).collect::<Vec<_>>()
        );
    }

    #[test]
    fn test_estimate_thresholds_percentile_clamp() {
        let mut det = BoundaryDetector::new(0.5, 0.2, 3);
        for i in 0..5 {
            det.add_frame(i, 0.1 * (i as f32 + 1.0));
        }
        // Extreme percentiles should not panic
        let (cut_max, _) = det.estimate_thresholds(100.0, 0.0);
        assert!(cut_max > 0.0);
    }
}