ws-audio: FLAC encoder (audio/flac/Encode: levels 0-8 like libFLAC presets, fixed + LPC + Rice/Rice2 partition search, wasted bits, stereo decorrelation, MD5, VORBIS_COMMENT, SEEKTABLE, padding; streams from a Decoder) wired into audio/flac encode; core/Md5 (new core module, RFC 1321) + test; fixtures: flac -t + MD5 bit-exact on 76 encodes, sizes within 1.1% of libFLAC; bench lines
This commit is contained in:
1 parent
bdab71df77
commit
c74c93ae11
10 files changed
+1553
-6
No files matched your search
@@ -0,0 +1,164 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- core/Md5: MD5 (RFC 1321), incremental. Written from the RFC. Used by the FLAC encoder (STREAMINFO's MD5 of the PCM);
|
||||
-- not for security.
|
||||
--
|
||||
-- local h = Md5.new()
|
||||
-- h:update(buf, offset?, length?) -- any number of times
|
||||
-- local digest = h:digest() -- 16-byte string (the state is finished; make a new one to hash again)
|
||||
-- Md5.hex(digest) / Md5.of(buf) -> hex string
|
||||
-- Never throws (ranges are clamped to the buffer).
|
||||
local Md5 = {}
|
||||
|
||||
local band, bor, bxor, bnot, lrotate = bit32.band, bit32.bor, bit32.bxor, bit32.bnot, bit32.lrotate
|
||||
local readu32, writeu32, writeu8 = buffer.readu32, buffer.writeu32, buffer.writeu8
|
||||
|
||||
local K = table.create(64, 0)
|
||||
for i = 0, 63 do
|
||||
K[i + 1] = math.floor(math.abs(math.sin(i + 1)) * 4294967296) % 4294967296
|
||||
end
|
||||
local S = {
|
||||
7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
|
||||
5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
|
||||
4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
|
||||
6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21,
|
||||
}
|
||||
local G = table.create(64, 0)
|
||||
for i = 0, 63 do
|
||||
if i < 16 then
|
||||
G[i + 1] = i
|
||||
elseif i < 32 then
|
||||
G[i + 1] = (5 * i + 1) % 16
|
||||
elseif i < 48 then
|
||||
G[i + 1] = (3 * i + 5) % 16
|
||||
else
|
||||
G[i + 1] = (7 * i) % 16
|
||||
end
|
||||
end
|
||||
|
||||
export type State = {
|
||||
a: number,
|
||||
b: number,
|
||||
c: number,
|
||||
d: number,
|
||||
block: buffer, -- 64-byte pending block
|
||||
fill: number,
|
||||
total: number, -- bytes hashed
|
||||
update: (self: State, buf: buffer, offset: number?, length: number?) -> (),
|
||||
digest: (self: State) -> string,
|
||||
}
|
||||
|
||||
local M = table.create(16, 0)
|
||||
|
||||
local function compress(st: State, blk: buffer, o: number)
|
||||
for i = 0, 15 do
|
||||
M[i + 1] = readu32(blk, o + i * 4)
|
||||
end
|
||||
local a, b, c, d = st.a, st.b, st.c, st.d
|
||||
for i = 0, 63 do
|
||||
local f
|
||||
if i < 16 then
|
||||
f = bor(band(b, c), band(bnot(b), d))
|
||||
elseif i < 32 then
|
||||
f = bor(band(d, b), band(bnot(d), c))
|
||||
elseif i < 48 then
|
||||
f = bxor(b, c, d)
|
||||
else
|
||||
f = bxor(c, bor(b, bnot(d)))
|
||||
end
|
||||
local tmp = d
|
||||
d = c
|
||||
c = b
|
||||
b = (b + lrotate((a + f + K[i + 1] + M[G[i + 1] + 1]) % 4294967296, S[i + 1])) % 4294967296
|
||||
a = tmp
|
||||
end
|
||||
st.a = (st.a + a) % 4294967296
|
||||
st.b = (st.b + b) % 4294967296
|
||||
st.c = (st.c + c) % 4294967296
|
||||
st.d = (st.d + d) % 4294967296
|
||||
end
|
||||
|
||||
local function update(self: State, buf: buffer, offset: number?, length: number?)
|
||||
local n = buffer.len(buf)
|
||||
local o = math.clamp(offset or 0, 0, n)
|
||||
local len = math.clamp(length or (n - o), 0, n - o)
|
||||
self.total += len
|
||||
local blk = self.block
|
||||
if self.fill > 0 then
|
||||
local take = math.min(64 - self.fill, len)
|
||||
buffer.copy(blk, self.fill, buf, o, take)
|
||||
self.fill += take
|
||||
o += take
|
||||
len -= take
|
||||
if self.fill == 64 then
|
||||
compress(self, blk, 0)
|
||||
self.fill = 0
|
||||
end
|
||||
end
|
||||
while len >= 64 do
|
||||
compress(self, buf, o)
|
||||
o += 64
|
||||
len -= 64
|
||||
end
|
||||
if len > 0 then
|
||||
buffer.copy(blk, 0, buf, o, len)
|
||||
self.fill = len
|
||||
end
|
||||
end
|
||||
|
||||
local function digest(self: State): string
|
||||
local bits = self.total * 8
|
||||
local blk = self.block
|
||||
local f = self.fill
|
||||
writeu8(blk, f, 0x80)
|
||||
f += 1
|
||||
if f > 56 then
|
||||
buffer.fill(blk, f, 0, 64 - f)
|
||||
compress(self, blk, 0)
|
||||
f = 0
|
||||
end
|
||||
buffer.fill(blk, f, 0, 56 - f)
|
||||
writeu32(blk, 56, bits % 4294967296)
|
||||
writeu32(blk, 60, math.floor(bits / 4294967296) % 4294967296)
|
||||
compress(self, blk, 0)
|
||||
self.fill = 0
|
||||
local out = buffer.create(16)
|
||||
writeu32(out, 0, self.a)
|
||||
writeu32(out, 4, self.b)
|
||||
writeu32(out, 8, self.c)
|
||||
writeu32(out, 12, self.d)
|
||||
return buffer.tostring(out)
|
||||
end
|
||||
|
||||
function Md5.new(): State
|
||||
return {
|
||||
a = 0x67452301,
|
||||
b = 0xEFCDAB89,
|
||||
c = 0x98BADCFE,
|
||||
d = 0x10325476,
|
||||
block = buffer.create(64),
|
||||
fill = 0,
|
||||
total = 0,
|
||||
update = update,
|
||||
digest = digest,
|
||||
}
|
||||
end
|
||||
|
||||
-- hex(digest) -> 32 lower-case hex digits
|
||||
function Md5.hex(d: string): string
|
||||
local out = table.create(#d)
|
||||
for i = 1, #d do
|
||||
out[i] = string.format("%02x", string.byte(d, i))
|
||||
end
|
||||
return table.concat(out)
|
||||
end
|
||||
|
||||
-- of(buf, offset?, length?) -> hex digest
|
||||
function Md5.of(buf: buffer, offset: number?, length: number?): string
|
||||
local h = Md5.new()
|
||||
h:update(buf, offset, length)
|
||||
return Md5.hex(h:digest())
|
||||
end
|
||||
|
||||
return Md5
|
||||
Reference in new issue
Block a user