✨ A downscale past 2x on an axis is an area filter, so 4K to 720p no longer aliases; 2x and under stays bilinear
Browse files at this commite1985faf90cfd7247a323c41ddf1c88a22d9b889Parent: 6a8d9901 changed file +92 -27
- crates/engine/src/scale.rs +92 -27
crates/engine/src/scale.rsmodified+92 -27
| @@ -11,13 +11,14 @@ | ||
| 11 | 11 | //! letterbox leaves over stay whatever the canvas was filled with |
| 12 | 12 | //! ([`black_i420`] is that fill). |
| 13 | 13 | //! |
| 14 | -//! The filter is bilinear in 16.16 fixed point: two taps per axis, column | |
| 15 | -//! weights precomputed once per plane, no float and no division in the pixel | |
| 16 | -//! loop. That is the honest quality level for v1 -- a downscale by more than 2x | |
| 17 | -//! aliases, because two taps cannot carry a wider kernel. The upgrade path is a | |
| 18 | -//! separable Lanczos-3 (or a box prefilter before the bilinear pass) behind the | |
| 19 | -//! same signature: nothing here exposes the tap count. 1280x720 -> 1920x1080 | |
| 20 | -//! measures 3.82 ms/frame in release (`perf_720p_to_1080p` below), against the | |
| 14 | +//! The filter is separable in 16.16 fixed point, weights precomputed once per | |
| 15 | +//! plane per axis, no float and no division in the pixel loop. An axis that | |
| 16 | +//! shrinks by no more than 2x is bilinear, two taps. An axis that shrinks by more | |
| 17 | +//! than 2x is an area (box) filter: each output sample is the coverage-weighted | |
| 18 | +//! average of the source samples its footprint overlaps, so a wide downscale does | |
| 19 | +//! not alias. The upgrade path is a wider kernel (Lanczos-3) behind the same | |
| 20 | +//! signature: nothing here exposes the tap count. 1280x720 -> 1920x1080 | |
| 21 | +//! measures 4.6 ms/frame in release (`perf_720p_to_1080p` below), against the | |
| 21 | 22 | //! 4.7 ms the BGRA conversion next door already costs per frame. |
| 22 | 23 | //! |
| 23 | 24 | //! Sample siting: each plane is resampled as its own image with centre-aligned |
| @@ -248,10 +249,11 @@ fn black_planes<T: Sample>(w: usize, h: usize) -> (Vec<T>, Vec<T>, Vec<T>) { | ||
| 248 | 249 | /// Resamples a tightly packed I420 frame (stride == width) into another one. |
| 249 | 250 | /// |
| 250 | 251 | /// Equal dimensions copy byte for byte -- a project at the media's own |
| 251 | -/// resolution is not a resample. Otherwise each plane is bilinear-filtered | |
| 252 | -/// independently, chroma at its own `(w + 1) / 2` size, so odd dimensions need | |
| 253 | -/// no special case: a 7x5 source has a 4x3 chroma plane and a 13x9 destination | |
| 254 | -/// wants a 7x5 one, and that is just another rescale. | |
| 252 | +/// resolution is not a resample. Otherwise each plane is resampled independently | |
| 253 | +/// (bilinear, or box on an axis that shrinks by more than 2x), chroma at its own | |
| 254 | +/// `(w + 1) / 2` size, so odd dimensions need no special case: a 7x5 source has | |
| 255 | +/// a 4x3 chroma plane and a 13x9 destination wants a 7x5 one, and that is just | |
| 256 | +/// another rescale. | |
| 255 | 257 | /// |
| 256 | 258 | /// Panics if a slice is not exactly its plane's size; a mis-sized buffer is a |
| 257 | 259 | /// caller bug, not a runtime condition, and every alternative silently produces |
| @@ -293,10 +295,21 @@ pub fn scale_i420<T: Sample>( | ||
| 293 | 295 | const ONE: u32 = 1 << 16; |
| 294 | 296 | const HALF: u32 = ONE / 2; |
| 295 | 297 | |
| 298 | +/// The source samples one output sample reads, and their 16.16 weights summing to [`ONE`]. | |
| 299 | +type Tap = Vec<(usize, u32)>; | |
| 300 | + | |
| 301 | +fn axis(dst_len: usize, src_len: usize) -> Vec<Tap> { | |
| 302 | + if src_len > 2 * dst_len { | |
| 303 | + box_taps(dst_len, src_len) | |
| 304 | + } else { | |
| 305 | + bilinear_taps(dst_len, src_len) | |
| 306 | + } | |
| 307 | +} | |
| 308 | + | |
| 296 | 309 | /// The two source samples and the weight of the second one, for one output |
| 297 | 310 | /// position: `src = (dst + 0.5) * src_len / dst_len - 0.5`, clamped to the |
| 298 | 311 | /// plane so the edges replicate instead of reading past it. |
| 299 | -fn taps(dst_len: usize, src_len: usize) -> Vec<(u32, u32, u32)> { | |
| 312 | +fn bilinear_taps(dst_len: usize, src_len: usize) -> Vec<Tap> { | |
| 300 | 313 | let step = (src_len as u64) * u64::from(ONE) / dst_len as u64; |
| 301 | 314 | let last = src_len as u32 - 1; |
| 302 | 315 | (0..dst_len) |
| @@ -311,31 +324,83 @@ fn taps(dst_len: usize, src_len: usize) -> Vec<(u32, u32, u32)> { | ||
| 311 | 324 | } else { |
| 312 | 325 | (pos as u32) & (ONE - 1) |
| 313 | 326 | }; |
| 314 | - (whole, (whole + 1).min(last), frac) | |
| 327 | + let next = (whole + 1).min(last); | |
| 328 | + vec![(whole as usize, ONE - frac), (next as usize, frac)] | |
| 315 | 329 | }) |
| 316 | 330 | .collect() |
| 317 | 331 | } |
| 318 | 332 | |
| 319 | -/// Bilinear resample of one tightly packed 8-bit plane. | |
| 333 | +fn box_taps(dst_len: usize, src_len: usize) -> Vec<Tap> { | |
| 334 | + let (d, s) = (dst_len as u64, src_len as u64); | |
| 335 | + (0..d) | |
| 336 | + .map(|i| { | |
| 337 | + let (a, b) = (i * s, (i + 1) * s); | |
| 338 | + let mut tap: Tap = (a / d..=(b - 1) / d) | |
| 339 | + .map(|j| { | |
| 340 | + let overlap = ((j + 1) * d).min(b) - (j * d).max(a); | |
| 341 | + (j as usize, ((overlap * u64::from(ONE) + s / 2) / s) as u32) | |
| 342 | + }) | |
| 343 | + .collect(); | |
| 344 | + let sum: i64 = tap.iter().map(|&(_, w)| i64::from(w)).sum(); | |
| 345 | + if let Some(big) = tap.iter_mut().max_by_key(|t| t.1) { | |
| 346 | + big.1 = (i64::from(big.1) + i64::from(ONE) - sum) as u32; | |
| 347 | + } | |
| 348 | + tap | |
| 349 | + }) | |
| 350 | + .collect() | |
| 351 | +} | |
| 352 | + | |
| 353 | +fn bilinear_plane<T: Sample>(src: &[T], src_w: usize, src_h: usize, dst: &mut [T], dst_w: usize, dst_h: usize) { | |
| 354 | + let cols = bilinear_taps(dst_w, src_w); | |
| 355 | + let rows = bilinear_taps(dst_h, src_h); | |
| 356 | + for (y, ry) in rows.iter().enumerate() { | |
| 357 | + let ((y0, inv_wy), (y1, wy)) = (ry[0], ry[1]); | |
| 358 | + let top = &src[y0 * src_w..][..src_w]; | |
| 359 | + let bottom = &src[y1 * src_w..][..src_w]; | |
| 360 | + let out = &mut dst[y * dst_w..][..dst_w]; | |
| 361 | + for (o, cx) in out.iter_mut().zip(cols.iter()) { | |
| 362 | + let ((x0, inv_wx), (x1, wx)) = (cx[0], cx[1]); | |
| 363 | + let a = (top[x0].code() * inv_wx + top[x1].code() * wx + HALF) >> 16; | |
| 364 | + let b = (bottom[x0].code() * inv_wx + bottom[x1].code() * wx + HALF) >> 16; | |
| 365 | + *o = T::from_code((a * inv_wy + b * wy + HALF) >> 16); | |
| 366 | + } | |
| 367 | + } | |
| 368 | +} | |
| 369 | + | |
| 370 | +fn round16(acc: u64) -> u32 { | |
| 371 | + ((acc + u64::from(HALF)) >> 16) as u32 | |
| 372 | +} | |
| 373 | + | |
| 374 | +/// Resample of one tightly packed plane: horizontal into codes at the source's | |
| 375 | +/// precision, then vertical. | |
| 320 | 376 | fn plane<T: Sample>(src: &[T], src_w: usize, src_h: usize, dst: &mut [T], dst_w: usize, dst_h: usize) { |
| 321 | 377 | if dst_w == 0 || dst_h == 0 || src_w == 0 || src_h == 0 { |
| 322 | 378 | return; |
| 323 | 379 | } |
| 324 | - let cols = taps(dst_w, src_w); | |
| 325 | - let rows = taps(dst_h, src_h); | |
| 380 | + if src_w <= 2 * dst_w && src_h <= 2 * dst_h { | |
| 381 | + return bilinear_plane(src, src_w, src_h, dst, dst_w, dst_h); | |
| 382 | + } | |
| 383 | + let cols = axis(dst_w, src_w); | |
| 384 | + let rows = axis(dst_h, src_h); | |
| 326 | 385 | |
| 327 | - for (y, &(y0, y1, wy)) in rows.iter().enumerate() { | |
| 328 | - let top = &src[y0 as usize * src_w..][..src_w]; | |
| 329 | - let bottom = &src[y1 as usize * src_w..][..src_w]; | |
| 330 | - let inv_wy = ONE - wy; | |
| 386 | + let mut mid = vec![0u32; dst_w * src_h]; | |
| 387 | + for (line, out) in src.chunks_exact(src_w).zip(mid.chunks_exact_mut(dst_w)) { | |
| 388 | + for (o, tap) in out.iter_mut().zip(&cols) { | |
| 389 | + let acc: u64 = tap | |
| 390 | + .iter() | |
| 391 | + .map(|&(x, w)| u64::from(line[x].code()) * u64::from(w)) | |
| 392 | + .sum(); | |
| 393 | + *o = round16(acc); | |
| 394 | + } | |
| 395 | + } | |
| 396 | + for (y, tap) in rows.iter().enumerate() { | |
| 331 | 397 | let out = &mut dst[y * dst_w..][..dst_w]; |
| 332 | - for (o, &(x0, x1, wx)) in out.iter_mut().zip(cols.iter()) { | |
| 333 | - let inv_wx = ONE - wx; | |
| 334 | - let (x0, x1) = (x0 as usize, x1 as usize); | |
| 335 | - // Each stage stays under 255 * 65536 + 32768, well inside u32. | |
| 336 | - let a = (top[x0].code() * inv_wx + top[x1].code() * wx + HALF) >> 16; | |
| 337 | - let b = (bottom[x0].code() * inv_wx + bottom[x1].code() * wx + HALF) >> 16; | |
| 338 | - *o = T::from_code((a * inv_wy + b * wy + HALF) >> 16); | |
| 398 | + for (x, o) in out.iter_mut().enumerate() { | |
| 399 | + let acc: u64 = tap | |
| 400 | + .iter() | |
| 401 | + .map(|&(r, w)| u64::from(mid[r * dst_w + x]) * u64::from(w)) | |
| 402 | + .sum(); | |
| 403 | + *o = T::from_code(round16(acc)); | |
| 339 | 404 | } |
| 340 | 405 | } |
| 341 | 406 | } |