FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
M3: NVENC Main 10 review fixes: saturating plane sizes, as_chunks for clippy
Brandon Thomasafd5a6b34794
diff --git a/crates/export/src/lib.rs b/crates/export/src/lib.rsindex b2cbd2b1c..98de260c4 100644--- a/crates/export/src/lib.rs+++ b/crates/export/src/lib.rs@@ -1229,8 +1229,7 @@ pub fn rgbf_to_yuv420_10(rgb: &[f32], w: usize, h: usize, kr: f32, kb: f32, y: &for (dy, yrow) in yr.chunks_exact_mut(w).enumerate().take(rows) {let row = cy * 2 + dy;let Some(src) = rgb.get(row * w * 3..(row + 1) * w * 3) else { continue };- for (x, (p, yo)) in src.chunks_exact(3).zip(yrow.iter_mut()).enumerate() {- let &[r, g, b] = p else { continue };+ for (x, (&[r, g, b], yo)) in src.as_chunks::<3>().0.iter().zip(yrow.iter_mut()).enumerate() {let (r, g, b) = (unit(r), unit(g), unit(b));let yy = kr * r + kg * g + kb * b;*yo = code(64.0 + 876.0 * yy);diff --git a/crates/platform/src/nvenc/mod.rs b/crates/platform/src/nvenc/mod.rsindex 7448045cb..3a960c9e6 100644--- a/crates/platform/src/nvenc/mod.rs+++ b/crates/platform/src/nvenc/mod.rs@@ -348,7 +348,8 @@ impl Nvenc {return Err("an 8-bit picture for a Main 10 encoder".into());}let (w, h) = (self.size.0 as usize, self.size.1 as usize);- if y.len() < w * h || u.len() < w / 2 * (h / 2) || v.len() < w / 2 * (h / 2) {+ let (luma, chroma) = (w.saturating_mul(h), (w / 2).saturating_mul(h / 2));+ if y.len() < luma || u.len() < chroma || v.len() < chroma {return Err("the picture is smaller than the encoder's size".into());}self.submit_picture(index, |l| fill_nv12(l, y, u, v, w, h))@@ -361,7 +362,8 @@ impl Nvenc {return Err("a 10-bit picture for a Main (8-bit) encoder".into());}let (w, h) = (self.size.0 as usize, self.size.1 as usize);- if y.len() < w * h || u.len() < w / 2 * (h / 2) || v.len() < w / 2 * (h / 2) {+ let (luma, chroma) = (w.saturating_mul(h), (w / 2).saturating_mul(h / 2));+ if y.len() < luma || u.len() < chroma || v.len() < chroma {return Err("the picture is smaller than the encoder's size".into());}self.submit_picture(index, |l| fill_p010(l, y, u, v, w, h))@@ -457,11 +459,11 @@ fn fill_p010(l: Locked<'_>, y: &[u16], u: &[u16], v: &[u16], w: usize, h: usize)if w == 0 || pitch == 0 {return;}- let put = |dst: &mut [u8], code: u16| dst.copy_from_slice(&((code & 0x3ff) << 6).to_le_bytes());+ let code = |c: u16| ((c & 0x3ff) << 6).to_le_bytes();for (row, dst) in y.chunks_exact(w).zip(luma.chunks_exact_mut(pitch)).take(h) {let Some(dst) = dst.get_mut(..w.saturating_mul(2)) else { continue };- for (d, c) in dst.chunks_exact_mut(2).zip(row) {- put(d, *c);+ for (d, c) in dst.as_chunks_mut::<2>().0.iter_mut().zip(row) {+ *d = code(*c);}}let cw = w / 2;@@ -470,10 +472,9 @@ fn fill_p010(l: Locked<'_>, y: &[u16], u: &[u16], v: &[u16], w: usize, h: usize)}for ((ur, vr), dst) in u.chunks_exact(cw).zip(v.chunks_exact(cw)).zip(chroma.chunks_exact_mut(pitch)).take(h / 2) {let Some(dst) = dst.get_mut(..w.saturating_mul(2)) else { continue };- for ((d, a), b) in dst.chunks_exact_mut(4).zip(ur).zip(vr) {- let (du, dv) = d.split_at_mut(2);- put(du, *a);- put(dv, *b);+ for ((d, a), b) in dst.as_chunks_mut::<4>().0.iter_mut().zip(ur).zip(vr) {+ let ([u0, u1], [v0, v1]) = (code(*a), code(*b));+ *d = [u0, u1, v0, v1];}}}diff --git a/crates/platform/tests/nvenc_hevc_hdr_export.rs b/crates/platform/tests/nvenc_hevc_hdr_export.rsindex f30b57fce..8acbc1cad 100644--- a/crates/platform/tests/nvenc_hevc_hdr_export.rs+++ b/crates/platform/tests/nvenc_hevc_hdr_export.rs@@ -114,7 +114,7 @@ fn assert_ffmpeg_decodes(ffmpeg: &Path, path: &str) {fn ffmpeg_first_picture(ffmpeg: &Path, path: &str, w: usize, h: usize) -> (Vec<u16>, Vec<u16>, Vec<u16>) {let out = Command::new(ffmpeg).args(["-v", "error", "-i", path, "-frames:v", "1", "-f", "rawvideo", "-pix_fmt", "yuv420p10le", "-"]).output().unwrap();assert!(out.status.success(), "ffmpeg: {}", String::from_utf8_lossy(&out.stderr));- let samples: Vec<u16> = out.stdout.chunks_exact(2).map(|b| u16::from_le_bytes([b[0], b[1]])).collect();+ let samples: Vec<u16> = out.stdout.as_chunks::<2>().0.iter().map(|b| u16::from_le_bytes(*b)).collect();assert_eq!(samples.len(), w * h * 3 / 2, "one yuv420p10le picture");let (y, c) = samples.split_at(w * h);let (u, v) = c.split_at(c.len() / 2);