ws-audio2: audio/adx (CRI ADX read / write, loops) and audio/vag (VAGp / VAGi / KVAG read, VAGp / VAGi write)
Bit-exact with vgmstream (ADX, PS-ADPCM) and ffmpeg (KVAG IMA); ADX and PS-ADPCM encoders in codecs/audio/Adpcm.
This commit is contained in:
1 parent
363b14932f
commit
fd9175883d
10 files changed
+987
-1
No files matched your search
@@ -565,4 +565,124 @@ function Adpcm.msEncode(x: buffer, xf: number, n: number, ch: number, spb: numbe
|
||||
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
|
||||
|
||||
return Adpcm
|
||||
Reference in new issue
Block a user