oximedia-accel 0.1.2

Hardware acceleration layer for OxiMedia using Vulkan compute
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
//! Deinterlacing operations for interlaced video content.
//!
//! Provides three deinterlacing methods:
//!
//! - **Bob**: Interpolates missing lines from the selected field by averaging
//!   adjacent field lines. Doubles frame rate, no temporal artifacts.
//!
//! - **Weave**: Combines top and bottom fields directly. Preserves full
//!   vertical resolution but produces combing artifacts on motion.
//!
//! - **Motion-adaptive**: Blends bob and weave based on per-pixel motion
//!   detection. Uses weave where the image is static for sharpness, and
//!   bob where motion is detected to reduce combing.

use crate::error::{AccelError, AccelResult};
use rayon::prelude::*;

/// Deinterlacing method to apply.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DeinterlaceMethod {
    /// Bob deinterlacing: doubles the selected field vertically.
    /// Fast and free of combing, but loses half the vertical resolution.
    Bob,
    /// Weave deinterlacing: interleaves top and bottom fields.
    /// Full resolution but combing artifacts on motion.
    Weave,
    /// Motion-adaptive deinterlacing: blends bob and weave per-pixel
    /// based on inter-field motion detection.
    MotionAdaptive,
}

/// Which field to treat as the "current" field.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FieldOrder {
    /// Top field first (even rows = top field).
    TopFieldFirst,
    /// Bottom field first (odd rows = top field).
    BottomFieldFirst,
}

/// Configuration for the deinterlace operation.
#[derive(Debug, Clone)]
pub struct DeinterlaceConfig {
    /// Deinterlacing method.
    pub method: DeinterlaceMethod,
    /// Field order of the input.
    pub field_order: FieldOrder,
    /// Motion threshold for motion-adaptive mode (0..255).
    /// Pixels with inter-field difference above this threshold are treated
    /// as moving and use bob; below use weave.
    pub motion_threshold: u8,
}

impl Default for DeinterlaceConfig {
    fn default() -> Self {
        Self {
            method: DeinterlaceMethod::MotionAdaptive,
            field_order: FieldOrder::TopFieldFirst,
            motion_threshold: 20,
        }
    }
}

/// Performs deinterlacing on a single grayscale or per-channel frame.
///
/// `input` must contain `width * height * channels` bytes in row-major order.
///
/// # Errors
///
/// Returns an error if the input buffer size does not match
/// `width * height * channels`.
pub fn deinterlace(
    input: &[u8],
    width: u32,
    height: u32,
    channels: u32,
    config: &DeinterlaceConfig,
) -> AccelResult<Vec<u8>> {
    let expected = (width * height * channels) as usize;
    if input.len() != expected {
        return Err(AccelError::BufferSizeMismatch {
            expected,
            actual: input.len(),
        });
    }
    if height < 2 {
        return Ok(input.to_vec());
    }

    match config.method {
        DeinterlaceMethod::Bob => deinterlace_bob(input, width, height, channels, config),
        DeinterlaceMethod::Weave => Ok(input.to_vec()), // weave is identity for single-frame
        DeinterlaceMethod::MotionAdaptive => {
            deinterlace_motion_adaptive(input, width, height, channels, config)
        }
    }
}

/// Performs deinterlacing on interlaced content given two fields.
///
/// `top_field` contains even rows; `bottom_field` contains odd rows.
/// Each field has `width * (height/2) * channels` bytes.
///
/// # Errors
///
/// Returns an error if field sizes do not match expectations.
pub fn deinterlace_fields(
    top_field: &[u8],
    bottom_field: &[u8],
    width: u32,
    height: u32,
    channels: u32,
    config: &DeinterlaceConfig,
) -> AccelResult<Vec<u8>> {
    let field_height = height / 2;
    let field_expected = (width * field_height * channels) as usize;

    if top_field.len() != field_expected {
        return Err(AccelError::BufferSizeMismatch {
            expected: field_expected,
            actual: top_field.len(),
        });
    }
    if bottom_field.len() != field_expected {
        return Err(AccelError::BufferSizeMismatch {
            expected: field_expected,
            actual: bottom_field.len(),
        });
    }

    let stride = (width * channels) as usize;
    let mut frame = vec![0u8; (width * height * channels) as usize];

    // Interleave fields into a full frame
    for y in 0..field_height as usize {
        let top_row_start = y * stride;
        let bottom_row_start = y * stride;
        let even_row_start = (y * 2) * stride;
        let odd_row_start = (y * 2 + 1) * stride;

        frame[even_row_start..even_row_start + stride]
            .copy_from_slice(&top_field[top_row_start..top_row_start + stride]);
        frame[odd_row_start..odd_row_start + stride]
            .copy_from_slice(&bottom_field[bottom_row_start..bottom_row_start + stride]);
    }

    match config.method {
        DeinterlaceMethod::Weave => Ok(frame),
        DeinterlaceMethod::Bob => deinterlace_bob(&frame, width, height, channels, config),
        DeinterlaceMethod::MotionAdaptive => {
            deinterlace_motion_adaptive(&frame, width, height, channels, config)
        }
    }
}

/// Bob deinterlacing: interpolates missing lines from neighboring field lines.
fn deinterlace_bob(
    input: &[u8],
    width: u32,
    height: u32,
    channels: u32,
    config: &DeinterlaceConfig,
) -> AccelResult<Vec<u8>> {
    let stride = (width * channels) as usize;
    let mut output = input.to_vec();

    // Determine which lines belong to the "other" field (to be interpolated)
    let interpolate_even = matches!(config.field_order, FieldOrder::BottomFieldFirst);

    output
        .par_chunks_exact_mut(stride)
        .enumerate()
        .for_each(|(y, row)| {
            let is_even = y % 2 == 0;
            let should_interpolate =
                (is_even && interpolate_even) || (!is_even && !interpolate_even);

            if should_interpolate {
                // Interpolate this row from its neighbors
                let above = if y > 0 { y - 1 } else { y + 1 };
                let below = if y + 1 < height as usize {
                    y + 1
                } else if y > 0 {
                    y - 1
                } else {
                    y
                };

                for x in 0..stride {
                    let a = f32::from(input[above * stride + x]);
                    let b = f32::from(input[below * stride + x]);
                    row[x] = ((a + b) * 0.5).clamp(0.0, 255.0) as u8;
                }
            }
        });

    Ok(output)
}

/// Motion-adaptive deinterlacing: blends bob and weave per-pixel.
///
/// For each pixel in the interpolated field lines, computes the absolute
/// difference between the lines above and below (inter-field motion).
/// If motion exceeds the threshold, uses the bob (interpolated) value;
/// otherwise keeps the weave (original) value for maximum sharpness.
fn deinterlace_motion_adaptive(
    input: &[u8],
    width: u32,
    height: u32,
    channels: u32,
    config: &DeinterlaceConfig,
) -> AccelResult<Vec<u8>> {
    let stride = (width * channels) as usize;
    let bob = deinterlace_bob(input, width, height, channels, config)?;
    let mut output = input.to_vec();
    let threshold = f32::from(config.motion_threshold);

    let interpolate_even = matches!(config.field_order, FieldOrder::BottomFieldFirst);

    output
        .par_chunks_exact_mut(stride)
        .enumerate()
        .for_each(|(y, row)| {
            let is_even = y % 2 == 0;
            let should_interpolate =
                (is_even && interpolate_even) || (!is_even && !interpolate_even);

            if should_interpolate {
                let above = if y > 0 { y - 1 } else { y + 1 };
                let below = if y + 1 < height as usize {
                    y + 1
                } else if y > 0 {
                    y - 1
                } else {
                    y
                };

                for x in 0..stride {
                    let a = f32::from(input[above * stride + x]);
                    let b = f32::from(input[below * stride + x]);
                    let motion = (a - b).abs();

                    if motion > threshold {
                        // High motion: use bob (interpolated) value
                        row[x] = bob[y * stride + x];
                    }
                    // else: keep weave (original) value for sharpness
                }
            }
        });

    Ok(output)
}

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

    fn make_interlaced_frame(width: u32, height: u32) -> Vec<u8> {
        let mut frame = vec![0u8; (width * height) as usize];
        for y in 0..height {
            let val = if y % 2 == 0 { 200u8 } else { 50u8 };
            for x in 0..width {
                frame[(y * width + x) as usize] = val;
            }
        }
        frame
    }

    #[test]
    fn test_bob_deinterlace_basic() {
        let width = 8u32;
        let height = 4u32;
        let frame = make_interlaced_frame(width, height);
        let config = DeinterlaceConfig {
            method: DeinterlaceMethod::Bob,
            field_order: FieldOrder::TopFieldFirst,
            motion_threshold: 20,
        };
        let result =
            deinterlace(&frame, width, height, 1, &config).expect("deinterlace should succeed");
        assert_eq!(result.len(), (width * height) as usize);
        // Even rows (top field) should be unchanged
        assert_eq!(result[0], 200);
        assert_eq!(result[(2 * width) as usize], 200);
        // Odd rows (interpolated) should be average of neighbors
        // Row 1: avg(200, 200) = 200
        assert_eq!(result[width as usize], 200);
    }

    #[test]
    fn test_weave_deinterlace_identity() {
        let width = 4u32;
        let height = 4u32;
        let frame = make_interlaced_frame(width, height);
        let config = DeinterlaceConfig {
            method: DeinterlaceMethod::Weave,
            ..Default::default()
        };
        let result =
            deinterlace(&frame, width, height, 1, &config).expect("deinterlace should succeed");
        assert_eq!(result, frame);
    }

    #[test]
    fn test_motion_adaptive_deinterlace() {
        let width = 8u32;
        let height = 4u32;
        let frame = make_interlaced_frame(width, height);
        let config = DeinterlaceConfig {
            method: DeinterlaceMethod::MotionAdaptive,
            field_order: FieldOrder::TopFieldFirst,
            motion_threshold: 10,
        };
        let result =
            deinterlace(&frame, width, height, 1, &config).expect("deinterlace should succeed");
        assert_eq!(result.len(), (width * height) as usize);
        // Even rows should remain unchanged
        assert_eq!(result[0], 200);
    }

    #[test]
    fn test_deinterlace_buffer_mismatch() {
        let config = DeinterlaceConfig::default();
        let result = deinterlace(&[0u8; 10], 4, 4, 1, &config);
        assert!(result.is_err());
    }

    #[test]
    fn test_deinterlace_tiny_height() {
        let width = 4u32;
        let height = 1u32;
        let frame = vec![128u8; (width * height) as usize];
        let config = DeinterlaceConfig::default();
        let result =
            deinterlace(&frame, width, height, 1, &config).expect("deinterlace should succeed");
        assert_eq!(result, frame);
    }

    #[test]
    fn test_bob_multichannel() {
        let width = 4u32;
        let height = 4u32;
        let channels = 3u32;
        let mut frame = vec![0u8; (width * height * channels) as usize];
        for y in 0..height {
            let val = if y % 2 == 0 { 240u8 } else { 16u8 };
            for x in 0..width {
                for c in 0..channels {
                    frame[((y * width + x) * channels + c) as usize] = val;
                }
            }
        }
        let config = DeinterlaceConfig {
            method: DeinterlaceMethod::Bob,
            field_order: FieldOrder::TopFieldFirst,
            motion_threshold: 20,
        };
        let result = deinterlace(&frame, width, height, channels, &config)
            .expect("deinterlace should succeed");
        assert_eq!(result.len(), (width * height * channels) as usize);
        // Even row pixels unchanged
        assert_eq!(result[0], 240);
        assert_eq!(result[1], 240);
        assert_eq!(result[2], 240);
    }

    #[test]
    fn test_bottom_field_first_bob() {
        let width = 4u32;
        let height = 4u32;
        let frame = make_interlaced_frame(width, height);
        let config = DeinterlaceConfig {
            method: DeinterlaceMethod::Bob,
            field_order: FieldOrder::BottomFieldFirst,
            motion_threshold: 20,
        };
        let result =
            deinterlace(&frame, width, height, 1, &config).expect("deinterlace should succeed");
        // In BFF mode, even rows are interpolated, odd rows kept
        assert_eq!(result[width as usize], 50); // odd row unchanged
    }

    #[test]
    fn test_deinterlace_fields_weave() {
        let width = 4u32;
        let height = 4u32;
        let field_height = height / 2;
        let top = vec![200u8; (width * field_height) as usize];
        let bottom = vec![50u8; (width * field_height) as usize];
        let config = DeinterlaceConfig {
            method: DeinterlaceMethod::Weave,
            ..Default::default()
        };
        let result = deinterlace_fields(&top, &bottom, width, height, 1, &config)
            .expect("deinterlace_fields should succeed");
        assert_eq!(result.len(), (width * height) as usize);
        // Even rows from top field
        assert_eq!(result[0], 200);
        // Odd rows from bottom field
        assert_eq!(result[width as usize], 50);
    }

    #[test]
    fn test_deinterlace_fields_bob() {
        let width = 4u32;
        let height = 4u32;
        let field_height = height / 2;
        let top = vec![200u8; (width * field_height) as usize];
        let bottom = vec![50u8; (width * field_height) as usize];
        let config = DeinterlaceConfig {
            method: DeinterlaceMethod::Bob,
            field_order: FieldOrder::TopFieldFirst,
            motion_threshold: 20,
        };
        let result = deinterlace_fields(&top, &bottom, width, height, 1, &config)
            .expect("deinterlace_fields should succeed");
        assert_eq!(result.len(), (width * height) as usize);
    }

    #[test]
    fn test_deinterlace_fields_size_mismatch() {
        let config = DeinterlaceConfig::default();
        let result = deinterlace_fields(&[0u8; 10], &[0u8; 8], 4, 4, 1, &config);
        assert!(result.is_err());
    }

    #[test]
    fn test_motion_adaptive_static_content() {
        // Uniform frame: no motion detected, should use weave (identity)
        let width = 8u32;
        let height = 4u32;
        let frame = vec![128u8; (width * height) as usize];
        let config = DeinterlaceConfig {
            method: DeinterlaceMethod::MotionAdaptive,
            field_order: FieldOrder::TopFieldFirst,
            motion_threshold: 10,
        };
        let result =
            deinterlace(&frame, width, height, 1, &config).expect("deinterlace should succeed");
        // Static content: all values should remain 128
        assert!(result.iter().all(|&v| v == 128));
    }

    #[test]
    fn test_deinterlace_config_default() {
        let config = DeinterlaceConfig::default();
        assert_eq!(config.method, DeinterlaceMethod::MotionAdaptive);
        assert_eq!(config.field_order, FieldOrder::TopFieldFirst);
        assert_eq!(config.motion_threshold, 20);
    }

    #[test]
    fn test_bob_preserves_field_lines() {
        let width = 4u32;
        let height = 6u32;
        let mut frame = vec![0u8; (width * height) as usize];
        // Set each row to a distinct value
        for y in 0..height {
            for x in 0..width {
                frame[(y * width + x) as usize] = (y * 40) as u8;
            }
        }
        let config = DeinterlaceConfig {
            method: DeinterlaceMethod::Bob,
            field_order: FieldOrder::TopFieldFirst,
            motion_threshold: 20,
        };
        let result =
            deinterlace(&frame, width, height, 1, &config).expect("deinterlace should succeed");
        // Even rows (field lines) should be preserved
        assert_eq!(result[0], 0);
        assert_eq!(result[(2 * width) as usize], 80);
        assert_eq!(result[(4 * width) as usize], 160);
    }

    #[test]
    fn test_large_frame_deinterlace() {
        let width = 1920u32;
        let height = 1080u32;
        let frame = vec![100u8; (width * height) as usize];
        let config = DeinterlaceConfig {
            method: DeinterlaceMethod::Bob,
            field_order: FieldOrder::TopFieldFirst,
            motion_threshold: 20,
        };
        let result =
            deinterlace(&frame, width, height, 1, &config).expect("deinterlace should succeed");
        assert_eq!(result.len(), (width * height) as usize);
    }
}