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
//! Comb detection for interlaced video.
//!
//! This module implements various algorithms for detecting combing artifacts
//! that appear in interlaced video content. Combing occurs when the two fields
//! of an interlaced frame contain motion, creating a "comb teeth" pattern along
//! moving edges.

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

use super::metrics::InterlaceMetrics;

/// Configuration for comb detection.
#[derive(Debug, Clone)]
pub struct CombDetectorConfig {
    /// Minimum difference threshold for detecting combing (0-255).
    pub threshold: u8,
    /// Spatial search radius for comb pattern detection.
    pub spatial_radius: usize,
    /// Minimum comb length (consecutive pixels) to consider valid.
    pub min_comb_length: usize,
    /// Edge detection threshold for focused analysis.
    pub edge_threshold: u8,
    /// Enable FFT-based frequency analysis.
    pub enable_fft: bool,
}

impl CombDetectorConfig {
    /// Creates a new configuration with default values.
    #[must_use]
    pub const fn new() -> Self {
        Self {
            threshold: 10,
            spatial_radius: 3,
            min_comb_length: 4,
            edge_threshold: 30,
            enable_fft: false,
        }
    }

    /// Creates a configuration optimized for sensitivity (detects more combing).
    #[must_use]
    pub const fn sensitive() -> Self {
        Self {
            threshold: 5,
            spatial_radius: 4,
            min_comb_length: 3,
            edge_threshold: 20,
            enable_fft: true,
        }
    }

    /// Creates a configuration optimized for specificity (fewer false positives).
    #[must_use]
    pub const fn specific() -> Self {
        Self {
            threshold: 15,
            spatial_radius: 2,
            min_comb_length: 5,
            edge_threshold: 40,
            enable_fft: false,
        }
    }
}

impl Default for CombDetectorConfig {
    fn default() -> Self {
        Self::new()
    }
}

/// Comb detector for interlaced video analysis.
pub struct CombDetector {
    config: CombDetectorConfig,
}

impl CombDetector {
    /// Creates a new comb detector with the given configuration.
    #[must_use]
    pub const fn new(config: CombDetectorConfig) -> Self {
        Self { config }
    }

    /// Creates a new comb detector with default configuration.
    #[must_use]
    pub fn with_defaults() -> Self {
        Self::new(CombDetectorConfig::default())
    }

    /// Detects combing artifacts in a single frame.
    ///
    /// Returns metrics quantifying the amount of combing detected.
    pub fn detect(&self, frame: &VideoFrame) -> CvResult<InterlaceMetrics> {
        if frame.width == 0 || frame.height == 0 {
            return Err(CvError::invalid_dimensions(frame.width, frame.height));
        }

        if frame.planes.is_empty() {
            return Err(CvError::insufficient_data(1, 0));
        }

        // Analyze luma plane (most visible combing)
        let luma = &frame.planes[0];
        let width = frame.width as usize;
        let height = frame.height as usize;

        // Calculate various comb metrics
        let comb_score = self.calculate_comb_score(luma, width, height)?;
        let field_diff = self.calculate_field_difference(luma, width, height)?;
        let spatial_comb = self.calculate_spatial_comb(luma, width, height)?;
        let temporal_comb = 0.0; // Requires multiple frames
        let edge_comb = self.calculate_edge_comb(luma, width, height)?;

        Ok(InterlaceMetrics::from_components(
            comb_score,
            field_diff,
            spatial_comb,
            temporal_comb,
            edge_comb,
        ))
    }

    /// Detects combing using temporal information from multiple frames.
    ///
    /// This provides more accurate detection by comparing consecutive frames.
    pub fn detect_temporal(&self, frames: &[VideoFrame]) -> CvResult<InterlaceMetrics> {
        if frames.is_empty() {
            return Err(CvError::insufficient_data(1, 0));
        }

        if frames.len() < 2 {
            // Fall back to single-frame detection
            return self.detect(&frames[0]);
        }

        // Analyze the most recent frame
        let current = &frames[frames.len() - 1];
        let mut metrics = self.detect(current)?;

        // Calculate temporal combing by comparing with previous frame
        let previous = &frames[frames.len() - 2];
        metrics.temporal_comb = self.calculate_temporal_comb(previous, current)?;

        Ok(metrics)
    }

    /// Calculates the basic comb score by detecting line-to-line differences.
    fn calculate_comb_score(
        &self,
        plane: &oximedia_codec::Plane,
        width: usize,
        height: usize,
    ) -> CvResult<f64> {
        if height < 3 {
            return Ok(0.0);
        }

        let mut comb_pixels = 0;
        let mut total_pixels = 0;

        for y in 1..height - 1 {
            let curr_row = plane.row(y);
            let prev_row = plane.row(y - 1);
            let next_row = plane.row(y + 1);

            if curr_row.len() < width || prev_row.len() < width || next_row.len() < width {
                continue;
            }

            for x in 0..width {
                let curr = i32::from(curr_row[x]);
                let prev = i32::from(prev_row[x]);
                let next = i32::from(next_row[x]);

                // Check for comb pattern: current line differs from neighbors
                let diff_prev = (curr - prev).abs();
                let diff_next = (curr - next).abs();

                // Both differences should be significant for combing
                if diff_prev > i32::from(self.config.threshold)
                    && diff_next > i32::from(self.config.threshold)
                {
                    // Check if neighbors are similar (typical of combing)
                    if (prev - next).abs() < i32::from(self.config.threshold) {
                        comb_pixels += 1;
                    }
                }

                total_pixels += 1;
            }
        }

        if total_pixels == 0 {
            return Ok(0.0);
        }

        Ok(comb_pixels as f64 / total_pixels as f64)
    }

    /// Calculates field difference metric.
    ///
    /// Separates even and odd lines (fields) and measures their difference.
    fn calculate_field_difference(
        &self,
        plane: &oximedia_codec::Plane,
        width: usize,
        height: usize,
    ) -> CvResult<f64> {
        if height < 4 {
            return Ok(0.0);
        }

        let mut diff_sum = 0i64;
        let mut count = 0;

        // Compare odd field lines with interpolated even field
        for y in (2..height - 2).step_by(2) {
            let curr_row = plane.row(y);
            let prev_row = plane.row(y - 1);
            let next_row = plane.row(y + 1);

            if curr_row.len() < width || prev_row.len() < width || next_row.len() < width {
                continue;
            }

            for x in 0..width {
                let actual = i32::from(curr_row[x]);
                // Interpolate from neighboring field
                let interpolated = (i32::from(prev_row[x]) + i32::from(next_row[x])) / 2;
                let diff = (actual - interpolated).abs();

                diff_sum += i64::from(diff);
                count += 1;
            }
        }

        if count == 0 {
            return Ok(0.0);
        }

        // Normalize to 0.0-1.0 range
        let avg_diff = diff_sum as f64 / count as f64;
        Ok((avg_diff / 255.0).clamp(0.0, 1.0))
    }

    /// Calculates spatial comb metric using local pattern analysis.
    fn calculate_spatial_comb(
        &self,
        plane: &oximedia_codec::Plane,
        width: usize,
        height: usize,
    ) -> CvResult<f64> {
        let radius = self.config.spatial_radius;
        if height < radius * 2 + 1 || width < radius * 2 + 1 {
            return Ok(0.0);
        }

        let mut comb_strength = 0.0;
        let mut count = 0;

        for y in radius..height - radius {
            let row = plane.row(y);
            if row.len() < width {
                continue;
            }

            for x in radius..width - radius {
                let center = f64::from(row[x]);

                // Calculate local variance in vertical direction
                let mut vertical_variance = 0.0;
                for dy in 1..=radius {
                    let up_row = plane.row(y.saturating_sub(dy));
                    let down_row = plane.row((y + dy).min(height - 1));

                    if up_row.len() > x && down_row.len() > x {
                        let up = f64::from(up_row[x]);
                        let down = f64::from(down_row[x]);
                        vertical_variance += (center - up).abs() + (center - down).abs();
                    }
                }

                // Calculate local variance in horizontal direction
                let mut horizontal_variance = 0.0;
                for dx in 1..=radius {
                    let left = f64::from(row[x.saturating_sub(dx)]);
                    let right = f64::from(row[(x + dx).min(width - 1)]);
                    horizontal_variance += (center - left).abs() + (center - right).abs();
                }

                // Combing shows high vertical variance compared to horizontal
                if horizontal_variance > 0.0 {
                    let ratio = vertical_variance / horizontal_variance;
                    if ratio > 1.5 {
                        comb_strength += ratio;
                        count += 1;
                    }
                }
            }
        }

        if count == 0 {
            return Ok(0.0);
        }

        // Normalize the result
        let avg_strength = comb_strength / count as f64;
        Ok((avg_strength / 10.0).clamp(0.0, 1.0))
    }

    /// Calculates edge-focused comb metric.
    ///
    /// Focuses analysis on edges where combing is most visible.
    fn calculate_edge_comb(
        &self,
        plane: &oximedia_codec::Plane,
        width: usize,
        height: usize,
    ) -> CvResult<f64> {
        if height < 3 || width < 3 {
            return Ok(0.0);
        }

        let mut edge_comb_pixels = 0;
        let mut edge_pixels = 0;

        for y in 1..height - 1 {
            let curr_row = plane.row(y);
            let prev_row = plane.row(y - 1);
            let next_row = plane.row(y + 1);

            if curr_row.len() < width || prev_row.len() < width || next_row.len() < width {
                continue;
            }

            for x in 1..width - 1 {
                // Simple edge detection using Sobel-like operator
                let center = i32::from(curr_row[x]);
                let left = i32::from(curr_row[x - 1]);
                let right = i32::from(curr_row[x + 1]);
                let up = i32::from(prev_row[x]);
                let down = i32::from(next_row[x]);

                let edge_strength = ((right - left).abs() + (down - up).abs()) / 2;

                if edge_strength > i32::from(self.config.edge_threshold) {
                    edge_pixels += 1;

                    // Check for combing at this edge
                    let vertical_diff = (center - up).abs() + (center - down).abs();
                    if vertical_diff > i32::from(self.config.threshold) * 2 {
                        edge_comb_pixels += 1;
                    }
                }
            }
        }

        if edge_pixels == 0 {
            return Ok(0.0);
        }

        Ok(edge_comb_pixels as f64 / edge_pixels as f64)
    }

    /// Calculates temporal comb metric by comparing consecutive frames.
    fn calculate_temporal_comb(
        &self,
        prev_frame: &VideoFrame,
        curr_frame: &VideoFrame,
    ) -> CvResult<f64> {
        if prev_frame.width != curr_frame.width || prev_frame.height != curr_frame.height {
            return Ok(0.0);
        }

        if prev_frame.planes.is_empty() || curr_frame.planes.is_empty() {
            return Ok(0.0);
        }

        let width = curr_frame.width as usize;
        let height = curr_frame.height as usize;

        if height < 4 {
            return Ok(0.0);
        }

        let prev_plane = &prev_frame.planes[0];
        let curr_plane = &curr_frame.planes[0];

        let mut comb_pixels = 0;
        let mut motion_pixels = 0;

        // Check odd and even fields separately
        for field in 0..2 {
            for y in (field + 1..height - 1).step_by(2) {
                let curr_row = curr_plane.row(y);
                let prev_row = prev_plane.row(y);

                if curr_row.len() < width || prev_row.len() < width {
                    continue;
                }

                for x in 0..width {
                    let diff = (i32::from(curr_row[x]) - i32::from(prev_row[x])).abs();

                    // Motion detected
                    if diff > i32::from(self.config.threshold) {
                        motion_pixels += 1;

                        // Check if neighboring line (other field) also changed
                        if y > 0 && y < height - 1 {
                            let curr_neighbor = curr_plane.row(y - 1);
                            let prev_neighbor = prev_plane.row(y - 1);

                            if curr_neighbor.len() > x && prev_neighbor.len() > x {
                                let neighbor_diff = (i32::from(curr_neighbor[x])
                                    - i32::from(prev_neighbor[x]))
                                .abs();

                                // If motion is different between fields, likely combing
                                if (diff - neighbor_diff).abs() > i32::from(self.config.threshold) {
                                    comb_pixels += 1;
                                }
                            }
                        }
                    }
                }
            }
        }

        if motion_pixels == 0 {
            return Ok(0.0);
        }

        Ok(comb_pixels as f64 / motion_pixels as f64)
    }

    /// Generates a comb map showing where combing was detected.
    ///
    /// Returns a binary map where 255 indicates combing and 0 indicates no combing.
    pub fn generate_comb_map(&self, frame: &VideoFrame) -> CvResult<Vec<u8>> {
        if frame.planes.is_empty() {
            return Err(CvError::insufficient_data(1, 0));
        }

        let luma = &frame.planes[0];
        let width = frame.width as usize;
        let height = frame.height as usize;

        let mut comb_map = vec![0u8; width * height];

        if height < 3 {
            return Ok(comb_map);
        }

        for y in 1..height - 1 {
            let curr_row = luma.row(y);
            let prev_row = luma.row(y - 1);
            let next_row = luma.row(y + 1);

            if curr_row.len() < width || prev_row.len() < width || next_row.len() < width {
                continue;
            }

            for x in 0..width {
                let curr = i32::from(curr_row[x]);
                let prev = i32::from(prev_row[x]);
                let next = i32::from(next_row[x]);

                let diff_prev = (curr - prev).abs();
                let diff_next = (curr - next).abs();

                if diff_prev > i32::from(self.config.threshold)
                    && diff_next > i32::from(self.config.threshold)
                    && (prev - next).abs() < i32::from(self.config.threshold)
                {
                    comb_map[y * width + x] = 255;
                }
            }
        }

        Ok(comb_map)
    }

    /// Detects comb patterns with length filtering.
    ///
    /// Only reports combing that extends for at least `min_comb_length` consecutive pixels.
    pub fn detect_comb_patterns(&self, frame: &VideoFrame) -> CvResult<Vec<CombPattern>> {
        let comb_map = self.generate_comb_map(frame)?;
        let width = frame.width as usize;
        let height = frame.height as usize;

        let mut patterns = Vec::new();

        for y in 0..height {
            let mut run_start = None;

            for x in 0..width {
                let is_comb = comb_map[y * width + x] > 0;

                match (is_comb, run_start) {
                    (true, None) => {
                        run_start = Some(x);
                    }
                    (false, Some(start)) => {
                        let length = x - start;
                        if length >= self.config.min_comb_length {
                            patterns.push(CombPattern {
                                y,
                                x_start: start,
                                x_end: x,
                                length,
                            });
                        }
                        run_start = None;
                    }
                    _ => {}
                }
            }

            // Handle pattern at end of row
            if let Some(start) = run_start {
                let length = width - start;
                if length >= self.config.min_comb_length {
                    patterns.push(CombPattern {
                        y,
                        x_start: start,
                        x_end: width,
                        length,
                    });
                }
            }
        }

        Ok(patterns)
    }
}

/// Represents a detected comb pattern in a frame.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CombPattern {
    /// Y coordinate (row) of the pattern.
    pub y: usize,
    /// Starting X coordinate.
    pub x_start: usize,
    /// Ending X coordinate.
    pub x_end: usize,
    /// Length of the comb pattern in pixels.
    pub length: usize,
}

impl CombPattern {
    /// Returns the center point of the comb pattern.
    #[must_use]
    pub const fn center(&self) -> (usize, usize) {
        ((self.x_start + self.x_end) / 2, self.y)
    }
}