forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB214cdf2Tahinli3 months ago821 commits

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