forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

seed.sh130 lines, 4.3 KiBHistoryRawDownload
1#!/usr/bin/env bash
2# seed.sh: build the Vorbis fuzz inputs from the repo's Ogg Vorbis fixtures.
3#
4# Writes two things:
5# headers/<set>.bin the identification, comment and setup packets of one fixture per channel
6# layout (mono, stereo, 5.1 at 48 kHz), each u16-BE-length-prefixed. The
7# decode target include_bytes!s these, so they are committed; rerun this
8# script only when the fixture set changes and commit the result.
9# corpus/<target>/ seeds: `setup` gets the three headers of every Ogg Vorbis file;
10# `decode` gets a selector byte plus u16-prefixed audio packets (capped).
11#
12# Corpus lives under fuzz/corpus, which is git-ignored.
13set -euo pipefail
14
15FUZZ_DIR=$(cd -- "$(dirname -- "$0")" && pwd)
16REPO=$(cd -- "$FUZZ_DIR/../../.." && pwd)
17FIXTURES="$REPO/fixtures"
18[ -d "$FIXTURES/audio" ] || { echo "seed.sh: $FIXTURES/audio missing; run scripts/link-fixtures.sh" >&2; exit 2; }
19
20HEADERS_DIR="$FUZZ_DIR/headers"
21SETUP_DIR="$FUZZ_DIR/corpus/setup"
22DECODE_DIR="$FUZZ_DIR/corpus/decode"
23mkdir -p "$HEADERS_DIR" "$SETUP_DIR" "$DECODE_DIR"
24
25python3 -I - "$FIXTURES" "$HEADERS_DIR" "$SETUP_DIR" "$DECODE_DIR" <<'PY'
26import glob, os, sys
27
28fixtures, headers_dir, setup_dir, decode_dir = sys.argv[1:5]
29
30def ogg_packets(path):
31 """Yield the logical packets of an Ogg stream, reassembled across pages."""
32 data = open(path, "rb").read()
33 cur = bytearray()
34 i = 0
35 while i + 27 <= len(data):
36 if data[i:i + 4] != b"OggS":
37 # Lost sync: resync on the next capture pattern.
38 j = data.find(b"OggS", i + 1)
39 if j < 0:
40 return
41 i = j
42 continue
43 nseg = data[i + 26]
44 seg = data[i + 27:i + 27 + nseg]
45 body_len = sum(seg)
46 body = data[i + 27 + nseg:i + 27 + nseg + body_len]
47 p = 0
48 for n in seg:
49 cur += body[p:p + n]
50 p += n
51 if n < 255:
52 yield bytes(cur)
53 cur = bytearray()
54 i += 27 + nseg + body_len
55
56def first_packets(path, count):
57 out = []
58 for pkt in ogg_packets(path):
59 out.append(pkt)
60 if len(out) == count:
61 break
62 return out
63
64def prefixed(packets):
65 out = bytearray()
66 for pkt in packets:
67 pkt = pkt[:65535]
68 out += len(pkt).to_bytes(2, "big") + pkt
69 return bytes(out)
70
71def is_vorbis(hdr):
72 return len(hdr) >= 30 and hdr[0] == 1 and hdr[1:7] == b"vorbis"
73
74files = sorted(glob.glob(os.path.join(fixtures, "audio", "*vorbis*.ogg")))
75files += sorted(glob.glob(os.path.join(fixtures, "vectors", "vorbis-xiph", "*.ogg")))
76
77# Fixed header sets: one fixture per layout, the 48 kHz ones.
78layouts = {
79 "mono-48k": "vorbis-ogg-mono-48000.ogg",
80 "stereo-48k": "vorbis-ogg-stereo-48000.ogg",
81 "surround-48k": "vorbis-ogg-5.1-48000.ogg",
82}
83by_name = {}
84for path in files:
85 by_name[os.path.basename(path)] = path
86for stem, name in layouts.items():
87 path = by_name[name]
88 hdrs = first_packets(path, 3)
89 assert is_vorbis(hdrs[0]) and hdrs[1][0] == 3 and hdrs[2][0] == 5, name
90 with open(os.path.join(headers_dir, stem + ".bin"), "wb") as f:
91 f.write(prefixed(hdrs))
92
93# setup seeds: the three headers of every file, as they are.
94setup_count = 0
95for path in files:
96 hdrs = first_packets(path, 3)
97 if len(hdrs) < 3 or not is_vorbis(hdrs[0]):
98 continue
99 stem = os.path.splitext(os.path.basename(path))[0]
100 with open(os.path.join(setup_dir, stem), "wb") as f:
101 f.write(prefixed(hdrs))
102 setup_count += 1
103
104# decode seeds: one selector byte per channel count, then up to 200 audio packets, capped.
105selector = {1: 0, 2: 1, 6: 2}
106decode_count = 0
107for path in files:
108 hdrs = first_packets(path, 3)
109 if len(hdrs) < 3 or not is_vorbis(hdrs[0]):
110 continue
111 sel = selector.get(hdrs[0][11])
112 if sel is None:
113 continue
114 audio = []
115 total = 0
116 for pkt in ogg_packets(path):
117 if len(audio) >= 3 + 200:
118 break
119 audio.append(pkt)
120 total += len(pkt)
121 if total > 65536:
122 break
123 audio = audio[3:]
124 stem = os.path.splitext(os.path.basename(path))[0]
125 with open(os.path.join(decode_dir, stem), "wb") as f:
126 f.write(bytes([sel]) + prefixed(audio))
127 decode_count += 1
128
129print(f"seeded {setup_count} setup and {decode_count} decode seeds; headers in {headers_dir}")
130PY