- 新 tier `qsv`:--tier/--force-tier 的 choices 加 qsv,锁是 run-qsv.lock(两个不同 ASIC 不共用一把 硬件规则锁),命令 -c:v hevc_qsv -preset <本 build 整数 7..1> -global_quality <q> -pix_fmt nv12; 不加 -maxrate(ICQ 模式下 MaxKbps 是否生效取决于 MFX 版本,本机无法验证;编码后的「不得超源码率」 断言仍在)。 - QSV_PRESET_ENUM + qsv_preset_value():三个 tier 收同一批 preset 词,引擎翻译成本 build 的整数。 - BACKEND_INIT_PATTERNS + backend_failure():ffmpeg 非 0 退出时把「后端没起来」认出来,状态写成 FAIL backend qsv unavailable: …(不是 FAIL ffmpeg rc=、不是 FAIL quality)。 - main() 守卫:--tier qsv 缺 --qsv-quality/--qsv-preset 就在任何运行准备之前退出非 0(两个旋钮故意 无默认值:本机没有 Intel 硬件,写死的数字就是编的)。 - capabilities:CAPABILITIES_VERSION 1→2、__version__ 1.1.0;quality_knobs 取值改从 QUALITY_KNOB_OPTIONS 派生(5a 手写的键名正好漏了 qsv_preset)。 - scripts/test_qsv_tier.py 25 例(含 x265/nvenc 整条 argv 的逐字回归);test_capabilities.py 42/42。
164 lines
8.2 KiB
Python
164 lines
8.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Batch 4b-1: the `qsv` TIER - command construction, preset translation, and the honest refusal.
|
|
|
|
The Intel QSV ENCODE PATH exists as of this batch, but its quality has not been measured (there is no
|
|
Intel iGPU on this machine - the self-test reports `no-device`), and this repo does not encode at an
|
|
invented number. So the tier is written to be:
|
|
* selectable - `--tier qsv` is a real tier with its own lock (`run-qsv.lock`), so the control
|
|
plane can point a lane at an Intel box without touching the other tiers;
|
|
* buildable - the command takes QSV's own arguments, not x265's (this is where the pixel-format
|
|
and quality-scale traps live): `-global_quality`, `-pix_fmt nv12`, an INTEGER `-preset`;
|
|
* REFUSABLE - with no measured `--qsv-quality/--qsv-preset` it exits non-zero, says why, and has
|
|
touched nothing (no mkdir, no stage, no ledger, no byte of the source).
|
|
|
|
And - the regression half that matters more than the new code - the x265 and NVENC command lines are
|
|
UNCHANGED, byte for byte, from before this batch.
|
|
|
|
Usage: python scripts/test_qsv_tier.py exit 0 = every assertion held
|
|
"""
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
|
|
HERE = os.path.dirname(os.path.abspath(__file__))
|
|
sys.path.insert(0, HERE)
|
|
import library_run as LR # noqa: E402
|
|
|
|
ENTRY = os.path.join(HERE, "library_run.py")
|
|
PY = sys.executable
|
|
|
|
checks = {}
|
|
|
|
|
|
def check(name, ok, detail=""):
|
|
checks[name] = bool(ok)
|
|
print("%-52s %s%s" % (name, "PASS" if ok else "FAIL", (" " + str(detail)) if detail else ""))
|
|
|
|
|
|
# ---------------------------------------------------------------- preset translation (pure)
|
|
def preset_cases():
|
|
got = {k: LR.qsv_preset_value(k) for k in ("veryfast", "faster", "fast", "medium", "slow",
|
|
"slower", "veryslow")}
|
|
return got
|
|
|
|
|
|
got = preset_cases()
|
|
check("preset_names_map_to_this_builds_ints",
|
|
got == {"veryfast": 7, "faster": 6, "fast": 5, "medium": 4, "slow": 3, "slower": 2,
|
|
"veryslow": 1}, got)
|
|
check("preset_accepts_a_digit", LR.qsv_preset_value("4") == 4 and LR.qsv_preset_value(3) == 3)
|
|
check("preset_normalises_case_and_space", LR.qsv_preset_value(" Medium ") == 4)
|
|
for bad in ("placebo", "", None, "9"):
|
|
try:
|
|
LR.qsv_preset_value(bad)
|
|
except ValueError as e:
|
|
check("preset_rejects_%s" % (bad or "empty"), "unknown qsv preset" in str(e) or "0-7" in str(e),
|
|
str(e)[:60])
|
|
else:
|
|
check("preset_rejects_%s" % (bad or "empty"), False, "no ValueError raised")
|
|
|
|
|
|
# ---------------------------------------------------------------- command construction
|
|
CAPTURED = {}
|
|
|
|
|
|
def fake_run(cmd, watch, stall_s=None, timeout=None):
|
|
CAPTURED["cmd"] = list(cmd)
|
|
return subprocess.CompletedProcess(cmd, 1, "", "boom: fake ffmpeg")
|
|
|
|
|
|
LR.run_watched = fake_run
|
|
parser = LR.build_parser()
|
|
INFO = {"acodec": "aac"}
|
|
|
|
|
|
def cmd_for(argv, vbps=1_500_000):
|
|
a = parser.parse_args(argv)
|
|
LR.encode("C:/tmp/in.mp4", "C:/tmp/out.mp4", INFO, a, a.force_tier or a.tier or "x265", vbps)
|
|
return CAPTURED["cmd"]
|
|
|
|
|
|
qsv = cmd_for(["--tier", "qsv", "--force-tier", "qsv", "--qsv-quality", "30", "--qsv-preset",
|
|
"medium"])
|
|
|
|
|
|
def argval(cmd, flag):
|
|
return cmd[cmd.index(flag) + 1] if flag in cmd else None
|
|
|
|
|
|
check("qsv_uses_hevc_qsv", argval(qsv, "-c:v") == "hevc_qsv", argval(qsv, "-c:v"))
|
|
check("qsv_preset_is_the_translated_int", argval(qsv, "-preset") == "4", argval(qsv, "-preset"))
|
|
check("qsv_quality_is_global_quality", argval(qsv, "-global_quality") == "30")
|
|
check("qsv_has_no_crf", "-crf" not in qsv, "QSV has no -crf at all; the scales are different")
|
|
check("qsv_pix_fmt_is_nv12", argval(qsv, "-pix_fmt") == "nv12", argval(qsv, "-pix_fmt"))
|
|
check("qsv_keeps_the_playability_flags",
|
|
argval(qsv, "-tag:v") == "hvc1" and argval(qsv, "-fps_mode") == "cfr"
|
|
and argval(qsv, "-profile:v") == "main")
|
|
check("qsv_has_no_maxrate", "-maxrate" not in qsv,
|
|
"whether MaxKbps binds in ICQ mode is a hardware question - the post-encode size gate still runs")
|
|
check("qsv_audio_is_copied_when_it_is_aac", argval(qsv, "-c:a") == "copy")
|
|
|
|
# the quality ladder must still work on this tier: one step better = one lower global_quality
|
|
a = parser.parse_args(["--tier", "qsv", "--qsv-quality", "30", "--qsv-preset", "medium"])
|
|
LR.encode("C:/tmp/in.mp4", "C:/tmp/out.mp4", INFO, a, "qsv", 1_500_000, q_off=2)
|
|
check("qsv_quality_ladder_step", argval(CAPTURED["cmd"], "-global_quality") == "28",
|
|
argval(CAPTURED["cmd"], "-global_quality"))
|
|
|
|
# ---- the regression: the OTHER two tiers' command lines are untouched -----------------------
|
|
x265 = cmd_for(["--tier", "x265", "--force-tier", "x265", "--crf", "26", "--x265-preset", "medium",
|
|
"--pools", "10"])
|
|
check("x265_command_is_unchanged",
|
|
x265 == [LR.FFMPEG, "-y", "-hide_banner", "-loglevel", "warning", "-stats",
|
|
"-i", "C:/tmp/in.mp4", "-map", "0:v:0", "-map", "0:a:0?", "-map_metadata", "0",
|
|
"-c:v", "libx265", "-preset", "medium", "-crf", "26", "-x265-params", "pools=10",
|
|
"-maxrate", "1500000", "-bufsize", "3750000",
|
|
"-profile:v", "main", "-pix_fmt", "yuv420p", "-tag:v", "hvc1", "-fps_mode", "cfr",
|
|
"-c:a", "copy", "-movflags", "+faststart", "C:/tmp/out.mp4"], x265)
|
|
nvenc = cmd_for(["--tier", "nvenc", "--force-tier", "nvenc", "--cq", "31", "--nvenc-preset", "p7"])
|
|
check("nvenc_command_is_unchanged",
|
|
nvenc == [LR.FFMPEG, "-y", "-hide_banner", "-loglevel", "warning", "-stats",
|
|
"-i", "C:/tmp/in.mp4", "-map", "0:v:0", "-map", "0:a:0?", "-map_metadata", "0",
|
|
"-c:v", "hevc_nvenc", "-preset", "p7", "-tune", "hq", "-rc", "vbr",
|
|
"-cq", "31", "-b:v", "0", "-spatial-aq", "1", "-temporal-aq", "1",
|
|
"-rc-lookahead", "32", "-b_ref_mode", "middle", "-bf", "4", "-g", "240",
|
|
"-maxrate", "1500000", "-bufsize", "3750000",
|
|
"-profile:v", "main", "-pix_fmt", "yuv420p", "-tag:v", "hvc1", "-fps_mode", "cfr",
|
|
"-c:a", "copy", "-movflags", "+faststart", "C:/tmp/out.mp4"], nvenc)
|
|
|
|
# ---------------------------------------------------------------- the failure classifier
|
|
which, line = LR.backend_failure("qsv", "Some noise\n[mfx] Error creating a MFX session: -9\nmore")
|
|
check("mfx_session_is_a_backend_failure", which == "qsv" and "MFX session" in line, (which, line))
|
|
which2, _ = LR.backend_failure("qsv", "Conversion failed!\nInvalid argument")
|
|
check("a_real_encode_failure_stays_a_real_failure", which2 is None)
|
|
which3, _ = LR.backend_failure("nvenc", "Cannot load nvcuda.dll")
|
|
check("nvenc_init_failure_is_recognised", which3 == "nvenc")
|
|
check("an_empty_stderr_is_not_a_backend_claim", LR.backend_failure("qsv", "")[0] is None)
|
|
|
|
# ---------------------------------------------------------------- the refusal (real process)
|
|
with tempfile.TemporaryDirectory(prefix="qsvtier_") as tmp:
|
|
work = os.path.join(tmp, "work")
|
|
os.makedirs(work)
|
|
src = os.path.join(tmp, "clip.mp4")
|
|
with open(src, "wb") as fh:
|
|
fh.write(b"\x00" * 4096)
|
|
before = os.path.getsize(src)
|
|
p = subprocess.run([PY, "-u", ENTRY, "--work-dir", work, "--path", src, "--tier", "qsv",
|
|
"--force-tier", "qsv"],
|
|
capture_output=True, text=True, encoding="utf-8", errors="replace")
|
|
check("qsv_without_measured_knobs_refuses", p.returncode != 0, "rc=%d" % p.returncode)
|
|
check("the_refusal_names_the_missing_numbers",
|
|
"backend qsv unmeasured" in (p.stdout or "") and "--qsv-quality" in (p.stdout or ""),
|
|
(p.stdout or "").strip()[:120])
|
|
check("the_refusal_touches_nothing",
|
|
os.listdir(work) == [] and os.path.getsize(src) == before,
|
|
"work=%s src=%d" % (os.listdir(work), os.path.getsize(src)))
|
|
p2 = subprocess.run([PY, "-u", ENTRY, "--work-dir", work, "--path", src, "--tier", "qsv",
|
|
"--qsv-quality", "30", "--qsv-preset", "ultrafast"],
|
|
capture_output=True, text=True, encoding="utf-8", errors="replace")
|
|
check("a_typo_in_the_preset_is_readable", p2.returncode != 0 and "unknown qsv preset" in
|
|
(p2.stdout or ""), (p2.stdout or "").strip()[:120])
|
|
|
|
print("\n%d/%d checks passed" % (sum(checks.values()), len(checks)))
|
|
sys.exit(0 if all(checks.values()) else 1)
|