oximedia-gpu 0.1.1

GPU compute pipeline using WGPU for OxiMedia - cross-platform acceleration
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
//! GPU-accelerated video frame processing.
//!
//! This module provides CPU-fallback simulations of GPU compute operations
//! for video frame processing. In production, these would be replaced by
//! actual WGPU compute shader kernels.

use crate::{GpuError, Result};
use std::time::{SystemTime, UNIX_EPOCH};

/// Configuration for GPU-based frame operations.
#[derive(Debug, Clone)]
pub struct FrameProcessConfig {
    /// Frame width in pixels.
    pub width: u32,
    /// Frame height in pixels.
    pub height: u32,
    /// Number of channels: 1=Y (grayscale), 3=RGB, 4=RGBA.
    pub channels: u8,
}

/// Result of a GPU frame processing operation.
#[derive(Debug, Clone)]
pub struct FrameProcessResult {
    /// Processed pixel data.
    pub data: Vec<u8>,
    /// Frame width.
    pub width: u32,
    /// Frame height.
    pub height: u32,
    /// Processing time in microseconds.
    pub processing_time_us: u64,
}

/// GPU-accelerated video frame processor.
///
/// Provides CPU-fallback implementations of common video frame operations
/// that would execute on the GPU in production environments.
pub struct VideoFrameProcessor {
    config: FrameProcessConfig,
}

impl VideoFrameProcessor {
    /// Create a new `VideoFrameProcessor` with the given configuration.
    #[must_use]
    pub fn new(config: FrameProcessConfig) -> Self {
        Self { config }
    }

    /// Get the current timestamp in microseconds (for timing).
    fn timestamp_us() -> u64 {
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .subsec_micros()
            .into()
    }

    /// Validate that a frame buffer has the expected size.
    fn validate_frame(&self, frame: &[u8]) -> Result<()> {
        let expected = self.config.width as usize
            * self.config.height as usize
            * self.config.channels as usize;
        if frame.len() != expected {
            return Err(GpuError::InvalidBufferSize {
                expected,
                actual: frame.len(),
            });
        }
        Ok(())
    }

    /// Simulate GPU-accelerated frame histogram computation.
    ///
    /// For each channel, counts pixel value occurrences (0-255).
    /// Returns a `Vec` of `256 * channels` counts (interleaved per channel).
    ///
    /// # Errors
    ///
    /// Returns an error if the frame buffer size does not match the configured dimensions.
    pub fn compute_histogram(&self, frame: &[u8]) -> Result<Vec<u32>> {
        self.validate_frame(frame)?;

        let channels = self.config.channels as usize;
        let mut histogram = vec![0u32; 256 * channels];

        for (i, &pixel) in frame.iter().enumerate() {
            let ch = i % channels;
            histogram[ch * 256 + pixel as usize] += 1;
        }

        Ok(histogram)
    }

    /// Simulate GPU-accelerated frame brightness adjustment.
    ///
    /// Adds `delta` to each pixel value, clamping the result to `[0, 255]`.
    ///
    /// # Errors
    ///
    /// Returns an error if the frame buffer size does not match the configured dimensions.
    pub fn adjust_brightness(&self, frame: &[u8], delta: i16) -> Result<Vec<u8>> {
        self.validate_frame(frame)?;

        let result = frame
            .iter()
            .map(|&p| (i16::from(p) + delta).clamp(0, 255) as u8)
            .collect();

        Ok(result)
    }

    /// Simulate GPU-accelerated contrast adjustment.
    ///
    /// For each pixel: `clamp((pixel - 128) * factor + 128, 0, 255)`.
    ///
    /// # Errors
    ///
    /// Returns an error if the frame buffer size does not match the configured dimensions.
    pub fn adjust_contrast(&self, frame: &[u8], factor: f32) -> Result<Vec<u8>> {
        self.validate_frame(frame)?;

        let result = frame
            .iter()
            .map(|&p| {
                let adjusted = (f32::from(p) - 128.0) * factor + 128.0;
                adjusted.clamp(0.0, 255.0) as u8
            })
            .collect();

        Ok(result)
    }

    /// Simulate GPU-accelerated saturation adjustment for RGB frames (3 channels).
    ///
    /// Converts each RGB pixel to HSL, multiplies the S component by `factor`,
    /// then converts back to RGB. For non-RGB frames this is a no-op copy.
    ///
    /// # Errors
    ///
    /// Returns an error if the frame buffer size does not match the configured dimensions.
    pub fn adjust_saturation(&self, frame: &[u8], factor: f32) -> Result<Vec<u8>> {
        self.validate_frame(frame)?;

        if self.config.channels != 3 {
            // For non-RGB frames, return as-is (saturation is RGB concept)
            return Ok(frame.to_vec());
        }

        let mut result = Vec::with_capacity(frame.len());
        for chunk in frame.chunks(3) {
            let (r, g, b) = (
                f32::from(chunk[0]) / 255.0,
                f32::from(chunk[1]) / 255.0,
                f32::from(chunk[2]) / 255.0,
            );

            let (h, s, l) = rgb_to_hsl(r, g, b);
            let new_s = (s * factor).clamp(0.0, 1.0);
            let (nr, ng, nb) = hsl_to_rgb(h, new_s, l);

            result.push((nr * 255.0).clamp(0.0, 255.0) as u8);
            result.push((ng * 255.0).clamp(0.0, 255.0) as u8);
            result.push((nb * 255.0).clamp(0.0, 255.0) as u8);
        }

        Ok(result)
    }

    /// Compute frame difference (absolute difference per pixel).
    ///
    /// # Errors
    ///
    /// Returns an error if either frame buffer size does not match the configured dimensions.
    pub fn frame_difference(&self, frame_a: &[u8], frame_b: &[u8]) -> Result<Vec<u8>> {
        self.validate_frame(frame_a)?;
        self.validate_frame(frame_b)?;

        let result = frame_a
            .iter()
            .zip(frame_b.iter())
            .map(|(&a, &b)| a.abs_diff(b))
            .collect();

        Ok(result)
    }

    /// Compute mean absolute error between two frames.
    ///
    /// # Errors
    ///
    /// Returns an error if either frame buffer size does not match the configured dimensions.
    pub fn mean_absolute_error(&self, frame_a: &[u8], frame_b: &[u8]) -> Result<f64> {
        self.validate_frame(frame_a)?;
        self.validate_frame(frame_b)?;

        if frame_a.is_empty() {
            return Ok(0.0);
        }

        let sum: u64 = frame_a
            .iter()
            .zip(frame_b.iter())
            .map(|(&a, &b)| u64::from(a.abs_diff(b)))
            .sum();

        Ok(sum as f64 / frame_a.len() as f64)
    }

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

    /// Process a frame and return a `FrameProcessResult` with timing information.
    ///
    /// This is a convenience wrapper that applies brightness adjustment and
    /// records the simulated GPU processing time.
    ///
    /// # Errors
    ///
    /// Returns an error if the frame buffer size does not match the configured dimensions.
    pub fn process_frame(&self, frame: &[u8], brightness_delta: i16) -> Result<FrameProcessResult> {
        let start = Self::timestamp_us();
        let data = self.adjust_brightness(frame, brightness_delta)?;
        let end = Self::timestamp_us();

        Ok(FrameProcessResult {
            data,
            width: self.config.width,
            height: self.config.height,
            processing_time_us: end.saturating_sub(start),
        })
    }
}

// ---------------------------------------------------------------------------
// HSL / RGB conversion helpers
// ---------------------------------------------------------------------------

/// Convert RGB (0.0–1.0 each) to HSL.
fn rgb_to_hsl(r: f32, g: f32, b: f32) -> (f32, f32, f32) {
    let max = r.max(g).max(b);
    let min = r.min(g).min(b);
    let delta = max - min;
    let l = (max + min) / 2.0;

    if delta < f32::EPSILON {
        return (0.0, 0.0, l);
    }

    let s = if l < 0.5 {
        delta / (max + min)
    } else {
        delta / (2.0 - max - min)
    };

    let h = if (max - r).abs() < f32::EPSILON {
        ((g - b) / delta).rem_euclid(6.0) / 6.0
    } else if (max - g).abs() < f32::EPSILON {
        ((b - r) / delta + 2.0) / 6.0
    } else {
        ((r - g) / delta + 4.0) / 6.0
    };

    (h, s, l)
}

/// Helper for HSL-to-RGB conversion.
fn hsl_hue_to_rgb(p: f32, q: f32, mut t: f32) -> f32 {
    if t < 0.0 {
        t += 1.0;
    }
    if t > 1.0 {
        t -= 1.0;
    }
    if t < 1.0 / 6.0 {
        return p + (q - p) * 6.0 * t;
    }
    if t < 1.0 / 2.0 {
        return q;
    }
    if t < 2.0 / 3.0 {
        return p + (q - p) * (2.0 / 3.0 - t) * 6.0;
    }
    p
}

/// Convert HSL to RGB (0.0–1.0 each).
fn hsl_to_rgb(h: f32, s: f32, l: f32) -> (f32, f32, f32) {
    if s < f32::EPSILON {
        return (l, l, l);
    }

    let q = if l < 0.5 {
        l * (1.0 + s)
    } else {
        l + s - l * s
    };
    let p = 2.0 * l - q;

    let r = hsl_hue_to_rgb(p, q, h + 1.0 / 3.0);
    let g = hsl_hue_to_rgb(p, q, h);
    let b = hsl_hue_to_rgb(p, q, h - 1.0 / 3.0);

    (r, g, b)
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn make_processor(w: u32, h: u32, ch: u8) -> VideoFrameProcessor {
        VideoFrameProcessor::new(FrameProcessConfig {
            width: w,
            height: h,
            channels: ch,
        })
    }

    #[test]
    fn test_histogram_uniform_frame() {
        // 4x4 single-channel frame, all pixels = 128
        let proc = make_processor(4, 4, 1);
        let frame = vec![128u8; 16];
        let hist = proc
            .compute_histogram(&frame)
            .expect("histogram computation should succeed");

        assert_eq!(hist.len(), 256);
        assert_eq!(hist[128], 16, "All 16 pixels should be at bin 128");
        for i in 0..256 {
            if i != 128 {
                assert_eq!(hist[i], 0);
            }
        }
    }

    #[test]
    fn test_histogram_rgb_frame() {
        // 2x2 RGB frame: all red=255, green=0, blue=128
        let proc = make_processor(2, 2, 3);
        let frame: Vec<u8> = (0..4).flat_map(|_| vec![255u8, 0u8, 128u8]).collect();
        let hist = proc
            .compute_histogram(&frame)
            .expect("histogram computation should succeed");

        assert_eq!(hist.len(), 768); // 3 * 256
                                     // Channel 0 (red): all 4 pixels at 255
        assert_eq!(hist[0 * 256 + 255], 4);
        // Channel 1 (green): all 4 pixels at 0
        assert_eq!(hist[1 * 256 + 0], 4);
        // Channel 2 (blue): all 4 pixels at 128
        assert_eq!(hist[2 * 256 + 128], 4);
    }

    #[test]
    fn test_adjust_brightness_clamp_up() {
        let proc = make_processor(2, 2, 1);
        let frame = vec![200u8, 100u8, 50u8, 10u8];
        let result = proc
            .adjust_brightness(&frame, 100)
            .expect("brightness adjustment should succeed");
        assert_eq!(result, vec![255, 200, 150, 110]);
    }

    #[test]
    fn test_adjust_brightness_clamp_down() {
        let proc = make_processor(2, 2, 1);
        let frame = vec![200u8, 100u8, 50u8, 10u8];
        let result = proc
            .adjust_brightness(&frame, -100)
            .expect("brightness adjustment should succeed");
        assert_eq!(result, vec![100, 0, 0, 0]);
    }

    #[test]
    fn test_adjust_contrast() {
        let proc = make_processor(1, 1, 1);
        // pixel=128, factor=1.0 → should stay at 128
        let frame = vec![128u8];
        let result = proc
            .adjust_contrast(&frame, 1.0)
            .expect("contrast adjustment should succeed");
        assert_eq!(result[0], 128);
    }

    #[test]
    fn test_adjust_contrast_increase() {
        let proc = make_processor(1, 1, 1);
        // pixel=200, factor=2.0 → (200-128)*2+128 = 272 → clamped to 255
        let frame = vec![200u8];
        let result = proc
            .adjust_contrast(&frame, 2.0)
            .expect("contrast adjustment should succeed");
        assert_eq!(result[0], 255);
    }

    #[test]
    fn test_adjust_saturation_no_change_at_one() {
        let proc = make_processor(1, 1, 3);
        let frame = vec![255u8, 0u8, 0u8]; // pure red
        let result = proc
            .adjust_saturation(&frame, 1.0)
            .expect("saturation adjustment should succeed");
        // With factor=1.0, saturation should be unchanged, red should stay red
        assert_eq!(result[0], 255);
        assert_eq!(result[1], 0);
        assert_eq!(result[2], 0);
    }

    #[test]
    fn test_adjust_saturation_zero_desaturates() {
        let proc = make_processor(1, 1, 3);
        let frame = vec![255u8, 0u8, 0u8]; // pure red
        let result = proc
            .adjust_saturation(&frame, 0.0)
            .expect("saturation adjustment should succeed");
        // With factor=0.0, becomes grayscale: all channels equal
        assert_eq!(result[0], result[1]);
        assert_eq!(result[1], result[2]);
    }

    #[test]
    fn test_frame_difference() {
        let proc = make_processor(2, 2, 1);
        let a = vec![100u8, 200u8, 50u8, 0u8];
        let b = vec![80u8, 210u8, 50u8, 255u8];
        let diff = proc
            .frame_difference(&a, &b)
            .expect("frame difference should succeed");
        assert_eq!(diff, vec![20, 10, 0, 255]);
    }

    #[test]
    fn test_mean_absolute_error() {
        let proc = make_processor(2, 2, 1);
        let a = vec![100u8, 100u8, 100u8, 100u8];
        let b = vec![110u8, 90u8, 100u8, 120u8];
        // diffs: 10, 10, 0, 20 → mean = 10.0
        let mae = proc
            .mean_absolute_error(&a, &b)
            .expect("MAE computation should succeed");
        assert!((mae - 10.0).abs() < 1e-9);
    }

    #[test]
    fn test_invalid_frame_size() {
        let proc = make_processor(4, 4, 1);
        let frame = vec![0u8; 10]; // wrong size
        assert!(proc.compute_histogram(&frame).is_err());
        assert!(proc.adjust_brightness(&frame, 0).is_err());
        assert!(proc.adjust_contrast(&frame, 1.0).is_err());
    }

    #[test]
    fn test_config_accessor() {
        let config = FrameProcessConfig {
            width: 1920,
            height: 1080,
            channels: 4,
        };
        let proc = VideoFrameProcessor::new(config.clone());
        assert_eq!(proc.config().width, 1920);
        assert_eq!(proc.config().height, 1080);
        assert_eq!(proc.config().channels, 4);
    }
}