forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB03dc6e9Tahinli3 months ago178 commits

gen-fixtures.sh225 lines, 9.1 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# ------------------------------------------------- probe/ALAC extras --------
114# 24-bit ALAC: the only path where the low bytes of a sample ride uncompressed
115# beside the residuals, which 16-bit fixtures never exercise.
116for lspec in "stereo|2" "5.1|6"; do
117 IFS='|' read -r lname nch <<<"$lspec"
118 build_audio_in "$nch" 48000
119 gen "$FIXTURES/audio/alac24-mp4-${lname}-48000.m4a" \
120 "${AIN[@]}" "${AMAP[@]}" -c:a alac -sample_fmt s32p -ar 48000
121done
122
123# Matroska-carried audio: what the probe hands to the Matroska reader rather
124# than parsing itself. One per codec family that has a Matroska mapping.
125for spec in "aac|-c:a aac -b:a 160k" "flac|-c:a flac" "opus|-c:a libopus -b:a 160k"; do
126 IFS='|' read -r cname cargs <<<"$spec"
127 read -r -a CARGS <<<"$cargs"
128 build_audio_in 2 48000
129 gen "$FIXTURES/audio/${cname}-mka-stereo-48000.mka" \
130 "${AIN[@]}" "${AMAP[@]}" "${CARGS[@]}" -ar 48000
131done
132# A film-shaped Matroska: picture and sound in one file, which is how a probe
133# meets one in the real library.
134build_audio_in 2 48000
135gen "$FIXTURES/audio/av-h264-aac-stereo-48000.mkv" \
136 "${AIN[@]}" -f lavfi -i "testsrc2=size=320x240:rate=24:duration=$DUR_A" \
137 "${AMAP[@]}" -map 2:v -c:v libx264 -preset ultrafast -c:a aac -b:a 160k
138
139# Cover art: an mjpeg "video" stream that is a still picture, in the two
140# containers that carry one. A probe that lists it as a video track is broken.
141if [ -s "$FIXTURES/stills/baseline-420.jpg" ]; then
142 build_audio_in 2 44100
143 gen "$FIXTURES/audio/cover-mp3-stereo-44100.mp3" \
144 "${AIN[@]}" -i "$FIXTURES/stills/baseline-420.jpg" \
145 "${AMAP[@]}" -map 2:v -c:a libmp3lame -b:a 192k -c:v copy \
146 -id3v2_version 3 -metadata title="A Tone" -metadata artist="edith_codecs" \
147 -metadata:s:v title="Album cover" -metadata:s:v comment="Cover (front)" \
148 -disposition:v attached_pic
149 gen "$FIXTURES/audio/cover-mp4-stereo-44100.m4a" \
150 "${AIN[@]}" -i "$FIXTURES/stills/baseline-420.jpg" \
151 "${AMAP[@]}" -map 2:v -c:a aac -b:a 160k -c:v copy \
152 -metadata title="A Tone" -metadata artist="edith_codecs" \
153 -disposition:v attached_pic
154fi
155
156# ---------------------------------------------------------------- video -----
157# name|encoder|extra args|pix_fmts (comma-separated)
158VIDEO_CODECS=(
159 "h264|libx264|-preset ultrafast -crf 23|yuv420p"
160 "hevc|libx265|-preset ultrafast -crf 28 -x265-params log-level=error|yuv420p,yuv420p10le"
161 "vp9|libvpx-vp9|-deadline realtime -cpu-used 8 -row-mt 1 -b:v 0 -crf 32|yuv420p,yuv420p10le"
162 "av1|librav1e|-speed 10 -qp 100|yuv420p,yuv420p10le"
163)
164FPS=("23.976|24000/1001" "60|60")
165CONTAINERS=(mp4 mkv)
166
167# gen_video <codec> <encoder> <extra-args-str> <pix_fmt> <fpstag> <fpsval> <WxH> <restag> <container>
168gen_video() {
169 local codec=$1 enc=$2 extra=$3 pix=$4 fpstag=$5 fpsval=$6 size=$7 restag=$8 cont=$9
170 local depth=8 tag=()
171 case $pix in *10le) depth=10 ;; esac
172 [ "$codec" = hevc ] && [ "$cont" = mp4 ] && tag=(-tag:v hvc1)
173 local ex=()
174 read -r -a ex <<<"$extra"
175 gen "$FIXTURES/video/${codec}-${restag}-${fpstag}-${depth}bit.${cont}" \
176 -f lavfi -i "testsrc2=size=${size}:rate=${fpsval}:duration=${DUR_V}" \
177 -pix_fmt "$pix" -c:v "$enc" "${ex[@]}" "${tag[@]}"
178}
179
180for spec in "${VIDEO_CODECS[@]}"; do
181 IFS='|' read -r vname venc vextra vpixs <<<"$spec"
182 IFS=',' read -r -a PIXS <<<"$vpixs"
183 for pix in "${PIXS[@]}"; do
184 for fspec in "${FPS[@]}"; do
185 IFS='|' read -r ftag fval <<<"$fspec"
186 for cont in "${CONTAINERS[@]}"; do
187 gen_video "$vname" "$venc" "$vextra" "$pix" "$ftag" "$fval" 1920x1080 1080p "$cont"
188 done
189 done
190 done
191 # One 4K sample per codec (8-bit, mp4, 23.976) — size/stride edge cases.
192 gen_video "$vname" "$venc" "$vextra" yuv420p 23.976 24000/1001 3840x2160 2160p mp4
193 # 1916x1080: not a multiple of the macroblock/CTU size, so the decoder must
194 # honour the cropping rect / conformance window. The real library has BluRay
195 # remuxes at exactly this size, and every other fixture here is CTU-aligned.
196 case $vname in
197 h264 | hevc) gen_video "$vname" "$venc" "$vextra" yuv420p 23.976 24000/1001 1916x1080 1916x1080 mp4 ;;
198 esac
199done
200
201# Open-GOP H.264: a non-IDR I picture (nal_unit_type 1, not 5) at a GOP
202# boundary, with B-pyramid pictures around it referencing across the
203# boundary. A decoder started fresh at that I (what a seek does) must not
204# hand back a picture whose references it never decoded — the H.264 sibling
205# of the HEVC RASL/NoRaslOutputFlag case.
206gen "$FIXTURES/video/h264-open-gop.mp4" \
207 -f lavfi -i "testsrc2=size=320x240:rate=30:duration=4" -pix_fmt yuv420p \
208 -c:v libx264 -x264-params "keyint=30:min-keyint=30:open-gop=1:bframes=3:b-pyramid=normal:scenecut=0"
209
210# -------------------------------------------------------------- summary -----
211printf '\n%s\n' "== fixtures: $FIXTURES =="
212printf '%-42s %10s %s\n' FILE SIZE PROBE
213total=0
214while IFS= read -r f; do
215 total=$((total + 1))
216 sz=$(stat -c %s -- "$f")
217 p=$(ffprobe -v error -show_entries stream=codec_name,channels,sample_rate,width,height,pix_fmt \
218 -of default=nw=1:nk=1 "$f" 2>/dev/null | tr '\n' ' ')
219 printf '%-42s %10s %s\n' "${f#"$FIXTURES"/}" "$sz" "$p"
220done < <(find "$FIXTURES/audio" "$FIXTURES/video" -type f 2>/dev/null | sort)
221
222printf '\n%s\n' "total=$total generated=$n_made skipped-existing=$n_skip unsupported=${#unsupported[@]} failed=${#failed[@]}"
223for u in "${unsupported[@]:-}"; do [ -n "$u" ] && printf 'UNSUPPORTED %s\n' "$u"; done
224for e in "${failed[@]:-}"; do [ -n "$e" ] && printf 'FAILED %s\n' "$e"; done
225[ "${#failed[@]}" -eq 0 ] || exit 1