From b5f5ec851ac01fe446be03fbb0473c6654f15c04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 14 Aug 2026 14:36:19 +0300 Subject: [PATCH] fix(h264): colZeroFlag reads the co-located block's own reference index Spatial direct prediction (8.4.1.2.2) zeroes a motion vector when the co-located block sits still on a short-term picture referencing entry 0 of that picture's list. The index that decides it is refIdxCol, of the list clause 8.4.1.2.1 selected; accepting "either index is 0" instead zeroes vectors that must keep their prediction whenever the co-located block is bi-predicted with a non-zero list 0 index. That only happens when the co-located picture is itself a coded B picture, which is the first B of every run under a B pyramid: two real 1080p library files decoded with localized wrong regions in exactly those pictures and nowhere else. The new test reproduces it with b-pyramid and four references, and fails on the previous reading. --- crates/ec-h264/src/decoder.rs | 27 +++++++++--------- crates/ec-h264/tests/conformance.rs | 43 +++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 13 deletions(-) diff --git a/crates/ec-h264/src/decoder.rs b/crates/ec-h264/src/decoder.rs index 80e1eb8e..beead04a 100644 --- a/crates/ec-h264/src/decoder.rs +++ b/crates/ec-h264/src/decoder.rs @@ -1802,23 +1802,24 @@ fn direct_block( mvc: &MvCtx, at: (usize, usize), ) -> Result<([[i16; 2]; 2], [i8; 2], [i32; 2])> { - // The co-located block, in both modes. + // The co-located block, in both modes (8.4.1.2.1): its list 0 motion, or + // its list 1 motion when list 0 is unused. `col_ref_idx` is refIdxCol — + // an index into the co-located picture's own reference list, not ours. let col = ctx.lists[1].get(0).and_then(|i| refs.get(i)); - let (col_ref_id, col_mv, col_list) = match col { + let (col_ref_id, col_mv, col_ref_idx) = match col { Some(c) => { let bx = mvc.mb_x * 4 + at.0; let by = mvc.mb_y * 4 + at.1; let idx = by * c.mb_w.max(1) * 4 + bx; if idx < c.ref_id.len() && c.blk[idx] & BLK_INTRA == 0 { let list = usize::from(c.ref_id[idx][0] < 0); - (c.ref_id[idx][list], c.mv[idx][list], list) + (c.ref_id[idx][list], c.mv[idx][list], c.ref_idx[idx][list]) } else { - (-1, [0i16; 2], 0) + (-1, [0i16; 2], -1) } } - None => (-1, [0i16; 2], 0), + None => (-1, [0i16; 2], -1), }; - let _ = col_list; if ctx.direct_spatial { // Reference indices from the macroblock's own neighbours, once. @@ -1833,16 +1834,16 @@ fn direct_block( if zero_prediction { ref_idx = [0, 0]; } - // colZeroFlag: the co-located block sits still on a short-term picture. + // colZeroFlag (8.4.1.2.2): the co-located block sits still on a + // short-term picture, referencing that picture's own list entry 0. The + // index is the one belonging to the list 8.4.1.2.1 picked — reading + // "either list is 0" instead lets a bi-predicted co-located block whose + // list 1 index happens to be 0 zero a motion vector it must not, which + // only shows on a B picture used as the co-located one. let col_short = col.is_some_and(|c| c.mark == Mark::Short); let col_zero = col_short && col_ref_id >= 0 - && col.is_some_and(|c| { - let bx = mvc.mb_x * 4 + at.0; - let by = mvc.mb_y * 4 + at.1; - let idx = by * c.mb_w.max(1) * 4 + bx; - idx < c.ref_idx.len() && (c.ref_idx[idx][0] == 0 || c.ref_idx[idx][1] == 0) - }) + && col_ref_idx == 0 && (-1..=1).contains(&col_mv[0]) && (-1..=1).contains(&col_mv[1]); let mut mv = [[0i16; 2]; 2]; diff --git a/crates/ec-h264/tests/conformance.rs b/crates/ec-h264/tests/conformance.rs index bc0e12d5..8d1a1f52 100644 --- a/crates/ec-h264/tests/conformance.rs +++ b/crates/ec-h264/tests/conformance.rs @@ -1946,3 +1946,46 @@ fn packet_entry_surface_reorders_and_carries_timestamps() { out.len() ); } + +/// Spatial direct prediction whose co-located picture is itself a B picture. +/// +/// With a B pyramid the picture at RefPicList1[0] of the first B in a run is a +/// coded B picture, so its blocks are bi-predicted and carry two reference +/// indices. colZeroFlag (8.4.1.2.2) reads refIdxCol — the index of the list +/// 8.4.1.2.1 selected — and a decoder that instead accepts "either index is 0" +/// zeroes motion vectors that must keep their prediction. Several references +/// are what makes the two readings differ at all, so this needs `-refs`. +#[test] +fn spatial_direct_over_a_b_reference_matches_ffmpeg() { + if !have_ffmpeg() { + eprintln!("SKIP: ffmpeg not on PATH"); + return; + } + let dir = scratch("direct-bref"); + let mut failures = Vec::new(); + for (tag, coder) in [("cabac", "ac"), ("cavlc", "0")] { + let extra = [ + "-profile:v", + "high", + "-coder", + coder, + "-qp", + "24", + "-bf", + "3", + "-refs", + "4", + "-x264-params", + "b-adapt=0:b-pyramid=normal:direct=spatial:8x8dct=1", + ]; + match x264_encode_gop(&dir, tag, "352x288", 40, &extra) { + None => failures.push(format!("{tag}: ffmpeg cannot encode")), + Some(stream) => match compare_sequence(&stream) { + Ok(n) => eprintln!("b-pyramid direct {tag}: {n} frames bit-exact"), + Err(e) => failures.push(format!("{tag}: {e}")), + }, + } + } + let _ = std::fs::remove_dir_all(&dir); + assert!(failures.is_empty(), "{failures:#?}"); +}