oximedia-codec 0.1.4

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
// Copyright 2024 The OxiMedia Project Developers
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Statistics collection and analysis for Theora encoding.
//!
//! Provides comprehensive encoding statistics for debugging and optimization.

use std::time::{Duration, Instant};

/// Frame encoding statistics.
#[derive(Debug, Clone)]
pub struct FrameEncodingStats {
    /// Frame number.
    pub frame_number: u64,
    /// Frame type (true = keyframe).
    pub is_keyframe: bool,
    /// Encoded size in bytes.
    pub size_bytes: usize,
    /// Quality parameter used.
    pub quality: u8,
    /// PSNR (Peak Signal-to-Noise Ratio) for Y component.
    pub psnr_y: f64,
    /// PSNR for U component.
    pub psnr_u: f64,
    /// PSNR for V component.
    pub psnr_v: f64,
    /// SSIM (Structural Similarity Index) for Y component.
    pub ssim_y: f64,
    /// Encoding time.
    pub encode_time: Duration,
    /// Number of intra blocks.
    pub intra_blocks: usize,
    /// Number of inter blocks.
    pub inter_blocks: usize,
    /// Number of skip blocks.
    pub skip_blocks: usize,
    /// Average motion vector magnitude.
    pub avg_mv_magnitude: f32,
    /// Bitrate (bits per second).
    pub bitrate: f64,
}

impl FrameEncodingStats {
    /// Create a new frame statistics entry.
    #[must_use]
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        frame_number: u64,
        is_keyframe: bool,
        size_bytes: usize,
        quality: u8,
        encode_time: Duration,
    ) -> Self {
        Self {
            frame_number,
            is_keyframe,
            size_bytes,
            quality,
            psnr_y: 0.0,
            psnr_u: 0.0,
            psnr_v: 0.0,
            ssim_y: 0.0,
            encode_time,
            intra_blocks: 0,
            inter_blocks: 0,
            skip_blocks: 0,
            avg_mv_magnitude: 0.0,
            bitrate: 0.0,
        }
    }

    /// Calculate average PSNR across all components.
    #[must_use]
    pub fn avg_psnr(&self) -> f64 {
        (self.psnr_y + self.psnr_u + self.psnr_v) / 3.0
    }

    /// Get total number of blocks.
    #[must_use]
    pub fn total_blocks(&self) -> usize {
        self.intra_blocks + self.inter_blocks + self.skip_blocks
    }

    /// Get percentage of intra blocks.
    #[must_use]
    pub fn intra_percentage(&self) -> f64 {
        let total = self.total_blocks();
        if total > 0 {
            (self.intra_blocks as f64 / total as f64) * 100.0
        } else {
            0.0
        }
    }
}

/// Encoder session statistics.
#[derive(Debug, Clone)]
pub struct EncoderStats {
    /// Frame statistics.
    pub frames: Vec<FrameEncodingStats>,
    /// Total encoding time.
    pub total_time: Duration,
    /// Start time.
    start_time: Option<Instant>,
}

impl EncoderStats {
    /// Create a new encoder statistics collector.
    #[must_use]
    pub fn new() -> Self {
        Self {
            frames: Vec::new(),
            total_time: Duration::ZERO,
            start_time: None,
        }
    }

    /// Start timing.
    pub fn start_timing(&mut self) {
        self.start_time = Some(Instant::now());
    }

    /// Stop timing and update total time.
    pub fn stop_timing(&mut self) {
        if let Some(start) = self.start_time.take() {
            self.total_time = start.elapsed();
        }
    }

    /// Add frame statistics.
    pub fn add_frame(&mut self, stats: FrameEncodingStats) {
        self.frames.push(stats);
    }

    /// Get average bitrate across all frames.
    #[must_use]
    pub fn average_bitrate(&self) -> f64 {
        if self.frames.is_empty() {
            return 0.0;
        }

        let total_bytes: usize = self.frames.iter().map(|f| f.size_bytes).sum();
        let total_bits = total_bytes * 8;
        let duration_secs = self.total_time.as_secs_f64();

        if duration_secs > 0.0 {
            total_bits as f64 / duration_secs
        } else {
            0.0
        }
    }

    /// Get average PSNR across all frames.
    #[must_use]
    pub fn average_psnr(&self) -> f64 {
        if self.frames.is_empty() {
            return 0.0;
        }

        let sum: f64 = self.frames.iter().map(|f| f.avg_psnr()).sum();
        sum / self.frames.len() as f64
    }

    /// Get average encoding time per frame.
    #[must_use]
    pub fn average_encode_time(&self) -> Duration {
        if self.frames.is_empty() {
            return Duration::ZERO;
        }

        let total_nanos: u128 = self.frames.iter().map(|f| f.encode_time.as_nanos()).sum();
        Duration::from_nanos((total_nanos / self.frames.len() as u128) as u64)
    }

    /// Get encoding speed in frames per second.
    #[must_use]
    pub fn frames_per_second(&self) -> f64 {
        let duration_secs = self.total_time.as_secs_f64();
        if duration_secs > 0.0 {
            self.frames.len() as f64 / duration_secs
        } else {
            0.0
        }
    }

    /// Get keyframe interval statistics.
    #[must_use]
    pub fn keyframe_interval_stats(&self) -> KeyframeIntervalStats {
        let mut intervals = Vec::new();
        let mut last_keyframe = 0usize;

        for (i, frame) in self.frames.iter().enumerate() {
            if frame.is_keyframe {
                if i > 0 {
                    intervals.push(i - last_keyframe);
                }
                last_keyframe = i;
            }
        }

        KeyframeIntervalStats::from_intervals(&intervals)
    }

    /// Generate summary report.
    #[must_use]
    pub fn summary(&self) -> String {
        format!(
            "Encoder Statistics:\n\
             Total Frames: {}\n\
             Total Time: {:.2}s\n\
             Average Bitrate: {:.2} kbps\n\
             Average PSNR: {:.2} dB\n\
             Average Encode Time: {:.2}ms/frame\n\
             Encoding Speed: {:.2} fps",
            self.frames.len(),
            self.total_time.as_secs_f64(),
            self.average_bitrate() / 1000.0,
            self.average_psnr(),
            self.average_encode_time().as_secs_f64() * 1000.0,
            self.frames_per_second()
        )
    }
}

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

/// Keyframe interval statistics.
#[derive(Debug, Clone)]
pub struct KeyframeIntervalStats {
    /// Minimum interval.
    pub min: usize,
    /// Maximum interval.
    pub max: usize,
    /// Average interval.
    pub average: f64,
}

impl KeyframeIntervalStats {
    /// Create from interval list.
    #[must_use]
    pub fn from_intervals(intervals: &[usize]) -> Self {
        if intervals.is_empty() {
            return Self {
                min: 0,
                max: 0,
                average: 0.0,
            };
        }

        let min = *intervals.iter().min().unwrap_or(&0);
        let max = *intervals.iter().max().unwrap_or(&0);
        let sum: usize = intervals.iter().sum();
        let average = sum as f64 / intervals.len() as f64;

        Self { min, max, average }
    }
}

/// Quality metrics calculator.
pub struct QualityMetrics;

impl QualityMetrics {
    /// Calculate PSNR between two planes.
    #[must_use]
    pub fn calculate_psnr(
        original: &[u8],
        reconstructed: &[u8],
        width: usize,
        height: usize,
    ) -> f64 {
        let size = width * height;
        if size == 0 || original.len() < size || reconstructed.len() < size {
            return 0.0;
        }

        let mut mse = 0.0;
        for i in 0..size {
            let diff = f64::from(original[i]) - f64::from(reconstructed[i]);
            mse += diff * diff;
        }
        mse /= size as f64;

        if mse < 1e-10 {
            return 100.0; // Perfect match
        }

        let max_val = 255.0;
        20.0 * (max_val / mse.sqrt()).log10()
    }

    /// Calculate SSIM between two planes.
    #[must_use]
    pub fn calculate_ssim(
        original: &[u8],
        reconstructed: &[u8],
        width: usize,
        height: usize,
        stride: usize,
    ) -> f64 {
        const C1: f64 = 6.5025; // (0.01 * 255)^2
        const C2: f64 = 58.5225; // (0.03 * 255)^2

        let mut ssim_sum = 0.0;
        let mut count = 0;

        // Calculate SSIM for 8x8 windows
        for y in (0..height - 8).step_by(4) {
            for x in (0..width - 8).step_by(4) {
                let (mu_x, mu_y, sigma_x, sigma_y, sigma_xy) =
                    Self::calculate_window_stats(original, reconstructed, stride, x, y);

                let numerator = (2.0 * mu_x * mu_y + C1) * (2.0 * sigma_xy + C2);
                let denominator = (mu_x * mu_x + mu_y * mu_y + C1) * (sigma_x + sigma_y + C2);

                if denominator > 0.0 {
                    ssim_sum += numerator / denominator;
                    count += 1;
                }
            }
        }

        if count > 0 {
            ssim_sum / count as f64
        } else {
            0.0
        }
    }

    /// Calculate statistics for an 8x8 window.
    fn calculate_window_stats(
        original: &[u8],
        reconstructed: &[u8],
        stride: usize,
        x: usize,
        y: usize,
    ) -> (f64, f64, f64, f64, f64) {
        let mut sum_x = 0.0;
        let mut sum_y = 0.0;
        let mut sum_xx = 0.0;
        let mut sum_yy = 0.0;
        let mut sum_xy = 0.0;
        let n = 64.0;

        for dy in 0..8 {
            for dx in 0..8 {
                let offset = (y + dy) * stride + x + dx;
                if offset < original.len() && offset < reconstructed.len() {
                    let px = f64::from(original[offset]);
                    let py = f64::from(reconstructed[offset]);

                    sum_x += px;
                    sum_y += py;
                    sum_xx += px * px;
                    sum_yy += py * py;
                    sum_xy += px * py;
                }
            }
        }

        let mu_x = sum_x / n;
        let mu_y = sum_y / n;
        let sigma_x = (sum_xx / n - mu_x * mu_x).max(0.0);
        let sigma_y = (sum_yy / n - mu_y * mu_y).max(0.0);
        let sigma_xy = sum_xy / n - mu_x * mu_y;

        (mu_x, mu_y, sigma_x, sigma_y, sigma_xy)
    }
}

/// Bitrate distribution analyzer.
#[derive(Debug, Clone)]
pub struct BitrateDistribution {
    /// Bitrate histogram (in kbps buckets).
    buckets: Vec<usize>,
    /// Bucket size in kbps.
    bucket_size: usize,
}

impl BitrateDistribution {
    /// Create a new bitrate distribution analyzer.
    #[must_use]
    pub fn new(bucket_size: usize) -> Self {
        Self {
            buckets: vec![0; 100],
            bucket_size,
        }
    }

    /// Add a frame bitrate.
    pub fn add_frame(&mut self, bitrate: f64) {
        let bucket_idx = (bitrate / 1000.0 / self.bucket_size as f64) as usize;
        if bucket_idx < self.buckets.len() {
            self.buckets[bucket_idx] += 1;
        }
    }

    /// Get the modal bitrate bucket.
    #[must_use]
    pub fn mode(&self) -> usize {
        self.buckets
            .iter()
            .enumerate()
            .max_by_key(|(_, &count)| count)
            .map(|(idx, _)| idx * self.bucket_size)
            .unwrap_or(0)
    }

    /// Get bitrate variance.
    #[must_use]
    pub fn variance(&self) -> f64 {
        let total: usize = self.buckets.iter().sum();
        if total == 0 {
            return 0.0;
        }

        let mean = self.mean();
        let mut variance = 0.0;

        for (idx, &count) in self.buckets.iter().enumerate() {
            let value = (idx * self.bucket_size) as f64;
            let diff = value - mean;
            variance += diff * diff * count as f64;
        }

        variance / total as f64
    }

    /// Get mean bitrate.
    #[must_use]
    pub fn mean(&self) -> f64 {
        let total: usize = self.buckets.iter().sum();
        if total == 0 {
            return 0.0;
        }

        let sum: usize = self
            .buckets
            .iter()
            .enumerate()
            .map(|(idx, &count)| idx * self.bucket_size * count)
            .sum();

        sum as f64 / total as f64
    }
}

/// Complexity analyzer for frames.
pub struct ComplexityAnalyzer {
    /// Complexity history.
    history: Vec<f64>,
    /// Maximum history size.
    max_history: usize,
}

impl ComplexityAnalyzer {
    /// Create a new complexity analyzer.
    #[must_use]
    pub fn new(max_history: usize) -> Self {
        Self {
            history: Vec::new(),
            max_history,
        }
    }

    /// Add frame complexity measurement.
    pub fn add_measurement(&mut self, complexity: f64) {
        self.history.push(complexity);
        if self.history.len() > self.max_history {
            self.history.remove(0);
        }
    }

    /// Get average complexity.
    #[must_use]
    pub fn average(&self) -> f64 {
        if self.history.is_empty() {
            return 0.0;
        }
        let sum: f64 = self.history.iter().sum();
        sum / self.history.len() as f64
    }

    /// Get complexity trend (positive = increasing).
    #[must_use]
    pub fn trend(&self) -> f64 {
        if self.history.len() < 2 {
            return 0.0;
        }

        let half = self.history.len() / 2;
        let first_half: f64 = self.history[..half].iter().sum::<f64>() / half as f64;
        let second_half: f64 =
            self.history[half..].iter().sum::<f64>() / (self.history.len() - half) as f64;

        second_half - first_half
    }

    /// Get complexity standard deviation.
    #[must_use]
    pub fn std_dev(&self) -> f64 {
        if self.history.is_empty() {
            return 0.0;
        }

        let mean = self.average();
        let variance: f64 = self
            .history
            .iter()
            .map(|&x| (x - mean).powi(2))
            .sum::<f64>()
            / self.history.len() as f64;

        variance.sqrt()
    }
}

/// Encoder performance profiler.
#[derive(Debug, Clone, Default)]
pub struct PerformanceProfiler {
    /// Time spent in motion estimation.
    pub motion_estimation_time: Duration,
    /// Time spent in mode decision.
    pub mode_decision_time: Duration,
    /// Time spent in transform.
    pub transform_time: Duration,
    /// Time spent in quantization.
    pub quantization_time: Duration,
    /// Time spent in entropy coding.
    pub entropy_coding_time: Duration,
    /// Time spent in reconstruction.
    pub reconstruction_time: Duration,
}

impl PerformanceProfiler {
    /// Get total profiled time.
    #[must_use]
    pub fn total_time(&self) -> Duration {
        self.motion_estimation_time
            + self.mode_decision_time
            + self.transform_time
            + self.quantization_time
            + self.entropy_coding_time
            + self.reconstruction_time
    }

    /// Get percentage breakdown.
    #[must_use]
    pub fn percentage_breakdown(&self) -> ProfilerBreakdown {
        let total = self.total_time().as_secs_f64();
        if total < 1e-6 {
            return ProfilerBreakdown::default();
        }

        ProfilerBreakdown {
            motion_estimation: (self.motion_estimation_time.as_secs_f64() / total) * 100.0,
            mode_decision: (self.mode_decision_time.as_secs_f64() / total) * 100.0,
            transform: (self.transform_time.as_secs_f64() / total) * 100.0,
            quantization: (self.quantization_time.as_secs_f64() / total) * 100.0,
            entropy_coding: (self.entropy_coding_time.as_secs_f64() / total) * 100.0,
            reconstruction: (self.reconstruction_time.as_secs_f64() / total) * 100.0,
        }
    }
}

/// Profiler percentage breakdown.
#[derive(Debug, Clone, Default)]
pub struct ProfilerBreakdown {
    /// Motion estimation percentage.
    pub motion_estimation: f64,
    /// Mode decision percentage.
    pub mode_decision: f64,
    /// Transform percentage.
    pub transform: f64,
    /// Quantization percentage.
    pub quantization: f64,
    /// Entropy coding percentage.
    pub entropy_coding: f64,
    /// Reconstruction percentage.
    pub reconstruction: f64,
}

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

    #[test]
    fn test_frame_stats() {
        let stats = FrameEncodingStats::new(0, true, 10000, 30, Duration::from_millis(50));
        assert_eq!(stats.frame_number, 0);
        assert!(stats.is_keyframe);
        assert_eq!(stats.size_bytes, 10000);
    }

    #[test]
    fn test_encoder_stats() {
        let mut stats = EncoderStats::new();
        stats.start_timing();

        stats.add_frame(FrameEncodingStats::new(
            0,
            true,
            10000,
            30,
            Duration::from_millis(50),
        ));
        stats.add_frame(FrameEncodingStats::new(
            1,
            false,
            5000,
            32,
            Duration::from_millis(40),
        ));

        assert_eq!(stats.frames.len(), 2);
        assert!(stats.average_encode_time() > Duration::ZERO);
    }

    #[test]
    fn test_psnr_calculation() {
        let original = vec![100u8; 64];
        let reconstructed = vec![100u8; 64];
        let psnr = QualityMetrics::calculate_psnr(&original, &reconstructed, 8, 8);
        assert!(psnr > 90.0); // Perfect match should have very high PSNR
    }

    #[test]
    fn test_bitrate_distribution() {
        let mut dist = BitrateDistribution::new(100);
        dist.add_frame(1500.0 * 1000.0); // 1500 kbps
        dist.add_frame(1600.0 * 1000.0); // 1600 kbps
        dist.add_frame(1550.0 * 1000.0); // 1550 kbps

        let mode = dist.mode();
        assert!(mode >= 1500 && mode <= 1600);
    }

    #[test]
    fn test_complexity_analyzer() {
        let mut analyzer = ComplexityAnalyzer::new(10);
        analyzer.add_measurement(100.0);
        analyzer.add_measurement(150.0);
        analyzer.add_measurement(200.0);

        assert!(analyzer.average() > 100.0);
        assert!(analyzer.trend() > 0.0); // Increasing complexity
    }

    #[test]
    fn test_performance_profiler() {
        let mut profiler = PerformanceProfiler::default();
        profiler.motion_estimation_time = Duration::from_millis(100);
        profiler.transform_time = Duration::from_millis(50);

        assert_eq!(profiler.total_time(), Duration::from_millis(150));

        let breakdown = profiler.percentage_breakdown();
        assert!((breakdown.motion_estimation - 66.67).abs() < 0.1);
    }
}