oximedia-clips 0.1.8

Professional clip management and logging system 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
//! Automatic scene detection and subclip creation at scene boundaries.
//!
//! This module provides scene-boundary detection algorithms that analyse
//! per-frame histogram data or inter-frame pixel differences to locate
//! *cuts* and *gradual transitions* in a clip. Detected boundaries can
//! then be used to automatically split a clip into subclips.
//!
//! Two detectors are provided:
//!
//! - **`ThresholdSceneDetector`** — compares successive frames by
//!   mean absolute difference.  Fast and suitable for most content.
//! - **`HistogramSceneDetector`** — compares normalised RGB histograms
//!   using histogram intersection.  More robust to small motion/noise.
//!
//! # Example
//!
//! ```rust
//! use oximedia_clips::clip_scene_detect::{ThresholdSceneDetector, SceneBoundary};
//!
//! let mut detector = ThresholdSceneDetector::new(0.3);
//! let frames: Vec<Vec<f32>> = vec![
//!     vec![0.0; 16], // frame 0 — black
//!     vec![0.0; 16], // frame 1 — black (same scene)
//!     vec![1.0; 16], // frame 2 — white (cut!)
//!     vec![0.9; 16], // frame 3 — still white
//! ];
//! let boundaries = detector.detect(&frames);
//! assert_eq!(boundaries.len(), 1);
//! assert_eq!(boundaries[0].frame_number, 2);
//! ```

#![allow(dead_code)]

use serde::{Deserialize, Serialize};

// ─────────────────────────────────────────────────────────────────────────────
// SceneBoundary
// ─────────────────────────────────────────────────────────────────────────────

/// The type of transition detected at a scene boundary.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum TransitionType {
    /// A hard cut (instantaneous transition).
    Cut,
    /// A gradual transition (dissolve, fade, wipe).
    Gradual,
}

/// A detected scene boundary.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SceneBoundary {
    /// The *first* frame of the new scene.
    pub frame_number: u64,
    /// Difference score that triggered the boundary (scale varies by detector).
    pub score: f32,
    /// Type of transition detected.
    pub transition: TransitionType,
}

// ─────────────────────────────────────────────────────────────────────────────
// SubclipSpec — output of scene splitting
// ─────────────────────────────────────────────────────────────────────────────

/// A subclip defined by its in-point, out-point, and optional label.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SubclipSpec {
    /// First frame of the subclip (inclusive).
    pub start_frame: u64,
    /// Last frame of the subclip (inclusive).
    pub end_frame: u64,
    /// Auto-generated label (e.g. `"Scene 1"`).
    pub label: String,
    /// Approximate duration in frames.
    pub duration_frames: u64,
}

impl SubclipSpec {
    fn new(start: u64, end: u64, index: usize) -> Self {
        Self {
            start_frame: start,
            end_frame: end,
            label: format!("Scene {}", index + 1),
            duration_frames: end.saturating_sub(start) + 1,
        }
    }
}

/// Split a clip into subclip specs based on detected scene boundaries.
///
/// `total_frames` is the number of frames in the clip (0-based, so the last
/// frame index is `total_frames - 1`).
///
/// Returns one [`SubclipSpec`] per detected scene, ordered chronologically.
#[must_use]
pub fn boundaries_to_subclips(boundaries: &[SceneBoundary], total_frames: u64) -> Vec<SubclipSpec> {
    if total_frames == 0 {
        return Vec::new();
    }
    let mut starts: Vec<u64> = std::iter::once(0)
        .chain(boundaries.iter().map(|b| b.frame_number))
        .collect();
    starts.sort_unstable();
    starts.dedup();

    starts
        .windows(2)
        .enumerate()
        .map(|(i, w)| SubclipSpec::new(w[0], w[1].saturating_sub(1), i))
        .chain(std::iter::once_with(|| {
            let last_start = *starts.last().unwrap_or(&0);
            SubclipSpec::new(last_start, total_frames - 1, starts.len() - 1)
        }))
        .collect()
}

// ─────────────────────────────────────────────────────────────────────────────
// ThresholdSceneDetector
// ─────────────────────────────────────────────────────────────────────────────

/// Frame-difference-based scene detector.
///
/// Each frame is represented as a flat slice of pixel values in `[0.0, 1.0]`.
/// The mean absolute difference between consecutive frames is compared with
/// `threshold`.  When the difference exceeds `threshold` a hard cut is
/// recorded.  When the difference exceeds `gradual_threshold` (if set) a
/// gradual transition is recorded instead.
pub struct ThresholdSceneDetector {
    /// Difference threshold for a hard cut (mean absolute difference).
    pub threshold: f32,
    /// Optional lower threshold for gradual-transition detection.
    pub gradual_threshold: Option<f32>,
    /// Minimum scene length in frames. Prevents spurious splits on short bursts.
    pub min_scene_length: u64,
}

impl ThresholdSceneDetector {
    /// Create a detector with a hard-cut threshold.
    #[must_use]
    pub fn new(threshold: f32) -> Self {
        Self {
            threshold,
            gradual_threshold: None,
            min_scene_length: 1,
        }
    }

    /// Set the gradual-transition threshold (must be < `threshold`).
    #[must_use]
    pub fn with_gradual_threshold(mut self, t: f32) -> Self {
        self.gradual_threshold = Some(t);
        self
    }

    /// Set the minimum scene length in frames.
    #[must_use]
    pub fn with_min_scene_length(mut self, frames: u64) -> Self {
        self.min_scene_length = frames.max(1);
        self
    }

    /// Run detection over a sequence of frames.
    ///
    /// Each element of `frames` is a flat slice of pixel values (e.g., a
    /// row-major RGB image scaled to `[0.0, 1.0]`).  The slices must all be
    /// the same length; mismatched lengths are skipped.
    #[must_use]
    pub fn detect(&self, frames: &[Vec<f32>]) -> Vec<SceneBoundary> {
        if frames.len() < 2 {
            return Vec::new();
        }
        let mut boundaries = Vec::new();
        // `None` means no boundary has been found yet; the min_scene_length
        // filter only applies *after* the first boundary is recorded.
        let mut last_boundary: Option<u64> = None;

        for i in 1..frames.len() {
            let a = &frames[i - 1];
            let b = &frames[i];
            if a.len() != b.len() || a.is_empty() {
                continue;
            }
            let n = a.len() as f32;
            let mad: f32 = a
                .iter()
                .zip(b.iter())
                .map(|(x, y)| (x - y).abs())
                .sum::<f32>()
                / n;

            let frame = i as u64;
            if let Some(prev) = last_boundary {
                if (frame - prev) < self.min_scene_length {
                    continue;
                }
            }

            let transition = if mad >= self.threshold {
                TransitionType::Cut
            } else if let Some(gt) = self.gradual_threshold {
                if mad >= gt {
                    TransitionType::Gradual
                } else {
                    continue;
                }
            } else {
                continue;
            };

            boundaries.push(SceneBoundary {
                frame_number: frame,
                score: mad,
                transition,
            });
            last_boundary = Some(frame);
        }
        boundaries
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// HistogramSceneDetector
// ─────────────────────────────────────────────────────────────────────────────

/// Histogram-intersection-based scene detector.
///
/// Each frame is represented as a pre-computed histogram (a flat slice of
/// bin counts or frequencies summing to approximately 1.0).  Histogram
/// intersection is used to measure similarity between successive frames:
///
/// ```text
///   intersection(H1, H2) = Σ min(H1[i], H2[i])
/// ```
///
/// Values close to 1.0 mean nearly identical histograms; values close to 0.0
/// mean very different histograms. A scene boundary is detected when
/// `intersection < (1 - threshold)`.
pub struct HistogramSceneDetector {
    /// A value in `[0.0, 1.0]`. Boundary detected when
    /// `intersection < (1 - threshold)`.
    pub threshold: f32,
    /// Minimum scene length in frames.
    pub min_scene_length: u64,
}

impl HistogramSceneDetector {
    /// Create a detector with the given threshold.
    #[must_use]
    pub fn new(threshold: f32) -> Self {
        Self {
            threshold: threshold.clamp(0.0, 1.0),
            min_scene_length: 1,
        }
    }

    /// Set the minimum scene length in frames.
    #[must_use]
    pub fn with_min_scene_length(mut self, frames: u64) -> Self {
        self.min_scene_length = frames.max(1);
        self
    }

    /// Run detection over a sequence of normalised histograms.
    #[must_use]
    pub fn detect(&self, histograms: &[Vec<f32>]) -> Vec<SceneBoundary> {
        if histograms.len() < 2 {
            return Vec::new();
        }
        let mut boundaries = Vec::new();
        let mut last_boundary: u64 = 0;
        let min_intersection = 1.0 - self.threshold;

        for i in 1..histograms.len() {
            let h1 = &histograms[i - 1];
            let h2 = &histograms[i];
            if h1.len() != h2.len() || h1.is_empty() {
                continue;
            }
            let intersection: f32 = h1.iter().zip(h2.iter()).map(|(a, b)| a.min(*b)).sum();

            let frame = i as u64;
            if (frame - last_boundary) < self.min_scene_length {
                continue;
            }

            if intersection < min_intersection {
                boundaries.push(SceneBoundary {
                    frame_number: frame,
                    score: 1.0 - intersection,
                    transition: TransitionType::Cut,
                });
                last_boundary = frame;
            }
        }
        boundaries
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Tests
// ─────────────────────────────────────────────────────────────────────────────

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

    // ─── ThresholdSceneDetector ──────────────────────────────────────────────

    #[test]
    fn test_threshold_no_boundaries_same_frames() {
        let frames = vec![vec![0.5_f32; 8]; 10];
        let det = ThresholdSceneDetector::new(0.3);
        assert!(det.detect(&frames).is_empty());
    }

    #[test]
    fn test_threshold_hard_cut_detected() {
        let mut frames: Vec<Vec<f32>> = vec![vec![0.0_f32; 16]; 3];
        frames.push(vec![1.0_f32; 16]); // abrupt change at frame 3
        frames.push(vec![1.0_f32; 16]);
        let det = ThresholdSceneDetector::new(0.3);
        let bd = det.detect(&frames);
        assert_eq!(bd.len(), 1);
        assert_eq!(bd[0].frame_number, 3);
        assert_eq!(bd[0].transition, TransitionType::Cut);
    }

    #[test]
    fn test_threshold_gradual_detected() {
        let frames = vec![
            vec![0.0_f32; 8],
            vec![0.1_f32; 8], // gradual
            vec![1.0_f32; 8], // hard cut
        ];
        let det = ThresholdSceneDetector::new(0.5).with_gradual_threshold(0.08);
        let bd = det.detect(&frames);
        // gradual at 1, cut at 2
        assert_eq!(bd.len(), 2);
        assert_eq!(bd[0].transition, TransitionType::Gradual);
        assert_eq!(bd[1].transition, TransitionType::Cut);
    }

    #[test]
    fn test_threshold_min_scene_length_suppresses_spurious() {
        let mut frames: Vec<Vec<f32>> = vec![vec![0.0_f32; 8]; 2];
        frames.push(vec![1.0_f32; 8]); // cut at 2
        frames.push(vec![0.0_f32; 8]); // would be cut at 3 — within min_scene_length=3
        frames.push(vec![1.0_f32; 8]);
        let det = ThresholdSceneDetector::new(0.3).with_min_scene_length(3);
        let bd = det.detect(&frames);
        // Only the first cut (frame 2) should be detected; frame 3 is within
        // the 3-frame minimum.
        assert_eq!(bd[0].frame_number, 2);
    }

    #[test]
    fn test_threshold_empty_input() {
        let det = ThresholdSceneDetector::new(0.3);
        assert!(det.detect(&[]).is_empty());
        assert!(det.detect(&[vec![0.0_f32]]).is_empty());
    }

    // ─── HistogramSceneDetector ──────────────────────────────────────────────

    #[test]
    fn test_histogram_no_boundaries() {
        // Identical histograms → no boundaries.
        let hist = vec![vec![0.25_f32; 4]; 5];
        let det = HistogramSceneDetector::new(0.3);
        assert!(det.detect(&hist).is_empty());
    }

    #[test]
    fn test_histogram_cut_detected() {
        // Frame 0: all-red histogram; frame 1: all-blue histogram.
        let hist = vec![vec![1.0_f32, 0.0, 0.0, 0.0], vec![0.0_f32, 0.0, 0.0, 1.0]];
        let det = HistogramSceneDetector::new(0.3);
        let bd = det.detect(&hist);
        assert_eq!(bd.len(), 1);
        assert_eq!(bd[0].frame_number, 1);
    }

    // ─── boundaries_to_subclips ──────────────────────────────────────────────

    #[test]
    fn test_no_boundaries_single_subclip() {
        let subs = boundaries_to_subclips(&[], 100);
        assert_eq!(subs.len(), 1);
        assert_eq!(subs[0].start_frame, 0);
        assert_eq!(subs[0].end_frame, 99);
    }

    #[test]
    fn test_one_boundary_two_subclips() {
        let bd = vec![SceneBoundary {
            frame_number: 50,
            score: 0.9,
            transition: TransitionType::Cut,
        }];
        let subs = boundaries_to_subclips(&bd, 100);
        assert_eq!(subs.len(), 2);
        assert_eq!(subs[0].start_frame, 0);
        assert_eq!(subs[0].end_frame, 49);
        assert_eq!(subs[1].start_frame, 50);
        assert_eq!(subs[1].end_frame, 99);
    }

    #[test]
    fn test_zero_frames_empty_output() {
        let subs = boundaries_to_subclips(&[], 0);
        assert!(subs.is_empty());
    }

    #[test]
    fn test_subclip_labels() {
        let bd = vec![
            SceneBoundary {
                frame_number: 30,
                score: 0.5,
                transition: TransitionType::Cut,
            },
            SceneBoundary {
                frame_number: 60,
                score: 0.5,
                transition: TransitionType::Cut,
            },
        ];
        let subs = boundaries_to_subclips(&bd, 90);
        assert_eq!(subs[0].label, "Scene 1");
        assert_eq!(subs[1].label, "Scene 2");
        assert_eq!(subs[2].label, "Scene 3");
    }
}