From 493fa90526e3028fc984a5b6a9b7e0d52f56644a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Dec 2026 15:13:59 +0300 Subject: [PATCH] S8: move chroma_mvs, ref_plane and compensate8/16 into mc.rs; sample.rs imports them from mc instead of decoder --- crates/ec-h264/src/decoder.rs | 466 +--------------------------------- crates/ec-h264/src/lib.rs | 1 + crates/ec-h264/src/mc.rs | 455 +++++++++++++++++++++++++++++++++ crates/ec-h264/src/sample.rs | 3 +- 4 files changed, 468 insertions(+), 457 deletions(-) create mode 100644 crates/ec-h264/src/mc.rs diff --git a/crates/ec-h264/src/decoder.rs b/crates/ec-h264/src/decoder.rs index fd4b0220..621264b6 100644 --- a/crates/ec-h264/src/decoder.rs +++ b/crates/ec-h264/src/decoder.rs @@ -38,7 +38,7 @@ use crate::entropy::{ BlockCat, Entropy, FLAG_CHROMA_PRED, FLAG_DECODED, FLAG_DIRECT, FLAG_I16, FLAG_INTER, FLAG_PCM, FLAG_SKIP, FLAG_TRANS8X8, MbCtx, MbInfo, MbaffA, }; -use crate::inter::{RefPlane, Weights, combine, mc_chroma, mc_luma}; +use crate::inter::Weights; use crate::mbaff::{self, MbPos}; use crate::mv::{ B_SHAPES, B_SUB, MbShape, MvCtx, P_SHAPES, P_SUB, Pred, SubShape, min_positive, neighbour_mvd, @@ -1575,7 +1575,7 @@ impl Geo { /// Picture row of chroma row `r` of the macroblock. #[inline] - fn cy(&self, r: usize) -> usize { + pub(crate) fn cy(&self, r: usize) -> usize { self.dst_cy + r * self.mul } } @@ -1590,7 +1590,7 @@ pub(crate) struct SliceCtx { /// Chroma rows per macroblock: 8 (4:2:0) or 16 (4:2:2). cmb_h: usize, /// ChromaArrayType of the picture being decoded. - cat: u8, + pub(crate) cat: u8, transform_8x8_mode: bool, constrained_intra_pred: bool, /// ctxIdxInc for the next macroblock's first mb_qp_delta bin: set when the @@ -1638,7 +1638,7 @@ impl SliceCtx { /// The reference picture `RefPicListX[idx]` points at: a frame, or one /// field of a stored frame. #[inline(always)] - fn reference<'a, S: Sample>( + pub(crate) fn reference<'a, S: Sample>( &self, refs: &'a [Picture], list: usize, @@ -1682,7 +1682,7 @@ impl SliceCtx { /// True while decoding a field picture or a field macroblock. #[inline] - fn is_field(&self) -> bool { + pub(crate) fn is_field(&self) -> bool { self.mb_parity != FRAME } @@ -1690,7 +1690,7 @@ impl SliceCtx { /// row of its first luma row (motion compensation reads fields at field /// row positions), the picture row it is written at, and the row step. #[inline] - fn geo(&self, mb_y: usize) -> Geo { + pub(crate) fn geo(&self, mb_y: usize) -> Geo { if self.pos.active && self.pos.field { Geo { src_y: (mb_y / 2) * 16, @@ -1712,7 +1712,7 @@ impl SliceCtx { /// a prediction from `rp`: fields of opposite parity sit half a field line /// apart, which is a quarter of a chroma sample. #[inline] - fn chroma_dy(&self, rp: &Picture) -> i16 { + pub(crate) fn chroma_dy(&self, rp: &Picture) -> i16 { if self.cat != 1 { // Tables 8-9 and 8-10 apply to ChromaArrayType 1 only. return 0; @@ -1728,12 +1728,12 @@ impl SliceCtx { /// component of every partition resolves to [`Weights::DEFAULT`] without /// the per-component lookup: neither weighted prediction (P) nor /// weighted/bi-prediction (B) is signalled. - fn no_weights(&self) -> bool { + pub(crate) fn no_weights(&self) -> bool { self.weighted_bipred_idc == 0 && !self.weighted_pred } /// Prediction weights of one component (clause 8.4.3). - fn weights_for( + pub(crate) fn weights_for( &self, refs: &[Picture], comp: usize, @@ -3425,453 +3425,6 @@ fn colocated( } } -/// The motion vectors chroma prediction uses: the luma vectors, with the -/// vertical offset of 8.4.1.4 added when a field predicts from a field of the -/// opposite parity. -#[inline] -fn chroma_mvs( - ctx: &SliceCtx, - refs: &[Picture], - ref_idx: [i8; 2], - mv: [[i16; 2]; 2], -) -> [[i16; 2]; 2] { - let mut out = mv; - if ctx.cat == 2 { - // 4:2:2 chroma is full height: the vertical vector stays in quarter - // samples of the chroma plane, which the eighth-sample interpolator - // reads as twice the value. - for l in out.iter_mut() { - l[1] = l[1].wrapping_mul(2); - } - } else if ctx.is_field() { - for l in 0..2 { - if let Some(rp) = ctx.reference(refs, l, ref_idx[l]) { - out[l][1] = out[l][1].wrapping_add(ctx.chroma_dy(rp)); - } - } - } - out -} - -/// The reference plane (`0` luma, `1` Cb, `2` Cr) list `list` predicts from. -#[inline(always)] -fn ref_plane<'a>( - ctx: &SliceCtx, - refs: &'a [Picture], - ref_idx: [i8; 2], - list: usize, - comp: usize, -) -> Result> { - let rp = ctx.reference(refs, list, ref_idx[list]).ok_or_else(|| { - Error::corrupt("reference index names a picture the buffer does not hold") - })?; - let src = match comp { - 0 => &rp.y, - 1 => &rp.cb, - _ => &rp.cr, - }; - Ok(RefPlane { - data: &src.data, - stride: src.stride, - origin: src.origin, - width: src.width, - height: src.height, - pad: src.pad, - }) -} - -/// Motion compensation of one partition (clause 8.4.2): interpolate each list's -/// prediction, then combine them by the weighting of 8.4.2.3 and write the -/// result straight into the picture, where the residual is added on top. -/// -/// `(px, py)` and `(pw, ph)` are in 4x4 blocks within the macroblock. -#[allow(clippy::too_many_arguments)] -pub(crate) fn compensate8( - pic: &mut Picture, - refs: &[Picture], - ctx: &SliceCtx, - mb_x: usize, - mb_y: usize, - px: usize, - py: usize, - pw: usize, - ph: usize, - mv: [[i16; 2]; 2], - ref_idx: [i8; 2], -) -> Result<()> { - let use0 = ref_idx[0] >= 0; - let use1 = ref_idx[1] >= 0; - if !use0 && !use1 { - return Ok(()); - } - let geo = ctx.geo(mb_y); - let x0 = (mb_x * 16 + px * 4) as i32; - let y0 = (geo.src_y + py * 4) as i32; - let (w, h) = (pw * 4, ph * 4); - // A stream carrying no weighted prediction at all resolves every - // component to the identity weighting without the per-component lookup. - let plain = ctx.no_weights(); - // ChromaArrayType 3 predicts Cb and Cr with the luma interpolation, on - // their own planes and weights. - let ncomp = if ctx.cat == 3 { 3 } else { 1 }; - for comp in 0..ncomp { - let wt = if plain { - Weights::DEFAULT - } else { - ctx.weights_for(refs, comp, ref_idx[0], ref_idx[1], use0, use1) - }; - // 8.4.2.3.1 over one list is the identity, so the interpolation of that - // one list IS the prediction: it is written straight into the picture at - // the picture's own pitch. Over both lists the combined prediction is the - // rounded byte average of the two clipped ones, so list 0 interpolates - // into the picture and list 1 averages on top of it (Put::Avg) — no - // temporaries, no combining pass, no copy back. - let direct = wt.is_default(); - let stride = plane_ref(pic, comp).stride * geo.mul; - let origin = plane_ref(pic, comp).at(x0 as usize, geo.y(py * 4)); - // Weighted bi-prediction fuses the same way: the combining formula is - // per-sample, so list 0 lands in the picture and list 1 combines on top - // of it (Put::AvgW) with 8.4.2.3.2's exact integers. - let weighted_put = if !direct && use0 && use1 { - Some(crate::inter::Put::AvgW { - w0: wt.w[0], - w1: wt.w[1], - shift: (wt.log_wd + 1) as u32, - round: 1 << wt.log_wd, - off: (wt.o[0] + wt.o[1] + 1) >> 1, - }) - } else { - None - }; - if direct && use0 && use1 { - let plane = ref_plane(ctx, refs, ref_idx, 0, comp)?; - mc_luma( - &plane, - x0, - y0, - mv[0], - w, - h, - stride, - &mut plane_mut(pic, comp).data[origin..], - crate::inter::Put::Set, - ); - let plane = ref_plane(ctx, refs, ref_idx, 1, comp)?; - mc_luma( - &plane, - x0, - y0, - mv[1], - w, - h, - stride, - &mut plane_mut(pic, comp).data[origin..], - crate::inter::Put::Avg, - ); - } else if direct { - // No scratch buffer is declared on this path at all: the scratch is - // zeroed on entry, and most partitions of a P slice come through here. - let list = usize::from(!use0); - let plane = ref_plane(ctx, refs, ref_idx, list, comp)?; - mc_luma( - &plane, - x0, - y0, - mv[list], - w, - h, - stride, - &mut plane_mut(pic, comp).data[origin..], - crate::inter::Put::Set, - ); - } else if let Some(put) = weighted_put { - let plane = ref_plane(ctx, refs, ref_idx, 0, comp)?; - mc_luma( - &plane, - x0, - y0, - mv[0], - w, - h, - stride, - &mut plane_mut(pic, comp).data[origin..], - crate::inter::Put::Set, - ); - let plane = ref_plane(ctx, refs, ref_idx, 1, comp)?; - mc_luma( - &plane, - x0, - y0, - mv[1], - w, - h, - stride, - &mut plane_mut(pic, comp).data[origin..], - put, - ); - } else { - let mut part = [[0u8; 256]; 2]; - for list in 0..2 { - if ref_idx[list] < 0 { - continue; - } - let plane = ref_plane(ctx, refs, ref_idx, list, comp)?; - mc_luma( - &plane, - x0, - y0, - mv[list], - w, - h, - w, - &mut part[list], - crate::inter::Put::Set, - ); - } - let mut out = [0u8; 256]; - let (p0, p1) = part.split_at(1); - combine(&mut out, &p0[0], &p1[0], use0, use1, &wt, w * h); - for row in 0..h { - let dst = origin + row * stride; - plane_mut(pic, comp).data[dst..dst + w].copy_from_slice(&out[row * w..row * w + w]); - } - } - } - - // Chroma (8.4.1.4): for 4:2:0 frame coding the chroma vector is the luma - // vector, read as eighth-sample units over the half-resolution plane. - if ctx.cat == 0 || ctx.cat == 3 { - return Ok(()); - } - let cmv = chroma_mvs(ctx, refs, ref_idx, mv); - let c422 = ctx.cat == 2; - let (cw, ch) = (w / 2, if c422 { h } else { h / 2 }); - let (cx0, cy0) = (x0 / 2, if c422 { y0 } else { y0 / 2 }); - let crow = if c422 { py * 4 } else { py * 2 }; - for comp in 0..2 { - let wt = if plain { - Weights::DEFAULT - } else { - ctx.weights_for(refs, comp + 1, ref_idx[0], ref_idx[1], use0, use1) - }; - let direct = wt.is_default(); - // The combining weights are per-component syntax (luma and chroma - // carry their own denoms, weights and offsets), so this component's - // fused mode is built from ITS OWN lookup — never the luma one. - let weighted_put = if !direct && use0 && use1 { - Some(crate::inter::Put::AvgW { - w0: wt.w[0], - w1: wt.w[1], - shift: (wt.log_wd + 1) as u32, - round: 1 << wt.log_wd, - off: (wt.o[0] + wt.o[1] + 1) >> 1, - }) - } else { - None - }; - let plane_out = if comp == 0 { &mut pic.cb } else { &mut pic.cr }; - let stride = plane_out.stride * geo.mul; - let origin = plane_out.at(cx0 as usize, geo.cy(crow)); - if direct && use0 && use1 { - let plane = ref_plane(ctx, refs, ref_idx, 0, 1 + comp)?; - mc_chroma( - &plane, - cx0, - cy0, - cmv[0], - cw, - ch, - stride, - &mut plane_out.data[origin..], - crate::inter::Put::Set, - ); - let plane = ref_plane(ctx, refs, ref_idx, 1, 1 + comp)?; - mc_chroma( - &plane, - cx0, - cy0, - cmv[1], - cw, - ch, - stride, - &mut plane_out.data[origin..], - crate::inter::Put::Avg, - ); - } else if let Some(put) = weighted_put { - let plane = ref_plane(ctx, refs, ref_idx, 0, 1 + comp)?; - mc_chroma( - &plane, - cx0, - cy0, - cmv[0], - cw, - ch, - stride, - &mut plane_out.data[origin..], - crate::inter::Put::Set, - ); - let plane = ref_plane(ctx, refs, ref_idx, 1, 1 + comp)?; - mc_chroma( - &plane, - cx0, - cy0, - cmv[1], - cw, - ch, - stride, - &mut plane_out.data[origin..], - put, - ); - } else if direct { - let list = usize::from(!use0); - let plane = ref_plane(ctx, refs, ref_idx, list, 1 + comp)?; - mc_chroma( - &plane, - cx0, - cy0, - cmv[list], - cw, - ch, - stride, - &mut plane_out.data[origin..], - crate::inter::Put::Set, - ); - } else { - let mut cpart = [[0u8; 128]; 2]; - for list in 0..2 { - if ref_idx[list] < 0 { - continue; - } - let plane = ref_plane(ctx, refs, ref_idx, list, 1 + comp)?; - mc_chroma( - &plane, - cx0, - cy0, - cmv[list], - cw, - ch, - cw, - &mut cpart[list], - crate::inter::Put::Set, - ); - } - let mut cout = [0u8; 128]; - let (c0, c1) = cpart.split_at(1); - combine(&mut cout, &c0[0], &c1[0], use0, use1, &wt, cw * ch); - for row in 0..ch { - let dst = origin + row * stride; - plane_out.data[dst..dst + cw].copy_from_slice(&cout[row * cw..row * cw + cw]); - } - } - } - Ok(()) -} - -/// Motion compensation of one partition over `u16` planes: the same clause -/// 8.4.2 process as [`compensate8`], computed per sample from the equations. -/// Weighted prediction offsets scale by `1 << (BitDepth - 8)` (8-298, 8-299). -#[allow(clippy::too_many_arguments)] -pub(crate) fn compensate16( - pic: &mut Picture, - refs: &[Picture], - ctx: &SliceCtx, - mb_x: usize, - mb_y: usize, - px: usize, - py: usize, - pw: usize, - ph: usize, - mv: [[i16; 2]; 2], - ref_idx: [i8; 2], -) -> Result<()> { - let use0 = ref_idx[0] >= 0; - let use1 = ref_idx[1] >= 0; - if !use0 && !use1 { - return Ok(()); - } - let bd = pic.bd; - let max = (1i32 << bd) - 1; - let geo = ctx.geo(mb_y); - let x0 = (mb_x * 16 + px * 4) as i32; - let y0 = (geo.src_y + py * 4) as i32; - let plain = ctx.no_weights(); - let cmv = chroma_mvs(ctx, refs, ref_idx, mv); - let cat = pic.cat; - let ncomp = if cat == 0 { 1 } else { 3 }; - for comp in 0..ncomp { - let c422 = cat == 2; - let (w, h, cx, cy) = if comp == 0 || cat == 3 { - (pw * 4, ph * 4, x0, y0) - } else if c422 { - (pw * 2, ph * 4, x0 / 2, y0) - } else { - (pw * 2, ph * 2, x0 / 2, y0 / 2) - }; - let mut wt = if plain { - Weights::DEFAULT - } else { - ctx.weights_for(refs, comp, ref_idx[0], ref_idx[1], use0, use1) - }; - wt.o[0] <<= bd - 8; - wt.o[1] <<= bd - 8; - let mut pred = [[0i32; 256]; 2]; - for list in 0..2 { - if ref_idx[list] < 0 { - continue; - } - let rp = ctx.reference(refs, list, ref_idx[list]).ok_or_else(|| { - Error::corrupt("reference index names a picture the buffer does not hold") - })?; - match comp { - 0 => crate::inter16::luma_pred(&rp.y, cx, cy, mv[list], w, h, max, &mut pred[list]), - 1 if cat == 3 => { - crate::inter16::luma_pred(&rp.cb, cx, cy, mv[list], w, h, max, &mut pred[list]) - } - _ if cat == 3 => { - crate::inter16::luma_pred(&rp.cr, cx, cy, mv[list], w, h, max, &mut pred[list]) - } - 1 => crate::inter16::chroma_pred(&rp.cb, cx, cy, cmv[list], w, h, &mut pred[list]), - _ => crate::inter16::chroma_pred(&rp.cr, cx, cy, cmv[list], w, h, &mut pred[list]), - } - } - let plane = match comp { - 0 => &mut pic.y, - 1 => &mut pic.cb, - _ => &mut pic.cr, - }; - let stride = plane.stride * geo.mul; - let origin = if comp == 0 || cat == 3 { - plane.at(cx as usize, geo.y(py * 4)) - } else { - plane.at(cx as usize, geo.cy(if c422 { py * 4 } else { py * 2 })) - }; - let round1 = if wt.log_wd >= 1 { - 1 << (wt.log_wd - 1) - } else { - 0 - }; - for row in 0..h { - for col in 0..w { - let i = row * w + col; - let v = if use0 && use1 { - // 8-301 / 8-300 (default weights reduce to the rounded mean). - ((pred[0][i] * wt.w[0] + pred[1][i] * wt.w[1] + (1 << wt.log_wd)) - >> (wt.log_wd + 1)) - + ((wt.o[0] + wt.o[1] + 1) >> 1) - } else { - let k = usize::from(!use0); - if wt.log_wd >= 1 { - ((pred[k][i] * wt.w[k] + round1) >> wt.log_wd) + wt.o[k] - } else { - pred[k][i] * wt.w[k] + wt.o[k] - } - }; - plane.data[origin + row * stride + col] = v.clamp(0, max) as u16; - } - } - } - Ok(()) -} /// Gather the 13 neighbour samples of a luma 4x4 block (spec 8.3.1.2), /// including the top-right substitution rule. @@ -4017,6 +3570,7 @@ pub(crate) fn gather_nbr8_in( mod tests { use super::*; use crate::dpb::RefList; + use crate::mc::compensate8; use ec_h264_syntax::{PredWeightTable, WeightEntry}; /// A picture whose visible samples are a fixed ramp, borders replicated. diff --git a/crates/ec-h264/src/lib.rs b/crates/ec-h264/src/lib.rs index a133f147..a8617d59 100644 --- a/crates/ec-h264/src/lib.rs +++ b/crates/ec-h264/src/lib.rs @@ -51,6 +51,7 @@ mod fmo; mod inter; mod inter16; mod mbaff; +mod mc; mod mv; mod pred; mod residual; diff --git a/crates/ec-h264/src/mc.rs b/crates/ec-h264/src/mc.rs new file mode 100644 index 00000000..dc9fa210 --- /dev/null +++ b/crates/ec-h264/src/mc.rs @@ -0,0 +1,455 @@ +use ec_core::error::{Error, Result}; + +use crate::decoder::{SliceCtx, plane_mut, plane_ref}; +use crate::dpb::Picture; +use crate::inter::{RefPlane, Weights, combine, mc_chroma, mc_luma}; +use crate::sample::Sample; + + +/// The motion vectors chroma prediction uses: the luma vectors, with the +/// vertical offset of 8.4.1.4 added when a field predicts from a field of the +/// opposite parity. +#[inline] +fn chroma_mvs( + ctx: &SliceCtx, + refs: &[Picture], + ref_idx: [i8; 2], + mv: [[i16; 2]; 2], +) -> [[i16; 2]; 2] { + let mut out = mv; + if ctx.cat == 2 { + // 4:2:2 chroma is full height: the vertical vector stays in quarter + // samples of the chroma plane, which the eighth-sample interpolator + // reads as twice the value. + for l in out.iter_mut() { + l[1] = l[1].wrapping_mul(2); + } + } else if ctx.is_field() { + for l in 0..2 { + if let Some(rp) = ctx.reference(refs, l, ref_idx[l]) { + out[l][1] = out[l][1].wrapping_add(ctx.chroma_dy(rp)); + } + } + } + out +} + +/// The reference plane (`0` luma, `1` Cb, `2` Cr) list `list` predicts from. +#[inline(always)] +fn ref_plane<'a>( + ctx: &SliceCtx, + refs: &'a [Picture], + ref_idx: [i8; 2], + list: usize, + comp: usize, +) -> Result> { + let rp = ctx.reference(refs, list, ref_idx[list]).ok_or_else(|| { + Error::corrupt("reference index names a picture the buffer does not hold") + })?; + let src = match comp { + 0 => &rp.y, + 1 => &rp.cb, + _ => &rp.cr, + }; + Ok(RefPlane { + data: &src.data, + stride: src.stride, + origin: src.origin, + width: src.width, + height: src.height, + pad: src.pad, + }) +} + +/// Motion compensation of one partition (clause 8.4.2): interpolate each list's +/// prediction, then combine them by the weighting of 8.4.2.3 and write the +/// result straight into the picture, where the residual is added on top. +/// +/// `(px, py)` and `(pw, ph)` are in 4x4 blocks within the macroblock. +#[allow(clippy::too_many_arguments)] +pub(crate) fn compensate8( + pic: &mut Picture, + refs: &[Picture], + ctx: &SliceCtx, + mb_x: usize, + mb_y: usize, + px: usize, + py: usize, + pw: usize, + ph: usize, + mv: [[i16; 2]; 2], + ref_idx: [i8; 2], +) -> Result<()> { + let use0 = ref_idx[0] >= 0; + let use1 = ref_idx[1] >= 0; + if !use0 && !use1 { + return Ok(()); + } + let geo = ctx.geo(mb_y); + let x0 = (mb_x * 16 + px * 4) as i32; + let y0 = (geo.src_y + py * 4) as i32; + let (w, h) = (pw * 4, ph * 4); + // A stream carrying no weighted prediction at all resolves every + // component to the identity weighting without the per-component lookup. + let plain = ctx.no_weights(); + // ChromaArrayType 3 predicts Cb and Cr with the luma interpolation, on + // their own planes and weights. + let ncomp = if ctx.cat == 3 { 3 } else { 1 }; + for comp in 0..ncomp { + let wt = if plain { + Weights::DEFAULT + } else { + ctx.weights_for(refs, comp, ref_idx[0], ref_idx[1], use0, use1) + }; + // 8.4.2.3.1 over one list is the identity, so the interpolation of that + // one list IS the prediction: it is written straight into the picture at + // the picture's own pitch. Over both lists the combined prediction is the + // rounded byte average of the two clipped ones, so list 0 interpolates + // into the picture and list 1 averages on top of it (Put::Avg) — no + // temporaries, no combining pass, no copy back. + let direct = wt.is_default(); + let stride = plane_ref(pic, comp).stride * geo.mul; + let origin = plane_ref(pic, comp).at(x0 as usize, geo.y(py * 4)); + // Weighted bi-prediction fuses the same way: the combining formula is + // per-sample, so list 0 lands in the picture and list 1 combines on top + // of it (Put::AvgW) with 8.4.2.3.2's exact integers. + let weighted_put = if !direct && use0 && use1 { + Some(crate::inter::Put::AvgW { + w0: wt.w[0], + w1: wt.w[1], + shift: (wt.log_wd + 1) as u32, + round: 1 << wt.log_wd, + off: (wt.o[0] + wt.o[1] + 1) >> 1, + }) + } else { + None + }; + if direct && use0 && use1 { + let plane = ref_plane(ctx, refs, ref_idx, 0, comp)?; + mc_luma( + &plane, + x0, + y0, + mv[0], + w, + h, + stride, + &mut plane_mut(pic, comp).data[origin..], + crate::inter::Put::Set, + ); + let plane = ref_plane(ctx, refs, ref_idx, 1, comp)?; + mc_luma( + &plane, + x0, + y0, + mv[1], + w, + h, + stride, + &mut plane_mut(pic, comp).data[origin..], + crate::inter::Put::Avg, + ); + } else if direct { + // No scratch buffer is declared on this path at all: the scratch is + // zeroed on entry, and most partitions of a P slice come through here. + let list = usize::from(!use0); + let plane = ref_plane(ctx, refs, ref_idx, list, comp)?; + mc_luma( + &plane, + x0, + y0, + mv[list], + w, + h, + stride, + &mut plane_mut(pic, comp).data[origin..], + crate::inter::Put::Set, + ); + } else if let Some(put) = weighted_put { + let plane = ref_plane(ctx, refs, ref_idx, 0, comp)?; + mc_luma( + &plane, + x0, + y0, + mv[0], + w, + h, + stride, + &mut plane_mut(pic, comp).data[origin..], + crate::inter::Put::Set, + ); + let plane = ref_plane(ctx, refs, ref_idx, 1, comp)?; + mc_luma( + &plane, + x0, + y0, + mv[1], + w, + h, + stride, + &mut plane_mut(pic, comp).data[origin..], + put, + ); + } else { + let mut part = [[0u8; 256]; 2]; + for list in 0..2 { + if ref_idx[list] < 0 { + continue; + } + let plane = ref_plane(ctx, refs, ref_idx, list, comp)?; + mc_luma( + &plane, + x0, + y0, + mv[list], + w, + h, + w, + &mut part[list], + crate::inter::Put::Set, + ); + } + let mut out = [0u8; 256]; + let (p0, p1) = part.split_at(1); + combine(&mut out, &p0[0], &p1[0], use0, use1, &wt, w * h); + for row in 0..h { + let dst = origin + row * stride; + plane_mut(pic, comp).data[dst..dst + w].copy_from_slice(&out[row * w..row * w + w]); + } + } + } + + // Chroma (8.4.1.4): for 4:2:0 frame coding the chroma vector is the luma + // vector, read as eighth-sample units over the half-resolution plane. + if ctx.cat == 0 || ctx.cat == 3 { + return Ok(()); + } + let cmv = chroma_mvs(ctx, refs, ref_idx, mv); + let c422 = ctx.cat == 2; + let (cw, ch) = (w / 2, if c422 { h } else { h / 2 }); + let (cx0, cy0) = (x0 / 2, if c422 { y0 } else { y0 / 2 }); + let crow = if c422 { py * 4 } else { py * 2 }; + for comp in 0..2 { + let wt = if plain { + Weights::DEFAULT + } else { + ctx.weights_for(refs, comp + 1, ref_idx[0], ref_idx[1], use0, use1) + }; + let direct = wt.is_default(); + // The combining weights are per-component syntax (luma and chroma + // carry their own denoms, weights and offsets), so this component's + // fused mode is built from ITS OWN lookup — never the luma one. + let weighted_put = if !direct && use0 && use1 { + Some(crate::inter::Put::AvgW { + w0: wt.w[0], + w1: wt.w[1], + shift: (wt.log_wd + 1) as u32, + round: 1 << wt.log_wd, + off: (wt.o[0] + wt.o[1] + 1) >> 1, + }) + } else { + None + }; + let plane_out = if comp == 0 { &mut pic.cb } else { &mut pic.cr }; + let stride = plane_out.stride * geo.mul; + let origin = plane_out.at(cx0 as usize, geo.cy(crow)); + if direct && use0 && use1 { + let plane = ref_plane(ctx, refs, ref_idx, 0, 1 + comp)?; + mc_chroma( + &plane, + cx0, + cy0, + cmv[0], + cw, + ch, + stride, + &mut plane_out.data[origin..], + crate::inter::Put::Set, + ); + let plane = ref_plane(ctx, refs, ref_idx, 1, 1 + comp)?; + mc_chroma( + &plane, + cx0, + cy0, + cmv[1], + cw, + ch, + stride, + &mut plane_out.data[origin..], + crate::inter::Put::Avg, + ); + } else if let Some(put) = weighted_put { + let plane = ref_plane(ctx, refs, ref_idx, 0, 1 + comp)?; + mc_chroma( + &plane, + cx0, + cy0, + cmv[0], + cw, + ch, + stride, + &mut plane_out.data[origin..], + crate::inter::Put::Set, + ); + let plane = ref_plane(ctx, refs, ref_idx, 1, 1 + comp)?; + mc_chroma( + &plane, + cx0, + cy0, + cmv[1], + cw, + ch, + stride, + &mut plane_out.data[origin..], + put, + ); + } else if direct { + let list = usize::from(!use0); + let plane = ref_plane(ctx, refs, ref_idx, list, 1 + comp)?; + mc_chroma( + &plane, + cx0, + cy0, + cmv[list], + cw, + ch, + stride, + &mut plane_out.data[origin..], + crate::inter::Put::Set, + ); + } else { + let mut cpart = [[0u8; 128]; 2]; + for list in 0..2 { + if ref_idx[list] < 0 { + continue; + } + let plane = ref_plane(ctx, refs, ref_idx, list, 1 + comp)?; + mc_chroma( + &plane, + cx0, + cy0, + cmv[list], + cw, + ch, + cw, + &mut cpart[list], + crate::inter::Put::Set, + ); + } + let mut cout = [0u8; 128]; + let (c0, c1) = cpart.split_at(1); + combine(&mut cout, &c0[0], &c1[0], use0, use1, &wt, cw * ch); + for row in 0..ch { + let dst = origin + row * stride; + plane_out.data[dst..dst + cw].copy_from_slice(&cout[row * cw..row * cw + cw]); + } + } + } + Ok(()) +} + +/// Motion compensation of one partition over `u16` planes: the same clause +/// 8.4.2 process as [`compensate8`], computed per sample from the equations. +/// Weighted prediction offsets scale by `1 << (BitDepth - 8)` (8-298, 8-299). +#[allow(clippy::too_many_arguments)] +pub(crate) fn compensate16( + pic: &mut Picture, + refs: &[Picture], + ctx: &SliceCtx, + mb_x: usize, + mb_y: usize, + px: usize, + py: usize, + pw: usize, + ph: usize, + mv: [[i16; 2]; 2], + ref_idx: [i8; 2], +) -> Result<()> { + let use0 = ref_idx[0] >= 0; + let use1 = ref_idx[1] >= 0; + if !use0 && !use1 { + return Ok(()); + } + let bd = pic.bd; + let max = (1i32 << bd) - 1; + let geo = ctx.geo(mb_y); + let x0 = (mb_x * 16 + px * 4) as i32; + let y0 = (geo.src_y + py * 4) as i32; + let plain = ctx.no_weights(); + let cmv = chroma_mvs(ctx, refs, ref_idx, mv); + let cat = pic.cat; + let ncomp = if cat == 0 { 1 } else { 3 }; + for comp in 0..ncomp { + let c422 = cat == 2; + let (w, h, cx, cy) = if comp == 0 || cat == 3 { + (pw * 4, ph * 4, x0, y0) + } else if c422 { + (pw * 2, ph * 4, x0 / 2, y0) + } else { + (pw * 2, ph * 2, x0 / 2, y0 / 2) + }; + let mut wt = if plain { + Weights::DEFAULT + } else { + ctx.weights_for(refs, comp, ref_idx[0], ref_idx[1], use0, use1) + }; + wt.o[0] <<= bd - 8; + wt.o[1] <<= bd - 8; + let mut pred = [[0i32; 256]; 2]; + for list in 0..2 { + if ref_idx[list] < 0 { + continue; + } + let rp = ctx.reference(refs, list, ref_idx[list]).ok_or_else(|| { + Error::corrupt("reference index names a picture the buffer does not hold") + })?; + match comp { + 0 => crate::inter16::luma_pred(&rp.y, cx, cy, mv[list], w, h, max, &mut pred[list]), + 1 if cat == 3 => { + crate::inter16::luma_pred(&rp.cb, cx, cy, mv[list], w, h, max, &mut pred[list]) + } + _ if cat == 3 => { + crate::inter16::luma_pred(&rp.cr, cx, cy, mv[list], w, h, max, &mut pred[list]) + } + 1 => crate::inter16::chroma_pred(&rp.cb, cx, cy, cmv[list], w, h, &mut pred[list]), + _ => crate::inter16::chroma_pred(&rp.cr, cx, cy, cmv[list], w, h, &mut pred[list]), + } + } + let plane = match comp { + 0 => &mut pic.y, + 1 => &mut pic.cb, + _ => &mut pic.cr, + }; + let stride = plane.stride * geo.mul; + let origin = if comp == 0 || cat == 3 { + plane.at(cx as usize, geo.y(py * 4)) + } else { + plane.at(cx as usize, geo.cy(if c422 { py * 4 } else { py * 2 })) + }; + let round1 = if wt.log_wd >= 1 { + 1 << (wt.log_wd - 1) + } else { + 0 + }; + for row in 0..h { + for col in 0..w { + let i = row * w + col; + let v = if use0 && use1 { + // 8-301 / 8-300 (default weights reduce to the rounded mean). + ((pred[0][i] * wt.w[0] + pred[1][i] * wt.w[1] + (1 << wt.log_wd)) + >> (wt.log_wd + 1)) + + ((wt.o[0] + wt.o[1] + 1) >> 1) + } else { + let k = usize::from(!use0); + if wt.log_wd >= 1 { + ((pred[k][i] * wt.w[k] + round1) >> wt.log_wd) + wt.o[k] + } else { + pred[k][i] * wt.w[k] + wt.o[k] + } + }; + plane.data[origin + row * stride + col] = v.clamp(0, max) as u16; + } + } + } + Ok(()) +} diff --git a/crates/ec-h264/src/sample.rs b/crates/ec-h264/src/sample.rs index 06ccf5f3..75850e96 100644 --- a/crates/ec-h264/src/sample.rs +++ b/crates/ec-h264/src/sample.rs @@ -6,7 +6,8 @@ use crate::deblock::{ EdgeParams, filter_chroma_h_edge8, filter_chroma_line, filter_luma_h_edge16, filter_luma_line, }; -use crate::decoder::{SliceCtx, compensate8, compensate16}; +use crate::decoder::SliceCtx; +use crate::mc::{compensate8, compensate16}; use crate::dpb::Picture; use ec_core::error::Result;