rename: src/Formats -> src/Rosetta, @formats -> @rosetta requires, build/luajit/rosetta + rosetta._compat, build/roblox/Rosetta, ROSETTA_* env/globals, writer vendor strings

This commit is contained in:
4DBug committed 2026-11-01 01:19:16 -05:00
1 parent 2956576d7f
commit 22ddcd78fa
941 files changed
+3364 -3364

No files matched your search

+259
View File
@@ -0,0 +1,259 @@
--!strict
--!native
--!optimize 2
-- codecs/audio/Tta/Decode: one TTA1 frame -> samples. Written from the TTA1 format description (the True Audio
-- format notes: adaptive Rice codes with two running sums per channel, the order-8 sign-LMS "hybrid" filter, the
-- first-order fixed predictor, inter-channel decorrelation, a CRC-32 per frame); no libtta / ffmpeg code. Checked
-- bit-exact against the ttaenc 3.4.1 decoder (tools/fixtures/tta.py). Never throws: returns nil, message.
local Crc32 = require("@rosetta/core/Crc32")
local readu8, readu32 = buffer.readu8, buffer.readu32
local band, rshift = bit32.band, bit32.rshift
local min, max, floor = math.min, math.max, math.floor
local Decode = {}
local TWO31, TWO32 = 2147483648, 4294967296
local INV31 = 1 / TWO31
local PAD = 16
local P2: { number } = table.create(40, 0) -- P2[k + 1] = 2^k
for i = 0, 39 do
P2[i + 1] = 2 ^ i
end
-- ONES[b + 1]: the trailing 1 bits of the byte b
local ONES: { number } = table.create(256, 0)
for b = 0, 255 do
local n, v = 0, b
while v % 2 == 1 do
n += 1
v //= 2
end
ONES[b + 1] = n
end
-- longUnary(S, pos, lim) -> the count of 1 bits from pos, the position after the 0 | nil (past lim)
local function longUnary(S: buffer, pos: number, lim: number): (number?, number)
local unary = 0
while pos <= lim do
local w = rshift(readu32(S, rshift(pos, 3)), band(pos, 7))
local ones = ONES[band(w, 255) + 1]
if ones < 8 then
return unary + ones, pos + ones + 1
end
unary += 8
pos += 8
end
return nil, pos
end
export type State = {
channels: number,
bytes: number, -- 1 .. 3
S: buffer, -- the frame's bits (+ PAD zeros)
-- per channel: rice k0, k1, sum0, sum1; filter error, Q (weights), X (steps), L (delay line) x 8; the last value
R: { number },
Q: { number },
X: { number },
L: { number },
E: { number },
last: { number },
cur: { number },
}
function Decode.state(channels: number, bytes: number): State
return {
channels = channels,
bytes = bytes,
S = buffer.create(4096),
R = table.create(channels * 4, 0),
Q = table.create(channels * 8, 0),
X = table.create(channels * 8, 0),
L = table.create(channels * 8, 0),
E = table.create(channels, 0),
last = table.create(channels, 0),
cur = table.create(channels, 0),
}
end
-- frame(st, buf, o, len, n, P) -> n | nil, message: the frame of len bytes at o (its CRC-32 last) holding n samples per
-- channel, into P (interleaved integers)
function Decode.frame(st: State, buf: buffer, o: number, len: number, n: number, P: { number }): (number?, string?)
if len < 4 then
return nil, "truncated frame"
end
local dlen = len - 4
if Crc32.of(buf, o, dlen) ~= readu32(buf, o + dlen) then
return nil, "checksum: frame CRC mismatch"
end
local S = st.S
if buffer.len(S) < dlen + PAD then
S = buffer.create(dlen + PAD + 4096)
st.S = S
end
buffer.copy(S, 0, buf, o, dlen)
buffer.fill(S, dlen, 0, PAD)
local C, bytes = st.channels, st.bytes
local shift = if bytes == 2 then 9 else 10 -- the filter's precision per sample size
local fdiv = 2 ^ shift
local round = fdiv // 2
local pk = if bytes == 1 then 16 else 32 -- the fixed predictor: last * (pk - 1) / pk
local R, Q, X, L, E, last, cur = st.R, st.Q, st.X, st.L, st.E, st.last, st.cur
for c = 0, C - 1 do
R[c * 4 + 1], R[c * 4 + 2], R[c * 4 + 3], R[c * 4 + 4] = 10, 10, 16384, 16384
E[c + 1], last[c + 1] = 0, 0
for i = 1, 8 do
Q[c * 8 + i], X[c * 8 + i], L[c * 8 + i] = 0, 0, 0
end
end
local pos = 0 -- bit position
local lim = dlen * 8
local c = 0 -- the channel of the sample at idx
for idx = 1, n * C do
do
if pos > lim then
return nil, "truncated frame"
end
-- the unary prefix: 1 bits up to a 0 (a run past one byte goes to longUnary)
local unary
local w = rshift(readu32(S, rshift(pos, 3)), band(pos, 7))
local ones = ONES[band(w, 255) + 1]
if ones < 8 then
unary = ones
pos += ones + 1
else
local u, p2 = longUnary(S, pos, lim)
if not u then
return nil, "truncated frame"
end
unary, pos = u, p2
end
local rb = c * 4
local k0, k1, s0, s1 = R[rb + 1], R[rb + 2], R[rb + 3], R[rb + 4]
local deep = unary > 0
local k
if deep then
k = k1
unary -= 1
else
k = k0
end
local value = unary
if k > 0 then
local bits
if k <= 24 then
bits = band(rshift(readu32(S, rshift(pos, 3)), band(pos, 7)), P2[k + 1] - 1)
else
local lo = band(rshift(readu32(S, rshift(pos, 3)), band(pos, 7)), 0xFFFF)
local p2 = pos + 16
bits = lo + band(rshift(readu32(S, rshift(p2, 3)), band(p2, 7)), P2[k - 15] - 1) * 65536
end
pos += k
value = unary * P2[k + 1] + bits
end
if deep then
s1 += value - s1 // 16
if k1 > 0 and s1 < P2[k1 + 5] then
k1 -= 1
elseif s1 > P2[k1 + 6] then
k1 += 1
end
value += P2[k0 + 1]
end
s0 += value - s0 // 16
if k0 > 0 and s0 < P2[k0 + 5] then
k0 -= 1
elseif s0 > P2[k0 + 6] then
k0 += 1
end
R[rb + 1] = math.min(k0, 31)
R[rb + 2] = math.min(k1, 31)
R[rb + 3] = s0
R[rb + 4] = s1
local odd = value % 2 -- odd -> (v + 1) / 2, even -> -v / 2 (branch-free)
value = (value + odd) // 2 * (2 * odd - 1)
-- the adaptive filter: the weights step toward the sign of the last residual
local fb = c * 8
local err = E[c + 1]
-- (one assignment per statement: LuaJIT cannot coalesce long multiple assignments)
local dq = max(-1, min(1, err))
local x1, x2, x3, x4 = X[fb + 1], X[fb + 2], X[fb + 3], X[fb + 4]
local x5, x6, x7, x8 = X[fb + 5], X[fb + 6], X[fb + 7], X[fb + 8]
local q1 = Q[fb + 1] + x1 * dq
local q2 = Q[fb + 2] + x2 * dq
local q3 = Q[fb + 3] + x3 * dq
local q4 = Q[fb + 4] + x4 * dq
local q5 = Q[fb + 5] + x5 * dq
local q6 = Q[fb + 6] + x6 * dq
local q7 = Q[fb + 7] + x7 * dq
local q8 = Q[fb + 8] + x8 * dq
Q[fb + 1] = q1
Q[fb + 2] = q2
Q[fb + 3] = q3
Q[fb + 4] = q4
Q[fb + 5] = q5
Q[fb + 6] = q6
Q[fb + 7] = q7
Q[fb + 8] = q8
local l2, l3, l4, l5 = L[fb + 2], L[fb + 3], L[fb + 4], L[fb + 5]
local l6, l7, l8 = L[fb + 6], L[fb + 7], L[fb + 8]
local sum = round + L[fb + 1] * q1 + l2 * q2 + l3 * q3 + l4 * q4 + l5 * q5 + l6 * q6 + l7 * q7 + l8 * q8
if sum >= TWO31 or sum < -TWO31 then
sum = (sum + TWO31) % TWO32 - TWO31
end
-- the steps follow the delay line's signs (x 1, 2, 2, 4)
X[fb + 1] = x2
X[fb + 2] = x3
X[fb + 3] = x4
X[fb + 4] = x5
X[fb + 5] = 1 + 2 * floor(l5 * INV31) -- -1 for a negative value, else 1 (branch-free)
X[fb + 6] = 2 + 4 * floor(l6 * INV31)
X[fb + 7] = 2 + 4 * floor(l7 * INV31)
X[fb + 8] = 4 + 8 * floor(l8 * INV31)
E[c + 1] = value
value += sum // fdiv
-- the delay line: the last value and its first and second differences
local n7 = value - l8
local n6 = n7 - l7
L[fb + 1] = l2
L[fb + 2] = l3
L[fb + 3] = l4
L[fb + 4] = l5
L[fb + 5] = n6 - l6
L[fb + 6] = n6
L[fb + 7] = n7
L[fb + 8] = value
-- the fixed predictor
value += (last[c + 1] * (pk - 1)) // pk
last[c + 1] = value
cur[c + 1] = value
end
c += 1
if c == C then
-- inter-channel decorrelation: the last channel carries half the previous one, the others are differences
c = 0
local o = idx - C
if C == 1 then
P[idx] = cur[1]
elseif C == 2 then
local a = cur[1]
local b = cur[2] + (if a >= 0 then a // 2 else -((-a) // 2))
P[o + 1], P[o + 2] = b - a, b
else
local a = cur[C - 1]
cur[C] += if a >= 0 then a // 2 else -((-a) // 2)
for r = C - 1, 1, -1 do
cur[r] = cur[r + 1] - cur[r]
end
for k = 1, C do
P[o + k] = cur[k]
end
end
end
end
if pos > lim then
return nil, "truncated frame"
end
return n, nil
end
return Decode
+126
View File
@@ -0,0 +1,126 @@
--!strict
-- codecs/audio/Tta: the TTA1 (True Audio) lossless codec. A codec (layer 3). Written from the TTA1 format description
-- (the 22-byte header with its CRC-32, the seek table of frame sizes with its CRC-32, frames of 256 / 245 seconds);
-- no libtta / ffmpeg code (those are LGPL). The frame decoder is codecs/audio/Tta/Decode. Supports format 1 (plain
-- PCM), 8 / 16 / 24 bits, any channel count, the header / seek table / frame CRCs. Not supported: format 2
-- (password-protected: UNSUPPORTED), TTA2.
--
-- local info, err = Tta.info(buf, at, limit) -- the header at `at` (after any ID3v2 tag) + the frame table
-- local dec = Tta.decoder(info)
-- local n, err = dec:frame(buf, i) -- frame i (1-based) -> dec.pcm (interleaved integers)
--
-- Never throws: bad data returns nil, Err.
local Err = require("@rosetta/core/Err")
local Crc32 = require("@rosetta/core/Crc32")
local Decode = require("@rosetta/codecs/audio/Tta/Decode")
local Tta = {}
export type Info = {
format: number,
channels: number,
bitsPerSample: number,
sampleRate: number,
samples: number, -- per channel
frameLength: number, -- samples per frame (256 * rate / 245)
frames: number,
offsets: { number }, -- each frame's byte offset
sizes: { number },
}
-- info(buf, at, limit) -> Info | nil, Err: the TTA1 header at byte `at`
function Tta.info(buf: buffer, at: number, limit: number): (Info?, Err.Err?)
if at + 22 > limit then
return nil, Err.new(Err.TRUNCATED, "truncated header")
end
if buffer.readstring(buf, at, 4) ~= "TTA1" then
return nil, Err.new(Err.CORRUPT, "no TTA1 header")
end
if Crc32.of(buf, at, 18) ~= buffer.readu32(buf, at + 18) then
return nil, Err.new(Err.CHECKSUM, "header CRC mismatch", at)
end
local format = buffer.readu16(buf, at + 4)
local info: Info = {
format = format,
channels = buffer.readu16(buf, at + 6),
bitsPerSample = buffer.readu16(buf, at + 8),
sampleRate = buffer.readu32(buf, at + 10),
samples = buffer.readu32(buf, at + 14),
frameLength = 0,
frames = 0,
offsets = {},
sizes = {},
}
if format == 2 then
return nil, Err.new(Err.UNSUPPORTED, "password-protected TTA")
elseif format ~= 1 then
return nil, Err.new(Err.UNSUPPORTED, "TTA format " .. format)
end
local bits = info.bitsPerSample
if bits ~= 8 and bits ~= 16 and bits ~= 24 then
return nil, Err.new(Err.UNSUPPORTED, bits .. "-bit TTA")
end
if info.channels < 1 or info.sampleRate < 1 then
return nil, Err.new(Err.CORRUPT, "bad header")
end
info.frameLength = (256 * info.sampleRate) // 245
if info.frameLength < 1 then
return nil, Err.new(Err.CORRUPT, "bad sample rate")
end
local frames = (info.samples + info.frameLength - 1) // info.frameLength
info.frames = frames
local st = at + 22
if st + frames * 4 + 4 > limit then
return nil, Err.new(Err.TRUNCATED, "truncated seek table")
end
if Crc32.of(buf, st, frames * 4) ~= buffer.readu32(buf, st + frames * 4) then
return nil, Err.new(Err.CHECKSUM, "seek table CRC mismatch", st)
end
local o = st + frames * 4 + 4
for i = 1, frames do
local size = buffer.readu32(buf, st + (i - 1) * 4)
info.offsets[i] = o
info.sizes[i] = size
o += size
end
return info, nil
end
-- ------------------------------------------------------------------------------------------------ decoder
export type Decoder = {
info: Info,
pcm: { number },
limit: number,
frame: (self: Decoder, buf: buffer, i: number) -> (number?, Err.Err?),
state: Decode.State,
}
local function frame(self: Decoder, buf: buffer, i: number): (number?, Err.Err?)
local info = self.info
if i < 1 or i > info.frames then
return nil, Err.new(Err.ARG, "no frame " .. i)
end
local o, size = info.offsets[i], info.sizes[i]
if o + size > self.limit then
return nil, Err.new(Err.TRUNCATED, "frame " .. i .. " runs past the end", o)
end
local n = if i < info.frames then info.frameLength else info.samples - (info.frames - 1) * info.frameLength
local got, msg = Decode.frame(self.state, buf, o, size, n, self.pcm)
if not got then
return nil, Err.from(msg, o)
end
return got, nil
end
-- decoder(info, limit) -> Decoder (limit: the end of the file data)
function Tta.decoder(info: Info, limit: number): Decoder
return {
info = info,
pcm = table.create(info.frameLength * info.channels, 0),
limit = limit,
frame = frame,
state = Decode.state(info.channels, info.bitsPerSample // 8),
}
end
return Tta