use super::cu_syntax::{PartMode, PredictionUnit}; use super::motion_field::{PbMotion, SliceMotion, to16}; const COMB_L0: [usize; 12] = [0, 1, 0, 2, 1, 2, 0, 3, 1, 3, 2, 3]; const COMB_L1: [usize; 12] = [1, 0, 2, 0, 2, 1, 3, 0, 3, 1, 3, 2]; pub fn derive_merge( src: &SliceMotion<'_>, cu_x: usize, cu_y: usize, cu_size: usize, part_mode: PartMode, pu_index: usize, pu: &PredictionUnit, ) -> PbMotion { let params = src.params; let single_list = params.log2_par_mrg_level > 2 && cu_size == 8; let (x, y, w, h, part_idx) = if single_list { (cu_x, cu_y, cu_size, cu_size, 0) } else { (pu.x, pu.y, pu.width, pu.height, pu_index) }; let mut cands = spatial_candidates(src, part_mode, x, y, w, h, part_idx); if let Some(col) = temporal_candidate(src, x, y, w, h) { cands.push(col); } let num_orig = cands.len(); let max = params.max_merge_cand; if params.is_b && num_orig > 1 && num_orig < max { combined_candidates(src, &mut cands, num_orig, max); } let num_ref_idx = if params.is_b { src.lists.l0.len().min(src.lists.l1.len()) } else { src.lists.l0.len() }; let mut zero_idx = 0; while cands.len() < max { let ref_idx = if zero_idx < num_ref_idx { zero_idx } else { 0 }; cands.push(zero_candidate(params.is_b, ref_idx)); zero_idx += 1; } let mut chosen = cands .get(usize::from(pu.merge_idx)) .copied() .unwrap_or_else(|| zero_candidate(params.is_b, 0)); if chosen.pred[0] && chosen.pred[1] && pu.width + pu.height == 12 { chosen = PbMotion::new( [true, false], [chosen.ref_idx[0], -1], [chosen.mv[0], [0, 0]], ); } chosen } fn spatial_candidates( src: &SliceMotion<'_>, part: PartMode, x: usize, y: usize, w: usize, h: usize, part_idx: usize, ) -> Vec { let (xp, yp) = (x as isize, y as isize); let (wi, hi) = (w as isize, h as isize); let level = src.params.log2_par_mrg_level; let fetch = |xn: isize, yn: isize| -> Option { if xp >> level == xn >> level && yp >> level == yn >> level { return None; } src.neighbour(xn, yn) }; let second_vertical = part_idx == 1 && matches!( part, PartMode::PartNx2N | PartMode::PartnLx2N | PartMode::PartnRx2N ); let second_horizontal = part_idx == 1 && matches!( part, PartMode::Part2NxN | PartMode::Part2NxnU | PartMode::Part2NxnD ); let a1 = if second_vertical { None } else { fetch(xp - 1, yp + hi - 1) }; let b1 = if second_horizontal { None } else { fetch(xp + wi - 1, yp - 1) }; let b0 = fetch(xp + wi, yp - 1); let a0 = fetch(xp - 1, yp + hi); let b2 = fetch(xp - 1, yp - 1); let mut out = Vec::with_capacity(5); out.extend(a1); out.extend(b1.filter(|m| a1 != Some(*m))); out.extend(b0.filter(|m| b1 != Some(*m))); out.extend(a0.filter(|m| a1 != Some(*m))); if out.len() != 4 { out.extend(b2.filter(|m| a1 != Some(*m) && b1 != Some(*m))); } out } fn temporal_candidate( src: &SliceMotion<'_>, x: usize, y: usize, w: usize, h: usize, ) -> Option { let mv0 = src.temporal_mv(x, y, w, h, 0, 0); let mv1 = if src.params.is_b { src.temporal_mv(x, y, w, h, 1, 0) } else { None }; if mv0.is_none() && mv1.is_none() { return None; } let conv = |mv: Option<[i32; 2]>| mv.map_or([0i16, 0], to16); Some(PbMotion::new( [mv0.is_some(), mv1.is_some()], [0, 0], [conv(mv0), conv(mv1)], )) } fn combined_candidates( src: &SliceMotion<'_>, cands: &mut Vec, num_orig: usize, max: usize, ) { let total = num_orig * (num_orig - 1); let mut comb = 0; while comb < total && comb < COMB_L0.len() && cands.len() < max { let (Some(&l0c), Some(&l1c)) = (cands.get(COMB_L0[comb]), cands.get(COMB_L1[comb])) else { break; }; if l0c.pred[0] && l1c.pred[1] && (src.ref_poc(0, l0c.ref_idx[0]) != src.ref_poc(1, l1c.ref_idx[1]) || l0c.mv[0] != l1c.mv[1]) { cands.push(PbMotion::new( [true, true], [l0c.ref_idx[0], l1c.ref_idx[1]], [l0c.mv[0], l1c.mv[1]], )); } comb += 1; } } fn zero_candidate(is_b: bool, ref_idx: usize) -> PbMotion { let r = i8::try_from(ref_idx).unwrap_or(0); if is_b { PbMotion::new([true, true], [r, r], [[0, 0], [0, 0]]) } else { PbMotion::new([true, false], [r, -1], [[0, 0], [0, 0]]) } }