| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | set -euo pipefail |
| 20 | |
| 21 | cd "$(dirname "$0")/.." |
| 22 | REPO="$(pwd)" |
| 23 | CLIPS="${BENCH_CLIPS:-$HOME/.cache/benchhw-clips}" |
| 24 | BIN="${BENCH_BIN:-$HOME/.cache/benchhw-bin}" |
| 25 | OUT="$CLIPS/out" |
| 26 | export CARGO_TARGET_DIR="${CARGO_TARGET_DIR:-$HOME/.cache/tgt-benchhw}" |
| 27 | export RUSTC_WRAPPER= |
| 28 | export TMPDIR="${TMPDIR:-$HOME/.cache/tmp-benchhw}" |
| 29 | FF=(ffmpeg -hide_banner -loglevel error -y) |
| 30 | DO_CLIPS=1 |
| 31 | DO_BUILD=1 |
| 32 | for a in "$@"; do |
| 33 | case "$a" in |
| 34 | --no-clips) DO_CLIPS=0 ;; |
| 35 | --no-build) DO_BUILD=0 ;; |
| 36 | *) echo "unknown option: $a" >&2; exit 2 ;; |
| 37 | esac |
| 38 | done |
| 39 | mkdir -p "$CLIPS" "$BIN" "$OUT" "$TMPDIR" |
| 40 | COMMIT="$(git -C "$REPO" rev-parse --short HEAD)" |
| 41 | echo "# host=local cores=$(nproc) commit=$COMMIT ffmpeg=$(ffmpeg -version | head -1 | awk '{print $3}') gpu=$(vainfo 2>/dev/null | sed -n 's/.*Driver version: //p' | head -1)" |
| 42 | |
| 43 | SRC="testsrc2=size=1920x1080:rate=30:duration=60" |
| 44 | W=1920 |
| 45 | H=1080 |
| 46 | |
| 47 | |
| 48 | if [ "$DO_CLIPS" = 1 ]; then |
| 49 | cd "$CLIPS" |
| 50 | |
| 51 | [ -f src_1080p30_8bit.yuv ] || "${FF[@]}" -f lavfi -i "$SRC,format=yuv420p,noise=alls=10:allf=t" \ |
| 52 | -f rawvideo -pix_fmt yuv420p src_1080p30_8bit.yuv |
| 53 | |
| 54 | [ -f h264_high_1080p30.264 ] || "${FF[@]}" -f lavfi -i "$SRC,format=yuv420p,noise=alls=10:allf=t" \ |
| 55 | -c:v libx264 -preset veryfast -profile:v high -crf 23 -g 30 -keyint_min 30 \ |
| 56 | -f h264 h264_high_1080p30.264 |
| 57 | [ -f hevc_main_1080p30.265 ] || "${FF[@]}" -f lavfi -i "$SRC,format=yuv420p,noise=alls=10:allf=t" \ |
| 58 | -c:v libx265 -preset veryfast -crf 23 -x265-params "keyint=30:min-keyint=30:log-level=error" \ |
| 59 | -profile:v main -f hevc hevc_main_1080p30.265 |
| 60 | [ -f hevc_main10_1080p30.265 ] || "${FF[@]}" -f lavfi -i "$SRC,format=yuv420p10le,noise=alls=20:allf=t" \ |
| 61 | -c:v libx265 -preset veryfast -crf 23 -x265-params "keyint=30:min-keyint=30:log-level=error" \ |
| 62 | -profile:v main10 -pix_fmt yuv420p10le -f hevc hevc_main10_1080p30.265 |
| 63 | [ -f vp9_profile0_1080p30.ivf ] || "${FF[@]}" -f lavfi -i "$SRC,format=yuv420p,noise=alls=10:allf=t" \ |
| 64 | -c:v libvpx-vp9 -deadline realtime -cpu-used 5 -row-mt 1 -b:v 0 -crf 31 -g 30 \ |
| 65 | -pix_fmt yuv420p -f ivf vp9_profile0_1080p30.ivf |
| 66 | [ -f av1_8bit_1080p30.ivf ] || "${FF[@]}" -f lavfi -i "$SRC,format=yuv420p,noise=alls=10:allf=t" \ |
| 67 | -c:v libsvtav1 -preset 8 -crf 35 -g 30 -pix_fmt yuv420p -f ivf av1_8bit_1080p30.ivf |
| 68 | |
| 69 | [ -f ref_h264.yuv ] || "${FF[@]}" -i h264_high_1080p30.264 -map 0:v:0 -fps_mode passthrough -f rawvideo -pix_fmt yuv420p ref_h264.yuv |
| 70 | [ -f ref_hevc.yuv ] || "${FF[@]}" -i hevc_main_1080p30.265 -map 0:v:0 -fps_mode passthrough -f rawvideo -pix_fmt yuv420p ref_hevc.yuv |
| 71 | [ -f ref_hevc10.yuv ] || "${FF[@]}" -i hevc_main10_1080p30.265 -map 0:v:0 -fps_mode passthrough -f rawvideo -pix_fmt yuv420p10le ref_hevc10.yuv |
| 72 | [ -f ref_vp9.yuv ] || "${FF[@]}" -i vp9_profile0_1080p30.ivf -map 0:v:0 -fps_mode passthrough -f rawvideo -pix_fmt yuv420p ref_vp9.yuv |
| 73 | [ -f ref_av1.yuv ] || "${FF[@]}" -i av1_8bit_1080p30.ivf -map 0:v:0 -fps_mode passthrough -f rawvideo -pix_fmt yuv420p ref_av1.yuv |
| 74 | cd "$REPO" |
| 75 | fi |
| 76 | |
| 77 | |
| 78 | if [ "$DO_BUILD" = 1 ]; then |
| 79 | cargo build --release -j6 -p ec-hw --example bench |
| 80 | cp "$CARGO_TARGET_DIR/release/examples/bench" "$BIN/ec-hw-bench" |
| 81 | fi |
| 82 | B="$BIN/ec-hw-bench" |
| 83 | |
| 84 | |
| 85 | wait_load() { |
| 86 | |
| 87 | while :; do |
| 88 | l=$(cut -d' ' -f1 /proc/loadavg) |
| 89 | if awk -v l="$l" 'BEGIN{exit !(l < 3)}'; then echo "$l"; return; fi |
| 90 | sleep 5 |
| 91 | done |
| 92 | } |
| 93 | field() { echo "$1" | tr ' ' '\n' | sed -n "s/^$2=//p"; } |
| 94 | |
| 95 | framecmp() { |
| 96 | python3 -I - "$1" "$2" "$3" <<'PY' |
| 97 | import hashlib, sys |
| 98 | ref, test, fb = sys.argv[1], sys.argv[2], int(sys.argv[3]) |
| 99 | def md5s(p): |
| 100 | out = [] |
| 101 | with open(p, 'rb') as f: |
| 102 | while True: |
| 103 | b = f.read(fb) |
| 104 | if not b: |
| 105 | break |
| 106 | out.append(hashlib.md5(b).hexdigest()[:12]) |
| 107 | return out |
| 108 | r, t = md5s(ref), md5s(test) |
| 109 | same = sum(1 for a, b in zip(r, t) if a == b) |
| 110 | first = next((i for i, (a, b) in enumerate(zip(r, t)) if a != b), None) |
| 111 | print(f"frames_ref={len(r)} frames_test={len(t)} frames_md5_equal={same} first_diff={first if first is not None else 'none'}") |
| 112 | PY |
| 113 | } |
| 114 | |
| 115 | |
| 116 | cd "$CLIPS" |
| 117 | dec_row() { |
| 118 | local label=$1 codec=$2 stream=$3 ref=$4 fb=$5 |
| 119 | local l res best fps x pics |
| 120 | l=$(wait_load) |
| 121 | res=$("$B" dec "$codec" "$stream" --out "$OUT/dump-$label.yuv" --runs 5) |
| 122 | best=$(echo "$res" | sed -n 's/^BEST|.*best_s=\([^ ]*\).*/\1/p') |
| 123 | fps=$(echo "$res" | sed -n 's/^BEST|.*fps=\([^ ]*\).*/\1/p') |
| 124 | x=$(echo "$res" | sed -n 's/^BEST|.*x_realtime=\([^ ]*\).*/\1/p') |
| 125 | pics=$(echo "$res" | sed -n 's/^BEST|.*pictures=\([^ ]*\).*/\1/p') |
| 126 | echo "ROW|ec-hw|decode $label|VA-API $label 1080p30 60 s, $pics pictures|best ${best} s|${fps} fps, ${x}x realtime|load1=$l runs=5 best" |
| 127 | echo "CHECK|ec-hw decode $label|$(framecmp "$ref" "$OUT/dump-$label.yuv" "$fb")" |
| 128 | rm -f "$OUT/dump-$label.yuv" |
| 129 | } |
| 130 | FB8=$((W * H * 3 / 2)) |
| 131 | FB10=$((W * H * 3)) |
| 132 | dec_row h264-high h264 h264_high_1080p30.264 ref_h264.yuv "$FB8" |
| 133 | dec_row hevc-main hevc hevc_main_1080p30.265 ref_hevc.yuv "$FB8" |
| 134 | dec_row hevc-main10 hevc hevc_main10_1080p30.265 ref_hevc10.yuv "$FB10" |
| 135 | dec_row vp9-profile0 vp9 vp9_profile0_1080p30.ivf ref_vp9.yuv "$FB8" |
| 136 | dec_row av1-8bit av1 av1_8bit_1080p30.ivf ref_av1.yuv "$FB8" |
| 137 | |
| 138 | |
| 139 | enc_row() { |
| 140 | local label=$1 codec=$2 ffmt=$3 |
| 141 | local l res best fps x kbps |
| 142 | l=$(wait_load) |
| 143 | res=$("$B" enc "$codec" src_1080p30_8bit.yuv "$W" "$H" 1800 8000000 --out "$OUT/enc-$label.bin" --runs 5) |
| 144 | best=$(echo "$res" | sed -n 's/^BEST|.*best_s=\([^ ]*\).*/\1/p') |
| 145 | fps=$(echo "$res" | sed -n 's/^BEST|.*fps=\([^ ]*\).*/\1/p') |
| 146 | x=$(echo "$res" | sed -n 's/^BEST|.*x_realtime=\([^ ]*\).*/\1/p') |
| 147 | kbps=$(echo "$res" | sed -n 's/^BEST|.*kbps=\([^ ]*\).*/\1/p') |
| 148 | echo "ROW|ec-hw|encode $label|VA-API $label 1080p30 CBR 8 Mb/s GOP 30, 60 s|best ${best} s|${fps} fps, ${x}x realtime, ${kbps} kb/s|load1=$l runs=5 best" |
| 149 | |
| 150 | local ff_out nb psnr |
| 151 | ff_out=$("${FF[@]}" -f "$ffmt" -i "$OUT/enc-$label.bin" -f rawvideo -pix_fmt yuv420p "$OUT/enc-$label.ref.yuv" 2>&1 && echo ok || echo FAIL) |
| 152 | nb=$(stat -c%s "$OUT/enc-$label.ref.yuv") |
| 153 | nb=$((nb / FB8)) |
| 154 | psnr=$(ffmpeg -hide_banner -nostats -f "$ffmt" -i "$OUT/enc-$label.bin" -f rawvideo -s ${W}x${H} -pix_fmt yuv420p -i src_1080p30_8bit.yuv -lavfi "[0:v][1:v]psnr" -f null - 2>&1 | sed -n 's/.*average:\([0-9.inf]*\).*/\1/p' | tail -1) |
| 155 | echo "CHECK|ec-hw encode $label|ffmpeg decode: $ff_out, pictures=$nb of 1800, PSNR vs source avg ${psnr} dB" |
| 156 | rm -f "$OUT/enc-$label.ref.yuv" |
| 157 | } |
| 158 | enc_row h264-high h264 h264 |
| 159 | enc_row hevc-main hevc hevc |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | for spec in "h264|libx264|h264" "hevc|libx265|hevc"; do |
| 165 | IFS='|' read -r lbl enc ffmt <<<"$spec" |
| 166 | "${FF[@]}" -f rawvideo -pix_fmt yuv420p -s ${W}x${H} -r 30 -i src_1080p30_8bit.yuv -c:v "$enc" \ |
| 167 | -preset veryfast -b:v 8M -maxrate 8M -bufsize 1M -g 30 -f "$ffmt" "$OUT/cal-$lbl.bin" |
| 168 | psnr=$(ffmpeg -hide_banner -nostats -f "$ffmt" -i "$OUT/cal-$lbl.bin" -f rawvideo -s ${W}x${H} -pix_fmt yuv420p -i src_1080p30_8bit.yuv -lavfi "[0:v][1:v]psnr" -f null - 2>&1 | sed -n 's/.*average:\([0-9.inf]*\).*/\1/p' | tail -1) |
| 169 | echo "CHECK|calibration $enc 8 Mb/s GOP 30|PSNR vs source avg ${psnr} dB (same source, ffmpeg software encoder)" |
| 170 | done |
| 171 | |
| 172 | cd "$REPO" |
| 173 | echo "# done" |