oximedia-cv 0.1.3

Computer vision 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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
//! Video scene detection and shot boundary detection.
//!
//! This module provides algorithms for detecting scene changes and shot boundaries
//! in video sequences. It supports various detection methods including:
//!
//! - **Histogram-based**: Compares color/intensity histograms between frames
//! - **Edge-based**: Detects changes in edge patterns
//! - **Motion-based**: Analyzes motion vectors and flow
//! - **Adaptive**: Uses adaptive thresholding for varying content
//!
//! # Scene Changes
//!
//! Scene changes can be:
//! - **Hard cuts**: Abrupt transitions between scenes
//! - **Gradual transitions**: Fades, dissolves, wipes
//!
//! # Example
//!
//! ```
//! use oximedia_cv::scene::{SceneDetector, SceneConfig, DetectionMethod};
//! use oximedia_codec::VideoFrame;
//!
//! let config = SceneConfig::default()
//!     .with_threshold(0.3)
//!     .with_method(DetectionMethod::Histogram);
//!
//! let detector = SceneDetector::new(config);
//! // let changes = detector.detect_scenes(&frames);
//! ```

pub mod adaptive;
pub mod classification;
pub mod edge;
pub mod histogram;
pub mod motion;
pub mod transition_detect;

use crate::error::{CvError, CvResult};
use oximedia_codec::VideoFrame;
use oximedia_core::Timestamp;

/// Type of scene change detected.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ChangeType {
    /// Hard cut - abrupt transition.
    Cut,
    /// Fade in or out.
    Fade,
    /// Dissolve transition.
    Dissolve,
    /// Unknown gradual transition.
    GradualUnknown,
}

impl ChangeType {
    /// Check if this is a gradual transition.
    #[must_use]
    pub const fn is_gradual(&self) -> bool {
        matches!(self, Self::Fade | Self::Dissolve | Self::GradualUnknown)
    }

    /// Check if this is a hard cut.
    #[must_use]
    pub const fn is_cut(&self) -> bool {
        matches!(self, Self::Cut)
    }
}

/// A detected scene change.
#[derive(Debug, Clone)]
pub struct SceneChange {
    /// Frame number where the change occurs.
    pub frame_number: usize,
    /// Timestamp of the scene change.
    pub timestamp: Timestamp,
    /// Confidence score (0.0 to 1.0).
    pub confidence: f64,
    /// Type of change detected.
    pub change_type: ChangeType,
    /// Additional metadata about the detection.
    pub metadata: SceneMetadata,
}

impl SceneChange {
    /// Create a new scene change.
    #[must_use]
    pub fn new(
        frame_number: usize,
        timestamp: Timestamp,
        confidence: f64,
        change_type: ChangeType,
    ) -> Self {
        Self {
            frame_number,
            timestamp,
            confidence: confidence.clamp(0.0, 1.0),
            change_type,
            metadata: SceneMetadata::default(),
        }
    }

    /// Check if this change meets a minimum confidence threshold.
    #[must_use]
    pub fn meets_threshold(&self, threshold: f64) -> bool {
        self.confidence >= threshold
    }
}

/// Metadata about a scene detection.
#[derive(Debug, Clone, Default)]
pub struct SceneMetadata {
    /// Histogram difference score.
    pub histogram_diff: Option<f64>,
    /// Edge change ratio.
    pub edge_change_ratio: Option<f64>,
    /// Motion score.
    pub motion_score: Option<f64>,
    /// Average color difference.
    pub color_diff: Option<f64>,
    /// Duration of gradual transition (in frames).
    pub transition_duration: Option<usize>,
}

/// Detection method to use.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DetectionMethod {
    /// Histogram-based detection (RGB).
    Histogram,
    /// Histogram-based detection (HSV color space).
    HistogramHsv,
    /// Edge-based detection.
    Edge,
    /// Motion-based detection.
    Motion,
    /// Adaptive threshold (combines multiple methods).
    Adaptive,
    /// Hybrid approach using multiple methods.
    Hybrid,
}

/// Configuration for scene detection.
#[derive(Debug, Clone)]
pub struct SceneConfig {
    /// Detection method to use.
    pub method: DetectionMethod,
    /// Threshold for scene change detection (0.0 to 1.0).
    pub threshold: f64,
    /// Minimum frames between scene changes (to avoid false positives).
    pub min_scene_length: usize,
    /// Enable gradual transition detection.
    pub detect_gradual: bool,
    /// Window size for gradual transition detection.
    pub gradual_window: usize,
    /// Threshold for gradual transitions (lower than hard cuts).
    pub gradual_threshold: f64,
    /// Enable temporal coherence analysis.
    pub use_temporal_coherence: bool,
    /// Adaptive threshold settings.
    pub adaptive_config: adaptive::AdaptiveConfig,
    /// Histogram settings.
    pub histogram_config: histogram::HistogramConfig,
    /// Edge detection settings.
    pub edge_config: edge::EdgeConfig,
    /// Motion detection settings.
    pub motion_config: motion::MotionConfig,
}

impl Default for SceneConfig {
    fn default() -> Self {
        Self {
            method: DetectionMethod::Histogram,
            threshold: 0.3,
            min_scene_length: 15, // ~0.5 seconds at 30fps
            detect_gradual: true,
            gradual_window: 10,
            gradual_threshold: 0.15,
            use_temporal_coherence: true,
            adaptive_config: adaptive::AdaptiveConfig::default(),
            histogram_config: histogram::HistogramConfig::default(),
            edge_config: edge::EdgeConfig::default(),
            motion_config: motion::MotionConfig::default(),
        }
    }
}

impl SceneConfig {
    /// Create a new scene configuration.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the detection method.
    #[must_use]
    pub const fn with_method(mut self, method: DetectionMethod) -> Self {
        self.method = method;
        self
    }

    /// Set the threshold.
    #[must_use]
    pub const fn with_threshold(mut self, threshold: f64) -> Self {
        self.threshold = threshold;
        self
    }

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

    /// Enable or disable gradual transition detection.
    #[must_use]
    pub const fn with_detect_gradual(mut self, detect_gradual: bool) -> Self {
        self.detect_gradual = detect_gradual;
        self
    }

    /// Set the gradual transition window size.
    #[must_use]
    pub const fn with_gradual_window(mut self, window: usize) -> Self {
        self.gradual_window = window;
        self
    }

    /// Set temporal coherence analysis.
    #[must_use]
    pub const fn with_temporal_coherence(mut self, enabled: bool) -> Self {
        self.use_temporal_coherence = enabled;
        self
    }

    /// Validate the configuration.
    pub fn validate(&self) -> CvResult<()> {
        if self.threshold < 0.0 || self.threshold > 1.0 {
            return Err(CvError::invalid_parameter(
                "threshold",
                format!("{} (must be 0.0-1.0)", self.threshold),
            ));
        }

        if self.gradual_threshold < 0.0 || self.gradual_threshold > 1.0 {
            return Err(CvError::invalid_parameter(
                "gradual_threshold",
                format!("{} (must be 0.0-1.0)", self.gradual_threshold),
            ));
        }

        if self.min_scene_length == 0 {
            return Err(CvError::invalid_parameter(
                "min_scene_length",
                "must be greater than 0",
            ));
        }

        if self.gradual_window == 0 {
            return Err(CvError::invalid_parameter(
                "gradual_window",
                "must be greater than 0",
            ));
        }

        Ok(())
    }
}

/// Scene detector for video analysis.
pub struct SceneDetector {
    /// Configuration.
    config: SceneConfig,
}

impl SceneDetector {
    /// Create a new scene detector with the given configuration.
    ///
    /// # Examples
    ///
    /// ```
    /// use oximedia_cv::scene::{SceneDetector, SceneConfig};
    ///
    /// let config = SceneConfig::default();
    /// let detector = SceneDetector::new(config);
    /// ```
    #[must_use]
    pub fn new(config: SceneConfig) -> Self {
        Self { config }
    }

    /// Create a scene detector with default configuration.
    #[must_use]
    pub fn default_detector() -> Self {
        Self::new(SceneConfig::default())
    }

    /// Get the current configuration.
    #[must_use]
    pub const fn config(&self) -> &SceneConfig {
        &self.config
    }

    /// Detect scene changes in a sequence of video frames.
    ///
    /// # Arguments
    ///
    /// * `frames` - Slice of video frames to analyze
    ///
    /// # Returns
    ///
    /// Vector of detected scene changes.
    ///
    /// # Errors
    ///
    /// Returns an error if the configuration is invalid or frame processing fails.
    pub fn detect_scenes(&self, frames: &[VideoFrame]) -> CvResult<Vec<SceneChange>> {
        self.config.validate()?;

        if frames.len() < 2 {
            return Ok(Vec::new());
        }

        let mut changes = match self.config.method {
            DetectionMethod::Histogram => self.detect_histogram(frames)?,
            DetectionMethod::HistogramHsv => self.detect_histogram_hsv(frames)?,
            DetectionMethod::Edge => self.detect_edge(frames)?,
            DetectionMethod::Motion => self.detect_motion(frames)?,
            DetectionMethod::Adaptive => self.detect_adaptive(frames)?,
            DetectionMethod::Hybrid => self.detect_hybrid(frames)?,
        };

        // Post-process: enforce minimum scene length
        changes = self.filter_min_scene_length(changes);

        // Post-process: temporal coherence
        if self.config.use_temporal_coherence {
            changes = self.apply_temporal_coherence(changes);
        }

        // Detect gradual transitions if enabled
        if self.config.detect_gradual {
            let gradual = self.detect_gradual_transitions(frames)?;
            changes.extend(gradual);
            changes.sort_by_key(|c| c.frame_number);
        }

        Ok(changes)
    }

    /// Detect scenes using histogram-based method (RGB).
    fn detect_histogram(&self, frames: &[VideoFrame]) -> CvResult<Vec<SceneChange>> {
        histogram::detect_histogram_changes(frames, &self.config)
    }

    /// Detect scenes using histogram-based method (HSV).
    fn detect_histogram_hsv(&self, frames: &[VideoFrame]) -> CvResult<Vec<SceneChange>> {
        histogram::detect_histogram_hsv_changes(frames, &self.config)
    }

    /// Detect scenes using edge-based method.
    fn detect_edge(&self, frames: &[VideoFrame]) -> CvResult<Vec<SceneChange>> {
        edge::detect_edge_changes(frames, &self.config)
    }

    /// Detect scenes using motion-based method.
    fn detect_motion(&self, frames: &[VideoFrame]) -> CvResult<Vec<SceneChange>> {
        motion::detect_motion_changes(frames, &self.config)
    }

    /// Detect scenes using adaptive method.
    fn detect_adaptive(&self, frames: &[VideoFrame]) -> CvResult<Vec<SceneChange>> {
        adaptive::detect_adaptive_changes(frames, &self.config)
    }

    /// Detect scenes using hybrid method.
    fn detect_hybrid(&self, frames: &[VideoFrame]) -> CvResult<Vec<SceneChange>> {
        let hist_changes = self.detect_histogram(frames)?;
        let edge_changes = self.detect_edge(frames)?;
        let motion_changes = self.detect_motion(frames)?;

        // Combine changes: a frame is a scene change if detected by multiple methods
        let mut combined = Vec::new();
        let all_frames: std::collections::HashSet<usize> = hist_changes
            .iter()
            .chain(edge_changes.iter())
            .chain(motion_changes.iter())
            .map(|c| c.frame_number)
            .collect();

        for frame_num in all_frames {
            let hist_score = hist_changes
                .iter()
                .find(|c| c.frame_number == frame_num)
                .map_or(0.0, |c| c.confidence);

            let edge_score = edge_changes
                .iter()
                .find(|c| c.frame_number == frame_num)
                .map_or(0.0, |c| c.confidence);

            let motion_score = motion_changes
                .iter()
                .find(|c| c.frame_number == frame_num)
                .map_or(0.0, |c| c.confidence);

            // Vote count: how many methods detected this change
            let vote_count =
                (hist_score > 0.0) as u32 + (edge_score > 0.0) as u32 + (motion_score > 0.0) as u32;

            // Require at least 2 out of 3 methods to agree
            if vote_count >= 2 {
                let avg_confidence = (hist_score + edge_score + motion_score) / 3.0;

                combined.push(SceneChange {
                    frame_number: frame_num,
                    timestamp: frames[frame_num].timestamp,
                    confidence: avg_confidence,
                    change_type: ChangeType::Cut,
                    metadata: SceneMetadata {
                        histogram_diff: Some(hist_score),
                        edge_change_ratio: Some(edge_score),
                        motion_score: Some(motion_score),
                        ..Default::default()
                    },
                });
            }
        }

        combined.sort_by_key(|c| c.frame_number);
        Ok(combined)
    }

    /// Detect gradual transitions (fades, dissolves).
    fn detect_gradual_transitions(&self, frames: &[VideoFrame]) -> CvResult<Vec<SceneChange>> {
        if frames.len() < self.config.gradual_window {
            return Ok(Vec::new());
        }

        let mut gradual_changes = Vec::new();
        let window = self.config.gradual_window;

        for i in 0..frames.len() - window {
            let start_frame = &frames[i];
            let end_frame = &frames[i + window];

            // Compute similarity between start and end of window
            let similarity = histogram::compute_frame_similarity(start_frame, end_frame)?;
            let diff = 1.0 - similarity;

            // Check if there's a gradual change
            if diff > self.config.gradual_threshold && diff < self.config.threshold {
                // Determine transition type
                let change_type = self.classify_gradual_transition(frames, i, i + window)?;

                gradual_changes.push(SceneChange {
                    frame_number: i + window / 2, // Middle of transition
                    timestamp: frames[i + window / 2].timestamp,
                    confidence: diff,
                    change_type,
                    metadata: SceneMetadata {
                        transition_duration: Some(window),
                        histogram_diff: Some(diff),
                        ..Default::default()
                    },
                });
            }
        }

        Ok(gradual_changes)
    }

    /// Classify the type of gradual transition.
    fn classify_gradual_transition(
        &self,
        frames: &[VideoFrame],
        start: usize,
        end: usize,
    ) -> CvResult<ChangeType> {
        // Compute brightness trend
        let start_brightness = histogram::compute_average_brightness(&frames[start])?;
        let end_brightness = histogram::compute_average_brightness(&frames[end])?;

        let brightness_change = (end_brightness - start_brightness).abs();
        let brightness_ratio = if start_brightness > 0.0 {
            brightness_change / start_brightness
        } else {
            0.0
        };

        // If brightness changes significantly, it's likely a fade
        if brightness_ratio > 0.5 {
            return Ok(ChangeType::Fade);
        }

        // Otherwise, it's likely a dissolve
        Ok(ChangeType::Dissolve)
    }

    /// Filter scene changes by minimum scene length.
    fn filter_min_scene_length(&self, changes: Vec<SceneChange>) -> Vec<SceneChange> {
        if changes.is_empty() {
            return changes;
        }

        let mut filtered = Vec::new();
        let mut last_frame = 0;

        for change in changes {
            if change.frame_number - last_frame >= self.config.min_scene_length {
                filtered.push(change.clone());
                last_frame = change.frame_number;
            }
        }

        filtered
    }

    /// Apply temporal coherence to reduce false positives.
    fn apply_temporal_coherence(&self, changes: Vec<SceneChange>) -> Vec<SceneChange> {
        // Simple temporal coherence: remove isolated detections
        if changes.len() < 2 {
            return changes;
        }

        let mut coherent = Vec::new();
        let window = 3; // Look at neighbors within 3 frames

        for (i, change) in changes.iter().enumerate() {
            // Count nearby detections
            let nearby_count = changes
                .iter()
                .enumerate()
                .filter(|(j, c)| {
                    *j != i && (c.frame_number as i64 - change.frame_number as i64).abs() <= window
                })
                .count();

            // Keep if there are nearby detections or if confidence is very high
            if nearby_count > 0 || change.confidence > self.config.threshold * 1.5 {
                coherent.push(change.clone());
            }
        }

        coherent
    }

    /// Analyze a pair of consecutive frames.
    pub fn analyze_frame_pair(
        &self,
        frame1: &VideoFrame,
        frame2: &VideoFrame,
    ) -> CvResult<SceneChange> {
        let similarity = match self.config.method {
            DetectionMethod::Histogram => histogram::compute_frame_similarity(frame1, frame2)?,
            DetectionMethod::HistogramHsv => {
                histogram::compute_frame_similarity_hsv(frame1, frame2)?
            }
            DetectionMethod::Edge => {
                edge::compute_edge_similarity(frame1, frame2, &self.config.edge_config)?
            }
            DetectionMethod::Motion => {
                motion::compute_motion_score(frame1, frame2, &self.config.motion_config)?
            }
            _ => {
                // For adaptive/hybrid, use histogram as default
                histogram::compute_frame_similarity(frame1, frame2)?
            }
        };

        let diff = 1.0 - similarity;
        let change_type = if diff > self.config.threshold {
            ChangeType::Cut
        } else {
            ChangeType::GradualUnknown
        };

        Ok(SceneChange::new(
            0, // Frame number unknown in pair analysis
            frame2.timestamp,
            diff,
            change_type,
        ))
    }
}

impl Default for SceneDetector {
    fn default() -> Self {
        Self::default_detector()
    }
}

/// Compute the difference between two frames using the specified method.
///
/// Returns a value between 0.0 (identical) and 1.0 (completely different).
pub fn compute_frame_difference(
    frame1: &VideoFrame,
    frame2: &VideoFrame,
    method: DetectionMethod,
) -> CvResult<f64> {
    let similarity = match method {
        DetectionMethod::Histogram => histogram::compute_frame_similarity(frame1, frame2)?,
        DetectionMethod::HistogramHsv => histogram::compute_frame_similarity_hsv(frame1, frame2)?,
        DetectionMethod::Edge => {
            let config = edge::EdgeConfig::default();
            edge::compute_edge_similarity(frame1, frame2, &config)?
        }
        DetectionMethod::Motion => {
            let config = motion::MotionConfig::default();
            motion::compute_motion_score(frame1, frame2, &config)?
        }
        DetectionMethod::Adaptive | DetectionMethod::Hybrid => {
            // Use histogram as default
            histogram::compute_frame_similarity(frame1, frame2)?
        }
    };

    Ok(1.0 - similarity)
}