diff --git a/crates/engine/src/convert.rs b/crates/engine/src/convert.rs index 833a3bc..3d8a876 100644 --- a/crates/engine/src/convert.rs +++ b/crates/engine/src/convert.rs @@ -63,43 +63,109 @@ impl Coeffs { } } -/// Converts a planar I420 frame (stride == width, chroma at half resolution) -/// into tightly packed BGRA8. Returns `width * height * 4` bytes. -pub fn i420_to_bgra( - color: &ColorDescription, +/// One row band's worth of the conversion: `rows` are absolute row indices +/// into `y`/`u`/`v` (chroma read at half resolution as ever), written into +/// `out` from its own start (`out[0]` is the first row in `rows`). Pulled out +/// of `i420_to_bgra`/`i420_to_bgra_with` so both can run it across disjoint +/// row ranges on separate threads -- each row is independent (chroma only +/// ever reads `row / 2`, never a neighbouring output row), so splitting the +/// row loop changes nothing about what a pixel comes out as. +fn convert_rows( + k: &Coeffs, + lut: Option<&crate::color::Lut>, y: &[u8], u: &[u8], v: &[u8], width: usize, - height: usize, -) -> Vec { - let k = Coeffs::new(color); - let cw = width.div_ceil(2); - let mut out = vec![0u8; width * height * 4]; - - for row in 0..height { + cw: usize, + rows: std::ops::Range, + out: &mut [u8], +) { + for (i, row) in rows.enumerate() { let y_row = row * width; let c_row = (row / 2) * cw; + let o_row = i * width * 4; for col in 0..width { - let c = y[y_row + col] as i32 - k.y_off; let ci = c_row + col / 2; - let d = u[ci] as i32 - 128; - let e = v[ci] as i32 - 128; + let (c, d, e) = match lut { + Some(lut) => ( + lut.y[y[y_row + col] as usize] as i32 - k.y_off, + lut.u[u[ci] as usize] as i32 - 128, + lut.v[v[ci] as usize] as i32 - 128, + ), + None => ( + y[y_row + col] as i32 - k.y_off, + u[ci] as i32 - 128, + v[ci] as i32 - 128, + ), + }; let r = ((k.y * c + k.rv * e + 128) >> 8).clamp(0, 255) as u8; let g = ((k.y * c - k.gu * d - k.gv * e + 128) >> 8).clamp(0, 255) as u8; let b = ((k.y * c + k.bu * d + 128) >> 8).clamp(0, 255) as u8; - let o = (y_row + col) * 4; + let o = o_row + col * 4; out[o] = b; out[o + 1] = g; out[o + 2] = r; out[o + 3] = 255; } } +} + +/// `convert_rows`, fanned out over as many threads as the machine has: rows +/// split into one contiguous band per lane (`std::thread::scope`, the same +/// pattern `HevcEnc::code_batch` uses, borrowing the planes rather than +/// copying them), each lane writing straight into its own slice of `out`. A +/// frame too short to be worth the spawn (or a machine that only reports one +/// core) falls back to running the whole thing on the calling thread. +fn convert_parallel( + k: &Coeffs, + lut: Option<&crate::color::Lut>, + y: &[u8], + u: &[u8], + v: &[u8], + width: usize, + height: usize, +) -> Vec { + let cw = width.div_ceil(2); + let mut out = vec![0u8; width * height * 4]; + let lanes = std::thread::available_parallelism() + .map_or(1, |n| n.get()) + .min(height); + if lanes <= 1 { + convert_rows(k, lut, y, u, v, width, cw, 0..height, &mut out); + return out; + } + let rows_per_lane = height.div_ceil(lanes); + std::thread::scope(|scope| { + let mut rest = &mut out[..]; + let mut start = 0; + while start < height { + let end = (start + rows_per_lane).min(height); + let (chunk, remainder) = rest.split_at_mut((end - start) * width * 4); + rest = remainder; + scope.spawn(move || convert_rows(k, lut, y, u, v, width, cw, start..end, chunk)); + start = end; + } + }); out } +/// Converts a planar I420 frame (stride == width, chroma at half resolution) +/// into tightly packed BGRA8. Returns `width * height * 4` bytes. +pub fn i420_to_bgra( + color: &ColorDescription, + y: &[u8], + u: &[u8], + v: &[u8], + width: usize, + height: usize, +) -> Vec { + let k = Coeffs::new(color); + convert_parallel(&k, None, y, u, v, width, height) +} + /// 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 @@ -121,30 +187,7 @@ pub fn i420_to_bgra_with( } let k = Coeffs::new(color); 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 - k.y_off; - 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 = ((k.y * c + k.rv * e + 128) >> 8).clamp(0, 255) as u8; - let g = ((k.y * c - k.gu * d - k.gv * e + 128) >> 8).clamp(0, 255) as u8; - let b = ((k.y * c + k.bu * 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 + convert_parallel(&k, Some(&lut), y, u, v, width, height) } #[cfg(test)] diff --git a/crates/engine/src/tonemap.rs b/crates/engine/src/tonemap.rs index d7e8574..87f545c 100644 --- a/crates/engine/src/tonemap.rs +++ b/crates/engine/src/tonemap.rs @@ -309,11 +309,54 @@ impl ToneMapper { if y.len() < width * height || u.len() < cw * ch || v.len() < cw * ch { return; } + let y = &mut y[..width * height]; + let u = &mut u[..cw * ch]; + let v = &mut v[..cw * ch]; + + // One lane's worth of bands is independent of every other's -- a band + // reads only its own chroma samples and its own two luma rows -- so + // the band loop below (`map_bands`) fans out over the machine's cores + // the same way `HevcEnc::code_batch` does: `std::thread::scope`, + // borrowing the planes rather than copying them, one contiguous run + // of bands per lane. A frame with fewer chroma rows than cores, or a + // machine that only reports one, just runs the whole thing here. + let lanes = std::thread::available_parallelism() + .map_or(1, |n| n.get()) + .min(ch); + if lanes <= 1 { + self.map_bands(y, u, v, width, cw); + return; + } + let bands_per_lane = ch.div_ceil(lanes); + std::thread::scope(|scope| { + let (mut y_rest, mut u_rest, mut v_rest) = (y, u, v); + let mut band = 0; + while band < ch { + let take = bands_per_lane.min(ch - band); + // The last lane's last band is one row short on an odd + // frame, which is the only reason `y`'s chunk isn't simply + // `take * 2 * width`. + let y_len = (take * 2 * width).min(y_rest.len()); + let u_len = take * cw; + let (y_chunk, y_remain) = y_rest.split_at_mut(y_len); + let (u_chunk, u_remain) = u_rest.split_at_mut(u_len); + let (v_chunk, v_remain) = v_rest.split_at_mut(u_len); + (y_rest, u_rest, v_rest) = (y_remain, u_remain, v_remain); + scope.spawn(move || self.map_bands(y_chunk, u_chunk, v_chunk, width, cw)); + band += take; + } + }); + } + + /// One contiguous run of bands: the loop `map` used to run over the whole + /// frame, unchanged, just handed a slice that may be the whole plane or + /// one lane's share of it. + fn map_bands(&self, y: &mut [u8], u: &mut [u8], v: &mut [u8], width: usize, cw: usize) { // Row slices rather than indices: the plane bounds are checked once per // band, not once per sample, which is most of the difference between // this and a frame's time budget. - let bands = y[..width * height].chunks_mut(2 * width); - let chroma_rows = u[..cw * ch].chunks_mut(cw).zip(v[..cw * ch].chunks_mut(cw)); + let bands = y.chunks_mut(2 * width); + let chroma_rows = u.chunks_mut(cw).zip(v.chunks_mut(cw)); for (band, (u_row, v_row)) in bands.zip(chroma_rows) { // The bottom half is empty on an odd frame's last band. let (top, bottom) = band.split_at_mut(width.min(band.len()));