diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..2ad197d --- /dev/null +++ b/.gitattributes @@ -0,0 +1,5 @@ +# Patch files are data, not source: they are applied to files whose line +# endings are decided by whoever fetched them. core.autocrlf would otherwise +# rewrite them on checkout, and the rewritten patch no longer matches the +# LF-terminated sources it is meant to apply to. +*.patch -text diff --git a/README.md b/README.md index 9b52d0d..4e3e1fc 100644 --- a/README.md +++ b/README.md @@ -7,8 +7,9 @@ documentation (library usage, speed testing, MOS correlation, etc). ## Usage -Compilation requires [Zig](https://ziglang.org/) ≥0.16.0 & a macOS, Linux, or -Unix-like operating system. To compile, run: +Compilation requires [Zig](https://ziglang.org/) ≥0.16.0. macOS, Linux, and +Windows are supported; Windows needs one extra step, described below. To +compile, run: ```sh zig build --release=fast @@ -16,6 +17,27 @@ zig build --release=fast You may add `-Dflto=true` for FLTO, and `-Dstrip=true` to strip the binary. +By default this builds the `fmetrics` binary and a static library. To also +build an installable shared library, pass `-Dshared=true`: + +```sh +zig build --release=fast -Dshared=true +``` + +On Windows, `fcvvdp` needs a small portability fix before the build will +succeed. It is carried as a patch in [`patches/`](patches/): + +```sh +zig build # populates zig-pkg/ +git apply --directory="zig-pkg/fcvvdp-" \ + patches/0001-windows-portability.patch +zig build --release=fast -Dshared=true +``` + +`--directory` is needed because Zig extracts dependencies without their `.git` +directory, so running `git apply` from inside `zig-pkg/fcvvdp-*/` reports +`Skipped patch`. + `fmetrics` binary usage: ``` diff --git a/build.zig b/build.zig index 5d3e44f..4d582a5 100644 --- a/build.zig +++ b/build.zig @@ -19,6 +19,7 @@ pub fn build(b: *std.Build) void { 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 shared = b.option(bool, "shared", "also build and install a shared library (.dll/.so/.dylib), defaults to false") orelse false; const options = b.addOptions(); const zon = b.build_root.handle.readFileAlloc( b.graph.io, @@ -164,6 +165,39 @@ pub fn build(b: *std.Build) void { // fmetrics.h lib.installHeader(b.path("src/fmetrics.h"), "fmetrics.h"); + // 'libfmetrics' shared library, built only when -Dshared=true. + // + // The static library above is linked in whole rather than having its source + // list repeated, so the two cannot drift apart: the DLL is the same code, + // packaged for dynamic loading. Only the export list is platform-specific. + if (shared) { + const shared_lib = b.addLibrary(.{ + .name = "fmetrics", + .linkage = .dynamic, + .root_module = b.createModule(.{ + .target = target, + .optimize = optimize, + .link_libc = true, + }), + }); + shared_lib.root_module.linkLibrary(lib); + // Only Windows needs an explicit export list; a DLL there exports + // nothing by default. On ELF and Mach-O the symbols are already visible, + // and naming them would only narrow visibility. + if (target.result.os.tag == .windows) { + shared_lib.win32_module_definition = b.path("src/fmetrics.def"); + } + // On Windows a DLL also produces an import library, installed as + // 'lib/.lib' -- the same path the static library above already + // occupies, so the two would overwrite each other. The static library + // already serves every purpose the import library would here, so skip + // it and leave 'lib/fmetrics.lib' alone. + b.getInstallStep().dependOn(&b.addInstallArtifact( + shared_lib, + .{ .implib_dir = .disabled }, + ).step); + } + // 'fmetrics' executable const bin = b.addExecutable(.{ .name = "fmetrics", diff --git a/patches/0001-windows-portability.patch b/patches/0001-windows-portability.patch new file mode 100644 index 0000000..e3b98ec --- /dev/null +++ b/patches/0001-windows-portability.patch @@ -0,0 +1,55 @@ +diff --git a/src/cvvdp.c b/src/cvvdp.c +index bcafd12..11d9319 100644 +--- a/src/cvvdp.c ++++ b/src/cvvdp.c +@@ -26,6 +26,10 @@ + #include + #include + ++#ifdef _WIN32 ++#include ++#endif ++ + static const CvvdpSimdDispatch* g_cvvdp_dispatch = NULL; + static pthread_once_t g_cvvdp_dispatch_once = PTHREAD_ONCE_INIT; + +@@ -126,7 +130,11 @@ static void* cvvdp_thread_pool_worker(void* arg) { + } + + static unsigned get_threadcnt(const unsigned threads) { ++#ifdef _WIN32 ++ return threads ? threads : GetActiveProcessorCount(ALL_PROCESSOR_GROUPS); ++#else + return threads ? threads : (unsigned)sysconf(_SC_NPROCESSORS_ONLN); ++#endif + } + + static CvvdpThreadPool* cvvdp_thread_pool_create(const unsigned total_threads) { +@@ -1228,13 +1236,11 @@ static FcvvdpError cvvdp_process_pyramid_threaded(FcvvdpCtx* const c, + const int num_levels = c->num_bands; + CvvdpThreadPool* const pool = c->thread_pool; + +- size_t total_size = 0; + int widths[CVVDP_MAX_LEVELS], heights[CVVDP_MAX_LEVELS]; + int tw = w, th = h; + for (int lev = 0; lev < num_levels; lev++) { + widths[lev] = tw; + heights[lev] = th; +- total_size += (size_t)tw * th; + tw = (tw + 1) / 2; + th = (th + 1) / 2; + } +@@ -1494,13 +1500,11 @@ static FcvvdpError cvvdp_process_pyramid_serial(FcvvdpCtx* const c, + const int w = c->width, h = c->height; + const int num_levels = c->num_bands; + +- size_t total_size = 0; + int widths[CVVDP_MAX_LEVELS], heights[CVVDP_MAX_LEVELS]; + int tw = w, th = h; + for (int lev = 0; lev < num_levels; lev++) { + widths[lev] = tw; + heights[lev] = th; +- total_size += (size_t)tw * th; + tw = (tw + 1) / 2; + th = (th + 1) / 2; + } diff --git a/src/fmetrics.def b/src/fmetrics.def new file mode 100644 index 0000000..e90d787 --- /dev/null +++ b/src/fmetrics.def @@ -0,0 +1,36 @@ +; Windows module definition for the optional shared library (-Dshared=true). +; +; A DLL exports nothing by default, so every entry point declared in +; fmetrics.h has to be named here or it stays private to the library and +; GetProcAddress cannot find it. Keep this list in step with the public +; declarations in fmetrics.h. +; +; fmetrics_workspace_reserve_data is intentionally absent: it is internal +; plumbing, not part of the published ABI. +; +; This file is ignored on ELF and Mach-O, where symbols are exported by +; default and listing them would only narrow visibility. + +EXPORTS + fmetrics_version_str + fmetrics_error_str + + fmetrics_workspace_create + fmetrics_workspace_destroy + + fmetrics_iwssim_cmp + fmetrics_msssim_cmp + + fmetrics_ssimu2_cmp + fmetrics_ssimu2_cmp_map + + fmetrics_butteraugli_cmp + fmetrics_butteraugli_cmp_map + + fmetrics_cvvdp_create + fmetrics_cvvdp_destroy + fmetrics_cvvdp_process_frame + fmetrics_cvvdp_reset + fmetrics_cvvdp_cmp + fmetrics_cvvdp_get_display_params + fmetrics_cvvdp_version_str \ No newline at end of file