Vectorise the motion compensation kernels
Browse files at this commitd69235f9cd19e8d491456db44b2bd143073928b2Parent: 22fb460- perf
- vectorise the motion compensation kernels
1 changed file +168 -54
- crates/ec-h264/src/inter.rs +168 -54
crates/ec-h264/src/inter.rsmodified+168 -54
| @@ -11,6 +11,8 @@ | ||
| 11 | 11 | //! branches per sample. The clamp bounds below are what makes that exact — |
| 12 | 12 | //! see [`clamp_origin`]. |
| 13 | 13 | |
| 14 | +use wide::{i16x8, i16x16, i32x8}; | |
| 15 | + | |
| 14 | 16 | /// A reference picture plane for motion compensation. |
| 15 | 17 | pub(crate) struct RefPlane<'a> { |
| 16 | 18 | /// The whole padded plane. |
| @@ -50,19 +52,122 @@ fn clamp_origin(v: i32, size: usize, extent: usize, pad: usize, left: i32, right | ||
| 50 | 52 | v.clamp(lo.min(hi), hi) |
| 51 | 53 | } |
| 52 | 54 | |
| 53 | -/// The 6-tap half-sample filter of Equations 8-241/8-242 over samples spaced | |
| 54 | -/// `step` apart, centred between `p[0]` and `p[step]`. | |
| 55 | +/// Sixteen samples as 16-bit lanes, zero past the end of the plane. | |
| 56 | +/// | |
| 57 | +/// A partition is at most 16 samples wide, so one vector covers a whole row and | |
| 58 | +/// the lanes past `w` are simply never stored. Every index the interpolator | |
| 59 | +/// reaches is inside the replicated border (see [`clamp_origin`]), so the zero | |
| 60 | +/// fill is reachable only by those discarded lanes. | |
| 55 | 61 | #[inline] |
| 56 | -fn tap6(data: &[u8], at: usize, step: usize) -> i32 { | |
| 57 | - let s = |k: isize| i32::from(data[(at as isize + k * step as isize) as usize]); | |
| 58 | - s(-2) - 5 * s(-1) + 20 * s(0) + 20 * s(1) - 5 * s(2) + s(3) | |
| 62 | +fn load16(data: &[u8], at: usize) -> i16x16 { | |
| 63 | + let mut a = [0i16; 16]; | |
| 64 | + // The fixed-length slice is what makes this one widening load: a zip | |
| 65 | + // against an open-ended slice leaves the trip count unknown and the | |
| 66 | + // compiler emits a byte loop instead. | |
| 67 | + match data.get(at..at + 16) { | |
| 68 | + Some(row) => { | |
| 69 | + for (o, &b) in a.iter_mut().zip(row) { | |
| 70 | + *o = i16::from(b); | |
| 71 | + } | |
| 72 | + } | |
| 73 | + None => { | |
| 74 | + for (o, &b) in a.iter_mut().zip(&data[at..]) { | |
| 75 | + *o = i16::from(b); | |
| 76 | + } | |
| 77 | + } | |
| 78 | + } | |
| 79 | + i16x16::from(a) | |
| 59 | 80 | } |
| 60 | 81 | |
| 61 | -/// The same over already-filtered intermediate values (Equations 8-245/8-246). | |
| 82 | +/// The same over eight lanes, for chroma blocks (at most eight wide). | |
| 62 | 83 | #[inline] |
| 63 | -fn tap6_i32(v: &[i32], at: usize, step: usize) -> i32 { | |
| 64 | - let s = |k: isize| v[(at as isize + k * step as isize) as usize]; | |
| 65 | - s(-2) - 5 * s(-1) + 20 * s(0) + 20 * s(1) - 5 * s(2) + s(3) | |
| 84 | +fn load8(data: &[u8], at: usize) -> i16x8 { | |
| 85 | + let mut a = [0i16; 8]; | |
| 86 | + match data.get(at..at + 8) { | |
| 87 | + Some(row) => { | |
| 88 | + for (o, &b) in a.iter_mut().zip(row) { | |
| 89 | + *o = i16::from(b); | |
| 90 | + } | |
| 91 | + } | |
| 92 | + None => { | |
| 93 | + for (o, &b) in a.iter_mut().zip(&data[at..]) { | |
| 94 | + *o = i16::from(b); | |
| 95 | + } | |
| 96 | + } | |
| 97 | + } | |
| 98 | + i16x8::from(a) | |
| 99 | +} | |
| 100 | + | |
| 101 | +/// The 6-tap half-sample filter of Equations 8-241/8-242 over sixteen | |
| 102 | +/// neighbouring positions at once, the taps spaced `step` apart. | |
| 103 | +/// | |
| 104 | +/// The unrounded result of Equation 8-241 spans -2550..10710, so 16-bit lanes | |
| 105 | +/// hold it exactly and one row of a partition is one vector. | |
| 106 | +#[inline] | |
| 107 | +fn tap6_16(data: &[u8], at: usize, step: usize) -> i16x16 { | |
| 108 | + let l = |k: isize| load16(data, (at as isize + k * step as isize) as usize); | |
| 109 | + let five = i16x16::splat(5); | |
| 110 | + let twenty = i16x16::splat(20); | |
| 111 | + l(-2) - five * l(-1) + twenty * l(0) + twenty * l(1) - five * l(2) + l(3) | |
| 112 | +} | |
| 113 | + | |
| 114 | +/// `Clip1Y( ( v + 16 ) >> 5 )` over a row, storing the first `dst.len()` lanes. | |
| 115 | +#[inline] | |
| 116 | +fn store_half(dst: &mut [u8], v: i16x16) { | |
| 117 | + let c = ((v + i16x16::splat(16)) >> 5u32) | |
| 118 | + .max(i16x16::ZERO) | |
| 119 | + .min(i16x16::splat(255)); | |
| 120 | + for (o, &s) in dst.iter_mut().zip(c.as_array()) { | |
| 121 | + *o = s as u8; | |
| 122 | + } | |
| 123 | +} | |
| 124 | + | |
| 125 | +/// Eight intermediate values as 32-bit lanes, zero past the end. | |
| 126 | +#[inline] | |
| 127 | +fn load_inter8(v: &[i16], at: usize) -> i32x8 { | |
| 128 | + let mut a = [0i32; 8]; | |
| 129 | + for (o, &s) in a.iter_mut().zip(&v[at..]) { | |
| 130 | + *o = i32::from(s); | |
| 131 | + } | |
| 132 | + i32x8::from(a) | |
| 133 | +} | |
| 134 | + | |
| 135 | +/// The 6-tap filter over already-filtered intermediate values (Equation | |
| 136 | +/// 8-246), eight columns at a time. Those sums reach 449820, so this pass is | |
| 137 | +/// 32-bit where the first one was 16-bit. | |
| 138 | +#[inline] | |
| 139 | +fn tap6_inter8(v: &[i16], at: usize, step: usize) -> i32x8 { | |
| 140 | + let l = |k: isize| load_inter8(v, (at as isize + k * step as isize) as usize); | |
| 141 | + let five = i32x8::splat(5); | |
| 142 | + let twenty = i32x8::splat(20); | |
| 143 | + l(-2) - five * l(-1) + twenty * l(0) + twenty * l(1) - five * l(2) + l(3) | |
| 144 | +} | |
| 145 | + | |
| 146 | +/// `Clip1Y( ( v + 512 ) >> 10 )` over eight lanes. | |
| 147 | +#[inline] | |
| 148 | +fn store_centre(dst: &mut [u8], v: i32x8) { | |
| 149 | + let c = ((v + i32x8::splat(512)) >> 10u32) | |
| 150 | + .max(i32x8::ZERO) | |
| 151 | + .min(i32x8::splat(255)); | |
| 152 | + for (o, &s) in dst.iter_mut().zip(c.as_array()) { | |
| 153 | + *o = s as u8; | |
| 154 | + } | |
| 155 | +} | |
| 156 | + | |
| 157 | +/// The rounded average of Equation 8-247 over `n` samples, sixteen at a time. | |
| 158 | +#[inline] | |
| 159 | +fn avg_rows(out: &mut [u8], a: &[u8], b: &[u8], n: usize) { | |
| 160 | + let mut i = 0; | |
| 161 | + while i + 16 <= n { | |
| 162 | + let v = (load16(a, i) + load16(b, i) + i16x16::ONE) >> 1u32; | |
| 163 | + for (o, &s) in out[i..i + 16].iter_mut().zip(v.as_array()) { | |
| 164 | + *o = s as u8; | |
| 165 | + } | |
| 166 | + i += 16; | |
| 167 | + } | |
| 168 | + for k in i..n { | |
| 169 | + out[k] = avg(a[k], b[k]); | |
| 170 | + } | |
| 66 | 171 | } |
| 67 | 172 | |
| 68 | 173 | #[inline] |
| @@ -118,9 +223,7 @@ pub(crate) fn mc_luma( | ||
| 118 | 223 | let row0 = yi + if fy == 3 { 1 } else { 0 }; |
| 119 | 224 | for row in 0..h { |
| 120 | 225 | let src = r.at(xi, row0 + row as i32); |
| 121 | - for col in 0..w { | |
| 122 | - bb[row * w + col] = clip8((tap6(r.data, src + col, 1) + 16) >> 5); | |
| 123 | - } | |
| 226 | + store_half(&mut bb[row * w..row * w + w], tap6_16(r.data, src, 1)); | |
| 124 | 227 | } |
| 125 | 228 | } |
| 126 | 229 | let mut hh = [0u8; 16 * 17]; |
| @@ -129,9 +232,7 @@ pub(crate) fn mc_luma( | ||
| 129 | 232 | let col0 = xi + if fx == 3 { 1 } else { 0 }; |
| 130 | 233 | for row in 0..h { |
| 131 | 234 | let src = r.at(col0, yi + row as i32); |
| 132 | - for col in 0..w { | |
| 133 | - hh[row * w + col] = clip8((tap6(r.data, src + col, stride) + 16) >> 5); | |
| 134 | - } | |
| 235 | + store_half(&mut hh[row * w..row * w + w], tap6_16(r.data, src, stride)); | |
| 135 | 236 | } |
| 136 | 237 | } |
| 137 | 238 | let mut jj = [0u8; 16 * 16]; |
| @@ -139,43 +240,51 @@ pub(crate) fn mc_luma( | ||
| 139 | 240 | // Equation 8-246: filter horizontally into intermediates, then |
| 140 | 241 | // vertically over those. The horizontal pass needs two rows above and |
| 141 | 242 | // three below the partition. |
| 142 | - let mut inter = [0i32; TMP_W * (16 + 5)]; | |
| 243 | + let mut inter = [0i16; TMP_W * (16 + 5)]; | |
| 143 | 244 | for row in 0..h + 5 { |
| 144 | 245 | let src = r.at(xi, yi + row as i32 - 2); |
| 145 | - for col in 0..w { | |
| 146 | - inter[row * TMP_W + col] = tap6(r.data, src + col, 1); | |
| 246 | + let v = tap6_16(r.data, src, 1); | |
| 247 | + let base = row * TMP_W; | |
| 248 | + for (o, &t) in inter[base..base + w].iter_mut().zip(v.as_array()) { | |
| 249 | + *o = t; | |
| 147 | 250 | } |
| 148 | 251 | } |
| 149 | 252 | for row in 0..h { |
| 150 | - for col in 0..w { | |
| 151 | - let v = tap6_i32(&inter, (row + 2) * TMP_W + col, TMP_W); | |
| 152 | - jj[row * w + col] = clip8((v + 512) >> 10); | |
| 253 | + for col in (0..w).step_by(8) { | |
| 254 | + let v = tap6_inter8(&inter, (row + 2) * TMP_W + col, TMP_W); | |
| 255 | + let n = (w - col).min(8); | |
| 256 | + store_centre(&mut jj[row * w + col..row * w + col + n], v); | |
| 153 | 257 | } |
| 154 | 258 | } |
| 155 | 259 | } |
| 156 | 260 | |
| 157 | - for row in 0..h { | |
| 158 | - for col in 0..w { | |
| 159 | - let i = row * w + col; | |
| 160 | - let g = |dx: i32, dy: i32| { | |
| 161 | - r.data[base + (row as i32 + dy) as usize * stride + (col as i32 + dx) as usize] | |
| 162 | - }; | |
| 163 | - out[i] = match (fx, fy) { | |
| 164 | - // Table 8-12, quarter positions on the integer rows/columns. | |
| 165 | - (1, 0) => avg(g(0, 0), bb[i]), | |
| 166 | - (2, 0) => bb[i], | |
| 167 | - (3, 0) => avg(g(1, 0), bb[i]), | |
| 168 | - (0, 1) => avg(g(0, 0), hh[i]), | |
| 169 | - (0, 2) => hh[i], | |
| 170 | - (0, 3) => avg(g(0, 1), hh[i]), | |
| 171 | - // Centre column / row: mix with the centre sample j. | |
| 172 | - (2, 1) | (2, 3) => avg(bb[i], jj[i]), | |
| 173 | - (1, 2) | (3, 2) => avg(hh[i], jj[i]), | |
| 174 | - (2, 2) => jj[i], | |
| 175 | - // Diagonal quarter positions: the two nearest half samples. | |
| 176 | - _ => avg(bb[i], hh[i]), | |
| 261 | + // Table 8-12: each quarter position is one of the half-sample arrays, or | |
| 262 | + // the average of two of them. Resolving it once per partition rather than | |
| 263 | + // once per sample is what keeps the copy loops straight. | |
| 264 | + let n = w * h; | |
| 265 | + match (fx, fy) { | |
| 266 | + (2, 0) => out[..n].copy_from_slice(&bb[..n]), | |
| 267 | + (0, 2) => out[..n].copy_from_slice(&hh[..n]), | |
| 268 | + (2, 2) => out[..n].copy_from_slice(&jj[..n]), | |
| 269 | + (2, 1) | (2, 3) => avg_rows(out, &bb, &jj, n), | |
| 270 | + (1, 2) | (3, 2) => avg_rows(out, &hh, &jj, n), | |
| 271 | + (1, 0) | (3, 0) | (0, 1) | (0, 3) => { | |
| 272 | + // Averaged with an integer sample one row or column over. | |
| 273 | + let (dx, dy) = match (fx, fy) { | |
| 274 | + (3, 0) => (1, 0), | |
| 275 | + (0, 3) => (0, 1), | |
| 276 | + _ => (0, 0), | |
| 177 | 277 | }; |
| 278 | + let half: &[u8] = if fy == 0 { &bb } else { &hh }; | |
| 279 | + for row in 0..h { | |
| 280 | + let src = base + (row + dy) * stride + dx; | |
| 281 | + for col in 0..w { | |
| 282 | + out[row * w + col] = avg(r.data[src + col], half[row * w + col]); | |
| 283 | + } | |
| 284 | + } | |
| 178 | 285 | } |
| 286 | + // Diagonal quarter positions: the two nearest half samples. | |
| 287 | + _ => avg_rows(out, &bb, &hh, n), | |
| 179 | 288 | } |
| 180 | 289 | } |
| 181 | 290 | |
| @@ -189,20 +298,29 @@ pub(crate) fn mc_chroma( | ||
| 189 | 298 | h: usize, |
| 190 | 299 | out: &mut [u8], |
| 191 | 300 | ) { |
| 192 | - let (fx, fy) = ((mv[0] & 7) as i32, (mv[1] & 7) as i32); | |
| 301 | + let (fx, fy) = ((mv[0] & 7) as i16, (mv[1] & 7) as i16); | |
| 193 | 302 | let xi = clamp_origin(x + (mv[0] as i32 >> 3), w, r.width, r.pad, 0, 1); |
| 194 | 303 | let yi = clamp_origin(y + (mv[1] as i32 >> 3), h, r.height, r.pad, 0, 1); |
| 195 | 304 | let stride = r.stride; |
| 196 | 305 | let base = r.at(xi, yi); |
| 197 | - let (wa, wb, wc, wd) = ((8 - fx) * (8 - fy), fx * (8 - fy), (8 - fx) * fy, fx * fy); | |
| 306 | + // The four weights sum to 64, so the weighted sum of Equation 8-270 never | |
| 307 | + // exceeds 64 * 255 and the whole bilinear fits in 16-bit lanes. A chroma | |
| 308 | + // block is at most eight samples wide, which is one vector per row. | |
| 309 | + let wa = i16x8::splat((8 - fx) * (8 - fy)); | |
| 310 | + let wb = i16x8::splat(fx * (8 - fy)); | |
| 311 | + let wc = i16x8::splat((8 - fx) * fy); | |
| 312 | + let wd = i16x8::splat(fx * fy); | |
| 313 | + let round = i16x8::splat(32); | |
| 198 | 314 | for row in 0..h { |
| 199 | 315 | let src = base + row * stride; |
| 200 | - for col in 0..w { | |
| 201 | - let a = i32::from(r.data[src + col]); | |
| 202 | - let b = i32::from(r.data[src + col + 1]); | |
| 203 | - let c = i32::from(r.data[src + col + stride]); | |
| 204 | - let d = i32::from(r.data[src + col + stride + 1]); | |
| 205 | - out[row * w + col] = ((wa * a + wb * b + wc * c + wd * d + 32) >> 6) as u8; | |
| 316 | + let v = (load8(r.data, src) * wa | |
| 317 | + + load8(r.data, src + 1) * wb | |
| 318 | + + load8(r.data, src + stride) * wc | |
| 319 | + + load8(r.data, src + stride + 1) * wd | |
| 320 | + + round) | |
| 321 | + >> 6u32; | |
| 322 | + for (o, &s) in out[row * w..row * w + w].iter_mut().zip(v.as_array()) { | |
| 323 | + *o = s as u8; | |
| 206 | 324 | } |
| 207 | 325 | } |
| 208 | 326 | } |
| @@ -249,11 +367,7 @@ pub(crate) fn combine( | ||
| 249 | 367 | match (use0, use1) { |
| 250 | 368 | (true, false) => out[..n].copy_from_slice(&p0[..n]), |
| 251 | 369 | (false, true) => out[..n].copy_from_slice(&p1[..n]), |
| 252 | - _ => { | |
| 253 | - for i in 0..n { | |
| 254 | - out[i] = avg(p0[i], p1[i]); | |
| 255 | - } | |
| 256 | - } | |
| 370 | + _ => avg_rows(out, p0, p1, n), | |
| 257 | 371 | } |
| 258 | 372 | return; |
| 259 | 373 | } |