forge
Explore
Log in

Don't you dare go Hollow

Combine explicit-weight bipred chroma with chroma weights

Browse files at this commit

1b55d6c93120f3dcdcfaa6caf1c379b6635d9fccAhmet Kaan GümüşParent: 928abb0
  • fix
    • combine explicit-weight bipred chroma with chroma weights
Show full message
fix(h264): combine explicit-weight bipred chroma with chroma weights

The fused weighted path built its Put::AvgW once from the luma weight
lookup and reused it inside the per-component chroma loop, discarding the
loop's own per-component lookup. Luma and chroma prediction weights are
independent syntax (separate log2 denoms, weights and offsets; absent
entries default to 1<<denom), so an explicit-weight B bipred
macroblock (weighted_bipred_idc == 1) whose chroma weights differ from
luma's combined chroma with luma weights: wrong output bytes. The
fixtures could not catch it (x264 B weights are implicit, hence
component-independent) and no JVT vector covers it.

The chroma loop now shadows weighted_put with one built from its own
lookup. Witness: a decoder-level unit test drives an explicit-weight
bipred macroblock with luma != chroma weights through compensate and
asserts per-component output — verified to fail on the pre-fix code
(cb 131 vs 165 at (0,0)) and pass post-fix. RefList::push and
Plane8::extend_borders become pub(crate) as the test's constructors.

Byte-exact A/B: all five fixture hashes unchanged. Suite green (80 lib
+ 23 conformance + 12 encode + 1 decode-first).
crates/ec-h264/src/decoder.rsmodified+153 -0
@@ -2373,6 +2373,20 @@ fn compensate(
2373 ctx.weights_for(refs, comp + 1, ref_idx[0], ref_idx[1], use0, use1)2373 ctx.weights_for(refs, comp + 1, ref_idx[0], ref_idx[1], use0, use1)
2374 };2374 };
2375 let direct = wt.is_default();2375 let direct = wt.is_default();
2376+ // The combining weights are per-component syntax (luma and chroma
2377+ // carry their own denoms, weights and offsets), so this component's
2378+ // fused mode is built from ITS OWN lookup — never the luma one.
2379+ let weighted_put = if !direct && use0 && use1 {
2380+ Some(crate::inter::Put::AvgW {
2381+ w0: wt.w[0],
2382+ w1: wt.w[1],
2383+ shift: (wt.log_wd + 1) as u32,
2384+ round: 1 << wt.log_wd,
2385+ off: (wt.o[0] + wt.o[1] + 1) >> 1,
2386+ })
2387+ } else {
2388+ None
2389+ };
2376 let chroma_ref = |list: usize| -> RefPlane<'_> {2390 let chroma_ref = |list: usize| -> RefPlane<'_> {
2377 let rp = ctx2391 let rp = ctx
2378 .reference(refs, list, ref_idx[list])2392 .reference(refs, list, ref_idx[list])
@@ -2963,3 +2977,142 @@ fn boundary_strength(
2963 };2977 };
2964 bs_between(&bs_side(pic, pbx, pby), &bs_side(pic, qbx, qby), edge == 0)2978 bs_between(&bs_side(pic, pbx, pby), &bs_side(pic, qbx, qby), edge == 0)
2965 }2979 }
2980+
2981+#[cfg(test)]
2982+mod tests {
2983+ use super::*;
2984+ use crate::dpb::RefList;
2985+ use ec_h264_syntax::{PredWeightTable, WeightEntry};
2986+
2987+ /// A picture whose visible samples are a fixed ramp, borders replicated.
2988+ fn ramp_picture(value: impl Fn(usize, usize) -> u8) -> Picture {
2989+ let mut pic = Picture::default();
2990+ pic.mb_w = 1;
2991+ pic.mb_h = 1;
2992+ for (plane, size) in [(0usize, 16usize), (1, 8), (2, 8)] {
2993+ let p = match plane {
2994+ 0 => &mut pic.y,
2995+ 1 => &mut pic.cb,
2996+ _ => &mut pic.cr,
2997+ };
2998+ let pad = 8;
2999+ let stride = size + 2 * pad;
3000+ let mut data = vec![0u8; stride * (size + 2 * pad)];
3001+ for y in 0..size {
3002+ for x in 0..size {
3003+ data[(pad + y) * stride + pad + x] = value(x, y);
3004+ }
3005+ }
3006+ *p = Plane8 {
3007+ data,
3008+ stride,
3009+ origin: pad * stride + pad,
3010+ width: size,
3011+ height: size,
3012+ pad,
3013+ };
3014+ p.extend_borders();
3015+ }
3016+ pic
3017+ }
3018+
3019+ fn plane_byte(p: &Plane8, x: usize, y: usize) -> u8 {
3020+ p.data[p.origin + y * p.stride + x]
3021+ }
3022+
3023+ /// Explicit-weight bipred combines each component with ITS OWN weights:
3024+ /// luma and chroma signal independent denoms/weights/offsets, so chroma
3025+ /// must never ride the luma lookup (regression guard for the fused
3026+ /// weighted path in [`compensate`]).
3027+ #[test]
3028+ fn weighted_bipred_combines_per_component() {
3029+ let refs = vec![
3030+ ramp_picture(|x, y| (40 + x * 3 + y) as u8),
3031+ ramp_picture(|x, y| (220 - x - y * 2) as u8),
3032+ ];
3033+ let mut l0 = RefList::default();
3034+ l0.push(0);
3035+ let mut l1 = RefList::default();
3036+ l1.push(1);
3037+ let ctx = SliceCtx {
3038+ slice_id: 0,
3039+ slice_type: SliceType::B,
3040+ qp: 30,
3041+ cb_qp_offset: 0,
3042+ cr_qp_offset: 0,
3043+ transform_8x8_mode: false,
3044+ constrained_intra_pred: false,
3045+ qp_delta_inc: 0,
3046+ lists: [l0, l1],
3047+ num_ref_idx: [1, 1],
3048+ direct_spatial: true,
3049+ direct_8x8_inference: true,
3050+ weighted_pred: false,
3051+ weighted_bipred_idc: 1,
3052+ weights: Some(PredWeightTable {
3053+ luma_log2_weight_denom: 1,
3054+ chroma_log2_weight_denom: 2,
3055+ l0: vec![WeightEntry {
3056+ luma: Some((2, 1)),
3057+ chroma: Some([(5, 2), (7, 3)]),
3058+ }],
3059+ l1: vec![WeightEntry {
3060+ luma: Some((2, 1)),
3061+ chroma: Some([(5, 2), (7, 3)]),
3062+ }],
3063+ }),
3064+ poc: 4,
3065+ shapes: MbShapes::default(),
3066+ };
3067+ let mut pic = ramp_picture(|_, _| 0);
3068+
3069+ compensate(&mut pic, &refs, &ctx, 0, 0, 0, 0, 4, 4, [[0, 0]; 2], [0, 0])
3070+ .expect("compensate");
3071+
3072+ let expected = |p0: u8, p1: u8, w: i32, log_wd: u32, o: i32| -> u8 {
3073+ let v = (((i32::from(p0) * w + i32::from(p1) * w + (1 << log_wd))
3074+ >> (log_wd + 1))
3075+ + o)
3076+ .clamp(0, 255);
3077+ v as u8
3078+ };
3079+ // Luma: weights (2, 1) — whatever luma does is the luma reference
3080+ // behaviour; the point is chroma below.
3081+ for y in 0..16 {
3082+ for x in 0..16 {
3083+ let (p0, p1) = (
3084+ plane_byte(&refs[0].y, x, y),
3085+ plane_byte(&refs[1].y, x, y),
3086+ );
3087+ assert_eq!(
3088+ plane_byte(&pic.y, x, y),
3089+ expected(p0, p1, 2, 1, 1),
3090+ "luma ({x},{y})"
3091+ );
3092+ }
3093+ }
3094+ // Chroma Cb: weights (5, 2) with denom 2 — NOT the luma weights.
3095+ for y in 0..8 {
3096+ for x in 0..8 {
3097+ let (p0, p1) = (
3098+ plane_byte(&refs[0].cb, x, y),
3099+ plane_byte(&refs[1].cb, x, y),
3100+ );
3101+ assert_eq!(
3102+ plane_byte(&pic.cb, x, y),
3103+ expected(p0, p1, 5, 2, 2),
3104+ "cb ({x},{y})"
3105+ );
3106+ let (p0, p1) = (
3107+ plane_byte(&refs[0].cr, x, y),
3108+ plane_byte(&refs[1].cr, x, y),
3109+ );
3110+ assert_eq!(
3111+ plane_byte(&pic.cr, x, y),
3112+ expected(p0, p1, 7, 2, 3),
3113+ "cr ({x},{y})"
3114+ );
3115+ }
3116+ }
3117+ }
3118+}
crates/ec-h264/src/dpb.rsmodified+6 -2
@@ -83,7 +83,9 @@ impl Plane8 {
83 /// Replicate the edge samples into the padding, which turns the per-sample83 /// Replicate the edge samples into the padding, which turns the per-sample
84 /// `Clip3` of Equations 8-239/8-240 into ordinary reads (see84 /// `Clip3` of Equations 8-239/8-240 into ordinary reads (see
85 /// [`crate::inter`]).85 /// [`crate::inter`]).
86- fn extend_borders(&mut self) {86+ /// Replicate edge samples into the border; also the setup decoder tests
87+ /// need for planes that inter::RefPlane reads.
88+ pub(crate) fn extend_borders(&mut self) {
87 let (w, h, pad, stride) = (self.width, self.height, self.pad, self.stride);89 let (w, h, pad, stride) = (self.width, self.height, self.pad, self.stride);
88 for y in 0..h {90 for y in 0..h {
89 let row = self.origin + y * stride;91 let row = self.origin + y * stride;
@@ -307,7 +309,9 @@ impl RefList {
307 (idx < self.len).then(|| usize::from(self.entries[idx]))309 (idx < self.len).then(|| usize::from(self.entries[idx]))
308 }310 }
309 311
310- fn push(&mut self, idx: usize) {312+ /// Append one DPB index; also the constructor decoder tests need to name
313+ /// a non-empty reference list.
314+ pub(crate) fn push(&mut self, idx: usize) {
311 if self.len < self.entries.len() {315 if self.len < self.entries.len() {
312 self.entries[self.len] = idx as u8;316 self.entries[self.len] = idx as u8;
313 self.len += 1;317 self.len += 1;