# scorpiox-scorpioplayer-ffmpeg-ios-android-playback-engine-linux-backend-c-egl-pipewire

The **Linux backend** for SCORPIO PLAYER 2027's pure-C playback core, `sppe_`
(`scorpiox-scorpioplayer-ffmpeg-ios-android-playback-engine`). It is the layer
that turns the core into something a Linux app — the Omarchy GTK4 shell today,
a Qt or plain Wayland app tomorrow — can watch and hear:

```
sppe_ core (separate repo)                 THIS repo
  demux, decode (our FFmpeg)                 upload planes to GL textures
  master clock + A/V sync                    PipeWire sink position = the clock
  drop / repeat decisions                    YUV -> RGB on the GPU, once per pixel
  track selection, seek                      RGBA subtitle overlay, alpha-composited
  subtitle decode + render (libass)          (nothing: those are the core's)
```

Nothing here decides *when* a frame is due, *which* stream plays, or *how far* a
seek lands. Those arrive as vtable calls with the arguments already fixed
(ADR-0001 §3/§7/§9).

| | |
|---|---|
| Core | `scorpiox-scorpioplayer-ffmpeg-ios-android-playback-engine` v1.0.0, **ABI 1**, 43 exports |
| Prefix | `splb_` · header `include/splb.h` · map `splb.map` · **27 exports** |
| Backend ABI | **1** — the 15 vtable entries `sppe.h` declares, nothing added |
| Link deps | **none** — EGL, GLESv2 and libpipewire are `dlopen()`ed at first use |
| Targets | `linux-x86_64` · `linux-aarch64` |

## Why dlopen and no link dependencies

* A shell can hand in the `EGLDisplay`/`EGLContext` it already owns (GTK4
  `GtkGLArea`, Qt, a raw `wl_egl_window`); the sink renders into it.
* With none handed in it makes a **surfaceless** display and an FBO, so the whole
  GPU path is testable with no window, no compositor and no GPU.
* The same binary starts on a box with no mesa, no PipeWire daemon, or a
  different soname — and says so in `splb_gl_info_json()` /
  `splb_audio_info_json()` instead of failing to load.
* `linux-aarch64` cross-builds on `.6` without an aarch64 mesa/pipewire sysroot.

## The two sinks

**Video — GPU, planes, never `sws_scale`.** The Pi5 (V3D, 3.9 GiB, 0 swap) cannot
afford a CPU YUV→RGB pass: the measured mpv GL baseline is 4.2 ms/frame at
1080p and software 4K 10-bit HEVC already costs ~286 % CPU. So every accepted
format goes up as *planes* (1.5 bytes/px for 8-bit 4:2:0 instead of 4) and the
conversion happens once, per pixel, in the fragment shader.

Accepted: `yuv420p yuv422p yuv444p yuvj420p/422p/444p nv12 nv21 p010le p016le
yuv420p10le/422p10le/444p10le gray gray10le gray16le rgba bgra argb abgr rgb0
bgr0 rgb24 bgr24`. **NV12 is the tuned path** — it is what FFmpeg's stateful V4L2
M2M decoders hand over on the Pi5, two textures and no de-interleave.

Two shader generations are compiled and both are gated: ES3
(`GL_R8`/`GL_RG8`/`GL_R16` + `GL_UNPACK_ROW_LENGTH`, so a source stride is free)
and ES2 (`GL_LUMINANCE*`, padded textures, scaled UVs) for a driver that only
offers ES2.

**Audio — PipeWire, and the clock.** On both targets the graph is owned by
PipeWire; the Pi5's `vc4-hdmi-1` sink (Sony TV on HDMI-A-2, HT-A7000 on eARC)
only exists as a PipeWire node, so opening ALSA directly would fight WirePlumber
and lose the TV. `audio_clock_now` returns the position the **device** is
reading (`pw_time.ticks` at `pw_time.now`) — not bytes written — and `-1` before
the first graph cycle, which is the vtable's documented "not started".

Three modes: `sink` (default; degrades to `virtual` when no daemon answers),
`virtual` (no PipeWire call at all: the count *is* the clock, so a headless run
is deterministic and runs at decode speed) and `off` (the core falls back to its
own monotonic clock).

## The hardware story (measured, not assumed)

Our shipped FFmpeg has **zero generic hwaccels** — `av_hwdevice_iterate_types()`
returns **0** on the dist tree (ADR-0001 §9). So:

* `hw_probe` returns **0 for every codec**, and that is not a stub: claiming
  otherwise would route every packet into a `hw_submit` with no decoder behind it.
* No dmabuf export, no zero-copy claim. ABI 1 has no import path anyway.
* **Hardware decode still happens on the Pi5 today** through FFmpeg's stateful
  V4L2 M2M decoders (`hevc_v4l2m2m`, `h264_v4l2m2m`, `vp9_v4l2m2m`,
  `mpeg2_v4l2m2m` are all in the build), which decode on `rpi-hevc-dec` and hand
  this sink **NV12 software frames**. `splb --file x.mkv --decoder hevc_v4l2m2m`
  measures exactly that path.

## Build

```bash
make                     # lib…linux-backend-c-egl-pipewire.so
make cli                 # the `splb` measurement CLI (needs FFmpeg headers)
make test                # every gate below, headless
make asan                # the same gates under ASAN/UBSAN
make exports             # regenerate splb.map AND exports.json from the header
make dist-check          # nm -D vs splb.map — the third opinion
./release.sh all         # cross-build + verify + publish (on .6)
```

## Gates (`make test`)

| gate | what it proves |
|---|---|
| lifecycle | create / malformed config is a typed error / destroy / destroy(NULL) |
| vtable | `abi == 1`, all 15 entries populated, `hw_probe == 0` for every codec, the hw path refuses cleanly |
| gl-es3 | real EGL + GLES program; **GPU picture vs a CPU reference written from FFmpeg's formulas**, not transcribed from the shader |
| gl-es2 | the same through the ES2 shader text (forced with `"shader_es":2`) |
| nv12 | the format hardware decode actually delivers |
| overlay | a synthetic RGBA overlay lands where asked; the pixel outside is untouched |
| audio-virtual | `-1` before the first write, advances, resets on flush |
| audio-pipewire | the sink position is the clock and it advances (SKIP without a daemon) |
| core | the **shipped `sppe_` core** `dlopen()`ed, this backend installed, a real corpus file played to EOF — frame count, pts, A/V drift |

The pixel gate compares **bar means** and the **share of samples within 3 LSB**,
not every sample: chroma is sampled through `GL_LINEAR`, so a colour-bar edge
interpolates and the rasteriser's varying precision moves one column by up to
40 LSB while the picture is perfect. A wrong matrix, a wrong range or a swapped
plane moves every bar's mean by 20–60 and drops agreement to ~60 %.

## `.console`

`scorpiox-scorpioplayer-ffmpeg-ios-android-playback-engine-linux-backend-c-egl-pipewire.console`
is the C# P/Invoke harness (one row per export). Vtable-driving tests stay in the
C suite, because a backend is only meaningful with the core attached.

## Related

* `working-on-scorpioplayer-ffmpeg-playback-engine-linux-backend-dll` — the skill
* `working-on-scorpioplayer-ffmpeg-ios-android-playback-engine-dll` — the core
* `scorpiox-scorpioplayer-linux-omarchy-gtk4-player` — the first consumer
* `working-on-scorpiox-ffmpeg-libav-windows-uwp-xbox-linux-shared-build` — the FFmpeg

SPDX-License-Identifier: MIT

## v1.1.0 — HDMI bitstream passthrough (ABI 2, 2026-11-21, UNVERIFIED until the Pi5 run)

Written hands-on without a compile pass; the build/test/measure pass is the factory task
`scorpioplayer-linux-backend-hdmi-audio-passthrough-pipewire-feature` (picobot `tasks/factory/`).

- vtable `abi` = **2**; of the v2 tail only `audio_configure_ex` + `audio_discontinuity` are filled,
  every other v2 entry stays NULL. Config `"abi":1` nulls the tail so the .so still installs on a
  v1.0.0 core.
- `audio_configure_ex(..., spdif_format, ...)`: refused unless `sample_fmt == S16` **and** the
  format is in the sink's capability list — config `"spdif"`: `""` (default, PCM only),
  `"auto"` (parse the HDMI ELD in `/proc/asound/card*/eld#*`), or `"ac3,eac3,dts,truehd"`.
  Then the PipeWire stream is rebuilt with an **iec958** EnumFormat
  (`spa_format_audio_iec958_build`) and we wait ≤ `iec_wait_ms` (1500) for the graph: a negotiated
  iec958 format = accepted, error / NULL format / timeout = refused → the sink is put back on
  F32 and the core decodes to PCM.
- `"iec_rate"`: `"transport"` (default, 192000 for E-AC-3 — mpv's convention) or `"codec"`
  (48000). Which one the Pi5's `vc4-hdmi` ALSA node wants is a measurement, not an assumption.
- The ring is byte-generic (`frame_bytes` = ch×4 F32 / ch×2 S16). Every bitstream write is checked
  for the IEC 61937 Pa/Pb preamble (`0xF872 0x4E1F`) → `audio_info_json` `iec_bursts` / `iec_bad`.
- New export `splb_audio_hdmi_codecs()` — the ELD's bitstream codecs.
- `tools/demo-gtk4.c`: `--spdif auto --passthrough ac3,eac3 --node <name> --iec-rate transport|codec`.
- `tests/splb_test.c` gate 10 `iec958`: default refuses, S16-only, list filter, eac3 @ 192 kHz,
  10 bursts counted, clock = first pts + 0.32 s at the transport rate, bad burst counted,
  discontinuity counted, PCM restore, ELD read; end to end through an ABI-2 core on
  `ac3.mkv` / `eac3.mkv` when `SPPE_SO` points at sppe_ ≥ 1.1.0.

**On the Pi5 the sink must allow it:** WirePlumber only links an iec958 stream to an ALSA node
whose `iec958.codecs` lists the codec (`wpctl settings` / a `wireplumber.conf.d` rule on the
`alsa_output.platform-…hdmi…` node). Record exactly what was set.

### v1.1.0 — `"present":"deferred"` (for the GTK4 shell)

A shell whose playback thread owns the core cannot draw from `video_present` — a GtkGLArea's
context is only current inside its `render` signal on the main thread. With
`splb_create({"present":"deferred"})`, `video_present` copies the planes into one pending slot
(newest wins) and the shell's render handler calls `splb_gl_present_pending()` (1 = new frame,
2 = redraw, 0 = nothing yet). `splb_pending_serial()` tells a timer whether to queue a render;
`splb_pending_info_json()` counts `replaced` = frames the core presented that the screen never
showed. Test gate 11 `deferred`: identical crc to the immediate path with the source scribbled
after the queue. Exports 31.
