diff --git a/crates/raw/src/huffman.rs b/crates/raw/src/huffman.rs index cce27237b..4f35f64bb 100644 --- a/crates/raw/src/huffman.rs +++ b/crates/raw/src/huffman.rs @@ -4,16 +4,19 @@ use crate::{RawError, Result}; -/// A canonical-by-table Huffman decoder with a 12-bit lookup. +/// A canonical-by-table Huffman decoder with a 12-bit lookup that also holds the difference when code and +/// additional bits fit in 12 bits. pub(crate) struct Huffman { - /// Indexed by the next 12 bits: (symbol, length); length 0 = invalid. - lut: Vec<(u8, u8)>, + /// Indexed by the next 12 bits. Per entry: bits 0-7 the symbol, bits 8-11 the code length (0 = invalid), bits + /// 12-15 the length of code plus additional bits when both fit in 12 bits (0 = they do not), bits 16-31 the + /// signed difference in that case. + lut: Box<[u32; 4096]>, } impl Huffman { /// From `(left-aligned 12-bit code, length)` per symbol. pub fn new(codes: &[(u16, u8)]) -> Result { - let mut lut = vec![(0u8, 0u8); 4096]; + let mut lut = Box::new([0u32; 4096]); for (sym, &(code, len)) in codes.iter().enumerate() { if len == 0 { continue; @@ -21,10 +24,19 @@ impl Huffman { if len > 12 || sym > 16 { return Err(RawError::Unsupported(format!("PEF Huffman code of length {len}"))); } + let (len, s) = (len as u32, sym as u32); let first = (code >> (12 - len) << (12 - len)) as usize; - for e in &mut lut[first..first + (1 << (12 - len))] { - if e.1 == 0 { - *e = (sym as u8, len); + for (i, e) in lut[first..first + (1 << (12 - len))].iter_mut().enumerate() { + if (*e >> 8) & 15 == 0 { + let mut entry = (len << 8) | s; + if s == 0 { + entry |= len << 12; + } else if len + s <= 12 { + let v = (((first + i) >> (12 - len - s)) & ((1 << s) - 1)) as i32; + let d = if v < 1 << (s - 1) { v - (1 << s) + 1 } else { v }; + entry |= ((len + s) << 12) | ((d as i16 as u16 as u32) << 16); + } + *e = entry; } } } @@ -85,21 +97,122 @@ impl<'a> Bits<'a> { } } -/// Decode one difference: Huffman category then T.81 additional bits. +/// Decode one difference: Huffman category then T.81 additional bits, in one lookup when both fit in 12 bits. #[inline] pub(crate) fn diff(bits: &mut Bits, h: &Huffman) -> Option { - let (sym, len) = h.lut[bits.peek(12) as usize]; + let e = h.lut[(bits.peek(12) & 0xfff) as usize]; + let len = (e >> 8) & 15; if len == 0 { return None; } - bits.skip(len as u32); - let s = sym as u32; - if s == 0 { - return Some(0); + let total = (e >> 12) & 15; + if total > 0 { + bits.skip(total); + return Some((e as i32) >> 16); } + bits.skip(len); + let s = e & 0xff; if s > 16 { return None; } let v = bits.get(s) as i32; Some(if v < 1 << (s - 1) { v - (1 << s) + 1 } else { v }) } + +#[cfg(test)] +mod tests { + use super::*; + + /// Canonical left-aligned 12-bit codes for symbols 0..lens.len() (shorter codes first, ties by symbol). + fn canonical(lens: &[u8]) -> Vec<(u16, u8)> { + let mut order: Vec = (0..lens.len()).collect(); + order.sort_by_key(|&s| (lens[s], s)); + let mut codes = vec![(0u16, 0u8); lens.len()]; + let (mut code, mut prev) = (0u32, 0u8); + for s in order { + code <<= lens[s] - prev; + prev = lens[s]; + codes[s] = ((code << (12 - lens[s])) as u16, lens[s]); + code += 1; + } + codes + } + + fn bit(src: &[u8], i: usize) -> u32 { + src.get(i / 8).map_or(0, |b| ((b >> (7 - i % 8)) & 1) as u32) + } + + /// 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 { + 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)))?; + *pos += len; + if sym == 0 { + return Some(0); + } + let v = (0..sym as usize).fold(0u32, |a, k| (a << 1) | bit(src, *pos + k)) as i32; + *pos += sym as usize; + Some(if v < 1 << (sym - 1) { v - (1 << sym) + 1 } else { v }) + } + + /// `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 = (0..400) + .map(|_| { + s = s.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407); + (s >> 40) as u8 + }) + .collect(); + 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() { + // 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]); + } +} diff --git a/crates/raw/src/ljpeg.rs b/crates/raw/src/ljpeg.rs index 56b07a533..498a8302f 100644 --- a/crates/raw/src/ljpeg.rs +++ b/crates/raw/src/ljpeg.rs @@ -32,15 +32,17 @@ fn err(s: &str) -> RawError { /// Huffman decoding table (T.81 Annex C "BITS"/"HUFFVAL"). #[derive(Clone, Debug)] pub struct Huffman { - /// For peeks of `LOOKUP` bits: (code length, symbol); length 0 = slow path. - fast: Vec<(u8, u8)>, + /// Indexed by the next `LOOKUP` bits; length 0 = slow path. Per entry: bits 0-7 the symbol, bits 8-11 the code + /// length, bits 12-15 the length of code plus extra bits when both fit in the lookup (0 = they do not), and + /// bits 16-31 the signed difference in that case. + fast: Vec, maxcode: [i32; 18], valptr: [i32; 17], mincode: [i32; 17], values: Vec, } -const LOOKUP: u32 = 10; +const LOOKUP: u32 = 12; impl Huffman { /// Build from the 16 code-length counts and the symbol list. @@ -52,7 +54,7 @@ impl Huffman { let mut maxcode = [-1i32; 18]; let mut valptr = [0i32; 17]; let mut mincode = [0i32; 17]; - let mut fast = vec![(0u8, 0u8); 1 << LOOKUP]; + let mut fast = vec![0u32; 1 << LOOKUP]; let mut code = 0i32; let mut k = 0usize; for len in 1..=16usize { @@ -63,9 +65,10 @@ impl Huffman { if len as u32 <= LOOKUP { let shift = LOOKUP - len as u32; let base = (code as usize) << shift; + let ssss = values[k] as u32; for i in 0..(1usize << shift) { if let Some(slot) = fast.get_mut(base + i) { - *slot = (len as u8, values[k]); + *slot = fast_entry(len as u32, ssss, base + i); } } } @@ -84,12 +87,41 @@ impl Huffman { #[inline] pub fn decode(&self, br: &mut B) -> Result { - let peek = br.peek(LOOKUP); - let (len, sym) = self.fast[peek as usize]; + let e = self.fast[br.peek(LOOKUP) as usize]; + let len = (e >> 8) & 15; if len > 0 { - br.consume(len as u32); - return Ok(sym); + br.consume(len); + return Ok(e as u8); } + self.decode_slow(br) + } + + /// One symbol and its T.81 difference in a single lookup when code and extra bits fit in `LOOKUP` bits; the same + /// as [`Huffman::decode`] followed by [`diff_value`]. `strict`: a category above 16 is an error (subsampled + /// scans) instead of reading 16 bits. + #[inline] + pub fn decode_diff(&self, br: &mut B, strict: bool) -> Result { + let e = self.fast[br.peek(LOOKUP) as usize]; + let len = (e >> 8) & 15; + let total = (e >> 12) & 15; + if total > 0 { + br.consume(total); + return Ok((e as i32) >> 16); + } + let ssss = if len > 0 { + br.consume(len); + e as u8 + } else { + self.decode_slow(br)? + }; + if strict && ssss > 16 { + return Err(err("invalid difference category")); + } + Ok(diff_value(br, ssss)) + } + + #[cold] + fn decode_slow(&self, br: &mut B) -> Result { let mut code = br.peek(LOOKUP + 1) as i32; let mut len = LOOKUP as usize + 1; while len <= 16 && code > self.maxcode[len] { @@ -105,6 +137,21 @@ impl Huffman { } } +/// Lookup entry for a code of `len` bits (`len <= LOOKUP`) naming category `ssss`, at window position `index` +/// (the code is the top `len` bits of the `LOOKUP`-bit window). See [`Huffman::fast`]. +fn fast_entry(len: u32, ssss: u32, index: usize) -> u32 { + let base = (len << 8) | ssss; + if ssss == 0 { + return base | (len << 12); + } + if ssss > 11 || len + ssss > LOOKUP { + return base; + } + let v = ((index >> (LOOKUP - len - ssss)) & ((1 << ssss) - 1)) as i32; + let d = if v < (1 << (ssss - 1)) { v - (1 << ssss) + 1 } else { v }; + base | ((len + ssss) << 12) | ((d as i16 as u16 as u32) << 16) +} + /// A source of entropy-coded bits, first bit most significant. pub trait Bits { /// The next `n` (1..=32) bits without consuming them; zeros past the end. @@ -614,11 +661,7 @@ pub fn decode_subsampled(d: &[u8], max_samples: usize, prediction: Prediction) - Prediction::Sequential if mx == 0 && dx == 0 && dy == 0 => row_first[c], Prediction::Sequential => last[c], }; - let ssss = tables[c].decode(&mut br)?; - if ssss > 16 { - return Err(err("invalid difference category")); - } - let value = ((pred + diff_value(&mut br, ssss)) & 0xffff) as u16; + let value = ((pred + tables[c].decode_diff(&mut br, true)?) & 0xffff) as u16; plane[i] = value; last[c] = i32::from(value); if mx == 0 && dx == 0 && dy == 0 { @@ -642,6 +685,44 @@ pub fn decode_subsampled(d: &[u8], max_samples: usize, prediction: Prediction) - Ok(FrameSubsampled { width: h.width, height: h.height, vertical_subsampling: vertical, planes }) } +/// T.81 Table H.1 predictor `pred` (1..=7; 0 = none) from the left, above and above-left neighbours. +#[inline(always)] +fn predict(pred: u8, ra: i32, rb: i32, rc: i32) -> i32 { + match pred { + 0 => 0, + 1 => ra, + 2 => rb, + 3 => rc, + 4 => ra + rb - rc, + 5 => ra + ((rb - rc) >> 1), + 6 => rb + ((ra - rc) >> 1), + _ => (ra + rb) >> 1, + } +} + +/// Decode the pixels `x0..x1` (`x0 >= 1`) of one row with the fixed predictor `P`. `prev` is empty on the first +/// line, which only predicts from the left (`P = 1`). +#[inline(always)] +fn decode_run( + tables: &[&Huffman], + br: &mut BitReader<'_>, + cur: &mut [u16], + prev: &[u16], + nc: usize, + x0: usize, + x1: usize, +) -> Result<(), RawError> { + for x in x0..x1 { + for (c, table) in tables.iter().enumerate() { + let i = x * nc + c; + let ra = cur[i - nc] as i32; + let p = if P <= 1 { predict(P, ra, 0, 0) } else { predict(P, ra, prev[i] as i32, prev[i - nc] as i32) }; + cur[i] = ((p + table.decode_diff(br, false)?) & 0xffff) as u16; + } + } + Ok(()) +} + /// Decode a lossless JPEG stream. `max_samples` bounds the allocation (hostile headers). pub fn decode(d: &[u8], max_samples: usize) -> Result { let h = parse_header(d)?; @@ -680,18 +761,23 @@ pub fn decode(d: &[u8], max_samples: usize) -> Result { let (prev_rows, cur_rows) = out.split_at_mut(y * row_len); let cur = &mut cur_rows[..row_len]; let prev = if y > 0 { &prev_rows[(y - 1) * row_len..] } else { &[][..] }; - for x in 0..w { - if h.restart > 0 { - if restart_left == 0 { - if !br.restart() { - return Err(err("missing restart marker")); - } - restart_left = h.restart; - first_line = true; - reset_pending = true; + let mut x = 0; + while x < w { + if h.restart > 0 && restart_left == 0 { + if !br.restart() { + return Err(err("missing restart marker")); } - restart_left -= 1; + restart_left = h.restart; + first_line = true; + reset_pending = true; } + // pixels up to the next restart marker or the end of the row + let mut end = w; + if h.restart > 0 { + end = w.min(x + restart_left); + 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; @@ -702,24 +788,28 @@ pub fn decode(d: &[u8], max_samples: usize) -> Result { } else if x == 0 || pred == 0 { if pred == 0 { 0 } else { prev[i] as i32 } } else { - let ra = cur[i - nc] as i32; - let rb = prev[i] as i32; - let rc = prev[i - nc] as i32; - match pred { - 1 => ra, - 2 => rb, - 3 => rc, - 4 => ra + rb - rc, - 5 => ra + ((rb - rc) >> 1), - 6 => rb + ((ra - rc) >> 1), - _ => (ra + rb) >> 1, - } + predict(pred, cur[i - nc] as i32, prev[i] as i32, prev[i - nc] as i32) }; - let ssss = tables[c].decode(&mut br)?; - let diff = diff_value(&mut br, ssss); - cur[i] = ((p + diff) & mask) as u16; + cur[i] = ((p + tables[c].decode_diff(&mut br, false)?) & mask) as u16; } reset_pending = false; + // the rest of the run: the predictor is fixed, so it is chosen once here, not per sample + if first_line { + // the first line of an interval predicts from the left neighbour + decode_run::<1>(&tables, &mut br, cur, prev, nc, x + 1, end)?; + } else { + match pred { + 0 => decode_run::<0>(&tables, &mut br, cur, prev, nc, x + 1, end)?, + 1 => decode_run::<1>(&tables, &mut br, cur, prev, nc, x + 1, end)?, + 2 => decode_run::<2>(&tables, &mut br, cur, prev, nc, x + 1, end)?, + 3 => decode_run::<3>(&tables, &mut br, cur, prev, nc, x + 1, end)?, + 4 => decode_run::<4>(&tables, &mut br, cur, prev, nc, x + 1, end)?, + 5 => decode_run::<5>(&tables, &mut br, cur, prev, nc, x + 1, end)?, + 6 => decode_run::<6>(&tables, &mut br, cur, prev, nc, x + 1, end)?, + _ => decode_run::<7>(&tables, &mut br, cur, prev, nc, x + 1, end)?, + } + } + x = end; } first_line = false; if br.overrun > 64 * 8 { diff --git a/crates/raw/src/ljpeg/tests.rs b/crates/raw/src/ljpeg/tests.rs index c5c6ccf25..a77425d40 100644 --- a/crates/raw/src/ljpeg/tests.rs +++ b/crates/raw/src/ljpeg/tests.rs @@ -125,6 +125,121 @@ fn roundtrip_extremes_and_restarts() { } } +/// Re-label an encoded stream as `precision` bits with point transform `pt`: the encoder codes the samples at +/// `precision - pt` bits, which is what a transformed scan holds. +fn with_point_transform(mut enc: Vec, precision: u8, pt: u8) -> Vec { + enc[6] = precision; // SOF3 precision follows SOI, marker and length + let sos = enc.windows(2).position(|w| w == [0xff, 0xda]).unwrap(); + let seg_len = u16::from_be_bytes([enc[sos + 2], enc[sos + 3]]) as usize; + enc[sos + 2 + seg_len - 1] = pt; + enc +} + +#[test] +fn roundtrip_point_transform_all_predictors() { + for pred in 1..=7u8 { + for comps in [1usize, 2, 3] { + for (bits, pt) in [(12u8, 1u8), (14, 2), (16, 4), (8, 3)] { + let (w, h) = (11, 6); + let data = noise(w * h * comps, (bits - pt) as u32, pred as u64 * 17 + comps as u64); + let enc = with_point_transform(encode(&data, w, h, comps, bits - pt, pred, 0), bits, pt); + let f = decode(&enc, usize::MAX).unwrap(); + assert_eq!((f.precision, f.point_transform, f.predictor), (bits, pt, pred)); + let want: Vec = data.iter().map(|&v| v << pt).collect(); + assert_eq!(f.data, want, "pred {pred} comps {comps} bits {bits} pt {pt}"); + } + } + } + // with restart intervals that end mid-row + let data = noise(20 * 9 * 2, 10, 9); + for rst in [3usize, 7, 20] { + let enc = with_point_transform(encode(&data, 20, 9, 2, 10, 4, rst), 12, 2); + let want: Vec = data.iter().map(|&v| v << 2).collect(); + assert_eq!(decode(&enc, usize::MAX).unwrap().data, want, "restart {rst}"); + } +} + +#[test] +fn roundtrip_skewed_categories_with_long_codes() { + // category k with probability 2^-(k+1): the table gets codes longer than the lookup, rare 16-bit differences + let mut s = 0x9e37_79b9_7f4a_7c15u64; + let mut last = 0u32; + let data: Vec = (0..256 * 256) + .map(|_| { + s = s.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407); + let r = (s >> 32) as u32; + let cat = r.trailing_zeros().min(16); + let mag = (((s >> 8) as u32) & ((1u32 << cat) - 1)) | (1 << cat) >> 1; + let d = if s & 1 == 0 { mag } else { 0u32.wrapping_sub(mag) }; + last = last.wrapping_add(d) & 0xffff; + last as u16 + }) + .collect(); + for pred in [1u8, 4, 7] { + let enc = encode(&data, 256, 256, 1, 16, pred, 0); + assert_eq!(decode(&enc, usize::MAX).unwrap().data, data, "pred {pred}"); + } +} + +/// 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 { + 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_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; + } + // symbols in code order: by length, ties by symbol + let mut values: Vec = (0..17).collect(); + values.sort_by_key(|&v| (lens[v as usize], v)); + let sorted: Vec = values.iter().map(|&v| lens[v as usize]).collect(); + let t = Huffman::new(&counts, &values).unwrap(); + let mut s = 77u64; + let src: Vec = (0..6000) + .map(|_| { + s = s.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407); + ((s >> 40) as u8).min(0xfe) + }) + .collect(); + let (mut a, mut b) = (BitReader::new(&src), BitReader::new(&src)); + for n in 0..8000 { + 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, "symbol {n}"); + assert_eq!(a.peek(32), b.peek(32), "symbol {n}"); + if want.is_none() { + // step past the invalid code so the walk goes on + a.consume(1); + b.consume(1); + } + } + } +} + #[test] fn constant_image_single_symbol_table() { let data = vec![1234u16; 16 * 4];