mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 09:18:37 +00:00
rsc8_audio: decode the AWC 0xbd tag as cue records, not a header with a magic
The tool read only the first word of a 0xbd tag and called it a magic
(0xE5C74063); docs/rsc8_audio.md described the tag as a 16-byte header.
Over all of audio.rpf the tag is a headerless list of 16-byte records
{name joaat, value, sample offset, extra}, sorted by offset, and that
value is the first word of only 88,082 of 420,168 tags.
decode_marker_records() decodes the list, every scan counts it in new
marker_* summary keys, and the doc carries the measured counts. The
record layout and the tag-id rule (low byte of the chunk name's joaat)
were read in TrinitronClient/GTA6-Asset-Info structure/03-audio-awc.md
(029e4c3) and re-measured here; no code was copied.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
@ -54,7 +54,7 @@ The exporters use three tag types:
|
||||
| `0x48` | streamed-music descriptor |
|
||||
|
||||
`tools/rsc8_audio.py` additionally decodes the speech companions `0x36` (peaks), `0xa3` (seek
|
||||
table), `0xb2` (loudness), `0xbd` (marker) and the two structural tags below; every other tag type
|
||||
table), `0xb2` (loudness), `0xbd` (cue records) and the two structural tags below; every other tag type
|
||||
is skipped.
|
||||
|
||||
| Tag | Meaning |
|
||||
|
||||
@ -114,7 +114,46 @@ streamed-music descriptor), speech streams carrying `0x5c` consistently carry th
|
||||
| `0x36` | Peak envelope | Array of `u16` amplitude peaks |
|
||||
| `0xa3` | Opus seek table | `u32` cumulative sample offsets per 2048-byte Opus block (step `0x2D00 = 11,520` = `12 × 960` samples) |
|
||||
| `0xb2` | Loudness curve | `(u32 sample_index, f32 dB)` pairs sampled every `4,800` samples (`100 ms` at 48 kHz) |
|
||||
| `0xbd` | Event / marker block | 16-byte header starting with magic `0xE5C74063` |
|
||||
| `0xbd` | Cue list (`markers`) | 16-byte records `{u32 name hash, u32 value, u32 sample offset, u32 extra}`, no header; see [below](#the-0xbd-cue-records) |
|
||||
|
||||
### The `0xbd` cue records
|
||||
|
||||
An earlier version of this table called `0xbd` "a 16-byte header starting with magic `0xE5C74063`". It is a list
|
||||
of 16-byte records with no header, and `0xE5C74063` is one cue name among 167. `tools/rsc8_audio.py` decodes the
|
||||
list (`decode_marker_records`) and counts it in every scan (`marker_*` summary keys: 420,168 streams, 781,350
|
||||
records, 0 invalid on `audio.rpf`). Over all of `audio.rpf`:
|
||||
|
||||
| Check | Result |
|
||||
|---|---:|
|
||||
| `0xbd` tags, records | 420,168 / 781,350 |
|
||||
| tag size a multiple of 16 | 420,168 / 420,168 |
|
||||
| sample offsets never decrease inside a tag | 420,168 / 420,168 |
|
||||
| offset at most the stream's `0x76` sample count (the 419,223 tags whose stream has a `0x76` tag) | 750,020 / 750,020 records |
|
||||
| records per tag | 1 on 88,542, 2 on 330,777, 3 or more on 849 |
|
||||
| distinct name hashes | 167 |
|
||||
| first record's name | `0xdbedd43c` on 330,756 tags, `0xE5C74063` on 88,082, 150 other values on 1,330 |
|
||||
|
||||
```
|
||||
+0 u32 name joaat of the cue name
|
||||
+4 u32 value an f32 for most names; a second name hash for region cues
|
||||
+8 u32 offset sample position in the stream
|
||||
+12 u32 extra 0 on 363,166 records, 1..6 on the rest
|
||||
```
|
||||
|
||||
- **The name is a joaat of a lowercase word.** Four of the stored values are the hashes of `beat` (`0xe89ae78c`,
|
||||
17,770 records), `trackid` (`0xa6d93246`, 1,042), `intro` (`0xda8f5f53`, 835) and `outro` (`0x5d40b28d`, 825),
|
||||
as the [GTA6-Asset-Info](https://github.com/TrinitronClient/GTA6-Asset-Info) byte reference reports
|
||||
(`structure/03-audio-awc.md`, commit `029e4c3`); the hashes were computed here, not copied. The names behind
|
||||
`0xE5C74063` (418,838 records, 402,357 of them on speech streams with a `0x5c` clip) and `0xdbedd43c` (330,756,
|
||||
all on such streams and all with `extra = 0`) are not known.
|
||||
- **`value`** is a whole number stored as `f32` on every record of those six names. On five of the other names it
|
||||
is `joaat("start")` or `joaat("end")` in equal or near-equal numbers (196/197, 101/101, 54/54, 49/49, 20/19),
|
||||
which reads as region start/end pairs (hypothesis: the split was counted, not matched record by record).
|
||||
- The same reference states that an AWC tag id is the low byte of the joaat of the chunk name. That holds for
|
||||
`markers` (`0xbd`), `data` (`0x55`), `peak` (`0x36`), `seektable` (`0xa3`) and `streamformat` (`0x48`), which
|
||||
`tests/test_rsc8_audio.py` checks, and by the same rule `0x5a` and `0xd9` are `granulargrains` and
|
||||
`granularloops`. It does not hold for `0x5c`, `0x76` and `0xb2` under the names `animation`, `format` and
|
||||
`metadata`, so those three names stay open.
|
||||
|
||||
## Binary format of `RSC8` (version `8001`)
|
||||
|
||||
|
||||
@ -985,5 +985,138 @@ class UndocumentedTagsTest(unittest.TestCase):
|
||||
self.assertEqual(rep["summary"]["indexrun_groups"], 2)
|
||||
|
||||
|
||||
def _joaat(name):
|
||||
"""Jenkins one-at-a-time, the hash the cue names and the tag ids are stored as."""
|
||||
h = 0
|
||||
for c in name.encode("ascii"):
|
||||
h = (h + c) & 0xFFFFFFFF
|
||||
h = (h + (h << 10)) & 0xFFFFFFFF
|
||||
h ^= h >> 6
|
||||
h = (h + (h << 3)) & 0xFFFFFFFF
|
||||
h ^= h >> 11
|
||||
return (h + (h << 15)) & 0xFFFFFFFF
|
||||
|
||||
|
||||
def build_synthetic_markers(records):
|
||||
"""16-byte cue records: u32 name hash, f32 value, u32 sample offset, u32 extra. No header."""
|
||||
return b"".join(struct.pack("<IfII", name, value, offset, extra) for name, value, offset, extra in records)
|
||||
|
||||
|
||||
class MarkerRecordsTest(unittest.TestCase):
|
||||
INTRO, BEAT, OUTRO = _joaat("intro"), _joaat("beat"), _joaat("outro")
|
||||
MARKS = build_synthetic_markers([
|
||||
(INTRO, 0.0, 0, 0),
|
||||
(BEAT, 1.0, 24000, 0),
|
||||
(BEAT, 2.0, 48000, 0),
|
||||
(OUTRO, 0.0, 90000, 3),
|
||||
])
|
||||
|
||||
def test_tag_ids_are_the_low_byte_of_the_chunk_name_hash(self):
|
||||
for name, tag in (
|
||||
("markers", rsc8_audio.TAG_MARKER),
|
||||
("data", rsc8_audio.TAG_DATA),
|
||||
("peak", rsc8_audio.TAG_PEAK),
|
||||
("seektable", rsc8_audio.TAG_SEEKTABLE),
|
||||
("streamformat", rsc8_audio.TAG_STREAMED_MUSIC),
|
||||
):
|
||||
self.assertEqual(_joaat(name) & 0xFF, tag, name)
|
||||
|
||||
def test_decodes_every_record_not_a_header(self):
|
||||
m = rsc8_audio.decode_marker_records(self.MARKS, 96000)
|
||||
self.assertTrue(m["size_ok"])
|
||||
self.assertEqual(m["records"], 4)
|
||||
self.assertEqual(m["names"], {self.INTRO: 1, self.BEAT: 2, self.OUTRO: 1})
|
||||
self.assertEqual(m["first_name"], self.INTRO)
|
||||
self.assertNotEqual(m["first_name"], rsc8_audio.MARKER_NAME_COMMON)
|
||||
self.assertTrue(m["offsets_ascending"])
|
||||
self.assertEqual(m["offset_max"], 90000)
|
||||
self.assertEqual(m["extra_max"], 3)
|
||||
self.assertTrue(m["offsets_within_samples"])
|
||||
# Two cues may share one sample offset; that is still in order.
|
||||
tied = build_synthetic_markers([(self.BEAT, 1.0, 10, 0), (self.OUTRO, 0.0, 10, 0)])
|
||||
self.assertTrue(rsc8_audio.decode_marker_records(tied)["offsets_ascending"])
|
||||
self.assertIsNone(rsc8_audio.decode_marker_records(tied)["offsets_within_samples"])
|
||||
|
||||
def test_flags_violations(self):
|
||||
self.assertFalse(rsc8_audio.decode_marker_records(b"")["size_ok"])
|
||||
self.assertFalse(rsc8_audio.decode_marker_records(self.MARKS[:-1])["size_ok"])
|
||||
self.assertFalse(rsc8_audio.decode_marker_records(self.MARKS[:8])["size_ok"])
|
||||
swapped = build_synthetic_markers([(self.BEAT, 1.0, 500, 0), (self.INTRO, 0.0, 0, 0)])
|
||||
self.assertFalse(rsc8_audio.decode_marker_records(swapped)["offsets_ascending"])
|
||||
self.assertFalse(rsc8_audio.decode_marker_records(self.MARKS, 89999)["offsets_within_samples"])
|
||||
|
||||
def test_bank_tally_and_scan_summary(self):
|
||||
fmt = struct.pack("<I", 96000) + bytes(4) + struct.pack("<H", 48000) + bytes(18) + b"\x0B\x01"
|
||||
music = build_minimal_tag_bank(0x00112233, {0x55: b"\xF8\x00" + bytes(62), 0x76: fmt, 0xBD: self.MARKS})
|
||||
speech = build_minimal_tag_bank(0x00445566, {
|
||||
0x55: b"\xF8\x00" + bytes(62),
|
||||
0xBD: build_synthetic_markers([(rsc8_audio.MARKER_NAME_COMMON, 2.0, 0, 2)]),
|
||||
})
|
||||
broken = build_minimal_tag_bank(0x00778899, {0x55: b"\xF8\x00" + bytes(62), 0xBD: self.MARKS[:-4]})
|
||||
|
||||
res = rsc8_audio.decode_awc_bank(music)
|
||||
self.assertEqual(res["marker_streams"], 1)
|
||||
self.assertEqual(res["marker_records"], 4)
|
||||
self.assertEqual(res["marker_invalid"], 0)
|
||||
self.assertEqual(res["marker_first_names"], {f"0x{self.INTRO:08x}": 1})
|
||||
res_bad = rsc8_audio.decode_awc_bank(broken)
|
||||
self.assertEqual(res_bad["marker_streams"], 1)
|
||||
self.assertEqual(res_bad["marker_invalid"], 1)
|
||||
self.assertEqual(res_bad["marker_records"], 0)
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
rpf = os.path.join(tmp, "synth.rpf")
|
||||
with open(rpf, "wb") as f:
|
||||
f.write(b"9FPR" + struct.pack("<III", 0x100, 0, 0) + bytes(0xF0))
|
||||
for bank in (music, speech, music, broken):
|
||||
f.write(bank)
|
||||
f.write(bytes(0x40))
|
||||
_records, summary = rsc8_audio.scan_rpf(rpf)
|
||||
self.assertEqual(summary["marker_streams"], 4)
|
||||
self.assertEqual(summary["marker_records"], 9)
|
||||
self.assertEqual(summary["marker_invalid"], 1)
|
||||
self.assertEqual(summary["marker_first_names"], {
|
||||
f"0x{self.INTRO:08x}": 2,
|
||||
f"0x{rsc8_audio.MARKER_NAME_COMMON:08x}": 1,
|
||||
})
|
||||
|
||||
# Directory and single-bank scans merge the per-name map the same way.
|
||||
for i, bank in enumerate((music, speech, music)):
|
||||
with open(os.path.join(tmp, f"bank{i}.awc"), "wb") as f:
|
||||
f.write(bank)
|
||||
os.remove(rpf)
|
||||
out_json = os.path.join(tmp, "report.json")
|
||||
cp = subprocess.run(
|
||||
[sys.executable, "-I", TOOL, tmp, out_json, "--quiet"],
|
||||
capture_output=True, text=True,
|
||||
)
|
||||
self.assertEqual(cp.returncode, 0, cp.stderr)
|
||||
with open(out_json, encoding="utf-8") as f:
|
||||
rep = json.load(f)
|
||||
self.assertEqual(rep["summary"]["marker_records"], 9)
|
||||
self.assertEqual(rep["summary"]["marker_first_names"][f"0x{self.INTRO:08x}"], 2)
|
||||
_records, single = rsc8_audio.scan_single_file(os.path.join(tmp, "bank1.awc"))
|
||||
self.assertEqual(single["marker_first_names"], {f"0x{rsc8_audio.MARKER_NAME_COMMON:08x}": 1})
|
||||
|
||||
def test_speech_stream_companion_decodes_its_marker(self):
|
||||
clip = build_synthetic_rsc8()
|
||||
bank = build_synthetic_awc([{"id": 0x00112233, "rsc8": clip, "samples": 48000}])
|
||||
comp = {}
|
||||
real = rsc8_audio._make_record
|
||||
|
||||
def spy(bank_offset, tag_offset, stream_id, decoded, companion):
|
||||
comp.update(companion or {})
|
||||
return real(bank_offset, tag_offset, stream_id, decoded, companion)
|
||||
|
||||
rsc8_audio._make_record = spy
|
||||
try:
|
||||
rsc8_audio.decode_awc_bank(bank)
|
||||
finally:
|
||||
rsc8_audio._make_record = real
|
||||
self.assertEqual(comp["markers"]["records"], 1)
|
||||
self.assertEqual(comp["markers"]["first_name"], rsc8_audio.MARKER_NAME_COMMON)
|
||||
self.assertTrue(comp["markers"]["offsets_within_samples"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
@ -57,7 +57,12 @@ TAG_SEEKTABLE = 0xA3
|
||||
TAG_LOUDNESS = 0xB2
|
||||
TAG_MARKER = 0xBD
|
||||
TAG_INDEXRUN = 0xD9 # undocumented grouped index runs into the 0x5a entries; decode_index_runs()
|
||||
MARKER_MAGIC = 0xE5C74063
|
||||
# The most common cue-name hash of a `0xbd` record, not a magic: it is the first word of 88,082 of the
|
||||
# 420,168 `0xbd` tags in `audio.rpf` (decode_marker_records()). The old name is kept for callers.
|
||||
MARKER_NAME_COMMON = 0xE5C74063
|
||||
MARKER_MAGIC = MARKER_NAME_COMMON
|
||||
MARKER_RECORD_BYTES = 16
|
||||
MAX_MARKER_BYTES = 16 * 1024 * 1024
|
||||
|
||||
ATTR_FLOAT = 0xFFFF0001
|
||||
ATTR_INT = 0xFFFF0002
|
||||
@ -892,6 +897,59 @@ def decode_index_runs(blob, grid_entries=None):
|
||||
return out
|
||||
|
||||
|
||||
def decode_marker_records(blob, sample_count=None):
|
||||
"""Structurally decode a ``0xbd`` (``markers``) tag: a list of 16-byte cue records.
|
||||
|
||||
Measured on all 420,168 ``0xbd`` tags of ``audio.rpf`` (781,350 records): the size is always a
|
||||
multiple of 16, there is no header, and every record is::
|
||||
|
||||
+0 u32 name joaat of the cue name (167 distinct values)
|
||||
+4 u32 value an f32 for most names, a second name hash for region cues
|
||||
+8 u32 offset sample position in the stream; never decreases from one record to the next
|
||||
+12 u32 extra 0..6 measured
|
||||
|
||||
``name`` and ``value`` are returned as stored. With ``sample_count`` (the stream's ``0x76`` count),
|
||||
``offsets_within_samples`` says whether every offset is at most that count.
|
||||
"""
|
||||
out = {
|
||||
"size_ok": False,
|
||||
"records": None,
|
||||
"names": None,
|
||||
"first_name": None,
|
||||
"offsets_ascending": None,
|
||||
"offset_max": None,
|
||||
"offsets_within_samples": None,
|
||||
"extra_max": None,
|
||||
}
|
||||
raw = bytes(blob)
|
||||
if not raw or len(raw) % MARKER_RECORD_BYTES or len(raw) > MAX_MARKER_BYTES:
|
||||
return out
|
||||
out["size_ok"] = True
|
||||
n = len(raw) // MARKER_RECORD_BYTES
|
||||
names = {}
|
||||
ascending = True
|
||||
prev = None
|
||||
offset_max = 0
|
||||
extra_max = 0
|
||||
for i in range(n):
|
||||
name, _value, offset, extra = struct.unpack_from("<IIII", raw, MARKER_RECORD_BYTES * i)
|
||||
names[name] = names.get(name, 0) + 1
|
||||
if prev is not None and offset < prev:
|
||||
ascending = False
|
||||
prev = offset
|
||||
offset_max = max(offset_max, offset)
|
||||
extra_max = max(extra_max, extra)
|
||||
out["records"] = n
|
||||
out["names"] = names
|
||||
out["first_name"] = _u32(raw, 0)
|
||||
out["offsets_ascending"] = ascending
|
||||
out["offset_max"] = offset_max
|
||||
out["extra_max"] = extra_max
|
||||
if sample_count is not None:
|
||||
out["offsets_within_samples"] = offset_max <= sample_count
|
||||
return out
|
||||
|
||||
|
||||
def _decode_companion_tags(read_slice, bank_offset, tag_map):
|
||||
"""Decode companion speech tags (`0x76`, `0x55`, `0x36`, `0x5a`, `0xa3`, `0xb2`, `0xbd`, `0xd9`) on a stream."""
|
||||
comp = {
|
||||
@ -905,7 +963,7 @@ def _decode_companion_tags(read_slice, bank_offset, tag_map):
|
||||
"peak_entries": tag_map[TAG_PEAK][0][1] // 2 if TAG_PEAK in tag_map else 0,
|
||||
"seek_entries": tag_map[TAG_SEEKTABLE][0][1] // 4 if TAG_SEEKTABLE in tag_map else 0,
|
||||
"loudness_entries": tag_map[TAG_LOUDNESS][0][1] // 8 if TAG_LOUDNESS in tag_map else 0,
|
||||
"marker_magic": None,
|
||||
"markers": None,
|
||||
}
|
||||
if TAG_FORMAT in tag_map:
|
||||
fo, fs = tag_map[TAG_FORMAT][0]
|
||||
@ -924,10 +982,8 @@ def _decode_companion_tags(read_slice, bank_offset, tag_map):
|
||||
comp["audio_duration"] = round(samples / float(sr), 6)
|
||||
if TAG_MARKER in tag_map:
|
||||
mo, ms = tag_map[TAG_MARKER][0]
|
||||
if ms >= 16:
|
||||
mb = read_slice(bank_offset + mo, 16)
|
||||
if len(mb) >= 16:
|
||||
comp["marker_magic"] = _u32(mb, 0)
|
||||
if 0 < ms <= MAX_MARKER_BYTES:
|
||||
comp["markers"] = decode_marker_records(read_slice(bank_offset + mo, ms), comp["audio_samples"])
|
||||
grid = None
|
||||
if TAG_GRID20 in tag_map:
|
||||
go, gs = tag_map[TAG_GRID20][0]
|
||||
@ -987,11 +1043,25 @@ def _new_grid_tally():
|
||||
"indexrun_groups": 0,
|
||||
"indexrun_invalid": 0,
|
||||
"indexrun_index_overruns": 0,
|
||||
"marker_streams": 0,
|
||||
"marker_records": 0,
|
||||
"marker_invalid": 0,
|
||||
"marker_first_names": {},
|
||||
}
|
||||
|
||||
|
||||
def _merge_grid_tally(dst, src):
|
||||
"""Add one bank's companion-table tally to a running summary (counts add, per-name maps merge)."""
|
||||
for key, start in _new_grid_tally().items():
|
||||
if isinstance(start, dict):
|
||||
for name, count in src[key].items():
|
||||
dst[key][name] = dst[key].get(name, 0) + count
|
||||
else:
|
||||
dst[key] += src[key]
|
||||
|
||||
|
||||
def _tally_undocumented_tables(read_slice, bank_base, tmap, tally):
|
||||
"""Decode the undocumented ``0x5a``/``0xd9`` tags on one stream into a running tally.
|
||||
"""Decode the ``0x5a``/``0xd9`` tables and the ``0xbd`` cue list on one stream into a running tally.
|
||||
|
||||
A stream counts as invalid when one of the layout invariants measured over the whole ``audio.rpf``
|
||||
population fails, so the tally doubles as a package-wide validator.
|
||||
@ -1032,6 +1102,19 @@ def _tally_undocumented_tables(read_slice, bank_base, tmap, tally):
|
||||
tally["indexrun_groups"] += runs["groups"]
|
||||
if runs["indices_within_grid"] is False:
|
||||
tally["indexrun_index_overruns"] += 1
|
||||
if TAG_MARKER in tmap:
|
||||
mo, ms = tmap[TAG_MARKER][0]
|
||||
marks = None
|
||||
if 0 < ms <= MAX_MARKER_BYTES:
|
||||
marks = decode_marker_records(read_slice(bank_base + mo, ms))
|
||||
tally["marker_streams"] += 1
|
||||
if marks is None or not (marks["size_ok"] and marks["offsets_ascending"]):
|
||||
tally["marker_invalid"] += 1
|
||||
else:
|
||||
tally["marker_records"] += marks["records"]
|
||||
key = f"0x{marks['first_name']:08x}"
|
||||
firsts = tally["marker_first_names"]
|
||||
firsts[key] = firsts.get(key, 0) + 1
|
||||
|
||||
|
||||
def decode_awc_bank(data, bank_offset=0, *, deep=True):
|
||||
@ -1394,8 +1477,7 @@ def scan_dir(path, *, max_banks=None, deep=True, collect_records=True):
|
||||
summary["rsc8_non_rsc8_0x5c"] += bank_res["non_rsc8_0x5c"]
|
||||
for tk, tv in bank_res["tag_counts"].items():
|
||||
summary["adat_tag_counts"][tk] = summary["adat_tag_counts"].get(tk, 0) + tv
|
||||
for gk in _new_grid_tally():
|
||||
summary[gk] += bank_res[gk]
|
||||
_merge_grid_tally(summary, bank_res)
|
||||
for rec in bank_res["records"]:
|
||||
_accumulate_record(summary, rec, in_adat=True, has_audio=rec.get("has_audio_data"))
|
||||
if collect_records:
|
||||
@ -1445,8 +1527,7 @@ def scan_single_file(path, *, start=0, limit=None, max_banks=None, deep=True, co
|
||||
summary = _new_summary("awc", path)
|
||||
summary["adat_banks"] = 1
|
||||
summary["adat_streams"] = bank_res["stream_count"]
|
||||
for gk in _new_grid_tally():
|
||||
summary[gk] += bank_res[gk]
|
||||
_merge_grid_tally(summary, bank_res)
|
||||
summary["adat_bytes"] = bank_res["bank_size"]
|
||||
summary["adat_tag_counts"] = bank_res["tag_counts"]
|
||||
summary["rsc8_non_rsc8_0x5c"] = bank_res["non_rsc8_0x5c"]
|
||||
|
||||
Reference in New Issue
Block a user