Files
imSp4rky 35871f3a52 feat(hybrid): generate dolby vision from hdr10+ with --generate-dv
Builds per-scene L1 in unshackle (PQ mean, letterbox-aware) with a generate_dv_mode config (v29/v40/legacy); a failed hybrid no longer exits 0 with no video.
2026-10-17 16:40:14 -06:00

172 lines
5.2 KiB
Python

"""Thin wrappers around dovi_tool subcommands used by DVFixup and Hybrid.
Centralises argv construction, status spinners, and error handling so callers do not
re-implement subprocess plumbing per call site. Each wrapper:
- Resolves `binaries.DoviTool` and raises EnvironmentError if missing.
- Delegates to `core.utils.subprocess.run_step` for execution, output validation, and
stderr-tail RuntimeError on failure.
- Returns captured stderr so callers can inspect specific failure modes (e.g. the
MAX_PQ_LUMINANCE retry path in extract_rpu).
"""
from __future__ import annotations
import json
import subprocess
import time
from pathlib import Path
from typing import Optional
from unshackle.core import binaries
from unshackle.core.utils.subprocess import log_tool_run, run_step
def require_dovi_tool() -> str:
if not binaries.DoviTool:
raise EnvironmentError("dovi_tool executable was not found but is required.")
return str(binaries.DoviTool)
def extract_rpu(
source: Path,
output: Path,
*,
mode: Optional[int] = 3,
status: Optional[str] = "Extracting DV RPU...",
label: str = "dovi_tool extract-rpu",
) -> bytes:
"""Extract DV RPU NALs from a raw HEVC bitstream. `mode=None` skips the -m flag (untouched)."""
tool = require_dovi_tool()
args: list = [tool]
if mode is not None:
args += ["-m", str(mode)]
args += ["extract-rpu", source, "-o", output]
return run_step(args, status=status, output=output, label=label)
def inject_rpu(
source: Path,
rpu: Path,
output: Path,
*,
status: Optional[str] = "Re-injecting DV RPU...",
label: str = "dovi_tool inject-rpu",
) -> bytes:
"""Inject a DV RPU back into a raw HEVC bitstream, producing DV-signaled output."""
tool = require_dovi_tool()
return run_step(
[tool, "inject-rpu", "-i", source, "--rpu-in", rpu, "-o", output],
status=status,
output=output,
label=label,
)
def editor(
source: Path,
json_spec: Path,
output: Path,
*,
status: Optional[str] = "Editing DV RPU...",
label: str = "dovi_tool editor",
) -> bytes:
"""Apply a JSON edit spec to an RPU file."""
tool = require_dovi_tool()
return run_step(
[tool, "editor", "-i", source, "-j", json_spec, "-o", output],
status=status,
output=output,
label=label,
)
def info_summary(rpu: Path) -> str:
"""Return the textual summary (`dovi_tool info -i ... -s`) for an RPU file."""
tool = require_dovi_tool()
info_start = time.monotonic()
p = subprocess.run(
[tool, "info", "-i", str(rpu), "-s"], capture_output=True, text=True, encoding="utf-8", errors="replace"
)
log_tool_run(
"dovi_tool info",
"dovi_tool",
p.returncode,
duration_ms=round((time.monotonic() - info_start) * 1000, 1),
)
if p.returncode != 0:
raise RuntimeError(f"dovi_tool info failed: {(p.stderr or '')[-400:]}")
return p.stdout
def info_frame(rpu: Path, frame: int = 0) -> dict:
"""Return the parsed RPU data for one frame (`dovi_tool info -i ... -f N`)."""
tool = require_dovi_tool()
info_start = time.monotonic()
p = subprocess.run(
[tool, "info", "-i", str(rpu), "-f", str(frame)],
capture_output=True,
text=True,
encoding="utf-8",
errors="replace",
)
log_tool_run(
"dovi_tool info",
"dovi_tool",
p.returncode,
duration_ms=round((time.monotonic() - info_start) * 1000, 1),
)
if p.returncode != 0:
raise RuntimeError(f"dovi_tool info failed: {(p.stderr or '')[-400:]}")
start = p.stdout.find("{")
if start < 0:
raise RuntimeError("dovi_tool info returned no JSON")
try:
return json.loads(p.stdout[start:])
except json.JSONDecodeError as e:
raise RuntimeError(f"dovi_tool info returned invalid JSON: {e}")
def generate(
config_json: Path,
output: Path,
*,
hdr10plus_json: Optional[Path] = None,
status: Optional[str] = "Generating DV RPU...",
label: str = "dovi_tool generate",
) -> bytes:
"""Build a DV RPU from a dovi_tool generator config, and from hdr10plus_tool JSON when given."""
tool = require_dovi_tool()
extra = ["--hdr10plus-json", hdr10plus_json] if hdr10plus_json else []
return run_step(
[tool, "generate", "-j", config_json, *extra, "-o", output],
status=status,
output=output,
label=label,
)
def extract_rpu_with_fallback(source: Path, output: Path, *, label: str = "dovi_tool extract-rpu") -> bytes:
"""Try `-m 3` first. On a MAX_PQ_LUMINANCE error, retry untouched (no -m). Returns stderr.
Used when the caller wants automatic normalization but cannot abort if the source
rejects mode-3 conversion.
"""
try:
return extract_rpu(source, output, mode=3, label=label)
except RuntimeError as e:
if "MAX_PQ_LUMINANCE" not in str(e):
raise
return extract_rpu(source, output, mode=None, status="Extracting DV RPU (untouched)...", label=label)
__all__ = (
"extract_rpu",
"extract_rpu_with_fallback",
"inject_rpu",
"editor",
"info_summary",
"info_frame",
"generate",
)