rename: src/Formats -> src/Rosetta, @formats -> @rosetta requires, build/luajit/rosetta + rosetta._compat, build/roblox/Rosetta, ROSETTA_* env/globals, writer vendor strings
This commit is contained in:
1 parent
2956576d7f
commit
22ddcd78fa
941 files changed
+3364
-3364
No files matched your search
@@ -0,0 +1,818 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/audio/Adpcm: the 4-bit ADPCM family, decode (and encode where it is cheap). New in rosetta; the IMA and
|
||||
-- MS block decoders moved here from audio/wav/Codecs (wav-luau, same author). ADX coefficients, PS-ADPCM and Nintendo
|
||||
-- DSP decoding follow vgmstream r2117 (ISC licence, licenses/vgmstream-LICENSE.txt) so the output is bit-exact with it:
|
||||
-- the ADX scale + 1 and the version 3 rounding, PS-ADPCM's float filter (exact in integers below 2^17), DSP's 1024
|
||||
-- rounding. Codec kernels only: the formats (wav, aiff, caf, adx, vag, brstm, adpcm, ...) walk the layouts.
|
||||
--
|
||||
-- Decoders write f32 (sample / 32768) to dst at float index d, every ds floats (interleaving), and keep their running
|
||||
-- state in a caller-owned buffer `st` at byte offset `so` (i32 pairs: IMA pred / step index, the others hist1 / hist2),
|
||||
-- so a decoder can stop between calls and snapshot the state for seeking (codecs/audio/Adpcm/Decoder).
|
||||
--
|
||||
-- Adpcm.ima(src, p, q0, qs, count, hi, st, so, dst, d, ds) -- IMA nibbles: count samples, nibble q0, q0+qs, ...
|
||||
-- Adpcm.imaQt(src, p, st, so, dst, d, ds) -- Apple ima4: one 34-byte packet -> 64 samples
|
||||
-- Adpcm.imaWav(src, off, avail, ch, spb, dst, d) -> frames -- Microsoft IMA block (WAV / AVI)
|
||||
-- Adpcm.msBlock(src, off, avail, ch, spb, dst, d, coefs, ncoef, st) -> frames -- MS ADPCM block
|
||||
-- Adpcm.adx(src, p, pstride, nframes, c1, c2, v3, st, so, dst, d, ds) -- CRI ADX: 18-byte frames, 32 samples
|
||||
-- Adpcm.adxCoefs(cutoff, rate) -> c1, c2
|
||||
-- Adpcm.psx(src, p, pstride, nframes, st, so, dst, d, ds) -- Sony PS-ADPCM (VAG): 16-byte frames, 28 samples
|
||||
-- Adpcm.dsp(src, p, count, coefs, co, st, so, dst, d, ds) -- Nintendo DSP: 8-byte frames, 14 samples
|
||||
-- Adpcm.modplug(src, p, count, dst, d) -- ModPlug ADPCM (XM / MOD samples)
|
||||
-- Adpcm.swfFrames(src, p, n, ch) -> frames -- Flash (SWF / FLV) ADPCM: one packet's length
|
||||
-- Adpcm.swf(src, p, n, ch, dst, d) -> frames -- Flash ADPCM: one packet of n bytes
|
||||
-- encoders: imaEncode, imaWavEncode, msEncode, adxEncode, psxEncode, dspEncode (see each)
|
||||
--
|
||||
-- Every function reads only the bytes its arguments describe: the caller checks the ranges (byte counts per call are
|
||||
-- documented on each). Never throws.
|
||||
local readu8, readi16, readu16, readi32 = buffer.readu8, buffer.readi16, buffer.readu16, buffer.readi32
|
||||
local writef32, readf32, writei32, writeu8, writeu16 = buffer.writef32, buffer.readf32, buffer.writei32, buffer.writeu8, buffer.writeu16
|
||||
local band, rshift, bor, lshift = bit32.band, bit32.rshift, bit32.bor, bit32.lshift
|
||||
local floor = math.floor
|
||||
|
||||
local Adpcm = {}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ IMA
|
||||
local IMA_STEP = {
|
||||
7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
|
||||
130, 143, 157, 173, 190, 209, 230, 253, 279, 307, 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, 876, 963, 1060,
|
||||
1166, 1282, 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484,
|
||||
7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767,
|
||||
}
|
||||
local IMA_ADJ = { -1, -1, -1, -1, 2, 4, 6, 8, -1, -1, -1, -1, 2, 4, 6, 8 }
|
||||
-- IMA_DIFF[index * 8 + (nibble & 7) + 1]: the shift-and-add difference (step/8 + bits of step/4, step/2, step), as
|
||||
-- the IMA reference computes it (not (2n + 1) * step / 8, which rounds differently)
|
||||
local IMA_DIFF = table.create(89 * 8, 0)
|
||||
-- IMA_NEXT[index * 16 + nibble + 1]: the next step index
|
||||
local IMA_NEXT = table.create(89 * 16, 0)
|
||||
for i = 0, 88 do
|
||||
local step = IMA_STEP[i + 1]
|
||||
for n = 0, 7 do
|
||||
local d = rshift(step, 3)
|
||||
if band(n, 1) ~= 0 then
|
||||
d += rshift(step, 2)
|
||||
end
|
||||
if band(n, 2) ~= 0 then
|
||||
d += rshift(step, 1)
|
||||
end
|
||||
if band(n, 4) ~= 0 then
|
||||
d += step
|
||||
end
|
||||
IMA_DIFF[i * 8 + n + 1] = d
|
||||
end
|
||||
for n = 0, 15 do
|
||||
IMA_NEXT[i * 16 + n + 1] = math.clamp(i + IMA_ADJ[n + 1], 0, 88)
|
||||
end
|
||||
end
|
||||
Adpcm.IMA_STEP = IMA_STEP
|
||||
|
||||
-- ima: `count` samples of one channel from a nibble stream at byte p: nibble q0, q0 + qs, q0 + 2 qs, ... (nibble q is
|
||||
-- in byte p + q // 2, the low half first unless hi). State: st[so] predictor, st[so + 4] step index. Reads bytes
|
||||
-- p .. p + (q0 + (count - 1) * qs) // 2.
|
||||
function Adpcm.ima(src: buffer, p: number, q0: number, qs: number, count: number, hi: boolean, st: buffer, so: number,
|
||||
dst: buffer, d: number, ds: number)
|
||||
local pred = readi32(st, so)
|
||||
local index = math.clamp(readi32(st, so + 4), 0, 88)
|
||||
local q = q0
|
||||
local o = d * 4
|
||||
local os = ds * 4
|
||||
for _ = 1, count do
|
||||
local b = readu8(src, p + rshift(q, 1))
|
||||
local nib
|
||||
if (band(q, 1) == 0) == hi then
|
||||
nib = rshift(b, 4)
|
||||
else
|
||||
nib = band(b, 15)
|
||||
end
|
||||
local diff = IMA_DIFF[index * 8 + band(nib, 7) + 1]
|
||||
if nib >= 8 then
|
||||
pred -= diff
|
||||
if pred < -32768 then
|
||||
pred = -32768
|
||||
end
|
||||
else
|
||||
pred += diff
|
||||
if pred > 32767 then
|
||||
pred = 32767
|
||||
end
|
||||
end
|
||||
index = IMA_NEXT[index * 16 + nib + 1]
|
||||
writef32(dst, o, pred / 32768)
|
||||
o += os
|
||||
q += qs
|
||||
end
|
||||
writei32(st, so, pred)
|
||||
writei32(st, so + 4, index)
|
||||
end
|
||||
|
||||
-- imaQt: one Apple ima4 packet (34 bytes at p: a big-endian header, the predictor's top 9 bits and the 7-bit step
|
||||
-- index, then 64 nibbles low first) -> 64 samples. Apple's decoders keep the running full-precision predictor while
|
||||
-- the header agrees with it (the same step index, the predictor within 127): st carries it from packet to packet
|
||||
-- (zeros before the first).
|
||||
function Adpcm.imaQt(src: buffer, p: number, st: buffer, so: number, dst: buffer, d: number, ds: number)
|
||||
local h = readu8(src, p) * 256 + readu8(src, p + 1)
|
||||
local pred = band(h, 0xFF80)
|
||||
if pred >= 32768 then
|
||||
pred -= 65536
|
||||
end
|
||||
local index = band(h, 0x7F)
|
||||
if index ~= readi32(st, so + 4) or math.abs(pred - readi32(st, so)) > 0x7F then
|
||||
writei32(st, so, pred)
|
||||
writei32(st, so + 4, index)
|
||||
end
|
||||
Adpcm.ima(src, p + 2, 0, 1, 64, false, st, so, dst, d, ds)
|
||||
end
|
||||
|
||||
-- imaWav: one Microsoft IMA ADPCM block (WAV / AVI) at src[off]: per channel a 4-byte header (s16 predictor = the
|
||||
-- first sample, u8 step index, pad), then groups of 4 bytes (8 nibbles, low first) per channel in turn. avail: the
|
||||
-- bytes present (a truncated last block decodes the whole groups it has). Writes interleaved frames to dst from float
|
||||
-- index d; returns the frame count (<= spb).
|
||||
function Adpcm.imaWav(src: buffer, off: number, avail: number, ch: number, spb: number, dst: buffer, d: number): number
|
||||
if avail < 4 * ch then
|
||||
return 0
|
||||
end
|
||||
local groups = (avail - 4 * ch) // (4 * ch)
|
||||
local frames = math.min(spb, 1 + groups * 8)
|
||||
local o = d * 4
|
||||
for c = 0, ch - 1 do
|
||||
local pred = readi16(src, off + c * 4)
|
||||
local index = math.min(readu8(src, off + c * 4 + 2), 88)
|
||||
writef32(dst, o + c * 4, pred / 32768)
|
||||
local f = 1
|
||||
local g = 0
|
||||
while f < frames do
|
||||
local p = off + 4 * ch + (g * ch + c) * 4
|
||||
local k = 0
|
||||
while k < 8 and f < frames do
|
||||
local b = readu8(src, p + rshift(k, 1))
|
||||
local nib = if band(k, 1) == 0 then band(b, 15) else rshift(b, 4)
|
||||
local diff = IMA_DIFF[index * 8 + band(nib, 7) + 1]
|
||||
if nib >= 8 then
|
||||
pred -= diff
|
||||
if pred < -32768 then
|
||||
pred = -32768
|
||||
end
|
||||
else
|
||||
pred += diff
|
||||
if pred > 32767 then
|
||||
pred = 32767
|
||||
end
|
||||
end
|
||||
index = IMA_NEXT[index * 16 + nib + 1]
|
||||
writef32(dst, o + (f * ch + c) * 4, pred / 32768)
|
||||
f += 1
|
||||
k += 1
|
||||
end
|
||||
g += 1
|
||||
end
|
||||
end
|
||||
return frames
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ MS ADPCM
|
||||
local MS_ADAPT = { 230, 230, 230, 230, 307, 409, 512, 614, 768, 614, 512, 409, 307, 230, 230, 230 }
|
||||
Adpcm.MS_COEF1 = { 256, 512, 0, 192, 240, 460, 392 }
|
||||
Adpcm.MS_COEF2 = { 0, -256, 0, 64, 0, -208, -232 }
|
||||
|
||||
-- msBlock: one MS ADPCM block: per channel u8 predictor index, s16 delta, s16 sample 1, s16 sample 2 (each field for
|
||||
-- every channel in turn), then nibbles high first, channels interleaved per nibble. coefs: i32 pairs (coef1, coef2) x
|
||||
-- ncoef (256 = 1.0). state: 20 * ch bytes of scratch. avail: the bytes present. Writes interleaved frames to dst from
|
||||
-- float index d; returns the frame count (<= spb).
|
||||
function Adpcm.msBlock(src: buffer, off: number, avail: number, ch: number, spb: number, dst: buffer, d: number,
|
||||
coefs: buffer, ncoef: number, state: buffer): number
|
||||
local hdr = 7 * ch
|
||||
if avail < hdr or ncoef < 1 then
|
||||
return 0
|
||||
end
|
||||
local frames = math.min(spb, 2 + ((avail - hdr) * 2) // ch)
|
||||
local o = d * 4
|
||||
for c = 0, ch - 1 do
|
||||
local pi = math.min(readu8(src, off + c), ncoef - 1)
|
||||
local idelta = readi16(src, off + ch + c * 2)
|
||||
local s1 = readi16(src, off + 3 * ch + c * 2)
|
||||
local s2 = readi16(src, off + 5 * ch + c * 2)
|
||||
local so = c * 20
|
||||
writei32(state, so, readi32(coefs, pi * 8))
|
||||
writei32(state, so + 4, readi32(coefs, pi * 8 + 4))
|
||||
writei32(state, so + 8, idelta)
|
||||
writei32(state, so + 12, s1)
|
||||
writei32(state, so + 16, s2)
|
||||
writef32(dst, o + c * 4, s2 / 32768)
|
||||
if frames > 1 then
|
||||
writef32(dst, o + (ch + c) * 4, s1 / 32768)
|
||||
end
|
||||
end
|
||||
local total = (frames - 2) * ch
|
||||
for k = 0, total - 1 do
|
||||
local b = readu8(src, off + hdr + rshift(k, 1))
|
||||
local nib = if band(k, 1) == 0 then rshift(b, 4) else band(b, 15)
|
||||
local so = (k % ch) * 20
|
||||
local c1, c2 = readi32(state, so), readi32(state, so + 4)
|
||||
local idelta = readi32(state, so + 8)
|
||||
local s1, s2 = readi32(state, so + 12), readi32(state, so + 16)
|
||||
local p = s1 * c1 + s2 * c2
|
||||
p = if p >= 0 then p // 256 else -((-p) // 256)
|
||||
p += (if nib >= 8 then nib - 16 else nib) * idelta
|
||||
if p > 32767 then
|
||||
p = 32767
|
||||
elseif p < -32768 then
|
||||
p = -32768
|
||||
end
|
||||
idelta = (MS_ADAPT[nib + 1] * idelta) // 256
|
||||
if idelta < 16 then
|
||||
idelta = 16
|
||||
elseif idelta > 2796202 then -- INT_MAX / 768
|
||||
idelta = 2796202
|
||||
end
|
||||
writei32(state, so + 8, idelta)
|
||||
writei32(state, so + 12, p)
|
||||
writei32(state, so + 16, s1)
|
||||
writef32(dst, o + (2 * ch + k) * 4, p / 32768)
|
||||
end
|
||||
return frames
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ CRI ADX
|
||||
local F32 = buffer.create(4)
|
||||
local function f32(x: number): number
|
||||
writef32(F32, 0, x)
|
||||
return readf32(F32, 0)
|
||||
end
|
||||
|
||||
-- adxCoefs(cutoff, rate) -> coef1, coef2 (fixed point, 12 fractional bits): the 2-pole high-pass predictor from the
|
||||
-- header's cutoff frequency, in single precision as CRI's decoders (and vgmstream) compute it
|
||||
function Adpcm.adxCoefs(cutoff: number, rate: number): (number, number)
|
||||
if rate <= 0 then
|
||||
return 0, 0
|
||||
end
|
||||
local z = f32(math.cos(f32(2 * math.pi * cutoff / rate)))
|
||||
local a = f32(math.sqrt(2) - z)
|
||||
local b = f32(math.sqrt(2) - 1)
|
||||
local c = f32(f32(a - f32(math.sqrt(f32(f32(a + b) * f32(a - b))))) / b)
|
||||
local c1 = c * 8192
|
||||
local c2 = f32(c * c) * -4096
|
||||
c1 = if c1 >= 0 then floor(c1) else -floor(-c1)
|
||||
c2 = if c2 >= 0 then floor(c2) else -floor(-c2)
|
||||
return c1, c2
|
||||
end
|
||||
|
||||
-- adx: nframes 18-byte frames of one channel (s16 big-endian scale, 32 nibbles high first), pstride bytes apart from
|
||||
-- p. v3: the version 3 rounding (each product shifted on its own). A scale of 0x8001 (CRI's end marker) decodes as
|
||||
-- silence. Reads p .. p + (nframes - 1) * pstride + 17; writes 32 * nframes samples.
|
||||
function Adpcm.adx(src: buffer, p: number, pstride: number, nframes: number, c1: number, c2: number, v3: boolean,
|
||||
st: buffer, so: number, dst: buffer, d: number, ds: number)
|
||||
local h1, h2 = readi32(st, so), readi32(st, so + 4)
|
||||
local o = d * 4
|
||||
local os = ds * 4
|
||||
for f = 0, nframes - 1 do
|
||||
local fp = p + f * pstride
|
||||
local scale = readu8(src, fp) * 256 + readu8(src, fp + 1)
|
||||
if scale == 0x8001 then
|
||||
scale = 0
|
||||
elseif scale >= 32768 then
|
||||
scale -= 65535 -- the signed value + 1
|
||||
else
|
||||
scale += 1
|
||||
end
|
||||
for k = 0, 31 do
|
||||
local b = readu8(src, fp + 2 + rshift(k, 1))
|
||||
local n = if band(k, 1) == 0 then rshift(b, 4) else band(b, 15)
|
||||
if n >= 8 then
|
||||
n -= 16
|
||||
end
|
||||
local s
|
||||
if v3 then
|
||||
s = n * scale + (c1 * h1) // 4096 + (c2 * h2) // 4096
|
||||
else
|
||||
s = n * scale + (c1 * h1 + c2 * h2) // 4096
|
||||
end
|
||||
if s > 32767 then
|
||||
s = 32767
|
||||
elseif s < -32768 then
|
||||
s = -32768
|
||||
end
|
||||
h2 = h1
|
||||
h1 = s
|
||||
writef32(dst, o, s / 32768)
|
||||
o += os
|
||||
end
|
||||
end
|
||||
writei32(st, so, h1)
|
||||
writei32(st, so + 4, h2)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ PS-ADPCM
|
||||
local PSX1 = { 0, 60, 115, 98, 122 }
|
||||
local PSX2 = { 0, 0, -52, -55, -60 }
|
||||
|
||||
-- the filter term (c1 * h1 + c2 * h2) / 64 * 256 truncated, as vgmstream's single-precision float code computes it:
|
||||
-- exact in integers while the products fit a float's 24 bits
|
||||
local function psxPred(f: number, h1: number, h2: number): number
|
||||
local x = PSX1[f] * h1 + PSX2[f] * h2
|
||||
if h1 > -65536 and h1 < 65536 and h2 > -65536 and h2 < 65536 then
|
||||
return 4 * x
|
||||
end
|
||||
local v = f32(f32(f32(PSX1[f] / 64) * h1) + f32(f32(PSX2[f] / 64) * h2)) * 256
|
||||
return if v >= 0 then floor(v) else -floor(-v)
|
||||
end
|
||||
|
||||
-- psx: nframes 16-byte frames of one channel (filter << 4 | shift, flags, 28 nibbles low first), pstride bytes apart
|
||||
-- from p. Filters past 4 decode as 0 and shifts past 12 as 9 (as vgmstream); a frame with flags 7 is silence. The
|
||||
-- history keeps the unclipped value. Reads p .. p + (nframes - 1) * pstride + 15; writes 28 * nframes samples.
|
||||
function Adpcm.psx(src: buffer, p: number, pstride: number, nframes: number, st: buffer, so: number, dst: buffer,
|
||||
d: number, ds: number)
|
||||
local h1, h2 = readi32(st, so), readi32(st, so + 4)
|
||||
local o = d * 4
|
||||
local os = ds * 4
|
||||
for f = 0, nframes - 1 do
|
||||
local fp = p + f * pstride
|
||||
local head = readu8(src, fp)
|
||||
local filter = rshift(head, 4)
|
||||
local shift = band(head, 15)
|
||||
if filter > 4 then
|
||||
filter = 0
|
||||
end
|
||||
if shift > 12 then
|
||||
shift = 9
|
||||
end
|
||||
local mul = 2 ^ (20 - shift)
|
||||
local fi = filter + 1
|
||||
local silent = band(readu8(src, fp + 1), 15) == 7
|
||||
for k = 0, 27 do
|
||||
local s = 0
|
||||
if not silent then
|
||||
local b = readu8(src, fp + 2 + rshift(k, 1))
|
||||
local n = if band(k, 1) == 0 then band(b, 15) else rshift(b, 4)
|
||||
if n >= 8 then
|
||||
n -= 16
|
||||
end
|
||||
s = (n * mul + psxPred(fi, h1, h2)) // 256
|
||||
end
|
||||
h2 = h1
|
||||
h1 = s
|
||||
if s > 32767 then
|
||||
s = 32767
|
||||
elseif s < -32768 then
|
||||
s = -32768
|
||||
end
|
||||
writef32(dst, o, s / 32768)
|
||||
o += os
|
||||
end
|
||||
end
|
||||
writei32(st, so, h1)
|
||||
writei32(st, so + 4, h2)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ Nintendo DSP
|
||||
-- dsp: count samples of one channel from 8-byte frames at p (predictor << 4 | scale shift, 14 nibbles high first).
|
||||
-- coefs: 16 i32 at byte co (8 pairs, 11 fractional bits); a predictor index past 7 uses pair 7. Reads p ..
|
||||
-- p + ceil(count / 14) * 8 - 1.
|
||||
function Adpcm.dsp(src: buffer, p: number, count: number, coefs: buffer, co: number, st: buffer, so: number,
|
||||
dst: buffer, d: number, ds: number)
|
||||
local h1, h2 = readi32(st, so), readi32(st, so + 4)
|
||||
local o = d * 4
|
||||
local os = ds * 4
|
||||
local done = 0
|
||||
local fp = p
|
||||
while done < count do
|
||||
local head = readu8(src, fp)
|
||||
local pi = math.min(rshift(head, 4), 7)
|
||||
local scale = 2 ^ band(head, 15) * 2048
|
||||
local c1, c2 = readi32(coefs, co + pi * 8), readi32(coefs, co + pi * 8 + 4)
|
||||
local n14 = math.min(14, count - done)
|
||||
for k = 0, n14 - 1 do
|
||||
local b = readu8(src, fp + 1 + rshift(k, 1))
|
||||
local n = if band(k, 1) == 0 then rshift(b, 4) else band(b, 15)
|
||||
if n >= 8 then
|
||||
n -= 16
|
||||
end
|
||||
local s = (n * scale + 1024 + c1 * h1 + c2 * h2) // 2048
|
||||
if s > 32767 then
|
||||
s = 32767
|
||||
elseif s < -32768 then
|
||||
s = -32768
|
||||
end
|
||||
h2 = h1
|
||||
h1 = s
|
||||
writef32(dst, o, s / 32768)
|
||||
o += os
|
||||
end
|
||||
done += n14
|
||||
fp += 8
|
||||
end
|
||||
writei32(st, so, h1)
|
||||
writei32(st, so + 4, h2)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encoders
|
||||
-- the nearest s16 to x * 32768 (NaN -> 0)
|
||||
local function toS16(x: number): number
|
||||
local v = floor(x * 32768 + 0.5)
|
||||
if v ~= v then
|
||||
return 0
|
||||
elseif v > 32767 then
|
||||
return 32767
|
||||
elseif v < -32768 then
|
||||
return -32768
|
||||
end
|
||||
return v
|
||||
end
|
||||
Adpcm.toS16 = toS16
|
||||
|
||||
-- the IMA nibble for x from (pred, index): the one whose decoded value lands nearest (greedy, all 16 tried)
|
||||
local function imaNibble(x: number, pred: number, index: number): number
|
||||
local base = index * 8 + 1
|
||||
local best, bestErr = 0, math.huge
|
||||
for m = 0, 7 do
|
||||
local diff = IMA_DIFF[base + m]
|
||||
local up = math.min(pred + diff, 32767)
|
||||
local e = math.abs(x - up)
|
||||
if e < bestErr then
|
||||
best, bestErr = m, e
|
||||
end
|
||||
local down = math.max(pred - diff, -32768)
|
||||
e = math.abs(x - down)
|
||||
if e < bestErr then
|
||||
best, bestErr = m + 8, e
|
||||
end
|
||||
end
|
||||
return best
|
||||
end
|
||||
|
||||
-- imaEncode: count samples of one channel, f32 at float index xi every xs floats of x, into nibbles q0, q0 + qs, ...
|
||||
-- of out at byte p (the other half of each byte is kept). State as Adpcm.ima. Writes bytes p ..
|
||||
-- p + (q0 + (count - 1) * qs) // 2.
|
||||
function Adpcm.imaEncode(x: buffer, xi: number, xs: number, count: number, st: buffer, so: number, out: buffer,
|
||||
p: number, q0: number, qs: number, hi: boolean)
|
||||
local pred = readi32(st, so)
|
||||
local index = math.clamp(readi32(st, so + 4), 0, 88)
|
||||
local q = q0
|
||||
for i = 0, count - 1 do
|
||||
local v = toS16(readf32(x, (xi + i * xs) * 4))
|
||||
local nib = imaNibble(v, pred, index)
|
||||
local diff = IMA_DIFF[index * 8 + band(nib, 7) + 1]
|
||||
pred = if nib >= 8 then math.max(pred - diff, -32768) else math.min(pred + diff, 32767)
|
||||
index = IMA_NEXT[index * 16 + nib + 1]
|
||||
local bp = p + rshift(q, 1)
|
||||
local b = readu8(out, bp)
|
||||
if (band(q, 1) == 0) == hi then
|
||||
b = bor(band(b, 0x0F), lshift(nib, 4))
|
||||
else
|
||||
b = bor(band(b, 0xF0), nib)
|
||||
end
|
||||
writeu8(out, bp, b)
|
||||
q += qs
|
||||
end
|
||||
writei32(st, so, pred)
|
||||
writei32(st, so + 4, index)
|
||||
end
|
||||
|
||||
-- imaWavEncode: one Microsoft IMA block of spb frames (spb = 1 + 8k) from interleaved f32 x at frame xf (frames
|
||||
-- available: n; the rest is padded with the last sample) to out[off] (4 * ch + (spb - 1) / 2 * ch bytes). st: 8 * ch
|
||||
-- bytes carrying the step index from block to block.
|
||||
function Adpcm.imaWavEncode(x: buffer, xf: number, n: number, ch: number, spb: number, st: buffer, out: buffer, off: number)
|
||||
local groups = (spb - 1) // 8
|
||||
for c = 0, ch - 1 do
|
||||
local so = c * 8
|
||||
local first = if n > 0 then toS16(readf32(x, (xf * ch + c) * 4)) else 0
|
||||
writei32(st, so, first)
|
||||
local index = math.clamp(readi32(st, so + 4), 0, 88)
|
||||
writei32(st, so + 4, index)
|
||||
buffer.writei16(out, off + c * 4, first)
|
||||
writeu8(out, off + c * 4 + 2, index)
|
||||
writeu8(out, off + c * 4 + 3, 0)
|
||||
local pred = first
|
||||
for g = 0, groups - 1 do
|
||||
local gp = off + 4 * ch + (g * ch + c) * 4
|
||||
for k = 0, 7 do
|
||||
local f = 1 + g * 8 + k
|
||||
local v = if f < n then toS16(readf32(x, ((xf + f) * ch + c) * 4)) else pred
|
||||
local nib = imaNibble(v, pred, index)
|
||||
local diff = IMA_DIFF[index * 8 + band(nib, 7) + 1]
|
||||
pred = if nib >= 8 then math.max(pred - diff, -32768) else math.min(pred + diff, 32767)
|
||||
index = IMA_NEXT[index * 16 + nib + 1]
|
||||
local bp = gp + rshift(k, 1)
|
||||
if band(k, 1) == 0 then
|
||||
writeu8(out, bp, nib)
|
||||
else
|
||||
writeu8(out, bp, bor(readu8(out, bp), lshift(nib, 4)))
|
||||
end
|
||||
end
|
||||
end
|
||||
writei32(st, so + 4, index)
|
||||
end
|
||||
end
|
||||
|
||||
-- one MS ADPCM channel run for the encoder: the nibbles for samples 2 .. n - 1 with predictor (c1, c2) and the first
|
||||
-- delta; returns the squared error and the nibbles in nibs (when given)
|
||||
local function msTrial(xs: { number }, n: number, c1: number, c2: number, delta0: number, nibs: { number }?): number
|
||||
local s1, s2 = xs[2], xs[1]
|
||||
local idelta = delta0
|
||||
local err = 0
|
||||
for i = 3, n do
|
||||
local p = s1 * c1 + s2 * c2
|
||||
p = if p >= 0 then p // 256 else -((-p) // 256)
|
||||
local e = xs[i] - p
|
||||
local nib = if e >= 0 then (e + idelta // 2) // idelta else -((-e + idelta // 2) // idelta)
|
||||
nib = math.clamp(nib, -8, 7)
|
||||
local v = math.clamp(p + nib * idelta, -32768, 32767)
|
||||
err += (xs[i] - v) * (xs[i] - v)
|
||||
if nibs then
|
||||
nibs[i] = band(nib, 15)
|
||||
end
|
||||
idelta = math.clamp((MS_ADAPT[band(nib, 15) + 1] * idelta) // 256, 16, 2796202)
|
||||
s2 = s1
|
||||
s1 = v
|
||||
end
|
||||
return err
|
||||
end
|
||||
|
||||
-- msEncode: one MS ADPCM block of spb frames (standard 7 coefficient pairs) from interleaved f32 x at frame xf
|
||||
-- (frames available: n; the rest is padded with silence) to out[off] (7 * ch + (spb - 2) * ch / 2 bytes). Each channel
|
||||
-- takes the predictor with the least error.
|
||||
function Adpcm.msEncode(x: buffer, xf: number, n: number, ch: number, spb: number, out: buffer, off: number)
|
||||
local C1, C2 = Adpcm.MS_COEF1, Adpcm.MS_COEF2
|
||||
local xs = table.create(spb, 0)
|
||||
local nibs = table.create(spb, 0)
|
||||
local all = table.create(spb * ch, 0)
|
||||
for c = 0, ch - 1 do
|
||||
for i = 1, spb do
|
||||
xs[i] = if i <= n then toS16(readf32(x, ((xf + i - 1) * ch + c) * 4)) else 0
|
||||
end
|
||||
-- the first delta from the first steps
|
||||
local sum = 0
|
||||
for i = 3, math.min(spb, 10) do
|
||||
sum += math.abs(xs[i] - xs[i - 1])
|
||||
end
|
||||
local delta0 = math.clamp(sum // 16, 16, 32767)
|
||||
local best, bestErr = 1, math.huge
|
||||
for k = 1, 7 do
|
||||
local e = msTrial(xs, spb, C1[k], C2[k], delta0, nil)
|
||||
if e < bestErr then
|
||||
best, bestErr = k, e
|
||||
end
|
||||
end
|
||||
msTrial(xs, spb, C1[best], C2[best], delta0, nibs)
|
||||
writeu8(out, off + c, best - 1)
|
||||
writeu16(out, off + ch + c * 2, delta0)
|
||||
buffer.writei16(out, off + 3 * ch + c * 2, xs[2] or 0)
|
||||
buffer.writei16(out, off + 5 * ch + c * 2, xs[1])
|
||||
for i = 3, spb do
|
||||
all[(i - 3) * ch + c + 1] = nibs[i]
|
||||
end
|
||||
end
|
||||
local total = (spb - 2) * ch
|
||||
for k = 0, total - 1, 2 do
|
||||
writeu8(out, off + 7 * ch + k // 2, all[k + 1] * 16 + (all[k + 2] or 0))
|
||||
end
|
||||
end
|
||||
|
||||
-- one ADX frame for the encoder: the squared error of encoding xs[1..32] with scale from (h1, h2) (v4 rounding), and
|
||||
-- the nibbles in nibs when given; returns the error and the new history
|
||||
local function adxTrial(xs: { number }, scale: number, c1: number, c2: number, h1: number, h2: number, nibs: { number }?): (number, number, number)
|
||||
local err = 0
|
||||
for k = 1, 32 do
|
||||
local pred = (c1 * h1 + c2 * h2) // 4096
|
||||
local e = xs[k] - pred
|
||||
local n = if e >= 0 then (e + scale // 2) // scale else -((-e + scale // 2) // scale)
|
||||
n = math.clamp(n, -8, 7)
|
||||
local s = math.clamp(n * scale + pred, -32768, 32767)
|
||||
err += (xs[k] - s) * (xs[k] - s)
|
||||
if nibs then
|
||||
nibs[k] = band(n, 15)
|
||||
end
|
||||
h2 = h1
|
||||
h1 = s
|
||||
end
|
||||
return err, h1, h2
|
||||
end
|
||||
|
||||
-- adxEncode: count samples of one channel (f32 at float index xi every xs floats of x) as ceil(count / 32) ADX frames
|
||||
-- (v4 rounding, the scale stored minus one as CRI's decoders add it back) at out[p], pstride bytes apart; the frame
|
||||
-- past count is padded with silence. State as Adpcm.adx.
|
||||
function Adpcm.adxEncode(x: buffer, xi: number, xs: number, count: number, c1: number, c2: number, st: buffer, so: number,
|
||||
out: buffer, p: number, pstride: number)
|
||||
local h1, h2 = readi32(st, so), readi32(st, so + 4)
|
||||
local v = table.create(32, 0)
|
||||
local nibs = table.create(32, 0)
|
||||
for f = 0, (count + 31) // 32 - 1 do
|
||||
local peak = 0
|
||||
local a, b = h1, h2
|
||||
for k = 1, 32 do
|
||||
local i = f * 32 + k - 1
|
||||
local s = if i < count then toS16(readf32(x, (xi + i * xs) * 4)) else 0
|
||||
v[k] = s
|
||||
peak = math.max(peak, math.abs(s - (c1 * a + c2 * b) // 4096))
|
||||
b = a
|
||||
a = s
|
||||
end
|
||||
-- the scale that covers the open-loop residual peak, and a few around it, closed loop
|
||||
local base = math.max(1, (peak + 6) // 7)
|
||||
local best, bestErr = base, math.huge
|
||||
for _, m in { 1, 0.75, 1.25, 0.5, 1.6 } do
|
||||
local sc = math.clamp(floor(base * m + 0.5), 1, 0x7FFF)
|
||||
local e = adxTrial(v, sc, c1, c2, h1, h2, nil)
|
||||
if e < bestErr then
|
||||
best, bestErr = sc, e
|
||||
end
|
||||
end
|
||||
local _, n1, n2 = adxTrial(v, best, c1, c2, h1, h2, nibs)
|
||||
h1, h2 = n1, n2
|
||||
local fp = p + f * pstride
|
||||
writeu8(out, fp, rshift(best - 1, 8))
|
||||
writeu8(out, fp + 1, band(best - 1, 255))
|
||||
for k = 0, 15 do
|
||||
writeu8(out, fp + 2 + k, nibs[k * 2 + 1] * 16 + nibs[k * 2 + 2])
|
||||
end
|
||||
end
|
||||
writei32(st, so, h1)
|
||||
writei32(st, so + 4, h2)
|
||||
end
|
||||
|
||||
-- psxEncode: count samples of one channel (f32 at float index xi every xs floats of x) as ceil(count / 28) PS-ADPCM
|
||||
-- frames at out[p], pstride bytes apart, flags byte 0 (the caller sets loop / end flags); every filter and shift is
|
||||
-- tried per frame against Adpcm.psx's decoding, the least squared error wins. State as Adpcm.psx.
|
||||
function Adpcm.psxEncode(x: buffer, xi: number, xs: number, count: number, st: buffer, so: number, out: buffer,
|
||||
p: number, pstride: number)
|
||||
local h1, h2 = readi32(st, so), readi32(st, so + 4)
|
||||
local v = table.create(28, 0)
|
||||
local nibs = table.create(28, 0)
|
||||
for f = 0, (count + 27) // 28 - 1 do
|
||||
for k = 1, 28 do
|
||||
local i = f * 28 + k - 1
|
||||
v[k] = if i < count then toS16(readf32(x, (xi + i * xs) * 4)) else 0
|
||||
end
|
||||
local bestErr, bestHead = math.huge, 0
|
||||
for fi = 1, 5 do
|
||||
for shift = 0, 12 do
|
||||
local mul = 2 ^ (20 - shift)
|
||||
local a, b = h1, h2
|
||||
local err = 0
|
||||
for k = 1, 28 do
|
||||
local pred = psxPred(fi, a, b)
|
||||
local n = floor((v[k] * 256 - pred) / mul + 0.5)
|
||||
n = if n > 7 then 7 elseif n < -8 then -8 else n
|
||||
local s = (n * mul + pred) // 256
|
||||
local c = if s > 32767 then 32767 elseif s < -32768 then -32768 else s
|
||||
err += (v[k] - c) * (v[k] - c)
|
||||
if err >= bestErr then
|
||||
break
|
||||
end
|
||||
b = a
|
||||
a = s
|
||||
end
|
||||
if err < bestErr then
|
||||
bestErr, bestHead = err, (fi - 1) * 16 + shift
|
||||
end
|
||||
end
|
||||
end
|
||||
-- the winner again, for its nibbles and history
|
||||
local fi, shift = rshift(bestHead, 4) + 1, band(bestHead, 15)
|
||||
local mul = 2 ^ (20 - shift)
|
||||
for k = 1, 28 do
|
||||
local pred = psxPred(fi, h1, h2)
|
||||
local n = math.clamp(floor((v[k] * 256 - pred) / mul + 0.5), -8, 7)
|
||||
nibs[k] = band(n, 15)
|
||||
h2 = h1
|
||||
h1 = (n * mul + pred) // 256
|
||||
end
|
||||
local fp = p + f * pstride
|
||||
writeu8(out, fp, bestHead)
|
||||
writeu8(out, fp + 1, 0)
|
||||
for k = 0, 13 do
|
||||
writeu8(out, fp + 2 + k, nibs[k * 2 + 1] + nibs[k * 2 + 2] * 16)
|
||||
end
|
||||
end
|
||||
writei32(st, so, h1)
|
||||
writei32(st, so + 4, h2)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ ModPlug
|
||||
-- modplug: ModPlug / OpenMPT ADPCM samples (XM / MOD sample data marked 0xAD): a table of 16 signed byte deltas at
|
||||
-- p, then count nibbles (low first) at p + 16, each adding its table delta to a running signed byte (wrapping).
|
||||
-- Writes count f32 (value / 128) to dst from float index d. Reads p .. p + 16 + ceil(count / 2) - 1.
|
||||
function Adpcm.modplug(src: buffer, p: number, count: number, dst: buffer, d: number)
|
||||
local tab = table.create(16, 0)
|
||||
for k = 0, 15 do
|
||||
tab[k + 1] = buffer.readi8(src, p + k)
|
||||
end
|
||||
local v = 0
|
||||
for i = 0, count - 1 do
|
||||
local b = readu8(src, p + 16 + rshift(i, 1))
|
||||
local nib = if band(i, 1) == 0 then band(b, 15) else rshift(b, 4)
|
||||
v = (v + tab[nib + 1] + 128) % 256 - 128
|
||||
writef32(dst, (d + i) * 4, v / 128)
|
||||
end
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ Flash ADPCM
|
||||
-- The SWF / FLV ADPCM (SoundFormat 1, Adobe's SWF specification, "ADPCM compression"): an MSB-first bit stream of
|
||||
-- 2 bits (code size - 2), then blocks of 4096 frames: per channel a signed 16-bit first sample and a 6-bit step
|
||||
-- index, then 4095 frames of `bits`-bit codes, channels interleaved. A code is IMA generalised to 2..5 bits: sign in
|
||||
-- the top bit, the magnitude bits adding step, step/2, ... and the final step/2^(bits-1) (as ffmpeg's adpcm_swf and
|
||||
-- the spec's reference decoder do); the step index moves by SWF_INDEX[bits]. The last block may be short.
|
||||
local SWF_INDEX: { { number } } = {
|
||||
{ -1, 2 },
|
||||
{ -1, -1, 2, 4 },
|
||||
{ -1, -1, -1, -1, 2, 4, 6, 8 },
|
||||
{ -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 },
|
||||
}
|
||||
|
||||
-- swfFrames: the frame count of one Flash ADPCM packet (n bytes at p, n >= 1)
|
||||
function Adpcm.swfFrames(src: buffer, p: number, n: number, ch: number): number
|
||||
if n < 1 or ch < 1 then
|
||||
return 0
|
||||
end
|
||||
local bits = rshift(readu8(src, p), 6) + 2
|
||||
local total = n * 8 - 2
|
||||
local hdr = 22 * ch
|
||||
local block = hdr + bits * ch * 4095
|
||||
local blocks = total // block
|
||||
local left = total - blocks * block
|
||||
local frames = blocks * 4096
|
||||
if left >= hdr then
|
||||
frames += 1 + (left - hdr) // (bits * ch)
|
||||
end
|
||||
return frames
|
||||
end
|
||||
|
||||
-- the k-bit (k <= 16) MSB-first value at bit position pos of src[p ..]; bytes at or past e read as 0
|
||||
local function msb(src: buffer, p: number, e: number, pos: number, k: number): number
|
||||
local q = p + rshift(pos, 3)
|
||||
local v = readu8(src, q) * 65536
|
||||
if q + 1 < e then
|
||||
v += readu8(src, q + 1) * 256
|
||||
end
|
||||
if q + 2 < e then
|
||||
v += readu8(src, q + 2)
|
||||
end
|
||||
return band(rshift(v, 24 - band(pos, 7) - k), lshift(1, k) - 1)
|
||||
end
|
||||
|
||||
-- swf: one Flash ADPCM packet (n bytes at p, ch channels) -> interleaved f32 frames to dst from float index d; returns
|
||||
-- the frame count (Adpcm.swfFrames). Reads p .. p + n - 1 only.
|
||||
function Adpcm.swf(src: buffer, p: number, n: number, ch: number, dst: buffer, d: number): number
|
||||
local frames = Adpcm.swfFrames(src, p, n, ch)
|
||||
if frames == 0 then
|
||||
return 0
|
||||
end
|
||||
local e = p + n
|
||||
local bits = rshift(readu8(src, p), 6) + 2
|
||||
local tab = SWF_INDEX[bits - 1]
|
||||
local k0 = lshift(1, bits - 2)
|
||||
local sign = lshift(1, bits - 1)
|
||||
local mask = sign - 1
|
||||
local pred = table.create(ch, 0)
|
||||
local index = table.create(ch, 0)
|
||||
local pos = 2
|
||||
local o = d * 4
|
||||
local done = 0
|
||||
while done < frames do
|
||||
for c = 1, ch do
|
||||
local v = msb(src, p, e, pos, 16)
|
||||
if v >= 32768 then
|
||||
v -= 65536
|
||||
end
|
||||
pred[c] = v
|
||||
index[c] = msb(src, p, e, pos + 16, 6)
|
||||
pos += 22
|
||||
writef32(dst, o, v / 32768)
|
||||
o += 4
|
||||
end
|
||||
done += 1
|
||||
local m = math.min(4095, frames - done)
|
||||
for _ = 1, m do
|
||||
for c = 1, ch do
|
||||
local code = msb(src, p, e, pos, bits)
|
||||
pos += bits
|
||||
local step = IMA_STEP[math.clamp(index[c], 0, 88) + 1]
|
||||
local diff = 0
|
||||
local k = k0
|
||||
while k > 0 do
|
||||
if band(code, k) ~= 0 then
|
||||
diff += step
|
||||
end
|
||||
step = rshift(step, 1)
|
||||
k = rshift(k, 1)
|
||||
end
|
||||
diff += step
|
||||
local v = pred[c]
|
||||
if band(code, sign) ~= 0 then
|
||||
v -= diff
|
||||
else
|
||||
v += diff
|
||||
end
|
||||
v = math.clamp(v, -32768, 32767)
|
||||
pred[c] = v
|
||||
index[c] = math.clamp(index[c] + tab[band(code, mask) + 1], 0, 88)
|
||||
writef32(dst, o, v / 32768)
|
||||
o += 4
|
||||
end
|
||||
end
|
||||
done += m
|
||||
end
|
||||
return frames
|
||||
end
|
||||
|
||||
return Adpcm
|
||||
Reference in new issue
Block a user