ws-video-codecs: mjpeg fixtures as base64 / zlib (8.6 -> 1.0 MB); codecs/Jpeg: no type assertion as an assignment target (darklua)

This commit is contained in:
4DBug committed 2026-10-31 09:42:30 -05:00
1 parent 2493b9db36
commit 87d22bf67a
3 files changed
+51 -17

No files matched your search

+28 -5
View File
@@ -9,11 +9,34 @@ local Jpeg = require("@formats/codecs/Jpeg")
local Crc32 = require("@formats/core/Crc32")
local Err = require("@formats/core/Err")
local Types = require("@formats/Types")
local Base64 = require("@formats/core/Base64")
local Deflate = require("@formats/codecs/Deflate")
local Fx = require("@tests/gen/mjpeg_cases")
local Enc = require("@tests/gen/mjpeg_enc")
local t = T.suite("codecs/jpeg")
-- the fixtures' binary fields: base64, and base64 of zlib data for raw planes / pixels
local function b64(s: string): string
return buffer.tostring(Base64.decode(s) :: buffer)
end
local function z64(s: string): buffer
return Deflate.zlib(Base64.decode(s) :: buffer) :: buffer
end
for _, s in Fx.streams do
for _, f in s.frames do
f.data = b64(f.data)
end
s.ff.yuv = z64(s.ff.yuv)
if s.ff.rgba then
s.ff.rgba = z64(s.ff.rgba)
end
end
Fx.fields.data = b64(Fx.fields.data)
for _, c in Enc do
c.want = b64(c.want)
end
-- PSNR of n bytes of a at ao against b at bo
local function psnr(a: buffer, ao: number, b: buffer, bo: number, n: number): number
local se = 0
@@ -68,7 +91,7 @@ do
t:check(rgba and rgba.format == "rgba8", label .. ": rgba8")
t:eq(Crc32.of(rgba.pixels), f.rgba, label .. ": rgba8 = djpeg")
if i == 1 then
local ref = buffer.fromstring(s.ff.yuv)
local ref = s.ff.yuv
local py = psnr(img.pixels, 0, ref, 0, w * h)
local pc = math.min(psnr(img.pixels, w * h, ref, w * h, cw * ch), psnr(img.pixels, w * h + cw * ch, ref, w * h + cw * ch, cw * ch))
-- ffmpeg's IDCT differs from libjpeg's islow by rounding only; its 4:2:2 / 4:4:4 -> 4:2:0 is a filter
@@ -76,7 +99,7 @@ do
t:check(pc > (if s.sampling == "420" then 55 else 20), string.format("%s: chroma PSNR %.1f dB against ffmpeg", s.name, pc))
worstY, worstC = math.min(worstY, py), math.min(worstC, pc)
if s.ff.rgba then
local pr = psnrRgb(rgba.pixels, buffer.fromstring(s.ff.rgba), w * h)
local pr = psnrRgb(rgba.pixels, s.ff.rgba, w * h)
-- swscale upsamples chroma differently from libjpeg (fancy): testsrc2's sharp colour edges cost ~10 dB
t:check(pr > 25, string.format("%s: rgba PSNR %.1f dB against ffmpeg", s.name, pr))
worstRgb = math.min(worstRgb, pr)
@@ -103,7 +126,7 @@ do
t:check(img and img.width == F.width and img.height == F.height and img.format == "yuv420p", "fields: one 320x240 yuv420p frame")
t:eq(img.meta.source.fields, 2, "fields: meta.source.fields")
t:eq(Crc32.of(img.pixels), F.yuv, "fields: woven yuv420p planes = TurboJPEG's fields woven")
local src = buffer.fromstring(F.src)
local src = z64(F.src)
local p = psnr(img.pixels, 0, src, 0, F.width * F.height)
t:check(p > 35, string.format("fields: luma PSNR %.1f dB against the source frame", p))
local rgba = Jpeg.decode(b) :: Types.Image
@@ -122,7 +145,7 @@ do
local exact = 0
for _, c in Enc do
local img = Types.Image.new(c.width, c.height, "yuv420p")
img.pixels = buffer.fromstring(c.yuv)
img.pixels = z64(c.yuv)
local out, err = Jpeg.encode(img, { quality = c.quality })
t:check(out, c.name .. ": " .. tostring(err))
local s = buffer.tostring(out :: buffer)
@@ -151,7 +174,7 @@ do
-- grey from yuv420p: the luma plane only
local c = Enc[1]
local img = Types.Image.new(c.width, c.height, "yuv420p")
img.pixels = buffer.fromstring(c.yuv)
img.pixels = z64(c.yuv)
local g = Jpeg.encode(img, { quality = 80, gray = true }) :: buffer
local gi = Jpeg.info(g)
t:check(gi and gi.components == 1, "yuv420p with gray = true: one component")