From 6e3a044556672d2b9f7bdd1cd70b89e8080c8641 Mon Sep 17 00:00:00 2001 From: 4DBug Date: Sat, 31 Oct 2026 09:42:30 -0500 Subject: [PATCH] ws-video-codecs: mjpeg fixtures as base64 / zlib (8.6 -> 1.0 MB); codecs/Jpeg: no type assertion as an assignment target (darklua) --- src/Formats/codecs/Jpeg/init.luau | 3 ++- tests/codecs/jpeg.luau | 33 ++++++++++++++++++++++++++----- tools/fixtures/mjpeg.py | 32 +++++++++++++++++++----------- 3 files changed, 51 insertions(+), 17 deletions(-) diff --git a/src/Formats/codecs/Jpeg/init.luau b/src/Formats/codecs/Jpeg/init.luau index ef3323d..3163222 100644 --- a/src/Formats/codecs/Jpeg/init.luau +++ b/src/Formats/codecs/Jpeg/init.luau @@ -296,7 +296,8 @@ function Jpeg.decode(buf: buffer, off: number?, len: number?, opts: DecodeOption local truncated = img.meta.truncated or b.meta.truncated img = weave(img, b) img.meta.truncated = truncated - (img.meta.source :: any).fields = 2 + local source: any = img.meta.source + source.fields = 2 end end end diff --git a/tests/codecs/jpeg.luau b/tests/codecs/jpeg.luau index 9542a11..5b42f8d 100644 --- a/tests/codecs/jpeg.luau +++ b/tests/codecs/jpeg.luau @@ -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") diff --git a/tools/fixtures/mjpeg.py b/tools/fixtures/mjpeg.py index 63c0eda..258ca8a 100644 --- a/tools/fixtures/mjpeg.py +++ b/tools/fixtures/mjpeg.py @@ -13,12 +13,22 @@ # ff = { yuv, rgba? } } }, fields = { data, width, height, yuv, rgba, src } } # tests/gen/mjpeg_enc.luau yuv420p sources and what tj3CompressFromYUV8 writes for them at a quality (our # encoder must write the same bytes), with djpeg's CRC and ffmpeg's PSNR of that file -# tests/gen/mjpeg_bench.luau 320x240 and 640x480 frames (4:2:0, 4:2:2) and a 640x480 yuv420p source -import ctypes, math, os, shutil, subprocess, tempfile, zlib +# tests/gen/mjpeg_bench.luau 320x240 and 640x480 frames (4:2:0, 4:2:2) and yuv420p sources +# Binary fields are base64 (JPEG data) or base64 of zlib data (raw planes / pixels, inflated with codecs/Deflate): +# Luau string literals of binary data cost ~3.7 bytes per byte, base64 1.33. +import base64, ctypes, math, os, shutil, subprocess, tempfile, zlib FF = ["-hide_banner", "-loglevel", "error", "-y"] +def b64(b): + return base64.b64encode(b).decode() + + +def z64(b): + return base64.b64encode(zlib.compress(b, 9)).decode() + + def tool(ctx, pkg, name): p = shutil.which(name) if p: @@ -206,10 +216,10 @@ def _generate(ctx, work): for fr in frames: ref = fr data = mutate(fr) if mutate else fr - entries.append({"data": data, "yuv": zlib.crc32(tj.yuv420(ref)), "rgba": rgba_crc(ref)}) - ref = {"yuv": ffdecode(frames[0], "yuvj420p")} + entries.append({"data": b64(data), "yuv": zlib.crc32(tj.yuv420(ref)), "rgba": rgba_crc(ref)}) + ref = {"yuv": z64(ffdecode(frames[0], "yuvj420p"))} if w * h <= 30000 or name == "testsrc_420": # keep tests/gen small: rgba for the small ones and one 320x240 - ref["rgba"] = ffdecode(frames[0], "rgba") + ref["rgba"] = z64(ffdecode(frames[0], "rgba")) streams.append({"name": name, "width": w, "height": h, "sampling": pix[4:7], "frames": entries, "ff": ref}) stream("testsrc_420", "testsrc2", 320, 240, "yuvj420p", 4, ("-q:v", "3")) @@ -267,8 +277,8 @@ def _generate(ctx, work): return rgba_of_pnm(subprocess.run([djpeg, "-dct", "int", "-pnm", p], check=True, capture_output=True).stdout) wr = weave(rgba_raw(fields[0]), rgba_raw(fields[1]), fw, hf, 4) - ctx.emit("mjpeg_cases", {"streams": streams, "fields": {"data": data, "width": fw, "height": fh, - "yuv": zlib.crc32(wy), "rgba": zlib.crc32(wr), "src": fy}}) + ctx.emit("mjpeg_cases", {"streams": streams, "fields": {"data": b64(data), "width": fw, "height": fh, + "yuv": zlib.crc32(wy), "rgba": zlib.crc32(wr), "src": z64(fy)}}) # the encoder: yuv420p sources, byte-identical with tj3CompressFromYUV8 enc = [] @@ -289,8 +299,8 @@ def _generate(ctx, work): if got: se = sum((a - b) ** 2 for a, b in zip(got[:w * h], raw[:w * h])) psnr = 99.0 if se == 0 else 10 * math.log10(255 * 255 * w * h / se) - enc.append({"name": "%s_q%d" % (name, q), "width": w, "height": h, "quality": q, "yuv": raw, - "want": want, "rgba": rgba_crc(want), "ffpsnr": round(psnr, 3)}) + enc.append({"name": "%s_q%d" % (name, q), "width": w, "height": h, "quality": q, "yuv": z64(raw), + "want": b64(want), "rgba": rgba_crc(want), "ffpsnr": round(psnr, 3)}) ctx.emit("mjpeg_enc", enc) bench = [] @@ -299,9 +309,9 @@ def _generate(ctx, work): o = W("bench.mjpeg") ff(*source("testsrc2", w, h, 1), "-pix_fmt", pix, "-c:v", "mjpeg", "-q:v", "3", "-f", "mjpeg", o) fr = split(open(o, "rb").read())[0] - bench.append({"name": name, "width": w, "height": h, "data": fr, "yuv": zlib.crc32(tj.yuv420(fr))}) + bench.append({"name": name, "width": w, "height": h, "data": b64(fr), "yuv": zlib.crc32(tj.yuv420(fr))}) srcs = [] for w, h in ((320, 240), (640, 480)): ff(*source("testsrc2", w, h, 1), "-pix_fmt", "yuv420p", "-f", "rawvideo", W("b.yuv")) - srcs.append({"width": w, "height": h, "yuv": open(W("b.yuv"), "rb").read()}) + srcs.append({"width": w, "height": h, "yuv": z64(open(W("b.yuv"), "rb").read())}) ctx.emit("mjpeg_bench", {"frames": bench, "sources": srcs})