Read Golomb tails up to 32 leading zeros instead of refusing at 20
Browse files at this commita36945894dfa5e435f9bbe86bad94e1d1eabd70cParent: c551af1- fix
- read Golomb tails up to 32 leading zeros instead of refusing at 20
2 changed files +55 -16
crates/ec-av1/src/decode.rsmodified+55 -15
| @@ -1346,19 +1346,30 @@ 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) -> Result<u32> { | 1349 | +fn read_golomb(dec: &mut SymbolDecoder) -> u32 { |
| 1350 | - let mut length = 1u32; | 1350 | + // lane-scaledref r1: the length prefix is capped at 32 leading zeros, not |
| 1351 | - while dec.literal(1) == 0 { | 1351 | + // refused. libaom's own `read_golomb` (decodetxb.c:30) calls a 21st bit a |
| 1352 | - length += 1; | 1352 | + // CORRUPT FRAME, which is where this decoder's old |
| 1353 | - if length > 20 { | 1353 | + // "a Golomb tail longer than this decoder reads" refusal sat; dav1d reads |
| 1354 | - return Err(unsupported("a Golomb tail longer than this decoder reads")); | 1354 | + // up to 32 (`len < 32`) and simply stops counting. Reading the longer |
| 1355 | - } | 1355 | + // prefix costs nothing and refuses nothing, and the short prefixes -- the |
| 1356 | - } | 1356 | + // only ones any encoder writes, our own included (`tile::MAX_LEVEL` is |
| 1357 | - let mut x = 1u32; | 1357 | + // `MAX_BR_LEVEL + (1 << 19)`, i.e. 19 leading zeros at most) -- are |
| 1358 | - for _ in 1..length { | 1358 | + // bit-identical to before, pinned by |
| 1359 | - x = (x << 1) | dec.literal(1); | 1359 | + // `golomb_tails_read_back_past_the_old_twenty_bit_cap`. |
| 1360 | - } | 1360 | + let mut zeros = 0u32; |
| 1361 | - Ok(x - 1) | 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) | ||
| 1362 | } | 1373 | } |
| 1363 | 1374 | ||
| 1364 | /// `decode_symbol`'s end-of-block position (spec 5.11.39): the inverse of | 1375 | /// `decode_symbol`'s end-of-block position (spec 5.11.39): the inverse of |
| @@ -1622,7 +1633,7 @@ fn read_coeffs( | |||
| 1622 | eprintln!("EC_COEFF_STEP tag=sign c={c} sign={} rng={rng}", negative as i32); | 1633 | eprintln!("EC_COEFF_STEP tag=sign c={c} sign={} rng={rng}", negative as i32); |
| 1623 | } | 1634 | } |
| 1624 | let level = if level.abs_diff(0) as i32 > MAX_BR_LEVEL { | 1635 | let level = if level.abs_diff(0) as i32 > MAX_BR_LEVEL { |
| 1625 | - let g = read_golomb(dec)?; | 1636 | + let g = read_golomb(dec); |
| 1626 | if trace { | 1637 | if trace { |
| 1627 | eprintln!("TRACE golomb pos={pos} value={g}"); | 1638 | eprintln!("TRACE golomb pos={pos} value={g}"); |
| 1628 | } | 1639 | } |
| @@ -1712,7 +1723,7 @@ fn read_coeffs_rect( | |||
| 1712 | dec.literal(1) == 1 | 1723 | dec.literal(1) == 1 |
| 1713 | }; | 1724 | }; |
| 1714 | let level = if level.abs_diff(0) as i32 > MAX_BR_LEVEL { | 1725 | let level = if level.abs_diff(0) as i32 > MAX_BR_LEVEL { |
| 1715 | - let g = read_golomb(dec)?; | 1726 | + let g = read_golomb(dec); |
| 1716 | level + g as i32 | 1727 | level + g as i32 |
| 1717 | } else { | 1728 | } else { |
| 1718 | level | 1729 | level |
| @@ -13686,6 +13697,35 @@ mod tests { | |||
| 13686 | } | 13697 | } |
| 13687 | } | 13698 | } |
| 13688 | 13699 | ||
| 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 | + | ||
| 13689 | /// A skipped block codes no residual syntax at all (spec 5.11.34): | 13729 | /// A skipped block codes no residual syntax at all (spec 5.11.34): |
| 13690 | /// straight prediction on every plane. The real key-frame writer never | 13730 | /// straight prediction on every plane. The real key-frame writer never |
| 13691 | /// emits `skip = 1` ([`crate::tile::write_intra_mode`] hardcodes it to | 13731 | /// 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] = &[ | |||
| 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", | ||
| 45 | "a bit depth other than 8 with film grain applied (film_grain.rs's LUT and blend are hardcoded 8-bit)", | 44 | "a bit depth other than 8 with film grain applied (film_grain.rs's LUT and blend are hardcoded 8-bit)", |
| 46 | "a bit depth other than 8 with use_superres set (superres.rs's upscale_row is hardcoded 8-bit)", | 45 | "a bit depth other than 8 with use_superres set (superres.rs's upscale_row is hardcoded 8-bit)", |
| 47 | "a superblock-level HORZ/VERT strip with a split transform (per-unit rect prediction is not ported)", | 46 | "a superblock-level HORZ/VERT strip with a split transform (per-unit rect prediction is not ported)", |