forge
Explore
Log in

Don't you dare go Hollow

Read Golomb tails up to 32 leading zeros instead of refusing at 20

Browse files at this commit

a36945894dfa5e435f9bbe86bad94e1d1eabd70cAhmet Kaan GümüşParent: c551af1
  • fix
    • read Golomb tails up to 32 leading zeros instead of refusing at 20
Show full message
fix(av1): read Golomb tails up to 32 leading zeros instead of refusing at 20

libaom's own read_golomb (decodetxb.c:30) calls a 21st prefix bit a corrupt
frame and this decoder mirrored that as a refusal string; dav1d reads up to 32
and stops counting. Reading the longer prefix refuses nothing and leaves every
short tail bit-identical (our own writer tops out at 19 leading zeros,
tile::MAX_LEVEL = MAX_BR_LEVEL + (1<<19)). Gate:
decode::tests::golomb_tails_read_back_past_the_old_twenty_bit_cap round-trips
the smallest and largest value at every prefix length 0..=31 through
tile::write_golomb and back. Refusal 'a Golomb tail longer than this decoder
reads' lifted from refusal_inventory.
crates/ec-av1/src/decode.rsmodified+55 -15
@@ -1346,19 +1346,30 @@ 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) -> 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)
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)
13621373 }
13631374
13641375 /// `decode_symbol`'s end-of-block position (spec 5.11.39): the inverse of
@@ -1622,7 +1633,7 @@ fn read_coeffs(
16221633 eprintln!("EC_COEFF_STEP tag=sign c={c} sign={} rng={rng}", negative as i32);
16231634 }
16241635 let level = if level.abs_diff(0) as i32 > MAX_BR_LEVEL {
1625- let g = read_golomb(dec)?;
1636+ let g = read_golomb(dec);
16261637 if trace {
16271638 eprintln!("TRACE golomb pos={pos} value={g}");
16281639 }
@@ -1712,7 +1723,7 @@ fn read_coeffs_rect(
17121723 dec.literal(1) == 1
17131724 };
17141725 let level = if level.abs_diff(0) as i32 > MAX_BR_LEVEL {
1715- let g = read_golomb(dec)?;
1726+ let g = read_golomb(dec);
17161727 level + g as i32
17171728 } else {
17181729 level
@@ -13686,6 +13697,35 @@ mod tests {
1368613697 }
1368713698 }
1368813699
13700+ /// lane-scaledref r1: the gate for lifting
13701+ /// "a Golomb tail longer than this decoder reads". The old reader refused
13702+ /// every tail with twenty or more leading zeros (libaom's own
13703+ /// `read_golomb` calls that a corrupt frame; dav1d reads up to 32), so the
13704+ /// values above `1 << 19` -- reachable levels at high bit depths -- came
13705+ /// back as an `unsupported` error rather than as themselves. Written by
13706+ /// the spec's own writer ([`crate::tile::write_golomb`], the exact inverse
13707+ /// of the syntax) and read back through the reader that decodes real
13708+ /// streams: both the smallest and the largest value carrying each prefix
13709+ /// length, so a cap left one bit either way fails here.
13710+ #[test]
13711+ fn golomb_tails_read_back_past_the_old_twenty_bit_cap() {
13712+ use crate::msac::SymbolEncoder;
13713+ for zeros in 0u32..=31 {
13714+ for value in [(1u64 << zeros) - 1, (1u64 << (zeros + 1)) - 2] {
13715+ let value = u32::try_from(value).expect("the tail's value is a u32");
13716+ let mut enc = SymbolEncoder::new();
13717+ crate::tile::write_golomb(&mut enc, value);
13718+ let data = enc.finish();
13719+ let mut dec = SymbolDecoder::new(&data);
13720+ assert_eq!(
13721+ read_golomb(&mut dec),
13722+ value,
13723+ "the tail of value {value} ({zeros} leading zeros)"
13724+ );
13725+ }
13726+ }
13727+ }
13728+
1368913729 /// A skipped block codes no residual syntax at all (spec 5.11.34):
1369013730 /// straight prediction on every plane. The real key-frame writer never
1369113731 /// emits `skip = 1` ([`crate::tile::write_intra_mode`] hardcodes it to
crates/ec-av1/src/refusal_inventory.rsmodified+0 -1
@@ -41,7 +41,6 @@ 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",
4544 "a bit depth other than 8 with film grain applied (film_grain.rs's LUT and blend are hardcoded 8-bit)",
4645 "a bit depth other than 8 with use_superres set (superres.rs's upscale_row is hardcoded 8-bit)",
4746 "a superblock-level HORZ/VERT strip with a split transform (per-unit rect prediction is not ported)",