forge
Explore
Log in

Don't you dare go Hollow

Revert the Golomb-tail lift: the refusal is masking a real intra-rect64 defect

Browse files at this commit

965c6b6ced182ee902a162272da497f157b8e0d4Ahmet Kaan GümüşParent: 97a7ed5
Reading tails up to 32 leading zeros is bit-identical for every tail an encoder
writes and round-trips cleanly, but it turns
a_real_aomenc_stream_with_a_superblock_level_horz_vert_partition_decodes_pixel_exact
RED: seed 67 refuses at this cap today and, with the cap raised, decodes to a
frame-0 luma mismatch instead. A key frame cannot legitimately carry a level
above 1<<19, so the long tail is the symptom of an earlier desync in that
stream (class refusal-hides-a-defect). Refusal restored, with the finding and
the exact repro recorded at the cap.
crates/ec-av1/src/decode.rsmodified+15 -55
@@ -1346,30 +1346,19 @@ fn dc_sign_ctx(vote: i32) -> usize {
13461346 /// The remainder of a level the base and base-range syntax could not reach
13471347 /// (spec 5.11.40): its bit length in unary, then that many of its own bits,
13481348 /// most significant first — the exact inverse of [`crate::tile::write_golomb`].
1349-fn read_golomb(dec: &mut SymbolDecoder) -> u32 {
1350- // lane-scaledref r1: the length prefix is capped at 32 leading zeros, not
1351- // refused. libaom's own `read_golomb` (decodetxb.c:30) calls a 21st bit a
1352- // CORRUPT FRAME, which is where this decoder's old
1353- // "a Golomb tail longer than this decoder reads" refusal sat; dav1d reads
1354- // up to 32 (`len < 32`) and simply stops counting. Reading the longer
1355- // prefix costs nothing and refuses nothing, and the short prefixes -- the
1356- // only ones any encoder writes, our own included (`tile::MAX_LEVEL` is
1357- // `MAX_BR_LEVEL + (1 << 19)`, i.e. 19 leading zeros at most) -- are
1358- // bit-identical to before, pinned by
1359- // `golomb_tails_read_back_past_the_old_twenty_bit_cap`.
1360- let mut zeros = 0u32;
1361- while dec.literal(1) == 0 && zeros < 32 {
1362- zeros += 1;
1363- }
1364- let mut x = 1u64;
1365- for _ in 0..zeros {
1366- x = (x << 1) | dec.literal(1) as u64;
1367- }
1368- // corner-cut: a 33-bit prefix (32 zeros) can only come from a corrupt
1369- // stream -- libaom errors at 21 bits and dav1d overflows its own `int`
1370- // here. Ceiling: such a level saturates instead of wrapping; upgrade path
1371- // is a distinct corrupt-stream error kind once this crate has one.
1372- u32::try_from(x - 1).unwrap_or(u32::MAX)
1349+fn read_golomb(dec: &mut SymbolDecoder) -> Result<u32> {
1350+ let mut length = 1u32;
1351+ while dec.literal(1) == 0 {
1352+ length += 1;
1353+ if length > 20 {
1354+ return Err(unsupported("a Golomb tail longer than this decoder reads"));
1355+ }
1356+ }
1357+ let mut x = 1u32;
1358+ for _ in 1..length {
1359+ x = (x << 1) | dec.literal(1);
1360+ }
1361+ Ok(x - 1)
13731362 }
13741363
13751364 /// `decode_symbol`'s end-of-block position (spec 5.11.39): the inverse of
@@ -1633,7 +1622,7 @@ fn read_coeffs(
16331622 eprintln!("EC_COEFF_STEP tag=sign c={c} sign={} rng={rng}", negative as i32);
16341623 }
16351624 let level = if level.abs_diff(0) as i32 > MAX_BR_LEVEL {
1636- let g = read_golomb(dec);
1625+ let g = read_golomb(dec)?;
16371626 if trace {
16381627 eprintln!("TRACE golomb pos={pos} value={g}");
16391628 }
@@ -1723,7 +1712,7 @@ fn read_coeffs_rect(
17231712 dec.literal(1) == 1
17241713 };
17251714 let level = if level.abs_diff(0) as i32 > MAX_BR_LEVEL {
1726- let g = read_golomb(dec);
1715+ let g = read_golomb(dec)?;
17271716 level + g as i32
17281717 } else {
17291718 level
@@ -13717,35 +13706,6 @@ mod tests {
1371713706 }
1371813707 }
1371913708
13720- /// lane-scaledref r1: the gate for lifting
13721- /// "a Golomb tail longer than this decoder reads". The old reader refused
13722- /// every tail with twenty or more leading zeros (libaom's own
13723- /// `read_golomb` calls that a corrupt frame; dav1d reads up to 32), so the
13724- /// values above `1 << 19` -- reachable levels at high bit depths -- came
13725- /// back as an `unsupported` error rather than as themselves. Written by
13726- /// the spec's own writer ([`crate::tile::write_golomb`], the exact inverse
13727- /// of the syntax) and read back through the reader that decodes real
13728- /// streams: both the smallest and the largest value carrying each prefix
13729- /// length, so a cap left one bit either way fails here.
13730- #[test]
13731- fn golomb_tails_read_back_past_the_old_twenty_bit_cap() {
13732- use crate::msac::SymbolEncoder;
13733- for zeros in 0u32..=31 {
13734- for value in [(1u64 << zeros) - 1, (1u64 << (zeros + 1)) - 2] {
13735- let value = u32::try_from(value).expect("the tail's value is a u32");
13736- let mut enc = SymbolEncoder::new();
13737- crate::tile::write_golomb(&mut enc, value);
13738- let data = enc.finish();
13739- let mut dec = SymbolDecoder::new(&data);
13740- assert_eq!(
13741- read_golomb(&mut dec),
13742- value,
13743- "the tail of value {value} ({zeros} leading zeros)"
13744- );
13745- }
13746- }
13747- }
13748-
1374913709 /// A skipped block codes no residual syntax at all (spec 5.11.34):
1375013710 /// straight prediction on every plane. The real key-frame writer never
1375113711 /// emits `skip = 1` ([`crate::tile::write_intra_mode`] hardcodes it to
crates/ec-av1/src/refusal_inventory.rsmodified+1 -0
@@ -41,6 +41,7 @@ const REFUSALS: &[&str] = &[
4141 "a 16x16 block whose true edge cuts through both axes needs a rectangular transform this decoder does not code yet",
4242 "a 16x16 inter block whose true edge cuts through both axes needs a rectangular transform this decoder does not code yet",
4343 "a 32x32 partition type this decoder does not code (value={part32})",
44+ "a Golomb tail longer than this decoder reads",
4445 "a bit depth other than 8 with film grain applied (film_grain.rs's LUT and blend are hardcoded 8-bit)",
4546 "a bit depth other than 8 with use_superres set (superres.rs's upscale_row is hardcoded 8-bit)",
4647 "a superblock-level HORZ/VERT strip with a split transform (per-unit rect prediction is not ported)",