Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

improve: reduce CPU YUV conversion work during playback

Tadhg Dowdallc398346642bb
diff --git a/crates/rendering/src/cpu_yuv.rs b/crates/rendering/src/cpu_yuv.rs
index 0207e4602..27214f349 100644
--- a/crates/rendering/src/cpu_yuv.rs
+++ b/crates/rendering/src/cpu_yuv.rs
@@ -468,28 +468,29 @@ fn nv12_convert_row_scalar(
out_row_start: usize,
output: &mut [u8],
) {
- for col in 0..width {
- let y_idx = y_row_start + col;
- let uv_idx = uv_row_start + (col / 2) * 2;
-
- let y = y_data.get(y_idx).copied().unwrap_or(0) as i32;
- let u = uv_data.get(uv_idx).copied().unwrap_or(128) as i32;
- let v = uv_data.get(uv_idx + 1).copied().unwrap_or(128) as i32;
-
- let c = y - 16;
- let d = u - 128;
- let e = v - 128;
-
- let r = clamp_u8((298 * c + 409 * e + 128) >> 8);
- let g = clamp_u8((298 * c - 100 * d - 208 * e + 128) >> 8);
- let b = clamp_u8((298 * c + 516 * d + 128) >> 8);
-
- let out_idx = out_row_start + col * 4;
- if out_idx + 3 < output.len() {
- output[out_idx] = r;
- output[out_idx + 1] = g;
- output[out_idx + 2] = b;
- output[out_idx + 3] = 255;
+ let y_row = &y_data[y_row_start..y_row_start + width];
+ let uv_row = &uv_data[uv_row_start..uv_row_start + width.div_ceil(2) * 2];
+ let output_row = &mut output[out_row_start..out_row_start + width * 4];
+
+ for ((luma, chroma), pixels) in y_row
+ .chunks(2)
+ .zip(uv_row.chunks_exact(2))
+ .zip(output_row.chunks_mut(8))
+ {
+ let d = i32::from(chroma[0]) - 128;
+ let e = i32::from(chroma[1]) - 128;
+ let red = 409 * e + 128;
+ let green = -100 * d - 208 * e + 128;
+ let blue = 516 * d + 128;
+
+ for (y, pixel) in luma.iter().zip(pixels.chunks_exact_mut(4)) {
+ let c = 298 * (i32::from(*y) - 16);
+ pixel.copy_from_slice(&[
+ clamp_u8((c + red) >> 8),
+ clamp_u8((c + green) >> 8),
+ clamp_u8((c + blue) >> 8),
+ 255,
+ ]);
}
}
}
@@ -850,28 +851,32 @@ fn yuv420p_convert_row_scalar(
out_row_start: usize,
output: &mut [u8],
) {
- for col in 0..width {
- let y_idx = y_row_start + col;
- let uv_idx = uv_row_start + (col / 2);
-
- let y = y_data.get(y_idx).copied().unwrap_or(0) as i32;
- let u = u_data.get(uv_idx).copied().unwrap_or(128) as i32;
- let v = v_data.get(uv_idx).copied().unwrap_or(128) as i32;
-
- let c = y - 16;
- let d = u - 128;
- let e = v - 128;
-
- let r = clamp_u8((298 * c + 409 * e + 128) >> 8);
- let g = clamp_u8((298 * c - 100 * d - 208 * e + 128) >> 8);
- let b = clamp_u8((298 * c + 516 * d + 128) >> 8);
-
- let out_idx = out_row_start + col * 4;
- if out_idx + 3 < output.len() {
- output[out_idx] = r;
- output[out_idx + 1] = g;
- output[out_idx + 2] = b;
- output[out_idx + 3] = 255;
+ let y_row = &y_data[y_row_start..y_row_start + width];
+ let chroma_width = width.div_ceil(2);
+ let u_row = &u_data[uv_row_start..uv_row_start + chroma_width];
+ let v_row = &v_data[uv_row_start..uv_row_start + chroma_width];
+ let output_row = &mut output[out_row_start..out_row_start + width * 4];
+
+ for (((luma, u), v), pixels) in y_row
+ .chunks(2)
+ .zip(u_row)
+ .zip(v_row)
+ .zip(output_row.chunks_mut(8))
+ {
+ let d = i32::from(*u) - 128;
+ let e = i32::from(*v) - 128;
+ let red = 409 * e + 128;
+ let green = -100 * d - 208 * e + 128;
+ let blue = 516 * d + 128;
+
+ for (y, pixel) in luma.iter().zip(pixels.chunks_exact_mut(4)) {
+ let c = 298 * (i32::from(*y) - 16);
+ pixel.copy_from_slice(&[
+ clamp_u8((c + red) >> 8),
+ clamp_u8((c + green) >> 8),
+ clamp_u8((c + blue) >> 8),
+ 255,
+ ]);
}
}
}