forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

seed.sh101 lines, 4.5 KiBHistoryRawDownload
1#!/usr/bin/env bash
2# seed.sh: fill fuzz/corpus/<target>/ for the ec-riff targets.
3#
4# wav_read gets every root fixtures/audio/*.wav as-is, plus an RF64 copy of each (the RIFF
5# magic swapped for RF64, so the unsupported-RF64 branch is reached too).
6# avi_read and avi_seek get the AVI files in fixtures/audio/*.avi when there are any, plus a
7# few synthetic AVIs written here (the repo has no AVI fixture yet). avi_seek gets
8# the same files behind its 10-byte control prefix.
9#
10# Corpus lives under fuzz/corpus, which is git-ignored.
11set -euo pipefail
12
13FUZZ_DIR=$(cd -- "$(dirname -- "$0")" && pwd)
14FIXTURES=$(cd -- "$FUZZ_DIR/../../.." && pwd)/fixtures/audio
15[ -d "$FIXTURES" ] || { echo "seed.sh: $FIXTURES missing; run scripts/link-fixtures.sh" >&2; exit 2; }
16
17WAVS=("$FIXTURES"/*.wav)
18[ -e "${WAVS[0]}" ] || { echo "seed.sh: no *.wav fixtures in $FIXTURES" >&2; exit 2; }
19
20WAV_DIR="$FUZZ_DIR/corpus/wav_read"
21AVI_DIR="$FUZZ_DIR/corpus/avi_read"
22SEEK_DIR="$FUZZ_DIR/corpus/avi_seek"
23mkdir -p "$WAV_DIR" "$AVI_DIR" "$SEEK_DIR"
24
25python3 -I - "$FIXTURES" "$WAV_DIR" "$AVI_DIR" "$SEEK_DIR" <<'PY'
26import glob, os, struct, sys
27
28fixtures, wav_dir, avi_dir, seek_dir = sys.argv[1:5]
29
30# RF64 copy: "RIFF" -> "RF64" and a ds64 chunk after the WAVE tag. The sizes in the file
31# stay as they were, so the reader still sees a fixed-size data chunk.
32def rf64_copy(data):
33 ds64 = b"ds64" + struct.pack("<I", 28) + bytes(28)
34 return b"RF64" + data[4:8] + b"WAVE" + ds64 + data[12:]
35
36for path in sorted(glob.glob(os.path.join(fixtures, "*.wav"))):
37 stem = os.path.splitext(os.path.basename(path))[0]
38 data = open(path, "rb").read()
39 open(os.path.join(wav_dir, stem), "wb").write(data)
40 open(os.path.join(wav_dir, stem + "-rf64"), "wb").write(rf64_copy(data))
41
42def chunk(id_, body):
43 pad = b"\0" if len(body) & 1 else b""
44 return id_ + struct.pack("<I", len(body)) + body + pad
45
46def lst(kind, body):
47 return chunk(b"LIST", kind + body)
48
49def avi(channels, rate, bits, nchunks, chunk_bytes, idx1=True, pcm_tag=1):
50 align = channels * bits // 8
51 wfx = struct.pack("<HHIIHH", pcm_tag, channels, rate, rate * align, align, bits)
52 # strh: 56 bytes; scale at 20, rate at 24, length at 32, sampleSize at 44.
53 strh = (b"auds" + bytes(4) + struct.pack("<IHHIIIIII", 0, 0, 0, 0, 1, rate, 0,
54 nchunks * (chunk_bytes // align), 0)
55 + struct.pack("<I", 0) + struct.pack("<I", align) + bytes(8))
56 assert len(strh) == 56, len(strh)
57 hdrl_body = (chunk(b"avih", bytes(56))
58 + lst(b"strl", chunk(b"strh", strh) + chunk(b"strf", wfx)))
59 body = bytearray()
60 entries = []
61 for i in range(nchunks):
62 payload = bytes((i * 37 + k) & 0xFF for k in range(chunk_bytes))
63 entries.append((len(body), len(payload)))
64 body += chunk(b"00wb", payload)
65 movi = lst(b"movi", bytes(body))
66 idx = b""
67 if idx1:
68 # idx1 offsets point at each chunk header, relative to the "movi" list's first chunk.
69 for off, size in entries:
70 idx += b"00wb" + struct.pack("<III", 0x10, off, size)
71 idx = chunk(b"idx1", idx)
72 payload = b"AVI " + chunk(b"LIST", b"hdrl" + hdrl_body) + movi + idx
73 return b"RIFF" + struct.pack("<I", len(payload)) + payload
74
75synth = {
76 "synth-mono-8k-16": avi(1, 8000, 16, 6, 320),
77 "synth-stereo-48k-16": avi(2, 48000, 16, 4, 1024),
78 "synth-stereo-48k-16-noidx": avi(2, 48000, 16, 4, 1024, idx1=False),
79 "synth-float-stereo": avi(2, 44100, 32, 3, 512, pcm_tag=3),
80 "synth-empty-movi": avi(2, 48000, 16, 0, 0),
81}
82for name, data in synth.items():
83 open(os.path.join(avi_dir, name), "wb").write(data)
84
85# Real AVI fixtures, if the repo ever gets some.
86for path in sorted(glob.glob(os.path.join(fixtures, "*.avi"))):
87 stem = os.path.splitext(os.path.basename(path))[0]
88 open(os.path.join(avi_dir, stem), "wb").write(open(path, "rb").read())
89
90# avi_seek: control prefix (flag, stream, target) then the AVI bytes.
91for name, data in list(synth.items()) + [
92 (os.path.splitext(os.path.basename(p))[0], open(p, "rb").read())
93 for p in sorted(glob.glob(os.path.join(fixtures, "*.avi")))
94]:
95 for flag, stream, target in [(0, 0, 0), (1, 1, 5), (0, 0, 1 << 40)]:
96 out = bytes([flag, stream]) + struct.pack("<Q", target) + data
97 open(os.path.join(seek_dir, f"{name}-f{flag}s{stream}"), "wb").write(out)
98
99print(f"seeded {len(os.listdir(wav_dir))} wav_read, {len(os.listdir(avi_dir))} avi_read, "
100 f"{len(os.listdir(seek_dir))} avi_seek seeds")
101PY