ws-cleanup: shared plumbing: Err.from, core/Sniff, Pcm.drain, Bits.msbPut; DUP lint; tools/move.py

- core/Err.from(message, offset?, kind?): the private-submodule -> Err plumbing (a kind, or a 'code: ' prefix /
  'truncated' / 'past the end' in the message) that Brotli, Bzip2, Zstd, Jpeg, WOFF2, FBX, plist, msgpack and
  bmfont each wrote.
- core/Sniff (new): Sniff.text(r, n, trim?) (the start of a text file without its BOM), Sniff.dropBom(s),
  Sniff.magic(r, s); 13 text formats' sniff / decode BOM handling goes through it.
- codecs/Pcm.drain(dec, source?, maxFrames?): a format's decode() through its open(); the collect-the-Decoder
  loops of aac, aiff, au, caf, flac, m4a, mp3, opus, vorbis and wav are one call each.
- core/Bits.msbPut: the MSB-first bit writer the MP3 / MP2 / FLAC encoders each copied.
- tools/lint_rules.py DUP: a function body of 12+ code lines repeated token for token in another module fails.
- tools/move.py: moves a module and rewrites every reference (requires, deps, headers, fixture INPUTS, docs).
- tools/fixtures/zstd.py: the incompressible corpus is smaller (zstd.luau 15.1 -> 13.4 MB; every fixture module is
  now under 14.3 MB).
- tools/bench.luau: MPEG-1 also runs with the api's LuaJIT trace limits; the sections that compare tuned / default
  limits set them explicitly (jitTune), so the order of sections no longer leaks the tuning.
This commit is contained in:
4DBug committed 2026-10-31 12:00:59 -05:00
1 parent 3243404374
commit 763f4a7c04
44 files changed
+374 -370

No files matched your search

+3 -17
View File
@@ -8,6 +8,7 @@
local Err = require("@formats/core/Err")
local Bytes = require("@formats/core/Bytes")
local Types = require("@formats/Types")
local Pcm = require("@formats/codecs/Pcm")
local Aac = require("@formats/codecs/Aac")
-- ------------------------------------------------------------------------------------------------ contract
@@ -16,7 +17,7 @@ local AacFormat = {
category = "audio",
extensions = { "aac", "adts" },
mime = { "audio/aac", "audio/aacp", "audio/x-aac" },
deps = { "core/Err", "core/Bytes", "Types", "codecs/Aac" },
deps = { "core/Err", "core/Bytes", "Types", "codecs/Aac", "codecs/Pcm" },
caps = { decode = true, encode = false, codecs = { "aac" }, streaming = true },
}
@@ -256,22 +257,7 @@ function AacFormat.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Audio?,
return nil, err
end
local d: any = dec
local audio = Types.Audio.new(d.sampleRate, d.channels, d.frames)
local done = 0
while done < d.frames do
local b, n = d:read(math.min(65536, d.frames - done))
if n == 0 then
break
end
buffer.copy(audio.samples, done * d.channels * 4, b, 0, n * d.channels * 4)
done += n
end
if d.error then
return nil, d.error
end
audio.tags = d.tags
audio.meta.source = { format = "aac", codec = "aac", bitrate = d.bitrate }
return audio, nil
return Pcm.drain(d, { format = "aac", codec = "aac", bitrate = d.bitrate })
end
return AacFormat
+1 -6
View File
@@ -248,12 +248,7 @@ function Aiff.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Audio?, Err.
if not dec then
return nil, err
end
local d = dec :: any
local a = Types.Audio.new(d.sampleRate, d.channels, d.frames)
d:read(d.frames, a.samples)
a.tags = d.tags
a.meta.source = d.source
return a, nil
return Pcm.drain(dec)
end
-- ------------------------------------------------------------------------------------------------ encode
+1 -6
View File
@@ -66,12 +66,7 @@ function Au.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Audio?, Err.Er
if not dec then
return nil, err
end
local d = dec :: any
local a = Types.Audio.new(d.sampleRate, d.channels, d.frames)
d:read(d.frames, a.samples)
a.tags = d.tags
a.meta.source = d.source
return a, nil
return Pcm.drain(dec)
end
-- ------------------------------------------------------------------------------------------------ encode
+3 -17
View File
@@ -9,6 +9,7 @@
local Err = require("@formats/core/Err")
local Bytes = require("@formats/core/Bytes")
local Types = require("@formats/Types")
local Pcm = require("@formats/codecs/Pcm")
local Aac = require("@formats/codecs/Aac")
local Alac = require("@formats/codecs/Alac")
@@ -18,7 +19,7 @@ local Caf = {
category = "audio",
extensions = { "caf", "caff" },
mime = { "audio/x-caf" },
deps = { "core/Err", "core/Bytes", "Types", "codecs/Aac", "codecs/Alac" },
deps = { "core/Err", "core/Bytes", "Types", "codecs/Aac", "codecs/Alac", "codecs/Pcm" },
caps = { decode = true, encode = true, codecs = { "pcm", "alac", "aac", "ulaw", "alaw" }, encodeCodecs = { "pcm", "alac" }, tags = true, streaming = true },
}
@@ -559,22 +560,7 @@ function Caf.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Audio?, Err.E
return nil, err
end
local d: any = dec
local audio = Types.Audio.new(d.sampleRate, d.channels, d.frames)
local done = 0
while done < d.frames do
local b, n = d:read(math.min(65536, d.frames - done))
if n == 0 then
break
end
buffer.copy(audio.samples, done * d.channels * 4, b, 0, n * d.channels * 4)
done += n
end
if d.error then
return nil, d.error
end
audio.tags = d.tags
audio.meta.source = { format = "caf", codec = d.codec, bitsPerSample = d.bitsPerSample }
return audio, nil
return Pcm.drain(d, { format = "caf", codec = d.codec, bitsPerSample = d.bitsPerSample })
end
-- ------------------------------------------------------------------------------------------------ encode
+3 -19
View File
@@ -10,6 +10,7 @@
-- CRC-16; STREAMINFO carries the sizes, total samples and the MD5 of the PCM; VORBIS_COMMENT from the tags; an optional
-- SEEKTABLE. Streams: input blocks are pulled from a codecs/Pcm Source, so a Decoder is encoded without decoding it
-- whole. Never throws: bad options are rejected by audio/flac before calling in.
local Bits = require("@formats/core/Bits")
local Md5 = require("@formats/core/Md5")
local VorbisComment = require("@formats/codecs/VorbisComment")
local Frame = require("@formats/audio/flac/Frame")
@@ -35,7 +36,7 @@ end
Encode.crc8, Encode.crc16 = crc8, crc16
-- ------------------------------------------------------------------------------------------------ bit writer (MSB first)
type W = { buf: buffer, pos: number, acc: number, cnt: number }
type W = Bits.RawWriter
local function ensure(w: W, n: number)
local need = w.pos + n + 16
@@ -47,24 +48,7 @@ local function ensure(w: W, n: number)
end
end
-- put(w, v, n): the low n bits (n <= 32) of the non-negative integer v
local function put(w: W, v: number, n: number)
if n == 0 then
return
end
local acc = w.acc * POW[n + 1] + v
local cnt = w.cnt + n
local b, p = w.buf, w.pos
while cnt >= 8 do
cnt -= 8
local m = POW[cnt + 1]
local byte = floor(acc / m)
writeu8(b, p, byte)
p += 1
acc -= byte * m
end
w.acc, w.cnt, w.pos = acc, cnt, p
end
local put = Bits.msbPut
local function putSigned(w: W, v: number, n: number)
put(w, if v < 0 then v + POW[n + 1] else v, n)
+2 -29
View File
@@ -29,7 +29,7 @@ local Flac = {
category = "audio",
extensions = { "flac", "oga" },
mime = { "audio/flac", "audio/x-flac" },
deps = { "core/Err", "core/Bytes", "Types", "containers/Ogg", "core/Md5", "codecs/Pcm", "codecs/VorbisComment" },
deps = { "core/Err", "core/Bytes", "core/Bits", "Types", "containers/Ogg", "core/Md5", "codecs/Pcm", "codecs/VorbisComment" },
caps = { decode = true, encode = true, streaming = true, depths = { 4, 8, 12, 16, 20, 24, 32 }, ogg = true, seek = true },
}
@@ -89,34 +89,7 @@ function Flac.decode(r: Bytes.Reader, opts: OpenOptions?): (Types.Audio?, Err.Er
if d.frames > limit or d.frames * d.channels > 2 ^ 28 then
return nil, Err.new(Err.LIMIT, string.format("%d frames is over the limit", d.frames))
end
local ch = d.channels
local parts, total = {}, 0
if d.frames > 0 then
local out, n = d:read(d.frames)
table.insert(parts, out)
total = n
end
while true do -- frames past what STREAMINFO / the scan said
local out, n = d:read(65536)
if n == 0 then
break
end
table.insert(parts, out)
total += n
if total > limit then
return nil, Err.new(Err.LIMIT, "stream over the frame limit")
end
end
local a = Types.Audio.new(d.sampleRate, ch, total)
local o = 0
for _, p in parts do
local n = math.min(buffer.len(p), total * ch * 4 - o)
buffer.copy(a.samples, o, p, 0, n)
o += n
end
a.tags = d.tags
a.meta.source = { format = "flac", bitDepth = d.bitsPerSample, container = d.container, vendor = d.vendor, md5 = d.md5, pictures = d.pictures, comments = d.comments }
return a, nil
return Pcm.drain(d, { format = "flac", bitDepth = d.bitsPerSample, container = d.container, vendor = d.vendor, md5 = d.md5, pictures = d.pictures, comments = d.comments }, limit)
end
-- ------------------------------------------------------------------------------------------------ encode
+5 -25
View File
@@ -9,6 +9,7 @@
local Err = require("@formats/core/Err")
local Bytes = require("@formats/core/Bytes")
local Types = require("@formats/Types")
local Pcm = require("@formats/codecs/Pcm")
local IsoBmff = require("@formats/containers/IsoBmff")
local Aac = require("@formats/codecs/Aac")
local Alac = require("@formats/codecs/Alac")
@@ -19,7 +20,7 @@ local M4a = {
category = "audio",
extensions = { "m4a", "m4b", "m4r" },
mime = { "audio/mp4", "audio/x-m4a", "audio/m4a" },
deps = { "core/Err", "core/Bytes", "Types", "containers/IsoBmff", "codecs/Aac", "codecs/Alac" },
deps = { "core/Err", "core/Bytes", "Types", "containers/IsoBmff", "codecs/Aac", "codecs/Alac", "codecs/Pcm" },
caps = { decode = true, encode = true, codecs = { "aac", "alac" }, encodeCodecs = { "alac" }, tags = true, streaming = true },
}
@@ -306,32 +307,11 @@ function M4a.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Audio?, Err.E
return nil, err
end
local d: any = dec
local audio = Types.Audio.new(d.sampleRate, d.channels, d.frames)
local done = 0
local chunk = buffer.create(65536 * d.channels * 4)
while done < d.frames do
local b, n = d:read(math.min(65536, d.frames - done), chunk)
if n == 0 then
break
end
buffer.copy(audio.samples, done * d.channels * 4, b, 0, n * d.channels * 4)
done += n
end
if d.error then
return nil, d.error
end
if done < d.frames then
local short = buffer.create(done * d.channels * 4)
buffer.copy(short, 0, audio.samples, 0, buffer.len(short))
audio.samples = short
audio.frames = done
end
audio.tags = d.tags
audio.meta.source = { format = "m4a", codec = d.codec, bitsPerSample = d.track.bitsPerSample, brand = d.movie.brand }
if #d.pictures > 0 then
local audio, derr = Pcm.drain(d, { format = "m4a", codec = d.codec, bitsPerSample = d.track.bitsPerSample, brand = d.movie.brand })
if audio and #d.pictures > 0 then
audio.meta.pictures = d.pictures
end
return audio, nil
return audio, derr
end
-- ------------------------------------------------------------------------------------------------ encode
+3 -18
View File
@@ -8,6 +8,7 @@
-- in this folder reads them), a greedy allocation that always gives the next step to the band with the most noise
-- (signal level minus the quantizer's SNR; no psychoacoustic model), uniform midtread quantization and grouping of
-- 3 / 5 / 9-level samples. Never throws.
local Bits = require("@formats/core/Bits")
local T = require("@formats/audio/mp3/Tables")
local Analysis = require("@formats/audio/mp3/Analysis")
@@ -87,24 +88,8 @@ local BITRATES_V1 = { 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320,
local BITRATES_V2 = { 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160 }
Encode2.BITRATES = { [1] = BITRATES_V1, [2] = BITRATES_V2 }
type W = { buf: buffer, pos: number, acc: number, cnt: number }
local function put(w: W, v: number, n: number)
if n == 0 then
return
end
local acc = w.acc * POW[n + 1] + v
local cnt = w.cnt + n
local b, p = w.buf, w.pos
while cnt >= 8 do
cnt -= 8
local m = POW[cnt + 1]
local byte = floor(acc / m)
writeu8(b, p, byte)
p += 1
acc -= byte * m
end
w.acc, w.cnt, w.pos = acc, cnt, p
end
type W = Bits.RawWriter
local put = Bits.msbPut
export type Source = {
sampleRate: number,
+3 -18
View File
@@ -8,6 +8,7 @@
-- analysis filterbank (audio/mp3/Analysis), the 36-point MDCT with the sine window, the alias-reduction butterflies,
-- the x^(3/4) quantizer, big_values / count1 partitioning with per-region Huffman table choice; the Huffman codes are
-- derived from the standard's decoder trees (audio/mp3/EncTables, from PDMP3, public domain). Never throws.
local Bits = require("@formats/core/Bits")
local E = require("@formats/audio/mp3/EncTables")
local T = require("@formats/audio/mp3/Tables")
local Analysis = require("@formats/audio/mp3/Analysis")
@@ -87,25 +88,9 @@ for m = 0, 15 do
end
-- ------------------------------------------------------------------------------------------------ bit writer
type W = { buf: buffer, pos: number, acc: number, cnt: number }
type W = Bits.RawWriter
local function put(w: W, v: number, n: number)
if n == 0 then
return
end
local acc = w.acc * POW[n + 1] + v
local cnt = w.cnt + n
local b, p = w.buf, w.pos
while cnt >= 8 do
cnt -= 8
local m = POW[cnt + 1]
local byte = floor(acc / m)
writeu8(b, p, byte)
p += 1
acc -= byte * m
end
w.acc, w.cnt, w.pos = acc, cnt, p
end
local put = Bits.msbPut
Encode3.put = put
-- ------------------------------------------------------------------------------------------------ constants
+5 -8
View File
@@ -32,7 +32,7 @@ local Mp3 = {
category = "audio",
extensions = { "mp3", "mp2", "mp1", "mpga" },
mime = { "audio/mpeg", "audio/mp3", "audio/mpa" },
deps = { "core/Err", "core/Bytes", "Types", "codecs/Pcm", "codecs/Resample" },
deps = { "core/Err", "core/Bytes", "core/Bits", "Types", "codecs/Pcm", "codecs/Resample" },
caps = { decode = true, encode = true, streaming = true, seek = true, layers = { 1, 2, 3 }, encodeLayers = { 2, 3 }, channels = 2 },
}
@@ -91,14 +91,11 @@ function Mp3.decode(r: Bytes.Reader, opts: OpenOptions?): (Types.Audio?, Err.Err
if not dec then
return nil, err
end
local a = Types.Audio.new(dec.sampleRate, dec.channels, dec.frames)
local _, n = dec:read(dec.frames, a.samples)
if n < dec.frames then
return nil, Err.new(Err.CORRUPT, string.format("decoded %d of %d frames", n, dec.frames))
local a, derr = Pcm.drain(dec, { format = "mp3", layer = dec.layer, version = dec.version, bitrate = dec.bitrate, vbr = dec.vbr })
if a and a.frames < dec.frames then
return nil, Err.new(Err.CORRUPT, string.format("decoded %d of %d frames", a.frames, dec.frames))
end
a.tags = dec.tags
a.meta.source = { format = "mp3", layer = dec.layer, version = dec.version, bitrate = dec.bitrate, vbr = dec.vbr }
return a, nil
return a, derr
end
-- ------------------------------------------------------------------------------------------------ encode
+1 -20
View File
@@ -191,26 +191,7 @@ function Opus.decode(r: any, opts: OpenOptions?): (Types.Audio?, Err.Err?)
if not dec then
return nil, err
end
local ch = dec.channels
local a = Types.Audio.new(SAMPLE_RATE, ch, dec.frames)
local total = 0
while total < dec.frames do
local pcm, n = dec:read(math.min(65536, dec.frames - total))
if n == 0 then
break
end
buffer.copy(a.samples, total * ch * 4, pcm, 0, n * ch * 4)
total += n
end
if total < dec.frames then -- a truncated file: keep what decoded
local s = buffer.create(total * ch * 4)
buffer.copy(s, 0, a.samples, 0, total * ch * 4)
a.samples = s
a.frames = total
end
a.tags = dec.tags
a.meta.source = { format = "opus", channels = dec.sourceChannels, preSkip = dec.preSkip, vendor = dec.vendor, comments = dec.comments, mappingFamily = dec.head.mappingFamily, inputSampleRate = dec.head.inputSampleRate }
return a, nil
return Pcm.drain(dec, { format = "opus", channels = dec.sourceChannels, preSkip = dec.preSkip, vendor = dec.vendor, comments = dec.comments, mappingFamily = dec.head.mappingFamily, inputSampleRate = dec.head.inputSampleRate })
end
-- ------------------------------------------------------------------------------------------------ encode
+1 -22
View File
@@ -93,28 +93,7 @@ function Vorbis.decode(r: Bytes.Reader, opts: OpenOptions?): (Types.Audio?, Err.
if not dec then
return nil, err
end
local d = dec :: any
local ch = d.channels
local parts, total = {}, 0
while true do
local b, n = d:read(65536)
if n == 0 then
break
end
local copy = buffer.create(n * ch * 4)
buffer.copy(copy, 0, b, 0, n * ch * 4)
table.insert(parts, copy)
total += n
end
local a = Types.Audio.new(d.sampleRate, ch, total)
local o = 0
for _, p in parts do
buffer.copy(a.samples, o, p)
o += buffer.len(p)
end
a.tags = d.tags
a.meta.source = d.source
return a, nil
return Pcm.drain(dec)
end
-- ------------------------------------------------------------------------------------------------ encode
+4 -12
View File
@@ -18,6 +18,7 @@
local Err = require("@formats/core/Err")
local Bytes = require("@formats/core/Bytes")
local Types = require("@formats/Types")
local Pcm = require("@formats/codecs/Pcm")
local Riff = require("@formats/containers/Riff")
local Codecs = require("@formats/audio/wav/Codecs")
@@ -27,7 +28,7 @@ local Wav = {
category = "audio",
extensions = { "wav", "wave", "rf64", "bw64" },
mime = { "audio/wav", "audio/x-wav", "audio/wave", "audio/vnd.wave" },
deps = { "core/Err", "core/Bytes", "Types", "containers/Riff" },
deps = { "core/Err", "core/Bytes", "Types", "containers/Riff", "codecs/Pcm" },
caps = { decode = true, encode = true, streaming = true, depths = { 8, 16, 24, 32 }, float = true, rf64 = true },
}
@@ -170,23 +171,14 @@ function Wav.decode(r: Bytes.Reader, opts: OpenOptions?): (Types.Audio?, Err.Err
if not dec then
return nil, err
end
local a = Types.Audio.new(dec.sampleRate, dec.channels, dec.frames)
local _, n = dec:read(dec.frames, a.samples)
if n < dec.frames then
local s = buffer.create(n * dec.channels * 4)
buffer.copy(s, 0, a.samples, 0, n * dec.channels * 4)
a.samples, a.frames = s, n
end
a.tags = dec.tags
a.meta.source = {
return Pcm.drain(dec, {
format = "wav",
codec = dec.format,
bitDepth = dec.bitsPerSample,
container = dec.container,
channelMask = dec.channelMask,
truncated = dec.truncated,
}
return a, nil
})
end
-- ------------------------------------------------------------------------------------------------ encode