forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

modes.rs567 lines, 19.0 KiBHistoryRawDownload
1//! Macroblock prediction modes and their coding trees
2//! (RFC 6386 §8.2, §11.2, §11.4, §16.1).
3
4use crate::bool::BoolDecoder;
5
6/// Intra macroblock luma modes (RFC 6386 §8.2 enum values are normative;
7/// chroma modes are the first four).
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9#[repr(u8)]
10pub enum YMode {
11 /// Predict DC from the row above and column to the left.
12 DcPred = 0,
13 /// Predict rows from the row above.
14 VPred = 1,
15 /// Predict columns from the column to the left.
16 HPred = 2,
17 /// True-motion (second-difference) prediction.
18 TmPred = 3,
19 /// Each 4x4 Y subblock is independently predicted.
20 BPred = 4,
21}
22
23impl YMode {
24 /// Value as coded in the trees (the RFC's enum integers).
25 pub fn as_u8(self) -> u8 {
26 self as u8
27 }
28
29 /// The subblock mode a 16x16 mode masquerades as for B_PRED context
30 /// purposes (RFC 6386 §11.3 item 4).
31 pub fn as_bmode(self) -> BMode {
32 match self {
33 YMode::DcPred => BMode::BDCPred,
34 YMode::VPred => BMode::BVEPred,
35 YMode::HPred => BMode::BHEPred,
36 YMode::TmPred => BMode::BTMPred,
37 YMode::BPred => unreachable!("B_PRED has no single subblock mode"),
38 }
39 }
40}
41
42/// The ten 4x4 subblock prediction modes (RFC 6386 §11.2).
43#[derive(Debug, Clone, Copy, PartialEq, Eq)]
44#[repr(u8)]
45pub enum BMode {
46 /// DC from above row and left column.
47 BDCPred = 0,
48 /// True-motion.
49 BTMPred = 1,
50 /// Vertical from smoothed above row.
51 BVEPred = 2,
52 /// Horizontal from smoothed left column.
53 BHEPred = 3,
54 /// Down-left diagonal.
55 BLDPred = 4,
56 /// Down-right diagonal.
57 BRDPred = 5,
58 /// Vertical-right diagonal.
59 BVRPred = 6,
60 /// Vertical-left diagonal.
61 BVLPred = 7,
62 /// Horizontal-down diagonal.
63 BHDPred = 8,
64 /// Horizontal-up diagonal.
65 BHUPred = 9,
66}
67
68impl BMode {
69 /// Value as coded in the bmode tree (the RFC's enum integers).
70 pub fn as_u8(self) -> u8 {
71 self as u8
72 }
73}
74
75/// Key-frame Y-mode coding tree (RFC 6386 §11.2): B_PRED, then the four
76/// full-block modes. Leaf values are the [`YMode`] enum integers.
77pub const KF_YMODE_TREE: [i8; 8] = [-4, 2, 4, 6, 0, -1, -2, -3];
78
79/// Key-frame Y-mode probabilities (fixed; RFC 6386 §11.2 and the
80/// reference decoder's `kf_y_mode_probs`).
81pub const KF_YMODE_PROBS: [u8; 4] = [145, 156, 163, 128];
82
83/// Interframe Y-mode coding tree (RFC 6386 §8.2).
84pub const YMODE_TREE: [i8; 8] = [0, 2, 4, 6, -1, -2, -3, -4];
85
86/// Chroma-mode coding tree (RFC 6386 §8.2/§11.4): DC, V, H, TM.
87pub const UV_MODE_TREE: [i8; 6] = [0, 2, -1, 4, -2, -3];
88
89/// Key-frame chroma-mode probabilities (fixed; RFC 6386 §11.4).
90pub const KF_UV_MODE_PROBS: [u8; 3] = [142, 114, 183];
91
92/// 4x4 subblock-mode coding tree (RFC 6386 §11.2). Leaf values are the
93/// [`BMode`] enum integers; note the leaf 0 (B_DC_PRED) relies on the
94/// tree reader's `i <= 0` leaf test.
95pub const BMODE_TREE: [i8; 18] = [
96 0, 2, // B_DC_PRED = "0"
97 -1, 4, // B_TM = "10"
98 -2, 6, // B_VE = "110"
99 8, 12, // inner
100 -3, 10, // B_HE = "11100"
101 -5, -6, // B_RD = "111010", B_VR = "111011"
102 -4, 14, // B_LD = "111110"
103 -7, 16, // B_VL = "1111110"
104 -8, -9, // B_HD = "11111110", B_HU = "11111111"
105];
106
107/// Subblock modes in interframes decode with this constant probability
108/// array, no context (RFC 6386 §11.3 item 5 / §16).
109pub const INTER_BMODE_PROBS: [u8; 9] = [120, 90, 79, 133, 87, 85, 80, 111, 151];
110
111/// Decode a key-frame Y mode (RFC 6386 §11.2).
112pub fn read_kf_ymode(d: &mut BoolDecoder<'_>) -> YMode {
113 YMode::from_u8(d.read_tree(&KF_YMODE_TREE, &KF_YMODE_PROBS))
114}
115
116/// Decode an interframe Y mode at the persisted probabilities
117/// (RFC 6386 §16.1; `probs` is the frame header's updated array).
118pub fn read_ymode(d: &mut BoolDecoder<'_>, probs: &[u8; 4]) -> YMode {
119 YMode::from_u8(d.read_tree(&YMODE_TREE, probs))
120}
121
122/// Decode a chroma mode at the given (key-frame fixed or persisted
123/// interframe) probabilities.
124pub fn read_uvmode(d: &mut BoolDecoder<'_>, probs: &[u8; 3]) -> YMode {
125 YMode::from_u8(d.read_tree(&UV_MODE_TREE, probs))
126}
127
128/// Decode a 4x4 subblock mode at `probs` (RFC 6386 §11.3: in key frames
129/// the probabilities come from the above/left context table).
130pub fn read_bmode(d: &mut BoolDecoder<'_>, probs: &[u8]) -> BMode {
131 BMode::from_u8(d.read_tree(&BMODE_TREE, probs))
132}
133
134impl YMode {
135 fn from_u8(v: u8) -> YMode {
136 match v {
137 0 => YMode::DcPred,
138 1 => YMode::VPred,
139 2 => YMode::HPred,
140 3 => YMode::TmPred,
141 4 => YMode::BPred,
142 _ => unreachable!("tree cannot produce mode {v}"),
143 }
144 }
145}
146
147impl BMode {
148 fn from_u8(v: u8) -> BMode {
149 match v {
150 0 => BMode::BDCPred,
151 1 => BMode::BTMPred,
152 2 => BMode::BVEPred,
153 3 => BMode::BHEPred,
154 4 => BMode::BLDPred,
155 5 => BMode::BRDPred,
156 6 => BMode::BVRPred,
157 7 => BMode::BVLPred,
158 8 => BMode::BHDPred,
159 9 => BMode::BHUPred,
160 _ => unreachable!("tree cannot produce bmode {v}"),
161 }
162 }
163}
164
165/// Inter-frame motion-vector reference modes (RFC 6386 §16.2), offset
166/// from the intra modes by `num_ymodes` in the reference enum; here they
167/// are their own small enum with the RFC's tree integers.
168#[derive(Debug, Clone, Copy, PartialEq, Eq)]
169#[repr(u8)]
170pub enum MvRef {
171 /// Zero motion vector ("10" second branch; coded 0).
172 Zero = 0,
173 /// "Nearest" vector (coded 1).
174 Nearest = 1,
175 /// "Near" vector (coded 2).
176 Near = 2,
177 /// Explicit offset from best (coded 3).
178 New = 3,
179 /// Per-subblock vectors (coded 4).
180 Split = 4,
181}
182
183/// mv_ref coding tree (RFC 6386 §16.2): leaves are [`MvRef`] integers.
184/// mv_ref coding tree (RFC 6386 §16.2); leaves are [`MvRef`] integers.
185pub const MV_REF_TREE: [i8; 8] = [0, 2, -1, 4, -2, 6, -3, -4];
186
187/// Motion-vector component: row then column, each with the 19-probability
188/// table (§17.1).
189/// Probability-table offsets of one MV component (RFC 6386 §17.1).
190pub const MVP_IS_SHORT: usize = 0;
191/// Sign probability offset (§17.1).
192pub const MVP_SIGN: usize = 1;
193/// Short-value tree offset (§17.1).
194/// Short-value tree offset (§17.1).
195pub const MVP_SHORT: usize = 2;
196/// Long-value bit probabilities offset (§17.1).
197/// Long-value bit probabilities offset (§17.1).
198pub const MVP_BITS: usize = MVP_SHORT + 7;
199/// Total probabilities per MV component (§17.1).
200/// Total probabilities per MV component (§17.1).
201pub const MVP_COUNT: usize = MVP_BITS + 10;
202
203/// Small-value coding tree for MV components (§17.1 `small_mvtree`).
204/// Small-value coding tree for MV components (§17.1).
205pub const SMALL_MV_TREE: [i8; 14] = [
206 2, 8, // "0" / "1" subtrees
207 4, 6, // "00" / "01"
208 0, -1, // 0 = "000", 1 = "001"
209 -2, -3, // 2, 3
210 10, 12, // "10" / "11"
211 -4, -5, // 4, 5
212 -6, -7, // 6, 7
213];
214
215/// SPLITMV partition shapes tree + fixed probs (§16.4). Leaves are the
216/// RFC's `MVpartition` integers: 0 = top/bottom, 1 = left/right,
217/// 2 = quarters, 3 = MV_16.
218pub const MV_PARTITION_TREE: [i8; 6] = [-3, 2, -2, 4, 0, -1];
219/// SPLITMV partition tree fixed probabilities (§16.4).
220pub const MV_PARTITION_PROBS: [u8; 3] = [110, 111, 150];
221
222/// The four SPLITMV partitionings as subblock groups (§16.4), indexed
223/// by the `MVpartition` integer: each group shares one motion vector,
224/// decoded in the listed (leader) order.
225pub const MV_PARTITIONS: [&[usize]; 4] = [
226 // mv_top_bottom: {0..7} then {8..15}.
227 &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
228 // mv_left_right: {0,1,4,5,8,9,12,13} then {2,3,6,7,10,11,14,15}.
229 &[0, 1, 4, 5, 8, 9, 12, 13, 2, 3, 6, 7, 10, 11, 14, 15],
230 // mv_quarters: {0,1,4,5} {2,3,6,7} {8,9,12,13} {10,11,14,15}.
231 &[0, 1, 4, 5, 2, 3, 6, 7, 8, 9, 12, 13, 10, 11, 14, 15],
232 // MV_16: every subblock gets its own vector, raster order.
233 &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
234];
235
236/// Number of distinct vectors per partition shape (§16.4).
237pub const MV_PARTITION_COUNT: [usize; 4] = [2, 2, 4, 16];
238
239/// Inter subblock modes (§16.4): reuse the already-coded MV left/above,
240/// zero, or an explicit offset.
241#[derive(Debug, Clone, Copy, PartialEq, Eq)]
242#[repr(u8)]
243pub enum SubMvRef {
244 /// Copy the left neighbour's MV (coded 0).
245 Left4x4 = 0,
246 /// Copy the above neighbour's MV (coded 1).
247 Above4x4 = 1,
248 /// Zero MV (coded 2).
249 Zero4x4 = 2,
250 /// Explicit offset from best (coded 3).
251 New4x4 = 3,
252}
253
254/// Sub-MV coding tree (RFC 6386 §16.4); leaves are [`SubMvRef`] integers.
255/// Sub-MV coding tree (RFC 6386 §16.4); leaves are [`SubMvRef`] integers.
256pub const SUB_MV_REF_TREE: [i8; 6] = [0, 2, -1, 4, -2, -3];
257
258/// Context-conditioned sub-MV probabilities (§16.4 `sub_mv_ref_prob`).
259pub const SUB_MV_REF_PROBS: [[u8; 3]; 5] = [
260 [147, 136, 18],
261 [106, 145, 1],
262 [179, 121, 1],
263 [223, 1, 34],
264 [208, 1, 1],
265];
266
267/// Sub-MV tree context (§16.4 `vp8_mvCont`): 4 = left==above==zero,
268/// 3 = left==above, 2 = above zero, 1 = left zero, 0 = normal.
269/// Sub-MV tree context (§16.4 `vp8_mvCont`): 4 = left==above==zero,
270pub fn mv_cont(l: (i16, i16), a: (i16, i16)) -> usize {
271 let lez = l == (0, 0);
272 let aez = a == (0, 0);
273 let lea = l == a;
274 if lea && lez {
275 4
276 } else if lea {
277 3
278 } else if aez {
279 2
280 } else if lez {
281 1
282 } else {
283 0
284 }
285}
286
287/// Weighted-census probability table for the mv_ref tree
288/// (§16.3 `vp8_mode_contexts`, indexed by clamped count per slot).
289pub const MODE_CONTEXTS: [[u8; 4]; 6] = [
290 [7, 1, 1, 143],
291 [14, 18, 14, 107],
292 [135, 64, 57, 68],
293 [60, 56, 128, 65],
294 [159, 134, 128, 34],
295 [234, 188, 128, 28],
296];
297
298/// Decode an mv_ref at the census-derived probabilities (§16.2/§16.3).
299pub fn read_mv_ref(d: &mut BoolDecoder<'_>, probs: &[u8; 4]) -> MvRef {
300 match d.read_tree(&MV_REF_TREE, probs) {
301 0 => MvRef::Zero,
302 1 => MvRef::Nearest,
303 2 => MvRef::Near,
304 3 => MvRef::New,
305 4 => MvRef::Split,
306 v => unreachable!("mv_ref tree produced {v}"),
307 }
308}
309
310/// Decode a SPLITMV partition shape (§16.4); returns the `MVpartition`
311/// integer indexing [`MV_PARTITIONS`].
312pub fn read_mv_partition(d: &mut BoolDecoder<'_>) -> usize {
313 usize::from(d.read_tree(&MV_PARTITION_TREE, &MV_PARTITION_PROBS))
314}
315
316/// Decode a sub-MV reference (§16.4) at the context's probabilities.
317pub fn read_sub_mv_ref(d: &mut BoolDecoder<'_>, ctx: usize) -> SubMvRef {
318 match d.read_tree(&SUB_MV_REF_TREE, &SUB_MV_REF_PROBS[ctx]) {
319 0 => SubMvRef::Left4x4,
320 1 => SubMvRef::Above4x4,
321 2 => SubMvRef::Zero4x4,
322 3 => SubMvRef::New4x4,
323 v => unreachable!("sub_mv_ref tree produced {v}"),
324 }
325}
326
327/// First subblock of each SPLITMV subset, per partition shape (libvpx
328/// `vp8_mbsplit_offset`; shapes 16x8, 8x16, 8x8, 4x4).
329pub const MBSPLIT_OFFSET: [[u8; 16]; 4] = [
330 [0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
331 [0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
332 [0, 2, 8, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
333 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
334];
335
336/// Subblocks filled by one subset, per partition shape (libvpx
337/// `mbsplit_fill_count`).
338pub const MBSPLIT_FILL_COUNT: [usize; 4] = [8, 8, 4, 1];
339
340/// Subblock indices each subset fills, per shape (libvpx
341/// `mbsplit_fill_offset`; subset j starts at j * fill_count).
342pub const MBSPLIT_FILL_OFFSET: [[u8; 16]; 4] = [
343 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
344 [0, 1, 4, 5, 8, 9, 12, 13, 2, 3, 6, 7, 10, 11, 14, 15],
345 [0, 1, 4, 5, 2, 3, 6, 7, 8, 9, 12, 13, 10, 11, 14, 15],
346 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
347];
348
349/// Decode one motion-vector component (§17.1, libvpx `read_mvcomponent`):
350/// either a 3-bit small value off [`SMALL_MV_TREE`] or a 9-bit long form
351/// whose bit 3 is sometimes implicit, then a sign flag.
352pub fn read_mv_component(d: &mut BoolDecoder<'_>, probs: &[u8; MVP_COUNT]) -> i16 {
353 let mut x: i32 = 0;
354 if d.read_bool(probs[MVP_IS_SHORT]) {
355 // Large: bits 0..2, then 8..4 (bit 3 is implicit below).
356 for i in 0..3 {
357 x += i32::from(d.read_bool(probs[MVP_BITS + i])) << i;
358 }
359 let mut i = 9;
360 loop {
361 x += i32::from(d.read_bool(probs[MVP_BITS + i])) << i;
362 i -= 1;
363 if i <= 3 {
364 break;
365 }
366 }
367 if (x & 0xFFF0) == 0 || d.read_bool(probs[MVP_BITS + 3]) {
368 x += 8;
369 }
370 } else {
371 x = i32::from(d.read_tree(&SMALL_MV_TREE, &probs[MVP_SHORT..MVP_SHORT + 7]));
372 }
373 if x != 0 && d.read_bool(probs[MVP_SIGN]) {
374 x = -x;
375 }
376 x as i16
377}
378
379/// Both components of a whole-block motion vector (§17.1); the decoded
380/// quarter-pel components double into the decoder's eighth-pel units.
381pub fn read_mv(d: &mut BoolDecoder<'_>, probs: &[[u8; MVP_COUNT]; 2]) -> (i16, i16) {
382 (
383 read_mv_component(d, &probs[0]).wrapping_mul(2),
384 read_mv_component(d, &probs[1]).wrapping_mul(2),
385 )
386}
387
388/// Flip a candidate motion vector when the reference it was coded
389/// against has a different sign bias than the current reference
390/// (§16.3, libvpx `mv_bias`).
391pub fn mv_bias(from_bias: bool, to_bias: bool, mv: (i16, i16)) -> (i16, i16) {
392 if from_bias != to_bias {
393 (-mv.0, -mv.1)
394 } else {
395 mv
396 }
397}
398
399/// One neighbour macroblock's motion-vector census input (§16.3).
400pub struct MvNeighbor {
401 /// The MB is intra-coded (no MV contribution at all).
402 pub intra: bool,
403 /// The MB is inter-coded with a zero motion vector (counts like
404 /// intra in the census).
405 pub zero: bool,
406 /// The MB's motion vector (already sign-bias-adjusted if applicable
407 /// — pass `sign_bias` and the census applies it).
408 pub mv: (i16, i16),
409 /// The MB is SPLITMV (feeds the split census count).
410 pub is_split: bool,
411 /// The sign bias of the reference the MB's MV points into.
412 pub sign_bias: bool,
413}
414
415/// Slot indices of the census result (§16.3).
416pub const CNT_INTRA: usize = 0;
417/// Slot index: nearest-candidate count.
418pub const CNT_NEAREST: usize = 1;
419/// Slot index: near-candidate count.
420pub const CNT_NEAR: usize = 2;
421/// Slot index: split-neighbour count.
422pub const CNT_SPLITMV: usize = 3;
423
424/// The neighborhood motion-vector census (§16.3, libvpx
425/// `find_near_mvs` inlined in `read_mb_modes_mv`): up to three distinct
426/// candidate vectors with per-slot counts, scanned above, left,
427/// above-left; each inter neighbour counts 2/2/1 toward either the
428/// candidate slot it extends or the zero/intra count.
429pub fn find_near_mvs(
430 above: MvNeighbor,
431 left: MvNeighbor,
432 aboveleft: MvNeighbor,
433 to_bias: bool,
434) -> ([(i16, i16); 4], [i32; 4]) {
435 let mut mvs = [(0i16, 0i16); 4];
436 let mut cnt = [0i32; 4];
437 let mut idx = 0usize;
438 let mut cntx = 0usize;
439
440 // Above: the first candidate is pushed unconditionally.
441 if !above.intra {
442 if !above.zero {
443 idx += 1;
444 mvs[idx] = mv_bias(above.sign_bias, to_bias, above.mv);
445 cntx += 1;
446 }
447 cnt[cntx] += 2;
448 }
449 for (mb, w) in [(&left, 2), (&aboveleft, 1)] {
450 if !mb.intra {
451 if !mb.zero {
452 let this = mv_bias(mb.sign_bias, to_bias, mb.mv);
453 if this != mvs[idx] {
454 idx += 1;
455 mvs[idx] = this;
456 cntx += 1;
457 }
458 cnt[cntx] += w;
459 } else {
460 cnt[CNT_INTRA] += w;
461 }
462 }
463 }
464
465 // Above-left merge (libvpx decodemv.c:366): when the above-left
466 // pushed a THIRD distinct candidate, its count landed in
467 // cnt[CNT_SPLITMV] (cntx reached slot 3), and if that candidate
468 // equals the nearest candidate, NEAREST absorbs it. The split count
469 // is then overwritten with the SPLITMV-neighbour census below.
470 if cnt[CNT_SPLITMV] != 0 && mvs[idx] == mvs[CNT_NEAREST] {
471 cnt[CNT_NEAREST] += 1;
472 }
473 cnt[CNT_SPLITMV] = i32::from(above.is_split) * 2
474 + i32::from(left.is_split) * 2
475 + i32::from(aboveleft.is_split);
476
477 if cnt[CNT_NEAR] > cnt[CNT_NEAREST] {
478 cnt.swap(CNT_NEAR, CNT_NEAREST);
479 mvs.swap(CNT_NEAR, CNT_NEAREST);
480 }
481 (mvs, cnt)
482}
483
484/// Per-MB motion-vector clamp edges in eighth-pels (libvpx
485/// `mb_to_*_edge`): distance from the MB's edge to the frame edge.
486pub fn mb_edges(col: usize, row: usize, mb_cols: usize, mb_rows: usize) -> (i32, i32, i32, i32) {
487 (
488 -((i32::try_from(col).unwrap() * 16) << 3),
489 ((i32::try_from(mb_cols - 1 - col).unwrap() * 16) << 3),
490 -((i32::try_from(row).unwrap() * 16) << 3),
491 ((i32::try_from(mb_rows - 1 - row).unwrap() * 16) << 3),
492 )
493}
494
495/// Clamp a candidate MV into the current MB's allowed range plus a
496/// 128-eighth-pel margin (§16.2, libvpx `vp8_clamp_mv2`).
497pub fn clamp_mv2(mv: &mut (i16, i16), edges: (i32, i32, i32, i32)) {
498 let (l, r, t, b) = edges;
499 let c = i32::from(mv.1);
500 if c < l - 128 {
501 mv.1 = (l - 128) as i16;
502 } else if c > r + 128 {
503 mv.1 = (r + 128) as i16;
504 }
505 let ro = i32::from(mv.0);
506 if ro < t - 128 {
507 mv.0 = (t - 128) as i16;
508 } else if ro > b + 128 {
509 mv.0 = (b + 128) as i16;
510 }
511}
512
513/// Whether an explicit MV exceeds the MB's bounds (libvpx
514/// `vp8_check_mv_bounds`): strictly outside, no margin.
515pub fn mv_out_of_bounds(mv: (i16, i16), edges: (i32, i32, i32, i32)) -> bool {
516 let (l, r, t, b) = edges;
517 i32::from(mv.1) < l || i32::from(mv.1) > r || i32::from(mv.0) < t || i32::from(mv.0) > b
518}
519
520/// Chroma motion vector for a whole-MB mode (libvpx
521/// `vp8_build_inter16x16_predictors_mb`): halve each component with
522/// round-half-away-from-zero (`x += 1 | (x >> 31); x /= 2`).
523pub fn chroma_mv_whole(mv: (i16, i16), fullpel: bool) -> (i16, i16) {
524 fn half(x: i16, fullpel: bool) -> i16 {
525 let x = i32::from(x);
526 let v = ((x + (1 | (x >> 31))) / 2) as i16;
527 // `& fullpixel_mask` (reconinter.c:333-334): version-3 streams
528 // are full-pel, so the sub-pel bits are cleared after rounding.
529 if fullpel {
530 v & !7
531 } else {
532 v
533 }
534 }
535 (half(mv.0, fullpel), half(mv.1, fullpel))
536}
537
538/// Chroma motion vector for one 8x8 quadrant of a SPLITMB (libvpx
539/// `build_4x4uvmvs`): sum the quadrant's four subblock vectors, then
540/// divide by 8 with round-half-away-from-zero
541/// (`t += 4 + (t >> 31) * 8; t /= 8`).
542pub fn chroma_mv_split(quadrant: &[(i16, i16); 4], fullpel: bool) -> (i16, i16) {
543 fn eighth(parts: [i16; 4], fullpel: bool) -> i16 {
544 let t = i32::from(parts[0])
545 + i32::from(parts[1])
546 + i32::from(parts[2])
547 + i32::from(parts[3]);
548 let t = t + 4 + (t >> 31) * 8;
549 let v = (t / 8) as i16;
550 // `& fullpixel_mask` (reconinter.c:472-481).
551 if fullpel {
552 v & !7
553 } else {
554 v
555 }
556 }
557 (
558 eighth(
559 [quadrant[0].0, quadrant[1].0, quadrant[2].0, quadrant[3].0],
560 fullpel,
561 ),
562 eighth(
563 [quadrant[0].1, quadrant[1].1, quadrant[2].1, quadrant[3].1],
564 fullpel,
565 ),
566 )
567}