craft-raw: test tables where code plus extra bits fill the lookup, canonical reference, clippy allow

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
vangel76
2026-11-15 18:13:19 +01:00
co-authored by Claude Sonnet 5.5
parent fed17b6bb3
commit df89f6b6b7
3 changed files with 76 additions and 16 deletions
+41 -9
View File
@@ -145,9 +145,10 @@ mod tests {
/// Reference: match the code bit by bit, then read the category's additional bits (T.81 F.1.2.1).
fn reference(src: &[u8], pos: &mut usize, codes: &[(u16, u8)]) -> Option<i32> {
let window: u32 = (0..12).fold(0, |a, k| (a << 1) | bit(src, *pos + k));
let (sym, len) = codes.iter().enumerate().find_map(|(s, &(c, l))| {
(l > 0 && (c as u32 >> (12 - l)) == window >> (12 - l)).then_some((s as u32, l as usize))
})?;
let (sym, len) = codes
.iter()
.enumerate()
.find_map(|(s, &(c, l))| (l > 0 && (c as u32 >> (12 - l)) == window >> (12 - l)).then_some((s as u32, l as usize)))?;
*pos += len;
if sym == 0 {
return Some(0);
@@ -157,13 +158,25 @@ mod tests {
Some(if v < 1 << (sym - 1) { v - (1 << sym) + 1 } else { v })
}
#[test]
fn diff_matches_bitwise_reference_for_every_category() {
// categories 0..=16, code lengths up to 12 so that short codes combine with their bits and long ones do not
let lens = [2u8, 2, 3, 3, 4, 4, 5, 6, 7, 8, 9, 10, 11, 12, 12, 12, 12];
let codes = canonical(&lens);
/// `diff` against the reference on random streams (invalid codes are stepped over) and on every (code, extra
/// bits) pair of every symbol.
fn check_table(lens: &[u8]) {
let codes = canonical(lens);
let h = Huffman::new(&codes).unwrap();
for (sym, &(code, len)) in codes.iter().enumerate() {
for v in 0..1u64 << sym {
let (len, n) = (len as u32, sym as u32);
let word = (((code >> (12 - len)) as u64) << n | v) << (64 - len - n);
let src = word.to_be_bytes();
let mut pos = 0;
let want = reference(&src, &mut pos, &codes);
let mut bits = Bits::new(&src);
assert_eq!(diff(&mut bits, &h), want, "symbol {sym} bits {v:#b}");
assert_eq!(bits.consumed_bits(), pos);
}
}
let mut s = 0x1234_5678_9abc_def0u64;
let (mut invalid, mut long) = (0, 0);
for _ in 0..40 {
let src: Vec<u8> = (0..400)
.map(|_| {
@@ -174,13 +187,32 @@ mod tests {
let mut bits = Bits::new(&src);
let mut pos = 0;
while pos < src.len() * 8 - 64 {
let before = pos;
let want = reference(&src, &mut pos, &codes);
assert_eq!(diff(&mut bits, &h), want);
if want.is_none() {
break;
// step over the invalid code
invalid += 1;
pos += 1;
bits.skip(1);
} else {
long += (pos - before > 12) as u32;
}
assert_eq!(bits.consumed_bits(), pos);
}
}
assert!(long > 0, "{invalid} invalid codes, no symbol longer than the lookup");
}
#[test]
fn diff_matches_bitwise_reference_for_every_category() {
// categories 0..=16, code lengths up to 12 so that short codes combine with their bits and long ones do not
check_table(&[2, 2, 3, 3, 4, 4, 5, 6, 7, 8, 9, 10, 11, 12, 12, 12, 12]);
}
#[test]
fn diff_matches_reference_when_code_and_bits_fill_the_lookup_exactly() {
// category 8 with a 4-bit code and category 7 with a 5-bit code: len + category == 12
check_table(&[2, 3, 3, 5, 5, 5, 6, 5, 4, 7, 8, 9, 10, 11, 12, 12, 12]);
}
}
+1
View File
@@ -778,6 +778,7 @@ pub fn decode(d: &[u8], max_samples: usize) -> Result<Frame, RawError> {
restart_left -= end - x;
}
// the first pixel of the run has the special predictions (start of the interval, first column)
#[allow(clippy::needless_range_loop)] // `c` indexes the tables and the sample position alike
for c in 0..nc {
let i = x * nc + c;
let p = if reset_pending {
+34 -7
View File
@@ -181,15 +181,42 @@ fn roundtrip_skewed_categories_with_long_codes() {
}
}
/// `decode_diff` is `decode` followed by `diff_value`, for tables with codes inside and beyond the lookup.
/// Canonical decoding from the code lengths alone, bit by bit (T.81 Annex F, Figure F.16): the reference for the
/// lookup tables.
fn canonical_symbol(br: &mut BitReader, lens: &[u8]) -> Option<u8> {
let window = br.peek(16);
let (mut first, mut index) = (0u32, 0usize);
for len in 1..=16u8 {
let code = window >> (16 - len);
let n = lens.iter().filter(|&&l| l == len).count() as u32;
if code < first + n {
br.consume(len as u32);
return Some((index + (code - first) as usize) as u8);
}
index += n as usize;
first = (first + n) << 1;
}
None
}
/// `decode_diff` against canonical decoding and `diff_value`, for tables with codes inside and beyond the lookup
/// and with `len + category` equal to the lookup width.
#[test]
fn decode_diff_matches_decode_and_diff_value() {
for lens in [[2u8, 2, 3, 3, 4, 4, 5, 6, 7, 8, 9, 10, 11, 12, 12, 12, 12], [2, 2, 3, 3, 4, 4, 5, 6, 7, 8, 9, 10, 13, 14, 15, 16, 16]] {
fn decode_diff_matches_canonical_decoding() {
let tables: [[u8; 17]; 3] = [
[2, 2, 3, 3, 4, 4, 5, 6, 7, 8, 9, 10, 11, 12, 12, 12, 12],
[2, 2, 3, 3, 4, 4, 5, 6, 7, 8, 9, 10, 13, 14, 15, 16, 16],
[2, 3, 3, 5, 5, 5, 6, 5, 4, 7, 8, 9, 10, 11, 12, 12, 12],
];
for lens in tables {
let mut counts = [0u8; 16];
for &l in &lens {
counts[l as usize - 1] += 1;
}
let values: Vec<u8> = (0..17).collect(); // lengths are already ascending
// symbols in code order: by length, ties by symbol
let mut values: Vec<u8> = (0..17).collect();
values.sort_by_key(|&v| (lens[v as usize], v));
let sorted: Vec<u8> = values.iter().map(|&v| lens[v as usize]).collect();
let t = Huffman::new(&counts, &values).unwrap();
let mut s = 77u64;
let src: Vec<u8> = (0..6000)
@@ -200,11 +227,11 @@ fn decode_diff_matches_decode_and_diff_value() {
.collect();
let (mut a, mut b) = (BitReader::new(&src), BitReader::new(&src));
for n in 0..8000 {
let want = t.decode(&mut a).map(|ssss| diff_value(&mut a, ssss));
let want = canonical_symbol(&mut a, &sorted).map(|i| diff_value(&mut a, values[i as usize]));
let got = t.decode_diff(&mut b, false);
assert_eq!(got.ok(), want.ok(), "symbol {n}");
assert_eq!(got.ok(), want, "symbol {n}");
assert_eq!(a.peek(32), b.peek(32), "symbol {n}");
if want.is_err() {
if want.is_none() {
// step past the invalid code so the walk goes on
a.consume(1);
b.consume(1);