From eb5844d4dad270a341fd66dee7ea1485779504bc Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sun, 11 Oct 2026 14:42:27 +0000 Subject: [PATCH] #1065: faster DNG decode and development, reduced-resolution path Problem: a 4096 x 2160 12-bit lossless-JPEG CinemaDNG frame took about 50 ms (tiles) to 115 ms (one strip) on 16 threads and about 200 ms on one, too slow for smooth 4K playback, and every frame was developed at full size even for small monitors and proxies. Fix, without changing the output (f32 bit patterns compared): - Lossless JPEG: one 13-bit lookup gives a code and its difference bits together; the bit reader takes eight bytes at once when they hold no 0xFF; per-predictor line loops. - Tiles decode in parallel by bands straight into place; a single strip covering the image is used without a copy. - Normalisation: without black-level deltas, a lookup table per CFA / black-level site replaces the per-sample float arithmetic (each entry computed by the same expression). - Demosaic, crop and colour run row by row from the normalised plane (no padded copy, no RGB intermediate), with 5 x 5 windows sliding along the five mirrored rows; the bilinear (Fast) Bayer demosaic sums in the reference order. - ProcessOptions::target_size now develops Bayer, LinearRaw and mono pictures at 1/2, 1/4 or 1/8 size (the smallest covering the target) by averaging each colour over the blocks, without demosaic. The first version is kept as `reference` (feature `writer`, tests and benchmarks only): tests/equivalence.rs checks identical raw samples and developed bits over all Bayer phases, both qualities, odd crops, active areas, linearisation, black deltas, codes above the bit depth, LinearRaw, mono, other CFAs and tiny pictures; examples/bench.rs times both interleaved at 1/4/16 threads. Best of 9 under load ~12: tiles 2.5x (1 thread) to 2.9x (16), one strip 2.1-2.6x; half size 3-4x. Co-Authored-By: Claude Opus 5.5 --- crates/raw/src/dng_lines.rs | 55 +++++++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/crates/raw/src/dng_lines.rs b/crates/raw/src/dng_lines.rs index b1c448ffc..201af3feb 100644 --- a/crates/raw/src/dng_lines.rs +++ b/crates/raw/src/dng_lines.rs @@ -234,21 +234,46 @@ pub fn read(t: &Tiff, ifd: &Ifd) -> Result { return Err(Error::Truncated); } } - #[cfg(feature = "threads")] - let decoded: Vec>> = chunks.par_iter().map(|c| decode_chunk(c, &l, t.big_endian)).collect(); - #[cfg(not(feature = "threads"))] - let decoded: Vec>> = chunks.iter().map(|c| decode_chunk(c, &l, t.big_endian)).collect(); - let mut data = vec![0u16; total]; - let row_len = l.width * spp; - for (c, d) in chunks.iter().zip(decoded) { - let d = d?; - let cw = c.w.min(l.width - c.x); - let ch = c.h.min(l.height - c.y); - for r in 0..ch { - let src = d.get(r * c.w * spp..(r * c.w + cw) * spp).ok_or(Error::Truncated)?; - let at = (c.y + r) * row_len + c.x * spp; - data.get_mut(at..at + cw * spp).ok_or(Error::Truncated)?.copy_from_slice(src); + let raw = |data| RawImage { width: l.width, height: l.height, samples_per_pixel: spp, bits: l.bits, data }; + // one chunk covering the image (a single strip): its samples are the image, no copy + if let [c] = chunks.as_slice() + && (c.x, c.y, c.w) == (0, 0, l.width) + && c.h >= l.height + { + let mut d = decode_chunk(c, &l, t.big_endian)?; + d.truncate(total); + if d.len() == total { + return Ok(raw(d)); } + return Err(Error::Truncated); } - Ok(RawImage { width: l.width, height: l.height, samples_per_pixel: spp, bits: l.bits, data }) + // bands of one row of tiles (or one strip): decoded in parallel, each band copying its own + // chunks into place + let row_len = l.width * spp; + let (Some(first), true) = (chunks.first(), row_len > 0) else { return Err(Error::Malformed("no strips or tiles")) }; + let (band_rows, across) = (first.h.max(1), l.width.div_ceil(first.w.max(1))); + let mut data = vec![0u16; total]; + let band = |k: usize, out: &mut [u16]| -> Result<()> { + let mine = chunks.get(k * across..((k + 1) * across).min(chunks.len())).unwrap_or(&[]); + #[cfg(feature = "threads")] + let decoded: Vec>> = mine.par_iter().map(|c| decode_chunk(c, &l, t.big_endian)).collect(); + #[cfg(not(feature = "threads"))] + let decoded: Vec>> = mine.iter().map(|c| decode_chunk(c, &l, t.big_endian)).collect(); + for (c, d) in mine.iter().zip(decoded) { + let d = d?; + let cw = c.w.min(l.width.saturating_sub(c.x)); + let ch = c.h.min(l.height.saturating_sub(c.y)); + for r in 0..ch { + let src = d.get(r * c.w * spp..(r * c.w + cw) * spp).ok_or(Error::Truncated)?; + let at = r * row_len + c.x * spp; + out.get_mut(at..at + cw * spp).ok_or(Error::Truncated)?.copy_from_slice(src); + } + } + Ok(()) + }; + #[cfg(feature = "threads")] + data.par_chunks_mut(band_rows * row_len).enumerate().try_for_each(|(k, out)| band(k, out))?; + #[cfg(not(feature = "threads"))] + data.chunks_mut(band_rows * row_len).enumerate().try_for_each(|(k, out)| band(k, out))?; + Ok(raw(data)) }