| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | set -euo pipefail |
| 14 | |
| 15 | FUZZ_DIR=$(cd -- "$(dirname -- "$0")" && pwd) |
| 16 | REPO=$(cd -- "$FUZZ_DIR/../../.." && pwd) |
| 17 | FIXTURES="$REPO/fixtures" |
| 18 | [ -d "$FIXTURES/audio" ] || { echo "seed.sh: $FIXTURES/audio missing; run scripts/link-fixtures.sh" >&2; exit 2; } |
| 19 | |
| 20 | HEADERS_DIR="$FUZZ_DIR/headers" |
| 21 | SETUP_DIR="$FUZZ_DIR/corpus/setup" |
| 22 | DECODE_DIR="$FUZZ_DIR/corpus/decode" |
| 23 | mkdir -p "$HEADERS_DIR" "$SETUP_DIR" "$DECODE_DIR" |
| 24 | |
| 25 | python3 -I - "$FIXTURES" "$HEADERS_DIR" "$SETUP_DIR" "$DECODE_DIR" <<'PY' |
| 26 | import glob, os, sys |
| 27 | |
| 28 | fixtures, headers_dir, setup_dir, decode_dir = sys.argv[1:5] |
| 29 | |
| 30 | def 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 | |
| 56 | def 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 | |
| 64 | def 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 | |
| 71 | def is_vorbis(hdr): |
| 72 | return len(hdr) >= 30 and hdr[0] == 1 and hdr[1:7] == b"vorbis" |
| 73 | |
| 74 | files = sorted(glob.glob(os.path.join(fixtures, "audio", "*vorbis*.ogg"))) |
| 75 | files += sorted(glob.glob(os.path.join(fixtures, "vectors", "vorbis-xiph", "*.ogg"))) |
| 76 | |
| 77 | # Fixed header sets: one fixture per layout, the 48 kHz ones. |
| 78 | layouts = { |
| 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 | } |
| 83 | by_name = {} |
| 84 | for path in files: |
| 85 | by_name[os.path.basename(path)] = path |
| 86 | for 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. |
| 94 | setup_count = 0 |
| 95 | for 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. |
| 105 | selector = {1: 0, 2: 1, 6: 2} |
| 106 | decode_count = 0 |
| 107 | for 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 | |
| 129 | print(f"seeded {setup_count} setup and {decode_count} decode seeds; headers in {headers_dir}") |
| 130 | PY |