forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB84fa44cTahinli2 months ago3331 commits

mc.rs808 lines, 26.4 KiBHistoryRawDownload
1//! Motion compensation: the decoder's inter predictor (`dec_build_inter_predictors`,
2//! libvpx `vp9/decoder/vp9_decodeframe.c:593`), the on-the-fly reference border
3//! extension (`build_mc_border`, :473), the UMV-border MV clamp
4//! (`clamp_mv_to_umv_border_sb`, `vp9/common/vp9_reconinter.c:88`), the
5//! switchable filter kernels (`vp9/common/vp9_filter.c`) and the sub-pixel
6//! convolve (`vpx_dsp/vpx_convolve.c`).
7//!
8//! The VP9 decoder does NOT extend the reference frame's borders in place: it
9//! builds a bordered copy of the block's reference window per prediction
10//! (`extend_and_predict`) whenever the window leaves the frame or the frame's
11//! dimensions are not a multiple of 8. A reference slot whose size differs
12//! from the current frame is SCALED: [`ScaleFactors`] carries
13//! `vp9_setup_scale_factors_for_frame`, the block/MV are mapped into the
14//! reference with `vp9_scale_mv`, and the convolve steps by `x_step_q4` /
15//! `y_step_q4` (`sf->predict`, `vpx_scaled_*`).
16//!
17//! Indexing note: libvpx's convolve helpers take a raw pointer and subtract
18//! `SUBPEL_TAPS / 2 - 1` from it, so they read taps *before* the block's first
19//! sample. Rust slices cannot go negative, so every helper here takes a buffer
20//! plus an `origin` (the block's first sample) and indexes with `isize`.
21
22/// `SUBPEL_BITS` / `SUBPEL_MASK` / `SUBPEL_SHIFTS` / `SUBPEL_TAPS`
23/// (`vpx_dsp/vpx_filter.h:23-26`).
24const SUBPEL_BITS: i32 = 4;
25const SUBPEL_MASK: i32 = (1 << SUBPEL_BITS) - 1;
26const SUBPEL_SHIFTS: i32 = 1 << SUBPEL_BITS;
27const TAPS: usize = 8;
28/// `FILTER_BITS` (`vpx_dsp/vpx_filter.h:21`).
29const FILTER_BITS: i32 = 7;
30/// `VP9_INTERP_EXTEND` (`vpx_scale/yv12config.h:25`).
31const INTERP_EXTEND: i32 = 4;
32
33/// `REF_SCALE_SHIFT` / `REF_NO_SCALE` / `REF_INVALID_SCALE`
34/// (`vp9/common/vp9_scale.h:21-23`): the fixed-point precision of a reference
35/// frame's scale factor.
36const REF_SCALE_SHIFT: i32 = 14;
37const REF_NO_SCALE: i32 = 1 << REF_SCALE_SHIFT;
38const REF_INVALID_SCALE: i32 = -1;
39
40/// `scaled_x` / `scaled_y` (`vp9_scale.c:22`).
41fn scale_value(val: i32, scale_fp: i32) -> i32 {
42 (((val as i64) * scale_fp as i64) >> REF_SCALE_SHIFT) as i32
43}
44
45/// `struct scale_factors` (`vp9/common/vp9_scale.h:27`) set up by
46/// `vp9_setup_scale_factors_for_frame` (`vp9_scale.c:44`): the scale from a
47/// reference frame's coded size to this frame's, its derived 1/16-pel step
48/// (`x_step_q4`/`y_step_q4`; 16 means "no scaling on that axis") and
49/// `vp9_scale_mv`.
50#[derive(Clone, Copy)]
51pub(crate) struct ScaleFactors {
52 x_scale_fp: i32,
53 y_scale_fp: i32,
54 x_step_q4: i32,
55 y_step_q4: i32,
56}
57
58impl ScaleFactors {
59 /// `vp9_setup_scale_factors_for_frame(other_w, other_h, this_w, this_h)`:
60 /// `other_*` is the REFERENCE's coded size, `this_*` the frame being
61 /// decoded. An out-of-range ratio leaves `REF_INVALID_SCALE`, which
62 /// libvpx turns into `Reference frame has invalid dimensions` when the
63 /// reference is actually predicted from.
64 pub(crate) fn setup(other_w: usize, other_h: usize, this_w: usize, this_h: usize) -> Self {
65 // `valid_ref_frame_size` (`vp9_scale.h:61`): at most 2x down, 16x up.
66 let valid = 2 * this_w >= other_w
67 && 2 * this_h >= other_h
68 && this_w <= 16 * other_w
69 && this_h <= 16 * other_h;
70 if !valid {
71 return ScaleFactors {
72 x_scale_fp: REF_INVALID_SCALE,
73 y_scale_fp: REF_INVALID_SCALE,
74 x_step_q4: 0,
75 y_step_q4: 0,
76 };
77 }
78 let x_scale_fp = (((other_w as i64) << REF_SCALE_SHIFT) / this_w as i64) as i32;
79 let y_scale_fp = (((other_h as i64) << REF_SCALE_SHIFT) / this_h as i64) as i32;
80 ScaleFactors {
81 x_scale_fp,
82 y_scale_fp,
83 x_step_q4: scale_value(SUBPEL_SHIFTS, x_scale_fp),
84 y_step_q4: scale_value(SUBPEL_SHIFTS, y_scale_fp),
85 }
86 }
87
88 /// `vp9_is_valid_scale`.
89 pub(crate) fn is_valid(&self) -> bool {
90 self.x_scale_fp != REF_INVALID_SCALE && self.y_scale_fp != REF_INVALID_SCALE
91 }
92
93 /// `vp9_is_scaled`.
94 pub(crate) fn is_scaled(&self) -> bool {
95 self.is_valid() && (self.x_scale_fp != REF_NO_SCALE || self.y_scale_fp != REF_NO_SCALE)
96 }
97
98 fn scale_x(&self, val: i32) -> i32 {
99 scale_value(val, self.x_scale_fp)
100 }
101
102 fn scale_y(&self, val: i32) -> i32 {
103 scale_value(val, self.y_scale_fp)
104 }
105
106 /// `vp9_scale_mv` (`vp9_scale.c:29`): the block MV scaled to the
107 /// reference's resolution plus the sub-pixel offset of the block's own
108 /// scaled position. Returns `(row, col)` in 1/16-pel units.
109 ///
110 /// `x`/`y` are the block's coordinates in the CURRENT frame (libvpx
111 /// passes `mi_x + x`, `mi_y + y` — luma units even for chroma planes; the
112 /// sub-8x8 chroma offsets are always 0, so the mismatch never bites).
113 pub(crate) fn scale_mv(&self, mv: (i32, i32), x: i32, y: i32) -> (i32, i32) {
114 let x_off_q4 = self.scale_x(x << SUBPEL_BITS) & SUBPEL_MASK;
115 let y_off_q4 = self.scale_y(y << SUBPEL_BITS) & SUBPEL_MASK;
116 (self.scale_y(mv.0) + y_off_q4, self.scale_x(mv.1) + x_off_q4)
117 }
118}
119
120/// `vp9_filter_kernels` (`vp9/common/vp9_filter.c:79`), in libvpx's filter
121/// numbering: 0 `EIGHTTAP`, 1 `EIGHTTAP_SMOOTH`, 2 `EIGHTTAP_SHARP`,
122/// 3 `BILINEAR` (the 4-tap index 4 is unreachable for profile 0).
123pub(crate) const FILTERS: [[[i16; TAPS]; SUBPEL_SHIFTS as usize]; 4] = [
124 // sub_pel_filters_8: Lagrangian interpolation filter.
125 [
126 [0, 0, 0, 128, 0, 0, 0, 0],
127 [0, 1, -5, 126, 8, -3, 1, 0],
128 [-1, 3, -10, 122, 18, -6, 2, 0],
129 [-1, 4, -13, 118, 27, -9, 3, -1],
130 [-1, 4, -16, 112, 37, -11, 4, -1],
131 [-1, 5, -18, 105, 48, -14, 4, -1],
132 [-1, 5, -19, 97, 58, -16, 5, -1],
133 [-1, 6, -19, 88, 68, -18, 5, -1],
134 [-1, 6, -19, 78, 78, -19, 6, -1],
135 [-1, 5, -18, 68, 88, -19, 6, -1],
136 [-1, 5, -16, 58, 97, -19, 5, -1],
137 [-1, 4, -14, 48, 105, -18, 5, -1],
138 [-1, 4, -11, 37, 112, -16, 4, -1],
139 [-1, 3, -9, 27, 118, -13, 4, -1],
140 [0, 2, -6, 18, 122, -10, 3, -1],
141 [0, 1, -3, 8, 126, -5, 1, 0],
142 ],
143 // sub_pel_filters_8lp: freqmultiplier = 0.5 (EIGHTTAP_SMOOTH).
144 [
145 [0, 0, 0, 128, 0, 0, 0, 0],
146 [-3, -1, 32, 64, 38, 1, -3, 0],
147 [-2, -2, 29, 63, 41, 2, -3, 0],
148 [-2, -2, 26, 63, 43, 4, -4, 0],
149 [-2, -3, 24, 62, 46, 5, -4, 0],
150 [-2, -3, 21, 60, 49, 7, -4, 0],
151 [-1, -4, 18, 59, 51, 9, -4, 0],
152 [-1, -4, 16, 57, 53, 12, -4, -1],
153 [-1, -4, 14, 55, 55, 14, -4, -1],
154 [-1, -4, 12, 53, 57, 16, -4, -1],
155 [0, -4, 9, 51, 59, 18, -4, -1],
156 [0, -4, 7, 49, 60, 21, -3, -2],
157 [0, -4, 5, 46, 62, 24, -3, -2],
158 [0, -4, 4, 43, 63, 26, -2, -2],
159 [0, -3, 2, 41, 63, 29, -2, -2],
160 [0, -3, 1, 38, 64, 32, -1, -3],
161 ],
162 // sub_pel_filters_8s: DCT based filter (EIGHTTAP_SHARP).
163 [
164 [0, 0, 0, 128, 0, 0, 0, 0],
165 [-1, 3, -7, 127, 8, -3, 1, 0],
166 [-2, 5, -13, 125, 17, -6, 3, -1],
167 [-3, 7, -17, 121, 27, -10, 5, -2],
168 [-4, 9, -20, 115, 37, -13, 6, -2],
169 [-4, 10, -23, 108, 48, -16, 8, -3],
170 [-4, 10, -24, 100, 59, -19, 9, -3],
171 [-4, 11, -24, 90, 70, -21, 10, -4],
172 [-4, 11, -23, 80, 80, -23, 11, -4],
173 [-4, 10, -21, 70, 90, -24, 11, -4],
174 [-3, 9, -19, 59, 100, -24, 10, -4],
175 [-3, 8, -16, 48, 108, -23, 10, -4],
176 [-2, 6, -13, 37, 115, -20, 9, -4],
177 [-2, 5, -10, 27, 121, -17, 7, -3],
178 [-1, 3, -6, 17, 125, -13, 5, -2],
179 [0, 1, -3, 8, 127, -7, 3, -1],
180 ],
181 // bilinear_filters.
182 [
183 [0, 0, 0, 128, 0, 0, 0, 0],
184 [0, 0, 0, 120, 8, 0, 0, 0],
185 [0, 0, 0, 112, 16, 0, 0, 0],
186 [0, 0, 0, 104, 24, 0, 0, 0],
187 [0, 0, 0, 96, 32, 0, 0, 0],
188 [0, 0, 0, 88, 40, 0, 0, 0],
189 [0, 0, 0, 80, 48, 0, 0, 0],
190 [0, 0, 0, 72, 56, 0, 0, 0],
191 [0, 0, 0, 64, 64, 0, 0, 0],
192 [0, 0, 0, 56, 72, 0, 0, 0],
193 [0, 0, 0, 48, 80, 0, 0, 0],
194 [0, 0, 0, 40, 88, 0, 0, 0],
195 [0, 0, 0, 32, 96, 0, 0, 0],
196 [0, 0, 0, 24, 104, 0, 0, 0],
197 [0, 0, 0, 16, 112, 0, 0, 0],
198 [0, 0, 0, 8, 120, 0, 0, 0],
199 ],
200];
201
202use crate::{Sample, clip_pixel_bd, round_pow2};
203
204/// Read `buf[i]` for a possibly negative `i`. Every caller keeps `i` inside
205/// the buffer (the border copy guarantees the tap margins exist).
206#[inline]
207fn at(buf: &[Sample], i: isize) -> Sample {
208 buf[i as usize]
209}
210
211/// `clamp_mv_to_umv_border_sb` (`vp9/common/vp9_reconinter.c:88`), returning
212/// the MV in 1/16-pel (`MV_PRECISION_Q4`) units.
213///
214/// `mb_*` are the block's distances to the frame edges in 1/8-pel units
215/// (`xd->mb_to_*_edge`, i.e. `±mi * 64`); `bw`/`bh` are the PLANE block's
216/// pixel dimensions. `clamp_mv(mv, min_col, max_col, min_row, max_row)`:
217/// columns are bounded by the left/right edges, rows by the top/bottom.
218pub(crate) fn clamp_mv_to_umv_border_sb(
219 mv: (i32, i32),
220 bw: i32,
221 bh: i32,
222 mb_left: i32,
223 mb_right: i32,
224 mb_top: i32,
225 mb_bottom: i32,
226 ss_x: usize,
227 ss_y: usize,
228) -> (i32, i32) {
229 let spel_left = (INTERP_EXTEND + bw) << SUBPEL_BITS;
230 let spel_right = spel_left - SUBPEL_SHIFTS;
231 let spel_top = (INTERP_EXTEND + bh) << SUBPEL_BITS;
232 let spel_bottom = spel_top - SUBPEL_SHIFTS;
233 let xs = 1 << (1 - ss_x);
234 let ys = 1 << (1 - ss_y);
235 let col = (mv.1 * xs).clamp(mb_left * xs - spel_left, mb_right * xs + spel_right);
236 let row = (mv.0 * ys).clamp(mb_top * ys - spel_top, mb_bottom * ys + spel_bottom);
237 (row, col)
238}
239
240/// `build_mc_border` (`vp9/decoder/vp9_decodeframe.c:473`): copy the
241/// `b_w` x `b_h` window whose top-left is frame pixel `(x, y)` into `dst`,
242/// replicating the frame's first/last row and column for reads outside
243/// `[0, w)` x `[0, h)`.
244#[allow(clippy::too_many_arguments)]
245fn build_mc_border(
246 src: &[Sample],
247 src_stride: usize,
248 dst: &mut [Sample],
249 dst_stride: usize,
250 x: i32,
251 mut y: i32,
252 b_w: usize,
253 mut b_h: usize,
254 w: i32,
255 h: i32,
256) {
257 // C computes `ref_row = src - x - y * src_stride`; for negative y the
258 // pointer arithmetic leaves the buffer and the row-index equivalent below
259 // is the same read.
260 let mut row = if y >= h {
261 (h - 1) as usize
262 } else if y > 0 {
263 y as usize
264 } else {
265 0
266 };
267 let mut out = 0usize;
268 loop {
269 let left = if x < 0 { (-x).min(b_w as i32) } else { 0 } as usize;
270 let right = if x + b_w as i32 > w {
271 (x + b_w as i32 - w).min(b_w as i32)
272 } else {
273 0
274 } as usize;
275 let copy = b_w - left - right;
276
277 let base = row * src_stride;
278 if left > 0 {
279 let v = src[base];
280 dst[out..out + left].fill(v);
281 }
282 if copy > 0 {
283 let from = (x + left as i32) as usize;
284 dst[out + left..out + left + copy]
285 .copy_from_slice(&src[base + from..base + from + copy]);
286 }
287 if right > 0 {
288 let v = src[base + (w - 1) as usize];
289 dst[out + left + copy..out + left + copy + right].fill(v);
290 }
291
292 out += dst_stride;
293 y += 1;
294 if y > 0 && y < h {
295 row += 1;
296 }
297 b_h -= 1;
298 if b_h == 0 {
299 break;
300 }
301 }
302}
303
304/// `convolve_horiz` / `convolve_avg_horiz` (`vpx_dsp/vpx_convolve.c:22`).
305/// `origin` is the block's first sample in `buf`; taps are read from
306/// `origin + y*stride + (x_q4 >> SUBPEL_BITS) - (SUBPEL_TAPS / 2 - 1) + k`.
307/// `x_step` advances `x_q4` per output sample — `SUBPEL_SHIFTS` for an
308/// unscaled reference, `sf->x_step_q4` (the scaled sampler, also used by
309/// `vpx_scaled_horiz`) otherwise.
310#[allow(clippy::too_many_arguments)]
311fn convolve_horiz(
312 buf: &[Sample],
313 stride: usize,
314 origin: isize,
315 dst: &mut [Sample],
316 dst_stride: usize,
317 f: &[[i16; TAPS]; SUBPEL_SHIFTS as usize],
318 x0_q4: i32,
319 x_step: i32,
320 w: usize,
321 rows: usize,
322 avg: bool,
323 bd: u8,
324) {
325 for y in 0..rows {
326 let mut x_q4 = x0_q4;
327 for x in 0..w {
328 let base = origin + y as isize * stride as isize + (x_q4 >> SUBPEL_BITS) as isize
329 - (TAPS as isize / 2 - 1);
330 let filt = &f[(x_q4 & SUBPEL_MASK) as usize];
331 let mut sum = 0i32;
332 for k in 0..TAPS {
333 sum += at(buf, base + k as isize) as i32 * filt[k] as i32;
334 }
335 let v = clip_pixel_bd(round_pow2(sum, FILTER_BITS), bd) as i32;
336 let d = y * dst_stride + x;
337 dst[d] = if avg {
338 round_pow2(dst[d] as i32 + v, 1) as Sample
339 } else {
340 v as Sample
341 };
342 x_q4 += x_step;
343 }
344 }
345}
346
347/// `convolve_vert` / `convolve_avg_vert` (`vpx_dsp/vpx_convolve.c:80`).
348/// `y_step` advances `y_q4` per output sample (`SUBPEL_SHIFTS` unscaled,
349/// `sf->y_step_q4` for `vpx_scaled_vert`).
350#[allow(clippy::too_many_arguments)]
351fn convolve_vert(
352 buf: &[Sample],
353 stride: usize,
354 origin: isize,
355 dst: &mut [Sample],
356 dst_stride: usize,
357 f: &[[i16; TAPS]; SUBPEL_SHIFTS as usize],
358 y0_q4: i32,
359 y_step: i32,
360 w: usize,
361 h: usize,
362 avg: bool,
363 bd: u8,
364) {
365 for x in 0..w {
366 let mut y_q4 = y0_q4;
367 for y in 0..h {
368 let base = origin
369 + ((y_q4 >> SUBPEL_BITS) as isize - (TAPS as isize / 2 - 1)) * stride as isize
370 + x as isize;
371 let filt = &f[(y_q4 & SUBPEL_MASK) as usize];
372 let mut sum = 0i32;
373 for k in 0..TAPS {
374 sum += at(buf, base + k as isize * stride as isize) as i32 * filt[k] as i32;
375 }
376 let v = clip_pixel_bd(round_pow2(sum, FILTER_BITS), bd) as i32;
377 let d = y * dst_stride + x;
378 dst[d] = if avg {
379 round_pow2(dst[d] as i32 + v, 1) as Sample
380 } else {
381 v as Sample
382 };
383 y_q4 += y_step;
384 }
385 }
386}
387
388/// `inter_predictor` + `sf->predict` (`vp9/common/vp9_reconinter.h:23`,
389/// `vp9/common/vp9_scale.c:79`): the kernel is picked by
390/// `predict[subpel_x != 0][subpel_y != 0][avg]`. Unscaled that is
391/// copy/avg, the vertical, the horizontal and the 2D convolve; with a scaled
392/// reference the table collapses to the scaled variants (`vpx_scaled_horiz` /
393/// `vpx_scaled_vert` — the same walk with `x_step_q4` / `y_step_q4` — and
394/// `vpx_scaled_2d`), reproduced here by the step-aware convolve helpers.
395///
396/// `w` x `h` output samples written at `dst`; `buf`/`origin` locate the
397/// block's first reference sample. `temp` is the `vpx_convolve8` intermediate
398/// (stride-64 scratch of `64 * 135` bytes plus a `64 * 64` second scratch for
399/// the averaging variant).
400#[allow(clippy::too_many_arguments)]
401fn inter_predictor(
402 buf: &[Sample],
403 stride: usize,
404 origin: isize,
405 dst: &mut [Sample],
406 dst_stride: usize,
407 f: &[[i16; TAPS]; SUBPEL_SHIFTS as usize],
408 subpel_x: i32,
409 subpel_y: i32,
410 x_step: i32,
411 y_step: i32,
412 w: usize,
413 h: usize,
414 avg: bool,
415 temp: &mut [Sample],
416 bd: u8,
417) {
418 let scaled_x = x_step != SUBPEL_SHIFTS;
419 let scaled_y = y_step != SUBPEL_SHIFTS;
420 if scaled_x || scaled_y {
421 // sf->predict with a scaled axis (vp9_scale.c:79-110): an unscaled
422 // axis keeps its 1D kernel only when the other axis has no subpel
423 // offset; anything else is the 2D scaled convolve.
424 if !scaled_x && subpel_x == 0 {
425 convolve_vert(
426 buf, stride, origin, dst, dst_stride, f, subpel_y, y_step, w, h, avg, bd,
427 );
428 return;
429 }
430 if !scaled_y && subpel_y == 0 {
431 convolve_horiz(
432 buf, stride, origin, dst, dst_stride, f, subpel_x, x_step, w, h, avg, bd,
433 );
434 return;
435 }
436 convolve_2d(
437 buf, stride, origin, dst, dst_stride, f, subpel_x, subpel_y, x_step, y_step, w, h, avg,
438 temp, bd,
439 );
440 return;
441 }
442 if subpel_x == 0 && subpel_y == 0 {
443 for y in 0..h {
444 for x in 0..w {
445 let s = at(buf, origin + (y * stride + x) as isize) as i32;
446 let d = y * dst_stride + x;
447 dst[d] = if avg {
448 round_pow2(dst[d] as i32 + s, 1) as Sample
449 } else {
450 s as Sample
451 };
452 }
453 }
454 return;
455 }
456 if subpel_y == 0 {
457 convolve_horiz(
458 buf,
459 stride,
460 origin,
461 dst,
462 dst_stride,
463 f,
464 subpel_x,
465 SUBPEL_SHIFTS,
466 w,
467 h,
468 avg,
469 bd,
470 );
471 return;
472 }
473 if subpel_x == 0 {
474 convolve_vert(
475 buf,
476 stride,
477 origin,
478 dst,
479 dst_stride,
480 f,
481 subpel_y,
482 SUBPEL_SHIFTS,
483 w,
484 h,
485 avg,
486 bd,
487 );
488 return;
489 }
490 convolve_2d(
491 buf, stride, origin, dst, dst_stride, f, subpel_x, subpel_y, x_step, y_step, w, h, avg,
492 temp, bd,
493 );
494}
495
496/// `vpx_convolve8_c` / `vpx_convolve8_avg_c` (`vpx_dsp/vpx_convolve.c:158`) =
497/// `vpx_scaled_2d` / `vpx_scaled_avg_2d`: horizontal pass into a stride-64
498/// intermediate with `intermediate_height = (((h - 1) * y_step_q4 + y0_q4) >>
499/// SUBPEL_BITS) + SUBPEL_TAPS`, then the vertical pass. The horizontal pass
500/// starts `SUBPEL_TAPS / 2 - 1` rows ABOVE the block (C passes
501/// `src - src_stride * (SUBPEL_TAPS / 2 - 1)`).
502#[allow(clippy::too_many_arguments)]
503fn convolve_2d(
504 buf: &[Sample],
505 stride: usize,
506 origin: isize,
507 dst: &mut [Sample],
508 dst_stride: usize,
509 f: &[[i16; TAPS]; SUBPEL_SHIFTS as usize],
510 subpel_x: i32,
511 subpel_y: i32,
512 x_step: i32,
513 y_step: i32,
514 w: usize,
515 h: usize,
516 avg: bool,
517 temp: &mut [Sample],
518 bd: u8,
519) {
520 let intermediate_height = (((h as i32 - 1) * y_step + subpel_y) >> SUBPEL_BITS) as usize + TAPS;
521 let temp_origin = (TAPS as isize / 2 - 1) * 64;
522 convolve_horiz(
523 buf,
524 stride,
525 origin - (TAPS as isize / 2 - 1) * stride as isize,
526 temp,
527 64,
528 f,
529 subpel_x,
530 x_step,
531 w,
532 intermediate_height,
533 false,
534 bd,
535 );
536 if avg {
537 // vpx_convolve8_avg_c: full 2D into a second scratch, then average
538 // with the destination (which holds the first reference's block).
539 // `temp[..64 * 135]` is the horizontal intermediate; the region after
540 // it is the 2D result.
541 let (horiz, mid2) = temp.split_at_mut(64 * 135);
542 convolve_vert(
543 horiz,
544 64,
545 temp_origin,
546 mid2,
547 w,
548 f,
549 subpel_y,
550 y_step,
551 w,
552 h,
553 false,
554 bd,
555 );
556 for y in 0..h {
557 for x in 0..w {
558 let d = y * dst_stride + x;
559 dst[d] = round_pow2(dst[d] as i32 + mid2[y * w + x] as i32, 1) as Sample;
560 }
561 }
562 return;
563 }
564 convolve_vert(
565 temp,
566 64,
567 temp_origin,
568 dst,
569 dst_stride,
570 f,
571 subpel_y,
572 y_step,
573 w,
574 h,
575 false,
576 bd,
577 );
578}
579
580/// One reference plane of a stored frame.
581pub(crate) struct RefPlane<'a> {
582 /// Plane base — the frame's first visible sample.
583 pub data: &'a [Sample],
584 /// Row pitch.
585 pub stride: usize,
586 /// `y_crop_width` / `uv_crop_width`: the coded extent, which is what
587 /// `build_mc_border` replicates past.
588 pub width: usize,
589 /// `y_crop_height` / `uv_crop_height`.
590 pub height: usize,
591 /// Bits per sample of the reference frame.
592 pub bd: u8,
593}
594
595/// `dec_build_inter_predictors` (`vp9/decoder/vp9_decodeframe.c:593`):
596/// predict one `w` x `h` plane block whose top-left is plane pixel `(x, y)`
597/// inside the block at MI `(mi_row, mi_col)`. `sf` is the reference's scale
598/// factors; when they scale, the block is mapped into the reference frame
599/// (`scale_value_x/y`), the MV is scaled with it (`vp9_scale_mv`) and the
600/// convolve steps by `x_step_q4` / `y_step_q4`.
601#[allow(clippy::too_many_arguments)]
602pub(crate) fn build_inter_predictors(
603 dst: &mut [Sample],
604 dst_stride: usize,
605 refp: &RefPlane<'_>,
606 mi_col: usize,
607 mi_row: usize,
608 x: usize,
609 y: usize,
610 w: usize,
611 h: usize,
612 mv: (i32, i32),
613 mb_left: i32,
614 mb_right: i32,
615 mb_top: i32,
616 mb_bottom: i32,
617 ss_x: usize,
618 ss_y: usize,
619 kernel: usize,
620 avg: bool,
621 sf: &ScaleFactors,
622 scratch: &mut Vec<Sample>,
623 temp: &mut Vec<Sample>,
624) {
625 let bd = refp.bd;
626 let is_scaled = sf.is_scaled();
627 let mv_q4 = clamp_mv_to_umv_border_sb(
628 mv, w as i32, h as i32, mb_left, mb_right, mb_top, mb_bottom, ss_x, ss_y,
629 );
630
631 // Co-ordinate of the containing block, pixel precision, plus the
632 // block-relative offset (`x`, `y`).
633 let x_pre = (-mb_left >> (3 + ss_x)) + x as i32;
634 let y_pre = (-mb_top >> (3 + ss_y)) + y as i32;
635 let (x_step, y_step, scaled_mv);
636 let (x0_16, y0_16, x0_pre, y0_pre);
637 if is_scaled {
638 // The block is mapped into the reference frame at 1/16-pel (the
639 // `x0_16` used for the border extent) and at pixel precision (`x0`,
640 // which shifts by the scaled MV's integer part below).
641 let x0 = sf.scale_x(x_pre);
642 let y0 = sf.scale_y(y_pre);
643 scaled_mv = sf.scale_mv(
644 mv_q4,
645 mi_col as i32 * 8 + x as i32,
646 mi_row as i32 * 8 + y as i32,
647 );
648 x_step = sf.x_step_q4;
649 y_step = sf.y_step_q4;
650 x0_16 = sf.scale_x(x_pre << SUBPEL_BITS) + scaled_mv.1;
651 y0_16 = sf.scale_y(y_pre << SUBPEL_BITS) + scaled_mv.0;
652 x0_pre = x0;
653 y0_pre = y0;
654 } else {
655 scaled_mv = mv_q4;
656 x_step = SUBPEL_SHIFTS;
657 y_step = SUBPEL_SHIFTS;
658 x0_16 = (x_pre << SUBPEL_BITS) + scaled_mv.1;
659 y0_16 = (y_pre << SUBPEL_BITS) + scaled_mv.0;
660 x0_pre = x_pre;
661 y0_pre = y_pre;
662 }
663 let subpel_x = scaled_mv.1 & SUBPEL_MASK;
664 let subpel_y = scaled_mv.0 & SUBPEL_MASK;
665
666 let frame_width = refp.width as i32;
667 let frame_height = refp.height as i32;
668 // `buf_ptr = ref_frame + y0 * stride + x0` is taken AFTER the MV integer
669 // part but BEFORE the tap padding below, so the direct read keeps its own
670 // copy of the coordinates.
671 let x0_mv = x0_pre + (scaled_mv.1 >> SUBPEL_BITS);
672 let y0_mv = y0_pre + (scaled_mv.0 >> SUBPEL_BITS);
673 let mut x0 = x0_mv;
674 let mut y0 = y0_mv;
675
676 let f = &FILTERS[kernel];
677
678 // `is_scaled || scaled_mv.col || scaled_mv.row || (frame_width & 7) ||
679 // (frame_height & 7)`: the border copy is skipped only when the window is
680 // provably inside the frame.
681 if is_scaled
682 || scaled_mv.1 != 0
683 || scaled_mv.0 != 0
684 || (frame_width & 7) != 0
685 || (frame_height & 7) != 0
686 {
687 let mut x1 = ((x0_16 + (w as i32 - 1) * x_step) >> SUBPEL_BITS) + 1;
688 let mut y1 = ((y0_16 + (h as i32 - 1) * y_step) >> SUBPEL_BITS) + 1;
689 let mut x_pad = false;
690 let mut y_pad = false;
691 if subpel_x != 0 || x_step != SUBPEL_SHIFTS {
692 x0 -= INTERP_EXTEND - 1;
693 x1 += INTERP_EXTEND;
694 x_pad = true;
695 }
696 if subpel_y != 0 || y_step != SUBPEL_SHIFTS {
697 y0 -= INTERP_EXTEND - 1;
698 y1 += INTERP_EXTEND;
699 y_pad = true;
700 }
701 if x0 < 0
702 || x0 > frame_width - 1
703 || x1 < 0
704 || x1 > frame_width - 1
705 || y0 < 0
706 || y0 > frame_height - 1
707 || y1 < 0
708 || y1 > frame_height - 1
709 {
710 let b_w = (x1 - x0 + 1) as usize;
711 let b_h = (y1 - y0 + 1) as usize;
712 let border_offset = usize::from(y_pad) * 3 * b_w + usize::from(x_pad) * 3;
713 scratch.resize(b_w * b_h, 0);
714 build_mc_border(
715 refp.data,
716 refp.stride,
717 scratch,
718 b_w,
719 x0,
720 y0,
721 b_w,
722 b_h,
723 frame_width,
724 frame_height,
725 );
726 inter_predictor(
727 scratch,
728 b_w,
729 border_offset as isize,
730 dst,
731 dst_stride,
732 f,
733 subpel_x,
734 subpel_y,
735 x_step,
736 y_step,
737 w,
738 h,
739 avg,
740 temp,
741 bd,
742 );
743 return;
744 }
745 }
746 // Inside the frame: read directly. The `x0`/`y0` checks above guarantee
747 // the `SUBPEL_TAPS / 2 - 1` tap margin that `inter_predictor` reads
748 // backwards also lies inside the visible plane.
749 let origin = y0_mv as isize * refp.stride as isize + x0_mv as isize;
750 inter_predictor(
751 refp.data,
752 refp.stride,
753 origin,
754 dst,
755 dst_stride,
756 f,
757 subpel_x,
758 subpel_y,
759 x_step,
760 y_step,
761 w,
762 h,
763 avg,
764 temp,
765 bd,
766 );
767}
768
769/// `average_split_mvs` (`vp9/common/vp9_reconinter.c:113`): the per-sub-block
770/// MV of a sub-8x8 inter block, selected by the plane's subsampling.
771///
772/// `ss_idx` 0 (luma and 4:4:4 chroma): the sub-block's own MV; 1 (4:4:0): the
773/// q2-rounded average of the vertical pair (`block`, `block + 2`); 2 (4:2:2):
774/// the horizontal pair (`block`, `block + 1`); 3 (4:2:0): the q4-rounded
775/// average of all four.
776///
777/// `round_mv_comp_qN(v) = (v < 0 ? v - N/2 : v + N/2) / N` — C's truncating
778/// division, which Rust's `/` on integers reproduces.
779pub(crate) fn average_split_mvs(
780 bmi: &[[(i32, i32); 2]; 4],
781 ref_idx: usize,
782 block: usize,
783 ss_x: usize,
784 ss_y: usize,
785) -> (i32, i32) {
786 let q2 = |v: i32| if v < 0 { (v - 1) / 2 } else { (v + 1) / 2 };
787 let q4 = |v: i32| if v < 0 { (v - 2) / 4 } else { (v + 2) / 4 };
788 match (ss_x, ss_y) {
789 (0, 0) => bmi[block][ref_idx],
790 (0, 1) => {
791 let (a, b) = (bmi[block][ref_idx], bmi[block + 2][ref_idx]);
792 (q2(a.0 + b.0), q2(a.1 + b.1))
793 }
794 (1, 0) => {
795 let (a, b) = (bmi[block][ref_idx], bmi[block + 1][ref_idx]);
796 (q2(a.0 + b.0), q2(a.1 + b.1))
797 }
798 _ => {
799 let mut row = 0;
800 let mut col = 0;
801 for b in bmi.iter() {
802 row += b[ref_idx].0;
803 col += b[ref_idx].1;
804 }
805 (q4(row), q4(col))
806 }
807 }
808}