forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB3f482f8Tahinli1 month ago3898 commits

bench-benchhw.sh173 lines, 9.2 KiBHistoryRawDownload
1#!/usr/bin/env bash
2# bench-benchhw.sh: the VA-API rows of lanes/bench.md ("lane benchhw", DONE.md gate G6).
3#
4# Local machine only, with the GPU (radeonsi VA-API; `vainfo` lists the profiles). Clips are
5# synthetic, generated here with the local ffmpeg (testsrc2 + noise, 1080p30, 60 s), and live in
6# $BENCH_CLIPS (default ~/.cache/benchhw-clips), outside the repo.
7#
8# Rows:
9# decode ec-hw Decoder, H.264 High, HEVC Main, HEVC Main10, VP9 profile 0, AV1 8-bit.
10# Each row: best of 5 timed runs (GPU waited per picture), then one readback pass whose
11# frames are compared md5-per-frame with ffmpeg's software decode of the same file.
12# encode ec-hw Encoder, H.264 High and HEVC Main, 1080p30 CBR 8 Mb/s, GOP 30, 1800 pictures.
13# Best of 5; the stream is decoded by ffmpeg (exit status, frame count, PSNR vs source).
14#
15# Output: one "ROW|..." line per row, then "CHECK|..." lines with the comparison details.
16# The lanes/bench.md "lane benchhw" table is written from these lines.
17#
18# usage: scripts/bench-benchhw.sh [--no-clips] [--no-build]
19set -euo pipefail
20
21cd "$(dirname "$0")/.."
22REPO="$(pwd)"
23CLIPS="${BENCH_CLIPS:-$HOME/.cache/benchhw-clips}"
24BIN="${BENCH_BIN:-$HOME/.cache/benchhw-bin}"
25OUT="$CLIPS/out"
26export CARGO_TARGET_DIR="${CARGO_TARGET_DIR:-$HOME/.cache/tgt-benchhw}"
27export RUSTC_WRAPPER=
28export TMPDIR="${TMPDIR:-$HOME/.cache/tmp-benchhw}"
29FF=(ffmpeg -hide_banner -loglevel error -y)
30DO_CLIPS=1
31DO_BUILD=1
32for 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
38done
39mkdir -p "$CLIPS" "$BIN" "$OUT" "$TMPDIR"
40COMMIT="$(git -C "$REPO" rev-parse --short HEAD)"
41echo "# 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
43SRC="testsrc2=size=1920x1080:rate=30:duration=60"
44W=1920
45H=1080
46
47# ---- 1. synthetic clips -------------------------------------------------------------------
48if [ "$DO_CLIPS" = 1 ]; then
49 cd "$CLIPS"
50 # Raw 8-bit 4:2:0 source for the encode rows (1800 pictures, about 5.6 GB).
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 # Decode inputs. Short GOP (30 pictures = 1 s). Annex B for H.264/HEVC, IVF for VP9/AV1.
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 # References: ffmpeg's software decoders, native pixel format, every picture.
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"
75fi
76
77# ---- 2. build the bench example -----------------------------------------------------------
78if [ "$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"
81fi
82B="$BIN/ec-hw-bench"
83
84# ---- 3. helpers --------------------------------------------------------------------------
85wait_load() {
86 # Start a row only when the 1-minute load is below 3.
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}
93field() { echo "$1" | tr ' ' '\n' | sed -n "s/^$2=//p"; }
94# framecmp REF TEST FRAME_BYTES: per-frame md5 comparison; prints frames, equal count, first diff.
95framecmp() {
96 python3 -I - "$1" "$2" "$3" <<'PY'
97import hashlib, sys
98ref, test, fb = sys.argv[1], sys.argv[2], int(sys.argv[3])
99def 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
108r, t = md5s(ref), md5s(test)
109same = sum(1 for a, b in zip(r, t) if a == b)
110first = next((i for i, (a, b) in enumerate(zip(r, t)) if a != b), None)
111print(f"frames_ref={len(r)} frames_test={len(t)} frames_md5_equal={same} first_diff={first if first is not None else 'none'}")
112PY
113}
114
115# ---- 4. decode rows ----------------------------------------------------------------------
116cd "$CLIPS"
117dec_row() { # label codec stream ref frame_bytes
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}
130FB8=$((W * H * 3 / 2))
131FB10=$((W * H * 3))
132dec_row h264-high h264 h264_high_1080p30.264 ref_h264.yuv "$FB8"
133dec_row hevc-main hevc hevc_main_1080p30.265 ref_hevc.yuv "$FB8"
134dec_row hevc-main10 hevc hevc_main10_1080p30.265 ref_hevc10.yuv "$FB10"
135dec_row vp9-profile0 vp9 vp9_profile0_1080p30.ivf ref_vp9.yuv "$FB8"
136dec_row av1-8bit av1 av1_8bit_1080p30.ivf ref_av1.yuv "$FB8"
137
138# ---- 5. encode rows ----------------------------------------------------------------------
139enc_row() { # label codec ffmt
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 # ffmpeg decodes our stream: exit status, picture count, and PSNR against the source.
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}
158enc_row h264-high h264 h264
159enc_row hevc-main hevc hevc
160
161# Calibration for the PSNR above: ffmpeg's libx264 and libx265 at the same 8 Mb/s, GOP 30, the
162# same source, decoded and measured the same way. Not a bench row; it says how much of the PSNR
163# is the bitrate and the noisy source, not the hardware encoder.
164for 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)"
170done
171
172cd "$REPO"
173echo "# done"