119 lines
3.2 KiB
Luau
119 lines
3.2 KiB
Luau
--!strict
|
|
--!native
|
|
--!optimize 2
|
|
-- video/smk/Audio: Smacker packed audio (SMKA, MultimediaWiki "Smacker"): u32 unpacked size, then LSB-first bits: data,
|
|
-- stereo, 16-bit flags, an 8-bit Huffman tree per channel and byte, the first samples raw (16-bit byte-swapped, right
|
|
-- first), then wrapping deltas. Raw u8 / s16le too; floats as ffmpeg gives them (u8 (v - 128) / 128, s16 v / 32768).
|
|
local Smacker = require("@rosetta/codecs/video/Legacy/Smacker")
|
|
|
|
local readu8, readi16 = buffer.readu8, buffer.readi16
|
|
|
|
local Audio = {}
|
|
|
|
function Audio.decode(d: buffer, channels: number, bits: number, packed: boolean): (number?, { number } | string)
|
|
local n = buffer.len(d)
|
|
if not packed then
|
|
local bps = bits // 8
|
|
local frames = n // (bps * channels)
|
|
local count = frames * channels
|
|
local out = table.create(count, 0)
|
|
if bps == 1 then
|
|
for i = 0, count - 1 do
|
|
out[i + 1] = (readu8(d, i) - 128) / 128
|
|
end
|
|
else
|
|
for i = 0, count - 1 do
|
|
out[i + 1] = readi16(d, i * 2) / 32768
|
|
end
|
|
end
|
|
return frames, out
|
|
end
|
|
if n <= 4 then
|
|
return nil, "packet too small"
|
|
end
|
|
local size = buffer.readu32(d, 0)
|
|
if size > 2 ^ 24 then
|
|
return nil, "packet too big"
|
|
end
|
|
local br = Smacker.bits(d, 4, n - 4)
|
|
local bit, read, walk = Smacker.bit, Smacker.read, Smacker.walk
|
|
if bit(br) == 0 then
|
|
return 0, {}
|
|
end
|
|
local stereo = bit(br)
|
|
local wide = bit(br)
|
|
if (stereo == 1) ~= (channels == 2) or (wide == 1) ~= (bits == 16) then
|
|
return nil, "packet does not match the track's format"
|
|
end
|
|
local bytes = wide + 1
|
|
local ch = stereo + 1
|
|
if size % (ch * bytes) ~= 0 then
|
|
return nil, "unpacked size is not a whole number of samples"
|
|
end
|
|
local trees: { { number } } = {}
|
|
for k = 1, 2 ^ (wide + stereo) do
|
|
bit(br)
|
|
local t, msg = Smacker.tree8(br)
|
|
if not t then
|
|
return nil, msg or "bad tree"
|
|
end
|
|
bit(br)
|
|
trees[k] = t
|
|
end
|
|
if br.nbits - br.pos < ch * bytes * 8 then
|
|
return nil, "truncated packet"
|
|
end
|
|
-- whether any tree needs bits (else every delta is a constant and no end check applies)
|
|
local coded = false
|
|
for _, t in trees do
|
|
if #t > 1 then
|
|
coded = true
|
|
end
|
|
end
|
|
local frames = size // (ch * bytes)
|
|
local total = frames * ch
|
|
local out = table.create(total, 0)
|
|
if wide == 1 then
|
|
local pred = { 0, 0 }
|
|
for c = ch, 1, -1 do
|
|
local v = read(br, 16)
|
|
pred[c] = (v // 256) + (v % 256) * 256
|
|
end
|
|
for c = 1, math.min(ch, total) do
|
|
local v = pred[c]
|
|
out[c] = (if v >= 32768 then v - 65536 else v) / 32768
|
|
end
|
|
for i = ch, total - 1 do
|
|
local c = if ch == 2 then i % 2 else 0
|
|
if coded and br.pos > br.nbits then
|
|
return nil, "truncated packet"
|
|
end
|
|
local lo = walk(trees[2 * c + 1], br)
|
|
local hi = walk(trees[2 * c + 2], br)
|
|
local v = (pred[c + 1] + lo + hi * 256) % 65536
|
|
pred[c + 1] = v
|
|
out[i + 1] = (if v >= 32768 then v - 65536 else v) / 32768
|
|
end
|
|
else
|
|
local pred = { 0, 0 }
|
|
for c = ch, 1, -1 do
|
|
pred[c] = read(br, 8)
|
|
end
|
|
for c = 1, math.min(ch, total) do
|
|
out[c] = (pred[c] - 128) / 128
|
|
end
|
|
for i = ch, total - 1 do
|
|
local c = if ch == 2 then i % 2 else 0
|
|
if br.pos > br.nbits then
|
|
return nil, "truncated packet"
|
|
end
|
|
local v = (pred[c + 1] + walk(trees[c + 1], br)) % 256
|
|
pred[c + 1] = v
|
|
out[i + 1] = (v - 128) / 128
|
|
end
|
|
end
|
|
return frames, out
|
|
end
|
|
|
|
return Audio
|