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

+11 -41
View File
@@ -1,13 +1,9 @@
--!strict
--!native
--!optimize 2
-- codecs/audio/WavPack/Block: one WavPack 4 / 5 block (one mono or stereo stream of a frame) -> samples. Ported from
-- WavPack 5.9.0 (David Bryant, BSD 3-clause, licenses/wavpack-LICENSE.txt): unpack.c (the decorrelation passes, joint
-- stereo, the hybrid lossy and hybrid lossless (+ wvc) paths with noise shaping and cross decorrelation, fixup_samples),
-- read_words.c (the median-adaptive entropy decoder: get_words_lossless, get_word, read_code), unpack_floats.c,
-- decorr_utils.c / entropy_utils.c (the metadata) and open_utils.c (metadata parsing, the block checksum). The block
-- CRC (and the wvx CRC) is checked. Not ported: DSD blocks (UNSUPPORTED), pre-4.0 streams, the lossy "mute" heuristics.
-- Never throws: returns nil, message.
-- codecs/audio/WavPack/Block: one WavPack 4 / 5 block (one mono or stereo stream of a frame) -> samples.
-- Ported from WavPack 5.9.0 (David Bryant, BSD 3-clause, licenses/wavpack-LICENSE.txt): unpack.c, read_words.c,
-- unpack_floats.c, decorr_utils.c / entropy_utils.c, open_utils.c. DSD blocks are UNSUPPORTED.
local Tables = require("@rosetta/codecs/audio/WavPack/Tables")
local readu8, readi8, readu16, readi16, readu32 = buffer.readu8, buffer.readi8, buffer.readu16, buffer.readi16, buffer.readu32
@@ -19,11 +15,10 @@ local sign, clamp = math.sign, math.clamp
local Block = {}
local TWO32 = 4294967296
local P2: { number } = table.create(34, 0) -- P2[k + 1] = 2^k
local P2: { number } = table.create(34, 0)
for i = 0, 33 do
P2[i + 1] = 2 ^ i
end
-- ONES[b + 1]: the number of trailing 1 bits of the byte b
local ONES: { number } = table.create(256, 0)
for b = 0, 255 do
local n, v = 0, b
@@ -34,18 +29,15 @@ for b = 0, 255 do
ONES[b + 1] = n
end
-- header flags
local MONO_FLAG, HYBRID, JOINT, CROSS, SHAPE, FLOAT, INT32 = 4, 8, 0x10, 0x20, 0x40, 0x80, 0x100
local HBITRATE, HBALANCE, NEW_SHAPING, FALSE_STEREO, DSD = 0x200, 0x400, 0x20000000, 0x40000000, 0x80000000
local FLOAT_SHIFT_ONES, FLOAT_SHIFT_SAME, FLOAT_SHIFT_SENT, FLOAT_ZEROS_SENT, FLOAT_NEG_ZEROS = 1, 2, 4, 8, 0x10
local PAD = 64 -- zero bytes after each copied bitstream: reads past the end stay in the buffer
local MAX_SAMPLES = 2 ^ 20 -- per block
local MAX_SAMPLES = 2 ^ 20
local D2, D1 = 17, 9 -- the first data index of the work table (stereo / mono): 8 frames of history in front
-- ------------------------------------------------------------------------------------------------ metadata
export type Meta = { [number]: number } -- id -> offset; id + 256 -> byte length
-- meta(buf, p, size) -> Meta | nil, message: the metadata sub-blocks of the block at p (header included in size)
function Block.meta(buf: buffer, p: number, size: number): (Meta?, string?)
local m: Meta = {}
local q, stop = p + 32, p + size
@@ -80,7 +72,6 @@ function Block.meta(buf: buffer, p: number, size: number): (Meta?, string?)
return m, nil
end
-- checksum(buf, p, size, m) -> true | false: the ID_BLOCK_CHECKSUM (when there is one) matches
function Block.checksum(buf: buffer, p: number, m: Meta): boolean
local o = m[0x2F]
if not o then
@@ -102,7 +93,6 @@ function Block.checksum(buf: buffer, p: number, m: Meta): boolean
return readu16(buf, o) == band(csum, 0xFFFF)
end
-- ------------------------------------------------------------------------------------------------ state
export type Reader = { b: buffer, p: number, e: number, n: number }
export type State = { [string]: any }
@@ -130,7 +120,6 @@ function Block.state(): State
}
end
-- open a bitstream: the bytes [o, o + len) copied with PAD zero bytes after them
local function openReader(R: Reader, buf: buffer, o: number, len: number)
if buffer.len(R.b) < len + PAD then
R.b = buffer.create(len + PAD + 1024)
@@ -143,8 +132,6 @@ local function openReader(R: Reader, buf: buffer, o: number, len: number)
R.n = len + PAD
end
-- ------------------------------------------------------------------------------------------------ bit reading
-- bits(R, n) -> the next n (<= 32) bits, LSB first
local function bits(R: Reader, n: number): number
if n <= 0 then
return 0
@@ -152,7 +139,7 @@ local function bits(R: Reader, n: number): number
local p = R.p
local byte = rshift(p, 3)
if byte + 8 > R.n then
R.p = R.e + 1 -- overrun: the caller's end check fails
R.p = R.e + 1
return 0
end
local b = R.b
@@ -170,8 +157,6 @@ local function bit(R: Reader): number
return bits(R, 1)
end
-- trailing(R, max) -> the number of 1 bits from the read position (up to max; a byte at a time), consumed with the 0
-- after them when fewer than max
local function trailing(R: Reader, max: number): number
local c = 0
while c < max do
@@ -193,7 +178,6 @@ local function trailing(R: Reader, max: number): number
c += 8
R.p = p + 8
end
-- (max reached inside a run of 8: give back the extra ones)
R.p -= c - max
return max
end
@@ -210,7 +194,7 @@ local function gamma(R: Reader): number?
return bits(R, c - 1) + P2[c]
end
-- readCode(R, maxcode) -> 0 .. maxcode (read_code)
-- read_code
local function readCode(R: Reader, maxcode: number): number
if maxcode < 2 then
return if maxcode == 1 then bits(R, 1) else 0
@@ -240,16 +224,13 @@ local function onesCount(R: Reader): number?
return c
end
-- ------------------------------------------------------------------------------------------------ lossless entropy
-- wordsLossless(st, n, stereo) -> true | nil: n samples (n * 2 when stereo) into st.W from the data index (the hot
-- loop of get_words_lossless, with the bit reader in locals)
-- get_words_lossless
local function wordsLossless(st: State, count: number, d: number): boolean
local R: Reader = st.R1
local b, pos, lim = R.b, R.p, R.e
local W = st.W
local med = st.med
local stereo = d == D2
-- (a0, a1, a2): the current channel's medians, (c0, c1, c2) the other's; swapped every sample when stereo
local a0, a1, a2, c0, c1, c2 = med[4], med[5], med[6], med[1], med[2], med[3]
if not stereo then
a0, a1, a2, c0, c1, c2 = med[1], med[2], med[3], 0, 0, 0
@@ -408,7 +389,6 @@ local function wordsLossless(st: State, count: number, d: number): boolean
return pos <= lim
end
-- ------------------------------------------------------------------------------------------------ hybrid entropy
local function updateErrorLimit(st: State, flags: number, mono: boolean)
local bacc, bdelta, slow, limit = st.bacc, st.bdelta, st.slow, st.limit
bacc[1] = (bacc[1] + bdelta[1]) % TWO32
@@ -448,7 +428,7 @@ local function updateErrorLimit(st: State, flags: number, mono: boolean)
end
end
-- getWord(st, chan, flags, mono) -> value, correction | nil (get_word; chan 0 / 1)
-- get_word
local function getWord(st: State, chan: number, flags: number, mono: boolean): (number?, number)
local R: Reader = st.R1
local med = st.med
@@ -564,7 +544,6 @@ local function getWord(st: State, chan: number, flags: number, mono: boolean): (
return if sign == 1 then -mid - 1 else mid, correction
end
-- ------------------------------------------------------------------------------------------------ decorrelation
-- the decorrelation passes over the whole block (stereo, from W[D2]): unpack.c decorr_stereo_pass
local function passesStereo(st: State, n: number)
local W = st.W
@@ -679,7 +658,6 @@ local function passesMono(st: State, n: number)
end
end
-- ------------------------------------------------------------------------------------------------ hybrid lossless
local function shaped(st: State, c: number, flags: number, correction: number): number
-- returns the value to subtract (temp) and updates the error state (noise shaping, unpack.c)
local sacc, sdelta, err = st.sacc, st.sdelta, st.err
@@ -872,7 +850,6 @@ local function hybridStereo(st: State, n: number, flags: number): boolean
return true
end
-- ------------------------------------------------------------------------------------------------ metadata -> state
local function readMeta(st: State, buf: buffer, m: Meta, flags: number, version: number): string?
local mono = band(flags, MONO_FLAG + FALSE_STEREO) ~= 0
local o = m[2]
@@ -1058,8 +1035,7 @@ local function readMeta(st: State, buf: buffer, m: Meta, flags: number, version:
return nil
end
-- ------------------------------------------------------------------------------------------------ fixup + output
-- the float conversion of one value (float_values; R = the wvx stream or nil)
-- float_values (R = the wvx stream or nil)
local function floatValue(st: State, v: number, R: Reader?): number
local fflags, exp = st.fflags, st.fmaxexp
local mant, e, sign = 0, 0, 0
@@ -1160,7 +1136,7 @@ local function floatValue(st: State, v: number, R: Reader?): number
return readf32(sc, 0)
end
-- emit: fixup_samples + false stereo + interleave into P (stride C, first channel c0 0-based)
-- fixup_samples + false stereo + interleave
local function emit(st: State, flags: number, n: number, d: number, P: { number }, c0: number, C: number, lossy: boolean)
local W = st.W
local s = if d == D2 then 2 else 1
@@ -1263,7 +1239,6 @@ local function emit(st: State, flags: number, n: number, d: number, P: { number
end
end
end
-- interleave
if s == 2 then
if c0 == 0 and C == 2 then
for k = 0, count - 1 do
@@ -1296,9 +1271,6 @@ local function emit(st: State, flags: number, n: number, d: number, P: { number
end
end
-- ------------------------------------------------------------------------------------------------ decode
-- decode(st, buf, p, size, P, c0, C, cbuf?, cp?, csize?) -> channels written (1 / 2) | nil, message: the block at p
-- (header included in size) into P (interleaved, stride C, from channel c0), with its correction block if given
function Block.decode(st: State, buf: buffer, p: number, size: number, P: { number }, c0: number, C: number, cbuf: buffer?, cp: number?, csize: number?): (number?, string?)
local version = readu16(buf, p + 8)
local n = readu32(buf, p + 20)
@@ -1332,7 +1304,6 @@ function Block.decode(st: State, buf: buffer, p: number, size: number, P: { numb
cm, cb, hasC = cmeta, cbuf, true
crcWant = readu32(cbuf, cp + 28)
end
-- reset
st.nterms = 0
local sA, sB, wA, wB = st.sA, st.sB, st.wA, st.wB
for k = 1, 128 do
@@ -1368,7 +1339,6 @@ function Block.decode(st: State, buf: buffer, p: number, size: number, P: { numb
if n == 0 then
return out, nil
end
-- bitstreams
local o = m[0xA]
if not o or m[0xA + 256] == 0 or m[0xA + 256] % 2 == 1 then
return nil, "no audio bitstream"