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

+14 -34
View File
@@ -1,13 +1,9 @@
--!strict
--!native
--!optimize 2
-- codecs/audio/Musepack/Decode: one Musepack SV7 / SV8 frame (1152 samples per channel): the bitstream (band
-- resolutions, scalefactor indices, quantised subband samples, with SV8's canonical Huffman / enumerative codes),
-- requantisation (M/S or L/R) and the 32-band polyphase synthesis. A line-by-line port of libmpcdec's mpc_decoder.c,
-- requant.c and synth_filter.c (musepack_src_r475, BSD-3-Clause, (c) The Musepack Development Team,
-- licenses/libmpcdec-BSD.txt) in double precision float. The bit reader is module state (decoding is synchronous) over
-- a buffer the caller pads by at least PAD bytes past `limit`: the band loops stop as soon as the position passes the
-- limit, so every read stays inside the buffer. No error / assert: failures return nil, "message".
-- codecs/audio/Musepack/Decode: one Musepack SV7 / SV8 frame (1152 samples per channel).
-- A line-by-line port of libmpcdec's mpc_decoder.c, requant.c and synth_filter.c (musepack_src_r475, BSD-3-Clause, (c) The
-- Musepack Development Team, licenses/libmpcdec-BSD.txt). The caller pads the buffer by PAD bytes past `limit`.
local T = require("@rosetta/codecs/audio/Musepack/Tables")
local readu32, bswap, extract, band, bor, rshift, lshift, bnot, bxor =
@@ -19,7 +15,6 @@ Decode.PAD = 8192
Decode.DELAY = 481 -- MPC_DECODER_SYNTH_DELAY
Decode.FRAME = 1152
-- ------------------------------------------------------------------------------------------------ tables
local L = T.lut
local HDR, SCFI7, DSCF7 = L(T.HuffHdr), L(T.HuffSCFI), L(T.HuffDSCF)
local Q7: { { T.Lut } } = {
@@ -83,9 +78,8 @@ for i, v in T.DI do
DW[i] = f32(v / 65536)
end
-- ------------------------------------------------------------------------------------------------ bit reader
local B: buffer = buffer.create(0)
local P = 0 -- bit position
local P = 0
local function bits(n: number): number
if n <= 0 then
@@ -106,7 +100,6 @@ local function peek16(): number
return extract(w, 16 - band(P, 7), 16)
end
-- a table-driven Huffman symbol (plain or canonical)
local function huff(t: T.Lut): number
local code = peek16()
local k = rshift(code, 8) + 1
@@ -176,7 +169,6 @@ for i = 0, 255 do
PARITY[i + 1] = p
end
-- ------------------------------------------------------------------------------------------------ synthesis
-- mpc_compute_new_V: 32 subband samples Y[y .. y + 31] -> 64 V values at V[o .. o + 63] (the ISO 11172-3 matrixing
-- through Byeong Gi Lee's fast DCT)
local function newV(Y: { number }, y: number, V: { number }, o: number)
@@ -392,26 +384,25 @@ local function newV(Y: { number }, y: number, V: { number }, o: number)
V[o + 96 - i] = V[o + i]
end
end
-- ------------------------------------------------------------------------------------------------ state
export type State = {
version: number, -- 7 | 8
maxBand: number, -- the highest band index (0 .. 31)
version: number,
maxBand: number,
ms: boolean,
channels: number,
resL: { number }, -- [band + 1]
resL: { number },
resR: { number },
scfL: { number }, -- [band * 3 + m + 1]
scfL: { number },
scfR: { number },
scfiL: { number },
scfiR: { number },
dscfL: { boolean },
dscfR: { boolean },
msf: { boolean },
qL: { number }, -- [band * 36 + k + 1]
qL: { number },
qR: { number },
yL: { number }, -- [n * 32 + band + 1], n = 0 .. 35
yL: { number },
yR: { number },
vL: { number }, -- synthesis history, 2304 + 960
vL: { number },
vR: { number },
r1: number,
r2: number,
@@ -446,9 +437,7 @@ function Decode.state(version: number, maxBand: number, ms: boolean, channels: n
return st
end
-- reset(st, scf): forget the history (before a seek); scf = the scalefactor indices to assume (libmpcdec: 0 at the
-- start of the stream, 0x01010101 in the middle of an SV7 stream). The noise generator restarts too, so a seek
-- decodes like libmpcdec seeking in a freshly opened stream.
-- libmpcdec assumes scf 0 at the stream start, 0x01010101 mid-SV7; the noise generator restarts, as in a fresh stream
function Decode.reset(st: State, scf: number)
for i = 1, 99 do
st.scfL[i] = scf
@@ -479,8 +468,7 @@ local function noise(st: State, q: { number }, o: number)
st.r1, st.r2 = r1, r2
end
-- ------------------------------------------------------------------------------------------------ SV7
-- mpc_decoder_read_bitstream_sv7 -> true | nil, message
-- mpc_decoder_read_bitstream_sv7
local function readSv7(st: State, limit: number): (boolean?, string?)
local resL, resR, msf = st.resL, st.resR, st.msf
local maxUsed = 0
@@ -607,7 +595,6 @@ local function readSv7(st: State, limit: number): (boolean?, string?)
return true, nil
end
-- ------------------------------------------------------------------------------------------------ SV8
local function readSv8(st: State, key: boolean, limit: number): (boolean?, string?)
local resL, resR, msf = st.resL, st.resR, st.msf
local maxBand = st.maxBand
@@ -802,7 +789,6 @@ local function readSv8(st: State, key: boolean, limit: number): (boolean?, strin
return true, nil
end
-- ------------------------------------------------------------------------------------------------ requantisation
local function requant(st: State)
local resL, resR, msf = st.resL, st.resR, st.msf
local scfL, scfR, qL, qR, yL, yR = st.scfL, st.scfR, st.qL, st.qR, st.yL, st.yR
@@ -833,7 +819,7 @@ local function requant(st: State)
end
-- mpc_synthese_filter_float for one channel: 36 x 32 subband samples -> 1152 samples at out[base + i * step]
-- mpc_synthese_filter_float
local function synth(V: { number }, Y: { number }, out: { number }, base: number, step: number)
table.move(V, 1, 960, 2305)
local o = 2304 + 1
@@ -854,10 +840,6 @@ local function synth(V: { number }, Y: { number }, out: { number }, base: number
end
end
-- ------------------------------------------------------------------------------------------------ frames
-- frame(st, buf, bitPos, limitBits, key, out, base) -> the bit position after the frame | nil, message: decodes one
-- frame from buf (padded by PAD bytes past limitBits / 8) into out[base ..] (1152 * channels interleaved floats).
-- key: an SV8 key frame (the first of a block). bitPos < 0 decodes a silent frame (the synthesis tail).
function Decode.frame(st: State, buf: buffer, bitPos: number, limitBits: number, key: boolean, out: { number }, base: number): (number?, string?)
if bitPos >= 0 then
if rshift(limitBits, 3) + Decode.PAD > buffer.len(buf) or bitPos > limitBits then
@@ -889,8 +871,6 @@ function Decode.frame(st: State, buf: buffer, bitPos: number, limitBits: number,
return if bitPos >= 0 then pos else bitPos, nil
end
-- bitsAt(buf, bitPos, n) -> n (1 .. 24) bits at bitPos (frame headers: the SV7 frame size); the caller keeps
-- bitPos / 8 + 4 inside buf
function Decode.bitsAt(buf: buffer, bitPos: number, n: number): number
local w = bswap(readu32(buf, rshift(bitPos, 3)))
return extract(w, 32 - band(bitPos, 7) - n, n)