| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | set -euo pipefail |
| 15 | |
| 16 | cd "$(dirname "$0")/.." |
| 17 | REPO="$(pwd)" |
| 18 | CLIPS="${BENCH_CLIPS:-$HOME/.cache/benchaudio-clips}" |
| 19 | BIN="${BENCH_BIN:-$HOME/.cache/benchaudio-bin}" |
| 20 | OUT="$CLIPS/out" |
| 21 | export CARGO_TARGET_DIR="${CARGO_TARGET_DIR:-$HOME/.cache/tgt-benchaudio}" |
| 22 | export RUSTC_WRAPPER= |
| 23 | export TMPDIR="${TMPDIR:-$HOME/.cache/tmp-benchaudio}" |
| 24 | FF=(ffmpeg -hide_banner -loglevel error -y) |
| 25 | DO_CLIPS=1 |
| 26 | DO_BUILD=1 |
| 27 | for 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 |
| 33 | done |
| 34 | mkdir -p "$CLIPS" "$BIN" "$OUT" "$TMPDIR" |
| 35 | COMMIT="$(git -C "$REPO" rev-parse --short HEAD)" |
| 36 | echo "# host=local cores=$(nproc) commit=$COMMIT ffmpeg=$(ffmpeg -version | head -1 | awk '{print $3}')" |
| 37 | |
| 38 | |
| 39 | if [ "$DO_CLIPS" = 1 ]; then |
| 40 | cd "$CLIPS" |
| 41 | if [ ! -f src_stereo.wav ]; then |
| 42 | |
| 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 | |
| 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 | |
| 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 | |
| 67 | |
| 68 | |
| 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 | |
| 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 | |
| 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" |
| 86 | fi |
| 87 | |
| 88 | |
| 89 | crates=(ec-aac ec-opus ec-vorbis ec-mp3 ec-truehd ec-mp4) |
| 90 | if [ "$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 |
| 95 | fi |
| 96 | |
| 97 | |
| 98 | wait_load() { |
| 99 | |
| 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 | } |
| 106 | field() { echo "$1" | tr ' ' '\n' | sed -n "s/^$2=//p"; } |
| 107 | ratio() { awk -v a="$1" -v b="$2" 'BEGIN{ if (b > 0) printf "%.2f", a / b; else print "nan" }'; } |
| 108 | |
| 109 | corr() { |
| 110 | python3 -I - "$1" "$2" "$3" "$4" "$5" <<'PY' |
| 111 | import sys |
| 112 | import numpy as np |
| 113 | ref_p, test_p, ch, rate, fmt = sys.argv[1], sys.argv[2], int(sys.argv[3]), int(sys.argv[4]), sys.argv[5] |
| 114 | def 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) |
| 120 | r, t = load(ref_p), load(test_p) |
| 121 | L = min(2 * rate, r.shape[0] // 2) |
| 122 | s0 = min(10 * rate, r.shape[0] - L) |
| 123 | seg = r[s0:s0 + L, 0] - r[s0:s0 + L, 0].mean() |
| 124 | seg_n = np.linalg.norm(seg) |
| 125 | best_c, best_lag = -2.0, 0 |
| 126 | for 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 |
| 135 | i0, i1 = max(0, -best_lag), min(r.shape[0], t.shape[0] - best_lag) |
| 136 | cs = [] |
| 137 | for 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))) |
| 141 | print(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}") |
| 142 | PY |
| 143 | } |
| 144 | md5_of() { md5sum "$1" | cut -c1-8; } |
| 145 | |
| 146 | |
| 147 | cd "$CLIPS" |
| 148 | B="$BIN" |
| 149 | |
| 150 | |
| 151 | for 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" |
| 161 | done |
| 162 | |
| 163 | |
| 164 | l=$(wait_load) |
| 165 | res=$("$B/ec-aac-bench" enc src_stereo.f32 2 48000 128 "$OUT/aacenc.adts" 5) |
| 166 | best=$(field "$res" best_encode_s); audio=$(field "$res" audio_s); bytes=$(field "$res" bytes) |
| 167 | echo "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" |
| 169 | echo "CHECK|ec-aac encode|ffmpeg decode of our ADTS vs source: $(corr src_stereo.f32 "$OUT/aacenc-ffdec.f32le" 2 48000 f32)" |
| 170 | |
| 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" |
| 173 | echo "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" |
| 176 | echo "CHECK|ec-aac encode calibration|ffmpeg libfdk_aac 128k, same check: $(corr src_stereo.f32 "$OUT/cal-fdk.f32le" 2 48000 f32)" |
| 177 | |
| 178 | |
| 179 | l=$(wait_load) |
| 180 | res=$("$B/ec-opus-bench" dec opus_96k.ogg "$OUT/opusdec.f32le" 5) |
| 181 | best=$(field "$res" best_decode_s); samples=$(field "$res" samples) |
| 182 | audio=$(awk -v s="$samples" 'BEGIN{printf "%.3f", s/48000}') |
| 183 | echo "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)" |
| 184 | echo "CHECK|ec-opus decode|vs ffmpeg Opus decode: $(corr ref_opus.f32le "$OUT/opusdec.f32le" 2 48000 f32)" |
| 185 | |
| 186 | l=$(wait_load) |
| 187 | res=$("$B/ec-opus-bench" enc src_stereo.f32 2 96 "$OUT/opusenc.ogg" 5) |
| 188 | best=$(field "$res" best_encode_s); audio=$(field "$res" audio_s); bytes=$(field "$res" bytes) |
| 189 | echo "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" |
| 191 | echo "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 | |
| 194 | l=$(wait_load) |
| 195 | res=$("$B/ec-vorbis-bench" dec vorbis_q5.ogg "$OUT/vorbisdec.f32le" 5) |
| 196 | best=$(field "$res" best_decode_s); samples=$(field "$res" samples) |
| 197 | audio=$(awk -v s="$samples" 'BEGIN{printf "%.3f", s/48000}') |
| 198 | echo "ROW|ec-vorbis|decode|Vorbis q5 stereo 60 s (libvorbis)|$best|$(ratio "$audio" "$best")x|load1=$l" |
| 199 | echo "CHECK|ec-vorbis decode|vs ffmpeg Vorbis decode: $(corr ref_vorbis.f32le "$OUT/vorbisdec.f32le" 2 48000 f32)" |
| 200 | |
| 201 | |
| 202 | l=$(wait_load) |
| 203 | res=$("$B/ec-mp3-bench" enc src_stereo.f32 2 48000 192 "$OUT/mp3enc.mp3" 5) |
| 204 | best=$(field "$res" best_encode_s); audio=$(field "$res" audio_s); bytes=$(field "$res" bytes) |
| 205 | echo "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" |
| 207 | echo "CHECK|ec-mp3 encode|ffmpeg decode of our MP3 vs source: $(corr src_stereo.f32 "$OUT/mp3enc-ffdec.f32le" 2 48000 f32)" |
| 208 | |
| 209 | |
| 210 | l=$(wait_load) |
| 211 | res=$("$B/ec-truehd-bench" dec src_51.thd "$OUT/truehd-dec.s32le" 5) |
| 212 | best=$(field "$res" best_decode_s); audio=$(field "$res" audio_s); fails=$(field "$res" check_failures) |
| 213 | echo "ROW|ec-truehd|decode|TrueHD 5.1 24-bit 48k 60 s|$best|$(ratio "$audio" "$best")x|load1=$l self_check_failures=$fails" |
| 214 | if 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)" |
| 216 | else |
| 217 | echo "CHECK|ec-truehd decode|md5 MISMATCH vs ffmpeg: ours $(md5_of "$OUT/truehd-dec.s32le") ffmpeg $(md5_of ref_truehd.s32le)" |
| 218 | fi |
| 219 | |
| 220 | |
| 221 | file_bytes=$(stat -c%s h264_aac_1080p30.mp4) |
| 222 | l=$(wait_load) |
| 223 | res=$("$B/ec-mp4-bench" demux h264_aac_1080p30.mp4 "$OUT/mp4" 5) |
| 224 | pps=$(field "$res" packets_per_s); mbs=$(field "$res" mb_per_s); pk=$(field "$res" packets) |
| 225 | echo "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" |
| 226 | v_ok=$([ "$(md5_of "$OUT/mp4.s0.bin")" = "$(md5_of ref_mp4.v.bin)" ] && echo match || echo MISMATCH) |
| 227 | a_ok=$([ "$(md5_of "$OUT/mp4.s1.bin")" = "$(md5_of ref_mp4.a.bin)" ] && echo match || echo MISMATCH) |
| 228 | echo "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 | |
| 230 | l=$(wait_load) |
| 231 | res=$("$B/ec-mp4-bench" mux h264_aac_1080p30.mp4 "$OUT/mux.mp4" 5) |
| 232 | echo "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" |
| 235 | v_ok=$([ "$(md5_of "$OUT/mux.v.bin")" = "$(md5_of ref_mp4.v.bin)" ] && echo match || echo MISMATCH) |
| 236 | a_ok=$([ "$(md5_of "$OUT/mux.a.bin")" = "$(md5_of ref_mp4.a.bin)" ] && echo match || echo MISMATCH) |
| 237 | echo "CHECK|ec-mp4 mux|ffmpeg -c copy -f data of our output vs source payload: video $v_ok, audio $a_ok" |