fix(plex): a codec the GPU cannot decode is decoded on the CPU and still encoded on the GPU

The prod iGPU has no VC-1 Advanced decode ("No support for codec vc1 profile 3"), so every VC-1
session died on the GPU and fell back to libx264, which needs ~17 s for the three warm-up segments:
a start or a seek took ~19.5 s. VC-1, XviD and DivX now take CPU decode -> hwupload -> h264_vaapi
(three segments in 5.5 s for a 1080p VC-1 remux, measured on prod hardware with this argv).

The transcode_hw facet now says whether the GPU encodes: VC-1/XviD/DivX count, and so does an HDR
film (the OpenCL tonemap runs on the GPU; the facet said software). Migration 0119 rewrites the
stored facet and re-queues 10-bit items for the HDR probe.
This commit is contained in:
2026-11-07 18:39:15 +01:00
parent 2b7620ded9
commit 9778bdf351
6 changed files with 385 additions and 87 deletions
+56 -20
View File
@@ -32,15 +32,25 @@ class TestTranscodeHwCapable:
assert sync.transcode_hw_capable("transcode", "hevc") is True
assert sync.transcode_hw_capable("transcode", "vp9", bit_depth=8) is True
def test_non_decodable_codec_is_sw(self):
# AV1 / MPEG-4 ASP (XviD) can't be HW-decoded on Gen 9.5 → software regardless of bit depth.
def test_a_codec_neither_path_knows_is_sw(self):
assert sync.transcode_hw_capable("transcode", "av1") is False
assert sync.transcode_hw_capable("transcode", "mpeg4", bit_depth=8) is False
assert sync.transcode_hw_capable("transcode", "av1", bit_depth=8) is False
def test_10bit_or_hdr_hevc_is_sw(self):
# A HW-decodable codec that is 10-bit or HDR still breaks the VAAPI nv12 pipeline → software.
def test_a_cpu_decoded_8bit_codec_is_encoded_on_the_gpu(self):
# VC-1 (the iGPU has no VC-1 Advanced decode), XviD, DivX: the CPU decodes, the GPU encodes.
for codec in ("vc1", "mpeg4", "msmpeg4v3"):
assert sync.transcode_hw_capable("transcode", codec) is True
assert sync.transcode_hw_capable("transcode", "mpeg4", bit_depth=10) is False
def test_10bit_sdr_hevc_is_sw(self):
# 10-bit SDR: an exact downconvert on the CPU (libx264), never the HDR tone curve.
assert sync.transcode_hw_capable("transcode", "hevc", bit_depth=10) is False
assert sync.transcode_hw_capable("transcode", "hevc", bit_depth=8, hdr=True) is False
def test_hdr_hevc_is_encoded_on_the_gpu(self):
# HDR goes through the OpenCL tonemap between VAAPI decode and encode (the prod image ships the
# runtime); the facet said software for it while every such session ran on the GPU.
assert sync.transcode_hw_capable("transcode", "hevc", bit_depth=10, hdr=True) is True
assert sync.transcode_hw_capable("transcode", "hevc", bit_depth=8, hdr=True) is True
class TestVideoStreamProps:
@@ -87,6 +97,9 @@ class TestSyncDerivation:
row2 = PlexItem(rating_key="y", kind="movie")
sync._apply_item(row2, lib_id=1, kind="movie", meta=self._meta("av1"))
assert row2.transcode_hw is False # av1 not HW-decodable
row3 = PlexItem(rating_key="z", kind="movie")
sync._apply_item(row3, lib_id=1, kind="movie", meta=self._meta("vc1"))
assert row3.transcode_hw is True # CPU decode, GPU encode
def test_probed_value_survives_resync_when_codec_unchanged(self):
row = PlexItem(rating_key="x", kind="movie")
@@ -124,21 +137,24 @@ class TestSyncDerivation:
assert row.video_bit_depth is None and row.media_probed_at is None # re-probe scheduled
def test_migration_codec_set_matches_hw_decodable_codecs():
# Migration 0077 hardcodes the HW-decodable codec set in raw SQL and its comment mandates it stay
# identical to plex.sync.HW_DECODABLE_CODECS. A comment isn't enforcement (house rule) — pin the
# equality so adding/removing a codec from the frozenset without editing the migration fails here
# (else already-migrated prod rows keep a wrong transcode_hw the runtime derivation wouldn't catch).
def test_migration_codec_sets_match_the_sync_sets():
# The newest migration that rewrites `transcode_hw` hardcodes both codec sets in raw SQL. Pin them
# to plex.sync, so a codec added to or removed from a frozenset without a migration that rewrites
# the stored rows fails here (else prod rows keep a facet the runtime choice disagrees with).
mig = (
Path(__file__).resolve().parent.parent
/ "alembic"
/ "versions"
/ "0077_plex_playback_class.py"
/ "0119_plex_transcode_gpu.py"
)
m = re.search(r"lower\(codec_video\)\s+IN\s+\(([^)]*)\)", mig.read_text())
assert m, "could not find the codec IN(...) list in migration 0077"
codecs = {c.strip().strip("'\"").lower() for c in m.group(1).split(",")}
assert codecs == {c.lower() for c in sync.HW_DECODABLE_CODECS}
src = mig.read_text()
sets = {}
for name in ("HW_DECODABLE_CODECS", "CPU_DECODE_CODECS"):
m = re.search(name + r"\s*=\s*\(([^)]*)\)", src)
assert m, f"could not find {name} in migration 0119"
sets[name] = {c.strip().strip("'\"").lower() for c in m.group(1).split(",") if c.strip()}
assert sets["HW_DECODABLE_CODECS"] == {c.lower() for c in sync.HW_DECODABLE_CODECS}
assert sets["CPU_DECODE_CODECS"] == {c.lower() for c in sync.CPU_DECODE_CODECS}
class _FakePlex:
@@ -182,22 +198,42 @@ def test_probe_media_batch_refines_hw_to_sw_for_10bit(db):
codec_video="mpeg4",
transcode_hw=False,
)
db.add_all([hevc8, hevc10, xvid])
hdr10 = PlexItem(
rating_key="hdr",
library_id=lib.id,
kind="movie",
title="hdr",
playable="transcode",
codec_video="hevc",
transcode_hw=True,
)
db.add_all([hevc8, hevc10, xvid, hdr10])
db.commit()
plex = _FakePlex(
{
"h8": {"Media": [{"Part": [{"Stream": [{"streamType": 1, "bitDepth": 8}]}]}]},
"h10": {"Media": [{"Part": [{"Stream": [{"streamType": 1, "bitDepth": 10}]}]}]},
"hdr": {
"Media": [
{
"Part": [
{"Stream": [{"streamType": 1, "bitDepth": 10, "colorTrc": "smpte2084"}]}
]
}
]
},
}
)
probed = sync._probe_media_batch(db, plex)
db.refresh(hevc8)
db.refresh(hevc10)
db.refresh(xvid)
# Only the two HW-decodable HEVC items are probed; the XviD is SW-by-codec and never fetched.
assert probed == 2
db.refresh(hdr10)
# Only the HW-decodable HEVC items are probed; the XviD's class follows from its codec alone.
assert probed == 3
assert hevc8.video_bit_depth == 8 and hevc8.transcode_hw is True and hevc8.media_probed_at
assert hevc10.video_bit_depth == 10 and hevc10.transcode_hw is False # downgraded to software
assert hevc10.video_bit_depth == 10 and hevc10.transcode_hw is False # 10-bit SDR: software
assert hdr10.video_bit_depth == 10 and hdr10.transcode_hw is True # HDR: the GPU tonemap
assert xvid.media_probed_at is None # not in the work-list
# A second run has nothing left to do (watermarks set).
assert sync._probe_media_batch(db, plex) == 0