forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB30d372eTahinli3 months ago42 commits

gen-fixtures.sh173 lines, 6.4 KiBHistoryRawDownload
1#!/usr/bin/env bash
2# gen-fixtures.sh — deterministic ffmpeg-generated media fixtures for edith_codecs.
3#
4# ffmpeg is ORACLE/test-input tooling only; no ec-* crate depends on it at runtime.
5# Idempotent: non-empty outputs are skipped, partial outputs are deleted on failure.
6# Content is deterministic (sine tones per channel, testsrc2) so regenerating on
7# another machine with the same ffmpeg yields the same bytes.
8#
9# Usage: scripts/gen-fixtures.sh [-f] (-f: regenerate everything)
10# Env: EC_FIXTURES=<dir> (default <repo>/fixtures)
11
12set -uo pipefail
13
14ROOT=$(cd -- "$(dirname -- "$0")/.." && pwd)
15FIXTURES=${EC_FIXTURES:-$ROOT/fixtures}
16DUR_A=3
17DUR_V=2
18FORCE=0
19[ "${1:-}" = "-f" ] && FORCE=1
20
21command -v ffmpeg >/dev/null || { echo "gen-fixtures: ffmpeg not found" >&2; exit 2; }
22
23TMPLOG=$(mktemp) || exit 2
24trap 'rm -f "$TMPLOG"' EXIT
25
26n_made=0
27n_skip=0
28failed=()
29unsupported=()
30
31# gen <out> <ffmpeg args...>
32gen() {
33 local out=$1
34 shift
35 if [ "$FORCE" -eq 0 ] && [ -s "$out" ]; then
36 n_skip=$((n_skip + 1))
37 return 0
38 fi
39 mkdir -p -- "$(dirname -- "$out")"
40 if ffmpeg -nostdin -y -v error "$@" "$out" 2>"$TMPLOG"; then
41 n_made=$((n_made + 1))
42
43 return 0
44 fi
45 rm -f -- "$out"
46 failed+=("${out#"$FIXTURES"/}: $(tr '\n' ' ' <"$TMPLOG" | cut -c1-140)")
47 return 1
48}
49
50# ---------------------------------------------------------------- audio -----
51# Per-channel distinct frequencies: lets the oracle catch channel-order bugs
52# (5.1 order FL,FR,FC,LFE,BL,BR) that identical-tone fixtures hide.
53FREQS=(220 440 660 55 880 1320)
54
55# build_audio_in <nch> <rate> -> sets AIN[] and AMAP[]
56build_audio_in() {
57 local nch=$1 rate=$2 i lbl="" layout
58 AIN=()
59 for ((i = 0; i < nch; i++)); do
60 AIN+=(-f lavfi -i "sine=frequency=${FREQS[i]}:sample_rate=${rate}:duration=${DUR_A}")
61 done
62 if [ "$nch" -eq 1 ]; then
63 AMAP=(-map 0:a)
64 return
65 fi
66 for ((i = 0; i < nch; i++)); do lbl="${lbl}[${i}:a]"; done
67 case $nch in
68 2) layout=stereo ;;
69 6) layout=5.1 ;;
70 *) layout=$nch ;;
71 esac
72 AMAP=(-filter_complex "${lbl}join=inputs=${nch}:channel_layout=${layout}[a]" -map "[a]")
73}
74
75# name|ext|encoder args
76AUDIO_CODECS=(
77 "wav16|wav|-c:a pcm_s16le"
78 "flac|flac|-c:a flac -compression_level 5"
79 "mp3|mp3|-c:a libmp3lame -b:a 192k"
80 "aac-adts|aac|-c:a aac -b:a 160k"
81 "aac-mp4|mp4|-c:a aac -b:a 160k"
82 "ac3|ac3|-c:a ac3 -b:a 448k"
83 "eac3|eac3|-c:a eac3 -b:a 448k"
84 "vorbis-ogg|ogg|-c:a libvorbis -q:a 5"
85 "opus-ogg|opus|-c:a libopus -b:a 160k -vbr on"
86 "alac-mp4|m4a|-c:a alac"
87)
88LAYOUTS=("mono|1" "stereo|2" "5.1|6")
89RATES=(44100 48000)
90
91for spec in "${AUDIO_CODECS[@]}"; do
92 IFS='|' read -r cname cext cargs <<<"$spec"
93 read -r -a CARGS <<<"$cargs"
94 for lspec in "${LAYOUTS[@]}"; do
95 IFS='|' read -r lname nch <<<"$lspec"
96 for rate in "${RATES[@]}"; do
97 # Encoder capability gates — a skipped combo is reported, never silent.
98 if [ "$cname" = "mp3" ] && [ "$nch" -gt 2 ]; then
99 unsupported+=("audio/${cname}-${lname}-${rate}: libmp3lame is mono/stereo only")
100 continue
101 fi
102 if [ "$cname" = "opus-ogg" ] && [ "$rate" != 48000 ]; then
103 unsupported+=("audio/${cname}-${lname}-${rate}: libopus encodes at 48 kHz only")
104 continue
105 fi
106 build_audio_in "$nch" "$rate"
107 gen "$FIXTURES/audio/${cname}-${lname}-${rate}.${cext}" \
108 "${AIN[@]}" "${AMAP[@]}" "${CARGS[@]}" -ar "$rate"
109 done
110 done
111done
112
113# ---------------------------------------------------------------- video -----
114# name|encoder|extra args|pix_fmts (comma-separated)
115VIDEO_CODECS=(
116 "h264|libx264|-preset ultrafast -crf 23|yuv420p"
117 "hevc|libx265|-preset ultrafast -crf 28 -x265-params log-level=error|yuv420p,yuv420p10le"
118 "vp9|libvpx-vp9|-deadline realtime -cpu-used 8 -row-mt 1 -b:v 0 -crf 32|yuv420p,yuv420p10le"
119 "av1|librav1e|-speed 10 -qp 100|yuv420p,yuv420p10le"
120)
121FPS=("23.976|24000/1001" "60|60")
122CONTAINERS=(mp4 mkv)
123
124# gen_video <codec> <encoder> <extra-args-str> <pix_fmt> <fpstag> <fpsval> <WxH> <restag> <container>
125gen_video() {
126 local codec=$1 enc=$2 extra=$3 pix=$4 fpstag=$5 fpsval=$6 size=$7 restag=$8 cont=$9
127 local depth=8 tag=()
128 case $pix in *10le) depth=10 ;; esac
129 [ "$codec" = hevc ] && [ "$cont" = mp4 ] && tag=(-tag:v hvc1)
130 local ex=()
131 read -r -a ex <<<"$extra"
132 gen "$FIXTURES/video/${codec}-${restag}-${fpstag}-${depth}bit.${cont}" \
133 -f lavfi -i "testsrc2=size=${size}:rate=${fpsval}:duration=${DUR_V}" \
134 -pix_fmt "$pix" -c:v "$enc" "${ex[@]}" "${tag[@]}"
135}
136
137for spec in "${VIDEO_CODECS[@]}"; do
138 IFS='|' read -r vname venc vextra vpixs <<<"$spec"
139 IFS=',' read -r -a PIXS <<<"$vpixs"
140 for pix in "${PIXS[@]}"; do
141 for fspec in "${FPS[@]}"; do
142 IFS='|' read -r ftag fval <<<"$fspec"
143 for cont in "${CONTAINERS[@]}"; do
144 gen_video "$vname" "$venc" "$vextra" "$pix" "$ftag" "$fval" 1920x1080 1080p "$cont"
145 done
146 done
147 done
148 # One 4K sample per codec (8-bit, mp4, 23.976) — size/stride edge cases.
149 gen_video "$vname" "$venc" "$vextra" yuv420p 23.976 24000/1001 3840x2160 2160p mp4
150 # 1916x1080: not a multiple of the macroblock/CTU size, so the decoder must
151 # honour the cropping rect / conformance window. The real library has BluRay
152 # remuxes at exactly this size, and every other fixture here is CTU-aligned.
153 case $vname in
154 h264 | hevc) gen_video "$vname" "$venc" "$vextra" yuv420p 23.976 24000/1001 1916x1080 1916x1080 mp4 ;;
155 esac
156done
157
158# -------------------------------------------------------------- summary -----
159printf '\n%s\n' "== fixtures: $FIXTURES =="
160printf '%-42s %10s %s\n' FILE SIZE PROBE
161total=0
162while IFS= read -r f; do
163 total=$((total + 1))
164 sz=$(stat -c %s -- "$f")
165 p=$(ffprobe -v error -show_entries stream=codec_name,channels,sample_rate,width,height,pix_fmt \
166 -of default=nw=1:nk=1 "$f" 2>/dev/null | tr '\n' ' ')
167 printf '%-42s %10s %s\n' "${f#"$FIXTURES"/}" "$sz" "$p"
168done < <(find "$FIXTURES/audio" "$FIXTURES/video" -type f 2>/dev/null | sort)
169
170printf '\n%s\n' "total=$total generated=$n_made skipped-existing=$n_skip unsupported=${#unsupported[@]} failed=${#failed[@]}"
171for u in "${unsupported[@]:-}"; do [ -n "$u" ] && printf 'UNSUPPORTED %s\n' "$u"; done
172for e in "${failed[@]:-}"; do [ -n "$e" ] && printf 'FAILED %s\n' "$e"; done
173[ "${#failed[@]}" -eq 0 ] || exit 1