Skip to content

ALI-2420: The shared libmpv playback crate, with colour tests - #1

Merged
michaelaguiar merged 4 commits into
mainfrom
ali-2420-playback-crate
Dec 2, 2026
Merged

michaelaguiar merged 4 commits into
mainfrom
ali-2420-playback-crate

Conversation

@michaelaguiar

@michaelaguiar michaelaguiar commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

Linear: ALI-2420

The first version of the crate Alias View, Gannet and Nitrate share for playback through libmpv. Alias View's video and music (ALI-2358) build on it next. Gannet and Nitrate each get a follow-up ticket to move onto it.

What's in it

  • Player: opens libmpv at run time (libmpv.so.2, client API 2.x), so an app starts without it. It sets the shared options and runs commands (load, play, pause, seek, volume, mute). Its events report position, duration, pause, volume, mute, the end and failure.
  • SwRender, GlRender: libmpv's CPU render API (frames as XRGB8888, ready for wl_shm) and its OpenGL render API.
  • Options and options::colour: the setup every app shares, plus the colour settings for each render path in one place.
  • helper and jail: Gannet's jailed CPU player, moved here. It covers both sides: the app's start, watch, Frames and Remote, and the player's run, which takes the app's seccomp filter as lock_down. The jail rules are Gannet's. Changes from Gannet's version:
    • the state line also says whether it has stopped on the last frame;
    • seekat seeks to a time (for MPRIS);
    • the player has no timer at rest, because it follows libmpv's property events instead of polling every 500 ms.

Every app behaves the same where it should:

  • End of a video: it stops on its last frame instead of closing. Play then starts again from the beginning.
  • The user's own mpv setup: none of it is read.
  • Files beside the one opened: nothing is looked for.

Hardware decoding tries NVDEC, VA-API and V4L2 by name, then falls back to the CPU. The software renderer gets the copy-back methods only. libmpv's own auto-safe list wasn't used because it includes VDPAU. On the RTX 4090, VDPAU accepts H.264 4:4:4, fails every frame and never falls back, so the video never starts. The GPU colour bench found this.

Colour tests

51 clips, each a 4x4 grid of solid patches: black, white, greys, primaries, secondaries, skin tones and Tarnished's #121C24.

  • Coverage:
    • all 27 combinations of range (limited, full, yuvj), matrix (601, 709, 2020) and transfer tag (709, sRGB, none) on H.264;
    • H.264, HEVC, VP9 and AV1 at 8 and 10 bits, 4:2:0 and 4:4:4;
    • each codec with full range and with the sRGB tag.
  • Expected values come from the source RGB. The test works out each clip's Y'CbCr itself, and FFmpeg encodes it losslessly without converting anything. Before a clip plays, two checks confirm the clip itself is right:
    • ffprobe must read back the case's tags;
    • FFmpeg must decode the clip back to exactly that Y'CbCr.
  • Pass: every pixel inside each patch is within 2 steps per channel.
Machine Renderer Result
Desktop CPU, as CI runs it (full size, half size) 51 of 51
Desktop, RTX 4090 GPU without hardware decoding / with it (NVDEC on 44 clips) / CPU with NVDEC copy-back 51 of 51 each
M1 Max, Asahi GPU with and without hardware decoding, CPU with it (all decoded on the CPU) 49 of 49, 2 skipped

The M1's 2 skipped clips are 10-bit HEVC, which its 8-bit-only libx265 can't make.

Without the CPU colour setting (vf=format=gamma=bt.1886, from ALI-2418), the sRGB-tagged clips draw up to 13 steps too light (#121C24 → #1E2831). The tests also found the same lift in full-range clips with no transfer tag, which the setting fixes too. The CPU run prints this for each case.

Not yet covered:

  • HDR (PQ, HLG), which needs its own reference and a wider tolerance;
  • real wf-recorder and gpu-screen-recorder recordings checked against screenshots;
  • primaries other than BT.709.

CI

CI runs on GitHub's hosted runners in an Arch container, with Arch's libmpv and FFmpeg. It runs cargo fmt --check, clippy -D warnings, and cargo test: unit tests, a real out-of-process helper test (without bubblewrap, because CI containers have no user namespaces) and the CPU colour tests.

Review

Three review rounds; the findings and decisions are in the PR comments. The fixes:

  • the jail's program path after an upgrade;
  • Matroska ordered chapters and file references turned off;
  • position, duration and end cleared when a file closes;
  • GlRender kept on its thread;
  • load always reads a file;
  • watch cleanup.

How to check

make check        # needs ffmpeg/ffprobe and libmpv
make colour-gpu   # on a machine with a graphics card: the GPU bench

One crate for Alias View, Gannet and Nitrate to play through libmpv:

- Player: libmpv opened at run time, the shared options, commands, and
  events for position, duration, pause, volume, mute, end and failure.
- SwRender and GlRender: libmpv's CPU and OpenGL render APIs.
- options::colour: the colour settings for each render path in one
  place. The CPU path keeps Gannet's BT.1886 relabel (ALI-2418).
- helper and jail: Gannet's jailed CPU player, with frames in shared
  memory, moved here so any app can use it.

Every app stops on a video's last frame instead of closing. Hardware
decoding tries NVDEC, VA-API and V4L2 by name. VDPAU is left out because
it stalls on H.264 4:4:4 on NVIDIA.

The colour tests play 51 clips of solid patches, made from Y'CbCr worked
out from source RGB and encoded losslessly. The clips cover range,
matrix, transfer tag, bit depth, chroma and codec. CI runs them on the CPU
renderer, and examples/colour-gpu.rs runs them on a graphics card.
@linear-code

linear-code Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
ALI-2420 One shared libmpv playback crate for Alias View, Gannet and Nitrate, with colour tests that prove exact colour

Why

Three of our apps play video through libmpv, each set up its own way:

  • Gannet's preview uses the CPU renderer inside a jail (crates/app/src/media.rs).
  • Nitrate's player uses the GPU renderer (spike/src/mpv.rs, render.rs).
  • Alias View will play video next (ALI-2358) and replace mpv in aliasOS (ALI-2360).

On 2026-11-30 an M1 screen recording drew lighter and greyer in Gannet's preview than in the mpv window (ALI-2417, ALI-2418). libmpv wasn't at fault: one rendering mode (the CPU renderer's zimg path) handled one tag (an sRGB transfer) differently, and nothing tested for it.

Decision (Mike, 2026-11-30): keep libmpv, the standard. Don't write our own decoder or player. Build one shared crate that all three apps use, and prove its colour with tests.

What the crate does

  • Wraps libmpv once: loading it, the options, the render API (CPU and GPU), and the events the apps need (position, pause, end, volume).
  • Holds the colour settings in one place, so a fix lands in every app at once.
  • Keeps Gannet's jail working: the CPU renderer in a sandboxed helper with frames over shared memory, as it is today.
  • Behaves the same in every app where it should: for example, stopping on the last frame when a video ends instead of closing (Mike's requirement on ALI-2358).

Colour tests

A set of test clips whose correct pixel values are known, played through every rendering mode we ship. A clip passes when every patch lands within the tolerance of its expected colour.

The clips. Solid colour patches, generated from known RGB values. They include black, white, greys, the primaries, skin tones, and real theme colours such as Tarnished's #121C24. The clips cover combinations of:

  • range: limited, full, and full labelled yuvj;
  • matrix: BT.601, BT.709, BT.2020;
  • transfer tag: BT.709, sRGB, none;
  • bit depth: 8 and 10;
  • chroma: 4:2:0 and 4:4:4;
  • codec: H.264, HEVC, VP9, AV1.

Real recordings are included too: one from each of our screen recorders (wf-recorder on the M1, gpu-screen-recorder on the desktop), checked against a screenshot taken at the same moment.

Expected values come from the source RGB, not from another player's output, so the tests can't inherit someone else's bug.

Tolerance:

  • 2 steps per channel for 8-bit SDR.
  • HDR (PQ, HLG) tone-maps by design, so it's checked against its own reference with a wider, stated tolerance.

Where they run:

  • In CI: the CPU renderer, without the GPU, on every PR to the crate.
  • On the desktop (NVIDIA) and the M1 (Asahi): the GPU renderer, with and without hardware decoding, as a bench step before a release. The runners have no GPU.

When a case fails: first look for an mpv option that fixes it (for example sws-allow-zimg=no for ALI-2418). Write our own conversion for that path, probably a GPU shader, only if no option does. The tests then prove it.

Done when

  • The crate exists, and Gannet's preview runs on it with its jail unchanged.
  • Alias View's video playback (ALI-2358) is built on it.
  • Nitrate moves to it, or has a ticket to.
  • The colour tests pass in CI (CPU) and on both machines (GPU), and ALI-2418's sRGB case is among them.

Open questions for Mike

  1. Where should the crate live: its own repo, or inside Alias View's with the others depending on it?
  2. Should Nitrate's Apple apps (libmpv on macOS, tvOS and iOS) run the same clips, or only the Linux apps?

+++ Agent notes

  • Today's measurements (M1, 2026-11-30), with a desktop pixel of #121C24:
    • wf-recorder as it was: recorded #1F2830 (limited range labelled full).
    • Full range tagged sRGB: ffmpeg #111C23, mpv CPU #1D282F, mpv window #101C23.
    • Full range tagged BT.709: ffmpeg #111C23, mpv CPU #101C24, mpv window #101C23.
  • To test the CPU renderer without the app: mpv --no-config --hwdec=no --vo=image --vo-image-format=png --start=1 --frames=1.
  • To test the mpv window: mpv --fs --pause --start=1, then grim -o <output> and crop a patch.
  • Patches can be generated with ffmpeg color=c=0x121C24 sources, encoded with an explicit scale=out_range=…:out_color_matrix=… and -color_range/-colorspace/-color_trc/-color_primaries. Check the tags with ffprobe after encoding: an H.264 colr box can differ from the VUI.

+++

Review in Linear

- jail::own_exe no longer falls back to /proc/self/exe. bubblewrap resolves
  that path in its own process and binds itself, so a video opened after the
  app's file was removed ran bwrap as the helper and failed as a jail error.
  It now fails plainly instead.
- ordered-chapters and access-references are off. A Matroska file could
  otherwise make an unjailed player read other files for its chapters, and
  a playlist could name anything.
- When a file closes, the position and duration go back to 0 rather than
  keeping the last file's values (new helper test, which fails without
  this change).
- GlRender can't move between threads. Dropping it frees libmpv's GL state,
  which needs its context current.
- watch stops the player when try_wait fails, as its documentation says.
- start's documentation says the player dies with the thread that started it.
- The GPU bench skips a case only when the machine's encoder can't make it.
  A missing FFmpeg or a clip that fails its check is now a failure.
- The CPU colour setting's comment covers full-range video with no
  transfer tag too.
- The unused rustix process feature is removed.
@michaelaguiar

Copy link
Copy Markdown
Member Author

Review round 1 (fresh reviewer; code-review ran). Fixes are in 24c2c60.

Fixed

  1. P2, jail::own_exe. It fell back to /proc/self/exe when the app's file was gone. bubblewrap resolves that path in its own process, so it bound bwrap itself (the reviewer reproduced this). It now returns an error instead. A /proc/<pid>/exe path can't be used either: bwrap is refused access to another process's link. The unit test covers the removed-file case.
  2. P3, watch. A try_wait error now stops the player before returning, as the documentation says.
  3. P3, files beside the one opened. ordered-chapters and access-references are now no. Before, a Matroska file with ordered chapters, or a playlist or EDL file, could make an unjailed player read other files. This is in the options test, and the README says so.
  4. P3, GlRender could be sent to another thread. It's no longer Send, because dropping it frees libmpv's GL state, which needs the context current.
  5. P3, the start documentation. It now says the player dies with the thread that started it (PR_SET_PDEATHSIG and --die-with-parent).
  6. P3, stale position and duration. When a property becomes unavailable, such as after a file is closed, the position and duration now go back to 0. A new helper test covers it, and it fails without the fix.
  7. P3, GPU bench skips. It skips a case only when the machine's encoder can't make the format (the M1's 8-bit-only libx265). A missing FFmpeg, or a clip that fails its integrity check, is a failure.
  8. P3, a comment. The CPU colour setting's comment now mentions full-range video with no transfer tag.
  9. P3, an unused feature. rustix's process feature is removed.

Declined

  • P3, failing on a libmpv that lacks an option. If a base option such as keep-open-pause or input-builtin-bindings is missing, Player::new fails. These options are mpv 0.24+ and 0.35+, Ubuntu 24.04 has 0.37 and aliasOS has 0.42. A player that silently loses "stop on the last frame" or gets mpv's own key bindings is worse than one that says it can't start. The colour settings stay best-effort, as before.
  • P3, libc and rustix repeated in dev-dependencies. Tests and examples can't use the library's own dependencies directly, so these are needed.

Checked and held (by the reviewer):

  • the FFI teardown order, callback lifetimes, event-data lifetimes and the SW buffer bounds check;
  • no busy-spinning or missed wakeups in the helper loop;
  • the colour test can't pass with a wrong player;
  • format=gamma=bt.1886 leaves a PQ clip unchanged.

- When a file closes, the end also goes back to not reached (Ended(false)),
  as the position and duration already do. Before, an app that had shown
  "ended" kept showing it with nothing loaded. The helper test now checks it,
  and fails without this.
- Player::load always passes a path libmpv reads as a file: a relative one
  gets "./" in front. A relative path shaped like "http://host/x" or
  "edl://..." would otherwise be fetched or built by libmpv.
@michaelaguiar

Copy link
Copy Markdown
Member Author

Review round 2 (fresh reviewer; code-review ran). It reviewed 24c2c60 and found all eight round-1 fixes hold.

Fixed in 827a83c

  1. P3, new in 24c2c60: the end didn't clear. When a file closes, eof-reached also goes unavailable, but no Ended(false) followed. An app that showed "ended" kept showing it with nothing loaded. The end now clears with the position and duration. The helper test checks it, and it fails without the fix.
  2. P3: Player::load and URL-shaped paths. A relative path shaped like http://host/x or edl://... would have been fetched or built by libmpv. A relative path now gets ./ in front, so libmpv always reads it as a file. A unit test covers it.

Declined

  • Remote::say gluing half-written lines. I disagree. Every command line is under 64 bytes, and a pipe write up to PIPE_BUF (4096) is atomic. A non-blocking write of that size either goes in whole or fails with EAGAIN, so a line can't be split.
  • parts_for_test is public. That's deliberate: it exists for apps' own tests of their player handling. Gannet's tests use it today.
  • HDR through format=gamma=bt.1886. Out of scope here, as the ticket and the README say. Round 1's reviewer rendered a PQ clip with and without the filter and got the same pixels. HDR clips are listed as not yet covered.

@michaelaguiar

Copy link
Copy Markdown
Member Author

Review round 3 (final, limited to 827a83c). The code-review skill's forked run looked in the wrong repository, so its output was discarded. The reviewer reviewed the commit by hand in the PR's worktree and ran cargo test --locked, which passed.

  • Ended(false) when a file closes holds. With keep-open, eof-reached stays true at the end. It only becomes unavailable when a file closes, so the event can't arrive during playback. A load right after an end gives one extra false, which does no harm, since apps treat Ended as a state.
  • as_file holds. libmpv only reads a path as a protocol when it starts with scheme://, so ./ is enough. access-references, ytdl and load-scripts cover the rest.
  • Declined (P3): load("") now opens the current folder as a playlist instead of failing. Apps pass the paths of real files, and this reads nothing outside the folder the app is already in. That isn't worth a change.

No P1 or P2 findings. The review is clean, and CI is green on 827a83c.

@michaelaguiar
michaelaguiar marked this pull request as ready for review December 2, 2026 13:07
@michaelaguiar
michaelaguiar merged commit 2cd7185 into main Dec 2, 2026
1 check passed
@michaelaguiar
michaelaguiar deleted the ali-2420-playback-crate branch December 3, 2026 00:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant