aboutsummaryrefslogtreecommitdiffstats
path: root/packages/meshbay-node/src/meshbay_node/hwaccel.py
blob: 051f64c4adea44b5b676a52ccf8dd3b533500806 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
"""
Hardware H.264 encoding for the streaming re-encode path.

Streaming copies the video whenever a browser can decode it (`-c:v copy`), and
that path costs nothing. The re-encode path is the expensive one — HEVC, and
the codecs with no MSE string at all (Xvid, MPEG-2, VC-1) — and it is
`libx264 -preset veryfast`, one process per viewer, for the length of a film.
On a laptop that is fine. On an Atom or Celeron mini-PC it does not reach real
time at 1080p, which is why `transcode_incompatible_video` exists as an
operator opt-out: a node that cannot transcode says so instead of serving a
stream that stutters.

**This module is the other answer to the same problem.** The GPU in every
machine of the last decade encodes H.264 in fixed-function silicon and does not
care how weak the CPU beside it is. Where that hardware is there and works, the
node re-encodes on it and the opt-out is not needed.

Nothing here is configured by hand, and nothing is inferred from a CPU model or
from a driver's name. **The capability is established by encoding**: a listed
`h264_qsv` proves only that ffmpeg was built with it, and a render node proves
only that a GPU exists — neither says the driver on this machine can do the
work. Measured on the development VM, where `ffmpeg -encoders` lists every
VAAPI encoder and the virtio-gpu driver then fails to initialise:
`libva: virtio_gpu_drv_video.so init failed`.

**The probe encodes 1080p and then reads the result back with ffprobe**, and
accepts nothing that is not High profile at level 4.1. That is not
belt-and-braces: `stream_init` announces `avc1.640029` and the client checks it
before it trusts a byte, so an encoder that quietly wrote a different level
would make the node a liar. It also means an argument spelled the way one
encoder wants and not another — `-level 4.1` against `-level 41` — is caught
here rather than by a viewer, which is why each candidate below may offer
several variants and the machine picks.

Candidates, in the order they are tried:

  vaapi   Linux, any GPU with a VA-API driver — Intel iGPU, AMD through mesa.
  qsv     Intel Quick Sync, which is how the same iGPU is reached on Windows.
  nvenc   NVIDIA, either platform.

Two gaps, named rather than left to be discovered: **AMD on Windows** (AMF) and
**macOS** (VideoToolbox). Neither set of arguments could be tried anywhere in
this project, and MeshBay ships no macOS package at all; a node on either
re-encodes in software, exactly as every node did before this module existed.
Adding one is a `Candidate` and nothing else — the probe is what decides
whether it works, so a wrong guess costs a rejected candidate, not a broken
stream.

Then three modes per stream, remembered per source codec:

  hw     Hardware decode and encode. The whole pipeline on the GPU, which is
         what makes 1080p HEVC playable on a machine that cannot decode it at
         all in software.
  hwenc  Software decode, hardware encode. The fallback for a source the GPU
         has no decoder for — iHD has none for MPEG-4 Part 2, so an Xvid .avi
         lands here, and an SD Xvid decodes in software for nearly nothing.
  sw     libx264, the path that was always here.
"""

from __future__ import annotations

import asyncio
import logging
import os
import shutil
import sys
import tempfile
from dataclasses import dataclass

from meshbay_node import platform

log = logging.getLogger(__name__)

HW = "hw"
HWENC = "hwenc"
SW = "sw"

# How long one test encode may take before it is considered broken. A driver
# that deadlocks must not hold the first viewer's stream, and 20 s is far
# beyond the fraction of a second the probe needs when it works.
PROBE_TIMEOUT_SECS = 20

# What the probe encodes. 1080p because the level a film gets is the level that
# has to be checked: an encoder asked for 4.1 on a 320x240 clip may well write
# a lower one, and rejecting a good candidate for that would be the probe
# failing rather than the hardware.
PROBE_SIZE = "1920x1080"

# The announced codec string is `avc1.640029` — High (profile_idc 100, no
# constraint flags) at level 4.1 (0x29) — and every encoder here is held to it.
_PROFILE_ARGS = ["-profile:v", "high", "-level", "4.1"]
_WANT_PROFILE = "High"
_WANT_LEVEL = 41

# -pix_fmt yuv420p: a 10-bit or 4:4:4 HEVC source (common for HDR WEB-DLs)
# fails "-profile:v high" outright otherwise — libx264's High profile is 8-bit
# 4:2:0 only. Downsampling loses nothing a browser could show anyway (MSE has
# no HDR path), which is also what each hardware filter below does on the GPU.
_SOFTWARE_ARGS = ["-c:v", "libx264", "-pix_fmt", "yuv420p", *_PROFILE_ARGS,
                  "-preset", "veryfast", "-crf", "21"]


@dataclass(frozen=True)
class Candidate:
    """One way a machine might encode H.264 without its CPU."""

    name: str
    encoder: str
    platforms: tuple[str, ...]
    # `-hwaccel <this>`, and the pixel format decoded frames stay in.
    hwaccel: str
    # Filters for frames already on the GPU, and for frames in system memory.
    hw_filter: str
    sw_filter: str
    # Rate control and anything else, tried in order until one produces High
    # at level 4.1. A later `-level` overrides the shared one, which is how a
    # spelling one encoder rejects is offered without being imposed.
    variants: tuple[tuple[str, tuple[str, ...]], ...]
    # VA-API alone names a device; QSV and NVENC find their own.
    needs_render_node: bool = False


CANDIDATES: tuple[Candidate, ...] = (
    Candidate(
        name="vaapi", encoder="h264_vaapi", platforms=("linux",),
        hwaccel="vaapi",
        hw_filter="scale_vaapi=format=nv12", sw_filter="format=nv12,hwupload",
        needs_render_node=True,
        # CQP is the constant-quality mode every VA-API driver implements (iHD
        # and i965 alike); ICQ and QVBR are not always there. Some Intel
        # generations implement H.264 encoding only on the low-power (VDEnc)
        # path and others only on the full one, which is a property of the chip
        # and the driver — so it is measured rather than looked up.
        variants=(
            ("standard", ("-rc_mode", "CQP", "-qp", "23")),
            ("low-power", ("-rc_mode", "CQP", "-qp", "23", "-low_power", "1")),
        ),
    ),
    Candidate(
        name="qsv", encoder="h264_qsv", platforms=("win32", "linux"),
        hwaccel="qsv",
        hw_filter="vpp_qsv=format=nv12", sw_filter="format=nv12",
        # `-global_quality` is QSV's constant-quality knob. The second variant
        # exists because h264_qsv has no `level` option of its own and takes
        # the generic integer one, which may or may not read "4.1" as 41 —
        # ffmpeg accepts both spellings without complaint, and only the encoded
        # file says which one was understood.
        variants=(
            ("icq", ("-global_quality", "23")),
            ("icq, integer level", ("-global_quality", "23", "-level", "41")),
        ),
    ),
    Candidate(
        name="nvenc", encoder="h264_nvenc", platforms=("win32", "linux"),
        hwaccel="cuda",
        hw_filter="scale_cuda=format=nv12", sw_filter="format=nv12",
        variants=(
            ("vbr", ("-rc", "vbr", "-cq", "23", "-b:v", "0")),
        ),
    ),
)


@dataclass(frozen=True)
class Encoder:
    """A hardware encoder that has been seen to produce what the node claims."""

    candidate: Candidate
    variant: str
    extra: tuple[str, ...]
    device: str | None = None


_enabled = os.environ.get("MESHBAY_HW_VIDEO_ENCODE", "1") not in ("0", "false", "no")
_encoder: Encoder | None = None
_probed = False
_probe_lock: asyncio.Lock | None = None
# (source codec, mode) pairs that have failed once and are not tried again.
_demoted: set[tuple[str, str]] = set()


def set_enabled(enabled: bool) -> None:
    """Operator switch (`node.toml`, `hardware_video_encode`)."""
    global _enabled
    _enabled = enabled


def render_node() -> str | None:
    """The first DRM render node this process can actually open.

    Readable *and* writable, because VA-API maps buffers on it. On a desktop
    session logind grants that through an ACL rather than through membership of
    the `render` group, so asking the kernel answers "can this process use it"
    — which is the question — rather than "is this user in a group", which is
    not.
    """
    try:
        names = sorted(os.listdir("/dev/dri"))
    except OSError:
        return None
    for name in names:
        if not name.startswith("renderD"):
            continue
        dev = f"/dev/dri/{name}"
        if os.access(dev, os.R_OK | os.W_OK):
            return dev
    return None


async def _run(args: list[str], timeout: int) -> tuple[int | None, str]:
    proc = await asyncio.create_subprocess_exec(
        *args, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE)
    try:
        out, err = await asyncio.wait_for(proc.communicate(), timeout)
    except TimeoutError:
        proc.kill()
        await proc.wait()
        return None, "timed out"
    text = (out or b"").decode("utf-8", "replace") or \
        (err or b"").decode("utf-8", "replace")
    return proc.returncode, text.strip()


async def _test_encode(cand: Candidate, device: str | None,
                       extra: tuple[str, ...]) -> str | None:
    """Encode 1080p and read the result back. Returns why it was rejected.

    `None` means it worked — the encoder exists, the driver initialises, and
    the file it wrote really is High at level 4.1.
    """
    ffmpeg = platform.ffmpeg_cmd()
    if not (os.path.isabs(ffmpeg) or shutil.which(ffmpeg)):
        return "ffmpeg not found"
    fd, out = tempfile.mkstemp(suffix=".mp4", prefix="meshbay-hwprobe-")
    os.close(fd)
    try:
        rc, err = await _run([
            ffmpeg, "-hide_banner", "-loglevel", "error", "-nostdin", "-y",
            *device_args(cand, device),
            "-f", "lavfi", "-i", f"testsrc=size={PROBE_SIZE}:rate=25:duration=0.2",
            "-vf", cand.sw_filter,
            "-c:v", cand.encoder, *_PROFILE_ARGS, *extra,
            out,
        ], PROBE_TIMEOUT_SECS)
        if rc != 0:
            # The *first* line: ffmpeg's last word is usually "nothing was
            # written into the output file", which is the consequence. The
            # cause is at the top — "Cannot load libcuda.so.1", "Error
            # creating a MFX session", "libva ... init failed".
            return (err.splitlines() or ["no output"])[0]
        rc, info = await _run([
            platform.ffprobe_cmd(), "-v", "error", "-select_streams", "v:0",
            "-show_entries", "stream=profile,level", "-of", "csv=p=0", out,
        ], PROBE_TIMEOUT_SECS)
        profile, _, level = info.partition(",")
        if rc != 0 or profile.strip() != _WANT_PROFILE or level.strip() != str(_WANT_LEVEL):
            return (f"produced {info or 'nothing'}, and the node announces "
                    f"{_WANT_PROFILE} at level {_WANT_LEVEL}")
        return None
    finally:
        try:
            os.unlink(out)
        except OSError:
            pass


async def encoder() -> Encoder | None:
    """The working hardware encoder, established once per process."""
    global _encoder, _probed, _probe_lock
    if not _enabled:
        return None
    if _probed:
        return _encoder
    if _probe_lock is None:
        _probe_lock = asyncio.Lock()
    async with _probe_lock:
        if _probed:
            return _encoder
        _encoder = await _find_encoder()
        _probed = True
    return _encoder


async def _find_encoder() -> Encoder | None:
    device = render_node()
    for cand in CANDIDATES:
        if sys.platform not in cand.platforms:
            continue
        if cand.needs_render_node and device is None:
            continue
        for variant, extra in cand.variants:
            why = await _test_encode(cand, device, extra)
            if why is None:
                log.info("hwaccel: %s (%s) — video re-encoding runs on the GPU",
                         cand.encoder, variant)
                return Encoder(candidate=cand, variant=variant, extra=extra,
                               device=device if cand.needs_render_node else None)
            log.debug("hwaccel: %s (%s) rejected: %s", cand.encoder, variant, why)
    log.info("hwaccel: no hardware H264 encoder works here — encoding in software")
    return None


async def modes_for(source_codec: str | None) -> list[str]:
    """Which re-encode modes to try for this source, best first.

    Always ends in `SW`: libx264 is the one that needs no hardware, so a plan
    that ran out of hardware modes is still a plan that plays the film.
    """
    enc = await encoder()
    if enc is None:
        return [SW]
    key = source_codec or "?"
    plan = [m for m in (HW, HWENC) if (key, m) not in _demoted]
    plan.append(SW)
    return plan


def demote(source_codec: str | None, mode: str, detail: str) -> None:
    """This mode does not work for this source codec; stop trying it."""
    if mode == SW:
        return
    key = source_codec or "?"
    if (key, mode) not in _demoted:
        _demoted.add((key, mode))
        log.info("hwaccel: %s re-encoding does not work for %s on this machine "
                 "(%s) — not trying it again", mode, key, detail)


def device_args(cand: Candidate, device: str | None) -> list[str]:
    """How this candidate is told which GPU to use, where it needs telling."""
    if cand.needs_render_node and device:
        return ["-vaapi_device", device]
    return []


def input_args(mode: str, enc: Encoder | None) -> list[str]:
    """ffmpeg options that must precede `-i`."""
    if enc is None or mode == SW:
        return []
    cand = enc.candidate
    if mode == HWENC:
        return device_args(cand, enc.device)
    # `-hwaccel_output_format` keeps decoded frames on the GPU rather than
    # reading them back to system memory, which is the whole saving: a readback
    # of every 1080p frame costs more on a weak machine than the encode it
    # feeds.
    args = ["-hwaccel", cand.hwaccel]
    if enc.device:
        args += ["-hwaccel_device", enc.device]
    return args + ["-hwaccel_output_format", cand.hwaccel]


def codec_args(mode: str, enc: Encoder | None) -> list[str]:
    """The `-vf`/`-c:v` half, where libx264's arguments used to be written."""
    if enc is None or mode == SW:
        return list(_SOFTWARE_ARGS)
    cand = enc.candidate
    # The hardware filter converts a 10-bit HDR source to the 8-bit NV12 the
    # encoder takes, on the GPU — the same downsampling the software path does
    # with `-pix_fmt yuv420p`, and for the same reason.
    filters = cand.hw_filter if mode == HW else cand.sw_filter
    return ["-vf", filters, "-c:v", cand.encoder, *_PROFILE_ARGS, *enc.extra]


def _reset_for_tests() -> None:
    global _encoder, _probed, _probe_lock
    _encoder, _probed, _probe_lock = None, False, None
    _demoted.clear()