codecs/audio: comments to specs only

This commit is contained in:
4DBug committed 2026-11-01 01:55:39 -05:00
1 parent 0bd7b87d2b
commit 6dcce481bb
69 files changed
+523 -1604

No files matched your search

+7 -16
View File
@@ -1,10 +1,8 @@
--!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.
-- codecs/audio/Tta/Decode: one TTA1 frame -> samples, from the TTA1 format description; no libtta / ffmpeg code.
-- Bit-exact against the ttaenc 3.4.1 decoder (tools/fixtures/tta.py).
local Crc32 = require("@rosetta/core/Crc32")
local readu8, readu32 = buffer.readu8, buffer.readu32
@@ -16,11 +14,10 @@ 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
local P2: { number } = table.create(40, 0)
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
@@ -31,7 +28,6 @@ for b = 0, 255 do
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
@@ -48,9 +44,8 @@ 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
bytes: number,
S: buffer,
R: { number },
Q: { number },
X: { number },
@@ -75,8 +70,6 @@ function Decode.state(channels: number, bytes: number): State
}
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"
@@ -105,15 +98,14 @@ function Decode.frame(st: State, buf: buffer, o: number, len: number, n: number,
Q[c * 8 + i], X[c * 8 + i], L[c * 8 + i] = 0, 0, 0
end
end
local pos = 0 -- bit position
local pos = 0
local lim = dlen * 8
local c = 0 -- the channel of the sample at idx
local c = 0
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]
@@ -222,7 +214,6 @@ function Decode.frame(st: State, buf: buffer, o: number, len: number, n: number,
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
+5 -16
View File
@@ -1,15 +1,7 @@
--!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.
-- codecs/audio/Tta: the TTA1 (True Audio) lossless codec.
-- From the TTA1 format description (22-byte header and seek table with CRC-32s, frames of 256 / 245 seconds); no libtta /
-- ffmpeg code (LGPL). Format 2 (password) is UNSUPPORTED.
local Err = require("@rosetta/core/Err")
local Crc32 = require("@rosetta/core/Crc32")
local Decode = require("@rosetta/codecs/audio/Tta/Decode")
@@ -21,14 +13,13 @@ export type Info = {
channels: number,
bitsPerSample: number,
sampleRate: number,
samples: number, -- per channel
samples: number,
frameLength: number, -- samples per frame (256 * rate / 245)
frames: number,
offsets: { number }, -- each frame's byte offset
offsets: { number },
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")
@@ -86,7 +77,6 @@ function Tta.info(buf: buffer, at: number, limit: number): (Info?, Err.Err?)
return info, nil
end
-- ------------------------------------------------------------------------------------------------ decoder
export type Decoder = {
info: Info,
pcm: { number },
@@ -112,7 +102,6 @@ local function frame(self: Decoder, buf: buffer, i: number): (number?, Err.Err?)
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,