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
234 store_row16(data, q0_row + 2 * stride, out[5]);234 store_row16(data, q0_row + 2 * stride, out[5]);
235 }235 }
236 236
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+
237 #[cfg(test)]286 #[cfg(test)]
238 mod tests {287 mod tests {
239 use super::*;288 use super::*;
crates/ec-h264/src/decoder.rsmodified+110 -57
@@ -30,7 +30,8 @@ use ec_h264_syntax::{
30 };30 };
31 31
32 use crate::deblock::{32 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,
34 };35 };
35 use crate::dpb::{36 use crate::dpb::{
36 BLK_DIRECT, BLK_INTRA, BLK_SKIP, Dpb, Mark, NO_SLICE, PicInfo, Picture, Plane8, RefList,37 BLK_DIRECT, BLK_INTRA, BLK_SKIP, Dpb, Mark, NO_SLICE, PicInfo, Picture, Plane8, RefList,
@@ -382,10 +383,13 @@ impl Decoder {
382 let (w, h) = (pic.out_size.0 as usize, pic.out_size.1 as usize);383 let (w, h) = (pic.out_size.0 as usize, pic.out_size.1 as usize);
383 let (cx, cy) = (pic.crop.0 as usize, pic.crop.1 as usize);384 let (cx, cy) = (pic.crop.0 as usize, pic.crop.1 as usize);
384 let copy = |p: &Plane8, x0: usize, y0: usize, w: usize, h: usize| -> Plane {385 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);
386 for row in 0..h {390 for row in 0..h {
387 let src = p.at(x0, y0 + row);391 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]);
389 }393 }
390 Plane::new(out, w)394 Plane::new(out, w)
391 };395 };
@@ -1930,12 +1934,39 @@ fn direct_quadrant(
1930 }1934 }
1931 compensate(pic, refs, ctx, mb_x, mb_y, qx, qy, 2, 2, mv, ref_idx)1935 compensate(pic, refs, ctx, mb_x, mb_y, qx, qy, 2, 2, mv, ref_idx)
1932 } else {1936 } 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];
1933 for dy in 0..2 {1942 for dy in 0..2 {
1934 for dx in 0..2 {1943 for dx in 0..2 {
1935 let (px, py) = (qx + dx, qy + dy);1944 let (px, py) = (qx + dx, qy + dy);
1936 let (mv, ref_idx, ref_id) = direct_block(pic, refs, ctx, mvc, (px, py))?;1945 let (mv, ref_idx, ref_id) = direct_block(pic, refs, ctx, mvc, (px, py))?;
1937 write_block(pic, mvc, px, py, mv, ref_idx, ref_id, flags);1946 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+ )?;
1939 }1970 }
1940 }1971 }
1941 Ok(())1972 Ok(())
@@ -2094,45 +2125,48 @@ fn compensate(
2094 // picture's own pitch, rather than into a temporary that is combined into a2125 // picture's own pitch, rather than into a temporary that is combined into a
2095 // second temporary and copied a third time.2126 // second temporary and copied a third time.
2096 let direct = wt.is_default() && (use0 != use1);2127 let direct = wt.is_default() && (use0 != use1);
2097- let mut part = [[0u8; 256]; 2];2128+ let luma_ref = |list: usize| -> Result<RefPlane<'_>> {
2098- for list in 0..2 {
2099- if ref_idx[list] < 0 {
2100- continue;
2101- }
2102 let rp = ctx.reference(refs, list, ref_idx[list]).ok_or_else(|| {2129 let rp = ctx.reference(refs, list, ref_idx[list]).ok_or_else(|| {
2103 Error::corrupt("reference index names a picture the buffer does not hold")2130 Error::corrupt("reference index names a picture the buffer does not hold")
2104 })?;2131 })?;
2105- let plane = RefPlane {2132+ Ok(RefPlane {
2106 data: &rp.y.data,2133 data: &rp.y.data,
2107 stride: rp.y.stride,2134 stride: rp.y.stride,
2108 origin: rp.y.origin,2135 origin: rp.y.origin,
2109 width: rp.y.width,2136 width: rp.y.width,
2110 height: rp.y.height,2137 height: rp.y.height,
2111 pad: rp.y.pad,2138 pad: rp.y.pad,
2112- };2139+ })
2113- if direct {2140+ };
2114- let stride = pic.y.stride;2141+ let stride = pic.y.stride;
2115- let origin = pic.y.at(x0 as usize, y0 as usize);2142+ let origin = pic.y.at(x0 as usize, y0 as usize);
2116- mc_luma(2143+ if direct {
2117- &plane,2144+ // No scratch buffer is declared on this path at all: the scratch is
2118- x0,2145+ // zeroed on entry, and most partitions of a P slice come through here.
2119- y0,2146+ let list = usize::from(!use0);
2120- mv[list],2147+ let plane = luma_ref(list)?;
2121- w,2148+ mc_luma(
2122- h,2149+ &plane,
2123- stride,2150+ x0,
2124- &mut pic.y.data[origin..],2151+ y0,
2125- );2152+ mv[list],
2126- } else {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)?;
2127 mc_luma(&plane, x0, y0, mv[list], w, h, w, &mut part[list]);2165 mc_luma(&plane, x0, y0, mv[list], w, h, w, &mut part[list]);
2128 }2166 }
2129- }
2130- if !direct {
2131 let mut out = [0u8; 256];2167 let mut out = [0u8; 256];
2132 let (p0, p1) = part.split_at(1);2168 let (p0, p1) = part.split_at(1);
2133 combine(&mut out, &p0[0], &p1[0], use0, use1, &wt, w * h);2169 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);
2136 for row in 0..h {2170 for row in 0..h {
2137 let dst = origin + row * stride;2171 let dst = origin + row * stride;
2138 pic.y.data[dst..dst + w].copy_from_slice(&out[row * w..row * w + w]);2172 pic.y.data[dst..dst + w].copy_from_slice(&out[row * w..row * w + w]);
@@ -2146,51 +2180,51 @@ fn compensate(
2146 for comp in 0..2 {2180 for comp in 0..2 {
2147 let wt = ctx.weights_for(refs, comp + 1, ref_idx[0], ref_idx[1], use0, use1);2181 let wt = ctx.weights_for(refs, comp + 1, ref_idx[0], ref_idx[1], use0, use1);
2148 let direct = wt.is_default() && (use0 != use1);2182 let direct = wt.is_default() && (use0 != use1);
2149- let mut cpart = [[0u8; 64]; 2];2183+ let chroma_ref = |list: usize| -> RefPlane<'_> {
2150- for list in 0..2 {
2151- if ref_idx[list] < 0 {
2152- continue;
2153- }
2154 let rp = ctx2184 let rp = ctx
2155 .reference(refs, list, ref_idx[list])2185 .reference(refs, list, ref_idx[list])
2156 .expect("checked above");2186 .expect("checked above");
2157 let src = if comp == 0 { &rp.cb } else { &rp.cr };2187 let src = if comp == 0 { &rp.cb } else { &rp.cr };
2158- let plane = RefPlane {2188+ RefPlane {
2159 data: &src.data,2189 data: &src.data,
2160 stride: src.stride,2190 stride: src.stride,
2161 origin: src.origin,2191 origin: src.origin,
2162 width: src.width,2192 width: src.width,
2163 height: src.height,2193 height: src.height,
2164 pad: src.pad,2194 pad: src.pad,
2165- };2195+ }
2166- if direct {2196+ };
2167- let dst = if comp == 0 { &mut pic.cb } else { &mut pic.cr };2197+ let plane_out = if comp == 0 { &mut pic.cb } else { &mut pic.cr };
2168- let stride = dst.stride;2198+ let stride = plane_out.stride;
2169- let origin = dst.at(cx0 as usize, cy0 as usize);2199+ let origin = plane_out.at(cx0 as usize, cy0 as usize);
2170- mc_chroma(2200+ if direct {
2171- &plane,2201+ let list = usize::from(!use0);
2172- cx0,2202+ let plane = chroma_ref(list);
2173- cy0,2203+ mc_chroma(
2174- mv[list],2204+ &plane,
2175- cw,2205+ cx0,
2176- ch,2206+ cy0,
2177- stride,2207+ mv[list],
2178- &mut dst.data[origin..],2208+ cw,
2179- );2209+ ch,
2180- } else {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);
2181 mc_chroma(&plane, cx0, cy0, mv[list], cw, ch, cw, &mut cpart[list]);2220 mc_chroma(&plane, cx0, cy0, mv[list], cw, ch, cw, &mut cpart[list]);
2182 }2221 }
2183- }
2184- if !direct {
2185 let mut cout = [0u8; 64];2222 let mut cout = [0u8; 64];
2186 let (c0, c1) = cpart.split_at(1);2223 let (c0, c1) = cpart.split_at(1);
2187 combine(&mut cout, &c0[0], &c1[0], use0, use1, &wt, cw * ch);2224 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);
2191 for row in 0..ch {2225 for row in 0..ch {
2192 let dst = origin + row * stride;2226 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]);
2194 }2228 }
2195 }2229 }
2196 }2230 }
@@ -2498,6 +2532,25 @@ pub(crate) fn deblock_picture(pic: &mut Picture) {
2498 }2532 }
2499 let cq_p = if e == 0 { cqp(addr - mb_w) } else { cq_q };2533 let cq_p = if e == 0 { cqp(addr - mb_w) } else { cq_q };
2500 let qp_avg = (cq_p + cq_q + 1) >> 1;2534 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+ }
2501 for k in 0..8 {2554 for k in 0..8 {
2502 let bs = bs_h[e * 2][k / 2];2555 let bs = bs_h[e * 2][k / 2];
2503 if bs == 0 {2556 if bs == 0 {
crates/ec-h264/src/inter.rsmodified+6 -10
@@ -213,16 +213,12 @@ fn store_centre(dst: &mut [u8], v: i32x8) {
213 /// The rounded average of Equation 8-247 over `n` samples, sixteen at a time.213 /// The rounded average of Equation 8-247 over `n` samples, sixteen at a time.
214 #[inline]214 #[inline]
215 fn avg_rows(out: &mut [u8], a: &[u8], b: &[u8], n: usize) {215 fn avg_rows(out: &mut [u8], a: &[u8], b: &[u8], n: usize) {
216- let mut i = 0;216+ // Written as a plain loop over equal-length slices: this is the rounded
217- while i + 16 <= n {217+ // byte average, which the compiler turns into one `pavgb` per sixteen
218- let v = (load16(a, i) + load16(b, i) + i16x16::ONE) >> 1u32;218+ // samples — half the work of widening to 16-bit lanes and narrowing back.
219- for (o, &s) in out[i..i + 16].iter_mut().zip(v.as_array()) {219+ let (out, a, b) = (&mut out[..n], &a[..n], &b[..n]);
220- *o = s as u8;220+ for i in 0..n {
221- }221+ out[i] = avg(a[i], b[i]);
222- i += 16;
223- }
224- for k in i..n {
225- out[k] = avg(a[k], b[k]);
226 }222 }
227 }223 }
228 224