diff --git a/crates/engine/src/scale.rs b/crates/engine/src/scale.rs index ccdd3e9..0ff94b4 100644 --- a/crates/engine/src/scale.rs +++ b/crates/engine/src/scale.rs @@ -11,13 +11,14 @@ //! letterbox leaves over stay whatever the canvas was filled with //! ([`black_i420`] is that fill). //! -//! The filter is bilinear in 16.16 fixed point: two taps per axis, column -//! weights precomputed once per plane, no float and no division in the pixel -//! loop. That is the honest quality level for v1 -- a downscale by more than 2x -//! aliases, because two taps cannot carry a wider kernel. The upgrade path is a -//! separable Lanczos-3 (or a box prefilter before the bilinear pass) behind the -//! same signature: nothing here exposes the tap count. 1280x720 -> 1920x1080 -//! measures 3.82 ms/frame in release (`perf_720p_to_1080p` below), against the +//! The filter is separable in 16.16 fixed point, weights precomputed once per +//! plane per axis, no float and no division in the pixel loop. An axis that +//! shrinks by no more than 2x is bilinear, two taps. An axis that shrinks by more +//! than 2x is an area (box) filter: each output sample is the coverage-weighted +//! average of the source samples its footprint overlaps, so a wide downscale does +//! not alias. The upgrade path is a wider kernel (Lanczos-3) behind the same +//! signature: nothing here exposes the tap count. 1280x720 -> 1920x1080 +//! measures 4.6 ms/frame in release (`perf_720p_to_1080p` below), against the //! 4.7 ms the BGRA conversion next door already costs per frame. //! //! Sample siting: each plane is resampled as its own image with centre-aligned @@ -248,10 +249,11 @@ fn black_planes(w: usize, h: usize) -> (Vec, Vec, Vec) { /// Resamples a tightly packed I420 frame (stride == width) into another one. /// /// Equal dimensions copy byte for byte -- a project at the media's own -/// resolution is not a resample. Otherwise each plane is bilinear-filtered -/// independently, chroma at its own `(w + 1) / 2` size, so odd dimensions need -/// no special case: a 7x5 source has a 4x3 chroma plane and a 13x9 destination -/// wants a 7x5 one, and that is just another rescale. +/// resolution is not a resample. Otherwise each plane is resampled independently +/// (bilinear, or box on an axis that shrinks by more than 2x), chroma at its own +/// `(w + 1) / 2` size, so odd dimensions need no special case: a 7x5 source has +/// a 4x3 chroma plane and a 13x9 destination wants a 7x5 one, and that is just +/// another rescale. /// /// Panics if a slice is not exactly its plane's size; a mis-sized buffer is a /// caller bug, not a runtime condition, and every alternative silently produces @@ -293,10 +295,21 @@ pub fn scale_i420( const ONE: u32 = 1 << 16; const HALF: u32 = ONE / 2; +/// The source samples one output sample reads, and their 16.16 weights summing to [`ONE`]. +type Tap = Vec<(usize, u32)>; + +fn axis(dst_len: usize, src_len: usize) -> Vec { + if src_len > 2 * dst_len { + box_taps(dst_len, src_len) + } else { + bilinear_taps(dst_len, src_len) + } +} + /// The two source samples and the weight of the second one, for one output /// position: `src = (dst + 0.5) * src_len / dst_len - 0.5`, clamped to the /// plane so the edges replicate instead of reading past it. -fn taps(dst_len: usize, src_len: usize) -> Vec<(u32, u32, u32)> { +fn bilinear_taps(dst_len: usize, src_len: usize) -> Vec { let step = (src_len as u64) * u64::from(ONE) / dst_len as u64; let last = src_len as u32 - 1; (0..dst_len) @@ -311,31 +324,83 @@ fn taps(dst_len: usize, src_len: usize) -> Vec<(u32, u32, u32)> { } else { (pos as u32) & (ONE - 1) }; - (whole, (whole + 1).min(last), frac) + let next = (whole + 1).min(last); + vec![(whole as usize, ONE - frac), (next as usize, frac)] }) .collect() } -/// Bilinear resample of one tightly packed 8-bit plane. +fn box_taps(dst_len: usize, src_len: usize) -> Vec { + let (d, s) = (dst_len as u64, src_len as u64); + (0..d) + .map(|i| { + let (a, b) = (i * s, (i + 1) * s); + let mut tap: Tap = (a / d..=(b - 1) / d) + .map(|j| { + let overlap = ((j + 1) * d).min(b) - (j * d).max(a); + (j as usize, ((overlap * u64::from(ONE) + s / 2) / s) as u32) + }) + .collect(); + let sum: i64 = tap.iter().map(|&(_, w)| i64::from(w)).sum(); + if let Some(big) = tap.iter_mut().max_by_key(|t| t.1) { + big.1 = (i64::from(big.1) + i64::from(ONE) - sum) as u32; + } + tap + }) + .collect() +} + +fn bilinear_plane(src: &[T], src_w: usize, src_h: usize, dst: &mut [T], dst_w: usize, dst_h: usize) { + let cols = bilinear_taps(dst_w, src_w); + let rows = bilinear_taps(dst_h, src_h); + for (y, ry) in rows.iter().enumerate() { + let ((y0, inv_wy), (y1, wy)) = (ry[0], ry[1]); + let top = &src[y0 * src_w..][..src_w]; + let bottom = &src[y1 * src_w..][..src_w]; + let out = &mut dst[y * dst_w..][..dst_w]; + for (o, cx) in out.iter_mut().zip(cols.iter()) { + let ((x0, inv_wx), (x1, wx)) = (cx[0], cx[1]); + let a = (top[x0].code() * inv_wx + top[x1].code() * wx + HALF) >> 16; + let b = (bottom[x0].code() * inv_wx + bottom[x1].code() * wx + HALF) >> 16; + *o = T::from_code((a * inv_wy + b * wy + HALF) >> 16); + } + } +} + +fn round16(acc: u64) -> u32 { + ((acc + u64::from(HALF)) >> 16) as u32 +} + +/// Resample of one tightly packed plane: horizontal into codes at the source's +/// precision, then vertical. fn plane(src: &[T], src_w: usize, src_h: usize, dst: &mut [T], dst_w: usize, dst_h: usize) { if dst_w == 0 || dst_h == 0 || src_w == 0 || src_h == 0 { return; } - let cols = taps(dst_w, src_w); - let rows = taps(dst_h, src_h); + if src_w <= 2 * dst_w && src_h <= 2 * dst_h { + return bilinear_plane(src, src_w, src_h, dst, dst_w, dst_h); + } + let cols = axis(dst_w, src_w); + let rows = axis(dst_h, src_h); - for (y, &(y0, y1, wy)) in rows.iter().enumerate() { - let top = &src[y0 as usize * src_w..][..src_w]; - let bottom = &src[y1 as usize * src_w..][..src_w]; - let inv_wy = ONE - wy; + let mut mid = vec![0u32; dst_w * src_h]; + for (line, out) in src.chunks_exact(src_w).zip(mid.chunks_exact_mut(dst_w)) { + for (o, tap) in out.iter_mut().zip(&cols) { + let acc: u64 = tap + .iter() + .map(|&(x, w)| u64::from(line[x].code()) * u64::from(w)) + .sum(); + *o = round16(acc); + } + } + for (y, tap) in rows.iter().enumerate() { let out = &mut dst[y * dst_w..][..dst_w]; - for (o, &(x0, x1, wx)) in out.iter_mut().zip(cols.iter()) { - let inv_wx = ONE - wx; - let (x0, x1) = (x0 as usize, x1 as usize); - // Each stage stays under 255 * 65536 + 32768, well inside u32. - let a = (top[x0].code() * inv_wx + top[x1].code() * wx + HALF) >> 16; - let b = (bottom[x0].code() * inv_wx + bottom[x1].code() * wx + HALF) >> 16; - *o = T::from_code((a * inv_wy + b * wy + HALF) >> 16); + for (x, o) in out.iter_mut().enumerate() { + let acc: u64 = tap + .iter() + .map(|&(r, w)| u64::from(mid[r * dst_w + x]) * u64::from(w)) + .sum(); + *o = T::from_code(round16(acc)); } } }