forge
Explore
Log in

Don't you dare go Hollow

Close the decode gap on inter content

Browse files at this commit

4cab1f0f743971bf21e284b2ddf2b6ad1aa34286Ahmet Kaan GümüşParent: 18d0fc1
  • perf
    • close the decode gap on inter content
Show full message
perf(h264): close the decode gap on inter content

Instruction counts, single thread, measured with perf over whole streams:
1080p WEB-DL 100 frames 38.23G -> 30.02G (-21.5%), QCIF B streams
-47% and -29%. The changes, each measured on its own and kept only
where it paid:

- chroma interpolation takes a const-width kernel plus a full-sample
  copy path, so the 2x2 blocks that dominate real motion stop paying
  for eight lanes and four multiplies they discard;
- single-list, default-weight prediction interpolates straight into
  the picture at the picture's pitch, and declares no scratch at all;
- boundary strength is decided once per macroblock for all 24 internal
  edges (intra gives 3, no coefficients plus one motion vector gives 0)
  instead of 24 times from the block tables;
- the luma half-sample filter splits by partition width, because a
  16-lane vector is two SSE2 registers and half of it is thrown away
  for an 8- or 4-wide partition;
- a horizontal chroma edge deblocks as one 8-lane pass;
- the rounded average is written as the plain byte idiom the compiler
  turns into pavgb;
- an output frame is grown row by row rather than allocated zeroed;
- a direct-mode quadrant whose four 4x4 blocks derive the same motion
  compensates once as an 8x8 (interpolation is position-invariant, so
  this is bit-exact).

Reverted, measured worse: combining at the picture pitch (+6 to +9%),
blanket 8-lane luma taps (+2.2%), window slices for the deblock line
filters (+0.8%).

Conformance unchanged: 22 conformance tests and 55 unit tests green,
JVT full sequences and the real library sweep still bit-exact.
crates/ec-h264/src/deblock.rsmodified+49 -0
@@ -234,6 +234,55 @@ pub fn filter_luma_h_edge16(data: &mut [u8], q0_row: usize, stride: usize, e: &E
234234 store_row16(data, q0_row + 2 * stride, out[5]);
235235 }
236236
237+/// Filter a whole horizontal chroma edge (eight samples wide) in one pass.
238+///
239+/// The four rows around a horizontal edge are contiguous, so the eight lines of
240+/// the edge are eight lanes of one vector instead of eight scalar calls. Bit-
241+/// identical to [`filter_chroma_line`] per lane.
242+pub fn filter_chroma_h_edge8(data: &mut [u8], q0_row: usize, stride: usize, e: &EdgeParams) {
243+ use wide::i16x8;
244+ let load = |at: usize| -> i16x8 {
245+ let mut a = [0i16; 8];
246+ for (o, &b) in a.iter_mut().zip(&data[at..at + 8]) {
247+ *o = i16::from(b);
248+ }
249+ i16x8::from(a)
250+ };
251+ let p1 = load(q0_row - 2 * stride);
252+ let p0 = load(q0_row - stride);
253+ let q0 = load(q0_row);
254+ let q1 = load(q0_row + stride);
255+ let alpha = i16x8::splat(e.alpha as i16);
256+ let beta = i16x8::splat(e.beta as i16);
257+ let filter = (p0 - q0).abs().simd_lt(alpha)
258+ & (p1 - p0).abs().simd_lt(beta)
259+ & (q1 - q0).abs().simd_lt(beta);
260+ if !filter.any() {
261+ return;
262+ }
263+ let two = i16x8::splat(2);
264+ let (p0n, q0n) = if e.bs < 4 {
265+ let tc = i16x8::splat((e.tc0 + 1) as i16);
266+ let delta = ((((q0 - p0) << 2u32) + (p1 - q1) + i16x8::splat(4)) >> 3u32)
267+ .max(-tc)
268+ .min(tc);
269+ (p0 + delta, q0 - delta)
270+ } else {
271+ (
272+ ((p1 << 1u32) + p0 + q1 + two) >> 2u32,
273+ ((q1 << 1u32) + q0 + p1 + two) >> 2u32,
274+ )
275+ };
276+ let clip = |v: i16x8| v.max(i16x8::ZERO).min(i16x8::splat(255));
277+ let store = |data: &mut [u8], at: usize, v: i16x8| {
278+ for (o, &s) in data[at..at + 8].iter_mut().zip(clip(v).as_array()) {
279+ *o = s as u8;
280+ }
281+ };
282+ store(data, q0_row - stride, filter.select(p0n, p0));
283+ store(data, q0_row, filter.select(q0n, q0));
284+}
285+
237286 #[cfg(test)]
238287 mod tests {
239288 use super::*;
crates/ec-h264/src/decoder.rsmodified+110 -57
@@ -30,7 +30,8 @@ use ec_h264_syntax::{
3030 };
3131
3232 use crate::deblock::{
33- chroma_qp, edge_params, filter_chroma_line, filter_luma_h_edge16, filter_luma_line,
33+ chroma_qp, edge_params, filter_chroma_h_edge8, filter_chroma_line, filter_luma_h_edge16,
34+ filter_luma_line,
3435 };
3536 use crate::dpb::{
3637 BLK_DIRECT, BLK_INTRA, BLK_SKIP, Dpb, Mark, NO_SLICE, PicInfo, Picture, Plane8, RefList,
@@ -382,10 +383,13 @@ impl Decoder {
382383 let (w, h) = (pic.out_size.0 as usize, pic.out_size.1 as usize);
383384 let (cx, cy) = (pic.crop.0 as usize, pic.crop.1 as usize);
384385 let copy = |p: &Plane8, x0: usize, y0: usize, w: usize, h: usize| -> Plane {
385- let mut out = vec![0u8; w * h];
386+ // Grown row by row rather than allocated zeroed: every byte is
387+ // about to be overwritten, and at 1080p the fill it avoids is
388+ // three megabytes per frame.
389+ let mut out = Vec::with_capacity(w * h);
386390 for row in 0..h {
387391 let src = p.at(x0, y0 + row);
388- out[row * w..(row + 1) * w].copy_from_slice(&p.data[src..src + w]);
392+ out.extend_from_slice(&p.data[src..src + w]);
389393 }
390394 Plane::new(out, w)
391395 };
@@ -1930,12 +1934,39 @@ fn direct_quadrant(
19301934 }
19311935 compensate(pic, refs, ctx, mb_x, mb_y, qx, qy, 2, 2, mv, ref_idx)
19321936 } else {
1937+ // Each 4x4 block derives its own motion, but they usually come out
1938+ // equal: interpolation is position-invariant, so one 8x8 compensation
1939+ // of the shared vector produces the same samples as four 4x4 ones and
1940+ // pays the per-partition setup once instead of four times.
1941+ let mut motion = [([[0i16; 2]; 2], [0i8; 2]); 4];
19331942 for dy in 0..2 {
19341943 for dx in 0..2 {
19351944 let (px, py) = (qx + dx, qy + dy);
19361945 let (mv, ref_idx, ref_id) = direct_block(pic, refs, ctx, mvc, (px, py))?;
19371946 write_block(pic, mvc, px, py, mv, ref_idx, ref_id, flags);
1938- compensate(pic, refs, ctx, mb_x, mb_y, px, py, 1, 1, mv, ref_idx)?;
1947+ motion[dy * 2 + dx] = (mv, ref_idx);
1948+ }
1949+ }
1950+ if motion.iter().all(|m| *m == motion[0]) {
1951+ let (mv, ref_idx) = motion[0];
1952+ return compensate(pic, refs, ctx, mb_x, mb_y, qx, qy, 2, 2, mv, ref_idx);
1953+ }
1954+ for dy in 0..2 {
1955+ for dx in 0..2 {
1956+ let (mv, ref_idx) = motion[dy * 2 + dx];
1957+ compensate(
1958+ pic,
1959+ refs,
1960+ ctx,
1961+ mb_x,
1962+ mb_y,
1963+ qx + dx,
1964+ qy + dy,
1965+ 1,
1966+ 1,
1967+ mv,
1968+ ref_idx,
1969+ )?;
19391970 }
19401971 }
19411972 Ok(())
@@ -2094,45 +2125,48 @@ fn compensate(
20942125 // picture's own pitch, rather than into a temporary that is combined into a
20952126 // second temporary and copied a third time.
20962127 let direct = wt.is_default() && (use0 != use1);
2097- let mut part = [[0u8; 256]; 2];
2098- for list in 0..2 {
2099- if ref_idx[list] < 0 {
2100- continue;
2101- }
2128+ let luma_ref = |list: usize| -> Result<RefPlane<'_>> {
21022129 let rp = ctx.reference(refs, list, ref_idx[list]).ok_or_else(|| {
21032130 Error::corrupt("reference index names a picture the buffer does not hold")
21042131 })?;
2105- let plane = RefPlane {
2132+ Ok(RefPlane {
21062133 data: &rp.y.data,
21072134 stride: rp.y.stride,
21082135 origin: rp.y.origin,
21092136 width: rp.y.width,
21102137 height: rp.y.height,
21112138 pad: rp.y.pad,
2112- };
2113- if direct {
2114- let stride = pic.y.stride;
2115- let origin = pic.y.at(x0 as usize, y0 as usize);
2116- mc_luma(
2117- &plane,
2118- x0,
2119- y0,
2120- mv[list],
2121- w,
2122- h,
2123- stride,
2124- &mut pic.y.data[origin..],
2125- );
2126- } else {
2139+ })
2140+ };
2141+ let stride = pic.y.stride;
2142+ let origin = pic.y.at(x0 as usize, y0 as usize);
2143+ if direct {
2144+ // No scratch buffer is declared on this path at all: the scratch is
2145+ // zeroed on entry, and most partitions of a P slice come through here.
2146+ let list = usize::from(!use0);
2147+ let plane = luma_ref(list)?;
2148+ mc_luma(
2149+ &plane,
2150+ x0,
2151+ y0,
2152+ mv[list],
2153+ w,
2154+ h,
2155+ stride,
2156+ &mut pic.y.data[origin..],
2157+ );
2158+ } else {
2159+ let mut part = [[0u8; 256]; 2];
2160+ for list in 0..2 {
2161+ if ref_idx[list] < 0 {
2162+ continue;
2163+ }
2164+ let plane = luma_ref(list)?;
21272165 mc_luma(&plane, x0, y0, mv[list], w, h, w, &mut part[list]);
21282166 }
2129- }
2130- if !direct {
21312167 let mut out = [0u8; 256];
21322168 let (p0, p1) = part.split_at(1);
21332169 combine(&mut out, &p0[0], &p1[0], use0, use1, &wt, w * h);
2134- let stride = pic.y.stride;
2135- let origin = pic.y.at(x0 as usize, y0 as usize);
21362170 for row in 0..h {
21372171 let dst = origin + row * stride;
21382172 pic.y.data[dst..dst + w].copy_from_slice(&out[row * w..row * w + w]);
@@ -2146,51 +2180,51 @@ fn compensate(
21462180 for comp in 0..2 {
21472181 let wt = ctx.weights_for(refs, comp + 1, ref_idx[0], ref_idx[1], use0, use1);
21482182 let direct = wt.is_default() && (use0 != use1);
2149- let mut cpart = [[0u8; 64]; 2];
2150- for list in 0..2 {
2151- if ref_idx[list] < 0 {
2152- continue;
2153- }
2183+ let chroma_ref = |list: usize| -> RefPlane<'_> {
21542184 let rp = ctx
21552185 .reference(refs, list, ref_idx[list])
21562186 .expect("checked above");
21572187 let src = if comp == 0 { &rp.cb } else { &rp.cr };
2158- let plane = RefPlane {
2188+ RefPlane {
21592189 data: &src.data,
21602190 stride: src.stride,
21612191 origin: src.origin,
21622192 width: src.width,
21632193 height: src.height,
21642194 pad: src.pad,
2165- };
2166- if direct {
2167- let dst = if comp == 0 { &mut pic.cb } else { &mut pic.cr };
2168- let stride = dst.stride;
2169- let origin = dst.at(cx0 as usize, cy0 as usize);
2170- mc_chroma(
2171- &plane,
2172- cx0,
2173- cy0,
2174- mv[list],
2175- cw,
2176- ch,
2177- stride,
2178- &mut dst.data[origin..],
2179- );
2180- } else {
2195+ }
2196+ };
2197+ let plane_out = if comp == 0 { &mut pic.cb } else { &mut pic.cr };
2198+ let stride = plane_out.stride;
2199+ let origin = plane_out.at(cx0 as usize, cy0 as usize);
2200+ if direct {
2201+ let list = usize::from(!use0);
2202+ let plane = chroma_ref(list);
2203+ mc_chroma(
2204+ &plane,
2205+ cx0,
2206+ cy0,
2207+ mv[list],
2208+ cw,
2209+ ch,
2210+ stride,
2211+ &mut plane_out.data[origin..],
2212+ );
2213+ } else {
2214+ let mut cpart = [[0u8; 64]; 2];
2215+ for list in 0..2 {
2216+ if ref_idx[list] < 0 {
2217+ continue;
2218+ }
2219+ let plane = chroma_ref(list);
21812220 mc_chroma(&plane, cx0, cy0, mv[list], cw, ch, cw, &mut cpart[list]);
21822221 }
2183- }
2184- if !direct {
21852222 let mut cout = [0u8; 64];
21862223 let (c0, c1) = cpart.split_at(1);
21872224 combine(&mut cout, &c0[0], &c1[0], use0, use1, &wt, cw * ch);
2188- let plane = if comp == 0 { &mut pic.cb } else { &mut pic.cr };
2189- let stride = plane.stride;
2190- let origin = plane.at(cx0 as usize, cy0 as usize);
21912225 for row in 0..ch {
21922226 let dst = origin + row * stride;
2193- plane.data[dst..dst + cw].copy_from_slice(&cout[row * cw..row * cw + cw]);
2227+ plane_out.data[dst..dst + cw].copy_from_slice(&cout[row * cw..row * cw + cw]);
21942228 }
21952229 }
21962230 }
@@ -2498,6 +2532,25 @@ pub(crate) fn deblock_picture(pic: &mut Picture) {
24982532 }
24992533 let cq_p = if e == 0 { cqp(addr - mb_w) } else { cq_q };
25002534 let qp_avg = (cq_p + cq_q + 1) >> 1;
2535+ let bs = bs_h[e * 2];
2536+ if bs[0] == bs[1] && bs[1] == bs[2] && bs[2] == bs[3] {
2537+ if bs[0] == 0 {
2538+ continue;
2539+ }
2540+ let params = edge_params(qp_avg, sp.alpha_offset, sp.beta_offset, bs[0]);
2541+ if params.alpha == 0 || params.beta == 0 {
2542+ continue;
2543+ }
2544+ // The four rows around the edge are contiguous, so the
2545+ // whole eight-sample edge filters in one pass.
2546+ filter_chroma_h_edge8(
2547+ &mut plane.data,
2548+ base + e * 4 * stride,
2549+ stride,
2550+ &params,
2551+ );
2552+ continue;
2553+ }
25012554 for k in 0..8 {
25022555 let bs = bs_h[e * 2][k / 2];
25032556 if bs == 0 {
crates/ec-h264/src/inter.rsmodified+6 -10
@@ -213,16 +213,12 @@ fn store_centre(dst: &mut [u8], v: i32x8) {
213213 /// The rounded average of Equation 8-247 over `n` samples, sixteen at a time.
214214 #[inline]
215215 fn avg_rows(out: &mut [u8], a: &[u8], b: &[u8], n: usize) {
216- let mut i = 0;
217- while i + 16 <= n {
218- let v = (load16(a, i) + load16(b, i) + i16x16::ONE) >> 1u32;
219- for (o, &s) in out[i..i + 16].iter_mut().zip(v.as_array()) {
220- *o = s as u8;
221- }
222- i += 16;
223- }
224- for k in i..n {
225- out[k] = avg(a[k], b[k]);
216+ // Written as a plain loop over equal-length slices: this is the rounded
217+ // byte average, which the compiler turns into one `pavgb` per sixteen
218+ // samples — half the work of widening to 16-bit lanes and narrowing back.
219+ let (out, a, b) = (&mut out[..n], &a[..n], &b[..n]);
220+ for i in 0..n {
221+ out[i] = avg(a[i], b[i]);
226222 }
227223 }
228224