ws-audio2: codecs/audio/Adpcm (IMA / ima4 / IMA WAV / MS / ADX / PS-ADPCM / DSP decode, IMA + MS encode, Decoder layouts) and audio/adpcm (raw streams)
- codecs/audio/Adpcm: decoder kernels bit-exact with ffmpeg (IMA, ima4, MS) and vgmstream (ADX, PS-ADPCM, DSP); IMA nibble / IMA WAV / MS block encoders. Adpcm/Decoder: streaming Decoders over the shared layouts (channel interleaved frames, nibble streams, self-contained blocks) with exact seeking through state snapshots. - audio/wav: its IMA / MS block decoders moved to codecs/audio/Adpcm (wav tests unchanged and green). - codecs/audio/Stream (shared, small and backward compatible): pcm() may return a buffer of f32 (copied with buffer.copy) instead of a table. - audio/adpcm: headerless ima / ima_wav / ms / ima_qt / psx streams by options; encode ima / ima_wav / ms. - licenses: vgmstream (ISC).
This commit is contained in:
1 parent
de4f14a343
commit
363b14932f
14 files changed
+1931
-166
No files matched your search
@@ -1,7 +1,8 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- audio/wav/Codecs: sample conversion loops (native) into interleaved f32, and back (the encoder's quantizers).
|
||||
-- audio/wav/Codecs: sample conversion loops (native) into interleaved f32, and back (the encoder's quantizers). The
|
||||
-- ADPCM block decoders moved to codecs/audio/Adpcm.
|
||||
-- Absorbed from wav-luau fd9a909 (same author); the encoder loops are new. They never throw: audio/wav (init.luau)
|
||||
-- only calls them with ranges it has checked against the source length and the output size.
|
||||
local readu8, readi8, readu16, readi16, readu32, readi32 =
|
||||
@@ -102,160 +103,6 @@ function Codecs.law(src: buffer, off: number, dst: buffer, d: number, count: num
|
||||
end
|
||||
end
|
||||
|
||||
local IMA_STEP = buffer.create(89 * 4)
|
||||
do
|
||||
local steps = {
|
||||
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,
|
||||
}
|
||||
for i, s in steps do
|
||||
buffer.writei32(IMA_STEP, (i - 1) * 4, s)
|
||||
end
|
||||
end
|
||||
local IMA_INDEX = buffer.create(16 * 4)
|
||||
do
|
||||
local t = { -1, -1, -1, -1, 2, 4, 6, 8, -1, -1, -1, -1, 2, 4, 6, 8 }
|
||||
for i, v in t do
|
||||
buffer.writei32(IMA_INDEX, (i - 1) * 4, v)
|
||||
end
|
||||
end
|
||||
|
||||
-- IMA ADPCM (Microsoft / WAV layout), one block at src[off] of blockAlign bytes with `avail` bytes present (a
|
||||
-- truncated last block decodes the whole 8-sample groups it has). Writes interleaved f32 to dst from frame 0 and
|
||||
-- returns the frame count.
|
||||
function Codecs.imaBlock(src: buffer, off: number, avail: number, ch: number, spb: number, dst: buffer): number
|
||||
if avail < 4 * ch then
|
||||
return 0
|
||||
end
|
||||
-- groups of 8 samples per channel (4 bytes each) after the per-channel headers
|
||||
local groups = (avail - 4 * ch) // (4 * ch)
|
||||
local frames = math.min(spb, 1 + groups * 8)
|
||||
for c = 0, ch - 1 do
|
||||
local pred = readi16(src, off + c * 4)
|
||||
local index = readu8(src, off + c * 4 + 2)
|
||||
if index > 88 then
|
||||
index = 88
|
||||
end
|
||||
writef32(dst, c * 4, pred / 32768)
|
||||
local f = 1
|
||||
local g = 0
|
||||
while f < frames do
|
||||
local p = off + 4 * ch + (g * ch + c) * 4
|
||||
for k = 0, 7 do
|
||||
if f >= frames then
|
||||
break
|
||||
end
|
||||
local b = readu8(src, p + rshift(k, 1))
|
||||
local nib = if band(k, 1) == 0 then band(b, 15) else rshift(b, 4)
|
||||
local step = buffer.readi32(IMA_STEP, index * 4)
|
||||
local diff = rshift(step, 3)
|
||||
if band(nib, 1) ~= 0 then
|
||||
diff += rshift(step, 2)
|
||||
end
|
||||
if band(nib, 2) ~= 0 then
|
||||
diff += rshift(step, 1)
|
||||
end
|
||||
if band(nib, 4) ~= 0 then
|
||||
diff += step
|
||||
end
|
||||
if band(nib, 8) ~= 0 then
|
||||
pred -= diff
|
||||
else
|
||||
pred += diff
|
||||
end
|
||||
if pred > 32767 then
|
||||
pred = 32767
|
||||
elseif pred < -32768 then
|
||||
pred = -32768
|
||||
end
|
||||
index += buffer.readi32(IMA_INDEX, nib * 4)
|
||||
if index < 0 then
|
||||
index = 0
|
||||
elseif index > 88 then
|
||||
index = 88
|
||||
end
|
||||
writef32(dst, (f * ch + c) * 4, pred / 32768)
|
||||
f += 1
|
||||
end
|
||||
g += 1
|
||||
end
|
||||
end
|
||||
return frames
|
||||
end
|
||||
|
||||
local MS_ADAPT = buffer.create(16 * 4)
|
||||
do
|
||||
local t = { 230, 230, 230, 230, 307, 409, 512, 614, 768, 614, 512, 409, 307, 230, 230, 230 }
|
||||
for i, v in t do
|
||||
buffer.writei32(MS_ADAPT, (i - 1) * 4, v)
|
||||
end
|
||||
end
|
||||
|
||||
-- MS ADPCM, one block. coefs: i32 pairs (coef1, coef2) x ncoef (256 = 1.0). state: 5 * ch i32 scratch.
|
||||
-- Returns the frame count (a truncated last block decodes the whole bytes it has).
|
||||
function Codecs.msBlock(src: buffer, off: number, avail: number, ch: number, spb: number, dst: buffer,
|
||||
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)
|
||||
-- state per channel: c1, c2, idelta, s1, s2 (5 i32)
|
||||
for c = 0, ch - 1 do
|
||||
local pi = readu8(src, off + c)
|
||||
if pi >= ncoef then
|
||||
pi = ncoef - 1
|
||||
end
|
||||
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
|
||||
buffer.writei32(state, so, buffer.readi32(coefs, pi * 8))
|
||||
buffer.writei32(state, so + 4, buffer.readi32(coefs, pi * 8 + 4))
|
||||
buffer.writei32(state, so + 8, idelta)
|
||||
buffer.writei32(state, so + 12, s1)
|
||||
buffer.writei32(state, so + 16, s2)
|
||||
writef32(dst, c * 4, s2 / 32768)
|
||||
if frames > 1 then
|
||||
writef32(dst, (ch + c) * 4, s1 / 32768)
|
||||
end
|
||||
end
|
||||
-- nibbles, high first, channels interleaved per nibble
|
||||
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 c = k % ch
|
||||
local so = c * 20
|
||||
local c1, c2 = buffer.readi32(state, so), buffer.readi32(state, so + 4)
|
||||
local idelta = buffer.readi32(state, so + 8)
|
||||
local s1, s2 = buffer.readi32(state, so + 12), buffer.readi32(state, so + 16)
|
||||
local p = s1 * c1 + s2 * c2
|
||||
p = if p >= 0 then p // 256 else -((-p) // 256)
|
||||
local sn = if nib >= 8 then nib - 16 else nib
|
||||
p += sn * idelta
|
||||
if p > 32767 then
|
||||
p = 32767
|
||||
elseif p < -32768 then
|
||||
p = -32768
|
||||
end
|
||||
idelta = (buffer.readi32(MS_ADAPT, nib * 4) * idelta) // 256
|
||||
if idelta < 16 then
|
||||
idelta = 16
|
||||
elseif idelta > 2796202 then -- INT_MAX / 768, as ffmpeg
|
||||
idelta = 2796202
|
||||
end
|
||||
buffer.writei32(state, so + 8, idelta)
|
||||
buffer.writei32(state, so + 12, p)
|
||||
buffer.writei32(state, so + 16, s1)
|
||||
writef32(dst, (2 * ch + k) * 4, p / 32768)
|
||||
end
|
||||
return frames
|
||||
end
|
||||
|
||||
-- copy frames from a block cache into the output: count f32 values
|
||||
function Codecs.copy(src: buffer, sOff: number, dst: buffer, dOff: number, count: number)
|
||||
buffer.copy(dst, dOff * 4, src, sOff * 4, count * 4)
|
||||
|
||||
@@ -21,6 +21,7 @@ local Types = require("@formats/Types")
|
||||
local Pcm = require("@formats/codecs/audio/Pcm")
|
||||
local Riff = require("@formats/containers/Riff")
|
||||
local Codecs = require("@formats/audio/wav/Codecs")
|
||||
local Adpcm = require("@formats/codecs/audio/Adpcm")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Wav = {
|
||||
@@ -28,7 +29,7 @@ local Wav = {
|
||||
category = "audio",
|
||||
extensions = { "wav", "wave", "rf64", "bw64" },
|
||||
mime = { "audio/wav", "audio/x-wav", "audio/wave", "audio/vnd.wave" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "containers/Riff", "codecs/audio/Pcm" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "containers/Riff", "codecs/audio/Pcm", "codecs/audio/Adpcm" },
|
||||
caps = { decode = true, encode = true, streaming = true, depths = { 8, 16, 24, 32 }, float = true, rf64 = true },
|
||||
}
|
||||
|
||||
@@ -488,8 +489,7 @@ function Wav.open(r: Bytes.Reader, _opts: OpenOptions?): (Decoder?, Err.Err?)
|
||||
spb = computed
|
||||
end
|
||||
-- coefficients from the fmt extension, else the 7 standard pairs
|
||||
local c1 = { 256, 512, 0, 192, 240, 460, 392 }
|
||||
local c2 = { 0, -256, 0, 64, 0, -208, -232 }
|
||||
local c1, c2 = Adpcm.MS_COEF1, Adpcm.MS_COEF2
|
||||
local ncoef = 7
|
||||
if fmt.avail >= 22 then
|
||||
local k = u16(fmt.body + 20)
|
||||
@@ -592,9 +592,9 @@ function Decoder.read(self: Decoder, maxFrames: number, out: buffer?): (buffer,
|
||||
local avail = math.min(ba, self.dataOffset + self.dataSize - off)
|
||||
local got
|
||||
if kind == "ima_adpcm" then
|
||||
got = Codecs.imaBlock(self._buf, off, avail, ch, spb, cache)
|
||||
got = Adpcm.imaWav(self._buf, off, avail, ch, spb, cache, 0)
|
||||
else
|
||||
got = Codecs.msBlock(self._buf, off, avail, ch, spb, cache, self._coefs :: buffer, self._ncoef,
|
||||
got = Adpcm.msBlock(self._buf, off, avail, ch, spb, cache, 0, self._coefs :: buffer, self._ncoef,
|
||||
self._state :: buffer)
|
||||
end
|
||||
self._cacheBlock = block
|
||||
|
||||
Reference in new issue
Block a user