forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

seed.sh74 lines, 2.8 KiBHistoryRawDownload
1#!/usr/bin/env bash
2# seed.sh: fill fuzz/corpus/<target>/ from the repo's AAC fixtures (root fixtures/audio).
3#
4# decode_adts gets each ADTS fixture as-is.
5# decode_he gets each HE-AAC v1/v2 ADTS fixture from tests/data/he as-is (no root fixtures needed).
6# decode_raw gets each ADTS fixture re-packed as raw access units: selector byte, then
7# u16 BE length + raw_data_block per ADTS frame. Mono/stereo/5.1 at 48 kHz pick
8# their matching config; the stereo fixtures are also replayed through the HE-AAC
9# config so SBR is reached.
10#
11# Corpus lives under fuzz/corpus, which is git-ignored.
12set -euo pipefail
13
14FUZZ_DIR=$(cd -- "$(dirname -- "$0")" && pwd)
15HE_DIR=$(cd -- "$FUZZ_DIR/../tests/data/he" && pwd)
16HE_CORPUS="$FUZZ_DIR/corpus/decode_he"
17mkdir -p "$HE_CORPUS"
18for f in "$HE_DIR"/*.aac; do cp -f "$f" "$HE_CORPUS/"; done
19echo "seeded $(ls "$HE_CORPUS" | wc -l) decode_he seeds"
20
21FIXTURES=$(cd -- "$FUZZ_DIR/../../.." && pwd)/fixtures/audio
22[ -d "$FIXTURES" ] || { echo "seed.sh: $FIXTURES missing; run scripts/link-fixtures.sh (decode_he is seeded)" >&2; exit 0; }
23
24ADTS=("$FIXTURES"/aac-adts-*.aac)
25[ -e "${ADTS[0]}" ] || { echo "seed.sh: no aac-adts-*.aac fixtures in $FIXTURES" >&2; exit 2; }
26
27ADTS_DIR="$FUZZ_DIR/corpus/decode_adts"
28RAW_DIR="$FUZZ_DIR/corpus/decode_raw"
29mkdir -p "$ADTS_DIR" "$RAW_DIR"
30
31python3 -I - "$FIXTURES" "$ADTS_DIR" "$RAW_DIR" <<'PY'
32import glob, os, sys
33
34fixtures, adts_dir, raw_dir = sys.argv[1], sys.argv[2], sys.argv[3]
35
36def adts_frames(data):
37 """Yield (body) for each ADTS frame; stops at the first malformed header."""
38 i = 0
39 while i + 7 <= len(data):
40 if not (data[i] == 0xFF and (data[i + 1] & 0xF0) == 0xF0):
41 break
42 prot_absent = data[i + 1] & 1
43 hdr = 7 if prot_absent else 9
44 flen = ((data[i + 3] & 3) << 11) | (data[i + 4] << 3) | (data[i + 5] >> 5)
45 if flen < hdr:
46 break
47 yield data[i + hdr:i + flen]
48 i += flen
49
50def selector(name):
51 if "mono" in name:
52 return 0
53 if "stereo" in name:
54 return 1
55 return 3 # 5.1
56
57for path in sorted(glob.glob(os.path.join(fixtures, "aac-adts-*.aac"))):
58 name = os.path.basename(path)
59 stem = os.path.splitext(name)[0]
60 data = open(path, "rb").read()
61 open(os.path.join(adts_dir, stem), "wb").write(data)
62
63 # Raw AUs under the matching config, and (for stereo) under HE-AAC as well.
64 sels = [selector(name)]
65 if "stereo" in name:
66 sels.append(2)
67 for sel in sels:
68 out = bytearray([sel])
69 for body in adts_frames(data):
70 body = body[:65535]
71 out += len(body).to_bytes(2, "big") + body
72 open(os.path.join(raw_dir, f"{stem}-sel{sel}"), "wb").write(bytes(out))
73print(f"seeded {len(os.listdir(adts_dir))} decode_adts and {len(os.listdir(raw_dir))} decode_raw seeds")
74PY