From 4b8dc73f66806447d54a88486e85b387874be130 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 13:29:56 +0300 Subject: [PATCH] fix(h264): reference index spans its whole 8x8, and numbering precedes marking Two more inter-only defects, both found against the JVT reference decoder traces that ship with the vectors: - ref_idx_lX covers a whole macroblock partition. Writing it over only the first sub-macroblock partition left the rest of an 8x8 marked "list unused", so the mvd contexts of 9.3.3.1.1.7 read absMvdComp 0 for a neighbour that had one, and CABAC desynchronised. - clause 8.2.4.1 (FrameNumWrap, PicNum) ran only when a slice built a reference list. An intra slice builds none, and the picture being stored had no FrameNumWrap of its own, so the sliding window of 8.2.5.3 compared stale numbers and evicted the wrong reference. Numbering now runs at the head of `store`, the one point every picture passes through. JVT full-sequence sweep: 18 vectors bit-exact end to end before, 25 now, of the 26 that are in scope. --- crates/ec-h264/src/decoder.rs | 35 +++++++++++++++++++++++++++++++---- crates/ec-h264/src/dpb.rs | 10 ++++++++++ 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/crates/ec-h264/src/decoder.rs b/crates/ec-h264/src/decoder.rs index 200623df..0b04f1d1 100644 --- a/crates/ec-h264/src/decoder.rs +++ b/crates/ec-h264/src/decoder.rs @@ -512,6 +512,17 @@ impl Decoder { sh.num_ref_idx_l1_active, sh.header_bits, &rbsp[..rbsp.len().min(6)] ); } + if trace_enabled() && !sh.slice_type.is_intra() { + for x in 0..2 { + let l: Vec<(u32, i32)> = (0..lists[x].len()) + .filter_map(|i| lists[x].get(i)) + .map(|k| (self.dpb.frames[k].frame_num, self.dpb.frames[k].poc)) + .collect(); + if !l.is_empty() { + eprintln!(" RefPicList{x} (frame_num, poc): {l:?}"); + } + } + } let intra_slice = sh.slice_type.is_intra(); let b_slice = sh.slice_type == SliceType::B; if !intra_slice && ctx.lists[0].len() == 0 { @@ -1475,12 +1486,19 @@ fn decode_inter_mb( let by = (mb_y * 4 + py) as i32; let inc = ref_idx_cond(pic, &mvc, bx - 1, by, list) + 2 * ref_idx_cond(pic, &mvc, bx, by - 1, list); - r.ref_idx(ctx.num_ref_idx[list] as u32 - 1, inc)? as i8 + let v = r.ref_idx(ctx.num_ref_idx[list] as u32 - 1, inc)? as i8; + if trace_enabled() { + eprintln!(" ref_idx part {part} list {list} inc {inc} = {v}"); + } + v }; motion.ref_idx[list][part] = idx; - // Publish it so the next partition's context can see it. + // Publish it over the whole macroblock partition — one + // reference index covers an entire 8x8, however it is further + // split — so the next partition's context (9.3.3.1.1.6) and + // the mvd contexts of 9.3.3.1.1.7 see it everywhere. let (px, py) = part_origin(&shape, part, (0, 0)); - let (pw, ph) = part_size(&shape, &motion, part); + let (pw, ph) = mb_part_size(&shape); for dy in 0..ph { for dx in 0..pw { let bi = (mb_y * 4 + py + dy) * pic.mb_w * 4 + mb_x * 4 + px + dx; @@ -1528,7 +1546,10 @@ fn decode_inter_mb( }; let v = r.mvd(comp, inc)?; if trace_enabled() { - eprintln!(" mvd part {part} sp {sp} list {list} comp {comp} inc {inc} = {v}"); + eprintln!( + " mvd part {part} sp {sp} at ({px},{py}) list {list} comp {comp} \ + absA {a} absB {bb} inc {inc} = {v}" + ); } mvd[comp] = i16::try_from(v) .map_err(|_| Error::corrupt("mvd outside the level limits"))?; @@ -1694,6 +1715,12 @@ fn sp_offset(shape: &MbShape, m: &MbMotion, part: usize, sp: usize) -> (usize, u } } +/// Size of one *macroblock* partition in 4x4 blocks: always 8x8 for a +/// macroblock that carries sub-macroblock types, however those split it. +fn mb_part_size(shape: &MbShape) -> (usize, usize) { + if shape.sub { (2, 2) } else { (shape.w, shape.h) } +} + /// Size of one (sub-)macroblock partition in 4x4 blocks. fn part_size(shape: &MbShape, m: &MbMotion, part: usize) -> (usize, usize) { if shape.sub { diff --git a/crates/ec-h264/src/dpb.rs b/crates/ec-h264/src/dpb.rs index 3a16d62d..3954ad51 100644 --- a/crates/ec-h264/src/dpb.rs +++ b/crates/ec-h264/src/dpb.rs @@ -227,6 +227,8 @@ impl Picture { self.output = false; self.non_existing = false; self.long_term_frame_idx = 0; + self.frame_num_wrap = 0; + self.pic_num = 0; } /// Replicate every plane's borders once the picture is fully reconstructed. @@ -812,6 +814,14 @@ impl Dpb { let has_mmco5 = marking.is_some_and(|m| m.mmcos.iter().any(|c| c.op == 5)); pic.output = true; pic.complete = true; + // Clause 8.2.4.1 against this picture's frame_num, which is what both + // the sliding window (8.2.5.3) and the MMCO picture numbers (8.2.5.4) + // compare. An intra slice never built a reference list, so this is the + // only place the numbering is guaranteed to be current — and the + // picture being stored has no FrameNumWrap of its own until now. + self.number_short_term(info.frame_num, sps); + pic.frame_num_wrap = pic.frame_num as i32; + pic.pic_num = pic.frame_num as i32; if info.is_idr { let no_output = marking.is_some_and(|m| m.no_output_of_prior_pics);