forge
Explore
Log in

Don't you dare go Hollow

ColZeroFlag reads the co-located block's own reference index

Browse files at this commit

b5f5ec851ac01fe446be03fbb0473c6654f15c04Ahmet Kaan GümüşParent: abd41b6
  • fix
    • colZeroFlag reads the co-located block's own reference index
Show full message
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.rsmodified+14 -13
@@ -1802,23 +1802,24 @@ fn direct_block(
1802 mvc: &MvCtx,1802 mvc: &MvCtx,
1803 at: (usize, usize),1803 at: (usize, usize),
1804 ) -> Result<([[i16; 2]; 2], [i8; 2], [i32; 2])> {1804 ) -> Result<([[i16; 2]; 2], [i8; 2], [i32; 2])> {
1805- // The co-located block, in both modes.1805+ // The co-located block, in both modes (8.4.1.2.1): its list 0 motion, or
1806+ // its list 1 motion when list 0 is unused. `col_ref_idx` is refIdxCol —
1807+ // an index into the co-located picture's own reference list, not ours.
1806 let col = ctx.lists[1].get(0).and_then(|i| refs.get(i));1808 let col = ctx.lists[1].get(0).and_then(|i| refs.get(i));
1807- let (col_ref_id, col_mv, col_list) = match col {1809+ let (col_ref_id, col_mv, col_ref_idx) = match col {
1808 Some(c) => {1810 Some(c) => {
1809 let bx = mvc.mb_x * 4 + at.0;1811 let bx = mvc.mb_x * 4 + at.0;
1810 let by = mvc.mb_y * 4 + at.1;1812 let by = mvc.mb_y * 4 + at.1;
1811 let idx = by * c.mb_w.max(1) * 4 + bx;1813 let idx = by * c.mb_w.max(1) * 4 + bx;
1812 if idx < c.ref_id.len() && c.blk[idx] & BLK_INTRA == 0 {1814 if idx < c.ref_id.len() && c.blk[idx] & BLK_INTRA == 0 {
1813 let list = usize::from(c.ref_id[idx][0] < 0);1815 let list = usize::from(c.ref_id[idx][0] < 0);
1814- (c.ref_id[idx][list], c.mv[idx][list], list)1816+ (c.ref_id[idx][list], c.mv[idx][list], c.ref_idx[idx][list])
1815 } else {1817 } else {
1816- (-1, [0i16; 2], 0)1818+ (-1, [0i16; 2], -1)
1817 }1819 }
1818 }1820 }
1819- None => (-1, [0i16; 2], 0),1821+ None => (-1, [0i16; 2], -1),
1820 };1822 };
1821- let _ = col_list;
1822 1823
1823 if ctx.direct_spatial {1824 if ctx.direct_spatial {
1824 // Reference indices from the macroblock's own neighbours, once.1825 // Reference indices from the macroblock's own neighbours, once.
@@ -1833,16 +1834,16 @@ fn direct_block(
1833 if zero_prediction {1834 if zero_prediction {
1834 ref_idx = [0, 0];1835 ref_idx = [0, 0];
1835 }1836 }
1836- // colZeroFlag: the co-located block sits still on a short-term picture.1837+ // colZeroFlag (8.4.1.2.2): the co-located block sits still on a
1838+ // short-term picture, referencing that picture's own list entry 0. The
1839+ // index is the one belonging to the list 8.4.1.2.1 picked — reading
1840+ // "either list is 0" instead lets a bi-predicted co-located block whose
1841+ // list 1 index happens to be 0 zero a motion vector it must not, which
1842+ // only shows on a B picture used as the co-located one.
1837 let col_short = col.is_some_and(|c| c.mark == Mark::Short);1843 let col_short = col.is_some_and(|c| c.mark == Mark::Short);
1838 let col_zero = col_short1844 let col_zero = col_short
1839 && col_ref_id >= 01845 && col_ref_id >= 0
1840- && col.is_some_and(|c| {1846+ && col_ref_idx == 0
1841- let bx = mvc.mb_x * 4 + at.0;
1842- let by = mvc.mb_y * 4 + at.1;
1843- let idx = by * c.mb_w.max(1) * 4 + bx;
1844- idx < c.ref_idx.len() && (c.ref_idx[idx][0] == 0 || c.ref_idx[idx][1] == 0)
1845- })
1846 && (-1..=1).contains(&col_mv[0])1847 && (-1..=1).contains(&col_mv[0])
1847 && (-1..=1).contains(&col_mv[1]);1848 && (-1..=1).contains(&col_mv[1]);
1848 let mut mv = [[0i16; 2]; 2];1849 let mut mv = [[0i16; 2]; 2];
crates/ec-h264/tests/conformance.rsmodified+43 -0
@@ -1946,3 +1946,46 @@ fn packet_entry_surface_reorders_and_carries_timestamps() {
1946 out.len()1946 out.len()
1947 );1947 );
1948 }1948 }
1949+
1950+/// Spatial direct prediction whose co-located picture is itself a B picture.
1951+///
1952+/// With a B pyramid the picture at RefPicList1[0] of the first B in a run is a
1953+/// coded B picture, so its blocks are bi-predicted and carry two reference
1954+/// indices. colZeroFlag (8.4.1.2.2) reads refIdxCol — the index of the list
1955+/// 8.4.1.2.1 selected — and a decoder that instead accepts "either index is 0"
1956+/// zeroes motion vectors that must keep their prediction. Several references
1957+/// are what makes the two readings differ at all, so this needs `-refs`.
1958+#[test]
1959+fn spatial_direct_over_a_b_reference_matches_ffmpeg() {
1960+ if !have_ffmpeg() {
1961+ eprintln!("SKIP: ffmpeg not on PATH");
1962+ return;
1963+ }
1964+ let dir = scratch("direct-bref");
1965+ let mut failures = Vec::new();
1966+ for (tag, coder) in [("cabac", "ac"), ("cavlc", "0")] {
1967+ let extra = [
1968+ "-profile:v",
1969+ "high",
1970+ "-coder",
1971+ coder,
1972+ "-qp",
1973+ "24",
1974+ "-bf",
1975+ "3",
1976+ "-refs",
1977+ "4",
1978+ "-x264-params",
1979+ "b-adapt=0:b-pyramid=normal:direct=spatial:8x8dct=1",
1980+ ];
1981+ match x264_encode_gop(&dir, tag, "352x288", 40, &extra) {
1982+ None => failures.push(format!("{tag}: ffmpeg cannot encode")),
1983+ Some(stream) => match compare_sequence(&stream) {
1984+ Ok(n) => eprintln!("b-pyramid direct {tag}: {n} frames bit-exact"),
1985+ Err(e) => failures.push(format!("{tag}: {e}")),
1986+ },
1987+ }
1988+ }
1989+ let _ = std::fs::remove_dir_all(&dir);
1990+ assert!(failures.is_empty(), "{failures:#?}");
1991+}