| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | pub fn i420_to_bgra(y: &[u8], u: &[u8], v: &[u8], width: usize, height: usize) -> Vec<u8> { |
| 6 | let cw = width.div_ceil(2); |
| 7 | let mut out = vec![0u8; width * height * 4]; |
| 8 | |
| 9 | for row in 0..height { |
| 10 | let y_row = row * width; |
| 11 | let c_row = (row / 2) * cw; |
| 12 | for col in 0..width { |
| 13 | let c = y[y_row + col] as i32 - 16; |
| 14 | let ci = c_row + col / 2; |
| 15 | let d = u[ci] as i32 - 128; |
| 16 | let e = v[ci] as i32 - 128; |
| 17 | |
| 18 | let r = ((298 * c + 409 * e + 128) >> 8).clamp(0, 255) as u8; |
| 19 | let g = ((298 * c - 100 * d - 208 * e + 128) >> 8).clamp(0, 255) as u8; |
| 20 | let b = ((298 * c + 516 * d + 128) >> 8).clamp(0, 255) as u8; |
| 21 | |
| 22 | let o = (y_row + col) * 4; |
| 23 | out[o] = b; |
| 24 | out[o + 1] = g; |
| 25 | out[o + 2] = r; |
| 26 | out[o + 3] = 255; |
| 27 | } |
| 28 | } |
| 29 | out |
| 30 | } |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | pub fn i420_to_bgra_with( |
| 40 | params: &crate::color::ColorParams, |
| 41 | y: &[u8], |
| 42 | u: &[u8], |
| 43 | v: &[u8], |
| 44 | width: usize, |
| 45 | height: usize, |
| 46 | ) -> Vec<u8> { |
| 47 | if params.is_identity() { |
| 48 | return i420_to_bgra(y, u, v, width, height); |
| 49 | } |
| 50 | let lut = crate::color::Lut::new(params); |
| 51 | let cw = width.div_ceil(2); |
| 52 | let mut out = vec![0u8; width * height * 4]; |
| 53 | |
| 54 | for row in 0..height { |
| 55 | let y_row = row * width; |
| 56 | let c_row = (row / 2) * cw; |
| 57 | for col in 0..width { |
| 58 | let c = lut.y[y[y_row + col] as usize] as i32 - 16; |
| 59 | let ci = c_row + col / 2; |
| 60 | let d = lut.u[u[ci] as usize] as i32 - 128; |
| 61 | let e = lut.v[v[ci] as usize] as i32 - 128; |
| 62 | |
| 63 | let r = ((298 * c + 409 * e + 128) >> 8).clamp(0, 255) as u8; |
| 64 | let g = ((298 * c - 100 * d - 208 * e + 128) >> 8).clamp(0, 255) as u8; |
| 65 | let b = ((298 * c + 516 * d + 128) >> 8).clamp(0, 255) as u8; |
| 66 | |
| 67 | let o = (y_row + col) * 4; |
| 68 | out[o] = b; |
| 69 | out[o + 1] = g; |
| 70 | out[o + 2] = r; |
| 71 | out[o + 3] = 255; |
| 72 | } |
| 73 | } |
| 74 | out |
| 75 | } |
| 76 | |
| 77 | #[cfg(test)] |
| 78 | mod tests { |
| 79 | use super::*; |
| 80 | use crate::color::ColorParams; |
| 81 | |
| 82 | |
| 83 | |
| 84 | #[test] |
| 85 | fn identity_grade_converts_identically() { |
| 86 | let y: Vec<u8> = (0..64 * 48).map(|i| (i % 256) as u8).collect(); |
| 87 | let u: Vec<u8> = (0..32 * 24).map(|i| (i % 256) as u8).collect(); |
| 88 | let v: Vec<u8> = (0..32 * 24).map(|i| (255 - i % 256) as u8).collect(); |
| 89 | assert_eq!( |
| 90 | i420_to_bgra_with(&ColorParams::default(), &y, &u, &v, 64, 48), |
| 91 | i420_to_bgra(&y, &u, &v, 64, 48) |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | #[test] |
| 99 | fn fused_matches_grade_then_convert() { |
| 100 | let params = ColorParams { |
| 101 | brightness: 0.1, |
| 102 | contrast: 1.3, |
| 103 | saturation: 0.4, |
| 104 | tint: 0.25, |
| 105 | }; |
| 106 | let mut y: Vec<u8> = (0..64 * 48).map(|i| (i % 256) as u8).collect(); |
| 107 | let mut u: Vec<u8> = (0..32 * 24).map(|i| (i % 256) as u8).collect(); |
| 108 | let mut v: Vec<u8> = (0..32 * 24).map(|i| (255 - i % 256) as u8).collect(); |
| 109 | let fused = i420_to_bgra_with(¶ms, &y, &u, &v, 64, 48); |
| 110 | crate::color::apply_yuv(¶ms, &mut y, &mut u, &mut v); |
| 111 | assert_eq!(fused, i420_to_bgra(&y, &u, &v, 64, 48)); |
| 112 | } |
| 113 | |
| 114 | |
| 115 | |
| 116 | #[test] |
| 117 | fn perf_1080p() { |
| 118 | let (w, h) = (1920usize, 1080usize); |
| 119 | let y: Vec<u8> = (0..w * h).map(|i| (i % 256) as u8).collect(); |
| 120 | let u: Vec<u8> = (0..w / 2 * h / 2).map(|i| (i % 256) as u8).collect(); |
| 121 | let v: Vec<u8> = (0..w / 2 * h / 2).map(|i| (255 - i % 256) as u8).collect(); |
| 122 | let params = ColorParams { |
| 123 | brightness: 0.1, |
| 124 | contrast: 1.2, |
| 125 | saturation: 1.3, |
| 126 | tint: 0.2, |
| 127 | }; |
| 128 | let runs = 20; |
| 129 | let bench = |name: &str, mut f: Box<dyn FnMut()>| { |
| 130 | let t = std::time::Instant::now(); |
| 131 | for _ in 0..runs { |
| 132 | f(); |
| 133 | } |
| 134 | println!( |
| 135 | "{name} 1920x1080: {:.3} ms/frame", |
| 136 | t.elapsed().as_secs_f64() * 1000.0 / f64::from(runs) |
| 137 | ); |
| 138 | }; |
| 139 | bench( |
| 140 | "i420_to_bgra ", |
| 141 | Box::new(|| { |
| 142 | std::hint::black_box(i420_to_bgra(&y, &u, &v, w, h)); |
| 143 | }), |
| 144 | ); |
| 145 | bench( |
| 146 | "i420_to_bgra_with ", |
| 147 | Box::new(|| { |
| 148 | std::hint::black_box(i420_to_bgra_with(¶ms, &y, &u, &v, w, h)); |
| 149 | }), |
| 150 | ); |
| 151 | bench( |
| 152 | "copy + apply_yuv ", |
| 153 | Box::new(|| { |
| 154 | let (mut y, mut u, mut v) = (y.clone(), u.clone(), v.clone()); |
| 155 | crate::color::apply_yuv(¶ms, &mut y, &mut u, &mut v); |
| 156 | std::hint::black_box((y, u, v)); |
| 157 | }), |
| 158 | ); |
| 159 | } |
| 160 | |
| 161 | #[test] |
| 162 | fn known_colors() { |
| 163 | |
| 164 | |
| 165 | let cases: [([u8; 3], [u8; 3]); 3] = [ |
| 166 | ([16, 128, 128], [0, 0, 0]), |
| 167 | ([235, 128, 128], [255, 255, 255]), |
| 168 | ([82, 90, 240], [255, 0, 0]), |
| 169 | ]; |
| 170 | for ([yv, uv, vv], [r, g, b]) in cases { |
| 171 | let out = i420_to_bgra(&[yv; 4], &[uv], &[vv], 2, 2); |
| 172 | assert_eq!(out.len(), 2 * 2 * 4); |
| 173 | for px in out.chunks_exact(4) { |
| 174 | assert!(px[0].abs_diff(b) <= 2, "B {} vs {}", px[0], b); |
| 175 | assert!(px[1].abs_diff(g) <= 2, "G {} vs {}", px[1], g); |
| 176 | assert!(px[2].abs_diff(r) <= 2, "R {} vs {}", px[2], r); |
| 177 | assert_eq!(px[3], 255); |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | } |