From 7962f8dbb5fc64e76a91551f6c9de115e3e34606 Mon Sep 17 00:00:00 2001 From: Gianni Date: Tue, 20 Oct 2026 22:52:26 -0700 Subject: [PATCH 1/4] Upgrade to Zig 0.17 and bump fcvvdp to 0.4.1 --- .github/workflows/ci.yml | 39 +++ .gitignore | 1 + README.md | 17 +- bench/compare.py | 131 ++++++++ build.zig | 60 +++- build.zig.zon | 17 +- doc/zig-0.17.md | 105 ++++++ src/cvvdp.h | 2 +- tests/api.c | 102 ++++++ tests/expected.json | 711 +++++++++++++++++++++++++++++++++++++++ tests/fixtures.py | 125 +++++++ tests/regression.py | 138 ++++++++ 12 files changed, 1435 insertions(+), 13 deletions(-) create mode 100644 .github/workflows/ci.yml create mode 100644 bench/compare.py create mode 100644 doc/zig-0.17.md create mode 100644 tests/api.c create mode 100644 tests/expected.json create mode 100644 tests/fixtures.py create mode 100644 tests/regression.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..297a6c3 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,39 @@ +name: Zig 0.17 + +on: + push: + branches: [main] + pull_request: + workflow_dispatch: + +jobs: + test: + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, macos-latest] + optimize: [debug, safe, fast, small] + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v4 + - uses: mlugg/setup-zig@v2 + with: + version: 0.17.0 + - uses: actions/setup-python@v5 + with: + python-version: '3.x' + - run: zig fmt --check build.zig build.zig.zon src + - run: zig build -Doptimize=${{ matrix.optimize }} --summary all + - run: zig build test -Doptimize=${{ matrix.optimize }} --summary all + + lto: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: mlugg/setup-zig@v2 + with: + version: 0.17.0 + - uses: actions/setup-python@v5 + with: + python-version: '3.x' + - run: zig build test --release=fast -Dflto=true --summary all diff --git a/.gitignore b/.gitignore index dca7e61..694954b 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ zig-out/ zig-pkg/ *.o test/ +__pycache__/ diff --git a/README.md b/README.md index 3bf6d1d..aa08c7d 100644 --- a/README.md +++ b/README.md @@ -49,13 +49,13 @@ multiple threads, fcvvdp is over 221% faster than the reference implementation. Compilation requires: -- [Zig](https://ziglang.org/) 0.16.x +- [Zig](https://ziglang.org/) 0.17.x - macOS, Linux, or Unix-like operating system ### Binary 0. Ensure all dependencies are installed -1. Run `zig build --release=fast` (add `-Dflto=true` for FLTO) +1. Run `zig build --release=fast` (add `-Dflto=true` for FLTO on ELF targets) 2. Your `fcvvdp` binary will be in `zig-out/bin/` ```sh @@ -84,12 +84,23 @@ sRGB PNG, PNM/PAM, QOI, or Y4M input expected ### Library 0. Ensure all dependencies are installed -1. Run `zig build --release=fast` (add `-Dflto=true` for FLTO) +1. Run `zig build --release=fast` (add `-Dflto=true` for FLTO on ELF targets) 2. The `libcvvdp` library will be in `zig-out/lib/`, alongside `cvvdp.h` in `zig-out/include/`. Library usage is clearly defined in `cvvdp.h`. +### Validation + +Run `zig build test --release=fast` with Python 3 installed. This runs the C API +checks, the pinned image library's tests, and 85 CLI metric cases with deterministic +PNG, PBM/PGM/PPM/PAM, QOI, and 8/9/10/12/16-bit Y4M inputs. It also checks all +display presets, thread parity, temporal reset, error handling, and text output. +Use `-Doptimize=debug`, `safe`, `fast`, or `small` to select an optimization mode. + +See [the Zig 0.17 validation notes](doc/zig-0.17.md) for baseline comparisons and +reproducible runtime/memory benchmarks. + ### FFmpeg Patch 0. Clone FFmpeg: diff --git a/bench/compare.py b/bench/compare.py new file mode 100644 index 0000000..6bd1103 --- /dev/null +++ b/bench/compare.py @@ -0,0 +1,131 @@ +#!/usr/bin/env python3 +"""Compare release CLI binaries using paired end-to-end time and peak RSS.""" + +import argparse +import hashlib +import json +import math +import platform +import random +import re +import statistics +import subprocess +import sys +import tempfile +import time +from pathlib import Path + +sys.dont_write_bytecode = True +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests")) +from fixtures import pixels, png, y4m +from regression import compare + + +def inputs(directory): + cases = {} + for label, distorted in (("ref", False), ("dis", True)): + rgb = pixels(1920, 1080, distorted) + png(directory / f"{label}.png", 1920, 1080, rgb) + data = bytearray() + for value in rgb: + data.extend(bytes([value, value])) + (directory / f"{label}.ppm").write_bytes(b"P6\n1920 1080\n65535\n" + data) + for width, height, frames, bits in ((640, 360, 8, 8), (640, 360, 8, 10), (1280, 720, 4, 10)): + name = f"{width}x{height}-{bits}" + y4m(directory / f"{label}-{name}.y4m", width, height, frames, bits, + "420jpeg" if bits == 8 else "420p10", distorted=distorted) + if label == "ref": + for threads in (1, 4): + cases[f"y4m-{name}-t{threads}"] = ["-t", str(threads), f"ref-{name}.y4m", f"dis-{name}.y4m"] + return {"png-1080p": ["ref.png", "dis.png"], + "ppm16-1080p": ["ref.ppm", "dis.ppm"], **cases} + + +def measure(binary, args, directory): + timer = ["/usr/bin/time", "-l" if sys.platform == "darwin" else "-v"] + start = time.perf_counter_ns() + run = subprocess.run([*timer, str(binary), "-j", *args], cwd=directory, + capture_output=True, text=True, timeout=180) + elapsed = time.perf_counter_ns() - start + if run.returncode: + raise RuntimeError(run.stderr) + result, _ = json.JSONDecoder().raw_decode(run.stderr[run.stderr.index("{"):]) + pattern = (r"(\d+)\s+maximum resident set size" if sys.platform == "darwin" + else r"Maximum resident set size \(kbytes\):\s*(\d+)") + rss = int(re.search(pattern, run.stderr).group(1)) + if sys.platform != "darwin": + rss *= 1024 + return {"wall_ns": elapsed, "peak_rss_bytes": rss, "result": result} + + +def summarize(pairs, key, limit, rng): + a = [p["baseline"][key] for p in pairs] + b = [p["candidate"][key] for p in pairs] + ratios = [math.log(y / x) for x, y in zip(a, b)] + bootstrap = sorted(math.exp(statistics.mean(rng.choices(ratios, k=len(ratios)))) + for _ in range(10000)) + low, high = bootstrap[250], bootstrap[9749] + return {"baseline_median": statistics.median(a), "candidate_median": statistics.median(b), + "paired_ratio": math.exp(statistics.mean(ratios)), "ratio_ci95": [low, high], + "verdict": "pass" if high <= limit else "regression" if low > limit else "inconclusive"} + + +def sha256(path): + return hashlib.sha256(path.read_bytes()).hexdigest() + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("baseline", type=lambda p: Path(p).resolve()) + parser.add_argument("candidate", type=lambda p: Path(p).resolve()) + parser.add_argument("--samples", type=int, default=16) + parser.add_argument("--threshold", type=float, default=1.0, help="Upper confidence bound slowdown limit, percent") + parser.add_argument("--output", type=Path, required=True) + args = parser.parse_args() + if args.samples < 8 or not math.isfinite(args.threshold) or args.threshold < 0: + parser.error("Require at least 8 samples and a finite, nonnegative threshold") + rng = random.Random(42) + report = {"host": platform.platform(), "processor": platform.machine(), + "samples": args.samples, "threshold_percent": args.threshold, + "binaries": {k: {"path": str(getattr(args, k)), "sha256": sha256(getattr(args, k))} + for k in ("baseline", "candidate")}, "results": {}, "raw_samples": {}} + with tempfile.TemporaryDirectory(prefix="fcvvdp-bench-") as tmp: + directory = Path(tmp) + print("Generating 1080p image and 360p/720p video fixtures...", flush=True) + cases = inputs(directory) + report["input_sha256"] = {p.name: sha256(p) for p in sorted(directory.iterdir())} + expected = {} + for name, case in cases.items(): + expected[name] = measure(args.baseline, case, directory)["result"] + compare(measure(args.candidate, case, directory)["result"], expected[name], name) + report["raw_samples"][name] = [] + for index in range(args.samples): + # Alternate program order and rotate case order to limit drift bias. + names = list(cases) + names = names[index % len(names):] + names[:index % len(names)] + for name in names: + pair = {} + order = ("baseline", "candidate") if index % 2 == 0 else ("candidate", "baseline") + for label in order: + pair[label] = measure(getattr(args, label), cases[name], directory) + compare(pair[label]["result"], expected[name], name) + report["raw_samples"][name].append(pair) + print(f"Completed pair {index + 1}/{args.samples}", flush=True) + status = 0 + for name, pairs in report["raw_samples"].items(): + result = {key: summarize(pairs, key, 1 + args.threshold / 100, rng) + for key in ("wall_ns", "peak_rss_bytes")} + report["results"][name] = result + for key, stats in result.items(): + if stats["verdict"] != "pass": + status = max(status, 1 if stats["verdict"] == "regression" else 2) + print(f"{name:25} {key:15} {100 * (stats['paired_ratio'] - 1):+7.2f}%" + f" 95% CI [{100 * (stats['ratio_ci95'][0] - 1):+.2f}%," + f" {100 * (stats['ratio_ci95'][1] - 1):+.2f}%] {stats['verdict']}") + args.output.parent.mkdir(parents=True, exist_ok=True) + args.output.write_text(json.dumps(report, indent=2) + "\n") + return status + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/build.zig b/build.zig index f28df10..3daf174 100644 --- a/build.zig +++ b/build.zig @@ -18,7 +18,13 @@ pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); const strip = b.option(bool, "strip", "strip symbols from the binary, defaults to false") orelse false; - const flto = b.option(bool, "flto", "enable Link Time Optimization, defaults to false") orelse false; + const flto = b.option(bool, "flto", "enable Link Time Optimization on ELF targets, defaults to false") orelse false; + if (flto and target.result.ofmt != .elf) { + const unsupported = b.addFail("Zig 0.17 supports this project's LTO build only on ELF targets; omit -Dflto=true for macOS"); + b.default_step.dependOn(&unsupported.step); + b.step("test", "Run regression tests").dependOn(&unsupported.step); + return; + } const options = b.addOptions(); // simpleimgio @@ -48,6 +54,7 @@ pub fn build(b: *std.Build) void { // 'libcvvdp.a' static lib const cvvdp = b.addLibrary(.{ .name = "cvvdp", + .use_lld = if (flto) true else null, .root_module = b.createModule(.{ .target = target, .optimize = optimize, @@ -74,10 +81,31 @@ pub fn build(b: *std.Build) void { }); if (target_result.cpu.arch == .x86_64) { - cvvdp.root_module.addCSourceFile(.{ + // Compile the SIMD object with its own CPU features. Zig's target + // configuration takes precedence over per-file -mavx2/-mfma flags. + var avx2_query = target.query; + avx2_query.cpu_features_sub.removeFeature(@backingInt(std.Target.x86.Feature.avx2)); + avx2_query.cpu_features_sub.removeFeature(@backingInt(std.Target.x86.Feature.fma)); + avx2_query.cpu_features_add.addFeature(@backingInt(std.Target.x86.Feature.avx2)); + avx2_query.cpu_features_add.addFeature(@backingInt(std.Target.x86.Feature.fma)); + const avx2_module = b.createModule(.{ + .target = b.resolveTargetQuery(avx2_query), + .optimize = optimize, + .link_libc = true, + .strip = strip, + }); + avx2_module.addIncludePath(b.path(".")); + avx2_module.addCSourceFile(.{ .file = b.path("src/cvvdp_avx2.c"), .flags = &cvvdp_flags, }); + const avx2 = b.addObject(.{ + .name = "cvvdp-avx2", + .root_module = avx2_module, + .use_lld = if (flto) true else null, + }); + avx2.lto = if (flto) .full else null; + cvvdp.root_module.addObject(avx2); } else if (target_result.cpu.arch == .aarch64) { cvvdp.root_module.addCSourceFile(.{ .file = b.path("src/cvvdp_neon.c"), @@ -102,6 +130,7 @@ pub fn build(b: *std.Build) void { .strip = strip, }), .use_llvm = true, + .use_lld = if (flto) true else null, }); cvvdpenc.root_module.addOptions("build_opts", options); cvvdpenc.root_module.addImport("c", c_module); @@ -109,5 +138,32 @@ pub fn build(b: *std.Build) void { cvvdpenc.root_module.addIncludePath(b.path(".")); cvvdpenc.root_module.linkLibrary(cvvdp); cvvdpenc.root_module.linkLibrary(spng); + cvvdpenc.lto = if (flto) .full else null; b.installArtifact(cvvdpenc); + + const test_step = b.step("test", "Run C API, image dependency, and CLI regression tests (requires Python 3)"); + const api_tests = b.addExecutable(.{ + .name = "cvvdp-api-tests", + .use_lld = if (flto) true else null, + .root_module = b.createModule(.{ + .target = target, + .optimize = optimize, + .link_libc = true, + }), + }); + api_tests.root_module.addIncludePath(b.path("src")); + api_tests.root_module.addCSourceFile(.{ + .file = b.path("tests/api.c"), + .flags = &.{ "-std=c23", "-Wall", "-Wextra", "-Werror" }, + }); + api_tests.root_module.linkLibrary(cvvdp); + api_tests.lto = if (flto) .full else null; + test_step.dependOn(&b.addRunArtifact(api_tests).step); + const image_tests = b.addTest(.{ .root_module = simpleimgio }); + test_step.dependOn(&b.addRunArtifact(image_tests).step); + const cli_tests = b.addSystemCommand(&.{"python3"}); + cli_tests.addFileArg(b.path("tests/regression.py")); + cli_tests.addArtifactArg(cvvdpenc); + cli_tests.has_side_effects = true; + test_step.dependOn(&cli_tests.step); } diff --git a/build.zig.zon b/build.zig.zon index b135aa4..35e41fb 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -1,23 +1,26 @@ .{ .name = .fcvvdp, - .version = "0.4.0", + .version = "0.4.1", .dependencies = .{ .spng = .{ - .url = "git+https://github.com/allyourcodebase/libspng.git#8858232bde47083739c3e25f676093423610ec5e", - .hash = "spng-0.0.1-T166cP8UAABcBGTOPKx7kUUyHEcu0eDvN8LnSIj3S_cW", + .url = "git+https://github.com/gianni-rosato/libspng.git#107d133ef4fa649b236fdb920b113f9c92615c16", + .hash = "spng-0.0.2-T166cFckAADyxclwh-9Onmk2UG9mQz5IMoxiFN2VCk7I", }, .simpleimgio = .{ - .url = "git+https://github.com/gianni-rosato/simpleimgio.git#98031ca5c7487fdc9d57f97184f3bd6044b3ab07", - .hash = "simpleimgio-0.0.1-oHTuOdP9AADRXI8TEcZr6hNxRYS-OH3ZoYMAS-jxxe5H", + .url = "git+https://github.com/gianni-rosato/simpleimgio.git#98473d19f243eefd4ef29a2149af47bc072c2484", + .hash = "simpleimgio-0.0.1-oHTuOfMbAQBWcIe2wQBOoIIrItr8a-kBVowFbAaZkwXy", }, }, .fingerprint = 0xfe41744458a5e60e, - .minimum_zig_version = "0.16.0", + .minimum_zig_version = "0.17.0", .paths = .{ "build.zig", "build.zig.zon", + "c_imports.h", "src", - "third-party", + "tests", + "bench", + "doc", "LICENSE", "README.md", }, diff --git a/doc/zig-0.17.md b/doc/zig-0.17.md new file mode 100644 index 0000000..913cb40 --- /dev/null +++ b/doc/zig-0.17.md @@ -0,0 +1,105 @@ +# Zig 0.17 upgrade validation + +Version 0.4.1 requires Zig 0.17.x. It pins `simpleimgio` main at +`98473d19f243eefd4ef29a2149af47bc072c2484` and the Zig 0.17 libspng wrapper from +[libspng PR #1](https://github.com/gianni-rosato/libspng/pull/1). The metric's C +implementation and C compiler optimization flags are unchanged. + +The AVX2/FMA implementation now has a separate build module with those CPU +features enabled. The dispatcher and scalar implementation retain the requested +target CPU, so generic x86_64 builds work without enabling AVX globally. + +`-Dflto=true` selects LLD and full LTO for the library, SIMD object, CLI, and +C API test executable on ELF targets. Zig cannot use this LTO path for Mach-O; +macOS requests now fail with an explicit message. The original Zig 0.16 baseline +also fails to build with `-Dflto=true` on macOS. + +## Correctness + +`zig build test` runs 22 dependency tests, a C API test executable, and 85 CLI +metric cases. Fixtures cover odd dimensions, grayscale/alpha/RGB/RGBA PNGs at +8/16 bits, all six classic Netpbm encodings, four PAM channel layouts, RGB/RGBA +QOI, and full/limited-range Y4M at 8/9/10/12/16 bits with chroma siting metadata. +The CLI cases include all 27 display presets, identical inputs, and one/four +threads. Twelve invalid-input cases and help/text output are also checked. + +The C tests cover equivalent 8/16-bit and linear float inputs, a larger odd-sized +image that enables threaded work, temporal reset, display parameters, and API +errors. Checks remain active in release builds. They also pass when linked to +the original Zig 0.16 library. + +`tests/expected.json` was recorded from unmodified fcvvdp commit +`98bd21a7a876be29ba06a29325b03624047ee850`, built with Zig 0.16.0 / ReleaseFast. +JSON fields and metadata must match exactly. Numerical checks permit 0.000002 +absolute JOD error and 10 ppm relative internal-quality error (with a 0.000002 +absolute floor) for compiler, optimization-mode, libm, and SIMD rounding. +The C float conversion comparison permits 20 ppm in internal quality because +the caller's strict conversion and the library's fast-math conversion can round +differently before the metric. + +On arm64 macOS 27.0.1, the final Zig 0.17 release binary differs from the Zig +0.16 baseline by at most **0.000001 JOD** and **0.000012 internal quality** +(3.08 ppm relative quality) across all 85 cases. Identical inputs still score +10 JOD and zero quality. These are bounded numerical comparisons, not a claim +of bitwise equality or proof for every possible input. + +Local validation includes debug/safe/fast/small on arm64 macOS, the full release +suite on x86_64 macOS under Rosetta, and generic x86_64 Linux release cross-builds +with and without LTO. CI covers all four modes on Linux and macOS plus Linux LTO. + +## Reproducing a baseline comparison + +Install Zig 0.16.0 separately alongside Zig 0.17.0. From this checkout: + +```sh +mkdir -p /tmp/fcvvdp-baseline +git archive 98bd21a7a876be29ba06a29325b03624047ee850 | tar -x -C /tmp/fcvvdp-baseline +(cd /tmp/fcvvdp-baseline && /path/to/zig-0.16.0/zig build --release=fast) +zig build --release=fast +python3 tests/regression.py zig-out/bin/fcvvdp \ + --baseline /tmp/fcvvdp-baseline/zig-out/bin/fcvvdp +python3 bench/compare.py /tmp/fcvvdp-baseline/zig-out/bin/fcvvdp \ + zig-out/bin/fcvvdp --samples 24 --output /tmp/fcvvdp-performance.json +``` + +The benchmark requires Python 3 and `/usr/bin/time` (BSD time on macOS or GNU +time on Linux). It generates deterministic 1080p PNG and 16-bit PPM images, +eight-frame 360p 8/10-bit Y4M clips, and four-frame 720p 10-bit Y4M clips. +Videos run with one and four threads. Setup and fixture encoding are excluded; +process startup, decoding, conversion, metric computation, and teardown are +included. Filesystem caches are warmed before measurement. + +Each sample alternates binary order, rotates workload order, and validates both +scores and metadata. Reports retain every wall-time/RSS measurement, per-case +results, input/binary SHA-256 hashes, paired geometric mean ratios, and bootstrap +95% confidence intervals. The configurable slowdown limit defaults to 1%; a +regression or an inconclusive interval returns a nonzero status. Passing that +limit does not establish literally zero performance regression. + +## Measured performance + +On arm64 macOS 27.0.1, 24 alternating paired measurements of the final pinned +release binary against Zig 0.16.0 / ReleaseFast gave the following wall-time +changes (negative is faster): + +| Workload | Paired change | Bootstrap 95% interval | +| --- | ---: | ---: | +| 1080p PNG | -0.88% | -1.62% to -0.19% | +| 1080p 16-bit PPM | -1.19% | -1.62% to -0.73% | +| 360p 8-bit Y4M, 1 thread | +0.01% | -0.43% to +0.48% | +| 360p 8-bit Y4M, 4 threads | +0.06% | -0.12% to +0.23% | +| 360p 10-bit Y4M, 1 thread | +0.31% | -0.08% to +0.66% | +| 360p 10-bit Y4M, 4 threads | +0.03% | -0.25% to +0.28% | +| 720p 10-bit Y4M, 1 thread | +0.38% | -0.02% to +0.76% | +| 720p 10-bit Y4M, 4 threads | +0.18% | -0.10% to +0.44% | + +No workload shows a statistically resolved runtime slowdown in this run; the +largest upper confidence bound is +0.76%. Peak RSS increases by 32–48 KiB +(at most 0.05%). This small memory increase means the result does **not** meet +a literal zero-regression requirement. Other hosts, inputs, and long-running +workloads still need their own measurements. + +The measured baseline binary SHA-256 is +`a5a942f6f5cd6e4b0767f9f9fbc48e29e484a9d02eed5d9dc7cb5001a4dfa4d8`; +the candidate binary SHA-256 is +`355cdf3747ac4223a1035b8018adeaa096003ef0966f6813ec251a8088e7760e`. diff --git a/src/cvvdp.h b/src/cvvdp.h index f8cd52b..dd6c884 100644 --- a/src/cvvdp.h +++ b/src/cvvdp.h @@ -26,7 +26,7 @@ extern "C" { #endif /* Version */ -static const char* CVVDP_VERSION = "0.4.0"; +static const char* CVVDP_VERSION = "0.4.1"; /* Error codes */ typedef enum FcvvdpError { diff --git a/tests/api.c b/tests/api.c new file mode 100644 index 0000000..3b2d148 --- /dev/null +++ b/tests/api.c @@ -0,0 +1,102 @@ +#include "cvvdp.h" +#include +#include +#include +#include +#include + +#define CHECK(expr) do { if (!(expr)) { \ + fprintf(stderr, "%s:%d: %s\n", __FILE__, __LINE__, #expr); \ + exit(1); \ +} } while (0) + +static void same_result(FcvvdpResult a, FcvvdpResult b) +{ + if (fabs(a.jod - b.jod) >= 0.000002 || + fabs(a.quality - b.quality) >= 0.00001 * fmax(1, fabs(b.quality))) + fprintf(stderr, "results: %.12g / %.12g versus %.12g / %.12g\n", + a.jod, a.quality, b.jod, b.quality); + CHECK(isfinite(a.jod) && isfinite(a.quality)); + CHECK(fabs(a.jod - b.jod) < 0.000002); + CHECK(fabs(a.quality - b.quality) < 0.00001 * fmax(1, fabs(b.quality))); +} + +static float linear(uint8_t sample) +{ + const float value = sample / 255.0f; + return value <= 0.04045f ? value / 12.92f : powf((value + 0.055f) / 1.055f, 2.4f); +} + +int main(void) +{ + CHECK(strcmp(cvvdp_version_string(), "0.4.1") == 0); + /* Odd dimensions exercise vector tails; >16384 pixels enables pool work. */ + enum { W = 193, H = 129, N = W * H * 3 }; + uint8_t reference[N], distorted[N]; + uint16_t ref16[N], dis16[N]; + float ref_float[N], dis_float[N]; + for (int i = 0; i < N; i++) { + reference[i] = (uint8_t)(i * 17 + i / 31); + distorted[i] = (uint8_t)(reference[i] ^ 7); + ref16[i] = reference[i] * 257; + dis16[i] = distorted[i] * 257; + ref_float[i] = linear(reference[i]); + dis_float[i] = linear(distorted[i]); + } + FcvvdpImage ref = {W, H, W * 3, reference, CVVDP_PIXEL_FORMAT_RGB_UINT8, CVVDP_COLORSPACE_SRGB}; + FcvvdpImage dis = {W, H, W * 3, distorted, CVVDP_PIXEL_FORMAT_RGB_UINT8, CVVDP_COLORSPACE_SRGB}; + FcvvdpResult expected, actual; + CHECK(cvvdp_compare_images(&ref, &dis, CVVDP_DISPLAY_STANDARD_FHD, 1, NULL, &expected) == CVVDP_OK); + CHECK(expected.jod > 0 && expected.jod < 10 && expected.quality > 0); + CHECK(cvvdp_compare_images(&ref, &dis, CVVDP_DISPLAY_STANDARD_FHD, 4, NULL, &actual) == CVVDP_OK); + same_result(actual, expected); + CHECK(cvvdp_compare_images(&ref, &ref, CVVDP_DISPLAY_STANDARD_FHD, 1, NULL, &actual) == CVVDP_OK); + CHECK(actual.jod == 10 && actual.quality == 0); + + FcvvdpImage r16 = {W, H, W * 6, ref16, CVVDP_PIXEL_FORMAT_RGB_UINT16, CVVDP_COLORSPACE_SRGB}; + FcvvdpImage d16 = {W, H, W * 6, dis16, CVVDP_PIXEL_FORMAT_RGB_UINT16, CVVDP_COLORSPACE_SRGB}; + CHECK(cvvdp_compare_images(&r16, &d16, CVVDP_DISPLAY_STANDARD_FHD, 1, NULL, &actual) == CVVDP_OK); + same_result(actual, expected); + FcvvdpImage rf = {W, H, W * 12, ref_float, CVVDP_PIXEL_FORMAT_RGB_FLOAT, CVVDP_COLORSPACE_LINEAR}; + FcvvdpImage df = {W, H, W * 12, dis_float, CVVDP_PIXEL_FORMAT_RGB_FLOAT, CVVDP_COLORSPACE_LINEAR}; + CHECK(cvvdp_compare_images(&rf, &df, CVVDP_DISPLAY_STANDARD_FHD, 1, NULL, &actual) == CVVDP_OK); + /* The caller's strict sRGB conversion and the library's fast-math + conversion can round differently before the nonlinear metric. */ + CHECK(fabs(actual.jod - expected.jod) < 0.000002); + CHECK(fabs(actual.quality - expected.quality) < 0.00002 * expected.quality); + FcvvdpResult float_expected = actual; + CHECK(cvvdp_compare_images(&rf, &df, CVVDP_DISPLAY_STANDARD_FHD, 4, NULL, &actual) == CVVDP_OK); + same_result(actual, float_expected); + + for (unsigned threads = 1; threads <= 4; threads += 3) { + FcvvdpCtx *ctx = NULL; + CHECK(cvvdp_create(W, H, 30, CVVDP_DISPLAY_STANDARD_FHD, threads, NULL, &ctx) == CVVDP_OK); + FcvvdpResult first; + CHECK(cvvdp_process_frame(ctx, &ref, &dis, &first) == CVVDP_OK); + for (int i = 0; i < 4; i++) + CHECK(cvvdp_process_frame(ctx, &ref, &dis, &actual) == CVVDP_OK); + CHECK(cvvdp_reset(ctx) == CVVDP_OK); + CHECK(cvvdp_process_frame(ctx, &ref, &dis, &actual) == CVVDP_OK); + same_result(actual, first); + FcvvdpImage wrong = dis; + wrong.width--; + CHECK(cvvdp_process_frame(ctx, &ref, &wrong, &actual) == CVVDP_ERROR_DIMENSION_MISMATCH); + cvvdp_destroy(ctx); + } + + FcvvdpCtx *ctx = NULL; + CHECK(cvvdp_create(0, H, 0, CVVDP_DISPLAY_STANDARD_FHD, 1, NULL, &ctx) == CVVDP_ERROR_INVALID_DIMENSIONS); + CHECK(ctx == NULL); + CHECK(cvvdp_create(W, H, 0, CVVDP_DISPLAY_STANDARD_FHD, 1, NULL, NULL) == CVVDP_ERROR_NULL_POINTER); + CHECK(cvvdp_compare_images(NULL, &dis, CVVDP_DISPLAY_STANDARD_FHD, 1, NULL, &actual) == CVVDP_ERROR_NULL_POINTER); + CHECK(cvvdp_process_frame(NULL, &ref, &dis, &actual) == CVVDP_ERROR_NULL_POINTER); + CHECK(cvvdp_reset(NULL) == CVVDP_ERROR_NULL_POINTER); + cvvdp_destroy(NULL); + for (int model = 0; model < CVVDP_DISPLAY_CUSTOM; model++) { + FcvvdpDisplayParams params; + CHECK(cvvdp_get_display_params(model, ¶ms) == CVVDP_OK); + CHECK(params.max_luminance > 0 && params.resolution_width > 0); + } + puts("C API: pixel formats, thread parity, temporal reset, presets and errors passed"); + return 0; +} diff --git a/tests/expected.json b/tests/expected.json new file mode 100644 index 0000000..675205f --- /dev/null +++ b/tests/expected.json @@ -0,0 +1,711 @@ +{ + "png-0-8": { + "jod": 9.120004, + "quality": 25.06716, + "reference": "ref-png0-8.png", + "distorted": "dis-png0-8.png", + "width": 65, + "height": 49 + }, + "png-0-16": { + "jod": 9.120004, + "quality": 25.06716, + "reference": "ref-png0-16.png", + "distorted": "dis-png0-16.png", + "width": 65, + "height": 49 + }, + "png-2-8": { + "jod": 9.658901, + "quality": 9.049433, + "reference": "ref-png2-8.png", + "distorted": "dis-png2-8.png", + "width": 65, + "height": 49 + }, + "png-2-16": { + "jod": 9.658901, + "quality": 9.049433, + "reference": "ref-png2-16.png", + "distorted": "dis-png2-16.png", + "width": 65, + "height": 49 + }, + "png-4-8": { + "jod": 9.120004, + "quality": 25.06716, + "reference": "ref-png4-8.png", + "distorted": "dis-png4-8.png", + "width": 65, + "height": 49 + }, + "png-4-16": { + "jod": 9.120004, + "quality": 25.06716, + "reference": "ref-png4-16.png", + "distorted": "dis-png4-16.png", + "width": 65, + "height": 49 + }, + "png-6-8": { + "jod": 9.658901, + "quality": 9.049433, + "reference": "ref-png6-8.png", + "distorted": "dis-png6-8.png", + "width": 65, + "height": 49 + }, + "png-6-16": { + "jod": 9.658901, + "quality": 9.049433, + "reference": "ref-png6-16.png", + "distorted": "dis-png6-16.png", + "width": 65, + "height": 49 + }, + "qoi": { + "jod": 9.658901, + "quality": 9.049433, + "reference": "ref.qoi", + "distorted": "dis.qoi", + "width": 65, + "height": 49 + }, + "qoi-rgba": { + "jod": 9.658901, + "quality": 9.049433, + "reference": "ref-rgba.qoi", + "distorted": "dis-rgba.qoi", + "width": 65, + "height": 49 + }, + "P1": { + "jod": 8.574686, + "quality": 42.096754, + "reference": "ref-P1.pbm", + "distorted": "dis-P1.pbm", + "width": 65, + "height": 49 + }, + "P4": { + "jod": 8.574686, + "quality": 42.096754, + "reference": "ref-P4.pbm", + "distorted": "dis-P4.pbm", + "width": 65, + "height": 49 + }, + "P2-255": { + "jod": 9.120004, + "quality": 25.06716, + "reference": "ref-P2-255.pgm", + "distorted": "dis-P2-255.pgm", + "width": 65, + "height": 49 + }, + "P2-1023": { + "jod": 9.120004, + "quality": 25.06716, + "reference": "ref-P2-1023.pgm", + "distorted": "dis-P2-1023.pgm", + "width": 65, + "height": 49 + }, + "P2-65535": { + "jod": 9.120004, + "quality": 25.06716, + "reference": "ref-P2-65535.pgm", + "distorted": "dis-P2-65535.pgm", + "width": 65, + "height": 49 + }, + "P3-255": { + "jod": 9.658901, + "quality": 9.049433, + "reference": "ref-P3-255.ppm", + "distorted": "dis-P3-255.ppm", + "width": 65, + "height": 49 + }, + "P3-1023": { + "jod": 9.658901, + "quality": 9.049433, + "reference": "ref-P3-1023.ppm", + "distorted": "dis-P3-1023.ppm", + "width": 65, + "height": 49 + }, + "P3-65535": { + "jod": 9.658901, + "quality": 9.049433, + "reference": "ref-P3-65535.ppm", + "distorted": "dis-P3-65535.ppm", + "width": 65, + "height": 49 + }, + "P5-255": { + "jod": 9.120004, + "quality": 25.06716, + "reference": "ref-P5-255.pgm", + "distorted": "dis-P5-255.pgm", + "width": 65, + "height": 49 + }, + "P5-1023": { + "jod": 9.120004, + "quality": 25.06716, + "reference": "ref-P5-1023.pgm", + "distorted": "dis-P5-1023.pgm", + "width": 65, + "height": 49 + }, + "P5-65535": { + "jod": 9.120004, + "quality": 25.06716, + "reference": "ref-P5-65535.pgm", + "distorted": "dis-P5-65535.pgm", + "width": 65, + "height": 49 + }, + "P6-255": { + "jod": 9.658901, + "quality": 9.049433, + "reference": "ref-P6-255.ppm", + "distorted": "dis-P6-255.ppm", + "width": 65, + "height": 49 + }, + "P6-1023": { + "jod": 9.658901, + "quality": 9.049433, + "reference": "ref-P6-1023.ppm", + "distorted": "dis-P6-1023.ppm", + "width": 65, + "height": 49 + }, + "P6-65535": { + "jod": 9.658901, + "quality": 9.049433, + "reference": "ref-P6-65535.ppm", + "distorted": "dis-P6-65535.ppm", + "width": 65, + "height": 49 + }, + "pam-1": { + "jod": 9.120004, + "quality": 25.06716, + "reference": "ref-1.pam", + "distorted": "dis-1.pam", + "width": 65, + "height": 49 + }, + "pam-2": { + "jod": 9.120004, + "quality": 25.06716, + "reference": "ref-2.pam", + "distorted": "dis-2.pam", + "width": 65, + "height": 49 + }, + "pam-3": { + "jod": 9.658901, + "quality": 9.049433, + "reference": "ref-3.pam", + "distorted": "dis-3.pam", + "width": 65, + "height": 49 + }, + "pam-4": { + "jod": 9.658901, + "quality": 9.049433, + "reference": "ref-4.pam", + "distorted": "dis-4.pam", + "width": 65, + "height": 49 + }, + "y4m-420jpeg-False-t1": { + "jod": 9.825127, + "quality": 4.412597, + "reference": "ref-420jpeg-False.y4m", + "distorted": "dis-420jpeg-False.y4m", + "width": 65, + "height": 49, + "frames": 5 + }, + "y4m-420jpeg-False-t4": { + "jod": 9.825127, + "quality": 4.412597, + "reference": "ref-420jpeg-False.y4m", + "distorted": "dis-420jpeg-False.y4m", + "width": 65, + "height": 49, + "frames": 5 + }, + "y4m-420jpeg-True-t1": { + "jod": 9.841313, + "quality": 3.975074, + "reference": "ref-420jpeg-True.y4m", + "distorted": "dis-420jpeg-True.y4m", + "width": 65, + "height": 49, + "frames": 5 + }, + "y4m-420jpeg-True-t4": { + "jod": 9.841313, + "quality": 3.975074, + "reference": "ref-420jpeg-True.y4m", + "distorted": "dis-420jpeg-True.y4m", + "width": 65, + "height": 49, + "frames": 5 + }, + "y4m-420mpeg2-False-t1": { + "jod": 9.822302, + "quality": 4.489258, + "reference": "ref-420mpeg2-False.y4m", + "distorted": "dis-420mpeg2-False.y4m", + "width": 65, + "height": 49, + "frames": 5 + }, + "y4m-420mpeg2-False-t4": { + "jod": 9.822302, + "quality": 4.489258, + "reference": "ref-420mpeg2-False.y4m", + "distorted": "dis-420mpeg2-False.y4m", + "width": 65, + "height": 49, + "frames": 5 + }, + "y4m-420mpeg2-True-t1": { + "jod": 9.844083, + "quality": 3.900546, + "reference": "ref-420mpeg2-True.y4m", + "distorted": "dis-420mpeg2-True.y4m", + "width": 65, + "height": 49, + "frames": 5 + }, + "y4m-420mpeg2-True-t4": { + "jod": 9.844083, + "quality": 3.900546, + "reference": "ref-420mpeg2-True.y4m", + "distorted": "dis-420mpeg2-True.y4m", + "width": 65, + "height": 49, + "frames": 5 + }, + "y4m-420paldv-False-t1": { + "jod": 9.823276, + "quality": 4.462807, + "reference": "ref-420paldv-False.y4m", + "distorted": "dis-420paldv-False.y4m", + "width": 65, + "height": 49, + "frames": 5 + }, + "y4m-420paldv-False-t4": { + "jod": 9.823276, + "quality": 4.462807, + "reference": "ref-420paldv-False.y4m", + "distorted": "dis-420paldv-False.y4m", + "width": 65, + "height": 49, + "frames": 5 + }, + "y4m-420paldv-True-t1": { + "jod": 9.84645, + "quality": 3.836917, + "reference": "ref-420paldv-True.y4m", + "distorted": "dis-420paldv-True.y4m", + "width": 65, + "height": 49, + "frames": 5 + }, + "y4m-420paldv-True-t4": { + "jod": 9.84645, + "quality": 3.836917, + "reference": "ref-420paldv-True.y4m", + "distorted": "dis-420paldv-True.y4m", + "width": 65, + "height": 49, + "frames": 5 + }, + "y4m-420p9-False-t1": { + "jod": 9.822458, + "quality": 4.485021, + "reference": "ref-420p9-False.y4m", + "distorted": "dis-420p9-False.y4m", + "width": 65, + "height": 49, + "frames": 5 + }, + "y4m-420p9-False-t4": { + "jod": 9.822458, + "quality": 4.485021, + "reference": "ref-420p9-False.y4m", + "distorted": "dis-420p9-False.y4m", + "width": 65, + "height": 49, + "frames": 5 + }, + "y4m-420p9-True-t1": { + "jod": 9.844584, + "quality": 3.887067, + "reference": "ref-420p9-True.y4m", + "distorted": "dis-420p9-True.y4m", + "width": 65, + "height": 49, + "frames": 5 + }, + "y4m-420p9-True-t4": { + "jod": 9.844584, + "quality": 3.887067, + "reference": "ref-420p9-True.y4m", + "distorted": "dis-420p9-True.y4m", + "width": 65, + "height": 49, + "frames": 5 + }, + "y4m-420p10-False-t1": { + "jod": 9.821852, + "quality": 4.501496, + "reference": "ref-420p10-False.y4m", + "distorted": "dis-420p10-False.y4m", + "width": 65, + "height": 49, + "frames": 5 + }, + "y4m-420p10-False-t4": { + "jod": 9.821852, + "quality": 4.501496, + "reference": "ref-420p10-False.y4m", + "distorted": "dis-420p10-False.y4m", + "width": 65, + "height": 49, + "frames": 5 + }, + "y4m-420p10-True-t1": { + "jod": 9.844431, + "quality": 3.891184, + "reference": "ref-420p10-True.y4m", + "distorted": "dis-420p10-True.y4m", + "width": 65, + "height": 49, + "frames": 5 + }, + "y4m-420p10-True-t4": { + "jod": 9.844431, + "quality": 3.891184, + "reference": "ref-420p10-True.y4m", + "distorted": "dis-420p10-True.y4m", + "width": 65, + "height": 49, + "frames": 5 + }, + "y4m-420p12-False-t1": { + "jod": 9.821164, + "quality": 4.520158, + "reference": "ref-420p12-False.y4m", + "distorted": "dis-420p12-False.y4m", + "width": 65, + "height": 49, + "frames": 5 + }, + "y4m-420p12-False-t4": { + "jod": 9.821164, + "quality": 4.520158, + "reference": "ref-420p12-False.y4m", + "distorted": "dis-420p12-False.y4m", + "width": 65, + "height": 49, + "frames": 5 + }, + "y4m-420p12-True-t1": { + "jod": 9.843992, + "quality": 3.90298, + "reference": "ref-420p12-True.y4m", + "distorted": "dis-420p12-True.y4m", + "width": 65, + "height": 49, + "frames": 5 + }, + "y4m-420p12-True-t4": { + "jod": 9.843992, + "quality": 3.90298, + "reference": "ref-420p12-True.y4m", + "distorted": "dis-420p12-True.y4m", + "width": 65, + "height": 49, + "frames": 5 + }, + "y4m-420p16-False-t1": { + "jod": 9.822351, + "quality": 4.487903, + "reference": "ref-420p16-False.y4m", + "distorted": "dis-420p16-False.y4m", + "width": 65, + "height": 49, + "frames": 5 + }, + "y4m-420p16-False-t4": { + "jod": 9.822351, + "quality": 4.487903, + "reference": "ref-420p16-False.y4m", + "distorted": "dis-420p16-False.y4m", + "width": 65, + "height": 49, + "frames": 5 + }, + "y4m-420p16-True-t1": { + "jod": 9.844381, + "quality": 3.89252, + "reference": "ref-420p16-True.y4m", + "distorted": "dis-420p16-True.y4m", + "width": 65, + "height": 49, + "frames": 5 + }, + "y4m-420p16-True-t4": { + "jod": 9.844381, + "quality": 3.89252, + "reference": "ref-420p16-True.y4m", + "distorted": "dis-420p16-True.y4m", + "width": 65, + "height": 49, + "frames": 5 + }, + "model-fhd": { + "jod": 9.658901, + "quality": 9.049433, + "reference": "ref.qoi", + "distorted": "dis.qoi", + "width": 65, + "height": 49 + }, + "model-4k": { + "jod": 9.842048, + "quality": 3.955288, + "reference": "ref.qoi", + "distorted": "dis.qoi", + "width": 65, + "height": 49 + }, + "model-hdr_pq": { + "jod": 9.876833, + "quality": 3.027208, + "reference": "ref.qoi", + "distorted": "dis.qoi", + "width": 65, + "height": 49 + }, + "model-hdr_hlg": { + "jod": 9.876833, + "quality": 3.027208, + "reference": "ref.qoi", + "distorted": "dis.qoi", + "width": 65, + "height": 49 + }, + "model-hdr_linear": { + "jod": 9.876833, + "quality": 3.027208, + "reference": "ref.qoi", + "distorted": "dis.qoi", + "width": 65, + "height": 49 + }, + "model-hdr_dark": { + "jod": 9.876798, + "quality": 3.028141, + "reference": "ref.qoi", + "distorted": "dis.qoi", + "width": 65, + "height": 49 + }, + "model-hdr_zoom": { + "jod": 9.658, + "quality": 9.075116, + "reference": "ref.qoi", + "distorted": "dis.qoi", + "width": 65, + "height": 49 + }, + "model-standard_hmd": { + "jod": 9.545499, + "quality": 12.320523, + "reference": "ref.qoi", + "distorted": "dis.qoi", + "width": 65, + "height": 49 + }, + "model-standard_phone": { + "jod": 9.93753, + "quality": 1.459162, + "reference": "ref.qoi", + "distorted": "dis.qoi", + "width": 65, + "height": 49 + }, + "model-sdr_4k_30": { + "jod": 9.813164, + "quality": 4.737898, + "reference": "ref.qoi", + "distorted": "dis.qoi", + "width": 65, + "height": 49 + }, + "model-sdr_fhd_24": { + "jod": 9.690567, + "quality": 8.149538, + "reference": "ref.qoi", + "distorted": "dis.qoi", + "width": 65, + "height": 49 + }, + "model-htc_vive_pro": { + "jod": 9.493234, + "quality": 13.849965, + "reference": "ref.qoi", + "distorted": "dis.qoi", + "width": 65, + "height": 49 + }, + "model-iphone_12_pro": { + "jod": 9.971341, + "quality": 0.631387, + "reference": "ref.qoi", + "distorted": "dis.qoi", + "width": 65, + "height": 49 + }, + "model-iphone_14_pro": { + "jod": 9.970325, + "quality": 0.655499, + "reference": "ref.qoi", + "distorted": "dis.qoi", + "width": 65, + "height": 49 + }, + "model-iphone_14_pro_vert": { + "jod": 9.970325, + "quality": 0.655499, + "reference": "ref.qoi", + "distorted": "dis.qoi", + "width": 65, + "height": 49 + }, + "model-iphone_14_pro_hdr": { + "jod": 9.983552, + "quality": 0.347573, + "reference": "ref.qoi", + "distorted": "dis.qoi", + "width": 65, + "height": 49 + }, + "model-iphone_14_pro_hdr_vert": { + "jod": 9.983552, + "quality": 0.347573, + "reference": "ref.qoi", + "distorted": "dis.qoi", + "width": 65, + "height": 49 + }, + "model-ipad_pro_12_9": { + "jod": 9.867132, + "quality": 3.284267, + "reference": "ref.qoi", + "distorted": "dis.qoi", + "width": 65, + "height": 49 + }, + "model-macbook_pro_16": { + "jod": 9.891242, + "quality": 2.648209, + "reference": "ref.qoi", + "distorted": "dis.qoi", + "width": 65, + "height": 49 + }, + "model-lg_oled_2017_sdr": { + "jod": 9.948006, + "quality": 1.197856, + "reference": "ref.qoi", + "distorted": "dis.qoi", + "width": 65, + "height": 49 + }, + "model-lg_oled_2017_hdr": { + "jod": 9.967978, + "quality": 0.711369, + "reference": "ref.qoi", + "distorted": "dis.qoi", + "width": 65, + "height": 49 + }, + "model-eizo_CG3146": { + "jod": 9.82275, + "quality": 4.477085, + "reference": "ref.qoi", + "distorted": "dis.qoi", + "width": 65, + "height": 49 + }, + "model-65inch_hdr_pq_4knit": { + "jod": 9.925881, + "quality": 1.753576, + "reference": "ref.qoi", + "distorted": "dis.qoi", + "width": 65, + "height": 49 + }, + "model-65inch_hdr_pq_2knit": { + "jod": 9.925879, + "quality": 1.753649, + "reference": "ref.qoi", + "distorted": "dis.qoi", + "width": 65, + "height": 49 + }, + "model-65inch_hdr_pq_1knit": { + "jod": 9.925878, + "quality": 1.753685, + "reference": "ref.qoi", + "distorted": "dis.qoi", + "width": 65, + "height": 49 + }, + "model-lg_oled_2026_hdr_pq": { + "jod": 9.946435, + "quality": 1.236793, + "reference": "ref.qoi", + "distorted": "dis.qoi", + "width": 65, + "height": 49 + }, + "model-mcos": { + "jod": 9.72968, + "quality": 7.047582, + "reference": "ref.qoi", + "distorted": "dis.qoi", + "width": 65, + "height": 49 + }, + "identical-image": { + "jod": 10.0, + "quality": 0.0, + "reference": "ref.qoi", + "distorted": "ref.qoi", + "width": 65, + "height": 49 + }, + "identical-video": { + "jod": 10.0, + "quality": 0.0, + "reference": "ref-420jpeg-False.y4m", + "distorted": "ref-420jpeg-False.y4m", + "width": 65, + "height": 49, + "frames": 5 + } +} diff --git a/tests/fixtures.py b/tests/fixtures.py new file mode 100644 index 0000000..c4dfbd8 --- /dev/null +++ b/tests/fixtures.py @@ -0,0 +1,125 @@ +"""Deterministic image/video inputs, generated with the Python standard library.""" + +import struct +import zlib +from pathlib import Path + + +def pixels(width, height, distorted=False, frame=0): + data = bytearray() + for y in range(height): + for x in range(width): + for c in range(3): + value = (x * (3 + c) + y * (7 - c) + frame * 11 + c * 53) % 256 + if distorted: + value = min(255, max(0, value + ((x + y + c + frame) % 13 - 6))) + data.append(value) + return bytes(data) + + +def png(path, width, height, rgb, color=2, depth=8): + def chunk(kind, data): + return (struct.pack(">I", len(data)) + kind + data + + struct.pack(">I", zlib.crc32(kind + data))) + + channels = {0: 1, 2: 3, 4: 2, 6: 4}[color] + raw = bytearray() + for y in range(height): + raw.append(0) # PNG filter: none + for x in range(width): + offset = (y * width + x) * 3 + p = rgb[offset:offset + 3] + samples = list(p if color in (2, 6) else p[:1]) + if color in (4, 6): + samples.append((x * 17 + y * 31) % 256) + assert len(samples) == channels + for value in samples: + raw.extend(bytes([value]) if depth == 8 else struct.pack(">H", value * 257)) + path.write_bytes(b"\x89PNG\r\n\x1a\n" + + chunk(b"IHDR", struct.pack(">IIBBBBB", width, height, depth, color, 0, 0, 0)) + + chunk(b"IDAT", zlib.compress(raw)) + chunk(b"IEND", b"")) + + +def y4m(path, width, height, frames=5, bits=8, chroma="420jpeg", full=False, + distorted=False): + header = (f"YUV4MPEG2 W{width} H{height} F30000:1001 Ip A1:1 C{chroma}" + f" XCOLORRANGE={'FULL' if full else 'LIMITED'}\n") + peak = (1 << bits) - 1 + scale = 1 << (bits - 8) + with path.open("wb") as out: + out.write(header.encode()) + for frame in range(frames): + out.write(b"FRAME\n") + for plane in range(3): + w, h = (width, height) if plane == 0 else ((width + 1) // 2, (height + 1) // 2) + values = bytearray() + for y in range(h): + for x in range(w): + value = (16 + (x * 3 + y * 5 + frame * 11) % 220 if plane == 0 + else 96 + (x * 7 + y * 3 + plane * 17 + frame * 2) % 65) + value = value * scale + ((x + y + frame) % scale) + if distorted: + value += ((x + y + plane + frame) % 7 - 3) * scale + value = min(peak, max(0, value)) + values.extend(bytes([value]) if bits == 8 else struct.pack("IIBB", width, height, 3, 0)) + for i in range(0, len(rgb), 3): + qoi.extend(b"\xfe" + rgb[i:i + 3]) + qoi.extend(b"\0\0\0\0\0\0\0\1") + (directory / f"{label}.qoi").write_bytes(qoi) + rgba_qoi = bytearray(b"qoif" + struct.pack(">IIBB", width, height, 4, 0)) + for i in range(width * height): + rgba_qoi.extend(b"\xff" + rgb[i * 3:i * 3 + 3] + bytes([(i * 17) % 256])) + rgba_qoi.extend(b"\0\0\0\0\0\0\0\1") + (directory / f"{label}-rgba.qoi").write_bytes(rgba_qoi) + bitmap = [int(v < 128) for v in gray] + (directory / f"{label}-P1.pbm").write_bytes( + f"P1\n{width} {height}\n".encode() + " ".join(map(str, bitmap)).encode()) + packed = bytearray() + for y in range(height): + for x in range(0, width, 8): + packed.append(sum(bitmap[y * width + x + b] << (7 - b) + for b in range(min(8, width - x)))) + (directory / f"{label}-P4.pbm").write_bytes(f"P4\n{width} {height}\n".encode() + packed) + for magic, ext, channels in (("P2", "pgm", 1), ("P3", "ppm", 3), + ("P5", "pgm", 1), ("P6", "ppm", 3)): + data = gray if channels == 1 else rgb + for maximum in (255, 1023, 65535): + samples = [v * maximum // 255 for v in data] + payload = (" ".join(map(str, samples)).encode() if magic in ("P2", "P3") + else bytes(samples) if maximum == 255 + else b"".join(struct.pack(">H", v) for v in samples)) + (directory / f"{label}-{magic}-{maximum}.{ext}").write_bytes( + f"{magic}\n# regression fixture\n{width} {height}\n{maximum}\n".encode() + payload) + for channels, kind in ((1, "GRAYSCALE"), (2, "GRAYSCALE_ALPHA"), (3, "RGB"), (4, "RGB_ALPHA")): + data = bytearray() + for i in range(width * height): + data.extend(rgb[i * 3:i * 3 + 3] if channels >= 3 else gray[i:i + 1]) + if channels in (2, 4): + data.append((i * 17) % 256) + (directory / f"{label}-{channels}.pam").write_bytes( + f"P7\nWIDTH {width}\nHEIGHT {height}\nDEPTH {channels}\nMAXVAL 255\nTUPLTYPE {kind}\nENDHDR\n".encode() + data) + for bits, chroma in ((8, "420jpeg"), (8, "420mpeg2"), (8, "420paldv"), + (9, "420p9"), (10, "420p10"), (12, "420p12"), (16, "420p16")): + for full in (False, True): + y4m(directory / f"{label}-{chroma}-{full}.y4m", 65, 49, + bits=bits, chroma=chroma, full=full, distorted=distorted) + png(directory / "wrong-size.png", 64, 48, pixels(64, 48)) + y4m(directory / "short.y4m", 65, 49, frames=1) + y4m(directory / "empty.y4m", 65, 49, frames=0) + (directory / "bad.png").write_bytes(b"not a png") + (directory / "bad.ppm").write_bytes(b"P6\n65 49\n255\n\0") + return directory diff --git a/tests/regression.py b/tests/regression.py new file mode 100644 index 0000000..bb6aa29 --- /dev/null +++ b/tests/regression.py @@ -0,0 +1,138 @@ +#!/usr/bin/env python3 +"""Exercise CLI decoding, metric results, threading, presets, and error paths.""" + +import argparse +import json +import math +import subprocess +import sys +import tempfile +from pathlib import Path + +sys.dont_write_bytecode = True +from fixtures import generate + + +MODELS = ("fhd", "4k", "hdr_pq", "hdr_hlg", "hdr_linear", "hdr_dark", "hdr_zoom", + "standard_hmd", "standard_phone", "sdr_4k_30", "sdr_fhd_24", "htc_vive_pro", + "iphone_12_pro", "iphone_14_pro", "iphone_14_pro_vert", "iphone_14_pro_hdr", + "iphone_14_pro_hdr_vert", "ipad_pro_12_9", "macbook_pro_16", "lg_oled_2017_sdr", + "lg_oled_2017_hdr", "eizo_CG3146", "65inch_hdr_pq_4knit", "65inch_hdr_pq_2knit", + "65inch_hdr_pq_1knit", "lg_oled_2026_hdr_pq", "mcos") + + +def cases(): + result = {} + for color in (0, 2, 4, 6): + for bits in (8, 16): + suffix = f"-png{color}-{bits}.png" + result[f"png-{color}-{bits}"] = ["ref" + suffix, "dis" + suffix] + result["qoi"] = ["ref.qoi", "dis.qoi"] + result["qoi-rgba"] = ["ref-rgba.qoi", "dis-rgba.qoi"] + for magic in ("P1", "P4"): + result[magic] = [f"ref-{magic}.pbm", f"dis-{magic}.pbm"] + for magic, ext in (("P2", "pgm"), ("P3", "ppm"), ("P5", "pgm"), ("P6", "ppm")): + for maximum in (255, 1023, 65535): + suffix = f"-{magic}-{maximum}.{ext}" + result[f"{magic}-{maximum}"] = ["ref" + suffix, "dis" + suffix] + for channels in (1, 2, 3, 4): + result[f"pam-{channels}"] = [f"ref-{channels}.pam", f"dis-{channels}.pam"] + for chroma in ("420jpeg", "420mpeg2", "420paldv", "420p9", "420p10", "420p12", "420p16"): + for full in (False, True): + for threads in (1, 4): + suffix = f"-{chroma}-{full}.y4m" + result[f"y4m-{chroma}-{full}-t{threads}"] = ["-t", str(threads), "ref" + suffix, "dis" + suffix] + for model in MODELS: + result[f"model-{model}"] = ["-m", model, "ref.qoi", "dis.qoi"] + result["identical-image"] = ["ref.qoi", "ref.qoi"] + result["identical-video"] = ["-t", "1", "ref-420jpeg-False.y4m", "ref-420jpeg-False.y4m"] + return result + + +def invoke(binary, args, directory): + return subprocess.run([str(binary), *args], cwd=directory, capture_output=True, + text=True, timeout=60) + + +def metric(binary, args, directory): + run = invoke(binary, ["-j", *args], directory) + if run.returncode: + raise AssertionError(f"{args}: exit {run.returncode}\n{run.stdout}{run.stderr}") + # The established CLI prints its banner and JSON on stderr. + text = run.stderr + data = json.loads(text[text.index("{"):]) + for key in ("jod", "quality"): + if not math.isfinite(data[key]): + raise AssertionError(f"{args}: non-finite {key}") + return data + + +def compare(actual, expected, name): + if actual.keys() != expected.keys(): + raise AssertionError(f"{name}: JSON fields differ") + for key, value in expected.items(): + # Fixed limits for compiler/libm/SIMD rounding: 2 micro-JOD and + # 10 parts per million in the internal quality accumulator. + equal = (math.isclose(actual[key], value, rel_tol=0, abs_tol=2e-6) + if key == "jod" else + math.isclose(actual[key], value, rel_tol=1e-5, abs_tol=2e-6) + if key == "quality" else actual[key] == value) + if not equal: + raise AssertionError(f"{name}.{key}: {actual[key]!r} != {value!r}") + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("binary", type=lambda p: Path(p).resolve()) + parser.add_argument("--baseline", type=lambda p: Path(p).resolve()) + parser.add_argument("--record", type=Path, help="Record snapshots from a trusted baseline") + args = parser.parse_args() + golden_path = Path(__file__).with_name("expected.json") + golden = {} if args.record else json.loads(golden_path.read_text()) + outputs = {} + with tempfile.TemporaryDirectory(prefix="fcvvdp-regression-") as tmp: + directory = generate(tmp) + for name, case in cases().items(): + actual = metric(args.binary, case, directory) + if args.baseline: + compare(actual, metric(args.baseline, case, directory), name) + if not args.record: + compare(actual, golden[name], name) + outputs[name] = actual + for threads in (1, 4): + for chroma in ("420jpeg", "420mpeg2", "420paldv", "420p9", "420p10", "420p12", "420p16"): + for full in (False, True): + compare(outputs[f"y4m-{chroma}-{full}-t{threads}"], + outputs[f"y4m-{chroma}-{full}-t1"], "thread parity") + for name in ("identical-image", "identical-video"): + if outputs[name]["jod"] != 10 or outputs[name]["quality"] != 0: + raise AssertionError(f"{name}: identical inputs must score 10 JOD / 0 quality") + errors = [([], "MissingFiles"), (["--unknown"], "UnknownOption"), + (["-m"], "InvalidArguments"), (["-t"], "InvalidArguments"), + (["ref.qoi", "dis.qoi", "ref.qoi"], "TooManyFiles"), + (["ref.qoi", "ref-420jpeg-False.y4m"], "MismatchedInputTypes"), + (["ref.qoi", "wrong-size.png"], "DimensionMismatch"), + (["ref-420jpeg-False.y4m", "short.y4m"], "FrameCountMismatch"), + (["empty.y4m", "empty.y4m"], "EmptyY4M"), + (["missing.png", "dis.qoi"], "FileNotFound"), + (["bad.png", "dis.qoi"], "GetHeaderFailed")] + errors.append((["bad.ppm", "dis.qoi"], "InsufficientData")) + for case, error in errors: + run = invoke(args.binary, case, directory) + if run.returncode <= 0 or f"error: {error}" not in run.stderr: + raise AssertionError(f"{case}: expected {error}, got {run.returncode}\n{run.stderr}") + for option in ("-h", "--help"): + run = invoke(args.binary, [option], directory) + if run.returncode or "usage: fcvvdp" not in run.stderr: + raise AssertionError(f"{option}: help failed") + for option in ([], ["-v"]): + run = invoke(args.binary, [*option, "ref.qoi", "dis.qoi"], directory) + if run.returncode or ("JOD Score:" if option else "JOD:") not in run.stderr: + raise AssertionError(f"{option}: text output failed") + if args.record: + args.record.write_text(json.dumps(outputs, indent=2) + "\n") + print(f"{len(outputs)} metric cases, {len(errors)} error paths, help/text output and thread parity passed") + + +if __name__ == "__main__": + main() From cd22d7ca83969e4ee814e111caaa5bf9520e0858 Mon Sep 17 00:00:00 2001 From: Gianni Date: Tue, 20 Oct 2026 23:05:22 -0700 Subject: [PATCH 2/4] Pin libspng cleanup commit --- build.zig.zon | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build.zig.zon b/build.zig.zon index 35e41fb..8ee054c 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -3,8 +3,8 @@ .version = "0.4.1", .dependencies = .{ .spng = .{ - .url = "git+https://github.com/gianni-rosato/libspng.git#107d133ef4fa649b236fdb920b113f9c92615c16", - .hash = "spng-0.0.2-T166cFckAADyxclwh-9Onmk2UG9mQz5IMoxiFN2VCk7I", + .url = "git+https://github.com/gianni-rosato/libspng.git#c459a577fe818f4dfdb3ceaea46b702bd8c9d118", + .hash = "spng-0.0.2-T166cHISAADR33WgZ6IfHBVOPj0gwjyqnxYxc-XTWF8Z", }, .simpleimgio = .{ .url = "git+https://github.com/gianni-rosato/simpleimgio.git#98473d19f243eefd4ef29a2149af47bc072c2484", From b166b0f87560d03c738bda61098954d0f43dd75a Mon Sep 17 00:00:00 2001 From: Gianni Date: Tue, 20 Oct 2026 23:06:49 -0700 Subject: [PATCH 3/4] Remove added validation tooling and CI --- .github/workflows/ci.yml | 39 --- .gitignore | 1 - README.md | 11 - bench/compare.py | 131 -------- build.zig | 27 -- build.zig.zon | 3 - doc/zig-0.17.md | 105 ------ tests/api.c | 102 ------ tests/expected.json | 711 --------------------------------------- tests/fixtures.py | 125 ------- tests/regression.py | 138 -------- 11 files changed, 1393 deletions(-) delete mode 100644 .github/workflows/ci.yml delete mode 100644 bench/compare.py delete mode 100644 doc/zig-0.17.md delete mode 100644 tests/api.c delete mode 100644 tests/expected.json delete mode 100644 tests/fixtures.py delete mode 100644 tests/regression.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml deleted file mode 100644 index 297a6c3..0000000 --- a/.github/workflows/ci.yml +++ /dev/null @@ -1,39 +0,0 @@ -name: Zig 0.17 - -on: - push: - branches: [main] - pull_request: - workflow_dispatch: - -jobs: - test: - strategy: - fail-fast: false - matrix: - os: [ubuntu-latest, macos-latest] - optimize: [debug, safe, fast, small] - runs-on: ${{ matrix.os }} - steps: - - uses: actions/checkout@v4 - - uses: mlugg/setup-zig@v2 - with: - version: 0.17.0 - - uses: actions/setup-python@v5 - with: - python-version: '3.x' - - run: zig fmt --check build.zig build.zig.zon src - - run: zig build -Doptimize=${{ matrix.optimize }} --summary all - - run: zig build test -Doptimize=${{ matrix.optimize }} --summary all - - lto: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: mlugg/setup-zig@v2 - with: - version: 0.17.0 - - uses: actions/setup-python@v5 - with: - python-version: '3.x' - - run: zig build test --release=fast -Dflto=true --summary all diff --git a/.gitignore b/.gitignore index 694954b..dca7e61 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,3 @@ zig-out/ zig-pkg/ *.o test/ -__pycache__/ diff --git a/README.md b/README.md index aa08c7d..c335574 100644 --- a/README.md +++ b/README.md @@ -90,17 +90,6 @@ sRGB PNG, PNM/PAM, QOI, or Y4M input expected Library usage is clearly defined in `cvvdp.h`. -### Validation - -Run `zig build test --release=fast` with Python 3 installed. This runs the C API -checks, the pinned image library's tests, and 85 CLI metric cases with deterministic -PNG, PBM/PGM/PPM/PAM, QOI, and 8/9/10/12/16-bit Y4M inputs. It also checks all -display presets, thread parity, temporal reset, error handling, and text output. -Use `-Doptimize=debug`, `safe`, `fast`, or `small` to select an optimization mode. - -See [the Zig 0.17 validation notes](doc/zig-0.17.md) for baseline comparisons and -reproducible runtime/memory benchmarks. - ### FFmpeg Patch 0. Clone FFmpeg: diff --git a/bench/compare.py b/bench/compare.py deleted file mode 100644 index 6bd1103..0000000 --- a/bench/compare.py +++ /dev/null @@ -1,131 +0,0 @@ -#!/usr/bin/env python3 -"""Compare release CLI binaries using paired end-to-end time and peak RSS.""" - -import argparse -import hashlib -import json -import math -import platform -import random -import re -import statistics -import subprocess -import sys -import tempfile -import time -from pathlib import Path - -sys.dont_write_bytecode = True -sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests")) -from fixtures import pixels, png, y4m -from regression import compare - - -def inputs(directory): - cases = {} - for label, distorted in (("ref", False), ("dis", True)): - rgb = pixels(1920, 1080, distorted) - png(directory / f"{label}.png", 1920, 1080, rgb) - data = bytearray() - for value in rgb: - data.extend(bytes([value, value])) - (directory / f"{label}.ppm").write_bytes(b"P6\n1920 1080\n65535\n" + data) - for width, height, frames, bits in ((640, 360, 8, 8), (640, 360, 8, 10), (1280, 720, 4, 10)): - name = f"{width}x{height}-{bits}" - y4m(directory / f"{label}-{name}.y4m", width, height, frames, bits, - "420jpeg" if bits == 8 else "420p10", distorted=distorted) - if label == "ref": - for threads in (1, 4): - cases[f"y4m-{name}-t{threads}"] = ["-t", str(threads), f"ref-{name}.y4m", f"dis-{name}.y4m"] - return {"png-1080p": ["ref.png", "dis.png"], - "ppm16-1080p": ["ref.ppm", "dis.ppm"], **cases} - - -def measure(binary, args, directory): - timer = ["/usr/bin/time", "-l" if sys.platform == "darwin" else "-v"] - start = time.perf_counter_ns() - run = subprocess.run([*timer, str(binary), "-j", *args], cwd=directory, - capture_output=True, text=True, timeout=180) - elapsed = time.perf_counter_ns() - start - if run.returncode: - raise RuntimeError(run.stderr) - result, _ = json.JSONDecoder().raw_decode(run.stderr[run.stderr.index("{"):]) - pattern = (r"(\d+)\s+maximum resident set size" if sys.platform == "darwin" - else r"Maximum resident set size \(kbytes\):\s*(\d+)") - rss = int(re.search(pattern, run.stderr).group(1)) - if sys.platform != "darwin": - rss *= 1024 - return {"wall_ns": elapsed, "peak_rss_bytes": rss, "result": result} - - -def summarize(pairs, key, limit, rng): - a = [p["baseline"][key] for p in pairs] - b = [p["candidate"][key] for p in pairs] - ratios = [math.log(y / x) for x, y in zip(a, b)] - bootstrap = sorted(math.exp(statistics.mean(rng.choices(ratios, k=len(ratios)))) - for _ in range(10000)) - low, high = bootstrap[250], bootstrap[9749] - return {"baseline_median": statistics.median(a), "candidate_median": statistics.median(b), - "paired_ratio": math.exp(statistics.mean(ratios)), "ratio_ci95": [low, high], - "verdict": "pass" if high <= limit else "regression" if low > limit else "inconclusive"} - - -def sha256(path): - return hashlib.sha256(path.read_bytes()).hexdigest() - - -def main(): - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument("baseline", type=lambda p: Path(p).resolve()) - parser.add_argument("candidate", type=lambda p: Path(p).resolve()) - parser.add_argument("--samples", type=int, default=16) - parser.add_argument("--threshold", type=float, default=1.0, help="Upper confidence bound slowdown limit, percent") - parser.add_argument("--output", type=Path, required=True) - args = parser.parse_args() - if args.samples < 8 or not math.isfinite(args.threshold) or args.threshold < 0: - parser.error("Require at least 8 samples and a finite, nonnegative threshold") - rng = random.Random(42) - report = {"host": platform.platform(), "processor": platform.machine(), - "samples": args.samples, "threshold_percent": args.threshold, - "binaries": {k: {"path": str(getattr(args, k)), "sha256": sha256(getattr(args, k))} - for k in ("baseline", "candidate")}, "results": {}, "raw_samples": {}} - with tempfile.TemporaryDirectory(prefix="fcvvdp-bench-") as tmp: - directory = Path(tmp) - print("Generating 1080p image and 360p/720p video fixtures...", flush=True) - cases = inputs(directory) - report["input_sha256"] = {p.name: sha256(p) for p in sorted(directory.iterdir())} - expected = {} - for name, case in cases.items(): - expected[name] = measure(args.baseline, case, directory)["result"] - compare(measure(args.candidate, case, directory)["result"], expected[name], name) - report["raw_samples"][name] = [] - for index in range(args.samples): - # Alternate program order and rotate case order to limit drift bias. - names = list(cases) - names = names[index % len(names):] + names[:index % len(names)] - for name in names: - pair = {} - order = ("baseline", "candidate") if index % 2 == 0 else ("candidate", "baseline") - for label in order: - pair[label] = measure(getattr(args, label), cases[name], directory) - compare(pair[label]["result"], expected[name], name) - report["raw_samples"][name].append(pair) - print(f"Completed pair {index + 1}/{args.samples}", flush=True) - status = 0 - for name, pairs in report["raw_samples"].items(): - result = {key: summarize(pairs, key, 1 + args.threshold / 100, rng) - for key in ("wall_ns", "peak_rss_bytes")} - report["results"][name] = result - for key, stats in result.items(): - if stats["verdict"] != "pass": - status = max(status, 1 if stats["verdict"] == "regression" else 2) - print(f"{name:25} {key:15} {100 * (stats['paired_ratio'] - 1):+7.2f}%" - f" 95% CI [{100 * (stats['ratio_ci95'][0] - 1):+.2f}%," - f" {100 * (stats['ratio_ci95'][1] - 1):+.2f}%] {stats['verdict']}") - args.output.parent.mkdir(parents=True, exist_ok=True) - args.output.write_text(json.dumps(report, indent=2) + "\n") - return status - - -if __name__ == "__main__": - raise SystemExit(main()) diff --git a/build.zig b/build.zig index 3daf174..c0b5ee7 100644 --- a/build.zig +++ b/build.zig @@ -22,7 +22,6 @@ pub fn build(b: *std.Build) void { if (flto and target.result.ofmt != .elf) { const unsupported = b.addFail("Zig 0.17 supports this project's LTO build only on ELF targets; omit -Dflto=true for macOS"); b.default_step.dependOn(&unsupported.step); - b.step("test", "Run regression tests").dependOn(&unsupported.step); return; } const options = b.addOptions(); @@ -140,30 +139,4 @@ pub fn build(b: *std.Build) void { cvvdpenc.root_module.linkLibrary(spng); cvvdpenc.lto = if (flto) .full else null; b.installArtifact(cvvdpenc); - - const test_step = b.step("test", "Run C API, image dependency, and CLI regression tests (requires Python 3)"); - const api_tests = b.addExecutable(.{ - .name = "cvvdp-api-tests", - .use_lld = if (flto) true else null, - .root_module = b.createModule(.{ - .target = target, - .optimize = optimize, - .link_libc = true, - }), - }); - api_tests.root_module.addIncludePath(b.path("src")); - api_tests.root_module.addCSourceFile(.{ - .file = b.path("tests/api.c"), - .flags = &.{ "-std=c23", "-Wall", "-Wextra", "-Werror" }, - }); - api_tests.root_module.linkLibrary(cvvdp); - api_tests.lto = if (flto) .full else null; - test_step.dependOn(&b.addRunArtifact(api_tests).step); - const image_tests = b.addTest(.{ .root_module = simpleimgio }); - test_step.dependOn(&b.addRunArtifact(image_tests).step); - const cli_tests = b.addSystemCommand(&.{"python3"}); - cli_tests.addFileArg(b.path("tests/regression.py")); - cli_tests.addArtifactArg(cvvdpenc); - cli_tests.has_side_effects = true; - test_step.dependOn(&cli_tests.step); } diff --git a/build.zig.zon b/build.zig.zon index 8ee054c..933473d 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -18,9 +18,6 @@ "build.zig.zon", "c_imports.h", "src", - "tests", - "bench", - "doc", "LICENSE", "README.md", }, diff --git a/doc/zig-0.17.md b/doc/zig-0.17.md deleted file mode 100644 index 913cb40..0000000 --- a/doc/zig-0.17.md +++ /dev/null @@ -1,105 +0,0 @@ -# Zig 0.17 upgrade validation - -Version 0.4.1 requires Zig 0.17.x. It pins `simpleimgio` main at -`98473d19f243eefd4ef29a2149af47bc072c2484` and the Zig 0.17 libspng wrapper from -[libspng PR #1](https://github.com/gianni-rosato/libspng/pull/1). The metric's C -implementation and C compiler optimization flags are unchanged. - -The AVX2/FMA implementation now has a separate build module with those CPU -features enabled. The dispatcher and scalar implementation retain the requested -target CPU, so generic x86_64 builds work without enabling AVX globally. - -`-Dflto=true` selects LLD and full LTO for the library, SIMD object, CLI, and -C API test executable on ELF targets. Zig cannot use this LTO path for Mach-O; -macOS requests now fail with an explicit message. The original Zig 0.16 baseline -also fails to build with `-Dflto=true` on macOS. - -## Correctness - -`zig build test` runs 22 dependency tests, a C API test executable, and 85 CLI -metric cases. Fixtures cover odd dimensions, grayscale/alpha/RGB/RGBA PNGs at -8/16 bits, all six classic Netpbm encodings, four PAM channel layouts, RGB/RGBA -QOI, and full/limited-range Y4M at 8/9/10/12/16 bits with chroma siting metadata. -The CLI cases include all 27 display presets, identical inputs, and one/four -threads. Twelve invalid-input cases and help/text output are also checked. - -The C tests cover equivalent 8/16-bit and linear float inputs, a larger odd-sized -image that enables threaded work, temporal reset, display parameters, and API -errors. Checks remain active in release builds. They also pass when linked to -the original Zig 0.16 library. - -`tests/expected.json` was recorded from unmodified fcvvdp commit -`98bd21a7a876be29ba06a29325b03624047ee850`, built with Zig 0.16.0 / ReleaseFast. -JSON fields and metadata must match exactly. Numerical checks permit 0.000002 -absolute JOD error and 10 ppm relative internal-quality error (with a 0.000002 -absolute floor) for compiler, optimization-mode, libm, and SIMD rounding. -The C float conversion comparison permits 20 ppm in internal quality because -the caller's strict conversion and the library's fast-math conversion can round -differently before the metric. - -On arm64 macOS 27.0.1, the final Zig 0.17 release binary differs from the Zig -0.16 baseline by at most **0.000001 JOD** and **0.000012 internal quality** -(3.08 ppm relative quality) across all 85 cases. Identical inputs still score -10 JOD and zero quality. These are bounded numerical comparisons, not a claim -of bitwise equality or proof for every possible input. - -Local validation includes debug/safe/fast/small on arm64 macOS, the full release -suite on x86_64 macOS under Rosetta, and generic x86_64 Linux release cross-builds -with and without LTO. CI covers all four modes on Linux and macOS plus Linux LTO. - -## Reproducing a baseline comparison - -Install Zig 0.16.0 separately alongside Zig 0.17.0. From this checkout: - -```sh -mkdir -p /tmp/fcvvdp-baseline -git archive 98bd21a7a876be29ba06a29325b03624047ee850 | tar -x -C /tmp/fcvvdp-baseline -(cd /tmp/fcvvdp-baseline && /path/to/zig-0.16.0/zig build --release=fast) -zig build --release=fast -python3 tests/regression.py zig-out/bin/fcvvdp \ - --baseline /tmp/fcvvdp-baseline/zig-out/bin/fcvvdp -python3 bench/compare.py /tmp/fcvvdp-baseline/zig-out/bin/fcvvdp \ - zig-out/bin/fcvvdp --samples 24 --output /tmp/fcvvdp-performance.json -``` - -The benchmark requires Python 3 and `/usr/bin/time` (BSD time on macOS or GNU -time on Linux). It generates deterministic 1080p PNG and 16-bit PPM images, -eight-frame 360p 8/10-bit Y4M clips, and four-frame 720p 10-bit Y4M clips. -Videos run with one and four threads. Setup and fixture encoding are excluded; -process startup, decoding, conversion, metric computation, and teardown are -included. Filesystem caches are warmed before measurement. - -Each sample alternates binary order, rotates workload order, and validates both -scores and metadata. Reports retain every wall-time/RSS measurement, per-case -results, input/binary SHA-256 hashes, paired geometric mean ratios, and bootstrap -95% confidence intervals. The configurable slowdown limit defaults to 1%; a -regression or an inconclusive interval returns a nonzero status. Passing that -limit does not establish literally zero performance regression. - -## Measured performance - -On arm64 macOS 27.0.1, 24 alternating paired measurements of the final pinned -release binary against Zig 0.16.0 / ReleaseFast gave the following wall-time -changes (negative is faster): - -| Workload | Paired change | Bootstrap 95% interval | -| --- | ---: | ---: | -| 1080p PNG | -0.88% | -1.62% to -0.19% | -| 1080p 16-bit PPM | -1.19% | -1.62% to -0.73% | -| 360p 8-bit Y4M, 1 thread | +0.01% | -0.43% to +0.48% | -| 360p 8-bit Y4M, 4 threads | +0.06% | -0.12% to +0.23% | -| 360p 10-bit Y4M, 1 thread | +0.31% | -0.08% to +0.66% | -| 360p 10-bit Y4M, 4 threads | +0.03% | -0.25% to +0.28% | -| 720p 10-bit Y4M, 1 thread | +0.38% | -0.02% to +0.76% | -| 720p 10-bit Y4M, 4 threads | +0.18% | -0.10% to +0.44% | - -No workload shows a statistically resolved runtime slowdown in this run; the -largest upper confidence bound is +0.76%. Peak RSS increases by 32–48 KiB -(at most 0.05%). This small memory increase means the result does **not** meet -a literal zero-regression requirement. Other hosts, inputs, and long-running -workloads still need their own measurements. - -The measured baseline binary SHA-256 is -`a5a942f6f5cd6e4b0767f9f9fbc48e29e484a9d02eed5d9dc7cb5001a4dfa4d8`; -the candidate binary SHA-256 is -`355cdf3747ac4223a1035b8018adeaa096003ef0966f6813ec251a8088e7760e`. diff --git a/tests/api.c b/tests/api.c deleted file mode 100644 index 3b2d148..0000000 --- a/tests/api.c +++ /dev/null @@ -1,102 +0,0 @@ -#include "cvvdp.h" -#include -#include -#include -#include -#include - -#define CHECK(expr) do { if (!(expr)) { \ - fprintf(stderr, "%s:%d: %s\n", __FILE__, __LINE__, #expr); \ - exit(1); \ -} } while (0) - -static void same_result(FcvvdpResult a, FcvvdpResult b) -{ - if (fabs(a.jod - b.jod) >= 0.000002 || - fabs(a.quality - b.quality) >= 0.00001 * fmax(1, fabs(b.quality))) - fprintf(stderr, "results: %.12g / %.12g versus %.12g / %.12g\n", - a.jod, a.quality, b.jod, b.quality); - CHECK(isfinite(a.jod) && isfinite(a.quality)); - CHECK(fabs(a.jod - b.jod) < 0.000002); - CHECK(fabs(a.quality - b.quality) < 0.00001 * fmax(1, fabs(b.quality))); -} - -static float linear(uint8_t sample) -{ - const float value = sample / 255.0f; - return value <= 0.04045f ? value / 12.92f : powf((value + 0.055f) / 1.055f, 2.4f); -} - -int main(void) -{ - CHECK(strcmp(cvvdp_version_string(), "0.4.1") == 0); - /* Odd dimensions exercise vector tails; >16384 pixels enables pool work. */ - enum { W = 193, H = 129, N = W * H * 3 }; - uint8_t reference[N], distorted[N]; - uint16_t ref16[N], dis16[N]; - float ref_float[N], dis_float[N]; - for (int i = 0; i < N; i++) { - reference[i] = (uint8_t)(i * 17 + i / 31); - distorted[i] = (uint8_t)(reference[i] ^ 7); - ref16[i] = reference[i] * 257; - dis16[i] = distorted[i] * 257; - ref_float[i] = linear(reference[i]); - dis_float[i] = linear(distorted[i]); - } - FcvvdpImage ref = {W, H, W * 3, reference, CVVDP_PIXEL_FORMAT_RGB_UINT8, CVVDP_COLORSPACE_SRGB}; - FcvvdpImage dis = {W, H, W * 3, distorted, CVVDP_PIXEL_FORMAT_RGB_UINT8, CVVDP_COLORSPACE_SRGB}; - FcvvdpResult expected, actual; - CHECK(cvvdp_compare_images(&ref, &dis, CVVDP_DISPLAY_STANDARD_FHD, 1, NULL, &expected) == CVVDP_OK); - CHECK(expected.jod > 0 && expected.jod < 10 && expected.quality > 0); - CHECK(cvvdp_compare_images(&ref, &dis, CVVDP_DISPLAY_STANDARD_FHD, 4, NULL, &actual) == CVVDP_OK); - same_result(actual, expected); - CHECK(cvvdp_compare_images(&ref, &ref, CVVDP_DISPLAY_STANDARD_FHD, 1, NULL, &actual) == CVVDP_OK); - CHECK(actual.jod == 10 && actual.quality == 0); - - FcvvdpImage r16 = {W, H, W * 6, ref16, CVVDP_PIXEL_FORMAT_RGB_UINT16, CVVDP_COLORSPACE_SRGB}; - FcvvdpImage d16 = {W, H, W * 6, dis16, CVVDP_PIXEL_FORMAT_RGB_UINT16, CVVDP_COLORSPACE_SRGB}; - CHECK(cvvdp_compare_images(&r16, &d16, CVVDP_DISPLAY_STANDARD_FHD, 1, NULL, &actual) == CVVDP_OK); - same_result(actual, expected); - FcvvdpImage rf = {W, H, W * 12, ref_float, CVVDP_PIXEL_FORMAT_RGB_FLOAT, CVVDP_COLORSPACE_LINEAR}; - FcvvdpImage df = {W, H, W * 12, dis_float, CVVDP_PIXEL_FORMAT_RGB_FLOAT, CVVDP_COLORSPACE_LINEAR}; - CHECK(cvvdp_compare_images(&rf, &df, CVVDP_DISPLAY_STANDARD_FHD, 1, NULL, &actual) == CVVDP_OK); - /* The caller's strict sRGB conversion and the library's fast-math - conversion can round differently before the nonlinear metric. */ - CHECK(fabs(actual.jod - expected.jod) < 0.000002); - CHECK(fabs(actual.quality - expected.quality) < 0.00002 * expected.quality); - FcvvdpResult float_expected = actual; - CHECK(cvvdp_compare_images(&rf, &df, CVVDP_DISPLAY_STANDARD_FHD, 4, NULL, &actual) == CVVDP_OK); - same_result(actual, float_expected); - - for (unsigned threads = 1; threads <= 4; threads += 3) { - FcvvdpCtx *ctx = NULL; - CHECK(cvvdp_create(W, H, 30, CVVDP_DISPLAY_STANDARD_FHD, threads, NULL, &ctx) == CVVDP_OK); - FcvvdpResult first; - CHECK(cvvdp_process_frame(ctx, &ref, &dis, &first) == CVVDP_OK); - for (int i = 0; i < 4; i++) - CHECK(cvvdp_process_frame(ctx, &ref, &dis, &actual) == CVVDP_OK); - CHECK(cvvdp_reset(ctx) == CVVDP_OK); - CHECK(cvvdp_process_frame(ctx, &ref, &dis, &actual) == CVVDP_OK); - same_result(actual, first); - FcvvdpImage wrong = dis; - wrong.width--; - CHECK(cvvdp_process_frame(ctx, &ref, &wrong, &actual) == CVVDP_ERROR_DIMENSION_MISMATCH); - cvvdp_destroy(ctx); - } - - FcvvdpCtx *ctx = NULL; - CHECK(cvvdp_create(0, H, 0, CVVDP_DISPLAY_STANDARD_FHD, 1, NULL, &ctx) == CVVDP_ERROR_INVALID_DIMENSIONS); - CHECK(ctx == NULL); - CHECK(cvvdp_create(W, H, 0, CVVDP_DISPLAY_STANDARD_FHD, 1, NULL, NULL) == CVVDP_ERROR_NULL_POINTER); - CHECK(cvvdp_compare_images(NULL, &dis, CVVDP_DISPLAY_STANDARD_FHD, 1, NULL, &actual) == CVVDP_ERROR_NULL_POINTER); - CHECK(cvvdp_process_frame(NULL, &ref, &dis, &actual) == CVVDP_ERROR_NULL_POINTER); - CHECK(cvvdp_reset(NULL) == CVVDP_ERROR_NULL_POINTER); - cvvdp_destroy(NULL); - for (int model = 0; model < CVVDP_DISPLAY_CUSTOM; model++) { - FcvvdpDisplayParams params; - CHECK(cvvdp_get_display_params(model, ¶ms) == CVVDP_OK); - CHECK(params.max_luminance > 0 && params.resolution_width > 0); - } - puts("C API: pixel formats, thread parity, temporal reset, presets and errors passed"); - return 0; -} diff --git a/tests/expected.json b/tests/expected.json deleted file mode 100644 index 675205f..0000000 --- a/tests/expected.json +++ /dev/null @@ -1,711 +0,0 @@ -{ - "png-0-8": { - "jod": 9.120004, - "quality": 25.06716, - "reference": "ref-png0-8.png", - "distorted": "dis-png0-8.png", - "width": 65, - "height": 49 - }, - "png-0-16": { - "jod": 9.120004, - "quality": 25.06716, - "reference": "ref-png0-16.png", - "distorted": "dis-png0-16.png", - "width": 65, - "height": 49 - }, - "png-2-8": { - "jod": 9.658901, - "quality": 9.049433, - "reference": "ref-png2-8.png", - "distorted": "dis-png2-8.png", - "width": 65, - "height": 49 - }, - "png-2-16": { - "jod": 9.658901, - "quality": 9.049433, - "reference": "ref-png2-16.png", - "distorted": "dis-png2-16.png", - "width": 65, - "height": 49 - }, - "png-4-8": { - "jod": 9.120004, - "quality": 25.06716, - "reference": "ref-png4-8.png", - "distorted": "dis-png4-8.png", - "width": 65, - "height": 49 - }, - "png-4-16": { - "jod": 9.120004, - "quality": 25.06716, - "reference": "ref-png4-16.png", - "distorted": "dis-png4-16.png", - "width": 65, - "height": 49 - }, - "png-6-8": { - "jod": 9.658901, - "quality": 9.049433, - "reference": "ref-png6-8.png", - "distorted": "dis-png6-8.png", - "width": 65, - "height": 49 - }, - "png-6-16": { - "jod": 9.658901, - "quality": 9.049433, - "reference": "ref-png6-16.png", - "distorted": "dis-png6-16.png", - "width": 65, - "height": 49 - }, - "qoi": { - "jod": 9.658901, - "quality": 9.049433, - "reference": "ref.qoi", - "distorted": "dis.qoi", - "width": 65, - "height": 49 - }, - "qoi-rgba": { - "jod": 9.658901, - "quality": 9.049433, - "reference": "ref-rgba.qoi", - "distorted": "dis-rgba.qoi", - "width": 65, - "height": 49 - }, - "P1": { - "jod": 8.574686, - "quality": 42.096754, - "reference": "ref-P1.pbm", - "distorted": "dis-P1.pbm", - "width": 65, - "height": 49 - }, - "P4": { - "jod": 8.574686, - "quality": 42.096754, - "reference": "ref-P4.pbm", - "distorted": "dis-P4.pbm", - "width": 65, - "height": 49 - }, - "P2-255": { - "jod": 9.120004, - "quality": 25.06716, - "reference": "ref-P2-255.pgm", - "distorted": "dis-P2-255.pgm", - "width": 65, - "height": 49 - }, - "P2-1023": { - "jod": 9.120004, - "quality": 25.06716, - "reference": "ref-P2-1023.pgm", - "distorted": "dis-P2-1023.pgm", - "width": 65, - "height": 49 - }, - "P2-65535": { - "jod": 9.120004, - "quality": 25.06716, - "reference": "ref-P2-65535.pgm", - "distorted": "dis-P2-65535.pgm", - "width": 65, - "height": 49 - }, - "P3-255": { - "jod": 9.658901, - "quality": 9.049433, - "reference": "ref-P3-255.ppm", - "distorted": "dis-P3-255.ppm", - "width": 65, - "height": 49 - }, - "P3-1023": { - "jod": 9.658901, - "quality": 9.049433, - "reference": "ref-P3-1023.ppm", - "distorted": "dis-P3-1023.ppm", - "width": 65, - "height": 49 - }, - "P3-65535": { - "jod": 9.658901, - "quality": 9.049433, - "reference": "ref-P3-65535.ppm", - "distorted": "dis-P3-65535.ppm", - "width": 65, - "height": 49 - }, - "P5-255": { - "jod": 9.120004, - "quality": 25.06716, - "reference": "ref-P5-255.pgm", - "distorted": "dis-P5-255.pgm", - "width": 65, - "height": 49 - }, - "P5-1023": { - "jod": 9.120004, - "quality": 25.06716, - "reference": "ref-P5-1023.pgm", - "distorted": "dis-P5-1023.pgm", - "width": 65, - "height": 49 - }, - "P5-65535": { - "jod": 9.120004, - "quality": 25.06716, - "reference": "ref-P5-65535.pgm", - "distorted": "dis-P5-65535.pgm", - "width": 65, - "height": 49 - }, - "P6-255": { - "jod": 9.658901, - "quality": 9.049433, - "reference": "ref-P6-255.ppm", - "distorted": "dis-P6-255.ppm", - "width": 65, - "height": 49 - }, - "P6-1023": { - "jod": 9.658901, - "quality": 9.049433, - "reference": "ref-P6-1023.ppm", - "distorted": "dis-P6-1023.ppm", - "width": 65, - "height": 49 - }, - "P6-65535": { - "jod": 9.658901, - "quality": 9.049433, - "reference": "ref-P6-65535.ppm", - "distorted": "dis-P6-65535.ppm", - "width": 65, - "height": 49 - }, - "pam-1": { - "jod": 9.120004, - "quality": 25.06716, - "reference": "ref-1.pam", - "distorted": "dis-1.pam", - "width": 65, - "height": 49 - }, - "pam-2": { - "jod": 9.120004, - "quality": 25.06716, - "reference": "ref-2.pam", - "distorted": "dis-2.pam", - "width": 65, - "height": 49 - }, - "pam-3": { - "jod": 9.658901, - "quality": 9.049433, - "reference": "ref-3.pam", - "distorted": "dis-3.pam", - "width": 65, - "height": 49 - }, - "pam-4": { - "jod": 9.658901, - "quality": 9.049433, - "reference": "ref-4.pam", - "distorted": "dis-4.pam", - "width": 65, - "height": 49 - }, - "y4m-420jpeg-False-t1": { - "jod": 9.825127, - "quality": 4.412597, - "reference": "ref-420jpeg-False.y4m", - "distorted": "dis-420jpeg-False.y4m", - "width": 65, - "height": 49, - "frames": 5 - }, - "y4m-420jpeg-False-t4": { - "jod": 9.825127, - "quality": 4.412597, - "reference": "ref-420jpeg-False.y4m", - "distorted": "dis-420jpeg-False.y4m", - "width": 65, - "height": 49, - "frames": 5 - }, - "y4m-420jpeg-True-t1": { - "jod": 9.841313, - "quality": 3.975074, - "reference": "ref-420jpeg-True.y4m", - "distorted": "dis-420jpeg-True.y4m", - "width": 65, - "height": 49, - "frames": 5 - }, - "y4m-420jpeg-True-t4": { - "jod": 9.841313, - "quality": 3.975074, - "reference": "ref-420jpeg-True.y4m", - "distorted": "dis-420jpeg-True.y4m", - "width": 65, - "height": 49, - "frames": 5 - }, - "y4m-420mpeg2-False-t1": { - "jod": 9.822302, - "quality": 4.489258, - "reference": "ref-420mpeg2-False.y4m", - "distorted": "dis-420mpeg2-False.y4m", - "width": 65, - "height": 49, - "frames": 5 - }, - "y4m-420mpeg2-False-t4": { - "jod": 9.822302, - "quality": 4.489258, - "reference": "ref-420mpeg2-False.y4m", - "distorted": "dis-420mpeg2-False.y4m", - "width": 65, - "height": 49, - "frames": 5 - }, - "y4m-420mpeg2-True-t1": { - "jod": 9.844083, - "quality": 3.900546, - "reference": "ref-420mpeg2-True.y4m", - "distorted": "dis-420mpeg2-True.y4m", - "width": 65, - "height": 49, - "frames": 5 - }, - "y4m-420mpeg2-True-t4": { - "jod": 9.844083, - "quality": 3.900546, - "reference": "ref-420mpeg2-True.y4m", - "distorted": "dis-420mpeg2-True.y4m", - "width": 65, - "height": 49, - "frames": 5 - }, - "y4m-420paldv-False-t1": { - "jod": 9.823276, - "quality": 4.462807, - "reference": "ref-420paldv-False.y4m", - "distorted": "dis-420paldv-False.y4m", - "width": 65, - "height": 49, - "frames": 5 - }, - "y4m-420paldv-False-t4": { - "jod": 9.823276, - "quality": 4.462807, - "reference": "ref-420paldv-False.y4m", - "distorted": "dis-420paldv-False.y4m", - "width": 65, - "height": 49, - "frames": 5 - }, - "y4m-420paldv-True-t1": { - "jod": 9.84645, - "quality": 3.836917, - "reference": "ref-420paldv-True.y4m", - "distorted": "dis-420paldv-True.y4m", - "width": 65, - "height": 49, - "frames": 5 - }, - "y4m-420paldv-True-t4": { - "jod": 9.84645, - "quality": 3.836917, - "reference": "ref-420paldv-True.y4m", - "distorted": "dis-420paldv-True.y4m", - "width": 65, - "height": 49, - "frames": 5 - }, - "y4m-420p9-False-t1": { - "jod": 9.822458, - "quality": 4.485021, - "reference": "ref-420p9-False.y4m", - "distorted": "dis-420p9-False.y4m", - "width": 65, - "height": 49, - "frames": 5 - }, - "y4m-420p9-False-t4": { - "jod": 9.822458, - "quality": 4.485021, - "reference": "ref-420p9-False.y4m", - "distorted": "dis-420p9-False.y4m", - "width": 65, - "height": 49, - "frames": 5 - }, - "y4m-420p9-True-t1": { - "jod": 9.844584, - "quality": 3.887067, - "reference": "ref-420p9-True.y4m", - "distorted": "dis-420p9-True.y4m", - "width": 65, - "height": 49, - "frames": 5 - }, - "y4m-420p9-True-t4": { - "jod": 9.844584, - "quality": 3.887067, - "reference": "ref-420p9-True.y4m", - "distorted": "dis-420p9-True.y4m", - "width": 65, - "height": 49, - "frames": 5 - }, - "y4m-420p10-False-t1": { - "jod": 9.821852, - "quality": 4.501496, - "reference": "ref-420p10-False.y4m", - "distorted": "dis-420p10-False.y4m", - "width": 65, - "height": 49, - "frames": 5 - }, - "y4m-420p10-False-t4": { - "jod": 9.821852, - "quality": 4.501496, - "reference": "ref-420p10-False.y4m", - "distorted": "dis-420p10-False.y4m", - "width": 65, - "height": 49, - "frames": 5 - }, - "y4m-420p10-True-t1": { - "jod": 9.844431, - "quality": 3.891184, - "reference": "ref-420p10-True.y4m", - "distorted": "dis-420p10-True.y4m", - "width": 65, - "height": 49, - "frames": 5 - }, - "y4m-420p10-True-t4": { - "jod": 9.844431, - "quality": 3.891184, - "reference": "ref-420p10-True.y4m", - "distorted": "dis-420p10-True.y4m", - "width": 65, - "height": 49, - "frames": 5 - }, - "y4m-420p12-False-t1": { - "jod": 9.821164, - "quality": 4.520158, - "reference": "ref-420p12-False.y4m", - "distorted": "dis-420p12-False.y4m", - "width": 65, - "height": 49, - "frames": 5 - }, - "y4m-420p12-False-t4": { - "jod": 9.821164, - "quality": 4.520158, - "reference": "ref-420p12-False.y4m", - "distorted": "dis-420p12-False.y4m", - "width": 65, - "height": 49, - "frames": 5 - }, - "y4m-420p12-True-t1": { - "jod": 9.843992, - "quality": 3.90298, - "reference": "ref-420p12-True.y4m", - "distorted": "dis-420p12-True.y4m", - "width": 65, - "height": 49, - "frames": 5 - }, - "y4m-420p12-True-t4": { - "jod": 9.843992, - "quality": 3.90298, - "reference": "ref-420p12-True.y4m", - "distorted": "dis-420p12-True.y4m", - "width": 65, - "height": 49, - "frames": 5 - }, - "y4m-420p16-False-t1": { - "jod": 9.822351, - "quality": 4.487903, - "reference": "ref-420p16-False.y4m", - "distorted": "dis-420p16-False.y4m", - "width": 65, - "height": 49, - "frames": 5 - }, - "y4m-420p16-False-t4": { - "jod": 9.822351, - "quality": 4.487903, - "reference": "ref-420p16-False.y4m", - "distorted": "dis-420p16-False.y4m", - "width": 65, - "height": 49, - "frames": 5 - }, - "y4m-420p16-True-t1": { - "jod": 9.844381, - "quality": 3.89252, - "reference": "ref-420p16-True.y4m", - "distorted": "dis-420p16-True.y4m", - "width": 65, - "height": 49, - "frames": 5 - }, - "y4m-420p16-True-t4": { - "jod": 9.844381, - "quality": 3.89252, - "reference": "ref-420p16-True.y4m", - "distorted": "dis-420p16-True.y4m", - "width": 65, - "height": 49, - "frames": 5 - }, - "model-fhd": { - "jod": 9.658901, - "quality": 9.049433, - "reference": "ref.qoi", - "distorted": "dis.qoi", - "width": 65, - "height": 49 - }, - "model-4k": { - "jod": 9.842048, - "quality": 3.955288, - "reference": "ref.qoi", - "distorted": "dis.qoi", - "width": 65, - "height": 49 - }, - "model-hdr_pq": { - "jod": 9.876833, - "quality": 3.027208, - "reference": "ref.qoi", - "distorted": "dis.qoi", - "width": 65, - "height": 49 - }, - "model-hdr_hlg": { - "jod": 9.876833, - "quality": 3.027208, - "reference": "ref.qoi", - "distorted": "dis.qoi", - "width": 65, - "height": 49 - }, - "model-hdr_linear": { - "jod": 9.876833, - "quality": 3.027208, - "reference": "ref.qoi", - "distorted": "dis.qoi", - "width": 65, - "height": 49 - }, - "model-hdr_dark": { - "jod": 9.876798, - "quality": 3.028141, - "reference": "ref.qoi", - "distorted": "dis.qoi", - "width": 65, - "height": 49 - }, - "model-hdr_zoom": { - "jod": 9.658, - "quality": 9.075116, - "reference": "ref.qoi", - "distorted": "dis.qoi", - "width": 65, - "height": 49 - }, - "model-standard_hmd": { - "jod": 9.545499, - "quality": 12.320523, - "reference": "ref.qoi", - "distorted": "dis.qoi", - "width": 65, - "height": 49 - }, - "model-standard_phone": { - "jod": 9.93753, - "quality": 1.459162, - "reference": "ref.qoi", - "distorted": "dis.qoi", - "width": 65, - "height": 49 - }, - "model-sdr_4k_30": { - "jod": 9.813164, - "quality": 4.737898, - "reference": "ref.qoi", - "distorted": "dis.qoi", - "width": 65, - "height": 49 - }, - "model-sdr_fhd_24": { - "jod": 9.690567, - "quality": 8.149538, - "reference": "ref.qoi", - "distorted": "dis.qoi", - "width": 65, - "height": 49 - }, - "model-htc_vive_pro": { - "jod": 9.493234, - "quality": 13.849965, - "reference": "ref.qoi", - "distorted": "dis.qoi", - "width": 65, - "height": 49 - }, - "model-iphone_12_pro": { - "jod": 9.971341, - "quality": 0.631387, - "reference": "ref.qoi", - "distorted": "dis.qoi", - "width": 65, - "height": 49 - }, - "model-iphone_14_pro": { - "jod": 9.970325, - "quality": 0.655499, - "reference": "ref.qoi", - "distorted": "dis.qoi", - "width": 65, - "height": 49 - }, - "model-iphone_14_pro_vert": { - "jod": 9.970325, - "quality": 0.655499, - "reference": "ref.qoi", - "distorted": "dis.qoi", - "width": 65, - "height": 49 - }, - "model-iphone_14_pro_hdr": { - "jod": 9.983552, - "quality": 0.347573, - "reference": "ref.qoi", - "distorted": "dis.qoi", - "width": 65, - "height": 49 - }, - "model-iphone_14_pro_hdr_vert": { - "jod": 9.983552, - "quality": 0.347573, - "reference": "ref.qoi", - "distorted": "dis.qoi", - "width": 65, - "height": 49 - }, - "model-ipad_pro_12_9": { - "jod": 9.867132, - "quality": 3.284267, - "reference": "ref.qoi", - "distorted": "dis.qoi", - "width": 65, - "height": 49 - }, - "model-macbook_pro_16": { - "jod": 9.891242, - "quality": 2.648209, - "reference": "ref.qoi", - "distorted": "dis.qoi", - "width": 65, - "height": 49 - }, - "model-lg_oled_2017_sdr": { - "jod": 9.948006, - "quality": 1.197856, - "reference": "ref.qoi", - "distorted": "dis.qoi", - "width": 65, - "height": 49 - }, - "model-lg_oled_2017_hdr": { - "jod": 9.967978, - "quality": 0.711369, - "reference": "ref.qoi", - "distorted": "dis.qoi", - "width": 65, - "height": 49 - }, - "model-eizo_CG3146": { - "jod": 9.82275, - "quality": 4.477085, - "reference": "ref.qoi", - "distorted": "dis.qoi", - "width": 65, - "height": 49 - }, - "model-65inch_hdr_pq_4knit": { - "jod": 9.925881, - "quality": 1.753576, - "reference": "ref.qoi", - "distorted": "dis.qoi", - "width": 65, - "height": 49 - }, - "model-65inch_hdr_pq_2knit": { - "jod": 9.925879, - "quality": 1.753649, - "reference": "ref.qoi", - "distorted": "dis.qoi", - "width": 65, - "height": 49 - }, - "model-65inch_hdr_pq_1knit": { - "jod": 9.925878, - "quality": 1.753685, - "reference": "ref.qoi", - "distorted": "dis.qoi", - "width": 65, - "height": 49 - }, - "model-lg_oled_2026_hdr_pq": { - "jod": 9.946435, - "quality": 1.236793, - "reference": "ref.qoi", - "distorted": "dis.qoi", - "width": 65, - "height": 49 - }, - "model-mcos": { - "jod": 9.72968, - "quality": 7.047582, - "reference": "ref.qoi", - "distorted": "dis.qoi", - "width": 65, - "height": 49 - }, - "identical-image": { - "jod": 10.0, - "quality": 0.0, - "reference": "ref.qoi", - "distorted": "ref.qoi", - "width": 65, - "height": 49 - }, - "identical-video": { - "jod": 10.0, - "quality": 0.0, - "reference": "ref-420jpeg-False.y4m", - "distorted": "ref-420jpeg-False.y4m", - "width": 65, - "height": 49, - "frames": 5 - } -} diff --git a/tests/fixtures.py b/tests/fixtures.py deleted file mode 100644 index c4dfbd8..0000000 --- a/tests/fixtures.py +++ /dev/null @@ -1,125 +0,0 @@ -"""Deterministic image/video inputs, generated with the Python standard library.""" - -import struct -import zlib -from pathlib import Path - - -def pixels(width, height, distorted=False, frame=0): - data = bytearray() - for y in range(height): - for x in range(width): - for c in range(3): - value = (x * (3 + c) + y * (7 - c) + frame * 11 + c * 53) % 256 - if distorted: - value = min(255, max(0, value + ((x + y + c + frame) % 13 - 6))) - data.append(value) - return bytes(data) - - -def png(path, width, height, rgb, color=2, depth=8): - def chunk(kind, data): - return (struct.pack(">I", len(data)) + kind + data - + struct.pack(">I", zlib.crc32(kind + data))) - - channels = {0: 1, 2: 3, 4: 2, 6: 4}[color] - raw = bytearray() - for y in range(height): - raw.append(0) # PNG filter: none - for x in range(width): - offset = (y * width + x) * 3 - p = rgb[offset:offset + 3] - samples = list(p if color in (2, 6) else p[:1]) - if color in (4, 6): - samples.append((x * 17 + y * 31) % 256) - assert len(samples) == channels - for value in samples: - raw.extend(bytes([value]) if depth == 8 else struct.pack(">H", value * 257)) - path.write_bytes(b"\x89PNG\r\n\x1a\n" - + chunk(b"IHDR", struct.pack(">IIBBBBB", width, height, depth, color, 0, 0, 0)) - + chunk(b"IDAT", zlib.compress(raw)) + chunk(b"IEND", b"")) - - -def y4m(path, width, height, frames=5, bits=8, chroma="420jpeg", full=False, - distorted=False): - header = (f"YUV4MPEG2 W{width} H{height} F30000:1001 Ip A1:1 C{chroma}" - f" XCOLORRANGE={'FULL' if full else 'LIMITED'}\n") - peak = (1 << bits) - 1 - scale = 1 << (bits - 8) - with path.open("wb") as out: - out.write(header.encode()) - for frame in range(frames): - out.write(b"FRAME\n") - for plane in range(3): - w, h = (width, height) if plane == 0 else ((width + 1) // 2, (height + 1) // 2) - values = bytearray() - for y in range(h): - for x in range(w): - value = (16 + (x * 3 + y * 5 + frame * 11) % 220 if plane == 0 - else 96 + (x * 7 + y * 3 + plane * 17 + frame * 2) % 65) - value = value * scale + ((x + y + frame) % scale) - if distorted: - value += ((x + y + plane + frame) % 7 - 3) * scale - value = min(peak, max(0, value)) - values.extend(bytes([value]) if bits == 8 else struct.pack("IIBB", width, height, 3, 0)) - for i in range(0, len(rgb), 3): - qoi.extend(b"\xfe" + rgb[i:i + 3]) - qoi.extend(b"\0\0\0\0\0\0\0\1") - (directory / f"{label}.qoi").write_bytes(qoi) - rgba_qoi = bytearray(b"qoif" + struct.pack(">IIBB", width, height, 4, 0)) - for i in range(width * height): - rgba_qoi.extend(b"\xff" + rgb[i * 3:i * 3 + 3] + bytes([(i * 17) % 256])) - rgba_qoi.extend(b"\0\0\0\0\0\0\0\1") - (directory / f"{label}-rgba.qoi").write_bytes(rgba_qoi) - bitmap = [int(v < 128) for v in gray] - (directory / f"{label}-P1.pbm").write_bytes( - f"P1\n{width} {height}\n".encode() + " ".join(map(str, bitmap)).encode()) - packed = bytearray() - for y in range(height): - for x in range(0, width, 8): - packed.append(sum(bitmap[y * width + x + b] << (7 - b) - for b in range(min(8, width - x)))) - (directory / f"{label}-P4.pbm").write_bytes(f"P4\n{width} {height}\n".encode() + packed) - for magic, ext, channels in (("P2", "pgm", 1), ("P3", "ppm", 3), - ("P5", "pgm", 1), ("P6", "ppm", 3)): - data = gray if channels == 1 else rgb - for maximum in (255, 1023, 65535): - samples = [v * maximum // 255 for v in data] - payload = (" ".join(map(str, samples)).encode() if magic in ("P2", "P3") - else bytes(samples) if maximum == 255 - else b"".join(struct.pack(">H", v) for v in samples)) - (directory / f"{label}-{magic}-{maximum}.{ext}").write_bytes( - f"{magic}\n# regression fixture\n{width} {height}\n{maximum}\n".encode() + payload) - for channels, kind in ((1, "GRAYSCALE"), (2, "GRAYSCALE_ALPHA"), (3, "RGB"), (4, "RGB_ALPHA")): - data = bytearray() - for i in range(width * height): - data.extend(rgb[i * 3:i * 3 + 3] if channels >= 3 else gray[i:i + 1]) - if channels in (2, 4): - data.append((i * 17) % 256) - (directory / f"{label}-{channels}.pam").write_bytes( - f"P7\nWIDTH {width}\nHEIGHT {height}\nDEPTH {channels}\nMAXVAL 255\nTUPLTYPE {kind}\nENDHDR\n".encode() + data) - for bits, chroma in ((8, "420jpeg"), (8, "420mpeg2"), (8, "420paldv"), - (9, "420p9"), (10, "420p10"), (12, "420p12"), (16, "420p16")): - for full in (False, True): - y4m(directory / f"{label}-{chroma}-{full}.y4m", 65, 49, - bits=bits, chroma=chroma, full=full, distorted=distorted) - png(directory / "wrong-size.png", 64, 48, pixels(64, 48)) - y4m(directory / "short.y4m", 65, 49, frames=1) - y4m(directory / "empty.y4m", 65, 49, frames=0) - (directory / "bad.png").write_bytes(b"not a png") - (directory / "bad.ppm").write_bytes(b"P6\n65 49\n255\n\0") - return directory diff --git a/tests/regression.py b/tests/regression.py deleted file mode 100644 index bb6aa29..0000000 --- a/tests/regression.py +++ /dev/null @@ -1,138 +0,0 @@ -#!/usr/bin/env python3 -"""Exercise CLI decoding, metric results, threading, presets, and error paths.""" - -import argparse -import json -import math -import subprocess -import sys -import tempfile -from pathlib import Path - -sys.dont_write_bytecode = True -from fixtures import generate - - -MODELS = ("fhd", "4k", "hdr_pq", "hdr_hlg", "hdr_linear", "hdr_dark", "hdr_zoom", - "standard_hmd", "standard_phone", "sdr_4k_30", "sdr_fhd_24", "htc_vive_pro", - "iphone_12_pro", "iphone_14_pro", "iphone_14_pro_vert", "iphone_14_pro_hdr", - "iphone_14_pro_hdr_vert", "ipad_pro_12_9", "macbook_pro_16", "lg_oled_2017_sdr", - "lg_oled_2017_hdr", "eizo_CG3146", "65inch_hdr_pq_4knit", "65inch_hdr_pq_2knit", - "65inch_hdr_pq_1knit", "lg_oled_2026_hdr_pq", "mcos") - - -def cases(): - result = {} - for color in (0, 2, 4, 6): - for bits in (8, 16): - suffix = f"-png{color}-{bits}.png" - result[f"png-{color}-{bits}"] = ["ref" + suffix, "dis" + suffix] - result["qoi"] = ["ref.qoi", "dis.qoi"] - result["qoi-rgba"] = ["ref-rgba.qoi", "dis-rgba.qoi"] - for magic in ("P1", "P4"): - result[magic] = [f"ref-{magic}.pbm", f"dis-{magic}.pbm"] - for magic, ext in (("P2", "pgm"), ("P3", "ppm"), ("P5", "pgm"), ("P6", "ppm")): - for maximum in (255, 1023, 65535): - suffix = f"-{magic}-{maximum}.{ext}" - result[f"{magic}-{maximum}"] = ["ref" + suffix, "dis" + suffix] - for channels in (1, 2, 3, 4): - result[f"pam-{channels}"] = [f"ref-{channels}.pam", f"dis-{channels}.pam"] - for chroma in ("420jpeg", "420mpeg2", "420paldv", "420p9", "420p10", "420p12", "420p16"): - for full in (False, True): - for threads in (1, 4): - suffix = f"-{chroma}-{full}.y4m" - result[f"y4m-{chroma}-{full}-t{threads}"] = ["-t", str(threads), "ref" + suffix, "dis" + suffix] - for model in MODELS: - result[f"model-{model}"] = ["-m", model, "ref.qoi", "dis.qoi"] - result["identical-image"] = ["ref.qoi", "ref.qoi"] - result["identical-video"] = ["-t", "1", "ref-420jpeg-False.y4m", "ref-420jpeg-False.y4m"] - return result - - -def invoke(binary, args, directory): - return subprocess.run([str(binary), *args], cwd=directory, capture_output=True, - text=True, timeout=60) - - -def metric(binary, args, directory): - run = invoke(binary, ["-j", *args], directory) - if run.returncode: - raise AssertionError(f"{args}: exit {run.returncode}\n{run.stdout}{run.stderr}") - # The established CLI prints its banner and JSON on stderr. - text = run.stderr - data = json.loads(text[text.index("{"):]) - for key in ("jod", "quality"): - if not math.isfinite(data[key]): - raise AssertionError(f"{args}: non-finite {key}") - return data - - -def compare(actual, expected, name): - if actual.keys() != expected.keys(): - raise AssertionError(f"{name}: JSON fields differ") - for key, value in expected.items(): - # Fixed limits for compiler/libm/SIMD rounding: 2 micro-JOD and - # 10 parts per million in the internal quality accumulator. - equal = (math.isclose(actual[key], value, rel_tol=0, abs_tol=2e-6) - if key == "jod" else - math.isclose(actual[key], value, rel_tol=1e-5, abs_tol=2e-6) - if key == "quality" else actual[key] == value) - if not equal: - raise AssertionError(f"{name}.{key}: {actual[key]!r} != {value!r}") - - -def main(): - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument("binary", type=lambda p: Path(p).resolve()) - parser.add_argument("--baseline", type=lambda p: Path(p).resolve()) - parser.add_argument("--record", type=Path, help="Record snapshots from a trusted baseline") - args = parser.parse_args() - golden_path = Path(__file__).with_name("expected.json") - golden = {} if args.record else json.loads(golden_path.read_text()) - outputs = {} - with tempfile.TemporaryDirectory(prefix="fcvvdp-regression-") as tmp: - directory = generate(tmp) - for name, case in cases().items(): - actual = metric(args.binary, case, directory) - if args.baseline: - compare(actual, metric(args.baseline, case, directory), name) - if not args.record: - compare(actual, golden[name], name) - outputs[name] = actual - for threads in (1, 4): - for chroma in ("420jpeg", "420mpeg2", "420paldv", "420p9", "420p10", "420p12", "420p16"): - for full in (False, True): - compare(outputs[f"y4m-{chroma}-{full}-t{threads}"], - outputs[f"y4m-{chroma}-{full}-t1"], "thread parity") - for name in ("identical-image", "identical-video"): - if outputs[name]["jod"] != 10 or outputs[name]["quality"] != 0: - raise AssertionError(f"{name}: identical inputs must score 10 JOD / 0 quality") - errors = [([], "MissingFiles"), (["--unknown"], "UnknownOption"), - (["-m"], "InvalidArguments"), (["-t"], "InvalidArguments"), - (["ref.qoi", "dis.qoi", "ref.qoi"], "TooManyFiles"), - (["ref.qoi", "ref-420jpeg-False.y4m"], "MismatchedInputTypes"), - (["ref.qoi", "wrong-size.png"], "DimensionMismatch"), - (["ref-420jpeg-False.y4m", "short.y4m"], "FrameCountMismatch"), - (["empty.y4m", "empty.y4m"], "EmptyY4M"), - (["missing.png", "dis.qoi"], "FileNotFound"), - (["bad.png", "dis.qoi"], "GetHeaderFailed")] - errors.append((["bad.ppm", "dis.qoi"], "InsufficientData")) - for case, error in errors: - run = invoke(args.binary, case, directory) - if run.returncode <= 0 or f"error: {error}" not in run.stderr: - raise AssertionError(f"{case}: expected {error}, got {run.returncode}\n{run.stderr}") - for option in ("-h", "--help"): - run = invoke(args.binary, [option], directory) - if run.returncode or "usage: fcvvdp" not in run.stderr: - raise AssertionError(f"{option}: help failed") - for option in ([], ["-v"]): - run = invoke(args.binary, [*option, "ref.qoi", "dis.qoi"], directory) - if run.returncode or ("JOD Score:" if option else "JOD:") not in run.stderr: - raise AssertionError(f"{option}: text output failed") - if args.record: - args.record.write_text(json.dumps(outputs, indent=2) + "\n") - print(f"{len(outputs)} metric cases, {len(errors)} error paths, help/text output and thread parity passed") - - -if __name__ == "__main__": - main() From 2d82c442921ed29d8e379faaad509d8585083937 Mon Sep 17 00:00:00 2001 From: Gianni Date: Tue, 20 Oct 2026 23:07:57 -0700 Subject: [PATCH 4/4] Remove comments from build.zig --- build.zig | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/build.zig b/build.zig index c0b5ee7..a98de03 100644 --- a/build.zig +++ b/build.zig @@ -1,17 +1,3 @@ -// Copyright © 2026, Halide Compression, LLC. -// All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at: -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. const std = @import("std"); pub fn build(b: *std.Build) void { @@ -26,14 +12,12 @@ pub fn build(b: *std.Build) void { } const options = b.addOptions(); - // simpleimgio const simpleimgio_dep = b.dependency("simpleimgio", .{ .target = target, .optimize = optimize, }); const simpleimgio = simpleimgio_dep.module("simpleimgio"); - // libspng const spng_dep = b.dependency("spng", .{ .target = target, .optimize = optimize, @@ -50,7 +34,6 @@ pub fn build(b: *std.Build) void { translate_c.addIncludePath(spng.getEmittedIncludeTree()); const c_module = translate_c.createModule(); - // 'libcvvdp.a' static lib const cvvdp = b.addLibrary(.{ .name = "cvvdp", .use_lld = if (flto) true else null, @@ -80,8 +63,6 @@ pub fn build(b: *std.Build) void { }); if (target_result.cpu.arch == .x86_64) { - // Compile the SIMD object with its own CPU features. Zig's target - // configuration takes precedence over per-file -mavx2/-mfma flags. var avx2_query = target.query; avx2_query.cpu_features_sub.removeFeature(@backingInt(std.Target.x86.Feature.avx2)); avx2_query.cpu_features_sub.removeFeature(@backingInt(std.Target.x86.Feature.fma)); @@ -115,10 +96,8 @@ pub fn build(b: *std.Build) void { cvvdp.lto = if (flto) .full else null; b.installArtifact(cvvdp); - // cvvdp.h cvvdp.installHeader(b.path("src/cvvdp.h"), "cvvdp.h"); - // 'fcvvdp' executable const cvvdpenc = b.addExecutable(.{ .name = "fcvvdp", .root_module = b.createModule(.{