oximedia-codec 0.1.7

Video codec implementations 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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
//! Frame complexity estimation.
//!
//! This module provides methods for estimating frame complexity, which is
//! essential for accurate rate control. Complexity metrics include:
//!
//! - Spatial complexity (texture/detail)
//! - Temporal complexity (motion)
//! - Combined metrics for rate control decisions

#![allow(clippy::cast_lossless)]
#![allow(clippy::cast_precision_loss)]
#![allow(clippy::cast_possible_truncation)]
#![allow(clippy::cast_sign_loss)]
#![allow(clippy::manual_let_else)]
#![allow(clippy::used_underscore_binding)]
#![allow(clippy::needless_range_loop)]
#![allow(clippy::manual_clamp)]
#![forbid(unsafe_code)]

/// Frame complexity estimator.
///
/// Estimates spatial and temporal complexity of video frames to guide
/// rate control decisions.
#[derive(Clone, Debug)]
pub struct ComplexityEstimator {
    /// Frame width.
    width: u32,
    /// Frame height.
    height: u32,
    /// Block size for analysis.
    block_size: u32,
    /// Running average of spatial complexity.
    avg_spatial: f32,
    /// Running average of temporal complexity.
    avg_temporal: f32,
    /// Running average of combined complexity.
    avg_combined: f32,
    /// Weight for exponential moving average.
    ema_weight: f32,
    /// Frame counter.
    frame_count: u64,
    /// Previous frame data for temporal analysis.
    prev_frame_data: Option<FrameData>,
}

/// Stored frame data for temporal analysis.
#[derive(Clone, Debug)]
struct FrameData {
    /// Per-block variance values (reserved for motion-compensated analysis).
    #[allow(dead_code)]
    block_variances: Vec<f32>,
    /// Per-block average values (reserved for motion-compensated analysis).
    #[allow(dead_code)]
    block_averages: Vec<f32>,
    /// Total frame variance.
    total_variance: f32,
}

impl ComplexityEstimator {
    /// Create a new complexity estimator.
    #[must_use]
    pub fn new(width: u32, height: u32) -> Self {
        Self {
            width,
            height,
            block_size: 16,
            avg_spatial: 1.0,
            avg_temporal: 1.0,
            avg_combined: 1.0,
            ema_weight: 0.1,
            frame_count: 0,
            prev_frame_data: None,
        }
    }

    /// Set the block size for analysis.
    pub fn set_block_size(&mut self, size: u32) {
        self.block_size = size.max(4).min(64);
    }

    /// Estimate complexity of a frame's luma plane.
    #[must_use]
    pub fn estimate(&mut self, luma: &[u8], stride: usize) -> ComplexityResult {
        let spatial = self.estimate_spatial(luma, stride);
        let temporal = self.estimate_temporal(luma, stride);

        // Combined metric (geometric mean)
        let combined = (spatial * temporal).sqrt();

        // Update running averages
        self.update_averages(spatial, temporal, combined);

        // Store frame data for next frame's temporal analysis
        self.store_frame_data(luma, stride);

        self.frame_count += 1;

        ComplexityResult {
            spatial,
            temporal,
            combined,
            normalized: combined / self.avg_combined,
        }
    }

    /// Estimate spatial complexity using variance-based method.
    fn estimate_spatial(&self, luma: &[u8], stride: usize) -> f32 {
        let blocks_x = self.width / self.block_size;
        let blocks_y = self.height / self.block_size;

        if blocks_x == 0 || blocks_y == 0 {
            return 1.0;
        }

        let mut total_variance = 0.0f64;
        let mut block_count = 0u32;

        for by in 0..blocks_y {
            for bx in 0..blocks_x {
                let variance = self.calculate_block_variance(luma, stride, bx, by);
                total_variance += variance as f64;
                block_count += 1;
            }
        }

        if block_count == 0 {
            return 1.0;
        }

        let avg_variance = (total_variance / block_count as f64) as f32;

        // Normalize variance to a reasonable scale
        // High variance = high complexity
        (avg_variance / 100.0).sqrt().max(0.1)
    }

    /// Calculate variance of a single block.
    fn calculate_block_variance(&self, luma: &[u8], stride: usize, bx: u32, by: u32) -> f32 {
        let start_x = (bx * self.block_size) as usize;
        let start_y = (by * self.block_size) as usize;
        let block_size = self.block_size as usize;

        let mut sum = 0u64;
        let mut sum_sq = 0u64;
        let mut count = 0u32;

        for y in 0..block_size {
            let row_start = (start_y + y) * stride + start_x;
            if row_start + block_size > luma.len() {
                continue;
            }

            for x in 0..block_size {
                let pixel = luma[row_start + x] as u64;
                sum += pixel;
                sum_sq += pixel * pixel;
                count += 1;
            }
        }

        if count == 0 {
            return 0.0;
        }

        let mean = sum as f32 / count as f32;
        let mean_sq = sum_sq as f32 / count as f32;
        let variance = mean_sq - mean * mean;

        variance.max(0.0)
    }

    /// Estimate temporal complexity using SAD-based method.
    fn estimate_temporal(&self, luma: &[u8], stride: usize) -> f32 {
        let prev_data = match &self.prev_frame_data {
            Some(data) => data,
            None => return 1.0, // First frame, assume average complexity
        };

        // Calculate SAD between current and previous frame
        let sad = self.calculate_frame_sad(luma, stride, prev_data);

        // Normalize SAD to complexity metric
        let pixels = self.width as f64 * self.height as f64;
        if pixels == 0.0 {
            return 1.0;
        }

        let normalized_sad = sad as f64 / pixels;

        // Map to reasonable range
        (normalized_sad as f32 / 10.0).sqrt().max(0.1)
    }

    /// Calculate SAD (Sum of Absolute Differences) against previous frame.
    fn calculate_frame_sad(&self, luma: &[u8], stride: usize, _prev: &FrameData) -> u64 {
        // Simplified: just calculate variance difference
        // In a full implementation, this would do motion-compensated SAD
        let current_variance = self.calculate_total_variance(luma, stride);
        let prev_variance = _prev.total_variance;

        ((current_variance - prev_variance).abs() * 1000.0) as u64
    }

    /// Calculate total frame variance.
    fn calculate_total_variance(&self, luma: &[u8], stride: usize) -> f32 {
        let total_pixels = (self.width * self.height) as usize;
        if total_pixels == 0 || luma.len() < total_pixels {
            return 0.0;
        }

        let mut sum = 0u64;
        let mut sum_sq = 0u64;
        let mut count = 0u64;

        for y in 0..self.height as usize {
            let row_start = y * stride;
            let row_end = (row_start + self.width as usize).min(luma.len());

            for x in row_start..row_end {
                let pixel = luma[x] as u64;
                sum += pixel;
                sum_sq += pixel * pixel;
                count += 1;
            }
        }

        if count == 0 {
            return 0.0;
        }

        let mean = sum as f32 / count as f32;
        let mean_sq = sum_sq as f32 / count as f32;
        (mean_sq - mean * mean).max(0.0)
    }

    /// Store frame data for temporal analysis.
    fn store_frame_data(&mut self, luma: &[u8], stride: usize) {
        let blocks_x = self.width / self.block_size;
        let blocks_y = self.height / self.block_size;

        let mut block_variances = Vec::with_capacity((blocks_x * blocks_y) as usize);
        let mut block_averages = Vec::with_capacity((blocks_x * blocks_y) as usize);

        for by in 0..blocks_y {
            for bx in 0..blocks_x {
                let (avg, var) = self.calculate_block_stats(luma, stride, bx, by);
                block_variances.push(var);
                block_averages.push(avg);
            }
        }

        let total_variance = self.calculate_total_variance(luma, stride);

        self.prev_frame_data = Some(FrameData {
            block_variances,
            block_averages,
            total_variance,
        });
    }

    /// Calculate block average and variance.
    fn calculate_block_stats(&self, luma: &[u8], stride: usize, bx: u32, by: u32) -> (f32, f32) {
        let start_x = (bx * self.block_size) as usize;
        let start_y = (by * self.block_size) as usize;
        let block_size = self.block_size as usize;

        let mut sum = 0u64;
        let mut sum_sq = 0u64;
        let mut count = 0u32;

        for y in 0..block_size {
            let row_start = (start_y + y) * stride + start_x;
            if row_start + block_size > luma.len() {
                continue;
            }

            for x in 0..block_size {
                let pixel = luma[row_start + x] as u64;
                sum += pixel;
                sum_sq += pixel * pixel;
                count += 1;
            }
        }

        if count == 0 {
            return (128.0, 0.0);
        }

        let mean = sum as f32 / count as f32;
        let mean_sq = sum_sq as f32 / count as f32;
        let variance = (mean_sq - mean * mean).max(0.0);

        (mean, variance)
    }

    /// Update running averages.
    fn update_averages(&mut self, spatial: f32, temporal: f32, combined: f32) {
        let w = self.ema_weight;
        self.avg_spatial = self.avg_spatial * (1.0 - w) + spatial * w;
        self.avg_temporal = self.avg_temporal * (1.0 - w) + temporal * w;
        self.avg_combined = self.avg_combined * (1.0 - w) + combined * w;
    }

    /// Get average spatial complexity.
    #[must_use]
    pub fn avg_spatial(&self) -> f32 {
        self.avg_spatial
    }

    /// Get average temporal complexity.
    #[must_use]
    pub fn avg_temporal(&self) -> f32 {
        self.avg_temporal
    }

    /// Get average combined complexity.
    #[must_use]
    pub fn avg_combined(&self) -> f32 {
        self.avg_combined
    }

    /// Get frame count.
    #[must_use]
    pub fn frame_count(&self) -> u64 {
        self.frame_count
    }

    /// Reset the estimator state.
    pub fn reset(&mut self) {
        self.avg_spatial = 1.0;
        self.avg_temporal = 1.0;
        self.avg_combined = 1.0;
        self.frame_count = 0;
        self.prev_frame_data = None;
    }
}

impl Default for ComplexityEstimator {
    fn default() -> Self {
        Self::new(1920, 1080)
    }
}

/// Result of complexity estimation.
#[derive(Clone, Copy, Debug, Default)]
pub struct ComplexityResult {
    /// Spatial complexity (texture/detail).
    pub spatial: f32,
    /// Temporal complexity (motion).
    pub temporal: f32,
    /// Combined complexity metric.
    pub combined: f32,
    /// Normalized complexity (relative to average).
    pub normalized: f32,
}

impl ComplexityResult {
    /// Create a result with default complexity.
    #[must_use]
    pub fn default_complexity() -> Self {
        Self {
            spatial: 1.0,
            temporal: 1.0,
            combined: 1.0,
            normalized: 1.0,
        }
    }

    /// Check if this is a high complexity frame.
    #[must_use]
    pub fn is_high_complexity(&self) -> bool {
        self.normalized > 1.5
    }

    /// Check if this is a low complexity frame.
    #[must_use]
    pub fn is_low_complexity(&self) -> bool {
        self.normalized < 0.7
    }

    /// Get suggested QP adjustment based on complexity.
    #[must_use]
    pub fn suggested_qp_offset(&self) -> f32 {
        // Higher complexity -> higher QP offset (use more compression)
        // Lower complexity -> lower QP offset (better quality)
        let log_ratio = self.normalized.ln();
        (log_ratio * 2.0).clamp(-4.0, 4.0)
    }
}

/// Motion complexity analyzer.
#[derive(Clone, Debug)]
pub struct MotionAnalyzer {
    /// Block size for motion analysis.
    block_size: u32,
    /// Search range for motion estimation (reserved for full motion search).
    #[allow(dead_code)]
    search_range: u32,
    /// Previous frame luma.
    prev_luma: Option<Vec<u8>>,
    /// Frame width.
    width: u32,
    /// Frame height.
    height: u32,
}

impl MotionAnalyzer {
    /// Create a new motion analyzer.
    #[must_use]
    pub fn new(width: u32, height: u32) -> Self {
        Self {
            block_size: 16,
            search_range: 16,
            prev_luma: None,
            width,
            height,
        }
    }

    /// Analyze motion in a frame.
    pub fn analyze(&mut self, luma: &[u8], stride: usize) -> MotionResult {
        let result = if let Some(ref prev) = self.prev_luma {
            self.calculate_motion(prev, luma, stride)
        } else {
            MotionResult::default()
        };

        // Store current frame for next comparison
        self.store_frame(luma, stride);

        result
    }

    /// Calculate motion metrics between frames.
    fn calculate_motion(&self, prev: &[u8], curr: &[u8], stride: usize) -> MotionResult {
        let blocks_x = self.width / self.block_size;
        let blocks_y = self.height / self.block_size;

        if blocks_x == 0 || blocks_y == 0 {
            return MotionResult::default();
        }

        let mut total_sad = 0u64;
        let mut motion_blocks = 0u32;
        let mut max_motion = 0f32;

        for by in 0..blocks_y {
            for bx in 0..blocks_x {
                let (sad, motion) = self.analyze_block(prev, curr, stride, bx, by);
                total_sad += sad;

                if motion > 2.0 {
                    motion_blocks += 1;
                }
                if motion > max_motion {
                    max_motion = motion;
                }
            }
        }

        let total_blocks = blocks_x * blocks_y;
        let avg_sad = total_sad as f32 / total_blocks as f32;
        let motion_ratio = motion_blocks as f32 / total_blocks as f32;

        MotionResult {
            average_sad: avg_sad,
            motion_ratio,
            max_motion,
            complexity: (avg_sad / 100.0).sqrt() * (1.0 + motion_ratio),
        }
    }

    /// Analyze a single block for motion.
    fn analyze_block(
        &self,
        prev: &[u8],
        curr: &[u8],
        stride: usize,
        bx: u32,
        by: u32,
    ) -> (u64, f32) {
        let start_x = (bx * self.block_size) as usize;
        let start_y = (by * self.block_size) as usize;
        let block_size = self.block_size as usize;

        // Calculate SAD at (0,0) position (no motion)
        let mut sad = 0u64;

        for y in 0..block_size {
            let curr_row = (start_y + y) * stride + start_x;
            let prev_row = (start_y + y) * stride + start_x;

            if curr_row + block_size > curr.len() || prev_row + block_size > prev.len() {
                continue;
            }

            for x in 0..block_size {
                let diff = (curr[curr_row + x] as i32 - prev[prev_row + x] as i32).unsigned_abs();
                sad += diff as u64;
            }
        }

        let pixels = (block_size * block_size) as f32;
        let avg_diff = sad as f32 / pixels;

        (sad, avg_diff)
    }

    /// Store frame for next comparison.
    fn store_frame(&mut self, luma: &[u8], stride: usize) {
        let height = self.height as usize;
        let width = self.width as usize;

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

        for y in 0..height {
            let src_start = y * stride;
            let dst_start = y * width;
            let copy_len = width.min(luma.len().saturating_sub(src_start));

            if copy_len > 0 {
                stored[dst_start..dst_start + copy_len]
                    .copy_from_slice(&luma[src_start..src_start + copy_len]);
            }
        }

        self.prev_luma = Some(stored);
    }

    /// Reset the analyzer.
    pub fn reset(&mut self) {
        self.prev_luma = None;
    }
}

impl Default for MotionAnalyzer {
    fn default() -> Self {
        Self::new(1920, 1080)
    }
}

/// Motion analysis result.
#[derive(Clone, Copy, Debug, Default)]
pub struct MotionResult {
    /// Average SAD (Sum of Absolute Differences).
    pub average_sad: f32,
    /// Ratio of blocks with significant motion.
    pub motion_ratio: f32,
    /// Maximum motion detected in any block.
    pub max_motion: f32,
    /// Overall motion complexity metric.
    pub complexity: f32,
}

impl MotionResult {
    /// Check if this indicates a high motion frame.
    #[must_use]
    pub fn is_high_motion(&self) -> bool {
        self.motion_ratio > 0.5 || self.max_motion > 20.0
    }

    /// Check if this indicates a static/low motion frame.
    #[must_use]
    pub fn is_static(&self) -> bool {
        self.motion_ratio < 0.1 && self.max_motion < 5.0
    }
}

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

    fn create_test_frame(width: u32, height: u32, value: u8) -> Vec<u8> {
        vec![value; (width * height) as usize]
    }

    fn create_gradient_frame(width: u32, height: u32) -> Vec<u8> {
        let mut frame = Vec::with_capacity((width * height) as usize);
        for y in 0..height {
            for x in 0..width {
                frame.push(((x + y) % 256) as u8);
            }
        }
        frame
    }

    #[test]
    fn test_complexity_estimator_creation() {
        let estimator = ComplexityEstimator::new(1920, 1080);
        assert_eq!(estimator.frame_count(), 0);
    }

    #[test]
    fn test_uniform_frame_low_complexity() {
        let mut estimator = ComplexityEstimator::new(64, 64);
        let frame = create_test_frame(64, 64, 128);

        let result = estimator.estimate(&frame, 64);

        // Uniform frame should have low spatial complexity
        assert!(result.spatial < 0.5);
    }

    #[test]
    fn test_gradient_frame_higher_complexity() {
        let mut estimator = ComplexityEstimator::new(64, 64);
        let frame = create_gradient_frame(64, 64);

        let result = estimator.estimate(&frame, 64);

        // Gradient has some texture, should have measurable complexity
        assert!(result.spatial > 0.0);
    }

    #[test]
    fn test_temporal_complexity_static() {
        let mut estimator = ComplexityEstimator::new(64, 64);
        let frame = create_test_frame(64, 64, 128);

        // First frame
        let _ = estimator.estimate(&frame, 64);

        // Same frame again - should have low temporal complexity
        let result = estimator.estimate(&frame, 64);
        assert!(result.temporal <= 1.0);
    }

    #[test]
    fn test_complexity_result_methods() {
        let high = ComplexityResult {
            spatial: 2.0,
            temporal: 2.0,
            combined: 2.0,
            normalized: 2.0,
        };
        assert!(high.is_high_complexity());
        assert!(!high.is_low_complexity());

        let low = ComplexityResult {
            spatial: 0.5,
            temporal: 0.5,
            combined: 0.5,
            normalized: 0.5,
        };
        assert!(low.is_low_complexity());
        assert!(!low.is_high_complexity());
    }

    #[test]
    fn test_suggested_qp_offset() {
        let high = ComplexityResult {
            spatial: 1.0,
            temporal: 1.0,
            combined: 1.0,
            normalized: 2.0,
        };
        assert!(high.suggested_qp_offset() > 0.0);

        let low = ComplexityResult {
            spatial: 1.0,
            temporal: 1.0,
            combined: 1.0,
            normalized: 0.5,
        };
        assert!(low.suggested_qp_offset() < 0.0);
    }

    #[test]
    fn test_motion_analyzer_creation() {
        let analyzer = MotionAnalyzer::new(1920, 1080);
        assert!(analyzer.prev_luma.is_none());
    }

    #[test]
    fn test_motion_analyzer_static() {
        let mut analyzer = MotionAnalyzer::new(64, 64);
        let frame = create_test_frame(64, 64, 128);

        // First frame
        let _ = analyzer.analyze(&frame, 64);

        // Same frame - no motion
        let result = analyzer.analyze(&frame, 64);
        assert!(result.is_static());
    }

    #[test]
    fn test_motion_analyzer_reset() {
        let mut analyzer = MotionAnalyzer::new(64, 64);
        let frame = create_test_frame(64, 64, 128);

        let _ = analyzer.analyze(&frame, 64);
        assert!(analyzer.prev_luma.is_some());

        analyzer.reset();
        assert!(analyzer.prev_luma.is_none());
    }

    #[test]
    fn test_estimator_reset() {
        let mut estimator = ComplexityEstimator::new(64, 64);
        let frame = create_test_frame(64, 64, 128);

        let _ = estimator.estimate(&frame, 64);
        assert_eq!(estimator.frame_count(), 1);

        estimator.reset();
        assert_eq!(estimator.frame_count(), 0);
    }

    #[test]
    fn test_running_averages() {
        let mut estimator = ComplexityEstimator::new(64, 64);
        let frame = create_gradient_frame(64, 64);

        for _ in 0..10 {
            let _ = estimator.estimate(&frame, 64);
        }

        // Averages should converge
        assert!(estimator.avg_spatial() > 0.0);
        assert!(estimator.avg_combined() > 0.0);
    }
}