merge master into ws/videoctl

This commit is contained in:
4DBug committed 2026-11-02 22:17:23 -06:00
commit f7c8abc1b4
24 files changed
+16704 -1565

No files matched your search

File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
+14 -19
View File
@@ -5,7 +5,7 @@
-- libjpeg's islow (jidctint.c / jfdctint.c), the reduced IDCTs (jidctred.c) and jcdctmgr.c's rounding quantiser, bit-exact
-- with libjpeg-turbo. From jpeg-luau 1.0 (same author); IJG code: licenses/libjpeg-turbo-LICENSE.md.
local readi16, readu16, writeu16, writeu32, readu8, writei16 = buffer.readi16, buffer.readu16, buffer.writeu16, buffer.writeu32, buffer.readu8, buffer.writei16
local clamp = math.clamp
local clamp, abs, sign = math.clamp, math.abs, math.sign
local Dct = {}
@@ -384,18 +384,13 @@ end
local fw = table.create(64, 0)
local function quant(coef: buffer, off: number, div: buffer, v: number, i: number)
local qv = readu16(div, i * 2) * 8
local h = qv // 2
if v < 0 then
writei16(coef, off + i * 2, -((h - v) // qv))
else
writei16(coef, off + i * 2, (v + h) // qv)
end
-- (|v| + q * 4) // (q * 8) with v's sign, branch-free (a sign branch here gave LuaJIT side traces it could not compile)
local function quant(coef: buffer, off: number, qv: number, v: number, i: number)
writei16(coef, off + i * 2, (abs(v) + qv / 2) // qv * sign(v))
end
-- quantised as jcdctmgr.c: (|v| + q * 4) // (q * 8) with the sign restored
function Dct.fdctQuantize(src: buffer, stride: number, x: number, y: number, w: number, h: number, div: buffer, coef: buffer, off: number)
-- quantised as jcdctmgr.c: (|v| + q * 4) // (q * 8) with the sign restored; div[i + 1] = q * 8 for natural index i
function Dct.fdctQuantize(src: buffer, stride: number, x: number, y: number, w: number, h: number, div: { number }, coef: buffer, off: number)
local lastX, lastY = w - 1, h - 1
for r = 0, 7 do
local row = math.min(y + r, lastY) * stride
@@ -484,14 +479,14 @@ function Dct.fdctQuantize(src: buffer, stride: number, x: number, y: number, w:
local o3 = (tmp6 + z2 + z3 + 16384) // 32768
local o1 = (tmp7 + z1 + z4 + 16384) // 32768
local cc = c - 1
quant(coef, off, div, o0, cc)
quant(coef, off, div, o1, cc + 8)
quant(coef, off, div, o2, cc + 16)
quant(coef, off, div, o3, cc + 24)
quant(coef, off, div, o4, cc + 32)
quant(coef, off, div, o5, cc + 40)
quant(coef, off, div, o6, cc + 48)
quant(coef, off, div, o7, cc + 56)
quant(coef, off, div[c], o0, cc)
quant(coef, off, div[c + 8], o1, cc + 8)
quant(coef, off, div[c + 16], o2, cc + 16)
quant(coef, off, div[c + 24], o3, cc + 24)
quant(coef, off, div[c + 32], o4, cc + 32)
quant(coef, off, div[c + 40], o5, cc + 40)
quant(coef, off, div[c + 48], o6, cc + 48)
quant(coef, off, div[c + 56], o7, cc + 56)
end
end
+3 -3
View File
@@ -595,11 +595,11 @@ function Encode.run(px: buffer, w: number, h: number, bpp: number, o: Options):
local qt = { Tables.quant(Tables.LUMA, o.quality), Tables.quant(Tables.CHROMA, o.quality) }
local qdiv = {}
for t = 1, 2 do
local b = buffer.create(128)
local d = table.create(64, 0)
for i = 1, 64 do
writeu16(b, (i - 1) * 2, qt[t][i])
d[i] = qt[t][i] * 8
end
qdiv[t] = b
qdiv[t] = d
end
local comps: { Comp } = {}
+77 -44
View File
@@ -78,6 +78,56 @@ end
local LATE: { [string]: number } = { br = 0.3 } -- archive/br: a trial decode of up to 256 KB
-- detection order: by the cost of a sniff (Registry.sniff: Sniffs.luau, Sniffs.luau reaching a codec, the format
-- module), then registry order; the costly LATE sniffs last
local order: { number }? = nil
local byExt: { [string]: { number } } = {}
local Sniffs: { [string]: (any) -> number }? = nil
local function sniffOrder(): { number }
local o = order
if o then
return o
end
local list: { number } = {}
for idx, e in Registry do
table.insert(list, idx)
for _, x in e.extensions do
byExt[x] = byExt[x] or {}
table.insert(byExt[x], idx)
end
end
local function rank(idx: number): number
local e = Registry[idx]
return if LATE[e.name] then 3 else e.sniff
end
table.sort(list, function(a: number, b: number): boolean
local ra, rb = rank(a), rank(b)
if ra ~= rb then
return ra < rb
end
return a < b
end)
order = list
return list
end
local function sniffer(e: Registry.Entry): ((any) -> number)?
if e.sniff < 2 then
local s = Sniffs
if not s then
s = require("@rosetta/Sniffs") :: any
Sniffs = s
end
local f = (s :: { [string]: (any) -> number })[e.path]
if f then
return f
end
end
local m = module(e)
return if m and type(m.sniff) == "function" then m.sniff else nil
end
local function detect(buf: buffer, name: string?, category: string?, format: string?): (Registry.Entry?, Err.Err?)
if format then
local e = find(format, category)
@@ -87,52 +137,35 @@ local function detect(buf: buffer, name: string?, category: string?, format: str
return e, nil
end
local ext = Input.extension(name)
local list = sniffOrder()
local hits = if ext then byExt[ext] else nil
local best: Registry.Entry? = nil
local bestScore, bestIdx = 0, 0
-- sniffs score 0..1 (core/Sniff); the extension's format scoring 1 cannot be beaten. Formats with a costly sniff
-- (LATE: the most they score) run last, and only when they could still win; ties go to registry order.
local late: { number }? = nil
for idx, e in Registry do
if inCategory(e, category) and (e.caps.decode or e.caps.open) then
local cap = LATE[e.name]
if cap then
late = late or {}
table.insert(late :: { number }, idx)
continue
end
local m = module(e)
if m and m.sniff then
local ok, score = pcall(m.sniff, Bytes.reader(buf))
if ok and type(score) == "number" and score > 0 then
local extHit = ext and table.find(e.extensions, ext)
if extHit then
score += 0.01 -- ties go to the extension
end
if score > bestScore then
best, bestScore, bestIdx = e, score, idx
if extHit and score >= 1.01 then
return e, nil
end
end
-- sniffs score 0..1 (core/Sniff), +0.01 for the extension's formats; the best wins, ties go to registry order. The
-- extension's formats run first, and a sniff runs only while its cap (1, or LATE) could still win.
for pass = 1, 2 do
local ids = if pass == 1 then hits else list
if ids then
for _, idx in ids do
if pass == 2 and bestScore > 1 then
break
end
end
end
end
if late then
for _, idx in late do
local e = Registry[idx]
local extHit = ext and table.find(e.extensions, ext)
local cap = LATE[e.name] + (if extHit then 0.01 else 0)
if cap > bestScore or (cap == bestScore and idx < bestIdx) then
local m = module(e)
if m and m.sniff then
local ok, score = pcall(m.sniff, Bytes.reader(buf))
if ok and type(score) == "number" and score > 0 then
if extHit then
score += 0.01
end
if score > bestScore or (score == bestScore and idx < bestIdx) then
best, bestScore, bestIdx = e, score, idx
local e = Registry[idx]
local extHit = pass == 1 or (hits ~= nil and table.find(hits, idx) ~= nil)
if (pass == 1 or not extHit) and inCategory(e, category) and (e.caps.decode or e.caps.open) then
local cap = (LATE[e.name] or 1) + (if extHit then 0.01 else 0)
if cap > bestScore or (cap == bestScore and idx < bestIdx) then
local sniff = sniffer(e)
if sniff then
local ok, score = pcall(sniff, Bytes.reader(buf))
if ok and type(score) == "number" and score > 0 then
if extHit then
score += 0.01
end
if score > bestScore or (score == bestScore and idx < bestIdx) then
best, bestScore, bestIdx = e, score, idx
end
end
end
end
end
@@ -696,7 +729,7 @@ function F.detect(src: any, opts: Options?): (string?, Err.Err?)
return nil, err
end
-- a format may name the file's flavour more precisely (video/mkv: "webm" for DocType webm)
local m = module(entry)
local m = if entry.variant then module(entry) else nil
if m and type(m.variant) == "function" then
local ok, v = pcall(m.variant, Bytes.reader(buf))
if ok and type(v) == "string" then