Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

frame: 10-bit Y'CbCr normalisation through per-code tables (#3216)

Balogh Zsoltb0ee6f143295
diff --git a/crates/frame/src/lib.rs b/crates/frame/src/lib.rs
index 6548ebe2c..9c8e643a2 100644
--- a/crates/frame/src/lib.rs
+++ b/crates/frame/src/lib.rs
@@ -442,6 +442,13 @@ impl VideoFrame {
let cw = w.div_ceil(1 << sx);
let bits = *bits;
let amax = ((1u32 << bits) - 1) as f32;
+ // Per-code tables (exactly the values `normalize_*` returns) instead of two
+ // divisions per sample; a code past the table (a corrupt plane) is computed.
+ let codes = if bits <= 12 { 1usize << bits } else { 0 };
+ let ytab: Vec<f32> = (0..codes as u32).map(|v| normalize_y(v, bits, info.range)).collect();
+ let ctab: Vec<f32> = (0..codes as u32).map(|v| normalize_c(v, bits, info.range)).collect();
+ let ny = |v: u16| ytab.get(usize::from(v)).copied().unwrap_or_else(|| normalize_y(u32::from(v), bits, info.range));
+ let nc = |v: u16| ctab.get(usize::from(v)).copied().unwrap_or_else(|| normalize_c(u32::from(v), bits, info.range));
out.par_chunks_mut(r.w * 4).enumerate().for_each(|(ry, row)| {
let oy = r.y + ry;
for rx in 0..r.w {
@@ -453,9 +460,9 @@ impl VideoFrame {
for dx in 0..n {
let x = (ox * n + dx).min(w - 1);
let cx = x >> sx;
- let yy = normalize_y(planes[0][y * w + x] as u32, bits, info.range);
- let u = normalize_c(planes[1][cy * cw + cx] as u32, bits, info.range);
- let v = normalize_c(planes[2][cy * cw + cx] as u32, bits, info.range);
+ let yy = ny(planes[0][y * w + x]);
+ let u = nc(planes[1][cy * cw + cx]);
+ let v = nc(planes[2][cy * cw + cx]);
let rgb = ycbcr_to_rgb(yy, u, v, info.matrix);
let a = alpha.as_ref().map_or(1.0, |al| al[y * w + x] as f32 / amax);
acc[0] += q(rgb[0]) * a;