forge
Explore
Log in

Don't you dare go Hollow

S8: move chroma_mvs, ref_plane and compensate8/16 into mc.rs; sample.rs imports them from mc instead of decoder

Browse files at this commit

493fa90526e3028fc984a5b6a9b7e0d52f56644aAhmet Kaan GümüşParent: 87aa56b
crates/ec-h264/src/decoder.rsmodified+10 -456
@@ -38,7 +38,7 @@ use crate::entropy::{
38 BlockCat, Entropy, FLAG_CHROMA_PRED, FLAG_DECODED, FLAG_DIRECT, FLAG_I16, FLAG_INTER, FLAG_PCM,38 BlockCat, Entropy, FLAG_CHROMA_PRED, FLAG_DECODED, FLAG_DIRECT, FLAG_I16, FLAG_INTER, FLAG_PCM,
39 FLAG_SKIP, FLAG_TRANS8X8, MbCtx, MbInfo, MbaffA,39 FLAG_SKIP, FLAG_TRANS8X8, MbCtx, MbInfo, MbaffA,
40 };40 };
41-use crate::inter::{RefPlane, Weights, combine, mc_chroma, mc_luma};41+use crate::inter::Weights;
42 use crate::mbaff::{self, MbPos};42 use crate::mbaff::{self, MbPos};
43 use crate::mv::{43 use crate::mv::{
44 B_SHAPES, B_SUB, MbShape, MvCtx, P_SHAPES, P_SUB, Pred, SubShape, min_positive, neighbour_mvd,44 B_SHAPES, B_SUB, MbShape, MvCtx, P_SHAPES, P_SUB, Pred, SubShape, min_positive, neighbour_mvd,
@@ -1575,7 +1575,7 @@ impl Geo {
1575 1575
1576 /// Picture row of chroma row `r` of the macroblock.1576 /// Picture row of chroma row `r` of the macroblock.
1577 #[inline]1577 #[inline]
1578- fn cy(&self, r: usize) -> usize {1578+ pub(crate) fn cy(&self, r: usize) -> usize {
1579 self.dst_cy + r * self.mul1579 self.dst_cy + r * self.mul
1580 }1580 }
1581 }1581 }
@@ -1590,7 +1590,7 @@ pub(crate) struct SliceCtx {
1590 /// Chroma rows per macroblock: 8 (4:2:0) or 16 (4:2:2).1590 /// Chroma rows per macroblock: 8 (4:2:0) or 16 (4:2:2).
1591 cmb_h: usize,1591 cmb_h: usize,
1592 /// ChromaArrayType of the picture being decoded.1592 /// ChromaArrayType of the picture being decoded.
1593- cat: u8,1593+ pub(crate) cat: u8,
1594 transform_8x8_mode: bool,1594 transform_8x8_mode: bool,
1595 constrained_intra_pred: bool,1595 constrained_intra_pred: bool,
1596 /// ctxIdxInc for the next macroblock's first mb_qp_delta bin: set when the1596 /// ctxIdxInc for the next macroblock's first mb_qp_delta bin: set when the
@@ -1638,7 +1638,7 @@ impl SliceCtx {
1638 /// The reference picture `RefPicListX[idx]` points at: a frame, or one1638 /// The reference picture `RefPicListX[idx]` points at: a frame, or one
1639 /// field of a stored frame.1639 /// field of a stored frame.
1640 #[inline(always)]1640 #[inline(always)]
1641- fn reference<'a, S: Sample>(1641+ pub(crate) fn reference<'a, S: Sample>(
1642 &self,1642 &self,
1643 refs: &'a [Picture<S>],1643 refs: &'a [Picture<S>],
1644 list: usize,1644 list: usize,
@@ -1682,7 +1682,7 @@ impl SliceCtx {
1682 1682
1683 /// True while decoding a field picture or a field macroblock.1683 /// True while decoding a field picture or a field macroblock.
1684 #[inline]1684 #[inline]
1685- fn is_field(&self) -> bool {1685+ pub(crate) fn is_field(&self) -> bool {
1686 self.mb_parity != FRAME1686 self.mb_parity != FRAME
1687 }1687 }
1688 1688
@@ -1690,7 +1690,7 @@ impl SliceCtx {
1690 /// row of its first luma row (motion compensation reads fields at field1690 /// row of its first luma row (motion compensation reads fields at field
1691 /// row positions), the picture row it is written at, and the row step.1691 /// row positions), the picture row it is written at, and the row step.
1692 #[inline]1692 #[inline]
1693- fn geo(&self, mb_y: usize) -> Geo {1693+ pub(crate) fn geo(&self, mb_y: usize) -> Geo {
1694 if self.pos.active && self.pos.field {1694 if self.pos.active && self.pos.field {
1695 Geo {1695 Geo {
1696 src_y: (mb_y / 2) * 16,1696 src_y: (mb_y / 2) * 16,
@@ -1712,7 +1712,7 @@ impl SliceCtx {
1712 /// a prediction from `rp`: fields of opposite parity sit half a field line1712 /// a prediction from `rp`: fields of opposite parity sit half a field line
1713 /// apart, which is a quarter of a chroma sample.1713 /// apart, which is a quarter of a chroma sample.
1714 #[inline]1714 #[inline]
1715- fn chroma_dy<S: Sample>(&self, rp: &Picture<S>) -> i16 {1715+ pub(crate) fn chroma_dy<S: Sample>(&self, rp: &Picture<S>) -> i16 {
1716 if self.cat != 1 {1716 if self.cat != 1 {
1717 // Tables 8-9 and 8-10 apply to ChromaArrayType 1 only.1717 // Tables 8-9 and 8-10 apply to ChromaArrayType 1 only.
1718 return 0;1718 return 0;
@@ -1728,12 +1728,12 @@ impl SliceCtx {
1728 /// component of every partition resolves to [`Weights::DEFAULT`] without1728 /// component of every partition resolves to [`Weights::DEFAULT`] without
1729 /// the per-component lookup: neither weighted prediction (P) nor1729 /// the per-component lookup: neither weighted prediction (P) nor
1730 /// weighted/bi-prediction (B) is signalled.1730 /// weighted/bi-prediction (B) is signalled.
1731- fn no_weights(&self) -> bool {1731+ pub(crate) fn no_weights(&self) -> bool {
1732 self.weighted_bipred_idc == 0 && !self.weighted_pred1732 self.weighted_bipred_idc == 0 && !self.weighted_pred
1733 }1733 }
1734 1734
1735 /// Prediction weights of one component (clause 8.4.3).1735 /// Prediction weights of one component (clause 8.4.3).
1736- fn weights_for<S: Sample>(1736+ pub(crate) fn weights_for<S: Sample>(
1737 &self,1737 &self,
1738 refs: &[Picture<S>],1738 refs: &[Picture<S>],
1739 comp: usize,1739 comp: usize,
@@ -3425,453 +3425,6 @@ fn colocated<S: Sample>(
3425 }3425 }
3426 }3426 }
3427 3427
3428-/// The motion vectors chroma prediction uses: the luma vectors, with the
3429-/// vertical offset of 8.4.1.4 added when a field predicts from a field of the
3430-/// opposite parity.
3431-#[inline]
3432-fn chroma_mvs<S: Sample>(
3433- ctx: &SliceCtx,
3434- refs: &[Picture<S>],
3435- ref_idx: [i8; 2],
3436- mv: [[i16; 2]; 2],
3437-) -> [[i16; 2]; 2] {
3438- let mut out = mv;
3439- if ctx.cat == 2 {
3440- // 4:2:2 chroma is full height: the vertical vector stays in quarter
3441- // samples of the chroma plane, which the eighth-sample interpolator
3442- // reads as twice the value.
3443- for l in out.iter_mut() {
3444- l[1] = l[1].wrapping_mul(2);
3445- }
3446- } else if ctx.is_field() {
3447- for l in 0..2 {
3448- if let Some(rp) = ctx.reference(refs, l, ref_idx[l]) {
3449- out[l][1] = out[l][1].wrapping_add(ctx.chroma_dy(rp));
3450- }
3451- }
3452- }
3453- out
3454-}
3455-
3456-/// The reference plane (`0` luma, `1` Cb, `2` Cr) list `list` predicts from.
3457-#[inline(always)]
3458-fn ref_plane<'a>(
3459- ctx: &SliceCtx,
3460- refs: &'a [Picture<u8>],
3461- ref_idx: [i8; 2],
3462- list: usize,
3463- comp: usize,
3464-) -> Result<RefPlane<'a>> {
3465- let rp = ctx.reference(refs, list, ref_idx[list]).ok_or_else(|| {
3466- Error::corrupt("reference index names a picture the buffer does not hold")
3467- })?;
3468- let src = match comp {
3469- 0 => &rp.y,
3470- 1 => &rp.cb,
3471- _ => &rp.cr,
3472- };
3473- Ok(RefPlane {
3474- data: &src.data,
3475- stride: src.stride,
3476- origin: src.origin,
3477- width: src.width,
3478- height: src.height,
3479- pad: src.pad,
3480- })
3481-}
3482-
3483-/// Motion compensation of one partition (clause 8.4.2): interpolate each list's
3484-/// prediction, then combine them by the weighting of 8.4.2.3 and write the
3485-/// result straight into the picture, where the residual is added on top.
3486-///
3487-/// `(px, py)` and `(pw, ph)` are in 4x4 blocks within the macroblock.
3488-#[allow(clippy::too_many_arguments)]
3489-pub(crate) fn compensate8(
3490- pic: &mut Picture<u8>,
3491- refs: &[Picture<u8>],
3492- ctx: &SliceCtx,
3493- mb_x: usize,
3494- mb_y: usize,
3495- px: usize,
3496- py: usize,
3497- pw: usize,
3498- ph: usize,
3499- mv: [[i16; 2]; 2],
3500- ref_idx: [i8; 2],
3501-) -> Result<()> {
3502- let use0 = ref_idx[0] >= 0;
3503- let use1 = ref_idx[1] >= 0;
3504- if !use0 && !use1 {
3505- return Ok(());
3506- }
3507- let geo = ctx.geo(mb_y);
3508- let x0 = (mb_x * 16 + px * 4) as i32;
3509- let y0 = (geo.src_y + py * 4) as i32;
3510- let (w, h) = (pw * 4, ph * 4);
3511- // A stream carrying no weighted prediction at all resolves every
3512- // component to the identity weighting without the per-component lookup.
3513- let plain = ctx.no_weights();
3514- // ChromaArrayType 3 predicts Cb and Cr with the luma interpolation, on
3515- // their own planes and weights.
3516- let ncomp = if ctx.cat == 3 { 3 } else { 1 };
3517- for comp in 0..ncomp {
3518- let wt = if plain {
3519- Weights::DEFAULT
3520- } else {
3521- ctx.weights_for(refs, comp, ref_idx[0], ref_idx[1], use0, use1)
3522- };
3523- // 8.4.2.3.1 over one list is the identity, so the interpolation of that
3524- // one list IS the prediction: it is written straight into the picture at
3525- // the picture's own pitch. Over both lists the combined prediction is the
3526- // rounded byte average of the two clipped ones, so list 0 interpolates
3527- // into the picture and list 1 averages on top of it (Put::Avg) — no
3528- // temporaries, no combining pass, no copy back.
3529- let direct = wt.is_default();
3530- let stride = plane_ref(pic, comp).stride * geo.mul;
3531- let origin = plane_ref(pic, comp).at(x0 as usize, geo.y(py * 4));
3532- // Weighted bi-prediction fuses the same way: the combining formula is
3533- // per-sample, so list 0 lands in the picture and list 1 combines on top
3534- // of it (Put::AvgW) with 8.4.2.3.2's exact integers.
3535- let weighted_put = if !direct && use0 && use1 {
3536- Some(crate::inter::Put::AvgW {
3537- w0: wt.w[0],
3538- w1: wt.w[1],
3539- shift: (wt.log_wd + 1) as u32,
3540- round: 1 << wt.log_wd,
3541- off: (wt.o[0] + wt.o[1] + 1) >> 1,
3542- })
3543- } else {
3544- None
3545- };
3546- if direct && use0 && use1 {
3547- let plane = ref_plane(ctx, refs, ref_idx, 0, comp)?;
3548- mc_luma(
3549- &plane,
3550- x0,
3551- y0,
3552- mv[0],
3553- w,
3554- h,
3555- stride,
3556- &mut plane_mut(pic, comp).data[origin..],
3557- crate::inter::Put::Set,
3558- );
3559- let plane = ref_plane(ctx, refs, ref_idx, 1, comp)?;
3560- mc_luma(
3561- &plane,
3562- x0,
3563- y0,
3564- mv[1],
3565- w,
3566- h,
3567- stride,
3568- &mut plane_mut(pic, comp).data[origin..],
3569- crate::inter::Put::Avg,
3570- );
3571- } else if direct {
3572- // No scratch buffer is declared on this path at all: the scratch is
3573- // zeroed on entry, and most partitions of a P slice come through here.
3574- let list = usize::from(!use0);
3575- let plane = ref_plane(ctx, refs, ref_idx, list, comp)?;
3576- mc_luma(
3577- &plane,
3578- x0,
3579- y0,
3580- mv[list],
3581- w,
3582- h,
3583- stride,
3584- &mut plane_mut(pic, comp).data[origin..],
3585- crate::inter::Put::Set,
3586- );
3587- } else if let Some(put) = weighted_put {
3588- let plane = ref_plane(ctx, refs, ref_idx, 0, comp)?;
3589- mc_luma(
3590- &plane,
3591- x0,
3592- y0,
3593- mv[0],
3594- w,
3595- h,
3596- stride,
3597- &mut plane_mut(pic, comp).data[origin..],
3598- crate::inter::Put::Set,
3599- );
3600- let plane = ref_plane(ctx, refs, ref_idx, 1, comp)?;
3601- mc_luma(
3602- &plane,
3603- x0,
3604- y0,
3605- mv[1],
3606- w,
3607- h,
3608- stride,
3609- &mut plane_mut(pic, comp).data[origin..],
3610- put,
3611- );
3612- } else {
3613- let mut part = [[0u8; 256]; 2];
3614- for list in 0..2 {
3615- if ref_idx[list] < 0 {
3616- continue;
3617- }
3618- let plane = ref_plane(ctx, refs, ref_idx, list, comp)?;
3619- mc_luma(
3620- &plane,
3621- x0,
3622- y0,
3623- mv[list],
3624- w,
3625- h,
3626- w,
3627- &mut part[list],
3628- crate::inter::Put::Set,
3629- );
3630- }
3631- let mut out = [0u8; 256];
3632- let (p0, p1) = part.split_at(1);
3633- combine(&mut out, &p0[0], &p1[0], use0, use1, &wt, w * h);
3634- for row in 0..h {
3635- let dst = origin + row * stride;
3636- plane_mut(pic, comp).data[dst..dst + w].copy_from_slice(&out[row * w..row * w + w]);
3637- }
3638- }
3639- }
3640-
3641- // Chroma (8.4.1.4): for 4:2:0 frame coding the chroma vector is the luma
3642- // vector, read as eighth-sample units over the half-resolution plane.
3643- if ctx.cat == 0 || ctx.cat == 3 {
3644- return Ok(());
3645- }
3646- let cmv = chroma_mvs(ctx, refs, ref_idx, mv);
3647- let c422 = ctx.cat == 2;
3648- let (cw, ch) = (w / 2, if c422 { h } else { h / 2 });
3649- let (cx0, cy0) = (x0 / 2, if c422 { y0 } else { y0 / 2 });
3650- let crow = if c422 { py * 4 } else { py * 2 };
3651- for comp in 0..2 {
3652- let wt = if plain {
3653- Weights::DEFAULT
3654- } else {
3655- ctx.weights_for(refs, comp + 1, ref_idx[0], ref_idx[1], use0, use1)
3656- };
3657- let direct = wt.is_default();
3658- // The combining weights are per-component syntax (luma and chroma
3659- // carry their own denoms, weights and offsets), so this component's
3660- // fused mode is built from ITS OWN lookup — never the luma one.
3661- let weighted_put = if !direct && use0 && use1 {
3662- Some(crate::inter::Put::AvgW {
3663- w0: wt.w[0],
3664- w1: wt.w[1],
3665- shift: (wt.log_wd + 1) as u32,
3666- round: 1 << wt.log_wd,
3667- off: (wt.o[0] + wt.o[1] + 1) >> 1,
3668- })
3669- } else {
3670- None
3671- };
3672- let plane_out = if comp == 0 { &mut pic.cb } else { &mut pic.cr };
3673- let stride = plane_out.stride * geo.mul;
3674- let origin = plane_out.at(cx0 as usize, geo.cy(crow));
3675- if direct && use0 && use1 {
3676- let plane = ref_plane(ctx, refs, ref_idx, 0, 1 + comp)?;
3677- mc_chroma(
3678- &plane,
3679- cx0,
3680- cy0,
3681- cmv[0],
3682- cw,
3683- ch,
3684- stride,
3685- &mut plane_out.data[origin..],
3686- crate::inter::Put::Set,
3687- );
3688- let plane = ref_plane(ctx, refs, ref_idx, 1, 1 + comp)?;
3689- mc_chroma(
3690- &plane,
3691- cx0,
3692- cy0,
3693- cmv[1],
3694- cw,
3695- ch,
3696- stride,
3697- &mut plane_out.data[origin..],
3698- crate::inter::Put::Avg,
3699- );
3700- } else if let Some(put) = weighted_put {
3701- let plane = ref_plane(ctx, refs, ref_idx, 0, 1 + comp)?;
3702- mc_chroma(
3703- &plane,
3704- cx0,
3705- cy0,
3706- cmv[0],
3707- cw,
3708- ch,
3709- stride,
3710- &mut plane_out.data[origin..],
3711- crate::inter::Put::Set,
3712- );
3713- let plane = ref_plane(ctx, refs, ref_idx, 1, 1 + comp)?;
3714- mc_chroma(
3715- &plane,
3716- cx0,
3717- cy0,
3718- cmv[1],
3719- cw,
3720- ch,
3721- stride,
3722- &mut plane_out.data[origin..],
3723- put,
3724- );
3725- } else if direct {
3726- let list = usize::from(!use0);
3727- let plane = ref_plane(ctx, refs, ref_idx, list, 1 + comp)?;
3728- mc_chroma(
3729- &plane,
3730- cx0,
3731- cy0,
3732- cmv[list],
3733- cw,
3734- ch,
3735- stride,
3736- &mut plane_out.data[origin..],
3737- crate::inter::Put::Set,
3738- );
3739- } else {
3740- let mut cpart = [[0u8; 128]; 2];
3741- for list in 0..2 {
3742- if ref_idx[list] < 0 {
3743- continue;
3744- }
3745- let plane = ref_plane(ctx, refs, ref_idx, list, 1 + comp)?;
3746- mc_chroma(
3747- &plane,
3748- cx0,
3749- cy0,
3750- cmv[list],
3751- cw,
3752- ch,
3753- cw,
3754- &mut cpart[list],
3755- crate::inter::Put::Set,
3756- );
3757- }
3758- let mut cout = [0u8; 128];
3759- let (c0, c1) = cpart.split_at(1);
3760- combine(&mut cout, &c0[0], &c1[0], use0, use1, &wt, cw * ch);
3761- for row in 0..ch {
3762- let dst = origin + row * stride;
3763- plane_out.data[dst..dst + cw].copy_from_slice(&cout[row * cw..row * cw + cw]);
3764- }
3765- }
3766- }
3767- Ok(())
3768-}
3769-
3770-/// Motion compensation of one partition over `u16` planes: the same clause
3771-/// 8.4.2 process as [`compensate8`], computed per sample from the equations.
3772-/// Weighted prediction offsets scale by `1 << (BitDepth - 8)` (8-298, 8-299).
3773-#[allow(clippy::too_many_arguments)]
3774-pub(crate) fn compensate16(
3775- pic: &mut Picture<u16>,
3776- refs: &[Picture<u16>],
3777- ctx: &SliceCtx,
3778- mb_x: usize,
3779- mb_y: usize,
3780- px: usize,
3781- py: usize,
3782- pw: usize,
3783- ph: usize,
3784- mv: [[i16; 2]; 2],
3785- ref_idx: [i8; 2],
3786-) -> Result<()> {
3787- let use0 = ref_idx[0] >= 0;
3788- let use1 = ref_idx[1] >= 0;
3789- if !use0 && !use1 {
3790- return Ok(());
3791- }
3792- let bd = pic.bd;
3793- let max = (1i32 << bd) - 1;
3794- let geo = ctx.geo(mb_y);
3795- let x0 = (mb_x * 16 + px * 4) as i32;
3796- let y0 = (geo.src_y + py * 4) as i32;
3797- let plain = ctx.no_weights();
3798- let cmv = chroma_mvs(ctx, refs, ref_idx, mv);
3799- let cat = pic.cat;
3800- let ncomp = if cat == 0 { 1 } else { 3 };
3801- for comp in 0..ncomp {
3802- let c422 = cat == 2;
3803- let (w, h, cx, cy) = if comp == 0 || cat == 3 {
3804- (pw * 4, ph * 4, x0, y0)
3805- } else if c422 {
3806- (pw * 2, ph * 4, x0 / 2, y0)
3807- } else {
3808- (pw * 2, ph * 2, x0 / 2, y0 / 2)
3809- };
3810- let mut wt = if plain {
3811- Weights::DEFAULT
3812- } else {
3813- ctx.weights_for(refs, comp, ref_idx[0], ref_idx[1], use0, use1)
3814- };
3815- wt.o[0] <<= bd - 8;
3816- wt.o[1] <<= bd - 8;
3817- let mut pred = [[0i32; 256]; 2];
3818- for list in 0..2 {
3819- if ref_idx[list] < 0 {
3820- continue;
3821- }
3822- let rp = ctx.reference(refs, list, ref_idx[list]).ok_or_else(|| {
3823- Error::corrupt("reference index names a picture the buffer does not hold")
3824- })?;
3825- match comp {
3826- 0 => crate::inter16::luma_pred(&rp.y, cx, cy, mv[list], w, h, max, &mut pred[list]),
3827- 1 if cat == 3 => {
3828- crate::inter16::luma_pred(&rp.cb, cx, cy, mv[list], w, h, max, &mut pred[list])
3829- }
3830- _ if cat == 3 => {
3831- crate::inter16::luma_pred(&rp.cr, cx, cy, mv[list], w, h, max, &mut pred[list])
3832- }
3833- 1 => crate::inter16::chroma_pred(&rp.cb, cx, cy, cmv[list], w, h, &mut pred[list]),
3834- _ => crate::inter16::chroma_pred(&rp.cr, cx, cy, cmv[list], w, h, &mut pred[list]),
3835- }
3836- }
3837- let plane = match comp {
3838- 0 => &mut pic.y,
3839- 1 => &mut pic.cb,
3840- _ => &mut pic.cr,
3841- };
3842- let stride = plane.stride * geo.mul;
3843- let origin = if comp == 0 || cat == 3 {
3844- plane.at(cx as usize, geo.y(py * 4))
3845- } else {
3846- plane.at(cx as usize, geo.cy(if c422 { py * 4 } else { py * 2 }))
3847- };
3848- let round1 = if wt.log_wd >= 1 {
3849- 1 << (wt.log_wd - 1)
3850- } else {
3851- 0
3852- };
3853- for row in 0..h {
3854- for col in 0..w {
3855- let i = row * w + col;
3856- let v = if use0 && use1 {
3857- // 8-301 / 8-300 (default weights reduce to the rounded mean).
3858- ((pred[0][i] * wt.w[0] + pred[1][i] * wt.w[1] + (1 << wt.log_wd))
3859- >> (wt.log_wd + 1))
3860- + ((wt.o[0] + wt.o[1] + 1) >> 1)
3861- } else {
3862- let k = usize::from(!use0);
3863- if wt.log_wd >= 1 {
3864- ((pred[k][i] * wt.w[k] + round1) >> wt.log_wd) + wt.o[k]
3865- } else {
3866- pred[k][i] * wt.w[k] + wt.o[k]
3867- }
3868- };
3869- plane.data[origin + row * stride + col] = v.clamp(0, max) as u16;
3870- }
3871- }
3872- }
3873- Ok(())
3874-}
3875 3428
3876 /// Gather the 13 neighbour samples of a luma 4x4 block (spec 8.3.1.2),3429 /// Gather the 13 neighbour samples of a luma 4x4 block (spec 8.3.1.2),
3877 /// including the top-right substitution rule.3430 /// including the top-right substitution rule.
@@ -4017,6 +3570,7 @@ pub(crate) fn gather_nbr8_in<S: Sample>(
4017 mod tests {3570 mod tests {
4018 use super::*;3571 use super::*;
4019 use crate::dpb::RefList;3572 use crate::dpb::RefList;
3573+ use crate::mc::compensate8;
4020 use ec_h264_syntax::{PredWeightTable, WeightEntry};3574 use ec_h264_syntax::{PredWeightTable, WeightEntry};
4021 3575
4022 /// A picture whose visible samples are a fixed ramp, borders replicated.3576 /// A picture whose visible samples are a fixed ramp, borders replicated.
crates/ec-h264/src/lib.rsmodified+1 -0
@@ -51,6 +51,7 @@ mod fmo;
51 mod inter;51 mod inter;
52 mod inter16;52 mod inter16;
53 mod mbaff;53 mod mbaff;
54+mod mc;
54 mod mv;55 mod mv;
55 mod pred;56 mod pred;
56 mod residual;57 mod residual;
crates/ec-h264/src/mc.rsadded+455 -0
@@ -0,0 +1,455 @@
1+use ec_core::error::{Error, Result};
2+
3+use crate::decoder::{SliceCtx, plane_mut, plane_ref};
4+use crate::dpb::Picture;
5+use crate::inter::{RefPlane, Weights, combine, mc_chroma, mc_luma};
6+use crate::sample::Sample;
7+
8+
9+/// The motion vectors chroma prediction uses: the luma vectors, with the
10+/// vertical offset of 8.4.1.4 added when a field predicts from a field of the
11+/// opposite parity.
12+#[inline]
13+fn chroma_mvs<S: Sample>(
14+ ctx: &SliceCtx,
15+ refs: &[Picture<S>],
16+ ref_idx: [i8; 2],
17+ mv: [[i16; 2]; 2],
18+) -> [[i16; 2]; 2] {
19+ let mut out = mv;
20+ if ctx.cat == 2 {
21+ // 4:2:2 chroma is full height: the vertical vector stays in quarter
22+ // samples of the chroma plane, which the eighth-sample interpolator
23+ // reads as twice the value.
24+ for l in out.iter_mut() {
25+ l[1] = l[1].wrapping_mul(2);
26+ }
27+ } else if ctx.is_field() {
28+ for l in 0..2 {
29+ if let Some(rp) = ctx.reference(refs, l, ref_idx[l]) {
30+ out[l][1] = out[l][1].wrapping_add(ctx.chroma_dy(rp));
31+ }
32+ }
33+ }
34+ out
35+}
36+
37+/// The reference plane (`0` luma, `1` Cb, `2` Cr) list `list` predicts from.
38+#[inline(always)]
39+fn ref_plane<'a>(
40+ ctx: &SliceCtx,
41+ refs: &'a [Picture<u8>],
42+ ref_idx: [i8; 2],
43+ list: usize,
44+ comp: usize,
45+) -> Result<RefPlane<'a>> {
46+ let rp = ctx.reference(refs, list, ref_idx[list]).ok_or_else(|| {
47+ Error::corrupt("reference index names a picture the buffer does not hold")
48+ })?;
49+ let src = match comp {
50+ 0 => &rp.y,
51+ 1 => &rp.cb,
52+ _ => &rp.cr,
53+ };
54+ Ok(RefPlane {
55+ data: &src.data,
56+ stride: src.stride,
57+ origin: src.origin,
58+ width: src.width,
59+ height: src.height,
60+ pad: src.pad,
61+ })
62+}
63+
64+/// Motion compensation of one partition (clause 8.4.2): interpolate each list's
65+/// prediction, then combine them by the weighting of 8.4.2.3 and write the
66+/// result straight into the picture, where the residual is added on top.
67+///
68+/// `(px, py)` and `(pw, ph)` are in 4x4 blocks within the macroblock.
69+#[allow(clippy::too_many_arguments)]
70+pub(crate) fn compensate8(
71+ pic: &mut Picture<u8>,
72+ refs: &[Picture<u8>],
73+ ctx: &SliceCtx,
74+ mb_x: usize,
75+ mb_y: usize,
76+ px: usize,
77+ py: usize,
78+ pw: usize,
79+ ph: usize,
80+ mv: [[i16; 2]; 2],
81+ ref_idx: [i8; 2],
82+) -> Result<()> {
83+ let use0 = ref_idx[0] >= 0;
84+ let use1 = ref_idx[1] >= 0;
85+ if !use0 && !use1 {
86+ return Ok(());
87+ }
88+ let geo = ctx.geo(mb_y);
89+ let x0 = (mb_x * 16 + px * 4) as i32;
90+ let y0 = (geo.src_y + py * 4) as i32;
91+ let (w, h) = (pw * 4, ph * 4);
92+ // A stream carrying no weighted prediction at all resolves every
93+ // component to the identity weighting without the per-component lookup.
94+ let plain = ctx.no_weights();
95+ // ChromaArrayType 3 predicts Cb and Cr with the luma interpolation, on
96+ // their own planes and weights.
97+ let ncomp = if ctx.cat == 3 { 3 } else { 1 };
98+ for comp in 0..ncomp {
99+ let wt = if plain {
100+ Weights::DEFAULT
101+ } else {
102+ ctx.weights_for(refs, comp, ref_idx[0], ref_idx[1], use0, use1)
103+ };
104+ // 8.4.2.3.1 over one list is the identity, so the interpolation of that
105+ // one list IS the prediction: it is written straight into the picture at
106+ // the picture's own pitch. Over both lists the combined prediction is the
107+ // rounded byte average of the two clipped ones, so list 0 interpolates
108+ // into the picture and list 1 averages on top of it (Put::Avg) — no
109+ // temporaries, no combining pass, no copy back.
110+ let direct = wt.is_default();
111+ let stride = plane_ref(pic, comp).stride * geo.mul;
112+ let origin = plane_ref(pic, comp).at(x0 as usize, geo.y(py * 4));
113+ // Weighted bi-prediction fuses the same way: the combining formula is
114+ // per-sample, so list 0 lands in the picture and list 1 combines on top
115+ // of it (Put::AvgW) with 8.4.2.3.2's exact integers.
116+ let weighted_put = if !direct && use0 && use1 {
117+ Some(crate::inter::Put::AvgW {
118+ w0: wt.w[0],
119+ w1: wt.w[1],
120+ shift: (wt.log_wd + 1) as u32,
121+ round: 1 << wt.log_wd,
122+ off: (wt.o[0] + wt.o[1] + 1) >> 1,
123+ })
124+ } else {
125+ None
126+ };
127+ if direct && use0 && use1 {
128+ let plane = ref_plane(ctx, refs, ref_idx, 0, comp)?;
129+ mc_luma(
130+ &plane,
131+ x0,
132+ y0,
133+ mv[0],
134+ w,
135+ h,
136+ stride,
137+ &mut plane_mut(pic, comp).data[origin..],
138+ crate::inter::Put::Set,
139+ );
140+ let plane = ref_plane(ctx, refs, ref_idx, 1, comp)?;
141+ mc_luma(
142+ &plane,
143+ x0,
144+ y0,
145+ mv[1],
146+ w,
147+ h,
148+ stride,
149+ &mut plane_mut(pic, comp).data[origin..],
150+ crate::inter::Put::Avg,
151+ );
152+ } else if direct {
153+ // No scratch buffer is declared on this path at all: the scratch is
154+ // zeroed on entry, and most partitions of a P slice come through here.
155+ let list = usize::from(!use0);
156+ let plane = ref_plane(ctx, refs, ref_idx, list, comp)?;
157+ mc_luma(
158+ &plane,
159+ x0,
160+ y0,
161+ mv[list],
162+ w,
163+ h,
164+ stride,
165+ &mut plane_mut(pic, comp).data[origin..],
166+ crate::inter::Put::Set,
167+ );
168+ } else if let Some(put) = weighted_put {
169+ let plane = ref_plane(ctx, refs, ref_idx, 0, comp)?;
170+ mc_luma(
171+ &plane,
172+ x0,
173+ y0,
174+ mv[0],
175+ w,
176+ h,
177+ stride,
178+ &mut plane_mut(pic, comp).data[origin..],
179+ crate::inter::Put::Set,
180+ );
181+ let plane = ref_plane(ctx, refs, ref_idx, 1, comp)?;
182+ mc_luma(
183+ &plane,
184+ x0,
185+ y0,
186+ mv[1],
187+ w,
188+ h,
189+ stride,
190+ &mut plane_mut(pic, comp).data[origin..],
191+ put,
192+ );
193+ } else {
194+ let mut part = [[0u8; 256]; 2];
195+ for list in 0..2 {
196+ if ref_idx[list] < 0 {
197+ continue;
198+ }
199+ let plane = ref_plane(ctx, refs, ref_idx, list, comp)?;
200+ mc_luma(
201+ &plane,
202+ x0,
203+ y0,
204+ mv[list],
205+ w,
206+ h,
207+ w,
208+ &mut part[list],
209+ crate::inter::Put::Set,
210+ );
211+ }
212+ let mut out = [0u8; 256];
213+ let (p0, p1) = part.split_at(1);
214+ combine(&mut out, &p0[0], &p1[0], use0, use1, &wt, w * h);
215+ for row in 0..h {
216+ let dst = origin + row * stride;
217+ plane_mut(pic, comp).data[dst..dst + w].copy_from_slice(&out[row * w..row * w + w]);
218+ }
219+ }
220+ }
221+
222+ // Chroma (8.4.1.4): for 4:2:0 frame coding the chroma vector is the luma
223+ // vector, read as eighth-sample units over the half-resolution plane.
224+ if ctx.cat == 0 || ctx.cat == 3 {
225+ return Ok(());
226+ }
227+ let cmv = chroma_mvs(ctx, refs, ref_idx, mv);
228+ let c422 = ctx.cat == 2;
229+ let (cw, ch) = (w / 2, if c422 { h } else { h / 2 });
230+ let (cx0, cy0) = (x0 / 2, if c422 { y0 } else { y0 / 2 });
231+ let crow = if c422 { py * 4 } else { py * 2 };
232+ for comp in 0..2 {
233+ let wt = if plain {
234+ Weights::DEFAULT
235+ } else {
236+ ctx.weights_for(refs, comp + 1, ref_idx[0], ref_idx[1], use0, use1)
237+ };
238+ let direct = wt.is_default();
239+ // The combining weights are per-component syntax (luma and chroma
240+ // carry their own denoms, weights and offsets), so this component's
241+ // fused mode is built from ITS OWN lookup — never the luma one.
242+ let weighted_put = if !direct && use0 && use1 {
243+ Some(crate::inter::Put::AvgW {
244+ w0: wt.w[0],
245+ w1: wt.w[1],
246+ shift: (wt.log_wd + 1) as u32,
247+ round: 1 << wt.log_wd,
248+ off: (wt.o[0] + wt.o[1] + 1) >> 1,
249+ })
250+ } else {
251+ None
252+ };
253+ let plane_out = if comp == 0 { &mut pic.cb } else { &mut pic.cr };
254+ let stride = plane_out.stride * geo.mul;
255+ let origin = plane_out.at(cx0 as usize, geo.cy(crow));
256+ if direct && use0 && use1 {
257+ let plane = ref_plane(ctx, refs, ref_idx, 0, 1 + comp)?;
258+ mc_chroma(
259+ &plane,
260+ cx0,
261+ cy0,
262+ cmv[0],
263+ cw,
264+ ch,
265+ stride,
266+ &mut plane_out.data[origin..],
267+ crate::inter::Put::Set,
268+ );
269+ let plane = ref_plane(ctx, refs, ref_idx, 1, 1 + comp)?;
270+ mc_chroma(
271+ &plane,
272+ cx0,
273+ cy0,
274+ cmv[1],
275+ cw,
276+ ch,
277+ stride,
278+ &mut plane_out.data[origin..],
279+ crate::inter::Put::Avg,
280+ );
281+ } else if let Some(put) = weighted_put {
282+ let plane = ref_plane(ctx, refs, ref_idx, 0, 1 + comp)?;
283+ mc_chroma(
284+ &plane,
285+ cx0,
286+ cy0,
287+ cmv[0],
288+ cw,
289+ ch,
290+ stride,
291+ &mut plane_out.data[origin..],
292+ crate::inter::Put::Set,
293+ );
294+ let plane = ref_plane(ctx, refs, ref_idx, 1, 1 + comp)?;
295+ mc_chroma(
296+ &plane,
297+ cx0,
298+ cy0,
299+ cmv[1],
300+ cw,
301+ ch,
302+ stride,
303+ &mut plane_out.data[origin..],
304+ put,
305+ );
306+ } else if direct {
307+ let list = usize::from(!use0);
308+ let plane = ref_plane(ctx, refs, ref_idx, list, 1 + comp)?;
309+ mc_chroma(
310+ &plane,
311+ cx0,
312+ cy0,
313+ cmv[list],
314+ cw,
315+ ch,
316+ stride,
317+ &mut plane_out.data[origin..],
318+ crate::inter::Put::Set,
319+ );
320+ } else {
321+ let mut cpart = [[0u8; 128]; 2];
322+ for list in 0..2 {
323+ if ref_idx[list] < 0 {
324+ continue;
325+ }
326+ let plane = ref_plane(ctx, refs, ref_idx, list, 1 + comp)?;
327+ mc_chroma(
328+ &plane,
329+ cx0,
330+ cy0,
331+ cmv[list],
332+ cw,
333+ ch,
334+ cw,
335+ &mut cpart[list],
336+ crate::inter::Put::Set,
337+ );
338+ }
339+ let mut cout = [0u8; 128];
340+ let (c0, c1) = cpart.split_at(1);
341+ combine(&mut cout, &c0[0], &c1[0], use0, use1, &wt, cw * ch);
342+ for row in 0..ch {
343+ let dst = origin + row * stride;
344+ plane_out.data[dst..dst + cw].copy_from_slice(&cout[row * cw..row * cw + cw]);
345+ }
346+ }
347+ }
348+ Ok(())
349+}
350+
351+/// Motion compensation of one partition over `u16` planes: the same clause
352+/// 8.4.2 process as [`compensate8`], computed per sample from the equations.
353+/// Weighted prediction offsets scale by `1 << (BitDepth - 8)` (8-298, 8-299).
354+#[allow(clippy::too_many_arguments)]
355+pub(crate) fn compensate16(
356+ pic: &mut Picture<u16>,
357+ refs: &[Picture<u16>],
358+ ctx: &SliceCtx,
359+ mb_x: usize,
360+ mb_y: usize,
361+ px: usize,
362+ py: usize,
363+ pw: usize,
364+ ph: usize,
365+ mv: [[i16; 2]; 2],
366+ ref_idx: [i8; 2],
367+) -> Result<()> {
368+ let use0 = ref_idx[0] >= 0;
369+ let use1 = ref_idx[1] >= 0;
370+ if !use0 && !use1 {
371+ return Ok(());
372+ }
373+ let bd = pic.bd;
374+ let max = (1i32 << bd) - 1;
375+ let geo = ctx.geo(mb_y);
376+ let x0 = (mb_x * 16 + px * 4) as i32;
377+ let y0 = (geo.src_y + py * 4) as i32;
378+ let plain = ctx.no_weights();
379+ let cmv = chroma_mvs(ctx, refs, ref_idx, mv);
380+ let cat = pic.cat;
381+ let ncomp = if cat == 0 { 1 } else { 3 };
382+ for comp in 0..ncomp {
383+ let c422 = cat == 2;
384+ let (w, h, cx, cy) = if comp == 0 || cat == 3 {
385+ (pw * 4, ph * 4, x0, y0)
386+ } else if c422 {
387+ (pw * 2, ph * 4, x0 / 2, y0)
388+ } else {
389+ (pw * 2, ph * 2, x0 / 2, y0 / 2)
390+ };
391+ let mut wt = if plain {
392+ Weights::DEFAULT
393+ } else {
394+ ctx.weights_for(refs, comp, ref_idx[0], ref_idx[1], use0, use1)
395+ };
396+ wt.o[0] <<= bd - 8;
397+ wt.o[1] <<= bd - 8;
398+ let mut pred = [[0i32; 256]; 2];
399+ for list in 0..2 {
400+ if ref_idx[list] < 0 {
401+ continue;
402+ }
403+ let rp = ctx.reference(refs, list, ref_idx[list]).ok_or_else(|| {
404+ Error::corrupt("reference index names a picture the buffer does not hold")
405+ })?;
406+ match comp {
407+ 0 => crate::inter16::luma_pred(&rp.y, cx, cy, mv[list], w, h, max, &mut pred[list]),
408+ 1 if cat == 3 => {
409+ crate::inter16::luma_pred(&rp.cb, cx, cy, mv[list], w, h, max, &mut pred[list])
410+ }
411+ _ if cat == 3 => {
412+ crate::inter16::luma_pred(&rp.cr, cx, cy, mv[list], w, h, max, &mut pred[list])
413+ }
414+ 1 => crate::inter16::chroma_pred(&rp.cb, cx, cy, cmv[list], w, h, &mut pred[list]),
415+ _ => crate::inter16::chroma_pred(&rp.cr, cx, cy, cmv[list], w, h, &mut pred[list]),
416+ }
417+ }
418+ let plane = match comp {
419+ 0 => &mut pic.y,
420+ 1 => &mut pic.cb,
421+ _ => &mut pic.cr,
422+ };
423+ let stride = plane.stride * geo.mul;
424+ let origin = if comp == 0 || cat == 3 {
425+ plane.at(cx as usize, geo.y(py * 4))
426+ } else {
427+ plane.at(cx as usize, geo.cy(if c422 { py * 4 } else { py * 2 }))
428+ };
429+ let round1 = if wt.log_wd >= 1 {
430+ 1 << (wt.log_wd - 1)
431+ } else {
432+ 0
433+ };
434+ for row in 0..h {
435+ for col in 0..w {
436+ let i = row * w + col;
437+ let v = if use0 && use1 {
438+ // 8-301 / 8-300 (default weights reduce to the rounded mean).
439+ ((pred[0][i] * wt.w[0] + pred[1][i] * wt.w[1] + (1 << wt.log_wd))
440+ >> (wt.log_wd + 1))
441+ + ((wt.o[0] + wt.o[1] + 1) >> 1)
442+ } else {
443+ let k = usize::from(!use0);
444+ if wt.log_wd >= 1 {
445+ ((pred[k][i] * wt.w[k] + round1) >> wt.log_wd) + wt.o[k]
446+ } else {
447+ pred[k][i] * wt.w[k] + wt.o[k]
448+ }
449+ };
450+ plane.data[origin + row * stride + col] = v.clamp(0, max) as u16;
451+ }
452+ }
453+ }
454+ Ok(())
455+}
crates/ec-h264/src/sample.rsmodified+2 -1
@@ -6,7 +6,8 @@
6 use crate::deblock::{6 use crate::deblock::{
7 EdgeParams, filter_chroma_h_edge8, filter_chroma_line, filter_luma_h_edge16, filter_luma_line,7 EdgeParams, filter_chroma_h_edge8, filter_chroma_line, filter_luma_h_edge16, filter_luma_line,
8 };8 };
9-use crate::decoder::{SliceCtx, compensate8, compensate16};9+use crate::decoder::SliceCtx;
10+use crate::mc::{compensate8, compensate16};
10 use crate::dpb::Picture;11 use crate::dpb::Picture;
11 use ec_core::error::Result;12 use ec_core::error::Result;
12 13