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
//! Frame complexity analysis for multi-pass encoding.
//!
//! This module provides advanced frame complexity metrics used in multi-pass
//! encoding to make better bitrate allocation decisions.

#![forbid(unsafe_code)]
#![allow(clippy::cast_precision_loss)]
#![allow(clippy::cast_possible_truncation)]
#![allow(clippy::cast_sign_loss)]
#![allow(clippy::cast_lossless)]

use crate::frame::{FrameType, VideoFrame};

/// Frame complexity metrics for bitrate allocation.
#[derive(Clone, Debug)]
pub struct FrameComplexity {
    /// Frame index in the stream.
    pub frame_index: u64,
    /// Frame type (Key, Inter, BiDir).
    pub frame_type: FrameType,
    /// Spatial complexity (0.0-1.0, based on variance).
    pub spatial_complexity: f64,
    /// Temporal complexity (0.0-1.0, based on motion).
    pub temporal_complexity: f64,
    /// Combined complexity metric.
    pub combined_complexity: f64,
    /// Sum of Absolute Differences with previous frame.
    pub sad: u64,
    /// Average luma variance across blocks.
    pub variance: f64,
    /// Estimated encoding difficulty (1.0 = average).
    pub encoding_difficulty: f64,
    /// Is this frame a scene change.
    pub is_scene_change: bool,
}

impl FrameComplexity {
    /// Create a new frame complexity with default values.
    #[must_use]
    pub fn new(frame_index: u64, frame_type: FrameType) -> Self {
        Self {
            frame_index,
            frame_type,
            spatial_complexity: 0.5,
            temporal_complexity: 0.5,
            combined_complexity: 0.5,
            sad: 0,
            variance: 0.0,
            encoding_difficulty: 1.0,
            is_scene_change: false,
        }
    }

    /// Calculate relative difficulty compared to average frame.
    #[must_use]
    pub fn relative_difficulty(&self) -> f64 {
        self.encoding_difficulty
    }
}

/// Complexity analyzer for video frames.
pub struct ComplexityAnalyzer {
    width: u32,
    height: u32,
    block_size: usize,
    prev_frame: Option<Vec<u8>>,
    spatial_history: Vec<f64>,
    temporal_history: Vec<f64>,
    scene_change_threshold: f64,
}

impl ComplexityAnalyzer {
    /// Create a new complexity analyzer.
    #[must_use]
    pub fn new(width: u32, height: u32) -> Self {
        Self {
            width,
            height,
            block_size: 16,
            prev_frame: None,
            spatial_history: Vec::new(),
            temporal_history: Vec::new(),
            scene_change_threshold: 0.4,
        }
    }

    /// Set the scene change detection threshold.
    pub fn set_scene_change_threshold(&mut self, threshold: f64) {
        self.scene_change_threshold = threshold.clamp(0.0, 1.0);
    }

    /// Analyze a video frame and compute complexity metrics.
    #[must_use]
    pub fn analyze(&mut self, frame: &VideoFrame, frame_index: u64) -> FrameComplexity {
        let mut complexity = FrameComplexity::new(frame_index, frame.frame_type);

        // Get luma plane
        if let Some(luma_plane) = frame.planes.first() {
            let luma_data = luma_plane.data.as_ref();
            let stride = luma_plane.stride;

            // Calculate spatial complexity
            complexity.spatial_complexity = self.compute_spatial_complexity(luma_data, stride);
            complexity.variance = self.compute_variance(luma_data, stride);

            // Calculate temporal complexity
            if let Some(prev) = &self.prev_frame {
                complexity.sad = self.compute_sad(luma_data, prev, stride);
                complexity.temporal_complexity = self.compute_temporal_complexity(complexity.sad);
                complexity.is_scene_change = self.detect_scene_change(complexity.sad);
            } else {
                complexity.temporal_complexity = 1.0;
                complexity.is_scene_change = true;
            }

            // Store current frame for next iteration
            self.prev_frame = Some(luma_data.to_vec());

            // Update history
            self.spatial_history.push(complexity.spatial_complexity);
            self.temporal_history.push(complexity.temporal_complexity);
            if self.spatial_history.len() > 100 {
                self.spatial_history.remove(0);
                self.temporal_history.remove(0);
            }

            // Calculate combined complexity
            complexity.combined_complexity = self.compute_combined_complexity(&complexity);

            // Estimate encoding difficulty
            complexity.encoding_difficulty = self.estimate_difficulty(&complexity);
        }

        complexity
    }

    /// Compute spatial complexity using block-based variance.
    fn compute_spatial_complexity(&self, luma: &[u8], stride: usize) -> f64 {
        let blocks_x = (self.width as usize) / self.block_size;
        let blocks_y = (self.height as usize) / self.block_size;

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

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

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

        if block_count == 0 {
            return 0.5;
        }

        let avg_variance = total_variance / block_count as f64;
        // Normalize to 0-1 range (assuming max variance ~1000)
        (avg_variance / 1000.0).min(1.0)
    }

    /// Compute block variance.
    fn compute_block_variance(&self, luma: &[u8], stride: usize, bx: usize, by: usize) -> f64 {
        let start_x = bx * self.block_size;
        let start_y = by * self.block_size;

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

        for y in 0..self.block_size {
            let row_y = start_y + y;
            if row_y >= self.height as usize {
                break;
            }

            for x in 0..self.block_size {
                let col_x = start_x + x;
                if col_x >= self.width as usize {
                    break;
                }

                let idx = row_y * stride + col_x;
                if idx < luma.len() {
                    let pixel = luma[idx] as u64;
                    sum += pixel;
                    sum_sq += pixel * pixel;
                    count += 1;
                }
            }
        }

        if count == 0 {
            return 0.0;
        }

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

    /// Compute overall frame variance.
    fn compute_variance(&self, luma: &[u8], stride: usize) -> f64 {
        let height = self.height as usize;
        let width = self.width as usize;

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

        for y in 0..height {
            for x in 0..width {
                let idx = y * stride + x;
                if idx < luma.len() {
                    let pixel = luma[idx] as u64;
                    sum += pixel;
                    sum_sq += pixel * pixel;
                    count += 1;
                }
            }
        }

        if count == 0 {
            return 0.0;
        }

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

    /// Compute Sum of Absolute Differences between frames.
    fn compute_sad(&self, current: &[u8], previous: &[u8], stride: usize) -> u64 {
        let height = self.height as usize;
        let width = self.width as usize;
        let mut sad = 0u64;

        for y in 0..height {
            for x in 0..width {
                let idx = y * stride + x;
                if idx < current.len() && idx < previous.len() {
                    let diff = (current[idx] as i32 - previous[idx] as i32).abs();
                    sad += diff as u64;
                }
            }
        }

        sad
    }

    /// Compute temporal complexity from SAD value.
    fn compute_temporal_complexity(&self, sad: u64) -> f64 {
        let pixels = (self.width as u64) * (self.height as u64);
        if pixels == 0 {
            return 0.5;
        }

        // Average SAD per pixel
        let avg_sad = sad as f64 / pixels as f64;
        // Normalize to 0-1 range (assuming max avg SAD ~50)
        (avg_sad / 50.0).min(1.0)
    }

    /// Detect scene changes based on SAD threshold.
    fn detect_scene_change(&self, sad: u64) -> bool {
        let pixels = (self.width as u64) * (self.height as u64);
        if pixels == 0 {
            return false;
        }

        let avg_sad = sad as f64 / pixels as f64;
        let normalized_sad = (avg_sad / 50.0).min(1.0);
        normalized_sad > self.scene_change_threshold
    }

    /// Compute combined complexity metric.
    fn compute_combined_complexity(&self, complexity: &FrameComplexity) -> f64 {
        // Weight spatial and temporal components
        let spatial_weight = 0.6;
        let temporal_weight = 0.4;

        spatial_weight * complexity.spatial_complexity
            + temporal_weight * complexity.temporal_complexity
    }

    /// Estimate encoding difficulty based on complexity metrics.
    fn estimate_difficulty(&self, complexity: &FrameComplexity) -> f64 {
        let mut difficulty = 1.0;

        // Base difficulty on combined complexity
        difficulty *= 0.5 + complexity.combined_complexity;

        // Adjust for frame type
        difficulty *= match complexity.frame_type {
            FrameType::Key => 2.0,    // Keyframes are more expensive
            FrameType::Inter => 1.0,  // Inter frames are baseline
            FrameType::BiDir => 0.8,  // B-frames can be cheaper
            FrameType::Switch => 1.5, // Switch frames need extra bits
        };

        // Scene changes require more bits
        if complexity.is_scene_change {
            difficulty *= 1.5;
        }

        // Normalize against historical average
        if !self.spatial_history.is_empty() {
            let avg_spatial: f64 =
                self.spatial_history.iter().sum::<f64>() / self.spatial_history.len() as f64;
            let avg_temporal: f64 =
                self.temporal_history.iter().sum::<f64>() / self.temporal_history.len() as f64;

            let historical_avg = 0.6 * avg_spatial + 0.4 * avg_temporal;
            if historical_avg > 0.01 {
                difficulty *= complexity.combined_complexity / historical_avg;
            }
        }

        difficulty.max(0.1).min(10.0)
    }

    /// Reset the analyzer state.
    pub fn reset(&mut self) {
        self.prev_frame = None;
        self.spatial_history.clear();
        self.temporal_history.clear();
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::frame::Plane;
    use oximedia_core::{PixelFormat, Rational, Timestamp};

    fn create_test_frame(width: u32, height: u32, value: u8) -> VideoFrame {
        let mut frame = VideoFrame::new(PixelFormat::Yuv420p, width, height);
        let size = (width * height) as usize;
        let data = vec![value; size];
        frame.planes.push(Plane::new(data, width as usize));
        frame.timestamp = Timestamp::new(0, Rational::new(1, 30));
        frame
    }

    #[test]
    fn test_complexity_analyzer_new() {
        let analyzer = ComplexityAnalyzer::new(1920, 1080);
        assert_eq!(analyzer.width, 1920);
        assert_eq!(analyzer.height, 1080);
        assert_eq!(analyzer.block_size, 16);
    }

    #[test]
    fn test_analyze_solid_frame() {
        let mut analyzer = ComplexityAnalyzer::new(320, 240);
        let frame = create_test_frame(320, 240, 128);

        let complexity = analyzer.analyze(&frame, 0);
        assert_eq!(complexity.frame_index, 0);
        assert!(complexity.spatial_complexity < 0.1); // Solid color = low spatial
        assert!(complexity.variance < 1.0); // Very low variance
    }

    #[test]
    fn test_scene_change_detection() {
        let mut analyzer = ComplexityAnalyzer::new(320, 240);

        // First frame
        let frame1 = create_test_frame(320, 240, 0);
        let complexity1 = analyzer.analyze(&frame1, 0);
        assert!(complexity1.is_scene_change); // First frame is always scene change

        // Very different second frame
        let frame2 = create_test_frame(320, 240, 255);
        let complexity2 = analyzer.analyze(&frame2, 1);
        assert!(complexity2.is_scene_change); // Should detect big difference
    }

    #[test]
    fn test_no_scene_change() {
        let mut analyzer = ComplexityAnalyzer::new(320, 240);

        // First frame
        let frame1 = create_test_frame(320, 240, 128);
        let _ = analyzer.analyze(&frame1, 0);

        // Similar second frame
        let frame2 = create_test_frame(320, 240, 130);
        let complexity2 = analyzer.analyze(&frame2, 1);
        assert!(!complexity2.is_scene_change); // Should not detect scene change
    }

    #[test]
    fn test_encoding_difficulty() {
        let mut analyzer = ComplexityAnalyzer::new(320, 240);
        let frame = create_test_frame(320, 240, 128);

        let complexity = analyzer.analyze(&frame, 0);
        assert!(complexity.encoding_difficulty > 0.0);
        assert!(complexity.encoding_difficulty <= 10.0);
    }
}