Skip to main content

threers_codec/vp9/
mc.rs

1//! VP9 inter prediction (`vpx_convolve8` + switchable 8-tap kernels).
2//!
3//! Motion vectors are stored in **1/8-pel luma** units (Q3). The convolve path
4//! works in Q4 (1/16-pel): luma MVs are doubled; chroma (4:2:0) keeps the Q3
5//! value as Q4 of chroma pixels (`clamp_mv_to_umv_border_sb` with `ss=1`).
6
7const FILTER_BITS: i32 = 7;
8const SUBPEL_TAPS: usize = 8;
9const SUBPEL_BITS: i32 = 4;
10const SUBPEL_MASK: i32 = (1 << SUBPEL_BITS) - 1;
11
12/// Frame / block interpolation filter (`INTERP_FILTER`).
13#[derive(Clone, Copy, Debug, PartialEq, Eq)]
14#[repr(u8)]
15pub enum InterpFilter {
16    EightTap = 0,
17    EightTapSmooth = 1,
18    EightTapSharp = 2,
19    /// Not used under `SWITCHABLE` (only the three 8-tap filters switch).
20    Bilinear = 3,
21}
22
23impl InterpFilter {
24    pub const SWITCHABLE_FILTERS: u8 = 3;
25
26    #[inline]
27    pub fn from_u8(v: u8) -> Self {
28        match v {
29            1 => Self::EightTapSmooth,
30            2 => Self::EightTapSharp,
31            3 => Self::Bilinear,
32            _ => Self::EightTap,
33        }
34    }
35}
36
37/// `sub_pel_filters_8` — EIGHTTAP / Lagrangian.
38const EIGHTTAP: [[i16; 8]; 16] = [
39    [0, 0, 0, 128, 0, 0, 0, 0],
40    [0, 1, -5, 126, 8, -3, 1, 0],
41    [-1, 3, -10, 122, 18, -6, 2, 0],
42    [-1, 4, -13, 118, 27, -9, 3, -1],
43    [-1, 4, -16, 112, 37, -11, 4, -1],
44    [-1, 5, -18, 105, 48, -14, 4, -1],
45    [-1, 5, -19, 97, 58, -16, 5, -1],
46    [-1, 6, -19, 88, 68, -18, 5, -1],
47    [-1, 6, -19, 78, 78, -19, 6, -1],
48    [-1, 5, -18, 68, 88, -19, 6, -1],
49    [-1, 5, -16, 58, 97, -19, 5, -1],
50    [-1, 4, -14, 48, 105, -18, 5, -1],
51    [-1, 4, -11, 37, 112, -16, 4, -1],
52    [-1, 3, -9, 27, 118, -13, 4, -1],
53    [0, 2, -6, 18, 122, -10, 3, -1],
54    [0, 1, -3, 8, 126, -5, 1, 0],
55];
56
57/// `sub_pel_filters_8lp` — EIGHTTAP_SMOOTH.
58const EIGHTTAP_SMOOTH: [[i16; 8]; 16] = [
59    [0, 0, 0, 128, 0, 0, 0, 0],
60    [-3, -1, 32, 64, 38, 1, -3, 0],
61    [-2, -2, 29, 63, 41, 2, -3, 0],
62    [-2, -2, 26, 63, 43, 4, -4, 0],
63    [-2, -3, 24, 62, 46, 5, -4, 0],
64    [-2, -3, 21, 60, 49, 7, -4, 0],
65    [-1, -4, 18, 59, 51, 9, -4, 0],
66    [-1, -4, 16, 57, 53, 12, -4, -1],
67    [-1, -4, 14, 55, 55, 14, -4, -1],
68    [-1, -4, 12, 53, 57, 16, -4, -1],
69    [0, -4, 9, 51, 59, 18, -4, -1],
70    [0, -4, 7, 49, 60, 21, -3, -2],
71    [0, -4, 5, 46, 62, 24, -3, -2],
72    [0, -4, 4, 43, 63, 26, -2, -2],
73    [0, -3, 2, 41, 63, 29, -2, -2],
74    [0, -3, 1, 38, 64, 32, -1, -3],
75];
76
77/// `sub_pel_filters_8s` — EIGHTTAP_SHARP.
78const EIGHTTAP_SHARP: [[i16; 8]; 16] = [
79    [0, 0, 0, 128, 0, 0, 0, 0],
80    [-1, 3, -7, 127, 8, -3, 1, 0],
81    [-2, 5, -13, 125, 17, -6, 3, -1],
82    [-3, 7, -17, 121, 27, -10, 5, -2],
83    [-4, 9, -20, 115, 37, -13, 6, -2],
84    [-4, 10, -23, 108, 48, -16, 8, -3],
85    [-4, 10, -24, 100, 59, -19, 9, -3],
86    [-4, 11, -24, 90, 70, -21, 10, -4],
87    [-4, 11, -23, 80, 80, -23, 11, -4],
88    [-4, 10, -21, 70, 90, -24, 11, -4],
89    [-3, 9, -19, 59, 100, -24, 10, -4],
90    [-3, 8, -16, 48, 108, -23, 10, -4],
91    [-2, 6, -13, 37, 115, -20, 9, -4],
92    [-2, 5, -10, 27, 121, -17, 7, -3],
93    [-1, 3, -6, 17, 125, -13, 5, -2],
94    [0, 1, -3, 8, 127, -7, 3, -1],
95];
96
97/// `bilinear_filters`.
98const BILINEAR: [[i16; 8]; 16] = [
99    [0, 0, 0, 128, 0, 0, 0, 0],
100    [0, 0, 0, 120, 8, 0, 0, 0],
101    [0, 0, 0, 112, 16, 0, 0, 0],
102    [0, 0, 0, 104, 24, 0, 0, 0],
103    [0, 0, 0, 96, 32, 0, 0, 0],
104    [0, 0, 0, 88, 40, 0, 0, 0],
105    [0, 0, 0, 80, 48, 0, 0, 0],
106    [0, 0, 0, 72, 56, 0, 0, 0],
107    [0, 0, 0, 64, 64, 0, 0, 0],
108    [0, 0, 0, 56, 72, 0, 0, 0],
109    [0, 0, 0, 48, 80, 0, 0, 0],
110    [0, 0, 0, 40, 88, 0, 0, 0],
111    [0, 0, 0, 32, 96, 0, 0, 0],
112    [0, 0, 0, 24, 104, 0, 0, 0],
113    [0, 0, 0, 16, 112, 0, 0, 0],
114    [0, 0, 0, 8, 120, 0, 0, 0],
115];
116
117fn kernel(filter: InterpFilter) -> &'static [[i16; 8]; 16] {
118    match filter {
119        InterpFilter::EightTap => &EIGHTTAP,
120        InterpFilter::EightTapSmooth => &EIGHTTAP_SMOOTH,
121        InterpFilter::EightTapSharp => &EIGHTTAP_SHARP,
122        InterpFilter::Bilinear => &BILINEAR,
123    }
124}
125
126#[inline]
127fn clip_pixel(v: i32) -> u8 {
128    v.clamp(0, 255) as u8
129}
130
131#[inline]
132fn round_bits(sum: i32) -> u8 {
133    clip_pixel((sum + (1 << (FILTER_BITS - 1))) >> FILTER_BITS)
134}
135
136#[inline]
137fn sample(plane: &[u8], stride: usize, width: usize, height: usize, x: i32, y: i32) -> u8 {
138    let sx = x.clamp(0, width as i32 - 1) as usize;
139    let sy = y.clamp(0, height as i32 - 1) as usize;
140    plane[sy * stride + sx]
141}
142
143/// Predict an `w×h` block at plane position `(x, y)` from `reference`.
144///
145/// `mv_row_q3` / `mv_col_q3` are luma 1/8-pel units. Pass `chroma = true` for
146/// U/V planes (4:2:0 subsampling of the MV into Q4).
147pub fn predict_inter(
148    reference: &[u8],
149    ref_stride: usize,
150    ref_w: usize,
151    ref_h: usize,
152    x: usize,
153    y: usize,
154    w: usize,
155    h: usize,
156    mv_row_q3: i16,
157    mv_col_q3: i16,
158    chroma: bool,
159    filter: InterpFilter,
160    out: &mut [u8],
161) {
162    debug_assert_eq!(out.len(), w * h);
163    let ss = if chroma { 1 } else { 0 };
164    // Q3 → Q4: luma ×2, chroma ×1 (libvpx `clamp_mv_to_umv_border_sb`).
165    let mv_q4_row = i32::from(mv_row_q3) << (1 - ss);
166    let mv_q4_col = i32::from(mv_col_q3) << (1 - ss);
167    let subpel_y = mv_q4_row & SUBPEL_MASK;
168    let subpel_x = mv_q4_col & SUBPEL_MASK;
169    let int_y = mv_q4_row >> SUBPEL_BITS;
170    let int_x = mv_q4_col >> SUBPEL_BITS;
171    let base_y = y as i32 + int_y;
172    let base_x = x as i32 + int_x;
173    let taps = kernel(filter);
174
175    if subpel_x == 0 && subpel_y == 0 {
176        for r in 0..h {
177            for c in 0..w {
178                out[r * w + c] = sample(
179                    reference,
180                    ref_stride,
181                    ref_w,
182                    ref_h,
183                    base_x + c as i32,
184                    base_y + r as i32,
185                );
186            }
187        }
188        return;
189    }
190
191    if subpel_y == 0 {
192        let f = &taps[subpel_x as usize];
193        for r in 0..h {
194            for c in 0..w {
195                let mut sum = 0i32;
196                for k in 0..SUBPEL_TAPS {
197                    let sx = base_x + c as i32 + k as i32 - 3;
198                    sum += i32::from(sample(
199                        reference,
200                        ref_stride,
201                        ref_w,
202                        ref_h,
203                        sx,
204                        base_y + r as i32,
205                    )) * i32::from(f[k]);
206                }
207                out[r * w + c] = round_bits(sum);
208            }
209        }
210        return;
211    }
212
213    if subpel_x == 0 {
214        let f = &taps[subpel_y as usize];
215        for r in 0..h {
216            for c in 0..w {
217                let mut sum = 0i32;
218                for k in 0..SUBPEL_TAPS {
219                    let sy = base_y + r as i32 + k as i32 - 3;
220                    sum += i32::from(sample(
221                        reference,
222                        ref_stride,
223                        ref_w,
224                        ref_h,
225                        base_x + c as i32,
226                        sy,
227                    )) * i32::from(f[k]);
228                }
229                out[r * w + c] = round_bits(sum);
230            }
231        }
232        return;
233    }
234
235    // 2D: horizontal into temp, then vertical.
236    let x_filter = &taps[subpel_x as usize];
237    let y_filter = &taps[subpel_y as usize];
238    let tmp_h = h + SUBPEL_TAPS - 1;
239    let mut tmp = vec![0u8; tmp_h * w];
240    for r in 0..tmp_h {
241        let sy = base_y + r as i32 - 3;
242        for c in 0..w {
243            let mut sum = 0i32;
244            for k in 0..SUBPEL_TAPS {
245                let sx = base_x + c as i32 + k as i32 - 3;
246                sum += i32::from(sample(reference, ref_stride, ref_w, ref_h, sx, sy))
247                    * i32::from(x_filter[k]);
248            }
249            tmp[r * w + c] = round_bits(sum);
250        }
251    }
252    for r in 0..h {
253        for c in 0..w {
254            let mut sum = 0i32;
255            for k in 0..SUBPEL_TAPS {
256                sum += i32::from(tmp[(r + k) * w + c]) * i32::from(y_filter[k]);
257            }
258            out[r * w + c] = round_bits(sum);
259        }
260    }
261}
262
263#[cfg(test)]
264mod tests {
265    use super::*;
266
267    #[test]
268    fn fullpel_is_copy() {
269        let mut plane = vec![0u8; 16 * 16];
270        for i in 0..plane.len() {
271            plane[i] = (i % 251) as u8;
272        }
273        let mut out = [0u8; 16];
274        predict_inter(
275            &plane,
276            16,
277            16,
278            16,
279            4,
280            4,
281            4,
282            4,
283            8,
284            16,
285            false,
286            InterpFilter::EightTap,
287            &mut out,
288        );
289        for r in 0..4 {
290            for c in 0..4 {
291                assert_eq!(out[r * 4 + c], plane[(5 + r) * 16 + (6 + c)]);
292            }
293        }
294    }
295
296    #[test]
297    fn phase0_matches_fullpel() {
298        let mut plane = vec![40u8; 32 * 32];
299        plane[8 * 32 + 8] = 200;
300        let mut a = [0u8; 4];
301        let mut b = [0u8; 4];
302        predict_inter(
303            &plane,
304            32,
305            32,
306            32,
307            8,
308            8,
309            2,
310            2,
311            0,
312            0,
313            false,
314            InterpFilter::EightTap,
315            &mut a,
316        );
317        predict_inter(
318            &plane,
319            32,
320            32,
321            32,
322            8,
323            8,
324            2,
325            2,
326            0,
327            0,
328            false,
329            InterpFilter::EightTapSmooth,
330            &mut b,
331        );
332        assert_eq!(a, b);
333    }
334
335    #[test]
336    fn halfpel_is_between_neighbors() {
337        let mut plane = vec![0u8; 16 * 16];
338        for c in 0..16 {
339            plane[4 * 16 + c] = 0;
340            plane[5 * 16 + c] = 255;
341        }
342        let mut out = [0u8; 1];
343        // Vertical half-pel (mv_row=4 → Q4 phase 8).
344        predict_inter(
345            &plane,
346            16,
347            16,
348            16,
349            4,
350            4,
351            1,
352            1,
353            4,
354            0,
355            false,
356            InterpFilter::EightTap,
357            &mut out,
358        );
359        let between = out[0] as u32;
360        assert!(
361            between > 32 && between < 224,
362            "half-pel MC should interpolate: {between}"
363        );
364    }
365
366    #[test]
367    fn smooth_differs_from_eighttap_at_halfpel() {
368        let mut plane = vec![0u8; 16 * 16];
369        for r in 0..16 {
370            for c in 0..16 {
371                plane[r * 16 + c] = if c < 8 { 0 } else { 255 };
372            }
373        }
374        let mut a = [0u8; 4];
375        let mut b = [0u8; 4];
376        predict_inter(
377            &plane,
378            16,
379            16,
380            16,
381            4,
382            4,
383            2,
384            2,
385            0,
386            4,
387            false,
388            InterpFilter::EightTap,
389            &mut a,
390        );
391        predict_inter(
392            &plane,
393            16,
394            16,
395            16,
396            4,
397            4,
398            2,
399            2,
400            0,
401            4,
402            false,
403            InterpFilter::EightTapSmooth,
404            &mut b,
405        );
406        assert_ne!(a, b);
407    }
408}