ws-cleanup: audio codecs out of the format folders (codecs/audio/Opus, Vorbis, Flac, Mpa)
The decoders / encoders are libraries, not the file format: audio/opus/{Celt, CeltTables, CeltEncoder, Mdct, Packet,
Silk, SilkTables, Decoder} -> codecs/audio/Opus; audio/vorbis/{Codebook, Packet, Setup, Tables} ->
codecs/audio/Vorbis; audio/flac/{Frame, Encode} -> codecs/audio/Flac; audio/mp3/{Core, Tables, Header, Encode2,
Encode3, EncTables, Analysis} -> codecs/audio/Mpa (tools/move.py; code unchanged). Each gets an index init.luau.
The format folders keep the file mapping: Ogg Opus / Ogg Vorbis (headers, granules, seeking, the Vorbis encoder's
Ogg writer), native / Ogg FLAC streams and metadata, ID3 / Xing / the MP3 frame index. docs: STYLE says a codec is
not the format. Full suite: 248 runs pass on both runtimes, lint green.
This commit is contained in:
1 parent
f5cf02026d
commit
9941113b7b
39 files changed
+146
-82
No files matched your search
@@ -0,0 +1,429 @@
|
||||
--!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.
|
||||
local Codebook = require("@formats/codecs/audio/Vorbis/Codebook")
|
||||
|
||||
local Setup = {}
|
||||
|
||||
local bits, ilog = Codebook.bits, Codebook.ilog
|
||||
local readu8, readu32 = buffer.readu8, buffer.readu32
|
||||
|
||||
export type Ident = {
|
||||
channels: number,
|
||||
sampleRate: number,
|
||||
bitrateMax: number,
|
||||
bitrateNominal: number,
|
||||
bitrateMin: number,
|
||||
blocksize0: number,
|
||||
blocksize1: number,
|
||||
}
|
||||
|
||||
export type Floor1 = {
|
||||
partitions: number,
|
||||
partitionClass: { number }, -- 1-based list of class numbers (0-based values)
|
||||
classDims: { number }, -- [class + 1]
|
||||
classSubs: { number },
|
||||
classMaster: { number },
|
||||
subBooks: { { number } }, -- [class + 1][sub + 1] = book (0-based) or -1
|
||||
multiplier: number,
|
||||
xlist: { number }, -- [j + 1]
|
||||
values: number,
|
||||
sorted: { number }, -- point indices (0-based) by increasing x
|
||||
low: { number }, -- neighbours, [j + 1] (0-based indices)
|
||||
high: { number },
|
||||
}
|
||||
|
||||
export type Residue = {
|
||||
type: number,
|
||||
begin: number,
|
||||
finish: number,
|
||||
partSize: number,
|
||||
classifications: number,
|
||||
classbook: number,
|
||||
books: { { number } }, -- [class + 1][pass + 1] = book or -1
|
||||
classdata: { { number } }, -- [classbook entry + 1][k + 1] = class
|
||||
}
|
||||
|
||||
export type Mapping = {
|
||||
submaps: number,
|
||||
couplingSteps: number,
|
||||
magnitude: { number }, -- [step + 1] channel (0-based)
|
||||
angle: { number },
|
||||
mux: { number }, -- [channel + 1] submap (0-based)
|
||||
submapFloor: { number },
|
||||
submapResidue: { number },
|
||||
}
|
||||
|
||||
export type Mode = { blockflag: number, mapping: number }
|
||||
|
||||
export type Setup = {
|
||||
books: { Codebook.Book },
|
||||
floors: { Floor1 },
|
||||
residues: { Residue },
|
||||
mappings: { Mapping },
|
||||
modes: { Mode },
|
||||
modeBits: number,
|
||||
maxFloorValues: number,
|
||||
}
|
||||
|
||||
local function isVorbis(b: buffer, o: number, n: number, kind: number): boolean
|
||||
return n >= 7 and readu8(b, o) == kind and buffer.readstring(b, o + 1, 6) == "vorbis"
|
||||
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"
|
||||
end
|
||||
if n < 30 then
|
||||
return nil, "short identification header"
|
||||
end
|
||||
if readu32(b, o + 7) ~= 0 then
|
||||
return nil, "Vorbis version " .. readu32(b, o + 7)
|
||||
end
|
||||
local ch = readu8(b, o + 11)
|
||||
local rate = readu32(b, o + 12)
|
||||
local bs = readu8(b, o + 28)
|
||||
local l0, l1 = bs % 16, bs // 16
|
||||
if ch == 0 or rate == 0 then
|
||||
return nil, "no channels or no sample rate"
|
||||
end
|
||||
if l0 < 6 or l0 > 13 or l1 < 6 or l1 > 13 or l0 > l1 then
|
||||
return nil, "bad blocksizes"
|
||||
end
|
||||
if readu8(b, o + 29) % 2 == 0 then
|
||||
return nil, "identification header framing bit not set"
|
||||
end
|
||||
return {
|
||||
channels = ch,
|
||||
sampleRate = rate,
|
||||
bitrateMax = buffer.readi32(b, o + 16),
|
||||
bitrateNominal = buffer.readi32(b, o + 20),
|
||||
bitrateMin = buffer.readi32(b, o + 24),
|
||||
blocksize0 = 2 ^ l0,
|
||||
blocksize1 = 2 ^ l1,
|
||||
},
|
||||
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
|
||||
end
|
||||
local p, e = o + 7, o + n
|
||||
if p + 4 > e then
|
||||
return nil, nil
|
||||
end
|
||||
local vlen = readu32(b, p)
|
||||
p += 4
|
||||
if vlen > e - p then
|
||||
return nil, nil
|
||||
end
|
||||
local vendor = buffer.readstring(b, p, vlen)
|
||||
p += vlen
|
||||
local list = {}
|
||||
if p + 4 > e then
|
||||
return vendor, list
|
||||
end
|
||||
local count = readu32(b, p)
|
||||
p += 4
|
||||
for _ = 1, count do
|
||||
if p + 4 > e then
|
||||
break
|
||||
end
|
||||
local len = readu32(b, p)
|
||||
p += 4
|
||||
if len > e - p then
|
||||
break
|
||||
end
|
||||
table.insert(list, buffer.readstring(b, p, len))
|
||||
p += len
|
||||
end
|
||||
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
|
||||
local book, err = Codebook.read(br)
|
||||
if not book then
|
||||
return nil, "codebook " .. (i - 1) .. ": " .. tostring(err)
|
||||
end
|
||||
books[i] = book
|
||||
end
|
||||
-- time domain transforms (placeholders)
|
||||
local ntimes = bits(br, 6) + 1
|
||||
for _ = 1, ntimes do
|
||||
if bits(br, 16) ~= 0 then
|
||||
return nil, "bad time domain transform"
|
||||
end
|
||||
end
|
||||
-- floors
|
||||
local floors: { Floor1 } = {}
|
||||
local maxValues = 0
|
||||
local nfloors = bits(br, 6) + 1
|
||||
for i = 1, nfloors do
|
||||
local ftype = bits(br, 16)
|
||||
if ftype == 0 then
|
||||
return nil, "floor 0"
|
||||
elseif ftype ~= 1 then
|
||||
return nil, "bad floor type " .. ftype
|
||||
end
|
||||
local parts = bits(br, 5)
|
||||
local pclass = table.create(parts, 0)
|
||||
local maxClass = -1
|
||||
for j = 1, parts do
|
||||
pclass[j] = bits(br, 4)
|
||||
if pclass[j] > maxClass then
|
||||
maxClass = pclass[j]
|
||||
end
|
||||
end
|
||||
local cdims, csubs, cmaster, subBooks = {}, {}, {}, {}
|
||||
for j = 1, maxClass + 1 do
|
||||
cdims[j] = bits(br, 3) + 1
|
||||
csubs[j] = bits(br, 2)
|
||||
cmaster[j] = -1
|
||||
if csubs[j] > 0 then
|
||||
cmaster[j] = bits(br, 8)
|
||||
if cmaster[j] >= nbooks then
|
||||
return nil, "bad floor master book"
|
||||
end
|
||||
end
|
||||
local sb = {}
|
||||
for k = 1, 2 ^ csubs[j] do
|
||||
sb[k] = bits(br, 8) - 1
|
||||
if sb[k] >= nbooks then
|
||||
return nil, "bad floor subclass book"
|
||||
end
|
||||
end
|
||||
subBooks[j] = sb
|
||||
end
|
||||
local mult = bits(br, 2) + 1
|
||||
local rangebits = bits(br, 4)
|
||||
local xlist = { 0, 2 ^ rangebits }
|
||||
for j = 1, parts do
|
||||
local c = pclass[j]
|
||||
for _ = 1, cdims[c + 1] do
|
||||
table.insert(xlist, bits(br, rangebits))
|
||||
end
|
||||
end
|
||||
local values = #xlist
|
||||
if values > 65 * 8 then
|
||||
return nil, "too many floor points"
|
||||
end
|
||||
if br.eop then
|
||||
return nil, "setup header ends in a floor"
|
||||
end
|
||||
local order = table.create(values, 0)
|
||||
for j = 1, values do
|
||||
order[j] = j - 1
|
||||
end
|
||||
table.sort(order, function(a: number, c: number): boolean
|
||||
return xlist[a + 1] < xlist[c + 1]
|
||||
end)
|
||||
for j = 1, values - 1 do
|
||||
if xlist[order[j] + 1] == xlist[order[j + 1] + 1] then
|
||||
return nil, "duplicate floor x"
|
||||
end
|
||||
end
|
||||
local low, high = table.create(values, 0), table.create(values, 0)
|
||||
for j = 2, values - 1 do
|
||||
local lo, hi = -1, 65536
|
||||
local x = xlist[j + 1]
|
||||
for k = 0, j - 1 do
|
||||
local xv = xlist[k + 1]
|
||||
if xv > lo and xv < x then
|
||||
low[j + 1], lo = k, xv
|
||||
end
|
||||
if xv < hi and xv > x then
|
||||
high[j + 1], hi = k, xv
|
||||
end
|
||||
end
|
||||
end
|
||||
floors[i] = {
|
||||
partitions = parts,
|
||||
partitionClass = pclass,
|
||||
classDims = cdims,
|
||||
classSubs = csubs,
|
||||
classMaster = cmaster,
|
||||
subBooks = subBooks,
|
||||
multiplier = mult,
|
||||
xlist = xlist,
|
||||
values = values,
|
||||
sorted = order,
|
||||
low = low,
|
||||
high = high,
|
||||
}
|
||||
if values > maxValues then
|
||||
maxValues = values
|
||||
end
|
||||
end
|
||||
-- residues
|
||||
local residues: { Residue } = {}
|
||||
local nres = bits(br, 6) + 1
|
||||
for i = 1, nres do
|
||||
local rtype = bits(br, 16)
|
||||
if rtype > 2 then
|
||||
return nil, "bad residue type " .. rtype
|
||||
end
|
||||
local rbegin, rend = bits(br, 24), bits(br, 24)
|
||||
local psize = bits(br, 24) + 1
|
||||
local ncls = bits(br, 6) + 1
|
||||
local cbook = bits(br, 8)
|
||||
if rend < rbegin then
|
||||
return nil, "residue end before begin"
|
||||
end
|
||||
if cbook >= nbooks then
|
||||
return nil, "bad residue classbook"
|
||||
end
|
||||
local cascade = table.create(ncls, 0)
|
||||
for j = 1, ncls do
|
||||
local lowBits = bits(br, 3)
|
||||
local highBits = if bits(br, 1) == 1 then bits(br, 5) else 0
|
||||
cascade[j] = highBits * 8 + lowBits
|
||||
end
|
||||
local rbooks = {}
|
||||
for j = 1, ncls do
|
||||
local row = table.create(8, -1)
|
||||
local c = cascade[j]
|
||||
for k = 1, 8 do
|
||||
if c % 2 == 1 then
|
||||
row[k] = bits(br, 8)
|
||||
if row[k] >= nbooks then
|
||||
return nil, "bad residue book"
|
||||
end
|
||||
end
|
||||
c = c // 2
|
||||
end
|
||||
rbooks[j] = row
|
||||
end
|
||||
if br.eop then
|
||||
return nil, "setup header ends in a residue"
|
||||
end
|
||||
-- class numbers of each classbook entry (stb's classdata)
|
||||
local cb = books[cbook + 1]
|
||||
local cw = cb.dims
|
||||
if cb.entries * cw > 2 ^ 22 then
|
||||
return nil, "residue classbook too large"
|
||||
end
|
||||
local classdata = table.create(cb.entries)
|
||||
for j = 0, cb.entries - 1 do
|
||||
local row = table.create(cw, 0)
|
||||
local tmp = j
|
||||
for k = cw, 1, -1 do
|
||||
row[k] = tmp % ncls
|
||||
tmp = tmp // ncls
|
||||
end
|
||||
classdata[j + 1] = row
|
||||
end
|
||||
residues[i] = {
|
||||
type = rtype,
|
||||
begin = rbegin,
|
||||
finish = rend,
|
||||
partSize = psize,
|
||||
classifications = ncls,
|
||||
classbook = cbook,
|
||||
books = rbooks,
|
||||
classdata = classdata,
|
||||
}
|
||||
end
|
||||
-- mappings
|
||||
local mappings: { Mapping } = {}
|
||||
local nmaps = bits(br, 6) + 1
|
||||
local chBits = ilog(channels - 1)
|
||||
for i = 1, nmaps do
|
||||
if bits(br, 16) ~= 0 then
|
||||
return nil, "bad mapping type"
|
||||
end
|
||||
local submaps = if bits(br, 1) == 1 then bits(br, 4) + 1 else 1
|
||||
local steps = 0
|
||||
local mag, ang = {}, {}
|
||||
if bits(br, 1) == 1 then
|
||||
steps = bits(br, 8) + 1
|
||||
if steps > channels then
|
||||
return nil, "too many coupling steps"
|
||||
end
|
||||
for k = 1, steps do
|
||||
mag[k] = bits(br, chBits)
|
||||
ang[k] = bits(br, chBits)
|
||||
if mag[k] >= channels or ang[k] >= channels or mag[k] == ang[k] then
|
||||
return nil, "bad coupling channels"
|
||||
end
|
||||
end
|
||||
end
|
||||
if bits(br, 2) ~= 0 then
|
||||
return nil, "mapping reserved bits set"
|
||||
end
|
||||
local mux = table.create(channels, 0)
|
||||
if submaps > 1 then
|
||||
for j = 1, channels do
|
||||
mux[j] = bits(br, 4)
|
||||
if mux[j] >= submaps then
|
||||
return nil, "bad channel mux"
|
||||
end
|
||||
end
|
||||
end
|
||||
local sf, sr = {}, {}
|
||||
for j = 1, submaps do
|
||||
bits(br, 8)
|
||||
sf[j] = bits(br, 8)
|
||||
sr[j] = bits(br, 8)
|
||||
if sf[j] >= nfloors or sr[j] >= nres then
|
||||
return nil, "bad submap floor / residue"
|
||||
end
|
||||
end
|
||||
mappings[i] = {
|
||||
submaps = submaps,
|
||||
couplingSteps = steps,
|
||||
magnitude = mag,
|
||||
angle = ang,
|
||||
mux = mux,
|
||||
submapFloor = sf,
|
||||
submapResidue = sr,
|
||||
}
|
||||
end
|
||||
-- modes
|
||||
local modes: { Mode } = {}
|
||||
local nmodes = bits(br, 6) + 1
|
||||
for i = 1, nmodes do
|
||||
local bf = bits(br, 1)
|
||||
local wt, tt = bits(br, 16), bits(br, 16)
|
||||
local m = bits(br, 8)
|
||||
if wt ~= 0 or tt ~= 0 or m >= nmaps then
|
||||
return nil, "bad mode"
|
||||
end
|
||||
modes[i] = { blockflag = bf, mapping = m }
|
||||
end
|
||||
if br.eop then
|
||||
return nil, "setup header truncated"
|
||||
end
|
||||
if bits(br, 1) ~= 1 then
|
||||
return nil, "setup header framing bit not set"
|
||||
end
|
||||
return {
|
||||
books = books,
|
||||
floors = floors,
|
||||
residues = residues,
|
||||
mappings = mappings,
|
||||
modes = modes,
|
||||
modeBits = ilog(nmodes - 1),
|
||||
maxFloorValues = maxValues,
|
||||
},
|
||||
nil
|
||||
end
|
||||
|
||||
return Setup
|
||||
Reference in new issue
Block a user