forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiBc256206Tahinli5 days ago4000 commits

bench-benchrest.sh246 lines, 13.9 KiBHistoryRawDownload
1#!/usr/bin/env bash
2# bench-benchrest.sh: the still-image, Matroska, FLAC and zlib rows of lanes/bench.md
3# ("lane benchrest", DONE.md gate G6).
4#
5# Local machine only (DONE.md D7 exception): build with -j6 into CARGO_TARGET_DIR and run each row
6# as the best of 5 inside the bench binary. Before each row, wait up to 40 minutes for the 1-minute
7# load to be at most 2; the load at the start of the row is printed. Inputs are generated here with
8# the local ffmpeg and python (never personal media) into $BENCH_INPUTS, which lives in the tmp dir
9# $TMPDIR (default ~/.cache/tmp-benchrest), outside the repo.
10#
11# Output: one "ROW|..." line per bench row, then "CHECK|..." lines with the self-check. The
12# lanes/bench.md "lane benchrest" table is written from these lines.
13#
14# usage: scripts/bench-benchrest.sh [--no-inputs] [--no-build]
15set -euo pipefail
16
17cd "$(dirname "$0")/.."
18REPO="$(pwd)"
19export RUSTC_WRAPPER=
20export CARGO_TARGET_DIR="${CARGO_TARGET_DIR:-$HOME/.cache/tgt-benchrest}"
21export TMPDIR="${TMPDIR:-$HOME/.cache/tmp-benchrest}"
22IN="${BENCH_INPUTS:-$TMPDIR/inputs}"
23BIN="$TMPDIR/bin"
24OUT="$IN/out"
25FF=(ffmpeg -hide_banner -loglevel error -y)
26DO_INPUTS=1
27DO_BUILD=1
28for a in "$@"; do
29 case "$a" in
30 --no-inputs) DO_INPUTS=0 ;;
31 --no-build) DO_BUILD=0 ;;
32 *) echo "unknown option: $a" >&2; exit 2 ;;
33 esac
34done
35mkdir -p "$IN" "$BIN" "$OUT" "$TMPDIR"
36COMMIT="$(git -C "$REPO" rev-parse --short HEAD)"
37echo "# host=local cores=$(nproc) commit=$COMMIT ffmpeg=$(ffmpeg -version | head -1 | awk '{print $3}') python=$(python3 -c 'import numpy; print("numpy " + numpy.__version__)')"
38
39# ---- 1. inputs ----------------------------------------------------------------------------------
40if [ "$DO_INPUTS" = 1 ]; then
41 cd "$IN"
42 # Still images: one testsrc2 frame at 1920x1080.
43 [ -f png_rgb.png ] || "${FF[@]}" -f lavfi -i "testsrc2=size=1920x1080:rate=1" -frames:v 1 -pix_fmt rgb24 png_rgb.png
44 [ -f ref_png_rgb.rgb ] || "${FF[@]}" -i png_rgb.png -f rawvideo -pix_fmt rgb24 ref_png_rgb.rgb
45 [ -f jpg_420_q2.jpg ] || "${FF[@]}" -f lavfi -i "testsrc2=size=1920x1080:rate=1" -frames:v 1 -c:v mjpeg -q:v 2 -pix_fmt yuvj420p jpg_420_q2.jpg
46 [ -f ref_jpg.rgb ] || "${FF[@]}" -i jpg_420_q2.jpg -f rawvideo -pix_fmt rgb24 ref_jpg.rgb
47 [ -f src_rgba.raw ] || "${FF[@]}" -f lavfi -i "testsrc2=size=1920x1080:rate=1" -frames:v 1 -pix_fmt rgba -f rawvideo src_rgba.raw
48 # Matroska: H.264 1080p30 + AAC 128k stereo, 60 s.
49 [ -f h264_aac_1080p30.mkv ] || "${FF[@]}" -f lavfi -i "testsrc2=size=1920x1080:rate=30:duration=60" \
50 -f lavfi -i "sine=frequency=220:sample_rate=48000:duration=60" -ac 2 \
51 -c:v libx264 -preset veryfast -b:v 4M -pix_fmt yuv420p -c:a aac -b:a 128k -f matroska h264_aac_1080p30.mkv
52 [ -f ref_mkv.v.bin ] || "${FF[@]}" -i h264_aac_1080p30.mkv -map 0:v:0 -c copy -f data ref_mkv.v.bin
53 [ -f ref_mkv.a.bin ] || "${FF[@]}" -i h264_aac_1080p30.mkv -map 0:a:0 -c copy -f data ref_mkv.a.bin
54 # FLAC: 60 s stereo 16-bit 44.1 kHz (tones plus pink noise, as benchaudio) and 60 s 5.1 24-bit 48 kHz.
55 [ -f st16_44k.flac ] || "${FF[@]}" \
56 -f lavfi -i "aevalsrc='0.25*sin(2*PI*220*t)+0.12*sin(2*PI*277.18*t)*(0.5+0.5*sin(2*PI*2*t))|0.25*sin(2*PI*222*t)+0.12*sin(2*PI*280*t)*(0.5+0.5*sin(2*PI*2*t))':s=44100:d=60" \
57 -f lavfi -i "anoisesrc=d=60:c=pink:a=0.03:s=44100:r=44100:seed=1" \
58 -f lavfi -i "anoisesrc=d=60:c=pink:a=0.03:s=44100:r=44100:seed=2" \
59 -filter_complex "[1:a][2:a]amerge=inputs=2,aformat=channel_layouts=stereo[n];[0:a][n]amix=inputs=2:normalize=0,aformat=channel_layouts=stereo[o]" \
60 -map "[o]" -c:a flac -sample_fmt s16 st16_44k.flac
61 [ -f ref_st16.pcm ] || "${FF[@]}" -i st16_44k.flac -f s16le ref_st16.pcm
62 [ -f m51_48k_24.flac ] || "${FF[@]}" \
63 -f lavfi -i "aevalsrc='0.25*sin(2*PI*220*t)|0.25*sin(2*PI*222*t)|0.2*sin(2*PI*110*t)|0.1*sin(2*PI*440*t)|0.15*sin(2*PI*330*t)|0.15*sin(2*PI*150*t)':s=48000:d=60" \
64 -f lavfi -i "anoisesrc=d=60:c=pink:a=0.03:s=48000:r=48000:seed=3" \
65 -f lavfi -i "anoisesrc=d=60:c=pink:a=0.03:s=48000:r=48000:seed=4" \
66 -f lavfi -i "anoisesrc=d=60:c=pink:a=0.03:s=48000:r=48000:seed=5" \
67 -f lavfi -i "anoisesrc=d=60:c=pink:a=0.03:s=48000:r=48000:seed=6" \
68 -f lavfi -i "anoisesrc=d=60:c=pink:a=0.03:s=48000:r=48000:seed=7" \
69 -f lavfi -i "anoisesrc=d=60:c=pink:a=0.03:s=48000:r=48000:seed=8" \
70 -filter_complex "[1:a][2:a][3:a][4:a][5:a][6:a]amerge=inputs=6,aformat=channel_layouts=5.1[n];[0:a][n]amix=inputs=2:normalize=0,aformat=channel_layouts=5.1[o]" \
71 -map "[o]" -c:a flac -sample_fmt s32 -bits_per_raw_sample 24 m51_48k_24.flac
72 [ -f ref_m51.s32 ] || "${FF[@]}" -i m51_48k_24.flac -f s32le ref_m51.s32
73 [ -f ref_m51.s24 ] || "${FF[@]}" -i m51_48k_24.flac -f s24le ref_m51.s24
74 # zlib: 64 MiB of mixed text and PCM (python zlib level 6), and 20k blocks of 3 KiB (python zlib
75 # level 6 each, length-prefixed). The refs are python's md5 of zlib.decompress.
76 [ -f big_mixed.zlib ] || python3 -I - "$IN" <<'PY'
77import hashlib, random, string, struct, sys, zlib
78import numpy as np
79d = sys.argv[1]
80rng = random.Random(1)
81words = [''.join(rng.choices(string.ascii_lowercase, k=rng.randint(2, 9))) for _ in range(4000)]
82def text(n):
83 return ' '.join(rng.choices(words, k=n // 5)).encode()[:n]
84def pcm(n):
85 gen = np.random.default_rng(7)
86 t = np.arange(n // 4)
87 x = 6000 * np.sin(2 * np.pi * 440 * t / 44100) + 2500 * np.sin(2 * np.pi * 659.3 * t / 44100) \
88 + gen.normal(0, 300, t.size)
89 return np.clip(x, -32768, 32767).astype('<i2').repeat(2).tobytes()[:n]
90big = text(32 << 20) + pcm(32 << 20)
91comp = zlib.compress(big, 6)
92open(f"{d}/big_mixed.zlib", "wb").write(comp)
93open(f"{d}/ref_big.md5", "w").write(hashlib.md5(zlib.decompress(comp)).hexdigest() + "\n")
94del big
95blocks = open(f"{d}/blocks_3k.bin", "wb")
96digest = hashlib.md5()
97for _ in range(20000):
98 c = zlib.compress(text(3072), 6)
99 blocks.write(struct.pack('<I', len(c)) + c)
100 digest.update(zlib.decompress(c))
101blocks.close()
102open(f"{d}/ref_blocks.md5", "w").write(digest.hexdigest() + "\n")
103PY
104 cd "$REPO"
105fi
106
107# ---- 2. build the four examples (one binary per crate, copied before the next build) ------------
108crates=(ec-image ec-matroska ec-flac ec-inflate)
109if [ "$DO_BUILD" = 1 ]; then
110 for c in "${crates[@]}"; do
111 cargo build --release -j6 -p "$c" --example bench
112 cp "$CARGO_TARGET_DIR/release/examples/bench" "$BIN/$c-bench"
113 done
114fi
115
116# ---- 3. helpers ----------------------------------------------------------------------------------
117wait_load() {
118 # Start a row when the 1-minute load is at most 2, or after 40 minutes at most (the row then
119 # records the load it actually ran at).
120 local waited=0 l
121 while :; do
122 l=$(cut -d' ' -f1 /proc/loadavg)
123 if awk -v l="$l" 'BEGIN{exit !(l <= 2)}' || [ "$waited" -ge 2400 ]; then echo "$l"; return; fi
124 sleep 5
125 waited=$((waited + 5))
126 done
127}
128field() { echo "$1" | tr ' ' '\n' | sed -n "s/^$2=//p"; }
129md5_of() { md5sum "$1" | cut -c1-32; }
130# psnr CHECK: max abs diff, mean abs diff and PSNR of two 8-bit RGB raw files of the same size.
131psnr() {
132 python3 -I - "$1" "$2" "$3" "$4" <<'PY'
133import sys
134import numpy as np
135a = np.fromfile(sys.argv[1], np.uint8).astype(np.int32)
136b = np.fromfile(sys.argv[2], np.uint8).astype(np.int32)
137w, h = int(sys.argv[3]), int(sys.argv[4])
138assert a.size == b.size == w * h * 3, (a.size, b.size)
139d = np.abs(a - b)
140mse = float(np.mean((a - b) ** 2))
141psnr = 10 * np.log10(255 * 255 / mse) if mse > 0 else float("inf")
142print(f"max_abs_diff={d.max()} mean_abs_diff={d.mean():.4f} psnr={psnr:.2f}dB exact_samples={np.mean(d == 0) * 100:.2f}%")
143PY
144}
145
146# ---- 4. rows -------------------------------------------------------------------------------------
147cd "$IN"
148B="$BIN"
149FFV="$(ffmpeg -version | head -1 | awk '{print $3}')"
150
151# ec-image: PNG decode, 1920x1080 RGB8.
152l=$(wait_load)
153res=$("$B/ec-image-bench" png-dec png_rgb.png "$OUT/png_rgb.rgb" 5)
154best=$(field "$res" best_decode_s)
155echo "ROW|ec-image|png decode|PNG RGB8 1920x1080 (ffmpeg testsrc2 frame)|$best|$(field "$res" mpix_per_s) Mpix/s, $(field "$res" mb_per_s) MB/s (RGB out)|load1=$l"
156if [ "$(md5_of ref_png_rgb.rgb)" = "$(md5_of "$OUT/png_rgb.rgb")" ]; then
157 echo "CHECK|ec-image png decode|md5 $(md5_of ref_png_rgb.rgb | cut -c1-8) = ffmpeg rawvideo rgb24 (bit-exact, $(stat -c%s ref_png_rgb.rgb) bytes)"
158else
159 echo "CHECK|ec-image png decode|md5 MISMATCH vs ffmpeg: ours $(md5_of "$OUT/png_rgb.rgb") ffmpeg $(md5_of ref_png_rgb.rgb)"
160fi
161
162# ec-image: JPEG decode, 1920x1080 4:2:0 q2.
163l=$(wait_load)
164res=$("$B/ec-image-bench" jpeg-dec jpg_420_q2.jpg "$OUT/jpg.rgb" 5)
165best=$(field "$res" best_decode_s)
166echo "ROW|ec-image|jpeg decode|JPEG 4:2:0 q2 1920x1080 (ffmpeg mjpeg -q:v 2)|$best|$(field "$res" mpix_per_s) Mpix/s, $(field "$res" mb_per_s) MB/s (RGB out)|load1=$l"
167echo "CHECK|ec-image jpeg decode|vs ffmpeg rawvideo rgb24 decode: $(psnr "$OUT/jpg.rgb" ref_jpg.rgb 1920 1080) (not bit-exact by design; see report)"
168
169# ec-image: PNG encode of the 1920x1080 RGBA frame, decoded back by ffmpeg.
170l=$(wait_load)
171res=$("$B/ec-image-bench" png-enc src_rgba.raw 1920 1080 "$OUT/enc.png" 5)
172best=$(field "$res" best_encode_s)
173echo "ROW|ec-image|png encode|PNG RGBA8 1920x1080 (raw input from ffmpeg testsrc2)|$best|$(field "$res" mpix_per_s) Mpix/s, $(field "$res" mb_per_s) MB/s (raw in), out $(field "$res" out_bytes) bytes|load1=$l"
174"${FF[@]}" -i "$OUT/enc.png" -f rawvideo -pix_fmt rgba "$OUT/enc-ffdec.rgba"
175if [ "$(md5_of src_rgba.raw)" = "$(md5_of "$OUT/enc-ffdec.rgba")" ]; then
176 echo "CHECK|ec-image png encode|ffmpeg decode of our PNG md5 $(md5_of src_rgba.raw | cut -c1-8) = source RGBA (bit-exact)"
177else
178 echo "CHECK|ec-image png encode|ffmpeg decode of our PNG md5 MISMATCH: ours $(md5_of "$OUT/enc-ffdec.rgba") source $(md5_of src_rgba.raw)"
179fi
180
181# ec-matroska: demux, H.264 1080p30 + AAC 128k stereo, 60 s.
182file_bytes=$(stat -c%s h264_aac_1080p30.mkv)
183l=$(wait_load)
184res=$("$B/ec-matroska-bench" demux h264_aac_1080p30.mkv "$OUT/mkv" 5)
185pk=$(field "$res" packets)
186echo "ROW|ec-matroska|demux|H.264 1080p30 + AAC 128k stereo, 60 s (${file_bytes} bytes)|$(field "$res" best_demux_s)|$(field "$res" packets_per_s) packets/s, $(field "$res" mb_per_s) MB/s (payload)|load1=$l packets=$pk"
187v_ok=$([ "$(md5_of "$OUT/mkv.s0.bin")" = "$(md5_of ref_mkv.v.bin)" ] && echo match || echo MISMATCH)
188a_ok=$([ "$(md5_of "$OUT/mkv.s1.bin")" = "$(md5_of ref_mkv.a.bin)" ] && echo match || echo MISMATCH)
189ff_pk=$(( $(ffprobe -v error -count_packets -select_streams v:0 -show_entries stream=nb_read_packets -of csv=p=0 h264_aac_1080p30.mkv) + $(ffprobe -v error -count_packets -select_streams a:0 -show_entries stream=nb_read_packets -of csv=p=0 h264_aac_1080p30.mkv) ))
190echo "CHECK|ec-matroska demux|payload md5 vs ffmpeg -c copy -f data: video $v_ok, audio $a_ok; packets ours=$pk ffprobe=$ff_pk"
191
192# ec-flac: decode, 16-bit stereo 44.1k 60 s and 24-bit 5.1 48k 60 s.
193for spec in "st16_44k.flac|ref_st16.pcm|FLAC 16-bit stereo 44.1 kHz 60 s (ffmpeg)|2|16|44100" \
194 "m51_48k_24.flac|ref_m51.s32|FLAC 24-bit 5.1 48 kHz 60 s (ffmpeg, s32 container)|6|24|48000"; do
195 IFS='|' read -r in ref content _ _ _ <<<"$spec"
196 l=$(wait_load)
197 res=$("$B/ec-flac-bench" dec "$in" "$OUT/flac-$in.pcm" 5)
198 best=$(field "$res" best_decode_s); xrt=$(field "$res" x_realtime)
199 echo "ROW|ec-flac|decode|$content|$best|${xrt}x realtime|load1=$l"
200 if [ "$(md5_of "$ref")" = "$(md5_of "$OUT/flac-$in.pcm")" ]; then
201 echo "CHECK|ec-flac decode $in|md5 $(md5_of "$ref" | cut -c1-8) = ffmpeg PCM ($(stat -c%s "$ref") bytes, bit-exact)"
202 else
203 echo "CHECK|ec-flac decode $in|md5 MISMATCH vs ffmpeg: ours $(md5_of "$OUT/flac-$in.pcm") ffmpeg $(md5_of "$ref")"
204 fi
205done
206
207# ec-flac: encode at the export level (EncoderConfig::default()), then ffmpeg decode of our output.
208for spec in "ref_st16.pcm|2|16|44100|s16le|FLAC encode 16-bit stereo 44.1 kHz 60 s (default level)" \
209 "ref_m51.s24|6|24|48000|s24le|FLAC encode 24-bit 5.1 48 kHz 60 s (default level)"; do
210 IFS='|' read -r src ch bits rate fmt content <<<"$spec"
211 l=$(wait_load)
212 res=$("$B/ec-flac-bench" enc "$src" "$ch" "$bits" "$rate" "$OUT/enc-$bits.flac" 5)
213 best=$(field "$res" best_encode_s); xrt=$(field "$res" x_realtime)
214 echo "ROW|ec-flac|encode|$content|$best|${xrt}x realtime, $(field "$res" kbps) kb/s|load1=$l"
215 "${FF[@]}" -i "$OUT/enc-$bits.flac" -f "$fmt" "$OUT/enc-$bits-ffdec.pcm"
216 if [ "$(md5_of "$src")" = "$(md5_of "$OUT/enc-$bits-ffdec.pcm")" ]; then
217 echo "CHECK|ec-flac encode $bits-bit|ffmpeg decode of our FLAC md5 $(md5_of "$src" | cut -c1-8) = source PCM (bit-exact)"
218 else
219 echo "CHECK|ec-flac encode $bits-bit|ffmpeg decode of our FLAC md5 MISMATCH: ours $(md5_of "$OUT/enc-$bits-ffdec.pcm") source $(md5_of "$src")"
220 fi
221done
222
223# ec-inflate: one zlib stream, 64 MiB mixed text and PCM.
224l=$(wait_load)
225res=$("$B/ec-inflate-bench" zlib big_mixed.zlib "$OUT/big.bin" 5)
226best=$(field "$res" best_inflate_s)
227echo "ROW|ec-inflate|zlib inflate|zlib, 64 MiB mixed text (32 MiB) and 16-bit PCM (32 MiB), python level 6|$best|$(field "$res" mb_per_s) MB/s out|load1=$l in_bytes=$(field "$res" in_bytes)"
228if [ "$(md5_of "$OUT/big.bin")" = "$(cut -d' ' -f1 ref_big.md5)" ]; then
229 echo "CHECK|ec-inflate zlib|md5 $(cut -c1-8 ref_big.md5) = python zlib.decompress ($(field "$res" out_bytes) bytes)"
230else
231 echo "CHECK|ec-inflate zlib|md5 MISMATCH vs python: ours $(md5_of "$OUT/big.bin") python $(cut -d' ' -f1 ref_big.md5)"
232fi
233rm -f "$OUT/big.bin"
234
235# ec-inflate: 20k length-prefixed zlib streams of 3 KiB text each (subtitle-sized packets).
236l=$(wait_load)
237res=$("$B/ec-inflate-bench" blocks blocks_3k.bin "$OUT/blocks.bin" 5)
238best=$(field "$res" best_inflate_s)
239echo "ROW|ec-inflate|zlib inflate, many blocks|20000 zlib streams of 3 KiB text (python level 6, subtitle-sized)|$best|$(field "$res" mb_per_s) MB/s out, $(field "$res" streams) streams|load1=$l"
240if [ "$(md5_of "$OUT/blocks.bin")" = "$(cut -d' ' -f1 ref_blocks.md5)" ]; then
241 echo "CHECK|ec-inflate blocks|md5 $(cut -c1-8 ref_blocks.md5) = python zlib.decompress per block ($(field "$res" out_bytes) bytes)"
242else
243 echo "CHECK|ec-inflate blocks|md5 MISMATCH vs python: ours $(md5_of "$OUT/blocks.bin") python $(cut -d' ' -f1 ref_blocks.md5)"
244fi
245rm -f "$OUT/blocks.bin"
246echo "# ffmpeg $FFV"