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
//! VP8 motion compensation and sub-pixel interpolation.
//!
//! This module implements motion compensation for VP8 inter prediction.
//! Motion vectors in VP8 have quarter-pixel precision and use 6-tap
//! Sinc-based interpolation filters.
//!
//! # Motion Vector Representation
//!
//! Motion vectors are stored in quarter-pixel units:
//! - Integer pixel: mv % 4 == 0
//! - Half pixel: mv % 4 == 2
//! - Quarter pixel: mv % 4 == 1 or 3

#![allow(dead_code)]
#![allow(clippy::cast_possible_truncation)]
#![allow(clippy::cast_sign_loss)]
#![allow(clippy::cast_possible_wrap)]
#![allow(clippy::too_many_arguments)]

/// Motion vector with quarter-pixel precision.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct MotionVector {
    /// Horizontal component (quarter-pixel units).
    pub x: i16,
    /// Vertical component (quarter-pixel units).
    pub y: i16,
}

impl MotionVector {
    /// Creates a new motion vector.
    #[must_use]
    pub const fn new(x: i16, y: i16) -> Self {
        Self { x, y }
    }

    /// Returns the integer pixel part of the horizontal component.
    #[must_use]
    pub const fn x_int(self) -> i32 {
        (self.x >> 2) as i32
    }

    /// Returns the integer pixel part of the vertical component.
    #[must_use]
    pub const fn y_int(self) -> i32 {
        (self.y >> 2) as i32
    }

    /// Returns the fractional part of the horizontal component (0-3).
    #[must_use]
    pub const fn x_frac(self) -> u8 {
        (self.x & 3) as u8
    }

    /// Returns the fractional part of the vertical component (0-3).
    #[must_use]
    pub const fn y_frac(self) -> u8 {
        (self.y & 3) as u8
    }

    /// Returns whether this is an integer-pixel motion vector.
    #[must_use]
    pub const fn is_integer(self) -> bool {
        self.x & 3 == 0 && self.y & 3 == 0
    }
}

/// VP8 6-tap interpolation filter coefficients.
///
/// VP8 uses different filter taps depending on the sub-pixel position.
/// Index: [fractional_position][tap]
const SUBPEL_FILTERS: [[i32; 6]; 8] = [
    [0, 0, 128, 0, 0, 0],     // Integer position (no filtering)
    [0, -6, 123, 12, -1, 0],  // 1/8 pixel
    [2, -11, 108, 36, -8, 1], // 1/4 pixel
    [0, -9, 93, 50, -6, 0],   // 3/8 pixel
    [3, -16, 77, 77, -16, 3], // 1/2 pixel (symmetric)
    [0, -6, 50, 93, -9, 0],   // 5/8 pixel
    [1, -8, 36, 108, -11, 2], // 3/4 pixel
    [0, -1, 12, 123, -6, 0],  // 7/8 pixel
];

/// Performs motion compensation for a block.
///
/// # Arguments
///
/// * `dst` - Destination buffer
/// * `dst_stride` - Stride of destination buffer
/// * `ref_frame` - Reference frame buffer
/// * `ref_stride` - Stride of reference frame
/// * `mv` - Motion vector
/// * `block_w` - Block width
/// * `block_h` - Block height
/// * `ref_x` - Reference block X position (integer pixels)
/// * `ref_y` - Reference block Y position (integer pixels)
#[allow(clippy::similar_names)]
pub fn motion_compensate(
    dst: &mut [u8],
    dst_stride: usize,
    ref_frame: &[u8],
    ref_stride: usize,
    mv: MotionVector,
    block_w: usize,
    block_h: usize,
    ref_x: usize,
    ref_y: usize,
) {
    let x_int = mv.x_int();
    let y_int = mv.y_int();
    let x_frac = mv.x_frac();
    let y_frac = mv.y_frac();

    // Calculate reference position
    let ref_x = (ref_x as i32 + x_int) as usize;
    let ref_y = (ref_y as i32 + y_int) as usize;

    if x_frac == 0 && y_frac == 0 {
        // Integer pixel - simple copy
        copy_block(
            dst, dst_stride, ref_frame, ref_stride, ref_x, ref_y, block_w, block_h,
        );
    } else if y_frac == 0 {
        // Horizontal filtering only
        filter_horizontal(
            dst, dst_stride, ref_frame, ref_stride, ref_x, ref_y, block_w, block_h, x_frac,
        );
    } else if x_frac == 0 {
        // Vertical filtering only
        filter_vertical(
            dst, dst_stride, ref_frame, ref_stride, ref_x, ref_y, block_w, block_h, y_frac,
        );
    } else {
        // Both horizontal and vertical filtering
        filter_2d(
            dst, dst_stride, ref_frame, ref_stride, ref_x, ref_y, block_w, block_h, x_frac, y_frac,
        );
    }
}

/// Copies a block without filtering (integer pixel).
fn copy_block(
    dst: &mut [u8],
    dst_stride: usize,
    src: &[u8],
    src_stride: usize,
    src_x: usize,
    src_y: usize,
    width: usize,
    height: usize,
) {
    for row in 0..height {
        let dst_offset = row * dst_stride;
        let src_offset = (src_y + row) * src_stride + src_x;

        if src_offset + width <= src.len() && dst_offset + width <= dst.len() {
            dst[dst_offset..dst_offset + width]
                .copy_from_slice(&src[src_offset..src_offset + width]);
        }
    }
}

/// Applies horizontal 6-tap filter.
fn filter_horizontal(
    dst: &mut [u8],
    dst_stride: usize,
    src: &[u8],
    src_stride: usize,
    src_x: usize,
    src_y: usize,
    width: usize,
    height: usize,
    frac: u8,
) {
    let filter = &SUBPEL_FILTERS[frac as usize];

    for row in 0..height {
        let dst_offset = row * dst_stride;
        let src_offset = (src_y + row) * src_stride + src_x;

        for col in 0..width {
            let mut sum = 0i32;

            // Apply 6-tap filter
            for (i, &tap) in filter.iter().enumerate() {
                let idx = src_offset + col + i;
                if idx < 2 || idx + 3 >= src.len() {
                    continue;
                }
                sum += tap * i32::from(src[idx - 2 + i]);
            }

            // Round and clamp
            let pixel = ((sum + 64) >> 7).clamp(0, 255) as u8;
            if dst_offset + col < dst.len() {
                dst[dst_offset + col] = pixel;
            }
        }
    }
}

/// Applies vertical 6-tap filter.
fn filter_vertical(
    dst: &mut [u8],
    dst_stride: usize,
    src: &[u8],
    src_stride: usize,
    src_x: usize,
    src_y: usize,
    width: usize,
    height: usize,
    frac: u8,
) {
    let filter = &SUBPEL_FILTERS[frac as usize];

    for row in 0..height {
        let dst_offset = row * dst_stride;

        for col in 0..width {
            let mut sum = 0i32;

            // Apply 6-tap filter vertically
            for (i, &tap) in filter.iter().enumerate() {
                let src_row = src_y + row + i;
                if src_row < 2 || src_row + 3 >= src.len() / src_stride {
                    continue;
                }
                let idx = (src_row - 2 + i) * src_stride + src_x + col;
                if idx < src.len() {
                    sum += tap * i32::from(src[idx]);
                }
            }

            // Round and clamp
            let pixel = ((sum + 64) >> 7).clamp(0, 255) as u8;
            if dst_offset + col < dst.len() {
                dst[dst_offset + col] = pixel;
            }
        }
    }
}

/// Applies 2D filtering (separable horizontal then vertical).
#[allow(clippy::similar_names)]
fn filter_2d(
    dst: &mut [u8],
    dst_stride: usize,
    src: &[u8],
    src_stride: usize,
    src_x: usize,
    src_y: usize,
    width: usize,
    height: usize,
    x_frac: u8,
    y_frac: u8,
) {
    // Temporary buffer for horizontal filtering
    let mut temp = vec![0u8; (height + 5) * width];

    // First apply horizontal filter
    filter_horizontal(
        &mut temp,
        width,
        src,
        src_stride,
        src_x,
        src_y.saturating_sub(2),
        width,
        height + 5,
        x_frac,
    );

    // Then apply vertical filter on the temp buffer
    filter_vertical(dst, dst_stride, &temp, width, 0, 2, width, height, y_frac);
}

/// Clamps motion vector to valid range.
///
/// # Arguments
///
/// * `mv` - Motion vector to clamp
/// * `x` - Current block X position
/// * `y` - Current block Y position
/// * `width` - Block width
/// * `height` - Block height
/// * `frame_w` - Frame width
/// * `frame_h` - Frame height
#[must_use]
pub fn clamp_mv(
    mv: MotionVector,
    x: usize,
    y: usize,
    width: usize,
    height: usize,
    frame_w: usize,
    frame_h: usize,
) -> MotionVector {
    let x = x as i32;
    let y = y as i32;
    let width = width as i32;
    let height = height as i32;
    let frame_w = frame_w as i32;
    let frame_h = frame_h as i32;

    // Calculate reference position
    let ref_x = x + mv.x_int();
    let ref_y = y + mv.y_int();

    // Clamp to frame boundaries
    let clamped_x = ref_x.clamp(0, frame_w - width);
    let clamped_y = ref_y.clamp(0, frame_h - height);

    // Adjust motion vector
    let new_x = ((clamped_x - x) << 2) as i16 + (mv.x & 3) as i16;
    let new_y = ((clamped_y - y) << 2) as i16 + (mv.y & 3) as i16;

    MotionVector::new(new_x, new_y)
}

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

    #[test]
    fn test_motion_vector() {
        let mv = MotionVector::new(10, 6); // 2.5 pixels horizontal, 1.5 pixels vertical

        assert_eq!(mv.x_int(), 2);
        assert_eq!(mv.y_int(), 1);
        assert_eq!(mv.x_frac(), 2);
        assert_eq!(mv.y_frac(), 2);
        assert!(!mv.is_integer());

        let int_mv = MotionVector::new(8, 12); // 2 pixels horizontal, 3 pixels vertical
        assert_eq!(int_mv.x_int(), 2);
        assert_eq!(int_mv.y_int(), 3);
        assert!(int_mv.is_integer());
    }

    #[test]
    fn test_subpel_filters() {
        // Integer position filter should be [0, 0, 128, 0, 0, 0]
        assert_eq!(SUBPEL_FILTERS[0], [0, 0, 128, 0, 0, 0]);

        // Half-pixel filter should be symmetric
        assert_eq!(SUBPEL_FILTERS[4], [3, -16, 77, 77, -16, 3]);

        // All filters should sum to 128 (approximately, due to rounding)
        for filter in &SUBPEL_FILTERS {
            let sum: i32 = filter.iter().sum();
            assert!((sum - 128).abs() <= 2);
        }
    }

    #[test]
    fn test_copy_block() {
        let src = vec![100u8; 64]; // 8x8 block of 100s
        let mut dst = vec![0u8; 64];

        copy_block(&mut dst, 8, &src, 8, 0, 0, 4, 4);

        // First 4x4 should be copied
        for row in 0..4 {
            for col in 0..4 {
                assert_eq!(dst[row * 8 + col], 100);
            }
        }
    }

    #[test]
    fn test_motion_compensate_integer() {
        let ref_frame = vec![50u8; 256]; // 16x16 reference
        let mut dst = vec![0u8; 64]; // 8x8 destination

        let mv = MotionVector::new(0, 0); // Integer pixel

        motion_compensate(&mut dst, 8, &ref_frame, 16, mv, 8, 8, 0, 0);

        // Should be simple copy
        assert!(dst.iter().all(|&p| p == 50));
    }

    #[test]
    fn test_clamp_mv() {
        let mv = MotionVector::new(100, 100); // Large motion vector

        let clamped = clamp_mv(mv, 10, 10, 4, 4, 32, 32);

        // Should be clamped to frame boundaries
        let ref_x = 10 + clamped.x_int();
        let ref_y = 10 + clamped.y_int();

        assert!(ref_x >= 0);
        assert!(ref_y >= 0);
        assert!(ref_x + 4 <= 32);
        assert!(ref_y + 4 <= 32);
    }

    #[test]
    fn test_clamp_mv_negative() {
        let mv = MotionVector::new(-100, -100); // Large negative MV

        let clamped = clamp_mv(mv, 10, 10, 4, 4, 32, 32);

        let ref_x = 10 + clamped.x_int();
        let ref_y = 10 + clamped.y_int();

        assert!(ref_x >= 0);
        assert!(ref_y >= 0);
    }

    #[test]
    fn test_filter_horizontal() {
        let src = vec![100u8; 256];
        let mut dst = vec![0u8; 64];

        // Apply horizontal filter with quarter-pixel precision
        filter_horizontal(&mut dst, 8, &src, 16, 0, 0, 8, 8, 2);

        // Output should be non-zero (filtered)
        assert!(dst.iter().any(|&p| p > 0));
    }

    #[test]
    fn test_filter_vertical() {
        let src = vec![100u8; 256];
        let mut dst = vec![0u8; 64];

        filter_vertical(&mut dst, 8, &src, 16, 0, 0, 8, 8, 2);

        // Output should be non-zero (filtered)
        assert!(dst.iter().any(|&p| p > 0));
    }

    #[test]
    fn test_filter_2d() {
        let src = vec![100u8; 256];
        let mut dst = vec![0u8; 64];

        filter_2d(&mut dst, 8, &src, 16, 0, 0, 8, 8, 2, 2);

        // Output should be non-zero (filtered)
        assert!(dst.iter().any(|&p| p > 0));
    }

    #[test]
    fn test_motion_vector_zero() {
        let mv = MotionVector::new(0, 0);
        assert_eq!(mv.x_int(), 0);
        assert_eq!(mv.y_int(), 0);
        assert_eq!(mv.x_frac(), 0);
        assert_eq!(mv.y_frac(), 0);
        assert!(mv.is_integer());
    }
}