Files

139 lines
6.8 KiB
Python

# ADPCM fixtures (tools/fixtures.py): codecs/audio/Adpcm and the raw audio/adpcm format.
# tests/gen/adpcm_codec.luau raw codec streams with the reference decoder's s16le output: { name, kind, data, ch,
# frames, ref, rate?, cutoff?, v3?, coefs? }
# adx ffmpeg -f adx (version 3), the frames after the header; vgmstream decodes (scale + 1, v3 rounding)
# psx our Python PS-ADPCM encoder (tools/fixtures/_adpcm.py) in a VAG; vgmstream decodes
# dsp our Python DSP encoder in a mono BRSTM; vgmstream decodes
# ima_qt ffmpeg adpcm_ima_qt in AIFF-C (the SSND packets); ffmpeg decodes
# ima our Python IMA encoder in KVAG (high nibble first, stereo by nibble); ffmpeg (adpcm_ima_ssi) decodes
# tests/gen/adpcm_raw.luau headerless streams for audio/adpcm: { name, codec, data, rate, ch, blockAlign?, ref }
# ima / ima-stereo (KVAG data, ffmpeg), ms (ffmpeg adpcm_ms WAV data chunk, ffmpeg), ima_wav (ffmpeg
# adpcm_ima_wav), ima_qt (the AIFF-C packets), psx (VAG data), dsp (mono), adx-free
# tests/gen/adpcm_ours.luau our encoder's raw output (audio/adpcm encode, run in this step) wrapped in a container
# ffmpeg reads, ffmpeg's decode, and our own decode of the same bytes (must match)
# tests/gen/adpcm_sig.luau the encoder input (stereo)
import importlib.util, os, struct
_dir = os.path.dirname(os.path.abspath(__file__))
def _load(name):
spec = importlib.util.spec_from_file_location(name, os.path.join(_dir, name + ".py"))
m = importlib.util.module_from_spec(spec)
spec.loader.exec_module(m)
return m
A = _load("_audio")
D = _load("_adpcm")
INPUTS = ["tools/fixtures/_adpcm.py", "src/Rosetta/codecs/audio/Adpcm", "src/Rosetta/audio/adpcm"]
RATE = 22050
def aiff_ssnd(d):
p = 12
while p + 8 <= len(d):
cid, size = d[p:p + 4], struct.unpack_from(">I", d, p + 4)[0]
if cid == b"SSND":
off = struct.unpack_from(">I", d, p + 8)[0]
return d[p + 16 + off:p + 8 + size]
p += 8 + size + (size & 1)
raise ValueError("no SSND")
def wav_data(d):
p = 12
fmt = None
while p + 8 <= len(d):
cid, size = d[p:p + 4], struct.unpack_from("<I", d, p + 4)[0]
if cid == b"fmt ":
fmt = d[p + 8:p + 8 + size]
elif cid == b"data":
return fmt, d[p + 8:p + 8 + size]
p += 8 + size + (size & 1)
raise ValueError("no data")
def generate(ctx):
stereo = A.signal("music", RATE, 2, 5000, seed=7)
mono = A.signal("sweep", RATE, 1, 4100, seed=8)
w2, w1 = A.wav(stereo, RATE, 2), A.wav(mono, RATE, 1)
codec, raw = [], []
# ADX (ffmpeg writes version 3; vgmstream is the reference: CRI's scale + 1)
for name, w, ch in (("adx-mono", w1, 1), ("adx-stereo", w2, 2)):
f = D.ffencode(ctx, w, ["-c:a", "adpcm_adx", "-f", "adx"], "adx")
start = struct.unpack_from(">H", f, 2)[0] + 4
rate, frames, cutoff, ver = struct.unpack_from(">IIHH", f, 8)
_, rch, ref = D.vgdecode(ctx, f, "adx")
assert rch == ch and len(ref) == frames * ch * 2, (len(ref), frames)
codec.append(dict(name=name, kind="adx", data=f[start:], ch=ch, frames=frames, ref=ref, rate=rate,
cutoff=cutoff, v3=ver == 0x0300))
# PS-ADPCM in a VAG (mono)
v = D.vag(mono, RATE)
_, _, ref = D.vgdecode(ctx, v, "vag")
codec.append(dict(name="psx", kind="psx", data=v[0x30:], ch=1, frames=len(ref) // 2, ref=ref))
raw.append(dict(name="psx", codec="psx", data=v[0x30:], rate=RATE, ch=1, ref=ref))
# DSP in a mono BRSTM (one block holds everything)
ints = D.s16(mono)
dspdata = D.dsp_encode(ints)
f = D.rstm(mono, RATE, 1, "RSTM", block=(len(dspdata) + 31) // 32 * 32)
_, _, ref = D.vgdecode(ctx, f, "brstm")
assert len(ref) == len(ints) * 2
codec.append(dict(name="dsp", kind="dsp", data=dspdata, ch=1, frames=len(ints), ref=ref, coefs=D.DSP_COEFS))
# Apple ima4 (AIFF-C)
for name, w, ch in (("ima_qt-mono", w1, 1), ("ima_qt-stereo", w2, 2)):
f = D.ffencode(ctx, w, ["-c:a", "adpcm_ima_qt", "-f", "aiff"], "aiff")
data = aiff_ssnd(f)
ref = D.ffs16(ctx, f, "aiff")
codec.append(dict(name=name, kind="ima_qt", data=data, ch=ch, frames=len(ref) // (2 * ch), ref=ref))
raw.append(dict(name=name, codec="ima_qt", data=data, rate=RATE, ch=ch, ref=ref))
# IMA nibble streams (KVAG, high nibble first)
for name, sig, ch in (("ima-mono", mono, 1), ("ima-stereo", stereo, 2)):
f = D.kvag(sig, RATE, ch)
ref = D.ffs16(ctx, f, "vag", fmt="kvag")
codec.append(dict(name=name, kind="ima", data=f[14:], ch=ch, frames=len(ref) // (2 * ch), ref=ref))
raw.append(dict(name=name, codec="ima", data=f[14:], rate=RATE, ch=ch, ref=ref))
# MS ADPCM / IMA WAV blocks (ffmpeg), as raw block streams
for name, codec_, w, ch in (("ms-stereo", "adpcm_ms", w2, 2), ("ms-mono", "adpcm_ms", w1, 1),
("ima_wav-stereo", "adpcm_ima_wav", w2, 2)):
f = D.ffencode(ctx, w, ["-c:a", codec_, "-f", "wav"], "wav")
fmt, data = wav_data(f)
ba = struct.unpack_from("<H", fmt, 12)[0]
ref = D.ffs16(ctx, f, "wav")
raw.append(dict(name=name, codec="ms" if codec_ == "adpcm_ms" else "ima_wav", data=data, rate=RATE, ch=ch,
blockAlign=ba, ref=ref))
ctx.emit("adpcm_codec", codec)
ctx.emit("adpcm_raw", raw)
# our encoder (audio/adpcm) -> containers ffmpeg reads -> ffmpeg's decode; the test decodes the same bytes itself
A.emit_signal(ctx, "adpcm_sig", stereo, RATE, 2)
ours = []
for codec_, opts in (("ima", '{ codec = "ima" }'), ("ms", '{ codec = "ms", blockAlign = 512 }'),
("ima_wav", '{ codec = "ima_wav", blockAlign = 512 }')):
data = A.encode_luau(ctx, "audio/adpcm", "adpcm_sig", opts)
if codec_ == "ima":
f = b"KVAG" + struct.pack("<IIH", len(data), RATE, 1) + data
ref = D.ffs16(ctx, f, "vag", fmt="kvag")
else:
ba = 512
spb = (ba - 7 * 2) * 2 // 2 + 2 if codec_ == "ms" else (ba - 8) + 1
if codec_ == "ms":
coefs = b"".join(struct.pack("<hh", a, b) for a, b in zip([256, 512, 0, 192, 240, 460, 392], [0, -256, 0, 64, 0, -208, -232]))
ext = struct.pack("<HH", spb, 7) + coefs
fmt = struct.pack("<HHIIHHH", 2, 2, RATE, RATE * ba // spb, ba, 4, len(ext)) + ext
else:
fmt = struct.pack("<HHIIHHHH", 0x11, 2, RATE, RATE * ba // spb, ba, 4, 2, spb)
body = b"WAVE" + b"fmt " + struct.pack("<I", len(fmt)) + fmt + b"data" + struct.pack("<I", len(data)) + data
f = b"RIFF" + struct.pack("<I", len(body)) + body
ref = D.ffs16(ctx, f, "wav")
ours.append(dict(name=codec_, opts=opts, data=data, ref=ref))
ctx.emit("adpcm_ours", ours)