Convert and scale pixels with simd in an always optimized crate
Browse files at this commitd671fb1c1856e003a640c81f9ea9dafcfcb36aafParent: b641192- perf
- convert and scale pixels with simd in an always optimized crate
7 changed files +157 -146
- Cargo.toml +5 -1
- crates/client/Cargo.toml +1 -0
- crates/client/src/media/convert.rs +2 -59
- crates/client/src/share/convert.rs +9 -84
- crates/client/src/share/encode.rs +2 -2
- crates/pixel/Cargo.toml +11 -0
- crates/pixel/src/lib.rs +127 -0
Cargo.tomlmodified+5 -1
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | [workspace] |
| 2 | 2 | resolver = "2" |
| 3 | -members = ["crates/client", "crates/common", "crates/server"] | |
| 3 | +members = ["crates/client", "crates/common", "crates/pixel", "crates/server"] | |
| 4 | 4 | |
| 5 | 5 | [profile.dev] |
| 6 | 6 | debug = "line-tables-only" |
| @@ -8,6 +8,9 @@ debug = "line-tables-only" | ||
| 8 | 8 | [profile.dev.package."*"] |
| 9 | 9 | opt-level = 3 |
| 10 | 10 | |
| 11 | +[profile.dev.package.pixel] | |
| 12 | +opt-level = 3 | |
| 13 | + | |
| 11 | 14 | [profile.harness] |
| 12 | 15 | inherits = "dev" |
| 13 | 16 | opt-level = 3 |
| @@ -26,6 +29,7 @@ unsafe_code = "forbid" | ||
| 26 | 29 | |
| 27 | 30 | [workspace.dependencies] |
| 28 | 31 | common = { path = "crates/common" } |
| 32 | +pixel = { path = "crates/pixel" } | |
| 29 | 33 | tokio = { version = "1.54.1", features = ["full"] } |
| 30 | 34 | bitcode = "0.6.9" |
| 31 | 35 | quinn = { version = "0.11.12", default-features = false, features = [ |
crates/client/Cargo.tomlmodified+1 -0
| @@ -5,6 +5,7 @@ edition = "2024" | ||
| 5 | 5 | |
| 6 | 6 | [dependencies] |
| 7 | 7 | common.workspace = true |
| 8 | +pixel.workspace = true | |
| 8 | 9 | reed-solomon-simd = "3.1.0" |
| 9 | 10 | tokio.workspace = true |
| 10 | 11 | quinn = { workspace = true, features = ["rustls-ring", "platform-verifier"] } |
crates/client/src/media/convert.rsmodified+2 -59
| @@ -1,61 +1,4 @@ | ||
| 1 | +pub use pixel::yuv_to_bgra; | |
| 2 | + | |
| 1 | 3 | pub const ATTACHMENT_WIDTH: u32 = 1280; |
| 2 | 4 | pub const SHARE_WIDTH: u32 = 1920; |
| 3 | - | |
| 4 | -pub fn yuv_to_bgra( | |
| 5 | - y: &[u8], | |
| 6 | - y_stride: usize, | |
| 7 | - u: &[u8], | |
| 8 | - v: &[u8], | |
| 9 | - uv_stride: usize, | |
| 10 | - width: u32, | |
| 11 | - height: u32, | |
| 12 | - max_width: u32, | |
| 13 | -) -> Option<(u32, u32, Vec<u8>)> { | |
| 14 | - if width == 0 || height == 0 { | |
| 15 | - return None; | |
| 16 | - } | |
| 17 | - let step = width.div_ceil(max_width); | |
| 18 | - let out_width = width.div_ceil(step); | |
| 19 | - let out_height = height.div_ceil(step); | |
| 20 | - let mut out = Vec::with_capacity(out_width as usize * out_height as usize * 4); | |
| 21 | - for row in 0..out_height { | |
| 22 | - let py = (row * step).min(height - 1) as usize; | |
| 23 | - for column in 0..out_width { | |
| 24 | - let px = (column * step).min(width - 1) as usize; | |
| 25 | - let luma = sample(y, y_stride, px, py) - 16; | |
| 26 | - let cb = sample(u, uv_stride, px / 2, py / 2) - 128; | |
| 27 | - let cr = sample(v, uv_stride, px / 2, py / 2) - 128; | |
| 28 | - let scaled = 298 * luma; | |
| 29 | - let red = clamp((scaled + 459 * cr + 128) >> 8); | |
| 30 | - let green = clamp((scaled - 55 * cb - 136 * cr + 128) >> 8); | |
| 31 | - let blue = clamp((scaled + 541 * cb + 128) >> 8); | |
| 32 | - out.extend_from_slice(&[blue, green, red, 255]); | |
| 33 | - } | |
| 34 | - } | |
| 35 | - Some((out_width, out_height, out)) | |
| 36 | -} | |
| 37 | - | |
| 38 | -fn sample(plane: &[u8], stride: usize, x: usize, y: usize) -> i32 { | |
| 39 | - plane | |
| 40 | - .get(y * stride + x) | |
| 41 | - .map_or(0, |&value| i32::from(value)) | |
| 42 | -} | |
| 43 | - | |
| 44 | -fn clamp(value: i32) -> u8 { | |
| 45 | - value.clamp(0, 255) as u8 | |
| 46 | -} | |
| 47 | - | |
| 48 | -#[cfg(test)] | |
| 49 | -mod tests { | |
| 50 | - use super::*; | |
| 51 | - | |
| 52 | - fn bgra(luma: u8) -> Option<(u32, u32, Vec<u8>)> { | |
| 53 | - yuv_to_bgra(&[luma; 4], 2, &[128], &[128], 1, 2, 2, ATTACHMENT_WIDTH) | |
| 54 | - } | |
| 55 | - | |
| 56 | - #[test] | |
| 57 | - fn i420_limits_map_to_black_and_white_bgra() { | |
| 58 | - assert_eq!(bgra(235), Some((2, 2, [255, 255, 255, 255].repeat(4)))); | |
| 59 | - assert_eq!(bgra(16), Some((2, 2, [0, 0, 0, 255].repeat(4)))); | |
| 60 | - } | |
| 61 | -} |
crates/client/src/share/convert.rsmodified+9 -84
| @@ -1,88 +1,13 @@ | ||
| 1 | -use std::ops::Range; | |
| 1 | +pub use pixel::I420; | |
| 2 | 2 | |
| 3 | 3 | use super::frame::Frame; |
| 4 | 4 | |
| 5 | -pub struct I420 { | |
| 6 | - pub y: Vec<u8>, | |
| 7 | - pub u: Vec<u8>, | |
| 8 | - pub v: Vec<u8>, | |
| 9 | - pub width: u32, | |
| 10 | - pub height: u32, | |
| 11 | -} | |
| 12 | - | |
| 13 | -pub fn bgra_to_i420(frame: &Frame, width: u32, height: u32) -> I420 { | |
| 14 | - let (w, h) = (width as usize, height as usize); | |
| 15 | - let mut y = vec![0; w * h]; | |
| 16 | - let mut cb_full = vec![0; w * h]; | |
| 17 | - let mut cr_full = vec![0; w * h]; | |
| 18 | - for dy in 0..h { | |
| 19 | - let rows = span(dy, h, frame.height as usize); | |
| 20 | - for dx in 0..w { | |
| 21 | - let columns = span(dx, w, frame.width as usize); | |
| 22 | - let (r, g, b) = average(frame, columns, rows.clone()); | |
| 23 | - let index = dy * w + dx; | |
| 24 | - y[index] = luma(r, g, b); | |
| 25 | - cb_full[index] = chroma_blue(r, g, b); | |
| 26 | - cr_full[index] = chroma_red(r, g, b); | |
| 27 | - } | |
| 28 | - } | |
| 29 | - I420 { | |
| 30 | - y, | |
| 31 | - u: subsample(&cb_full, w, h), | |
| 32 | - v: subsample(&cr_full, w, h), | |
| 33 | - width, | |
| 34 | - height, | |
| 35 | - } | |
| 36 | -} | |
| 37 | - | |
| 38 | -fn span(index: usize, target: usize, source: usize) -> Range<usize> { | |
| 39 | - let start = index * source / target; | |
| 40 | - let end = ((index + 1) * source / target).max(start + 1); | |
| 41 | - start..end | |
| 42 | -} | |
| 43 | - | |
| 44 | -fn average(frame: &Frame, columns: Range<usize>, rows: Range<usize>) -> (i32, i32, i32) { | |
| 45 | - let stride = frame.stride as usize; | |
| 46 | - let (mut r, mut g, mut b, mut count) = (0u32, 0u32, 0u32, 0u32); | |
| 47 | - for y in rows { | |
| 48 | - for x in columns.clone() { | |
| 49 | - let pixel = y * stride + x * 4; | |
| 50 | - b += u32::from(frame.bgra[pixel]); | |
| 51 | - g += u32::from(frame.bgra[pixel + 1]); | |
| 52 | - r += u32::from(frame.bgra[pixel + 2]); | |
| 53 | - count += 1; | |
| 54 | - } | |
| 55 | - } | |
| 56 | - ((r / count) as i32, (g / count) as i32, (b / count) as i32) | |
| 57 | -} | |
| 58 | - | |
| 59 | -fn luma(r: i32, g: i32, b: i32) -> u8 { | |
| 60 | - (((47 * r + 157 * g + 16 * b + 128) >> 8) + 16) as u8 | |
| 61 | -} | |
| 62 | - | |
| 63 | -fn chroma_blue(r: i32, g: i32, b: i32) -> u8 { | |
| 64 | - (((-26 * r - 86 * g + 112 * b + 128) >> 8) + 128) as u8 | |
| 65 | -} | |
| 66 | - | |
| 67 | -fn chroma_red(r: i32, g: i32, b: i32) -> u8 { | |
| 68 | - (((112 * r - 102 * g - 10 * b + 128) >> 8) + 128) as u8 | |
| 69 | -} | |
| 70 | - | |
| 71 | -fn subsample(plane: &[u8], width: usize, height: usize) -> Vec<u8> { | |
| 72 | - let mut out = Vec::with_capacity(width / 2 * height / 2); | |
| 73 | - for y in (0..height).step_by(2) { | |
| 74 | - for x in (0..width).step_by(2) { | |
| 75 | - let sum: u32 = [ | |
| 76 | - plane[y * width + x], | |
| 77 | - plane[y * width + x + 1], | |
| 78 | - plane[(y + 1) * width + x], | |
| 79 | - plane[(y + 1) * width + x + 1], | |
| 80 | - ] | |
| 81 | - .into_iter() | |
| 82 | - .map(u32::from) | |
| 83 | - .sum(); | |
| 84 | - out.push(((sum + 2) / 4) as u8); | |
| 85 | - } | |
| 86 | - } | |
| 87 | - out | |
| 5 | +pub fn bgra_to_i420(frame: &Frame, width: u32, height: u32) -> Option<I420> { | |
| 6 | + let source = pixel::Bgra { | |
| 7 | + data: &frame.bgra, | |
| 8 | + width: frame.width, | |
| 9 | + height: frame.height, | |
| 10 | + stride: frame.stride, | |
| 11 | + }; | |
| 12 | + pixel::bgra_to_i420(&source, width, height) | |
| 88 | 13 | } |
crates/client/src/share/encode.rsmodified+2 -2
| @@ -115,7 +115,7 @@ impl Backend { | ||
| 115 | 115 | match self { |
| 116 | 116 | Self::Hardware(encoder) => encoder.encode(frame, settings, force), |
| 117 | 117 | Self::H264(encoder) => { |
| 118 | - let planes = convert::bgra_to_i420(frame, settings.width, settings.height); | |
| 118 | + let planes = convert::bgra_to_i420(frame, settings.width, settings.height)?; | |
| 119 | 119 | let picture = ec_h264::PictureView::i420( |
| 120 | 120 | planes.width, |
| 121 | 121 | planes.height, |
| @@ -165,7 +165,7 @@ mod hardware { | ||
| 165 | 165 | }; |
| 166 | 166 | self.0.encode_rgb(&rgb, meta) |
| 167 | 167 | } else { |
| 168 | - let planes = convert::bgra_to_i420(frame, settings.width, settings.height); | |
| 168 | + let planes = convert::bgra_to_i420(frame, settings.width, settings.height)?; | |
| 169 | 169 | let picture = ec_hw::I420 { |
| 170 | 170 | y: planes.y, |
| 171 | 171 | u: planes.u, |
crates/pixel/Cargo.tomladded+11 -0
| @@ -0,0 +1,11 @@ | ||
| 1 | +[package] | |
| 2 | +name = "pixel" | |
| 3 | +version = "0.1.0" | |
| 4 | +edition = "2024" | |
| 5 | + | |
| 6 | +[dependencies] | |
| 7 | +yuv = "0.8.21" | |
| 8 | +fast_image_resize = "6.2.0" | |
| 9 | + | |
| 10 | +[lints] | |
| 11 | +workspace = true |
crates/pixel/src/lib.rsadded+127 -0
| @@ -0,0 +1,127 @@ | ||
| 1 | +use fast_image_resize::{ | |
| 2 | + FilterType, PixelType, ResizeAlg, ResizeOptions, Resizer, | |
| 3 | + images::{Image, ImageRef}, | |
| 4 | +}; | |
| 5 | +use yuv::{ | |
| 6 | + YuvChromaSubsampling, YuvConversionMode, YuvPlanarImage, YuvPlanarImageMut, YuvRange, | |
| 7 | + YuvStandardMatrix, | |
| 8 | +}; | |
| 9 | + | |
| 10 | +const BYTES_PER_PIXEL: u32 = 4; | |
| 11 | +const RANGE: YuvRange = YuvRange::Limited; | |
| 12 | +const MATRIX: YuvStandardMatrix = YuvStandardMatrix::Bt709; | |
| 13 | + | |
| 14 | +pub struct Bgra<'a> { | |
| 15 | + pub data: &'a [u8], | |
| 16 | + pub width: u32, | |
| 17 | + pub height: u32, | |
| 18 | + pub stride: u32, | |
| 19 | +} | |
| 20 | + | |
| 21 | +pub struct I420 { | |
| 22 | + pub y: Vec<u8>, | |
| 23 | + pub u: Vec<u8>, | |
| 24 | + pub v: Vec<u8>, | |
| 25 | + pub width: u32, | |
| 26 | + pub height: u32, | |
| 27 | +} | |
| 28 | + | |
| 29 | +pub fn yuv_to_bgra( | |
| 30 | + y: &[u8], | |
| 31 | + y_stride: usize, | |
| 32 | + u: &[u8], | |
| 33 | + v: &[u8], | |
| 34 | + uv_stride: usize, | |
| 35 | + width: u32, | |
| 36 | + height: u32, | |
| 37 | + max_width: u32, | |
| 38 | +) -> Option<(u32, u32, Vec<u8>)> { | |
| 39 | + if width == 0 || height == 0 { | |
| 40 | + return None; | |
| 41 | + } | |
| 42 | + let image = YuvPlanarImage { | |
| 43 | + y_plane: y, | |
| 44 | + y_stride: y_stride as u32, | |
| 45 | + u_plane: u, | |
| 46 | + u_stride: uv_stride as u32, | |
| 47 | + v_plane: v, | |
| 48 | + v_stride: uv_stride as u32, | |
| 49 | + width, | |
| 50 | + height, | |
| 51 | + }; | |
| 52 | + let mut out = vec![0; (width * height * BYTES_PER_PIXEL) as usize]; | |
| 53 | + yuv::yuv420_to_bgra(&image, &mut out, width * BYTES_PER_PIXEL, RANGE, MATRIX).ok()?; | |
| 54 | + if width <= max_width { | |
| 55 | + return Some((width, height, out)); | |
| 56 | + } | |
| 57 | + let out_height = (u64::from(height) * u64::from(max_width) / u64::from(width)).max(1) as u32; | |
| 58 | + let source = Bgra { | |
| 59 | + data: &out, | |
| 60 | + width, | |
| 61 | + height, | |
| 62 | + stride: width * BYTES_PER_PIXEL, | |
| 63 | + }; | |
| 64 | + Some(( | |
| 65 | + max_width, | |
| 66 | + out_height, | |
| 67 | + scale(&source, max_width, out_height)?, | |
| 68 | + )) | |
| 69 | +} | |
| 70 | + | |
| 71 | +pub fn bgra_to_i420(source: &Bgra, width: u32, height: u32) -> Option<I420> { | |
| 72 | + let scaled = (source.width, source.height) != (width, height); | |
| 73 | + let resized = match scaled { | |
| 74 | + true => scale(source, width, height)?, | |
| 75 | + false => Vec::new(), | |
| 76 | + }; | |
| 77 | + let (data, stride) = match scaled { | |
| 78 | + true => (resized.as_slice(), width * BYTES_PER_PIXEL), | |
| 79 | + false => (source.data, source.stride), | |
| 80 | + }; | |
| 81 | + let mut planes = YuvPlanarImageMut::alloc(width, height, YuvChromaSubsampling::Yuv420); | |
| 82 | + yuv::bgra_to_yuv420( | |
| 83 | + &mut planes, | |
| 84 | + data, | |
| 85 | + stride, | |
| 86 | + RANGE, | |
| 87 | + MATRIX, | |
| 88 | + YuvConversionMode::Balanced, | |
| 89 | + ) | |
| 90 | + .ok()?; | |
| 91 | + Some(I420 { | |
| 92 | + y: planes.y_plane.borrow().to_vec(), | |
| 93 | + u: planes.u_plane.borrow().to_vec(), | |
| 94 | + v: planes.v_plane.borrow().to_vec(), | |
| 95 | + width, | |
| 96 | + height, | |
| 97 | + }) | |
| 98 | +} | |
| 99 | + | |
| 100 | +fn scale(source: &Bgra, width: u32, height: u32) -> Option<Vec<u8>> { | |
| 101 | + let view = ImageRef::new( | |
| 102 | + source.stride / BYTES_PER_PIXEL, | |
| 103 | + source.height, | |
| 104 | + source.data, | |
| 105 | + PixelType::U8x4, | |
| 106 | + ) | |
| 107 | + .ok()?; | |
| 108 | + let mut target = Image::new(width, height, PixelType::U8x4); | |
| 109 | + let options = ResizeOptions::new() | |
| 110 | + .resize_alg(ResizeAlg::Convolution(FilterType::Box)) | |
| 111 | + .use_alpha(false) | |
| 112 | + .crop(0.0, 0.0, f64::from(source.width), f64::from(source.height)); | |
| 113 | + Resizer::new().resize(&view, &mut target, &options).ok()?; | |
| 114 | + Some(target.into_vec()) | |
| 115 | +} | |
| 116 | + | |
| 117 | +#[cfg(test)] | |
| 118 | +mod tests { | |
| 119 | + use super::*; | |
| 120 | + | |
| 121 | + #[test] | |
| 122 | + fn limited_range_maps_to_black_and_white() { | |
| 123 | + let bgra = |luma| yuv_to_bgra(&[luma; 4], 2, &[128], &[128], 1, 2, 2, 1920); | |
| 124 | + assert_eq!(bgra(235), Some((2, 2, [255, 255, 255, 255].repeat(4)))); | |
| 125 | + assert_eq!(bgra(16), Some((2, 2, [0, 0, 0, 255].repeat(4)))); | |
| 126 | + } | |
| 127 | +} |