| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | set -euo pipefail |
| 16 | |
| 17 | cd "$(dirname "$0")/.." |
| 18 | REPO="$(pwd)" |
| 19 | export RUSTC_WRAPPER= |
| 20 | export CARGO_TARGET_DIR="${CARGO_TARGET_DIR:-$HOME/.cache/tgt-benchrest}" |
| 21 | export TMPDIR="${TMPDIR:-$HOME/.cache/tmp-benchrest}" |
| 22 | IN="${BENCH_INPUTS:-$TMPDIR/inputs}" |
| 23 | BIN="$TMPDIR/bin" |
| 24 | OUT="$IN/out" |
| 25 | FF=(ffmpeg -hide_banner -loglevel error -y) |
| 26 | DO_INPUTS=1 |
| 27 | DO_BUILD=1 |
| 28 | for 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 |
| 34 | done |
| 35 | mkdir -p "$IN" "$BIN" "$OUT" "$TMPDIR" |
| 36 | COMMIT="$(git -C "$REPO" rev-parse --short HEAD)" |
| 37 | echo "# 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 | |
| 40 | if [ "$DO_INPUTS" = 1 ]; then |
| 41 | cd "$IN" |
| 42 | |
| 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 | |
| 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 | |
| 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 | |
| 75 | |
| 76 | [ -f big_mixed.zlib ] || python3 -I - "$IN" <<'PY' |
| 77 | import hashlib, random, string, struct, sys, zlib |
| 78 | import numpy as np |
| 79 | d = sys.argv[1] |
| 80 | rng = random.Random(1) |
| 81 | words = [''.join(rng.choices(string.ascii_lowercase, k=rng.randint(2, 9))) for _ in range(4000)] |
| 82 | def text(n): |
| 83 | return ' '.join(rng.choices(words, k=n // 5)).encode()[:n] |
| 84 | def 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] |
| 90 | big = text(32 << 20) + pcm(32 << 20) |
| 91 | comp = zlib.compress(big, 6) |
| 92 | open(f"{d}/big_mixed.zlib", "wb").write(comp) |
| 93 | open(f"{d}/ref_big.md5", "w").write(hashlib.md5(zlib.decompress(comp)).hexdigest() + "\n") |
| 94 | del big |
| 95 | blocks = open(f"{d}/blocks_3k.bin", "wb") |
| 96 | digest = hashlib.md5() |
| 97 | for _ 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)) |
| 101 | blocks.close() |
| 102 | open(f"{d}/ref_blocks.md5", "w").write(digest.hexdigest() + "\n") |
| 103 | PY |
| 104 | cd "$REPO" |
| 105 | fi |
| 106 | |
| 107 | |
| 108 | crates=(ec-image ec-matroska ec-flac ec-inflate) |
| 109 | if [ "$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 |
| 114 | fi |
| 115 | |
| 116 | |
| 117 | wait_load() { |
| 118 | |
| 119 | |
| 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 | } |
| 128 | field() { echo "$1" | tr ' ' '\n' | sed -n "s/^$2=//p"; } |
| 129 | md5_of() { md5sum "$1" | cut -c1-32; } |
| 130 | |
| 131 | psnr() { |
| 132 | python3 -I - "$1" "$2" "$3" "$4" <<'PY' |
| 133 | import sys |
| 134 | import numpy as np |
| 135 | a = np.fromfile(sys.argv[1], np.uint8).astype(np.int32) |
| 136 | b = np.fromfile(sys.argv[2], np.uint8).astype(np.int32) |
| 137 | w, h = int(sys.argv[3]), int(sys.argv[4]) |
| 138 | assert a.size == b.size == w * h * 3, (a.size, b.size) |
| 139 | d = np.abs(a - b) |
| 140 | mse = float(np.mean((a - b) ** 2)) |
| 141 | psnr = 10 * np.log10(255 * 255 / mse) if mse > 0 else float("inf") |
| 142 | print(f"max_abs_diff={d.max()} mean_abs_diff={d.mean():.4f} psnr={psnr:.2f}dB exact_samples={np.mean(d == 0) * 100:.2f}%") |
| 143 | PY |
| 144 | } |
| 145 | |
| 146 | |
| 147 | cd "$IN" |
| 148 | B="$BIN" |
| 149 | FFV="$(ffmpeg -version | head -1 | awk '{print $3}')" |
| 150 | |
| 151 | |
| 152 | l=$(wait_load) |
| 153 | res=$("$B/ec-image-bench" png-dec png_rgb.png "$OUT/png_rgb.rgb" 5) |
| 154 | best=$(field "$res" best_decode_s) |
| 155 | echo "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" |
| 156 | if [ "$(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)" |
| 158 | else |
| 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)" |
| 160 | fi |
| 161 | |
| 162 | |
| 163 | l=$(wait_load) |
| 164 | res=$("$B/ec-image-bench" jpeg-dec jpg_420_q2.jpg "$OUT/jpg.rgb" 5) |
| 165 | best=$(field "$res" best_decode_s) |
| 166 | echo "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" |
| 167 | echo "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 | |
| 170 | l=$(wait_load) |
| 171 | res=$("$B/ec-image-bench" png-enc src_rgba.raw 1920 1080 "$OUT/enc.png" 5) |
| 172 | best=$(field "$res" best_encode_s) |
| 173 | echo "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" |
| 175 | if [ "$(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)" |
| 177 | else |
| 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)" |
| 179 | fi |
| 180 | |
| 181 | |
| 182 | file_bytes=$(stat -c%s h264_aac_1080p30.mkv) |
| 183 | l=$(wait_load) |
| 184 | res=$("$B/ec-matroska-bench" demux h264_aac_1080p30.mkv "$OUT/mkv" 5) |
| 185 | pk=$(field "$res" packets) |
| 186 | echo "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" |
| 187 | v_ok=$([ "$(md5_of "$OUT/mkv.s0.bin")" = "$(md5_of ref_mkv.v.bin)" ] && echo match || echo MISMATCH) |
| 188 | a_ok=$([ "$(md5_of "$OUT/mkv.s1.bin")" = "$(md5_of ref_mkv.a.bin)" ] && echo match || echo MISMATCH) |
| 189 | ff_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) )) |
| 190 | echo "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 | |
| 193 | for 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 |
| 205 | done |
| 206 | |
| 207 | |
| 208 | for 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 |
| 221 | done |
| 222 | |
| 223 | |
| 224 | l=$(wait_load) |
| 225 | res=$("$B/ec-inflate-bench" zlib big_mixed.zlib "$OUT/big.bin" 5) |
| 226 | best=$(field "$res" best_inflate_s) |
| 227 | echo "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)" |
| 228 | if [ "$(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)" |
| 230 | else |
| 231 | echo "CHECK|ec-inflate zlib|md5 MISMATCH vs python: ours $(md5_of "$OUT/big.bin") python $(cut -d' ' -f1 ref_big.md5)" |
| 232 | fi |
| 233 | rm -f "$OUT/big.bin" |
| 234 | |
| 235 | |
| 236 | l=$(wait_load) |
| 237 | res=$("$B/ec-inflate-bench" blocks blocks_3k.bin "$OUT/blocks.bin" 5) |
| 238 | best=$(field "$res" best_inflate_s) |
| 239 | echo "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" |
| 240 | if [ "$(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)" |
| 242 | else |
| 243 | echo "CHECK|ec-inflate blocks|md5 MISMATCH vs python: ours $(md5_of "$OUT/blocks.bin") python $(cut -d' ' -f1 ref_blocks.md5)" |
| 244 | fi |
| 245 | rm -f "$OUT/blocks.bin" |
| 246 | echo "# ffmpeg $FFV" |