forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

mv.rs642 lines, 20.0 KiBHistoryRawDownload
1//! Macroblock partitioning and motion vector derivation (spec 7.4.5, 8.4.1).
2//!
3//! Motion data lives in the picture's per-4x4-block arrays, written as each
4//! partition is derived. Neighbour derivation (8.4.1.3.2) then reads those same
5//! arrays, which is what makes "the partition inside this macroblock has not
6//! been decoded yet" expressible: a bitmask of the blocks written so far stands
7//! in for the availability rule of clause 6.4.11.7, exactly rather than
8//! approximately.
9
10use crate::dpb::{BLK_DIRECT, BLK_INTRA, BLK_SKIP, Picture};
11use crate::sample::Sample;
12
13/// Prediction mode of a macroblock or sub-macroblock partition (Table 7-13,
14/// 7-14, 7-17, 7-18).
15#[derive(Clone, Copy, PartialEq, Eq, Debug)]
16pub(crate) enum Pred {
17 /// Pred_L0.
18 L0,
19 /// Pred_L1.
20 L1,
21 /// BiPred.
22 Bi,
23 /// Direct (B_Skip, B_Direct_16x16, B_Direct_8x8).
24 Direct,
25}
26
27impl Pred {
28 /// `(predFlagL0, predFlagL1)`; direct mode resolves later.
29 #[inline]
30 pub(crate) fn uses(self, list: usize) -> bool {
31 match self {
32 Pred::L0 => list == 0,
33 Pred::L1 => list == 1,
34 Pred::Bi => true,
35 Pred::Direct => false,
36 }
37 }
38}
39
40/// The partitioning of one inter macroblock.
41#[derive(Clone, Copy, Debug)]
42pub(crate) struct MbShape {
43 /// `NumMbPart( mb_type )`.
44 pub parts: usize,
45 /// `MbPartWidth`/`MbPartHeight` in 4x4 blocks.
46 pub w: usize,
47 pub h: usize,
48 /// `MbPartPredMode` of each partition.
49 pub pred: [Pred; 2],
50 /// mb_type is P_8x8/P_8x8ref0/B_8x8: sub-macroblock types follow.
51 pub sub: bool,
52}
53
54const fn shape(parts: usize, w: usize, h: usize, p0: Pred, p1: Pred) -> MbShape {
55 MbShape {
56 parts,
57 w,
58 h,
59 pred: [p0, p1],
60 sub: false,
61 }
62}
63
64const SUB8X8: MbShape = MbShape {
65 parts: 4,
66 w: 2,
67 h: 2,
68 pred: [Pred::L0, Pred::L0],
69 sub: true,
70};
71
72/// Table 7-13, mb_type 0..4 of a P or SP slice.
73pub(crate) const P_SHAPES: [MbShape; 5] = [
74 shape(1, 4, 4, Pred::L0, Pred::L0),
75 shape(2, 4, 2, Pred::L0, Pred::L0),
76 shape(2, 2, 4, Pred::L0, Pred::L0),
77 SUB8X8,
78 SUB8X8, // P_8x8ref0: same partitioning, ref_idx inferred 0.
79];
80
81/// Table 7-14, mb_type 0..22 of a B slice.
82pub(crate) const B_SHAPES: [MbShape; 23] = [
83 shape(1, 4, 4, Pred::Direct, Pred::Direct),
84 shape(1, 4, 4, Pred::L0, Pred::L0),
85 shape(1, 4, 4, Pred::L1, Pred::L1),
86 shape(1, 4, 4, Pred::Bi, Pred::Bi),
87 shape(2, 4, 2, Pred::L0, Pred::L0),
88 shape(2, 2, 4, Pred::L0, Pred::L0),
89 shape(2, 4, 2, Pred::L1, Pred::L1),
90 shape(2, 2, 4, Pred::L1, Pred::L1),
91 shape(2, 4, 2, Pred::L0, Pred::L1),
92 shape(2, 2, 4, Pred::L0, Pred::L1),
93 shape(2, 4, 2, Pred::L1, Pred::L0),
94 shape(2, 2, 4, Pred::L1, Pred::L0),
95 shape(2, 4, 2, Pred::L0, Pred::Bi),
96 shape(2, 2, 4, Pred::L0, Pred::Bi),
97 shape(2, 4, 2, Pred::L1, Pred::Bi),
98 shape(2, 2, 4, Pred::L1, Pred::Bi),
99 shape(2, 4, 2, Pred::Bi, Pred::L0),
100 shape(2, 2, 4, Pred::Bi, Pred::L0),
101 shape(2, 4, 2, Pred::Bi, Pred::L1),
102 shape(2, 2, 4, Pred::Bi, Pred::L1),
103 shape(2, 4, 2, Pred::Bi, Pred::Bi),
104 shape(2, 2, 4, Pred::Bi, Pred::Bi),
105 SUB8X8,
106];
107
108/// The partitioning of one 8x8 sub-macroblock.
109#[derive(Clone, Copy, Debug)]
110pub(crate) struct SubShape {
111 /// `NumSubMbPart( sub_mb_type )`.
112 pub parts: usize,
113 /// `SubMbPartWidth`/`SubMbPartHeight` in 4x4 blocks.
114 pub w: usize,
115 pub h: usize,
116 /// `SubMbPredMode`.
117 pub pred: Pred,
118}
119
120const fn sub(parts: usize, w: usize, h: usize, pred: Pred) -> SubShape {
121 SubShape { parts, w, h, pred }
122}
123
124/// Table 7-17, sub_mb_type of a P macroblock.
125pub(crate) const P_SUB: [SubShape; 4] = [
126 sub(1, 2, 2, Pred::L0),
127 sub(2, 2, 1, Pred::L0),
128 sub(2, 1, 2, Pred::L0),
129 sub(4, 1, 1, Pred::L0),
130];
131
132/// Table 7-18, sub_mb_type of a B macroblock.
133pub(crate) const B_SUB: [SubShape; 13] = [
134 sub(4, 1, 1, Pred::Direct),
135 sub(1, 2, 2, Pred::L0),
136 sub(1, 2, 2, Pred::L1),
137 sub(1, 2, 2, Pred::Bi),
138 sub(2, 2, 1, Pred::L0),
139 sub(2, 1, 2, Pred::L0),
140 sub(2, 2, 1, Pred::L1),
141 sub(2, 1, 2, Pred::L1),
142 sub(2, 2, 1, Pred::Bi),
143 sub(2, 1, 2, Pred::Bi),
144 sub(4, 1, 1, Pred::L0),
145 sub(4, 1, 1, Pred::L1),
146 sub(4, 1, 1, Pred::Bi),
147];
148
149/// Motion data of one neighbouring partition (8.4.1.3.2).
150#[derive(Clone, Copy, Debug)]
151pub(crate) struct Nb {
152 /// The partition exists and has been decoded (clause 6.4.11.7).
153 pub avail: bool,
154 pub mv: [i16; 2],
155 /// -1 when the neighbour does not use this list, or is intra.
156 pub ref_idx: i8,
157}
158
159impl Default for Nb {
160 /// An unavailable partition, which 8.4.1.3.2 gives reference index -1 —
161 /// *not* 0. A derived `Default` would make it look like a neighbour on
162 /// reference picture 0 and hand the median rules a phantom match.
163 fn default() -> Nb {
164 Nb {
165 avail: false,
166 mv: [0; 2],
167 ref_idx: -1,
168 }
169 }
170}
171
172/// Where the current macroblock's motion data is being written, and which of
173/// its 4x4 blocks already carry it.
174pub(crate) struct MvCtx {
175 pub mb_x: usize,
176 pub mb_y: usize,
177 pub slice_id: u16,
178 /// Bit `y * 4 + x` set once block (x, y) of this macroblock is derived.
179 pub written: u16,
180 /// The macroblock's place in an MBAFF frame (inert otherwise).
181 pub pos: crate::mbaff::MbPos,
182}
183
184impl MvCtx {
185 /// Resolve the 4x4 block at picture block coordinates `(bx, by)` — the
186 /// raster neighbour of a block of the current macroblock — to its index
187 /// in the per-block arrays, or `None` when it is unavailable (outside the
188 /// picture, in another slice, not yet decoded, or not a neighbour Table
189 /// 6-4 allows). In an MBAFF frame the coordinates only say *which*
190 /// neighbour (left, above, above-right, above-left) and row; the block
191 /// actually read is the one 6.4.12.2 derives. The flag is the neighbouring
192 /// macroblock's field mode (the current macroblock's for blocks inside it).
193 #[inline]
194 fn resolve<S: Sample>(
195 &self,
196 pic: &Picture<S>,
197 bx: i32,
198 by: i32,
199 diag: bool,
200 ) -> Option<(usize, bool)> {
201 let w4 = (pic.mb_w * 4) as i32;
202 if self.pos.active {
203 return self.resolve_mbaff(pic, bx, by, diag);
204 }
205 let h4 = (pic.mb_h * 4) as i32;
206 if bx < 0 || by < 0 || bx >= w4 || by >= h4 {
207 return None;
208 }
209 let (mbx, mby) = (bx as usize / 4, by as usize / 4);
210 let addr = mby * pic.mb_w + mbx;
211 if mbx == self.mb_x && mby == self.mb_y {
212 let within = (by as usize % 4) * 4 + bx as usize % 4;
213 if self.written & (1 << within) == 0 {
214 return None;
215 }
216 } else if pic.mb_slice[addr] != self.slice_id {
217 return None;
218 }
219 Some((by as usize * pic.mb_w * 4 + bx as usize, false))
220 }
221
222 /// [`MvCtx::resolve`] for an MBAFF frame (Table 6-4).
223 #[inline(never)]
224 fn resolve_mbaff<S: Sample>(
225 &self,
226 pic: &Picture<S>,
227 bx: i32,
228 by: i32,
229 diag: bool,
230 ) -> Option<(usize, bool)> {
231 let w4 = (pic.mb_w * 4) as i32;
232 {
233 let (rx, ry) = (bx - 4 * self.mb_x as i32, by - 4 * self.mb_y as i32);
234 if (0..4).contains(&rx) && (0..4).contains(&ry) {
235 if self.written & (1 << (ry * 4 + rx)) == 0 {
236 return None;
237 }
238 return Some((by as usize * w4 as usize + bx as usize, self.pos.field));
239 }
240 let xn = if rx < 0 {
241 -1
242 } else if rx >= 4 {
243 16
244 } else {
245 rx * 4
246 };
247 // Neighbour D of a block below the top row sits at luma row
248 // `y - 1`, the last row of the block above; the others at the
249 // block's own first row.
250 let yn = if ry < 0 {
251 -1
252 } else if diag {
253 ry * 4 + 3
254 } else {
255 ry * 4
256 };
257 let l = crate::mbaff::locate(pic, &self.pos, xn, yn, 16, 16)?;
258 let gbx = (l.slot % pic.mb_w) * 4 + (l.xw as usize >> 2);
259 let gby = (l.slot / pic.mb_w) * 4 + (l.yw as usize >> 2);
260 Some((gby * w4 as usize + gbx, l.field))
261 }
262 }
263
264 /// [`MvCtx::at`] outside MBAFF frames: raster addressing.
265 #[inline]
266 fn at_plain<S: Sample>(&self, pic: &Picture<S>, bx: i32, by: i32, list: usize) -> Nb {
267 let w4 = (pic.mb_w * 4) as i32;
268 let h4 = (pic.mb_h * 4) as i32;
269 if bx < 0 || by < 0 || bx >= w4 || by >= h4 {
270 return Nb::default();
271 }
272 let (mbx, mby) = (bx as usize / 4, by as usize / 4);
273 let addr = mby * pic.mb_w + mbx;
274 if mbx == self.mb_x && mby == self.mb_y {
275 let within = (by as usize % 4) * 4 + bx as usize % 4;
276 if self.written & (1 << within) == 0 {
277 return Nb::default();
278 }
279 } else if pic.mb_slice[addr] != self.slice_id {
280 return Nb::default();
281 }
282 let idx = by as usize * pic.mb_w * 4 + bx as usize;
283 if pic.blk[idx] & BLK_INTRA != 0 {
284 // Available as a partition, but contributes no motion (8.4.1.3.2).
285 return Nb {
286 avail: true,
287 mv: [0; 2],
288 ref_idx: -1,
289 };
290 }
291 Nb {
292 avail: true,
293 mv: pic.mv[idx][list],
294 ref_idx: pic.ref_idx[idx][list],
295 }
296 }
297
298 /// Motion data of the 4x4 block at picture block coordinates `(bx, by)`,
299 /// for list `list`.
300 #[inline]
301 pub(crate) fn at<S: Sample>(&self, pic: &Picture<S>, bx: i32, by: i32, list: usize) -> Nb {
302 self.at_d(pic, bx, by, list, false)
303 }
304
305 /// [`MvCtx::at`] for neighbour D when `diag` (the block diagonally above
306 /// and left, `(bx - 1, by - 1)`).
307 #[inline]
308 fn at_d<S: Sample>(&self, pic: &Picture<S>, bx: i32, by: i32, list: usize, diag: bool) -> Nb {
309 let Some((idx, nfield)) = self.resolve(pic, bx, by, diag) else {
310 return Nb::default();
311 };
312 if pic.blk[idx] & BLK_INTRA != 0 {
313 // Available as a partition, but contributes no motion (8.4.1.3.2).
314 return Nb {
315 avail: true,
316 mv: [0; 2],
317 ref_idx: -1,
318 };
319 }
320 let mut nb = Nb {
321 avail: true,
322 mv: pic.mv[idx][list],
323 ref_idx: pic.ref_idx[idx][list],
324 };
325 if self.pos.active && nfield != self.pos.field && nb.ref_idx >= 0 {
326 nb = self.rescale(nb);
327 }
328 nb
329 }
330
331 #[inline(never)]
332 fn rescale(&self, mut nb: Nb) -> Nb {
333 {
334 // 8.4.1.3.2: motion of a neighbour of the other mode is converted
335 // to the current macroblock's units.
336 if self.pos.field {
337 nb.mv[1] /= 2;
338 nb.ref_idx *= 2;
339 } else {
340 nb.mv[1] = nb.mv[1].wrapping_mul(2);
341 nb.ref_idx >>= 1;
342 }
343 }
344 nb
345 }
346
347 /// Neighbours A, B and C of a partition whose top-left 4x4 block is at
348 /// macroblock-relative `(px, py)` and whose `predPartWidth` is `pw` blocks
349 /// (clause 6.4.11.7, with the C-to-D substitution of 8-214).
350 #[inline]
351 pub(crate) fn neighbours<S: Sample>(
352 &self,
353 pic: &Picture<S>,
354 px: usize,
355 py: usize,
356 pw: usize,
357 list: usize,
358 ) -> [Nb; 3] {
359 let bx = (self.mb_x * 4 + px) as i32;
360 let by = (self.mb_y * 4 + py) as i32;
361 if self.pos.active {
362 return self.neighbours_mbaff(pic, bx, by, pw, list);
363 }
364 let a = self.at_plain(pic, bx - 1, by, list);
365 let b = self.at_plain(pic, bx, by - 1, list);
366 let mut c = self.at_plain(pic, bx + pw as i32, by - 1, list);
367 if !c.avail {
368 c = self.at_plain(pic, bx - 1, by - 1, list);
369 }
370 [a, b, c]
371 }
372
373 #[inline(never)]
374 fn neighbours_mbaff<S: Sample>(
375 &self,
376 pic: &Picture<S>,
377 bx: i32,
378 by: i32,
379 pw: usize,
380 list: usize,
381 ) -> [Nb; 3] {
382 let a = self.at_d(pic, bx - 1, by, list, false);
383 let b = self.at_d(pic, bx, by - 1, list, false);
384 let mut c = self.at_d(pic, bx + pw as i32, by - 1, list, false);
385 if !c.avail {
386 c = self.at_d(pic, bx - 1, by - 1, list, true);
387 }
388 [a, b, c]
389 }
390}
391
392#[inline]
393fn median(a: i16, b: i16, c: i16) -> i16 {
394 a.max(b).min(a.min(b).max(c))
395}
396
397/// Motion vector prediction (clause 8.4.1.3), including the directional
398/// segmentation shortcuts of Equations 8-203 to 8-206.
399///
400/// `part_w`/`part_h` are `MbPartWidth`/`MbPartHeight` in 4x4 blocks and only
401/// select those shortcuts; `part` is the macroblock partition index.
402pub(crate) fn predict_mv(
403 n: &[Nb; 3],
404 ref_idx: i8,
405 part_w: usize,
406 part_h: usize,
407 part: usize,
408) -> [i16; 2] {
409 let [a, mut b, mut c] = *n;
410 match (part_w, part_h, part) {
411 (4, 2, 0) if b.ref_idx == ref_idx => return b.mv,
412 (4, 2, 1) if a.ref_idx == ref_idx => return a.mv,
413 (2, 4, 0) if a.ref_idx == ref_idx => return a.mv,
414 (2, 4, 1) if c.ref_idx == ref_idx => return c.mv,
415 _ => {}
416 }
417 // 8.4.1.3.1 step 1.
418 if !b.avail && !c.avail && a.avail {
419 b = a;
420 c = a;
421 }
422 let matches = u8::from(a.ref_idx == ref_idx)
423 + u8::from(b.ref_idx == ref_idx)
424 + u8::from(c.ref_idx == ref_idx);
425 if matches == 1 {
426 if a.ref_idx == ref_idx {
427 return a.mv;
428 }
429 if b.ref_idx == ref_idx {
430 return b.mv;
431 }
432 return c.mv;
433 }
434 [
435 median(a.mv[0], b.mv[0], c.mv[0]),
436 median(a.mv[1], b.mv[1], c.mv[1]),
437 ]
438}
439
440/// `MinPositive` (Equation 8-187).
441#[inline]
442pub(crate) fn min_positive(x: i8, y: i8) -> i8 {
443 if x >= 0 && y >= 0 { x.min(y) } else { x.max(y) }
444}
445
446/// Record one 4x4 block's motion data into the picture arrays.
447#[allow(clippy::too_many_arguments)]
448pub(crate) fn write_block<S: Sample>(
449 pic: &mut Picture<S>,
450 ctx: &mut MvCtx,
451 px: usize,
452 py: usize,
453 mv: [[i16; 2]; 2],
454 ref_idx: [i8; 2],
455 ref_id: [i32; 2],
456 flags: u8,
457) {
458 let bx = ctx.mb_x * 4 + px;
459 let by = ctx.mb_y * 4 + py;
460 let idx = by * pic.mb_w * 4 + bx;
461 pic.mv[idx] = mv;
462 pic.ref_idx[idx] = ref_idx;
463 pic.ref_id[idx] = ref_id;
464 pic.blk[idx] = flags;
465 ctx.written |= 1 << (py * 4 + px);
466}
467
468/// Mark every 4x4 block of the current macroblock as intra, clearing the motion
469/// data an inter neighbour would otherwise read (8.4.1.3.2, 9.3.3.1.1.7).
470pub(crate) fn write_intra_mb<S: Sample>(pic: &mut Picture<S>, mb_x: usize, mb_y: usize) {
471 let w4 = pic.mb_w * 4;
472 for py in 0..4 {
473 let base = (mb_y * 4 + py) * w4 + mb_x * 4;
474 for idx in base..base + 4 {
475 pic.mv[idx] = [[0; 2]; 2];
476 pic.ref_idx[idx] = [-1; 2];
477 pic.ref_id[idx] = [-1; 2];
478 pic.mvd_abs[idx] = [0; 4];
479 pic.blk[idx] = BLK_INTRA;
480 }
481 }
482}
483
484/// Record `Abs( mvd )` for one 4x4 block, saturated at the largest value the
485/// context thresholds of 9.3.3.1.1.7 can distinguish.
486#[inline]
487pub(crate) fn write_mvd<S: Sample>(
488 pic: &mut Picture<S>,
489 mb_x: usize,
490 mb_y: usize,
491 px: usize,
492 py: usize,
493 list: usize,
494 mvd: [i16; 2],
495) {
496 let idx = (mb_y * 4 + py) * pic.mb_w * 4 + mb_x * 4 + px;
497 let e = &mut pic.mvd_abs[idx];
498 e[list * 2] = mvd[0].unsigned_abs().min(127) as u8;
499 e[list * 2 + 1] = mvd[1].unsigned_abs().min(127) as u8;
500}
501
502/// `absMvdCompN` of the neighbouring partition at picture block coordinates
503/// `(bx, by)` (9.3.3.1.1.7): zero when the neighbour is unavailable, skipped,
504/// intra or direct coded.
505pub(crate) fn neighbour_mvd<S: Sample>(
506 pic: &Picture<S>,
507 ctx: &MvCtx,
508 bx: i32,
509 by: i32,
510 list: usize,
511) -> [u32; 2] {
512 let Some((idx, nfield)) = ctx.resolve(pic, bx, by, false) else {
513 return [0; 2];
514 };
515 if pic.blk[idx] & (BLK_INTRA | BLK_SKIP | BLK_DIRECT) != 0 || pic.ref_idx[idx][list] < 0 {
516 return [0; 2];
517 }
518 let e = pic.mvd_abs[idx];
519 let mut out = [u32::from(e[list * 2]), u32::from(e[list * 2 + 1])];
520 if ctx.pos.active && nfield != ctx.pos.field {
521 // The vertical component is compared in the current macroblock's
522 // units (9.3.3.1.1.7).
523 if ctx.pos.field {
524 out[1] /= 2;
525 } else {
526 out[1] *= 2;
527 }
528 }
529 out
530}
531
532/// `condTermFlagN` for ref_idx_lX (9.3.3.1.1.6).
533pub(crate) fn ref_idx_cond<S: Sample>(
534 pic: &Picture<S>,
535 ctx: &MvCtx,
536 bx: i32,
537 by: i32,
538 list: usize,
539) -> usize {
540 let Some((idx, nfield)) = ctx.resolve(pic, bx, by, false) else {
541 return 0;
542 };
543 if pic.blk[idx] & (BLK_INTRA | BLK_SKIP | BLK_DIRECT) != 0 {
544 return 0;
545 }
546 // refIdxZeroFlagN: a frame macroblock reading a field neighbour in an
547 // MBAFF frame compares against 1, because field reference indices count
548 // fields.
549 let limit = if ctx.pos.active && !ctx.pos.field && nfield {
550 1
551 } else {
552 0
553 };
554 usize::from(pic.ref_idx[idx][list] > limit)
555}
556
557#[cfg(test)]
558mod tests {
559 use super::*;
560
561 fn nb(avail: bool, mv: [i16; 2], ref_idx: i8) -> Nb {
562 Nb { avail, mv, ref_idx }
563 }
564
565 /// With exactly one neighbour on the same reference picture, that
566 /// neighbour's vector is the prediction (Equation 8-211).
567 #[test]
568 fn single_matching_reference_wins() {
569 let n = [
570 nb(true, [4, 4], 1),
571 nb(true, [8, 8], 0),
572 nb(true, [12, 12], 2),
573 ];
574 assert_eq!(predict_mv(&n, 0, 4, 4, 0), [8, 8]);
575 }
576
577 /// Otherwise the prediction is the component-wise median (8-212, 8-213).
578 #[test]
579 fn median_of_three_when_several_match() {
580 let n = [
581 nb(true, [4, -8], 0),
582 nb(true, [8, 2], 0),
583 nb(true, [-2, 6], 1),
584 ];
585 assert_eq!(predict_mv(&n, 0, 4, 4, 0), [4, 2]);
586 }
587
588 /// B and C unavailable with A available copies A into both, which turns the
589 /// median into A (8-207 to 8-210).
590 #[test]
591 fn unavailable_b_and_c_fall_back_to_a() {
592 let n = [
593 nb(true, [6, -6], 3),
594 nb(false, [0, 0], -1),
595 nb(false, [0, 0], -1),
596 ];
597 assert_eq!(predict_mv(&n, 0, 4, 4, 0), [6, -6]);
598 // With refIdx 3 the single-match rule fires first, same answer.
599 assert_eq!(predict_mv(&n, 3, 4, 4, 0), [6, -6]);
600 }
601
602 /// The 16x8 and 8x16 shortcuts bypass the median entirely.
603 #[test]
604 fn directional_segmentation_shortcuts() {
605 let n = [
606 nb(true, [1, 1], 0),
607 nb(true, [2, 2], 0),
608 nb(true, [3, 3], 0),
609 ];
610 assert_eq!(predict_mv(&n, 0, 4, 2, 0), [2, 2], "16x8 part 0 takes B");
611 assert_eq!(predict_mv(&n, 0, 4, 2, 1), [1, 1], "16x8 part 1 takes A");
612 assert_eq!(predict_mv(&n, 0, 2, 4, 0), [1, 1], "8x16 part 0 takes A");
613 assert_eq!(predict_mv(&n, 0, 2, 4, 1), [3, 3], "8x16 part 1 takes C");
614 // A shortcut that does not match the reference index falls through.
615 assert_eq!(predict_mv(&n, 1, 4, 2, 0), [2, 2], "median of 1,2,3");
616 }
617
618 #[test]
619 fn min_positive_prefers_the_non_negative() {
620 assert_eq!(min_positive(2, 5), 2);
621 assert_eq!(min_positive(-1, 5), 5);
622 assert_eq!(min_positive(-1, -1), -1);
623 assert_eq!(min_positive(0, -1), 0);
624 }
625
626 /// Every B macroblock type maps to the partition shape Table 7-14 states.
627 #[test]
628 fn b_shapes_match_table_7_14() {
629 assert_eq!(B_SHAPES[0].pred[0], Pred::Direct);
630 assert_eq!((B_SHAPES[3].parts, B_SHAPES[3].pred[0]), (1, Pred::Bi));
631 // The 16x8 / 8x16 pairs alternate, starting at mb_type 4.
632 #[allow(clippy::needless_range_loop)]
633 for t in 4..22 {
634 let s = B_SHAPES[t];
635 assert_eq!(s.parts, 2, "mb_type {t}");
636 let (w, h) = if t % 2 == 0 { (4, 2) } else { (2, 4) };
637 assert_eq!((s.w, s.h), (w, h), "mb_type {t}");
638 }
639 assert!(B_SHAPES[22].sub);
640 assert!(P_SHAPES[3].sub && P_SHAPES[4].sub);
641 }
642}