Skip to content

DLSS 5 Neural Rendering for Images & Video with Frame Generation, RTX Video Super Resolution, HDR, and Color Grading

Add files via upload

Merserkf331f88b2288
diff --git a/app.py b/app.py
index e92106f..b8e0228 100644
--- a/app.py
+++ b/app.py
@@ -19,12 +19,19 @@ except Exception:
import gradio as gr
-from src.core.paths import LOGS, OUTPUTS
+from src.core.cache_cleanup import (
+ CACHE_MAX_AGE_SECONDS,
+ CACHE_SWEEP_INTERVAL_SECONDS,
+ cleanup_old_caches,
+)
+from src.core.dlss_architecture import apply_dlss_architecture, init_dlssnr_runtime
+from src.core.paths import LIVE_DIR, LOGS, OUTPUTS
from src.core.runtime import prepare_runtime
from src.core.terminal import init_console
from src.frame_interpolation.ui import build_frame_interpolation_tab
from src.image.decoder import initialize_image_runtime
from src.image.ui import build_image_tab
+from src.live.ui import build_live_tab
from src.settings.ui import bind_settings_events, build_settings_tab, initialize_settings
from src.video.ui import build_video_tab
@@ -109,8 +116,21 @@ def build_app() -> gr.Blocks:
prepared = prepare_runtime()
initialize_image_runtime()
settings, _gpu_warning, ai_gpu_choices, video_gpu_choices = initialize_settings(prepared)
+ # Re-apply the DLSS Architecture NR runtime for the loaded setting
+ # (no-op when the host copy already matches; failures only warn).
+ try:
+ apply_dlss_architecture(settings.dlss_architecture, prepared.gpu)
+ except Exception:
+ pass
- with gr.Blocks(title="DLSS 5 Visual Enhancer") as demo:
+ with gr.Blocks(
+ title="DLSS 5 Visual Enhancer",
+ # Official Gradio cache cleanup (see guides/resource-cleanup): every
+ # CACHE_SWEEP_INTERVAL_SECONDS, delete tracked temp files older than
+ # CACHE_MAX_AGE_SECONDS; full wipe on graceful shutdown. Crash orphans
+ # are covered by cleanup_old_caches() at startup in main().
+ delete_cache=(CACHE_SWEEP_INTERVAL_SECONDS, CACHE_MAX_AGE_SECONDS),
+ ) as demo:
gr.Markdown(
"# DLSS 5 Visual Enhancer\n"
"[Support on Patreon](https://www.patreon.com/MM744) | "
@@ -124,16 +144,39 @@ def build_app() -> gr.Blocks:
video_tab = build_video_tab(settings)
with gr.Tab("Frame Interpolation", id="frame-interpolation"):
frame_tab = build_frame_interpolation_tab(settings)
+ with gr.Tab("Live", id="live"):
+ live_tab = build_live_tab(settings)
with gr.Tab("Settings", id="settings"):
settings_tab = build_settings_tab(settings, ai_gpu_choices, video_gpu_choices)
- bind_settings_events(settings_tab, image_tab, video_tab, frame_tab)
+ bind_settings_events(settings_tab, image_tab, video_tab, frame_tab, live_tab)
return demo
def main() -> None:
OUTPUTS.mkdir(exist_ok=True)
LOGS.mkdir(exist_ok=True)
+ LIVE_DIR.mkdir(exist_ok=True)
+ # Drop leftover Live session dirs from dead runs (previous process is gone).
+ try:
+ from src.live.pipeline import sweep_stale_live_dirs
+
+ sweep_stale_live_dirs()
+ except Exception:
+ pass
+ # Remove stale Gradio caches / temp leftovers from previous (possibly
+ # crashed) runs before serving. Best effort: never blocks startup.
+ try:
+ cleanup_old_caches()
+ except Exception:
+ pass
+ # Ensure the per-architecture NR runtimes exist and stage the saved
+ # DLSS Architecture build into host/ before prepare_runtime() warms it.
+ # Best effort: never blocks startup.
+ try:
+ init_dlssnr_runtime()
+ except Exception:
+ pass
# Reuse early loading UI if it was already rendered at import time (avoids second flash and keeps alt buffer)
global _early_ui # type: ignore
if "_EARLY_UI" in globals() and _early_ui is not None and getattr(_early_ui, "_alt_active", False):
diff --git a/src/core/__init__.py b/src/core/__init__.py
index 035fb1f..826ec2c 100644
--- a/src/core/__init__.py
+++ b/src/core/__init__.py
@@ -1,12 +1,12 @@
from .gpu_detection import clear_gpu_detection_cache, detect_gpus
from .gpu_selection import detect_gpu, gpu_choice_label, resolve_ai_gpu, resolve_runtime_ai_gpu
from .jobs import Cancelled, JobController, active_job, cancel_active_job
-from .paths import ADDON, DLSS_DIR, DLSS_SUPERRES, DLSSG_DIR, FFMPEG, FFPROBE, HOST_DIR, HOST_DXGI, JOBS, LOGS, NEURAL_RUNTIME, OUTPUTS, RESHADE_LOG, ROOT, RUNTIME, WORKER
+from .paths import ADDON, DLSS_DIR, DLSS_SUPERRES, DLSSG_DIR, DLSSNR_DIR, FFMPEG, FFPROBE, HOST_DIR, HOST_DXGI, JOBS, LIVE_DIR, LOGS, MPV, NEURAL_RUNTIME, OUTPUTS, RESHADE_LOG, ROOT, RUNTIME, WORKER, YTDLP
__all__ = [
- "ADDON", "Cancelled", "DLSS_DIR", "DLSS_SUPERRES", "DLSSG_DIR", "FFMPEG", "FFPROBE",
- "HOST_DIR", "HOST_DXGI", "JOBS", "LOGS", "NEURAL_RUNTIME",
- "OUTPUTS", "RESHADE_LOG", "ROOT", "RUNTIME", "WORKER", "JobController", "active_job",
+ "ADDON", "Cancelled", "DLSS_DIR", "DLSS_SUPERRES", "DLSSG_DIR", "DLSSNR_DIR", "FFMPEG", "FFPROBE",
+ "HOST_DIR", "HOST_DXGI", "JOBS", "LIVE_DIR", "LOGS", "MPV", "NEURAL_RUNTIME",
+ "OUTPUTS", "RESHADE_LOG", "ROOT", "RUNTIME", "WORKER", "YTDLP", "JobController", "active_job",
"cancel_active_job", "clear_gpu_detection_cache", "detect_gpu", "detect_gpus",
"gpu_choice_label", "resolve_ai_gpu", "resolve_runtime_ai_gpu",
]
diff --git a/src/core/batch_progress.py b/src/core/batch_progress.py
new file mode 100644
index 0000000..dcec055
--- /dev/null
+++ b/src/core/batch_progress.py
@@ -0,0 +1,165 @@
+"""Structured, monotonic batch progress shared by processors and the UI."""
+from __future__ import annotations
+
+import math
+import logging
+import threading
+import time
+from dataclasses import asdict, dataclass, replace
+from pathlib import Path
+from typing import Callable
+
+TERMINAL = {"Complete", "Failed", "Cancelled", "Skipped"}
+
+
+@dataclass(frozen=True, slots=True)
+class BatchItemUpdate:
+ index: int | None
+ state: str
+ progress: float = 0.0
+ detail: str = ""
+ output_path: str = ""
+ elapsed_seconds: float = 0.0
+ manifest_path: str = ""
+
+
+@dataclass(slots=True)
+class BatchItem:
+ index: int
+ input_path: str
+ state: str = "Queued"
+ progress: float = 0.0
+ detail: str = ""
+ output_path: str = ""
+ elapsed_seconds: float = 0.0
+ started: float | None = None
+
+
+class BatchProgress:
+ def __init__(self, paths, callback: Callable[[BatchItemUpdate], None] | None = None, progress=None):
+ self.items = [BatchItem(i, str(path)) for i, path in enumerate(paths)]
+ self.callback = callback
+ self.legacy_progress = progress
+ self.started = time.monotonic()
+ self.finished: float | None = None
+ self.state = "Running"
+ self.detail = ""
+ self.manifest_path = ""
+ self.lock = threading.RLock()
+
+ def apply(self, update: BatchItemUpdate) -> None:
+ with self.lock:
+ if self.finished is not None:
+ return
+ if update.index is None:
+ self.finished = time.monotonic()
+ self.state, self.detail = update.state, update.detail
+ self.manifest_path = update.manifest_path
+ return
+ item = self.items[update.index]
+ if item.state in TERMINAL:
+ return
+ if update.state == "Running" and item.started is None:
+ item.started = time.monotonic()
+ value = update.progress if math.isfinite(update.progress) else item.progress
+ item.progress = (1.0 if update.state == "Complete" else
+ 0.0 if update.state == "Skipped" else
+ min(.99, max(item.progress, value)))
+ item.state = update.state
+ item.detail = update.detail
+ item.output_path = update.output_path or item.output_path
+ item.elapsed_seconds = max(item.elapsed_seconds, update.elapsed_seconds)
+
+ def emit(self, update: BatchItemUpdate) -> None:
+ self.apply(update)
+ if self.callback:
+ try:
+ self.callback(update)
+ except Exception:
+ logging.getLogger(__name__).exception("Batch progress listener failed; rendering continues")
+ self.callback = None
+ if self.legacy_progress:
+ try:
+ self.legacy_progress(self.statistics()["progress"], update.detail or update.state)
+ except Exception:
+ self.legacy_progress = None
+
+ def advance(self, index: int, value: float = 0.0, detail: str = "Preparing") -> None:
+ self.emit(BatchItemUpdate(index, "Running", value, detail, elapsed_seconds=self.elapsed(index)))
+
+ def elapsed(self, index: int) -> float:
+ item = self.items[index]
+ return time.monotonic() - item.started if item.started is not None else 0.0
+
+ def complete(self, index: int, output_path: str, detail: str = "Saved and verified") -> None:
+ self.emit(BatchItemUpdate(index, "Complete", 1.0, detail, str(output_path), self.elapsed(index)))
+
+ def fail(self, index: int, error, *, cancelled: bool = False) -> None:
+ self.emit(BatchItemUpdate(index, "Cancelled" if cancelled else "Failed",
+ self.items[index].progress, str(error), elapsed_seconds=self.elapsed(index)))
+
+ def skip_from(self, index: int, detail: str = "Cancelled before rendering.") -> None:
+ for item in self.items[index:]:
+ if item.state == "Queued":
+ self.emit(BatchItemUpdate(item.index, "Skipped", detail=detail))
+
+ def finish(self, *, cancelled: bool = False, error: str = "", manifest_path: str = "") -> None:
+ if self.finished is not None:
+ return
+ if cancelled or error:
+ for item in self.items:
+ if item.state == "Running":
+ self.fail(item.index, error or "Stopped by user.", cancelled=cancelled)
+ self.skip_from(0, "Cancelled before rendering." if cancelled else "Batch could not continue.")
+ failures = sum(item.state == "Failed" for item in self.items)
+ successes = sum(item.state == "Complete" for item in self.items)
+ state = ("Cancelled" if cancelled else "Failed" if error or (failures and not successes)
+ else "Complete with errors" if failures else "Complete")
+ self.emit(BatchItemUpdate(None, state, detail=error, manifest_path=manifest_path))
+
+ def statistics(self) -> dict:
+ with self.lock:
+ counts = {state.lower(): sum(item.state == state for item in self.items)
+ for state in ("Queued", "Running", "Complete", "Failed", "Cancelled", "Skipped")}
+ progress = sum(1.0 if item.state in {"Complete", "Failed"} else item.progress
+ for item in self.items) / max(1, len(self.items))
+ if self.finished is None:
+ progress = min(.99, progress)
+ return {"state": self.state, "total": len(self.items), **counts,
+ "progress": progress, "elapsed_seconds": (self.finished or time.monotonic()) - self.started}
+
+ def diagnostics(self, *, final: bool = False) -> dict:
+ with self.lock:
+ statistics = self.statistics()
+ if final:
+ statistics["state"] = ("Cancelled" if statistics["cancelled"] or statistics["skipped"] else
+ "Failed" if statistics["failed"] and not statistics["complete"] else
+ "Complete with errors" if statistics["failed"] else "Complete")
+ if statistics["state"] != "Cancelled":
+ statistics["progress"] = 1.0
+ return {"items": [{k: v for k, v in asdict(item).items() if k != "started"} for item in self.items],
+ "statistics": statistics}
+
+ def display(self, output_dir: str = "") -> tuple[list[list[str]], str]:
+ with self.lock:
+ items = [replace(item) for item in self.items]
+ stats = self.statistics()
+ detail, manifest = self.detail, self.manifest_path
+ rows = [[Path(item.input_path).name, item.state,
+ f"{min(99, int(item.progress * 100)) if item.state != 'Complete' else 100}%",
+ f"{self.elapsed(item.index) if item.state == 'Running' else item.elapsed_seconds:.1f}s",
+ item.output_path, (item.detail if len(item.detail) <= 600 else
+ item.detail[:600] + "… (full details in the batch report)")] for item in items]
+ current = next((f"{item.index + 1}/{len(items)} — {Path(item.input_path).name}" for item in items
+ if item.state == "Running"), "None")
+ status = (f"{stats['state']} — {stats['progress']:.0%}\n"
+ f"Files: {stats['total']} | Completed: {stats['complete']} | Failed: {stats['failed']} | "
+ f"Cancelled: {stats['cancelled']} | Skipped: {stats['skipped']} | Queued: {stats['queued']}\n"
+ f"Current: {current} | Elapsed: {stats['elapsed_seconds']:.1f}s")
+ if output_dir:
+ status += f"\nOutput folder: {output_dir}"
+ if detail:
+ status += f"\n{detail}"
+ if manifest:
+ status += f"\nBatch report: {manifest}"
+ return rows, status
diff --git a/src/core/batch_ui.py b/src/core/batch_ui.py
new file mode 100644
index 0000000..94a3956
--- /dev/null
+++ b/src/core/batch_ui.py
@@ -0,0 +1,233 @@
+"""Shared Gradio disk controls and a bounded, session-owned streaming batch UI."""
+from __future__ import annotations
+
+import threading
+import uuid
+from dataclasses import dataclass, field
+
+import gradio as gr
+
+from .batch_progress import BatchProgress
+from .disk_paths import direct_disk_mode, prepare_output_dir, resolve_inputs
+from .jobs import JobController, use_job_controller
+
+BATCH_HEADERS = ["File", "State", "Progress", "Elapsed", "Output path", "Details"]
+
+
+def build_path_controls():
+ source = gr.Textbox(label="Input path", placeholder=r"C:\Users\You\Downloads\Videos",
+ info="Absolute file or folder path. Overrides uploads; folder contents only, sorted by filename.")
+ destination = gr.Textbox(label="Output path", placeholder=r"D:\Rendered outputs",
+ info="Output folder; blank uses the app's outputs folder. Either path disables previews and downloads.")
+ return source, destination
+
+
+class BatchRun:
+ def __init__(self, paths):
+ self.controller = JobController()
+ self.progress = BatchProgress(paths)
+ self.done = threading.Event()
+ self.result = None
+ self.error = ""
+ self.thread = None
+ self.is_preview = False
+
+ def start(self, operation):
+ def work():
+ try:
+ with use_job_controller(self.controller):
+ self.result = operation(self)
+ except BaseException as exc:
+ self.error = str(exc)
+ self.progress.finish(cancelled=self.controller.cancel.is_set(), error=str(exc))
+ finally:
+ self.done.set()
+ self.thread = threading.Thread(target=work, name="dlss-batch-ui", daemon=True)
+ self.thread.start()
+
+ def close(self):
+ if not self.done.is_set():
+ self.controller.stop()
+ if self.thread is not None:
+ self.thread.join()
+
+
+@dataclass
+class _View:
+ lock: threading.RLock = field(default_factory=threading.RLock)
+ job: BatchRun | None = None
+ revision: int = 0
+ disk: bool = False
+
+
+_views: dict[str, _View] = {}
+_views_lock = threading.Lock()
+
+
+def _view(key):
+ with _views_lock:
+ return _views.setdefault(key, _View())
+
+
+def release_view(key):
+ with _views_lock:
+ view = _views.pop(key, None)
+ if view and view.job:
+ view.job.controller.stop()
+
+
+def bind_batch_ui(tab, render_function, *, kind, preview_mode, preview_actions=()):
+ """Media values are only returned in upload mode, once a batch is finished."""
+ tab.job_state = gr.State(value=lambda: uuid.uuid4().hex, delete_callback=release_view)
+ is_image = kind == "image"
+ input_media = tab.input_gallery if is_image else tab.input_preview
+ media = ([tab.output_gallery, tab.output_files, tab.zip_download] if is_image else
+ [tab.output_video, tab.output_files])
+ preview_buttons = [button for button, _fn in preview_actions]
+ controls = [tab.sources, tab.input_path, tab.output_path, tab.render, tab.reset, *preview_buttons]
+ outputs = [*media, tab.results, tab.status, *controls]
+ path_inputs = [tab.job_state, tab.input_path, tab.output_path]
+
+ def empty_media(disk):
+ return [gr.update(value=None, visible=not disk) for _ in media]
+
+ def control_updates(busy, input_path):
+ return [gr.update(interactive=not busy and not bool(str(input_path or "").strip())),
+ *[gr.update(interactive=not busy) for _ in controls[1:]]]
+
+ def refresh(key, input_path, output_path, *args):
+ view = _view(key)
+ disk = direct_disk_mode(input_path, output_path)
+ with view.lock:
+ view.revision += 1
+ revision = view.revision
+ view.disk = disk
+ busy = view.job is not None and not view.job.done.is_set()
+ if busy and disk and view.job is not None and view.job.is_preview:
+ view.job.controller.stop()
+ if busy and not disk:
+ return [gr.skip()] * (1 + len(media) + len(preview_buttons) + 1)
+ if disk:
+ values = [gr.update(value=None, visible=False), *empty_media(True),
+ *[gr.update(visible=False) for _ in preview_buttons]]
+ elif is_image:
+ previews = preview_mode(args[0])
+ values = [gr.update(value=previews, visible=bool(previews)), *empty_media(False)]
+ else:
+ input_update, output_update, *buttons = preview_mode(*args)
+ values = [input_update, output_update, gr.update(value=None, visible=True), *buttons]
+ with view.lock:
+ if revision != view.revision:
+ return [gr.skip()] * (1 + len(media) + len(preview_buttons) + 1)
+ return [*values, gr.update(interactive=not bool(str(input_path or "").strip()))]
+
+ source_args = [tab.sources]
+ if hasattr(tab, "target_fps"):
+ source_args += [tab.target_fps, tab.engine]
+ refresh_outputs = [input_media, *media, *preview_buttons, tab.sources]
+ for component in (tab.sources, tab.input_path, tab.output_path):
+ event = component.change if component is tab.sources else component.input
+ event(refresh, inputs=[*path_inputs, *source_args], outputs=refresh_outputs,
+ queue=False, show_progress="hidden")
+
+ def stream(key, input_path, output_path, *args):
+ view = _view(key)
+ with view.lock:
+ if view.job is not None:
+ raise gr.Error("A job is already running in this tab.")
+ job = BatchRun([])
+ view.job = job
+ view.disk = direct_disk_mode(input_path, output_path)
+ disk = view.disk
+ destination = ""
+ started = False
+ try:
+ # Resolve once. Direct paths never become a gr.File/gr.Video/Gallery value.
+ paths = resolve_inputs(input_path, args[0], kind)
+ destination = str(prepare_output_dir(output_path, user_input=True))
+ job.progress = BatchProgress(paths)
+ row_values, status = job.progress.display(destination)
+ yield (*empty_media(disk), row_values, status, *control_updates(True, input_path))
+ def operation(run):
+ return render_function(
+ paths, *args[1:], progress=lambda *a, **k: None,
+ output_dir=destination, controller=run.controller,
+ on_item_update=run.progress.apply, direct_disk=disk,
+ )
+ job.start(operation)
+ started = True
+ while not job.done.wait(.25):
+ with view.lock:
+ if view.job is not job:
+ return
+ row_values, status = job.progress.display(destination)
+ if job.controller.cancel.is_set():
+ status += "\nStopping — cleaning up the current job. Completed outputs will be kept."
+ # No media postprocessing/cache transfer during progress refreshes.
+ yield (*[gr.skip() for _ in media], row_values, status,
+ *[gr.skip() for _ in controls])
+ job.close()
+ row_values, status = job.progress.display(destination)
+ final_media = empty_media(disk)
+ if job.result is not None and not disk:
+ final_media = list(job.result[:len(media)])
+ if job.error and job.error not in status:
+ status += f"\n{job.error}"
+ with view.lock:
+ if view.job is not job:
+ return
+ yield (*final_media, row_values, status, *control_updates(False, input_path))
+ except Exception as exc:
+ job.progress.finish(cancelled=job.controller.cancel.is_set(), error=str(exc))
+ row_values, status = job.progress.display(destination)
+ yield (*empty_media(view.disk), row_values, status, *control_updates(False, input_path))
+ finally:
+ if started:
+ job.close()
+ with view.lock:
+ if view.job is job:
+ view.job = None
+
+ tab.render.click(stream, inputs=[*path_inputs, *tab.render_inputs], outputs=outputs,
+ concurrency_limit=None, trigger_mode="once", show_progress="hidden")
+
+ def stop(key):
+ view = _view(key)
+ with view.lock:
+ job = view.job
+ if job is not None and not job.done.is_set():
+ job.controller.stop()
+ # The streaming callback remains the only writer of batch status.
+ return gr.skip()
+ tab.stop.click(stop, inputs=tab.job_state, outputs=tab.status, queue=False, show_progress="hidden")
+
+ def guarded_preview(function):
+ def preview(key, input_path, output_path, *args, progress=gr.Progress(track_tqdm=False)):
+ view = _view(key)
+ if direct_disk_mode(input_path, output_path):
+ return gr.update(value=None, visible=False), "Previews are disabled when a path is supplied."
+ with view.lock:
+ if view.job is not None:
+ raise gr.Error("A job is already running in this tab.")
+ job = BatchRun([])
+ job.is_preview = True
+ view.job = job
+ revision = view.revision
+ try:
+ with use_job_controller(job.controller):
+ result = function(*args, progress=progress)
+ if job.controller.cancel.is_set():
+ return gr.update(value=None, visible=False), "Preview cancelled."
+ with view.lock:
+ if view.revision != revision or view.disk:
+ return gr.skip(), gr.skip()
+ return result
+ finally:
+ job.done.set()
+ with view.lock:
+ if view.job is job:
+ view.job = None
+ return preview
+ for button, function in preview_actions:
+ button.click(guarded_preview(function), inputs=[*path_inputs, *tab.preview_inputs],
+ outputs=[tab.output_video, tab.status], concurrency_limit=None, show_progress="hidden")
diff --git a/src/core/cache_cleanup.py b/src/core/cache_cleanup.py
new file mode 100644
index 0000000..9be819a
--- /dev/null
+++ b/src/core/cache_cleanup.py
@@ -0,0 +1,183 @@
+from __future__ import annotations
+
+"""Automatic cleanup of stale Gradio caches and app-owned temp leftovers.
+
+Official Gradio mechanism (see https://gradio.app/guides/resource-cleanup and
+``gr.Blocks(delete_cache=...)`` docs): while the server runs, Gradio
+periodically deletes tracked temp files older than ``age`` seconds, and wipes
+the session cache on graceful shutdown. That does NOT cover files orphaned by
+a crashed/killed process (untracked, never cleaned), which is why this module
+performs a best-effort, age-gated sweep at startup over:
+
+1. The Gradio cache dir (``GRADIO_TEMP_DIR`` or ``<temp>/gradio``).
+2. ``<temp>/dlss5-settings-preset-*`` dirs leaked by settings preset export.
+3. Orphaned ``JOBS/*`` per-render dirs left behind by killed runs.
+
+Only entries older than ``CACHE_MAX_AGE_SECONDS`` are removed, so files from
+concurrently running Gradio apps (fresh mtime/ctime) are left alone. Final
+deliverables in ``OUTPUTS/`` and anything in ``LOGS/`` are never touched.
+Every failure is swallowed per-entry so cleanup can never block startup.
+"""
+
+import os
+import shutil
+import tempfile
+import time
+from pathlib import Path
+
+from .paths import JOBS, LOGS
+
+# Agreed retention: sweep hourly while running (via Blocks delete_cache),
+# treat anything older than 24h as stale (startup sweep + periodic sweep).
+CACHE_SWEEP_INTERVAL_SECONDS = 3600
+CACHE_MAX_AGE_SECONDS = 24 * 3600
+
+_PRESET_TMP_PREFIX = "dlss5-settings-preset-"
+
+
+def resolve_gradio_temp_dir() -> Path | None:
+ """Mirror Gradio's own cache location (env override or ``<temp>/gradio``)."""
+ configured = os.environ.get("GRADIO_TEMP_DIR")
+ candidate = Path(configured) if configured else Path(tempfile.gettempdir()) / "gradio"
+ try:
+ resolved = candidate.resolve()
+ except OSError:
+ return None
+ return resolved if resolved.is_dir() else None
+
+
+def _entry_age_seconds(stat_result: os.stat_result, now: float) -> float:
+ # Stale only when BOTH mtime and ctime are old: protects files that were
+ # created long ago but are still being written/read, and vice versa.
+ newest = max(stat_result.st_mtime, getattr(stat_result, "st_ctime", stat_result.st_mtime))
+ return now - newest
+
+
+def _remove_entry(path: Path) -> int:
+ """Remove one top-level cache entry; return bytes freed (best effort)."""
+ try:
+ if path.is_symlink() or path.is_file():
+ try:
+ size = path.stat().st_size
+ except OSError:
+ size = 0
+ path.unlink(missing_ok=True)
+ return size
+ if path.is_dir():
+ size = 0
+ try:
+ for root, _dirs, files in os.walk(path):
+ for name in files:
+ try:
+ size += (Path(root) / name).stat().st_size
+ except OSError:
+ continue
+ except OSError:
+ pass
+ shutil.rmtree(path, ignore_errors=True)
+ return size
+ except OSError:
+ pass
+ return 0
+
+
+def sweep_dir_by_age(root: Path, max_age_seconds: int) -> tuple[int, int]:
+ """Remove direct children of ``root`` older than ``max_age_seconds``.
+
+ Returns ``(removed_count, freed_bytes)``. Never raises, never removes
+ ``root`` itself, never follows symlinks.
+ """
+ removed = 0
+ freed = 0
+ try:
+ entries = list(os.scandir(root))
+ except OSError:
+ return (0, 0)
+ now = time.time()
+ for entry in entries:
+ try:
+ stat_result = os.stat(entry.path, follow_symlinks=False)
+ except OSError:
+ continue
+ if _entry_age_seconds(stat_result, now) <= max_age_seconds:
+ continue
+ freed += _remove_entry(Path(entry.path))
+ removed += 1
+ return (removed, freed)
+
+
+def _sweep_preset_tmpdirs(temp_root: Path, max_age_seconds: int) -> tuple[int, int]:
+ """Remove stale ``dlss5-settings-preset-*`` dirs from the system temp dir."""
+ removed = 0
+ freed = 0
+ try:
+ entries = list(os.scandir(temp_root))
+ except OSError:
+ return (0, 0)
+ now = time.time()
+ for entry in entries:
+ if not entry.name.startswith(_PRESET_TMP_PREFIX):
+ continue
+ try:
+ stat_result = os.stat(entry.path, follow_symlinks=False)
+ except OSError:
+ continue
+ if _entry_age_seconds(stat_result, now) <= max_age_seconds:
+ continue
+ freed += _remove_entry(Path(entry.path))
+ removed += 1
+ return (removed, freed)
+
+
+def cleanup_old_caches(
+ max_age_seconds: int = CACHE_MAX_AGE_SECONDS,
+ logs_dir: Path | None = None,
+) -> dict[str, int]:
+ """Sweep all stale cache locations. Never raises; logs one summary line.
+
+ Returns ``{"removed": N, "freed_bytes": B}`` for optional status display.
+ """
+ started = time.time()
+ removed_total = 0
+ freed_total = 0
+
+ gradio_dir = resolve_gradio_temp_dir()
+ if gradio_dir is not None:
+ try:
+ removed, freed = sweep_dir_by_age(gradio_dir, max_age_seconds)
+ removed_total += removed
+ freed_total += freed
+ except Exception:
+ pass
+
+ try:
+ system_temp = Path(tempfile.gettempdir())
+ removed, freed = _sweep_preset_tmpdirs(system_temp, max_age_seconds)
+ removed_total += removed
+ freed_total += freed
+ except Exception:
+ pass
+
+ try:
+ if JOBS.is_dir():
+ removed, freed = sweep_dir_by_age(JOBS, max_age_seconds)
+ removed_total += removed
+ freed_total += freed
+ except Exception:
+ pass
+
+ elapsed = time.time() - started
+ try:
+ destination = logs_dir or LOGS
+ destination.mkdir(parents=True, exist_ok=True)
+ stamp = time.strftime("%Y-%m-%d %H:%M:%S")
+ with open(destination / "cache_cleanup.log", "a", encoding="utf-8") as handle:
+ handle.write(
+ f"{stamp} removed={removed_total} "
+ f"freed_mb={freed_total / (1024 * 1024):.1f} "
+ f"elapsed_s={elapsed:.1f} "
+ f"max_age_s={max_age_seconds}\n"
+ )
+ except OSError:
+ pass
+ return {"removed": removed_total, "freed_bytes": freed_total}
diff --git a/src/core/disk_paths.py b/src/core/disk_paths.py
new file mode 100644
index 0000000..754cddd
--- /dev/null
+++ b/src/core/disk_paths.py
@@ -0,0 +1,118 @@
+"""Direct filesystem input and non-overwriting publication of rendered files."""
+from __future__ import annotations
+
+import mimetypes
+import os
+import tempfile
+from pathlib import Path
+
+from .naming import require_available_output
+from .paths import OUTPUTS
+
+
+def clean_path(value: str | None) -> str:
+ value = str(value or "").strip()
+ if len(value) >= 2 and value[0] == value[-1] and value[0] in "\"'":
+ value = value[1:-1].strip()
+ return value
+
+
+def direct_disk_mode(input_path: str | None, output_path: str | None) -> bool:
+ return bool(str(input_path or "").strip() or str(output_path or "").strip())
+
+
+def _absolute(value: str, label: str) -> Path:
+ path = Path(clean_path(value))
+ if not clean_path(value) or not path.is_absolute():
+ raise ValueError(f"{label} must be an absolute path on this computer.")
+ return path.resolve()
+
+
+def supported_file(path: Path, kind: str) -> bool:
+ suffix = path.suffix.casefold()
+ if kind == "image":
+ # Lazy imports avoid coupling the core module's initialization to Image.
+ from PIL import Image
+ from ..image.models import RAW_EXTENSIONS
+ return suffix in (set(Image.registered_extensions()) | RAW_EXTENSIONS | {".svg", ".heic", ".heif"})
+ if kind != "video":
+ raise ValueError(f"Unknown media type: {kind}")
+ # Windows file associations sometimes label still-image containers video/heic.
+ if suffix in {".heic", ".heif", ".avif", ".avifs"}:
+ return False
+ mime = mimetypes.guess_type(path.name)[0] or ""
+ return mime.startswith("video/") or suffix in {
+ ".mkv", ".m4v", ".mts", ".m2ts", ".ts", ".mxf", ".vob", ".wmv", ".flv",
+ ".mp4", ".mov", ".avi", ".webm", ".mpg", ".mpeg", ".mpe", ".ogv",
+ ".3gp", ".3g2", ".asf", ".divx", ".f4v", ".m2v", ".m1v", ".m2t",
+ }
+
+
+def resolve_inputs(input_path: str | None, uploads, kind: str) -> list[str]:
+ if str(input_path or "").strip():
+ path = _absolute(input_path, "Input path")
+ if path.is_file():
+ if not supported_file(path, kind):
+ raise ValueError(f"Not a supported {kind} file: {path.name}")
+ return [str(path)]
+ if not path.is_dir():
+ raise ValueError(f"Input path does not exist or is not accessible: {path}")
+ files = sorted(
+ (item for item in path.iterdir() if item.is_file() and supported_file(item, kind)),
+ key=lambda item: (item.name.casefold(), item.name),
+ )
+ if not files:
+ raise ValueError(f"No supported {kind} files in this folder (subfolders are excluded).")
+ return [str(item.resolve()) for item in files]
+ values = [uploads] if isinstance(uploads, (str, os.PathLike)) else list(uploads or [])
+ if not values:
+ raise ValueError(f"Enter an Input path or choose at least one {kind}.")
+ return [str(Path(value).resolve()) for value in values]
+
+
+def prepare_output_dir(value=None, *, default: Path = OUTPUTS, user_input: bool = False) -> Path:
+ if user_input and str(value or "").strip():
+ directory = _absolute(value, "Output path")
+ else:
+ directory = Path(value or default).resolve()
+ directory.mkdir(parents=True, exist_ok=True)
+ if not directory.is_dir():
+ raise ValueError(f"Output path must be a directory: {directory}")
+ # Check actual create permission, including ACLs and network shares.
+ with tempfile.TemporaryFile(dir=directory):
+ pass
+ return directory
+
+
+class OutputFile:
+ """Own a same-directory temporary file; never replace an existing destination."""
+
+ def __init__(self, destination: Path):
+ self.destination = destination
+ require_available_output(destination)
+ handle, name = tempfile.mkstemp(prefix=".dlss-", suffix=destination.suffix, dir=destination.parent)
+ os.close(handle)
+ self.temporary = Path(name)
+ self._published_identity = None
+
+ def publish(self) -> None:
+ if not self.temporary.stat().st_size:
+ raise RuntimeError("The encoder produced an empty output.")
+ if os.name == "nt":
+ # Windows rename is atomic and fails if destination already exists.
+ os.rename(self.temporary, self.destination)
+ else:
+ os.link(self.temporary, self.destination)
+ self.temporary.unlink()
+ stat = self.destination.stat()
+ self._published_identity = (stat.st_dev, stat.st_ino)
+
+ def cleanup(self, *, rollback: bool = False) -> None:
+ self.temporary.unlink(missing_ok=True)
+ if rollback and self._published_identity is not None:
+ try:
+ stat = self.destination.stat()
+ if (stat.st_dev, stat.st_ino) == self._published_identity:
+ self.destination.unlink()
+ except FileNotFoundError:
+ pass
diff --git a/src/core/dlss_architecture.py b/src/core/dlss_architecture.py
new file mode 100644
index 0000000..6c5a4fe
--- /dev/null
+++ b/src/core/dlss_architecture.py
@@ -0,0 +1,250 @@
+from __future__ import annotations
+
+"""Selectable DLSS 5 Neural Rendering architecture builds.
+
+``bin/runtime/dlssnr/`` holds one ``nvngx_dlssnr.dll`` per GPU-architecture
+floor (extracted from the ``bin/*.zip`` archives)::
+
+ dlssnr/Turing+/nvngx_dlssnr.dll
+ dlssnr/Ada Lovelace+/nvngx_dlssnr.dll
+ dlssnr/Blackwell+/nvngx_dlssnr.dll
+
+The native worker (``bin/runtime/host/nvngx.dll``) loads the NR runtime
+through the NGX API by filename convention from ``host/`` (and ``dlss/``),
+with no architecture gating of its own — so selecting an architecture is a
+pure file swap of ``host/nvngx_dlssnr.dll`` (which RenoDX requires to sit
+next to itself). No native recompilation is involved.
+
+Policy (per product decision):
+- ``Auto`` maps RTX 20/30 (Turing/Ampere) to ``Turing+``, RTX 40 (Ada) to
+ ``Ada Lovelace+``, RTX 50 (Blackwell) to ``Blackwell+``; unknown GPUs fall
+ back to ``Turing+`` (the historical default build).
+- Applied at startup (before ``prepare_runtime()`` warms the DLL) and again
+ before each render. The worker is a per-job subprocess, so swapping
+ between jobs cannot race a running render. Identical content is skipped.
+- Any failure warns (``logs/dlssnr_architecture.log``) and keeps the
+ current host DLL; renders proceed. These helpers never raise.
+"""
+
+import hashlib
+import time
+import zipfile
+from pathlib import Path
+from typing import Any
+
+from .paths import DLSSNR_DIR, LOGS, NEURAL_RUNTIME, ROOT
+
+DLL_FILENAME = "nvngx_dlssnr.dll"
+LOG_FILENAME = "dlssnr_architecture.log"
+
+DLSS_ARCHITECTURE_CHOICES = (
+ "Auto",
+ "Turing and higher",
+ "Ada Lovelace and higher",
+ "Blackwell and higher",
+)
+DEFAULT_DLSS_ARCHITECTURE = "Auto"
+
+_CHOICE_SUBFOLDERS: dict[str, str | None] = {
+ "Auto": None,
+ "Turing and higher": "Turing+",
+ "Ada Lovelace and higher": "Ada Lovelace+",
+ "Blackwell and higher": "Blackwell+",
+}
+
+_SUBFOLDER_ZIPS: dict[str, str] = {
+ "Turing+": "Turing+.zip",
+ "Ada Lovelace+": "Ada Lovelace+.zip",
+ "Blackwell+": "Blackwell+.zip",
+}
+
+
+def _warn(message: str) -> None:
+ try:
+ LOGS.mkdir(parents=True, exist_ok=True)
+ stamp = time.strftime("%Y-%m-%d %H:%M:%S")
+ with open(LOGS / LOG_FILENAME, "a", encoding="utf-8") as handle:
+ handle.write(f"{stamp} {message}\n")
+ except OSError:
+ pass
+
+
+def _sha256(path: Path) -> str | None:
+ try:
+ digest = hashlib.sha256()
+ with path.open("rb") as stream:
+ for block in iter(lambda: stream.read(1024 * 1024), b""):
+ digest.update(block)
+ return digest.hexdigest()
+ except OSError:
+ return None
+
+
+def subfolder_for_choice(choice: str) -> str | None:
+ """Return the explicit arch subfolder, or None for ``Auto``."""
+ return _CHOICE_SUBFOLDERS.get(choice)
+
+
+def subfolder_for_gpu(gpu: dict[str, Any] | None) -> str:
+ """Map a detected GPU to its ``Auto`` arch subfolder (Turing+ default)."""
+ architecture = ""
+ generation: Any = None
+ if isinstance(gpu, dict):
+ architecture = str(gpu.get("architecture") or "")
+ generation = gpu.get("generation")
+ if architecture == "Blackwell" or generation == 50:
+ return "Blackwell+"
+ if architecture == "Ada" or generation == 40:
+ return "Ada Lovelace+"
+ return "Turing+"
+
+
+def resolve_subfolder(choice: str, gpu: dict[str, Any] | None) -> str:
+ """Resolve a DLSS Architecture setting (possibly ``Auto``) to a folder."""
+ if choice not in DLSS_ARCHITECTURE_CHOICES:
+ _warn(f"Unknown DLSS Architecture {choice!r}; using {DEFAULT_DLSS_ARCHITECTURE!r}.")
+ choice = DEFAULT_DLSS_ARCHITECTURE
+ explicit = subfolder_for_choice(choice)
+ if explicit is not None:
+ return explicit
+ return subfolder_for_gpu(gpu)
+
+
+def arch_dll_path(subfolder: str) -> Path:
+ return DLSSNR_DIR / subfolder / DLL_FILENAME
+
+
+def ensure_dlssnr_dlls() -> dict[str, bool]:
+ """Extract any missing arch DLL from its ``bin/*.zip``. Never raises.
+
+ Returns ``{subfolder: available}``.
+ """
+ availability: dict[str, bool] = {}
+ for subfolder, zip_name in _SUBFOLDER_ZIPS.items():
+ destination = arch_dll_path(subfolder)
+ try:
+ if destination.is_file():
+ availability[subfolder] = True
+ continue
+ zip_path = ROOT / "bin" / zip_name
+ if not zip_path.is_file():
+ _warn(
+ f"DLSS Architecture DLL missing: {destination} "
+ f"(archive {zip_path} not found)."
+ )
+ availability[subfolder] = False
+ continue
+ with zipfile.ZipFile(zip_path) as archive:
+ if archive.namelist() != [DLL_FILENAME]:
+ _warn(
+ f"Archive {zip_path} has unexpected contents; "
+ f"expected only {DLL_FILENAME}."
+ )
+ availability[subfolder] = False
+ continue
+ data = archive.read(DLL_FILENAME)
+ destination.parent.mkdir(parents=True, exist_ok=True)
+ temporary = destination.with_name(f".{DLL_FILENAME}.tmp")
+ temporary.write_bytes(data)
+ temporary.replace(destination)
+ availability[subfolder] = True
+ except Exception as exc: # noqa: BLE001 - warn-and-continue policy
+ _warn(f"Could not provide {destination}: {exc}.")
+ availability[subfolder] = False
+ return availability
+
+
+def apply_dlss_architecture(choice: str, gpu: dict[str, Any] | None) -> str:
+ """Copy the resolved arch DLL over ``host/nvngx_dlssnr.dll``.
+
+ Returns the applied subfolder name, or ``""`` when nothing was applied
+ (missing source or copy failure — the current host DLL is kept and the
+ caller should proceed). Never raises.
+ """
+ try:
+ ensure_dlssnr_dlls()
+ subfolder = resolve_subfolder(choice, gpu)
+ source = arch_dll_path(subfolder)
+ if not source.is_file():
+ _warn(
+ f"DLSS Architecture {choice!r} unavailable "
+ f"({source} missing); keeping current host DLL."
+ )
+ return ""
+ source_hash = _sha256(source)
+ if source_hash is not None and source_hash == _sha256(NEURAL_RUNTIME):
+ return subfolder
+ # prepare_runtime() memory-maps the host DLL for warming and holds
+ # the mapping for the process lifetime; on Windows an open mapping
+ # blocks replacement, so release it before swapping (next
+ # prepare_runtime() call transparently re-warms the new file).
+ try:
+ from .runtime import close_prepared_runtime
+
+ close_prepared_runtime()
+ except Exception: # noqa: BLE001 - best effort
+ pass
+ NEURAL_RUNTIME.parent.mkdir(parents=True, exist_ok=True)
+ temporary = NEURAL_RUNTIME.with_name(f".{DLL_FILENAME}.tmp")
+ temporary.write_bytes(source.read_bytes())
+ temporary.replace(NEURAL_RUNTIME)
+ return subfolder
+ except Exception as exc: # noqa: BLE001 - warn-and-continue policy
+ _warn(f"Could not apply DLSS Architecture {choice!r}: {exc}.")
+ return ""
+
+
+def _current_choice_and_gpu() -> tuple[str, dict[str, Any] | None]:
+ from ..settings.storage import CONFIG_PATH, SETTINGS_STATE, load_settings
+
+ from .gpu_detection import detect_gpus
+ from .gpu_selection import resolve_runtime_ai_gpu
+
+ with SETTINGS_STATE.lock:
+ settings = SETTINGS_STATE.current or load_settings(CONFIG_PATH)
+ choice = getattr(settings, "dlss_architecture", DEFAULT_DLSS_ARCHITECTURE)
+ if choice not in DLSS_ARCHITECTURE_CHOICES:
+ choice = DEFAULT_DLSS_ARCHITECTURE
+ try:
+ gpus = detect_gpus()
+ except Exception as exc: # noqa: BLE001 - no GPU info available
+ _warn(f"GPU detection unavailable for DLSS Architecture: {exc}.")
+ return choice, None
+ try:
+ gpu = resolve_runtime_ai_gpu(
+ gpus, {}, getattr(settings, "ai_gpu_uuid", "auto")
+ )
+ except Exception as exc: # noqa: BLE001 - e.g. no RTX GPU
+ _warn(f"AI GPU resolution unavailable for DLSS Architecture: {exc}.")
+ return choice, None
+ return choice, gpu
+
+
+def init_dlssnr_runtime() -> str:
+ """Ensure arch DLLs and apply the saved setting. Call before ``prepare_runtime()``.
+
+ Returns the applied subfolder name or ``""``. Never raises.
+ """
+ try:
+ choice, gpu = _current_choice_and_gpu()
+ return apply_dlss_architecture(choice, gpu)
+ except Exception as exc: # noqa: BLE001 - startup must never block
+ _warn(f"DLSS Architecture init failed: {exc}.")
+ return ""
+
+
+def apply_current_dlss_architecture(gpu: dict[str, Any] | None) -> str:
+ """Re-apply the live setting before a render. Call with the job's GPU.
+
+ Returns the applied subfolder name or ``""``. Never raises.
+ """
+ try:
+ from ..settings.storage import CONFIG_PATH, SETTINGS_STATE, load_settings
+
+ with SETTINGS_STATE.lock:
+ settings = SETTINGS_STATE.current or load_settings(CONFIG_PATH)
+ choice = getattr(settings, "dlss_architecture", DEFAULT_DLSS_ARCHITECTURE)
+ return apply_dlss_architecture(choice, gpu)
+ except Exception as exc: # noqa: BLE001 - render must never block
+ _warn(f"DLSS Architecture pre-render apply failed: {exc}.")
+ return ""
diff --git a/src/core/ffmpeg/codecs.py b/src/core/ffmpeg/codecs.py
index 31c999e..f0ecced 100644
--- a/src/core/ffmpeg/codecs.py
+++ b/src/core/ffmpeg/codecs.py
@@ -39,12 +39,6 @@ _IS_NVENC_SET = {
"AV1 (NVIDIA NVENC)",
}
-_CPU_ENCODERS = {
- "H.264": "libx264",
- "H.265": "libx265",
- "AV1": "libsvtav1",
-}
-
_NVENC_ENCODERS = {
"H.264 (NVIDIA NVENC)": "h264_nvenc",
"H.265 (NVIDIA NVENC)": "hevc_nvenc",
@@ -91,11 +85,6 @@ def _is_hdr_allowed_codec(codec: str) -> bool:
return _normalize_codec(codec) in HDR_ALLOWED_CODECS
-def _is_hdr_allowed_for_encoding(codec: str) -> bool:
- """Check if HDR Mode (10-bit + colorspace copy) is supported for this codec."""
- return _is_hdr_allowed_codec(codec)
-
-
def hdr_mode_supported(codec: str) -> bool:
"""Public helper for UI: is HDR Mode toggle meaningful for this codec?"""
return _is_hdr_allowed_codec(codec)
diff --git a/src/core/ffmpeg/encoder.py b/src/core/ffmpeg/encoder.py
index 1e4b002..dba456a 100644
--- a/src/core/ffmpeg/encoder.py
+++ b/src/core/ffmpeg/encoder.py
@@ -2,7 +2,6 @@ from __future__ import annotations
import subprocess
import threading
-from functools import lru_cache
from pathlib import Path
from ..jobs import JobController, drain_text
@@ -302,6 +301,7 @@ def start_encoder(
require_nvenc: bool = False,
hdr_mode: bool = False,
hdr_metadata: dict | None = None,
+ preserve_timestamps: bool = False,
):
codec_args, selected, quality = _codec_command(
codec, quality_name, width, height, fps, gpu_ordinal, require_nvenc, hdr_mode, hdr_metadata
@@ -322,6 +322,7 @@ def start_encoder(
*codec_args,
"-fps_mode",
"passthrough",
+ *(["-enc_time_base:v", "demux"] if preserve_timestamps else []),
str(temp_video),
]
process = subprocess.Popen(
diff --git a/src/core/ffmpeg/mux.py b/src/core/ffmpeg/mux.py
index 5c8ead1..235044f 100644
--- a/src/core/ffmpeg/mux.py
+++ b/src/core/ffmpeg/mux.py
@@ -4,11 +4,15 @@ import math
import subprocess
from pathlib import Path
-from ..jobs import JobController
+from ..jobs import Cancelled, JobController
+from ..render_metadata import (
+ VIDEO_NOTE_FORMATS, MetadataNoteError, check_cancelled, embedding_warning,
+ merge_render_note, record_embedding,
+)
from ..paths import FFMPEG, FFPROBE
from .probe import _run_json
-def _probe_rendered_duration(path: Path) -> float:
+def _probe_rendered_duration(path: Path, controller: JobController | None = None) -> float:
"""Read the intermediate video's duration without decoding/counting its frames."""
data = _run_json(
[
@@ -22,7 +26,7 @@ def _probe_rendered_duration(path: Path) -> float:
"-of",
"json",
str(path),
- ]
+ ], controller=controller,
)
streams = data.get("streams") or []
values = [
@@ -48,8 +52,72 @@ def final_mux(
container: str,
controller: JobController | None = None,
preserve_supported_subtitles: bool = False,
+ *, render_note: str | None = None, metadata_diagnostics: dict | None = None,
+) -> None:
+ check_cancelled(controller)
+ if render_note is None or container not in VIDEO_NOTE_FORMATS:
+ if render_note is not None:
+ record_embedding(metadata_diagnostics, "skipped", reason="unsupported_format")
+ elif metadata_diagnostics is not None and not metadata_diagnostics:
+ record_embedding(metadata_diagnostics, "not_requested")
+ _final_mux_once(temp_video, source, output, container, controller, preserve_supported_subtitles)
+ return
+
+ try:
+ comment = merge_render_note(_read_comment(source, controller), render_note)
+ # Windows has a finite command-line length. Never truncate source text.
+ if len(comment.encode("utf-16-le")) > 16000:
+ raise MetadataNoteError("Existing comment is too large to safely extend")
+ except Cancelled:
+ raise
+ except (ValueError, TypeError, RuntimeError) as exc:
+ check_cancelled(controller)
+ embedding_warning(metadata_diagnostics, exc)
+ _final_mux_once(temp_video, source, output, container, controller, preserve_supported_subtitles)
+ return
+
+ try:
+ _final_mux_once(temp_video, source, output, container, controller,
+ preserve_supported_subtitles, comment=comment)
+ if _read_comment(output, controller) != comment:
+ raise MetadataNoteError("Saved video did not retain the settings note")
+ except Cancelled:
+ raise
+ except (ValueError, RuntimeError) as exc:
+ check_cancelled(controller)
+ if isinstance(exc, _MuxFailure) and exc.filesystem_error:
+ raise
+ embedding_warning(metadata_diagnostics, exc)
+ _final_mux_once(temp_video, source, output, container, controller, preserve_supported_subtitles)
+ return
+ record_embedding(metadata_diagnostics, "embedded", field="format.comment")
+
+
+def _read_comment(path: Path, controller: JobController | None) -> str | None:
+ data = _run_json([
+ str(FFPROBE), "-v", "error", "-show_entries", "format_tags=comment",
+ "-of", "json", str(path),
+ ], controller=controller)
+ return next((value for key, value in (data.get("format", {}).get("tags") or {}).items()
+ if key.casefold() == "comment"), None)
+
+
+class _MuxFailure(RuntimeError):
+ def __init__(self, stderr: str):
+ super().__init__("Final audio/metadata mux failed:\n" + stderr[-4000:])
+ self.filesystem_error = any(message in stderr.casefold() for message in (
+ "permission denied", "access is denied", "no space left", "disk full",
+ "input/output error", "i/o error", "read-only file system", "error opening output",
+ ))
+
+
+def _final_mux_once(
+ temp_video: Path, source: Path, output: Path, container: str,
+ controller: JobController | None, preserve_supported_subtitles: bool,
+ *, comment: str | None = None,
) -> None:
- duration = _probe_rendered_duration(temp_video)
+ check_cancelled(controller)
+ duration = _probe_rendered_duration(temp_video, controller)
if container == "MKV":
maps = ["-map", "0:v:0", "-map", "1:a?", "-map", "1:s?"]
streams = ["-c:v", "copy", "-c:a", "copy", "-c:s", "copy"]
@@ -78,7 +146,7 @@ def final_mux(
"-of",
"json",
str(source),
- ]
+ ], controller=controller,
)
supported_text = {"ass", "ssa", "subrip", "mov_text", "webvtt", "text"}
subtitle_indices = [
@@ -108,6 +176,7 @@ def final_mux(
"-map_chapters",
"1",
*streams,
+ *(["-metadata", "comment=" + comment] if comment is not None else []),
str(output),
]
process = subprocess.Popen(
@@ -117,13 +186,32 @@ def final_mux(
text=True,
encoding="utf-8",
errors="replace",
+ creationflags=getattr(subprocess, "CREATE_NO_WINDOW", 0),
)
if controller is not None:
controller.register(process)
try:
- _stdout, stderr = process.communicate()
+ while True:
+ check_cancelled(controller)
+ try:
+ _stdout, stderr = process.communicate(timeout=0.2)
+ break
+ except subprocess.TimeoutExpired:
+ continue
+ check_cancelled(controller)
finally:
+ if process.poll() is None:
+ process.terminate()
+ try:
+ process.wait(timeout=5)
+ except subprocess.TimeoutExpired:
+ process.kill()
+ process.wait(timeout=5)
+ if process.stdout is not None:
+ process.stdout.close()
+ if process.stderr is not None:
+ process.stderr.close()
if controller is not None:
controller.unregister(process)
if process.returncode:
- raise RuntimeError("Final audio/metadata mux failed:\n" + stderr[-4000:])
+ raise _MuxFailure(stderr)
diff --git a/src/core/ffmpeg/preview.py b/src/core/ffmpeg/preview.py
index 0c76c77..f1443d3 100644
--- a/src/core/ffmpeg/preview.py
+++ b/src/core/ffmpeg/preview.py
@@ -90,6 +90,10 @@ def make_browser_preview(
Returns the new preview path as a string. Raises RuntimeError on failure.
"""
from ..paths import OUTPUTS
+ from ..jobs import current_job_controller
+ from ..disk_paths import OutputFile
+
+ controller = controller or current_job_controller()
src = Path(source)
if not src.is_file():
@@ -101,6 +105,7 @@ def make_browser_preview(
while dest.exists():
counter += 1
dest = out_dir / f"{src.stem}_BROWSERPREVIEW_{counter}.mp4"
+ output_file = OutputFile(dest)
command = [
str(FFMPEG),
"-hide_banner",
@@ -127,38 +132,33 @@ def make_browser_preview(
"192k",
"-movflags",
"+faststart",
- str(dest),
+ str(output_file.temporary),
]
- process = subprocess.Popen(
- command,
- stdout=subprocess.PIPE,
- stderr=subprocess.PIPE,
- text=True,
- encoding="utf-8",
- errors="replace",
- )
- if controller is not None:
- try:
- controller.register(process)
- except Exception:
- pass
+ process = None
try:
+ process = subprocess.Popen(
+ command, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
+ text=True, encoding="utf-8", errors="replace",
+ )
+ if controller is not None:
+ controller.register(process)
_stdout, stderr = process.communicate()
+ if process.returncode:
+ raise RuntimeError("Browser preview transcode failed:\n" + (stderr or "")[-4000:])
+ if not is_browser_playable(output_file.temporary):
+ raise RuntimeError("Browser preview transcode produced an unplayable file.")
+ if controller is not None and controller.cancel.is_set():
+ from ..jobs import Cancelled
+ raise Cancelled("Preview cancelled.")
+ output_file.publish()
finally:
- if controller is not None:
- try:
+ if process is not None:
+ if process.poll() is None:
+ process.terminate()
+ process.communicate()
+ if controller is not None:
controller.unregister(process)
- except Exception:
- pass
- if process.returncode:
- raise RuntimeError(
- "Browser preview transcode failed:\n" + (stderr or "")[-4000:]
- )
- if not dest.is_file():
- raise RuntimeError("Browser preview transcode produced no output file.")
- # Verify the derivative is actually playable before handing it to Gradio.
- if not is_browser_playable(dest):
- raise RuntimeError("Browser preview transcode produced an unplayable file.")
+ output_file.cleanup()
return str(dest)
diff --git a/src/core/ffmpeg/probe.py b/src/core/ffmpeg/probe.py
index 7e667dc..6c36f52 100644
--- a/src/core/ffmpeg/probe.py
+++ b/src/core/ffmpeg/probe.py
@@ -3,36 +3,85 @@ from __future__ import annotations
import json
import os
import subprocess
+import tempfile
from fractions import Fraction
from pathlib import Path
import av
from ..paths import FFPROBE
+from ..jobs import Cancelled, JobController
-def _run_json(command: list[str]) -> dict:
- result = subprocess.run(
- command,
- capture_output=True,
- text=True,
- encoding="utf-8",
- errors="replace",
- )
- if result.returncode:
- raise RuntimeError(result.stderr.strip() or "Media probe failed")
- return json.loads(result.stdout)
+def _run_json(
+ command: list[str], *, strict_decode: bool = False,
+ controller: JobController | None = None,
+) -> dict:
+ if controller is not None and controller.cancel.is_set():
+ raise Cancelled("Render stopped by user.")
+ # ffprobe can report decoding errors and still exit with code zero. Keep its
+ # error output outside RAM, and retain a bounded excerpt for diagnostics.
+ with tempfile.TemporaryFile() as errors:
+ process = subprocess.Popen(
+ command, stdout=subprocess.PIPE, stderr=errors,
+ creationflags=getattr(subprocess, "CREATE_NO_WINDOW", 0),
+ )
+ if controller is not None:
+ controller.register(process)
+ try:
+ while True:
+ if controller is not None and controller.cancel.is_set():
+ raise Cancelled("Render stopped by user.")
+ try:
+ stdout, _ = process.communicate(timeout=0.2)
+ break
+ except subprocess.TimeoutExpired:
+ continue
+ if controller is not None and controller.cancel.is_set():
+ raise Cancelled("Render stopped by user.")
+ size = errors.seek(0, os.SEEK_END)
+ errors.seek(max(0, size - 8000))
+ details = errors.read().decode("utf-8", "replace").strip()
+ if strict_decode and size:
+ raise RuntimeError("Source decoding failed during frame-count verification:\n" + details)
+ if process.returncode:
+ raise RuntimeError(details or "Media probe failed")
+ return json.loads(stdout)
+ finally:
+ if process.poll() is None:
+ process.terminate()
+ try:
+ process.wait(timeout=5)
+ except subprocess.TimeoutExpired:
+ process.kill()
+ process.wait(timeout=5)
+ if process.stdout is not None:
+ process.stdout.close()
+ if controller is not None:
+ controller.unregister(process)
+
+
+def _positive_count(value: object) -> int:
+ try:
+ return max(0, int(value))
+ except (ValueError, TypeError, OverflowError):
+ return 0
def probe_video(
- path: str | os.PathLike[str], *, count_mode: str = "exact"
+ path: str | os.PathLike[str], *, count_mode: str = "exact",
+ strict_decode: bool = False, controller: JobController | None = None,
) -> dict:
"""Probe video metadata, optionally counting decoded frames or packets.
The default remains the legacy exact decoded-frame count for external callers.
Conversion paths use metadata first and pay for exact counting only as fallback.
+ Strict decoding rejects decoder errors and unavailable decoded counts, even
+ when ffprobe exits successfully or the container declares a positive count.
"""
if count_mode not in {"metadata", "exact", "packets"}:
raise ValueError(f"Unknown video count mode: {count_mode!r}.")
+ if strict_decode and count_mode != "exact":
+ raise ValueError("Strict decoding requires count_mode='exact'.")
count_args = {
"metadata": [],
"exact": ["-count_frames"],
@@ -53,7 +102,7 @@ def probe_video(
"-of",
"json",
str(path),
- ]
+ ], strict_decode=strict_decode, controller=controller,
)
streams = data.get("streams") or []
if not streams:
@@ -67,14 +116,17 @@ def probe_video(
width, height = int(stream["width"]), int(stream["height"])
if rotation in (90, 270):
width, height = height, width
+ declared_frames = _positive_count(stream.get("nb_frames"))
+ decoded_frames = _positive_count(stream.get("nb_read_frames"))
+ packet_frames = _positive_count(stream.get("nb_read_packets"))
if count_mode == "packets":
- frames = int(stream.get("nb_read_packets") or stream.get("nb_frames") or 0)
- frame_count_source = "packets" if stream.get("nb_read_packets") else "metadata"
+ frames = packet_frames or declared_frames
+ frame_count_source = "packets" if packet_frames else "metadata"
elif count_mode == "exact":
- frames = int(stream.get("nb_read_frames") or stream.get("nb_frames") or 0)
- frame_count_source = "decoded" if stream.get("nb_read_frames") else "metadata"
+ frames = decoded_frames if strict_decode else (decoded_frames or declared_frames)
+ frame_count_source = "decoded" if decoded_frames else "metadata"
else:
- frames = int(stream.get("nb_frames") or 0)
+ frames = declared_frames
frame_count_source = "metadata" if frames > 0 else "unavailable"
if count_mode == "exact" and frames <= 0:
raise ValueError("Could not determine an exact frame count for this video.")
diff --git a/src/core/jobs.py b/src/core/jobs.py
index c71faf5..b84b073 100644
--- a/src/core/jobs.py
+++ b/src/core/jobs.py
@@ -4,6 +4,7 @@ import subprocess
import threading
from collections import deque
from contextlib import contextmanager
+from contextvars import ContextVar
from typing import Iterator
@@ -52,15 +53,29 @@ class JobController:
_RENDER_LOCK = threading.Lock()
_ACTIVE_LOCK = threading.Lock()
_ACTIVE: JobController | None = None
+_JOB_CONTEXT = ContextVar("render_job_controller", default=None)
+
+
+def current_job_controller():
+ return _JOB_CONTEXT.get()
+
+
+@contextmanager
+def use_job_controller(controller: JobController):
+ token = _JOB_CONTEXT.set(controller)
+ try:
+ yield
+ finally:
+ _JOB_CONTEXT.reset(token)
@contextmanager
-def active_job() -> Iterator[JobController]:
+def active_job(controller: JobController | None = None) -> Iterator[JobController]:
"""Claim the single GPU render slot and always release its resources."""
global _ACTIVE
if not _RENDER_LOCK.acquire(blocking=False):
raise RuntimeError("Another GPU render is already running.")
- controller = JobController()
+ controller = controller or current_job_controller() or JobController()
with _ACTIVE_LOCK:
_ACTIVE = controller
try:
diff --git a/src/core/paths.py b/src/core/paths.py
index cb14a8b..646ba71 100644
--- a/src/core/paths.py
+++ b/src/core/paths.py
@@ -8,6 +8,9 @@ RUNTIME = ROOT / "bin" / "runtime"
HOST_DIR = RUNTIME / "host"
DLSS_DIR = RUNTIME / "dlss"
DLSSG_DIR = RUNTIME / "dlssg"
+# Per-architecture Neural Rendering builds (see src/core/dlss_architecture.py).
+# The live copy stays at NEURAL_RUNTIME in host/ (RenoDX discovery rule).
+DLSSNR_DIR = RUNTIME / "dlssnr"
FFMPEG = ROOT / "bin" / "ffmpeg" / "bin" / "ffmpeg.exe"
FFPROBE = ROOT / "bin" / "ffmpeg" / "bin" / "ffprobe.exe"
WORKER = HOST_DIR / "nvngx.dll" # Signed-snippet caller checks require this image name.
@@ -18,6 +21,11 @@ DLSS_SUPERRES = DLSS_DIR / "nvngx_dlss.dll"
# The RenoDX add-on only discovers the signed NR runtime next to itself in
# host/, so NEURAL_RUNTIME lives there even though it is a DLSS component.
NEURAL_RUNTIME = HOST_DIR / "nvngx_dlssnr.dll"
+# Live-tab externals (vendored, optional: only Live sessions require them).
+MPV = ROOT / "bin" / "mpv" / "mpv.exe"
+YTDLP = ROOT / "bin" / "yt-dlp" / "yt-dlp.exe"
+# Ephemeral per-session HLS working dirs for Live (swept on stop/startup).
+LIVE_DIR = ROOT / "live"
OUTPUTS = ROOT / "outputs"
LOGS = ROOT / "logs"
JOBS = ROOT / "jobs"
diff --git a/src/core/render_metadata.py b/src/core/render_metadata.py
new file mode 100644
index 0000000..9258f0b
--- /dev/null
+++ b/src/core/render_metadata.py
@@ -0,0 +1,98 @@
+"""Small, human-readable render notes; never embed diagnostic or machine data."""
+from __future__ import annotations
+
+import re
+
+from .jobs import Cancelled
+
+APPLICATION = "DLSS 5 Visual Enhancer"
+PREFIX = "DLSS 5 Neural Rendering Settings - "
+IMAGE_NOTE_FORMATS = frozenset({"PNG", "JPEG", "WebP", "AVIF", "TIFF"})
+VIDEO_NOTE_FORMATS = frozenset({"MP4", "MKV"})
+_LABELS = (
+ "NR Preset", "NR Style", "NR Intensity", "Local Tone Strength",
+ "Local Structure Strength", "Skin Structure Strength", "Automatic Mask",
+ "DLSS Model Preset", "Upscaling Factor",
+)
+_NUMBER = r"-?\d+(?:\.\d+)?(?:e[+-]?\d+)?"
+_VALUES = (
+ r"(?:Default|Preset #[123])", r"(?:Default|Natural|Cinematic)",
+ _NUMBER, _NUMBER, _NUMBER, _NUMBER + r"(?: \(Default\))?",
+ r"(?:On|Off)", r"(?:Default|J|K|L|M)", _NUMBER + "x",
+)
+_OWN_NOTE = re.compile(
+ r"(?m)^" + re.escape(APPLICATION + ":") + r"\r?\n" + re.escape(PREFIX)
+ + ", ".join(re.escape(label + " - ") + value for label, value in zip(_LABELS, _VALUES))
+ + r"(?=\r?$)"
+)
+
+
+class MetadataNoteError(ValueError):
+ """The optional note could not be safely embedded or read back."""
+
+
+def check_cancelled(controller) -> None:
+ if controller is not None and controller.cancel.is_set():
+ raise Cancelled("Render stopped by user.")
+
+
+def record_embedding(diagnostics: dict | None, status: str, *, field=None, reason=None) -> None:
+ if diagnostics is not None:
+ diagnostics.update(status=status)
+ if field is not None:
+ diagnostics["field"] = field
+ if reason is not None:
+ diagnostics["reason"] = str(reason)
+
+
+def embedding_warning(diagnostics: dict | None, exc: Exception) -> str:
+ message = f"DLSS settings metadata was skipped; saved without the new note ({str(exc)[:500]})."
+ record_embedding(diagnostics, "skipped", reason=str(exc)[:500])
+ if diagnostics is not None:
+ diagnostics["warning"] = message
+ return message
+
+
+def build_render_note(options, applied_model_preset: int) -> str:
+ # Use the same validation and labels as the native protocol, with its
+ # confirmed model preset rather than an unrelated current UI selection.
+ from .runtime import DLSS_MODEL_PRESETS, resolve_native_settings, resolve_upscaling_mode
+
+ native = resolve_native_settings(options)
+ factor, _ = resolve_upscaling_mode(options.upscaling_factor)
+ model = next(name for name, code in DLSS_MODEL_PRESETS.items() if code == applied_model_preset)
+ number = lambda value: str(float(value)).removesuffix(".0")
+ skin = number(native["skin_structure"])
+ if native["skin_structure"] == -1:
+ skin += " (Default)"
+ values = (
+ options.nr_preset, options.nr_style, number(native["intensity"]),
+ number(native["local_tone"]), number(native["local_structure"]), skin,
+ "On" if native["auto_mask"] else "Off", model, number(factor) + "x",
+ )
+ return APPLICATION + ":\n" + PREFIX + ", ".join(
+ label + " - " + value for label, value in zip(_LABELS, values)
+ )
+
+
+def prepare_render_note(options, applied_model_preset: int, diagnostics: dict) -> str | None:
+ try:
+ return build_render_note(options, applied_model_preset)
+ except (ValueError, TypeError, KeyError, AttributeError, StopIteration) as exc:
+ embedding_warning(diagnostics, exc)
+ return None
+
+
+def merge_render_note(existing: str | None, note: str) -> str:
+ if not isinstance(note, str) or not _OWN_NOTE.fullmatch(note):
+ raise MetadataNoteError("Unrecognized render note")
+ if existing is None or existing == "":
+ return note
+ if not isinstance(existing, str) or "\x00" in existing:
+ raise MetadataNoteError("The existing description cannot be safely extended")
+ matches = list(_OWN_NOTE.finditer(existing))
+ if matches:
+ # Replace only a complete, recognized note. User descriptions, including
+ # similarly named prose, remain untouched.
+ return _OWN_NOTE.sub(lambda _match: note, existing)
+ return existing + "\n\n" + note
diff --git a/src/core/runtime.py b/src/core/runtime.py
index 8f71a12..9921d53 100644
--- a/src/core/runtime.py
+++ b/src/core/runtime.py
@@ -9,6 +9,7 @@ import struct
import subprocess
import threading
import time
+from contextlib import suppress
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any
@@ -172,6 +173,7 @@ VIDEO_MAGIC = 0x34563544
SETUP_MAGIC = 0x34505553
FRAME_MAGIC = 0x314D5246
OUT_MAGIC = 0x3154554F
+END_MAGIC = 0x31444E45 # "END1": counted completion for an unknown-length stream.
VIDEO_HEADER_FORMAT = "<14I4f"
SETUP_RESPONSE_FORMAT = "<12I"
@@ -269,6 +271,7 @@ def write_failure_report(
worker_logs: list[str] | None = None,
reshade_lines: list[str] | None = None,
logs_dir: Path | None = None,
+ diagnostics: dict[str, Any] | None = None,
) -> Path:
"""Persist diagnostics even when the incomplete media output is removed."""
destination = logs_dir or LOGS
@@ -287,6 +290,8 @@ def write_failure_report(
"worker_log": list(worker_logs or []),
"reshade_feature_18_log": list(reshade_lines or []),
}
+ if diagnostics is not None:
+ report["diagnostics"] = diagnostics
report_path.write_text(json.dumps(report, indent=2), encoding="utf-8")
return report_path.resolve()
@@ -391,7 +396,7 @@ class DLSSFrameSession:
input_height: int,
output_width: int,
output_height: int,
- frame_count: int,
+ frame_count: int | None,
warmup_frames: int,
factor: float,
mode: dict[str, str | int],
@@ -400,7 +405,15 @@ class DLSSFrameSession:
runtime_bundle: dict[str, Any],
controller: JobController,
) -> None:
+ if frame_count is not None and (
+ isinstance(frame_count, bool) or not isinstance(frame_count, int)
+ or not 0 < frame_count <= 0xFFFFFFFF
+ ):
+ raise ValueError("Native frame count must be a positive uint32 or None.")
self.controller = controller
+ self._streaming = frame_count is None
+ self._processed_frames = 0
+ self.completed_frames: int | None = None
self._worker_log_buffer = BoundedLogBuffer()
self.closed = False
self.factor = factor
@@ -444,7 +457,7 @@ class DLSSFrameSession:
output_width,
output_height,
int(warmup_frames),
- frame_count,
+ 0 if frame_count is None else frame_count,
int(mode["perf_quality"]),
int(native["dlss_model_preset"]),
native["profile"],
@@ -473,8 +486,10 @@ class DLSSFrameSession:
or "The worker produced no diagnostic output."
)
raise RuntimeError(
- "The native worker is incompatible with the version-4 model-preset protocol "
+ "The native worker is incompatible with the requested video protocol "
f"or failed during DLSS setup (exit {worker_code}):\n{details}"
+ + ("\nInstall the updated native worker with streaming completion support."
+ if self._streaming else "")
) from exc
(
setup_magic,
@@ -581,6 +596,12 @@ class DLSSFrameSession:
) -> tuple[np.ndarray, int]:
if self.controller.cancel.is_set():
raise Cancelled("Render stopped by user.")
+ if self.closed:
+ raise RuntimeError("The native DLSS session is closed.")
+ if self._streaming and (
+ index != self._processed_frames or self._processed_frames >= 0xFFFFFFFF
+ ):
+ raise ValueError("Streaming native frames must have consecutive uint32 indices.")
assert self.worker.stdin is not None and self.worker.stdout is not None
frame_header = struct.pack("<4Iq", FRAME_MAGIC, index, int(reset), 0, pts)
self.worker.stdin.write(frame_header)
@@ -605,7 +626,8 @@ class DLSSFrameSession:
"<5Iq", result_header
)
expected = self.output_width * self.output_height * 4
- if magic != OUT_MAGIC or not ok or out_index != index or byte_count != expected:
+ if (magic != OUT_MAGIC or not ok or out_index != index
+ or byte_count != expected or out_pts != pts):
raise RuntimeError(f"Invalid native worker response for frame {index}")
if ngx_result != 1:
raise RuntimeError(
@@ -613,21 +635,52 @@ class DLSSFrameSession:
)
output = np.empty((self.output_height, self.output_width, 4), dtype=np.uint8)
_read_exact_into(self.worker.stdout, output)
+ self._processed_frames += 1
return output, out_pts
def close(self) -> None:
if self.closed:
return
- if self.worker.stdin and not self.worker.stdin.closed:
- self.worker.stdin.close()
- worker_code = self.worker.wait(timeout=60)
- self.worker_thread.join(timeout=2)
- self.controller.unregister(self.worker)
- self.closed = True
- if worker_code:
- raise RuntimeError(
- "Native DLSS worker failed:\n" + "\n".join(self.worker_logs[-40:])
- )
+ try:
+ if self.controller.cancel.is_set():
+ raise Cancelled("Render stopped by user.")
+ if self._streaming:
+ if not self._processed_frames:
+ raise RuntimeError("The input video contains no decodable frames.")
+ assert self.worker.stdin is not None
+ self.worker.stdin.write(struct.pack(
+ "<4Iq", END_MAGIC, self._processed_frames, 0, 0, 0,
+ ))
+ self.worker.stdin.flush()
+ if self.worker.stdin and not self.worker.stdin.closed:
+ self.worker.stdin.close()
+ # The completion response is only 28 bytes and fits in the pipe.
+ # Wait first so a missing acknowledgement cannot block past this timeout.
+ worker_code = self.worker.wait(timeout=60)
+ self.worker_thread.join(timeout=2)
+ if self.controller.cancel.is_set():
+ raise Cancelled("Render stopped by user.")
+ if worker_code:
+ raise RuntimeError(
+ "Native DLSS worker failed:\n" + "\n".join(self.worker_logs[-40:])
+ )
+ if self._streaming:
+ assert self.worker.stdout is not None
+ acknowledgement = struct.unpack(
+ "<5Iq", _read_exact(self.worker.stdout, struct.calcsize("<5Iq"))
+ )
+ if acknowledgement != (END_MAGIC, self._processed_frames, 1, 0, 1, 0):
+ raise RuntimeError("Native DLSS worker returned an invalid completion count.")
+ self.completed_frames = self._processed_frames
+ self.controller.unregister(self.worker)
+ self.closed = True
+ for stream in (self.worker.stdout, self.worker.stderr):
+ if stream is not None and not stream.closed:
+ stream.close()
+ except BaseException:
+ with suppress(Exception):
+ self.abort()
+ raise
def abort(self) -> None:
if self.closed:
@@ -641,10 +694,18 @@ class DLSSFrameSession:
self.worker.kill()
except OSError:
pass
+ self.worker.wait(timeout=5)
self.worker_thread.join(timeout=2)
self.controller.unregister(self.worker)
self.closed = True
+ for stream in (self.worker.stdin, self.worker.stdout, self.worker.stderr):
+ if stream is not None and not stream.closed:
+ try:
+ stream.close()
+ except OSError:
+ pass
+
def verify_feature_18(
worker_logs: list[str], reshade_log: str | None = None
diff --git a/src/core/terminal.py b/src/core/terminal.py
index 0d9b012..e56faba 100644
--- a/src/core/terminal.py
+++ b/src/core/terminal.py
@@ -26,57 +26,6 @@ C_LEFT = (2, 134, 195)
C_MID = (15, 184, 129)
C_RIGHT = (7, 158, 110)
-CHEVRON = [
- " ███ ",
- "░░░███ ",
- " ░░░███ ",
- " ░░░███",
- " ███░ ",
- " ███░ ",
- " ███░ ",
- "░░░ ",
-]
-CHAR_D = [
- "█████████ ",
- "░░███░░░░██",
- " ░███ ░░██",
- " ░███ ░██",
- " ░███ ░░██",
- " ░███░░░░██",
- " █████████ ",
- " ░░░░░░░░░",
-]
-CHAR_L = [
- "█████ ",
- "░░███ ",
- " ░███ ",
- " ░███ ",
- " ░███ █",
- " ░███ ██",
- " ████████",
- " ░░░░░░░",
-]
-CHAR_S = [
- " █████████ ",
- "███░░░░░███",
- "░███░░░ ░░░",
- " ░░███████ ",
- " ░░░░░███",
- "███░░ ███",
- "░░█████████",
- " ░░░░░░░░░",
-]
-CHAR_5 = [
- "██████████",
- "░░███░░░░░",
- " ░████████",
- " ░░░░░░░███",
- " ░███",
- "███░░ ███",
- "░░█████████",
- " ░░░░░░░░░",
-]
-
def get_gradient_rgb(t: float) -> tuple[int, int, int]:
"""Interpolate RGB color according to the 90deg linear gradient."""
@@ -230,9 +179,7 @@ class TerminalUI:
dlss_part = "D L S S 5"
visual_part = "V I S U A L E N H A N C E R"
title_text = dlss_part + " " + visual_part
- # Fullwidth chars are 2 cols wide, so use display width for centering (approx)
- title_w = len(title_text) + dlss_part.count("D") + dlss_part.count("L") + dlss_part.count("S") + dlss_part.count("5")
- # Simpler: use len as approximation, will still center visually
+ # Use len as an approximation for centering.
title_w = len(title_text)
pad_title = " " * max(0, (term_cols - title_w) // 2)
diff --git a/src/frame_interpolation/batch.py b/src/frame_interpolation/batch.py
index d66e95b..37b8001 100644
--- a/src/frame_interpolation/batch.py
+++ b/src/frame_interpolation/batch.py
@@ -3,74 +3,78 @@ from __future__ import annotations
import json
import os
import time
-from dataclasses import asdict
+from dataclasses import asdict, replace
from pathlib import Path
from typing import Callable, Iterable
-from ..core.jobs import Cancelled, active_job
+from ..core.batch_progress import BatchItemUpdate, BatchProgress
+from ..core.disk_paths import prepare_output_dir
+from ..core.jobs import Cancelled, JobController, active_job
from ..core.paths import LOGS
-from .models import (
- FrameInterpolationBatchResult, FrameInterpolationFailure, FrameInterpolationOptions,
- FrameInterpolationSuccess,
-)
-from .processor import _BATCH_CONTEXT, _json_safe, interpolate_video
+from .models import FrameInterpolationBatchResult, FrameInterpolationFailure, FrameInterpolationOptions, FrameInterpolationSuccess
+from . import processor
+
def interpolate_videos(
input_paths: Iterable[str | os.PathLike[str]],
options: FrameInterpolationOptions | None = None,
progress: Callable[[float, str], None] | None = None,
+ *, output_dir: str | os.PathLike[str] | None = None,
+ controller: JobController | None = None,
+ on_item_update: Callable[[BatchItemUpdate], None] | None = None,
) -> FrameInterpolationBatchResult:
- options = options or FrameInterpolationOptions()
+ options = replace(options) if options else FrameInterpolationOptions()
paths = [Path(path).resolve() for path in input_paths]
if not paths:
raise ValueError("Choose at least one video.")
+ controller = controller or JobController()
+ reporter = BatchProgress(paths, on_item_update, progress)
stamp = time.strftime("%Y%m%d-%H%M%S") + f"-{time.time_ns() % 1_000_000:06d}"
- successes: list[FrameInterpolationSuccess] = []
- failures: list[FrameInterpolationFailure] = []
- cancelled = False
- with active_job() as controller:
- _BATCH_CONTEXT.controller = controller
- try:
- for index, path in enumerate(paths):
- if controller.cancel.is_set():
- cancelled = True
- failures.extend(
- FrameInterpolationFailure(i, str(item), "Cancelled before rendering.", True)
- for i, item in enumerate(paths[index:], start=index)
- )
- break
- def item_progress(value: float, message: str, *, position=index) -> None:
- if progress:
- progress((position + value) / len(paths), f"[{position + 1}/{len(paths)}] {message}")
- try:
- result = interpolate_video(path, options, item_progress)
- successes.append(FrameInterpolationSuccess(index, str(path), result))
- except Cancelled as exc:
- cancelled = True
- failures.append(FrameInterpolationFailure(index, str(path), str(exc), True))
- failures.extend(
- FrameInterpolationFailure(
- queued_index,
- str(queued),
- "Cancelled before rendering.",
- True,
- )
- for queued_index, queued in enumerate(
- paths[index + 1 :], start=index + 1
+ successes, failures = [], []
+ try:
+ destination = prepare_output_dir(output_dir, default=processor.OUTPUTS)
+ with active_job(controller):
+ processor._BATCH_CONTEXT.controller = controller
+ try:
+ for index, path in enumerate(paths):
+ if controller.cancel.is_set():
+ break
+ reporter.advance(index)
+ try:
+ result = processor.interpolate_video(
+ path, options, lambda value, message, i=index: reporter.advance(i, value, message),
+ output_dir=destination,
)
- )
- break
- except Exception as exc:
- failures.append(FrameInterpolationFailure(index, str(path), str(exc)))
- finally:
- del _BATCH_CONTEXT.controller
- manifest = {
- "status": "cancelled" if cancelled else ("partial" if failures else "success"),
- "options": _json_safe(asdict(options)),
- "successes": [asdict(item) for item in successes],
- "failures": [asdict(item) for item in failures],
- }
- LOGS.mkdir(exist_ok=True)
- manifest_path = LOGS / f"DLSSFG_BATCH_{stamp}.manifest.json"
- manifest_path.write_text(json.dumps(manifest, indent=2), encoding="utf-8")
- return FrameInterpolationBatchResult(successes, failures, cancelled, str(manifest_path.resolve()))
+ except Exception as exc:
+ cancelled = isinstance(exc, Cancelled) or controller.cancel.is_set()
+ failures.append(FrameInterpolationFailure(index, str(path), str(exc), cancelled))
+ reporter.fail(index, exc, cancelled=cancelled)
+ if cancelled:
+ controller.stop()
+ break
+ else:
+ successes.append(FrameInterpolationSuccess(index, str(path), result))
+ reporter.complete(index, result.output_path, f"{result.output_frames} frames; report: {result.report_path}")
+ finally:
+ del processor._BATCH_CONTEXT.controller
+ cancelled = controller.cancel.is_set()
+ if cancelled:
+ for item in reporter.items:
+ if item.state == "Queued":
+ failures.append(FrameInterpolationFailure(item.index, item.input_path, "Cancelled before rendering.", True))
+ reporter.skip_from(0)
+ manifest = {
+ "status": "cancelled" if cancelled else ("partial" if failures else "success"),
+ "options": processor._json_safe(asdict(options)),
+ "successes": [asdict(item) for item in successes],
+ "failures": [asdict(item) for item in failures],
+ "output_directory": str(destination), "batch": reporter.diagnostics(final=True),
+ }
+ LOGS.mkdir(exist_ok=True)
+ manifest_path = LOGS / f"DLSSFG_BATCH_{stamp}.manifest.json"
+ manifest_path.write_text(json.dumps(manifest, indent=2), encoding="utf-8")
+ reporter.finish(cancelled=cancelled, manifest_path=str(manifest_path))
+ return FrameInterpolationBatchResult(successes, failures, cancelled, str(manifest_path.resolve()))
+ except BaseException as exc:
+ reporter.finish(cancelled=controller.cancel.is_set(), error=str(exc))
+ raise
diff --git a/src/frame_interpolation/guides.py b/src/frame_interpolation/guides.py
index 09f6ac1..b66135d 100644
--- a/src/frame_interpolation/guides.py
+++ b/src/frame_interpolation/guides.py
@@ -53,9 +53,13 @@ class DLSSGGuideGenerator:
)
calculated[..., 0] *= self.width / self.flow_width
calculated[..., 1] *= self.height / self.flow_height
- finite = np.isfinite(calculated).all(axis=2)
+ # Reducing an H×W×2 boolean array along its tiny last axis is
+ # expensive. These two channel checks produce the same mask.
+ finite = np.isfinite(calculated[..., 0])
+ np.logical_and(finite, np.isfinite(calculated[..., 1]), out=finite)
confidence = float(np.mean(finite))
- calculated[~finite] = 0
+ if confidence < 1.0:
+ calculated[~finite] = 0
reset = confidence < 0.98
vectors = self.zero if reset else np.ascontiguousarray(calculated.astype(np.float16))
guide = Guide(vectors, reset, score, duplicate, confidence)
diff --git a/src/frame_interpolation/models.py b/src/frame_interpolation/models.py
index ef4e2fd..467bf56 100644
--- a/src/frame_interpolation/models.py
+++ b/src/frame_interpolation/models.py
@@ -13,7 +13,14 @@ FPS_RATES: dict[str, Fraction] = {
"59.94": Fraction(60000, 1001),
"60": Fraction(60, 1),
"90": Fraction(90, 1),
+ "119.88": Fraction(120000, 1001),
"120": Fraction(120, 1),
+ "144": Fraction(144, 1),
+ "165": Fraction(165, 1),
+ "180": Fraction(180, 1),
+ "240": Fraction(240, 1),
+ "360": Fraction(360, 1),
+ "480": Fraction(480, 1),
}
FPS_CHOICES = tuple(FPS_RATES)
ENGINE_CHOICES = ("Auto", "Native DLSSG", "Cascade")
diff --git a/src/frame_interpolation/native.py b/src/frame_interpolation/native.py
index ce822ce..d72b813 100644
--- a/src/frame_interpolation/native.py
+++ b/src/frame_interpolation/native.py
@@ -18,16 +18,32 @@ FRAME_MAGIC = 0x31464746
FRAME_OUT_MAGIC = 0x314F4746
-def _read_exact(stream, size: int) -> bytes:
- data = bytearray()
- while len(data) < size:
- block = stream.read(size - len(data))
- if not block:
+def _read_into_exact(stream, destination) -> None:
+ view = memoryview(destination).cast("B")
+ offset = 0
+ while offset < len(view):
+ count = stream.readinto(view[offset:])
+ if not count:
raise RuntimeError("The direct DLSSG worker closed its output unexpectedly.")
- data.extend(block)
+ offset += count
+
+
+def _read_exact(stream, size: int) -> bytes:
+ data = bytearray(size)
+ _read_into_exact(stream, data)
return bytes(data)
+def _write_all(stream, source) -> None:
+ view = memoryview(source).cast("B")
+ offset = 0
+ while offset < len(view):
+ count = stream.write(view[offset:])
+ if not count:
+ raise RuntimeError("The direct DLSSG worker closed its input unexpectedly.")
+ offset += count
+
+
class DirectDLSSGSession:
"""One direct D3D12 NGX DLSSG history and binary stream."""
@@ -55,6 +71,7 @@ class DirectDLSSGSession:
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
bufsize=0,
+ creationflags=getattr(subprocess, "CREATE_NO_WINDOW", 0),
)
controller.register(self.process)
assert self.process.stdin is not None
@@ -62,7 +79,18 @@ class DirectDLSSGSession:
assert self.process.stderr is not None
self._log_thread = threading.Thread(target=self._read_logs, daemon=True)
self._log_thread.start()
- self.process.stdin.write(
+ self.closed = False
+ self._next_index = 0
+ try:
+ self._setup(frame_count)
+ except BaseException:
+ self.abort()
+ self.close()
+ raise
+
+ def _setup(self, frame_count: int) -> None:
+ _write_all(
+ self.process.stdin,
struct.pack(
"<5I",
SETUP_MAGIC,
@@ -88,8 +116,6 @@ class DirectDLSSGSession:
f"DLSSG worker rejected {self.generated_count} generated frame(s); "
f"MultiFrameCountMax is {maximum}."
)
- self._next_index = 0
- self.closed = False
def _read_logs(self) -> None:
assert self.process.stderr is not None
@@ -119,7 +145,8 @@ class DirectDLSSGSession:
raise ValueError(f"DLSSG motion field has unexpected shape {vectors.shape}.")
assert self.process.stdin is not None
assert self.process.stdout is not None
- self.process.stdin.write(
+ _write_all(
+ self.process.stdin,
struct.pack(
"<4I2q",
FRAME_MAGIC,
@@ -130,8 +157,8 @@ class DirectDLSSGSession:
timestamp.denominator,
)
)
- self.process.stdin.write(memoryview(color).cast("B"))
- self.process.stdin.write(memoryview(vectors).cast("B"))
+ _write_all(self.process.stdin, color)
+ _write_all(self.process.stdin, vectors)
self.process.stdin.flush()
self._next_index += 1
magic, status, generated, disabled = struct.unpack(
@@ -142,14 +169,25 @@ class DirectDLSSGSession:
f"DLSSG evaluation failed at input frame {self._next_index - 1} "
f"(status {status}).\n{self.log_text()}"
)
+ if generated > self.generated_count or (disabled and generated):
+ raise RuntimeError("DLSSG worker returned an invalid generated-frame count.")
if disabled:
return []
- return [
- np.frombuffer(_read_exact(self.process.stdout, self.frame_bytes), np.uint8)
- .reshape(self.height, self.width, 4)
- .copy()
- for _ in range(generated)
- ]
+ frames = []
+ for _ in range(generated):
+ # Each returned array owns its storage until every downstream user
+ # releases it. Reading into it avoids three full-frame copies.
+ frame = np.empty((self.height, self.width, 4), dtype=np.uint8)
+ _read_into_exact(self.process.stdout, frame)
+ frames.append(frame)
+ return frames
+
+ def abort(self) -> None:
+ if self.process.poll() is None:
+ try:
+ self.process.terminate()
+ except OSError:
+ pass
def close(self) -> None:
if getattr(self, "closed", False):
@@ -169,8 +207,12 @@ class DirectDLSSGSession:
process.wait(timeout=5)
except subprocess.TimeoutExpired:
process.kill()
+ process.wait(timeout=5)
self.controller.unregister(process)
self._log_thread.join(timeout=1)
+ for stream in (process.stdout, process.stderr):
+ if stream is not None:
+ stream.close()
def __enter__(self) -> "DirectDLSSGSession":
return self
diff --git a/src/frame_interpolation/pipeline.py b/src/frame_interpolation/pipeline.py
new file mode 100644
index 0000000..61746f1
--- /dev/null
+++ b/src/frame_interpolation/pipeline.py
@@ -0,0 +1,218 @@
+"""Ordered CPU/GPU overlap with bounded admission and a single queue owner."""
+from __future__ import annotations
+
+import time
+from collections import deque
+from concurrent.futures import FIRST_COMPLETED, ThreadPoolExecutor, wait
+from typing import Callable, Iterable
+
+from ..core.jobs import Cancelled, JobController
+
+BUFFER_LIMIT_BYTES = 1 << 30
+
+
+class PipeWriter:
+ """A subprocess pipe cannot seek, even when its Python wrapper has seek()."""
+
+ def __init__(self, stream):
+ self.stream = stream
+
+ def write(self, data):
+ view = memoryview(data)
+ offset = 0
+ while offset < len(view):
+ count = self.stream.write(view[offset:])
+ if not count:
+ raise BrokenPipeError("Video encoder closed its input.")
+ offset += count
+ return offset
+
+ def flush(self):
+ self.stream.flush()
+
+
+def run_pipeline(
+ source: Iterable,
+ stages: list,
+ writer,
+ controller: JobController,
+ frame_bytes: int,
+ timings: dict[str, float],
+ progress: Callable[[int], None] | None = None,
+ *,
+ buffer_limit_bytes: int = BUFFER_LIMIT_BYTES,
+) -> dict:
+ """Coordinate four single-owner workers; workers never wait on queue puts.
+
+ One credit represents one packed RGBA or FP16 motion array. Reserve all
+ possible native outputs before evaluation, plus motion storage before guide
+ preparation. Conservative accounting counts shared arrays more than once.
+ Existing per-stage history, guide scratch space and codec buffers are not
+ additional queue storage and are not included in this limit.
+ """
+ if frame_bytes <= 0 or buffer_limit_bytes <= 0:
+ raise ValueError("Frame size and buffer limit must be positive.")
+ started = time.perf_counter()
+ iterator = iter(source)
+ max_generated = max((stage.generated_count for stage in stages), default=0)
+ capacity = buffer_limit_bytes // frame_bytes
+ decoded = 0
+ processed = 0
+ used = peak = 0
+
+ def timed(key, function, *args):
+ before = time.perf_counter()
+ try:
+ return function(*args)
+ finally:
+ timings[key] = timings.get(key, 0.0) + time.perf_counter() - before
+
+ # Even one input/output transaction may exceed the queue budget for enormous
+ # frames. Keep the sequential path available without allocating extra queues.
+ if capacity < max_generated + 3:
+ try:
+ for frame in iterator:
+ if controller.cancel.is_set():
+ raise Cancelled("Frame interpolation stopped by user.")
+ items = [frame]
+ for index, stage in enumerate(stages):
+ next_items = []
+ for item in items:
+ prepared = timed(f"guide_{index + 1}_seconds", stage.prepare, item)
+ next_items.extend(timed(f"native_{index + 1}_seconds", stage.evaluate, prepared))
+ items = next_items
+ for item in items:
+ timed("encode_write_seconds", writer.push, item)
+ decoded += 1
+ if progress:
+ progress(decoded)
+ timed("encode_write_seconds", writer.finish)
+ except BaseException:
+ controller.terminate_processes()
+ raise
+ finally:
+ if hasattr(iterator, "close"):
+ iterator.close()
+ timings["pipeline_seconds"] = time.perf_counter() - started
+ return {"mode": "sequential", "buffer_limit_bytes": buffer_limit_bytes,
+ "peak_buffer_bytes": 0, "decoded_frames": decoded}
+
+ edges = [deque() for _ in range(len(stages) + 1)]
+ prepared_queues = [deque() for _ in stages]
+ edge_capacity = max(4, max_generated + 1)
+ pools = {name: ThreadPoolExecutor(max_workers=1, thread_name_prefix=f"FI-{name}")
+ for name in ("decode", "guide", "native", "encode")}
+ pending = {}
+ ended = False
+
+ def reserve(credits):
+ nonlocal used, peak
+ used += credits
+ if used > capacity:
+ raise RuntimeError("Frame interpolation buffer reservation exceeded its limit.")
+ peak = max(peak, used)
+
+ def consume(items):
+ for item in items:
+ if controller.cancel.is_set():
+ raise Cancelled("Frame interpolation stopped by user.")
+ writer.push(item)
+
+ try:
+ while True:
+ if controller.cancel.is_set():
+ raise Cancelled("Frame interpolation stopped by user.")
+ # Harvest before dispatch. Results own their arrays until consumed;
+ # neither futures nor workers can block while publishing a result.
+ for name, (future, index, credits) in list(pending.items()):
+ if not future.done():
+ continue
+ result = future.result()
+ del pending[name]
+ if name == "decode":
+ if result is None:
+ ended = True
+ used -= 1
+ else:
+ edges[0].append(result)
+ decoded += 1
+ elif name == "guide":
+ prepared_queues[index].append(result)
+ elif name == "native":
+ if len(result) > credits + 1:
+ raise RuntimeError("DLSSG produced more frames than reserved.")
+ edges[index + 1].extend(result)
+ used -= 2 + credits - len(result)
+ if index == 0:
+ processed += 1
+ if progress:
+ progress(processed)
+ else:
+ used -= credits
+ if not stages and progress:
+ processed += credits
+ progress(processed)
+ # Do not keep the last harvested frame/result alive in locals.
+ result = None
+ future = None
+
+ if "encode" not in pending and edges[-1]:
+ items = [edges[-1].popleft() for _ in range(min(edge_capacity, len(edges[-1])))]
+ pending["encode"] = (pools["encode"].submit(timed, "encode_write_seconds", consume, items),
+ -1, len(items))
+ items = None
+
+ if "native" not in pending:
+ for index in reversed(range(len(stages))):
+ count = stages[index].generated_count
+ if (prepared_queues[index] and used + count <= capacity
+ and len(edges[index + 1]) + count + 1 <= edge_capacity):
+ reserve(count)
+ prepared = prepared_queues[index].popleft()
+ pending["native"] = (pools["native"].submit(
+ timed, f"native_{index + 1}_seconds", stages[index].evaluate, prepared), index, count)
+ prepared = None
+ break
+
+ if "guide" not in pending:
+ for index in reversed(range(len(stages))):
+ # Retain native output headroom even if the GPU is idle.
+ if (edges[index] and len(prepared_queues[index]) < 2
+ and used + 1 <= capacity - max_generated):
+ reserve(1)
+ item = edges[index].popleft()
+ pending["guide"] = (pools["guide"].submit(
+ timed, f"guide_{index + 1}_seconds", stages[index].prepare, item), index, 1)
+ item = None
+ break
+
+ if (not ended and "decode" not in pending and len(edges[0]) < edge_capacity
+ and used + 1 <= capacity - max_generated - 1):
+ reserve(1)
+ pending["decode"] = (pools["decode"].submit(timed, "decode_seconds", next, iterator, None), -1, 1)
+
+ if not pending:
+ if ended and not any(edges) and not any(prepared_queues):
+ break
+ raise RuntimeError("Frame interpolation could not drain its bounded pipeline.")
+ before_wait = time.perf_counter()
+ wait([value[0] for value in pending.values()], timeout=0.05, return_when=FIRST_COMPLETED)
+ timings["coordinator_wait_seconds"] = timings.get("coordinator_wait_seconds", 0.0) + time.perf_counter() - before_wait
+
+ pools["encode"].submit(timed, "encode_write_seconds", writer.finish).result()
+ except BaseException:
+ # Closing worker/encoder processes releases any OS pipe I/O in flight.
+ # Do not mark the shared batch controller cancelled for a failed item.
+ controller.terminate_processes()
+ raise
+ finally:
+ for pool in pools.values():
+ pool.shutdown(wait=True, cancel_futures=True)
+ pending.clear()
+ for edge in [*edges, *prepared_queues]:
+ edge.clear()
+ if hasattr(iterator, "close"):
+ iterator.close()
+ timings["pipeline_seconds"] = time.perf_counter() - started
+ return {"mode": "overlapped", "buffer_limit_bytes": buffer_limit_bytes,
+ "peak_buffer_bytes": peak * frame_bytes, "decoded_frames": decoded}
diff --git a/src/frame_interpolation/processor.py b/src/frame_interpolation/processor.py
index bd0905c..5b2afac 100644
--- a/src/frame_interpolation/processor.py
+++ b/src/frame_interpolation/processor.py
@@ -4,10 +4,11 @@ import json
import math
import os
import shutil
+import subprocess
import threading
import time
from contextlib import nullcontext
-from dataclasses import asdict, dataclass
+from dataclasses import asdict, dataclass, replace
from fractions import Fraction
from pathlib import Path
from typing import Callable
@@ -16,20 +17,23 @@ import av
import numpy as np
from ..core import ffmpeg
+from ..core.dlss_architecture import apply_current_dlss_architecture
from ..core.gpu_selection import resolve_runtime_ai_gpu
from ..core.jobs import Cancelled, active_job
-from ..core.naming import output_filename, require_available_output, validate_rename
+from ..core.naming import output_filename, validate_rename
+from ..core.disk_paths import OutputFile, prepare_output_dir
from ..core.paths import JOBS, LOGS, OUTPUTS
from ..core.runtime import prepare_runtime, rotate_frame
from .capabilities import probe_frame_interpolation_capabilities
-from .guides import DLSSGGuideGenerator
+from .guides import DLSSGGuideGenerator, Guide
from .models import FrameInterpolationOptions, FrameInterpolationResult
from .native import DirectDLSSGSession
from .scheduler import choose_interpolation_plan, output_frame_count
+from .pipeline import run_pipeline, PipeWriter
_BATCH_CONTEXT = threading.local()
-@dataclass(slots=True)
+@dataclass(frozen=True, slots=True)
class TimedFrame:
rgba: np.ndarray
timestamp: Fraction
@@ -38,6 +42,14 @@ class TimedFrame:
source_index: int | None = None
+@dataclass(frozen=True, slots=True)
+class PreparedFrame:
+ frame: TimedFrame
+ previous_timestamp: Fraction | None
+ guide: Guide
+ reset: bool
+
+
class DLSSGStage:
def __init__(
self,
@@ -56,33 +68,38 @@ class DLSSGStage:
self.scene_cuts = 0
self.duplicates = 0
- def push(self, frame: TimedFrame) -> list[TimedFrame]:
+ def prepare(self, frame: TimedFrame) -> PreparedFrame:
+ # Only the guide worker advances this history. Capture all temporal
+ # decisions here so native evaluation can overlap the next preparation.
+ previous = self.previous
force_reset = self.previous is not None and frame.segment != self.previous.segment
guide = self.guides.process(frame.rgba, force_reset=force_reset)
- if self.previous is None:
- self.session.process_frame(frame.rgba, guide.motion, frame.timestamp, reset=True)
- self.previous = frame
- return [frame]
- if self.detect_source_cuts and guide.reset and not force_reset:
- frame.segment = self.previous.segment + 1
+ if previous is not None and self.detect_source_cuts and guide.reset and not force_reset:
+ frame = replace(frame, segment=previous.segment + 1)
force_reset = True
self.scene_cuts += 1
- if guide.duplicate:
+ if previous is not None and guide.duplicate:
self.duplicates += 1
+ self.previous = frame
+ return PreparedFrame(frame, previous.timestamp if previous else None, guide,
+ previous is None or force_reset or guide.reset)
+
+ def evaluate(self, prepared: PreparedFrame) -> list[TimedFrame]:
+ frame = prepared.frame
generated = self.session.process_frame(
frame.rgba,
- guide.motion,
+ prepared.guide.motion,
frame.timestamp,
- reset=force_reset or guide.reset,
+ reset=prepared.reset,
)
output: list[TimedFrame] = []
- if not (force_reset or guide.reset):
- interval = frame.timestamp - self.previous.timestamp
+ if not prepared.reset and prepared.previous_timestamp is not None:
+ interval = frame.timestamp - prepared.previous_timestamp
for index, rgba in enumerate(generated, start=1):
output.append(
TimedFrame(
rgba,
- self.previous.timestamp
+ prepared.previous_timestamp
+ interval * Fraction(index, self.generated_count + 1),
frame.segment,
"DLSSG",
@@ -90,9 +107,11 @@ class DLSSGStage:
)
)
output.append(frame)
- self.previous = frame
return output
+ def push(self, frame: TimedFrame) -> list[TimedFrame]:
+ return self.evaluate(self.prepare(frame))
+
class NearestTimestampWriter:
def __init__(self, nut, stream, target_rate: Fraction, output_count: int) -> None:
@@ -109,11 +128,13 @@ class NearestTimestampWriter:
self.selected_real_ids: set[int] = set()
def _write(self, frame: TimedFrame, ideal: Fraction) -> None:
- video_frame = av.VideoFrame.from_ndarray(frame.rgba, format="rgba")
- video_frame.pts = self.next_index
- video_frame.time_base = Fraction(1, 1) / self.target_rate
- for packet in self.stream.encode(video_frame):
- self.nut.mux(packet)
+ packet = av.Packet(memoryview(frame.rgba).cast("B"))
+ packet.stream = self.stream
+ packet.pts = packet.dts = self.next_index
+ packet.duration = 1
+ packet.time_base = Fraction(1, 1) / self.target_rate
+ packet.is_keyframe = True
+ self.nut.mux(packet)
error = abs(frame.timestamp - ideal)
self.max_error = max(self.max_error, error)
if frame.provenance == "DLSSG":
@@ -157,8 +178,6 @@ class NearestTimestampWriter:
self.previous.source_index,
)
self._write(endpoint, ideal)
- for packet in self.stream.encode():
- self.nut.mux(packet)
def _json_safe(value):
if isinstance(value, Fraction):
@@ -194,6 +213,7 @@ def interpolate_video(
input_path: str | os.PathLike[str],
options: FrameInterpolationOptions | None = None,
progress: Callable[[float, str], None] | None = None,
+ *, output_dir: str | os.PathLike[str] | None = None, controller=None,
) -> FrameInterpolationResult:
options = options or FrameInterpolationOptions()
_validate(options)
@@ -204,13 +224,19 @@ def interpolate_video(
ai_gpu = resolve_runtime_ai_gpu(
prepared_runtime.gpus, prepared_runtime.runtime_bundle, options.ai_gpu_uuid
)
+ # Stage the selected DLSS Architecture NR build before workers spawn
+ # (warn-and-continue: render proceeds with the current DLL).
+ try:
+ apply_current_dlss_architecture(ai_gpu)
+ except Exception:
+ pass
capabilities = probe_frame_interpolation_capabilities(options.ai_gpu_uuid)
if not capabilities.available:
raise RuntimeError(
"Direct NVIDIA DLSS Frame Generation is unavailable. " + capabilities.detail
)
prepared_controller = getattr(_BATCH_CONTEXT, "controller", None)
- job_context = nullcontext(prepared_controller) if prepared_controller is not None else active_job()
+ job_context = nullcontext(prepared_controller) if prepared_controller is not None else active_job(controller)
with job_context as controller:
assert controller is not None
started = time.perf_counter()
@@ -237,6 +263,10 @@ def interpolate_video(
job_dir: Path | None = None
sessions: list[DirectDLSSGSession] = []
encoder = None
+ encoder_thread = None
+ nut = None
+ output_file: OutputFile | None = None
+ performance = {}
try:
probe_started = time.perf_counter()
metadata = ffmpeg.probe_video(source, count_mode="metadata")
@@ -283,7 +313,7 @@ def interpolate_video(
timings["probe_seconds"] = time.perf_counter() - probe_started
_report_progress(0.01, f"{plan.path}: {source_rate} → {options.target_rate} FPS")
- OUTPUTS.mkdir(exist_ok=True)
+ destination = prepare_output_dir(output_dir, default=OUTPUTS)
JOBS.mkdir(exist_ok=True)
LOGS.mkdir(exist_ok=True)
stamp = time.strftime("%Y%m%d-%H%M%S") + f"-{time.time_ns() % 1_000_000:06d}"
@@ -291,10 +321,10 @@ def interpolate_video(
job_dir.mkdir(parents=True, exist_ok=False)
extension = {"MP4": ".mp4", "MKV": ".mkv", "MOV": ".mov"}[options.container]
auto_stem = f"{source.stem}_{'DLSSFG_PREVIEW' if options.preview_seconds else 'DLSSFG'}_{stamp}"
- output = OUTPUTS / output_filename(
+ output = destination / output_filename(
source, extension, options.rename_mode, options.custom_suffix, auto_stem
)
- require_available_output(output)
+ output_file = OutputFile(output)
# NUT preserves the exact rational frame clock between PyAV and FFmpeg;
# Matroska's millisecond timestamp scale rounds 60000/1001 and short previews.
temp_video = job_dir / "interpolated-video.nut"
@@ -306,6 +336,7 @@ def interpolate_video(
int(metadata["width"]),
int(metadata["height"]),
)
+ setup_started = time.perf_counter()
encoder, encoder_thread, encoder_logs, selected_encoder, quality = ffmpeg.start_encoder(
temp_video,
selected_codec,
@@ -345,79 +376,90 @@ def interpolate_video(
)
for index, session in enumerate(sessions)
]
- nut = av.open(encoder.stdin, mode="w", format="nut")
+ nut = av.open(PipeWriter(encoder.stdin), mode="w", format="nut")
raw_stream = nut.add_stream("rawvideo", rate=options.target_rate)
raw_stream.width = int(metadata["width"])
raw_stream.height = int(metadata["height"])
raw_stream.pix_fmt = "rgba"
+ raw_stream.codec_context.codec_tag = "RGBA"
raw_stream.time_base = Fraction(1, 1) / options.target_rate
+ nut.start_encoding()
writer = NearestTimestampWriter(nut, raw_stream, options.target_rate, output_count)
+ timings["encoder_and_worker_setup_seconds"] = time.perf_counter() - setup_started
decoded = 0
- segment = 0
discontinuities = 0
- last_timestamp: Fraction | None = None
nominal_interval = Fraction(1, 1) / source_rate
- container = av.open(str(source))
- try:
- stream = container.streams.video[0]
- stream.thread_type = "AUTO"
- for frame in container.decode(stream):
- if decoded >= frames:
- break
- if controller.cancel.is_set():
- raise Cancelled("Frame interpolation stopped by user.")
- timestamp = (
- Fraction(frame.pts) * Fraction(stream.time_base)
- if frame.pts is not None and stream.time_base is not None
- else Fraction(decoded, 1) / source_rate
- )
- if last_timestamp is not None and (
- timestamp <= last_timestamp or timestamp - last_timestamp > nominal_interval * 2
- ):
- segment += 1
- discontinuities += 1
- last_timestamp = timestamp
- rgba = rotate_frame(frame.to_ndarray(format="rgba"), int(metadata["rotation"]))
- timed = TimedFrame(
- np.ascontiguousarray(rgba), timestamp, segment, "Source", decoded
- )
- items = [timed]
- for stage in stages:
- next_items: list[TimedFrame] = []
- for item in items:
- next_items.extend(stage.push(item))
- items = next_items
- for item in items:
- writer.push(item)
- decoded += 1
- if decoded % max(1, frames // 100) == 0:
- _report_progress(0.04 + 0.82 * decoded / frames, f"DLSSG source frame {decoded}/{frames}")
- finally:
- container.close()
- writer.finish()
+
+ def decode_frames():
+ nonlocal decoded, discontinuities
+ segment = 0
+ last_timestamp = None
+ with av.open(str(source)) as container:
+ stream = container.streams.video[0]
+ stream.thread_type = "AUTO"
+ for frame in container.decode(stream):
+ if decoded >= frames:
+ break
+ if controller.cancel.is_set():
+ raise Cancelled("Frame interpolation stopped by user.")
+ timestamp = (
+ Fraction(frame.pts) * Fraction(stream.time_base)
+ if frame.pts is not None and stream.time_base is not None
+ else Fraction(decoded, 1) / source_rate
+ )
+ if last_timestamp is not None and (
+ timestamp <= last_timestamp or timestamp - last_timestamp > nominal_interval * 2
+ ):
+ segment += 1
+ discontinuities += 1
+ last_timestamp = timestamp
+ rgba = rotate_frame(frame.to_ndarray(format="rgba"), int(metadata["rotation"]))
+ item = TimedFrame(np.ascontiguousarray(rgba), timestamp, segment, "Source", decoded)
+ decoded += 1
+ yield item
+
+ def pipeline_progress(processed):
+ if processed % max(1, frames // 100) == 0:
+ _report_progress(0.04 + 0.82 * processed / frames,
+ f"DLSSG source frame {processed}/{frames}")
+
+ performance = run_pipeline(
+ decode_frames(), stages, writer, controller,
+ int(metadata["width"]) * int(metadata["height"]) * 4,
+ timings, pipeline_progress,
+ )
nut.close()
+ nut = None
encoder.stdin.close()
+ shutdown_started = time.perf_counter()
for session in sessions:
session.close()
sessions.clear()
+ timings["worker_shutdown_seconds"] = time.perf_counter() - shutdown_started
+ drain_started = time.perf_counter()
encoder_code = encoder.wait(timeout=180)
encoder_thread.join(timeout=2)
controller.unregister(encoder)
if encoder_code:
raise RuntimeError("Video encoder failed:\n" + "\n".join(encoder_logs[-40:]))
+ timings["encoder_drain_seconds"] = time.perf_counter() - drain_started
_report_progress(0.9, "Muxing original audio, subtitles, chapters, and metadata")
+ mux_started = time.perf_counter()
ffmpeg.final_mux(
temp_video,
source,
- output,
+ output_file.temporary,
options.container,
controller,
preserve_supported_subtitles=True,
)
- verified = ffmpeg.probe_video(output, count_mode="packets")
+ timings["mux_seconds"] = time.perf_counter() - mux_started
+ verification_started = time.perf_counter()
+ _report_progress(0.96, "Verifying saved output")
+ verified = ffmpeg.probe_video(output_file.temporary, count_mode="packets", controller=controller)
if int(verified["frames"]) != output_count:
- verified = ffmpeg.probe_video(output, count_mode="exact")
+ verified = ffmpeg.probe_video(output_file.temporary, count_mode="exact", controller=controller)
if int(verified["frames"]) != output_count:
raise RuntimeError(
f"Output verification found {verified['frames']} frames; expected {output_count}."
@@ -429,7 +471,11 @@ def interpolate_video(
scene_cuts = sum(stage.scene_cuts for stage in stages)
duplicate_intervals = sum(stage.duplicates for stage in stages)
dropped_frames = max(0, decoded - len(writer.selected_real_ids))
+ timings["verification_seconds"] = time.perf_counter() - verification_started
elapsed = time.perf_counter() - started
+ timings["total_seconds"] = elapsed
+ performance.update(source_frames_per_second=decoded / elapsed,
+ output_frames_per_second=output_count / elapsed)
report = {
"status": "success",
"input": str(source),
@@ -451,9 +497,14 @@ def interpolate_video(
"encoder": selected_encoder,
"encoding_quality": quality,
"elapsed_seconds": elapsed,
+ "timings": timings,
+ "performance": performance,
}
report_path = LOGS / f"DLSSFG_{source.stem}_{stamp}.report.json"
report_path.write_text(json.dumps(report, indent=2), encoding="utf-8")
+ if controller.cancel.is_set():
+ raise Cancelled("Frame interpolation stopped by user.")
+ output_file.publish()
_report_progress(1.0, "Frame interpolation complete")
return FrameInterpolationResult(
output_path=str(output.resolve()),
@@ -471,8 +522,9 @@ def interpolate_video(
timings=timings,
)
except Exception as exc:
- if output and output.exists():
- output.unlink()
+ controller.terminate_processes()
+ if output_file is not None:
+ output_file.cleanup(rollback=True)
if controller.cancel.is_set():
raise Cancelled("Frame interpolation stopped by user.") from exc
if isinstance(exc, Cancelled):
@@ -489,6 +541,8 @@ def interpolate_video(
"error": str(exc),
"capabilities": asdict(capabilities),
"worker_logs": [session.log_text() for session in sessions],
+ "timings": timings,
+ "performance": performance,
},
indent=2,
),
@@ -496,9 +550,31 @@ def interpolate_video(
)
raise RuntimeError(f"{exc}\nDiagnostic report: {failure_path.resolve()}") from exc
finally:
- for session in sessions:
- session.close()
+ if output_file is not None:
+ output_file.cleanup()
if encoder is not None and encoder.poll() is None:
encoder.terminate()
+ try:
+ encoder.wait(timeout=5)
+ except subprocess.TimeoutExpired:
+ encoder.kill()
+ encoder.wait(timeout=5)
+ if nut is not None:
+ try:
+ nut.close()
+ except (OSError, ValueError):
+ pass
+ for session in sessions:
+ session.close()
+ if encoder is not None:
+ if encoder_thread is not None:
+ encoder_thread.join(timeout=2)
+ for stream in (encoder.stdin, encoder.stderr):
+ if stream is not None and not stream.closed:
+ try:
+ stream.close()
+ except OSError:
+ pass
+ controller.unregister(encoder)
if job_dir and job_dir.exists():
shutil.rmtree(job_dir, ignore_errors=True)
diff --git a/src/frame_interpolation/scheduler.py b/src/frame_interpolation/scheduler.py
index 126c786..a5951d0 100644
--- a/src/frame_interpolation/scheduler.py
+++ b/src/frame_interpolation/scheduler.py
@@ -6,9 +6,6 @@ from fractions import Fraction
from .models import ENGINE_CHOICES, InterpolationPlan
-MAX_OUTPUT_MULTIPLIER = Fraction(6, 1)
-
-
def output_frame_count(duration: Fraction, target_rate: Fraction) -> int:
"""Count timestamps n/rate in the half-open interval [0, duration)."""
if duration <= 0 or target_rate <= 0:
@@ -45,10 +42,6 @@ def choose_interpolation_plan(
if source_rate <= 0 or target_rate <= 0:
raise ValueError("Source and output FPS must be positive.")
ratio = target_rate / source_rate
- if ratio > MAX_OUTPUT_MULTIPLIER:
- raise ValueError(
- f"Requested {float(ratio):.3f}× output exceeds the safe 6× limit for this file."
- )
if target_rate <= source_rate:
return InterpolationPlan(
path="Source-frame resampling",
diff --git a/src/frame_interpolation/ui.py b/src/frame_interpolation/ui.py
index 58a53db..7201b9d 100644
--- a/src/frame_interpolation/ui.py
+++ b/src/frame_interpolation/ui.py
@@ -5,10 +5,10 @@ from dataclasses import dataclass
from pathlib import Path
import gradio as gr
+from ..core.batch_ui import BATCH_HEADERS, bind_batch_ui, build_path_controls
from ..core.ffmpeg import hdr_mode_supported
from ..core.ffmpeg.preview import normalize_preview_encoding, resolve_final_preview
-from ..core.jobs import cancel_active_job
from ..core.naming import RENAME_MODES
from ..settings.models import CODEC_CHOICES, CONTAINER_CHOICES, QUALITY_CHOICES, UISettings, coerce_hdr_mode
from ..settings.storage import current_preview_encoding, processing_gpu_settings
@@ -38,6 +38,7 @@ def render_frame_interpolation_batch(
rename_mode: str,
custom_suffix: str,
progress=gr.Progress(track_tqdm=False),
+ *, output_dir=None, controller=None, on_item_update=None, direct_disk=False,
):
paths = normalize_video_paths(input_paths)
if not paths:
@@ -60,15 +61,19 @@ def render_frame_interpolation_batch(
progress(value, desc=message)
try:
- result = interpolate_videos(paths, options, report)
+ result = interpolate_videos(paths, options, report, output_dir=output_dir,
+ controller=controller, on_item_update=on_item_update)
except Exception as exc:
traceback.print_exc()
+ if on_item_update is not None:
+ raise
return gr.update(value=None, visible=len(paths) == 1), [], [], f"Failed: {exc}"
ordered: list[tuple[int, list[str]]] = []
for item in result.successes:
value = item.result
details = (
f"{value.output_frames} frames; {value.selected_path}; "
+ f"{value.elapsed_seconds:.1f}s; processing {value.output_frames / max(value.elapsed_seconds, 0.001):.1f} frames/s; "
f"native {value.native_multiplier}×; cascade stages {value.cascade_stages}; "
f"copied {value.copied_frames}, DLSSG {value.generated_frames}, "
f"cuts {value.scene_cuts}; report: {value.report_path}"
@@ -89,8 +94,8 @@ def render_frame_interpolation_batch(
preview_mode = "Auto"
preview = None
used_derivative = False
- if len(paths) == 1 and result.successes:
- preview, used_derivative = resolve_final_preview(files[0], preview_mode)
+ if not direct_disk and len(paths) == 1 and result.successes and not result.cancelled:
+ preview, used_derivative = resolve_final_preview(files[0], preview_mode, controller=controller)
status = (
f"{'Cancelled' if result.cancelled else 'Complete'}: "
f"{len(result.successes)} completed, {len(result.failures)} failed/cancelled. "
@@ -100,7 +105,7 @@ def render_frame_interpolation_batch(
status += f"\nFirst error: {result.failures[0].error}"
if used_derivative:
status += "\nBrowser preview transcoded to H.264; the original file is unchanged."
- return gr.update(value=preview, visible=len(paths) == 1), files, rows, status
+ return gr.update(value=preview, visible=not direct_disk and len(paths) == 1), ([] if direct_disk else files), rows, status
@dataclass(slots=True)
class FrameInterpolationTab:
@@ -122,6 +127,9 @@ class FrameInterpolationTab:
output_files: object
status: object
results: object
+ input_path: object = None
+ output_path: object = None
+ job_state: object = None
@property
def render_inputs(self) -> list[object]:
@@ -149,6 +157,7 @@ def build_frame_interpolation_tab(settings: UISettings) -> FrameInterpolationTab
label="Input video(s)", file_count="multiple", file_types=["video"],
type="filepath", allow_reordering=True, elem_id="frame-interpolation-upload-list",
)
+ input_path, output_path = build_path_controls()
input_preview = gr.Video(label="Input video preview", interactive=False, visible=False)
with gr.Accordion("DLSS Frame Generation Settings", open=True):
with gr.Row():
@@ -198,36 +207,24 @@ def build_frame_interpolation_tab(settings: UISettings) -> FrameInterpolationTab
label="Interpolated video files", file_count="multiple", interactive=False,
elem_id="frame-interpolation-output-list",
)
- status = gr.Textbox(label="Status", interactive=False)
+ status = gr.Textbox(label="Status", interactive=False, lines=5, max_lines=12)
results = gr.Dataframe(
- headers=["Input", "Result", "Output", "Details"],
- datatype=["str", "str", "str", "str"], interactive=False,
+ headers=BATCH_HEADERS,
+ datatype=["str"] * len(BATCH_HEADERS), interactive=False,
label="Batch results", wrap=True,
)
tab = FrameInterpolationTab(
sources, input_preview, target_fps, engine, quality, codec, container, rename_mode,
custom_suffix, hdr_mode, preview, render, stop, reset, output_video, output_files, status, results
)
+ tab.input_path, tab.output_path = input_path, output_path
bind_frame_interpolation_events(tab)
return tab
def bind_frame_interpolation_events(tab: FrameInterpolationTab) -> None:
- tab.sources.change(
- update_frame_interpolation_preview_mode, inputs=[tab.sources, tab.target_fps, tab.engine],
- outputs=[tab.input_preview, tab.output_video, tab.preview],
- queue=False, show_progress="hidden",
- )
+ bind_batch_ui(tab, render_frame_interpolation_batch, kind="video",
+ preview_mode=update_frame_interpolation_preview_mode,
+ preview_actions=[(tab.preview, preview_frame_interpolation)])
tab.codec.change(hdr_mode_update, inputs=tab.codec, outputs=tab.hdr_mode, queue=False)
tab.rename_mode.change(rename_suffix_update, inputs=tab.rename_mode, outputs=tab.custom_suffix, queue=False)
- tab.render.click(
- render_frame_interpolation_batch, inputs=tab.render_inputs,
- outputs=[tab.output_video, tab.output_files, tab.results, tab.status],
- concurrency_limit=1, show_progress="full", show_progress_on=tab.output_video,
- )
- tab.preview.click(
- preview_frame_interpolation, inputs=tab.preview_inputs, outputs=[tab.output_video, tab.status],
- concurrency_limit=1, show_progress="full", show_progress_on=tab.output_video,
- )
- tab.stop.click(cancel_active_job, outputs=tab.status, queue=False)
-
diff --git a/src/image/batch.py b/src/image/batch.py
index db394fe..7438e47 100644
--- a/src/image/batch.py
+++ b/src/image/batch.py
@@ -2,39 +2,32 @@ from __future__ import annotations
import os
import time
-from concurrent.futures import Future, ThreadPoolExecutor
-from dataclasses import dataclass
+from contextlib import suppress
+from concurrent.futures import ThreadPoolExecutor, TimeoutError
+from dataclasses import replace
from pathlib import Path
from typing import Callable, Iterable
import cv2
import numpy as np
+from ..core.batch_progress import BatchItemUpdate, BatchProgress
+from ..core.disk_paths import OutputFile, prepare_output_dir
+from ..core.dlss_architecture import apply_current_dlss_architecture
from ..core.gpu_selection import resolve_runtime_ai_gpu
-from ..core.jobs import Cancelled, active_job
-from ..core.naming import output_filename, require_available_output, validate_rename
+from ..core.jobs import Cancelled, JobController, active_job
+from ..core.render_metadata import prepare_render_note
+from ..core.naming import output_filename, validate_rename
from ..core.paths import LOGS, OUTPUTS
from ..core.runtime import (
DLSSFrameSession, prepare_runtime, resize_fit, resolve_native_settings,
resolve_output_size, resolve_upscaling_mode, verify_feature_18, write_failure_report,
)
-from .decoder import _DecodedImage, decode_image, probe_image
-from .encoder import _encode_image
-from .models import (
- IMAGE_EXTENSIONS, IMAGE_FORMATS, RAW_EXTENSIONS, ImageBatchResult, ImageConversionFailure,
- ImageConversionOptions, ImageConversionResult,
-)
-from .reports import _ImageReportData, _build_manifest_and_zip, _report_data, _write_report
+from .decoder import decode_image
+from .encoder import _encode_image, take_image_preview
+from .models import IMAGE_EXTENSIONS, IMAGE_FORMATS, ImageBatchResult, ImageConversionFailure, ImageConversionOptions, ImageConversionResult
+from .reports import _build_manifest_and_zip, _report_data, _write_report
-@dataclass(slots=True)
-class _ImageProbe:
- index: int
- path: Path
- width: int
- height: int
- output_width: int
- output_height: int
- decoded: _DecodedImage | None = None
def _validate_options(options: ImageConversionOptions) -> ImageConversionOptions:
if options.output_format not in IMAGE_FORMATS:
@@ -52,364 +45,182 @@ def _validate_options(options: ImageConversionOptions) -> ImageConversionOptions
resolve_upscaling_mode(options.upscaling_factor)
return options
-def _output_path(
- source: Path,
- output_format: str,
- stamp: str,
- index: int,
- rename_mode: str,
- custom_suffix: str,
-) -> Path:
- suffix = IMAGE_EXTENSIONS[output_format]
+
+def _output_path(source, output_format, stamp, index, rename_mode, custom_suffix, *, output_dir=None):
safe_stem = source.stem.strip().rstrip(".") or "image"
- return OUTPUTS / output_filename(
- source,
- suffix,
- rename_mode,
- custom_suffix,
+ return (output_dir or OUTPUTS) / output_filename(
+ source, IMAGE_EXTENSIONS[output_format], rename_mode, custom_suffix,
f"{safe_stem}_DLSS5_IMAGE_{stamp}-{index + 1:04d}",
)
+
def convert_images(
input_paths: Iterable[str | os.PathLike[str]],
options: ImageConversionOptions | None = None,
progress: Callable[[float, str], None] | None = None,
+ *, output_dir: str | os.PathLike[str] | None = None,
+ controller: JobController | None = None,
+ on_item_update: Callable[[BatchItemUpdate], None] | None = None,
+ generate_previews: bool = True, create_zip: bool = True,
) -> ImageBatchResult:
- options = options or ImageConversionOptions()
- neural = _validate_options(options)
+ options = _validate_options(replace(options) if options else ImageConversionOptions())
paths = [Path(path).resolve() for path in input_paths]
if not paths:
raise ValueError("Choose at least one image.")
- prepared_runtime = prepare_runtime()
- OUTPUTS.mkdir(exist_ok=True)
- LOGS.mkdir(exist_ok=True)
+ controller = controller or JobController()
+ reporter = BatchProgress(paths, on_item_update, progress)
+ successes, failures = [], []
+ session = None
stamp = time.strftime("%Y%m%d-%H%M%S") + f"-{time.time_ns() % 1_000_000:06d}"
- batch_started = time.perf_counter()
-
- def _report_progress(value: float, desc: str) -> None:
- if progress is None:
- return
- try:
- v = float(value)
- v = 0.0 if v < 0 else (1.0 if v > 1 else v)
- elapsed = time.perf_counter() - batch_started
- if 0.01 < v < 0.99 and elapsed > 0.5:
- eta = elapsed * (1 - v) / max(v, 1e-6)
- desc = f"{desc} - Time Remaining: {eta:.1f}s"
- progress(v, desc)
- except Exception:
+ try:
+ destination = prepare_output_dir(output_dir, default=OUTPUTS)
+ LOGS.mkdir(exist_ok=True)
+ with active_job(controller), ThreadPoolExecutor(max_workers=1, thread_name_prefix="dlss5-image-decode") as decoder:
+ if controller.cancel.is_set():
+ raise Cancelled("Stopped before rendering.")
+ prepared = prepare_runtime()
+ gpu = resolve_runtime_ai_gpu(prepared.gpus, prepared.runtime_bundle, options.ai_gpu_uuid)
try:
- progress(value, desc)
+ apply_current_dlss_architecture(gpu)
except Exception:
pass
-
- failures_by_index: dict[int, ImageConversionFailure] = {}
- probes: list[_ImageProbe] = []
- for index, path in enumerate(paths):
- try:
- decoded: _DecodedImage | None = None
- if path.suffix.lower() in {*RAW_EXTENSIONS, ".svg"}:
- decoded = decode_image(path)
- height, width = decoded.rgba.shape[:2]
- if width < 64 or height < 64:
- raise ValueError(
- f"{path.name} is {width}×{height}; DLSS requires both input "
- "dimensions to be at least 64 pixels."
- )
- else:
- width, height = probe_image(path)
- output_width, output_height = resolve_output_size(
- width, height, options.upscaling_factor
- )
- probes.append(
- _ImageProbe(
- index, path, width, height, output_width, output_height, decoded
- )
- )
- except Exception as exc:
- failures_by_index[index] = ImageConversionFailure(str(path), str(exc))
-
- planned_outputs: dict[int, Path] = {}
- reserved_outputs: set[str] = set()
- available_probes: list[_ImageProbe] = []
- for probe in probes:
- output = _output_path(
- probe.path,
- options.output_format,
- stamp,
- probe.index,
- options.rename_mode,
- options.custom_suffix,
- )
- output_key = str(output.resolve()).casefold()
- try:
- require_available_output(output)
- if output_key in reserved_outputs:
- raise FileExistsError(
- f"More than one input would create {output.name}. "
- "Choose Auto naming or use unique input names."
- )
- except Exception as exc:
- failures_by_index[probe.index] = ImageConversionFailure(
- str(probe.path), str(exc)
- )
- continue
- reserved_outputs.add(output_key)
- planned_outputs[probe.index] = output
- available_probes.append(probe)
- probes = available_probes
-
- if not probes:
- manifest_path, zip_path = _build_manifest_and_zip(
- stamp, options, [], list(failures_by_index.values()), False
- )
- return ImageBatchResult([], list(failures_by_index.values()), False, manifest_path, zip_path)
-
- groups: dict[tuple[int, int], list[_ImageProbe]] = {}
- for probe in probes:
- groups.setdefault((probe.output_width, probe.output_height), []).append(probe)
-
- successes_by_index: dict[int, ImageConversionResult] = {}
- cancelled = False
- gpu: dict | None = None
- processed_total = 0
- decode_executor = ThreadPoolExecutor(max_workers=1, thread_name_prefix="dlss5-image-decode")
- encode_executor = ThreadPoolExecutor(max_workers=2, thread_name_prefix="dlss5-image-encode")
- with active_job() as controller:
- gpu = resolve_runtime_ai_gpu(
- prepared_runtime.gpus, prepared_runtime.runtime_bundle, options.ai_gpu_uuid
- )
- runtime_bundle = prepared_runtime.runtime_bundle
- _report_progress(0.0, f"Starting feature 18 on {gpu['display_name']}")
- total = len(probes)
- factor, mode = resolve_upscaling_mode(neural.upscaling_factor)
- native_settings = resolve_native_settings(neural)
- try:
- for (output_width, output_height), group in groups.items():
- cursor = 0
- while cursor < len(group):
+ factor, mode = resolve_upscaling_mode(options.upscaling_factor)
+ native = resolve_native_settings(options)
+ next_decode = None
+ session_size = None
+ sent = 0
+ try:
+ for index, path in enumerate(paths):
if controller.cancel.is_set():
- cancelled = True
break
- remaining = group[cursor:]
- first = remaining[0]
+ reporter.advance(index, 0.0, "Decoding image")
+ output_file = None
+ output = _output_path(path, options.output_format, stamp, index,
+ options.rename_mode, options.custom_suffix, output_dir=destination)
+ decoded = processed = render_rgba = None
try:
- session = DLSSFrameSession(
- input_width=first.width,
- input_height=first.height,
- output_width=output_width,
- output_height=output_height,
- frame_count=len(remaining),
- warmup_frames=neural.warmup_frames,
- factor=factor,
- mode=mode,
- native_settings=native_settings,
- gpu=gpu,
- runtime_bundle=runtime_bundle,
- controller=controller,
- )
- except Exception as exc:
- controller.terminate_processes()
- report_path = write_failure_report(
- operation="image-batch-dlss-setup",
- source="; ".join(str(item.path) for item in remaining),
- error=exc,
- gpu=gpu,
- runtime_bundle=runtime_bundle,
+ future = next_decode or decoder.submit(decode_image, path)
+ next_decode = None
+ while True:
+ if controller.cancel.is_set():
+ future.cancel()
+ raise Cancelled("Image rendering stopped by user.")
+ try:
+ decoded = future.result(timeout=.1)
+ break
+ except TimeoutError:
+ continue
+ del future
+ # Only one future image may be decoded ahead of the active file.
+ if index + 1 < len(paths):
+ next_decode = decoder.submit(decode_image, paths[index + 1])
+ height, width = decoded.rgba.shape[:2]
+ if width < 64 or height < 64:
+ raise ValueError(f"{path.name} is {width}×{height}; DLSS requires at least 64×64.")
+ output_width, output_height = resolve_output_size(width, height, options.upscaling_factor)
+ output_file = OutputFile(output)
+ dimensions = (output_width, output_height)
+ if session is not None and dimensions != session_size:
+ session.close()
+ session = None
+ if session is None:
+ reporter.advance(index, .1, "Starting DLSS")
+ session = DLSSFrameSession(
+ input_width=width, input_height=height,
+ output_width=output_width, output_height=output_height,
+ frame_count=None, warmup_frames=options.warmup_frames,
+ factor=factor, mode=mode, native_settings=native, gpu=gpu,
+ runtime_bundle=prepared.runtime_bundle, controller=controller,
+ )
+ session_size, sent = dimensions, 0
+ motion = np.zeros((session.render_height, session.render_width, 2), dtype=np.float16)
+ reporter.advance(index, .25, "Processing image")
+ render_rgba = resize_fit(decoded.rgba, session.render_width, session.render_height)
+ processed, _pts = session.process(index=sent, rgba=render_rgba, motion=motion, reset=True, pts=sent)
+ sent += 1
+ evidence = verify_feature_18(session.worker_logs, session.reshade_log_text())
+ alpha = (decoded.alpha if decoded.alpha.shape == dimensions[::-1] else
+ cv2.resize(decoded.alpha, dimensions, interpolation=cv2.INTER_LANCZOS4))
+ processed[..., 3] = alpha
+ reporter.advance(index, .8, "Saving image")
+ metadata_diagnostics = {}
+ render_note = prepare_render_note(options, session.applied_dlss_model_preset,
+ metadata_diagnostics)
+ warnings = _encode_image(output_file.temporary, processed, options, decoded.metadata,
+ generate_preview=generate_previews, preview_path=output,
+ render_note=render_note, metadata_diagnostics=metadata_diagnostics,
+ controller=controller)
+ if metadata_diagnostics.get("warning") not in warnings and metadata_diagnostics.get("warning"):
+ warnings.append(metadata_diagnostics["warning"])
+ if controller.cancel.is_set():
+ raise Cancelled("Image rendering stopped by user.")
+ reporter.advance(index, .95, "Verifying image and writing report")
+ if index == len(paths) - 1:
+ session.close()
+ result = ImageConversionResult(
+ str(path), str(output), "", reporter.elapsed(index), str(gpu["display_name"]),
+ width, height, session.render_width, session.render_height, output_width, output_height,
+ float(options.upscaling_factor), str(session.mode["name"]), options.output_format,
+ options.dlss_model_preset, session.applied_dlss_model_preset, [*decoded.warnings, *warnings],
)
- failure = f"{exc}\nDiagnostic report: {report_path}"
- for item in remaining:
- failures_by_index[item.index] = ImageConversionFailure(str(item.path), failure)
- break
-
- segment: list[
- tuple[
- _ImageProbe,
- ImageConversionResult,
- _ImageReportData,
- Future[list[str]],
- float,
- ]
- ] = []
- sent = 0
- motion = np.zeros(
- (session.render_height, session.render_width, 2), dtype=np.float16
- )
- interrupted = False
- close_error: Exception | None = None
- decode_future: Future[_DecodedImage] | None = decode_executor.submit(
- lambda probe=group[cursor]: probe.decoded or decode_image(probe.path)
- )
- while cursor < len(group):
- item = group[cursor]
- started = time.perf_counter()
+ result.report_path = _write_report(result, options, _report_data(decoded), gpu, session, evidence,
+ metadata_diagnostics=metadata_diagnostics)
if controller.cancel.is_set():
- cancelled = True
- interrupted = True
- session.abort()
- break
- _report_progress(processed_total / max(1, total), f"Preparing {item.path.name}")
- try:
- assert decode_future is not None
- decoded = decode_future.result()
- item.decoded = None
- next_cursor = cursor + 1
- decode_future = (
- decode_executor.submit(
- lambda probe=group[next_cursor]:
- probe.decoded or decode_image(probe.path)
- )
- if next_cursor < len(group)
- else None
- )
- except Exception as exc:
- failures_by_index[item.index] = ImageConversionFailure(str(item.path), str(exc))
- cursor += 1
- processed_total += 1
- session.abort()
- interrupted = True
- break
- try:
- render_rgba = resize_fit(
- decoded.rgba, session.render_width, session.render_height
- )
- processed, _pts = session.process(
- index=sent,
- rgba=render_rgba,
- motion=motion,
- reset=True,
- pts=sent,
- )
- alpha = (
- decoded.alpha
- if decoded.alpha.shape == (output_height, output_width)
- else cv2.resize(
- decoded.alpha,
- (output_width, output_height),
- interpolation=cv2.INTER_LANCZOS4,
- )
- )
- processed[..., 3] = alpha
- output = planned_outputs[item.index]
- result = ImageConversionResult(
- str(item.path),
- str(output),
- "",
- time.perf_counter() - started,
- str(gpu["display_name"]),
- item.width,
- item.height,
- session.render_width,
- session.render_height,
- output_width,
- output_height,
- float(options.upscaling_factor),
- str(session.mode["name"]),
- options.output_format,
- options.dlss_model_preset,
- session.applied_dlss_model_preset,
- list(decoded.warnings),
- )
- encoding = encode_executor.submit(
- _encode_image, output, processed, options, decoded.metadata
- )
- segment.append(
- (item, result, _report_data(decoded), encoding, started)
- )
- # Keep at most two full rendered frames queued for encoding.
- # Completed futures retain only their small warning list.
- if len(segment) > 2:
- segment[-3][3].result()
- sent += 1
- except Cancelled:
- cancelled = True
- session.abort()
- interrupted = True
- break
- except Exception as exc:
+ raise Cancelled("Image rendering stopped by user.")
+ output_file.publish()
+ successes.append(result)
+ reporter.complete(index, str(output), "; ".join([*result.warnings, f"Report: {result.report_path}"]))
+ except Exception as exc:
+ cancelled = isinstance(exc, Cancelled) or controller.cancel.is_set()
+ if output_file is not None:
+ output_file.cleanup(rollback=True)
+ preview = take_image_preview(output)
+ if preview is not None:
+ preview.close()
+ if session is not None:
session.abort()
- report_path = write_failure_report(
- operation="image-render",
- source=str(item.path),
- error=exc,
- gpu=gpu,
- runtime_bundle=runtime_bundle,
- worker_code=session.worker.poll(),
- worker_logs=session.worker_logs,
- reshade_lines=session.reshade_diagnostics(),
- )
- failures_by_index[item.index] = ImageConversionFailure(
- str(item.path), f"{exc}\nDiagnostic report: {report_path}"
- )
- interrupted = True
- cursor += 1
- processed_total += 1
- break
- cursor += 1
- processed_total += 1
- _report_progress(processed_total / total, f"Rendered {item.path.name}")
-
- if not segment and interrupted:
+ if not cancelled:
+ with suppress(Exception):
+ report = write_failure_report(
+ operation="image-render", source=str(path), error=exc, gpu=gpu,
+ runtime_bundle=prepared.runtime_bundle, worker_logs=session.worker_logs,
+ reshade_lines=session.reshade_log_text().splitlines(), logs_dir=LOGS,
+ )
+ exc = RuntimeError(f"{exc}\nDiagnostic report: {report}")
+ session = None
+ failures.append(ImageConversionFailure(str(path), str(exc)))
+ reporter.fail(index, exc, cancelled=cancelled)
if cancelled:
+ controller.stop()
break
- continue
- if not interrupted:
- try:
- session.close()
- except Exception as exc:
- close_error = exc
- try:
- if close_error:
- raise close_error
- evidence = verify_feature_18(
- session.worker_logs, session.reshade_log_text()
- )
- assert gpu is not None
- for item, result, decoded, encoding, item_started in segment:
- result.warnings.extend(encoding.result())
- result.elapsed_seconds = time.perf_counter() - item_started
- for item, result, decoded, _encoding, _item_started in segment:
- result.report_path = _write_report(
- result, options, decoded, gpu, session, evidence
- )
- successes_by_index[item.index] = result
- except Exception as exc:
- report_path = write_failure_report(
- operation="image-batch-verification",
- source="; ".join(str(item.path) for item, *_rest in segment),
- error=exc,
- gpu=gpu,
- runtime_bundle=runtime_bundle,
- worker_code=session.worker.poll(),
- worker_logs=session.worker_logs,
- reshade_lines=session.reshade_diagnostics(),
- )
- failure = f"{exc}\nDiagnostic report: {report_path}"
- for item, result, _decoded, encoding, _item_started in segment:
- if not encoding.done():
- encoding.cancel()
- output = Path(result.output_path)
- if output.exists():
- output.unlink()
- failures_by_index[item.index] = ImageConversionFailure(str(item.path), failure)
- if cancelled:
- break
- if cancelled:
- break
- finally:
- decode_executor.shutdown(wait=True, cancel_futures=True)
- encode_executor.shutdown(wait=True, cancel_futures=True)
- if cancelled:
- completed = set(successes_by_index) | set(failures_by_index)
- for probe in probes:
- if probe.index not in completed:
- failures_by_index[probe.index] = ImageConversionFailure(
- str(probe.path), "Cancelled before rendering."
- )
- successes = [successes_by_index[index] for index in sorted(successes_by_index)]
- failures = [failures_by_index[index] for index in sorted(failures_by_index)]
- manifest_path, zip_path = _build_manifest_and_zip(
- stamp, options, successes, failures, cancelled
- )
- _report_progress(1.0, "Cancelled" if cancelled else "Complete")
- return ImageBatchResult(successes, failures, cancelled, manifest_path, zip_path)
+ finally:
+ if output_file is not None:
+ output_file.cleanup()
+ decoded = processed = render_rgba = None
+ if session is not None and not session.closed:
+ if controller.cancel.is_set():
+ session.abort()
+ else:
+ session.close()
+ finally:
+ if next_decode is not None:
+ next_decode.cancel()
+ if session is not None and not session.closed:
+ session.abort()
+ cancelled = controller.cancel.is_set()
+ if cancelled:
+ for item in reporter.items:
+ if item.state == "Queued":
+ failures.append(ImageConversionFailure(item.input_path, "Cancelled before rendering."))
+ reporter.skip_from(0)
+ manifest, zip_path = _build_manifest_and_zip(
+ stamp, options, successes, failures, cancelled, create_zip=create_zip and not cancelled,
+ output_dir=destination, batch_diagnostics=reporter.diagnostics(final=True),
+ controller=controller,
+ )
+ cancelled = controller.cancel.is_set()
+ reporter.finish(cancelled=cancelled, manifest_path=manifest)
+ return ImageBatchResult(successes, failures, cancelled, manifest, zip_path)
+ except BaseException as exc:
+ reporter.finish(cancelled=controller.cancel.is_set(), error=str(exc))
+ raise
diff --git a/src/image/encoder.py b/src/image/encoder.py
index fcd66f7..ec5abcd 100644
--- a/src/image/encoder.py
+++ b/src/image/encoder.py
@@ -3,6 +3,7 @@ from __future__ import annotations
import os
import threading
import time
+import warnings as python_warnings
from collections import OrderedDict
from pathlib import Path
@@ -10,6 +11,11 @@ import numpy as np
from PIL import Image
from .models import ImageConversionOptions
+from ..core.jobs import Cancelled
+from ..core.render_metadata import (
+ IMAGE_NOTE_FORMATS, MetadataNoteError, check_cancelled, embedding_warning,
+ merge_render_note, record_embedding,
+)
_PREVIEW_CACHE_LIMIT = 32
_preview_cache: OrderedDict[str, Image.Image] = OrderedDict()
@@ -52,10 +58,14 @@ def _encode_image(
rgba: np.ndarray,
options: ImageConversionOptions,
metadata: dict[str, object],
+ *, generate_preview: bool = True, preview_path: Path | None = None,
+ render_note: str | None = None, metadata_diagnostics: dict | None = None, controller=None,
) -> list[str]:
- warnings = save_image(output, rgba, options, metadata)
+ warnings = save_image(output, rgba, options, metadata, render_note=render_note,
+ metadata_diagnostics=metadata_diagnostics, controller=controller)
try:
- _remember_image_preview(output, rgba, options.output_format)
+ if generate_preview:
+ _remember_image_preview(preview_path or output, rgba, options.output_format)
except Exception:
# A UI convenience must never invalidate an otherwise correct output.
pass
@@ -79,6 +89,7 @@ def save_image(
rgba: np.ndarray,
options: ImageConversionOptions,
metadata: dict[str, object],
+ *, render_note: str | None = None, metadata_diagnostics: dict | None = None, controller=None,
) -> list[str]:
output_format = options.output_format
image = Image.fromarray(rgba, mode="RGBA")
@@ -106,11 +117,89 @@ def save_image(
args.update(quality=int(options.quality), speed=4)
temporary = output.with_name(f".{output.stem}.{time.time_ns()}{output.suffix}")
+ note_requested = render_note is not None and output_format in IMAGE_NOTE_FORMATS
+ original_args = args.copy()
+ description = None
try:
- image.save(temporary, format=output_format, **args)
+ check_cancelled(controller)
+ if note_requested:
+ try:
+ args, description = _add_render_note(args, render_note)
+ except (ValueError, TypeError, KeyError, SyntaxError, OSError) as exc:
+ if isinstance(exc, OSError) and exc.errno is not None:
+ raise
+ warnings.append(embedding_warning(metadata_diagnostics, exc))
+ note_requested = False
+ elif render_note is not None:
+ record_embedding(metadata_diagnostics, "skipped", reason="unsupported_format")
+ elif metadata_diagnostics is not None and not metadata_diagnostics:
+ record_embedding(metadata_diagnostics, "not_requested")
+
+ try:
+ check_cancelled(controller)
+ image.save(temporary, format=output_format, **args)
+ check_cancelled(controller)
+ # Note-free legacy calls retain their existing validation behavior.
+ if render_note is not None or metadata_diagnostics is not None:
+ _verify_image(temporary, image.size, description if note_requested else None)
+ except Cancelled:
+ raise
+ except (ValueError, TypeError, KeyError, SyntaxError, OSError, RuntimeError) as exc:
+ # Disk/permission errors and cancellation are not metadata failures.
+ if not note_requested or (isinstance(exc, OSError) and exc.errno is not None):
+ raise
+ check_cancelled(controller)
+ warnings.append(embedding_warning(metadata_diagnostics, exc))
+ image.save(temporary, format=output_format, **original_args)
+ check_cancelled(controller)
+ _verify_image(temporary, image.size, None)
+ note_requested = False
+ check_cancelled(controller)
os.replace(temporary, output)
+ if note_requested:
+ record_embedding(metadata_diagnostics, "embedded", field="EXIF.ImageDescription")
finally:
image.close()
if temporary.exists():
temporary.unlink()
return warnings
+
+
+def _add_render_note(args: dict, note: str) -> tuple[dict, str]:
+ # EXIF may contain nested IFDs; Pillow preserves these through its Exif API.
+ # Never alter the decoder's metadata object or its original byte payload.
+ exif = Image.Exif()
+ with python_warnings.catch_warnings(record=True) as caught:
+ python_warnings.simplefilter("always")
+ if args.get("exif"):
+ exif.load(args["exif"])
+ original_fields = _exif_fields(exif)
+ description = merge_render_note(exif.get(270), note)
+ exif[270] = description
+ encoded = exif.tobytes()
+ reread = Image.Exif()
+ reread.load(encoded)
+ if caught or reread.get(270) != description or _exif_fields(reread) != original_fields:
+ raise MetadataNoteError("EXIF description could not be preserved exactly")
+ return {**args, "exif": encoded}, description
+
+
+def _exif_fields(exif: Image.Exif) -> dict:
+ """Compare content, not relocated IFD offsets, before adding the note."""
+ fields = {tag: value for tag, value in exif.items() if tag not in {270, 34665, 34853}}
+ for tag in (34665, 34853):
+ if tag in exif:
+ nested = dict(exif.get_ifd(tag))
+ if tag == 34665 and 40965 in nested:
+ nested[40965] = dict(exif.get_ifd(40965))
+ fields[tag] = nested
+ return fields
+
+
+def _verify_image(path: Path, size: tuple[int, int], description: str | None) -> None:
+ with Image.open(path) as saved:
+ saved.load()
+ if saved.size != size:
+ raise MetadataNoteError("Saved image dimensions changed")
+ if description is not None and saved.getexif().get(270) != description:
+ raise MetadataNoteError("Saved image did not retain the settings note")
diff --git a/src/image/processor.py b/src/image/processor.py
index f266970..b1441f8 100644
--- a/src/image/processor.py
+++ b/src/image/processor.py
@@ -10,8 +10,10 @@ def convert_image(
input_path: str | os.PathLike[str],
options: ImageConversionOptions | None = None,
progress: Callable[[float, str], None] | None = None,
+ *, output_dir=None, controller=None, generate_previews: bool = True, create_zip: bool = True,
) -> ImageConversionResult:
- result = convert_images([input_path], options, progress)
+ result = convert_images([input_path], options, progress, output_dir=output_dir, controller=controller,
+ generate_previews=generate_previews, create_zip=create_zip)
if result.successes:
return result.successes[0]
if result.failures:
diff --git a/src/image/reports.py b/src/image/reports.py
index 249af01..40f586b 100644
--- a/src/image/reports.py
+++ b/src/image/reports.py
@@ -1,8 +1,6 @@
from __future__ import annotations
import json
-import os
-import time
import zipfile
from dataclasses import asdict, dataclass
from pathlib import Path
@@ -10,6 +8,8 @@ from pathlib import Path
from PIL import TiffImagePlugin
from ..core.paths import LOGS, OUTPUTS
+from ..core.disk_paths import OutputFile
+from ..core.jobs import Cancelled
from ..core.runtime import DLSSFrameSession, DLSS_MODEL_PRESETS
from .decoder import _DecodedImage
from .models import ImageConversionFailure, ImageConversionOptions, ImageConversionResult
@@ -47,6 +47,7 @@ def _write_report(
gpu: dict,
session: DLSSFrameSession,
evidence: dict[str, object],
+ *, metadata_diagnostics: dict | None = None,
) -> str:
report = {
"status": "success",
@@ -61,6 +62,7 @@ def _write_report(
if key not in {"icc_profile", "exif", "xmp"}
},
"warnings": result.warnings,
+ "metadata_embedding": metadata_diagnostics or {"status": "not_requested"},
"input_dimensions": {"width": result.input_width, "height": result.input_height},
"negotiated_render_dimensions": {"width": result.render_width, "height": result.render_height},
"negotiated_render_range": {
@@ -104,25 +106,44 @@ def _build_manifest_and_zip(
successes: list[ImageConversionResult],
failures: list[ImageConversionFailure],
cancelled: bool,
+ *, create_zip: bool = True, output_dir: Path | None = None, batch_diagnostics: dict | None = None,
+ controller=None,
) -> tuple[str, str | None]:
manifest = {
"status": "cancelled" if cancelled else ("partial" if failures else "success"),
"options": asdict(options),
"successes": [asdict(item) for item in successes],
"failures": [asdict(item) for item in failures],
+ "batch": batch_diagnostics or {},
+ "output_directory": str(output_dir or OUTPUTS),
}
manifest_path = LOGS / f"DLSS5_IMAGE_BATCH_{stamp}.manifest.json"
manifest_path.write_text(json.dumps(manifest, indent=2), encoding="utf-8")
- if not successes:
+ if not successes or not create_zip:
return str(manifest_path), None
- zip_path = OUTPUTS / f"DLSS5_IMAGE_BATCH_{stamp}.zip"
- temporary = zip_path.with_name(f".{zip_path.stem}.{time.time_ns()}.zip")
+ zip_path = (output_dir or OUTPUTS) / f"DLSS5_IMAGE_BATCH_{stamp}.zip"
+ output_file = OutputFile(zip_path)
try:
- with zipfile.ZipFile(temporary, "w", compression=zipfile.ZIP_STORED) as archive:
+ with zipfile.ZipFile(output_file.temporary, "w", compression=zipfile.ZIP_STORED) as archive:
for item in successes:
- archive.write(item.output_path, arcname=Path(item.output_path).name)
- os.replace(temporary, zip_path)
+ info = zipfile.ZipInfo.from_file(item.output_path, arcname=Path(item.output_path).name)
+ with open(item.output_path, "rb") as source, archive.open(info, "w", force_zip64=True) as target:
+ while True:
+ if controller is not None and controller.cancel.is_set():
+ raise Cancelled("ZIP creation cancelled.")
+ chunk = source.read(1024 * 1024)
+ if not chunk:
+ break
+ target.write(chunk)
+ if controller is not None and controller.cancel.is_set():
+ raise Cancelled("ZIP creation cancelled.")
+ output_file.publish()
+ except Cancelled:
+ manifest["status"] = "cancelled"
+ if batch_diagnostics:
+ manifest["batch"]["statistics"]["state"] = "Cancelled"
+ manifest_path.write_text(json.dumps(manifest, indent=2), encoding="utf-8")
+ return str(manifest_path), None
finally:
- if temporary.exists():
- temporary.unlink()
+ output_file.cleanup()
return str(manifest_path), str(zip_path)
diff --git a/src/image/ui.py b/src/image/ui.py
index 54c8172..62ea40a 100644
--- a/src/image/ui.py
+++ b/src/image/ui.py
@@ -5,9 +5,9 @@ from dataclasses import dataclass
from pathlib import Path
import gradio as gr
+from ..core.batch_ui import BATCH_HEADERS, bind_batch_ui, build_path_controls
from PIL import Image
-from ..core.jobs import cancel_active_job
from ..core.naming import RENAME_MODES
from ..core.runtime import DLSS_MODEL_PRESETS, NR_PRESETS, NR_STYLES, UPSCALING_MODES
from ..settings.models import AUTOMATIC_MASK_CHOICES, UISettings, automatic_mask_choice, parse_automatic_mask
@@ -116,6 +116,7 @@ def render_image_batch(
rename_mode: str,
custom_suffix: str,
progress=gr.Progress(track_tqdm=False),
+ *, output_dir=None, controller=None, on_item_update=None, direct_disk=False,
):
if not input_paths:
raise gr.Error("Choose at least one image first.")
@@ -142,13 +143,17 @@ def render_image_batch(
progress(value, desc=message)
try:
- result = convert_images(input_paths, options, progress=report)
+ result = convert_images(input_paths, options, progress=report, output_dir=output_dir,
+ controller=controller, on_item_update=on_item_update,
+ generate_previews=not direct_disk, create_zip=not direct_disk)
except Exception as exc:
traceback.print_exc()
+ if on_item_update is not None:
+ raise
return [], [], None, [], f"Failed: {exc}"
gallery = []
- for item in result.successes:
+ for item in ([] if direct_disk else result.successes):
preview = take_image_preview(item.output_path)
if preview is None:
with Image.open(item.output_path) as output:
@@ -156,7 +161,7 @@ def render_image_batch(
preview.thumbnail((1600, 1200), Image.Resampling.LANCZOS)
preview = preview.copy()
gallery.append((preview, Path(item.output_path).name))
- files = [item.output_path for item in result.successes]
+ files = [] if direct_disk else [item.output_path for item in result.successes]
rows = [
[Path(item.input_path).name, "Complete", Path(item.output_path).name, "; ".join(item.warnings)]
for item in result.successes
@@ -191,6 +196,9 @@ class ImageTab:
zip_download: object
status: object
results: object
+ input_path: object = None
+ output_path: object = None
+ job_state: object = None
@property
def render_inputs(self) -> list[object]:
@@ -209,9 +217,10 @@ def build_image_tab(settings: UISettings) -> ImageTab:
label="Input image(s)", file_count="multiple", file_types=upload_types,
type="filepath", allow_reordering=True, elem_id="image-upload-list",
)
+ input_path, output_path = build_path_controls()
input_gallery = gr.Gallery(
label="Input preview", columns=3, height=520, object_fit="contain",
- interactive=False, buttons=["fullscreen"], elem_id="image-input-preview",
+ interactive=False, visible=False, buttons=["fullscreen"], elem_id="image-input-preview",
)
with gr.Accordion("DLSS 5 Neural Rendering Settings", open=True):
neural = build_neural_controls(settings)
@@ -250,32 +259,23 @@ def build_image_tab(settings: UISettings) -> ImageTab:
elem_id="image-output-list",
)
zip_download = gr.DownloadButton("Download successful images as ZIP")
- status = gr.Textbox(label="Status", interactive=False)
+ status = gr.Textbox(label="Status", interactive=False, lines=5, max_lines=12)
results = gr.Dataframe(
- headers=["Input", "Result", "Output", "Details"],
- datatype=["str", "str", "str", "str"], interactive=False,
+ headers=BATCH_HEADERS,
+ datatype=["str"] * len(BATCH_HEADERS), interactive=False,
label="Batch results", wrap=True,
)
tab = ImageTab(
sources, input_gallery, neural, model_preset, output_format, quality, rename_mode,
custom_suffix, render, stop, reset, output_gallery, output_files, zip_download, status, results
)
+ tab.input_path, tab.output_path = input_path, output_path
bind_image_events(tab)
return tab
def bind_image_events(tab: ImageTab) -> None:
- tab.sources.change(
- preview_input_images, inputs=tab.sources, outputs=tab.input_gallery,
- queue=False, show_progress="hidden",
- )
+ bind_batch_ui(tab, render_image_batch, kind="image", preview_mode=preview_input_images)
tab.rename_mode.change(
rename_suffix_update, inputs=tab.rename_mode, outputs=tab.custom_suffix, queue=False,
)
- tab.render.click(
- render_image_batch, inputs=tab.render_inputs,
- outputs=[tab.output_gallery, tab.output_files, tab.zip_download, tab.results, tab.status],
- concurrency_limit=1, show_progress="full", show_progress_on=tab.output_gallery,
- )
- tab.stop.click(cancel_active_job, outputs=tab.status, queue=False)
-
diff --git a/src/live/__init__.py b/src/live/__init__.py
new file mode 100644
index 0000000..2905f8e
--- /dev/null
+++ b/src/live/__init__.py
@@ -0,0 +1,21 @@
+from .models import LiveOptions, LiveSessionInfo
+from .pipeline import (
+ is_live_running,
+ live_status,
+ start_live_session,
+ stop_live_session,
+ sweep_stale_live_dirs,
+)
+from .ui import LiveTab, build_live_tab
+
+__all__ = [
+ "LiveOptions",
+ "LiveSessionInfo",
+ "LiveTab",
+ "build_live_tab",
+ "is_live_running",
+ "live_status",
+ "start_live_session",
+ "stop_live_session",
+ "sweep_stale_live_dirs",
+]
diff --git a/src/live/effects.py b/src/live/effects.py
new file mode 100644
index 0000000..b18fdb4
--- /dev/null
+++ b/src/live/effects.py
@@ -0,0 +1,195 @@
+"""Debounced effect snapshots and a deadline confined to one native worker."""
+from __future__ import annotations
+
+import subprocess
+import threading
+import time
+from collections import deque
+from dataclasses import asdict, dataclass, fields
+
+from ..core.jobs import Cancelled, JobController
+from ..core.runtime import resolve_native_settings
+
+EFFECT_DEBOUNCE_SECONDS = 0.5
+EFFECT_REPLACEMENT_TIMEOUT = 15.0
+
+
+@dataclass(frozen=True, slots=True)
+class EffectSettings:
+ nr_preset: str
+ nr_style: str
+ nr_intensity: float
+ local_tone_strength: float
+ local_structure_strength: float
+ skin_structure_strength: float
+ automatic_mask: bool
+ dlss_model_preset: str
+
+ @classmethod
+ def from_options(cls, options) -> EffectSettings:
+ result = cls(**{field.name: getattr(options, field.name) for field in fields(cls)})
+ resolve_native_settings(result)
+ return result
+
+
+@dataclass(frozen=True, slots=True)
+class EffectRequest:
+ revision: int
+ settings: EffectSettings
+ requested_at: float
+ due_at: float
+
+
+class EffectUpdates:
+ """One pending update per session; no timer thread or FIFO of slider edits."""
+
+ def __init__(self, options, *, clock=time.monotonic) -> None:
+ self.initial = self.applied = self.requested = EffectSettings.from_options(options)
+ self._clock = clock
+ self._lock = threading.Lock()
+ self._revision = self.applied_revision = 0
+ self._pending: EffectRequest | None = None
+ self._applying: EffectRequest | None = None
+ self._accepting = True
+ self._status = "Initial settings"
+ self._error = ""
+ self._applied_at_pts: int | None = None
+ self._successes = self._failures = 0
+ self._history: deque[dict] = deque(maxlen=128)
+
+ def submit(self, options) -> bool:
+ settings = EffectSettings.from_options(options)
+ with self._lock:
+ if settings == self.requested:
+ return False
+ self.requested = settings
+ if not self._accepting:
+ self._status = "Processing finished or stopped; changes apply on the next Start."
+ return False
+ self._revision += 1
+ if settings == self.applied and self._applying is None:
+ self._pending = None
+ return False
+ now = self._clock()
+ self._pending = EffectRequest(self._revision, settings, now, now + EFFECT_DEBOUNCE_SECONDS)
+ return True
+
+ def take_due(self) -> EffectRequest | None:
+ with self._lock:
+ if (not self._accepting or self._applying is not None or self._pending is None
+ or self._clock() < self._pending.due_at):
+ return None
+ self._applying, self._pending = self._pending, None
+ return self._applying
+
+ def complete(self, request: EffectRequest, *, pts: int, milliseconds: float,
+ error: str = "", restored: bool = True, worker_pid: int | None = None) -> None:
+ with self._lock:
+ changed = [field.name for field in fields(EffectSettings)
+ if getattr(self.applied, field.name) != getattr(request.settings, field.name)]
+ self._history.append({"revision": request.revision, "settings": asdict(request.settings),
+ "changed": changed, "pts": pts, "refresh_ms": milliseconds,
+ "request_to_frame_ms": (self._clock() - request.requested_at) * 1000,
+ "error": error, "restored": bool(error) and restored, "worker_pid": worker_pid})
+ self._applying = None
+ self._error = error
+ if error:
+ self._failures += 1
+ self._status = ("Update failed—previous settings restored" if restored
+ else "Update failed; recovery failed")
+ else:
+ self.applied = request.settings
+ self.applied_revision = request.revision
+ self._applied_at_pts = pts
+ self._successes += 1
+ self._status = "Applied to processing"
+ # An edit back to A while A->B was failing needs no second restart.
+ if self._pending and self._pending.settings == self.applied:
+ self._pending = None
+
+ def finish(self) -> None:
+ with self._lock:
+ self._accepting = False
+ if self._pending or self._applying:
+ self._status = "Processing finished or stopped; changes apply on the next Start."
+ self._pending = None
+
+ def snapshot(self) -> dict:
+ with self._lock:
+ status = self._status
+ if self._accepting and self._applying:
+ status = "Applying" + ("; newer changes pending" if self._pending else "")
+ elif self._accepting and self._pending:
+ status = "Pending"
+ return {"effects_status": status, "effects_error": self._error,
+ "pending_revision": self._pending.revision if self._pending else None,
+ "applied_revision": self.applied_revision, "applied_at_pts": self._applied_at_pts,
+ "effect_updates": self._successes, "effect_update_failures": self._failures}
+
+ def report(self) -> dict:
+ with self._lock:
+ return {"initial": asdict(self.initial), "applied": asdict(self.applied),
+ "requested": asdict(self.requested), "requests": self._revision,
+ "applied_count": self._successes, "failed_count": self._failures,
+ "history": list(self._history)}
+
+
+class NativeDeadline(JobController):
+ """Kill only this replacement on timeout; Stop still cancels the whole job.
+
+ The returned worker keeps using this controller after the timer is disarmed.
+ Its processes remain registered with the parent until normal native cleanup.
+ """
+
+ def __init__(self, parent: JobController, seconds: float) -> None:
+ super().__init__()
+ self.parent = parent
+ self.cancel = parent.cancel
+ self.seconds = seconds
+ self.expired = threading.Event()
+ self._timer = threading.Timer(seconds, self._expire)
+ self._timer.daemon = True
+
+ def register(self, process: subprocess.Popen) -> None:
+ super().register(process)
+ self.parent.register(process)
+ if self.expired.is_set() and process.poll() is None:
+ process.kill()
+
+ def unregister(self, process: subprocess.Popen) -> None:
+ super().unregister(process)
+ self.parent.unregister(process)
+
+ def _expire(self) -> None:
+ self.expired.set()
+ with self._lock:
+ processes = list(self._processes)
+ for process in processes:
+ if process.poll() is None:
+ try:
+ process.kill()
+ except OSError:
+ pass
+
+ def __enter__(self) -> NativeDeadline:
+ if self.cancel.is_set():
+ raise Cancelled("Live stopped.")
+ self._timer.start()
+ return self
+
+ def __exit__(self, exc_type, exc, traceback) -> None:
+ self._timer.cancel()
+ self._timer.join()
+ if exc_type or self.expired.is_set() or self.cancel.is_set():
+ # Also cover constructors that failed before returning an object.
+ with self._lock:
+ processes = list(self._processes)
+ for process in processes:
+ if process.poll() is None:
+ process.kill()
+ process.wait(timeout=5)
+ self.unregister(process)
+ if self.cancel.is_set():
+ raise Cancelled("Live stopped.") from exc
+ if self.expired.is_set():
+ raise TimeoutError(f"DLSS initialization and verification exceeded {self.seconds:g} seconds.") from exc
diff --git a/src/live/hls_server.py b/src/live/hls_server.py
new file mode 100644
index 0000000..211f4e3
--- /dev/null
+++ b/src/live/hls_server.py
@@ -0,0 +1,73 @@
+from __future__ import annotations
+
+import threading
+from functools import partial
+from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
+from pathlib import Path
+
+
+class _NoCacheHandler(SimpleHTTPRequestHandler):
+ """Serve a Live HLS dir on loopback; never cache playlists."""
+
+ def __init__(self, *args, directory: str, **kwargs) -> None:
+ super().__init__(*args, directory=directory, **kwargs)
+
+ def end_headers(self) -> None:
+ if self.path.split("?")[0].endswith(".m3u8"):
+ self.send_header("Cache-Control", "no-cache, no-store, must-revalidate")
+ self.send_header("Pragma", "no-cache")
+ self.send_header("Access-Control-Allow-Origin", "*")
+ super().end_headers()
+
+ def log_message(self, *args) -> None: # noqa: ANN002, ANN003 - quiet by design
+ return
+
+ def handle(self) -> None:
+ try:
+ super().handle()
+ except (ConnectionResetError, ConnectionAbortedError, BrokenPipeError):
+ # Players routinely cancel segment prefetches when seeking/exiting.
+ pass
+
+ def do_GET(self) -> None:
+ if self.path.split("?")[0].endswith(".tmp"):
+ self.send_error(404)
+ return
+ super().do_GET()
+
+
+class HlsServer:
+ """Loopback HTTP server for one Live session directory."""
+
+ def __init__(self, directory: Path) -> None:
+ self.directory = directory
+ handler = partial(_NoCacheHandler, directory=str(directory))
+ self._server = ThreadingHTTPServer(("127.0.0.1", 0), handler)
+ self._server.daemon_threads = True
+ self._thread = threading.Thread(
+ target=self._server.serve_forever,
+ kwargs={"poll_interval": 0.2},
+ daemon=True,
+ name="dlss5-live-hls",
+ )
+
+ @property
+ def port(self) -> int:
+ return int(self._server.server_address[1])
+
+ def playlist_url(self, name: str = "index.m3u8") -> str:
+ return f"http://127.0.0.1:{self.port}/{name}"
+
+ def start(self) -> None:
+ self._thread.start()
+
+ def stop(self) -> None:
+ try:
+ self._server.shutdown()
+ except Exception:
+ pass
+ try:
+ self._server.server_close()
+ except Exception:
+ pass
+ self._thread.join(timeout=5)
diff --git a/src/live/models.py b/src/live/models.py
new file mode 100644
index 0000000..46d2a28
--- /dev/null
+++ b/src/live/models.py
@@ -0,0 +1,99 @@
+from __future__ import annotations
+
+from dataclasses import dataclass, field
+
+
+LIVE_MAX_HEIGHTS = (480, 720, 1080, 1440, 2160)
+LIVE_MAX_HEIGHT_CHOICES = tuple(str(height) for height in LIVE_MAX_HEIGHTS)
+LIVE_SOURCE_QUALITY_CHOICES = ("Auto", *LIVE_MAX_HEIGHT_CHOICES)
+LIVE_SEGMENT_CHOICES = ("1", "2", "4")
+LIVE_FPS_CHOICES = ("Auto", "Source", "60", "30", "24")
+LIVE_GUIDE_CHOICES = ("Fast", "Quality")
+
+# Frame count reported to the native worker for endless sessions
+# (the worker treats it as informational).
+LIVE_FRAME_COUNT = 1_000_000_000
+
+
+@dataclass(slots=True)
+class LiveOptions:
+ """Session-scoped Live settings (never persisted to config.ini)."""
+
+ source: str = ""
+ max_height: int = 720
+ # Shared DLSS values (mirrored with the Image/Video tabs, persisted
+ # globally); effects can update during playback, sizing stays fixed.
+ nr_preset: str = "Default"
+ nr_style: str = "Default"
+ nr_intensity: float = 1.0
+ local_tone_strength: float = 1.0
+ local_structure_strength: float = 1.0
+ skin_structure_strength: float = -1.0
+ automatic_mask: bool = False
+ dlss_model_preset: str = "Default"
+ # Speed-first default: 720p in -> 1080p out keeps the pipe/NVENC cheap
+ # on mid-range GPUs (2x/3x cost ~2x the output bytes per frame).
+ upscaling_factor: float = 1.5
+ segment_seconds: int = 2
+ target_fps: str = "Auto"
+ buffer_seconds: float = 6.0
+ guide_quality: str = "Fast"
+ queue_frames: int = 3
+ network_timeout: float = 20.0
+ open_mpv: bool = True
+ mpv_args: tuple[str, ...] = ()
+ # Debug/diagnostics: keep the session HLS dir instead of sweeping it.
+ keep_files: bool = False
+ source_quality: str = "Auto"
+
+
+@dataclass(slots=True)
+class ResolvedSource:
+ kind: str # "file" | "direct" | "youtube" | "twitch"
+ title: str
+ video_url: str
+ audio_url: str | None
+ is_live: bool
+ video_headers: dict[str, str] = field(default_factory=dict)
+ audio_headers: dict[str, str] = field(default_factory=dict)
+
+
+@dataclass(slots=True)
+class LiveSessionInfo:
+ running: bool = False
+ processing: bool = False
+ status: str = "Idle."
+ playlist_url: str = ""
+ segments: int = 0
+ processed_frames: int = 0
+ dropped_frames: int = 0
+ source_frames: int = 0
+ sampled_frames: int = 0
+ source_fps: float = 0.0
+ input_size: str = ""
+ output_size: str = ""
+ encoder: str = ""
+ target_fps: float = 0.0
+ media_seconds: float = 0.0
+ buffer_seconds: float = 0.0
+ guide_ms: float = 0.0
+ dlss_ms: float = 0.0
+ encode_ms: float = 0.0
+ report_path: str = ""
+ effective_fps: float = 0.0
+ elapsed_seconds: float = 0.0
+ mpv_running: bool = False
+ player_dropped_frames: int = 0
+ rebuffer_events: int = 0
+ av_sync_ms: float = 0.0
+ failures: list[str] = field(default_factory=list)
+ source_size: str = ""
+ source_quality: str = "Auto"
+ source_quality_note: str = ""
+ effects_status: str = "Initial settings"
+ effects_error: str = ""
+ pending_revision: int | None = None
+ applied_revision: int = 0
+ applied_at_pts: int | None = None
+ effect_updates: int = 0
+ effect_update_failures: int = 0
diff --git a/src/live/pipeline.py b/src/live/pipeline.py
new file mode 100644
index 0000000..2f54765
--- /dev/null
+++ b/src/live/pipeline.py
@@ -0,0 +1,768 @@
+from __future__ import annotations
+
+import atexit
+import json
+import math
+import os
+import queue
+import re
+import shutil
+import subprocess
+import threading
+import time
+from dataclasses import asdict, replace
+from pathlib import Path
+
+import av
+import numpy as np
+
+from ..core.dlss_architecture import apply_current_dlss_architecture
+from ..core.gpu_selection import resolve_runtime_ai_gpu
+from ..core.jobs import BoundedLogBuffer, Cancelled, JobController, active_job, drain_bounded_text
+from ..core.paths import FFMPEG, LIVE_DIR, LOGS
+from ..core.runtime import (DLSSFrameSession, prepare_runtime, resolve_native_settings,
+ resolve_output_size, resolve_upscaling_mode, verify_feature_18)
+from ..settings.storage import processing_gpu_settings
+from ..video.guides import TemporalGuideGenerator
+from .effects import EffectSettings, EffectUpdates, NativeDeadline, EFFECT_REPLACEMENT_TIMEOUT
+from .hls_server import HlsServer
+from .models import (LIVE_FRAME_COUNT, LIVE_FPS_CHOICES, LIVE_GUIDE_CHOICES,
+ LIVE_MAX_HEIGHTS, LIVE_SOURCE_QUALITY_CHOICES, LiveOptions, LiveSessionInfo, ResolvedSource)
+from .player import check_live_binaries, launch_mpv
+from .source_resolver import _classify, input_args, probe_source, resolve_source, run_capture
+from .transport import TIME_BASE, AdaptiveRate, PipeReader, TimestampMuxer, VideoFrame, get, put
+
+_LOCK = threading.Lock()
+_CURRENT: LiveSession | None = None
+_LAST: LiveSessionInfo | None = None
+
+
+def _fit_height(width: int, height: int, max_height: int) -> tuple[int, int]:
+ scale = min(1.0, max_height / height)
+ return max(2, round(width * scale / 2) * 2), max(2, round(height * scale / 2) * 2)
+
+
+def validate_options(options: LiveOptions) -> None:
+ if not options.source.strip():
+ raise ValueError("Enter a video file or a stream URL.")
+ if options.max_height not in LIVE_MAX_HEIGHTS:
+ raise ValueError(f"Max input height must be one of: {', '.join(map(str, LIVE_MAX_HEIGHTS))}.")
+ if options.source_quality not in LIVE_SOURCE_QUALITY_CHOICES:
+ raise ValueError("Choose a valid Source quality.")
+ if options.target_fps not in LIVE_FPS_CHOICES or options.guide_quality not in LIVE_GUIDE_CHOICES:
+ raise ValueError("Choose a valid frame rate and motion guide quality.")
+ if options.segment_seconds not in (1, 2, 4, 6):
+ raise ValueError("Segment length must be 1, 2, 4 or 6 seconds.")
+ if not math.isfinite(options.buffer_seconds) or not 2 <= options.buffer_seconds <= 30:
+ raise ValueError("Playback buffer must be between 2 and 30 seconds.")
+ if not 1 <= options.queue_frames <= 8 or not 5 <= options.network_timeout <= 60:
+ raise ValueError("Invalid Live queue size or network timeout.")
+ resolve_upscaling_mode(options.upscaling_factor)
+ resolve_native_settings(options)
+
+
+def _redact(text: str) -> str:
+ # Keep diagnostic errors, never store signed CDN query tokens.
+ return re.sub(r"(https?://[^\s?]+)\?[^\s'\"]+", r"\1?<redacted>", text)
+
+
+class LiveSession(threading.Thread):
+ """Bounded decode/guide -> DLSS -> timestamped encode -> buffered playback."""
+
+ def __init__(self, options: LiveOptions) -> None:
+ super().__init__(daemon=True, name="dlss5-live-session")
+ self.options = replace(options)
+ self.effects = EffectUpdates(options)
+ self.controller = JobController()
+ self.info = LiveSessionInfo(running=True, processing=True, status="Starting...")
+ self._info_lock = threading.Lock()
+ self._ready = threading.Event()
+ self._produced = threading.Event()
+ self._mpv: subprocess.Popen | None = None
+ self._session_dir: Path | None = None
+ self._player_attempted = False
+ self._player_state: dict = {}
+ self._playback_started: float | None = None
+ self._last_ui = 0.0
+ self._last_progress = time.monotonic()
+ self._error: str | None = None
+ self._error_lock = threading.Lock()
+ self._logs: dict[str, BoundedLogBuffer] = {}
+ self._log_threads: list[threading.Thread] = []
+ self._rate: AdaptiveRate | None = None
+ self._native_logs: list[str] = []
+ self._feature_evidence: dict = {}
+ self._title = ""
+ self._published_seconds = 0.0
+ self._published_sequence = -1
+ self._segment_count = 0
+ self._first_frame_at: float | None = None
+ self._finished = False
+ self._started_at = time.monotonic()
+
+ def _set(self, **fields) -> None:
+ with self._info_lock:
+ for key, value in fields.items():
+ setattr(self.info, key, value)
+
+ def snapshot(self) -> LiveSessionInfo:
+ effects = self.effects.snapshot()
+ with self._info_lock:
+ return replace(self.info, failures=list(self.info.failures), **effects)
+
+ def request_effects(self, settings) -> bool:
+ return self.effects.submit(settings)
+
+ def stop(self) -> None:
+ self.effects.finish()
+ if self.info.processing:
+ self._set(status="Stopping...")
+ self.controller.stop()
+
+ def _stage_error(self, stage: str, exc: Exception) -> None:
+ with self._error_lock:
+ if self._error is None and not self.controller.cancel.is_set():
+ self._error = _redact(f"{stage}: {exc}")
+ self.controller.stop()
+
+ def _spawn(self, name: str, command: list[str], **kwargs) -> subprocess.Popen:
+ process = subprocess.Popen(command, stderr=subprocess.PIPE,
+ creationflags=getattr(subprocess, "CREATE_NO_WINDOW", 0), **kwargs)
+ self.controller.register(process)
+ log = self._logs[name] = BoundedLogBuffer(max_tail=80)
+ thread = threading.Thread(target=drain_bounded_text, args=(process.stderr, log), daemon=True)
+ thread.start()
+ self._log_threads.append(thread)
+ return process
+
+ def _process_error(self, name: str, process: subprocess.Popen) -> RuntimeError:
+ detail = "\n".join(self._logs[name].snapshot()[-12:])
+ return RuntimeError(_redact(f"{name} exited with code {process.returncode}.\n{detail}"))
+
+ def _remember_native(self, native) -> None:
+ self._native_logs = [*self._native_logs, *native.worker_logs][-500:]
+
+ def _verified_effect_worker(self, settings: EffectSettings, native_args: dict,
+ item: VideoFrame, index: int):
+ native = None
+ try:
+ with NativeDeadline(self.controller, EFFECT_REPLACEMENT_TIMEOUT) as deadline:
+ native = DLSSFrameSession(**native_args, controller=deadline,
+ native_settings=resolve_native_settings(settings))
+ start = time.perf_counter()
+ output, pts = native.process(index=index, rgba=item.rgba, motion=item.motion,
+ reset=True, pts=item.pts)
+ cost = time.perf_counter() - start
+ if pts != item.pts:
+ raise RuntimeError("Replacement DLSS worker returned a different frame timestamp.")
+ evidence = verify_feature_18(native.worker_logs, native.reshade_log_text())
+ self._feature_evidence = {k: v for k, v in evidence.items() if k != "reshade_log"}
+ return native, output, cost
+ except Exception:
+ if native is not None:
+ self._remember_native(native)
+ native.abort()
+ raise
+
+ def _replace_effects(self, native, request, native_args: dict, item: VideoFrame, index: int):
+ start = time.perf_counter()
+ self._remember_native(native)
+ native.abort() # release resources/config/log ownership before starting another
+ error = ""
+ try:
+ replacement, output, cost = self._verified_effect_worker(request.settings, native_args, item, index)
+ except Cancelled:
+ raise
+ except Exception as exc:
+ if self.controller.cancel.is_set():
+ raise Cancelled("Live stopped.") from exc
+ error = _redact(str(exc))
+ try:
+ replacement, output, cost = self._verified_effect_worker(self.effects.applied, native_args, item, index)
+ except Cancelled:
+ raise
+ except Exception as recovery:
+ detail = f"{error}; recovery failed: {_redact(str(recovery))}"
+ self.effects.complete(request, pts=item.pts, milliseconds=(time.perf_counter()-start)*1000,
+ error=detail, restored=False)
+ raise RuntimeError(detail) from recovery
+ self.effects.complete(request, pts=item.pts, milliseconds=(time.perf_counter()-start)*1000,
+ error=error, worker_pid=getattr(getattr(replacement, "worker", None), "pid", None))
+ self._rate.reset_measurements()
+ self._last_progress = time.monotonic()
+ self._write_report()
+ return replacement, output, cost
+
+ def _select_encoder(self, gpus: tuple, selected_uuid: str, width: int, height: int) -> tuple[bool, int | None]:
+ candidates = [g for g in gpus if g.get("cuda_ordinal") is not None
+ and (selected_uuid == "auto" or g.get("uuid") == selected_uuid)]
+ for gpu in candidates:
+ ordinal = int(gpu["cuda_ordinal"])
+ try:
+ result = run_capture([str(FFMPEG), "-v", "error", "-f", "lavfi", "-i",
+ f"color=size={width}x{height}:rate=1", "-frames:v", "1", "-c:v", "h264_nvenc",
+ "-gpu", str(ordinal), "-f", "null", "-"], self.controller, 15)
+ if result.returncode == 0:
+ return True, ordinal
+ except Cancelled:
+ raise
+ except RuntimeError:
+ continue
+ # Live has always allowed a CPU fallback. Probe it via the real encoder
+ # startup, and include its cost in automatic cadence selection.
+ return False, None
+
+ def _decoder_command(self, source: ResolvedSource, width: int, height: int, rate) -> list[str]:
+ opts = self.options
+ command = [str(FFMPEG), "-hide_banner", "-loglevel", "warning", "-nostdin", "-filter_threads", "2"]
+ # HLS broadcasts can jump clocks at reconnects/ad boundaries. Let
+ # FFmpeg repair those jumps for both streams before preserving the
+ # resulting timeline through DLSS. copyts disables that repair.
+ command += (["-dts_delta_threshold", "2"] if source.is_live
+ else ["-copyts", "-start_at_zero"])
+ command += [*input_args(source.video_url, source.video_headers, opts.network_timeout),
+ "-thread_queue_size", "32", "-i", source.video_url]
+ audio_input = "0"
+ if source.audio_url and source.audio_url != source.video_url:
+ command += [*input_args(source.audio_url, source.audio_headers, opts.network_timeout),
+ "-thread_queue_size", "32", "-i", source.audio_url]
+ audio_input = "1"
+ # FFmpeg autorotates once. Explicit transpose here used to rotate twice.
+ filters = [] if opts.target_fps == "Source" else [f"fps={rate}"]
+ filters += [f"scale={width}:{height}:flags=fast_bilinear", "setsar=1"]
+ command += ["-map", "0:v:0", "-map", f"{audio_input}:a:0?", "-sn", "-dn",
+ "-vf", ",".join(filters),
+ "-c:v", "rawvideo", "-pix_fmt", "rgba", "-threads:v", "1",
+ "-c:a", "pcm_s16le", "-ar", "48000", "-ac", "2",
+ "-af", "aresample=async=1:first_pts=0",
+ "-fps_mode", "passthrough", "-f", "nut", "-write_index", "0", "pipe:1"]
+ return command
+
+ def _encoder_command(self, rate, gpu: int | None, nvenc: bool) -> list[str]:
+ opts = self.options
+ if nvenc:
+ video = ["-c:v", "h264_nvenc"]
+ if gpu is not None:
+ video += ["-gpu", str(gpu)]
+ video += ["-preset", "p1", "-tune", "ll", "-rc", "vbr", "-cq", "23", "-b:v", "0",
+ "-rc-lookahead", "0", "-zerolatency", "1", "-bf", "0", "-forced-idr", "1"]
+ else:
+ video = ["-c:v", "libx264", "-preset", "ultrafast", "-tune", "zerolatency", "-crf", "23",
+ "-sc_threshold", "0"]
+ # Timestamps from NUT survive frame selection. There is no -r or CFR
+ # conversion after DLSS to squeeze skipped source time out of the video.
+ return [str(FFMPEG), "-hide_banner", "-loglevel", "warning", "-nostdin", "-y",
+ "-filter_threads", "2", "-probesize", "32768", "-analyzeduration", "0",
+ "-f", "nut", "-i", "pipe:0", "-map", "0:v:0", "-map", "0:a:0?",
+ *video, "-pix_fmt", "yuv420p", "-fps_mode", "passthrough", "-enc_time_base", "1:90000",
+ "-g", str(max(1, math.ceil(float(rate) * opts.segment_seconds))),
+ "-force_key_frames", f"expr:gte(t,n_forced*{opts.segment_seconds})",
+ "-c:a", "aac", "-b:a", "160k", "-max_interleave_delta", "1000000",
+ "-f", "hls", "-hls_time", str(opts.segment_seconds),
+ "-hls_list_size", str(max(12, math.ceil((opts.buffer_seconds + 60) / opts.segment_seconds))),
+ "-hls_delete_threshold", "6", "-hls_flags", "delete_segments+independent_segments+temp_file",
+ "-hls_segment_filename", "seg%07d.ts", "index.m3u8"]
+
+ def _decode(self, decoder: subprocess.Popen, prepared: queue.Queue, width: int, height: int) -> None:
+ assert self._rate is not None
+ rate = self._rate
+ try:
+ with av.open(PipeReader(decoder.stdout), format="nut", options={"probesize": "32768", "analyzeduration": "0"}) as container:
+ audio = next(iter(container.streams.audio), None)
+ put(prepared, audio, self.controller.cancel) # stream header (None means silent)
+ guides = TemporalGuideGenerator(width, height,
+ flow_width=320 if self.options.guide_quality == "Fast" else 640)
+ index = 0
+ last_pts: int | None = None
+ sampled = 0
+ for packet in container.demux():
+ if self.controller.cancel.is_set():
+ raise Cancelled("Live stopped.")
+ if not packet.size:
+ continue
+ self._last_progress = time.monotonic()
+ if packet.stream.type == "audio":
+ put(prepared, packet, self.controller.cancel)
+ continue
+ if packet.stream.type != "video":
+ continue
+ pts = round(packet.pts * packet.time_base / TIME_BASE)
+ current = index
+ index += 1
+ self._set(source_frames=index)
+ if not rate.accepts(current):
+ sampled += 1
+ self._set(sampled_frames=sampled)
+ continue
+ if packet.size != width * height * 4:
+ raise RuntimeError("Decoder returned an incomplete RGBA frame.")
+ rgba = np.frombuffer(packet, np.uint8).reshape(height, width, 4)
+ start = time.perf_counter()
+ guide = guides.process(rgba)
+ guide_ms = (time.perf_counter() - start) * 1000
+ old = self.snapshot().guide_ms
+ self._set(guide_ms=guide_ms if not old else old * 0.9 + guide_ms * 0.1)
+ # Temporal history spans the selected cadence, but a real
+ # timestamp discontinuity must reset both guide and DLSS.
+ discontinuity = last_pts is not None and (pts <= last_pts or (pts - last_pts) * TIME_BASE > 0.5)
+ if discontinuity:
+ guides.previous_gray = None
+ duration = (max(1, round(packet.duration * packet.time_base / TIME_BASE))
+ if self.options.target_fps == "Source" and packet.duration
+ else max(1, round(1 / rate.fps / TIME_BASE)))
+ put(prepared, VideoFrame(current, pts, duration, rgba, guide.motion,
+ guide.reset or discontinuity), self.controller.cancel)
+ last_pts = pts
+ code = decoder.wait(timeout=5)
+ if code:
+ raise self._process_error("decoder", decoder)
+ if not index:
+ raise RuntimeError("The decoder produced no video frames.")
+ put(prepared, None, self.controller.cancel)
+ except Cancelled:
+ pass
+ except Exception as exc:
+ self._stage_error("Decode / motion guides", exc)
+
+ def _encode(self, encoder: subprocess.Popen, outgoing: queue.Queue, width: int, height: int) -> None:
+ muxer = None
+ try:
+ audio = get(outgoing, self.controller.cancel)
+ muxer = TimestampMuxer(encoder.stdin, width, height, self._rate.rate, audio)
+ while True:
+ item = get(outgoing, self.controller.cancel)
+ if item is None:
+ break
+ start = time.perf_counter()
+ muxer.write(item)
+ if isinstance(item, VideoFrame):
+ cost = (time.perf_counter() - start) * 1000
+ old = self.snapshot().encode_ms
+ self._set(encode_ms=cost if not old else old * 0.9 + cost * 0.1,
+ media_seconds=float((item.pts + item.duration) * TIME_BASE))
+ self._last_progress = time.monotonic()
+ muxer.close()
+ muxer = None
+ encoder.stdin.close()
+ code = encoder.wait(timeout=30)
+ if code:
+ raise self._process_error("encoder", encoder)
+ except Cancelled:
+ pass
+ except Exception as exc:
+ if encoder.poll() is not None and not self.controller.cancel.is_set():
+ exc = self._process_error("encoder", encoder)
+ self._stage_error("Encode / audio mux", exc)
+ finally:
+ if muxer:
+ try:
+ muxer.close()
+ except Exception:
+ pass
+
+ def _playlist(self) -> tuple[int, float]:
+ try:
+ lines = (self._session_dir / "index.m3u8").read_text(encoding="utf-8").splitlines()
+ except OSError:
+ return 0, 0.0
+ sequence = 0
+ duration = 0.0
+ count = 0
+ for line in lines:
+ if line.startswith("#EXT-X-MEDIA-SEQUENCE:"):
+ sequence = int(line.split(":")[1])
+ elif line.startswith("#EXTINF:"):
+ duration = float(line.split(":")[1].split(",")[0])
+ elif line and not line.startswith("#"):
+ if sequence > self._published_sequence:
+ self._published_seconds += duration
+ self._published_sequence = sequence
+ self._segment_count += 1
+ count += 1
+ sequence += 1
+ return count, self._published_seconds
+
+ def _update_playback(self, *, finished: bool = False) -> None:
+ now = time.monotonic()
+ if now - self._last_ui < 0.25 and not finished:
+ return
+ self._last_ui = now
+ count, published = self._playlist()
+ if self._mpv:
+ try:
+ self._player_state = json.loads((self._session_dir / "player.json").read_text(encoding="utf-8"))
+ except (OSError, ValueError):
+ pass
+ code = self._mpv.poll()
+ self._set(mpv_running=code is None)
+ self._set(player_dropped_frames=int(self._player_state.get("dropped", 0)) +
+ int(self._player_state.get("decoder_dropped", 0)),
+ rebuffer_events=int(self._player_state.get("stalls", 0)),
+ av_sync_ms=float(self._player_state.get("avsync", 0)) * 1000)
+ if code is not None and not finished:
+ if code:
+ raise RuntimeError(f"MPV exited with code {code}.")
+ self.stop()
+ return
+ ready = count and (published >= self.options.buffer_seconds or finished)
+ if ready and not self._player_attempted:
+ self._player_attempted = True
+ self._playback_started = now
+ if self.options.open_mpv:
+ self._mpv = launch_mpv(self.info.playlist_url, self._title, self.options.mpv_args,
+ buffer_seconds=min(self.options.buffer_seconds, published),
+ state_path=self._session_dir / "player.json")
+ self._set(mpv_running=True)
+ info = self.snapshot()
+ position = (float(self._player_state.get("position", 0)) if self._mpv
+ else max(0, now - self._playback_started) if self._playback_started else 0)
+ buffer = max(0, published - position)
+ status = (f"Playing: {self._title}" if self._player_attempted else f"Buffering: {self._title}")
+ if finished:
+ status = f"Finished processing: {self._title}. Playback available until Stop."
+ status += (f"\n{info.target_fps:.2f} fps target | {info.effective_fps:.1f} fps processing | "
+ f"{info.processed_frames} enhanced | {info.sampled_frames} sampled out | {buffer:.1f}s buffered")
+ self._set(status=status, segments=self._segment_count, buffer_seconds=buffer,
+ elapsed_seconds=now - self._started_at)
+
+ def _pace(self, pts: int) -> float:
+ # Compress time only in the processing schedule, never in media PTS.
+ # Keep finite videos from racing through/deleting their HLS window.
+ start = time.perf_counter()
+ while self._playback_started is not None and not self.controller.cancel.is_set():
+ position = (float(self._player_state.get("position", 0)) if self._mpv
+ else time.monotonic() - self._playback_started)
+ if pts * TIME_BASE - position <= self.options.buffer_seconds + 2 * self.options.segment_seconds:
+ break
+ self._update_playback()
+ self.controller.cancel.wait(0.05)
+ return time.perf_counter() - start
+
+ def _watchdog(self, done: threading.Event) -> None:
+ while not done.wait(0.5):
+ if self.controller.cancel.is_set():
+ return
+ if self._playback_started and (self._player_state.get("paused") or self._player_state.get("buffering")):
+ # Pausing the player intentionally backpressures the pipeline.
+ # Only explicit pause excuses inactivity; rebuffering doesn't.
+ if self._player_state.get("paused"):
+ self._last_progress = time.monotonic()
+ if time.monotonic() - self._last_progress > max(45, self.options.network_timeout * 2):
+ self._stage_error("Live stalled", RuntimeError("No pipeline progress. Check the source/network and try again."))
+ return
+
+ def _produce(self) -> None:
+ options = self.options
+ self._set(status="Resolving source...")
+ resolved = resolve_source(options.source, options.max_height, self.controller,
+ source_quality=options.source_quality)
+ self._title = resolved.title
+ self._set(status=f"Probing {resolved.title}...")
+ metadata = probe_source(resolved, self.controller, options.network_timeout)
+ source_size = f"{metadata['width']}x{metadata['height']}"
+ source_limit = options.max_height if options.source_quality == "Auto" else int(options.source_quality)
+ source_note = (f"Requested up to {source_limit}p; received {source_size}."
+ if resolved.kind in ("youtube", "twitch") else
+ "Source quality selection applies to YouTube/Twitch pages; using the supplied source.")
+ if resolved.kind in ("youtube", "twitch") and metadata["height"] != source_limit:
+ source_note += " The resolved stream differs from the requested height."
+ self._set(source_size=source_size, source_quality=options.source_quality, source_quality_note=source_note)
+ self._rate = AdaptiveRate(metadata["rate"], options.target_fps)
+ self._set(source_fps=float(metadata["rate"]), target_fps=self._rate.fps)
+ in_w, in_h = _fit_height(metadata["width"], metadata["height"], options.max_height)
+ factor, mode = resolve_upscaling_mode(options.upscaling_factor)
+ out_w, out_h = resolve_output_size(in_w, in_h, factor)
+ self._set(status=f"Preparing DLSS: {in_w}x{in_h} -> {out_w}x{out_h}...")
+ prepared_runtime = prepare_runtime()
+ ai_uuid, video_uuid = processing_gpu_settings()
+ gpu = resolve_runtime_ai_gpu(prepared_runtime.gpus, prepared_runtime.runtime_bundle, ai_uuid)
+ apply_current_dlss_architecture(gpu)
+ nvenc, ordinal = self._select_encoder(prepared_runtime.gpus, video_uuid, out_w, out_h)
+ self._set(input_size=f"{in_w}x{in_h}", output_size=f"{out_w}x{out_h}",
+ encoder="NVIDIA NVENC" if nvenc else "CPU x264")
+ native = None
+ processes: list[subprocess.Popen] = []
+ threads: list[threading.Thread] = []
+ watchdog_done = threading.Event()
+ self._last_progress = time.monotonic()
+ watchdog = threading.Thread(target=self._watchdog, args=(watchdog_done,), daemon=True)
+ watchdog.start()
+ try:
+ native_args = dict(input_width=in_w, input_height=in_h, output_width=out_w, output_height=out_h,
+ frame_count=LIVE_FRAME_COUNT, warmup_frames=0, factor=factor, mode=mode,
+ gpu=gpu, runtime_bundle=prepared_runtime.runtime_bundle)
+ native = DLSSFrameSession(**native_args, native_settings=resolve_native_settings(options),
+ controller=self.controller)
+ self._last_progress = time.monotonic()
+ decoder = self._spawn("decoder", self._decoder_command(resolved, in_w, in_h, self._rate.rate),
+ stdout=subprocess.PIPE, stdin=subprocess.DEVNULL)
+ processes.append(decoder)
+ encoder = self._spawn("encoder", self._encoder_command(self._rate.rate, ordinal, nvenc),
+ stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, cwd=self._session_dir)
+ processes.append(encoder)
+ # Small bounded queues overlap CPU guides, GPU evaluation and pipe
+ # writes. Playback seconds are stored as compressed HLS, not RGBA.
+ incoming: queue.Queue = queue.Queue(maxsize=options.queue_frames)
+ outgoing: queue.Queue = queue.Queue(maxsize=options.queue_frames)
+ threads = [threading.Thread(target=self._decode, args=(decoder, incoming, in_w, in_h), daemon=True),
+ threading.Thread(target=self._encode, args=(encoder, outgoing, out_w, out_h), daemon=True)]
+ for thread in threads:
+ thread.start()
+ put(outgoing, get(incoming, self.controller.cancel), self.controller.cancel)
+ self._ready.set()
+ processing_start = time.perf_counter()
+ pacing_seconds = 0.0
+ processed = 0
+ while not self.controller.cancel.is_set():
+ item = get(incoming, self.controller.cancel)
+ if item is None:
+ self.effects.finish()
+ put(outgoing, None, self.controller.cancel)
+ break
+ if isinstance(item, VideoFrame):
+ self._update_playback()
+ pacing_seconds += self._pace(item.pts)
+ request = self.effects.take_due()
+ if request:
+ native, output, cost = self._replace_effects(native, request, native_args, item, processed)
+ else:
+ start = time.perf_counter()
+ output, pts = native.process(index=processed, rgba=item.rgba, motion=item.motion,
+ reset=item.reset, pts=item.pts)
+ cost = time.perf_counter() - start
+ if pts != item.pts:
+ raise RuntimeError("DLSS worker returned a different frame timestamp.")
+ processed += 1
+ if processed == 1:
+ evidence = verify_feature_18(native.worker_logs, native.reshade_log_text())
+ self._feature_evidence = {k: v for k, v in evidence.items() if k != "reshade_log"}
+ if self._first_frame_at is None:
+ self._first_frame_at = time.monotonic()
+ info = self.snapshot()
+ self._rate.observe(cost, info.guide_ms / 1000, info.encode_ms / 1000)
+ self._set(processed_frames=processed, dlss_ms=cost * 1000 if processed == 1 or request else
+ info.dlss_ms * 0.9 + cost * 100, target_fps=self._rate.fps,
+ effective_fps=processed / max(time.perf_counter() - processing_start - pacing_seconds, 0.001))
+ item = VideoFrame(item.index, item.pts, item.duration, output)
+ put(outgoing, item, self.controller.cancel)
+ self._last_progress = time.monotonic()
+ for thread in threads:
+ # Check progress and player closure while the last segment drains.
+ while thread.is_alive() and not self.controller.cancel.is_set():
+ thread.join(timeout=0.2)
+ self._update_playback()
+ if self._error:
+ raise RuntimeError(self._error)
+ if self.controller.cancel.is_set():
+ raise Cancelled("Live stopped.")
+ self._finished = True
+ except Exception as exc:
+ if not self.controller.cancel.is_set():
+ self._error = _redact(str(exc))
+ self.controller.stop()
+ raise
+ finally:
+ watchdog_done.set()
+ if native is not None:
+ self._remember_native(native)
+ native.abort()
+ for process in processes:
+ if process.poll() is None:
+ process.terminate()
+ try:
+ process.wait(timeout=5)
+ except subprocess.TimeoutExpired:
+ process.kill()
+ process.wait(timeout=5)
+ self.controller.unregister(process)
+ # Errors/Stop unblock queue operations as well as pipe reads/writes.
+ for thread in threads:
+ thread.join(timeout=2)
+ for process in processes:
+ for pipe in (process.stdin, process.stdout, process.stderr):
+ if pipe and not pipe.closed:
+ try:
+ pipe.close()
+ except OSError:
+ pass
+ watchdog.join(timeout=1)
+
+ def _write_report(self) -> None:
+ if not self.info.report_path:
+ return
+ payload = {"session": asdict(self.snapshot()),
+ "settings": {k: v for k, v in asdict(self.options).items() if k not in ("source", "mpv_args")},
+ "effects": self.effects.report(),
+ "player": self._player_state, "rate_changes": self._rate.changes if self._rate else [],
+ "native": self._native_logs,
+ "feature_18": self._feature_evidence,
+ "logs": {name: [_redact(line) for line in log.snapshot()] for name, log in self._logs.items()}}
+ try:
+ Path(self.info.report_path).write_text(json.dumps(payload, indent=2), encoding="utf-8")
+ except OSError:
+ pass
+
+ def run(self) -> None:
+ started = time.monotonic()
+ self._started_at = started
+ server = None
+ try:
+ validate_options(self.options)
+ LIVE_DIR.mkdir(parents=True, exist_ok=True)
+ stamp = time.strftime("%Y%m%d-%H%M%S") + f"-{time.time_ns() % 1_000_000:06d}-{os.getpid()}"
+ self._session_dir = LIVE_DIR / f"live-{stamp}"
+ self._session_dir.mkdir()
+ (self._session_dir / "owner.json").write_text(json.dumps({"pid": os.getpid(), "keep": self.options.keep_files}))
+ report = LOGS / "live" / f"{stamp}.json"
+ report.parent.mkdir(parents=True, exist_ok=True)
+ self._set(report_path=str(report))
+ server = HlsServer(self._session_dir)
+ server.start()
+ self._set(playlist_url=server.playlist_url())
+ with active_job(self.controller):
+ self._produce()
+ self._set(processing=False)
+ self._update_playback(finished=True)
+ self._produced.set()
+ self._write_report()
+ # The server and final segments belong to playback, not GPU work.
+ # Manual players can continue using the URL until Stop is pressed.
+ while not self.controller.cancel.wait(0.25):
+ self._update_playback(finished=True)
+ if self._mpv and self._mpv.poll() is not None:
+ if self._mpv.returncode:
+ raise RuntimeError(f"MPV exited with code {self._mpv.returncode}.")
+ break
+ except Exception as exc:
+ error = self._error or (None if self.controller.cancel.is_set() else _redact(str(exc)))
+ if error:
+ self._set(status=f"Failed: {error}", failures=[error])
+ else:
+ self._set(status="Stopped.")
+ finally:
+ self.effects.finish()
+ self.controller.terminate_processes()
+ if self._mpv and self._mpv.poll() is None:
+ self._mpv.terminate()
+ try:
+ self._mpv.wait(timeout=5)
+ except subprocess.TimeoutExpired:
+ self._mpv.kill()
+ self._mpv.wait(timeout=5)
+ if server:
+ server.stop()
+ self._set(running=False, processing=False, mpv_running=False, playlist_url="",
+ elapsed_seconds=time.monotonic() - started)
+ if self._finished and not self.snapshot().failures:
+ self._set(status=f"Finished: {self._title}. {self.info.processed_frames} frames enhanced.")
+ elif not self.snapshot().failures:
+ self._set(status="Stopped.")
+ self._write_report()
+ if self._session_dir and not self.options.keep_files:
+ shutil.rmtree(self._session_dir, ignore_errors=True)
+ self._ready.set()
+ self._produced.set()
+ with _LOCK:
+ global _CURRENT, _LAST
+ _LAST = self.snapshot()
+ if _CURRENT is self:
+ _CURRENT = None
+
+
+def start_live_session(options: LiveOptions) -> LiveSessionInfo:
+ try:
+ validate_options(options)
+ kind = _classify(options.source)
+ except ValueError as exc:
+ raise RuntimeError(str(exc)) from exc
+ check_live_binaries(resolve_pages=kind in ("youtube", "twitch"), play=options.open_mpv)
+ global _CURRENT
+ with _LOCK:
+ if _CURRENT is not None and _CURRENT.is_alive():
+ raise RuntimeError("A Live session is already running; Stop it first.")
+ session = LiveSession(options)
+ _CURRENT = session
+ session.start()
+ # Resolution, probe and native setup are cancellable background work.
+ return session.snapshot()
+
+
+def stop_live_session() -> LiveSessionInfo:
+ with _LOCK:
+ session = _CURRENT
+ if session is None or not session.is_alive():
+ return live_status()
+ session.stop()
+ session.join(timeout=3)
+ return session.snapshot()
+
+
+def update_live_effects(settings) -> bool:
+ """Called in shared-settings commit order; enqueue only, never do GPU work."""
+ with _LOCK:
+ session = _CURRENT
+ return session.request_effects(settings) if session is not None else False
+
+
+def is_live_running() -> bool:
+ with _LOCK:
+ return _CURRENT is not None and _CURRENT.is_alive()
+
+
+def live_status() -> LiveSessionInfo:
+ with _LOCK:
+ session, last = _CURRENT, _LAST
+ return session.snapshot() if session else replace(last, failures=list(last.failures)) if last else LiveSessionInfo()
+
+
+def sweep_stale_live_dirs() -> int:
+ """Keep diagnostics and other running app instances' playback intact."""
+ removed = 0
+ if not LIVE_DIR.is_dir():
+ return 0
+ for child in LIVE_DIR.glob("live-*"):
+ if not child.is_dir():
+ continue
+ try:
+ owner = json.loads((child / "owner.json").read_text())
+ if owner.get("keep"):
+ continue
+ pid = int(owner["pid"])
+ # On Windows os.kill(pid, 0) is not a safe process-existence check.
+ if os.name == "nt":
+ import ctypes
+ kernel = ctypes.WinDLL("kernel32", use_last_error=True)
+ kernel.OpenProcess.restype = ctypes.c_void_p
+ handle = kernel.OpenProcess(0x1000, False, pid)
+ if handle:
+ kernel.CloseHandle.argtypes = [ctypes.c_void_p]
+ kernel.CloseHandle(handle)
+ continue
+ if ctypes.get_last_error() == 5: # access denied implies it exists
+ continue
+ else:
+ try:
+ os.kill(pid, 0)
+ continue
+ except ProcessLookupError:
+ pass
+ except PermissionError:
+ continue
+ except (OSError, ValueError, KeyError):
+ # Unknown legacy directories are only swept after a full day.
+ if time.time() - child.stat().st_mtime < 86400:
+ continue
+ shutil.rmtree(child, ignore_errors=True)
+ removed += not child.exists()
+ return removed
+
+
+def _shutdown() -> None:
+ with _LOCK:
+ session = _CURRENT
+ if session:
+ session.stop()
+ session.join(timeout=5)
+
+
+atexit.register(_shutdown)
diff --git a/src/live/player.py b/src/live/player.py
new file mode 100644
index 0000000..d1a61da
--- /dev/null
+++ b/src/live/player.py
@@ -0,0 +1,72 @@
+from __future__ import annotations
+
+import os
+import subprocess
+from pathlib import Path
+
+from ..core.paths import MPV, YTDLP
+
+
+def check_live_binaries(*, resolve_pages: bool, play: bool) -> None:
+ """Raise a user-facing error if required Live externals are missing."""
+ if resolve_pages and not YTDLP.is_file():
+ raise RuntimeError(
+ f"yt-dlp is not installed ({YTDLP} missing); Live cannot "
+ "resolve YouTube/Twitch page URLs. Local files still work."
+ )
+ if play and not MPV.is_file():
+ raise RuntimeError(
+ f"MPV is not installed ({MPV} missing); uncheck "
+ "'Open in MPV' or restore the portable player."
+ )
+
+
+def launch_mpv(
+ playlist_url: str,
+ title: str,
+ extra_args: tuple[str, ...] = (),
+ *,
+ buffer_seconds: float = 6.0,
+ state_path: Path | None = None,
+) -> subprocess.Popen:
+ """Open the vendored MPV on a Live playlist. Raises on missing binary."""
+ if not MPV.is_file():
+ raise RuntimeError(
+ f"MPV is not installed ({MPV} missing); the Live stream "
+ "is still being produced — restore the player to watch it."
+ )
+ env = os.environ.copy()
+ env["PATH"] = str(YTDLP.parent) + os.pathsep + env.get("PATH", "")
+ if state_path is not None:
+ env["DLSS5_LIVE_PLAYER_STATE"] = str(state_path)
+ command = [
+ str(MPV),
+ "--no-config",
+ "--ytdl=no",
+ "--cache=yes",
+ "--cache-pause=yes",
+ "--cache-pause-initial=yes",
+ f"--cache-pause-wait={buffer_seconds:g}",
+ f"--cache-secs={max(20, buffer_seconds * 3):g}",
+ "--demuxer-max-bytes=128MiB",
+ "--demuxer-lavf-o=live_start_index=0",
+ "--hwdec=auto-safe",
+ "--video-sync=audio",
+ f"--script={Path(__file__).with_name('player_status.lua')}",
+ f"--title=DLSS 5 Live — {title}",
+ "--terminal=no",
+ *extra_args,
+ playlist_url,
+ ]
+ creation_flags = getattr(subprocess, "CREATE_NO_WINDOW", 0)
+ try:
+ return subprocess.Popen(
+ command,
+ stdout=subprocess.DEVNULL,
+ stderr=subprocess.DEVNULL,
+ stdin=subprocess.DEVNULL,
+ env=env,
+ creationflags=creation_flags,
+ )
+ except OSError as exc:
+ raise RuntimeError(f"Could not launch MPV: {exc}.") from exc
diff --git a/src/live/player_status.lua b/src/live/player_status.lua
new file mode 100644
index 0000000..4a2b82d
--- /dev/null
+++ b/src/live/player_status.lua
@@ -0,0 +1,36 @@
+local utils = require "mp.utils"
+local path = os.getenv("DLSS5_LIVE_PLAYER_STATE")
+if path then
+ local stalls, was_stalled, started = 0, false, false
+ local max_av, max_drop, max_decoder_drop, last_position, last_duration = 0, 0, 0, 0, 0
+ local function save(ended, reason)
+ local stalled = mp.get_property_bool("paused-for-cache", false)
+ local position = mp.get_property_number("time-pos", last_position)
+ if stalled and not was_stalled and started then stalls = stalls + 1 end
+ was_stalled = stalled
+ if not stalled and position > 0.1 then started = true end
+ if not ended then
+ last_position = position
+ last_duration = mp.get_property_number("duration", last_duration)
+ end
+ local av = mp.get_property_number("avsync", 0)
+ max_av = math.max(max_av, math.abs(av))
+ -- Unavailable properties can also return an error string at EOF.
+ -- Parentheses pass only the numeric value/default to math.max.
+ max_drop = math.max(max_drop, (mp.get_property_number("frame-drop-count", 0)))
+ max_decoder_drop = math.max(max_decoder_drop, (mp.get_property_number("decoder-frame-drop-count", 0)))
+ local state = {
+ position = last_position, duration = last_duration,
+ cache_seconds = mp.get_property_number("demuxer-cache-duration", 0),
+ paused = mp.get_property_bool("pause", false),
+ buffering = stalled, stalls = stalls, started = started,
+ dropped = max_drop, decoder_dropped = max_decoder_drop,
+ avsync = av, max_avsync = max_av,
+ ended = ended or false, reason = reason
+ }
+ local file = io.open(path, "w")
+ if file then file:write(utils.format_json(state)); file:close() end
+ end
+ mp.add_periodic_timer(0.5, function() save(false) end)
+ mp.register_event("end-file", function(event) save(true, event.reason) end)
+end
diff --git a/src/live/source_resolver.py b/src/live/source_resolver.py
new file mode 100644
index 0000000..d6dd6c2
--- /dev/null
+++ b/src/live/source_resolver.py
@@ -0,0 +1,152 @@
+from __future__ import annotations
+
+import json
+import subprocess
+import time
+from fractions import Fraction
+from pathlib import Path
+from urllib.parse import urlparse
+
+from ..core.jobs import Cancelled, JobController
+from ..core.paths import FFPROBE, YTDLP
+from .models import LIVE_SOURCE_QUALITY_CHOICES, ResolvedSource
+
+
+def run_capture(command: list[str], controller: JobController | None = None,
+ timeout: float = 60) -> subprocess.CompletedProcess[str]:
+ """Drain both pipes while allowing Stop during resolution and probing."""
+ process = subprocess.Popen(command, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE, creationflags=getattr(subprocess, "CREATE_NO_WINDOW", 0))
+ if controller:
+ controller.register(process)
+ deadline = time.monotonic() + timeout
+ try:
+ while True:
+ if controller and controller.cancel.is_set():
+ raise Cancelled("Live stopped.")
+ if time.monotonic() >= deadline:
+ raise RuntimeError(f"{Path(command[0]).stem} timed out after {timeout:g} seconds.")
+ try:
+ out, err = process.communicate(timeout=0.2)
+ if controller and controller.cancel.is_set():
+ raise Cancelled("Live stopped.")
+ return subprocess.CompletedProcess(command, process.returncode,
+ out.decode("utf-8", "replace"), err.decode("utf-8", "replace"))
+ except subprocess.TimeoutExpired:
+ continue
+ finally:
+ if process.poll() is None:
+ process.kill()
+ process.wait(timeout=5)
+ process.communicate(timeout=5)
+ if controller:
+ controller.unregister(process)
+
+
+def _classify(raw: str) -> str:
+ text = raw.strip().strip('"')
+ if not text:
+ raise ValueError("Enter a local video path, direct stream URL, YouTube or Twitch URL.")
+ if Path(text).is_file():
+ return "file"
+ parsed = urlparse(text)
+ host = (parsed.hostname or "").casefold()
+ for domain, kind in (("youtube.com", "youtube"), ("youtu.be", "youtube"), ("twitch.tv", "twitch")):
+ if host == domain or host.endswith("." + domain):
+ return kind
+ if parsed.scheme in ("http", "https", "rtmp", "rtmps", "rtsp", "srt"):
+ return "direct"
+ raise ValueError("Source is not an existing video file or a supported stream URL.")
+
+
+def _resolved_metadata(kind: str, data: dict) -> ResolvedSource:
+ formats = data.get("requested_formats") or [data]
+ video = next((f for f in formats if f.get("vcodec") != "none" and f.get("url")), None)
+ if video is None:
+ raise RuntimeError("yt-dlp returned no playable video for this page.")
+ audio = next((f for f in formats if f.get("vcodec") == "none" and f.get("acodec") != "none"), None)
+ # Muxed Twitch/HLS already contains audio. A second connection can join at
+ # a different live edge and desynchronize it from the video.
+ return ResolvedSource(kind, str(data.get("title") or data.get("id") or kind),
+ video["url"], audio.get("url") if audio else None,
+ bool(data.get("is_live") or data.get("live_status") == "is_live"),
+ dict(video.get("http_headers") or data.get("http_headers") or {}),
+ dict((audio or {}).get("http_headers") or data.get("http_headers") or {}))
+
+
+def resolve_source(raw: str, max_height: int,
+ controller: JobController | None = None, *,
+ source_quality: str = "Auto") -> ResolvedSource:
+ if source_quality not in LIVE_SOURCE_QUALITY_CHOICES:
+ raise ValueError("Choose a valid Source quality.")
+ kind = _classify(raw)
+ text = raw.strip().strip('"')
+ if kind == "file":
+ path = Path(text).resolve()
+ return ResolvedSource(kind, path.name, str(path), None, False)
+ if kind == "direct":
+ path = urlparse(text).path
+ live = Path(path).suffix.lower() not in (".mp4", ".mkv", ".mov", ".webm", ".avi")
+ return ResolvedSource(kind, Path(path).name or "Network stream", text, None, live)
+ if not YTDLP.is_file():
+ raise RuntimeError(f"yt-dlp is missing: {YTDLP}")
+ source_height = max_height if source_quality == "Auto" else int(source_quality)
+ result = run_capture([str(YTDLP), "--no-playlist", "--no-warnings", "--skip-download",
+ "--dump-single-json", "--socket-timeout", "15", "--retries", "2", "-f",
+ f"bv*[height<={source_height}]+ba/b[height<={source_height}]/b", text], controller, 90)
+ if result.returncode:
+ detail = "\n".join(result.stderr.strip().splitlines()[-4:])
+ if "offline" in detail.lower() or "not currently live" in detail.lower():
+ raise RuntimeError("This channel is currently offline. Try again when the broadcast is live.")
+ raise RuntimeError(f"Could not resolve this video: {detail}")
+ return _resolved_metadata(kind, json.loads(result.stdout))
+
+
+def input_args(url: str, headers: dict[str, str], timeout: float, *, reconnect: bool = True) -> list[str]:
+ if not url.startswith(("http://", "https://", "rtmp://", "rtmps://", "rtsp://", "srt://")):
+ return []
+ args = ["-rw_timeout", str(int(timeout * 1_000_000))]
+ if url.startswith(("http://", "https://")):
+ if reconnect:
+ args += ["-reconnect", "1", "-reconnect_streamed", "1", "-reconnect_on_network_error", "1",
+ "-reconnect_delay_max", "3", "-reconnect_max_retries", "3"]
+ clean = {str(k).replace("\r", "").replace("\n", ""): str(v).replace("\r", "").replace("\n", "")
+ for k, v in headers.items()}
+ if clean:
+ args += ["-headers", "".join(f"{k}: {v}\r\n" for k, v in clean.items())]
+ return args
+
+
+def probe_source(source: ResolvedSource, controller: JobController, timeout: float) -> dict:
+ result = run_capture([str(FFPROBE), "-v", "error",
+ *input_args(source.video_url, source.video_headers, timeout, reconnect=False),
+ "-analyzeduration", "2000000", "-probesize", "5000000", "-show_streams", "-show_format",
+ "-of", "json", source.video_url], controller, timeout + 10)
+ if result.returncode:
+ raise RuntimeError(f"Source probe failed: {result.stderr.strip()[-2000:]}")
+ data = json.loads(result.stdout)
+ video = next((s for s in data.get("streams", []) if s.get("codec_type") == "video"), None)
+ if video is None:
+ raise RuntimeError("This source contains no decodable video.")
+ rotation = int((video.get("tags") or {}).get("rotate") or 0)
+ for side in video.get("side_data_list") or []:
+ rotation = int(side.get("rotation", rotation))
+ width, height = int(video["width"]), int(video["height"])
+ if rotation % 360 in (90, 270):
+ width, height = height, width
+
+ def rate(key: str) -> Fraction:
+ try:
+ return Fraction(video.get(key) or "0")
+ except (ValueError, ZeroDivisionError):
+ return Fraction(0)
+
+ avg, nominal = rate("avg_frame_rate"), rate("r_frame_rate")
+ fps = nominal if nominal > 0 and (avg <= 0 or abs(float(avg / nominal) - 1) < 0.001) else avg
+ if not 1 <= fps <= 240:
+ fps = Fraction(30)
+ duration = float((data.get("format") or {}).get("duration") or video.get("duration") or 0)
+ if source.kind == "direct" and duration > 0:
+ source.is_live = False
+ return {"width": width, "height": height, "rate": fps, "duration": duration,
+ "has_audio": bool(source.audio_url) or any(s.get("codec_type") == "audio" for s in data.get("streams", []))}
diff --git a/src/live/transport.py b/src/live/transport.py
new file mode 100644
index 0000000..afdcdc3
--- /dev/null
+++ b/src/live/transport.py
@@ -0,0 +1,158 @@
+from __future__ import annotations
+
+import math
+import queue
+import threading
+from collections import deque
+from dataclasses import dataclass
+from fractions import Fraction
+
+import av
+import numpy as np
+
+from ..core.jobs import Cancelled
+
+TIME_BASE = Fraction(1, 90000)
+
+
+class PipeReader:
+ """Expose only operations supported by a subprocess pipe to libavformat.
+
+ BufferedReader has a seek method even on Windows pipes. Advertising it
+ makes NUT probing attempt seeks and can silently consume opening frames.
+ """
+
+ def __init__(self, stream) -> None:
+ self.stream = stream
+
+ def read(self, size: int) -> bytes:
+ return self.stream.read(size)
+
+
+class PipeWriter:
+ def __init__(self, stream) -> None:
+ self.stream = stream
+
+ def write(self, data) -> int:
+ return self.stream.write(data)
+
+ def flush(self) -> None:
+ self.stream.flush()
+
+
+def put(queue_: queue.Queue, item, cancel: threading.Event) -> None:
+ while not cancel.is_set():
+ try:
+ queue_.put(item, timeout=0.1)
+ return
+ except queue.Full:
+ pass
+ raise Cancelled("Live stopped.")
+
+
+def get(queue_: queue.Queue, cancel: threading.Event):
+ while not cancel.is_set():
+ try:
+ return queue_.get(timeout=0.1)
+ except queue.Empty:
+ pass
+ raise Cancelled("Live stopped.")
+
+
+@dataclass(slots=True)
+class VideoFrame:
+ index: int
+ pts: int
+ duration: int
+ rgba: np.ndarray
+ motion: np.ndarray | None = None
+ reset: bool = False
+
+
+class AdaptiveRate:
+ """Select a regular source-frame cadence, never renumber media timestamps.
+
+ Auto only steps down during a session, with headroom and hysteresis. It
+ cannot oscillate between rates in response to network or player waits.
+ """
+
+ def __init__(self, source_rate: Fraction, choice: str) -> None:
+ self.rate = min(source_rate, Fraction(60)) if choice == "Auto" else (
+ source_rate if choice == "Source" else min(source_rate, Fraction(choice)))
+ self.automatic = choice == "Auto"
+ self.divisor = 1
+ self.samples: deque[float] = deque(maxlen=60)
+ self.observed = 0
+ self._warmup_left = 6
+ self.changes: list[dict] = []
+ self._lock = threading.Lock()
+
+ @property
+ def fps(self) -> float:
+ with self._lock:
+ return float(self.rate) / self.divisor
+
+ def accepts(self, index: int) -> bool:
+ with self._lock:
+ return index % self.divisor == 0
+
+ def observe(self, dlss_seconds: float, guide_seconds: float, encode_seconds: float) -> None:
+ if not self.automatic:
+ return
+ with self._lock:
+ self.observed += 1
+ # Exclude the cold feature evaluation and the first GPU ramp-up.
+ if self._warmup_left:
+ self._warmup_left -= 1
+ return
+ self.samples.append(max(dlss_seconds, guide_seconds, encode_seconds) + 0.002)
+ if len(self.samples) < 24 or self.observed % 30:
+ return
+ sample = sorted(self.samples)[int((len(self.samples) - 1) * 0.90)]
+ needed = max(1, math.ceil(float(self.rate) * sample / 0.80))
+ # Short scheduling spikes should not keep ratcheting the rate down.
+ if needed > self.divisor and sample * self.fps_unlocked() > 0.88:
+ self.divisor = min(needed, max(1, math.ceil(float(self.rate))))
+ self.changes.append({"after_frames": self.observed, "fps": self.fps_unlocked()})
+
+ def reset_measurements(self) -> None:
+ """Discard startup costs after a worker change, keeping the cadence."""
+ with self._lock:
+ self.samples.clear()
+ self._warmup_left = 6
+
+ def fps_unlocked(self) -> float:
+ return float(self.rate) / self.divisor
+
+
+class TimestampMuxer:
+ """Carry enhanced RGBA and original audio timestamps over one NUT pipe."""
+
+ def __init__(self, pipe, width: int, height: int, rate: Fraction, audio_template=None) -> None:
+ self.container = av.open(PipeWriter(pipe), mode="w", format="nut", options={"write_index": "0"})
+ self.video = self.container.add_stream("rawvideo", rate=rate)
+ self.video.width = width
+ self.video.height = height
+ self.video.pix_fmt = "rgba"
+ self.video.codec_context.codec_tag = "RGBA"
+ self.video.time_base = TIME_BASE
+ self.audio = self.container.add_stream_from_template(audio_template) if audio_template else None
+ self.container.start_encoding()
+
+ def write(self, item: VideoFrame | av.Packet) -> None:
+ if isinstance(item, VideoFrame):
+ packet = av.Packet(memoryview(item.rgba).cast("B"))
+ packet.stream = self.video
+ packet.pts = packet.dts = item.pts
+ packet.duration = item.duration
+ packet.time_base = TIME_BASE
+ packet.is_keyframe = True
+ else:
+ if self.audio is None:
+ return
+ packet = item
+ packet.stream = self.audio
+ self.container.mux(packet)
+
+ def close(self) -> None:
+ self.container.close()
diff --git a/src/live/ui.py b/src/live/ui.py
new file mode 100644
index 0000000..e97cadc
--- /dev/null
+++ b/src/live/ui.py
@@ -0,0 +1,244 @@
+from __future__ import annotations
+
+from dataclasses import dataclass
+from pathlib import Path
+
+import gradio as gr
+
+from ..settings.models import UISettings, parse_automatic_mask
+from ..video.ui import build_dlss_model_control, build_neural_controls
+from .models import (LIVE_FPS_CHOICES, LIVE_GUIDE_CHOICES, LIVE_MAX_HEIGHT_CHOICES,
+ LIVE_MAX_HEIGHTS, LIVE_SEGMENT_CHOICES, LIVE_SOURCE_QUALITY_CHOICES)
+from .pipeline import is_live_running, live_status, start_live_session, stop_live_session
+from .models import LiveOptions
+
+
+def start_live(
+ source: str,
+ nr_preset: str,
+ nr_style: str,
+ nr_intensity: float,
+ local_tone_strength: float,
+ local_structure_strength: float,
+ skin_structure_strength: float,
+ upscaling_factor: float,
+ automatic_mask: str,
+ dlss_model_preset: str,
+ max_height: str,
+ segment_seconds: str,
+ open_mpv: bool,
+ target_fps: str = "Auto",
+ buffer_seconds: float = 6.0,
+ guide_quality: str = "Fast",
+ source_quality: str = "Auto",
+ source_mode: str = "Online",
+ local_video: str | None = None,
+) -> str:
+ if is_live_running():
+ raise gr.Error("A Live session is already running; Stop it first.")
+ if source_mode == "Local":
+ if not local_video or not Path(local_video).is_file():
+ raise gr.Error("Select a local video before starting Live.")
+ selected_source = str(local_video)
+ elif source_mode == "Online":
+ selected_source = (source or "").strip()
+ if not selected_source:
+ raise gr.Error("Enter an online URL, or select Local to use the uploaded video.")
+ else:
+ raise gr.Error("Choose Online or Local as the source.")
+ try:
+ height = int(max_height)
+ except (TypeError, ValueError):
+ height = 720
+ try:
+ segment = int(segment_seconds)
+ except (TypeError, ValueError):
+ segment = 2
+ try:
+ factor = float(upscaling_factor)
+ except (TypeError, ValueError):
+ raise gr.Error("Choose a valid upscaling factor.") from None
+ try:
+ auto_mask = parse_automatic_mask(automatic_mask)
+ except ValueError as exc:
+ raise gr.Error(str(exc)) from exc
+ options = LiveOptions(
+ source=selected_source,
+ nr_preset=nr_preset,
+ nr_style=nr_style,
+ nr_intensity=float(nr_intensity),
+ local_tone_strength=float(local_tone_strength),
+ local_structure_strength=float(local_structure_strength),
+ skin_structure_strength=float(skin_structure_strength),
+ automatic_mask=auto_mask,
+ dlss_model_preset=dlss_model_preset,
+ max_height=height if height in LIVE_MAX_HEIGHTS else 720,
+ upscaling_factor=factor,
+ segment_seconds=segment if segment in (1, 2, 4, 6) else 2,
+ open_mpv=bool(open_mpv),
+ target_fps=target_fps,
+ buffer_seconds=float(buffer_seconds),
+ guide_quality=guide_quality,
+ source_quality=source_quality,
+ )
+ try:
+ info = start_live_session(options)
+ except RuntimeError as exc:
+ raise gr.Error(str(exc)) from exc
+ return info.status
+
+
+def stop_live() -> str:
+ try:
+ info = stop_live_session()
+ except RuntimeError as exc:
+ raise gr.Error(str(exc)) from exc
+ return info.status
+
+
+def refresh_live_status() -> str:
+ info = live_status()
+ if not info.running and info.status == "Idle.":
+ return "Idle. Enter a source and press Start Live."
+ parts = [info.status]
+ if info.playlist_url:
+ parts.append(f"Playlist: {info.playlist_url}")
+ if info.mpv_running:
+ parts.append(f"MPV: {info.player_dropped_frames} dropped | {info.rebuffer_events} rebuffer events | "
+ f"A/V offset {info.av_sync_ms:+.1f} ms")
+ if info.output_size:
+ parts.append(f"Received {info.source_size} -> Processing {info.input_size} -> Output {info.output_size} | {info.encoder}")
+ if info.source_quality_note:
+ parts.append(info.source_quality_note)
+ parts.append(f"DLSS effects: {info.effects_status}")
+ if info.applied_at_pts is not None:
+ parts.append(f"Latest applied change starts at video time {info.applied_at_pts / 90000:.2f}s; "
+ "buffered video keeps its previous appearance until then.")
+ if info.effects_error:
+ parts.append(f"Effect update: {info.effects_error}")
+ if info.processing and info.source_fps:
+ parts.append(f"Source: {info.source_fps:.2f} fps. Motion guides: {info.guide_ms:.1f} ms | "
+ f"DLSS: {info.dlss_ms:.1f} ms | Encode transport: {info.encode_ms:.1f} ms")
+ if info.report_path:
+ parts.append(f"Diagnostics: {info.report_path}")
+ return "\n".join(parts)
+
+
+@dataclass(slots=True)
+class LiveTab:
+ source: object
+ neural: list[object]
+ model_preset: object
+ max_height: object
+ segment: object
+ open_mpv: object
+ start: object
+ stop: object
+ status: object
+ target_fps: object
+ buffer: object
+ guide_quality: object
+ source_quality: object
+ source_mode: object
+ local_video: object
+
+ @property
+ def settings_inputs(self) -> list[object]:
+ # Shared DLSS values only (persisted globally + mirrored); the rest
+ # of the Live options are session-local.
+ return [*self.neural, self.model_preset]
+
+
+def build_live_tab(settings: UISettings) -> LiveTab:
+ height_labels = {"1440": "1440p (2K)", "2160": "2160p (4K)"}
+ with gr.Row():
+ with gr.Column(scale=3):
+ source_mode = gr.Radio(
+ choices=["Online", "Local"], value="Online", label="Source",
+ info="Start Live uses the selected source. You can keep both inputs filled. Changes apply on the next Start.",
+ )
+ source = gr.Textbox(
+ label="Online URL",
+ placeholder="Direct stream URL, YouTube or Twitch URL",
+ info="YouTube/Twitch pages are resolved with the portable yt-dlp. Changes apply on the next Start.",
+ )
+ local_video = gr.File(
+ label="Local video", file_count="single", file_types=["video"],
+ type="filepath", interactive=True,
+ )
+ gr.Markdown("Live applies DLSS effect changes automatically, including shared edits from other tabs. "
+ "Edits are combined for 0.5 seconds; applying them takes a few seconds, "
+ "plus playback buffering.")
+ with gr.Accordion("DLSS 5 Neural Rendering Settings", open=True):
+ neural = build_neural_controls(settings)
+ neural[6].info += " In Live, changes apply on the next Start."
+ with gr.Accordion("DLSS 5 Settings", open=True):
+ model_preset = build_dlss_model_control(settings)
+ with gr.Row():
+ source_quality = gr.Dropdown(
+ choices=[("Auto (follow Max input height)" if height == "Auto" else
+ height_labels.get(height, f"{height}p"), height)
+ for height in LIVE_SOURCE_QUALITY_CHOICES],
+ value="Auto", label="Source quality",
+ info="YouTube/Twitch stream quality before resizing. Uses the best available format within "
+ "the limit, with a fallback if unavailable. Changes apply on the next Start.",
+ )
+ max_height = gr.Dropdown(
+ choices=[(height_labels.get(height, height), height) for height in LIVE_MAX_HEIGHT_CHOICES],
+ value="720",
+ label="Max input height",
+ info="Caps processing height before DLSS independently of Source quality. "
+ "2K/4K use more GPU time and memory. Changes apply on the next Start.",
+ )
+ segment = gr.Dropdown(
+ choices=list(LIVE_SEGMENT_CHOICES),
+ value="2",
+ label="Segment length (s)",
+ info="2 seconds is a good balance. The player waits for completed segments. Changes apply on the next Start.",
+ )
+ with gr.Row():
+ target_fps = gr.Dropdown(
+ choices=list(LIVE_FPS_CHOICES), value="Auto", label="Live frame rate",
+ info="Auto measures processing cost and selects a steady rate up to 60 fps. "
+ "Source keeps every frame; slower GPUs may need more buffering. Changes apply on the next Start.",
+ )
+ buffer = gr.Slider(
+ minimum=2, maximum=30, step=1, value=6, label="Playback buffer (seconds)",
+ info="More buffering absorbs network and GPU stalls, with more playback delay. Changes apply on the next Start.",
+ )
+ guide_quality = gr.Dropdown(
+ choices=list(LIVE_GUIDE_CHOICES), value="Fast", label="Motion guide quality",
+ info="Fast uses smaller motion estimation for Live. Quality uses the offline detail level. Changes apply on the next Start.",
+ )
+ open_mpv = gr.Checkbox(
+ value=True,
+ label="Open in MPV",
+ info="Launch the portable player automatically on the live playlist. Changes apply on the next Start.",
+ )
+ with gr.Row():
+ start = gr.Button("Start Live", variant="primary")
+ stop = gr.Button("Stop", variant="stop")
+ with gr.Column(scale=3):
+ status = gr.Textbox(
+ label="Live status",
+ value="Idle. Enter a source and press Start Live.",
+ interactive=False,
+ lines=10,
+ )
+ tab = LiveTab(
+ source, neural, model_preset, max_height, segment,
+ open_mpv, start, stop, status, target_fps, buffer, guide_quality, source_quality,
+ source_mode, local_video,
+ )
+ start.click(
+ start_live,
+ inputs=[source, *neural, model_preset, max_height, segment, open_mpv, target_fps, buffer,
+ guide_quality, source_quality, source_mode, local_video],
+ outputs=status,
+ concurrency_limit=1,
+ show_progress="full",
+ )
+ stop.click(stop_live, outputs=status, queue=False, show_progress="hidden")
+ timer = gr.Timer(0.5)
+ timer.tick(refresh_live_status, outputs=status, queue=False, show_progress="hidden")
+ return tab
diff --git a/src/settings/models.py b/src/settings/models.py
index 473cfc8..f9b7c86 100644
--- a/src/settings/models.py
+++ b/src/settings/models.py
@@ -2,9 +2,10 @@ from __future__ import annotations
from dataclasses import dataclass, replace
+from ..core.dlss_architecture import DLSS_ARCHITECTURE_CHOICES
from ..core.ffmpeg import CODEC_CHOICES as FFMPEG_CODEC_CHOICES, ENCODING_QUALITIES, HDR_ALLOWED_CODECS, hdr_mode_supported
-from ..core.naming import RENAME_MODES, validate_rename
-from ..core.runtime import DLSS_MODEL_PRESETS, NR_PRESETS, NR_STYLES, resolve_native_settings, resolve_upscaling_mode
+from ..core.naming import validate_rename
+from ..core.runtime import resolve_native_settings, resolve_upscaling_mode
from ..frame_interpolation.models import ENGINE_CHOICES, FPS_CHOICES
from .migration import _migrate_codec
@@ -68,6 +69,7 @@ class UISettings:
frame_interpolation_rename_mode: str = "Auto"
frame_interpolation_custom_suffix: str = "_DLSSFG"
preview_encoding: str = "Auto"
+ dlss_architecture: str = "Auto"
def component_values(
self,
@@ -152,6 +154,10 @@ def _validate(settings: UISettings) -> UISettings:
settings.preview_encoding,
PREVIEW_ENCODING_CHOICES,
),
+ "DLSS Architecture": (
+ settings.dlss_architecture,
+ DLSS_ARCHITECTURE_CHOICES,
+ ),
}
for label, (value, choices) in allowed.items():
if value not in choices:
diff --git a/src/settings/presets.py b/src/settings/presets.py
index 5ea0ab0..fd83487 100644
--- a/src/settings/presets.py
+++ b/src/settings/presets.py
@@ -10,7 +10,7 @@ from pathlib import Path
from typing import Any
from .models import (
- DEFAULT_SETTINGS, MAX_PRESET_BYTES, PRESET_FORMAT, PRESET_SCHEMA_VERSION, UISettings, _validate,
+ MAX_PRESET_BYTES, PRESET_FORMAT, PRESET_SCHEMA_VERSION, UISettings, _validate,
)
_WINDOWS_RESERVED_NAMES = {
diff --git a/src/settings/storage.py b/src/settings/storage.py
index 7824143..5a4b0ed 100644
--- a/src/settings/storage.py
+++ b/src/settings/storage.py
@@ -17,6 +17,7 @@ from .models import (
CODEC_CHOICES, CONFIG_SECTION, CONTAINER_CHOICES, DEFAULT_SETTINGS, IMAGE_FORMAT_CHOICES,
PREVIEW_ENCODING_CHOICES, QUALITY_CHOICES, UISettings, _validate,
)
+from ..core.dlss_architecture import DLSS_ARCHITECTURE_CHOICES
def load_settings(path: str | os.PathLike[str]) -> UISettings:
config_path = Path(path)
@@ -202,6 +203,11 @@ def load_settings(path: str | os.PathLike[str]) -> UISettings:
PREVIEW_ENCODING_CHOICES,
DEFAULT_SETTINGS.preview_encoding,
),
+ dlss_architecture=choice(
+ "dlss_architecture",
+ DLSS_ARCHITECTURE_CHOICES,
+ DEFAULT_SETTINGS.dlss_architecture,
+ ),
)
# Auto-disable HDR Mode if codec does not support it (e.g. H.264)
try:
@@ -250,6 +256,7 @@ def save_settings(path: str | os.PathLike[str], settings: UISettings) -> None:
"frame_interpolation_rename_mode": settings.frame_interpolation_rename_mode,
"frame_interpolation_custom_suffix": settings.frame_interpolation_custom_suffix,
"preview_encoding": settings.preview_encoding,
+ "dlss_architecture": settings.dlss_architecture,
}
temporary = config_path.with_name(f".{config_path.name}.tmp")
diff --git a/src/settings/ui.py b/src/settings/ui.py
index 29a5118..452346f 100644
--- a/src/settings/ui.py
+++ b/src/settings/ui.py
@@ -4,16 +4,15 @@ from dataclasses import dataclass, replace
import gradio as gr
+from ..core.dlss_architecture import apply_dlss_architecture
from ..core.ffmpeg import hdr_mode_supported, probe_nvenc_codecs
from ..core.gpu_selection import gpu_choice_label
from ..core.paths import CONFIG_PATH
-from ..core.runtime import prepare_runtime
-from ..core.runtime import DLSS_MODEL_PRESETS, NR_PRESETS, NR_STYLES, UPSCALING_MODES
-from ..core.naming import RENAME_MODES
+from ..core.runtime import UPSCALING_MODES, prepare_runtime
from ..core.ffmpeg.preview import normalize_preview_encoding
from .models import (
- AUTOMATIC_MASK_CHOICES, CODEC_CHOICES, CONTAINER_CHOICES, DEFAULT_SETTINGS, PREVIEW_ENCODING_CHOICES,
- QUALITY_CHOICES, UISettings,
+ DEFAULT_SETTINGS, DLSS_ARCHITECTURE_CHOICES,
+ PREVIEW_ENCODING_CHOICES, UISettings,
automatic_mask_choice, coerce_hdr_mode, parse_automatic_mask,
)
from .presets import export_settings_preset, import_settings_preset, preset_filename
@@ -43,6 +42,20 @@ def _shared_dlss_values(
return (*_neural_values(settings), settings.dlss_model_preset)
+def _mirrored_dlss_values(settings: UISettings) -> tuple:
+ """Shared 9-tuple twice: feeds two sibling tabs' neural controls."""
+ shared = _shared_dlss_values(settings)
+ return (*shared, *shared)
+
+
+def _notify_live_effects(settings: UISettings) -> None:
+ # Call while holding _CONFIG_LOCK: submissions follow the saved commit
+ # order, including imports/resets. This only enqueues an immutable snapshot.
+ from ..live.pipeline import update_live_effects
+
+ update_live_effects(settings)
+
+
def persist_image_settings(
nr_preset: str,
nr_style: str,
@@ -57,7 +70,7 @@ def persist_image_settings(
image_quality: float,
rename_mode: str,
custom_suffix: str,
-) -> tuple[str, str, float, float, float, float, float, str, str]:
+) -> tuple:
with _CONFIG_LOCK:
current = SETTINGS_STATE.current or load_settings(CONFIG_PATH)
settings = replace(
@@ -79,7 +92,8 @@ def persist_image_settings(
if settings != current:
save_settings(CONFIG_PATH, settings)
SETTINGS_STATE.current = settings
- return _shared_dlss_values(settings)
+ _notify_live_effects(settings)
+ return _mirrored_dlss_values(settings)
def persist_video_settings(
@@ -98,7 +112,7 @@ def persist_video_settings(
hdr_mode: bool,
rename_mode: str,
custom_suffix: str,
-) -> tuple[str, str, float, float, float, float, float, str, str]:
+) -> tuple:
with _CONFIG_LOCK:
current = SETTINGS_STATE.current or load_settings(CONFIG_PATH)
coerced_hdr = coerce_hdr_mode(codec, hdr_mode)
@@ -123,7 +137,40 @@ def persist_video_settings(
if settings != current:
save_settings(CONFIG_PATH, settings)
SETTINGS_STATE.current = settings
- return _shared_dlss_values(settings)
+ _notify_live_effects(settings)
+ return _mirrored_dlss_values(settings)
+
+
+def persist_live_settings(
+ nr_preset: str,
+ nr_style: str,
+ nr_intensity: float,
+ local_tone_strength: float,
+ local_structure_strength: float,
+ skin_structure_strength: float,
+ upscaling_factor: float,
+ automatic_mask: str,
+ dlss_model_preset: str,
+) -> tuple:
+ with _CONFIG_LOCK:
+ current = SETTINGS_STATE.current or load_settings(CONFIG_PATH)
+ settings = replace(
+ current,
+ nr_preset=nr_preset,
+ nr_style=nr_style,
+ nr_intensity=nr_intensity,
+ local_tone_strength=local_tone_strength,
+ local_structure_strength=local_structure_strength,
+ skin_structure_strength=skin_structure_strength,
+ upscaling_factor=upscaling_factor,
+ automatic_mask=parse_automatic_mask(automatic_mask),
+ dlss_model_preset=dlss_model_preset,
+ )
+ if settings != current:
+ save_settings(CONFIG_PATH, settings)
+ SETTINGS_STATE.current = settings
+ _notify_live_effects(settings)
+ return _mirrored_dlss_values(settings)
def persist_frame_interpolation_settings(
@@ -190,6 +237,8 @@ def _settings_component_values(settings: UISettings) -> tuple:
settings.ai_gpu_uuid,
settings.video_gpu_uuid,
settings.preview_encoding,
+ settings.dlss_architecture,
+ *shared,
)
@@ -239,6 +288,31 @@ def persist_preview_encoding(preview_encoding: str) -> None:
SETTINGS_STATE.current = settings
+def persist_dlss_architecture(dlss_architecture: str) -> None:
+ if not isinstance(dlss_architecture, str) or dlss_architecture not in DLSS_ARCHITECTURE_CHOICES:
+ raise gr.Error(
+ f"DLSS Architecture must be one of: {', '.join(DLSS_ARCHITECTURE_CHOICES)}."
+ )
+ with _CONFIG_LOCK:
+ current = SETTINGS_STATE.current or load_settings(CONFIG_PATH)
+ settings = replace(current, dlss_architecture=dlss_architecture)
+ if settings != current:
+ save_settings(CONFIG_PATH, settings)
+ SETTINGS_STATE.current = settings
+ # A Live worker may be between replacements. Pin its runtime for the
+ # entire session, including that brief interval with no DLL loaded.
+ from ..live.pipeline import is_live_running
+ if is_live_running():
+ return
+ # Swap the host NR runtime immediately so the next render uses it.
+ # Failures only warn (logged); the saved setting still applies later.
+ try:
+ prepared = prepare_runtime()
+ apply_dlss_architecture(settings.dlss_architecture, prepared.gpu)
+ except Exception:
+ pass
+
+
def persist_gpu_settings(ai_gpu_uuid: str, video_gpu_uuid: str) -> str:
prepared = prepare_runtime()
ai_choices, video_choices = _gpu_choices(prepared)
@@ -267,6 +341,7 @@ def reset_saved_settings() -> tuple:
with _CONFIG_LOCK:
save_settings(CONFIG_PATH, DEFAULT_SETTINGS)
SETTINGS_STATE.current = DEFAULT_SETTINGS
+ _notify_live_effects(DEFAULT_SETTINGS)
message = "All Image, Video, and Frame Interpolation settings were reset to defaults."
return (
*_settings_component_values(DEFAULT_SETTINGS),
@@ -316,6 +391,7 @@ def apply_settings_preset(
f"Import failed: {exc}",
)
SETTINGS_STATE.current = imported
+ _notify_live_effects(imported)
return (
name,
*_settings_component_values(imported),
@@ -328,6 +404,7 @@ class SettingsTab:
ai_gpu_selector: object
video_gpu_selector: object
preview_encoding_selector: object
+ architecture_selector: object
preset_name: object
preset_export: object
preset_import: object
@@ -389,6 +466,19 @@ def build_settings_tab(
"Non-H.264 previews can be slower and larger."
),
)
+ gr.Markdown(
+ "## DLSS 5 Architecture\n"
+ "Selects which Neural Rendering runtime build is used for Image, Video, "
+ "and Frame Interpolation. Auto picks from the AI Processing GPU: "
+ "RTX 20/30 uses Turing+, RTX 40 uses Ada Lovelace+, RTX 50 uses Blackwell+. "
+ "Takes effect on the next render."
+ )
+ architecture_selector = gr.Dropdown(
+ choices=list(DLSS_ARCHITECTURE_CHOICES),
+ value=settings.dlss_architecture,
+ label="DLSS 5 Architecture",
+ info="Auto, Turing and higher, Ada Lovelace and higher, or Blackwell and higher.",
+ )
gr.Markdown(
"## Settings Presets\n"
"Export every adjustable option from the Image, Video, and Frame "
@@ -418,12 +508,13 @@ def build_settings_tab(
preset_status = gr.Markdown("", elem_id="preset-status")
return SettingsTab(
ai_gpu_selector, video_gpu_selector, preview_encoding_selector,
+ architecture_selector,
preset_name, preset_export, preset_import, preset_status
)
-def settings_component_outputs(image_tab, video_tab, frame_tab, settings_tab) -> list[object]:
+def settings_component_outputs(image_tab, video_tab, frame_tab, settings_tab, live_tab) -> list[object]:
return [
*image_tab.neural,
image_tab.model_preset,
@@ -450,22 +541,35 @@ def settings_component_outputs(image_tab, video_tab, frame_tab, settings_tab) ->
settings_tab.ai_gpu_selector,
settings_tab.video_gpu_selector,
settings_tab.preview_encoding_selector,
+ settings_tab.architecture_selector,
+ *live_tab.neural,
+ live_tab.model_preset,
]
-def bind_settings_events(settings_tab, image_tab, video_tab, frame_tab) -> None:
+def bind_settings_events(settings_tab, image_tab, video_tab, frame_tab, live_tab) -> None:
+ video_mirror = [*video_tab.neural, video_tab.model_preset]
+ image_mirror = [*image_tab.neural, image_tab.model_preset]
+ live_mirror = [*live_tab.neural, live_tab.model_preset]
for component in image_tab.settings_inputs:
component.input(
persist_image_settings,
inputs=image_tab.settings_inputs,
- outputs=[*video_tab.neural, video_tab.model_preset],
+ outputs=[*video_mirror, *live_mirror],
queue=False,
)
for component in video_tab.settings_inputs:
component.input(
persist_video_settings,
inputs=video_tab.settings_inputs,
- outputs=[*image_tab.neural, image_tab.model_preset],
+ outputs=[*image_mirror, *live_mirror],
+ queue=False,
+ )
+ for component in live_tab.settings_inputs:
+ component.input(
+ persist_live_settings,
+ inputs=live_tab.settings_inputs,
+ outputs=[*image_mirror, *video_mirror],
queue=False,
)
for component in frame_tab.settings_inputs:
@@ -475,7 +579,7 @@ def bind_settings_events(settings_tab, image_tab, video_tab, frame_tab) -> None:
queue=False,
)
- outputs = settings_component_outputs(image_tab, video_tab, frame_tab, settings_tab)
+ outputs = settings_component_outputs(image_tab, video_tab, frame_tab, settings_tab, live_tab)
settings_tab.preset_export.click(
settings_preset_export_status,
inputs=settings_tab.preset_name,
@@ -499,6 +603,11 @@ def bind_settings_events(settings_tab, image_tab, video_tab, frame_tab) -> None:
inputs=settings_tab.preview_encoding_selector,
queue=False,
)
+ settings_tab.architecture_selector.input(
+ persist_dlss_architecture,
+ inputs=settings_tab.architecture_selector,
+ queue=False,
+ )
reset_outputs = [*outputs, image_tab.status, video_tab.status, frame_tab.status]
image_tab.reset.click(reset_saved_settings, outputs=reset_outputs, queue=False)
diff --git a/src/video/batch.py b/src/video/batch.py
index 6eb661d..2e62b2e 100644
--- a/src/video/batch.py
+++ b/src/video/batch.py
@@ -2,111 +2,79 @@ from __future__ import annotations
import os
import time
+from dataclasses import replace
from pathlib import Path
from typing import Callable, Iterable
-from ..core.jobs import Cancelled, active_job
+from ..core.batch_progress import BatchItemUpdate, BatchProgress
+from ..core.disk_paths import prepare_output_dir
+from ..core.jobs import Cancelled, JobController, active_job
from ..core.paths import LOGS
from ..core.runtime import prepare_runtime
-from .models import (
- ConversionOptions, VideoBatchResult, VideoConversionFailure, VideoConversionSuccess,
-)
-from .processor import _BATCH_CONTEXT, convert_video
+from .models import ConversionOptions, VideoBatchResult, VideoConversionFailure, VideoConversionSuccess
+from . import processor
from .reports import _write_video_batch_manifest
+
def convert_videos(
input_paths: Iterable[str | os.PathLike[str]],
options: ConversionOptions | None = None,
progress: Callable[[float, str], None] | None = None,
+ *, output_dir: str | os.PathLike[str] | None = None,
+ controller: JobController | None = None,
+ on_item_update: Callable[[BatchItemUpdate], None] | None = None,
) -> VideoBatchResult:
- """Convert videos sequentially while holding one cancellable GPU batch slot."""
- options = options or ConversionOptions()
+ """Render in input order, publishing a completed row before starting the next file."""
+ options = replace(options) if options else ConversionOptions()
paths = [Path(path).resolve() for path in input_paths]
if not paths:
raise ValueError("Choose at least one video.")
-
- prepared_runtime = prepare_runtime()
- LOGS.mkdir(exist_ok=True)
+ controller = controller or JobController()
+ reporter = BatchProgress(paths, on_item_update, progress)
+ successes, failures = [], []
stamp = time.strftime("%Y%m%d-%H%M%S") + f"-{time.time_ns() % 1_000_000:06d}"
- successes: list[VideoConversionSuccess] = []
- failures: list[VideoConversionFailure] = []
- cancelled = False
- total = len(paths)
-
- with active_job() as controller:
- if getattr(_BATCH_CONTEXT, "controller", None) is not None:
- raise RuntimeError("A video batch is already active on this worker thread.")
- _BATCH_CONTEXT.controller = controller
- _BATCH_CONTEXT.prepared_runtime = prepared_runtime
- try:
- for index, path in enumerate(paths):
- position = index + 1
- prefix = f"[{position}/{total}] {path.name}"
- if controller.cancel.is_set():
- cancelled = True
- failures.extend(
- VideoConversionFailure(
- queued_index,
- str(queued),
- "Cancelled before rendering.",
- cancelled=True,
+ try:
+ destination = prepare_output_dir(output_dir, default=processor.OUTPUTS)
+ with active_job(controller):
+ if controller.cancel.is_set():
+ raise Cancelled("Stopped before rendering.")
+ prepared = prepare_runtime()
+ processor._BATCH_CONTEXT.controller = controller
+ processor._BATCH_CONTEXT.prepared_runtime = prepared
+ try:
+ for index, path in enumerate(paths):
+ if controller.cancel.is_set():
+ break
+ reporter.advance(index)
+ try:
+ result = processor.convert_video(
+ path, options, lambda value, message, i=index: reporter.advance(i, value, message),
+ output_dir=destination,
)
- for queued_index, queued in enumerate(paths[index:], start=index)
- )
- break
-
- def report_item(
- value: float,
- message: str,
- *,
- item_index: int = index,
- item_prefix: str = prefix,
- ) -> None:
- bounded = min(1.0, max(0.0, float(value)))
- overall = (item_index + bounded) / total
- if progress:
- progress(overall, f"{item_prefix} — {message}")
-
- if progress:
- progress(index / total, f"{prefix} — starting")
- try:
- result = convert_video(path, options, progress=report_item)
- except Cancelled:
- cancelled = True
- failures.append(
- VideoConversionFailure(
- index,
- str(path),
- "Cancelled during rendering.",
- cancelled=True,
- )
- )
- failures.extend(
- VideoConversionFailure(
- queued_index,
- str(queued),
- "Cancelled before rendering.",
- cancelled=True,
- )
- for queued_index, queued in enumerate(paths[position:], start=position)
- )
- break
- except Exception as exc:
- failures.append(VideoConversionFailure(index, str(path), str(exc)))
- if progress:
- progress(position / total, f"{prefix} — failed")
- continue
-
- successes.append(VideoConversionSuccess(index, str(path), result))
- if progress:
- progress(position / total, f"{prefix} — complete")
- finally:
- del _BATCH_CONTEXT.controller
- del _BATCH_CONTEXT.prepared_runtime
-
- manifest_path = _write_video_batch_manifest(
- stamp, options, successes, failures, cancelled
- )
- if progress:
- progress(1.0, "Cancelled" if cancelled else "Complete")
- return VideoBatchResult(successes, failures, cancelled, manifest_path)
+ except Exception as exc:
+ cancelled = isinstance(exc, Cancelled) or controller.cancel.is_set()
+ failures.append(VideoConversionFailure(index, str(path), str(exc), cancelled))
+ reporter.fail(index, exc, cancelled=cancelled)
+ if cancelled:
+ controller.stop()
+ break
+ else:
+ successes.append(VideoConversionSuccess(index, str(path), result))
+ reporter.complete(index, result.output_path, f"{result.frames} frames; report: {result.report_path}")
+ finally:
+ del processor._BATCH_CONTEXT.controller
+ del processor._BATCH_CONTEXT.prepared_runtime
+ cancelled = controller.cancel.is_set()
+ if cancelled:
+ for item in reporter.items:
+ if item.state == "Queued":
+ failures.append(VideoConversionFailure(item.index, item.input_path, "Cancelled before rendering.", True))
+ reporter.skip_from(0)
+ manifest = _write_video_batch_manifest(stamp, options, successes, failures, cancelled,
+ batch_diagnostics=reporter.diagnostics(final=True),
+ output_dir=str(destination))
+ reporter.finish(cancelled=cancelled, manifest_path=manifest)
+ return VideoBatchResult(successes, failures, cancelled, manifest)
+ except BaseException as exc:
+ reporter.finish(cancelled=controller.cancel.is_set(), error=str(exc))
+ raise
diff --git a/src/video/processor.py b/src/video/processor.py
index f2e548b..39c3ce2 100644
--- a/src/video/processor.py
+++ b/src/video/processor.py
@@ -5,22 +5,25 @@ import math
import os
import queue
import shutil
+import subprocess
import threading
import time
from contextlib import nullcontext, suppress
-from dataclasses import asdict
+from dataclasses import asdict, replace
from fractions import Fraction
from pathlib import Path
from typing import Callable
import av
-import cv2
import numpy as np
from ..core import ffmpeg
+from ..core.dlss_architecture import apply_current_dlss_architecture
from ..core.gpu_selection import resolve_runtime_ai_gpu
from ..core.jobs import Cancelled, active_job
-from ..core.naming import output_filename, require_available_output, validate_rename
+from ..core.naming import output_filename, validate_rename
+from ..core.disk_paths import OutputFile, prepare_output_dir
+from ..core.render_metadata import prepare_render_note
from ..core.paths import JOBS, LOGS, OUTPUTS
from ..core.runtime import (
DLSSFrameSession, prepare_runtime, resize_fit, rotate_frame, verify_feature_18,
@@ -64,8 +67,9 @@ def convert_video(
input_path: str | os.PathLike[str],
options: ConversionOptions | None = None,
progress: Callable[[float, str], None] | None = None,
+ *, output_dir: str | os.PathLike[str] | None = None, controller=None,
) -> ConversionResult:
- options = options or ConversionOptions()
+ options = replace(options) if options is not None else ConversionOptions()
source = Path(input_path).resolve()
if not source.is_file():
raise FileNotFoundError(source)
@@ -88,7 +92,7 @@ def convert_video(
if prepared_runtime is None:
prepared_runtime = prepare_runtime()
batch_controller = getattr(_BATCH_CONTEXT, "controller", None)
- job_context = nullcontext(batch_controller) if batch_controller is not None else active_job()
+ job_context = nullcontext(batch_controller) if batch_controller is not None else active_job(controller)
with job_context as controller:
assert controller is not None
@@ -117,10 +121,17 @@ def convert_video(
timings: dict[str, float] = {}
job_dir: Path | None = None
output: Path | None = None
+ output_file: OutputFile | None = None
session: DLSSFrameSession | None = None
gpu: dict | None = resolve_runtime_ai_gpu(
prepared_runtime.gpus, prepared_runtime.runtime_bundle, options.ai_gpu_uuid
)
+ # Stage the selected DLSS Architecture NR build before the worker
+ # spawns (warn-and-continue: render proceeds with the current DLL).
+ try:
+ apply_current_dlss_architecture(gpu)
+ except Exception:
+ pass
video_gpu: dict | None = None
runtime_bundle: dict | None = prepared_runtime.runtime_bundle
encoder = None
@@ -129,6 +140,9 @@ def convert_video(
writer_thread: threading.Thread | None = None
pipeline_stop = threading.Event()
pipeline_errors: queue.Queue[BaseException] = queue.Queue(maxsize=4)
+ producer_stats: dict[str, float | int | str] = {}
+ writer_stats: dict[str, float | int] = {}
+ frame_accounting: dict[str, object] = {"source_verification": "not_required"}
def record_pipeline_error(exc: BaseException) -> None:
pipeline_stop.set()
@@ -139,19 +153,21 @@ def convert_video(
try:
stage_started = time.perf_counter()
- metadata = ffmpeg.probe_video(source, count_mode="metadata")
+ metadata = ffmpeg.probe_video(source, count_mode="metadata", controller=controller)
+ declared_frames = int(metadata["frames"])
+ estimated_frames = declared_frames or max(
+ 1, int(math.ceil(float(metadata["duration"]) * float(metadata["fps"])))
+ )
if preview_frames is not None:
- known_frames = int(metadata["frames"])
- frame_count = min(known_frames, preview_frames) if known_frames else preview_frames
+ estimated_frames = min(estimated_frames, preview_frames) if declared_frames else preview_frames
elif preview_seconds is not None:
- frame_count = ffmpeg.preview_frame_count(source, preview_seconds)
- else:
- frame_count = int(metadata["frames"])
- if frame_count <= 0:
- exact = ffmpeg.probe_video(source, count_mode="exact")
- frame_count = int(exact["frames"])
- metadata["frames"] = frame_count
- metadata["frame_count_source"] = exact["frame_count_source"]
+ estimated_frames = min(estimated_frames, max(
+ 1, int(math.ceil(preview_seconds * float(metadata["fps"])))
+ ))
+ frame_accounting.update(
+ declared_frames=declared_frames, estimated_frames=estimated_frames,
+ metadata_corrected=False,
+ )
timings["probe_seconds"] = time.perf_counter() - stage_started
input_width = int(metadata["width"])
input_height = int(metadata["height"])
@@ -176,7 +192,7 @@ def convert_video(
"color_transfer": metadata.get("color_transfer", "unknown"),
"hdr": bool(metadata.get("hdr", False)),
}
- OUTPUTS.mkdir(exist_ok=True)
+ destination = prepare_output_dir(output_dir, default=OUTPUTS)
LOGS.mkdir(exist_ok=True)
JOBS.mkdir(exist_ok=True)
stamp = time.strftime("%Y%m%d-%H%M%S") + f"-{time.time_ns() % 1_000_000:06d}"
@@ -196,14 +212,14 @@ def convert_video(
else "DLSS5_PREVIEW"
)
)
- output = OUTPUTS / output_filename(
+ output = destination / output_filename(
source,
extension,
options.rename_mode,
options.custom_suffix,
f"{source.stem}_{output_kind}_{stamp}",
)
- require_available_output(output)
+ output_file = OutputFile(output)
temp_video = job_dir / "processed-video.mkv"
native = resolve_native_settings(options)
_report_progress(0.01, f"Starting feature 18 on {gpu['display_name']}")
@@ -227,6 +243,7 @@ def convert_video(
video_gpu is not None,
hdr_mode=effective_hdr,
hdr_metadata=hdr_metadata,
+ preserve_timestamps=not metadata["cfr"],
)
)
except BaseException as exc:
@@ -246,7 +263,7 @@ def convert_video(
input_height=input_height,
output_width=output_width,
output_height=output_height,
- frame_count=frame_count,
+ frame_count=None,
warmup_frames=options.warmup_frames,
factor=factor,
mode=mode,
@@ -297,8 +314,6 @@ def convert_video(
prepared_frames: queue.Queue[object] = queue.Queue(maxsize=queue_slots)
rendered_frames: queue.Queue[object] = queue.Queue(maxsize=queue_slots)
stop_marker = object()
- producer_stats: dict[str, float | int] = {}
- writer_stats: dict[str, float | int] = {}
def put_pipeline(target: queue.Queue[object], item: object) -> bool:
while not pipeline_stop.is_set():
@@ -320,11 +335,29 @@ def convert_video(
stream = container.streams.video[0]
stream.thread_type = "AUTO"
guides = TemporalGuideGenerator(render_width, render_height)
+ first_time: float | None = None
+ rate = float(stream.average_rate or 30)
for index, frame in enumerate(container.decode(stream)):
- if index >= frame_count or pipeline_stop.is_set():
- break
if controller.cancel.is_set():
raise Cancelled("Render stopped by user.")
+ if pipeline_stop.is_set():
+ return
+ if preview_frames is not None and index >= preview_frames:
+ producer_stats["completion_reason"] = "preview_limit"
+ break
+ if preview_seconds is not None:
+ timestamp = (
+ float(frame.pts * stream.time_base)
+ if frame.pts is not None and stream.time_base is not None
+ else decoded / rate
+ )
+ if first_time is None:
+ first_time = timestamp
+ if decoded and timestamp - first_time >= preview_seconds:
+ producer_stats["completion_reason"] = "preview_limit"
+ break
+ if frame.is_corrupt:
+ raise RuntimeError(f"The video decoder marked source frame {index} as corrupt.")
rgba = rotate_frame(
frame.to_ndarray(format="rgba"), metadata["rotation"]
)
@@ -338,12 +371,14 @@ def convert_video(
):
return
decoded += 1
- producer_stats["decoded_frames"] = decoded
+ else:
+ producer_stats["completion_reason"] = "eof"
put_pipeline(prepared_frames, stop_marker)
except BaseException as exc:
record_pipeline_error(exc)
put_pipeline(prepared_frames, stop_marker)
finally:
+ producer_stats["decoded_frames"] = decoded
if container is not None:
with suppress(Exception):
container.close()
@@ -360,6 +395,9 @@ def convert_video(
raw_stream.height = output_height
raw_stream.pix_fmt = "rgba"
raw_stream.time_base = metadata["time_base"]
+ # The raw encoder otherwise quantizes VFR timestamps to the
+ # average frame interval, potentially producing duplicate DTS.
+ raw_stream.codec_context.time_base = metadata["time_base"]
while not pipeline_stop.is_set():
if controller.cancel.is_set():
raise Cancelled("Render stopped by user.")
@@ -381,10 +419,10 @@ def convert_video(
nut.mux(packet)
nut.close()
nut = None
- writer_stats["written_frames"] = written
except BaseException as exc:
record_pipeline_error(exc)
finally:
+ writer_stats["written_frames"] = written
if nut is not None:
with suppress(Exception):
nut.close()
@@ -434,19 +472,17 @@ def convert_video(
raise pipeline_errors.get_nowait()
raise Cancelled("Render stopped by user.")
delivered += 1
+ frame_accounting["processed_frames"] = delivered
now = time.perf_counter()
- if delivered == frame_count or now - last_progress_update >= 0.1:
+ if now - last_progress_update >= 0.1:
_report_progress(
- 0.04 + 0.84 * delivered / frame_count,
- f"DLSS 5 frame {delivered}/{frame_count}",
+ 0.04 + 0.84 * min(1.0, delivered / estimated_frames),
+ f"DLSS 5 frame {delivered} (estimated {estimated_frames})",
)
last_progress_update = now
- if delivered != frame_count:
- raise RuntimeError(
- f"Decoded {delivered} frames instead of the expected {frame_count}; "
- "refusing an incomplete render."
- )
+ if not delivered:
+ raise RuntimeError("The input video contains no decodable frames.")
if not put_pipeline(rendered_frames, stop_marker):
if not pipeline_errors.empty():
raise pipeline_errors.get_nowait()
@@ -457,6 +493,15 @@ def convert_video(
writer_thread = None
if not pipeline_errors.empty():
raise pipeline_errors.get_nowait()
+ frame_accounting.update(
+ decoded_frames=producer_stats.get("decoded_frames"),
+ written_frames=writer_stats.get("written_frames"),
+ completion_reason=producer_stats.get("completion_reason"),
+ )
+ if (producer_stats.get("decoded_frames") != delivered
+ or writer_stats.get("written_frames") != delivered
+ or producer_stats.get("completion_reason") not in {"eof", "preview_limit"}):
+ raise RuntimeError(f"Video pipeline did not deliver every decoded frame: {frame_accounting}")
timings["producer_seconds"] = float(producer_stats.get("seconds", 0.0))
timings["decode_and_guide_seconds"] = timings["producer_seconds"]
timings["dlss_seconds"] = dlss_seconds
@@ -464,6 +509,9 @@ def convert_video(
if encoder.stdin and not encoder.stdin.closed:
encoder.stdin.close()
session.close()
+ frame_accounting["worker_completed_frames"] = session.completed_frames
+ if session.completed_frames != delivered:
+ raise RuntimeError("Native worker completion does not match the processed frame count.")
encoder_code = encoder.wait(timeout=120)
encoder_log_thread.join(timeout=2)
controller.unregister(encoder)
@@ -473,6 +521,32 @@ def convert_video(
"Video encoder failed:\n" + "\n".join(encoder_logs[-40:])
)
+ if producer_stats["completion_reason"] == "eof" and (
+ not declared_frames or declared_frames != delivered
+ ):
+ _report_progress(0.89, "Verifying the source's actual decoded frame count")
+ verification_started = time.perf_counter()
+ frame_accounting["source_verification"] = "pending"
+ try:
+ exact = ffmpeg.probe_video(
+ source, count_mode="exact", strict_decode=True, controller=controller,
+ )
+ frame_accounting["verified_source_frames"] = exact["frames"]
+ if exact["frames"] != delivered:
+ raise RuntimeError(
+ f"Source verification decoded {exact['frames']} frames, but the render "
+ f"decoder delivered {delivered}; refusing an incomplete render."
+ )
+ except Exception:
+ frame_accounting["source_verification"] = "failed"
+ raise
+ finally:
+ timings["source_verification_seconds"] = time.perf_counter() - verification_started
+ frame_accounting["source_verification"] = "passed"
+ frame_accounting["metadata_corrected"] = bool(declared_frames != delivered)
+ else:
+ timings["source_verification_seconds"] = 0.0
+
feature_evidence = verify_feature_18(
session.worker_logs, session.reshade_log_text()
)
@@ -483,13 +557,20 @@ def convert_video(
carrier_create_result = str(feature_evidence["carrier_create_result"])
_report_progress(0.91, "Muxing original audio and metadata")
mux_started = time.perf_counter()
- ffmpeg.final_mux(temp_video, source, output, options.container, controller)
+ metadata_diagnostics = {}
+ render_note = prepare_render_note(options, session.applied_dlss_model_preset, metadata_diagnostics)
+ ffmpeg.final_mux(temp_video, source, output_file.temporary, options.container, controller,
+ render_note=render_note, metadata_diagnostics=metadata_diagnostics)
timings["final_mux_seconds"] = time.perf_counter() - mux_started
timings["muxing_seconds"] = timings["final_mux_seconds"]
verify_started = time.perf_counter()
- verified = ffmpeg.probe_video(output, count_mode="packets")
- if verified["frames"] != delivered:
- verified = ffmpeg.probe_video(output, count_mode="exact")
+ _report_progress(0.96, "Verifying saved output")
+ verified = ffmpeg.probe_video(output_file.temporary, count_mode="packets", controller=controller)
+ if verified["frames"] != delivered or verified["frame_count_source"] != "packets":
+ verified = ffmpeg.probe_video(
+ output_file.temporary, count_mode="exact", strict_decode=True, controller=controller,
+ )
+ frame_accounting["verified_output_frames"] = verified["frames"]
if verified["frames"] != delivered:
raise RuntimeError(
f"Output verification found {verified['frames']} frames instead of "
@@ -508,6 +589,8 @@ def convert_video(
elapsed = time.perf_counter() - started
report = {
"status": "success",
+ "metadata_embedding": metadata_diagnostics,
+ "warnings": [metadata_diagnostics["warning"]] if metadata_diagnostics.get("warning") else [],
"input": str(source),
"output": str(output),
"options": asdict(options),
@@ -525,6 +608,7 @@ def convert_video(
"encoder": selected_encoder,
"encoding_quality": encoding_quality,
"frames_processed": delivered,
+ "frame_accounting": frame_accounting,
"render_mode": (
"full"
if not is_preview
@@ -585,6 +669,9 @@ def convert_video(
}
report_path = LOGS / f"{output.name}.report.json"
report_path.write_text(json.dumps(report, indent=2), encoding="utf-8")
+ if controller.cancel.is_set():
+ raise Cancelled("Render stopped by user.")
+ output_file.publish()
_report_progress(1.0, "Complete — feature 18 confirmed")
return ConversionResult(
str(output),
@@ -625,8 +712,8 @@ def convert_video(
if encoder is not None and encoder.stdin and not encoder.stdin.closed:
with suppress(OSError):
encoder.stdin.close()
- if output and output.exists():
- output.unlink()
+ if output_file is not None:
+ output_file.cleanup(rollback=True)
if was_cancelled and not isinstance(exc, Cancelled):
raise Cancelled("Render stopped by user.") from exc
if isinstance(exc, Cancelled):
@@ -643,9 +730,33 @@ def convert_video(
worker_code=worker_code,
worker_logs=worker_logs,
reshade_lines=reshade_lines,
+ diagnostics={
+ "frame_accounting": frame_accounting,
+ "producer": producer_stats, "writer": writer_stats, "timings": timings,
+ },
)
raise RuntimeError(f"{exc}\nDiagnostic report: {report_path}") from exc
finally:
+ if output_file is not None:
+ output_file.cleanup()
pipeline_stop.set()
+ # Also reap encoders from a failed parallel setup, before `encoder`
+ # has been assigned. Pipes/log readers must not outlive the job.
+ encoder_resources = encoder_setup[0] if "encoder_setup" in locals() and encoder_setup else None
+ if encoder_resources is not None:
+ encoder_process, log_thread = encoder_resources[:2]
+ if encoder_process.poll() is None:
+ encoder_process.terminate()
+ try:
+ encoder_process.wait(timeout=5)
+ except subprocess.TimeoutExpired:
+ encoder_process.kill()
+ encoder_process.wait(timeout=5)
+ log_thread.join(timeout=2)
+ controller.unregister(encoder_process)
+ for stream in (encoder_process.stdin, encoder_process.stdout, encoder_process.stderr):
+ if stream is not None and not stream.closed:
+ with suppress(OSError):
+ stream.close()
if job_dir and job_dir.exists():
shutil.rmtree(job_dir, ignore_errors=True)
diff --git a/src/video/reports.py b/src/video/reports.py
index ddb2d89..027c738 100644
--- a/src/video/reports.py
+++ b/src/video/reports.py
@@ -12,6 +12,7 @@ def _write_video_batch_manifest(
successes: list[VideoConversionSuccess],
failures: list[VideoConversionFailure],
cancelled: bool,
+ *, batch_diagnostics: dict | None = None, output_dir: str | None = None,
) -> str:
LOGS.mkdir(exist_ok=True)
manifest = {
@@ -19,6 +20,8 @@ def _write_video_batch_manifest(
"options": asdict(options),
"successes": [asdict(item) for item in successes],
"failures": [asdict(item) for item in failures],
+ "batch": batch_diagnostics or {},
+ "output_directory": output_dir,
}
manifest_path = LOGS / f"DLSS5_VIDEO_BATCH_{stamp}.manifest.json"
manifest_path.write_text(json.dumps(manifest, indent=2), encoding="utf-8")
diff --git a/src/video/sizing.py b/src/video/sizing.py
index adb88a0..090a8b0 100644
--- a/src/video/sizing.py
+++ b/src/video/sizing.py
@@ -2,7 +2,7 @@ from __future__ import annotations
from ..core import ffmpeg
from ..core.runtime import (
- DLSS_MODEL_PRESETS, NR_PRESETS, NR_STYLES, UPSCALING_MODES,
+ UPSCALING_MODES,
resolve_native_settings, resolve_output_size, resolve_upscaling_mode,
)
from .models import ConversionOptions
diff --git a/src/video/ui.py b/src/video/ui.py
index ffc0659..3b8e99d 100644
--- a/src/video/ui.py
+++ b/src/video/ui.py
@@ -5,10 +5,10 @@ from dataclasses import dataclass
from pathlib import Path
import gradio as gr
+from ..core.batch_ui import BATCH_HEADERS, bind_batch_ui, build_path_controls
from ..core.ffmpeg import hdr_mode_supported
from ..core.ffmpeg.preview import normalize_preview_encoding, resolve_final_preview
-from ..core.jobs import cancel_active_job
from ..core.naming import RENAME_MODES
from ..core.runtime import DLSS_MODEL_PRESETS, NR_PRESETS, NR_STYLES, UPSCALING_MODES
from ..settings.models import (
@@ -137,6 +137,7 @@ def render_video_batch(
rename_mode: str,
custom_suffix: str,
progress=gr.Progress(track_tqdm=False),
+ *, output_dir=None, controller=None, on_item_update=None, direct_disk=False,
) -> tuple[object, list[str], list[list[str]], str]:
paths = normalize_video_paths(input_paths)
if not paths:
@@ -166,9 +167,12 @@ def render_video_batch(
progress(value, desc=message)
try:
- result = convert_videos(paths, options, progress=report)
+ result = convert_videos(paths, options, progress=report, output_dir=output_dir,
+ controller=controller, on_item_update=on_item_update)
except Exception as exc:
traceback.print_exc()
+ if on_item_update is not None:
+ raise
return gr.update(value=None, visible=len(paths) == 1), [], [], f"Failed: {exc}"
ordered_rows: list[tuple[int, list[str]]] = []
@@ -207,10 +211,10 @@ def render_video_batch(
preview_mode = "Auto"
output_preview = None
used_derivative = False
- if len(paths) == 1 and result.successes:
+ if not direct_disk and len(paths) == 1 and result.successes and not result.cancelled:
candidate = result.successes[0].result.output_path
output_preview, used_derivative = resolve_final_preview(
- candidate, preview_mode
+ candidate, preview_mode, controller=controller
)
failed_count = sum(not item.cancelled for item in result.failures)
cancelled_count = sum(
@@ -230,7 +234,7 @@ def render_video_batch(
status += f"\nFirst error: {result.failures[0].error}"
if used_derivative:
status += "\nBrowser preview transcoded to H.264; the original file is unchanged."
- return gr.update(value=output_preview, visible=len(paths) == 1), files, rows, status
+ return gr.update(value=output_preview, visible=not direct_disk and len(paths) == 1), ([] if direct_disk else files), rows, status
@dataclass(slots=True)
class VideoTab:
@@ -253,6 +257,9 @@ class VideoTab:
output_files: object
status: object
results: object
+ input_path: object = None
+ output_path: object = None
+ job_state: object = None
@property
def render_inputs(self) -> list[object]:
@@ -283,6 +290,7 @@ def build_video_tab(settings: UISettings) -> VideoTab:
label="Input video(s)", file_count="multiple", file_types=["video"],
type="filepath", allow_reordering=True, elem_id="video-upload-list",
)
+ input_path, output_path = build_path_controls()
input_preview = gr.Video(label="Input video preview", interactive=False, visible=False)
with gr.Accordion("DLSS 5 Neural Rendering Settings", open=True):
neural = build_neural_controls(settings)
@@ -325,10 +333,10 @@ def build_video_tab(settings: UISettings) -> VideoTab:
label="Rendered video files", file_count="multiple", interactive=False,
elem_id="video-output-list",
)
- status = gr.Textbox(label="Status", interactive=False)
+ status = gr.Textbox(label="Status", interactive=False, lines=5, max_lines=12)
results = gr.Dataframe(
- headers=["Input", "Result", "Output", "Details"],
- datatype=["str", "str", "str", "str"], interactive=False,
+ headers=BATCH_HEADERS,
+ datatype=["str"] * len(BATCH_HEADERS), interactive=False,
label="Batch results", wrap=True,
)
tab = VideoTab(
@@ -336,30 +344,13 @@ def build_video_tab(settings: UISettings) -> VideoTab:
custom_suffix, hdr_mode, preview_frame, preview, render, stop, reset, output_video,
output_files, status, results
)
+ tab.input_path, tab.output_path = input_path, output_path
bind_video_events(tab)
return tab
def bind_video_events(tab: VideoTab) -> None:
- tab.sources.change(
- update_video_preview_mode, inputs=tab.sources,
- outputs=[tab.input_preview, tab.output_video, tab.preview_frame, tab.preview],
- queue=False, show_progress="hidden",
- )
+ bind_batch_ui(tab, render_video_batch, kind="video", preview_mode=update_video_preview_mode,
+ preview_actions=[(tab.preview_frame, preview_one_frame), (tab.preview, preview_video)])
tab.rename_mode.change(rename_suffix_update, inputs=tab.rename_mode, outputs=tab.custom_suffix, queue=False)
tab.codec.change(hdr_mode_update, inputs=tab.codec, outputs=tab.hdr_mode, queue=False)
- tab.render.click(
- render_video_batch, inputs=tab.render_inputs,
- outputs=[tab.output_video, tab.output_files, tab.results, tab.status],
- concurrency_limit=1, show_progress="full", show_progress_on=tab.output_video,
- )
- tab.preview.click(
- preview_video, inputs=tab.preview_inputs, outputs=[tab.output_video, tab.status],
- concurrency_limit=1, show_progress="full", show_progress_on=tab.output_video,
- )
- tab.preview_frame.click(
- preview_one_frame, inputs=tab.preview_inputs, outputs=[tab.output_video, tab.status],
- concurrency_limit=1, show_progress="full", show_progress_on=tab.output_video,
- )
- tab.stop.click(cancel_active_job, outputs=tab.status, queue=False)
-