596 lines
14 KiB
Luau
596 lines
14 KiB
Luau
--!strict
|
|
--!native
|
|
--!optimize 2
|
|
-- codecs/audio/Flac/Frame: FLAC frames: header (CRC-8), sync search, subframes, residuals, decorrelation, CRC-16.
|
|
-- Absorbed from flac-luau 7083d0d (no third-party code). The input needs PAD zero bytes after `len`: the bit reader
|
|
-- reads 32-bit words unchecked and compares its position with the data end once per sample.
|
|
local readu8, readu32 = buffer.readu8, buffer.readu32
|
|
local band, bxor, lshift, rshift, countlz, byteswap = bit32.band, bit32.bxor, bit32.lshift, bit32.rshift,
|
|
bit32.countlz, bit32.byteswap
|
|
local floor = math.floor
|
|
|
|
local Frame = {}
|
|
Frame.PAD = 32
|
|
|
|
-- CRC-8 (poly 0x07) and CRC-16 (poly 0x8005), MSB first, init 0
|
|
local CRC8 = table.create(256, 0)
|
|
local CRC16 = table.create(256, 0)
|
|
for i = 0, 255 do
|
|
local c = i
|
|
for _ = 1, 8 do
|
|
c = if band(c, 0x80) ~= 0 then band(bxor(lshift(c, 1), 0x07), 0xFF) else band(lshift(c, 1), 0xFF)
|
|
end
|
|
CRC8[i + 1] = c
|
|
local d = lshift(i, 8)
|
|
for _ = 1, 8 do
|
|
d = if band(d, 0x8000) ~= 0 then band(bxor(lshift(d, 1), 0x8005), 0xFFFF) else band(lshift(d, 1), 0xFFFF)
|
|
end
|
|
CRC16[i + 1] = d
|
|
end
|
|
|
|
local function crc16(b: buffer, from: number, to: number): number
|
|
local c = 0
|
|
for i = from, to - 1 do
|
|
c = bxor(band(lshift(c, 8), 0xFFFF), CRC16[bxor(rshift(c, 8), readu8(b, i)) + 1])
|
|
end
|
|
return c
|
|
end
|
|
Frame.crc16 = crc16
|
|
|
|
local function crc8(b: buffer, from: number, to: number): number
|
|
local c = 0
|
|
for i = from, to - 1 do
|
|
c = CRC8[bxor(c, readu8(b, i)) + 1]
|
|
end
|
|
return c
|
|
end
|
|
Frame.crc8 = crc8
|
|
|
|
local POW2 = table.create(64, 0)
|
|
for i = 0, 63 do
|
|
POW2[i + 1] = 2 ^ i
|
|
end
|
|
|
|
local BLOCK = { [1] = 192, [2] = 576, [3] = 1152, [4] = 2304, [5] = 4608, [8] = 256, [9] = 512, [10] = 1024,
|
|
[11] = 2048, [12] = 4096, [13] = 8192, [14] = 16384, [15] = 32768 }
|
|
local RATE = { [1] = 88200, [2] = 176400, [3] = 192000, [4] = 8000, [5] = 16000, [6] = 22050, [7] = 24000,
|
|
[8] = 32000, [9] = 44100, [10] = 48000, [11] = 96000 }
|
|
local BITS = { [1] = 8, [2] = 12, [4] = 16, [5] = 20, [6] = 24, [7] = 32 }
|
|
|
|
export type Header = {
|
|
offset: number,
|
|
size: number, -- header bytes, CRC-8 included
|
|
blockSize: number,
|
|
sampleRate: number,
|
|
channels: number,
|
|
assignment: number, -- 0..7 independent, 8 left/side, 9 side/right, 10 mid/side
|
|
bps: number,
|
|
variable: boolean, -- variable block size: number is a sample number, else a frame number
|
|
number: number,
|
|
}
|
|
|
|
function Frame.header(b: buffer, pos: number, len: number, si: any): Header?
|
|
if pos < 0 or pos + 6 > len then
|
|
return nil
|
|
end
|
|
if readu8(b, pos) ~= 0xFF then
|
|
return nil
|
|
end
|
|
local b1 = readu8(b, pos + 1)
|
|
if band(b1, 0xFE) ~= 0xF8 then
|
|
return nil
|
|
end
|
|
local b2 = readu8(b, pos + 2)
|
|
local b3 = readu8(b, pos + 3)
|
|
local bsCode, rCode = rshift(b2, 4), band(b2, 15)
|
|
local chCode, sCode = rshift(b3, 4), band(rshift(b3, 1), 7)
|
|
if bsCode == 0 or rCode == 15 or chCode > 10 or sCode == 3 or band(b3, 1) ~= 0 then
|
|
return nil
|
|
end
|
|
-- UTF-8-like coded number
|
|
local p = pos + 4
|
|
local x = readu8(b, p)
|
|
p += 1
|
|
local num, more
|
|
if x < 0x80 then
|
|
num, more = x, 0
|
|
elseif x >= 0xC0 and x < 0xE0 then
|
|
num, more = band(x, 0x1F), 1
|
|
elseif x >= 0xE0 and x < 0xF0 then
|
|
num, more = band(x, 0x0F), 2
|
|
elseif x >= 0xF0 and x < 0xF8 then
|
|
num, more = band(x, 0x07), 3
|
|
elseif x >= 0xF8 and x < 0xFC then
|
|
num, more = band(x, 0x03), 4
|
|
elseif x >= 0xFC and x < 0xFE then
|
|
num, more = band(x, 0x01), 5
|
|
elseif x == 0xFE then
|
|
num, more = 0, 6
|
|
else
|
|
return nil
|
|
end
|
|
for _ = 1, more do
|
|
local c = readu8(b, p)
|
|
p += 1
|
|
if band(c, 0xC0) ~= 0x80 then
|
|
return nil
|
|
end
|
|
num = num * 64 + band(c, 0x3F)
|
|
end
|
|
local variable = band(b1, 1) == 1
|
|
if not variable and more > 5 then
|
|
return nil
|
|
end
|
|
local bs = BLOCK[bsCode]
|
|
if bsCode == 6 then
|
|
bs = readu8(b, p) + 1
|
|
p += 1
|
|
elseif bsCode == 7 then
|
|
bs = readu8(b, p) * 256 + readu8(b, p + 1) + 1
|
|
p += 2
|
|
end
|
|
local rate = RATE[rCode]
|
|
if rCode == 0 then
|
|
rate = si and si.sampleRate or 0
|
|
elseif rCode == 12 then
|
|
rate = readu8(b, p) * 1000
|
|
p += 1
|
|
elseif rCode == 13 then
|
|
rate = readu8(b, p) * 256 + readu8(b, p + 1)
|
|
p += 2
|
|
elseif rCode == 14 then
|
|
rate = (readu8(b, p) * 256 + readu8(b, p + 1)) * 10
|
|
p += 2
|
|
end
|
|
local bps = BITS[sCode]
|
|
if sCode == 0 then
|
|
bps = si and si.bitsPerSample or 0
|
|
end
|
|
if not bs or not bps or bps < 4 or bps > 32 or p + 1 > len then
|
|
return nil
|
|
end
|
|
if crc8(b, pos, p) ~= readu8(b, p) then
|
|
return nil
|
|
end
|
|
return {
|
|
offset = pos,
|
|
size = p + 1 - pos,
|
|
blockSize = bs,
|
|
sampleRate = rate or 0,
|
|
channels = if chCode < 8 then chCode + 1 else 2,
|
|
assignment = chCode,
|
|
bps = bps,
|
|
variable = variable,
|
|
number = num,
|
|
}
|
|
end
|
|
|
|
function Frame.find(b: buffer, pos: number, limit: number, len: number, si: any): Header?
|
|
local lim = math.min(limit, len - 6)
|
|
if pos < 0 then
|
|
pos = 0
|
|
end
|
|
while pos < lim do
|
|
if readu8(b, pos) == 0xFF then
|
|
local b1 = readu8(b, pos + 1)
|
|
if b1 == 0xF8 or b1 == 0xF9 then
|
|
local h = Frame.header(b, pos, len, si)
|
|
if h then
|
|
return h
|
|
end
|
|
end
|
|
end
|
|
pos += 1
|
|
end
|
|
return nil
|
|
end
|
|
|
|
local function bits(b: buffer, p: number, n: number): number
|
|
if n == 0 then
|
|
return 0
|
|
elseif n <= 24 then
|
|
return rshift(lshift(byteswap(readu32(b, rshift(p, 3))), band(p, 7)), 32 - n)
|
|
end
|
|
local hi = rshift(lshift(byteswap(readu32(b, rshift(p, 3))), band(p, 7)), 32 - (n - 16))
|
|
local q = p + n - 16
|
|
return hi * 65536 + rshift(lshift(byteswap(readu32(b, rshift(q, 3))), band(q, 7)), 16)
|
|
end
|
|
|
|
local function sbits(b: buffer, p: number, n: number): number
|
|
local v = bits(b, p, n)
|
|
if n > 0 and v >= POW2[n] then
|
|
v -= POW2[n + 1]
|
|
end
|
|
return v
|
|
end
|
|
|
|
local function residual(b: buffer, p: number, lim: number, s: { number }, n: number, order: number): number
|
|
local method = bits(b, p, 2)
|
|
if method > 1 then
|
|
return -1
|
|
end
|
|
local porder = bits(b, p + 2, 4)
|
|
p += 6
|
|
local pbits = if method == 0 then 4 else 5
|
|
local escape = if method == 0 then 15 else 31
|
|
local parts = POW2[porder + 1]
|
|
local psize = rshift(n, porder)
|
|
if psize * parts ~= n or psize < order then
|
|
return -1
|
|
end
|
|
local i = order + 1
|
|
for part = 0, parts - 1 do
|
|
if p > lim then
|
|
return -1
|
|
end
|
|
local k = bits(b, p, pbits)
|
|
p += pbits
|
|
local stop = (part + 1) * psize
|
|
if k == escape then
|
|
local nb = bits(b, p, 5)
|
|
p += 5
|
|
if nb == 0 then
|
|
for j = i, stop do
|
|
s[j] = 0
|
|
end
|
|
else
|
|
local half, full = POW2[nb], POW2[nb + 1]
|
|
for j = i, stop do
|
|
if p > lim then
|
|
return -1
|
|
end
|
|
local v = bits(b, p, nb)
|
|
p += nb
|
|
if v >= half then
|
|
v -= full
|
|
end
|
|
s[j] = v
|
|
end
|
|
end
|
|
elseif k <= 24 then
|
|
local pk = POW2[k + 1]
|
|
local rk = 32 - k
|
|
for j = i, stop do
|
|
if p > lim then
|
|
return -1
|
|
end
|
|
local sh = band(p, 7)
|
|
local w = lshift(byteswap(readu32(b, rshift(p, 3))), sh)
|
|
local q, r
|
|
if w ~= 0 then
|
|
q = countlz(w)
|
|
local used = q + 1 + k
|
|
if used <= 32 - sh then
|
|
r = if k == 0 then 0 else rshift(lshift(w, q + 1), rk)
|
|
p += used
|
|
else
|
|
p += q + 1
|
|
r = if k == 0 then 0 else rshift(lshift(byteswap(readu32(b, rshift(p, 3))), band(p, 7)), rk)
|
|
p += k
|
|
end
|
|
else
|
|
q = 32 - sh
|
|
p += q
|
|
while true do
|
|
if p > lim then
|
|
return -1
|
|
end
|
|
local w2 = lshift(byteswap(readu32(b, rshift(p, 3))), band(p, 7))
|
|
if w2 ~= 0 then
|
|
local z = countlz(w2)
|
|
q += z
|
|
p += z + 1
|
|
break
|
|
end
|
|
local adv = 32 - band(p, 7)
|
|
q += adv
|
|
p += adv
|
|
end
|
|
r = if k == 0 then 0 else rshift(lshift(byteswap(readu32(b, rshift(p, 3))), band(p, 7)), rk)
|
|
p += k
|
|
end
|
|
local u = q * pk + r
|
|
local h = floor(u * 0.5)
|
|
s[j] = if h + h == u then h else -h - 1
|
|
end
|
|
else
|
|
-- k = 25..30 (Rice2): the remainder takes two reads
|
|
local pk = POW2[k + 1]
|
|
for j = i, stop do
|
|
if p > lim then
|
|
return -1
|
|
end
|
|
local q = 0
|
|
while true do
|
|
if p > lim then
|
|
return -1
|
|
end
|
|
local w = lshift(byteswap(readu32(b, rshift(p, 3))), band(p, 7))
|
|
if w ~= 0 then
|
|
local z = countlz(w)
|
|
q += z
|
|
p += z + 1
|
|
break
|
|
end
|
|
local adv = 32 - band(p, 7)
|
|
q += adv
|
|
p += adv
|
|
end
|
|
local r = bits(b, p, k)
|
|
p += k
|
|
local u = q * pk + r
|
|
local h = floor(u * 0.5)
|
|
s[j] = if h + h == u then h else -h - 1
|
|
end
|
|
end
|
|
i = stop + 1
|
|
end
|
|
return p
|
|
end
|
|
|
|
local function fixed(s: { number }, n: number, order: number)
|
|
if order == 1 then
|
|
local a = s[1]
|
|
for i = 2, n do
|
|
a += s[i]
|
|
s[i] = a
|
|
end
|
|
elseif order == 2 then
|
|
local a, c = s[2], s[1]
|
|
for i = 3, n do
|
|
local v = s[i] + 2 * a - c
|
|
s[i] = v
|
|
c = a
|
|
a = v
|
|
end
|
|
elseif order == 3 then
|
|
local a, c, d = s[3], s[2], s[1]
|
|
for i = 4, n do
|
|
local v = s[i] + 3 * a - 3 * c + d
|
|
s[i] = v
|
|
d = c
|
|
c = a
|
|
a = v
|
|
end
|
|
elseif order == 4 then
|
|
local a, c, d, e = s[4], s[3], s[2], s[1]
|
|
for i = 5, n do
|
|
local v = s[i] + 4 * a - 6 * c + 4 * d - e
|
|
s[i] = v
|
|
e = d
|
|
d = c
|
|
c = a
|
|
a = v
|
|
end
|
|
end
|
|
end
|
|
|
|
-- LPC restoration: s[i] += floor(sum(coef[j] * s[i - j]) / 2^shift); unrolled for orders up to 12
|
|
local function lpc(s: { number }, n: number, cf: { number }, order: number, shift: number)
|
|
local div = POW2[shift + 1]
|
|
local inv = 1 / div
|
|
if order <= 4 then
|
|
local c1, c2, c3, c4 = cf[1], cf[2] or 0, cf[3] or 0, cf[4] or 0
|
|
local s1, s2, s3, s4 = s[order], s[order - 1] or 0, s[order - 2] or 0, s[order - 3] or 0
|
|
for i = order + 1, n do
|
|
local v = s[i] + floor((c1 * s1 + c2 * s2 + c3 * s3 + c4 * s4) * inv)
|
|
s[i] = v
|
|
s4 = s3
|
|
s3 = s2
|
|
s2 = s1
|
|
s1 = v
|
|
end
|
|
elseif order <= 8 then
|
|
local c1, c2, c3, c4, c5, c6, c7, c8 = cf[1], cf[2], cf[3], cf[4], cf[5], cf[6] or 0, cf[7] or 0, cf[8] or 0
|
|
local s1, s2, s3, s4, s5 = s[order], s[order - 1], s[order - 2], s[order - 3], s[order - 4]
|
|
local s6, s7, s8 = s[order - 5] or 0, s[order - 6] or 0, s[order - 7] or 0
|
|
for i = order + 1, n do
|
|
local v = s[i]
|
|
+ floor((c1 * s1 + c2 * s2 + c3 * s3 + c4 * s4 + c5 * s5 + c6 * s6 + c7 * s7 + c8 * s8) * inv)
|
|
s[i] = v
|
|
s8 = s7
|
|
s7 = s6
|
|
s6 = s5
|
|
s5 = s4
|
|
s4 = s3
|
|
s3 = s2
|
|
s2 = s1
|
|
s1 = v
|
|
end
|
|
elseif order <= 12 then
|
|
local c1, c2, c3, c4, c5, c6, c7, c8 = cf[1], cf[2], cf[3], cf[4], cf[5], cf[6], cf[7], cf[8]
|
|
local c9, c10, c11, c12 = cf[9], cf[10] or 0, cf[11] or 0, cf[12] or 0
|
|
local s1, s2, s3, s4, s5, s6 = s[order], s[order - 1], s[order - 2], s[order - 3], s[order - 4], s[order - 5]
|
|
local s7, s8, s9 = s[order - 6], s[order - 7], s[order - 8]
|
|
local s10, s11, s12 = s[order - 9] or 0, s[order - 10] or 0, s[order - 11] or 0
|
|
for i = order + 1, n do
|
|
local v = s[i]
|
|
+ floor(
|
|
(
|
|
c1 * s1 + c2 * s2 + c3 * s3 + c4 * s4 + c5 * s5 + c6 * s6
|
|
+ c7 * s7 + c8 * s8 + c9 * s9 + c10 * s10 + c11 * s11 + c12 * s12
|
|
) * inv
|
|
)
|
|
s[i] = v
|
|
s12 = s11
|
|
s11 = s10
|
|
s10 = s9
|
|
s9 = s8
|
|
s8 = s7
|
|
s7 = s6
|
|
s6 = s5
|
|
s5 = s4
|
|
s4 = s3
|
|
s3 = s2
|
|
s2 = s1
|
|
s1 = v
|
|
end
|
|
else
|
|
for i = order + 1, n do
|
|
local sum = 0
|
|
for j = 1, order do
|
|
sum += cf[j] * s[i - j]
|
|
end
|
|
s[i] += floor(sum * inv)
|
|
end
|
|
end
|
|
end
|
|
|
|
local function subframe(b: buffer, p: number, lim: number, s: { number }, n: number, bps: number, cf: { number }): number
|
|
if p > lim then
|
|
return -1
|
|
end
|
|
local hdr = bits(b, p, 8)
|
|
p += 8
|
|
if hdr >= 128 then
|
|
return -1
|
|
end
|
|
local typ = rshift(hdr, 1)
|
|
local wasted = 0
|
|
if band(hdr, 1) == 1 then
|
|
-- unary count of wasted bits (minus one)
|
|
while true do
|
|
if p > lim or wasted > 32 then
|
|
return -1
|
|
end
|
|
local bit = bits(b, p, 1)
|
|
p += 1
|
|
wasted += 1
|
|
if bit == 1 then
|
|
break
|
|
end
|
|
end
|
|
bps -= wasted
|
|
if bps < 1 then
|
|
return -1
|
|
end
|
|
end
|
|
if typ == 0 then
|
|
local v = sbits(b, p, bps)
|
|
p += bps
|
|
for i = 1, n do
|
|
s[i] = v
|
|
end
|
|
elseif typ == 1 then
|
|
for i = 1, n do
|
|
if p > lim then
|
|
return -1
|
|
end
|
|
s[i] = sbits(b, p, bps)
|
|
p += bps
|
|
end
|
|
elseif typ >= 8 and typ <= 12 then
|
|
local order = typ - 8
|
|
if order > n then
|
|
return -1
|
|
end
|
|
for i = 1, order do
|
|
if p > lim then
|
|
return -1
|
|
end
|
|
s[i] = sbits(b, p, bps)
|
|
p += bps
|
|
end
|
|
p = residual(b, p, lim, s, n, order)
|
|
if p < 0 then
|
|
return -1
|
|
end
|
|
fixed(s, n, order)
|
|
elseif typ >= 32 then
|
|
local order = typ - 31
|
|
if order > n then
|
|
return -1
|
|
end
|
|
for i = 1, order do
|
|
if p > lim then
|
|
return -1
|
|
end
|
|
s[i] = sbits(b, p, bps)
|
|
p += bps
|
|
end
|
|
local prec = bits(b, p, 4) + 1
|
|
p += 4
|
|
if prec == 16 then
|
|
return -1
|
|
end
|
|
local shift = sbits(b, p, 5)
|
|
p += 5
|
|
if shift < 0 then
|
|
return -1
|
|
end
|
|
for j = 1, order do
|
|
if p > lim then
|
|
return -1
|
|
end
|
|
cf[j] = sbits(b, p, prec)
|
|
p += prec
|
|
end
|
|
for j = order + 1, 12 do
|
|
cf[j] = 0 -- the unrolled filters read up to 12 coefficients
|
|
end
|
|
p = residual(b, p, lim, s, n, order)
|
|
if p < 0 then
|
|
return -1
|
|
end
|
|
lpc(s, n, cf, order, shift)
|
|
else
|
|
return -1
|
|
end
|
|
if wasted > 0 then
|
|
local m = POW2[wasted + 1]
|
|
for i = 1, n do
|
|
s[i] *= m
|
|
end
|
|
end
|
|
return p
|
|
end
|
|
|
|
function Frame.decode(b: buffer, len: number, h: Header, chans: { { number } }, cf: { number }, checkCrc: boolean): (number?, string?)
|
|
local n = h.blockSize
|
|
local lim = len * 8
|
|
local p = (h.offset + h.size) * 8
|
|
local asg = h.assignment
|
|
for c = 1, h.channels do
|
|
local bps = h.bps
|
|
if (asg == 8 and c == 2) or (asg == 9 and c == 1) or (asg == 10 and c == 2) then
|
|
bps += 1 -- the side channel
|
|
end
|
|
p = subframe(b, p, lim, chans[c], n, bps, cf)
|
|
if p < 0 then
|
|
return nil, "bad subframe"
|
|
end
|
|
end
|
|
local e = rshift(p + 7, 3)
|
|
if e + 2 > len then
|
|
return nil, "truncated frame"
|
|
end
|
|
if checkCrc then
|
|
local want = readu8(b, e) * 256 + readu8(b, e + 1)
|
|
if crc16(b, h.offset, e) ~= want then
|
|
return nil, "crc16 mismatch"
|
|
end
|
|
end
|
|
if asg >= 8 then
|
|
local l, r = chans[1], chans[2]
|
|
if asg == 8 then
|
|
for i = 1, n do
|
|
r[i] = l[i] - r[i]
|
|
end
|
|
elseif asg == 9 then
|
|
for i = 1, n do
|
|
l[i] += r[i]
|
|
end
|
|
else
|
|
for i = 1, n do
|
|
local side = r[i]
|
|
local m = l[i] * 2 + (side % 2)
|
|
l[i] = (m + side) * 0.5
|
|
r[i] = (m - side) * 0.5
|
|
end
|
|
end
|
|
end
|
|
return e + 2, nil
|
|
end
|
|
|
|
return Frame
|