Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Runs on GitHub's hosted runners, as the org's other open-source repositories do (the home
# runners take private ones only), in an Arch container, where aliasOS's packages are built:
# the libmpv and FFmpeg here are the ones aliasOS ships.
#
# The colour tests run on libmpv's software renderer, which needs no graphics card. The GPU
# renderer's run (`examples/colour-gpu.rs`) is a bench step on the desktop and the M1.
name: CI

on:
push:
branches: [main]
pull_request:
workflow_dispatch:

# A PR's runs share a group, so a new push cancels the one it supersedes.
# Each push to main has a group of its own, so every commit gets a result.
concurrency:
group: ci-${{ github.event_name == 'pull_request' && github.ref || github.run_id }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}

# The jobs run pull request code: the token reads only.
permissions:
contents: read

env:
CARGO_TERM_COLOR: always

jobs:
check:
name: fmt, clippy, tests and colour tests
runs-on: ubuntu-24.04
container: archlinux:latest
timeout-minutes: 30
steps:
# git before checkout, or checkout falls back to a tarball. FFmpeg makes the test clips
# (libx264, libx265, libvpx and libaom come with Arch's), and mpv brings libmpv.
- name: Install build dependencies
run: pacman -Syu --noconfirm --needed git rust gcc pkgconf mpv ffmpeg >/dev/null
- uses: actions/checkout@v5
with:
persist-credentials: false
- uses: actions/cache@v4
with:
path: |
.ci/cargo/registry
target
key: check-${{ hashFiles('Cargo.lock') }}
restore-keys: check-
- name: cargo fmt
run: cargo fmt --check
- name: cargo clippy
run: CARGO_HOME="$PWD/.ci/cargo" cargo clippy --all-targets --locked -- -D warnings
- name: cargo test (the colour tests print a table of every case)
run: CARGO_HOME="$PWD/.ci/cargo" cargo test --locked
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
/.ci
67 changes: 67 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[package]
name = "alias-playback"
version = "0.1.0"
edition = "2024"
license = "MIT"
description = "libmpv playback for Alias View, Gannet and Nitrate: one wrapper, one set of colour settings, and tests that prove the colours"
publish = false

[dependencies]
# dlopen for libmpv, and the system calls a helper player needs (dup2, close_range, prctl).
libc = "0.2.191"
# memfd, mmap, pipes and poll for a helper player's frames and messages.
rustix = { version = "1.1.6", features = ["fs", "mm", "pipe", "event"] }

# The colour tests and the helper test pick their own main, so they can run a copy of
# themselves as a helper player.
[[test]]
name = "colour"
harness = false

[[test]]
name = "helper"
harness = false

[dev-dependencies]
# The helper test's cancel signal, an eventfd, as an app's would be.
rustix = { version = "1.1.6", features = ["event"] }
# The GPU colour bench opens EGL at run time, as the crate opens libmpv.
libc = "0.2.191"
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.PHONY: check colour-gpu

check:
cargo fmt --check
cargo clippy --all-targets --locked -- -D warnings
cargo test --locked

# The colour tests on a graphics card. CI has none, so this is a bench step on the desktop
# and the M1 before a release.
colour-gpu:
cargo run --release --locked --example colour-gpu
133 changes: 132 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,134 @@
# alias-playback

One libmpv playback crate for Alias View, Gannet and Nitrate (ALI-2420).
One libmpv playback crate for Alias View, Gannet and Nitrate (ALI-2420). It wraps libmpv
once, keeps the colour settings in one place so a fix reaches every app at once, and proves
the colours with tests.

libmpv stays the decoder and player (decided 2026-11-30). This crate doesn't decode anything
itself.

## What's in it

| Module | What it does |
| --- | --- |
| `Player` (`player.rs`) | One libmpv player, set up from `Options`. Commands (load, play, pause, seek, volume, mute) and the events every app needs: position, duration, pause, volume, mute, end, failure. |
| `SwRender`, `GlRender` (`render.rs`) | Drawing frames on the CPU into memory (Wayland's `XRGB8888`, ready for `wl_shm`), or with OpenGL into the app's framebuffer. |
| `Options`, `options::colour` (`options.rs`) | The setup every app shares, the few choices that differ (render path, hardware decoding, sound, cover art), and the colour settings for each render path. |
| `helper` (`helper.rs`) | A player in another process, drawing on the CPU into shared memory, with its state as short lines of text. Both sides: the app's (`start`, `watch`, `Frames`, `Remote`) and the player's (`run`). Moved here from Gannet's preview. |
| `jail` (`jail.rs`) | The bubblewrap jail for that process, as Gannet's preview uses it. The app adds its own seccomp filter through `helper::run`'s `lock_down`. |

### The same in every app

- A video stops on its last frame, paused, instead of closing (`keep-open`). Play after the
end starts again from the beginning.
- Nothing of the user's own mpv setup is read: no `mpv.conf`, scripts, profiles or saved
positions. Nothing beside the file is looked for (subtitles, sound, cover art, the chapters a Matroska
file can borrow from others, the files a playlist names), and nothing
is fetched online.
- No on-screen display or key bindings: the app draws the controls.
- Hardware decoding, when on, tries NVDEC, VA-API, then V4L2, and falls back to the CPU.
VDPAU is left out: on NVIDIA it takes H.264 4:4:4, fails every frame, and never falls back,
so the video never starts. The software renderer gets the copy-back methods only.

libmpv is opened at run time (`libmpv.so.2`, client API 2.x), so an app using this starts
without it and only loses playback.

## Colour

`options::colour` holds the colour settings for each render path:

| Path | Setting | Why |
| --- | --- | --- |
| CPU | `vf=format=gamma=bt.1886` | The software renderer's converter, zimg, lifts the darks of video tagged with the sRGB transfer (ALI-2418). It also lifts full-range video with no transfer tag, which the colour tests found. Labelling every picture BT.1886 first fixes both; the other SDR tags come out the same either way. |
| GPU | none | The OpenGL renderer draws every SDR tag correctly as it is. |

### The colour tests

`tests/colour.rs` (CPU, in CI) and `examples/colour-gpu.rs` (GPU, a bench step) play the
same 51 clips. Each clip is a 4x4 grid of solid patches: black, white, greys, the
primaries and secondaries, three skin tones, and Tarnished's `#121C24`.

- **Cases:** all 27 combinations of range (limited, full, full as `yuvj`), matrix (BT.601,
BT.709, BT.2020) and transfer tag (BT.709, sRGB, none) on H.264; H.264, HEVC, VP9 and AV1
each at 8 and 10 bits, 4:2:0 and 4:4:4; and 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
(ITU-T H.273) and FFmpeg encodes it losslessly, converting nothing. Before a clip is
played, ffprobe must read back the case's tags, and FFmpeg must decode it back to that
Y'CbCr exactly. A failure is then the player's, never the clip's.
- **Pass:** every pixel inside every patch (16 px in from the edges) within 2 steps per
channel of its source colour.
- The CPU run draws each clip at full and half size, and once more without the colour
settings, printing what they fix.
- The GPU run draws each clip with OpenGL (no hardware decoding, then hardware decoding) and
with the software renderer plus hardware decoding. It runs off-screen on EGL's device
platform, with no window or compositor. It reports which decoder libmpv used.

Not covered yet: HDR (PQ, HLG), which tone-maps by design and needs its own reference with a
wider tolerance; real recordings from wf-recorder (M1) and gpu-screen-recorder (desktop)
checked against screenshots taken at the same moment; and primaries other than BT.709.

### Results, 2026-12-02 (libmpv 0.42.0, FFmpeg 9.1)

| Machine | Renderer | Result |
| --- | --- | --- |
| Desktop, x86_64 | CPU, no hardware decoding (as CI runs it) | 51 of 51 |
| Desktop, RTX 4090 | GPU, no hardware decoding | 51 of 51 |
| Desktop, RTX 4090 | GPU, hardware decoding (NVDEC for 44 clips, the CPU for 7 it can't take) | 51 of 51 |
| Desktop, RTX 4090 | CPU, hardware decoding (NVDEC copy-back) | 51 of 51 |
| 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: its libx265 is 8-bit only, so it can't make them.
Without the CPU colour setting, the sRGB-tagged clips and the full-range clips with no
transfer tag draw up to 13 steps too light (`#121C24` as `#1E2831`).

## Using it

```rust
use alias_playback::{Event, Options, Player, SwFormat, SwRender};

let player = Player::new(&Options::cpu())?;
let mut render = SwRender::new(&player)?;
player.on_wakeup(|| { /* wake the thread that reads events */ });
render.on_update(|| { /* wake the thread that draws */ });
player.load(path)?;
player.set_paused(false)?;
// On the drawing thread, when woken:
if render.new_frame() {
render.render(width, height, stride, SwFormat::Bgr0, buffer)?;
}
// On the event thread, when woken:
while let Some(event) = player.next_event() {
match event {
Event::Position(seconds) => { /* … */ }
Event::Ended(true) => { /* stopped on its last frame */ }
_ => {}
}
}
```

A jailed player: the app's `main` calls `helper::run(args, &options, SANDBOX_CODE,
lock_down)` when started with its helper argument, and starts one with
`helper::start(jail::command(HELPER_ARG, sound)?, file, size, sound)`.

## Checks

```sh
make check # fmt, clippy, unit tests, the helper test and the CPU colour tests
make colour-gpu # the GPU bench, on a machine with a graphics card
```

Both need `ffmpeg` and `ffprobe` (with libx264, libx265, libvpx and libaom) and libmpv. The
helper test runs a real player in another process, without the jail: bubblewrap needs user
namespaces, which CI's containers don't give.

## Dependencies

| Crate | Why |
| --- | --- |
| `libc` | `dlopen` for libmpv, and the calls a helper needs: `dup2`, `close_range`, `prctl`. |
| `rustix` | Shared memory (`memfd`, `mmap`), pipes and `poll` for a helper's frames and messages. |

## Licence

MIT. Copyright (c) 2026 Alias Project, Inc.
Loading
Loading