Revert the Golomb-tail lift: the refusal is masking a real intra-rect64 defect
Browse files at this commit965c6b6ced182ee902a162272da497f157b8e0d4Parent: 97a7ed52 changed files +16 -55
crates/ec-av1/src/decode.rsmodified+15 -55
| @@ -1346,30 +1346,19 @@ fn dc_sign_ctx(vote: i32) -> usize { | |||
| 1346 | /// The remainder of a level the base and base-range syntax could not reach | 1346 | /// The remainder of a level the base and base-range syntax could not reach |
| 1347 | /// (spec 5.11.40): its bit length in unary, then that many of its own bits, | 1347 | /// (spec 5.11.40): its bit length in unary, then that many of its own bits, |
| 1348 | /// most significant first — the exact inverse of [`crate::tile::write_golomb`]. | 1348 | /// most significant first — the exact inverse of [`crate::tile::write_golomb`]. |
| 1349 | -fn read_golomb(dec: &mut SymbolDecoder) -> u32 { | 1349 | +fn read_golomb(dec: &mut SymbolDecoder) -> Result<u32> { |
| 1350 | - // lane-scaledref r1: the length prefix is capped at 32 leading zeros, not | 1350 | + let mut length = 1u32; |
| 1351 | - // refused. libaom's own `read_golomb` (decodetxb.c:30) calls a 21st bit a | 1351 | + while dec.literal(1) == 0 { |
| 1352 | - // CORRUPT FRAME, which is where this decoder's old | 1352 | + length += 1; |
| 1353 | - // "a Golomb tail longer than this decoder reads" refusal sat; dav1d reads | 1353 | + if length > 20 { |
| 1354 | - // up to 32 (`len < 32`) and simply stops counting. Reading the longer | 1354 | + return Err(unsupported("a Golomb tail longer than this decoder reads")); |
| 1355 | - // prefix costs nothing and refuses nothing, and the short prefixes -- the | 1355 | + } |
| 1356 | - // only ones any encoder writes, our own included (`tile::MAX_LEVEL` is | 1356 | + } |
| 1357 | - // `MAX_BR_LEVEL + (1 << 19)`, i.e. 19 leading zeros at most) -- are | 1357 | + let mut x = 1u32; |
| 1358 | - // bit-identical to before, pinned by | 1358 | + for _ in 1..length { |
| 1359 | - // `golomb_tails_read_back_past_the_old_twenty_bit_cap`. | 1359 | + x = (x << 1) | dec.literal(1); |
| 1360 | - let mut zeros = 0u32; | 1360 | + } |
| 1361 | - while dec.literal(1) == 0 && zeros < 32 { | 1361 | + Ok(x - 1) |
| 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) | ||
| 1373 | } | 1362 | } |
| 1374 | 1363 | ||
| 1375 | /// `decode_symbol`'s end-of-block position (spec 5.11.39): the inverse of | 1364 | /// `decode_symbol`'s end-of-block position (spec 5.11.39): the inverse of |
| @@ -1633,7 +1622,7 @@ fn read_coeffs( | |||
| 1633 | eprintln!("EC_COEFF_STEP tag=sign c={c} sign={} rng={rng}", negative as i32); | 1622 | eprintln!("EC_COEFF_STEP tag=sign c={c} sign={} rng={rng}", negative as i32); |
| 1634 | } | 1623 | } |
| 1635 | let level = if level.abs_diff(0) as i32 > MAX_BR_LEVEL { | 1624 | let level = if level.abs_diff(0) as i32 > MAX_BR_LEVEL { |
| 1636 | - let g = read_golomb(dec); | 1625 | + let g = read_golomb(dec)?; |
| 1637 | if trace { | 1626 | if trace { |
| 1638 | eprintln!("TRACE golomb pos={pos} value={g}"); | 1627 | eprintln!("TRACE golomb pos={pos} value={g}"); |
| 1639 | } | 1628 | } |
| @@ -1723,7 +1712,7 @@ fn read_coeffs_rect( | |||
| 1723 | dec.literal(1) == 1 | 1712 | dec.literal(1) == 1 |
| 1724 | }; | 1713 | }; |
| 1725 | let level = if level.abs_diff(0) as i32 > MAX_BR_LEVEL { | 1714 | let level = if level.abs_diff(0) as i32 > MAX_BR_LEVEL { |
| 1726 | - let g = read_golomb(dec); | 1715 | + let g = read_golomb(dec)?; |
| 1727 | level + g as i32 | 1716 | level + g as i32 |
| 1728 | } else { | 1717 | } else { |
| 1729 | level | 1718 | level |
| @@ -13717,35 +13706,6 @@ mod tests { | |||
| 13717 | } | 13706 | } |
| 13718 | } | 13707 | } |
| 13719 | 13708 | ||
| 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 | - | ||
| 13749 | /// A skipped block codes no residual syntax at all (spec 5.11.34): | 13709 | /// A skipped block codes no residual syntax at all (spec 5.11.34): |
| 13750 | /// straight prediction on every plane. The real key-frame writer never | 13710 | /// straight prediction on every plane. The real key-frame writer never |
| 13751 | /// emits `skip = 1` ([`crate::tile::write_intra_mode`] hardcodes it to | 13711 | /// 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] = &[ | |||
| 41 | "a 16x16 block whose true edge cuts through both axes needs a rectangular transform this decoder does not code yet", | 41 | "a 16x16 block whose true edge cuts through both axes needs a rectangular transform this decoder does not code yet", |
| 42 | "a 16x16 inter block whose true edge cuts through both axes needs a rectangular transform this decoder does not code yet", | 42 | "a 16x16 inter block whose true edge cuts through both axes needs a rectangular transform this decoder does not code yet", |
| 43 | "a 32x32 partition type this decoder does not code (value={part32})", | 43 | "a 32x32 partition type this decoder does not code (value={part32})", |
| 44 | + "a Golomb tail longer than this decoder reads", | ||
| 44 | "a bit depth other than 8 with film grain applied (film_grain.rs's LUT and blend are hardcoded 8-bit)", | 45 | "a bit depth other than 8 with film grain applied (film_grain.rs's LUT and blend are hardcoded 8-bit)", |
| 45 | "a bit depth other than 8 with use_superres set (superres.rs's upscale_row is hardcoded 8-bit)", | 46 | "a bit depth other than 8 with use_superres set (superres.rs's upscale_row is hardcoded 8-bit)", |
| 46 | "a superblock-level HORZ/VERT strip with a split transform (per-unit rect prediction is not ported)", | 47 | "a superblock-level HORZ/VERT strip with a split transform (per-unit rect prediction is not ported)", |