Merge branch 'lane-t900' (d33bd9bc): four more refusals proven (SB-root and 16x16 partition alphabets, tx_type set map, Golomb value domain); 17 of 40 proven
Browse files at this commit2 changed files +333 -46
- crates/ec-av1/src/decode.rs +24 -13
- crates/ec-av1/src/refusal_inventory.rs +309 -33
crates/ec-av1/src/decode.rsmodified+24 -13
| @@ -4133,10 +4133,31 @@ fn dc_sign_ctx(vote: i32) -> usize { | |||
| 4133 | } | 4133 | } |
| 4134 | } | 4134 | } |
| 4135 | 4135 | ||
| 4136 | +/// The `tx_type` a CDF symbol names, given the width of the row it was read | ||
| 4137 | +/// from. | ||
| 4138 | +/// | ||
| 4139 | +/// The row's own width names its set (spec `Tx_Type_Intra_Inv_Set*` / | ||
| 4140 | +/// `Tx_Type_Inter_Inv_Set*`): 17 slots (16 symbols) is `EXT_TX_SET_ALL16`, 13 | ||
| 4141 | +/// (12) `EXT_TX_SET_DTT9_IDTX_1DDCT`, 8 (7) `TX_SET_INTRA_1`, and everything | ||
| 4142 | +/// narrower the reduced `TX_SET_INTRA_2`/`TX_SET_INTER_3` order, whose two | ||
| 4143 | +/// members are a prefix of the five. | ||
| 4144 | +/// | ||
| 4145 | +/// One function rather than a `match` at each reader, so | ||
| 4146 | +/// [`crate::refusal_inventory`]'s enumeration of every (width, symbol) pair | ||
| 4147 | +/// tests the mapping the readers actually use. | ||
| 4148 | +pub(crate) fn tx_type_from_symbol(cdf_len: usize, t: usize) -> Option<TxType> { | ||
| 4149 | + match cdf_len { | ||
| 4150 | + 17 => TxType::from_symbol_all16(t), | ||
| 4151 | + 13 => TxType::from_symbol_set2_12(t), | ||
| 4152 | + 8 => TxType::from_symbol_set1(t), | ||
| 4153 | + _ => TxType::from_symbol(t), | ||
| 4154 | + } | ||
| 4155 | +} | ||
| 4156 | + | ||
| 4136 | /// The remainder of a level the base and base-range syntax could not reach | 4157 | /// The remainder of a level the base and base-range syntax could not reach |
| 4137 | /// (spec 5.11.40): its bit length in unary, then that many of its own bits, | 4158 | /// (spec 5.11.40): its bit length in unary, then that many of its own bits, |
| 4138 | /// most significant first — the exact inverse of [`crate::tile::write_golomb`]. | 4159 | /// most significant first — the exact inverse of [`crate::tile::write_golomb`]. |
| 4139 | -fn read_golomb(dec: &mut SymbolDecoder) -> Result<u32> { | 4160 | +pub(crate) fn read_golomb(dec: &mut SymbolDecoder) -> Result<u32> { |
| 4140 | // lane-scaledref r1: this cap MASKS A REAL DEFECT and must not be lifted | 4161 | // lane-scaledref r1: this cap MASKS A REAL DEFECT and must not be lifted |
| 4141 | // on its own. Reading up to 32 leading zeros (dav1d's `len < 32`; libaom | 4162 | // on its own. Reading up to 32 leading zeros (dav1d's `len < 32`; libaom |
| 4142 | // itself calls a 21st bit a corrupt frame, decodetxb.c:30) is bit-identical | 4163 | // itself calls a 21st bit a corrupt frame, decodetxb.c:30) is bit-identical |
| @@ -4327,12 +4348,7 @@ fn read_coeffs( | |||
| 4327 | if trace { | 4348 | if trace { |
| 4328 | eprintln!("TRACE tx_type value={t} len={len}"); | 4349 | eprintln!("TRACE tx_type value={t} len={len}"); |
| 4329 | } | 4350 | } |
| 4330 | - tx_type = match len { | 4351 | + tx_type = tx_type_from_symbol(len, t) |
| 4331 | - 17 => TxType::from_symbol_all16(t), | ||
| 4332 | - 13 => TxType::from_symbol_set2_12(t), | ||
| 4333 | - 8 => TxType::from_symbol_set1(t), | ||
| 4334 | - _ => TxType::from_symbol(t), | ||
| 4335 | - } | ||
| 4336 | .ok_or_else(|| unsupported(format!("a tx_type symbol outside its CDF's own set: {t}")))?; | 4352 | .ok_or_else(|| unsupported(format!("a tx_type symbol outside its CDF's own set: {t}")))?; |
| 4337 | } | 4353 | } |
| 4338 | 4354 | ||
| @@ -4512,12 +4528,7 @@ fn read_coeffs_rect( | |||
| 4512 | if let Some(tx_type_cdf) = coding.tx_type.as_deref_mut() { | 4528 | if let Some(tx_type_cdf) = coding.tx_type.as_deref_mut() { |
| 4513 | let len = tx_type_cdf.len(); | 4529 | let len = tx_type_cdf.len(); |
| 4514 | let t = dec.symbol(tx_type_cdf); | 4530 | let t = dec.symbol(tx_type_cdf); |
| 4515 | - tx_type = match len { | 4531 | + tx_type = tx_type_from_symbol(len, t) |
| 4516 | - 17 => TxType::from_symbol_all16(t), | ||
| 4517 | - 13 => TxType::from_symbol_set2_12(t), | ||
| 4518 | - 8 => TxType::from_symbol_set1(t), | ||
| 4519 | - _ => TxType::from_symbol(t), | ||
| 4520 | - } | ||
| 4521 | .ok_or_else(|| unsupported(format!("a tx_type symbol outside its CDF's own set: {t}")))?; | 4532 | .ok_or_else(|| unsupported(format!("a tx_type symbol outside its CDF's own set: {t}")))?; |
| 4522 | if rect_trace { | 4533 | if rect_trace { |
| 4523 | eprintln!("EC_COEFF_STEP tag=tx_type plane=0 rng={}", dec.debug_state().0); | 4534 | eprintln!("EC_COEFF_STEP tag=tx_type plane=0 rng={}", dec.debug_state().0); |
crates/ec-av1/src/refusal_inventory.rsmodified+309 -33
| @@ -235,6 +235,33 @@ const PROVEN: &[(&str, &str)] = &[ | |||
| 235 | "a 128x128 superblock partition value outside the 8-symbol alphabet", | 235 | "a 128x128 superblock partition value outside the 8-symbol alphabet", |
| 236 | "every_partition_value_of_an_enumerated_alphabet_has_an_arm", | 236 | "every_partition_value_of_an_enumerated_alphabet_has_an_arm", |
| 237 | ), | 237 | ), |
| 238 | + // lane-t900 r24, enumeration: the key-frame superblock root's own `match` | ||
| 239 | + // has an arm for all ten `partition_w64` values. | ||
| 240 | + ( | ||
| 241 | + "a superblock-level partition value outside PARTITION_NONE..PARTITION_VERT_4", | ||
| 242 | + "every_partition_value_of_an_enumerated_alphabet_has_an_arm", | ||
| 243 | + ), | ||
| 244 | + // lane-t900 r24, enumeration: the inter 16x16-level if/else chain's | ||
| 245 | + // branches name all ten `partition_w16` values between them. | ||
| 246 | + ( | ||
| 247 | + "an inter 16x16-level partition value outside NONE/HORZ/VERT/SPLIT/AB/1:4", | ||
| 248 | + "every_partition_value_of_an_if_chain_alphabet_is_named_by_a_branch", | ||
| 249 | + ), | ||
| 250 | + // lane-t900 r24, enumeration: every (CDF width, symbol) pair a tx_type row | ||
| 251 | + // can present maps to a distinct member of that width's own set. | ||
| 252 | + ( | ||
| 253 | + "a tx_type symbol outside its CDF's own set: {t}", | ||
| 254 | + "every_tx_type_symbol_of_every_cdf_width_maps_into_its_own_set", | ||
| 255 | + ), | ||
| 256 | + // lane-t900 r24, enumeration: the reader covers spec 5.11.40's whole value | ||
| 257 | + // domain (`0..=(1 << 20) - 2`, both ends of every prefix length), so the | ||
| 258 | + // refusal is outside it. What it still names is a bit pattern no encoder | ||
| 259 | + // writes -- a 20th prefix bit of 0, which the spec's `length == 20` break | ||
| 260 | + // makes a don't-care. | ||
| 261 | + ( | ||
| 262 | + "a Golomb tail longer than this decoder reads", | ||
| 263 | + "read_golomb_reads_every_value_a_conformant_stream_can_carry", | ||
| 264 | + ), | ||
| 238 | ( | 265 | ( |
| 239 | "a bit depth of 12 (this decoder is gated at 8 and 10 only: warp/MC/wiener rounding shifts change at 12-bit and no 12-bit gate exists)", | 266 | "a bit depth of 12 (this decoder is gated at 8 and 10 only: warp/MC/wiener rounding shifts change at 12-bit and no 12-bit gate exists)", |
| 240 | "a_twelve_bit_sequence_header_is_refused_by_name", | 267 | "a_twelve_bit_sequence_header_is_refused_by_name", |
| @@ -422,6 +449,41 @@ mod tests { | |||
| 422 | ); | 449 | ); |
| 423 | } | 450 | } |
| 424 | 451 | ||
| 452 | + /// The partition alphabet, in CDF symbol order. | ||
| 453 | + const PARTITION_NAMES: [&str; 10] = [ | ||
| 454 | + "PARTITION_NONE", | ||
| 455 | + "PARTITION_HORZ", | ||
| 456 | + "PARTITION_VERT", | ||
| 457 | + "PARTITION_SPLIT", | ||
| 458 | + "PARTITION_HORZ_A", | ||
| 459 | + "PARTITION_HORZ_B", | ||
| 460 | + "PARTITION_VERT_A", | ||
| 461 | + "PARTITION_VERT_B", | ||
| 462 | + "PARTITION_HORZ_4", | ||
| 463 | + "PARTITION_VERT_4", | ||
| 464 | + ]; | ||
| 465 | + | ||
| 466 | + /// The partition values a `match` arm pattern -- or an `if` chain's | ||
| 467 | + /// condition -- names: its own tokens, plus every value of an inclusive | ||
| 468 | + /// range (`(PARTITION_HORZ_A..=PARTITION_VERT_B).contains(&p)` is four | ||
| 469 | + /// values in one). Tokenised, never `contains()`d: a misspelled constant | ||
| 470 | + /// in a match pattern is an irrefutable binding that compiles and swallows | ||
| 471 | + /// the alphabet, and a substring check would call that covered. | ||
| 472 | + fn partition_values_named(head: &str) -> BTreeSet<usize> { | ||
| 473 | + let mut out = BTreeSet::new(); | ||
| 474 | + let idx: Vec<usize> = head | ||
| 475 | + .split(|c: char| !(c.is_ascii_alphanumeric() || c == '_')) | ||
| 476 | + .filter_map(|tok| PARTITION_NAMES.iter().position(|n| *n == tok)) | ||
| 477 | + .collect(); | ||
| 478 | + if head.contains("..=") && idx.len() == 2 { | ||
| 479 | + for v in idx[0]..=idx[1] { | ||
| 480 | + out.insert(v); | ||
| 481 | + } | ||
| 482 | + } | ||
| 483 | + out.extend(idx); | ||
| 484 | + out | ||
| 485 | + } | ||
| 486 | + | ||
| 425 | /// The partition alphabets, enumerated against the arms that code them. | 487 | /// The partition alphabets, enumerated against the arms that code them. |
| 426 | /// | 488 | /// |
| 427 | /// A partition value is a CDF symbol, and `SymbolDecoder::symbol_fixed` | 489 | /// A partition value is a CDF symbol, and `SymbolDecoder::symbol_fixed` |
| @@ -443,42 +505,20 @@ mod tests { | |||
| 443 | /// ladder rather than a match, so it needs its own enumerator. | 505 | /// ladder rather than a match, so it needs its own enumerator. |
| 444 | #[test] | 506 | #[test] |
| 445 | fn every_partition_value_of_an_enumerated_alphabet_has_an_arm() { | 507 | fn every_partition_value_of_an_enumerated_alphabet_has_an_arm() { |
| 446 | - const NAMES: [&str; 10] = [ | ||
| 447 | - "PARTITION_NONE", | ||
| 448 | - "PARTITION_HORZ", | ||
| 449 | - "PARTITION_VERT", | ||
| 450 | - "PARTITION_SPLIT", | ||
| 451 | - "PARTITION_HORZ_A", | ||
| 452 | - "PARTITION_HORZ_B", | ||
| 453 | - "PARTITION_VERT_A", | ||
| 454 | - "PARTITION_VERT_B", | ||
| 455 | - "PARTITION_HORZ_4", | ||
| 456 | - "PARTITION_VERT_4", | ||
| 457 | - ]; | ||
| 458 | // (the refusal in the fallback arm, the alphabet its CDF codes) | 508 | // (the refusal in the fallback arm, the alphabet its CDF codes) |
| 459 | - let blocks: [(&str, usize); 3] = [ | 509 | + let blocks: [(&str, usize); 4] = [ |
| 460 | ("a 32x32 partition type this decoder does not code", crate::cdf::PARTITION_W32[0].len() - 1), | 510 | ("a 32x32 partition type this decoder does not code", crate::cdf::PARTITION_W32[0].len() - 1), |
| 461 | ("an INTER 32x32 partition type this decoder does not code", crate::cdf::PARTITION_W32[0].len() - 1), | 511 | ("an INTER 32x32 partition type this decoder does not code", crate::cdf::PARTITION_W32[0].len() - 1), |
| 462 | ("a 128x128 superblock partition value outside the 8-symbol alphabet", crate::cdf::PARTITION_W128[0].len() - 1), | 512 | ("a 128x128 superblock partition value outside the 8-symbol alphabet", crate::cdf::PARTITION_W128[0].len() - 1), |
| 513 | + // lane-t900 r24: the key-frame superblock root. Its `part` is | ||
| 514 | + // either a `partition_w64` symbol or, at a frame edge, one of the | ||
| 515 | + // three gathered outcomes (PARTITION_HORZ, PARTITION_VERT, | ||
| 516 | + // PARTITION_SPLIT) -- all inside the same ten-value alphabet. | ||
| 517 | + ( | ||
| 518 | + "a superblock-level partition value outside PARTITION_NONE..PARTITION_VERT_4", | ||
| 519 | + crate::cdf::PARTITION_W64[0].len() - 1, | ||
| 520 | + ), | ||
| 463 | ]; | 521 | ]; |
| 464 | - // A pattern's own tokens, plus every value of an inclusive range | ||
| 465 | - // (`p if (PARTITION_HORZ_A..=PARTITION_VERT_B).contains(&p)` is four | ||
| 466 | - // arms in one). | ||
| 467 | - let covered = |head: &str| -> BTreeSet<usize> { | ||
| 468 | - let mut out = BTreeSet::new(); | ||
| 469 | - let idx: Vec<usize> = head | ||
| 470 | - .split(|c: char| !(c.is_ascii_alphanumeric() || c == '_')) | ||
| 471 | - .filter_map(|tok| NAMES.iter().position(|n| *n == tok)) | ||
| 472 | - .collect(); | ||
| 473 | - if head.contains("..=") && idx.len() == 2 { | ||
| 474 | - for v in idx[0]..=idx[1] { | ||
| 475 | - out.insert(v); | ||
| 476 | - } | ||
| 477 | - } | ||
| 478 | - out.extend(idx); | ||
| 479 | - out | ||
| 480 | - }; | ||
| 481 | - | ||
| 482 | let src = include_str!("decode.rs"); | 522 | let src = include_str!("decode.rs"); |
| 483 | let lines: Vec<&str> = src.lines().collect(); | 523 | let lines: Vec<&str> = src.lines().collect(); |
| 484 | let indent = |l: &str| l.len() - l.trim_start().len(); | 524 | let indent = |l: &str| l.len() - l.trim_start().len(); |
| @@ -509,7 +549,7 @@ mod tests { | |||
| 509 | continue; | 549 | continue; |
| 510 | } | 550 | } |
| 511 | in_fallback = l.trim_start().starts_with("_ =>"); | 551 | in_fallback = l.trim_start().starts_with("_ =>"); |
| 512 | - arms.extend(covered(l.split("=>").next().unwrap_or(""))); | 552 | + arms.extend(partition_values_named(l.split("=>").next().unwrap_or(""))); |
| 513 | } | 553 | } |
| 514 | let Some((reason, alphabet)) = | 554 | let Some((reason, alphabet)) = |
| 515 | blocks.iter().find(|(reason, _)| fallback.contains(reason)) | 555 | blocks.iter().find(|(reason, _)| fallback.contains(reason)) |
| @@ -523,7 +563,7 @@ mod tests { | |||
| 523 | "the `match` at decode.rs:{} refuses {reason:?} but has no arm for {:?} -- that \ | 563 | "the `match` at decode.rs:{} refuses {reason:?} but has no arm for {:?} -- that \ |
| 524 | is a real gap in its {alphabet}-value alphabet, not a dead refusal", | 564 | is a real gap in its {alphabet}-value alphabet, not a dead refusal", |
| 525 | i + 1, | 565 | i + 1, |
| 526 | - missing.iter().map(|v| NAMES[*v]).collect::<Vec<_>>() | 566 | + missing.iter().map(|v| PARTITION_NAMES[*v]).collect::<Vec<_>>() |
| 527 | ); | 567 | ); |
| 528 | } | 568 | } |
| 529 | assert_eq!( | 569 | assert_eq!( |
| @@ -533,6 +573,242 @@ mod tests { | |||
| 533 | ); | 573 | ); |
| 534 | } | 574 | } |
| 535 | 575 | ||
| 576 | + /// The same enumeration for a partition refusal that sits in an `if` | ||
| 577 | + /// chain rather than a `match` fallback. | ||
| 578 | + /// | ||
| 579 | + /// The inter 16x16-level ladder tests `part16` against constants arm by | ||
| 580 | + /// arm and refuses in its second-to-last branch, with `PARTITION_SPLIT` | ||
| 581 | + /// left to the final `else`. Nothing about that shape is weaker than a | ||
| 582 | + /// `match`: `part16` is a `partition_w16` symbol (or, at a frame edge, a | ||
| 583 | + /// gathered value of the same alphabet), so if the chain's conditions | ||
| 584 | + /// between them name every value of that alphabet, the refusing branch is | ||
| 585 | + /// unreachable. | ||
| 586 | + /// | ||
| 587 | + /// The conditions are read from the source, tokenised (never | ||
| 588 | + /// substring-matched -- lane-t900 r21), and a condition carrying `&&` is | ||
| 589 | + /// rejected outright: a value it names is only conditionally handled, and | ||
| 590 | + /// counting it would overstate the coverage. | ||
| 591 | + #[test] | ||
| 592 | + fn every_partition_value_of_an_if_chain_alphabet_is_named_by_a_branch() { | ||
| 593 | + // (the refusal in the chain's refusing branch, the alphabet its CDF codes) | ||
| 594 | + let chains: [(&str, usize); 1] = [( | ||
| 595 | + "an inter 16x16-level partition value outside NONE/HORZ/VERT/SPLIT/AB/1:4", | ||
| 596 | + crate::cdf::PARTITION_W16[0].len() - 1, | ||
| 597 | + )]; | ||
| 598 | + | ||
| 599 | + let src = include_str!("decode.rs"); | ||
| 600 | + let lines: Vec<&str> = src.lines().collect(); | ||
| 601 | + let indent = |l: &str| l.len() - l.trim_start().len(); | ||
| 602 | + let mut found = 0usize; | ||
| 603 | + for (reason, alphabet) in chains { | ||
| 604 | + let refusal = lines | ||
| 605 | + .iter() | ||
| 606 | + .position(|l| l.contains(reason)) | ||
| 607 | + .unwrap_or_else(|| panic!("{reason:?} is not in decode.rs")); | ||
| 608 | + // The branch that carries the refusal: the nearest `} else if` | ||
| 609 | + // above it. Its indentation is the chain's own. | ||
| 610 | + let head = (0..refusal) | ||
| 611 | + .rev() | ||
| 612 | + .find(|&i| lines[i].trim_start().starts_with("} else if ")) | ||
| 613 | + .unwrap_or_else(|| panic!("{reason:?} is not inside an if/else chain")); | ||
| 614 | + let outer = indent(lines[head]); | ||
| 615 | + let var = lines[head].trim_start()["} else if ".len()..] | ||
| 616 | + .split_whitespace() | ||
| 617 | + .next() | ||
| 618 | + .unwrap_or("") | ||
| 619 | + .to_owned(); | ||
| 620 | + assert!(!var.is_empty(), "the chain at decode.rs:{} tests nothing", head + 1); | ||
| 621 | + | ||
| 622 | + // Walk up collecting every branch head of this chain, each read to | ||
| 623 | + // the line that opens its body. | ||
| 624 | + let mut named = BTreeSet::new(); | ||
| 625 | + let mut i = head + 1; | ||
| 626 | + let mut heads = 0usize; | ||
| 627 | + loop { | ||
| 628 | + i -= 1; | ||
| 629 | + let t = lines[i].trim_start(); | ||
| 630 | + if indent(lines[i]) != outer || !(t.starts_with("} else if ") || t.starts_with("if ")) | ||
| 631 | + { | ||
| 632 | + if i == 0 { | ||
| 633 | + break; | ||
| 634 | + } | ||
| 635 | + continue; | ||
| 636 | + } | ||
| 637 | + let mut cond = String::new(); | ||
| 638 | + for l in &lines[i..] { | ||
| 639 | + cond.push(' '); | ||
| 640 | + cond.push_str(l.trim()); | ||
| 641 | + if l.trim_end().ends_with('{') { | ||
| 642 | + break; | ||
| 643 | + } | ||
| 644 | + } | ||
| 645 | + assert!( | ||
| 646 | + cond.contains(&var), | ||
| 647 | + "the branch at decode.rs:{} sits in the chain that refuses {reason:?} but \ | ||
| 648 | + does not test {var}", | ||
| 649 | + i + 1 | ||
| 650 | + ); | ||
| 651 | + assert!( | ||
| 652 | + !cond.contains("&&"), | ||
| 653 | + "the branch at decode.rs:{} handles its partition values only conditionally \ | ||
| 654 | + ({cond:?}) -- the enumeration below would overstate what the chain covers", | ||
| 655 | + i + 1 | ||
| 656 | + ); | ||
| 657 | + named.extend(partition_values_named(&cond)); | ||
| 658 | + heads += 1; | ||
| 659 | + if t.starts_with("if ") { | ||
| 660 | + break; | ||
| 661 | + } | ||
| 662 | + assert!(i > 0, "the chain that refuses {reason:?} has no start"); | ||
| 663 | + } | ||
| 664 | + // The value the refusing branch excludes (`part16 != PARTITION_SPLIT`) | ||
| 665 | + // is handled by the chain's final `else`, so require one. | ||
| 666 | + let close = " ".repeat(outer) + "} else {"; | ||
| 667 | + assert!( | ||
| 668 | + lines[refusal..].iter().any(|l| *l == close), | ||
| 669 | + "the chain that refuses {reason:?} has no final `else`, so the value its \ | ||
| 670 | + refusing branch excludes is handled nowhere" | ||
| 671 | + ); | ||
| 672 | + assert!(heads >= 2, "only {heads} branch(es) found for {reason:?}"); | ||
| 673 | + found += 1; | ||
| 674 | + let missing: Vec<usize> = (0..alphabet).filter(|v| !named.contains(v)).collect(); | ||
| 675 | + assert!( | ||
| 676 | + missing.is_empty(), | ||
| 677 | + "the chain at decode.rs:{} refuses {reason:?} but no branch names {:?} -- that \ | ||
| 678 | + is a real gap in its {alphabet}-value alphabet, not a dead refusal", | ||
| 679 | + head + 1, | ||
| 680 | + missing.iter().map(|v| PARTITION_NAMES[*v]).collect::<Vec<_>>() | ||
| 681 | + ); | ||
| 682 | + } | ||
| 683 | + assert_eq!(found, chains.len()); | ||
| 684 | + } | ||
| 685 | + | ||
| 686 | + /// Every `tx_type` symbol of every CDF width, mapped through the set that | ||
| 687 | + /// width names. | ||
| 688 | + /// | ||
| 689 | + /// The reader decodes `t` from a row of `n` slots, so `t` is in | ||
| 690 | + /// `0..n - 1`, and hands `(n, t)` to [`crate::decode::tx_type_from_symbol`] | ||
| 691 | + /// -- the readers' own mapping, called here rather than transcribed | ||
| 692 | + /// (class `shared-oracle-blindness`: a second copy of a table can be | ||
| 693 | + /// self-consistently wrong). The widths are read from `cdf_state.rs`'s | ||
| 694 | + /// `tx_type` field declarations, so a new table with a width the mapping | ||
| 695 | + /// does not name fails this test rather than reaching the refusal at | ||
| 696 | + /// runtime. | ||
| 697 | + /// | ||
| 698 | + /// Distinctness is part of the claim: a set of `n - 1` symbols must map to | ||
| 699 | + /// `n - 1` DIFFERENT transform types, or the mapping has lost a member of | ||
| 700 | + /// the spec's `Tx_Type_*_Inv_Set*` even while every symbol resolves. | ||
| 701 | + #[test] | ||
| 702 | + fn every_tx_type_symbol_of_every_cdf_width_maps_into_its_own_set() { | ||
| 703 | + let mut widths: BTreeSet<usize> = BTreeSet::new(); | ||
| 704 | + for line in include_str!("cdf_state.rs").lines() { | ||
| 705 | + let t = line.trim(); | ||
| 706 | + if !(t.starts_with("pub ") && t.contains("tx_type") && t.contains("[u16;")) { | ||
| 707 | + continue; | ||
| 708 | + } | ||
| 709 | + let tail = t.rsplit("[u16;").next().unwrap_or(""); | ||
| 710 | + let n: usize = tail | ||
| 711 | + .split(']') | ||
| 712 | + .next() | ||
| 713 | + .unwrap_or("") | ||
| 714 | + .trim() | ||
| 715 | + .parse() | ||
| 716 | + .unwrap_or_else(|_| panic!("cannot read a CDF width out of {t:?}")); | ||
| 717 | + widths.insert(n); | ||
| 718 | + } | ||
| 719 | + assert!( | ||
| 720 | + widths.len() >= 4, | ||
| 721 | + "the cdf_state.rs scan found only {widths:?} -- it is broken, not the decoder" | ||
| 722 | + ); | ||
| 723 | + for &n in &widths { | ||
| 724 | + let nsyms = n - 1; | ||
| 725 | + let mut set: Vec<crate::transform::TxType> = Vec::new(); | ||
| 726 | + for t in 0..nsyms { | ||
| 727 | + let ty = crate::decode::tx_type_from_symbol(n, t).unwrap_or_else(|| { | ||
| 728 | + panic!( | ||
| 729 | + "a {n}-slot tx_type CDF codes symbol {t}, which maps to no transform \ | ||
| 730 | + type -- the refusal \"a tx_type symbol outside its CDF's own set\" is \ | ||
| 731 | + reachable, and that is a mapping-table gap to fix" | ||
| 732 | + ) | ||
| 733 | + }); | ||
| 734 | + assert!( | ||
| 735 | + !set.contains(&ty), | ||
| 736 | + "a {n}-slot tx_type CDF maps symbol {t} to {ty:?}, which another symbol of \ | ||
| 737 | + the same set already names -- a member of the spec's inverse table is lost" | ||
| 738 | + ); | ||
| 739 | + set.push(ty); | ||
| 740 | + } | ||
| 741 | + assert_eq!(set.len(), nsyms); | ||
| 742 | + } | ||
| 743 | + } | ||
| 744 | + | ||
| 745 | + /// The Golomb tail, enumerated over every value a conformant stream can | ||
| 746 | + /// carry. | ||
| 747 | + /// | ||
| 748 | + /// Spec 5.11.40 `read_golomb` counts its unary prefix and BREAKS at | ||
| 749 | + /// `length == 20`, then reads `length - 1` payload bits into an `x` whose | ||
| 750 | + /// top bit is implicit -- so no conformant stream carries a value above | ||
| 751 | + /// `(1 << 20) - 2`, and spec 5.11.39 agrees from the other side by masking | ||
| 752 | + /// the level it feeds with `0xFFFFF`. This walks both ends of every | ||
| 753 | + /// prefix length 1..=20 (plus the small values every real stream uses) | ||
| 754 | + /// through [`crate::tile::write_golomb`] and back through the decoder's own | ||
| 755 | + /// [`crate::decode::read_golomb`], so the refusal "a Golomb tail longer | ||
| 756 | + /// than this decoder reads" is proved to sit strictly OUTSIDE the value | ||
| 757 | + /// domain, not inside it. | ||
| 758 | + /// | ||
| 759 | + /// The residue is a bit pattern, not a value: the spec's break makes the | ||
| 760 | + /// 20th prefix bit a don't-care, so a stream whose 20th prefix bit is 0 | ||
| 761 | + /// decodes to the same value there and is refused here. Every encoder | ||
| 762 | + /// writes the terminating 1 (our own writer tops out at 19 zeros), and | ||
| 763 | + /// lifting the cap is blocked on the defect it currently masks -- see the | ||
| 764 | + /// comment on `read_golomb`. | ||
| 765 | + #[test] | ||
| 766 | + fn read_golomb_reads_every_value_a_conformant_stream_can_carry() { | ||
| 767 | + let roundtrip = |value: u32| -> ec_core::Result<u32> { | ||
| 768 | + let mut enc = crate::msac::SymbolEncoder::new(); | ||
| 769 | + crate::tile::write_golomb(&mut enc, value); | ||
| 770 | + let data = enc.finish(); | ||
| 771 | + let mut dec = crate::msac::SymbolDecoder::new(&data); | ||
| 772 | + crate::decode::read_golomb(&mut dec) | ||
| 773 | + }; | ||
| 774 | + let mut values: BTreeSet<u32> = (0..=64u32).collect(); | ||
| 775 | + for length in 1..=20u32 { | ||
| 776 | + // `x` has `length` bits with its top bit set, and the value is | ||
| 777 | + // `x - 1`: the two ends of what this prefix length can express. | ||
| 778 | + values.insert((1u32 << (length - 1)) - 1); | ||
| 779 | + values.insert(((1u64 << length) - 2) as u32); | ||
| 780 | + } | ||
| 781 | + let max = *values.iter().max().unwrap(); | ||
| 782 | + assert_eq!(max, (1u32 << 20) - 2, "the enumeration misses the spec's own ceiling"); | ||
| 783 | + // A coefficient is masked to 20 bits (spec 5.11.39), and the base and | ||
| 784 | + // base-range syntax contribute at most 15 of that before the tail, so | ||
| 785 | + // this is the largest tail a legal coefficient can need. | ||
| 786 | + assert!(max >= 0xF_FFFF - 15, "the ceiling is below the largest legal tail"); | ||
| 787 | + for value in values { | ||
| 788 | + match roundtrip(value) { | ||
| 789 | + Ok(read) => assert_eq!(read, value, "the Golomb tail {value} read back as {read}"), | ||
| 790 | + Err(e) => panic!( | ||
| 791 | + "the Golomb tail {value} is inside the domain spec 5.11.40 can write but \ | ||
| 792 | + this decoder refuses it: {e}" | ||
| 793 | + ), | ||
| 794 | + } | ||
| 795 | + } | ||
| 796 | + // And the refusal still names something: 20 zero prefix bits. | ||
| 797 | + let mut enc = crate::msac::SymbolEncoder::new(); | ||
| 798 | + for _ in 0..20 { | ||
| 799 | + enc.literal(0, 1); | ||
| 800 | + } | ||
| 801 | + for _ in 0..20 { | ||
| 802 | + enc.literal(1, 1); | ||
| 803 | + } | ||
| 804 | + let data = enc.finish(); | ||
| 805 | + let mut dec = crate::msac::SymbolDecoder::new(&data); | ||
| 806 | + assert!( | ||
| 807 | + crate::decode::read_golomb(&mut dec).is_err(), | ||
| 808 | + "the Golomb refusal is dead code -- drop it from the inventory" | ||
| 809 | + ); | ||
| 810 | + } | ||
| 811 | + | ||
| 536 | /// Every entry of [`PROVEN`] must still name a live refusal and a test | 812 | /// Every entry of [`PROVEN`] must still name a live refusal and a test |
| 537 | /// that exists, and the count it prints is the inventory's numerator. | 813 | /// that exists, and the count it prints is the inventory's numerator. |
| 538 | #[test] | 814 | #[test] |