ws-audio2: codecs/audio/Qoa and audio/qoa (QOA read / write, port of qoa.h, MIT)

Decode bit-exact with ffmpeg and qoa.h; encode byte-for-byte qoa.h's qoa_encode (fixtures compile the committed
qoa.h reference with nix gcc).
This commit is contained in:
4DBug committed 2026-10-31 14:26:33 -05:00
1 parent 5dfaaf7524
commit f765763537
9 files changed
+1468

No files matched your search

+185
View File
@@ -0,0 +1,185 @@
--!strict
-- audio/qoa: QOA, "Quite OK Audio" (.qoa), over codecs/audio/Qoa (a port of qoa.h, phoboslab, MIT). New in
-- formats-luau. Decode / open: static files and streaming files (total 0 in the header), 1-255 channels (WAVE order
-- for 1-8, as the spec lays them out), any rate up to 16777215 Hz, frame-accurate seeking (every frame carries its
-- own predictor state), a truncated file decodes its whole frames. Encode (Audio or Decoder, streamed frame by frame):
-- byte-for-byte what qoa.h's qoa_encode writes, 1-8 channels (qoa.h's limit). Not supported: frames whose channel
-- count or rate change mid-stream (Err.UNSUPPORTED).
--
-- local dec = Qoa.open(Bytes.reader(buf)) -- Types.Decoder
-- local file = Qoa.encode(audio)
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@formats/core/Err")
local Bytes = require("@formats/core/Bytes")
local Types = require("@formats/Types")
local Pcm = require("@formats/codecs/audio/Pcm")
local Stream = require("@formats/codecs/audio/Stream")
local Codec = require("@formats/codecs/audio/Qoa")
-- ------------------------------------------------------------------------------------------------ contract
local Qoa = {
name = "qoa",
category = "audio",
extensions = { "qoa" },
mime = { "audio/qoa" },
deps = { "core/Err", "core/Bytes", "Types", "codecs/audio/Pcm", "codecs/audio/Stream", "codecs/audio/Qoa" },
caps = { decode = true, encode = true, streaming = true, lossy = true, seek = true, channels = 8 },
}
-- ------------------------------------------------------------------------------------------------ types
export type DecodeOptions = {
maxFrames: number?, -- decode(): Err.LIMIT past this many sample frames (default 2^28)
}
-- ------------------------------------------------------------------------------------------------ sniff
function Qoa.sniff(r: Bytes.Reader): number
return if r:peek(4) == "qoaf" then 1 else 0
end
-- ------------------------------------------------------------------------------------------------ decode
function Qoa.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.Audio?, Err.Err?)
local dec, err = Qoa.open(r)
if not dec then
return nil, err
end
local d = dec :: any
local limit = opts and opts.maxFrames or 2 ^ 28
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
return Pcm.drain(d, { format = "qoa", streaming = d.streaming }, limit)
end
-- ------------------------------------------------------------------------------------------------ encode
function Qoa.encode(value: any, _opts: any?): (buffer?, Err.Err?)
local src, serr = Pcm.source(value)
if not src then
return nil, serr
end
local ch, rate = src.channels, src.sampleRate
if ch > 8 then
return nil, Err.new(Err.ARG, "QOA (qoa.h) holds at most 8 channels")
end
if rate ~= math.floor(rate) or rate < 1 or rate > 0xFFFFFF then
return nil, Err.new(Err.ARG, "the sample rate must be an integer in 1 .. 16777215")
end
local FL = Codec.FRAME_LEN
local w = Bytes.writer(8 + (if src.frames > 0 then Codec.frameSize(ch, (src.frames + 19) // 20) + 24 * ch * (src.frames // FL + 1) else 65536))
w:string("qoaf")
w:u32be(0)
local lms = Codec.newLms(ch)
local fb = buffer.create(FL * ch * 4)
local tmp: buffer? = nil
local total = 0
while true do
-- one frame: 5120 sample frames (the last may be shorter), as qoa.h cuts them
local have = 0
while have < FL do
local b, n = src:read(FL - have, tmp)
if n <= 0 then
break
end
tmp = b
buffer.copy(fb, have * ch * 4, b, 0, n * ch * 4)
have += n
end
if have == 0 then
break
end
local at = w:reserve(Codec.frameSize(ch, (have + 19) // 20))
w.pos = at + Codec.encodeFrame(fb, 0, have, ch, rate, lms, w.buf, at)
total += have
if have < FL then
break
end
end
if src.err then
return nil, src.err
end
if total == 0 then
return nil, Err.new(Err.ARG, "QOA needs at least one sample")
end
if total >= 2 ^ 32 then
return nil, Err.new(Err.ARG, "too many samples for QOA")
end
local out = w:finish()
buffer.writeu32(out, 4, bit32.byteswap(total))
return out, nil
end
-- ------------------------------------------------------------------------------------------------ open
function Qoa.open(r: Bytes.Reader, _opts: any?): (Types.Decoder?, Err.Err?)
if type(r) ~= "table" or type(r.buf) ~= "buffer" then
return nil, Err.new(Err.ARG, "open: expected a Reader")
end
local h = r:sub(0)
if h:string(4) ~= "qoaf" then
return nil, Err.new(Err.CORRUPT, "not a QOA file")
end
local declared = h:u32be()
if h.err then
return nil, h.err
end
local buf = r.buf
-- the frame index: offsets, first frames
local offs, starts = {}, {}
local p, total = r.base + 8, 0
local ch, rate = 0, 0
while p < r.limit do
local info, msg = Codec.frameInfo(buf, p, r.limit - p)
if not info then
if #offs == 0 then
return nil, Err.from(msg, p - r.base)
end
break -- a cut or damaged tail: keep the whole frames
end
if #offs == 0 then
ch, rate = info.channels, info.rate
elseif info.channels ~= ch or info.rate ~= rate then
return nil, Err.new(Err.UNSUPPORTED, "the channel count or rate changes mid-stream", p - r.base)
end
table.insert(offs, p)
table.insert(starts, total)
total += info.samples
p += info.size
if declared > 0 and total >= declared then
break
end
end
if #offs == 0 then
return nil, Err.new(Err.TRUNCATED, "no frames")
end
local frames = if declared > 0 then math.min(declared, total) else total
local pcm = buffer.create(Codec.FRAME_LEN * ch * 4)
local dec: any = {
sampleRate = rate,
channels = ch,
frames = frames,
duration = frames / rate,
bitrate = math.floor((p - r.base) * 8 / math.max(frames / rate, 1e-9)),
tags = {},
streaming = declared == 0,
}
local limit = r.limit
return Stream.packets(dec, {
count = #offs,
start = function(i: number): number
return if i > #offs then total else starts[i]
end,
decode = function(i: number): (number?, Err.Err?)
local n, msg = Codec.decodeFrame(buf, offs[i], limit - offs[i], pcm, 0)
if not n then
return nil, Err.from(msg, offs[i] - r.base)
end
return n, nil
end,
pcm = function(): buffer
return pcm
end,
reset = function() end,
stop = frames,
})
end
return Qoa