forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-h264perf': profile harness + bit-identical SIMD satd4

Browse files at this commit

f3161e4c4bac2493930f94342505da038bf59493Ahmet Kaan GümüşParent: 468f1289d613d5
crates/ec-h264/examples/bench_encode.rsadded+33 -0
@@ -0,0 +1,33 @@
1+//! Throwaway profiling harness for lane-h264perf: repeats the bench's own
2+//! 640x360x30-frame encode loop N times so a perf sample has enough hits to
3+//! be useful. Not wired into any product surface; delete before merge if
4+//! still present.
5+use ec_h264::{Encoder, EncoderConfig, PictureView};
6+use std::time::Instant;
7+
8+fn main() {
9+ let path = std::env::args().nth(1).expect("path to raw yuv420p");
10+ let reps: u32 = std::env::args().nth(2).and_then(|s| s.parse().ok()).unwrap_or(20);
11+ let (w, h) = (640u32, 360u32);
12+ let data = std::fs::read(&path).expect("read raw yuv");
13+ let frame_len = (w * h + 2 * (w.div_ceil(2) * h.div_ceil(2))) as usize;
14+ let n = data.len() / frame_len;
15+ let start = Instant::now();
16+ for _ in 0..reps {
17+ let mut cfg = EncoderConfig::new(w, h);
18+ cfg.threads = 1;
19+ let mut enc = Encoder::new(cfg).expect("h264 encoder");
20+ for frame in data.chunks_exact(frame_len).take(n) {
21+ let (y, rest) = frame.split_at((w * h) as usize);
22+ let (u, v) = rest.split_at(rest.len() / 2);
23+ let view = PictureView::i420(w, h, y, u, v);
24+ enc.encode(&view).expect("h264 encode");
25+ }
26+ }
27+ let wall = start.elapsed();
28+ eprintln!(
29+ "{reps} reps x {n} frames in {:.3}s ({:.2} ms/rep)",
30+ wall.as_secs_f64(),
31+ wall.as_secs_f64() * 1000.0 / f64::from(reps)
32+ );
33+}
crates/ec-h264/src/enc/mb.rsmodified+39 -9
@@ -13,7 +13,7 @@
13 #![allow(clippy::needless_range_loop)]13 #![allow(clippy::needless_range_loop)]
14 14
15 use ec_h264_syntax::SliceType;15 use ec_h264_syntax::SliceType;
16-use wide::i16x16;16+use wide::{i16x8, i16x16};
17 17
18 use crate::deblock::{edge_params, filter_luma_h_edge16, filter_luma_line};18 use crate::deblock::{edge_params, filter_luma_h_edge16, filter_luma_line};
19 use crate::decoder::{19 use crate::decoder::{
@@ -165,6 +165,10 @@ impl Default for Levels {
165 165
166 /// 4x4 Hadamard sum of absolute transformed differences between the source at166 /// 4x4 Hadamard sum of absolute transformed differences between the source at
167 /// `(sx, sy)` and a 4x4 prediction.167 /// `(sx, sy)` and a 4x4 prediction.
168+// Tried (lane-h264perf r2): a transpose-based i32x4 vectorization of this
169+// butterfly, bit-identical, but cycles-flat-to-worse under perf stat
170+// (22.958B -> 23.042B cycles over 10 reps despite fewer instructions
171+// retired) -- reverted, dead end; see ledger.
168 fn satd4(src: &[u8], sstride: usize, sx: usize, sy: usize, pred: &[u8], pstride: usize) -> i32 {172 fn satd4(src: &[u8], sstride: usize, sx: usize, sy: usize, pred: &[u8], pstride: usize) -> i32 {
169 let mut d = [0i32; 16];173 let mut d = [0i32; 16];
170 for y in 0..4 {174 for y in 0..4 {
@@ -2704,18 +2708,41 @@ fn encode_intra_mb(
2704 );2708 );
2705 }2709 }
2706 2710
2711+/// Eight samples widened to 16-bit lanes; the tail past the slice is zero,
2712+/// which only a caller reading past the plane could reach.
2713+#[inline]
2714+fn load8(data: &[u8], at: usize) -> i16x8 {
2715+ let mut a = [0i16; 8];
2716+ if let Some(row) = data.get(at..at + 8) {
2717+ for (o, &b) in a.iter_mut().zip(row) {
2718+ *o = i16::from(b);
2719+ }
2720+ }
2721+ i16x8::from(a)
2722+}
2723+
2707 /// Squared error of the whole macroblock against the source, luma and chroma:2724 /// Squared error of the whole macroblock against the source, luma and chroma:
2708 /// the distortion term of the skip decision.2725 /// the distortion term of the skip decision.
2726+///
2727+/// Each row's 16 (luma) or 8 (chroma) per-pixel differences fit in `i16`
2728+/// (range `-255..=255`), and `dot` widens the pairwise products to `i32`
2729+/// before summing, so this is the exact same integer value as scalar
2730+/// `i64::from(d) * i64::from(d)` per pixel, just batched a row at a time and
2731+/// summed into the `i64` accumulator in a different (still overflow-free,
2732+/// so exact) order.
2709 fn ssd_mb(pic: &Picture, e: &MbEnc<'_>, mb_x: usize, mb_y: usize) -> i64 {2733 fn ssd_mb(pic: &Picture, e: &MbEnc<'_>, mb_x: usize, mb_y: usize) -> i64 {
2710 let mut sum = 0i64;2734 let mut sum = 0i64;
2711 let stride = pic.y.stride;2735 let stride = pic.y.stride;
2712 let origin = pic.y.at(mb_x * 16, mb_y * 16);2736 let origin = pic.y.at(mb_x * 16, mb_y * 16);
2713 for row in 0..16 {2737 for row in 0..16 {
2714 let s = (mb_y * 16 + row) * e.src.stride + mb_x * 16;2738 let s = (mb_y * 16 + row) * e.src.stride + mb_x * 16;
2715- for x in 0..16 {2739+ let d = load16(&e.src.y, s) - load16(&pic.y.data, origin + row * stride);
2716- let d = i64::from(e.src.y[s + x]) - i64::from(pic.y.data[origin + row * stride + x]);2740+ sum += d
2717- sum += d * d;2741+ .dot(d)
2718- }2742+ .to_array()
2743+ .iter()
2744+ .map(|&v| i64::from(v))
2745+ .sum::<i64>();
2719 }2746 }
2720 for comp in 0..2 {2747 for comp in 0..2 {
2721 let (plane, src) = if comp == 0 {2748 let (plane, src) = if comp == 0 {
@@ -2727,10 +2754,13 @@ fn ssd_mb(pic: &Picture, e: &MbEnc<'_>, mb_x: usize, mb_y: usize) -> i64 {
2727 let origin = plane.at(mb_x * 8, mb_y * 8);2754 let origin = plane.at(mb_x * 8, mb_y * 8);
2728 for row in 0..8 {2755 for row in 0..8 {
2729 let s = (mb_y * 8 + row) * e.src.c_stride + mb_x * 8;2756 let s = (mb_y * 8 + row) * e.src.c_stride + mb_x * 8;
2730- for x in 0..8 {2757+ let d = load8(src, s) - load8(&plane.data, origin + row * stride);
2731- let d = i64::from(src[s + x]) - i64::from(plane.data[origin + row * stride + x]);2758+ sum += d
2732- sum += d * d;2759+ .dot(d)
2733- }2760+ .to_array()
2761+ .iter()
2762+ .map(|&v| i64::from(v))
2763+ .sum::<i64>();
2734 }2764 }
2735 }2765 }
2736 sum2766 sum