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

+12 -24
View File
@@ -1,14 +1,9 @@
--!strict
--!native
--!optimize 2
-- codecs/audio/Vorbis/Codebook: the Vorbis LSB-first packet bit reader and codebooks (Vorbis I spec sections 2 and 3):
-- reading a codebook from the setup header (ordered / sparse length lists, lookup types 1 and 2, float32_unpack),
-- the codeword assignment, and Huffman / VQ decoding. Ported from stb_vorbis 1.22 (public domain / MIT; see
-- licenses/stb_vorbis-LICENSE.txt): compute_codewords' lowest-free-leaf assignment, lookup1_values; the decode side
-- is a fast table (up to 10 bits) plus a binary tree for longer codes instead of stb's sorted binary search.
--
-- Never throws. Reading past the packet end gives zero bits and sets br.eop (the spec's end-of-packet condition);
-- an invalid codeword also sets eop and decodes to -1.
-- codecs/audio/Vorbis/Codebook: the Vorbis LSB-first bit reader and codebooks (Vorbis I spec sections 2 and 3).
-- Ported from stb_vorbis 1.22 (public domain / MIT, licenses/stb_vorbis-LICENSE.txt). Reading past the packet end gives
-- zero bits and sets eop (the spec's end-of-packet condition); an invalid codeword also sets eop and decodes to -1.
local Codebook = {}
local readu8 = buffer.readu8
@@ -19,22 +14,20 @@ for i = 0, 40 do
POW[i + 1] = 2 ^ i
end
-- ------------------------------------------------------------------------------------------------ bit reader
export type Br = {
buf: buffer,
pos: number, -- next byte to load
pos: number,
limit: number,
acc: number, -- loaded, unread bits (LSB first)
acc: number,
cnt: number,
pad: number, -- how many of the top bits of acc are past the end of the packet
eop: boolean, -- read past the end of the packet
pad: number,
eop: boolean,
}
function Codebook.reader(buf: buffer, offset: number, size: number): Br
return { buf = buf, pos = offset, limit = offset + size, acc = 0, cnt = 0, pad = 0, eop = false }
end
-- bits(br, n) -> the next n (0..32) bits as an unsigned number
local function bits(br: Br, n: number): number
if n == 0 then
return 0
@@ -66,7 +59,7 @@ local function bits(br: Br, n: number): number
end
Codebook.bits = bits
-- ilog(x): the number of bits needed to hold x (0 for x <= 0)
-- ilog (spec 9.2.1)
local function ilog(x: number): number
local n = 0
while x > 0 do
@@ -86,7 +79,6 @@ local function float32Unpack(x: number): number
return if sign then -v else v
end
-- ------------------------------------------------------------------------------------------------ codebooks
export type Book = {
dims: number,
entries: number,
@@ -95,9 +87,9 @@ export type Book = {
fastSym: { number }, -- symbol, or the tree node to continue from
c0: { number }, -- tree: child for bit 0 / 1: > 0 node, < 0 leaf (-symbol - 1), 0 none
c1: { number },
lookup: number, -- 0, 1, 2
vq: { number }?, -- entries * dims values (lookup 1 / 2, expanded)
used: number, -- entries with a codeword
lookup: number,
vq: { number }?,
used: number,
}
local MAX_ENTRIES = 2 ^ 20
@@ -152,7 +144,6 @@ local function build(book: Book, lengths: { number }): string?
available[y + 1] = code + POW[33 - y] + 1
end
end
-- walk the MSB-first code into the tree
local node = 1
for d = 1, len do
local bit = floor(code / POW[33 - d]) % 2
@@ -189,7 +180,6 @@ local function build(book: Book, lengths: { number }): string?
end
end
book.used = used
-- the fast table: every fastBits-bit prefix (read LSB first) walked down the tree
local fb = math.clamp(maxLen, 1, 10)
book.fastBits = fb
local size = POW[fb + 1]
@@ -205,7 +195,7 @@ local function build(book: Book, lengths: { number }): string?
fl[idx + 1], fs[idx + 1] = d, -child - 1
break
elseif child == 0 then
break -- invalid (-1)
break
end
node = child
if d == fb then
@@ -217,7 +207,6 @@ local function build(book: Book, lengths: { number }): string?
return nil
end
-- read(br) -> Book | nil, message: one codebook from the setup header
function Codebook.read(br: Br): (Book?, string?)
if bits(br, 24) ~= 0x564342 then
return nil, "bad codebook sync"
@@ -337,7 +326,6 @@ function Codebook.read(br: Br): (Book?, string?)
return book, nil
end
-- decode(br, book) -> entry number | -1 (end of packet / invalid codeword: br.eop is set)
local function decode(br: Br, book: Book): number
local fb = book.fastBits
local acc, cnt = br.acc, br.cnt
+11 -33
View File
@@ -1,18 +1,10 @@
--!strict
--!native
--!optimize 2
-- codecs/audio/Vorbis/Packet: Vorbis audio packet decoding (Vorbis I spec sections 4.3, 7, 8): mode and window shape, floor 1
-- curves, residue types 0 / 1 / 2, inverse channel coupling, the IMDCT (codecs/audio/Mdct), windowing and overlap-add.
-- Floors, residues and coupling are ported from stb_vorbis 1.22 vorbis_decode_packet_rest / decode_residue /
-- do_floor / draw_line (public domain / MIT; see licenses/stb_vorbis-LICENSE.txt). The overlap follows libvorbis:
-- each packet after the first returns the n_prev/4 + n_cur/4 samples between the centres of the two windows, so
-- granule positions count exactly the samples returned (the first packet returns none).
--
-- local st = Packet.new(ident, setup)
-- local n = Packet.decode(st, buf, offset, size) -- frames now in st.out[ch][1 .. n]; -1 = not an audio packet
-- Packet.reset(st) -- forget the overlap (after a seek / a gap)
-- local bs = Packet.blocksize(st, buf, offset, size) -- the packet's block size (0 = not audio), header bits only
-- Never throws: a damaged packet decodes as far as its bits go (the spec's end-of-packet rule) or returns -1.
-- codecs/audio/Vorbis/Packet: Vorbis audio packet decoding (Vorbis I spec sections 4.3, 7, 8).
-- Floors, residues and coupling ported from stb_vorbis 1.22 (public domain / MIT, licenses/stb_vorbis-LICENSE.txt). The
-- overlap follows libvorbis: each packet after the first returns the n_prev/4 + n_cur/4 samples between the two window
-- centres, so granule positions count exactly the samples returned (the first packet returns none).
local Codebook = require("@rosetta/codecs/audio/Vorbis/Codebook")
local Setup = require("@rosetta/codecs/audio/Vorbis/Setup")
local Tables = require("@rosetta/codecs/audio/Vorbis/Tables")
@@ -31,16 +23,16 @@ export type State = {
n1: number,
setup: Setup.Setup,
plans: { [number]: Mdct.Plan },
slopes: { [number]: { number } }, -- rising half-window of overlap length L (= blocksize / 2)
spec: { { number } }, -- per channel: n/2 spectral values (residue, then times the floor)
frame: { { number } }, -- per channel: n windowed time samples
tail: { { number } }, -- per channel: the previous frame from its centre on
slopes: { [number]: { number } },
spec: { { number } },
frame: { { number } },
tail: { { number } },
tailLen: number,
prevN: number, -- the previous block size, 0 = no previous frame
out: { { number } }, -- per channel: the returned samples
prevN: number,
out: { { number } },
finalY: { { number } },
step2: { number },
inter: { number }, -- residue type 2 interleaved scratch
inter: { number },
zero: { boolean },
reallyZero: { boolean },
partClass: { { { number } } },
@@ -101,7 +93,6 @@ function Packet.reset(st: State)
st.tailLen = 0
end
-- blocksize(st, buf, off, size) -> the block size of an audio packet (0 if not audio)
function Packet.blocksize(st: State, b: buffer, off: number, size: number): number
if size < 1 then
return 0
@@ -118,7 +109,6 @@ function Packet.blocksize(st: State, b: buffer, off: number, size: number): numb
return if mode.blockflag == 1 then st.n1 else st.n0
end
-- ------------------------------------------------------------------------------------------------ floor 1
local function predict(x: number, x0: number, x1: number, y0: number, y1: number): number
local dy = y1 - y0
local adx = x1 - x0
@@ -155,7 +145,6 @@ local function drawLine(out: { number }, x0: number, y0: number, x1: number, y1:
end
end
-- decode the floor of one channel into finalY (-1 = unused point); false = this channel is silent
local function floorDecode(st: State, br: Codebook.Br, g: Setup.Floor1, finalY: { number }): boolean
if bits(br, 1) == 0 then
return false
@@ -259,8 +248,6 @@ local function floorCurve(g: Setup.Floor1, finalY: { number }, target: { number
end
end
-- ------------------------------------------------------------------------------------------------ residue
-- add the vectors of one partition to target at offset (0-based) .. offset + psize (bounded by limit)
local function partition(br: Codebook.Br, book: Codebook.Book, target: { number }, offset: number, psize: number, rtype: number, limit: number): boolean
local vq = book.vq
if not vq then
@@ -304,7 +291,6 @@ local function partition(br: Codebook.Br, book: Codebook.Book, target: { number
return true
end
-- decode residue r for the channels `targets` (nil = do not decode) of length n2 each
local function residue(st: State, br: Codebook.Br, r: Setup.Residue, targets: { { number } | false }, ch: number, n2: number)
local books = st.setup.books
local rtype = r.type
@@ -374,7 +360,6 @@ local function residue(st: State, br: Codebook.Br, r: Setup.Residue, targets: {
classSet += 1
end
end
-- de-interleave into the channels that are decoded
for j = 1, ch do
local t = targets[j]
if t then
@@ -428,8 +413,6 @@ local function residue(st: State, br: Codebook.Br, r: Setup.Residue, targets: {
end
end
-- ------------------------------------------------------------------------------------------------ packet
-- decode(st, buf, off, size) -> frames returned in st.out (0 for the first packet), -1 for a non-audio / bad packet
function Packet.decode(st: State, b: buffer, off: number, size: number): number
if size < 1 then
return -1
@@ -471,7 +454,6 @@ function Packet.decode(st: State, b: buffer, off: number, size: number): number
local map = setup.mappings[mode.mapping + 1]
local ch = st.channels
local spec, zero, reallyZero = st.spec, st.zero, st.reallyZero
-- floors
for c = 1, ch do
local s = map.mux[c] + 1
local g = setup.floors[map.submapFloor[s] + 1]
@@ -486,7 +468,6 @@ function Packet.decode(st: State, b: buffer, off: number, size: number): number
zero[m], zero[a] = false, false
end
end
-- residues, per submap
for i = 1, map.submaps do
local targets: { { number } | false } = {}
local k = 0
@@ -498,7 +479,6 @@ function Packet.decode(st: State, b: buffer, off: number, size: number): number
end
residue(st, br, setup.residues[map.submapResidue[i] + 1], targets, k, n2)
end
-- inverse coupling
for i = map.couplingSteps, 1, -1 do
local m, a = spec[map.magnitude[i] + 1], spec[map.angle[i] + 1]
for j = 1, n2 do
@@ -520,7 +500,6 @@ function Packet.decode(st: State, b: buffer, off: number, size: number): number
m[j], a[j] = m2, a2
end
end
-- floor curves, IMDCT, windows
local plan = st.plans[n]
local lslope = st.slopes[leftEnd - leftStart]
local rslope = st.slopes[rightEnd - rightStart]
@@ -570,7 +549,6 @@ function Packet.decode(st: State, b: buffer, off: number, size: number): number
end
end
end
-- the new tail: this frame from its centre to the end of its window
local tl = rightEnd - n2
for c = 1, ch do
local tail, fr = st.tail[c], st.frame[c]
+13 -23
View File
@@ -1,10 +1,8 @@
--!strict
--!native
-- codecs/audio/Vorbis/Setup: the three Vorbis header packets (Vorbis I spec section 4.2): identification (channels, rate,
-- bitrates, blocksizes), comment (vendor + user comments -> tags) and setup (codebooks, floors, residues, mappings,
-- modes). Ported from stb_vorbis 1.22 start_decoder (public domain / MIT; see licenses/stb_vorbis-LICENSE.txt).
-- Floor 0 is rejected as unsupported, as stb_vorbis does (no encoder has produced it since 2000).
-- Never throws: every parser returns nil, message on bad data.
-- codecs/audio/Vorbis/Setup: the three Vorbis header packets (Vorbis I spec section 4.2).
-- Ported from stb_vorbis 1.22 start_decoder (public domain / MIT, licenses/stb_vorbis-LICENSE.txt). Floor 0 is
-- rejected as unsupported, as stb_vorbis does.
local Codebook = require("@rosetta/codecs/audio/Vorbis/Codebook")
local Setup = {}
@@ -24,16 +22,16 @@ export type Ident = {
export type Floor1 = {
partitions: number,
partitionClass: { number }, -- 1-based list of class numbers (0-based values)
classDims: { number }, -- [class + 1]
partitionClass: { number },
classDims: { number },
classSubs: { number },
classMaster: { number },
subBooks: { { number } }, -- [class + 1][sub + 1] = book (0-based) or -1
subBooks: { { number } },
multiplier: number,
xlist: { number }, -- [j + 1]
xlist: { number },
values: number,
sorted: { number }, -- point indices (0-based) by increasing x
low: { number }, -- neighbours, [j + 1] (0-based indices)
sorted: { number },
low: { number },
high: { number },
}
@@ -44,16 +42,16 @@ export type Residue = {
partSize: number,
classifications: number,
classbook: number,
books: { { number } }, -- [class + 1][pass + 1] = book or -1
classdata: { { number } }, -- [classbook entry + 1][k + 1] = class
books: { { number } },
classdata: { { number } },
}
export type Mapping = {
submaps: number,
couplingSteps: number,
magnitude: { number }, -- [step + 1] channel (0-based)
magnitude: { number },
angle: { number },
mux: { number }, -- [channel + 1] submap (0-based)
mux: { number },
submapFloor: { number },
submapResidue: { number },
}
@@ -75,7 +73,6 @@ local function isVorbis(b: buffer, o: number, n: number, kind: number): boolean
end
Setup.isVorbis = isVorbis
-- ident(buf, offset, size) -> Ident | nil, message
function Setup.ident(b: buffer, o: number, n: number): (Ident?, string?)
if not isVorbis(b, o, n, 1) then
return nil, "not a Vorbis identification header"
@@ -111,7 +108,6 @@ function Setup.ident(b: buffer, o: number, n: number): (Ident?, string?)
nil
end
-- comment(buf, offset, size) -> vendor, { "KEY=value" } | nil, message
function Setup.comment(b: buffer, o: number, n: number): (string?, { string }?)
if not isVorbis(b, o, n, 3) then
return nil, nil
@@ -148,14 +144,12 @@ function Setup.comment(b: buffer, o: number, n: number): (string?, { string }?)
return vendor, list
end
-- setup(buf, offset, size, ident) -> Setup | nil, message
function Setup.setup(b: buffer, o: number, n: number, id: Ident): (Setup?, string?)
if not isVorbis(b, o, n, 5) then
return nil, "not a Vorbis setup header"
end
local br = Codebook.reader(b, o + 7, n - 7)
local channels = id.channels
-- codebooks
local books: { Codebook.Book } = {}
local nbooks = bits(br, 8) + 1
for i = 1, nbooks do
@@ -172,7 +166,6 @@ function Setup.setup(b: buffer, o: number, n: number, id: Ident): (Setup?, strin
return nil, "bad time domain transform"
end
end
-- floors
local floors: { Floor1 } = {}
local maxValues = 0
local nfloors = bits(br, 6) + 1
@@ -272,7 +265,6 @@ function Setup.setup(b: buffer, o: number, n: number, id: Ident): (Setup?, strin
maxValues = values
end
end
-- residues
local residues: { Residue } = {}
local nres = bits(br, 6) + 1
for i = 1, nres do
@@ -341,7 +333,6 @@ function Setup.setup(b: buffer, o: number, n: number, id: Ident): (Setup?, strin
classdata = classdata,
}
end
-- mappings
local mappings: { Mapping } = {}
local nmaps = bits(br, 6) + 1
local chBits = ilog(channels - 1)
@@ -396,7 +387,6 @@ function Setup.setup(b: buffer, o: number, n: number, id: Ident): (Setup?, strin
submapResidue = sr,
}
end
-- modes
local modes: { Mode } = {}
local nmodes = bits(br, 6) + 1
for i = 1, nmodes do
+2 -2
View File
@@ -1,6 +1,6 @@
--!strict
-- codecs/audio/Vorbis/Tables: the floor 1 inverse dB table (Vorbis I specification, section 10.1, as in stb_vorbis 1.22,
-- public domain / MIT, licenses/stb_vorbis-LICENSE.txt). 256 values, index = floor value (1-based here).
-- codecs/audio/Vorbis/Tables: the floor 1 inverse dB table (Vorbis I specification section 10.1, as in stb_vorbis 1.22,
-- public domain / MIT, licenses/stb_vorbis-LICENSE.txt).
return table.freeze({
1.0649863e-07, 1.1341951e-07, 1.2079015e-07, 1.2863978e-07, 1.3699951e-07, 1.4590251e-07, 1.5538408e-07, 1.6548181e-07,
1.7623575e-07, 1.8768855e-07, 1.9988561e-07, 2.1287530e-07, 2.2670913e-07, 2.4144197e-07, 2.5713223e-07, 2.7384213e-07,
+2 -4
View File
@@ -1,8 +1,6 @@
--!strict
-- codecs/audio/Vorbis: the Vorbis I decoder without its container: identification / comment / setup headers (Setup),
-- codebooks (Codebook), audio packets to PCM (Packet: floors 0 / 1, residues 0 / 1 / 2, coupling, the IMDCT through
-- codecs/audio/Mdct) and the shared tables. A Luau port of stb_vorbis 1.22 (public domain / MIT,
-- licenses/stb_vorbis-LICENSE.txt). Ogg Vorbis (pages, granules, seeking) and the encoder are audio/vorbis.
-- codecs/audio/Vorbis: the Vorbis I decoder without its container.
-- A Luau port of stb_vorbis 1.22 (public domain / MIT, licenses/stb_vorbis-LICENSE.txt).
local Vorbis = {
Setup = require("@rosetta/codecs/audio/Vorbis/Setup"),
Codebook = require("@rosetta/codecs/audio/Vorbis/Codebook"),