forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB2602aecTahinli6 days ago3958 commits

bench-benchaudio.sh237 lines, 14.5 KiBHistoryRawDownload
1#!/usr/bin/env bash
2# bench-benchaudio.sh: the audio rows of lanes/bench.md ("lane benchaudio", DONE.md gate G6).
3#
4# Local machine only (DONE.md D7 exception): build with -j6 into CARGO_TARGET_DIR and run each
5# row as the best of 5 inside the bench binary. Before each row, wait until the 1-minute load is
6# below 3. Clips are synthetic and generated here with the local ffmpeg (never personal media);
7# they live in $BENCH_CLIPS (default ~/.cache/benchaudio-clips), outside the repo.
8#
9# Output: one "ROW|..." line per bench row (crate, op, content, best time, x realtime or rates,
10# and the output check), then "CHECK|..." lines with the comparison details. The lanes/bench.md
11# "lane benchaudio" table is written from these lines.
12#
13# usage: scripts/bench-benchaudio.sh [--no-clips] [--no-build]
14set -euo pipefail
15
16cd "$(dirname "$0")/.."
17REPO="$(pwd)"
18CLIPS="${BENCH_CLIPS:-$HOME/.cache/benchaudio-clips}"
19BIN="${BENCH_BIN:-$HOME/.cache/benchaudio-bin}"
20OUT="$CLIPS/out"
21export CARGO_TARGET_DIR="${CARGO_TARGET_DIR:-$HOME/.cache/tgt-benchaudio}"
22export RUSTC_WRAPPER=
23export TMPDIR="${TMPDIR:-$HOME/.cache/tmp-benchaudio}"
24FF=(ffmpeg -hide_banner -loglevel error -y)
25DO_CLIPS=1
26DO_BUILD=1
27for a in "$@"; do
28 case "$a" in
29 --no-clips) DO_CLIPS=0 ;;
30 --no-build) DO_BUILD=0 ;;
31 *) echo "unknown option: $a" >&2; exit 2 ;;
32 esac
33done
34mkdir -p "$CLIPS" "$BIN" "$OUT" "$TMPDIR"
35COMMIT="$(git -C "$REPO" rev-parse --short HEAD)"
36echo "# host=local cores=$(nproc) commit=$COMMIT ffmpeg=$(ffmpeg -version | head -1 | awk '{print $3}')"
37
38# ---- 1. synthetic clips -------------------------------------------------------------------
39if [ "$DO_CLIPS" = 1 ]; then
40 cd "$CLIPS"
41 if [ ! -f src_stereo.wav ]; then
42 # 60 s stereo: tones with a slow AM, plus two independent pink-noise channels.
43 "${FF[@]}" -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.1*sin(2*PI*329.63*t)|0.25*sin(2*PI*222*t)+0.12*sin(2*PI*280*t)*(0.5+0.5*sin(2*PI*2*t))+0.1*sin(2*PI*331*t)':s=48000:d=60" \
44 -f lavfi -i "anoisesrc=d=60:c=pink:a=0.03:s=48000:r=48000:seed=1" \
45 -f lavfi -i "anoisesrc=d=60:c=pink:a=0.03:s=48000:r=48000:seed=2" \
46 -filter_complex "[1:a][2:a]amerge=inputs=2,aformat=channel_layouts=stereo[n];[0:a][n]amix=inputs=2:normalize=0,aformat=sample_fmts=s16:channel_layouts=stereo[o]" \
47 -map "[o]" -c:a pcm_s16le src_stereo.wav
48 fi
49 if [ ! -f src_stereo.f32 ]; then "${FF[@]}" -i src_stereo.wav -f f32le -acodec pcm_f32le src_stereo.f32; fi
50 if [ ! -f src_51.thd ]; then
51 # 60 s 5.1 for TrueHD: six tones plus six independent pink-noise channels.
52 "${FF[@]}" -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" \
53 -f lavfi -i "anoisesrc=d=60:c=pink:a=0.03:s=48000:r=48000:seed=3" \
54 -f lavfi -i "anoisesrc=d=60:c=pink:a=0.03:s=48000:r=48000:seed=4" \
55 -f lavfi -i "anoisesrc=d=60:c=pink:a=0.03:s=48000:r=48000:seed=5" \
56 -f lavfi -i "anoisesrc=d=60:c=pink:a=0.03:s=48000:r=48000:seed=6" \
57 -f lavfi -i "anoisesrc=d=60:c=pink:a=0.03:s=48000:r=48000:seed=7" \
58 -f lavfi -i "anoisesrc=d=60:c=pink:a=0.03:s=48000:r=48000:seed=8" \
59 -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=sample_fmts=s32:channel_layouts=5.1[o]" \
60 -map "[o]" -c:a truehd -strict -2 -f truehd src_51.thd
61 fi
62 # AAC-LC 128k stereo (libfdk_aac) and Opus 96k, Vorbis q5 (libvorbis), MP3 192k reference.
63 [ -f aac_lc_128k.adts ] || "${FF[@]}" -i src_stereo.wav -c:a libfdk_aac -profile:a aac_low -b:a 128k -f adts aac_lc_128k.adts
64 [ -f opus_96k.ogg ] || "${FF[@]}" -i src_stereo.wav -c:a libopus -b:a 96k -f ogg opus_96k.ogg
65 [ -f vorbis_q5.ogg ] || "${FF[@]}" -i src_stereo.wav -c:a libvorbis -q:a 5 -f ogg vorbis_q5.ogg
66 # HE-AAC v1: the local libfdk_aac cannot encode SBR (it fails "Unable to set the AOT 5" at every
67 # rate and bitrate tried), so the stream is the 2.1 s HE-AAC v1 fixture of the repo
68 # (ec-aac tests/data/heaac_44100_48k_implicit_2s.aac, implicit SBR), repeated 28 times to 60 s.
69 if [ ! -f aac_he_v1_60s.adts ]; then
70 fx="$REPO/crates/ec-aac/tests/data/heaac_44100_48k_implicit_2s.aac"
71 : > aac_he_v1_60s.adts
72 for _ in $(seq 28); do cat "$fx" >> aac_he_v1_60s.adts; done
73 fi
74 # H.264 1080p30 + AAC stereo, 60 s synthetic (testsrc2 video, the tones+noise audio).
75 [ -f h264_aac_1080p30.mp4 ] || "${FF[@]}" -f lavfi -i "testsrc2=size=1920x1080:rate=30:duration=60" -i src_stereo.wav \
76 -c:v libx264 -preset veryfast -b:v 4M -pix_fmt yuv420p -c:a aac -b:a 128k -shortest h264_aac_1080p30.mp4
77 # References for the output checks (ffmpeg's own decoders and remux).
78 [ -f ref_aac_lc.f32le ] || "${FF[@]}" -i aac_lc_128k.adts -f f32le -acodec pcm_f32le ref_aac_lc.f32le
79 [ -f ref_aac_he.f32le ] || "${FF[@]}" -i aac_he_v1_60s.adts -f f32le -acodec pcm_f32le ref_aac_he.f32le
80 [ -f ref_opus.f32le ] || "${FF[@]}" -i opus_96k.ogg -f f32le -acodec pcm_f32le ref_opus.f32le
81 [ -f ref_vorbis.f32le ] || "${FF[@]}" -i vorbis_q5.ogg -f f32le -acodec pcm_f32le ref_vorbis.f32le
82 [ -f ref_truehd.s32le ] || "${FF[@]}" -i src_51.thd -f s32le -acodec pcm_s32le ref_truehd.s32le
83 [ -f ref_mp4.v.bin ] || "${FF[@]}" -i h264_aac_1080p30.mp4 -map 0:v:0 -c copy -f data ref_mp4.v.bin
84 [ -f ref_mp4.a.bin ] || "${FF[@]}" -i h264_aac_1080p30.mp4 -map 0:a:0 -c copy -f data ref_mp4.a.bin
85 cd "$REPO"
86fi
87
88# ---- 2. build the six examples (one binary per crate, copied before the next build) --------
89crates=(ec-aac ec-opus ec-vorbis ec-mp3 ec-truehd ec-mp4)
90if [ "$DO_BUILD" = 1 ]; then
91 for c in "${crates[@]}"; do
92 cargo build --release -j6 -p "$c" --example bench
93 cp "$CARGO_TARGET_DIR/release/examples/bench" "$BIN/$c-bench"
94 done
95fi
96
97# ---- 3. helpers --------------------------------------------------------------------------
98wait_load() {
99 # Start a row only when the 1-minute load is below 3.
100 while :; do
101 l=$(cut -d' ' -f1 /proc/loadavg)
102 if awk -v l="$l" 'BEGIN{exit !(l < 3)}'; then echo "$l"; return; fi
103 sleep 5
104 done
105}
106field() { echo "$1" | tr ' ' '\n' | sed -n "s/^$2=//p"; }
107ratio() { awk -v a="$1" -v b="$2" 'BEGIN{ if (b > 0) printf "%.2f", a / b; else print "nan" }'; }
108# corr CHECK: min per-channel Pearson correlation at the best lag (+-4096 samples), ref vs test.
109corr() {
110 python3 -I - "$1" "$2" "$3" "$4" "$5" <<'PY'
111import sys
112import numpy as np
113ref_p, test_p, ch, rate, fmt = sys.argv[1], sys.argv[2], int(sys.argv[3]), int(sys.argv[4]), sys.argv[5]
114def load(p):
115 a = np.fromfile(p, '<f4' if fmt == 'f32' else '<i4').astype(np.float64)
116 if fmt == 's32':
117 a /= 2147483648.0
118 n = (a.size // ch) * ch
119 return a[:n].reshape(-1, ch)
120r, t = load(ref_p), load(test_p)
121L = min(2 * rate, r.shape[0] // 2)
122s0 = min(10 * rate, r.shape[0] - L)
123seg = r[s0:s0 + L, 0] - r[s0:s0 + L, 0].mean()
124seg_n = np.linalg.norm(seg)
125best_c, best_lag = -2.0, 0
126for lag in range(-4096, 4097):
127 s = s0 + lag
128 if s < 0 or s + L > t.shape[0]:
129 continue
130 x = t[s:s + L, 0]
131 x = x - x.mean()
132 c = float(np.dot(seg, x) / (seg_n * np.linalg.norm(x) + 1e-30))
133 if c > best_c:
134 best_c, best_lag = c, lag
135i0, i1 = max(0, -best_lag), min(r.shape[0], t.shape[0] - best_lag)
136cs = []
137for k in range(ch):
138 a = r[i0:i1, k] - r[i0:i1, k].mean()
139 b = t[i0 + best_lag:i1 + best_lag, k] - t[i0 + best_lag:i1 + best_lag, k].mean()
140 cs.append(float(np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b) + 1e-30)))
141print(f"corr_min={min(cs):.6f} corr_max={max(cs):.6f} lag={best_lag} ref_frames={r.shape[0]} test_frames={t.shape[0]} overlap_frames={i1 - i0}")
142PY
143}
144md5_of() { md5sum "$1" | cut -c1-8; }
145
146# ---- 4. rows -----------------------------------------------------------------------------
147cd "$CLIPS"
148B="$BIN"
149
150# AAC decode, LC and HE-AAC v1 (ADTS), 2 ch.
151for spec in "ec-aac|aac_lc_128k.adts|AAC-LC stereo 128 kb/s 60 s (libfdk_aac)|ref_aac_lc.f32le" \
152 "ec-aac|aac_he_v1_60s.adts|HE-AAC v1 stereo 64 kb/s 2.1 s fixture x28 (implicit SBR)|ref_aac_he.f32le"; do
153 IFS='|' read -r crate in content ref <<<"$spec"
154 l=$(wait_load)
155 res=$("$B/$crate-bench" dec "$in" "$OUT/aacdec-$in.f32le" 5)
156 best=$(field "$res" best_decode_s); samples=$(field "$res" samples); fails=$(field "$res" frames_failed)
157 audio=$(awk -v s="$samples" 'BEGIN{printf "%.3f", s/2/48000}')
158 echo "ROW|$crate|decode|$content|$best|$(ratio "$audio" "$best")x|load1=$l frames_failed=$fails"
159 c=$(corr "$ref" "$OUT/aacdec-$in.f32le" 2 48000 f32)
160 echo "CHECK|$crate decode $in|vs ffmpeg $(ffmpeg -version | head -1 | awk '{print $3}') native aac: $c"
161done
162
163# AAC-LC encode, stereo 128k: encode the source, then decode the result with ffmpeg and compare.
164l=$(wait_load)
165res=$("$B/ec-aac-bench" enc src_stereo.f32 2 48000 128 "$OUT/aacenc.adts" 5)
166best=$(field "$res" best_encode_s); audio=$(field "$res" audio_s); bytes=$(field "$res" bytes)
167echo "ROW|ec-aac|encode|AAC-LC stereo 128 kb/s from 60 s PCM|$best|$(ratio "$audio" "$best")x|load1=$l kbps=$(awk -v b="$bytes" -v a="$audio" 'BEGIN{printf "%.1f", b*8/a/1000}')"
168"${FF[@]}" -i "$OUT/aacenc.adts" -f f32le -acodec pcm_f32le "$OUT/aacenc-ffdec.f32le"
169echo "CHECK|ec-aac encode|ffmpeg decode of our ADTS vs source: $(corr src_stereo.f32 "$OUT/aacenc-ffdec.f32le" 2 48000 f32)"
170# Calibration: ffmpeg's own AAC encoders at the same 128k, through the same decode and check.
171"${FF[@]}" -i src_stereo.wav -c:a aac -b:a 128k -f adts "$OUT/cal-native.adts"
172"${FF[@]}" -i "$OUT/cal-native.adts" -f f32le -acodec pcm_f32le "$OUT/cal-native.f32le"
173echo "CHECK|ec-aac encode calibration|ffmpeg native aac 128k, same check: $(corr src_stereo.f32 "$OUT/cal-native.f32le" 2 48000 f32)"
174"${FF[@]}" -i src_stereo.wav -c:a libfdk_aac -profile:a aac_low -b:a 128k -f adts "$OUT/cal-fdk.adts"
175"${FF[@]}" -i "$OUT/cal-fdk.adts" -f f32le -acodec pcm_f32le "$OUT/cal-fdk.f32le"
176echo "CHECK|ec-aac encode calibration|ffmpeg libfdk_aac 128k, same check: $(corr src_stereo.f32 "$OUT/cal-fdk.f32le" 2 48000 f32)"
177
178# Opus decode 48k stereo 96k, and encode.
179l=$(wait_load)
180res=$("$B/ec-opus-bench" dec opus_96k.ogg "$OUT/opusdec.f32le" 5)
181best=$(field "$res" best_decode_s); samples=$(field "$res" samples)
182audio=$(awk -v s="$samples" 'BEGIN{printf "%.3f", s/48000}')
183echo "ROW|ec-opus|decode|Opus 48k stereo 96 kb/s 60 s (libopus)|$best|$(ratio "$audio" "$best")x|load1=$l packets_failed=$(field "$res" failed)"
184echo "CHECK|ec-opus decode|vs ffmpeg Opus decode: $(corr ref_opus.f32le "$OUT/opusdec.f32le" 2 48000 f32)"
185
186l=$(wait_load)
187res=$("$B/ec-opus-bench" enc src_stereo.f32 2 96 "$OUT/opusenc.ogg" 5)
188best=$(field "$res" best_encode_s); audio=$(field "$res" audio_s); bytes=$(field "$res" bytes)
189echo "ROW|ec-opus|encode|Opus 48k stereo 96 kb/s 60 s (vbr off, 20 ms frames)|$best|$(ratio "$audio" "$best")x|load1=$l kbps=$(awk -v b="$bytes" -v a="$audio" 'BEGIN{printf "%.1f", b*8/a/1000}')"
190"${FF[@]}" -i "$OUT/opusenc.ogg" -f f32le -acodec pcm_f32le "$OUT/opusenc-ffdec.f32le"
191echo "CHECK|ec-opus encode|ffmpeg decode of our Ogg Opus vs source: $(corr src_stereo.f32 "$OUT/opusenc-ffdec.f32le" 2 48000 f32)"
192
193# Vorbis decode, q5 stereo.
194l=$(wait_load)
195res=$("$B/ec-vorbis-bench" dec vorbis_q5.ogg "$OUT/vorbisdec.f32le" 5)
196best=$(field "$res" best_decode_s); samples=$(field "$res" samples)
197audio=$(awk -v s="$samples" 'BEGIN{printf "%.3f", s/48000}')
198echo "ROW|ec-vorbis|decode|Vorbis q5 stereo 60 s (libvorbis)|$best|$(ratio "$audio" "$best")x|load1=$l"
199echo "CHECK|ec-vorbis decode|vs ffmpeg Vorbis decode: $(corr ref_vorbis.f32le "$OUT/vorbisdec.f32le" 2 48000 f32)"
200
201# MP3 encode, 192k CBR stereo (Layer III); the output is decoded with ffmpeg and compared.
202l=$(wait_load)
203res=$("$B/ec-mp3-bench" enc src_stereo.f32 2 48000 192 "$OUT/mp3enc.mp3" 5)
204best=$(field "$res" best_encode_s); audio=$(field "$res" audio_s); bytes=$(field "$res" bytes)
205echo "ROW|ec-mp3|encode|MP3 192 kb/s CBR stereo 48k 60 s|$best|$(ratio "$audio" "$best")x|load1=$l kbps=$(awk -v b="$bytes" -v a="$audio" 'BEGIN{printf "%.1f", b*8/a/1000}')"
206"${FF[@]}" -i "$OUT/mp3enc.mp3" -f f32le -acodec pcm_f32le "$OUT/mp3enc-ffdec.f32le"
207echo "CHECK|ec-mp3 encode|ffmpeg decode of our MP3 vs source: $(corr src_stereo.f32 "$OUT/mp3enc-ffdec.f32le" 2 48000 f32)"
208
209# TrueHD decode, 5.1 24-bit (S32 container). Lossless: the output must equal ffmpeg's bytes.
210l=$(wait_load)
211res=$("$B/ec-truehd-bench" dec src_51.thd "$OUT/truehd-dec.s32le" 5)
212best=$(field "$res" best_decode_s); audio=$(field "$res" audio_s); fails=$(field "$res" check_failures)
213echo "ROW|ec-truehd|decode|TrueHD 5.1 24-bit 48k 60 s|$best|$(ratio "$audio" "$best")x|load1=$l self_check_failures=$fails"
214if cmp -s ref_truehd.s32le "$OUT/truehd-dec.s32le"; then
215 echo "CHECK|ec-truehd decode|md5 $(md5_of ref_truehd.s32le) = ffmpeg (bit-exact, $(stat -c%s ref_truehd.s32le) bytes)"
216else
217 echo "CHECK|ec-truehd decode|md5 MISMATCH vs ffmpeg: ours $(md5_of "$OUT/truehd-dec.s32le") ffmpeg $(md5_of ref_truehd.s32le)"
218fi
219
220# MP4 demux and mux, H.264 1080p30 + AAC, 60 s.
221file_bytes=$(stat -c%s h264_aac_1080p30.mp4)
222l=$(wait_load)
223res=$("$B/ec-mp4-bench" demux h264_aac_1080p30.mp4 "$OUT/mp4" 5)
224pps=$(field "$res" packets_per_s); mbs=$(field "$res" mb_per_s); pk=$(field "$res" packets)
225echo "ROW|ec-mp4|demux|H.264 1080p30 + AAC 128k stereo, 60 s (${file_bytes} bytes)|$(field "$res" best_demux_s)|$pps packets/s, $mbs MB/s (payload)|load1=$l packets=$pk"
226v_ok=$([ "$(md5_of "$OUT/mp4.s0.bin")" = "$(md5_of ref_mp4.v.bin)" ] && echo match || echo MISMATCH)
227a_ok=$([ "$(md5_of "$OUT/mp4.s1.bin")" = "$(md5_of ref_mp4.a.bin)" ] && echo match || echo MISMATCH)
228echo "CHECK|ec-mp4 demux|payload md5 vs ffmpeg -c copy -f data: video $v_ok, audio $a_ok; packets ours=$pk ffprobe=$(( $(ffprobe -v error -count_packets -select_streams v:0 -show_entries stream=nb_read_packets -of csv=p=0 h264_aac_1080p30.mp4) + $(ffprobe -v error -count_packets -select_streams a:0 -show_entries stream=nb_read_packets -of csv=p=0 h264_aac_1080p30.mp4) ))"
229
230l=$(wait_load)
231res=$("$B/ec-mp4-bench" mux h264_aac_1080p30.mp4 "$OUT/mux.mp4" 5)
232echo "ROW|ec-mp4|mux|H.264 1080p30 + AAC 128k stereo, 60 s (remux of the same packets)|$(field "$res" best_mux_s)|$(field "$res" packets_per_s) packets/s, $(field "$res" mb_per_s) MB/s (payload)|load1=$l"
233"${FF[@]}" -i "$OUT/mux.mp4" -map 0:v:0 -c copy -f data "$OUT/mux.v.bin"
234"${FF[@]}" -i "$OUT/mux.mp4" -map 0:a:0 -c copy -f data "$OUT/mux.a.bin"
235v_ok=$([ "$(md5_of "$OUT/mux.v.bin")" = "$(md5_of ref_mp4.v.bin)" ] && echo match || echo MISMATCH)
236a_ok=$([ "$(md5_of "$OUT/mux.a.bin")" = "$(md5_of ref_mp4.a.bin)" ] && echo match || echo MISMATCH)
237echo "CHECK|ec-mp4 mux|ffmpeg -c copy -f data of our output vs source payload: video $v_ok, audio $a_ok"