oximedia-codec 0.1.5

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
//! AV1 motion compensation SIMD operations.
//!
//! Implements interpolation filters and motion compensation for AV1.

use crate::simd::traits::SimdOps;
use crate::simd::types::{I16x8, I32x4, U8x16};

/// AV1 motion compensation SIMD operations.
pub struct MotionCompSimd<S> {
    simd: S,
}

impl<S: SimdOps> MotionCompSimd<S> {
    /// Create a new motion compensation SIMD instance.
    #[inline]
    pub const fn new(simd: S) -> Self {
        Self { simd }
    }

    /// Copy a block without interpolation (integer-pel motion).
    pub fn copy_block(
        &self,
        src: &[u8],
        src_stride: usize,
        dst: &mut [u8],
        dst_stride: usize,
        width: usize,
        height: usize,
    ) {
        for y in 0..height {
            let src_offset = y * src_stride;
            let dst_offset = y * dst_stride;

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

    /// Horizontal 8-tap interpolation filter.
    ///
    /// Applies an 8-tap filter horizontally for sub-pixel motion compensation.
    pub fn filter_h_8tap(
        &self,
        src: &[u8],
        src_stride: usize,
        dst: &mut [u8],
        dst_stride: usize,
        coeffs: &[i16; 8],
        width: usize,
        height: usize,
    ) {
        for y in 0..height {
            for x in 0..width {
                let src_offset = y * src_stride + x;
                let dst_offset = y * dst_stride + x;

                if src.len() < src_offset + 8 || dst_offset >= dst.len() {
                    continue;
                }

                // Load 8 source pixels
                let mut pixels = I16x8::zero();
                for i in 0..8 {
                    if src_offset + i < src.len() {
                        pixels[i] = i16::from(src[src_offset + i]);
                    }
                }

                // Load filter coefficients
                let filter = I16x8::from_array(*coeffs);

                // Multiply and accumulate
                let products = self.simd.mul_i16x8(pixels, filter);
                let sum = self.simd.horizontal_sum_i16x8(products);

                // Round and shift
                let result = (sum + 64) >> 7;
                dst[dst_offset] = result.clamp(0, 255) as u8;
            }
        }
    }

    /// Vertical 8-tap interpolation filter.
    pub fn filter_v_8tap(
        &self,
        src: &[u8],
        src_stride: usize,
        dst: &mut [u8],
        dst_stride: usize,
        coeffs: &[i16; 8],
        width: usize,
        height: usize,
    ) {
        for y in 0..height {
            for x in 0..width {
                let dst_offset = y * dst_stride + x;

                if dst_offset >= dst.len() {
                    continue;
                }

                // Load 8 vertical pixels
                let mut pixels = I16x8::zero();
                for i in 0..8 {
                    let src_offset = (y + i) * src_stride + x;
                    if src_offset < src.len() {
                        pixels[i] = i16::from(src[src_offset]);
                    }
                }

                // Load filter coefficients
                let filter = I16x8::from_array(*coeffs);

                // Multiply and accumulate
                let products = self.simd.mul_i16x8(pixels, filter);
                let sum = self.simd.horizontal_sum_i16x8(products);

                // Round and shift
                let result = (sum + 64) >> 7;
                dst[dst_offset] = result.clamp(0, 255) as u8;
            }
        }
    }

    /// 2D 8-tap interpolation (both horizontal and vertical).
    #[allow(clippy::too_many_arguments)]
    pub fn filter_2d_8tap(
        &self,
        src: &[u8],
        src_stride: usize,
        dst: &mut [u8],
        dst_stride: usize,
        h_coeffs: &[i16; 8],
        v_coeffs: &[i16; 8],
        width: usize,
        height: usize,
    ) {
        // Allocate temporary buffer for horizontal filtering
        let temp_size = (height + 7) * width;
        let mut temp = vec![0i16; temp_size];

        // Horizontal filtering to temp buffer
        for y in 0..height + 7 {
            for x in 0..width {
                let src_offset = y * src_stride + x;
                let temp_offset = y * width + x;

                if temp_offset >= temp.len() {
                    continue;
                }

                // Load 8 horizontal pixels
                let mut pixels = I16x8::zero();
                for i in 0..8 {
                    if src_offset + i < src.len() {
                        pixels[i] = i16::from(src[src_offset + i]);
                    }
                }

                // Apply horizontal filter
                let filter = I16x8::from_array(*h_coeffs);
                let products = self.simd.mul_i16x8(pixels, filter);
                let sum = self.simd.horizontal_sum_i16x8(products);

                temp[temp_offset] = ((sum + 64) >> 7) as i16;
            }
        }

        // Vertical filtering from temp to dst
        for y in 0..height {
            for x in 0..width {
                let dst_offset = y * dst_stride + x;

                if dst_offset >= dst.len() {
                    continue;
                }

                // Load 8 vertical pixels from temp
                let mut pixels = I16x8::zero();
                for i in 0..8 {
                    let temp_offset = (y + i) * width + x;
                    if temp_offset < temp.len() {
                        pixels[i] = temp[temp_offset];
                    }
                }

                // Apply vertical filter
                let filter = I16x8::from_array(*v_coeffs);
                let products = self.simd.mul_i16x8(pixels, filter);
                let sum = self.simd.horizontal_sum_i16x8(products);

                // Round and shift
                let result = (sum + 64) >> 7;
                dst[dst_offset] = result.clamp(0, 255) as u8;
            }
        }
    }

    /// Bilinear interpolation (simple 2-tap filter).
    pub fn bilinear_h(
        &self,
        src: &[u8],
        src_stride: usize,
        dst: &mut [u8],
        dst_stride: usize,
        fraction: u8,
        width: usize,
        height: usize,
    ) {
        let w1 = fraction;
        let w0 = 64 - w1;

        for y in 0..height {
            for x in 0..width {
                let src_offset = y * src_stride + x;
                let dst_offset = y * dst_stride + x;

                if src_offset + 1 >= src.len() || dst_offset >= dst.len() {
                    continue;
                }

                let p0 = u32::from(src[src_offset]);
                let p1 = u32::from(src[src_offset + 1]);

                let result = (p0 * u32::from(w0) + p1 * u32::from(w1) + 32) / 64;
                dst[dst_offset] = result as u8;
            }
        }
    }

    /// Bilinear vertical interpolation.
    pub fn bilinear_v(
        &self,
        src: &[u8],
        src_stride: usize,
        dst: &mut [u8],
        dst_stride: usize,
        fraction: u8,
        width: usize,
        height: usize,
    ) {
        let w1 = fraction;
        let w0 = 64 - w1;

        for y in 0..height {
            for x in 0..width {
                let src_offset = y * src_stride + x;
                let dst_offset = y * dst_stride + x;

                if src_offset + src_stride >= src.len() || dst_offset >= dst.len() {
                    continue;
                }

                let p0 = u32::from(src[src_offset]);
                let p1 = u32::from(src[src_offset + src_stride]);

                let result = (p0 * u32::from(w0) + p1 * u32::from(w1) + 32) / 64;
                dst[dst_offset] = result as u8;
            }
        }
    }

    /// Average two blocks for bi-directional prediction.
    pub fn average_blocks(
        &self,
        src1: &[u8],
        src2: &[u8],
        dst: &mut [u8],
        width: usize,
        height: usize,
        stride: usize,
    ) {
        for y in 0..height {
            let offset = y * stride;

            // Process 16 pixels at a time using SIMD
            let chunks = width / 16;
            for i in 0..chunks {
                let pos = offset + i * 16;

                if src1.len() < pos + 16 || src2.len() < pos + 16 || dst.len() < pos + 16 {
                    continue;
                }

                let mut v1 = U8x16::zero();
                let mut v2 = U8x16::zero();
                v1.copy_from_slice(&src1[pos..pos + 16]);
                v2.copy_from_slice(&src2[pos..pos + 16]);

                let avg = self.simd.avg_u8x16(v1, v2);
                let avg_array = avg.to_array();
                dst[pos..pos + 16].copy_from_slice(&avg_array);
            }

            // Handle remaining pixels
            for x in (chunks * 16)..width {
                let pos = offset + x;
                if src1.len() > pos && src2.len() > pos && dst.len() > pos {
                    dst[pos] = ((u16::from(src1[pos]) + u16::from(src2[pos]) + 1) / 2) as u8;
                }
            }
        }
    }

    /// Weighted prediction (combine two blocks with weights).
    #[allow(clippy::too_many_arguments)]
    pub fn weighted_pred(
        &self,
        src1: &[u8],
        src2: &[u8],
        dst: &mut [u8],
        weight1: u8,
        weight2: u8,
        width: usize,
        height: usize,
        stride: usize,
    ) {
        let total_weight = u32::from(weight1) + u32::from(weight2);

        for y in 0..height {
            for x in 0..width {
                let offset = y * stride + x;

                if src1.len() <= offset || src2.len() <= offset || dst.len() <= offset {
                    continue;
                }

                let p1 = u32::from(src1[offset]) * u32::from(weight1);
                let p2 = u32::from(src2[offset]) * u32::from(weight2);

                let result = (p1 + p2 + total_weight / 2) / total_weight;
                dst[offset] = result.clamp(0, 255) as u8;
            }
        }
    }

    /// OBMC (Overlapped Block Motion Compensation) blending.
    #[allow(clippy::too_many_arguments)]
    pub fn obmc_blend(
        &self,
        pred: &[u8],
        obmc: &[u8],
        dst: &mut [u8],
        width: usize,
        height: usize,
        stride: usize,
        weights: &[u8],
    ) {
        for y in 0..height {
            for x in 0..width {
                let offset = y * stride + x;
                let weight_idx = (y * width + x).min(weights.len().saturating_sub(1));

                if pred.len() <= offset || obmc.len() <= offset || dst.len() <= offset {
                    continue;
                }

                let w = u32::from(weights[weight_idx]);
                let p1 = u32::from(pred[offset]) * w;
                let p2 = u32::from(obmc[offset]) * (64 - w);

                let result = (p1 + p2 + 32) / 64;
                dst[offset] = result as u8;
            }
        }
    }

    /// SIMD-optimized horizontal filtering for 4-pixel wide blocks.
    #[allow(dead_code)]
    fn filter_h_4_simd(&self, src: &[u8], coeffs: &[i16; 8]) -> [u8; 4] {
        let mut pixels = I16x8::zero();
        for i in 0..8.min(src.len()) {
            pixels[i] = i16::from(src[i]);
        }

        let filter = I16x8::from_array(*coeffs);
        let result = self.simd.pmaddwd(pixels, filter);

        let sum = self.simd.horizontal_sum_i32x4(result);
        let final_val = (sum + 64) >> 7;

        [
            final_val.clamp(0, 255) as u8,
            final_val.clamp(0, 255) as u8,
            final_val.clamp(0, 255) as u8,
            final_val.clamp(0, 255) as u8,
        ]
    }
}

/// Standard AV1 8-tap interpolation filter coefficients.
pub mod filter_coeffs {
    /// Regular filter (smooth).
    pub const REGULAR: [i16; 8] = [-1, 3, -7, 127, 8, -3, 1, 0];

    /// Sharp filter (preserves edges).
    pub const SHARP: [i16; 8] = [-1, 3, -8, 127, 8, -2, 1, 0];

    /// Smooth filter (reduces high frequencies).
    pub const SMOOTH: [i16; 8] = [-2, 6, -13, 120, 13, -6, 2, 0];

    /// Bilinear filter.
    pub const BILINEAR: [i16; 8] = [0, 0, 0, 128, 0, 0, 0, 0];
}