-- codecs/image/Jpeg tests (Motion-JPEG; still images are in tests/image/jpeg.luau): ffmpeg mjpeg frames, the encoder. -- Fixtures: tools/fixtures/mjpeg.py. local T = require("@tests/T") local Jpeg = require("@rosetta/codecs/image/Jpeg") local Crc32 = require("@rosetta/core/Crc32") local Err = require("@rosetta/core/Err") local Types = require("@rosetta/Types") local Base64 = require("@rosetta/core/Base64") local Deflate = require("@rosetta/codecs/compress/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 local function psnr(a: buffer, ao: number, b: buffer, bo: number, n: number): number local se = 0 for i = 0, n - 1 do local d = buffer.readu8(a, ao + i) - buffer.readu8(b, bo + i) se += d * d end if se == 0 then return 99 end return 10 * math.log10(255 * 255 * n / se) end local function psnrRgb(a: buffer, b: buffer, n: number): number local se = 0 for i = 0, n - 1 do for k = 0, 2 do local d = buffer.readu8(a, i * 4 + k) - buffer.readu8(b, i * 4 + k) se += d * d end end if se == 0 then return 99 end return 10 * math.log10(255 * 255 * n * 3 / se) end local function hasDht(s: string): boolean local at = string.find(s, "\255\196", 1, true) local sos = string.find(s, "\255\218", 1, true) return at ~= nil and sos ~= nil and at < sos end -- yuv420p bit-exact with TurboJPEG's planes, rgba8 bit-exact with djpeg, PSNR against ffmpeg's decode do local frames, worstY, worstC, worstRgb = 0, 99, 99, 99 for _, s in Fx.streams do local w, h = s.width, s.height local cw, ch = (w + 1) // 2, (h + 1) // 2 for i, f in s.frames do local label = string.format("%s frame %d", s.name, i) local b = buffer.fromstring(f.data) local img, err = Jpeg.decode(b, 0, buffer.len(b), { yuv = true }) t:check(img, label .. ": " .. tostring(err)) t:check(img.format == "yuv420p" and img.width == w and img.height == h, label .. ": a yuv420p " .. w .. "x" .. h .. " Image") t:eq(buffer.len(img.pixels), w * h + 2 * cw * ch, label .. ": plane sizes") t:check(img.meta.source.yuv == s.sampling and img.meta.source.range == "full", label .. ": meta.source.yuv " .. tostring(img.meta.source.yuv)) t:check(not img.meta.truncated, label .. ": not truncated") t:eq(Crc32.of(img.pixels), f.yuv, label .. ": yuv420p planes = TurboJPEG's") local rgba = Jpeg.decode(b) :: Types.Image 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 = 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 t:check(py > 55, string.format("%s: luma PSNR %.1f dB against ffmpeg", s.name, py)) 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, 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) end end if string.find(s.name, "nodht") then t:check(not hasDht(f.data), label .. ": no DHT segment") end if s.name == "avi1_junk" then t:eq(img.meta.source.avi1, 0, label .. ": AVI1 polarity") t:eq(Jpeg.info(b).avi1, 0, label .. ": info.avi1") end frames += 1 end end t:note("%d ffmpeg mjpeg frames bit-exact (yuv420p: TurboJPEG, rgba8: djpeg); worst PSNR against ffmpeg: luma %.1f, chroma %.1f, rgba %.1f dB", frames, worstY, worstC, worstRgb) end -- an interlaced frame: two fields (AVI1 polarity 1) woven, field 1 on the even rows do local F = Fx.fields local b = buffer.fromstring(F.data) local img = Jpeg.decode(b, 0, nil, { yuv = true }) :: Types.Image 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 = 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 t:check(rgba.height == F.height and Crc32.of(rgba.pixels) == F.rgba, "fields: woven rgba8 = djpeg's fields woven") local one = Jpeg.decode(b, 0, nil, { yuv = true, fields = false }) :: Types.Image t:check(one.height == F.height // 2 and one.meta.source.fields == 1, "fields = false: the first field only") -- the first field alone (no second JPEG): a half-height frame, not an error local first = string.find(F.data, "\255\217", 1, true) :: number local half = Jpeg.decode(buffer.fromstring(string.sub(F.data, 1, first + 1)), 0, nil, { yuv = true }) :: Types.Image t:check(half and half.height == F.height // 2 and half.meta.source.fields == 1, "a missing second field leaves the first") t:check(Jpeg.info(b).avi1 == 1, "fields: info.avi1") end -- the yuv420p encoder: byte-identical with TurboJPEG's tj3CompressFromYUV8 do local exact = 0 for _, c in Enc do local img = Types.Image.new(c.width, c.height, "yuv420p") 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) if s ~= c.want then local at = 0 for i = 1, math.min(#s, #c.want) do if string.byte(s, i) ~= string.byte(c.want, i) then at = i break end end t:check(false, string.format("%s: differs from tj3CompressFromYUV8 at byte %d (ours %d bytes, TurboJPEG %d)", c.name, at, #s, #c.want)) end exact += 1 local back = Jpeg.decode(out :: buffer) :: Types.Image t:eq(Crc32.of(back.pixels), c.rgba, c.name .. ": our decode of it = djpeg's") local floor = if c.quality >= 75 then 30 else 26 if c.width * c.height >= 1000 then t:check(c.ffpsnr > floor, string.format("%s: ffmpeg reads it at %.1f dB luma PSNR", c.name, c.ffpsnr)) local y = Jpeg.decode(out :: buffer, 0, nil, { yuv = true }) :: Types.Image local p = psnr(y.pixels, 0, img.pixels, 0, c.width * c.height) t:check(p > floor, string.format("%s: yuv round trip %.1f dB", c.name, p)) end end t:note("%d yuv420p encodes byte-identical with tj3CompressFromYUV8", exact) -- grey from yuv420p: the luma plane only local c = Enc[1] local img = Types.Image.new(c.width, c.height, "yuv420p") 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") local gy = Jpeg.decode(g, 0, nil, { yuv = true }) :: Types.Image t:check(gy.format == "yuv420p" and gy.meta.source.yuv == "gray" and buffer.readu8(gy.pixels, c.width * c.height) == 128, "grey decodes to yuv420p with chroma 128") local p = Jpeg.encode(img, { quality = 80, progressive = true, restart = 2 }) :: buffer local pi = Jpeg.info(p) t:check(pi and pi.progressive and pi.sampling[1][1] == 2 and pi.sampling[1][2] == 2, "yuv420p progressive") local pd = Jpeg.decode(p, 0, nil, { yuv = true }) :: Types.Image t:check(psnr(pd.pixels, 0, img.pixels, 0, c.width * c.height) > 30, "yuv420p progressive round trip") local s4 = Jpeg.encode(img, { subsampling = "444" }) :: buffer t:eq(Jpeg.info(s4).sampling[1][1], 2, "yuv420p input is always 4:2:0") -- extra segments and density local seg = Jpeg.encode(img, { segments = { { marker = 0xFE, data = buffer.fromstring("hello") } }, density = { unit = 1, x = 72, y = 72 } }) :: buffer local sd = Jpeg.decode(seg) :: Types.Image t:check(sd.meta.text and sd.meta.text.comment == "hello" and sd.meta.dpi and sd.meta.dpi.x == 72, "segments and density") end do local s = Fx.streams[1] local data = s.frames[1].data local big = buffer.create(#data + 30) buffer.writestring(big, 17, data) local a = Jpeg.decode(big, 17, #data, { yuv = true }) :: Types.Image t:eq(Crc32.of(a.pixels), s.frames[1].yuv, "decode from a sub-range") local i = Jpeg.info(big, 17, #data) t:check(i and i.width == s.width and i.colorSpace == "ycbcr" and i.sampling[1][2] == 2, "info from a sub-range") local sc = Jpeg.decode(buffer.fromstring(data), 0, nil, { yuv = true, scale = 2 }) :: Types.Image t:check(sc.format == "rgba8" and sc.width == s.width // 2, "yuv with scale 2 falls back to rgba8") local _, e1 = Jpeg.decode("nope" :: any) t:check(e1 and e1.code == Err.ARG, "decode(string)") local _, e2 = Jpeg.decode(buffer.fromstring("nope")) t:check(e2 and e2.code == Err.CORRUPT, "junk") local _, e3 = Jpeg.decode(buffer.fromstring(data), 0, nil, { maxPixels = 100, yuv = true }) t:check(e3 and e3.code == Err.LIMIT, "maxPixels") local _, e4 = Jpeg.decode(buffer.fromstring(data), 0, nil, 5 :: any) t:check(e4 and e4.code == Err.ARG, "non-table options") local _, e5 = Jpeg.encode(Types.Image.new(4, 4, "rgba16")) t:check(e5 and e5.code == Err.ARG, "rgba16 is refused") local short = Types.Image.new(8, 8, "yuv420p") short.pixels = buffer.create(70) local _, e6 = Jpeg.encode(short) t:check(e6 and e6.code == Err.ARG, "a short yuv420p buffer") local _, e7 = Jpeg.encode(Types.Image.new(8, 8), { segments = { { marker = 0xC4, data = buffer.create(1) } } }) t:check(e7 and e7.code == Err.ARG, "a bad segment marker") local _, e8 = Jpeg.info(buffer.create(0)) t:check(e8 and Err.is(e8), "info of nothing") -- truncated frames decode as far as they go or fail with an Err local cuts, partial = 0, 0 for cut = 0, #data - 1, #data // 37 do local img, err = Jpeg.decode(buffer.fromstring(string.sub(data, 1, cut)), 0, nil, { yuv = true }) t:check(img or Err.is(err), "cut at " .. cut .. ": no image and no Err") if img then partial += 1 t:check(img.meta.truncated, "cut at " .. cut .. ": flagged truncated") end cuts += 1 end t:note("%d truncated frames, %d decoded partially", cuts, partial) end -- speed (a note; tools/bench.luau has the real numbers) do local s = Fx.streams[1] local frames = {} for _, f in s.frames do table.insert(frames, buffer.fromstring(f.data)) end local t0, n = os.clock(), 0 while os.clock() - t0 < 0.3 do for _, b in frames do Jpeg.decode(b, 0, nil, { yuv = true }) n += 1 end end t:note("%dx%d yuv420p decode: %.0f fps", s.width, s.height, n / (os.clock() - t0)) end do local samples = {} for k, s in Fx.streams do if k % 2 == 1 then table.insert(samples, s.frames[1].data) end end t:fuzz("decode yuv", function(b: buffer) return Jpeg.decode(b, 0, nil, { yuv = true }) end, samples, { mutations = 4, keepHeader = 2 }) t:fuzz("decode fields", function(b: buffer) return Jpeg.decode(b, 0, nil, { fields = true }) end, { Fx.fields.data }, { mutations = 3, garbage = 10 }) end do -- opts.yield: the cooperative hook runs (scans, IDCT, colour), output unchanged, inside a coroutine, errors ignored local s = Fx.streams[1] local f = s.frames[1] local b = buffer.fromstring(f.data) local calls = 0 local img = Jpeg.decode(b, 0, nil, { yield = function() calls += 1 end }) :: Types.Image t:check(img and calls > 0, "yield hook runs (" .. calls .. " calls)") t:eq(Crc32.of(img.pixels), f.rgba, "yield: rgba8 unchanged") local yuv = Jpeg.decode(b, 0, nil, { yuv = true, yield = function() end }) :: Types.Image t:eq(Crc32.of(yuv.pixels), f.yuv, "yield: yuv420p unchanged") local resumes = 0 local co = coroutine.create(function() return Jpeg.decode(b, 0, nil, { yield = coroutine.yield }) end) local ok, res = coroutine.resume(co) while ok and coroutine.status(co) == "suspended" do resumes += 1 ok, res = coroutine.resume(co) end t:check(ok and resumes > 1 and res ~= nil, "a decode suspended at the hook resumes") t:eq(res and Crc32.of((res :: any).pixels), f.rgba, "suspended decode: same pixels") local e = Jpeg.decode(b, 0, nil, { yield = function() error("hook error") end }) :: Types.Image t:eq(e and Crc32.of(e.pixels), f.rgba, "an erroring hook is ignored") end t:done()