merge master (ws/apifix) into ws/arch2

This commit is contained in:
4DBug committed 2026-10-31 14:25:30 -05:00
commit 94d79313d4
39 files changed
+1158 -73

No files matched your search

+160 -9
View File
@@ -12,7 +12,7 @@
-- local s = F.saveString(img, { format = "png" })
-- local buf = F.convert(src, "png", opts?) -- load + save
-- F.formats() -- the capability list (also docs/FORMATS.md)
-- F.ops.image.resize(img, 64, 64) -- editing: F.ops.image / mesh / layers / audio / vector.rasterize
-- F.ops.image.resize(img, 64, 64) -- editing: F.ops.image / mesh / layers / audio / music / vector
--
-- Never throws on bad data or bad arguments: every function returns nil, Err (Err.code, Err.message, tostring(err)).
local Err = require("@formats/core/Err")
@@ -129,6 +129,20 @@ local function detect(buf: buffer, name: string?, category: string?, format: str
return nil, Err.new(Err.UNKNOWN_FORMAT, "unrecognised data" .. (if name then " (" .. name .. ")" else "") .. (if category then " for " .. category else ""))
end
-- detectFor(buf, name, o) -> entry | nil, Err, rendered: detect with o.category / o.format; every music file that
-- renders (MOD, S3M, XM, IT, MIDI) is also audio, so an audio lookup that finds nothing falls back to them (rendered =
-- true: open the format, which renders it, instead of decoding it)
local function detectFor(buf: buffer, name: string?, o: { [string]: any }): (Registry.Entry?, Err.Err?, boolean)
local entry, err = detect(buf, name, o.category, o.format)
if not entry and o.category == "audio" then
local e = detect(buf, name, "music", o.format)
if e and e.caps.render then
return e, nil, true
end
end
return entry, err, false
end
local function tag(value: any, formatName: string)
if type(value) == "table" then
local meta = value.meta
@@ -285,8 +299,13 @@ local function openAudio(demuxer: any, track: any): (any, Err.Err?)
local dec, oerr = F.open(buf, { format = fmt, category = "audio" })
-- Matroska CodecDelay: ffmpeg drops it for MPEG audio / AAC; Opus has its own pre-skip and Vorbis's delay is
-- already the decoder's own (its first packet returns nothing)
if dec and fmt ~= "opus" and fmt ~= "vorbis" and type(track.codecDelay) == "number" and track.codecDelay > 0 then
dec = AudioWrap.trim(dec, math.floor(track.codecDelay * dec.sampleRate + 0.5))
-- MP4 edit lists add the play length (the encoder's end padding is cut, as ffmpeg does)
if dec and fmt ~= "opus" and fmt ~= "vorbis" then
local skip = if type(track.codecDelay) == "number" and track.codecDelay > 0 then math.floor(track.codecDelay * dec.sampleRate + 0.5) else 0
local length = if type(track.playDuration) == "number" and track.playDuration > 0 then math.floor(track.playDuration * dec.sampleRate + 0.5) else nil
if skip > 0 or length then
dec = AudioWrap.trim(dec, skip, length)
end
end
return dec, oerr
end
@@ -326,7 +345,7 @@ function F.load(src: any, opts: Options?): (any, Err.Err?)
o = table.clone(o)
o.name = name -- formats may use the file name (single-stream archives name their entry after it)
end
local entry, derr = detect(buf, name, o.category, o.format)
local entry, derr, rendered = detectFor(buf, name, o)
if not entry then
return nil, derr
end
@@ -340,7 +359,7 @@ function F.load(src: any, opts: Options?): (any, Err.Err?)
o.resolve = siblings(name)
end
local value, err
if m.decode then
if m.decode and not rendered then
local ok, v, e = pcall(m.decode, Bytes.reader(buf), withHooks(o))
if not ok then
return nil, Err.wrap(Err.new(Err.CORRUPT, "decoder threw: " .. tostring(v)), entry.name)
@@ -374,7 +393,7 @@ function F.open(src: any, opts: Options?): (any, Err.Err?)
if not buf then
return nil, ierr
end
local entry, derr = detect(buf, name, o.category, o.format)
local entry, derr = detectFor(buf, name, o)
if not entry and o.category == "video" and o.format == nil then
entry = detect(buf, name, "image", nil) -- animated images open as frames-backed videos
end
@@ -410,6 +429,91 @@ end
local encodeTextures: (scene: any) -> Err.Err?
-- videoToImage(v, opts) -> an animated rgba8 Image of the video's frames (for the GIF / APNG / WebP writers) | nil,
-- Err. opts.frames: how many frames to keep (default all, up to the cap); opts.fps: the output frame rate (frames are
-- picked at 1 / fps steps, each held as long as it covers; default the source's own timing); opts.maxFrames: the cap
-- (default 300); opts.maxPixels: a cap on frames * width * height (default 2^25). Reads the decoder from where it is.
local function videoToImage(v: any, o: Options): (any, Err.Err?)
if type(v.nextFrame) ~= "function" or type(v.toRGBA) ~= "function" then
return nil, Err.new(Err.ARG, "save: not a VideoDecoder")
end
local cap = o.maxFrames or 300
local want = o.frames
local fps = o.fps
if type(cap) ~= "number" or cap < 1 or (want ~= nil and (type(want) ~= "number" or want < 1)) or (fps ~= nil and (type(fps) ~= "number" or fps <= 0 or fps > 1000)) then
return nil, Err.new(Err.ARG, "save: frames / maxFrames must be >= 1 and fps 0..1000")
end
local limit = math.floor(if want then math.min(want, cap) else cap)
local w, h = math.max(1, tonumber(v.width) or 1), math.max(1, tonumber(v.height) or 1)
limit = math.max(1, math.min(limit, math.floor((o.maxPixels or 2 ^ 25) / (w * h))))
local defaultDur = if type(v.frameRate) == "number" and v.frameRate > 0 then 1 / v.frameRate else 0.1
local images: { any }, delays: { number } = {}, {}
local t0: number? = nil
local taken = 0 -- output slots used (fps mode)
while #images < limit do
local ok, f, ferr = pcall(v.nextFrame, v)
if not ok then
return nil, Err.new(Err.CORRUPT, "video decoder threw: " .. tostring(f))
end
if f == nil then
if ferr then
return nil, ferr
end
break
end
local dur = if type(f.duration) == "number" and f.duration > 0 then f.duration else defaultDur
local delay = dur
if fps then
local start: number = t0 or f.pts
t0 = start
local slots = math.ceil((f.pts - start + dur) * fps - 1e-6) - taken
delay = slots / fps
taken += math.max(0, slots)
end
if delay > 0 then
local rok, img = pcall(v.toRGBA, v, f)
if not rok or type(img) ~= "table" then
return nil, Err.new(Err.CORRUPT, "frame " .. tostring(f.index) .. " did not convert to rgba8")
end
table.insert(images, img)
table.insert(delays, delay)
end
end
if #images == 0 then
return nil, Err.new(Err.ARG, "save: the video has no frames")
end
local img, ierr = ImageOps.fromFrames(images, delays)
if not img then
return nil, ierr
end
img.loops = if type(v.loops) == "number" then v.loops else 0
img.meta.source = { format = v.format }
return img, nil
end
-- renderMusic(value, opts) -> the audio Decoder of a Module (codecs/audio/Tracker) or a Midi (music/midi render) |
-- nil, Err. opts are the render options: sampleRate, soundfont, gain, tail, interpolation, stereoSeparation, ...
local function renderMusic(value: any, o: Options): (any, Err.Err?)
local ok, dec, err
if value.kind == "midi" then
local m, merr = F.format("midi")
if not m then
return nil, merr
end
ok, dec, err = pcall(m.render, value, o)
else
local Tracker: any = require("@formats/codecs/audio/Tracker")
ok, dec, err = pcall(Tracker.render, value, o)
end
if not ok then
return nil, Err.new(Err.ARG, "render threw: " .. tostring(dec))
end
if dec == nil then
return nil, err or Err.new(Err.ARG, "render failed")
end
return dec, nil
end
-- the writable format for a value: opts.format, the destination's extension, or the source format
local function encoderFor(value: any, dest: string?, o: Options): (Registry.Entry?, any, Err.Err?, string?)
local category = o.category
@@ -427,6 +531,12 @@ local function encoderFor(value: any, dest: string?, o: Options): (Registry.Entr
if not entry and category == "image" and type(value) == "table" and value.kind == "image" then
entry = find(key, "video") -- an (animated) image written as a video: F.convert(gif, "avi")
end
if not entry and category == "video" and type(value) == "table" and value.kind == "video" then
entry = find(key, "image") -- a video written as an animated image (F.save): F.convert(avi, "gif")
end
if not entry and category == "music" and type(value) == "table" and (value.kind == "music" or value.kind == "midi") then
entry = find(key, "audio") -- a Module / Midi saved as audio is rendered first (F.save): F.convert(mod, "wav")
end
if not entry then
return nil, nil, Err.new(Err.UNKNOWN_FORMAT, "no format named " .. key .. (if category then " for " .. category else ""))
end
@@ -464,8 +574,10 @@ function encodeTextures(scene: any): Err.Err?
return nil
end
-- save(value, dest?, opts?) -> buffer (no dest) | true (written to dest) | nil, Err
function F.save(value: any, dest: string?, opts: Options?): (any, Err.Err?)
-- save(value, dest?, opts?) -> buffer (no dest) | true (written to dest) | nil, Err. Formats that write extra files
-- (caps.extraFiles: bmfont pages) return them without a dest as a third value { [file name] = bytes } (or fill the
-- caller's opts.files); with a dest they are written next to it.
function F.save(value: any, dest: string?, opts: Options?): (any, Err.Err?, { [string]: any }?)
local o: Options = opts or {}
local entry, m, err, key = encoderFor(value, dest, o)
if not entry then
@@ -480,6 +592,8 @@ function F.save(value: any, dest: string?, opts: Options?): (any, Err.Err?)
if o.files == nil then
o.files = {}
end
elseif o.files == nil and entry.caps.extraFiles then
o.files = {} -- returned as the third value: F.save(ttf, nil, { format = "bmfont" }) -> fnt, nil, pages
end
if entry.category == "video" and type(value) == "table" and value.kind == "image" then
local v, verr = F.video.fromImage(value)
@@ -488,6 +602,20 @@ function F.save(value: any, dest: string?, opts: Options?): (any, Err.Err?)
end
value = v
end
if entry.category == "image" and type(value) == "table" and value.kind == "video" then
local img, ierr = videoToImage(value, o)
if not img then
return nil, ierr
end
value = img
end
if entry.category == "audio" and type(value) == "table" and (value.kind == "music" or value.kind == "midi") then
local dec, rerr = renderMusic(value, o) -- the render options (sampleRate, soundfont, loop, ...) come from opts
if not dec then
return nil, rerr
end
value = dec
end
if type(value) == "table" and value.kind == "scene" then
local terr = encodeTextures(value)
if terr then
@@ -517,6 +645,9 @@ function F.save(value: any, dest: string?, opts: Options?): (any, Err.Err?)
end
return true
end
if type(o.files) == "table" and o.files ~= (opts and opts.files) and next(o.files) ~= nil then
return buf, nil, o.files
end
return buf
end
@@ -530,7 +661,7 @@ function F.saveString(value: any, opts: Options?): (string?, Err.Err?)
end
-- convert(src, format, opts?) -> buffer (or true with opts.out = a path) | nil, Err. opts go to both load and save.
function F.convert(src: any, format: string, opts: Options?): (any, Err.Err?)
function F.convert(src: any, format: string, opts: Options?): (any, Err.Err?, { [string]: any }?)
local o: Options = opts or {}
local value, err = F.load(src, o)
if value == nil then
@@ -553,6 +684,14 @@ function F.detect(src: any, opts: Options?): (string?, Err.Err?)
if not entry then
return nil, err
end
-- a format may name the file's flavour more precisely (video/mkv: "webm" for DocType webm)
local m = module(entry)
if m and type(m.variant) == "function" then
local ok, v = pcall(m.variant, Bytes.reader(buf))
if ok and type(v) == "string" then
return v, nil
end
end
return entry.name, nil
end
@@ -628,6 +767,14 @@ for _, fn in { "toBodies", "fromBodies", "tessellate" } do
return a, b
end
end
-- F.music.new / addPattern / setCell / getCell / addSample / addInstrument / note: building and editing tracker
-- Modules (ops/MusicOps, loaded on first use): F.music.new({ format = "mod", channels = 4 }) -> Module
for _, fn in { "new", "addPattern", "setCell", "getCell", "addSample", "addInstrument", "note" } do
local name: string = fn
F.music[name] = function(...: any): (any, Err.Err?)
return (F.ops.music :: any)[name](...)
end
end
-- F.video.fromImage(img, opts?) -> a frames-backed VideoDecoder: an animated GIF / APNG / WebP (img.frames, composited
-- onto full canvases by ImageOps.coalesce) or a still image (one frame). opts.rgba is accepted (frames are rgba8).
F.video.fromImage = function(img: any, opts: Options?): (any, Err.Err?)
@@ -699,6 +846,7 @@ end
-- F.ops.layers ops/LayerOps flatten / flattenFrames a Layered (PSD, Aseprite), leaves
-- F.ops.audio ops/AudioOps toAudio, gain / gainDb / normalize, mix, remix / downmix / upmix, resample, trim,
-- concat, fade, peak, rms
-- F.ops.music ops/MusicOps new, addPattern, setCell / getCell, addSample, addInstrument, note (also on F.music)
-- F.ops.vector.rasterize(vector, w?, h?, opts?) -> rgba8 Image: the SVG renderer (vector/svg) for any Vector
local OPS: { [string]: () -> any } = {
image = function()
@@ -713,6 +861,9 @@ local OPS: { [string]: () -> any } = {
audio = function()
return require("@formats/ops/AudioOps")
end,
music = function()
return require("@formats/ops/MusicOps")
end,
vector = function()
return {
rasterize = function(v: any, w: number?, h: number?, opts: any?): (any, Err.Err?)