diff --git a/crates/engine/src/color.rs b/crates/engine/src/color.rs new file mode 100644 index 0000000..e9e28fa --- /dev/null +++ b/crates/engine/src/color.rs @@ -0,0 +1,287 @@ +//! Per-segment colour transform, applied engine-side on the decoded YUV samples +//! so that playback and export produce the same picture from the same numbers. +//! +//! The maths lives on YUV rather than RGB because every path already has YUV in +//! hand -- the decoders hand back I420 and the encoders take it -- and because +//! the four controls are each a straight per-sample affine there: +//! +//! * brightness and contrast move Y alone (contrast pivots on limited-range mid +//! grey, `(16 + 235) / 2`; brightness shifts by a fraction of the 219-code +//! luma range, so `1.0` lifts black to white), +//! * saturation scales U and V about 128, so `0.0` is exactly grey, +//! * tint is a warm/cool *temperature* axis, not a magenta/green one: U carries +//! blue-difference and V red-difference, so warm (positive) pushes V up and U +//! down by the same amount. A hue rotation would need a 2x2 matrix on (U,V) +//! and buys nothing a colourist asks for by name. +//! +//! Every control therefore collapses into three 256-entry lookup tables built +//! once per call, and the pixel loop is a byte indexing a table -- no float per +//! pixel, no chance of a `u8` wrap. Identity params short-circuit before the +//! tables exist, which is what makes "no colour set" byte-identical rather than +//! merely close. + +/// Limited-range mid grey; contrast pivots here so a change does not also +/// shift the picture's overall level. +const MID_Y: f32 = 125.5; +/// Codes between limited-range black (16) and white (235). +const LUMA_RANGE: f32 = 219.0; +/// Chroma codes a full-warm or full-cool tint moves U and V by. Picked so the +/// extreme is a strong grade rather than a broken picture. +const TINT_RANGE: f32 = 32.0; + +/// One segment's colour setting. Plain data with no invariants: out-of-range or +/// non-finite values are clamped where the tables are built, so a deserializer +/// can hand this over unchecked. +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct ColorParams { + /// -1..1, 0 = identity. 1.0 lifts limited-range black to white. + pub brightness: f32, + /// 0..2, 1 = identity. 0 flattens the picture to mid grey. + pub contrast: f32, + /// 0..2, 1 = identity. 0 is greyscale, 2 is doubled chroma. + pub saturation: f32, + /// -1..1, 0 = identity. Positive is warm (more red, less blue). + pub tint: f32, +} + +impl Default for ColorParams { + fn default() -> Self { + Self { + brightness: 0.0, + contrast: 1.0, + saturation: 1.0, + tint: 0.0, + } + } +} + +impl ColorParams { + /// Whether this setting is the do-nothing one. Compared exactly on purpose: + /// anything that is not the identity value goes through the transform, and + /// only a `true` here is allowed to skip it. + pub fn is_identity(&self) -> bool { + self.brightness == 0.0 && self.contrast == 1.0 && self.saturation == 1.0 && self.tint == 0.0 + } +} + +/// The three per-plane tables. Built once per frame (768 float ops against ~3 M +/// samples at 1080p, so the build never shows up in a measurement). +pub(crate) struct Lut { + pub(crate) y: [u8; 256], + pub(crate) u: [u8; 256], + pub(crate) v: [u8; 256], +} + +impl Lut { + pub(crate) fn new(params: &ColorParams) -> Self { + let brightness = sane(params.brightness, 0.0).clamp(-1.0, 1.0) * LUMA_RANGE; + let contrast = sane(params.contrast, 1.0).clamp(0.0, 2.0); + let saturation = sane(params.saturation, 1.0).clamp(0.0, 2.0); + let tint = sane(params.tint, 0.0).clamp(-1.0, 1.0) * TINT_RANGE; + + let mut lut = Self { + y: [0; 256], + u: [0; 256], + v: [0; 256], + }; + for code in 0..256 { + let f = code as f32; + // Clamped to the full 0..255 swing, not to 16..235: content that is + // already blacker than black or whiter than white keeps whatever a + // grade leaves of it instead of being crushed by this pass. + lut.y[code] = byte((f - MID_Y) * contrast + MID_Y + brightness); + lut.u[code] = byte((f - 128.0) * saturation + 128.0 - tint); + lut.v[code] = byte((f - 128.0) * saturation + 128.0 + tint); + } + lut + } +} + +/// A parameter that survived a file or a UI edit; NaN and infinity fall back to +/// the identity value rather than blanking the picture. +fn sane(value: f32, fallback: f32) -> f32 { + if value.is_finite() { value } else { fallback } +} + +fn byte(value: f32) -> u8 { + value.round().clamp(0.0, 255.0) as u8 +} + +/// Applies `params` in place to one planar 8-bit frame. +/// +/// Per-sample, so the plane layout is free: I420 with `stride == width` (what +/// [`crate::convert`] and the export path use) or any other planar 8-bit YUV, +/// padding included. Interleaved chroma (NV12) is *not* this function's frame. +/// +/// Identity params return without touching a byte. +pub fn apply_yuv(params: &ColorParams, y: &mut [u8], u: &mut [u8], v: &mut [u8]) { + if params.is_identity() { + return; + } + let lut = Lut::new(params); + for s in y.iter_mut() { + *s = lut.y[*s as usize]; + } + for s in u.iter_mut() { + *s = lut.u[*s as usize]; + } + for s in v.iter_mut() { + *s = lut.v[*s as usize]; + } +} + +#[cfg(test)] +mod tests { + use super::*; + + /// A gradient frame with every luma code and a chroma sweep, so a test that + /// walks it has seen the whole table. + fn frame(w: usize, h: usize) -> (Vec, Vec, Vec) { + let y = (0..w * h).map(|i| (i % 256) as u8).collect(); + let cw = w.div_ceil(2) * h.div_ceil(2); + let u = (0..cw).map(|i| (i % 256) as u8).collect(); + let v = (0..cw).map(|i| (255 - i % 256) as u8).collect(); + (y, u, v) + } + + /// The charter invariant: no colour set means the exact same bytes, not + /// bytes that round back to the same picture. + #[test] + fn identity_params_leave_every_byte_alone() { + let (y, u, v) = frame(64, 48); + let (mut gy, mut gu, mut gv) = (y.clone(), u.clone(), v.clone()); + apply_yuv(&ColorParams::default(), &mut gy, &mut gu, &mut gv); + assert_eq!((gy, gu, gv), (y, u, v)); + } + + #[test] + fn zero_saturation_is_grey_everywhere() { + let (mut y, mut u, mut v) = frame(64, 48); + let before = y.clone(); + apply_yuv( + &ColorParams { + saturation: 0.0, + ..Default::default() + }, + &mut y, + &mut u, + &mut v, + ); + assert_eq!(y, before, "saturation must not touch luma"); + assert!(u.iter().chain(v.iter()).all(|&c| c == 128), "not grey"); + } + + /// Every extreme, on every code, stays inside `u8` -- a wrap would show as + /// black specks in a blown highlight. + #[test] + fn extremes_clamp_instead_of_wrapping() { + let extremes = [-1.0f32, 1.0, -100.0, 100.0, f32::NAN, f32::INFINITY]; + for b in extremes { + for c in [0.0f32, 2.0, 50.0] { + let lut = Lut::new(&ColorParams { + brightness: b, + contrast: c, + saturation: c, + tint: b, + }); + // A wrap shows up as a curve that falls: every one of these is + // non-decreasing in the input code, saturated ends included. + assert!( + lut.y.windows(2).all(|w| w[0] <= w[1]), + "Y wrapped at {b}/{c}" + ); + assert!( + lut.u.windows(2).all(|w| w[0] <= w[1]), + "U wrapped at {b}/{c}" + ); + assert!( + lut.v.windows(2).all(|w| w[0] <= w[1]), + "V wrapped at {b}/{c}" + ); + } + } + // The two saturating ends specifically: +219 on code 255 is 474, which + // a wrapping cast would hand back as 218. + let lut = Lut::new(&ColorParams { + brightness: 1.0, + ..Default::default() + }); + assert_eq!(lut.y[255], 255, "full brightness must saturate, not wrap"); + assert_eq!(lut.y[0], 219, "black lifted by the whole luma range"); + let lut = Lut::new(&ColorParams { + brightness: -1.0, + ..Default::default() + }); + assert_eq!(lut.y[255], 36, "255 - 219"); + } + + #[test] + fn brightness_is_monotonic_in_mean_luma() { + let (y0, u0, v0) = frame(64, 48); + let mut last = 0.0; + for step in 0..=8 { + let (mut y, mut u, mut v) = (y0.clone(), u0.clone(), v0.clone()); + apply_yuv( + &ColorParams { + brightness: -0.5 + step as f32 * 0.125, + ..Default::default() + }, + &mut y, + &mut u, + &mut v, + ); + let mean = y.iter().map(|&s| s as f64).sum::() / y.len() as f64; + assert!(mean >= last, "brighter setting darkened the frame"); + last = mean; + } + } + + /// The tables are the float maths, sampled: this is what lets the pixel loop + /// be a byte lookup. Same formula, computed per sample, must agree exactly. + #[test] + fn tables_agree_with_the_float_maths() { + let params = ColorParams { + brightness: 0.13, + contrast: 1.4, + saturation: 0.7, + tint: -0.35, + }; + let lut = Lut::new(¶ms); + for code in 0..256 { + let f = code as f32; + let y = ((f - MID_Y) * 1.4 + MID_Y + 0.13 * LUMA_RANGE) + .round() + .clamp(0.0, 255.0) as u8; + let u = ((f - 128.0) * 0.7 + 128.0 + 0.35 * TINT_RANGE) + .round() + .clamp(0.0, 255.0) as u8; + let v = ((f - 128.0) * 0.7 + 128.0 - 0.35 * TINT_RANGE) + .round() + .clamp(0.0, 255.0) as u8; + assert!(lut.y[code].abs_diff(y) <= 1, "Y {code}"); + assert!(lut.u[code].abs_diff(u) <= 1, "U {code}"); + assert!(lut.v[code].abs_diff(v) <= 1, "V {code}"); + } + } + + /// Not asserted: a debug build is an order of magnitude off and the number + /// only means something in release. Run with + /// `cargo test -p engine --release color:: -- --nocapture`. + #[test] + fn perf_1080p() { + let (mut y, mut u, mut v) = frame(1920, 1080); + let params = ColorParams { + brightness: 0.1, + contrast: 1.2, + saturation: 1.3, + tint: 0.2, + }; + let runs = 30; + let t = std::time::Instant::now(); + for _ in 0..runs { + apply_yuv(¶ms, &mut y, &mut u, &mut v); + } + let ms = t.elapsed().as_secs_f64() * 1000.0 / f64::from(runs); + println!("color::apply_yuv 1920x1080: {ms:.3} ms/frame"); + } +} diff --git a/crates/engine/src/convert.rs b/crates/engine/src/convert.rs index 739120c..67e8ede 100644 --- a/crates/engine/src/convert.rs +++ b/crates/engine/src/convert.rs @@ -29,9 +29,134 @@ pub fn i420_to_bgra(y: &[u8], u: &[u8], v: &[u8], width: usize, height: usize) - out } +/// The same conversion with a segment's colour grade folded in: the grade is +/// three table lookups per pixel inside the loop that was reading those samples +/// anyway, and measured free: 4.73 ms/frame against 4.74 for the ungraded +/// conversion, where grading a scratch copy of the planes first costs a further +/// 0.61 (1920x1080, release, `perf_1080p` below). +/// +/// Identity params take the ungraded loop above, byte for byte. +pub fn i420_to_bgra_with( + params: &crate::color::ColorParams, + y: &[u8], + u: &[u8], + v: &[u8], + width: usize, + height: usize, +) -> Vec { + if params.is_identity() { + return i420_to_bgra(y, u, v, width, height); + } + let lut = crate::color::Lut::new(params); + let cw = width.div_ceil(2); + let mut out = vec![0u8; width * height * 4]; + + for row in 0..height { + let y_row = row * width; + let c_row = (row / 2) * cw; + for col in 0..width { + let c = lut.y[y[y_row + col] as usize] as i32 - 16; + let ci = c_row + col / 2; + let d = lut.u[u[ci] as usize] as i32 - 128; + let e = lut.v[v[ci] as usize] as i32 - 128; + + let r = ((298 * c + 409 * e + 128) >> 8).clamp(0, 255) as u8; + let g = ((298 * c - 100 * d - 208 * e + 128) >> 8).clamp(0, 255) as u8; + let b = ((298 * c + 516 * d + 128) >> 8).clamp(0, 255) as u8; + + let o = (y_row + col) * 4; + out[o] = b; + out[o + 1] = g; + out[o + 2] = r; + out[o + 3] = 255; + } + } + out +} + #[cfg(test)] mod tests { use super::*; + use crate::color::ColorParams; + + /// The render path's half of the charter invariant: an ungraded segment is + /// the untouched converter, not a grade that happens to round back. + #[test] + fn identity_grade_converts_identically() { + let y: Vec = (0..64 * 48).map(|i| (i % 256) as u8).collect(); + let u: Vec = (0..32 * 24).map(|i| (i % 256) as u8).collect(); + let v: Vec = (0..32 * 24).map(|i| (255 - i % 256) as u8).collect(); + assert_eq!( + i420_to_bgra_with(&ColorParams::default(), &y, &u, &v, 64, 48), + i420_to_bgra(&y, &u, &v, 64, 48) + ); + } + + /// Fused must equal grading the planes first and converting after -- that is + /// what makes the export path (which grades a scratch copy before the + /// encoder) and the render path show the same picture. + #[test] + fn fused_matches_grade_then_convert() { + let params = ColorParams { + brightness: 0.1, + contrast: 1.3, + saturation: 0.4, + tint: 0.25, + }; + let mut y: Vec = (0..64 * 48).map(|i| (i % 256) as u8).collect(); + let mut u: Vec = (0..32 * 24).map(|i| (i % 256) as u8).collect(); + let mut v: Vec = (0..32 * 24).map(|i| (255 - i % 256) as u8).collect(); + let fused = i420_to_bgra_with(¶ms, &y, &u, &v, 64, 48); + crate::color::apply_yuv(¶ms, &mut y, &mut u, &mut v); + assert_eq!(fused, i420_to_bgra(&y, &u, &v, 64, 48)); + } + + /// Not asserted, printed: the numbers in `i420_to_bgra_with`'s docs come + /// from here. `cargo test -p engine --release convert:: -- --nocapture`. + #[test] + fn perf_1080p() { + let (w, h) = (1920usize, 1080usize); + let y: Vec = (0..w * h).map(|i| (i % 256) as u8).collect(); + let u: Vec = (0..w / 2 * h / 2).map(|i| (i % 256) as u8).collect(); + let v: Vec = (0..w / 2 * h / 2).map(|i| (255 - i % 256) as u8).collect(); + let params = ColorParams { + brightness: 0.1, + contrast: 1.2, + saturation: 1.3, + tint: 0.2, + }; + let runs = 20; + let bench = |name: &str, mut f: Box| { + let t = std::time::Instant::now(); + for _ in 0..runs { + f(); + } + println!( + "{name} 1920x1080: {:.3} ms/frame", + t.elapsed().as_secs_f64() * 1000.0 / f64::from(runs) + ); + }; + bench( + "i420_to_bgra ", + Box::new(|| { + std::hint::black_box(i420_to_bgra(&y, &u, &v, w, h)); + }), + ); + bench( + "i420_to_bgra_with ", + Box::new(|| { + std::hint::black_box(i420_to_bgra_with(¶ms, &y, &u, &v, w, h)); + }), + ); + bench( + "copy + apply_yuv ", + Box::new(|| { + let (mut y, mut u, mut v) = (y.clone(), u.clone(), v.clone()); + crate::color::apply_yuv(¶ms, &mut y, &mut u, &mut v); + std::hint::black_box((y, u, v)); + }), + ); + } #[test] fn known_colors() { diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 3a9faa0..d3e7f32 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -3,6 +3,7 @@ pub mod ao; pub mod audio; pub mod clock; +pub mod color; pub mod convert; pub mod decode; pub mod demux;