From 5585ce53cef2d020ecb364fb70ea00c261831c10 Mon Sep 17 00:00:00 2001 From: 4DBug Date: Sat, 31 Oct 2026 14:30:45 -0500 Subject: [PATCH] ws-video2: MPEG-1 tests bit-exact against ffmpeg -idct int (308 of 312 frames; 4 with concealed macroblocks) --- tests/codecs/mpeg1.luau | 27 +++++++++++++++++++++++++-- tools/fixtures/mpeg1.py | 15 +++++++++++---- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/tests/codecs/mpeg1.luau b/tests/codecs/mpeg1.luau index 34cafb4..d5ca329 100644 --- a/tests/codecs/mpeg1.luau +++ b/tests/codecs/mpeg1.luau @@ -1,4 +1,5 @@ --- codecs/video/Mpeg1 tests, ported from mpeg1-luau tests/decode.luau and tests/robust.luau. Against ffmpeg +-- codecs/video/Mpeg1 tests, ported from mpeg1-luau tests/decode.luau and tests/robust.luau. Every frame bit-exact with +-- `ffmpeg -idct int` (the IDCT codecs/video/Dsp reproduces; frames with concealed macroblocks excepted). Against ffmpeg -- (tools/fixtures/mpeg1.py): every stream decodes, one access unit at a time, to as many frames as ffmpeg's decode, -- in display order; each frame's RGBA channel means within 1.5 of ffmpeg's; whole sampled frames within 2 MAE per -- channel (IDCT and colour rounding) and, as yuv420p (frameImage), within a PSNR floor of ffmpeg -pix_fmt yuv420p; @@ -11,6 +12,7 @@ local Mpeg1 = require("@formats/codecs/video/Mpeg1") local Base64 = require("@formats/core/Base64") local Deflate = require("@formats/codecs/compress/Deflate") local Types = require("@formats/Types") +local Crc32 = require("@formats/core/Crc32") local STREAMS = { require("@tests/gen/mpeg1_qcif_200k"), @@ -81,6 +83,7 @@ local function psnr(a: buffer, ao: number, b: buffer, bo: number, n: number): nu end local MAE_LIMIT = 2 +local totalExact, totalConcealed = 0, 0 local MEAN_LIMIT = 1.5 -- 1. every stream against ffmpeg @@ -101,6 +104,7 @@ for _, fx in STREAMS do local hasB = string.find(name, "bframes") ~= nil local dec = Mpeg1.new({ reorder = hasB }) local frames = 0 + local exact, concealed = 0, 0 local maxMae, maxMeanErr = 0, 0 local sums = {} local function onFrame(f: any) @@ -136,6 +140,21 @@ for _, fx in STREAMS do end t:check(buffer.readu8(px, 3) == 255 and buffer.readu8(px, n * 4 - 1) == 255, "alpha 255") sums[frames] = buffer.tostring(px) + -- bit-exact against ffmpeg -idct int (outside macroblocks the decoder had to conceal) + local ex = fx.exact[frames] + if ex then + local img = Mpeg1.frameImage(f) :: Types.Image + local yc = Crc32.of(img.pixels, 0, n) + local uc = Crc32.of(img.pixels, n, cw * ch) + local vc = Crc32.of(img.pixels, n + cw * ch, cw * ch) + if yc == ex[1] and uc == ex[2] and vc == ex[3] then + exact += 1 + elseif next(f.concealed) ~= nil then + concealed += 1 + else + t:check(false, string.format("%s frame %d (type %d): differs from ffmpeg -idct int", name, frames, f.pictureType)) + end + end end local t0 = os.clock() for _, u in fx.units do @@ -155,7 +174,10 @@ for _, fx in STREAMS do t:eq(frames, fx.frames, name .. ": frames (ffmpeg's count)") t:eq(dec.stats.errors, 0, name .. ": no bitstream errors") worst = math.max(worst, maxMae) - t:note("%-13s %4dx%-4d %3d frames worst MAE %.3f means err %.3f %6.1f fps (with the checks)", name, w, h, frames, maxMae, maxMeanErr, frames / dt) + t:note("%-13s %4dx%-4d %3d frames %3d bit-exact worst MAE %.3f means err %.3f %6.1f fps (with the checks)", name, w, h, frames, exact, maxMae, maxMeanErr, frames / dt) + t:eq(exact + concealed, frames, name .. ": frames equal to ffmpeg -idct int (or with concealed macroblocks)") + totalExact += exact + totalConcealed += concealed -- streamed: random pieces through write / decodeFrame, then flush local d2 = Mpeg1.new({ reorder = hasB }) @@ -187,6 +209,7 @@ for _, fx in STREAMS do t:eq(got, frames, name .. ": streamed frame count") end t:note("worst sampled-frame MAE %.3f (limit %d); worst yuv420p PSNR luma %.1f, chroma %.1f dB", worst, MAE_LIMIT, worstY, worstC) +t:note("%d frames bit-exact with ffmpeg -idct int, %d with concealed macroblocks (ffmpeg error concealment)", totalExact, totalConcealed) -- 2. a size change mid-stream do diff --git a/tools/fixtures/mpeg1.py b/tools/fixtures/mpeg1.py index 0400555..3b5dfd8 100644 --- a/tools/fixtures/mpeg1.py +++ b/tools/fixtures/mpeg1.py @@ -4,7 +4,9 @@ # tests/gen/mpeg1_.luau { name, width, height, frames, file (base64), units = { { offset, length } }, # means = { { r, g, b } } (every frame, display order, ffmpeg's RGBA), # rgba = { { index, pixels } } (two whole RGBA frames, base64 zlib), -# yuv = { { index, planes } } (three whole frames as ffmpeg -pix_fmt yuv420p, base64 zlib) } +# yuv = { { index, planes } } (three whole frames as ffmpeg -pix_fmt yuv420p, base64 zlib), +# exact = { { y, u, v } } (CRC-32 of every frame's planes from `ffmpeg -idct int`, the IDCT +# codecs/video/Dsp reproduces: the decode must equal it) } # tests/gen/mpeg1_sizechange.luau { file (base64), parts = { { width, height, frames, last (RGBA, base64 zlib) } } } # tests/gen/mpeg1_bench.luau { { width, height, file (base64), units } }: 320x240 and 640x480 streams (with # B-frames) for tools/bench.luau @@ -122,9 +124,9 @@ def generate(ctx): es = open(path, "rb").read() return (patch_size(es, *size) if size else es), path - def reference(path, fmt): - out = path + "." + fmt - ff(["-i", path, "-f", "rawvideo", "-pix_fmt", fmt, out]) + def reference(path, fmt, idct=None): + out = path + "." + fmt + (idct or "") + ff((["-idct", idct] if idct else []) + ["-i", path, "-f", "rawvideo", "-pix_fmt", fmt, out]) r = subprocess.run([ffprobe, "-v", "error", "-select_streams", "v:0", "-show_entries", "stream=width,height", "-of", "csv=p=0", path], check=True, capture_output=True) w, h = (int(x) for x in r.stdout.decode().strip().split(",")[:2]) @@ -134,10 +136,13 @@ def generate(ctx): es, path = encode(name, args, size) rgba, w, h = reference(path, "rgba") yuv, _, _ = reference(path, "yuv420p") + exact, _, _ = reference(path, "yuv420p", "int") if size: rgba, yuv = crop(rgba, w, h, size[0], size[1], 4), crop_yuv(yuv, w, h, size[0], size[1]) + exact = crop_yuv(exact, w, h, size[0], size[1]) w, h = size fs, ys = w * h * 4, w * h + 2 * ((w + 1) // 2) * ((h + 1) // 2) + c = ((w + 1) // 2) * ((h + 1) // 2) n = len(rgba) // fs means = [] for k in range(n): @@ -151,6 +156,8 @@ def generate(ctx): "means": means, "rgba": [{"index": k + 1, "pixels": z64(rgba[k * fs:(k + 1) * fs])} for k in picks], "yuv": [{"index": k + 1, "planes": z64(yuv[k * ys:(k + 1) * ys])} for k in ypicks], + "exact": [[zlib.crc32(f[:w * h]), zlib.crc32(f[w * h:w * h + c]), zlib.crc32(f[w * h + c:ys])] + for f in (exact[k * ys:(k + 1) * ys] for k in range(n))], }) # a size change mid-stream: 176x144 then 320x180 (each part decoded by ffmpeg on its own)