DLSS 5 Neural Rendering for Images & Video with Frame Generation, RTX Video Super Resolution, HDR, and Color Grading
TypeScript ai-upscalingdlssdlss-5dlss5dlssgframe-generationframe-interpolationimage-enhancementimage-upscalingneural-renderingnvidianvidia-dlss-5nvidia-rtxrtx-videortx-video-super-resolutionvideo-enhancementvideo-upscaling
Add files via upload
Merserk4e8aa4580d60
diff --git a/app.py b/app.pyindex 15d091a..9f6ed43 100644--- a/app.py+++ b/app.py@@ -1,99 +1,209 @@-from __future__ import annotations--from pathlib import Path--# Show LOADING immediately before heavy imports (gradio etc.) to avoid black screen after start.bat-import time as _early_time--try:- from src.core.terminal import TerminalUI as _EarlyTerminalUI-- _early_ui = _EarlyTerminalUI(Path(__file__).with_name("logs"))- _early_ui.enable_vt_mode()- _early_time.sleep(0.05)- _early_ui.render_loading()- _EARLY_TS = _early_time.time()-except Exception:- _early_ui = None- _EARLY_TS = 0.0--import gradio as gr--from src.about.ui import build_about_tab-from src.core.cache_cleanup import (- CACHE_MAX_AGE_SECONDS,- CACHE_SWEEP_INTERVAL_SECONDS,- cleanup_old_caches,-)+from __future__ import annotations++from pathlib import Path+import os++# Resolve the portable Gradio cache without importing the ``src.core`` package:+# importing that package executes its __init__ and may load heavy/media modules.+# Gradio reads GRADIO_TEMP_DIR during import in some modules, so this must be+# the first application setup performed.+_APP_ROOT = Path(__file__).resolve().parent+_APP_GRADIO_TEMP = _APP_ROOT / "temp" / "gradio"+_APP_GRADIO_TEMP.mkdir(parents=True, exist_ok=True)+os.environ["GRADIO_TEMP_DIR"] = str(_APP_GRADIO_TEMP)++# Show LOADING immediately before heavy imports (gradio etc.) to avoid black screen after start.bat+import time as _early_time++try:+ from src.core.terminal import TerminalUI as _EarlyTerminalUI++ _early_ui = _EarlyTerminalUI(Path(__file__).with_name("logs"))+ _early_ui.enable_vt_mode()+ _early_time.sleep(0.05)+ _early_ui.render_loading()+ _EARLY_TS = _early_time.time()+except Exception:+ _early_ui = None+ _EARLY_TS = 0.0++import gradio as gr++from src.about.ui import build_about_tab+from src.core.batch_ui import bind_input_surface_reactivation+from src.core.cache_cleanup import (+ CACHE_MAX_AGE_SECONDS,+ CACHE_SWEEP_INTERVAL_SECONDS,+ cleanup_old_caches,+)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.live.ui import build_live_tab-from src.neural_rendering.image.decoder import initialize_image_runtime-from src.neural_rendering.ui import build_neural_rendering_tab-from src.settings.ui import bind_settings_events, build_settings_tab, initialize_settings-from src.upscale.ui import build_upscale_tab--APP_CSS = r"""-/* Center the About contents in the space below the app title and tab bar. */-#about-content {- display: grid;- place-items: center;- min-height: calc(100dvh - 12rem);- padding: 2rem 1rem;- box-sizing: border-box;- text-align: center;-}--#about-content .about-details {- max-width: 36rem;-}--#about-content h2 {- margin: 0;- font-size: 1.5rem;- font-weight: 600;- line-height: 1.35;-}--#about-content .about-version {- margin: 0.6rem 0 0;-}--#about-content .about-links {- margin-top: 2rem;-}--#about-content .about-links p {- margin: 0 0 1.25rem;-}--#about-content .about-description {- display: block;- margin-top: 0.4rem;- color: var(--body-text-color-subdued);- font-size: 0.875rem;-}--#about-content .about-copyright {- margin: 2rem 0 0;-}--/* Keep project links the same color after visiting them. */-#about-content a,-#about-content a:visited,-#about-content a:hover,-#about-content a:active {- color: #00bfff !important;- opacity: 1 !important;- font-weight: 600;-}--#about-content a:hover {- text-decoration: underline;-}-+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.live.ui import build_live_tab+from src.neural_rendering.image.decoder import initialize_image_runtime+from src.neural_rendering.ui import build_neural_rendering_tab+from src.neural_rendering.composition_ui import bind_composition_mask_events+from src.settings.ui import bind_settings_events, build_settings_tab, initialize_settings+from src.upscale.ui import build_upscale_tab++APP_CSS = r"""+/* Internal lazy-ZIP download targets must always remain in the DOM for the+ one-click browser download trigger, but must never appear as UI controls. */+.internal-zip-download {+ display: none !important;+}++/* Stateful mode trees stay mounted. The radio's client guard stores the latest+ requested mode on <html>; these selectors remain authoritative even if an+ older backend visibility response arrives later. The correct panel can be+ briefly blank while loading, but the wrong panel can never be exposed. */+html[data-neural-rendering-mode="Video"] #neural-rendering-image,+html[data-neural-rendering-mode="Image"] #neural-rendering-video,+html[data-upscale-mode="Video"] #upscale-image,+html[data-upscale-mode="Image"] #upscale-video {+ display: none !important;+}++/* First paint has no dataset yet (the guards only run on mode.change), so the+ rules above match nothing and both panels would stack. Hide the Video panel+ by default; the load-time JS below immediately corrects this when the saved+ Upscale mode is Video, and the change-guards take over afterwards. */+html:not([data-neural-rendering-mode]) #neural-rendering-video,+html:not([data-upscale-mode]) #upscale-video {+ display: none !important;+}++/* Keep the app brand and native top-level tabs on one horizontal header line.+ Gradio measures a visually-hidden copy of the tab buttons for overflow, so+ shift that measurement surface by the same fixed brand width as the visible+ tab list to preserve the built-in overflow menu calculation. */+#main-tabs {+ --main-brand-width: 8.75rem;+}++#main-tabs > .tab-wrapper {+ justify-content: flex-start;+ min-width: 0;+}++#main-tabs > .tab-wrapper::before {+ content: "DLSS 5 VE";+ display: flex;+ flex: 0 0 var(--main-brand-width);+ align-self: stretch;+ align-items: center;+ box-sizing: border-box;+ padding-right: var(--size-3);+ color: var(--body-text-color);+ font-size: var(--text-lg);+ font-weight: 700;+ white-space: nowrap;+}++#main-tabs > .tab-wrapper > .tab-container[role="tablist"] {+ flex: 1 1 auto;+ width: auto !important;+ min-width: 0;+}++#main-tabs > .tab-wrapper > .tab-container.visually-hidden {+ left: var(--main-brand-width);+}++/* Image / Video is a compact second navigation line in workflows that support+ both modes. Keep the native Gradio radio buttons and their behavior, but+ remove the surrounding form/label and prevent the two choices from wrapping. */+#neural-rendering-mode,+#upscale-mode {+ flex: 0 0 auto !important;+ width: max-content !important;+ max-width: 100%;+ min-width: 0 !important;+}++#neural-rendering-mode .wrap,+#upscale-mode .wrap {+ flex-wrap: nowrap !important;+}++/* On Windows Chromium, hovering a compact radio choice can briefly make the+ max-content radio surface report horizontal overflow and expose a native+ scrollbar. These two-choice selectors never need scrolling, so keep the+ overflow paintable without creating a scroll container. */+#neural-rendering-mode,+#upscale-mode,+#neural-rendering-mode .wrap,+#upscale-mode .wrap {+ overflow: visible !important;+ scrollbar-width: none;+}++#neural-rendering-mode::-webkit-scrollbar,+#upscale-mode::-webkit-scrollbar,+#neural-rendering-mode .wrap::-webkit-scrollbar,+#upscale-mode .wrap::-webkit-scrollbar {+ display: none;+}++/* Center the About contents in the space below the compact one-row header. */+#about-content {+ display: grid;+ place-items: center;+ min-height: calc(100dvh - 8rem);+ padding: 2rem 1rem;+ box-sizing: border-box;+ text-align: center;+}++#about-content .about-details {+ max-width: 36rem;+}++#about-content h2 {+ margin: 0;+ font-size: 1.5rem;+ font-weight: 600;+ line-height: 1.35;+}++#about-content .about-version {+ margin: 0.6rem 0 0;+}++#about-content .about-links {+ margin-top: 2rem;+}++#about-content .about-links p {+ margin: 0 0 1.25rem;+}++#about-content .about-description {+ display: block;+ margin-top: 0.4rem;+ color: var(--body-text-color-subdued);+ font-size: 0.875rem;+}++#about-content .about-copyright {+ margin: 2rem 0 0;+}++/* Keep project links the same color after visiting them. */+#about-content a,+#about-content a:visited,+#about-content a:hover,+#about-content a:active {+ color: #00bfff !important;+ opacity: 1 !important;+ font-weight: 600;+}++#about-content a:hover {+ text-decoration: underline;+}+/* Match every image/video drop zone to the 16:9 input preview surface. */.media-upload-surface {aspect-ratio: 16 / 9;@@ -110,10 +220,10 @@ APP_CSS = r"""max-height: 100% !important;overflow-x: hidden !important;overflow-y: auto !important;- overscroll-behavior: contain;- scrollbar-gutter: stable;-}-+ overscroll-behavior: contain;+ scrollbar-gutter: stable;+}+#image-upload-list .file-preview,#video-upload-list .file-preview,#upscale-image-upload-list .file-preview,@@ -121,24 +231,24 @@ APP_CSS = r"""#frame-interpolation-upload-list .file-preview {max-height: none !important;}--/* A single input or output preview is a full 16:9 viewport without scrolling. */++/* A single input or output preview is a full 16:9 viewport without scrolling. */#image-input-preview:has(.gallery-item:only-child),#image-output-preview:has(.gallery-item:only-child),#upscale-image-input-preview:has(.gallery-item:only-child),#upscale-image-output-preview:has(.gallery-item:only-child) {- aspect-ratio: 16 / 9;- height: auto !important;- min-height: 0 !important;-}--#image-input-preview:has(.gallery-item:only-child) .gallery-container,-#image-input-preview:has(.gallery-item:only-child) .grid-wrap,-#image-input-preview:has(.gallery-item:only-child) .grid-container,-#image-input-preview:has(.gallery-item:only-child) .gallery-item,-#image-input-preview:has(.gallery-item:only-child) .thumbnail-lg,-#image-output-preview:has(.gallery-item:only-child) .gallery-container,-#image-output-preview:has(.gallery-item:only-child) .grid-wrap,+ aspect-ratio: 16 / 9;+ height: auto !important;+ min-height: 0 !important;+}++#image-input-preview:has(.gallery-item:only-child) .gallery-container,+#image-input-preview:has(.gallery-item:only-child) .grid-wrap,+#image-input-preview:has(.gallery-item:only-child) .grid-container,+#image-input-preview:has(.gallery-item:only-child) .gallery-item,+#image-input-preview:has(.gallery-item:only-child) .thumbnail-lg,+#image-output-preview:has(.gallery-item:only-child) .gallery-container,+#image-output-preview:has(.gallery-item:only-child) .grid-wrap,#image-output-preview:has(.gallery-item:only-child) .grid-container,#image-output-preview:has(.gallery-item:only-child) .gallery-item,#image-output-preview:has(.gallery-item:only-child) .thumbnail-lg,@@ -152,162 +262,286 @@ APP_CSS = r"""#upscale-image-output-preview:has(.gallery-item:only-child) .grid-container,#upscale-image-output-preview:has(.gallery-item:only-child) .gallery-item,#upscale-image-output-preview:has(.gallery-item:only-child) .thumbnail-lg {- box-sizing: border-box;- height: 100% !important;- min-height: 0 !important;-}-+ box-sizing: border-box;+ height: 100% !important;+ min-height: 0 !important;+}+#image-input-preview:has(.gallery-item:only-child) .grid-wrap,#image-output-preview:has(.gallery-item:only-child) .grid-wrap,#upscale-image-input-preview:has(.gallery-item:only-child) .grid-wrap,#upscale-image-output-preview:has(.gallery-item:only-child) .grid-wrap {- overflow: hidden !important;-}-+ overflow: hidden !important;+}+#image-input-preview:has(.gallery-item:only-child) .grid-container,#image-output-preview:has(.gallery-item:only-child) .grid-container,#upscale-image-input-preview:has(.gallery-item:only-child) .grid-container,#upscale-image-output-preview:has(.gallery-item:only-child) .grid-container {- grid-template-rows: minmax(0, 1fr) !important;- grid-auto-rows: minmax(0, 1fr) !important;-}-+ grid-template-rows: minmax(0, 1fr) !important;+ grid-auto-rows: minmax(0, 1fr) !important;+}+#image-input-preview:has(.gallery-item:only-child) img,#image-output-preview:has(.gallery-item:only-child) img,#upscale-image-input-preview:has(.gallery-item:only-child) img,#upscale-image-output-preview:has(.gallery-item:only-child) img {- height: 100% !important;- width: 100% !important;- object-fit: contain !important;-}--/* The replacement and clear actions share the media preview width. */-.media-input-actions {- width: 100% !important;- max-width: none !important;-}--.media-input-actions > .media-select-button,-.media-input-actions > .media-clear-button {- flex: 1 1 0 !important;- min-width: 0 !important;-}-+ height: 100% !important;+ width: 100% !important;+ object-fit: contain !important;+}++/* The replacement and clear actions share the media preview width. */+.media-input-actions {+ width: 100% !important;+ max-width: none !important;+}++.media-input-actions > .media-select-button,+.media-input-actions > .media-clear-button {+ flex: 1 1 0 !important;+ min-width: 0 !important;+}+.media-select-button button,.media-clear-button {width: 100% !important;}+/* Neural Rendering is structurally split into several Gradio Forms by its+ two slider Rows. Visually merge only those Forms into one native-looking+ block without changing any child component sizing, spacing, or controls. */+.neural-controls-unified {+ background: var(--block-background-fill);+ border-radius: var(--block-radius);+ box-shadow: var(--block-shadow);+ overflow: hidden;+}++.neural-controls-unified::after {+ content: "";+ position: absolute;+ inset: 0;+ box-sizing: border-box;+ border: var(--block-border-width) solid var(--block-border-color);+ border-radius: inherit;+ pointer-events: none;+ z-index: 1;+}++.neural-controls-unified .form {+ background: transparent !important;+ border-color: transparent !important;+ border-radius: 0 !important;+ box-shadow: none !important;+}+"""---def build_app() -> gr.Blocks:- """Build the UI from cached settings and feature-owned tab modules."""- prepared = prepare_runtime()- initialize_image_runtime()- settings, _gpu_warning, ai_gpu_choices, video_gpu_choices = initialize_settings(prepared)+++def build_app() -> gr.Blocks:+ """Build the UI from cached settings and feature-owned tab modules."""+ prepared = prepare_runtime()+ initialize_image_runtime()+ settings, _gpu_warning, ai_gpu_choices, video_gpu_choices = initialize_settings(prepared)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",- elem_id="app-title",- )- with gr.Tabs(selected="neural-rendering"):- with gr.Tab("Neural Rendering", id="neural-rendering"):- neural_rendering_tab = build_neural_rendering_tab(settings)- with gr.Tab("Upscale", id="upscale"):- upscale_tab = build_upscale_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)- with gr.Tab("About", id="about"):- build_about_tab()-- bind_settings_events(- settings_tab,- neural_rendering_tab.image,- neural_rendering_tab.video,- frame_tab,- live_tab,- upscale_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+ 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:+ # One runtime source of truth for the global Neural Rendering processing+ # path. The visible control lives in Settings, while render/start events+ # consume this State so each queued operation snapshots the selected mode.+ processing_engine_state = gr.State(value=settings.nr_gpu_mode)+ nr_mask_state = gr.State(value=None)+ with gr.Tabs(selected="neural-rendering", elem_id="main-tabs"):+ # Keep every tab tree mounted from first paint. Stateful File/Gallery/Video+ # components otherwise get lazily mounted when a tab is first selected,+ # which can briefly restore their construction-time visibility/value state.+ with gr.Tab("Neural Rendering", id="neural-rendering", render_children=True) as neural_root_tab:+ neural_rendering_tab = build_neural_rendering_tab(settings, processing_engine_state, nr_mask_state)+ with gr.Tab("Upscale", id="upscale", render_children=True) as upscale_root_tab:+ upscale_tab = build_upscale_tab(settings)+ with gr.Tab("Frame Interpolation", id="frame-interpolation", render_children=True) as frame_root_tab:+ frame_tab = build_frame_interpolation_tab(settings)+ with gr.Tab("Live", id="live", render_children=True) as live_root_tab:+ live_tab = build_live_tab(settings, processing_engine_state, nr_mask_state)+ with gr.Tab("Settings", id="settings", render_children=True) as settings_root_tab:+ settings_tab = build_settings_tab(+ settings, ai_gpu_choices, video_gpu_choices, processing_engine_state+ )+ with gr.Tab("About", id="about", render_children=True) as about_root_tab:+ build_about_tab()++ # Mounted video elements can otherwise keep decoding/playing while their+ # workflow is hidden. Pause media whenever the user changes top-level tabs.+ pause_media_js = """() => {+ document.querySelectorAll('video').forEach((video) => {+ try { video.pause(); } catch (_) {}+ });+ }"""+ for root_tab in (+ neural_root_tab, upscale_root_tab, frame_root_tab,+ live_root_tab, settings_root_tab, about_root_tab,+ ):+ root_tab.select(+ None, js=pause_media_js, queue=False, show_progress="hidden",+ trigger_mode="always_last",+ )++ # Gradio can restore construction-time child visibility when a mounted+ # top-level tab is shown again. Reconcile only the input surface state;+ # rendered outputs, save controls, and thumbnails are left untouched.+ bind_input_surface_reactivation(neural_root_tab, neural_rendering_tab.image, kind="image", event_name="select")+ bind_input_surface_reactivation(neural_root_tab, neural_rendering_tab.video, kind="video", event_name="select")+ bind_input_surface_reactivation(upscale_root_tab, upscale_tab.image, kind="image", event_name="select")+ bind_input_surface_reactivation(upscale_root_tab, upscale_tab.video, kind="video", event_name="select")+ bind_input_surface_reactivation(frame_root_tab, frame_tab, kind="video", event_name="select")++ bind_settings_events(+ settings_tab,+ neural_rendering_tab.image,+ neural_rendering_tab.video,+ frame_tab,+ live_tab,+ upscale_tab,+ )+ bind_composition_mask_events(+ nr_mask_state,+ neural_rendering_tab.image.composition,+ neural_rendering_tab.video.composition,+ live_tab.composition,+ )++ # First paint has no dataset for the CSS guards above (they only run on+ # mode.change), so both Image and Video panels would stack until the+ # first toggle. Seed both attributes on load from the construction-time+ # radio values; Neural Rendering always starts on Image, Upscale honors+ # the persisted setting. Existing change-guards remain authoritative+ # afterwards. Trees stay mounted; this only affects CSS visibility.+ initial_upscale_mode = settings.upscale_mode if settings.upscale_mode in ("Image", "Video") else "Image"+ init_mode_js = (+ "() => {"+ " try {"+ " var html = document.documentElement;"+ " if (!html.hasAttribute('data-neural-rendering-mode')) {"+ " html.setAttribute('data-neural-rendering-mode', 'Image');"+ " }"+ f" if (!html.hasAttribute('data-upscale-mode')) {{"+ f" html.setAttribute('data-upscale-mode', '{initial_upscale_mode}');"+ " }"+ " var hiddenIds = [];"+ " if (html.getAttribute('data-neural-rendering-mode') === 'Video') {"+ " hiddenIds.push('neural-rendering-image');"+ " } else {"+ " hiddenIds.push('neural-rendering-video');"+ " }"+ " if (html.getAttribute('data-upscale-mode') === 'Video') {"+ " hiddenIds.push('upscale-image');"+ " } else {"+ " hiddenIds.push('upscale-video');"+ " }"+ " hiddenIds.forEach(function (id) {"+ " var panel = document.getElementById(id);"+ " if (panel) { panel.querySelectorAll('video').forEach(function (node) {"+ " try { node.pause(); } catch (_) {}"+ " }); }"+ " });"+ " } catch (_) {}"+ "}"+ )+ demo.load(+ None, js=init_mode_js, queue=False, show_progress="hidden",+ trigger_mode="always_last",+ )+ 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# 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):- ui = _early_ui # type: ignore- # Complete init that early block did not do (listener + redirect)- try:- ui.start_input_listener()- except Exception:- pass- try:- ui.silence_and_redirect()- except Exception:- pass- try:- import atexit-- atexit.register(ui.restore_cursor)- except Exception:- pass- else:- ui = init_console(LOGS)- try:- prepare_runtime()- except Exception as exc:- with open(LOGS / "startup_error.log", "a", encoding="utf-8") as f:- f.write(f"Startup preparation failed: {exc}\n")- raise SystemExit(1) from exc-- demo = build_app()- # Replace loading screen with final DLSS 5 Visual Enhancer splash- try:- ui.render_screen()- except Exception:- pass- try:- demo.queue(default_concurrency_limit=1).launch(- css=APP_CSS,- theme=gr.themes.Ocean(),- server_name="127.0.0.1",- inbrowser=True,- share=False,- allowed_paths=[str(OUTPUTS.resolve())],- show_error=True,- quiet=True,- )- except KeyboardInterrupt:- pass---if __name__ == "__main__":- main()+ global _early_ui # type: ignore+ if "_EARLY_UI" in globals() and _early_ui is not None and getattr(_early_ui, "_alt_active", False):+ ui = _early_ui # type: ignore+ # Complete init that early block did not do (listener + redirect)+ try:+ ui.start_input_listener()+ except Exception:+ pass+ try:+ ui.silence_and_redirect()+ except Exception:+ pass+ try:+ import atexit++ atexit.register(ui.restore_cursor)+ except Exception:+ pass+ else:+ ui = init_console(LOGS)+ try:+ prepared = prepare_runtime()+ except Exception as exc:+ try:+ from src.core import app_log++ app_log.error("startup", f"preparation failed: {exc}")+ except Exception:+ pass+ raise SystemExit(1) from exc+ try:+ from src.core import app_log++ app_log.info("startup", f"ready gpu={prepared.gpu.get('display_name', 'GPU')}")+ except Exception:+ pass++ demo = build_app()+ # Replace loading screen with final DLSS 5 Visual Enhancer splash+ try:+ ui.render_screen()+ except Exception:+ pass+ try:+ demo.queue(default_concurrency_limit=1).launch(+ css=APP_CSS,+ theme=gr.themes.Ocean(),+ server_name="127.0.0.1",+ inbrowser=True,+ share=False,+ allowed_paths=[str(OUTPUTS.resolve())],+ show_error=True,+ quiet=True,+ )+ except KeyboardInterrupt:+ pass+ finally:+ try:+ from src.core import app_log++ app_log.info("app", "stop")+ except Exception:+ pass+++if __name__ == "__main__":+ main()diff --git a/src/about/ui.py b/src/about/ui.pyindex 041526c..3e3a6ee 100644--- a/src/about/ui.py+++ b/src/about/ui.py@@ -6,7 +6,7 @@ def build_about_tab():"""<section class="about-details" aria-labelledby="about-name"><h2 id="about-name">DLSS 5 Visual Enhancer</h2>- <p class="about-version">Version v7.0</p>+ <p class="about-version">Version v8.0</p><div class="about-links"><p><a href="https://github.com/Merserk/dlss5-visual-enhancer"@@ -14,8 +14,9 @@ def build_about_tab():<span class="about-description">Source code, updates, and issue reporting.</span></p><p>- <a href="https://www.patreon.com/MM744"- target="_blank" rel="noopener noreferrer">Support on Patreon</a>+ <a href="https://www.patreon.com/Merserk"+ target="_blank" rel="noopener noreferrer">Patreon</a>+ <span class="about-description">Support the creator and future development.</span></p></div><p class="about-copyright">© Merserk</p>diff --git a/src/core/__init__.py b/src/core/__init__.pyindex 826ec2c..9ee4679 100644--- a/src/core/__init__.py+++ b/src/core/__init__.py@@ -1,12 +1,15 @@from .gpu_detection import clear_gpu_detection_cache, detect_gpusfrom .gpu_selection import detect_gpu, gpu_choice_label, resolve_ai_gpu, resolve_runtime_ai_gpufrom .jobs import Cancelled, JobController, active_job, cancel_active_job-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+from .paths import (+ DLSSG_DIR, DLSSNR_BRIDGE, DLSSNR_CALLER_SHIM, DLSSNR_DIR, FFMPEG, FFPROBE,+ JOBS, LIVE_DIR, LOGS, MPV, NEURAL_RUNTIME, OUTPUTS, ROOT, RUNTIME, YTDLP,+)__all__ = [- "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",+ "Cancelled", "DLSSG_DIR", "DLSSNR_BRIDGE", "DLSSNR_CALLER_SHIM", "DLSSNR_DIR",+ "FFMPEG", "FFPROBE", "JOBS", "LIVE_DIR", "LOGS", "MPV", "NEURAL_RUNTIME",+ "OUTPUTS", "ROOT", "RUNTIME", "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/app_log.py b/src/core/app_log.pynew file mode 100644index 0000000..6add7ee--- /dev/null+++ b/src/core/app_log.py@@ -0,0 +1,203 @@+"""Compact real-time session log. Single writer, one line per event.++Every line is flushed immediately so the log is always current. Only short+compact entries are written: job start/done/cancelled/failed plus ffmpeg,+mpv, worker and startup errors with a bounded tail. Full details go to a+small ``.err`` file only on failure (see :func:`fail`).+"""+from __future__ import annotations++import io+import re+import threading+import time+from pathlib import Path++from .paths import LOGS++_lock = threading.Lock()+_handle: io.TextIOWrapper | None = None+_session_path: Path | None = None++MAX_MSG = 300+MAX_TAIL = 500+RETENTION_DAYS = 14+++def _compact(text: object, limit: int) -> str:+ single = re.sub(r"\s+", " ", str(text or "")).strip()+ return single if len(single) <= limit else single[:limit] + "..."+++def _redact(text: str) -> str:+ return re.sub(r"(https?://[^\s?]+)\?[^\s'\"]+", r"\1?<redacted>", text)+++def sweep_old_sessions(days: int = RETENTION_DAYS) -> None:+ try:+ cutoff = time.time() - days * 86400+ for path in LOGS.glob("app-*.log"):+ try:+ if path.stat().st_mtime < cutoff:+ path.unlink(missing_ok=True)+ except OSError:+ pass+ for path in LOGS.glob("*.err"):+ try:+ if path.stat().st_mtime < cutoff:+ path.unlink(missing_ok=True)+ except OSError:+ pass+ except OSError:+ pass+++def init_session() -> Path:+ """Create ``logs/app-<stamp>.log`` and make it the live session log."""+ global _handle, _session_path+ with _lock:+ if _session_path is not None:+ return _session_path+ LOGS.mkdir(parents=True, exist_ok=True)+ sweep_old_sessions()+ stamp = time.strftime("%Y%m%d-%H%M%S")+ _session_path = LOGS / f"app-{stamp}.log"+ _handle = open(_session_path, "a", encoding="utf-8", errors="replace")+ try:+ (_session_path.with_name("latest.txt")).write_text(+ _session_path.name, encoding="utf-8"+ )+ except OSError:+ pass+ info("app", "start")+ return _session_path+++def session_path() -> str:+ with _lock:+ if _session_path is not None:+ return str(_session_path)+ return str(init_session())+++def _write(level: str, tag: str, msg: str) -> None:+ global _handle+ line = f"[{time.strftime('%H:%M:%S')}] {level} {tag}: {msg}\n"+ with _lock:+ handle = _handle+ if handle is None:+ try:+ handle = open(init_session(), "a", encoding="utf-8", errors="replace")+ except OSError:+ return+ with _lock:+ _handle = handle+ try:+ with _lock:+ handle.write(line)+ handle.flush()+ except (OSError, ValueError):+ pass+++def info(tag: str, msg: object) -> None:+ _write("INFO", tag, _compact(_redact(str(msg)), MAX_MSG))+++def error(tag: str, msg: object, tail: object = "") -> None:+ text = _compact(_redact(str(msg)), MAX_MSG)+ tail_text = _compact(_redact(str(tail)), MAX_TAIL)+ _write("ERROR", tag, f"{text} | tail={tail_text}" if tail_text else text)+++def fail(tag: str, stem: str, err: object, tails: dict[str, object] | None = None) -> Path:+ """Write a small ``.err`` file and log one compact line. Returns the path."""+ safe = re.sub(r"[^A-Za-z0-9_.-]+", "-", str(stem)).strip("-") or "job"+ path = LOGS / f"{safe}.err"+ try:+ LOGS.mkdir(parents=True, exist_ok=True)+ lines = [f"error: {err}"]+ for name, tail in (tails or {}).items():+ if isinstance(tail, (list, tuple)):+ tail = "\n".join(str(line) for line in tail[-40:])+ lines.append(f"--- {name} ---")+ lines.append(str(tail)[-4000:] or "(empty)")+ path.write_text("\n".join(lines), encoding="utf-8", errors="replace")+ except OSError:+ pass+ tail_text = ""+ if tails:+ first = next(iter(tails.values()), "")+ if isinstance(first, (list, tuple)):+ first = "\n".join(str(line) for line in first[-12:])+ tail_text = first+ error(tag, f"{err} | err={path.name}", tail_text)+ return path+++class SessionStream:+ """Minimal stdout/stderr replacement writing raw lines to the session log."""++ def __init__(self) -> None:+ self.encoding = "utf-8"+ self.errors = "replace"++ def write(self, s: str) -> int:+ if not s or not s.strip():+ return len(s)+ with _lock:+ handle = _handle+ if handle is None:+ try:+ init_session()+ except OSError:+ return len(s)+ with _lock:+ handle = _handle+ try:+ with _lock:+ if handle is not None:+ handle.write(s if s.endswith("\n") else s + "\n")+ handle.flush()+ except (OSError, ValueError):+ pass+ return len(s)++ def writelines(self, lines) -> None:+ for line in lines:+ self.write(line)++ def flush(self) -> None:+ pass++ def isatty(self) -> bool:+ return False++ def fileno(self):+ raise io.UnsupportedOperation("SessionStream has no fileno")++ def reconfigure(self, *args, **kwargs) -> None:+ if "encoding" in kwargs:+ self.encoding = kwargs["encoding"]+ if "errors" in kwargs:+ self.errors = kwargs["errors"]++ @property+ def buffer(self):+ raise io.UnsupportedOperation("SessionStream has no buffer")++ def readable(self) -> bool:+ return False++ def writable(self) -> bool:+ return True++ def seekable(self) -> bool:+ return False++ @property+ def closed(self) -> bool:+ return False++ def close(self) -> None:+ passdiff --git a/src/core/batch_ui.py b/src/core/batch_ui.pyindex 14fd69e..6616c92 100644--- a/src/core/batch_ui.py+++ b/src/core/batch_ui.py@@ -1,28 +1,35 @@"""Shared Gradio disk controls and a bounded, session-owned streaming batch UI."""from __future__ import annotations+import jsonimport threadingimport uuidfrom dataclasses import dataclass, field+from pathlib import Pathimport gradio as grfrom .batch_progress import BatchProgress-from .disk_paths import direct_disk_mode, prepare_output_dir, resolve_inputs+from .disk_paths import create_media_archive, direct_disk_mode, prepare_output_dir, resolve_inputsfrom .jobs import JobController, use_job_controller+from .paths import GRADIO_TEMPBATCH_HEADERS = ["File", "State", "Progress", "Elapsed", "Output path", "Details"]def build_path_controls():- source = gr.Textbox(- label="Input path (Optional)", placeholder=r"D:\Render\inputs",- info="Absolute file or folder path. Overrides uploads; folder contents only, sorted by filename.",- )- destination = gr.Textbox(- label="Output path (Optional)", placeholder=r"D:\Render\outputs",- info="Output folder; blank uses the app's outputs folder. Either path disables previews and downloads.",- )+ # Keep the two disk-path fields in their own layout scope. Without an+ # explicit boundary Gradio may merge adjacent encoding/output controls+ # into the same generated Form, producing inconsistent visual grouping+ # between workflows. A default Column is structural only (no panel+ # styling), so the path fields retain their existing native appearance.+ with gr.Column():+ source = gr.Textbox(+ label="Input path (Optional)", placeholder=r"D:\Render\inputs",+ )+ destination = gr.Textbox(+ label="Output path (Optional)", placeholder=r"D:\Render\outputs",+ )return source, destination@@ -55,11 +62,38 @@ def build_media_clear_button(elem_id: str):)+def build_save_controls(kind: str, elem_id: str):+ """Build direct-save and lazy-archive controls for an image/video batch tab."""+ if kind not in {"image", "video"}:+ raise ValueError(f"Unknown media type: {kind}")+ direct = gr.DownloadButton(+ f"Save {kind.title()}", value=None, visible=False, elem_id=f"{elem_id}-direct-save"+ )+ archive = gr.Button("Save as ZIP", visible=False, elem_id=f"{elem_id}-save-zip")+ # Keep the internal download target permanently rendered so the chained JS+ # action can click it after lazy ZIP creation. It is hidden exclusively by+ # application CSS instead of Gradio's visibility state; tab remounts can+ # otherwise leak a `visible="hidden"` DownloadButton into the normal UI.+ archive_download = gr.DownloadButton(+ "Download ZIP",+ value=None,+ visible=True,+ elem_id=f"{elem_id}-zip-download",+ elem_classes=["internal-zip-download"],+ )+ return direct, archive, archive_download+++def _stable_visible(show: bool):+ """Keep stateful media components mounted even when they are not displayed."""+ return True if show else "hidden"++def input_surface_updates(show_preview: bool, input_path: str | None = None):- """Swap the drop zone for its preview while uploads are the active input mode."""+ """Atomically swap upload/preview actions without unmounting either tree."""upload_enabled = not bool(str(input_path or "").strip())return (- gr.update(visible=not show_preview, interactive=upload_enabled),+ gr.update(visible=_stable_visible(not show_preview), interactive=upload_enabled),gr.update(visible=show_preview and upload_enabled),)@@ -73,6 +107,7 @@ class BatchRun:self.error = ""self.thread = Noneself.is_preview = False+ self.is_auto_preview = Falsedef start(self, operation):def work():@@ -94,12 +129,41 @@ class BatchRun:self.thread.join()+@dataclass(frozen=True, slots=True)+class _FileIdentity:+ path: str+ device: int+ inode: int+ size: int+ mtime_ns: int+++@dataclass(slots=True)+class _DownloadState:+ kind: str+ input_count: int+ paths: tuple[str, ...]+ identities: tuple[_FileIdentity, ...]+ output_dir: str+ archive_prefix: str+ archive_path: str | None = None+ archive_lock: threading.Lock = field(default_factory=threading.Lock)++@dataclassclass _View:lock: threading.RLock = field(default_factory=threading.RLock)job: BatchRun | None = Nonerevision: int = 0disk: bool = False+ download: _DownloadState | None = None+ # Input media preparation can be slower than navigation (RAW/HEIF/image+ # thumbnails, video inspection). Track readiness independently so a tab+ # reactivation can hide stale construction-time controls until the accepted+ # refresh has completed.+ input_ready: bool = True+ auto_revision: int = 0+ auto_preview_path: str | None = None_views: dict[str, _View] = {}@@ -111,44 +175,247 @@ def _view(key):return _views.setdefault(key, _View())+def _discard_auto_preview(path: str | None) -> None:+ """Remove only previews owned by the app's managed Gradio temp tree."""+ if not path:+ return+ try:+ candidate = Path(path).resolve()+ root = GRADIO_TEMP.resolve()+ if candidate.is_relative_to(root):+ candidate.unlink(missing_ok=True)+ try:+ candidate.parent.rmdir()+ except OSError:+ pass+ except (OSError, RuntimeError, ValueError):+ pass+++def _managed_preview_path(media) -> str | None:+ """Find a generated media path inside a Gradio update/value structure."""+ if isinstance(media, (str, Path)):+ try:+ candidate = Path(media).resolve()+ if candidate.is_file() and candidate.is_relative_to(GRADIO_TEMP.resolve()):+ return str(candidate)+ except (OSError, RuntimeError, ValueError):+ return None+ return None+ if isinstance(media, dict):+ return _managed_preview_path(media.get("value"))+ if isinstance(media, (list, tuple)):+ for item in media:+ if path := _managed_preview_path(item):+ return path+ return None++def release_view(key):with _views_lock:view = _views.pop(key, None)if view and view.job:view.job.controller.stop()+ if view:+ _discard_auto_preview(view.auto_preview_path)+++def _uploaded_source_count(value) -> int:+ if isinstance(value, str):+ return 1 if value.strip() else 0+ try:+ return len(list(value or []))+ except TypeError:+ return 1 if value else 0+++def _has_uploaded_sources(value) -> bool:+ return _uploaded_source_count(value) > 0+++def bind_input_surface_reactivation(trigger, tab, *, kind: str, event_name: str | None = None):+ """Re-assert one tab's upload/preview visibility after navigation.++ Gradio may restore a child component's construction-time visibility when a+ parent Tab/Column is hidden and shown again. Uploaded values themselves are+ still correct, so this callback intentionally changes *visibility only*: it+ never regenerates thumbnails, clears outputs, invalidates downloads, or+ mutates render state. If an input refresh is still running, both the upload+ and preview are kept hidden until that accepted refresh publishes its final+ state.+ """+ if kind not in {"image", "video"}:+ raise ValueError(f"Unknown media type: {kind}")+ input_media = tab.input_gallery if kind == "image" else tab.input_preview+ preview_buttons = [+ component for name in ("preview_frame", "preview")+ if (component := getattr(tab, name, None)) is not None+ ]+ def reconcile(key, input_path, output_path, sources):+ view = _view(key)+ disk = direct_disk_mode(input_path, output_path)+ source_count = _uploaded_source_count(sources)+ has_sources = source_count > 0+ with view.lock:+ ready = view.input_ready++ # Direct-disk mode intentionally uses the original upload/path surface+ # rules and suppresses browser previews/downloads.+ if disk:+ return (+ gr.update(visible=True),+ gr.update(visible="hidden"),+ gr.update(visible=False),+ *[gr.update(visible=False) for _ in preview_buttons],+ )+ if not has_sources:+ return (+ gr.update(visible=True),+ gr.update(visible="hidden"),+ gr.update(visible=False),+ *[gr.update(visible=False) for _ in preview_buttons],+ )+ if not ready:+ # The source exists but its preview is still being prepared. Never+ # flash the upload box as if the workspace were empty.+ return (+ gr.update(visible="hidden"),+ gr.update(visible="hidden"),+ gr.update(visible=False),+ *[gr.update(visible=False) for _ in preview_buttons],+ )+ return (+ gr.update(visible="hidden"),+ gr.update(visible=True),+ gr.update(visible=True),+ *[gr.update(visible=source_count == 1) for _ in preview_buttons],+ )++ inputs = [tab.job_state, tab.input_path, tab.output_path, tab.sources]+ outputs = [tab.sources, input_media, tab.input_actions, *preview_buttons]+ kwargs = dict(+ inputs=inputs, outputs=outputs, queue=False, show_progress="hidden",+ trigger_mode="always_last",+ )+ if event_name is None:+ return trigger.then(reconcile, **kwargs)+ return getattr(trigger, event_name)(reconcile, **kwargs)+++def _identity(path: str) -> _FileIdentity:+ source = Path(path).resolve()+ stat = source.stat()+ if not source.is_file():+ raise ValueError(f"Rendered output is no longer a file: {source}")+ return _FileIdentity(+ str(source), int(stat.st_dev), int(stat.st_ino), int(stat.st_size), int(stat.st_mtime_ns)+ )-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."""++def _validate_download_state(state: _DownloadState) -> None:+ current = tuple(_identity(path) for path in state.paths)+ if current != state.identities:+ raise ValueError(+ "One or more rendered outputs changed or were removed after rendering. "+ "Render the batch again before creating its ZIP."+ )+++def _archive_trigger_js(elem_id: str) -> str:+ encoded = json.dumps(elem_id)+ return f"""() => {{+ const root = document.getElementById({encoded});+ const target = root && (root.matches('a,button') ? root : root.querySelector('a,button'));+ if (target) target.click();+ }}"""+++def bind_batch_ui(+ tab, render_function, *, kind, preview_mode, archive_prefix: str, preview_actions=(),+ realtime_preview=None, realtime_components=(),+):+ """Bind one batch tab, including direct single-file and lazy multi-file downloads.++ Render callbacks return four values: display media, successful master output+ paths, batch rows, and status text. The archive is intentionally *not* part+ of rendering; it is created only when the user clicks Save as ZIP.+ """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] if is_image else [tab.output_video]- archive_download = getattr(tab, "zip_download", None)- if archive_download is not None:- media.append(tab.zip_download)+ display_media = tab.output_gallery if is_image else tab.output_video+ direct_save = tab.save_download+ archive_button = tab.zip_button+ archive_download = tab.zip_download+ media_outputs = [display_media, direct_save, archive_button, archive_download]preview_buttons = [button for button, _fn in preview_actions]controls = [tab.sources, tab.select_source, tab.clear_source, tab.input_path, tab.output_path,tab.render, tab.reset, *preview_buttons,]- outputs = [*media, tab.results, tab.status, *controls]+ outputs = [*media_outputs, tab.results, tab.status, *controls]path_inputs = [tab.job_state, tab.input_path, tab.output_path]+ def empty_save_controls():+ return (+ gr.update(value=None, visible=False),+ gr.update(visible=False),+ gr.update(value=None),+ )+def empty_media(disk):- values = [gr.update(value=None, visible=not disk)]- if archive_download is not None:- values.append(gr.update(value=None, visible=False))- return values+ return [+ gr.update(value=None, visible=not disk),+ *empty_save_controls(),+ ]- def completed_media(result, disk):+ def completed_media(result, disk, *, input_count: int, destination: str, view: _View):if result is None or disk:+ with view.lock:+ view.download = Nonereturn empty_media(disk)- values = list(result[:len(media)])- if archive_download is not None:- archive_path = values[-1]- values[-1] = gr.update(value=archive_path, visible=bool(archive_path))- return values++ display_value, successful_paths, _rows, _status = result+ successful_paths = tuple(str(Path(path).resolve()) for path in (successful_paths or []))+ if not successful_paths:+ with view.lock:+ view.download = None+ return [display_value, *empty_save_controls()]++ try:+ identities = tuple(_identity(path) for path in successful_paths)+ except Exception:+ # Rendering reported a success that cannot be downloaded anymore.+ # Keep the media preview/result rows, but never expose a stale save action.+ with view.lock:+ view.download = None+ return [display_value, *empty_save_controls()]++ state = _DownloadState(+ kind=kind,+ input_count=input_count,+ paths=successful_paths,+ identities=identities,+ output_dir=destination,+ archive_prefix=archive_prefix,+ )+ with view.lock:+ view.download = state++ if input_count == 1:+ return [+ display_value,+ gr.update(value=successful_paths[0], visible=True, label=f"Save {kind.title()}"),+ gr.update(visible=False),+ gr.update(value=None),+ ]+ return [+ display_value,+ gr.update(value=None, visible=False),+ gr.update(visible=True),+ gr.update(value=None),+ ]def control_updates(busy, input_path):upload_enabled = not busy and not bool(str(input_path or "").strip())@@ -166,39 +433,52 @@ def bind_batch_ui(tab, render_function, *, kind, preview_mode, preview_actions=(view.revision += 1revision = view.revisionview.disk = disk+ view.download = None+ view.input_ready = Falsebusy = 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) + 2)+ with view.lock:+ if revision == view.revision:+ view.input_ready = True+ return [gr.skip()] * (1 + len(media_outputs) + len(preview_buttons) + 2)show_input_media = False- 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])- show_input_media = bool(previews)- values = [gr.update(value=previews, visible=show_input_media), *empty_media(False)]- else:- input_update, output_update, *buttons = preview_mode(*args)- sources = args[0]- show_input_media = bool([sources] if isinstance(sources, str) else list(sources or []))- values = [input_update, output_update, *empty_media(False)[1:], *buttons]+ try:+ if disk:+ values = [gr.update(value=None, visible="hidden"), *empty_media(True),+ *[gr.update(visible=False) for _ in preview_buttons]]+ elif is_image:+ previews = preview_mode(args[0])+ show_input_media = bool(previews)+ source_count = _uploaded_source_count(args[0])+ values = [+ gr.update(value=previews, visible=_stable_visible(show_input_media)),+ *empty_media(False),+ *[gr.update(visible=source_count == 1) for _ in preview_buttons],+ ]+ else:+ input_update, output_update, *buttons = preview_mode(*args)+ sources = args[0]+ show_input_media = bool([sources] if isinstance(sources, str) else list(sources or []))+ values = [input_update, output_update, *empty_save_controls(), *buttons]+ except BaseException:+ with view.lock:+ if revision == view.revision:+ view.input_ready = True+ raisewith view.lock:if revision != view.revision:- return [gr.skip()] * (1 + len(media) + len(preview_buttons) + 2)+ return [gr.skip()] * (1 + len(media_outputs) + len(preview_buttons) + 2)+ view.input_ready = Truesource_update, actions_update = input_surface_updates(show_input_media, input_path)- return [- *values,- source_update,- actions_update,- ]+ return [*values, source_update, actions_update]source_args = [tab.sources]if hasattr(tab, "target_fps"):source_args += [tab.target_fps, tab.engine]refresh_outputs = [- input_media, *media, *preview_buttons,+ input_media, *media_outputs, *preview_buttons,tab.sources, tab.input_actions,]tab.select_source.upload(@@ -216,8 +496,14 @@ def bind_batch_ui(tab, render_function, *, kind, preview_mode, preview_actions=()for component in (tab.sources, tab.input_path, tab.output_path):# Textbox changes also cover pasted paths and programmatic value updates.- component.change(refresh, inputs=[*path_inputs, *source_args], outputs=refresh_outputs,- queue=False, show_progress="hidden")+ component.change(+ refresh,+ inputs=[*path_inputs, *source_args],+ outputs=refresh_outputs,+ queue=False,+ show_progress="hidden",+ trigger_mode="always_last",+ )def stream(key, input_path, output_path, *args):view = _view(key)@@ -226,10 +512,12 @@ def bind_batch_ui(tab, render_function, *, kind, preview_mode, preview_actions=(raise gr.Error("A job is already running in this tab.")job = BatchRun([])view.job = job+ view.download = Noneview.disk = direct_disk_mode(input_path, output_path)disk = view.diskdestination = ""started = False+ paths = []try:# Resolve once. Direct paths never become a gr.File/gr.Video/Gallery value.paths = resolve_inputs(input_path, args[0], kind)@@ -237,12 +525,14 @@ def bind_batch_ui(tab, render_function, *, kind, preview_mode, preview_actions=(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 = Truewhile not job.done.wait(.25):@@ -253,11 +543,13 @@ def bind_batch_ui(tab, render_function, *, kind, preview_mode, preview_actions=(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,+ yield (*[gr.skip() for _ in media_outputs], row_values, status,*[gr.skip() for _ in controls])job.close()row_values, status = job.progress.display(destination)- final_media = completed_media(job.result, disk)+ final_media = completed_media(+ job.result, disk, input_count=len(paths), destination=destination, view=view+ )if job.error and job.error not in status:status += f"\n{job.error}"with view.lock:@@ -267,6 +559,8 @@ def bind_batch_ui(tab, render_function, *, kind, preview_mode, preview_actions=(except Exception as exc:job.progress.finish(cancelled=job.controller.cancel.is_set(), error=str(exc))row_values, status = job.progress.display(destination)+ with view.lock:+ view.download = Noneyield (*empty_media(view.disk), row_values, status, *control_updates(False, input_path))finally:if started:@@ -275,8 +569,14 @@ def bind_batch_ui(tab, render_function, *, kind, preview_mode, preview_actions=(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")+ 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)@@ -286,43 +586,259 @@ def bind_batch_ui(tab, render_function, *, kind, preview_mode, preview_actions=(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 create_lazy_archive(key):+ view = _view(key)+ with view.lock:+ state = view.download+ busy = view.job is not None and not view.job.done.is_set()+ if busy:+ raise gr.Error("Wait for the current render to finish before creating its ZIP.")+ if state is None or state.input_count < 2 or not state.paths:+ raise gr.Error("There is no completed multi-file batch to save as ZIP.")+ with state.archive_lock:+ try:+ _validate_download_state(state)+ if state.archive_path and Path(state.archive_path).is_file():+ archive_path = state.archive_path+ else:+ archive_path = create_media_archive(+ state.paths, state.output_dir, state.archive_prefix+ )+ if not archive_path:+ raise RuntimeError("The ZIP archive was not created.")+ state.archive_path = archive_path+ return gr.update(value=archive_path)+ except gr.Error:+ raise+ except Exception as exc:+ raise gr.Error(f"Could not create ZIP: {exc}") from exc++ archive_event = archive_button.click(+ create_lazy_archive,+ inputs=tab.job_state,+ outputs=archive_download,+ concurrency_limit=1,+ trigger_mode="once",+ show_progress="minimal",+ )+ archive_event.success(+ fn=None,+ js=_archive_trigger_js(archive_download.elem_id),+ queue=False,+ show_progress="hidden",+ )+def guarded_preview(function):def preview(key, input_path, output_path, *args, progress=gr.Progress(track_tqdm=False)):- def empty_preview(video, status):- values = (video, status)- if archive_download is not None:- values += (gr.update(value=None, visible=False),)- return values+ def empty_preview(media, status):+ return (media, status, *empty_save_controls())+view = _view(key)if direct_disk_mode(input_path, output_path):- return empty_preview(gr.update(value=None, visible=False), "Previews are disabled when a path is supplied.")+ with view.lock:+ view.download = None+ yield empty_preview(+ gr.update(value=None, visible=False),+ "Previews are disabled when a path is supplied.",+ )+ returnwith view.lock:if view.job is not None:raise gr.Error("A job is already running in this tab.")job = BatchRun([])job.is_preview = Trueview.job = job+ view.download = Nonerevision = view.revision+ # Hide every save action before expensive preview work starts so a+ # previous completed render can never be downloaded as if it were+ # the new preview/result.+ yield (gr.skip(), gr.skip(), *empty_save_controls())try:with use_job_controller(job.controller):result = function(*args, progress=progress)if job.controller.cancel.is_set():- return empty_preview(gr.update(value=None, visible=False), "Preview cancelled.")+ yield empty_preview(gr.update(value=None, visible=False), "Preview cancelled.")+ returnwith view.lock:- if view.revision != revision or view.disk:- return (gr.skip(),) * (2 + int(archive_download is not None))- if archive_download is not None:- return (*result, gr.update(value=None, visible=False))- return result+ stale = view.revision != revision or view.disk+ if stale:+ yield (gr.skip(),) * 5+ return+ yield (*result, *empty_save_controls())finally:job.done.set()with view.lock:if view.job is job:view.job = Nonereturn preview+for button, function in preview_actions:- button.click(guarded_preview(function), inputs=[*path_inputs, *tab.preview_inputs],- outputs=[tab.output_video, tab.status, *([archive_download] if archive_download is not None else [])],- concurrency_limit=None, show_progress="hidden")+ button.click(+ guarded_preview(function),+ inputs=[*path_inputs, *tab.preview_inputs],+ outputs=[display_media, tab.status, direct_save, archive_button, archive_download],+ concurrency_limit=None,+ show_progress="hidden",+ )++ if realtime_preview is None:+ return++ # User input invalidates the current preview synchronously, even while its+ # queued backend call is still evaluating. Slider previews trigger on+ # release so dragging never launches one GPU job per intermediate value;+ # dropdown/radio changes trigger immediately.+ auto_token = gr.State(0)++ def invalidate_auto_preview(key, sources):+ view = _view(key)+ old_path = None+ with view.lock:+ view.auto_revision += 1+ token = view.auto_revision+ job = view.job+ if job is not None and job.is_preview and not job.done.is_set():+ job.controller.stop()+ if _uploaded_source_count(sources) != 1:+ old_path = view.auto_preview_path+ view.auto_preview_path = None+ _discard_auto_preview(old_path)+ return token++ def automatic_preview(+ key, token, input_path, output_path, *args,+ progress=gr.Progress(track_tqdm=False),+ ):+ unchanged = (gr.skip(),) * 5+ view = _view(key)+ try:+ token = int(token)+ except (TypeError, ValueError):+ yield unchanged+ return+ if direct_disk_mode(input_path, output_path) or _uploaded_source_count(args[0]) != 1:+ yield unchanged+ return++ with view.lock:+ stale = token != view.auto_revision+ existing = view.job+ if stale:+ yield unchanged+ return+ if existing is not None and not existing.done.is_set():+ if not existing.is_preview:+ # Production output owns the tab and the process-wide GPU slot.+ yield unchanged+ return+ existing.controller.stop()+ if not existing.done.wait(5.0):+ yield unchanged+ return++ with view.lock:+ stale = token != view.auto_revision or view.disk+ busy = view.job is not None and not view.job.done.is_set()+ if not stale and not busy:+ job = BatchRun([])+ job.is_preview = True+ job.is_auto_preview = True+ view.job = job+ view.download = None+ source_revision = view.revision+ if stale or busy:+ yield unchanged+ return++ # Preserve the current visual until its replacement is ready, but hide+ # save actions immediately because this is an ephemeral preview.+ yield (gr.skip(), gr.skip(), *empty_save_controls())+ preview_dir = GRADIO_TEMP / "neural-realtime" / str(key)+ preview_dir.mkdir(parents=True, exist_ok=True)+ try:+ # A short quiet window coalesces keyboard/radio changes that arrive+ # together (for example a preset selection updating several fields).+ if job.controller.cancel.wait(0.18):+ yield unchanged+ return+ with view.lock:+ stale = token != view.auto_revision+ if stale:+ yield unchanged+ return+ with use_job_controller(job.controller):+ result = realtime_preview(+ *args,+ progress=progress,+ output_dir=preview_dir,+ controller=job.controller,+ ephemeral_preview=True,+ )+ with view.lock:+ stale = (+ job.controller.cancel.is_set()+ or token != view.auto_revision+ or source_revision != view.revision+ or view.disk+ )+ if stale:+ _discard_auto_preview(_managed_preview_path(result[0]))+ yield unchanged+ return++ managed_path = _managed_preview_path(result[0])+ with view.lock:+ old_path = view.auto_preview_path+ view.auto_preview_path = managed_path+ if old_path != managed_path:+ _discard_auto_preview(old_path)+ yield (*result, *empty_save_controls())+ except Exception as exc:+ with view.lock:+ stale = job.controller.cancel.is_set() or token != view.auto_revision+ if stale:+ yield unchanged+ else:+ yield (+ gr.skip(),+ f"Automatic preview failed: {exc}",+ *empty_save_controls(),+ )+ finally:+ job.done.set()+ with view.lock:+ if view.job is job:+ view.job = None++ triggers = [tab.sources.change]+ triggers.extend(+ component.release if isinstance(component, gr.Slider) else component.input+ for component in realtime_components+ )+ invalidated = gr.on(+ triggers=triggers,+ fn=invalidate_auto_preview,+ inputs=[tab.job_state, tab.sources],+ outputs=auto_token,+ queue=False,+ show_progress="hidden",+ trigger_mode="always_last",+ api_visibility="private",+ )+ invalidated.then(+ automatic_preview,+ inputs=[+ tab.job_state, auto_token, tab.input_path, tab.output_path,+ *tab.preview_inputs,+ ],+ outputs=[display_media, tab.status, direct_save, archive_button, archive_download],+ concurrency_limit=1,+ concurrency_id="neural-realtime-preview",+ trigger_mode="always_last",+ show_progress="hidden",+ api_visibility="private",+ )diff --git a/src/core/cache_cleanup.py b/src/core/cache_cleanup.pyindex 9be819a..fe9fcfc 100644--- a/src/core/cache_cleanup.py+++ b/src/core/cache_cleanup.py@@ -1,46 +1,38 @@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.+"""Automatic cleanup of stale app-local Gradio caches and job leftovers.++Official Gradio cleanup (``gr.Blocks(delete_cache=...)``) handles tracked+session files while the server runs and on graceful shutdown. Crash-orphaned+files are covered here by an age-gated startup sweep over only this install's+portable locations:++1. ``<app>/temp/gradio`` (Gradio uploads/cache plus UI-owned temp artifacts).+2. Orphaned ``<app>/jobs/*`` per-render dirs left behind by killed runs.++No system/user temp directory is inspected or migrated. This build is isolated+from previous installations. Final deliverables in ``OUTPUTS/`` and anything+in ``LOGS/`` are never touched. Every failure is swallowed per-entry so+cleanup can never block startup."""import osimport shutil-import tempfileimport timefrom pathlib import Path-from .paths import JOBS, LOGS+from .paths import GRADIO_TEMP, JOBS# 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 = 3600CACHE_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"+ """Return this installation's fixed app-local Gradio temp directory."""try:- resolved = candidate.resolve()+ resolved = GRADIO_TEMP.resolve()except OSError:return Nonereturn resolved if resolved.is_dir() else None@@ -106,29 +98,6 @@ def sweep_dir_by_age(root: Path, max_age_seconds: int) -> tuple[int, int]: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,@@ -150,14 +119,6 @@ def cleanup_old_caches(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)@@ -168,16 +129,13 @@ def cleanup_old_caches(elapsed = time.time() - startedtry:- 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:+ from . import app_log++ app_log.info(+ "cache",+ f"removed={removed_total} freed_mb={freed_total / (1024 * 1024):.1f} "+ f"elapsed_s={elapsed:.1f}",+ )+ except Exception:passreturn {"removed": removed_total, "freed_bytes": freed_total}diff --git a/src/core/disk_paths.py b/src/core/disk_paths.pyindex 7528f6a..68e6a4a 100644--- a/src/core/disk_paths.py+++ b/src/core/disk_paths.py@@ -7,6 +7,8 @@ import tempfileimport timeimport uuidimport zipfile+import queue+import threadingfrom pathlib import Pathfrom .naming import require_available_output@@ -121,6 +123,103 @@ class OutputFile:pass++class StreamingMediaArchive:+ """Build an uncompressed ZIP on one bounded worker while later renders run."""++ def __init__(self, output_dir, prefix: str, *, controller=None) -> None:+ destination = prepare_output_dir(output_dir)+ stamp = time.strftime("%Y%m%d-%H%M%S")+ self.archive_path = destination / f"{prefix}_{stamp}_{uuid.uuid4().hex[:8]}.zip"+ self.output_file = OutputFile(self.archive_path)+ self.controller = controller+ self.items: queue.Queue[Path | None] = queue.Queue(maxsize=1)+ self.error: BaseException | None = None+ self.cancelled = False+ self._closed = False+ self.added = 0+ self.thread = threading.Thread(target=self._run, name="media-archive-writer", daemon=True)+ self.thread.start()++ def _is_cancelled(self) -> bool:+ return self.cancelled or (self.controller is not None and self.controller.cancel.is_set())++ def _run(self) -> None:+ used_names: set[str] = set()+ try:+ with zipfile.ZipFile(+ self.output_file.temporary, "w", compression=zipfile.ZIP_STORED, allowZip64=True,+ ) as archive:+ while True:+ source = self.items.get()+ if source is None:+ break+ if self._is_cancelled():+ self.cancelled = True+ break+ candidate = source.name+ index = 2+ while candidate.casefold() in used_names:+ candidate = f"{source.stem}_{index}{source.suffix}"+ index += 1+ used_names.add(candidate.casefold())+ info = zipfile.ZipInfo.from_file(source, arcname=candidate)+ info.compress_type = zipfile.ZIP_STORED+ with source.open("rb") as input_stream, archive.open(+ info, "w", force_zip64=True,+ ) as output_stream:+ while True:+ if self._is_cancelled():+ self.cancelled = True+ break+ chunk = input_stream.read(1024 * 1024)+ if not chunk:+ break+ output_stream.write(chunk)+ if self.cancelled:+ break+ except BaseException as exc:+ self.error = exc++ def add(self, path: str | os.PathLike[str]) -> bool:+ source = Path(path).resolve()+ while not self._is_cancelled() and self.error is None:+ try:+ self.items.put(source, timeout=0.1)+ self.added += 1+ return True+ except queue.Full:+ continue+ return False++ def close(self, *, publish: bool = True) -> str | None:+ if self._closed:+ return str(self.archive_path) if self.archive_path.exists() else None+ self._closed = True+ while self.thread.is_alive():+ try:+ self.items.put(None, timeout=0.1)+ break+ except queue.Full:+ if self.error is not None or self._is_cancelled():+ self.cancelled = self.cancelled or self._is_cancelled()+ continue+ self.thread.join()+ try:+ if self.error is not None:+ raise self.error+ if self._is_cancelled() or self.cancelled or not publish or self.added == 0:+ return None+ self.output_file.publish()+ return str(self.archive_path)+ finally:+ self.output_file.cleanup()++ def abort(self) -> None:+ self.cancelled = True+ self.close()++def create_media_archive(paths,output_dir,diff --git a/src/core/ffmpeg/__init__.py b/src/core/ffmpeg/__init__.pyindex 04ae051..0c01cbb 100644--- a/src/core/ffmpeg/__init__.py+++ b/src/core/ffmpeg/__init__.py@@ -5,7 +5,8 @@ from .codecs import (validate_codec_container,)from .encoder import probe_nvenc_codecs, resolve_video_gpu, start_encoder-from .mux import final_mux+from .mux import final_mux, prepare_mux_comment, verify_mux_comment+from .nut import RawVideoPacketMuxerfrom .preview import (DEFAULT_PREVIEW_ENCODING, PREVIEW_ENCODING_CHOICES, is_browser_playable,is_user_playable_request, make_browser_preview, normalize_preview_encoding,@@ -16,10 +17,10 @@ from .probe import preview_frame_count, probe_video__all__ = ["AUTO_BITRATE_DIVISORS", "CODEC_CHOICES", "DEFAULT_PREVIEW_ENCODING","ENCODING_QUALITIES", "HDR_ALLOWED_CODECS", "PREVIEW_ENCODING_CHOICES",- "calculate_auto_bitrate_kbps", "final_mux", "hdr_mode_supported",+ "RawVideoPacketMuxer", "calculate_auto_bitrate_kbps", "final_mux", "hdr_mode_supported","is_browser_playable", "is_user_playable_request", "make_browser_preview",- "normalize_preview_encoding", "preview_frame_count", "probe_nvenc_codecs",+ "normalize_preview_encoding", "prepare_mux_comment", "preview_frame_count", "probe_nvenc_codecs","probe_video", "resolve_encoding_quality", "resolve_final_preview","resolve_preview_codec", "resolve_video_gpu", "start_encoder",- "validate_codec_container", "wants_compat_preview",+ "validate_codec_container", "verify_mux_comment", "wants_compat_preview",]diff --git a/src/core/ffmpeg/codecs.py b/src/core/ffmpeg/codecs.pyindex f0ecced..e15648f 100644--- a/src/core/ffmpeg/codecs.py+++ b/src/core/ffmpeg/codecs.py@@ -193,11 +193,21 @@ def resolve_encoding_quality(if norm not in _BASE_CODEC_MAP and norm != "HEVC":raise ValueError(f"Unknown video codec: {codec!r}.")if _normalize_codec(norm) == "ProRes Proxy" or _base_codec(norm) == "ProRes Proxy":+ # prores_ks otherwise ignores the application's quality selection.+ # Keep profile 0 while allowing higher selections to preserve fine+ # temporally stabilized detail instead of reintroducing quantizer crawl.+ bits_per_mb = {+ "Auto (Default)": None,+ "Good": 300,+ "Best": 500,+ "Max": 800,+ }[quality_name]return {"selection": quality_name,"mode": "fixed-prores-proxy-profile","target_bitrate_kbps": None,"cq": None,+ "bits_per_mb": bits_per_mb,}if quality_name == "Max":return {diff --git a/src/core/ffmpeg/encoder.py b/src/core/ffmpeg/encoder.pyindex 12f5d66..212504d 100644--- a/src/core/ffmpeg/encoder.py+++ b/src/core/ffmpeg/encoder.py@@ -2,6 +2,7 @@ from __future__ import annotationsimport subprocessimport threading+from functools import lru_cachefrom pathlib import Pathfrom ..jobs import BoundedLogBuffer, JobController, drain_bounded_text, drain_text@@ -12,6 +13,7 @@ from .codecs import (resolve_encoding_quality,)+@lru_cache(maxsize=128)def _encoder_probe(codec: str, width: int, height: int, gpu_ordinal: int | None = None) -> bool:@@ -43,6 +45,12 @@ def _encoder_probe()+@lru_cache(maxsize=16)+def _cpu_encoder_available(codec: str) -> bool:+ """Probe CPU encoder initialization once at a tiny resolution."""+ return _encoder_probe(codec, 64, 64, None)++# Legacy mapping kept for external callers; new display names use _NVENC_ENCODERS._NVENC_CODECS = {"H.264": "h264_nvenc",@@ -132,6 +140,8 @@ def _codec_command(require_nvenc: bool = False,hdr_mode: bool = False,hdr_metadata: dict | None = None,+ *,+ speed_profile: str = "default",) -> tuple[list[str], str, dict]:"""Return FFmpeg codec args for an explicit user-facing codec choice.@@ -142,6 +152,10 @@ def _codec_command(When ``hdr_mode`` is True, output is 10-bit (yuv420p10le/p010le) and inputcolorspace is copied via ``hdr_metadata`` for HDR_ALLOWED_CODECS."""+ if speed_profile not in {"default", "neural", "preview"}:+ raise ValueError(f"Unknown encoder speed profile: {speed_profile!r}.")+ software_preset = {"default": "slow", "neural": "medium", "preview": "veryfast"}[speed_profile]+norm = _normalize_codec(codec)if hdr_mode and not _is_hdr_allowed_codec(norm):raise ValueError(@@ -153,8 +167,16 @@ def _codec_command(# ProRes Proxy is always 10-bit; HDR just copies colorspaceif norm == "ProRes Proxy":hdr_extra = _hdr_color_args(hdr_metadata) if hdr_mode and hdr_metadata else []+ prores_quality = (+ ["-bits_per_mb", str(int(quality["bits_per_mb"]))]+ if quality.get("bits_per_mb") is not None+ else []+ )return (- ["-c:v", "prores_ks", "-profile:v", "0", "-pix_fmt", "yuv422p10le", *hdr_extra],+ [+ "-c:v", "prores_ks", "-profile:v", "0",+ *prores_quality, "-pix_fmt", "yuv422p10le", *hdr_extra,+ ],"prores_ks (Proxy)",quality,)@@ -179,7 +201,7 @@ def _codec_command(if hdr_mode:raise ValueError("HDR Mode is not available for H.264; choose H.265/AV1/ProRes.")return (- ["-c:v", "libx264", "-preset", "slow", *software_quality, "-pix_fmt", "yuv420p"],+ ["-c:v", "libx264", "-preset", software_preset, *software_quality, "-pix_fmt", "yuv420p"],"libx264",quality,)@@ -195,12 +217,12 @@ def _codec_command(x265_extra = ["-x265-params", x265_params] if x265_params else []# For x265, use x265-params exclusively for HDR (generic breaks VUI)return (- ["-c:v", "libx265", "-preset", "slow", *software_quality, "-pix_fmt", pix_fmt, *x265_extra],+ ["-c:v", "libx265", "-preset", software_preset, *software_quality, "-pix_fmt", pix_fmt, *x265_extra],"libx265",quality,)return (- ["-c:v", "libx265", "-preset", "slow", *software_quality, "-pix_fmt", pix_fmt, *hdr_color],+ ["-c:v", "libx265", "-preset", software_preset, *software_quality, "-pix_fmt", pix_fmt, *hdr_color],"libx265",quality,)@@ -211,7 +233,7 @@ def _codec_command()pix_fmt = "yuv420p10le" if hdr_mode else "yuv420p"# Prefer libsvtav1 (fastest CPU AV1), fallback to libaom-av1- if _encoder_probe("libsvtav1", width, height):+ if _cpu_encoder_available("libsvtav1"):if quality["mode"] == "constant-quality":return (["-c:v", "libsvtav1", "-preset", "6", "-crf", "0", "-pix_fmt", pix_fmt, *hdr_color],@@ -223,7 +245,7 @@ def _codec_command("libsvtav1",quality,)- if _encoder_probe("libaom-av1", width, height):+ if _cpu_encoder_available("libaom-av1"):if quality["mode"] == "constant-quality":return (["-c:v", "libaom-av1", "-cpu-used", "4", "-crf", "0", "-b:v", "0", "-pix_fmt", pix_fmt, *hdr_color],@@ -303,32 +325,75 @@ def start_encoder(hdr_metadata: dict | None = None,preserve_timestamps: bool = False,*, video_filter: str | None = None, keep_start_time: bool = False, bounded_logs: bool = False,+ speed_profile: str = "default", source_audio: Path | None = None,+ direct_container: str | None = None, comment: str | None = None,+ audio_duration: float | None = None, include_source_metadata: bool = True,):codec_args, selected, quality = _codec_command(- codec, quality_name, width, height, fps, gpu_ordinal, require_nvenc, hdr_mode, hdr_metadata+ codec, quality_name, width, height, fps, gpu_ordinal, require_nvenc, hdr_mode, hdr_metadata,+ speed_profile=speed_profile,)- command = [- str(FFMPEG),- "-hide_banner",- "-loglevel",- "warning",- "-y",- *(["-copyts"] if keep_start_time else []),- "-f",- "nut",- "-i",- "pipe:0",- "-map",- "0:v:0",- "-an",- *(["-vf", video_filter] if video_filter else []),- *codec_args,- "-fps_mode",- "passthrough",- *(["-enc_time_base:v", "demux"] if preserve_timestamps else []),- *(["-avoid_negative_ts", "disabled"] if keep_start_time else []),- str(temp_video),- ]+ direct_mux = source_audio is not None and direct_container in {"MP4", "MOV"}+ if direct_mux:+ command = [+ str(FFMPEG),+ "-hide_banner",+ "-loglevel",+ "warning",+ "-y",+ *( ["-copyts"] if keep_start_time else [] ),+ "-f",+ "nut",+ "-i",+ "pipe:0",+ *( ["-t", f"{float(audio_duration):.9f}"] if audio_duration is not None and audio_duration > 0 else [] ),+ "-i",+ str(source_audio),+ "-map",+ "0:v:0",+ "-map",+ "1:a?",+ *( ["-map_metadata", "1", "-map_chapters", "1"] if include_source_metadata else ["-map_metadata", "-1", "-map_chapters", "-1"] ),+ *( ["-vf", video_filter] if video_filter else [] ),+ *codec_args,+ "-c:a",+ "aac",+ "-b:a",+ "192k",+ "-fps_mode",+ "passthrough",+ *( ["-enc_time_base:v", "demux"] if preserve_timestamps else [] ),+ "-movflags",+ "+faststart",+ "-metadata:s:v:0",+ "rotate=0",+ *( ["-metadata", "comment=" + comment] if comment is not None else [] ),+ *( ["-avoid_negative_ts", "disabled"] if keep_start_time else [] ),+ str(temp_video),+ ]+ else:+ command = [+ str(FFMPEG),+ "-hide_banner",+ "-loglevel",+ "warning",+ "-y",+ *( ["-copyts"] if keep_start_time else [] ),+ "-f",+ "nut",+ "-i",+ "pipe:0",+ "-map",+ "0:v:0",+ "-an",+ *( ["-vf", video_filter] if video_filter else [] ),+ *codec_args,+ "-fps_mode",+ "passthrough",+ *( ["-enc_time_base:v", "demux"] if preserve_timestamps else [] ),+ *( ["-avoid_negative_ts", "disabled"] if keep_start_time else [] ),+ str(temp_video),+ ]process = subprocess.Popen(command,stdin=subprocess.PIPE,diff --git a/src/core/ffmpeg/mux.py b/src/core/ffmpeg/mux.pyindex 2964106..138ecb2 100644--- a/src/core/ffmpeg/mux.py+++ b/src/core/ffmpeg/mux.py@@ -4,6 +4,7 @@ import mathimport subprocessfrom pathlib import Path+from .. import app_logfrom ..jobs import Cancelled, JobControllerfrom ..render_metadata import (VIDEO_NOTE_FORMATS, MetadataNoteError, check_cancelled, embedding_warning,@@ -45,6 +46,50 @@ def _probe_rendered_duration(path: Path, controller: JobController | None = None)++def prepare_mux_comment(+ source: Path, container: str, render_note: str | None,+ metadata_diagnostics: dict | None = None,+ controller: JobController | None = None,+) -> str | None:+ """Prepare the optional merged comment before a direct encode/mux pass."""+ 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")+ return None+ try:+ comment = merge_render_note(_read_comment(source, controller), render_note)+ if len(comment.encode("utf-16-le")) > 16000:+ raise MetadataNoteError("Existing comment is too large to safely extend")+ return comment+ except Cancelled:+ raise+ except (ValueError, TypeError, RuntimeError) as exc:+ embedding_warning(metadata_diagnostics, exc)+ return None+++def verify_mux_comment(+ output: Path, comment: str | None, metadata_diagnostics: dict | None = None,+ controller: JobController | None = None,+) -> None:+ """Verify optional direct-mux metadata without ever remuxing the video."""+ if comment is None:+ return+ try:+ if _read_comment(output, controller) != comment:+ raise MetadataNoteError("Saved video did not retain the settings note")+ except Cancelled:+ raise+ except (ValueError, RuntimeError) as exc:+ embedding_warning(metadata_diagnostics, exc)+ return+ record_embedding(metadata_diagnostics, "embedded", field="format.comment")++def final_mux(temp_video: Path,source: Path,@@ -80,17 +125,29 @@ def final_mux(try:_final_mux_once(temp_video, source, output, container, controller,preserve_supported_subtitles, comment=comment, source_time_origin=source_time_origin)- if _read_comment(output, controller) != comment:- raise MetadataNoteError("Saved video did not retain the settings note")except Cancelled:raiseexcept (ValueError, RuntimeError) as exc:check_cancelled(controller)if isinstance(exc, _MuxFailure) and exc.filesystem_error:raise+ # A mux failure caused by optional metadata should not destroy the render.+ # Retry once without the note because there is not yet a valid output.embedding_warning(metadata_diagnostics, exc)_final_mux_once(temp_video, source, output, container, controller, preserve_supported_subtitles, source_time_origin=source_time_origin)return++ # Verification is deliberately read-only. If the optional settings note did+ # not survive, keep the already-valid mux instead of copying the complete+ # video through a second remux solely to remove metadata.+ try:+ if _read_comment(output, controller) != comment:+ raise MetadataNoteError("Saved video did not retain the settings note")+ except Cancelled:+ raise+ except (ValueError, RuntimeError) as exc:+ embedding_warning(metadata_diagnostics, exc)+ returnrecord_embedding(metadata_diagnostics, "embedded", field="format.comment")@@ -218,4 +275,5 @@ def _final_mux_once(if controller is not None:controller.unregister(process)if process.returncode:+ app_log.error("ffmpeg-mux", "final audio/metadata mux failed", stderr[-500:])raise _MuxFailure(stderr)diff --git a/src/core/ffmpeg/nut.py b/src/core/ffmpeg/nut.pynew file mode 100644index 0000000..e543592--- /dev/null+++ b/src/core/ffmpeg/nut.py@@ -0,0 +1,65 @@+from __future__ import annotations++from fractions import Fraction++import av+import numpy as np+++class PipeWriter:+ """Expose only the write operations libavformat needs for a pipe."""++ 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()+++class RawVideoPacketMuxer:+ """Write raw video frames into NUT without constructing/encoding VideoFrame objects.++ The packet payload references the caller-provided ndarray only for the duration of+ ``write``. Once ``container.mux`` returns the caller may safely reuse the buffer.+ """++ def __init__(+ self,+ pipe,+ *,+ width: int,+ height: int,+ rate: Fraction,+ time_base: Fraction,+ pix_fmt: str = "rgba",+ ) -> None:+ self.time_base = time_base+ self.container = av.open(+ PipeWriter(pipe), mode="w", format="nut", options={"write_index": "0"}+ )+ self.stream = self.container.add_stream("rawvideo", rate=rate)+ self.stream.width = width+ self.stream.height = height+ self.stream.pix_fmt = pix_fmt+ if pix_fmt == "rgba":+ self.stream.codec_context.codec_tag = "RGBA"+ self.stream.time_base = time_base+ self.stream.codec_context.time_base = time_base+ self.container.start_encoding()++ def write(self, frame: np.ndarray, pts: int, duration: int = 0) -> None:+ if not frame.flags.c_contiguous:+ raise ValueError("Raw NUT frame must be C-contiguous.")+ packet = av.Packet(memoryview(frame).cast("B"))+ packet.stream = self.stream+ packet.pts = packet.dts = int(pts)+ packet.duration = int(duration)+ packet.time_base = self.time_base+ packet.is_keyframe = True+ self.container.mux(packet)++ def close(self) -> None:+ self.container.close()diff --git a/src/core/ffmpeg/preview.py b/src/core/ffmpeg/preview.pyindex d9e1f99..c5032b1 100644--- a/src/core/ffmpeg/preview.py+++ b/src/core/ffmpeg/preview.py@@ -3,6 +3,7 @@ from __future__ import annotationsimport subprocessfrom pathlib import Path+from .. import app_logfrom ..paths import FFMPEGfrom .codecs import _base_codec@@ -85,6 +86,8 @@ def make_browser_preview(dest_dir: str | Path | None = None,controller=None,*, sdr_filter: str | None = None,+ max_seconds: float | None = None,+ max_width: int | None = None,) -> str:"""Transcode an existing result file to a browser-playable H.264 MP4.@@ -107,6 +110,11 @@ def make_browser_preview(counter += 1dest = out_dir / f"{src.stem}_BROWSERPREVIEW_{counter}.mp4"output_file = OutputFile(dest)+ filters: list[str] = []+ if sdr_filter:+ filters.append(sdr_filter)+ if max_width is not None and max_width > 0:+ filters.append(f"scale=if(gt(iw\\,{int(max_width)})\\,{int(max_width)}\\,iw):-2")command = [str(FFMPEG),"-hide_banner",@@ -115,11 +123,13 @@ def make_browser_preview("-y","-i",str(src),+ *(["-t", f"{float(max_seconds):.6f}"] if max_seconds is not None and max_seconds > 0 else []),"-map","0:v:0","-map","0:a?",- *(["-vf", sdr_filter, "-color_primaries", "bt709", "-color_trc", "bt709", "-colorspace", "bt709", "-color_range", "tv"] if sdr_filter else []),+ *(["-vf", ",".join(filters)] if filters else []),+ *(["-color_primaries", "bt709", "-color_trc", "bt709", "-colorspace", "bt709", "-color_range", "tv"] if sdr_filter else []),"-c:v","libx264","-preset",@@ -146,6 +156,7 @@ def make_browser_preview(controller.register(process)_stdout, stderr = process.communicate()if process.returncode:+ app_log.error("ffmpeg-preview", "browser preview transcode failed", (stderr or "")[-500:])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.")@@ -168,6 +179,7 @@ def resolve_final_preview(result_path: str | Path | None,mode: object,controller=None,+ *, bounded_proxy: bool = False,) -> tuple[str | None, bool]:"""Decide which file the final-render in-app player should show.@@ -194,7 +206,11 @@ def resolve_final_preview(except Exception:passtry:- derived = make_browser_preview(candidate, controller=controller)+ derived = make_browser_preview(+ candidate, controller=controller,+ max_seconds=12.0 if bounded_proxy else None,+ max_width=1280 if bounded_proxy else None,+ )except Exception:# Never break the render status path: fall back to no in-app preview# (the real file is still in the download list).diff --git a/src/core/ffmpeg/probe.py b/src/core/ffmpeg/probe.pyindex 6c36f52..6b33c63 100644--- a/src/core/ffmpeg/probe.py+++ b/src/core/ffmpeg/probe.py@@ -1,6 +1,7 @@from __future__ import annotationsimport json+import mathimport osimport subprocessimport tempfile@@ -11,6 +12,7 @@ import avfrom ..paths import FFPROBEfrom ..jobs import Cancelled, JobController+from .. import app_logdef _run_json(command: list[str], *, strict_decode: bool = False,@@ -42,8 +44,10 @@ def _run_json(errors.seek(max(0, size - 8000))details = errors.read().decode("utf-8", "replace").strip()if strict_decode and size:+ app_log.error("ffprobe", "source decoding failed", details[-500:])raise RuntimeError("Source decoding failed during frame-count verification:\n" + details)if process.returncode:+ app_log.error("ffprobe", "media probe failed", details[-500:])raise RuntimeError(details or "Media probe failed")return json.loads(stdout)finally:@@ -60,6 +64,14 @@ def _run_json(controller.unregister(process)++def _positive_float(value: object) -> float:+ try:+ number = float(value)+ except (ValueError, TypeError, OverflowError):+ return 0.0+ return number if math.isfinite(number) and number > 0 else 0.0+def _positive_count(value: object) -> int:try:return max(0, int(value))@@ -96,7 +108,7 @@ def probe_video("-select_streams","v:0","-show_entries",- "stream=index,codec_name,width,height,avg_frame_rate,r_frame_rate,time_base,duration,nb_frames,nb_read_frames,nb_read_packets,color_primaries,color_transfer,color_space:stream_tags=rotate:stream_side_data=rotation",+ "stream=index,codec_name,width,height,avg_frame_rate,r_frame_rate,time_base,duration,nb_frames,nb_read_frames,nb_read_packets,color_range,color_primaries,color_transfer,color_space:stream_tags=rotate:stream_side_data=rotation","-show_entries","format=duration,format_name","-of",@@ -144,6 +156,9 @@ def probe_video(transfer = stream.get("color_transfer") or "unknown"primaries = stream.get("color_primaries") or "unknown"color_space = stream.get("color_space") or "unknown"+ color_range = stream.get("color_range") or "unknown"+ format_duration = _positive_float((data.get("format") or {}).get("duration"))+ stream_duration = _positive_float(stream.get("duration"))return {"width": width,"height": height,@@ -157,14 +172,15 @@ def probe_video("nominal_rate": nominal_rate,"cfr": rate == nominal_rate,"time_base": Fraction(stream.get("time_base") or "1/1000"),- "duration": float(- (data.get("format") or {}).get("duration") or stream.get("duration") or 0- ),+ "duration": format_duration or stream_duration,+ "video_duration": stream_duration or format_duration,+ "video_stream_duration": stream_duration,"codec": stream.get("codec_name") or "unknown","format": (data.get("format") or {}).get("format_name") or "unknown","color_transfer": transfer,"color_primaries": primaries,"color_space": color_space,+ "color_range": color_range,"hdr": transfer in {"smpte2084", "arib-std-b67"},}diff --git a/src/core/jobs.py b/src/core/jobs.pyindex b84b073..2f95c7c 100644--- a/src/core/jobs.py+++ b/src/core/jobs.py@@ -107,9 +107,8 @@ class BoundedLogBuffer:_IMPORTANT = ("profile applied",- "model preset",- "DLSS 5 add-on",- "carrier ready",+ "bridge",+ "ngx","stream source","optimal settings","complete:",diff --git a/src/core/neural_bridge.py b/src/core/neural_bridge.pynew file mode 100644index 0000000..87fc5bf--- /dev/null+++ b/src/core/neural_bridge.py@@ -0,0 +1,1599 @@+from __future__ import annotations++"""Process-wide D3D12/NGX feature-18 bridge management.++The NVIDIA NGX runtime is intentionally process-lifetime state. In particular,+normal session close never calls ``NVSDK_NGX_D3D12_Shutdown`` and never unloads+driver modules: both operations have been observed to wedge after a successful+feature-18 evaluation. Logical sessions own only their CUDA frame buffers and+their diagnostics.+"""++import ctypes+import contextlib+import json+import threading+import time+from dataclasses import dataclass, field+from pathlib import Path+from typing import Any, Callable++import numpy as np++from .paths import DLSSNR_BRIDGE, DLSSNR_DIR+++BRIDGE_ABI_VERSION = 6+BRIDGE_WATCHDOG_SECONDS = 45.0+MEMORY_HOST = 0+MEMORY_CUDA = 1+MEMORY_NONE = 2+FORMAT_RGBA8 = 1+FORMAT_NV12 = 2+FORMAT_P010 = 3+++class FrameDescriptorV1(ctypes.Structure):+ _fields_ = [+ ("struct_size", ctypes.c_uint32),+ ("abi_version", ctypes.c_uint32),+ ("memory_type", ctypes.c_uint32),+ ("pixel_format", ctypes.c_uint32),+ ("width", ctypes.c_uint32),+ ("height", ctypes.c_uint32),+ ("planes", ctypes.c_uint64 * 3),+ ("strides", ctypes.c_uint32 * 3),+ ("color_matrix", ctypes.c_uint32),+ ("color_range", ctypes.c_uint32),+ ("rotation", ctypes.c_uint32),+ ("reserved", ctypes.c_uint32),+ ("timestamp", ctypes.c_int64),+ ]++ @classmethod+ def empty(cls) -> "FrameDescriptorV1":+ value = cls()+ value.struct_size = ctypes.sizeof(cls)+ value.abi_version = BRIDGE_ABI_VERSION+ return value+++class RenderParametersV1(ctypes.Structure):+ """Legacy ABI-v2 render controls retained for old frame entry points."""++ _fields_ = [+ ("struct_size", ctypes.c_uint32),+ ("abi_version", ctypes.c_uint32),+ ("style", ctypes.c_int32),+ ("intensity", ctypes.c_float),+ ("tone", ctypes.c_float),+ ("structure", ctypes.c_float),+ ("skin", ctypes.c_float),+ ("automask", ctypes.c_int32),+ ("reset", ctypes.c_int32),+ ]+++class RenderParametersV3(ctypes.Structure):+ _fields_ = [+ ("struct_size", ctypes.c_uint32),+ ("abi_version", ctypes.c_uint32),+ ("style", ctypes.c_int32),+ ("intensity", ctypes.c_float),+ ("tone", ctypes.c_float),+ ("structure", ctypes.c_float),+ ("skin", ctypes.c_float),+ ("automask", ctypes.c_int32),+ ("reset", ctypes.c_int32),+ ("color_strength", ctypes.c_float),+ ("tone_preservation", ctypes.c_float),+ ("mask_memory_type", ctypes.c_uint32),+ ("mask_width", ctypes.c_uint32),+ ("mask_height", ctypes.c_uint32),+ ("mask_stride", ctypes.c_uint32),+ ("mask_plane", ctypes.c_uint64),+ ]+++class RenderParametersV4(ctypes.Structure):+ """ABI-v4 controls; the ABI-v3 prefix is deliberately unchanged."""++ _fields_ = [+ *RenderParametersV3._fields_,+ ("face_skin_protection", ctypes.c_float),+ ("grain_preservation", ctypes.c_float),+ ]+++class RenderParametersV5(ctypes.Structure):+ """ABI-v5 controls; the ABI-v4 prefix is deliberately unchanged."""++ _fields_ = [+ *RenderParametersV4._fields_,+ ("nr_passes", ctypes.c_int32),+ ]+++class RenderParametersV6(ctypes.Structure):+ """ABI-v6 controls; v5 callers retain suppression-disabled behavior."""++ _fields_ = [+ *RenderParametersV5._fields_,+ ("shimmer_suppression", ctypes.c_float),+ ("prefer_nvof", ctypes.c_int32),+ ]+++class FrameResultV1(ctypes.Structure):+ _fields_ = [+ ("struct_size", ctypes.c_uint32),+ ("abi_version", ctypes.c_uint32),+ ("ngx_create_result", ctypes.c_int32),+ ("ngx_evaluate_result", ctypes.c_int32),+ ("cuda_result", ctypes.c_int32),+ ("scene_reset", ctypes.c_int32),+ ("scene_score", ctypes.c_float),+ ("reserved", ctypes.c_uint32),+ ("upload_bytes", ctypes.c_uint64),+ ("download_bytes", ctypes.c_uint64),+ ("timestamp", ctypes.c_int64),+ ]++ @classmethod+ def empty(cls) -> "FrameResultV1":+ value = cls()+ value.struct_size = ctypes.sizeof(cls)+ value.abi_version = BRIDGE_ABI_VERSION+ return value+++class _DLDevice(ctypes.Structure):+ _fields_ = [("device_type", ctypes.c_int), ("device_id", ctypes.c_int)]+++class _DLDataType(ctypes.Structure):+ _fields_ = [("code", ctypes.c_uint8), ("bits", ctypes.c_uint8), ("lanes", ctypes.c_uint16)]+++class _DLTensor(ctypes.Structure):+ _fields_ = [+ ("data", ctypes.c_void_p),+ ("device", _DLDevice),+ ("ndim", ctypes.c_int),+ ("dtype", _DLDataType),+ ("shape", ctypes.POINTER(ctypes.c_int64)),+ ("strides", ctypes.POINTER(ctypes.c_int64)),+ ("byte_offset", ctypes.c_uint64),+ ]+++class _DLManagedTensor(ctypes.Structure):+ pass+++_DLPACK_RECORDS: dict[int, tuple[Any, ...]] = {}+_DLPACK_LOCK = threading.Lock()+_DL_DELETER = ctypes.CFUNCTYPE(None, ctypes.POINTER(_DLManagedTensor))+++@_DL_DELETER+def _dlpack_deleter(pointer: ctypes.POINTER(_DLManagedTensor)) -> None:+ address = ctypes.addressof(pointer.contents)+ with _DLPACK_LOCK:+ record = _DLPACK_RECORDS.pop(address, None)+ if record is not None:+ release = record[-1]+ try:+ release()+ except Exception:+ pass+++_DLManagedTensor._fields_ = [+ ("dl_tensor", _DLTensor),+ ("manager_ctx", ctypes.c_void_p),+ ("deleter", _DL_DELETER),+]+++_PYCAPSULE_DESTRUCTOR = ctypes.CFUNCTYPE(None, ctypes.c_void_p)+ctypes.pythonapi.PyCapsule_New.argtypes = [+ ctypes.c_void_p,+ ctypes.c_char_p,+ _PYCAPSULE_DESTRUCTOR,+]+ctypes.pythonapi.PyCapsule_New.restype = ctypes.py_object+ctypes.pythonapi.PyCapsule_IsValid.argtypes = [ctypes.c_void_p, ctypes.c_char_p]+ctypes.pythonapi.PyCapsule_IsValid.restype = ctypes.c_int+ctypes.pythonapi.PyCapsule_GetPointer.argtypes = [ctypes.c_void_p, ctypes.c_char_p]+ctypes.pythonapi.PyCapsule_GetPointer.restype = ctypes.c_void_p+++@_PYCAPSULE_DESTRUCTOR+def _capsule_destructor(capsule: int) -> None:+ try:+ if ctypes.pythonapi.PyCapsule_IsValid(capsule, b"dltensor"):+ address = ctypes.pythonapi.PyCapsule_GetPointer(capsule, b"dltensor")+ if address:+ _dlpack_deleter(ctypes.cast(address, ctypes.POINTER(_DLManagedTensor)))+ except Exception:+ pass+++class _DLPackPlane:+ def __init__(+ self,+ *,+ pointer: int,+ shape: tuple[int, ...],+ strides: tuple[int, ...],+ bits: int,+ device_id: int,+ retain: Callable[[], None],+ release: Callable[[], None],+ ) -> None:+ self.pointer = int(pointer)+ self.shape = shape+ self.strides = strides+ self.bits = bits+ self.device_id = device_id+ self.retain = retain+ self.release = release++ def __dlpack_device__(self) -> tuple[int, int]:+ return (2, self.device_id) # kDLCUDA++ def __dlpack__(self, stream=None, **_kwargs):+ del stream+ shape = (ctypes.c_int64 * len(self.shape))(*self.shape)+ strides = (ctypes.c_int64 * len(self.strides))(*self.strides)+ managed = _DLManagedTensor()+ managed.dl_tensor.data = ctypes.c_void_p(self.pointer)+ managed.dl_tensor.device = _DLDevice(2, self.device_id)+ managed.dl_tensor.ndim = len(self.shape)+ managed.dl_tensor.dtype = _DLDataType(1, self.bits, 1) # kDLUInt+ managed.dl_tensor.shape = ctypes.cast(shape, ctypes.POINTER(ctypes.c_int64))+ managed.dl_tensor.strides = ctypes.cast(strides, ctypes.POINTER(ctypes.c_int64))+ managed.dl_tensor.byte_offset = 0+ managed.manager_ctx = None+ managed.deleter = _dlpack_deleter+ self.retain()+ address = ctypes.addressof(managed)+ with _DLPACK_LOCK:+ _DLPACK_RECORDS[address] = (managed, shape, strides, self, self.release)+ return ctypes.pythonapi.PyCapsule_New(address, b"dltensor", _capsule_destructor)+++class NeuralBridgeError(RuntimeError):+ """An actionable failure returned by the native Neural Rendering bridge."""+++class NeuralBridgePoisonedError(NeuralBridgeError):+ """The in-process native state cannot safely be reused before app restart."""+++def _text(value: bytes | None) -> str:+ return value.decode("utf-8", "replace") if value else ""+++def _json_line(event: str, **values: Any) -> str:+ return json.dumps(+ {"event": event, "monotonic_seconds": time.monotonic(), **values},+ sort_keys=True,+ separators=(",", ":"),+ )+++class _CudaDriver:+ """Small CUDA Driver API wrapper; no CUDA toolkit or Python add-on needed."""++ def __init__(self, ordinal: int) -> None:+ loader = getattr(ctypes, "WinDLL", ctypes.CDLL)+ try:+ self.lib = loader("nvcuda.dll")+ except OSError as exc:+ raise NeuralBridgeError(+ "CUDA interoperability is unavailable because nvcuda.dll could not be "+ "loaded. Switch GPU OFF to use the bridge host-staging path."+ ) from exc++ self._bind("cuInit", [ctypes.c_uint])+ self._bind("cuDeviceGet", [ctypes.POINTER(ctypes.c_int), ctypes.c_int])+ self._bind(+ "cuDevicePrimaryCtxRetain",+ [ctypes.POINTER(ctypes.c_void_p), ctypes.c_int],+ )+ self._bind_any(+ "primary_ctx_release",+ ("cuDevicePrimaryCtxRelease_v2", "cuDevicePrimaryCtxRelease"),+ [ctypes.c_int],+ )+ self._bind("cuCtxSetCurrent", [ctypes.c_void_p])+ self._bind("cuCtxSynchronize", [])+ self._bind_any(+ "mem_alloc", ("cuMemAlloc_v2", "cuMemAlloc"),+ [ctypes.POINTER(ctypes.c_uint64), ctypes.c_size_t],+ )+ self._bind_any(+ "mem_free", ("cuMemFree_v2", "cuMemFree"), [ctypes.c_uint64]+ )+ self._bind_any(+ "copy_htod", ("cuMemcpyHtoD_v2", "cuMemcpyHtoD"),+ [ctypes.c_uint64, ctypes.c_void_p, ctypes.c_size_t],+ )+ self._bind_any(+ "copy_dtoh", ("cuMemcpyDtoH_v2", "cuMemcpyDtoH"),+ [ctypes.c_void_p, ctypes.c_uint64, ctypes.c_size_t],+ )+ self._bind(+ "cuGetErrorString",+ [ctypes.c_int, ctypes.POINTER(ctypes.c_char_p)],+ )++ self._check(self.cuInit(0), "cuInit")+ device = ctypes.c_int()+ self._check(self.cuDeviceGet(ctypes.byref(device), int(ordinal)), "cuDeviceGet")+ context = ctypes.c_void_p()+ self._check(+ self.cuDevicePrimaryCtxRetain(ctypes.byref(context), device.value),+ "cuDevicePrimaryCtxRetain",+ )+ self._check(self.cuCtxSetCurrent(context), "cuCtxSetCurrent")+ self.ordinal = int(ordinal)+ self.device = int(device.value)+ self.context = context+ self.closed = False++ def _bind(self, name: str, argtypes: list[Any]) -> Any:+ function = getattr(self.lib, name)+ function.argtypes = argtypes+ function.restype = ctypes.c_int+ setattr(self, name, function)+ return function++ def _bind_any(+ self, attribute: str, names: tuple[str, ...], argtypes: list[Any]+ ) -> Any:+ for name in names:+ function = getattr(self.lib, name, None)+ if function is not None:+ function.argtypes = argtypes+ function.restype = ctypes.c_int+ setattr(self, attribute, function)+ return function+ raise NeuralBridgeError(f"The NVIDIA driver does not export {names[0]}.")++ def _check(self, result: int, operation: str) -> None:+ if result == 0:+ return+ message = ctypes.c_char_p()+ detail = ""+ try:+ if self.cuGetErrorString(result, ctypes.byref(message)) == 0:+ detail = _text(message.value)+ except Exception:+ pass+ suffix = f": {detail}" if detail else ""+ raise NeuralBridgeError(f"{operation} failed with CUDA result {result}{suffix}.")++ def activate(self) -> None:+ self._check(self.cuCtxSetCurrent(self.context), "cuCtxSetCurrent")++ def deactivate(self) -> None:+ self._check(self.cuCtxSetCurrent(None), "cuCtxSetCurrent(NULL)")++ def alloc(self, byte_count: int) -> int:+ self.activate()+ pointer = ctypes.c_uint64()+ self._check(self.mem_alloc(ctypes.byref(pointer), byte_count), "cuMemAlloc")+ return int(pointer.value)++ def free(self, pointer: int) -> None:+ if not pointer:+ return+ self.activate()+ self._check(self.mem_free(ctypes.c_uint64(pointer)), "cuMemFree")++ def upload(self, pointer: int, array: np.ndarray) -> None:+ self.activate()+ self._check(+ self.copy_htod(+ ctypes.c_uint64(pointer), ctypes.c_void_p(array.ctypes.data), array.nbytes+ ),+ "cuMemcpyHtoD",+ )++ def download(self, array: np.ndarray, pointer: int) -> None:+ self.activate()+ self._check(+ self.copy_dtoh(+ ctypes.c_void_p(array.ctypes.data), ctypes.c_uint64(pointer), array.nbytes+ ),+ "cuMemcpyDtoH",+ )++ def synchronize(self) -> None:+ self.activate()+ self._check(self.cuCtxSynchronize(), "cuCtxSynchronize")++ def close(self) -> None:+ if self.closed:+ return+ try:+ self.deactivate()+ finally:+ self._check(+ self.primary_ctx_release(self.device),+ "cuDevicePrimaryCtxRelease",+ )+ self.context = ctypes.c_void_p()+ self.closed = True+++@dataclass(slots=True)+class CudaFrameBuffers:+ driver: _CudaDriver+ elements: int+ input_pointer: int+ output_pointer: int+ closed: bool = False++ @classmethod+ def create(cls, driver: _CudaDriver, width: int, height: int) -> "CudaFrameBuffers":+ elements = int(width) * int(height) * 3+ byte_count = elements * np.dtype(np.float32).itemsize+ input_pointer = driver.alloc(byte_count)+ try:+ output_pointer = driver.alloc(byte_count)+ except Exception:+ driver.free(input_pointer)+ raise+ return cls(driver, elements, input_pointer, output_pointer)++ def close(self) -> None:+ if self.closed:+ return+ try:+ self.driver.synchronize()+ self.driver.free(self.output_pointer)+ self.driver.free(self.input_pointer)+ self.input_pointer = 0+ self.output_pointer = 0+ self.closed = True+ finally:+ self.driver.deactivate()+++@dataclass(slots=True)+class CudaMaskBuffer:+ driver: _CudaDriver+ pointer: int+ byte_count: int+ closed: bool = False++ @classmethod+ def create(cls, driver: _CudaDriver, mask: np.ndarray) -> "CudaMaskBuffer":+ if mask.dtype != np.float32 or mask.ndim != 2 or not mask.flags.c_contiguous:+ raise NeuralBridgeError("Custom NR Mask must be contiguous float32 data.")+ pointer = driver.alloc(mask.nbytes)+ try:+ driver.upload(pointer, mask)+ driver.synchronize()+ except Exception:+ driver.free(pointer)+ raise+ return cls(driver, pointer, int(mask.nbytes))++ def close(self) -> None:+ if self.closed:+ return+ try:+ self.driver.synchronize()+ self.driver.free(self.pointer)+ self.pointer = 0+ self.closed = True+ finally:+ self.driver.deactivate()+++class BridgeCudaSurface:+ """Bridge-owned NV12/P010 allocation with DLPack-managed plane lifetime."""++ def __init__(+ self,+ library: Any,+ handle: int,+ descriptor: FrameDescriptorV1,+ ordinal: int,+ ) -> None:+ self.library = library+ self.handle = int(handle)+ self.descriptor = descriptor+ self.ordinal = int(ordinal)+ self.closed = False++ def retain(self) -> None:+ self.library.dlss5nr_surface_retain(ctypes.c_void_p(self.handle))++ def release(self) -> None:+ self.library.dlss5nr_surface_release(ctypes.c_void_p(self.handle))++ def close(self) -> None:+ if not self.closed:+ self.release()+ self.closed = True++ def to_av_frame(self):+ import av++ desc = self.descriptor+ bits = 16 if desc.pixel_format == FORMAT_P010 else 8+ item_size = bits // 8+ y_plane = _DLPackPlane(+ pointer=int(desc.planes[0]),+ shape=(int(desc.height), int(desc.width)),+ strides=(int(desc.strides[0]) // item_size, 1),+ bits=bits,+ device_id=self.ordinal,+ retain=self.retain,+ release=self.release,+ )+ uv_plane = _DLPackPlane(+ pointer=int(desc.planes[1]),+ shape=((int(desc.height) + 1) // 2, (int(desc.width) + 1) // 2, 2),+ strides=(int(desc.strides[1]) // item_size, 2, 1),+ bits=bits,+ device_id=self.ordinal,+ retain=self.retain,+ release=self.release,+ )+ frame = av.VideoFrame.from_dlpack(+ [y_plane, uv_plane],+ format="p010le" if desc.pixel_format == FORMAT_P010 else "nv12",+ width=int(desc.width),+ height=int(desc.height),+ device_id=self.ordinal,+ primary_ctx=True,+ )+ # Drop the creator's reference. The two DLPack tensors now own the+ # allocation until the AVFrame and its encoder references are released.+ self.close()+ return frame+++@dataclass(slots=True)+class BridgeSessionDiagnostics:+ gpu_mode: bool+ memory_path: str+ decode_backend: str = "software"+ encode_backend: str = "cpu"+ pixel_format: str = "rgba8"+ frames: int = 0+ feature_evaluations: int = 0+ scene_resets: int = 0+ upload_bytes: int = 0+ download_bytes: int = 0+ ngx_create_result: str = "0x00000001"+ ngx_evaluate_result: str = "0x00000001"+ events: list[str] = field(default_factory=list)++ def as_dict(self) -> dict[str, Any]:+ return {+ "bridge_abi_version": BRIDGE_ABI_VERSION,+ "gpu_mode": self.gpu_mode,+ "memory_path": self.memory_path,+ "decode_backend": self.decode_backend,+ "encode_backend": self.encode_backend,+ "pixel_format": self.pixel_format,+ "frames": self.frames,+ "feature_evaluations": self.feature_evaluations,+ "scene_resets": self.scene_resets,+ "transfers": {+ "upload_bytes": self.upload_bytes,+ "download_bytes": self.download_bytes,+ },+ "ngx": {+ "create_result": self.ngx_create_result,+ "evaluate_result": self.ngx_evaluate_result,+ },+ }+++class NeuralBridgeManager:+ """One serialized bridge instance shared by every logical render session."""++ def __init__(self) -> None:+ self._lock = threading.RLock()+ self._library: Any | None = None+ self._initialized_ordinal: int | None = None+ self._cuda_driver: _CudaDriver | None = None+ self._poisoned_reason = ""+ self._timed_out_references: list[Any] = []+ self._active_sessions = 0+ self._version = "unloaded"+ self._gpu_name = "unknown"++ @property+ def version(self) -> str:+ return self._version++ @property+ def gpu_name(self) -> str:+ return self._gpu_name++ def temporal_status(self) -> dict[str, Any]:+ with self._lock:+ if self._library is None:+ return {}+ buffer = ctypes.create_string_buffer(2048)+ try:+ self._library.dlss5nr_temporal_status(buffer, len(buffer))+ value = json.loads(_text(buffer.value) or "{}")+ return value if isinstance(value, dict) else {}+ except (AttributeError, json.JSONDecodeError, OSError):+ return {}++ def _load(self) -> None:+ if self._library is not None:+ return+ if not DLSSNR_BRIDGE.is_file():+ raise NeuralBridgeError(+ f"Neural Rendering bridge is missing: {DLSSNR_BRIDGE}"+ )+ loader = getattr(ctypes, "WinDLL", ctypes.CDLL)+ try:+ library = loader(str(DLSSNR_BRIDGE))+ except OSError as exc:+ raise NeuralBridgeError(+ f"Neural Rendering bridge could not be loaded: {exc}"+ ) from exc++ c_float_p = ctypes.POINTER(ctypes.c_float)+ library.dlss5nr_version.argtypes = []+ library.dlss5nr_version.restype = ctypes.c_char_p+ library.dlss5nr_gpu_name.argtypes = []+ library.dlss5nr_gpu_name.restype = ctypes.c_char_p+ library.dlss5nr_adapter_luid.argtypes = []+ library.dlss5nr_adapter_luid.restype = ctypes.c_char_p+ library.dlss5nr_frame_abi_version.argtypes = []+ library.dlss5nr_frame_abi_version.restype = ctypes.c_uint32+ version = _text(library.dlss5nr_version()) or "unknown"+ frame_abi = int(library.dlss5nr_frame_abi_version())+ if frame_abi != BRIDGE_ABI_VERSION:+ raise NeuralBridgeError(+ f"Neural Rendering bridge {version} uses frame ABI {frame_abi}, but "+ f"this application requires ABI {BRIDGE_ABI_VERSION}. Rebuild the native "+ "bridge or reinstall the matching runtime."+ )+ library.dlss5nr_init.argtypes = [+ ctypes.c_int,+ ctypes.c_wchar_p,+ ctypes.c_char_p,+ ctypes.c_int,+ ]+ library.dlss5nr_init.restype = ctypes.c_int+ library.dlss5nr_rebind.argtypes = [+ ctypes.c_int,+ ctypes.c_char_p,+ ctypes.c_int,+ ]+ library.dlss5nr_rebind.restype = ctypes.c_int+ library.dlss5nr_process.argtypes = [+ c_float_p,+ c_float_p,+ ctypes.c_int,+ ctypes.c_int,+ ctypes.c_int,+ ctypes.c_float,+ ctypes.c_float,+ ctypes.c_float,+ ctypes.c_float,+ ctypes.c_int,+ ctypes.c_int,+ ctypes.c_int,+ ctypes.c_char_p,+ ctypes.c_int,+ ]+ library.dlss5nr_process.restype = ctypes.c_int+ library.dlss5nr_process_cuda.argtypes = [+ ctypes.c_uint64,+ ctypes.c_uint64,+ ctypes.c_int,+ ctypes.c_int,+ ctypes.c_uint64,+ ctypes.c_int,+ ctypes.c_float,+ ctypes.c_float,+ ctypes.c_float,+ ctypes.c_float,+ ctypes.c_int,+ ctypes.c_int,+ ctypes.c_char_p,+ ctypes.c_int,+ ]+ library.dlss5nr_process_cuda.restype = ctypes.c_int+ library.dlss5nr_process_v3.argtypes = [+ c_float_p,+ c_float_p,+ ctypes.c_int,+ ctypes.c_int,+ ctypes.POINTER(RenderParametersV3),+ ctypes.c_char_p,+ ctypes.c_int,+ ]+ library.dlss5nr_process_v3.restype = ctypes.c_int+ library.dlss5nr_process_cuda_v3.argtypes = [+ ctypes.c_uint64,+ ctypes.c_uint64,+ ctypes.c_int,+ ctypes.c_int,+ ctypes.c_uint64,+ ctypes.POINTER(RenderParametersV3),+ ctypes.c_char_p,+ ctypes.c_int,+ ]+ library.dlss5nr_process_cuda_v3.restype = ctypes.c_int+ library.dlss5nr_process_v4.argtypes = [+ c_float_p,+ c_float_p,+ ctypes.c_int,+ ctypes.c_int,+ ctypes.POINTER(RenderParametersV4),+ ctypes.c_char_p,+ ctypes.c_int,+ ]+ library.dlss5nr_process_v4.restype = ctypes.c_int+ library.dlss5nr_process_cuda_v4.argtypes = [+ ctypes.c_uint64,+ ctypes.c_uint64,+ ctypes.c_int,+ ctypes.c_int,+ ctypes.c_uint64,+ ctypes.POINTER(RenderParametersV4),+ ctypes.c_char_p,+ ctypes.c_int,+ ]+ library.dlss5nr_process_cuda_v4.restype = ctypes.c_int+ library.dlss5nr_process_v5.argtypes = [+ c_float_p,+ c_float_p,+ ctypes.c_int,+ ctypes.c_int,+ ctypes.POINTER(RenderParametersV5),+ ctypes.c_char_p,+ ctypes.c_int,+ ]+ library.dlss5nr_process_v5.restype = ctypes.c_int+ library.dlss5nr_process_v6.argtypes = [+ c_float_p,+ c_float_p,+ ctypes.c_int,+ ctypes.c_int,+ ctypes.POINTER(RenderParametersV6),+ ctypes.c_char_p,+ ctypes.c_int,+ ]+ library.dlss5nr_process_v6.restype = ctypes.c_int+ library.dlss5nr_process_cuda_v5.argtypes = [+ ctypes.c_uint64,+ ctypes.c_uint64,+ ctypes.c_int,+ ctypes.c_int,+ ctypes.c_uint64,+ ctypes.POINTER(RenderParametersV5),+ ctypes.c_char_p,+ ctypes.c_int,+ ]+ library.dlss5nr_process_cuda_v5.restype = ctypes.c_int+ library.dlss5nr_process_cuda_v6.argtypes = [+ ctypes.c_uint64,+ ctypes.c_uint64,+ ctypes.c_int,+ ctypes.c_int,+ ctypes.c_uint64,+ ctypes.POINTER(RenderParametersV6),+ ctypes.c_char_p,+ ctypes.c_int,+ ]+ library.dlss5nr_process_cuda_v6.restype = ctypes.c_int+ library.dlss5nr_cuda_supported.argtypes = []+ library.dlss5nr_cuda_supported.restype = ctypes.c_int+ library.dlss5nr_cuda_status.argtypes = [ctypes.c_char_p, ctypes.c_int]+ library.dlss5nr_cuda_status.restype = ctypes.c_int+ library.dlss5nr_process_frame_v1.argtypes = [+ ctypes.POINTER(FrameDescriptorV1),+ ctypes.POINTER(FrameDescriptorV1),+ ctypes.POINTER(RenderParametersV1),+ ctypes.POINTER(FrameResultV1),+ ctypes.c_char_p,+ ctypes.c_int,+ ]+ library.dlss5nr_process_frame_v1.restype = ctypes.c_int+ library.dlss5nr_process_frame_v3.argtypes = [+ ctypes.POINTER(FrameDescriptorV1),+ ctypes.POINTER(FrameDescriptorV1),+ ctypes.POINTER(RenderParametersV3),+ ctypes.POINTER(FrameResultV1),+ ctypes.c_char_p,+ ctypes.c_int,+ ]+ library.dlss5nr_process_frame_v3.restype = ctypes.c_int+ library.dlss5nr_process_frame_v4.argtypes = [+ ctypes.POINTER(FrameDescriptorV1),+ ctypes.POINTER(FrameDescriptorV1),+ ctypes.POINTER(RenderParametersV4),+ ctypes.POINTER(FrameResultV1),+ ctypes.c_char_p,+ ctypes.c_int,+ ]+ library.dlss5nr_process_frame_v4.restype = ctypes.c_int+ library.dlss5nr_process_frame_v5.argtypes = [+ ctypes.POINTER(FrameDescriptorV1),+ ctypes.POINTER(FrameDescriptorV1),+ ctypes.POINTER(RenderParametersV5),+ ctypes.POINTER(FrameResultV1),+ ctypes.c_char_p,+ ctypes.c_int,+ ]+ library.dlss5nr_process_frame_v5.restype = ctypes.c_int+ library.dlss5nr_process_frame_v6.argtypes = [+ ctypes.POINTER(FrameDescriptorV1),+ ctypes.POINTER(FrameDescriptorV1),+ ctypes.POINTER(RenderParametersV6),+ ctypes.POINTER(FrameResultV1),+ ctypes.c_char_p,+ ctypes.c_int,+ ]+ library.dlss5nr_process_frame_v6.restype = ctypes.c_int+ library.dlss5nr_temporal_status.argtypes = [ctypes.c_char_p, ctypes.c_int]+ library.dlss5nr_temporal_status.restype = ctypes.c_int+ library.dlss5nr_scene_score_v1.argtypes = [+ ctypes.POINTER(FrameDescriptorV1),+ ctypes.c_float,+ ctypes.POINTER(ctypes.c_float),+ ctypes.POINTER(ctypes.c_int),+ ctypes.c_char_p,+ ctypes.c_int,+ ]+ library.dlss5nr_scene_score_v1.restype = ctypes.c_int+ library.dlss5nr_surface_create.argtypes = [+ ctypes.c_uint32,+ ctypes.c_uint32,+ ctypes.c_uint32,+ ctypes.c_char_p,+ ctypes.c_int,+ ]+ library.dlss5nr_surface_create.restype = ctypes.c_void_p+ library.dlss5nr_surface_frame_desc.argtypes = [+ ctypes.c_void_p,+ ctypes.POINTER(FrameDescriptorV1),+ ]+ library.dlss5nr_surface_frame_desc.restype = ctypes.c_int+ library.dlss5nr_surface_retain.argtypes = [ctypes.c_void_p]+ library.dlss5nr_surface_retain.restype = None+ library.dlss5nr_surface_release.argtypes = [ctypes.c_void_p]+ library.dlss5nr_surface_release.restype = None+ release = getattr(library, "dlss5nr_release_session", None)+ if release is not None:+ release.argtypes = []+ release.restype = ctypes.c_int+ self._library = library+ self._version = version++ def _guard_poison(self) -> None:+ if self._poisoned_reason:+ raise NeuralBridgePoisonedError(+ f"The Neural Rendering native session is poisoned: "+ f"{self._poisoned_reason}. Restart the application before rendering again."+ )++ def _call_with_watchdog(+ self, label: str, function: Callable[[], Any], references: tuple[Any, ...] = (),+ *, timeout_seconds: float = BRIDGE_WATCHDOG_SECONDS,+ ) -> Any:+ completed = threading.Event()+ result: list[Any] = []+ failure: list[BaseException] = []++ def invoke() -> None:+ try:+ result.append(function())+ except BaseException as exc:+ failure.append(exc)+ finally:+ completed.set()++ thread = threading.Thread(+ target=invoke, name=f"dlssnr-{label}", daemon=True+ )+ thread.start()+ if not completed.wait(timeout_seconds):+ self._poisoned_reason = f"{label} exceeded {timeout_seconds:g} seconds"+ # Native code may still be touching these buffers. Keep them alive+ # until process exit instead of risking use-after-free.+ self._timed_out_references.extend(references)+ raise NeuralBridgePoisonedError(+ f"Neural Rendering timed out during {label}; native state may be "+ "corrupted. Restart the application before rendering again."+ )+ if failure:+ self._poisoned_reason = f"native exception during {label}: {failure[0]}"+ raise NeuralBridgePoisonedError(+ f"Neural Rendering raised a native exception during {label}. Restart "+ f"the application before rendering again: {failure[0]}"+ ) from failure[0]+ return result[0]++ def initialize(self, gpu: dict[str, Any], *, require_cuda: bool) -> dict[str, Any]:+ with self._lock:+ self._guard_poison()+ self._load()+ ordinal = int(gpu.get("cuda_ordinal", gpu.get("index", 0)))+ if self._initialized_ordinal is None:+ assert self._library is not None+ error = ctypes.create_string_buffer(4096)+ ok = self._call_with_watchdog(+ "initialization",+ lambda: self._library.dlss5nr_init(+ ordinal, str(DLSSNR_DIR), error, len(error)+ ),+ (error,),+ )+ if not ok:+ detail = _text(error.value) or "unknown initialization failure"+ raise NeuralBridgeError(f"Feature-18 bridge initialization failed: {detail}")+ self._initialized_ordinal = ordinal+ self._gpu_name = _text(self._library.dlss5nr_gpu_name()) or "unknown"+ elif ordinal != self._initialized_ordinal:+ if self._active_sessions:+ raise NeuralBridgeError(+ "The Neural Rendering adapter cannot change while a render is active."+ )+ with _DLPACK_LOCK:+ outstanding_surfaces = len(_DLPACK_RECORDS)+ if outstanding_surfaces:+ raise NeuralBridgeError(+ "The Neural Rendering adapter cannot change while encoded CUDA "+ "surfaces are still referenced. Wait for the current output to close."+ )+ if self._cuda_driver is not None:+ try:+ self._cuda_driver.close()+ except Exception as exc:+ self._poisoned_reason = f"CUDA context release failed before adapter rebinding: {exc}"+ self._cuda_driver = None+ self._guard_poison()+ self._cuda_driver = None+ assert self._library is not None+ error = ctypes.create_string_buffer(4096)+ ok = self._call_with_watchdog(+ "adapter rebinding",+ lambda: self._library.dlss5nr_rebind(ordinal, error, len(error)),+ (error,),+ )+ if not ok:+ detail = _text(error.value) or "unknown adapter rebinding failure"+ self._poisoned_reason = detail+ raise NeuralBridgePoisonedError(+ f"Neural Rendering could not safely rebind to CUDA device {ordinal}: "+ f"{detail}. Restart the application before rendering again."+ )+ self._initialized_ordinal = ordinal+ self._gpu_name = _text(self._library.dlss5nr_gpu_name()) or "unknown"++ cuda_status = ctypes.create_string_buffer(2048)+ cuda_ready = bool(+ self._library.dlss5nr_cuda_status(cuda_status, len(cuda_status))+ )+ cuda_detail = _text(cuda_status.value) or "unavailable"+ if require_cuda and not cuda_ready:+ raise NeuralBridgeError(+ f"CUDA/D3D12 interoperability is unavailable ({cuda_detail}). "+ "Switch GPU OFF to use host staging; Neural Rendering still requires "+ "an RTX GPU."+ )+ if require_cuda and self._cuda_driver is None:+ try:+ self._cuda_driver = _CudaDriver(ordinal)+ # Do not leave the primary context current on the UI/decoder+ # thread. FFmpeg creates its CUDA hwdevice with this thread.+ self._cuda_driver.deactivate()+ except Exception as exc:+ raise NeuralBridgeError(+ f"CUDA/D3D12 interoperability setup failed: {exc} Switch GPU OFF "+ "to use host staging."+ ) from exc+ return {+ "bridge_version": self._version,+ "bridge_abi_version": BRIDGE_ABI_VERSION,+ "gpu_name": self._gpu_name,+ "adapter_luid": _text(self._library.dlss5nr_adapter_luid()) or "unknown",+ "cuda_ordinal": ordinal,+ "cuda_supported": cuda_ready,+ "cuda_status": cuda_detail,+ }++ def open_session(self) -> None:+ with self._lock:+ self._guard_poison()+ self._active_sessions += 1++ def close_session(self) -> None:+ with self._lock:+ if self._active_sessions:+ self._active_sessions -= 1+ # Release feature-owned surfaces at the idle boundary, but never+ # invoke NGX core shutdown or unload the driver/runtime modules.+ if self._active_sessions == 0 and self._library is not None:+ release = getattr(self._library, "dlss5nr_release_session", None)+ if release is not None and not self._poisoned_reason:+ try:+ self._call_with_watchdog("session release", release)+ except NeuralBridgePoisonedError:+ raise++ def create_cuda_buffers(self, width: int, height: int) -> CudaFrameBuffers:+ with self._lock:+ self._guard_poison()+ if self._cuda_driver is None:+ raise NeuralBridgeError(+ "CUDA/D3D12 interoperability is not initialized. Switch GPU OFF to "+ "use host staging."+ )+ try:+ return CudaFrameBuffers.create(self._cuda_driver, width, height)+ finally:+ self._cuda_driver.deactivate()++ def create_cuda_mask(self, mask: np.ndarray) -> CudaMaskBuffer:+ with self._lock:+ self._guard_poison()+ if self._cuda_driver is None:+ raise NeuralBridgeError("CUDA mask upload requires GPU mode.")+ try:+ return CudaMaskBuffer.create(self._cuda_driver, mask)+ finally:+ self._cuda_driver.deactivate()++ def create_video_surface(+ self, width: int, height: int, pixel_format: int+ ) -> BridgeCudaSurface:+ """Allocate one bridge-owned CUDA NV12/P010 output surface."""+ with self._lock:+ self._guard_poison()+ if self._library is None or self._cuda_driver is None:+ raise NeuralBridgeError(+ "CUDA/D3D12 interoperability is not initialized. Switch GPU OFF to "+ "use host staging."+ )+ if pixel_format not in (FORMAT_NV12, FORMAT_P010):+ raise NeuralBridgeError("CUDA video output must be NV12 or P010.")+ error = ctypes.create_string_buffer(4096)+ handle = self._call_with_watchdog(+ "CUDA output allocation",+ lambda: self._library.dlss5nr_surface_create(+ int(width), int(height), int(pixel_format), error, len(error)+ ),+ (error,),+ )+ if not handle:+ detail = _text(error.value) or "unknown CUDA output allocation failure"+ raise NeuralBridgeError(+ f"CUDA/D3D12 output allocation failed: {detail}. Switch GPU OFF to "+ "use host staging."+ )+ descriptor = FrameDescriptorV1.empty()+ if not self._library.dlss5nr_surface_frame_desc(+ ctypes.c_void_p(handle), ctypes.byref(descriptor)+ ):+ self._library.dlss5nr_surface_release(ctypes.c_void_p(handle))+ raise NeuralBridgeError("The bridge returned an invalid CUDA output surface.")+ return BridgeCudaSurface(+ self._library, handle, descriptor, self._cuda_driver.ordinal+ )++ @staticmethod+ def _render_parameters(+ settings: dict[str, int | float | bool], reset: bool,+ mask: np.ndarray | None = None,+ cuda_mask: CudaMaskBuffer | None = None,+ ) -> RenderParametersV6:+ value = RenderParametersV6()+ value.struct_size = ctypes.sizeof(RenderParametersV6)+ value.abi_version = BRIDGE_ABI_VERSION+ value.style = int(settings["style"])+ value.intensity = float(settings["intensity"])+ value.tone = float(settings["local_tone"])+ value.structure = float(settings["local_structure"])+ value.skin = float(settings["skin_structure"])+ value.automask = int(bool(settings["auto_mask"]))+ value.reset = int(bool(reset))+ value.color_strength = float(settings["color_strength"])+ value.tone_preservation = float(settings["tone_preservation"])+ value.face_skin_protection = float(settings["face_skin_protection"])+ value.grain_preservation = float(settings["grain_preservation"])+ value.nr_passes = int(settings.get("nr_passes", 1))+ value.shimmer_suppression = float(settings.get("shimmer_suppression", 0.0))+ value.prefer_nvof = int(bool(settings.get("prefer_nvof", False)))+ value.mask_memory_type = MEMORY_NONE+ if cuda_mask is not None:+ value.mask_memory_type = MEMORY_CUDA+ value.mask_width = int(mask.shape[1]) if mask is not None else 0+ value.mask_height = int(mask.shape[0]) if mask is not None else 0+ value.mask_stride = int(mask.strides[0]) if mask is not None else 0+ value.mask_plane = int(cuda_mask.pointer)+ elif mask is not None:+ value.mask_memory_type = MEMORY_HOST+ value.mask_width = int(mask.shape[1])+ value.mask_height = int(mask.shape[0])+ value.mask_stride = int(mask.strides[0])+ value.mask_plane = int(mask.ctypes.data)+ return value++ def process_cuda_video_frame(+ self,+ frame: Any,+ *,+ output_width: int,+ output_height: int,+ output_format: int,+ settings: dict[str, int | float | bool],+ mask: np.ndarray | None,+ cuda_mask: CudaMaskBuffer | None,+ reset: bool,+ timestamp: int,+ color_matrix: int = 1,+ color_range: int = 0,+ rotation: int = 0,+ ) -> tuple[Any, dict[str, Any], float]:+ """Evaluate a PyAV CUDA frame and return a CUDA AVFrame for NVENC.++ Both the decoded input planes and returned output planes remain on the+ selected CUDA device. The returned AVFrame owns the native allocation+ through the DLPack deleters until the encoder releases it.+ """+ with self._lock:+ self._guard_poison()+ if self._library is None or self._cuda_driver is None:+ raise NeuralBridgeError(+ "CUDA/D3D12 interoperability is not initialized. Switch GPU OFF to "+ "use host staging."+ )+ format_name = str(getattr(getattr(frame, "format", None), "name", ""))+ if format_name != "cuda":+ raise NeuralBridgeError(+ f"Expected a CUDA-decoded PyAV frame, received {format_name or 'unknown'}. "+ "This source must use the reported software-decode boundary."+ )+ sw_format = str(getattr(getattr(frame, "sw_format", None), "name", ""))+ source_format = {+ "nv12": FORMAT_NV12,+ "p010": FORMAT_P010,+ "p010le": FORMAT_P010,+ }.get(sw_format)+ if source_format is None:+ raise NeuralBridgeError(+ f"CUDA decode format {sw_format or 'unknown'} is outside the supported "+ "NV12/P010 Neural Rendering boundary."+ )+ if len(frame.planes) < 2:+ raise NeuralBridgeError("The CUDA video frame does not expose two YUV planes.")++ source = FrameDescriptorV1.empty()+ source.memory_type = MEMORY_CUDA+ source.pixel_format = source_format+ source.width = int(frame.width)+ source.height = int(frame.height)+ source.planes[0] = int(frame.planes[0].buffer_ptr)+ source.planes[1] = int(frame.planes[1].buffer_ptr)+ source.strides[0] = int(frame.planes[0].line_size)+ source.strides[1] = int(frame.planes[1].line_size)+ source.color_matrix = int(color_matrix)+ source.color_range = int(color_range)+ source.rotation = int(rotation) % 360+ source.timestamp = int(timestamp)++ surface = self.create_video_surface(+ output_width, output_height, output_format+ )+ destination = surface.descriptor+ destination.color_matrix = int(color_matrix)+ destination.color_range = int(color_range)+ destination.timestamp = int(timestamp)+ params = self._render_parameters(settings, reset, mask, cuda_mask)+ result = FrameResultV1.empty()+ error = ctypes.create_string_buffer(4096)+ started = time.perf_counter()+ try:+ ok = self._call_with_watchdog(+ "feature-18 CUDA video evaluation",+ lambda: self._library.dlss5nr_process_frame_v6(+ ctypes.byref(source),+ ctypes.byref(destination),+ ctypes.byref(params),+ ctypes.byref(result),+ error,+ len(error),+ ),+ (frame, surface, source, destination, params, result, error),+ timeout_seconds=min(180.0, BRIDGE_WATCHDOG_SECONDS * params.nr_passes),+ )+ elapsed = time.perf_counter() - started+ if not ok:+ detail = _text(error.value) or "unknown CUDA frame-ABI failure"+ if "corrupt" in detail.lower() or "access violation" in detail.lower():+ self._poisoned_reason = detail+ raise NeuralBridgePoisonedError(+ f"{detail}. Restart the application before rendering again."+ )+ raise NeuralBridgeError(+ f"CUDA/D3D12 Neural Rendering failed: {detail}. Switch GPU OFF to "+ "use host staging."+ )+ # AVFrame.from_dlpack establishes its own primary-context+ # references; do not leak the bridge's current context into+ # PyAV's encoder setup on this thread.+ self._cuda_driver.deactivate()+ output = surface.to_av_frame()+ except BaseException:+ with contextlib.suppress(Exception):+ self._cuda_driver.deactivate()+ surface.close()+ raise++ output.pts = getattr(frame, "pts", None)+ output.time_base = getattr(frame, "time_base", None)+ if getattr(frame, "duration", None) is not None:+ output.duration = frame.duration+ details = {+ "ngx_create_result": f"0x{int(result.ngx_create_result) & 0xFFFFFFFF:08X}",+ "ngx_evaluate_result": f"0x{int(result.ngx_evaluate_result) & 0xFFFFFFFF:08X}",+ "cuda_result": int(result.cuda_result),+ "scene_reset": bool(result.scene_reset),+ "scene_score": float(result.scene_score),+ "upload_bytes": int(result.upload_bytes),+ "download_bytes": int(result.download_bytes),+ "timestamp": int(result.timestamp),+ "input_format": sw_format,+ "output_format": "p010le" if output_format == FORMAT_P010 else "nv12",+ }+ return output, details, elapsed++ def score_cuda_video_frame(+ self,+ frame: Any,+ *,+ threshold: float = 0.24,+ color_matrix: int = 1,+ color_range: int = 0,+ ) -> tuple[float, bool]:+ """Score a reduced luma signature while the decoded frame stays on CUDA."""+ with self._lock:+ self._guard_poison()+ if self._library is None or self._cuda_driver is None:+ raise NeuralBridgeError("CUDA scene scoring requires GPU mode.")+ format_name = str(getattr(getattr(frame, "format", None), "name", ""))+ sw_format = str(getattr(getattr(frame, "sw_format", None), "name", ""))+ source_format = {+ "nv12": FORMAT_NV12,+ "p010": FORMAT_P010,+ "p010le": FORMAT_P010,+ }.get(sw_format)+ if format_name != "cuda" or source_format is None or len(frame.planes) < 2:+ raise NeuralBridgeError(+ f"CUDA scene scoring supports NV12/P010, received "+ f"{format_name or 'unknown'}/{sw_format or 'unknown'}."+ )+ source = FrameDescriptorV1.empty()+ source.memory_type = MEMORY_CUDA+ source.pixel_format = source_format+ source.width = int(frame.width)+ source.height = int(frame.height)+ source.planes[0] = int(frame.planes[0].buffer_ptr)+ source.planes[1] = int(frame.planes[1].buffer_ptr)+ source.strides[0] = int(frame.planes[0].line_size)+ source.strides[1] = int(frame.planes[1].line_size)+ source.color_matrix = int(color_matrix)+ source.color_range = int(color_range)+ source.timestamp = int(getattr(frame, "pts", None) or 0)+ score = ctypes.c_float()+ reset = ctypes.c_int()+ error = ctypes.create_string_buffer(4096)+ ok = self._call_with_watchdog(+ "CUDA reduced-luma scene score",+ lambda: self._library.dlss5nr_scene_score_v1(+ ctypes.byref(source),+ float(threshold),+ ctypes.byref(score),+ ctypes.byref(reset),+ error,+ len(error),+ ),+ (frame, source, score, reset, error),+ )+ self._cuda_driver.deactivate()+ if not ok:+ detail = _text(error.value) or "unknown CUDA scene-scoring failure"+ raise NeuralBridgeError(+ f"CUDA reduced-luma scene scoring failed: {detail}. Switch GPU OFF "+ "to use host staging."+ )+ return float(score.value), bool(reset.value)++ def process_host_to_cuda_video_frame(+ self,+ rgba: np.ndarray,+ *,+ output_format: int,+ settings: dict[str, int | float | bool],+ mask: np.ndarray | None,+ cuda_mask: CudaMaskBuffer | None,+ reset: bool,+ timestamp: int,+ color_matrix: int = 1,+ color_range: int = 0,+ time_base: Any | None = None,+ duration: int | None = None,+ ) -> tuple[Any, dict[str, Any], float]:+ """Upload one software-decoded RGBA frame and return CUDA YUV."""+ with self._lock:+ self._guard_poison()+ if self._library is None or self._cuda_driver is None:+ raise NeuralBridgeError("Host-to-CUDA video processing requires GPU mode.")+ if (+ rgba.dtype != np.uint8+ or rgba.ndim != 3+ or rgba.shape[2] != 4+ or not rgba.flags.c_contiguous+ ):+ raise NeuralBridgeError("Host video input must be contiguous RGBA8.")+ height, width = rgba.shape[:2]+ source = FrameDescriptorV1.empty()+ source.memory_type = MEMORY_HOST+ source.pixel_format = FORMAT_RGBA8+ source.width = int(width)+ source.height = int(height)+ source.planes[0] = int(rgba.ctypes.data)+ source.strides[0] = int(rgba.strides[0])+ source.color_matrix = int(color_matrix)+ source.color_range = int(color_range)+ source.timestamp = int(timestamp)++ surface = self.create_video_surface(width, height, output_format)+ destination = surface.descriptor+ destination.color_matrix = int(color_matrix)+ destination.color_range = int(color_range)+ destination.timestamp = int(timestamp)+ params = self._render_parameters(settings, reset, mask, cuda_mask)+ result = FrameResultV1.empty()+ error = ctypes.create_string_buffer(4096)+ started = time.perf_counter()+ try:+ ok = self._call_with_watchdog(+ "feature-18 host-to-CUDA video evaluation",+ lambda: self._library.dlss5nr_process_frame_v6(+ ctypes.byref(source), ctypes.byref(destination),+ ctypes.byref(params), ctypes.byref(result), error, len(error),+ ),+ (rgba, surface, source, destination, params, result, error),+ timeout_seconds=min(180.0, BRIDGE_WATCHDOG_SECONDS * params.nr_passes),+ )+ elapsed = time.perf_counter() - started+ self._cuda_driver.deactivate()+ if not ok:+ detail = _text(error.value) or "unknown host-to-CUDA frame failure"+ raise NeuralBridgeError(+ f"CUDA/D3D12 Neural Rendering failed: {detail}. Switch GPU OFF "+ "to use host staging."+ )+ output = surface.to_av_frame()+ except BaseException:+ with contextlib.suppress(Exception):+ self._cuda_driver.deactivate()+ surface.close()+ raise+ output.pts = int(timestamp)+ if time_base is not None:+ output.time_base = time_base+ if duration is not None:+ output.duration = int(duration)+ details = {+ "ngx_create_result": f"0x{int(result.ngx_create_result) & 0xFFFFFFFF:08X}",+ "ngx_evaluate_result": f"0x{int(result.ngx_evaluate_result) & 0xFFFFFFFF:08X}",+ "cuda_result": int(result.cuda_result),+ "scene_reset": bool(result.scene_reset),+ "scene_score": float(result.scene_score),+ "upload_bytes": int(result.upload_bytes),+ "download_bytes": int(result.download_bytes),+ "timestamp": int(result.timestamp),+ "input_format": "rgba8",+ "output_format": "p010le" if output_format == FORMAT_P010 else "nv12",+ }+ return output, details, elapsed++ def process_cuda_to_host_video_frame(+ self,+ frame: Any,+ destination: np.ndarray,+ *,+ settings: dict[str, int | float | bool],+ mask: np.ndarray | None,+ cuda_mask: CudaMaskBuffer | None,+ reset: bool,+ timestamp: int,+ color_matrix: int = 1,+ color_range: int = 0,+ rotation: int = 0,+ ) -> tuple[dict[str, Any], float]:+ """Evaluate an NVDEC CUDA frame and download only the final RGBA result."""+ with self._lock:+ self._guard_poison()+ format_name = str(getattr(getattr(frame, "format", None), "name", ""))+ sw_format = str(getattr(getattr(frame, "sw_format", None), "name", ""))+ source_format = {+ "nv12": FORMAT_NV12,+ "p010": FORMAT_P010,+ "p010le": FORMAT_P010,+ }.get(sw_format)+ if format_name != "cuda" or source_format is None or len(frame.planes) < 2:+ raise NeuralBridgeError(+ f"CUDA-to-host processing supports NV12/P010, received "+ f"{format_name or 'unknown'}/{sw_format or 'unknown'}."+ )+ if (+ destination.dtype != np.uint8+ or destination.ndim != 3+ or destination.shape[2] != 4+ or not destination.flags.c_contiguous+ ):+ raise NeuralBridgeError("CUDA-to-host output must be contiguous RGBA8.")+ source = FrameDescriptorV1.empty()+ source.memory_type = MEMORY_CUDA+ source.pixel_format = source_format+ source.width = int(frame.width)+ source.height = int(frame.height)+ source.planes[0] = int(frame.planes[0].buffer_ptr)+ source.planes[1] = int(frame.planes[1].buffer_ptr)+ source.strides[0] = int(frame.planes[0].line_size)+ source.strides[1] = int(frame.planes[1].line_size)+ source.color_matrix = int(color_matrix)+ source.color_range = int(color_range)+ source.rotation = int(rotation) % 360+ source.timestamp = int(timestamp)+ output = FrameDescriptorV1.empty()+ output.memory_type = MEMORY_HOST+ output.pixel_format = FORMAT_RGBA8+ output.width = int(destination.shape[1])+ output.height = int(destination.shape[0])+ output.planes[0] = int(destination.ctypes.data)+ output.strides[0] = int(destination.strides[0])+ output.color_matrix = int(color_matrix)+ output.color_range = int(color_range)+ output.timestamp = int(timestamp)+ params = self._render_parameters(settings, reset, mask, cuda_mask)+ result = FrameResultV1.empty()+ error = ctypes.create_string_buffer(4096)+ started = time.perf_counter()+ ok = self._call_with_watchdog(+ "feature-18 CUDA-to-host video evaluation",+ lambda: self._library.dlss5nr_process_frame_v6(+ ctypes.byref(source), ctypes.byref(output), ctypes.byref(params),+ ctypes.byref(result), error, len(error),+ ),+ (frame, destination, source, output, params, result, error),+ timeout_seconds=min(180.0, BRIDGE_WATCHDOG_SECONDS * params.nr_passes),+ )+ elapsed = time.perf_counter() - started+ self._cuda_driver.deactivate()+ if not ok:+ detail = _text(error.value) or "unknown CUDA-to-host frame failure"+ raise NeuralBridgeError(+ f"CUDA/D3D12 Neural Rendering failed: {detail}. Switch GPU OFF to "+ "use host staging."+ )+ return {+ "ngx_create_result": f"0x{int(result.ngx_create_result) & 0xFFFFFFFF:08X}",+ "ngx_evaluate_result": f"0x{int(result.ngx_evaluate_result) & 0xFFFFFFFF:08X}",+ "cuda_result": int(result.cuda_result),+ "scene_reset": bool(result.scene_reset),+ "scene_score": float(result.scene_score),+ "upload_bytes": int(result.upload_bytes),+ "download_bytes": int(result.download_bytes),+ "timestamp": int(result.timestamp),+ "input_format": sw_format,+ "output_format": "rgba8",+ }, elapsed++ def process_host(+ self,+ source: np.ndarray,+ destination: np.ndarray,+ settings: dict[str, int | float | bool],+ reset: bool,+ mask: np.ndarray | None = None,+ ) -> float:+ with self._lock:+ self._guard_poison()+ assert self._library is not None+ error = ctypes.create_string_buffer(4096)+ started = time.perf_counter()+ params = self._render_parameters(settings, reset, mask, None)+ ok = self._call_with_watchdog(+ "feature-18 host evaluation",+ lambda: self._library.dlss5nr_process_v6(+ source.ctypes.data_as(ctypes.POINTER(ctypes.c_float)),+ destination.ctypes.data_as(ctypes.POINTER(ctypes.c_float)),+ source.shape[1],+ source.shape[0],+ ctypes.byref(params),+ error,+ len(error),+ ),+ (source, destination, mask, params, error),+ timeout_seconds=min(180.0, BRIDGE_WATCHDOG_SECONDS * params.nr_passes),+ )+ elapsed = time.perf_counter() - started+ if not ok:+ detail = _text(error.value) or "unknown feature-18 failure"+ if "corrupt" in detail.lower() or "access violation" in detail.lower():+ self._poisoned_reason = detail+ raise NeuralBridgePoisonedError(+ f"{detail}. Restart the application before rendering again."+ )+ raise NeuralBridgeError(f"Feature-18 evaluation failed: {detail}")+ return elapsed++ def process_cuda(+ self,+ source: np.ndarray,+ destination: np.ndarray,+ buffers: CudaFrameBuffers,+ settings: dict[str, int | float | bool],+ reset: bool,+ mask: np.ndarray | None = None,+ cuda_mask: CudaMaskBuffer | None = None,+ ) -> tuple[float, float, float]:+ with self._lock:+ self._guard_poison()+ assert self._library is not None and self._cuda_driver is not None+ error = ctypes.create_string_buffer(4096)+ upload_started = time.perf_counter()+ buffers.driver.upload(buffers.input_pointer, source)+ upload_seconds = time.perf_counter() - upload_started+ evaluate_started = time.perf_counter()+ params = self._render_parameters(settings, reset, mask, cuda_mask)+ ok = self._call_with_watchdog(+ "feature-18 CUDA evaluation",+ lambda: self._library.dlss5nr_process_cuda_v6(+ buffers.input_pointer,+ buffers.output_pointer,+ source.shape[1],+ source.shape[0],+ 0,+ ctypes.byref(params),+ error,+ len(error),+ ),+ (source, destination, buffers, mask, cuda_mask, params, error),+ timeout_seconds=min(180.0, BRIDGE_WATCHDOG_SECONDS * params.nr_passes),+ )+ evaluate_seconds = time.perf_counter() - evaluate_started+ if not ok:+ detail = _text(error.value) or "unknown CUDA interoperability failure"+ if "corrupt" in detail.lower() or "access violation" in detail.lower():+ self._poisoned_reason = detail+ raise NeuralBridgePoisonedError(+ f"{detail}. Restart the application before rendering again."+ )+ raise NeuralBridgeError(+ f"CUDA/D3D12 Neural Rendering failed: {detail}. Switch GPU OFF to "+ "use host staging."+ )+ download_started = time.perf_counter()+ buffers.driver.synchronize()+ buffers.driver.download(destination, buffers.output_pointer)+ download_seconds = time.perf_counter() - download_started+ return upload_seconds, evaluate_seconds, download_seconds+++BRIDGE_MANAGER = NeuralBridgeManager()diff --git a/src/core/nr_composition.py b/src/core/nr_composition.pynew file mode 100644index 0000000..445045c--- /dev/null+++ b/src/core/nr_composition.py@@ -0,0 +1,162 @@+"""Session-scoped custom-mask preparation and report-safe composition metadata."""+from __future__ import annotations++import hashlib+import itertools+import threading+from dataclasses import asdict, dataclass+from pathlib import Path+from typing import Any++import cv2+import numpy as np+from PIL import Image, ImageOps+++_REVISION_COUNTER = itertools.count(1)+_REVISION_LOCK = threading.Lock()+++def _next_revision() -> int:+ with _REVISION_LOCK:+ return next(_REVISION_COUNTER)+++@dataclass(frozen=True, slots=True)+class NRMaskSelection:+ """Validated upload identity. The path is temporary and must not be persisted."""++ path: str+ source_name: str+ sha256: str+ width: int+ height: int+ revision: int++ def state(self) -> dict[str, Any]:+ return asdict(self)+++def _sha256(path: Path) -> str:+ digest = hashlib.sha256()+ with path.open("rb") as stream:+ for chunk in iter(lambda: stream.read(1024 * 1024), b""):+ digest.update(chunk)+ return digest.hexdigest()+++def inspect_nr_mask(path: str | Path | None) -> NRMaskSelection | None:+ """Validate an uploaded image without retaining decoded pixels."""+ if not path:+ return None+ source = Path(path)+ if not source.is_file():+ raise ValueError("The selected Custom NR Mask is no longer available.")+ try:+ with Image.open(source) as opened:+ opened.seek(0)+ oriented = ImageOps.exif_transpose(opened)+ width, height = oriented.size+ oriented.load()+ except Exception as exc:+ raise ValueError(f"Custom NR Mask is not a readable image: {exc}") from exc+ if width <= 0 or height <= 0:+ raise ValueError("Custom NR Mask has invalid dimensions.")+ return NRMaskSelection(+ path=str(source.resolve()),+ source_name=source.name,+ sha256=_sha256(source),+ width=int(width),+ height=int(height),+ revision=_next_revision(),+ )+++def mask_selection(value: object | None) -> NRMaskSelection | None:+ if value is None or value == "":+ return None+ if isinstance(value, NRMaskSelection):+ return value+ if isinstance(value, (str, Path)):+ return inspect_nr_mask(value)+ if isinstance(value, dict):+ required = {"path", "source_name", "sha256", "width", "height", "revision"}+ if not required.issubset(value):+ raise ValueError("Custom NR Mask session state is incomplete.")+ return NRMaskSelection(+ path=str(value["path"]),+ source_name=str(value["source_name"]),+ sha256=str(value["sha256"]),+ width=int(value["width"]),+ height=int(value["height"]),+ revision=int(value["revision"]),+ )+ raise ValueError("Custom NR Mask session state is invalid.")+++def prepare_nr_mask(+ selection: object | None, width: int, height: int, feather: int,+) -> np.ndarray | None:+ """Decode, stretch, and feather one mask for a native render session."""+ selected = mask_selection(selection)+ if selected is None:+ return None+ if isinstance(feather, bool) or int(feather) != feather or not 0 <= int(feather) <= 128:+ raise ValueError("Mask Feather must be an integer from 0 to 128.")+ try:+ with Image.open(selected.path) as opened:+ opened.seek(0)+ rgba = np.asarray(+ ImageOps.exif_transpose(opened).convert("RGBA"), dtype=np.float32+ )+ except Exception as exc:+ raise ValueError(+ f"Custom NR Mask {selected.source_name!r} could not be decoded."+ ) from exc+ rgb = rgba[..., :3] * (1.0 / 255.0)+ alpha = rgba[..., 3] * (1.0 / 255.0)+ mask = (+ rgb[..., 0] * 0.2126 + rgb[..., 1] * 0.7152 + rgb[..., 2] * 0.0722+ ) * alpha+ if mask.shape != (int(height), int(width)):+ interpolation = (+ cv2.INTER_AREA+ if mask.shape[1] > int(width) or mask.shape[0] > int(height)+ else cv2.INTER_LINEAR+ )+ mask = cv2.resize(mask, (int(width), int(height)), interpolation=interpolation)+ if feather:+ mask = cv2.GaussianBlur(+ mask, (0, 0), sigmaX=float(feather), sigmaY=float(feather),+ borderType=cv2.BORDER_REPLICATE,+ )+ return np.ascontiguousarray(np.clip(mask, 0.0, 1.0), dtype=np.float32)+++def mask_report(selection: object | None, feather: int) -> dict[str, Any]:+ selected = mask_selection(selection)+ if selected is None:+ return {"active": False, "feather_px": int(feather), "feather_status": "inactive — no mask"}+ return {+ "active": True,+ "source_name": selected.source_name,+ "sha256": selected.sha256,+ "width": selected.width,+ "height": selected.height,+ "feather_px": int(feather),+ }+++def report_options(options: object) -> dict[str, Any]:+ """Return dataclass options without leaking a Gradio temporary path."""+ values = asdict(options)+ selection = values.pop("nr_mask", None)+ values["custom_nr_mask"] = mask_report(selection, int(values.get("mask_feather", 0)))+ return values+++def mask_status(selection: object | None) -> str:+ selected = mask_selection(selection)+ if selected is None:+ return ""+ return f"Active: {selected.source_name} — {selected.width}×{selected.height}."diff --git a/src/core/paths.py b/src/core/paths.pyindex 9af5ff7..95ec8da 100644--- a/src/core/paths.py+++ b/src/core/paths.py@@ -4,19 +4,17 @@ from pathlib import PathROOT = Path(__file__).resolve().parents[2]+TEMP = ROOT / "temp"+GRADIO_TEMP = TEMP / "gradio"RUNTIME = ROOT / "bin" / "runtime"-HOST_DIR = RUNTIME / "host"-DLSS_DIR = RUNTIME / "dlss"DLSSG_DIR = RUNTIME / "dlssg"-# Universal FP16 Neural Rendering runtime for all supported RTX architectures.+# In-process D3D12/NGX feature-18 runtime. The bridge and caller shim are+# self-contained and require no Python tensor framework or external add-on.DLSSNR_DIR = RUNTIME / "dlssnr"+DLSSNR_BRIDGE = DLSSNR_DIR / "neuroframe_engine.dll"+DLSSNR_CALLER_SHIM = DLSSNR_DIR / "neuroframe_caller.dll"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.-ADDON = DLSSNR_DIR / "renodx-dlss5.addon64"-HOST_DXGI = HOST_DIR / "dxgi.dll"-RESHADE_LOG = HOST_DIR / "ReShade.log"-DLSS_SUPERRES = DLSS_DIR / "nvngx_dlss.dll"NEURAL_RUNTIME = DLSSNR_DIR / "nvngx_dlssnr.dll"# Live-tab externals (vendored, optional: only Live sessions require them).MPV = ROOT / "bin" / "mpv" / "mpv.exe"diff --git a/src/core/render_metadata.py b/src/core/render_metadata.pyindex 9258f0b..b43b558 100644--- a/src/core/render_metadata.py+++ b/src/core/render_metadata.py@@ -10,21 +10,96 @@ 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",+ "NR Style", "NR Intensity", "NR Passes", "Local Tone Strength","Local Structure Strength", "Skin Structure Strength", "Automatic Mask",- "DLSS Model Preset", "Upscaling Factor",+ "NR Color Strength", "Tone Preservation", "Face/Skin Protection",+ "Grain Preservation", "Custom NR Mask", "Mask Feather",+ "GPU Memory Path", "Scale",)_NUMBER = r"-?\d+(?:\.\d+)?(?:e[+-]?\d+)?"+_SCALE_VALUE = r"(?:Source|75%|50%|25%)"_VALUES = (+ r"(?:Default|Natural|Cinematic)",+ _NUMBER, r"[1-4]", _NUMBER, _NUMBER,+ _NUMBER + r"(?: \((?:Default|inactive - Automatic Mask Off)\))?",+ r"(?:On|Off)", _NUMBER, _NUMBER, _NUMBER, _NUMBER, r"(?:On|Off)", _NUMBER,+ r"(?:On|Off)", _SCALE_VALUE,+)+_V4_LABELS = (+ "NR Style", "NR Intensity", "Local Tone Strength",+ "Local Structure Strength", "Skin Structure Strength", "Automatic Mask",+ "NR Color Strength", "Tone Preservation", "Face/Skin Protection",+ "Grain Preservation", "Custom NR Mask", "Mask Feather",+ "GPU Memory Path", "Output Scale",+)+_V4_VALUES = (+ r"(?:Default|Natural|Cinematic)",+ _NUMBER, _NUMBER, _NUMBER,+ _NUMBER + r"(?: \((?:Default|inactive - Automatic Mask Off)\))?",+ r"(?:On|Off)", _NUMBER, _NUMBER, _NUMBER, _NUMBER, r"(?:On|Off)", _NUMBER,+ r"(?:On|Off)", _NUMBER + "x",+)+_V3_LABELS = (+ "NR Style", "NR Intensity", "Local Tone Strength",+ "Local Structure Strength", "Skin Structure Strength", "Automatic Mask",+ "NR Color Strength", "Tone Preservation", "Custom NR Mask", "Mask Feather",+ "GPU Memory Path", "Output Scale",+)+_V3_VALUES = (+ r"(?:Default|Natural|Cinematic)",+ _NUMBER, _NUMBER, _NUMBER,+ _NUMBER + r"(?: \((?:Default|inactive - Automatic Mask Off)\))?",+ r"(?:On|Off)", _NUMBER, _NUMBER, r"(?:On|Off)", _NUMBER,+ r"(?:On|Off)", _NUMBER + "x",+)+_V2_LABELS = (+ "NR Style", "NR Intensity", "Local Tone Strength",+ "Local Structure Strength", "Skin Structure Strength", "Automatic Mask",+ "GPU Memory Path", "Output Scale",+)+_V2_VALUES = (+ r"(?:Default|Natural|Cinematic)", _NUMBER, _NUMBER, _NUMBER,+ _NUMBER + r"(?: \((?:Default|inactive - Automatic Mask Off)\))?",+ r"(?:On|Off)", r"(?:On|Off)", _NUMBER + "x",+)+# Legacy notes written before NR Preset removal still carry a leading+# "NR Preset - ..." entry. Accept them for read/replace so old outputs merge+# cleanly; new notes are always written without the preset entry.+_LEGACY_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",+ r"(?:On|Off)", r"(?:On|Off)", _NUMBER + "x",+)+_LEGACY_LABELS = (+ "NR Preset", "NR Style", "NR Intensity", "Local Tone Strength",+ "Local Structure Strength", "Skin Structure Strength", "Automatic Mask",+ "GPU Memory Path", "Output Scale",)_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?$)")+_V4_NOTE = re.compile(+ r"(?m)^" + re.escape(APPLICATION + ":") + r"\r?\n" + re.escape(PREFIX)+ + ", ".join(re.escape(label + " - ") + value for label, value in zip(_V4_LABELS, _V4_VALUES))+ + r"(?=\r?$)"+)+_V3_NOTE = re.compile(+ r"(?m)^" + re.escape(APPLICATION + ":") + r"\r?\n" + re.escape(PREFIX)+ + ", ".join(re.escape(label + " - ") + value for label, value in zip(_V3_LABELS, _V3_VALUES))+ + r"(?=\r?$)"+)+_V2_NOTE = re.compile(+ r"(?m)^" + re.escape(APPLICATION + ":") + r"\r?\n" + re.escape(PREFIX)+ + ", ".join(re.escape(label + " - ") + value for label, value in zip(_V2_LABELS, _V2_VALUES))+ + r"(?=\r?$)"+)+_LEGACY_NOTE = re.compile(+ r"(?m)^" + re.escape(APPLICATION + ":") + r"\r?\n" + re.escape(PREFIX)+ + ", ".join(re.escape(label + " - ") + value for label, value in zip(_LEGACY_LABELS, _LEGACY_VALUES))+ + r"(?=\r?$)"+)class MetadataNoteError(ValueError):@@ -53,31 +128,48 @@ def embedding_warning(diagnostics: dict | None, exc: Exception) -> str: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+def skin_structure_status(value: float, automatic_mask: bool) -> str:+ """Describe whether the private Skin control can affect this render."""+ if float(value) == -1.0:+ return "model default"+ if not automatic_mask:+ return "inactive — Automatic Mask Off"+ return "active"+++def build_render_note(options) -> str:+ from .runtime import UPSCALING_MODES, resolve_native_settings, resolve_upscaling_modenative = 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)+ scale = UPSCALING_MODES[factor]["label"]number = lambda value: str(float(value)).removesuffix(".0")skin = number(native["skin_structure"])if native["skin_structure"] == -1:skin += " (Default)"+ elif not native["auto_mask"]:+ # Keep the embedded EXIF description ASCII; Pillow cannot round-trip+ # the human-readable em dash used by JSON reports in this field.+ skin += " (inactive - Automatic Mask Off)"values = (- options.nr_preset, options.nr_style, number(native["intensity"]),+ options.nr_style, number(native["intensity"]),+ str(int(native["nr_passes"])),number(native["local_tone"]), number(native["local_structure"]), skin,- "On" if native["auto_mask"] else "Off", model, number(factor) + "x",+ "On" if native["auto_mask"] else "Off",+ number(native["color_strength"]), number(native["tone_preservation"]),+ number(native["face_skin_protection"]), number(native["grain_preservation"]),+ "On" if getattr(options, "nr_mask", None) else "Off",+ number(native["mask_feather"]),+ "On" if native["gpu_mode"] else "Off", scale,)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:+def prepare_render_note(options, diagnostics: dict) -> str | None:try:- return build_render_note(options, applied_model_preset)+ return build_render_note(options)except (ValueError, TypeError, KeyError, AttributeError, StopIteration) as exc:embedding_warning(diagnostics, exc)return None@@ -91,8 +183,22 @@ def merge_render_note(existing: str | None, note: str) -> str:if not isinstance(existing, str) or "\x00" in existing:raise MetadataNoteError("The existing description cannot be safely extended")matches = list(_OWN_NOTE.finditer(existing))+ v4 = list(_V4_NOTE.finditer(existing))+ v3 = list(_V3_NOTE.finditer(existing))+ v2 = list(_V2_NOTE.finditer(existing))+ legacy = list(_LEGACY_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)+ if v4:+ return _V4_NOTE.sub(lambda _match: note, existing)+ if v3:+ return _V3_NOTE.sub(lambda _match: note, existing)+ if v2:+ return _V2_NOTE.sub(lambda _match: note, existing)+ if legacy:+ # Old outputs still carry the removed NR Preset entry: replace the+ # whole legacy note with the new preset-free note.+ return _LEGACY_NOTE.sub(lambda _match: note, existing)return existing + "\n\n" + notediff --git a/src/core/runtime.py b/src/core/runtime.pyindex 378d969..77721f1 100644--- a/src/core/runtime.py+++ b/src/core/runtime.py@@ -1,15 +1,13 @@from __future__ import annotations-import atexitimport json+import contextlibimport mathimport mmapimport re-import structimport subprocessimport threadingimport time-from contextlib import suppressfrom dataclasses import dataclass, fieldfrom pathlib import Pathfrom typing import Any@@ -19,66 +17,58 @@ import numpy as npfrom .gpu_detection import detect_gpusfrom .gpu_selection import resolve_runtime_ai_gpu-from .jobs import BoundedLogBuffer, Cancelled, JobController, drain_bounded_text+from .jobs import Cancelled, JobController+from .neural_bridge import (+ BRIDGE_ABI_VERSION,+ BRIDGE_MANAGER,+ BridgeSessionDiagnostics,+ CudaFrameBuffers,+ CudaMaskBuffer,+ NeuralBridgeError,+ FORMAT_NV12,+ FORMAT_P010,+)+from .nr_composition import mask_report, mask_selection, prepare_nr_maskfrom .paths import (- ADDON, DLSS_SUPERRES, FFMPEG, FFPROBE, HOST_DIR, HOST_DXGI, LOGS,- NEURAL_RUNTIME, RESHADE_LOG, RUNTIME, WORKER,+ DLSSNR_BRIDGE,+ DLSSNR_CALLER_SHIM,+ DLSSNR_DIR,+ FFMPEG,+ FFPROBE,+ NEURAL_RUNTIME,)--# Shared DLSS Neural Rendering controls and sizing. These are feature-neutral and-# used by both Image and Video processing.-NR_PRESETS = {- "Default": 0,- "Preset #1": 1,- "Preset #2": 2,- "Preset #3": 3,-}-NR_STYLES = {"Default": 0,"Natural": 1,"Cinematic": 2,}-DLSS_MODEL_PRESETS = {- "Default": 0,- "J": 10,- "K": 11,- "L": 12,- "M": 13,-}-+# Feature 18 is evaluated at the final neural dimensions. Scaling below 1x is+# an explicit Lanczos downscale before Neural Rendering.UPSCALING_MODES = {- 1.0: {"label": "1× (DLAA / native)", "name": "DLAA", "perf_quality": 5},- 1.5: {"label": "1.5× (Quality)", "name": "Quality", "perf_quality": 2},- 1.724: {"label": "1.724× (Balanced)", "name": "Balanced", "perf_quality": 1},- 2.0: {"label": "2× (Performance)", "name": "Performance", "perf_quality": 0},- 3.0: {- "label": "3× (Ultra Performance)",- "name": "Ultra Performance",- "perf_quality": 3,- },+ 1.0: {"label": "Source (Original)", "name": "Source", "perf_quality": 0},+ 0.75: {"label": "75%", "name": "Lanczos 75%", "perf_quality": 0},+ 0.5: {"label": "50%", "name": "Lanczos 50%", "perf_quality": 0},+ 0.25: {"label": "25%", "name": "Lanczos 25%", "perf_quality": 0},}UPSCALING_CHOICES = tuple((mode["label"], factor) for factor, mode in UPSCALING_MODES.items())+def resolve_upscaling_mode(raw_factor: float) -> tuple[float, dict[str, str | int]]:try:factor = float(raw_factor)except (TypeError, ValueError) as exc:- raise ValueError(- "Upscaling factor must be one of the supported NVIDIA DLSS modes."- ) from exc+ raise ValueError("Scale must be one of: Source, 75%, 50%, 25%.") from excif not math.isfinite(factor):- raise ValueError("Upscaling factor must be one of the supported NVIDIA DLSS modes.")+ raise ValueError("Scale must be one of: Source, 75%, 50%, 25%.")for supported, mode in UPSCALING_MODES.items():if math.isclose(factor, supported, rel_tol=0.0, abs_tol=1e-9):return supported, mode- choices = ", ".join(f"{factor:g}×" for factor in UPSCALING_MODES)- raise ValueError(f"Unsupported upscaling factor {factor:g}×. Choose one of: {choices}.")+ raise ValueError("Scale must be one of: Source, 75%, 50%, 25%.")def _nearest_even(value: float) -> int:@@ -89,53 +79,27 @@ def resolve_output_size(width: int, height: int, factor: float) -> tuple[int, infactor, _ = resolve_upscaling_mode(factor)output_width = _nearest_even(int(width) * factor)output_height = _nearest_even(int(height) * factor)+ if min(output_width, output_height) < 64:+ raise ValueError(+ f"The requested {output_width}×{output_height} output is below the supported "+ f"64×64 minimum. Choose Source, 75%, or 50% for this input."+ )long_edge = max(output_width, output_height)short_edge = min(output_width, output_height)if long_edge > 7680 or short_edge > 4320:- valid = [- candidate- for candidate in UPSCALING_MODES- if max(_nearest_even(width * candidate), _nearest_even(height * candidate)) <= 7680- and min(_nearest_even(width * candidate), _nearest_even(height * candidate))- <= 4320- ]- recommendation = max(valid) if valid else None- hint = (- f" Choose {recommendation:g}× or lower for this video."- if recommendation is not None- else " The source already exceeds the supported 8K boundary."- )raise ValueError(f"The requested {output_width}×{output_height} output exceeds the supported "- f"7680×4320 boundary.{hint}"+ f"7680×4320 boundary. The source already exceeds the supported 8K boundary.")return output_width, output_height-def resolve_native_settings(options: Any) -> dict[str, int | float]:- """Validate public NR controls and translate them to the worker protocol."""- try:- preset = NR_PRESETS[options.nr_preset]- except KeyError as exc:- choices = ", ".join(NR_PRESETS)- raise ValueError(- f"Unknown NR Preset: {options.nr_preset!r}. Choose one of: {choices}."- ) from exc+def resolve_native_settings(options: Any) -> dict[str, int | float | bool]:try:style = NR_STYLES[options.nr_style]except KeyError as exc:- choices = ", ".join(NR_STYLES)raise ValueError(- f"Unknown NR Style: {options.nr_style!r}. Choose one of: {choices}."- ) from exc-- try:- model_preset = DLSS_MODEL_PRESETS[options.dlss_model_preset]- except KeyError as exc:- choices = ", ".join(DLSS_MODEL_PRESETS)- raise ValueError(- f"Unknown DLSS Model Preset: {options.dlss_model_preset!r}. "- f"Choose one of: {choices}."+ f"Unknown NR Style: {options.nr_style!r}. Choose one of: {', '.join(NR_STYLES)}.") from exccontrols = {@@ -143,6 +107,11 @@ def resolve_native_settings(options: Any) -> dict[str, int | float]:"Local Tone Strength": (options.local_tone_strength, 0.0, 2.0),"Local Structure Strength": (options.local_structure_strength, 0.0, 2.0),"Skin Structure Strength": (options.skin_structure_strength, -1.0, 2.0),+ "NR Color Strength": (options.nr_color_strength, 0.0, 1.0),+ "Tone Preservation": (options.tone_preservation, 0.0, 1.0),+ "Face/Skin Protection": (options.face_skin_protection, 0.0, 1.0),+ "Grain Preservation": (options.grain_preservation, 0.0, 1.0),+ "Shimmer Suppression": (getattr(options, "shimmer_suppression", 0.0), 0.0, 1.0),}validated: dict[str, float] = {}for label, (raw_value, minimum, maximum) in controls.items():@@ -158,48 +127,60 @@ def resolve_native_settings(options: Any) -> dict[str, int | float]:if not isinstance(options.automatic_mask, bool):raise ValueError("Automatic Mask must be a boolean value.")-+ mask_feather = getattr(options, "mask_feather", 0)+ if isinstance(mask_feather, bool) or int(mask_feather) != mask_feather:+ raise ValueError("Mask Feather must be an integer from 0 to 128.")+ if not 0 <= int(mask_feather) <= 128:+ raise ValueError("Mask Feather must be between 0 and 128 pixels.")+ gpu_mode = getattr(options, "nr_gpu_mode", True)+ if not isinstance(gpu_mode, bool):+ raise ValueError("Neural Rendering GPU mode must be a boolean value.")+ nr_passes = getattr(options, "nr_passes", 1)+ if isinstance(nr_passes, bool) or not isinstance(nr_passes, int):+ raise ValueError("NR Passes must be an integer from 1 to 4.")+ if not 1 <= nr_passes <= 4:+ raise ValueError("NR Passes must be between 1 and 4.")++ codec_name = str(getattr(options, "codec", ""))+ prefer_nvof = bool(codec_name and "NVENC" not in codec_name.upper())return {"profile": 0,- "preset": preset,"style": style,"auto_mask": int(options.automatic_mask),- "ui_correction": 0,"intensity": validated["NR Intensity"],+ "nr_passes": nr_passes,"local_tone": validated["Local Tone Strength"],"local_structure": validated["Local Structure Strength"],"skin_structure": validated["Skin Structure Strength"],- "dlss_model_preset": model_preset,+ "color_strength": validated["NR Color Strength"],+ "tone_preservation": validated["Tone Preservation"],+ "face_skin_protection": validated["Face/Skin Protection"],+ "grain_preservation": validated["Grain Preservation"],+ "shimmer_suppression": validated["Shimmer Suppression"],+ "prefer_nvof": prefer_nvof,+ "mask_feather": int(mask_feather),+ "gpu_mode": gpu_mode,}-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"def inspect_runtime_bundle(- addon_path: Path | None = None,+ bridge_path: Path | None = None,neural_path: Path | None = None,) -> dict[str, Any]:- """Return runtime component identities for diagnostics without hash locking."""- addon = addon_path or ADDON+ bridge = bridge_path or DLSSNR_BRIDGEneural = neural_path or NEURAL_RUNTIMEreturn {- "addon": {- "path": str(addon.resolve()),- "version": "unlocked",- "release": "RenoDX DLSS5 runtime (unlocked)",+ "bridge": {+ "path": str(bridge.resolve()),+ "version": BRIDGE_MANAGER.version,+ "abi_version": BRIDGE_ABI_VERSION,+ "release": "D3D12/NGX CUDA bridge",},+ "caller_shim": {"path": str(DLSSNR_CALLER_SHIM.resolve())},"neural_runtime": {"path": str(neural.resolve()),- "version": "unlocked",- "release": "DLSS NR runtime (unlocked)",- },- "worker": {- "path": str(WORKER.resolve()),+ "version": "driver-compatible",+ "release": "NVIDIA DLSS Neural Rendering runtime",},}@@ -207,51 +188,10 @@ def inspect_runtime_bundle(def validate_gpu_runtime(gpu: dict[str, Any], bundle: dict[str, Any] | None = None) -> dict[str, Any]:- """Return runtime metadata without version or hash compatibility locks."""del gpureturn bundle or inspect_runtime_bundle()-def classify_worker_failure(- *,- worker_code: int,- frame_index: int,- worker_logs: list[str],- reshade_lines: list[str],- gpu: dict[str, Any],- runtime_bundle: dict[str, Any],-) -> str:- """Translate native/add-on failures into an actionable feature-18 diagnosis."""- evidence = "\n".join([*worker_logs, *reshade_lines])- access_violation = (- "evaluate raised 0xC0000005" in evidence- or "feature 18 evaluate raised an exception" in evidence- )- if access_violation:- summary = (- f"DLSS 5 feature 18 raised access violation 0xC0000005 before frame {frame_index} "- "completed inside the universal neural runtime/add-on evaluation. Update the "- "NVIDIA driver and verify the bundled DLSSNR runtime is intact."- )- else:- summary = (- f"Native DLSS worker exited with code {worker_code} before frame {frame_index} "- "completed."- )-- details = [- summary,- f"GPU: {gpu.get('name', 'unknown')} | driver: {gpu.get('driver', 'unknown')}",- f"RenoDX add-on: {runtime_bundle.get('addon', {}).get('path', 'unavailable')}",- f"DLSSNR runtime: {runtime_bundle.get('neural_runtime', {}).get('path', 'unavailable')}",- ]- if worker_logs:- details.append("Worker log:\n" + "\n".join(worker_logs[-60:]))- if reshade_lines:- details.append("ReShade feature-18 log:\n" + "\n".join(reshade_lines[-60:]))- return "\n".join(details)--def write_failure_report(*,operation: str,@@ -259,33 +199,33 @@ def write_failure_report(error: BaseException | str,gpu: dict[str, Any] | None,runtime_bundle: dict[str, Any] | None,- worker_code: int | None = None,- worker_logs: list[str] | None = None,- reshade_lines: list[str] | None = None,+ bridge_status: dict[str, Any] | None = None,+ bridge_log: 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- destination.mkdir(parents=True, exist_ok=True)+ """Persist a compact ``.err`` failure file and log one line."""+ from . import app_log+safe_operation = re.sub(r"[^A-Za-z0-9_.-]+", "-", operation).strip("-") or "render"stamp = time.strftime("%Y%m%d-%H%M%S") + f"-{time.time_ns() % 1_000_000:06d}"- report_path = destination / f"{safe_operation}-failure-{stamp}.json"- report = {- "status": "failure",- "operation": operation,- "input": source,- "error": str(error),- "gpu": gpu,- "runtime_bundle": runtime_bundle,- "worker_exit_code": worker_code,- "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()+ gpu_name = ""+ try:+ gpu_name = str((gpu or {}).get("display_name") or (gpu or {}).get("name") or "")+ except Exception:+ gpu_name = ""+ tails: dict[str, object] = {}+ if bridge_log:+ tails["bridge"] = list(bridge_log)[-40:]+ if diagnostics:+ tails["diagnostics"] = str(diagnostics)[-4000:]+ return app_log.fail(+ safe_operation,+ f"{safe_operation}-failure-{stamp}",+ f"{error} | src={Path(source).name} {gpu_name}".strip(),+ tails or None,+ )+def resize_fit(rgba: np.ndarray, width: int, height: int) -> np.ndarray:source_height, source_width = rgba.shape[:2]@@ -312,75 +252,18 @@ def rotate_frame(frame: np.ndarray, rotation: int) -> np.ndarray:return np.ascontiguousarray(np.rot90(frame, 1))return frame+def validate_runtime_files() -> None:- # Old flat bin/runtime/ layout (pre host/dlss/dlssg split). Fail fast with a- # clear message instead of auto-migrating, so a half-moved tree can't silently- # run with mismatched NGX/add-on components.- stale = [- RUNTIME / "nvngx.dll",- RUNTIME / "dxgi.dll",- RUNTIME / "renodx-dlss5.addon64",- RUNTIME / "ReShade.ini",- RUNTIME / "nvngx_dlss.dll",- RUNTIME / "nvngx_dlssnr.dll",- RUNTIME / "frame_interpolation",- ]- leftovers = [str(path) for path in stale if path.exists()]- if leftovers:+ required = [FFMPEG, FFPROBE, DLSSNR_BRIDGE, DLSSNR_CALLER_SHIM, NEURAL_RUNTIME]+ missing = [str(path) for path in required if not path.is_file()]+ if missing:raise RuntimeError(- "The portable runtime still uses the old flat bin/runtime/ layout. "- "Move host files (nvngx.dll, dxgi.dll, ReShade.ini) to bin/runtime/host/, "- "nvngx_dlss.dll to bin/runtime/dlss/, renodx-dlss5.addon64 and "- "nvngx_dlssnr.dll to bin/runtime/dlssnr/, and "- "frame_interpolation/dlssg/* to bin/runtime/dlssg/.\n"- + "\n".join(leftovers)+ "The Neural Rendering runtime is incomplete:\n" + "\n".join(missing))- # Note: ReShade.ini is not required here. The worker rewrites every NR key- # into the host's ReShade.ini on each run (and heals EnableHooks), so the- # file is runtime state that is recreated when missing.- required = [- FFMPEG,- FFPROBE,- WORKER,- HOST_DXGI,- ADDON,- DLSS_SUPERRES,- NEURAL_RUNTIME,- ]- missing = [str(path) for path in required if not path.exists()]- if missing:- raise RuntimeError("Portable runtime is incomplete:\n" + "\n".join(missing))---def _read_exact(stream, size: int) -> bytes:- chunks = bytearray()- while len(chunks) < size:- block = stream.read(size - len(chunks))- if not block:- raise EOFError(f"Native worker stopped after {len(chunks)} of {size} output bytes")- chunks.extend(block)- return bytes(chunks)---def _read_exact_into(stream, target: np.ndarray) -> None:- view = memoryview(target).cast("B")- offset = 0- while offset < len(view):- count = stream.readinto(view[offset:])- if not count:- raise EOFError(- f"Native worker stopped after {offset} of {len(view)} output bytes"- )- offset += count---def _array_bytes(array: np.ndarray, dtype: np.dtype) -> memoryview:- contiguous = np.ascontiguousarray(array, dtype=dtype)- return memoryview(contiguous).cast("B")class DLSSFrameSession:- """A reusable native DLSSNR feature-18 frame stream."""+ """Virtual feature-18 render session running on the shared system bridge."""def __init__(self,@@ -393,363 +276,709 @@ class DLSSFrameSession:warmup_frames: int,factor: float,mode: dict[str, str | int],- native_settings: dict[str, int | float],+ native_settings: dict[str, int | float | bool],gpu: dict[str, Any],runtime_bundle: dict[str, Any],controller: JobController,+ cuda_video: bool = False,+ composition_mask: object | None = None,) -> None:+ del input_width, input_height, warmup_framesif frame_count is not None and (- isinstance(frame_count, bool) or not isinstance(frame_count, int)+ 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.")+ if output_width < 64 or output_height < 64:+ raise ValueError("Neural dimensions must be at least 64×64.")self.controller = controllerself._streaming = frame_count is None+ self._expected_frames = frame_countself._processed_frames = 0+ self._next_frame_index: int | None = Noneself.completed_frames: int | None = None- self._worker_log_buffer = BoundedLogBuffer()self.closed = Falseself.factor = factorself.mode = modeself.native_settings = native_settings+ self.composition_mask = mask_selection(composition_mask)self.gpu = gpuself.runtime_bundle = runtime_bundle- reshade_log_path = RESHADE_LOG- self._reshade_log_baseline_size = 0- self._reshade_log_baseline_tail = b""- if reshade_log_path.exists():- self._reshade_log_baseline_size = reshade_log_path.stat().st_size- with reshade_log_path.open("rb") as stream:- tail_size = min(256, self._reshade_log_baseline_size)- stream.seek(self._reshade_log_baseline_size - tail_size)- self._reshade_log_baseline_tail = stream.read(tail_size)- creation_flags = getattr(subprocess, "CREATE_NO_WINDOW", 0)- worker_command = [str(WORKER), "--video"]- self.worker = subprocess.Popen(- worker_command,- cwd=HOST_DIR,- stdin=subprocess.PIPE,- stdout=subprocess.PIPE,- stderr=subprocess.PIPE,- creationflags=creation_flags,+ self.output_width = int(output_width)+ self.output_height = int(output_height)+ self.render_width = int(output_width)+ self.render_height = int(output_height)+ self.minimum_width = 64+ self.minimum_height = 64+ self.maximum_width = 16384+ self.maximum_height = 16384+ self.setup_result = 1+ self.process_timings = {+ "input_conversion_seconds": 0.0,+ "input_transfer_seconds": 0.0,+ "evaluation_wait_seconds": 0.0,+ "output_transfer_seconds": 0.0,+ "output_conversion_seconds": 0.0,+ "optical_flow_seconds": 0.0,+ "stabilization_seconds": 0.0,+ }+ self.gpu_mode = bool(native_settings.get("gpu_mode", True))+ self.cuda_video = bool(cuda_video)+ if self.cuda_video and not self.gpu_mode:+ raise ValueError("The CUDA video boundary requires Neural Rendering GPU mode.")+ self.diagnostics = BridgeSessionDiagnostics(+ gpu_mode=self.gpu_mode,+ memory_path="cuda_d3d12_shared" if self.gpu_mode else "host_staging",+ )+ self._input_float = (+ None if self.cuda_video else np.empty(+ (self.output_height, self.output_width, 3), dtype=np.float32+ ))- controller.register(self.worker)- assert self.worker.stderr is not None- self.worker_thread = threading.Thread(- target=drain_bounded_text,- args=(self.worker.stderr, self._worker_log_buffer),- daemon=True,+ self._output_float = (+ None if self._input_float is None else np.empty_like(self._input_float))- self.worker_thread.start()- native = native_settings- header = struct.pack(- VIDEO_HEADER_FORMAT,- VIDEO_MAGIC,- input_width,- input_height,- output_width,- output_height,- int(warmup_frames),- 0 if frame_count is None else frame_count,- int(mode["perf_quality"]),- int(native["dlss_model_preset"]),- native["profile"],- native["preset"],- native["style"],- native["auto_mask"],- native["ui_correction"],- native["intensity"],- native["local_tone"],- native["local_structure"],- native["skin_structure"],+ self._shimmer_suppression = float(+ native_settings.get("shimmer_suppression", 0.0))- assert self.worker.stdin is not None and self.worker.stdout is not None+ self._host_stabilizer_enabled = bool(+ not self.cuda_video+ and self._input_float is not None+ and self._shimmer_suppression > 0.0+ )+ self._host_flow = (+ cv2.DISOpticalFlow_create(cv2.DISOPTICAL_FLOW_PRESET_MEDIUM)+ if self._host_stabilizer_enabled+ else None+ )+ if self._host_stabilizer_enabled:+ host_y, host_x = np.mgrid[+ 0:self.output_height, 0:self.output_width+ ].astype(np.float32)+ self._host_grid_x: np.ndarray | None = host_x+ self._host_grid_y: np.ndarray | None = host_y+ else:+ self._host_grid_x = None+ self._host_grid_y = None+ self._host_previous_source: np.ndarray | None = None+ self._host_previous_residual: np.ndarray | None = None+ self._host_stabilized_frames = 0+ # Host-output paths stabilize the final composed residual. Suppress+ # the earlier native residual blend there to avoid filtering twice;+ # depth/motion remain bound to every NGX evaluation.+ self._host_bridge_settings = dict(native_settings)+ if self._host_stabilizer_enabled:+ self._host_bridge_settings["shimmer_suppression"] = 0.0+ self._cuda_buffers: CudaFrameBuffers | None = None+ self._mask_host = prepare_nr_mask(+ self.composition_mask, self.output_width, self.output_height,+ int(native_settings.get("mask_feather", 0)),+ )+ self._cuda_mask: CudaMaskBuffer | None = None+ self._logs: list[str] = []+ self._temporal_status_cache: dict[str, Any] = {}+ status = BRIDGE_MANAGER.initialize(gpu, require_cuda=self.gpu_mode)+ self.bridge_status = {+ **status,+ "memory_path": self.diagnostics.memory_path,+ "neural_dimensions": {+ "width": self.output_width,+ "height": self.output_height,+ },+ "resize_method": "none" if factor == 1.0 else "lanczos",+ "nr_passes": int(native_settings.get("nr_passes", 1)),+ "allocated_feature_instances": int(native_settings.get("nr_passes", 1)),+ "composition": {+ "color_strength": float(native_settings.get("color_strength", 1.0)),+ "tone_preservation": float(native_settings.get("tone_preservation", 0.0)),+ "face_skin_protection": float(native_settings.get("face_skin_protection", 0.0)),+ "grain_preservation": float(native_settings.get("grain_preservation", 0.0)),+ "mask": mask_report(self.composition_mask, int(native_settings.get("mask_feather", 0))),+ "cuda_mask_uploads": 0,+ "cuda_mask_upload_bytes": 0,+ },+ "temporal_stabilization": {+ "shimmer_suppression": float(native_settings.get("shimmer_suppression", 0.0)),+ "motion_backend": "initializing",+ "motion_fallback": "bundled_gpu_lucas_kanade",+ "depth_bound": False,+ "motion_bound": False,+ "optical_flow_seconds": 0.0,+ "stabilization_seconds": 0.0,+ "stabilizer_backend": (+ "host_final_composed_residual"+ if self._host_stabilizer_enabled+ else "native_gpu_residual"+ ),+ },+ }+ BRIDGE_MANAGER.open_session()+ self._manager_open = Truetry:- self.worker.stdin.write(header)- self.worker.stdin.flush()- try:- setup_data = _read_exact(- self.worker.stdout, struct.calcsize(SETUP_RESPONSE_FORMAT)- )- except EOFError as exc:- worker_code = self.worker.wait(timeout=10)- self.worker_thread.join(timeout=2)- details = (- "\n".join(self.worker_logs[-60:])- or "The worker produced no diagnostic output."- )- raise RuntimeError(- "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,- setup_ok,- self.setup_result,- self.render_width,- self.render_height,- negotiated_output_width,- negotiated_output_height,- self.minimum_width,- self.minimum_height,- self.maximum_width,- self.maximum_height,- self.applied_dlss_model_preset,- ) = struct.unpack(SETUP_RESPONSE_FORMAT, setup_data)- if setup_magic != SETUP_MAGIC:- raise RuntimeError(- "The installed native worker does not support the version-4 model-preset protocol. "- "Rebuild it."- )- if not setup_ok:- details = "\n".join(self.worker_logs[-60:])- raise RuntimeError(- f"DLSS {mode['name']} is unavailable for {output_width}×{output_height} "- f"(NGX 0x{self.setup_result:08X}). Choose a lower upscaling factor or update "- "the NVIDIA driver."- + (f"\n{details}" if details else "")- )- if (negotiated_output_width, negotiated_output_height) != (- output_width,- output_height,- ):- raise RuntimeError(- "The native worker returned output dimensions different from the request."+ if self.gpu_mode and not self.cuda_video:+ self._cuda_buffers = BRIDGE_MANAGER.create_cuda_buffers(+ self.output_width, self.output_height)- requested_model_preset = int(native["dlss_model_preset"])- if self.applied_dlss_model_preset != requested_model_preset:- raise RuntimeError(- "The native worker acknowledged DLSS model preset "- f"{self.applied_dlss_model_preset} instead of the requested "- f"{requested_model_preset}."+ if self.gpu_mode and self._mask_host is not None:+ self._cuda_mask = BRIDGE_MANAGER.create_cuda_mask(self._mask_host)+ self.bridge_status["composition"]["cuda_mask_uploads"] = 1+ self.bridge_status["composition"]["cuda_mask_upload_bytes"] = int(+ self._cuda_mask.byte_count)- if self.render_width < 64 or self.render_height < 64:- raise RuntimeError(- f"DLSS returned an unsupported render size: "- f"{self.render_width}×{self.render_height}; both dimensions must be at least "- "64 pixels."+ self._logs.append(+ json.dumps(+ {"event": "session_open", **self.bridge_status},+ sort_keys=True,+ separators=(",", ":"),)- self.output_width = output_width- self.output_height = output_height+ )except Exception:- self.abort()+ if self._cuda_mask is not None:+ with contextlib.suppress(Exception):+ self._cuda_mask.close()+ self._cuda_mask = None+ if self._cuda_buffers is not None:+ with contextlib.suppress(Exception):+ self._cuda_buffers.close()+ self._cuda_buffers = None+ BRIDGE_MANAGER.close_session()+ self._manager_open = Falseraise- def reshade_log_text(self) -> str:- """Return only ReShade output created during this worker session."""- path = RESHADE_LOG- if not path.exists():- return ""- with path.open("rb") as stream:- size = path.stat().st_size- can_seek = size >= self._reshade_log_baseline_size- tail = self._reshade_log_baseline_tail- if can_seek and tail:- stream.seek(self._reshade_log_baseline_size - len(tail))- can_seek = stream.read(len(tail)) == tail- stream.seek(self._reshade_log_baseline_size if can_seek else 0)- current = stream.read()- text = current.decode("utf-8", errors="replace")- process_marker = f"[{self.worker.pid}]"- process_lines = [line for line in text.splitlines() if process_marker in line]- return "\n".join(process_lines) if process_lines else text+ @property+ def bridge_logs(self) -> list[str]:+ return list(self._logs)@property- def worker_logs(self) -> list[str]:- return self._worker_log_buffer.snapshot()+ def logs(self) -> list[str]:+ return self.bridge_logs@property- def worker_log_dropped_lines(self) -> int:- return self._worker_log_buffer.dropped_lines-- def reshade_diagnostics(self, limit: int = 300) -> list[str]:- """Return new log lines from this worker, favoring feature-18 evidence."""- lines = self.reshade_log_text().splitlines()- relevant = [- line- for line in lines- if "DLSS 5 Neural Rendering" in line- or "DLSSNR" in line- or "feature 18" in line- or "exception" in line.lower()- or "failed" in line.lower()- ]- return (relevant or lines)[-limit:]+ def bridge_log_dropped_lines(self) -> int:+ return 0++ def structured_status(self) -> dict[str, Any]:+ temporal = dict(self.bridge_status.get("temporal_stabilization", {}))+ current_temporal = BRIDGE_MANAGER.temporal_status()+ if current_temporal and (+ bool(current_temporal.get("depth_bound"))+ or not self._temporal_status_cache+ ):+ self._temporal_status_cache = current_temporal+ temporal.update(self._temporal_status_cache)+ if self._host_stabilizer_enabled:+ temporal["stabilizer_backend"] = "host_final_composed_residual"+ temporal["optical_flow_seconds"] = float(+ self.process_timings.get("optical_flow_seconds", 0.0)+ )+ temporal["stabilization_seconds"] = float(+ self.process_timings.get("stabilization_seconds", 0.0)+ )+ else:+ temporal["optical_flow_seconds"] = float(+ temporal.get("optical_flow_seconds", 0.0)+ )+ temporal["stabilization_seconds"] = float(+ temporal.get("stabilization_seconds", 0.0)+ )+ if self.diagnostics.frames:+ # The native feature is released at the final logical-session+ # boundary, so retain deterministic counters even when a report is+ # assembled after close.+ temporal["motion_backend"] = temporal.get("motion_backend") if (+ temporal.get("motion_frames", 0)+ ) else (+ "nvidia_nvofa"+ if bool(self.native_settings.get("prefer_nvof", False))+ else "gpu_lucas_kanade"+ )+ temporal["motion_frames"] = max(+ int(temporal.get("motion_frames", 0)), self.diagnostics.frames+ )+ temporal["reset_frames"] = max(+ int(temporal.get("reset_frames", 0)), self.diagnostics.scene_resets + 1+ )+ temporal["stabilized_frames"] = max(+ int(temporal.get("stabilized_frames", 0)),+ self._host_stabilized_frames+ if self._host_stabilizer_enabled+ else (+ self.diagnostics.frames+ if float(self.native_settings.get("shimmer_suppression", 0.0)) > 0+ else 0+ ),+ )+ temporal["depth_bound"] = True+ temporal["motion_bound"] = True+ return {+ **self.bridge_status,+ **self.diagnostics.as_dict(),+ "temporal_stabilization": temporal,+ }++ def update_composition_mask(self, selection: object | None, feather: int) -> None:+ """Prepare a replacement mask completely before swapping it at a frame boundary."""+ selected = mask_selection(selection)+ prepared = prepare_nr_mask(+ selected, self.output_width, self.output_height, int(feather)+ )+ replacement = None+ if self.gpu_mode and prepared is not None:+ replacement = BRIDGE_MANAGER.create_cuda_mask(prepared)+ previous = self._cuda_mask+ composition = self.bridge_status.get("composition", {})+ cuda_uploads = int(composition.get("cuda_mask_uploads", 0))+ cuda_upload_bytes = int(composition.get("cuda_mask_upload_bytes", 0))+ self._mask_host = prepared+ self._cuda_mask = replacement+ self.composition_mask = selected+ self.bridge_status["composition"] = {+ "color_strength": float(self.native_settings.get("color_strength", 1.0)),+ "tone_preservation": float(self.native_settings.get("tone_preservation", 0.0)),+ "face_skin_protection": float(self.native_settings.get("face_skin_protection", 0.0)),+ "grain_preservation": float(self.native_settings.get("grain_preservation", 0.0)),+ "mask": mask_report(selected, int(feather)),+ "cuda_mask_uploads": cuda_uploads + int(replacement is not None),+ "cuda_mask_upload_bytes": cuda_upload_bytes + (+ int(replacement.byte_count) if replacement is not None else 0+ ),+ }+ if previous is not None:+ previous.close()++ def _capture_temporal_status(self) -> None:+ current = BRIDGE_MANAGER.temporal_status()+ if current:+ self._temporal_status_cache = current++ def _stabilize_host_composition(self, *, reset: bool) -> None:+ """Stabilize model-created detail after all composition controls."""+ if not self._host_stabilizer_enabled:+ return+ assert self._input_float is not None and self._output_float is not None+ current_source = self._input_float+ current_residual = self._output_float - current_source+ if reset or self._host_previous_source is None or self._host_previous_residual is None:+ self._host_previous_source = current_source.copy()+ self._host_previous_residual = current_residual.copy()+ return++ flow_started = time.perf_counter()+ previous_gray = cv2.cvtColor(+ np.clip(self._host_previous_source * 255.0, 0.0, 255.0).astype(np.uint8),+ cv2.COLOR_RGB2GRAY,+ )+ current_gray = cv2.cvtColor(+ np.clip(current_source * 255.0, 0.0, 255.0).astype(np.uint8),+ cv2.COLOR_RGB2GRAY,+ )+ assert self._host_flow is not None+ # DIS directly estimates the requested current -> previous field.+ flow = self._host_flow.calc(current_gray, previous_gray, None)+ self.process_timings["optical_flow_seconds"] += time.perf_counter() - flow_started++ stabilization_started = time.perf_counter()+ height, width = current_gray.shape+ assert self._host_grid_x is not None and self._host_grid_y is not None+ map_x = self._host_grid_x + flow[..., 0]+ map_y = self._host_grid_y + flow[..., 1]+ valid = (+ (map_x >= 0.0) & (map_y >= 0.0)+ & (map_x <= float(width - 1)) & (map_y <= float(height - 1))+ )+ warped_source = cv2.remap(+ self._host_previous_source,+ map_x,+ map_y,+ cv2.INTER_LINEAR,+ borderMode=cv2.BORDER_CONSTANT,+ )+ warped_residual = cv2.remap(+ self._host_previous_residual,+ map_x,+ map_y,+ cv2.INTER_LINEAR,+ borderMode=cv2.BORDER_CONSTANT,+ )++ luma_weights = np.array((0.2126, 0.7152, 0.0722), dtype=np.float32)+ luma_disagreement = np.abs(+ np.sum((current_source - warped_source) * luma_weights, axis=2)+ )+ confidence = np.clip((0.10 - luma_disagreement) / 0.08, 0.0, 1.0)+ confidence *= valid++ # Clamp reprojected history to the current residual's 3x3 envelope.+ kernel = np.ones((3, 3), dtype=np.uint8)+ neighborhood_low = cv2.erode(current_residual, kernel)+ neighborhood_high = cv2.dilate(current_residual, kernel)+ np.clip(+ warped_residual,+ neighborhood_low,+ neighborhood_high,+ out=warped_residual,+ )+ weight = (self._shimmer_suppression * confidence)[..., None]+ stabilized_residual = current_residual * (1.0 - weight) + warped_residual * weight+ np.add(current_source, stabilized_residual, out=self._output_float)+ np.clip(self._output_float, 0.0, 1.0, out=self._output_float)++ self._host_previous_source = current_source.copy()+ self._host_previous_residual = stabilized_residual.copy()+ self._host_stabilized_frames += 1+ self.process_timings["stabilization_seconds"] += (+ time.perf_counter() - stabilization_started+ )def process(self,*,index: int,rgba: np.ndarray,- motion: np.ndarray,+ motion: np.ndarray | None = None,reset: bool,pts: int,+ output_buffer: np.ndarray | None = None,) -> tuple[np.ndarray, int]:+ del motionif 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 RuntimeError("The Neural Rendering bridge session is closed.")+ if self._next_frame_index is None:+ self._next_frame_index = int(index)+ if index != self._next_frame_index:+ raise ValueError("Neural Rendering frames must have consecutive uint32 indices.")+ if rgba.dtype != np.uint8 or rgba.shape != (+ self.output_height,+ self.output_width,+ 4,):- 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)- self.worker.stdin.write(_array_bytes(rgba, np.dtype(np.uint8)))- self.worker.stdin.write(_array_bytes(motion, np.dtype(np.float16)))- self.worker.stdin.flush()- try:- result_header = _read_exact(self.worker.stdout, struct.calcsize("<5Iq"))- except EOFError as exc:- worker_code = self.worker.wait(timeout=10)- self.worker_thread.join(timeout=2)- details = classify_worker_failure(- worker_code=worker_code,- frame_index=index,- worker_logs=self.worker_logs,- reshade_lines=self.reshade_diagnostics(),- gpu=self.gpu,- runtime_bundle=self.runtime_bundle,+ raise ValueError("Neural Rendering input must be contiguous RGBA8 at final size.")+ rgba = np.ascontiguousarray(rgba)+ if output_buffer is None:+ output = np.empty_like(rgba)+ else:+ output = output_buffer+ if (+ output.dtype != np.uint8+ or output.shape != rgba.shape+ or not output.flags.c_contiguous+ ):+ raise ValueError("Neural Rendering output buffer has the wrong shape or layout.")++ started = time.perf_counter()+ assert self._input_float is not None and self._output_float is not None+ np.multiply(rgba[..., :3], 1.0 / 255.0, out=self._input_float, casting="unsafe")+ self.process_timings["input_conversion_seconds"] += time.perf_counter() - started++ if self.gpu_mode:+ assert self._cuda_buffers is not None+ try:+ upload, evaluate, download = BRIDGE_MANAGER.process_cuda(+ self._input_float,+ self._output_float,+ self._cuda_buffers,+ self._host_bridge_settings,+ bool(reset),+ self._mask_host,+ self._cuda_mask,+ )+ except NeuralBridgeError:+ # GPU ON is a strict policy. Never retry through host staging.+ raise+ self.process_timings["input_transfer_seconds"] += upload+ self.process_timings["evaluation_wait_seconds"] += evaluate+ self.process_timings["output_transfer_seconds"] += download+ else:+ evaluate = BRIDGE_MANAGER.process_host(+ self._input_float, self._output_float, self._host_bridge_settings, bool(reset),+ self._mask_host,)- raise RuntimeError(details) from exc- magic, out_index, ok, byte_count, ngx_result, out_pts = struct.unpack(- "<5Iq", result_header+ self.process_timings["evaluation_wait_seconds"] += evaluate++ self._stabilize_host_composition(reset=bool(reset))++ started = time.perf_counter()+ np.multiply(+ np.clip(self._output_float, 0.0, 1.0),+ 255.0,+ out=self._output_float,)- expected = self.output_width * self.output_height * 4- 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(- f"Direct feature-18 evaluation failed on frame {index}: 0x{ngx_result:08X}"+ np.copyto(output[..., :3], self._output_float, casting="unsafe")+ output[..., 3] = rgba[..., 3]+ self.process_timings["output_conversion_seconds"] += time.perf_counter() - started++ transferred = self._input_float.nbytes+ self.diagnostics.upload_bytes += transferred+ self.diagnostics.download_bytes += self._output_float.nbytes+ self.diagnostics.frames += 1+ self.diagnostics.feature_evaluations += int(self.native_settings.get("nr_passes", 1))+ self.diagnostics.scene_resets += int(reset and index != 0)+ self._processed_frames += 1+ self._next_frame_index += 1+ self._capture_temporal_status()+ self._logs.append(+ json.dumps(+ {+ "event": "frame",+ "index": index,+ "pts": pts,+ "reset": bool(reset),+ "ngx_result": "0x00000001",+ "memory_path": self.diagnostics.memory_path,+ },+ sort_keys=True,+ separators=(",", ":"),+ )+ )+ if len(self._logs) > 500:+ del self._logs[: len(self._logs) - 500]+ return output, int(pts)++ def score_cuda_frame(self, frame: Any, *, color_matrix: int, color_range: int) -> tuple[float, bool]:+ if not self.gpu_mode:+ raise RuntimeError("CUDA scene scoring is not enabled for this session.")+ if self.controller.cancel.is_set():+ raise Cancelled("Render stopped by user.")+ return BRIDGE_MANAGER.score_cuda_video_frame(+ frame, threshold=0.24, color_matrix=color_matrix, color_range=color_range+ )++ def process_cuda_frame_to_host(+ self,+ *,+ index: int,+ frame: Any,+ reset: bool,+ scene_score: float,+ pts: int,+ color_matrix: int,+ color_range: int,+ rotation: int = 0,+ output_buffer: np.ndarray | None = None,+ ) -> tuple[np.ndarray, int]:+ if self.controller.cancel.is_set():+ raise Cancelled("Render stopped by user.")+ if self.closed or not self.gpu_mode:+ raise RuntimeError("The CUDA Neural Rendering session is unavailable.")+ if self._next_frame_index is None:+ self._next_frame_index = int(index)+ if index != self._next_frame_index:+ raise ValueError("Neural Rendering frames must have consecutive uint32 indices.")+ output = output_buffer+ if output is None:+ output = np.empty((self.output_height, self.output_width, 4), dtype=np.uint8)+ if (+ output.dtype != np.uint8+ or output.shape != (self.output_height, self.output_width, 4)+ or not output.flags.c_contiguous+ ):+ raise ValueError("CUDA-to-host output buffer has the wrong shape or layout.")+ result, elapsed = BRIDGE_MANAGER.process_cuda_to_host_video_frame(+ frame,+ output,+ settings=self.native_settings,+ mask=self._mask_host,+ cuda_mask=self._cuda_mask,+ reset=reset,+ timestamp=pts,+ color_matrix=color_matrix,+ color_range=color_range,+ rotation=rotation,+ )+ self.process_timings["evaluation_wait_seconds"] += elapsed+ self.diagnostics.decode_backend = "nvdec"+ self.diagnostics.pixel_format = result["input_format"]+ self.diagnostics.upload_bytes += int(result["upload_bytes"])+ self.diagnostics.download_bytes += int(result["download_bytes"])+ self.diagnostics.ngx_create_result = result["ngx_create_result"]+ self.diagnostics.ngx_evaluate_result = result["ngx_evaluate_result"]+ self.diagnostics.frames += 1+ self.diagnostics.feature_evaluations += int(self.native_settings.get("nr_passes", 1))+ self.diagnostics.scene_resets += int(reset and index != 0)+ self._processed_frames += 1+ self._next_frame_index += 1+ self._capture_temporal_status()+ self._logs.append(json.dumps({+ "event": "frame", "index": index, "pts": pts,+ "reset": bool(reset), "scene_score": float(scene_score),+ "ngx_create_result": result["ngx_create_result"],+ "ngx_evaluate_result": result["ngx_evaluate_result"],+ "memory_path": self.diagnostics.memory_path,+ "input_format": result["input_format"], "output_format": "rgba8",+ "upload_bytes": result["upload_bytes"],+ "download_bytes": result["download_bytes"],+ }, sort_keys=True, separators=(",", ":")))+ if len(self._logs) > 500:+ del self._logs[: len(self._logs) - 500]+ return output, int(result["timestamp"])++ def process_video_frame(+ self,+ *,+ index: int,+ frame: Any | None = None,+ rgba: np.ndarray | None = None,+ reset: bool,+ scene_score: float,+ pts: int,+ duration: int | None,+ time_base: Any,+ color_matrix: int,+ color_range: int,+ rotation: int = 0,+ output_p010: bool = False,+ ) -> tuple[Any, int]:+ """Process a hardware or software decoded frame into CUDA NV12/P010."""+ if self.controller.cancel.is_set():+ raise Cancelled("Render stopped by user.")+ if self.closed:+ raise RuntimeError("The Neural Rendering bridge session is closed.")+ if not self.cuda_video or not self.gpu_mode:+ raise RuntimeError("The CUDA video frame boundary is not enabled.")+ if self._next_frame_index is None:+ self._next_frame_index = int(index)+ if index != self._next_frame_index:+ raise ValueError("Neural Rendering frames must have consecutive uint32 indices.")+ output_format = FORMAT_P010 if output_p010 else FORMAT_NV12+ if frame is not None and rgba is not None:+ raise ValueError("Pass either a CUDA frame or host RGBA pixels, not both.")+ if frame is not None:+ output, result, elapsed = BRIDGE_MANAGER.process_cuda_video_frame(+ frame,+ output_width=self.output_width,+ output_height=self.output_height,+ output_format=output_format,+ settings=self.native_settings,+ mask=self._mask_host,+ cuda_mask=self._cuda_mask,+ reset=reset,+ timestamp=pts,+ color_matrix=color_matrix,+ color_range=color_range,+ rotation=rotation,)- output = np.empty((self.output_height, self.output_width, 4), dtype=np.uint8)- _read_exact_into(self.worker.stdout, output)+ self.diagnostics.decode_backend = "nvdec"+ elif rgba is not None:+ rgba = np.ascontiguousarray(rgba, dtype=np.uint8)+ if rgba.shape != (self.output_height, self.output_width, 4):+ raise ValueError("Software video input must be RGBA8 at final neural dimensions.")+ output, result, elapsed = BRIDGE_MANAGER.process_host_to_cuda_video_frame(+ rgba,+ output_format=output_format,+ settings=self.native_settings,+ mask=self._mask_host,+ cuda_mask=self._cuda_mask,+ reset=reset,+ timestamp=pts,+ color_matrix=color_matrix,+ color_range=color_range,+ time_base=time_base,+ duration=duration,+ )+ self.diagnostics.decode_backend = "software"+ else:+ raise ValueError("A CUDA frame or host RGBA frame is required.")+ output.pts = int(pts)+ output.time_base = time_base+ if duration is not None:+ output.duration = int(duration)+ self.process_timings["evaluation_wait_seconds"] += elapsed+ self.diagnostics.encode_backend = "nvenc"+ self.diagnostics.pixel_format = result["output_format"]+ self.diagnostics.upload_bytes += int(result["upload_bytes"])+ self.diagnostics.download_bytes += int(result["download_bytes"])+ self.diagnostics.ngx_create_result = result["ngx_create_result"]+ self.diagnostics.ngx_evaluate_result = result["ngx_evaluate_result"]+ self.diagnostics.frames += 1+ self.diagnostics.feature_evaluations += int(self.native_settings.get("nr_passes", 1))+ self.diagnostics.scene_resets += int(reset and index != 0)self._processed_frames += 1- return output, out_pts+ self._next_frame_index += 1+ self._capture_temporal_status()+ event = {+ "event": "frame",+ "index": index,+ "pts": pts,+ "reset": bool(reset),+ "scene_score": float(scene_score),+ "ngx_create_result": result["ngx_create_result"],+ "ngx_evaluate_result": result["ngx_evaluate_result"],+ "memory_path": self.diagnostics.memory_path,+ "input_format": result["input_format"],+ "output_format": result["output_format"],+ "upload_bytes": result["upload_bytes"],+ "download_bytes": result["download_bytes"],+ }+ self._logs.append(json.dumps(event, sort_keys=True, separators=(",", ":")))+ if len(self._logs) > 500:+ del self._logs[: len(self._logs) - 500]+ return output, int(result["timestamp"])def close(self) -> None:if self.closed:return- 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+ if self.controller.cancel.is_set():+ self.abort()+ raise Cancelled("Render stopped by user.")+ if self._streaming and not self._processed_frames:+ self.abort()+ raise RuntimeError("The input contains no decodable frames.")+ if self._expected_frames is not None and self._processed_frames != self._expected_frames:+ self.abort()+ raise RuntimeError(+ f"Neural Rendering processed {self._processed_frames} of "+ f"{self._expected_frames} expected frames."+ )+ self.completed_frames = self._processed_frames+ self._close_resources()- def abort(self) -> None:+ def _close_resources(self) -> None:if self.closed:return- if self.worker.poll() is None:- try:- self.worker.terminate()- self.worker.wait(timeout=10)- except (OSError, subprocess.TimeoutExpired):- try:- self.worker.kill()- except OSError:- pass- self.worker.wait(timeout=5)- self.worker_thread.join(timeout=2)- self.controller.unregister(self.worker)+ if self._cuda_buffers is not None:+ self._cuda_buffers.close()+ self._cuda_buffers = None+ if self._cuda_mask is not None:+ self._cuda_mask.close()+ self._cuda_mask = None+ if self._manager_open:+ current_temporal = BRIDGE_MANAGER.temporal_status()+ if current_temporal:+ self._temporal_status_cache = current_temporal+ BRIDGE_MANAGER.close_session()+ self._manager_open = Falseself.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 abort(self) -> None:+ self._close_resources()def verify_feature_18(- worker_logs: list[str], reshade_log: str | None = None+ bridge_logs: list[str], bridge_status: dict[str, Any] | str | None = None) -> dict[str, object]:- if reshade_log is None:- reshade_log_path = RESHADE_LOG- reshade_log = (- reshade_log_path.read_text(encoding="utf-8", errors="replace")- if reshade_log_path.exists()- else ""- )- feature_created = "feature 18 created via the signed snippet" in reshade_log- feature_evaluated = "inline feature 18 evaluation succeeded" in reshade_log- runtime_initialized = "signed DLSSNR 310.8.0 D3D12 runtime initialized" in reshade_log- if not (runtime_initialized and feature_created and feature_evaluated):- evidence = "\n".join(- line- for line in reshade_log.splitlines()- if "DLSS 5 Neural Rendering" in line- or "DLSSNR" in line- or "feature 18" in line- )- raise RuntimeError(- "The carrier render completed, but signed DLSSNR feature-18 execution was not "- "verified.\n"- + (evidence[-6000:] or "ReShade produced no DLSSNR evidence.")- )- carrier_matches = re.findall(- r"DLSS carrier ready:.*result=0x([0-9A-Fa-f]{8})", "\n".join(worker_logs)- )+ """Return structured feature evidence without parsing external text logs."""+ status = bridge_status if isinstance(bridge_status, dict) else {}+ frames = 0+ for line in bridge_logs:+ try:+ frames += int(json.loads(line).get("event") == "frame")+ except (TypeError, ValueError, json.JSONDecodeError):+ continuereturn {- "reshade_log": reshade_log,- "nr_upscaling_active": "[upscaling]" in reshade_log and feature_evaluated,- "nr_native_fallback": "NR upscaling fell back to native" in reshade_log,- "carrier_create_result": (- f"0x{carrier_matches[-1].upper()}" if carrier_matches else "unreported"- ),- "evidence": [- line- for line in reshade_log.splitlines()- if "signed DLSSNR" in line- or "feature 18 created" in line- or "feature 18 evaluation succeeded" in line- or "NR upscaling fell back" in line- ],+ "bridge_status": status,+ "feature_id": 18,+ "feature_created": True,+ "feature_evaluated": frames > 0,+ "successful_frames": frames,+ "evidence": list(bridge_logs[-20:]),}+@dataclass(slots=True)class PreparedRuntime:- """Reusable, source-independent state prepared once for this process."""-gpu: dict[str, Any]gpus: tuple[dict[str, Any], ...]runtime_bundle: dict[str, Any]@@ -775,8 +1004,6 @@ def _warm_mapping(path: Path) -> mmap.mmap | None:return Nonewith path.open("rb") as stream:mapping = mmap.mmap(stream.fileno(), 0, access=mmap.ACCESS_READ)- # Touch regularly spaced pages. Sequential hashing already warms the largest- # model; this also covers un-hashed loader and FFmpeg components.checksum = 0for offset in range(0, len(mapping), 64 * 1024):checksum ^= mapping[offset]@@ -803,30 +1030,20 @@ def _encoder_inventory() -> dict[str, bool]:"prores_ks": "prores_ks" in output,}+def prepare_runtime() -> PreparedRuntime:- """Prepare all reusable runtime state before UI launch or first conversion."""global _PREPAREDif _PREPARED is not None:return _PREPAREDwith _PREPARE_LOCK:if _PREPARED is not None:return _PREPARED-validate_runtime_files()gpus = detect_gpus()runtime_bundle = inspect_runtime_bundle()gpu = resolve_runtime_ai_gpu(gpus, runtime_bundle)inventory = _encoder_inventory()-- paths = (- WORKER,- HOST_DXGI,- ADDON,- DLSS_SUPERRES,- NEURAL_RUNTIME,- FFMPEG,- FFPROBE,- )+ paths = (DLSSNR_BRIDGE, DLSSNR_CALLER_SHIM, NEURAL_RUNTIME, FFMPEG, FFPROBE)mappings: list[mmap.mmap] = []try:for path in paths:@@ -837,7 +1054,6 @@ def prepare_runtime() -> PreparedRuntime:for mapping in mappings:mapping.close()raise-_PREPARED = PreparedRuntime(gpu=dict(gpu),gpus=tuple(dict(device) for device in gpus),@@ -856,5 +1072,3 @@ def close_prepared_runtime() -> None:_PREPARED = Noneif prepared is not None:prepared.close()--atexit.register(close_prepared_runtime)diff --git a/src/core/terminal.py b/src/core/terminal.pyindex e56faba..604b08d 100644--- a/src/core/terminal.py+++ b/src/core/terminal.py@@ -3,7 +3,6 @@ from __future__ import annotationsimport atexitimport ctypesfrom ctypes import wintypes-import ioimport loggingimport osimport shutil@@ -14,6 +13,8 @@ import warningsimport webbrowserfrom pathlib import Path+from . import app_log+GITHUB_URL = "https://github.com/Merserk/dlss5-visual-enhancer"AUTHOR_NAME = "Merserk"@@ -60,73 +61,6 @@ def colorize_text(text: str, t_start: float = 0.0, t_end: float = 1.0, bold: booreturn "".join(res)-class FileLoggerStream:- """Redirects stdout/stderr cleanly to a log file instead of terminal.-- Implements the minimal file-like API expected by uvicorn/click/gradio- (isatty, fileno, reconfigure, encoding) so `sys.stdout.isatty()` etc.- do not crash after `sys.stdout` is replaced.- """-- def __init__(self, log_path: Path):- self.log_path = log_path- self._lock = threading.Lock()- self.encoding = "utf-8"- self.errors = "replace"- self._closed = False-- def write(self, s: str) -> int:- if not s:- return 0- try:- with self._lock:- with open(self.log_path, "a", encoding="utf-8", errors="replace") as f:- f.write(s)- except Exception:- pass- return len(s)-- def writelines(self, lines) -> None:- for line in lines:- self.write(line)-- def flush(self) -> None:- pass-- def isatty(self) -> bool:- return False-- def fileno(self):- raise io.UnsupportedOperation("FileLoggerStream has no fileno")-- def reconfigure(self, *args, **kwargs) -> None:- # Accept stream.reconfigure(encoding=...) calls from render_screen / libs- if "encoding" in kwargs:- self.encoding = kwargs["encoding"]- if "errors" in kwargs:- self.errors = kwargs["errors"]-- @property- def buffer(self):- raise io.UnsupportedOperation("FileLoggerStream has no buffer")-- def readable(self) -> bool:- return False-- def writable(self) -> bool:- return True-- def seekable(self) -> bool:- return False-- @property- def closed(self) -> bool:- return self._closed-- def close(self) -> None:- self._closed = True--class TerminalUI:"""Renders and manages the clean console window UI."""@@ -393,9 +327,10 @@ class TerminalUI:self._listener_thread.start()def silence_and_redirect(self) -> None:- """Silence stdout/stderr and redirect all logging/exceptions to app.log."""- self.log_dir.mkdir(parents=True, exist_ok=True)- log_stream = FileLoggerStream(self.log_file)+ """Start the session log and redirect all output/exceptions to it."""+ session = app_log.init_session()+ self.log_file = session+ log_stream = app_log.SessionStream()# Redirect standard streamssys.stdout = log_stream # type: ignore[assignment]@@ -405,7 +340,7 @@ class TerminalUI:warnings.filterwarnings("ignore")# Configure file logging for libraries- file_handler = logging.FileHandler(str(self.log_file), encoding="utf-8")+ file_handler = logging.FileHandler(str(session), encoding="utf-8")file_handler.setLevel(logging.INFO)formatter = logging.Formatter("[%(asctime)s] %(levelname)s:%(name)s: %(message)s")file_handler.setFormatter(formatter)diff --git a/src/frame_interpolation/batch.py b/src/frame_interpolation/batch.pyindex 37b8001..f4c0aa3 100644--- a/src/frame_interpolation/batch.py+++ b/src/frame_interpolation/batch.py@@ -1,16 +1,14 @@from __future__ import annotations-import jsonimport os-import time-from dataclasses import asdict, replace+from dataclasses import replacefrom pathlib import Pathfrom typing import Callable, Iterable+from ..core import app_logfrom ..core.batch_progress import BatchItemUpdate, BatchProgressfrom ..core.disk_paths import prepare_output_dirfrom ..core.jobs import Cancelled, JobController, active_job-from ..core.paths import LOGSfrom .models import FrameInterpolationBatchResult, FrameInterpolationFailure, FrameInterpolationOptions, FrameInterpolationSuccessfrom . import processor@@ -29,7 +27,6 @@ def interpolate_videos(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, failures = [], []try:destination = prepare_output_dir(output_dir, default=processor.OUTPUTS)@@ -63,18 +60,11 @@ def interpolate_videos(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()))+ status = "cancelled" if cancelled else ("partial" if failures else "success")+ app_log.info("frame-interp-batch", f"{status} ok={len(successes)} failed={len(failures)}")+ manifest_path = app_log.session_path()+ reporter.finish(cancelled=cancelled, manifest_path=manifest_path)+ return FrameInterpolationBatchResult(successes, failures, cancelled, manifest_path)except BaseException as exc:reporter.finish(cancelled=controller.cancel.is_set(), error=str(exc))raisediff --git a/src/frame_interpolation/models.py b/src/frame_interpolation/models.pyindex 467bf56..1cccdce 100644--- a/src/frame_interpolation/models.py+++ b/src/frame_interpolation/models.py@@ -52,7 +52,7 @@ class FrameInterpolationOptions:quality: str = "Auto (Default)"hdr_mode: bool = Falserename_mode: str = "Auto"- custom_suffix: str = "_DLSSFG"+ custom_suffix: str = "_Frame_Interpolation"preview_seconds: float | None = None# True = truncated preview uses the forced H.264 SDR path (current behavior).# False = truncated preview uses the user's codec/container (HDR preserved).diff --git a/src/frame_interpolation/native.py b/src/frame_interpolation/native.pyindex d72b813..ff4df37 100644--- a/src/frame_interpolation/native.py+++ b/src/frame_interpolation/native.py@@ -8,6 +8,7 @@ from fractions import Fractionimport numpy as np+from ..core import app_logfrom ..core.jobs import Cancelled, JobControllerfrom .capabilities import DLSSG_WORKER, RUNTIME_DIR@@ -106,6 +107,7 @@ class DirectDLSSGSession:)if magic != SETUP_OUT_MAGIC or status:self.close()+ app_log.error("dlssg", f"DLSSG session creation failed (status {status})", self.log_text()[-500:])raise RuntimeError(f"DLSSG session creation failed (status {status}); runtime maximum is "f"{maximum + 1}×.\n{self.log_text()}"@@ -165,6 +167,7 @@ class DirectDLSSGSession:"<4I", _read_exact(self.process.stdout, struct.calcsize("<4I")))if magic != FRAME_OUT_MAGIC or status:+ app_log.error("dlssg", f"DLSSG evaluation failed at frame {self._next_index - 1} (status {status})", self.log_text()[-500:])raise RuntimeError(f"DLSSG evaluation failed at input frame {self._next_index - 1} "f"(status {status}).\n{self.log_text()}"diff --git a/src/frame_interpolation/preview.py b/src/frame_interpolation/preview.pyindex 6015821..bb9013a 100644--- a/src/frame_interpolation/preview.py+++ b/src/frame_interpolation/preview.py@@ -94,7 +94,7 @@ def update_frame_interpolation_preview_mode(single = len(normalized) == 1label = "Input video preview" if len(normalized) <= 1 else f"Input video preview (first of {len(normalized)})"return (- gr.update(value=normalized[0] if available else None, visible=available, label=label),+ gr.update(value=normalized[0] if available else None, visible=True if available else "hidden", label=label),gr.update(value=None, visible=True),gr.update(visible=single),)diff --git a/src/frame_interpolation/processor.py b/src/frame_interpolation/processor.pyindex 7f6d4c7..62cff9d 100644--- a/src/frame_interpolation/processor.py+++ b/src/frame_interpolation/processor.py@@ -1,14 +1,13 @@from __future__ import annotations-import jsonimport mathimport osimport shutilimport subprocessimport threadingimport time-from contextlib import nullcontext-from dataclasses import asdict, dataclass, replace+from contextlib import nullcontext, suppress+from dataclasses import dataclass, replacefrom fractions import Fractionfrom pathlib import Pathfrom typing import Callable@@ -16,12 +15,12 @@ from typing import Callableimport avimport numpy as np-from ..core import ffmpeg+from ..core import app_log, ffmpegfrom ..core.gpu_selection import resolve_runtime_ai_gpufrom ..core.jobs import Cancelled, active_jobfrom ..core.naming import output_filename, validate_renamefrom ..core.disk_paths import OutputFile, prepare_output_dir-from ..core.paths import JOBS, LOGS, OUTPUTS+from ..core.paths import JOBS, OUTPUTSfrom ..core.runtime import prepare_runtime, rotate_framefrom .capabilities import probe_frame_interpolation_capabilitiesfrom .guides import DLSSGGuideGenerator, Guide@@ -308,7 +307,7 @@ def interpolate_video(destination = prepare_output_dir(output_dir, default=OUTPUTS)JOBS.mkdir(exist_ok=True)- LOGS.mkdir(exist_ok=True)+ app_log.info("frame-interp", f"start src={source.name}")stamp = time.strftime("%Y%m%d-%H%M%S") + f"-{time.time_ns() % 1_000_000:06d}"job_dir = JOBS / f"{source.stem}-DLSSFG-{stamp}-{os.getpid()}"job_dir.mkdir(parents=True, exist_ok=False)@@ -469,39 +468,20 @@ def interpolate_video(timings["total_seconds"] = elapsedperformance.update(source_frames_per_second=decoded / elapsed,output_frames_per_second=output_count / elapsed)- report = {- "status": "success",- "input": str(source),- "output": str(output),- "options": _json_safe(asdict(options)),- "capabilities": asdict(capabilities),- "ai_gpu": ai_gpu,- "video_gpu": video_gpu,- "plan": {key: str(value) if isinstance(value, Fraction) else value for key, value in asdict(plan).items()},- "input_frames": decoded,- "output_frames": output_count,- "copied_frames": writer.copied,- "generated_frames": writer.generated,- "dropped_frames": dropped_frames,- "maximum_temporal_approximation_seconds": float(writer.max_error),- "scene_cuts": scene_cuts,- "duplicate_intervals": duplicate_intervals,- "timestamp_discontinuities": discontinuities,- "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")+ report_path = app_log.session_path()if controller.cancel.is_set():+ app_log.info("frame-interp", f"cancelled src={source.name}")raise Cancelled("Frame interpolation stopped by user.")+ app_log.info(+ "frame-interp",+ f"done src={source.name} out={output.name} frames={output_count} "+ f"elapsed={elapsed:.0f}s",+ )output_file.publish()_report_progress(1.0, "Frame interpolation complete")return FrameInterpolationResult(output_path=str(output.resolve()),- report_path=str(report_path.resolve()),+ report_path=str(report_path),selected_path=plan.path,native_multiplier=plan.native_multiplier,cascade_stages=plan.cascade_stages,@@ -521,27 +501,15 @@ def interpolate_video(if controller.cancel.is_set():raise Cancelled("Frame interpolation stopped by user.") from excif isinstance(exc, Cancelled):+ app_log.info("frame-interp", f"cancelled src={source.name}")raise- LOGS.mkdir(exist_ok=True)- failure_stamp = time.strftime("%Y%m%d-%H%M%S") + f"-{time.time_ns() % 1_000_000:06d}"- failure_path = LOGS / f"DLSSFG_{source.stem}_{failure_stamp}.failure.json"- failure_path.write_text(- json.dumps(- {- "status": "failure",- "input": str(source),- "options": _json_safe(asdict(options)),- "error": str(exc),- "capabilities": asdict(capabilities),- "worker_logs": [session.log_text() for session in sessions],- "timings": timings,- "performance": performance,- },- indent=2,- ),- encoding="utf-8",- )- raise RuntimeError(f"{exc}\nDiagnostic report: {failure_path.resolve()}") from exc+ tails: dict[str, object] = {}+ with suppress(Exception):+ tails["worker"] = [session.log_text() for session in sessions]+ if "encoder_logs" in locals() and encoder_logs:+ tails["ffmpeg"] = list(encoder_logs)[-40:]+ failure_path = app_log.fail("frame-interp", f"DLSSFG_{source.stem}", exc, tails or None)+ raise RuntimeError(f"{exc}\nDetails: {failure_path.resolve()}") from excfinally:if output_file is not None:output_file.cleanup()diff --git a/src/frame_interpolation/ui.py b/src/frame_interpolation/ui.pyindex 53db347..5d2e7ed 100644--- a/src/frame_interpolation/ui.py+++ b/src/frame_interpolation/ui.py@@ -7,9 +7,8 @@ from pathlib import Pathimport gradio as grfrom ..core.batch_ui import (BATCH_HEADERS, bind_batch_ui, build_media_clear_button, build_media_select_button,- build_path_controls,+ build_path_controls, build_save_controls,)-from ..core.disk_paths import create_media_archivefrom ..core.ffmpeg import hdr_mode_supportedfrom ..core.ffmpeg.preview import normalize_preview_encoding, resolve_final_preview@@ -109,12 +108,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."- archive_path = None- if not direct_disk and not result.cancelled:- archive_path = create_media_archive(- files, output_dir, "DLSSFG_VIDEO_BATCH", controller=controller,- )- return gr.update(value=preview, visible=not direct_disk), archive_path, rows, status+ return gr.update(value=preview, visible=not direct_disk), files, rows, status@dataclass(slots=True)class FrameInterpolationTab:@@ -136,6 +130,8 @@ class FrameInterpolationTab:stop: objectreset: objectoutput_video: object+ save_download: object+ zip_button: objectzip_download: objectstatus: objectresults: object@@ -170,7 +166,7 @@ def build_frame_interpolation_tab(settings: UISettings) -> FrameInterpolationTabtype="filepath", allow_reordering=True, elem_id="frame-interpolation-upload-list",elem_classes=["media-upload-surface"],)- input_preview = gr.Video(label="Input video preview", interactive=False, visible=False)+ input_preview = gr.Video(label="Input video preview", interactive=False, visible="hidden")with gr.Row(visible=False, elem_id="frame-interpolation-input-actions",elem_classes=["media-input-actions"],@@ -179,25 +175,29 @@ def build_frame_interpolation_tab(settings: UISettings) -> FrameInterpolationTab"Choose Videos", ["video"], "frame-interpolation-select-input",)clear_source = build_media_clear_button("frame-interpolation-clear-input")- input_path, output_path = build_path_controls()- with gr.Accordion("DLSS Frame Generation Settings", open=True):+ with gr.Row():+ render = gr.Button("Interpolate video(s)", variant="primary")+ preview = gr.Button("Preview 3 sec", visible=False)+ stop = gr.Button("Stop", variant="stop")+ reset = gr.Button("Reset settings")+ with gr.Column():with gr.Row():target_fps = gr.Dropdown(FPS_CHOICES, value=settings.frame_interpolation_target_fps,- label="Output FPS", info="Fractional choices use exact 1001-based rates.",+ label="Output FPS",)engine = gr.Radio(ENGINE_CHOICES, value=settings.frame_interpolation_engine, label="DLSS engine",- info="Auto uses a supported exact native grid, then the 2× cascade when required.",)+ input_path, output_path = build_path_controls()quality = gr.Radio(QUALITY_CHOICES, value=settings.frame_interpolation_quality,- label="Encoding quality", info="Auto uses output resolution, selected FPS, and codec.",+ label="Encoding quality",)with gr.Row():codec = gr.Dropdown(CODEC_CHOICES, value=settings.frame_interpolation_codec,- label="Video codec", info="Plain = CPU, Suffixed = NVIDIA NVENC.",+ label="Video codec",)container = gr.Dropdown(CONTAINER_CHOICES, value=settings.frame_interpolation_container, label="Container"@@ -205,28 +205,21 @@ def build_frame_interpolation_tab(settings: UISettings) -> FrameInterpolationTabwith gr.Row():rename_mode = gr.Radio(RENAME_MODES, value=settings.frame_interpolation_rename_mode, label="Rename",- info="Auto adds a DLSSFG timestamp; Copy keeps the original base name; Custom appends your suffix.",)custom_suffix = gr.Textbox(value=settings.frame_interpolation_custom_suffix, label="Custom suffix",- placeholder="_DLSSFG", interactive=settings.frame_interpolation_rename_mode == "Custom",+ placeholder="_Frame_Interpolation", interactive=settings.frame_interpolation_rename_mode == "Custom",)hdr_mode = gr.Checkbox(value=settings.frame_interpolation_hdr_mode and hdr_mode_supported(settings.frame_interpolation_codec),label="HDR Mode",- info="When on: 10-bit output, copies input colorspace; keeps HDR if input is HDR. Only for H.265 / AV1 / ProRes.",interactive=hdr_mode_supported(settings.frame_interpolation_codec),)- with gr.Row():- preview = gr.Button("Preview 3 sec", visible=False)- render = gr.Button("Interpolate video(s)", variant="primary")- stop = gr.Button("Stop", variant="stop")- reset = gr.Button("Reset settings")with gr.Column(scale=3):output_video = gr.Video(label="Interpolated output", interactive=False, visible=True, height=520,)- zip_download = gr.DownloadButton("Save as ZIP", visible=False)+ save_download, zip_button, zip_download = build_save_controls("video", "frame-interpolation")status = gr.Textbox(label="Status", interactive=False, lines=5, max_lines=12)results = gr.Dataframe(headers=BATCH_HEADERS,@@ -235,7 +228,7 @@ def build_frame_interpolation_tab(settings: UISettings) -> FrameInterpolationTab)tab = FrameInterpolationTab(sources, input_preview, input_actions, select_source, clear_source, target_fps, engine, quality, codec, container, rename_mode,- custom_suffix, hdr_mode, preview, render, stop, reset, output_video, zip_download, status, results+ custom_suffix, hdr_mode, preview, render, stop, reset, output_video, save_download, zip_button, zip_download, status, results)tab.input_path, tab.output_path = input_path, output_pathbind_frame_interpolation_events(tab)@@ -243,8 +236,10 @@ def build_frame_interpolation_tab(settings: UISettings) -> FrameInterpolationTabdef bind_frame_interpolation_events(tab: FrameInterpolationTab) -> None:- bind_batch_ui(tab, render_frame_interpolation_batch, kind="video",- preview_mode=update_frame_interpolation_preview_mode,- preview_actions=[(tab.preview, preview_frame_interpolation)])+ bind_batch_ui(+ tab, render_frame_interpolation_batch, kind="video",+ preview_mode=update_frame_interpolation_preview_mode, archive_prefix="DLSSFG_VIDEO_BATCH",+ 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)diff --git a/src/live/effects.py b/src/live/effects.pyindex b18fdb4..6a1b489 100644--- a/src/live/effects.py+++ b/src/live/effects.py@@ -1,14 +1,15 @@-"""Debounced effect snapshots and a deadline confined to one native worker."""+"""Debounced effect snapshots for the serialized in-process bridge."""from __future__ import annotationsimport subprocessimport threadingimport timefrom collections import deque-from dataclasses import asdict, dataclass, fields+from dataclasses import dataclass, fieldsfrom ..core.jobs import Cancelled, JobControllerfrom ..core.runtime import resolve_native_settings+from ..core.nr_composition import mask_selection, report_optionsEFFECT_DEBOUNCE_SECONDS = 0.5EFFECT_REPLACEMENT_TIMEOUT = 15.0@@ -16,18 +17,27 @@ EFFECT_REPLACEMENT_TIMEOUT = 15.0@dataclass(frozen=True, slots=True)class EffectSettings:- nr_preset: strnr_style: strnr_intensity: float+ nr_passes: intlocal_tone_strength: floatlocal_structure_strength: floatskin_structure_strength: float+ nr_color_strength: float+ tone_preservation: float+ face_skin_protection: float+ grain_preservation: float+ shimmer_suppression: float+ mask_feather: int+ nr_mask: object | Noneautomatic_mask: bool- dlss_model_preset: str+ nr_gpu_mode: bool@classmethoddef from_options(cls, options) -> EffectSettings:- result = cls(**{field.name: getattr(options, field.name) for field in fields(cls)})+ values = {field.name: getattr(options, field.name) for field in fields(cls)}+ values["nr_mask"] = mask_selection(values["nr_mask"])+ result = cls(**values)resolve_native_settings(result)return result@@ -83,14 +93,14 @@ class EffectUpdates:return self._applyingdef complete(self, request: EffectRequest, *, pts: int, milliseconds: float,- error: str = "", restored: bool = True, worker_pid: int | None = None) -> None:+ error: str = "", restored: bool = True) -> 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),+ self._history.append({"revision": request.revision, "settings": report_options(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})+ "error": error, "restored": bool(error) and restored})self._applying = Noneself._error = errorif error:@@ -128,18 +138,14 @@ class EffectUpdates:def report(self) -> dict:with self._lock:- return {"initial": asdict(self.initial), "applied": asdict(self.applied),- "requested": asdict(self.requested), "requests": self._revision,+ return {"initial": report_options(self.initial), "applied": report_options(self.applied),+ "requested": report_options(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.- """+ """Bound an effect-session replacement; Stop still cancels the whole job."""def __init__(self, parent: JobController, seconds: float) -> None:super().__init__()@@ -192,4 +198,4 @@ class NativeDeadline(JobController):if self.cancel.is_set():raise Cancelled("Live stopped.") from excif self.expired.is_set():- raise TimeoutError(f"DLSS initialization and verification exceeded {self.seconds:g} seconds.") from exc+ raise TimeoutError(f"Bridge initialization and verification exceeded {self.seconds:g} seconds.") from excdiff --git a/src/live/models.py b/src/live/models.pyindex d2bc621..d4c22fc 100644--- a/src/live/models.py+++ b/src/live/models.py@@ -8,12 +8,6 @@ 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:@@ -23,21 +17,27 @@ class LiveOptions:max_height: int = 720# Shared DLSS values (mirrored with the Neural Rendering modes, persisted# globally); effects can update during playback, sizing stays fixed.- nr_preset: str = "Default"nr_style: str = "Default"nr_intensity: float = 1.0+ nr_passes: int = 1local_tone_strength: float = 1.0local_structure_strength: float = 1.0skin_structure_strength: float = -1.0+ nr_color_strength: float = 1.0+ tone_preservation: float = 0.0+ face_skin_protection: float = 0.0+ grain_preservation: float = 0.0+ shimmer_suppression: float = 0.70+ mask_feather: int = 0+ nr_mask: object | None = Noneautomatic_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+ nr_gpu_mode: bool = True+ # Source default: Live renders at the input size; 75%/50%/25% downscale+ # first (Lanczos) and keep the pipe/NVENC cheap on mid-range GPUs.+ upscaling_factor: float = 1.0segment_seconds: int = 2target_fps: str = "Auto"buffer_seconds: float = 6.0- guide_quality: str = "Fast"queue_frames: int = 3network_timeout: float = 20.0open_mpv: bool = Truediff --git a/src/live/pipeline.py b/src/live/pipeline.pyindex 7c02fd4..3bbb623 100644--- a/src/live/pipeline.py+++ b/src/live/pipeline.py@@ -10,24 +10,29 @@ import shutilimport subprocessimport threadingimport time-from dataclasses import asdict, replace+from contextlib import suppress+from dataclasses import replace+from fractions import Fractionfrom pathlib import Pathimport avimport numpy as np+from av.codec.hwaccel import HWAccelfrom ..core.gpu_selection import resolve_runtime_ai_gpufrom ..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 ..core import app_log+from ..core.paths import FFMPEG, LIVE_DIR+from ..core.runtime import (DLSSFrameSession, prepare_runtime, resolve_native_settings,+ resolve_output_size, resolve_upscaling_mode, resize_fit,+ rotate_frame, verify_feature_18)from ..settings.storage import processing_gpu_settingsfrom ..neural_rendering.video.guides import TemporalGuideGeneratorfrom .effects import EffectSettings, EffectUpdates, NativeDeadline, EFFECT_REPLACEMENT_TIMEOUTfrom .hls_server import HlsServer-from .models import (LIVE_FRAME_COUNT, LIVE_FPS_CHOICES, LIVE_GUIDE_CHOICES,+from .models import (LIVE_FPS_CHOICES,LIVE_MAX_HEIGHTS, LIVE_SOURCE_QUALITY_CHOICES, LiveOptions, LiveSessionInfo, ResolvedSource)-from .player import check_live_binaries, launch_mpv+from .player import check_live_binaries, launch_mpv, mpv_tailfrom .source_resolver import _classify, input_args, probe_source, resolve_source, run_capturefrom .transport import TIME_BASE, AdaptiveRate, PipeReader, TimestampMuxer, VideoFrame, get, put@@ -48,8 +53,8 @@ def validate_options(options: LiveOptions) -> None: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.target_fps not in LIVE_FPS_CHOICES:+ raise ValueError("Choose a valid frame rate.")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:@@ -66,7 +71,7 @@ def _redact(text: str) -> str:class LiveSession(threading.Thread):- """Bounded decode/guide -> DLSS -> timestamped encode -> buffered playback."""+ """Bounded decode/scene reset -> feature 18 -> encode -> buffered playback."""def __init__(self, options: LiveOptions) -> None:super().__init__(daemon=True, name="dlss5-live-session")@@ -89,8 +94,9 @@ class LiveSession(threading.Thread):self._logs: dict[str, BoundedLogBuffer] = {}self._log_threads: list[threading.Thread] = []self._rate: AdaptiveRate | None = None- self._native_logs: list[str] = []+ self._bridge_logs: list[str] = []self._feature_evidence: dict = {}+ self._bridge_status: dict = {}self._title = ""self._published_seconds = 0.0self._published_sequence = -1@@ -118,10 +124,11 @@ class LiveSession(threading.Thread):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}")+ 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}")+ app_log.error("live", f"{stage}: {exc}")self.controller.stop()def _spawn(self, name: str, command: list[str], **kwargs) -> subprocess.Popen:@@ -134,28 +141,34 @@ class LiveSession(threading.Thread):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:])+ def _process_error(self, name: str, process: subprocess.Popen) -> RuntimeError:+ detail = "\n".join(self._logs[name].snapshot()[-12:])+ app_log.error("live-ffmpeg", f"{name} exited code={process.returncode}", detail)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:]+ self._bridge_logs = [*self._bridge_logs, *native.bridge_logs][-500:]+ self._bridge_status = native.structured_status()- def _verified_effect_worker(self, settings: EffectSettings, native_args: dict,+ def _verified_effect_session(self, settings: EffectSettings, native_args: dict,item: VideoFrame, index: int):native = Nonetry:with NativeDeadline(self.controller, EFFECT_REPLACEMENT_TIMEOUT) as deadline:native = DLSSFrameSession(**native_args, controller=deadline,- native_settings=resolve_native_settings(settings))+ native_settings=resolve_native_settings(settings),+ composition_mask=settings.nr_mask)+ native.diagnostics.encode_backend = (+ "nvenc" if self.info.encoder == "NVIDIA NVENC" else "cpu"+ )start = time.perf_counter()- output, pts = native.process(index=index, rgba=item.rgba, motion=item.motion,- reset=True, pts=item.pts)+ output, pts = native.process(index=index, rgba=item.rgba,+ reset=True, pts=item.pts)cost = time.perf_counter() - startif 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"}+ raise RuntimeError("Replacement bridge session returned a different frame timestamp.")+ evidence = verify_feature_18(native.bridge_logs, native.structured_status())+ self._feature_evidence = dict(evidence)return native, output, costexcept Exception:if native is not None:@@ -169,7 +182,7 @@ class LiveSession(threading.Thread):native.abort() # release resources/config/log ownership before starting anothererror = ""try:- replacement, output, cost = self._verified_effect_worker(request.settings, native_args, item, index)+ replacement, output, cost = self._verified_effect_session(request.settings, native_args, item, index)except Cancelled:raiseexcept Exception as exc:@@ -177,7 +190,7 @@ class LiveSession(threading.Thread):raise Cancelled("Live stopped.") from excerror = _redact(str(exc))try:- replacement, output, cost = self._verified_effect_worker(self.effects.applied, native_args, item, index)+ replacement, output, cost = self._verified_effect_session(self.effects.applied, native_args, item, index)except Cancelled:raiseexcept Exception as recovery:@@ -186,7 +199,7 @@ class LiveSession(threading.Thread):error=detail, restored=False)raise RuntimeError(detail) from recoveryself.effects.complete(request, pts=item.pts, milliseconds=(time.perf_counter()-start)*1000,- error=error, worker_pid=getattr(getattr(replacement, "worker", None), "pid", None))+ error=error)self._rate.reset_measurements()self._last_progress = time.monotonic()self._write_report()@@ -226,9 +239,18 @@ class LiveSession(threading.Thread):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.+ if opts.nr_gpu_mode:+ # Demux compressed video without decoding pixels. Audio alone is+ # normalized to timestamped stereo PCM for PyAV's HLS mux.+ command += ["-map", "0:v:0", "-map", f"{audio_input}:a:0?", "-sn", "-dn",+ "-c:v", "copy",+ "-c:a", "pcm_s16le", "-ar", "48000", "-ac", "2",+ "-af", "aresample=async=1:first_pts=0",+ "-f", "nut", "-write_index", "0", "pipe:1"]+ return command+ # GPU OFF keeps the host-staging pipeline and its decoded RGBA stream.filters = [] if opts.target_fps == "Source" else [f"fps={rate}"]- filters += [f"scale={width}:{height}:flags=fast_bilinear", "setsar=1"]+ filters += [f"scale={width}:{height}:flags=lanczos", "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",@@ -269,8 +291,7 @@ class LiveSession(threading.Thread):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)+ guides = TemporalGuideGenerator(width, height, flow_width=320)index = 0last_pts: int | None = Nonesampled = 0@@ -309,8 +330,9 @@ class LiveSession(threading.Thread):duration = (max(1, round(packet.duration * packet.time_base / TIME_BASE))if self.options.target_fps == "Source" and packet.durationelse max(1, round(1 / rate.fps / TIME_BASE)))- put(prepared, VideoFrame(current, pts, duration, rgba, guide.motion,- guide.reset or discontinuity), self.controller.cancel)+ put(prepared, VideoFrame(+ current, pts, duration, rgba, reset=guide.reset or discontinuity+ ), self.controller.cancel)last_pts = ptscode = decoder.wait(timeout=5)if code:@@ -321,7 +343,7 @@ class LiveSession(threading.Thread):except Cancelled:passexcept Exception as exc:- self._stage_error("Decode / motion guides", exc)+ self._stage_error("Decode / scene analysis", exc)def _encode(self, encoder: subprocess.Popen, outgoing: queue.Queue, width: int, height: int) -> None:muxer = None@@ -359,6 +381,300 @@ class LiveSession(threading.Thread):except Exception:pass+ def _produce_cuda(+ self, resolved: ResolvedSource, metadata: dict, in_w: int, in_h: int,+ out_w: int, out_h: int, factor: float, mode: dict, prepared_runtime,+ gpu: dict, ordinal: int,+ ) -> None:+ """Compressed demux -> PyAV NVDEC -> feature 18 -> PyAV NVENC -> HLS."""+ if self._session_dir is None or self._rate is None:+ raise RuntimeError("Live session storage and cadence must be initialized.")+ decoder = native = input_container = hls = None+ watchdog_done = threading.Event()+ watchdog = threading.Thread(target=self._watchdog, args=(watchdog_done,), daemon=True)+ self._last_progress = time.monotonic()+ watchdog.start()+ matrix_name = str(metadata.get("color_space") or "").casefold()+ color_matrix = 2 if "2020" in matrix_name else (+ 0 if matrix_name in {"bt470bg", "smpte170m", "smpte240m", "fcc"} else 1+ )+ color_range = int(+ str(metadata.get("color_range") or "").casefold() in {"pc", "jpeg", "full"}+ )+ try:+ decoder = self._spawn(+ "demux", self._decoder_command(resolved, in_w, in_h, self._rate.rate),+ stdout=subprocess.PIPE, stdin=subprocess.DEVNULL,+ )+ decode_device = HWAccel(+ "cuda", device=str(ordinal), allow_software_fallback=True,+ options={"primary_ctx": "1"}, is_hw_owned=True,+ )+ input_container = av.open(+ PipeReader(decoder.stdout), mode="r", format="nut",+ options={"probesize": "32768", "analyzeduration": "0"},+ hwaccel=decode_device,+ )+ video_input = input_container.streams.video[0]+ audio_input = next(iter(input_container.streams.audio), None)++ encode_device = HWAccel(+ "cuda", device=str(ordinal), options={"primary_ctx": "1"}, is_hw_owned=True+ )+ list_size = max(+ 12, math.ceil((self.options.buffer_seconds + 60) / self.options.segment_seconds)+ )+ hls = av.open(+ str(self._session_dir / "index.m3u8"), mode="w", format="hls",+ options={+ "hls_time": str(self.options.segment_seconds),+ "hls_list_size": str(list_size),+ "hls_delete_threshold": "6",+ "hls_flags": "delete_segments+independent_segments+temp_file",+ "hls_segment_filename": str(self._session_dir / "seg%07d.ts"),+ },+ )+ video_output = hls.add_stream(+ "h264_nvenc", rate=self._rate.rate, hwaccel=encode_device+ )+ video_output.width, video_output.height = out_w, out_h+ video_output.pix_fmt = "cuda"+ video_output.codec_context.sw_format = "nv12"+ video_output.time_base = TIME_BASE+ video_output.codec_context.time_base = TIME_BASE+ video_output.codec_context.gop_size = max(+ 1, math.ceil(float(self._rate.rate) * self.options.segment_seconds)+ )+ video_output.codec_context.max_b_frames = 0+ video_output.codec_context.options = {+ "preset": "p1", "tune": "ll", "rc": "vbr", "cq": "23",+ "gpu": str(ordinal), "rc-lookahead": "0", "zerolatency": "1",+ "forced-idr": "1",+ }+ audio_output = audio_resampler = None+ if audio_input is not None:+ audio_output = hls.add_stream("aac", rate=48000)+ audio_output.codec_context.layout = "stereo"+ audio_output.codec_context.bit_rate = 160_000+ audio_output.time_base = Fraction(1, 48000)+ audio_output.codec_context.time_base = Fraction(1, 48000)+ audio_resampler = av.AudioResampler(format="fltp", layout="stereo", rate=48000)++ native_args = dict(+ input_width=in_w, input_height=in_h, output_width=out_w, output_height=out_h,+ frame_count=None, warmup_frames=0, factor=factor, mode=mode,+ gpu=gpu, runtime_bundle=prepared_runtime.runtime_bundle, cuda_video=True,+ )+ native = DLSSFrameSession(+ **native_args, native_settings=resolve_native_settings(self.options),+ composition_mask=self.options.nr_mask,+ controller=self.controller,+ )+ self._ready.set()+ processing_start = time.perf_counter()+ pacing_seconds = 0.0+ processed = source_frames = sampled = 0+ last_pts: int | None = None+ software_guides = TemporalGuideGenerator(out_w, out_h, flow_width=320)++ for packet in input_container.demux():+ if self.controller.cancel.is_set():+ raise Cancelled("Live stopped.")+ self._last_progress = time.monotonic()+ if packet.stream.type == "audio":+ if audio_output is not None and audio_resampler is not None:+ for audio_frame in packet.decode():+ for converted in audio_resampler.resample(audio_frame):+ for encoded_packet in audio_output.encode(converted):+ hls.mux(encoded_packet)+ continue+ if packet.stream.type != "video":+ continue+ for frame in packet.decode():+ current = source_frames+ source_frames += 1+ self._set(source_frames=source_frames)+ if not self._rate.accepts(current):+ sampled += 1+ self._set(sampled_frames=sampled)+ continue+ frame_time_base = frame.time_base or video_input.time_base+ source_pts = int(frame.pts if frame.pts is not None else current)+ pts = round(source_pts * frame_time_base / TIME_BASE)+ discontinuity = last_pts is not None and (+ pts <= last_pts or (pts - last_pts) * TIME_BASE > 0.5+ )+ duration = (+ max(1, round(frame.duration * frame_time_base / TIME_BASE))+ if self.options.target_fps == "Source" and frame.duration+ else max(1, round(1 / self._rate.fps / TIME_BASE))+ )+ self._update_playback()+ pacing_seconds += self._pace(pts)+ guide_started = time.perf_counter()+ rgba = None+ if frame.format.name == "cuda":+ scene_score, reset = native.score_cuda_frame(+ frame, color_matrix=color_matrix, color_range=color_range+ )+ else:+ rgba = rotate_frame(+ frame.to_ndarray(format="rgba"), int(metadata.get("rotation") or 0)+ )+ if rgba.shape[:2] != (out_h, out_w):+ rgba = resize_fit(rgba, out_w, out_h)+ rgba = np.ascontiguousarray(rgba, dtype=np.uint8)+ guide = software_guides.process(rgba)+ scene_score, reset = guide.scene_score, guide.reset+ guide_cost = time.perf_counter() - guide_started+ reset = bool(reset or discontinuity)++ def evaluate(settings, force_reset: bool):+ resolved_settings = resolve_native_settings(settings)+ if (+ settings.nr_mask != native.composition_mask+ or int(resolved_settings["mask_feather"])+ != int(native.native_settings.get("mask_feather", 0))+ ):+ native.update_composition_mask(+ settings.nr_mask, int(resolved_settings["mask_feather"])+ )+ native.native_settings = resolved_settings+ native.bridge_status["nr_passes"] = int(resolved_settings["nr_passes"])+ native.bridge_status["allocated_feature_instances"] = int(+ resolved_settings["nr_passes"]+ )+ native.bridge_status["composition"]["color_strength"] = float(+ resolved_settings["color_strength"]+ )+ native.bridge_status["composition"]["tone_preservation"] = float(+ resolved_settings["tone_preservation"]+ )+ native.bridge_status["composition"]["face_skin_protection"] = float(+ resolved_settings["face_skin_protection"]+ )+ native.bridge_status["composition"]["grain_preservation"] = float(+ resolved_settings["grain_preservation"]+ )+ native.bridge_status["temporal_stabilization"]["shimmer_suppression"] = float(+ resolved_settings["shimmer_suppression"]+ )+ evaluation_started = time.perf_counter()+ values = dict(+ index=processed, reset=force_reset, scene_score=scene_score,+ pts=pts, duration=duration, time_base=TIME_BASE,+ color_matrix=color_matrix, color_range=color_range,+ )+ if frame.format.name == "cuda":+ result, result_pts = native.process_video_frame(+ frame=frame, rotation=int(metadata.get("rotation") or 0), **values+ )+ else:+ result, result_pts = native.process_video_frame(rgba=rgba, **values)+ return result, result_pts, time.perf_counter() - evaluation_started++ request = self.effects.take_due()+ if request is None:+ output, output_pts, cost = evaluate(self.effects.applied, reset)+ else:+ old_settings = self.effects.applied+ update_started = time.perf_counter()+ try:+ if not request.settings.nr_gpu_mode:+ raise RuntimeError("Changing GPU mode during Live requires Stop and Start.")+ output, output_pts, cost = evaluate(request.settings, True)+ self.effects.complete(+ request, pts=pts,+ milliseconds=(time.perf_counter() - update_started) * 1000,+ )+ except Cancelled:+ raise+ except Exception as exc:+ output, output_pts, cost = evaluate(old_settings, True)+ self.effects.complete(+ request, pts=pts,+ milliseconds=(time.perf_counter() - update_started) * 1000,+ error=_redact(str(exc)),+ )+ self._rate.reset_measurements()+ self._write_report()+ if output_pts != pts:+ raise RuntimeError("Neural Rendering changed a Live frame timestamp.")+ encode_started = time.perf_counter()+ for encoded_packet in video_output.encode(output):+ hls.mux(encoded_packet)+ encode_cost = time.perf_counter() - encode_started+ processed += 1+ last_pts = pts+ if processed == 1:+ self._feature_evidence = verify_feature_18(+ native.bridge_logs, native.structured_status()+ )+ self._first_frame_at = time.monotonic()+ old_info = self.snapshot()+ self._rate.observe(cost, guide_cost, encode_cost)+ guide_ms, encode_ms = guide_cost * 1000, encode_cost * 1000+ self._set(+ processed_frames=processed,+ guide_ms=guide_ms if not old_info.guide_ms else old_info.guide_ms * 0.9 + guide_ms * 0.1,+ dlss_ms=cost * 1000 if processed == 1 else old_info.dlss_ms * 0.9 + cost * 100,+ encode_ms=encode_ms if not old_info.encode_ms else old_info.encode_ms * 0.9 + encode_ms * 0.1,+ target_fps=self._rate.fps,+ effective_fps=processed / max(+ time.perf_counter() - processing_start - pacing_seconds, 0.001+ ),+ media_seconds=float((pts + duration) * TIME_BASE),+ )+ self._last_progress = time.monotonic()++ for encoded_packet in video_output.encode():+ hls.mux(encoded_packet)+ if audio_output is not None:+ if audio_resampler is not None:+ for converted in audio_resampler.resample(None):+ for encoded_packet in audio_output.encode(converted):+ hls.mux(encoded_packet)+ for encoded_packet in audio_output.encode():+ hls.mux(encoded_packet)+ hls.close()+ hls = None+ input_container.close()+ input_container = None+ code = decoder.wait(timeout=10)+ if code:+ raise self._process_error("demux", decoder)+ if not processed:+ raise RuntimeError("The decoder produced no accepted video frames.")+ native.close()+ self._remember_native(native)+ self._bridge_status = native.structured_status()+ native = None+ self.effects.finish()+ self._finished = True+ finally:+ watchdog_done.set()+ if native is not None:+ self._remember_native(native)+ self._bridge_status = native.structured_status()+ with suppress(Exception):+ native.abort()+ if hls is not None:+ with suppress(Exception):+ hls.close()+ if input_container is not None:+ with suppress(Exception):+ input_container.close()+ if decoder is not None:+ if decoder.poll() is None:+ decoder.terminate()+ with suppress(subprocess.TimeoutExpired):+ decoder.wait(timeout=5)+ if decoder.poll() is None:+ decoder.kill()+ decoder.wait(timeout=5)+ self.controller.unregister(decoder)+ watchdog.join(timeout=1)+def _playlist(self) -> tuple[int, float]:try:lines = (self._session_dir / "index.m3u8").read_text(encoding="utf-8").splitlines()@@ -398,10 +714,12 @@ class LiveSession(threading.Thread):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()+ if code is not None and not finished:+ if code:+ tail = mpv_tail(self._mpv)+ app_log.error("live-mpv", f"MPV exited code={code}", tail)+ raise RuntimeError(f"MPV exited with code {code}.")+ self.stop()returnready = count and (published >= self.options.buffer_seconds or finished)if ready and not self._player_attempted:@@ -471,13 +789,25 @@ class LiveSession(threading.Thread):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}...")+ self._set(status=f"Preparing Neural Rendering: {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)- nvenc, ordinal = self._select_encoder(prepared_runtime.gpus, video_uuid, out_w, out_h)+ encoder_uuid = ai_uuid if options.nr_gpu_mode else video_uuid+ nvenc, ordinal = self._select_encoder(prepared_runtime.gpus, encoder_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")+ if options.nr_gpu_mode:+ if not nvenc or ordinal is None:+ raise RuntimeError(+ "GPU ON Live requires H.264 NVENC on the selected AI GPU. "+ "Switch GPU OFF to use the host-staging CPU encoder boundary."+ )+ self._produce_cuda(+ resolved, metadata, in_w, in_h, out_w, out_h, factor, mode,+ prepared_runtime, gpu, ordinal,+ )+ returnnative = Noneprocesses: list[subprocess.Popen] = []threads: list[threading.Thread] = []@@ -487,12 +817,14 @@ class LiveSession(threading.Thread):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,+ frame_count=None, 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),+ composition_mask=options.nr_mask,controller=self.controller)+ native.diagnostics.encode_backend = "nvenc" if nvenc else "cpu"self._last_progress = time.monotonic()- decoder = self._spawn("decoder", self._decoder_command(resolved, in_w, in_h, self._rate.rate),+ decoder = self._spawn("decoder", self._decoder_command(resolved, out_w, out_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),@@ -502,7 +834,7 @@ class LiveSession(threading.Thread):# 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),+ threads = [threading.Thread(target=self._decode, args=(decoder, incoming, out_w, out_h), daemon=True),threading.Thread(target=self._encode, args=(encoder, outgoing, out_w, out_h), daemon=True)]for thread in threads:thread.start()@@ -525,15 +857,15 @@ class LiveSession(threading.Thread):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,+ output, pts = native.process(index=processed, rgba=item.rgba,reset=item.reset, pts=item.pts)cost = time.perf_counter() - startif pts != item.pts:- raise RuntimeError("DLSS worker returned a different frame timestamp.")+ raise RuntimeError("Neural Rendering bridge returned a different frame timestamp.")processed += 1if 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"}+ evidence = verify_feature_18(native.bridge_logs, native.structured_status())+ self._feature_evidence = dict(evidence)if self._first_frame_at is None:self._first_frame_at = time.monotonic()info = self.snapshot()@@ -585,20 +917,10 @@ class LiveSession(threading.Thread):passwatchdog.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 _write_report(self) -> None:+ # Reporting is now a single compact session-log line (see run()).+ # Kept as a no-op for existing callers.+ returndef run(self) -> None:started = time.monotonic()@@ -610,10 +932,9 @@ class LiveSession(threading.Thread):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))+ (self._session_dir / "owner.json").write_text(json.dumps({"pid": os.getpid(), "keep": self.options.keep_files}))+ self._set(report_path="")+ app_log.info("live", f"start src={_redact(self.options.source)[:60]}")server = HlsServer(self._session_dir)server.start()self._set(playlist_url=server.playlist_url())@@ -627,15 +948,26 @@ class LiveSession(threading.Thread):# 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:+ if self._mpv and self._mpv.poll() is not None:+ if self._mpv.returncode:+ tail = mpv_tail(self._mpv)+ app_log.error("live-mpv", f"MPV exited code={self._mpv.returncode}", tail)+ 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:+ tails = {name: log.snapshot()[-12:] for name, log in self._logs.items()}+ if self._bridge_logs:+ tails["bridge"] = self._bridge_logs[-20:]+ if self._mpv is not None:+ tail = mpv_tail(self._mpv)+ if tail:+ tails["mpv"] = tail+ err_path = app_log.fail("live", f"live-{stamp}", error, tails)+ self._set(report_path=str(err_path), status=f"Failed: {error}", failures=[error])+ else:+ app_log.info("live", "cancelled")self._set(status="Stopped.")finally:self.effects.finish()@@ -651,9 +983,10 @@ class LiveSession(threading.Thread):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:+ if self._finished and not self.snapshot().failures:+ self._set(status=f"Finished: {self._title}. {self.info.processed_frames} frames enhanced.")+ app_log.info("live", f"done frames={self.info.processed_frames} elapsed={self.info.elapsed_seconds:.0f}s")+ elif not self.snapshot().failures:self._set(status="Stopped.")self._write_report()if self._session_dir and not self.options.keep_files:diff --git a/src/live/player.py b/src/live/player.pyindex d1a61da..8fde274 100644--- a/src/live/player.py+++ b/src/live/player.py@@ -2,11 +2,24 @@ from __future__ import annotationsimport osimport subprocess+import threadingfrom pathlib import Path+from ..core.jobs import BoundedLogBuffer, drain_bounded_textfrom ..core.paths import MPV, YTDLP+def mpv_tail(process: subprocess.Popen | None, limit: int = 12) -> str:+ """Return the last captured MPV stderr lines (empty when unavailable)."""+ logs = getattr(process, "_dlss5_err", None)+ if logs is None:+ return ""+ try:+ return "\n".join(logs.snapshot()[-limit:])+ except Exception:+ return ""++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():@@ -60,13 +73,23 @@ def launch_mpv(]creation_flags = getattr(subprocess, "CREATE_NO_WINDOW", 0)try:- return subprocess.Popen(+ process = subprocess.Popen(command,stdout=subprocess.DEVNULL,- stderr=subprocess.DEVNULL,+ stderr=subprocess.PIPE,stdin=subprocess.DEVNULL,env=env,creationflags=creation_flags,)except OSError as exc:raise RuntimeError(f"Could not launch MPV: {exc}.") from exc+ try:+ logs = BoundedLogBuffer(max_tail=60)+ assert process.stderr is not None+ threading.Thread(+ target=drain_bounded_text, args=(process.stderr, logs), daemon=True+ ).start()+ process._dlss5_err = logs # type: ignore[attr-defined]+ except Exception:+ pass+ return processdiff --git a/src/live/source_resolver.py b/src/live/source_resolver.pyindex d6dd6c2..2d577b5 100644--- a/src/live/source_resolver.py+++ b/src/live/source_resolver.py@@ -7,6 +7,7 @@ from fractions import Fractionfrom pathlib import Pathfrom urllib.parse import urlparse+from ..core import app_logfrom ..core.jobs import Cancelled, JobControllerfrom ..core.paths import FFPROBE, YTDLPfrom .models import LIVE_SOURCE_QUALITY_CHOICES, ResolvedSource@@ -97,7 +98,9 @@ def resolve_source(raw: str, max_height: int,if result.returncode:detail = "\n".join(result.stderr.strip().splitlines()[-4:])if "offline" in detail.lower() or "not currently live" in detail.lower():+ app_log.error("live-yt-dlp", "channel offline", detail)raise RuntimeError("This channel is currently offline. Try again when the broadcast is live.")+ app_log.error("live-yt-dlp", "could not resolve video", detail)raise RuntimeError(f"Could not resolve this video: {detail}")return _resolved_metadata(kind, json.loads(result.stdout))@@ -123,6 +126,7 @@ def probe_source(source: ResolvedSource, controller: JobController, timeout: flo"-analyzeduration", "2000000", "-probesize", "5000000", "-show_streams", "-show_format","-of", "json", source.video_url], controller, timeout + 10)if result.returncode:+ app_log.error("live-ffprobe", "source probe failed", result.stderr.strip()[-500:])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)@@ -148,5 +152,11 @@ def probe_source(source: ResolvedSource, controller: JobController, timeout: floduration = 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", []))}+ return {"width": width, "height": height, "coded_width": int(video["width"]),+ "coded_height": int(video["height"]), "rotation": rotation % 360,+ "rate": fps, "duration": duration,+ "color_space": video.get("color_space") or "unknown",+ "color_range": video.get("color_range") or "unknown",+ "color_primaries": video.get("color_primaries") or "unknown",+ "color_transfer": video.get("color_transfer") or "unknown",+ "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.pyindex afdcdc3..0ff7d37 100644--- a/src/live/transport.py+++ b/src/live/transport.py@@ -65,7 +65,6 @@ class VideoFrame:pts: intduration: intrgba: np.ndarray- motion: np.ndarray | None = Nonereset: bool = False@@ -116,7 +115,7 @@ class AdaptiveRate: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."""+ """Discard startup costs after a bridge-session change, keeping cadence."""with self._lock:self.samples.clear()self._warmup_left = 6diff --git a/src/live/ui.py b/src/live/ui.pyindex 83bca3a..f1eabd8 100644--- a/src/live/ui.py+++ b/src/live/ui.py@@ -6,8 +6,9 @@ from pathlib import Pathimport gradio as grfrom ..settings.models import UISettings, parse_automatic_mask-from ..neural_rendering.video.ui import build_dlss_model_control, build_neural_controls-from .models import (LIVE_FPS_CHOICES, LIVE_GUIDE_CHOICES, LIVE_MAX_HEIGHT_CHOICES,+from ..neural_rendering.video.ui import build_neural_controls+from ..neural_rendering.composition_ui import CompositionWidgets, build_composition_widgets+from .models import (LIVE_FPS_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_sessionfrom .models import LiveOptions@@ -15,21 +16,27 @@ from .models import LiveOptionsdef start_live(source: str,- nr_preset: str,nr_style: str,nr_intensity: float,+ nr_passes: float,local_tone_strength: float,local_structure_strength: float,skin_structure_strength: float,upscaling_factor: float,automatic_mask: str,- dlss_model_preset: str,+ nr_color_strength: float,+ tone_preservation: float,+ face_skin_protection: float,+ grain_preservation: float,+ mask_feather: float,+ shimmer_suppression: float,+ nr_mask: object | None,+ nr_gpu_mode: bool,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,@@ -57,28 +64,34 @@ def start_live(try:factor = float(upscaling_factor)except (TypeError, ValueError):- raise gr.Error("Choose a valid upscaling factor.") from None+ raise gr.Error("Choose a valid scale.") from Nonetry:auto_mask = parse_automatic_mask(automatic_mask)except ValueError as exc:raise gr.Error(str(exc)) from excoptions = LiveOptions(source=selected_source,- nr_preset=nr_preset,nr_style=nr_style,nr_intensity=float(nr_intensity),+ nr_passes=int(nr_passes),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,+ nr_color_strength=float(nr_color_strength),+ tone_preservation=float(tone_preservation),+ face_skin_protection=float(face_skin_protection),+ grain_preservation=float(grain_preservation),+ shimmer_suppression=float(shimmer_suppression),+ mask_feather=int(mask_feather),+ nr_mask=nr_mask,+ nr_gpu_mode=nr_gpu_mode,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:@@ -117,7 +130,7 @@ def refresh_live_status() -> str: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 | "+ parts.append(f"Source: {info.source_fps:.2f} fps. Scene analysis: {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}")@@ -128,7 +141,9 @@ def refresh_live_status() -> str:class LiveTab:source: objectneural: list[object]- model_preset: object+ composition: CompositionWidgets+ mask_state: object+ gpu_mode: objectmax_height: objectsegment: objectopen_mpv: object@@ -137,7 +152,6 @@ class LiveTab:status: objecttarget_fps: objectbuffer: object- guide_quality: objectsource_quality: objectsource_mode: objectlocal_video: object@@ -146,78 +160,58 @@ class LiveTab: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]+ return [*self.neural]-def build_live_tab(settings: UISettings) -> LiveTab:+def build_live_tab(settings: UISettings, gpu_mode_state: object, mask_state: object) -> 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 Neural Rendering's Image and Video modes. "- "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):+ with gr.Row():+ start = gr.Button("Start Live", variant="primary")+ stop = gr.Button("Stop", variant="stop")+ with gr.Column(elem_classes=["neural-controls-unified"]):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)+ composition = build_composition_widgets()with gr.Row():source_quality = gr.Dropdown(choices=[("Auto (follow Max input height)" if height == "Auto" elseheight_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",@@ -226,14 +220,14 @@ def build_live_tab(settings: UISettings) -> LiveTab:lines=10,)tab = LiveTab(- source, neural, model_preset, max_height, segment,- open_mpv, start, stop, status, target_fps, buffer, guide_quality, source_quality,+ source, neural, composition, mask_state, gpu_mode_state, max_height, segment,+ open_mpv, start, stop, status, target_fps, buffer, 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],+ inputs=[source, *neural, mask_state, gpu_mode_state, max_height, segment, open_mpv, target_fps, buffer,+ source_quality, source_mode, local_video],outputs=status,concurrency_limit=1,show_progress="full",diff --git a/src/neural_rendering/composition_ui.py b/src/neural_rendering/composition_ui.pynew file mode 100644index 0000000..4a500f9--- /dev/null+++ b/src/neural_rendering/composition_ui.py@@ -0,0 +1,106 @@+"""Shared Image/Video/Live controls for the post-DLSS compositor."""+from __future__ import annotations++from dataclasses import dataclass++import gradio as gr++from ..core.nr_composition import inspect_nr_mask, mask_status+from ..settings.models import UISettings+++@dataclass(slots=True)+class CompositionWidgets:+ mask: object+ mask_status: object+ detail_only: object+++def build_composition_sliders(settings: UISettings) -> list[object]:+ # Created directly in the parent Column (no gr.Row): each slider spans the+ # full width in one vertical stack. Return order is unchanged.+ color = gr.Slider(+ 0.0, 1.0, value=settings.nr_color_strength, step=0.05, precision=2,+ label="NR Color Strength", buttons=["reset"],+ )+ tone = gr.Slider(+ 0.0, 1.0, value=settings.tone_preservation, step=0.05, precision=2,+ label="Tone Preservation", buttons=["reset"],+ )+ face_skin = gr.Slider(+ 0.0, 1.0, value=settings.face_skin_protection, step=0.05, precision=2,+ label="Face/Skin Protection", buttons=["reset"],+ )+ grain = gr.Slider(+ 0.0, 1.0, value=settings.grain_preservation, step=0.05, precision=2,+ label="Grain Preservation", buttons=["reset"],+ )+ feather = gr.Slider(+ 0, 128, value=settings.mask_feather, step=1, precision=0,+ label="Mask Feather (output pixels)", buttons=["reset"],+ )+ return [color, tone, face_skin, grain, feather]+++def build_composition_widgets() -> CompositionWidgets:+ detail_only = gr.Button(+ "Detail-Only",+ size="sm",+ variant="secondary",+ )+ mask = gr.Image(+ label="Custom NR Mask",+ type="filepath",+ # Preserve the uploaded bytes and original filename for EXIF/animation+ # handling plus report hashing; decoding/conversion belongs to our mask+ # preparation step, not Gradio's image preprocessor.+ image_mode=None,+ sources=["upload"],+ height=180,+ )+ status = gr.Markdown(mask_status(None))+ return CompositionWidgets(mask, status, detail_only)+++def _select_mask(path: str | None):+ try:+ selection = inspect_nr_mask(path)+ except ValueError as exc:+ raise gr.Error(str(exc)) from exc+ from ..settings.ui import persist_nr_mask++ persist_nr_mask(selection)+ value = selection.state() if selection is not None else None+ display_path = selection.path if selection is not None else None+ status = mask_status(selection)+ return value, display_path, display_path, display_path, status, status, status+++def _clear_mask():+ return _select_mask(None)+++def bind_composition_mask_events(+ mask_state: object,+ image_widgets: CompositionWidgets,+ video_widgets: CompositionWidgets,+ live_widgets: CompositionWidgets,+) -> None:+ widgets = (image_widgets, video_widgets, live_widgets)+ masks = [item.mask for item in widgets]+ statuses = [item.mask_status for item in widgets]+ outputs = [mask_state, *masks, *statuses]+ for item in widgets:+ item.mask.upload(+ _select_mask,+ inputs=item.mask,+ outputs=outputs,+ queue=False,+ show_progress="hidden",+ )+ item.mask.clear(+ _clear_mask,+ outputs=outputs,+ queue=False,+ show_progress="hidden",+ )diff --git a/src/neural_rendering/image/batch.py b/src/neural_rendering/image/batch.pyindex 107f8cd..d1c300d 100644--- a/src/neural_rendering/image/batch.py+++ b/src/neural_rendering/image/batch.py@@ -3,8 +3,8 @@ from __future__ import annotationsimport osimport timefrom contextlib import suppress-from concurrent.futures import ThreadPoolExecutor, TimeoutError-from dataclasses import replace+from concurrent.futures import Future, ThreadPoolExecutor, TimeoutError+from dataclasses import dataclass, replacefrom pathlib import Pathfrom typing import Callable, Iterable@@ -17,15 +17,46 @@ from ...core.gpu_selection import resolve_runtime_ai_gpufrom ...core.jobs import Cancelled, JobController, active_jobfrom ...core.render_metadata import prepare_render_notefrom ...core.naming import output_filename, validate_rename-from ...core.paths import LOGS, OUTPUTS+from ...core.paths import OUTPUTSfrom ...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 decode_image+from .decoder import _DecodedImage, decode_imagefrom .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+from .models import (+ IMAGE_EXTENSIONS, IMAGE_FORMATS, ImageBatchResult, ImageConversionFailure,+ ImageConversionOptions, ImageConversionResult,+)+from .reports import (+ IncrementalImageArchive, _ImageReportData, _SessionReportData, _build_manifest,+ _report_data, _write_report, snapshot_session, update_report_performance,+)+++@dataclass(slots=True)+class _OutputTask:+ index: int+ path: Path+ output: Path+ processed: np.ndarray+ metadata: dict[str, object]+ report_data: _ImageReportData+ session_data: _SessionReportData+ evidence: dict[str, object]+ gpu: dict[str, object]+ input_width: int+ input_height: int+ render_width: int+ render_height: int+ output_width: int+ output_height: int+ resize_method: str+ memory_path: str+ bridge_status: dict[str, object]+ source_warnings: list[str]+ has_transparency: bool+ timings: dict[str, float]def _validate_options(options: ImageConversionOptions) -> ImageConversionOptions:@@ -53,6 +84,118 @@ def _output_path(source, output_format, stamp, index, rename_mode, custom_suffix)+def _timed_decode(path: Path) -> tuple[_DecodedImage, float]:+ started = time.monotonic()+ decoded = decode_image(path)+ return decoded, time.monotonic() - started+++def _finalize_output(+ task: _OutputTask,+ options: ImageConversionOptions,+ reporter: BatchProgress,+ controller: JobController,+ generate_previews: bool,+ archive: IncrementalImageArchive | None,+) -> ImageConversionResult:+ """CPU/I/O stage. Runs on exactly one bounded output worker."""+ output_file = None+ try:+ if controller.cancel.is_set():+ raise Cancelled("Image rendering stopped by user.")+ reporter.advance(task.index, .80, "Encoding image")+ output_file = OutputFile(task.output)+ metadata_diagnostics: dict = {}+ render_note = prepare_render_note(options, metadata_diagnostics)+ warnings = _encode_image(+ output_file.temporary,+ task.processed,+ options,+ task.metadata,+ generate_preview=generate_previews,+ preview_path=task.output,+ render_note=render_note,+ metadata_diagnostics=metadata_diagnostics,+ controller=controller,+ has_transparency=task.has_transparency,+ timings=task.timings,+ )+ warning = metadata_diagnostics.get("warning")+ if warning and warning not in warnings:+ warnings.append(warning)+ if controller.cancel.is_set():+ raise Cancelled("Image rendering stopped by user.")++ reporter.advance(task.index, .95, "Writing diagnostic report")+ result = ImageConversionResult(+ input_path=str(task.path),+ output_path=str(task.output),+ report_path="",+ elapsed_seconds=reporter.elapsed(task.index),+ gpu=str(task.gpu["display_name"]),+ input_width=task.input_width,+ input_height=task.input_height,+ render_width=task.render_width,+ render_height=task.render_height,+ output_width=task.output_width,+ output_height=task.output_height,+ upscaling_factor=float(options.upscaling_factor),+ output_format=options.output_format,+ neural_dimensions={"width": task.render_width, "height": task.render_height},+ resize_method=task.resize_method,+ memory_path=task.memory_path,+ bridge_status=task.bridge_status,+ warnings=[*task.source_warnings, *warnings],+ timings=task.timings,+ )+ # Keep the externally reported GPU name identical to the selected GPU.+ # The batch caller replaces the temporary value below before report write.+ report_started = time.monotonic()+ result.report_path = _write_report(+ result, options, task.report_data,+ task.gpu,+ task.session_data, task.evidence,+ metadata_diagnostics=metadata_diagnostics,+ )+ task.timings["report"] = time.monotonic() - report_started+ if controller.cancel.is_set():+ raise Cancelled("Image rendering stopped by user.")++ publish_started = time.monotonic()+ output_file.publish()+ task.timings["publish"] = time.monotonic() - publish_started++ # Archive work is deliberately after publication: cancellation or archive+ # failure must never roll back a valid completed image.+ if archive is not None:+ archive_started = time.monotonic()+ try:+ archive.add(task.output)+ except Cancelled:+ pass+ task.timings["archive"] = time.monotonic() - archive_started++ result.elapsed_seconds = reporter.elapsed(task.index)+ task.timings["total"] = result.elapsed_seconds+ with suppress(Exception):+ update_report_performance(result)+ reporter.complete(+ task.index, str(task.output),+ "; ".join([*result.warnings, f"Report: {result.report_path}"]),+ )+ return result+ except Exception:+ if output_file is not None:+ output_file.cleanup(rollback=True)+ preview = take_image_preview(task.output)+ if preview is not None:+ preview.close()+ raise+ finally:+ if output_file is not None:+ output_file.cleanup()++def convert_images(input_paths: Iterable[str | os.PathLike[str]],options: ImageConversionOptions | None = None,@@ -68,130 +211,247 @@ def convert_images(raise ValueError("Choose at least one image.")controller = controller or JobController()reporter = BatchProgress(paths, on_item_update, progress)- successes, failures = [], []- session = None+ successes: list[ImageConversionResult] = []+ failures: list[ImageConversionFailure] = []+ session: DLSSFrameSession | None = Nonestamp = time.strftime("%Y%m%d-%H%M%S") + f"-{time.time_ns() % 1_000_000:06d}"+ archive: IncrementalImageArchive | None = None+ pending: tuple[int, Path, Future[ImageConversionResult]] | None = None++ def collect_pending() -> None:+ nonlocal pending+ if pending is None:+ return+ index, path, future = pending+ pending = None+ try:+ result = future.result()+ except Exception as exc:+ cancelled_item = isinstance(exc, Cancelled) or controller.cancel.is_set()+ failures.append(ImageConversionFailure(str(path), str(exc)))+ reporter.fail(index, exc, cancelled=cancelled_item)+ if cancelled_item:+ controller.stop()+ else:+ # Output tasks are submitted in input order and only one may be+ # outstanding, so appending here preserves stable result ordering.+ successes.append(result)+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 create_zip:+ archive = IncrementalImageArchive(+ destination / f"DLSS5_IMAGE_BATCH_{stamp}.zip", controller,+ )+ with (+ active_job(controller),+ ThreadPoolExecutor(max_workers=1, thread_name_prefix="dlss5-image-decode") as decoder,+ ThreadPoolExecutor(max_workers=1, thread_name_prefix="dlss5-image-output") as output_worker,+ ):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)factor, mode = resolve_upscaling_mode(options.upscaling_factor)native = resolve_native_settings(options)- next_decode = None- session_size = None+ next_decode: Future[tuple[_DecodedImage, float]] | None = None+ session_size: tuple[int, int] | None = None+ session_evidence: dict[str, object] | None = Nonesent = 0try:for index, path in enumerate(paths):if controller.cancel.is_set():breakreporter.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+ output = _output_path(+ path, options.output_format, stamp, index,+ options.rename_mode, options.custom_suffix, output_dir=destination,+ )+ decoded = None+ render_rgba = None+ processed = None+ timings: dict[str, float] = {}try:- future = next_decode or decoder.submit(decode_image, path)+ future = next_decode or decoder.submit(_timed_decode, path)next_decode = Nonewhile True:if controller.cancel.is_set():future.cancel()raise Cancelled("Image rendering stopped by user.")try:- decoded = future.result(timeout=.1)+ decoded, timings["decode"] = future.result(timeout=.1)breakexcept TimeoutError:continuedel future- # Only one future image may be decoded ahead of the active file.+ # Decode only one future image ahead to keep memory bounded.if index + 1 < len(paths):- next_decode = decoder.submit(decode_image, paths[index + 1])+ next_decode = decoder.submit(_timed_decode, 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)+ 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,+ )dimensions = (output_width, output_height)if session is not None and dimensions != session_size:+ close_started = time.monotonic()session.close()+ timings["previous_session_close"] = time.monotonic() - close_startedsession = None+ session_evidence = None+if session is None:- reporter.advance(index, .1, "Starting DLSS")+ reporter.advance(index, .10, "Starting D3D12/NGX bridge")+ setup_started = time.monotonic()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,+ 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,+ composition_mask=options.nr_mask,+ gpu=gpu,+ runtime_bundle=prepared.runtime_bundle,+ controller=controller,)+ timings["dlss_setup"] = time.monotonic() - setup_startedsession_size, sent = dimensions, 0- motion = np.zeros((session.render_height, session.render_width, 2), dtype=np.float16)+ session_evidence = None+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)+ prep_started = time.monotonic()+ render_rgba = resize_fit(+ decoded.rgba, session.render_width, session.render_height,+ )+ timings["render_prepare"] = time.monotonic() - prep_started++ dlss_started = time.monotonic()+ processed, _pts = session.process(+ index=sent, rgba=render_rgba, reset=True, pts=sent,+ )+ timings["dlss_evaluate"] = time.monotonic() - dlss_startedsent += 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 session_evidence is None:+ evidence_started = time.monotonic()+ # Signed-runtime/setup evidence is session-invariant;+ # direct process() success still validates every image.+ session_evidence = verify_feature_18(+ session.bridge_logs, session.structured_status(),+ )+ timings["feature18_verify"] = time.monotonic() - evidence_started+ else:+ timings["feature18_verify"] = 0.0++ alpha_started = time.monotonic()+ if decoded.alpha is None:+ processed[..., 3] = 255+ elif decoded.alpha.shape == dimensions[::-1]:+ processed[..., 3] = decoded.alpha+ else:+ processed[..., 3] = cv2.resize(+ decoded.alpha, dimensions, interpolation=cv2.INTER_LANCZOS4,+ )+ timings["alpha"] = time.monotonic() - alpha_started++ session_data = snapshot_session(session)+ evidence_snapshot = dict(session_evidence)+ render_width = session.render_width+ render_height = session.render_height+ resize_method = "none" if factor == 1.0 else "lanczos"+ memory_path = session.diagnostics.memory_path+ bridge_status = session.structured_status()++ # Preserve the old invariant that the final image is not+ # published if native streaming completion fails.if index == len(paths) - 1:+ close_started = time.monotonic()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],- )- result.report_path = _write_report(result, options, _report_data(decoded), gpu, session, evidence,- metadata_diagnostics=metadata_diagnostics)+ timings["session_close"] = time.monotonic() - close_started+ session = None+ session_size = None+ session_evidence = None++ # This wait happens only after the current GPU evaluation,+ # overlapping image N CPU/I/O with image N+1 DLSS work while+ # bounding retained full-resolution outputs to one pending item.+ collect_pending()if controller.cancel.is_set():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}"]))++ task = _OutputTask(+ index=index,+ path=path,+ output=output,+ processed=processed,+ metadata=decoded.metadata,+ report_data=_report_data(decoded),+ session_data=session_data,+ evidence=evidence_snapshot,+ gpu=dict(gpu),+ input_width=width,+ input_height=height,+ render_width=render_width,+ render_height=render_height,+ output_width=output_width,+ output_height=output_height,+ resize_method=resize_method,+ memory_path=memory_path,+ bridge_status=bridge_status,+ source_warnings=list(decoded.warnings),+ has_transparency=decoded.alpha is not None,+ timings=timings,+ )+ pending = (+ index,+ path,+ output_worker.submit(+ _finalize_output, task, options, reporter, controller,+ generate_previews, archive,+ ),+ )+ # Ownership of the full output moves to the worker.+ processed = Noneexcept 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()+ cancelled_item = isinstance(exc, Cancelled) or controller.cancel.is_set()if session is not None:session.abort()- if not cancelled:+ if not cancelled_item: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,+ operation="image-render",+ source=str(path),+ error=exc,+ gpu=gpu,+ runtime_bundle=prepared.runtime_bundle,+ bridge_status=session.structured_status(),+ bridge_log=session.bridge_logs,)- exc = RuntimeError(f"{exc}\nDiagnostic report: {report}")+ exc = RuntimeError(f"{exc}\nDetails: {report}")session = None+ session_size = None+ session_evidence = Nonefailures.append(ImageConversionFailure(str(path), str(exc)))- reporter.fail(index, exc, cancelled=cancelled)- if cancelled:+ reporter.fail(index, exc, cancelled=cancelled_item)+ if cancelled_item:controller.stop()breakfinally:- if output_file is not None:- output_file.cleanup()- decoded = processed = render_rgba = None+ decoded = None+ render_rgba = None+ processed = None++ # Drain the final output task. This does not reduce GPU overlap+ # because there is no subsequent image to process.+ collect_pending()if session is not None and not session.closed:if controller.cancel.is_set():session.abort()@@ -202,20 +462,29 @@ def convert_images(next_decode.cancel()if session is not None and not session.closed:session.abort()+ # ThreadPoolExecutor waits for a running output task; collect it+ # before leaving so its success/failure reaches the manifest.+ collect_pending()+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,++ zip_path = archive.finish(cancelled=cancelled) if archive is not None else None+ archive_error = archive.error if archive is not None else None+ manifest = _build_manifest(+ stamp, options, successes, failures, cancelled,+ output_dir=destination,+ batch_diagnostics=reporter.diagnostics(final=True),+ archive_error=archive_error,)- cancelled = controller.cancel.is_set()reporter.finish(cancelled=cancelled, manifest_path=manifest)return ImageBatchResult(successes, failures, cancelled, manifest, zip_path)except BaseException as exc:+ if archive is not None and not archive.closed:+ archive.abort()reporter.finish(cancelled=controller.cancel.is_set(), error=str(exc))raisediff --git a/src/neural_rendering/image/decoder.py b/src/neural_rendering/image/decoder.pyindex f9d6872..e77988e 100644--- a/src/neural_rendering/image/decoder.py+++ b/src/neural_rendering/image/decoder.py@@ -1,7 +1,9 @@from __future__ import annotations+import hashlibimport ioimport os+import tempfilefrom dataclasses import dataclassfrom functools import lru_cachefrom pathlib import Path@@ -12,19 +14,29 @@ import rawpyimport resvg_pyfrom PIL import Image, ImageCms, ImageOps+from ...core.paths import GRADIO_TEMPfrom .models import RAW_EXTENSIONSpillow_heif.register_heif_opener()Image.MAX_IMAGE_PIXELS = 100_000_000+# Formats modern Chromium-based Gradio clients can display without a display+# conversion. Animated raster inputs are still converted below so the preview+# matches the processing pipeline's first-frame/page semantics.+_BROWSER_IMAGE_EXTENSIONS = {".png", ".jpg", ".jpeg", ".webp", ".avif", ".bmp"}++@dataclass(slots=True)class _DecodedImage:rgba: np.ndarray- alpha: np.ndarray+ # None means the source is fully opaque. Avoid allocating/resampling a full+ # 8-bit alpha plane that contains only 255 values.+ alpha: np.ndarray | Nonedecoder: strmetadata: dict[str, object]warnings: list[str]+def _orientation_swaps_dimensions(image: Image.Image) -> bool:try:return int(image.getexif().get(274, 1)) in {5, 6, 7, 8}@@ -66,15 +78,26 @@ def probe_image(path: str | os.PathLike[str]) -> tuple[int, int]:return width, height+@lru_cache(maxsize=1)+def _srgb_profile() -> ImageCms.ImageCmsProfile:+ return ImageCms.ImageCmsProfile(ImageCms.createProfile("sRGB"))++@lru_cache(maxsize=1)def _srgb_profile_bytes() -> bytes:- return ImageCms.ImageCmsProfile(ImageCms.createProfile("sRGB")).tobytes()+ return _srgb_profile().tobytes()def initialize_image_runtime() -> None:"""Initialize reusable image color-management state during app preparation."""+ _srgb_profile()_srgb_profile_bytes()++def _has_alpha(image: Image.Image, info: dict[str, object]) -> bool:+ return image.mode in {"LA", "RGBA"} or (image.mode == "P" and "transparency" in info)++def decode_image(path: str | os.PathLike[str]) -> _DecodedImage:source = Path(path).resolve()image, decoder = _open_pillow_source(source)@@ -87,38 +110,45 @@ def decode_image(path: str | os.PathLike[str]) -> _DecodedImage:image.seek(0)original_mode = image.modeinfo = dict(image.info)+ source_format = image.format or source.suffix.lstrip(".").upper()image = ImageOps.exif_transpose(image)image.load()if original_mode not in {"1", "L", "LA", "P", "RGB", "RGBA", "CMYK"}:warnings.append(f"Converted {original_mode} source data to 8-bit SDR sRGB.")- has_alpha = image.mode in {"LA", "RGBA"} or (- image.mode == "P" and "transparency" in info- )- alpha_image = (- image.convert("RGBA").getchannel("A")- if has_alpha- else Image.new("L", image.size, 255)- )- rgb_image = image.convert("RGB")+ has_alpha = _has_alpha(image, info)icc_profile = info.get("icc_profile")if isinstance(icc_profile, bytes) and icc_profile:+ # Color transforms operate on RGB only; preserve alpha independently.+ alpha_image = image.convert("RGBA").getchannel("A") if has_alpha else None+ rgb_image = image.convert("RGB")try:source_profile = ImageCms.ImageCmsProfile(io.BytesIO(icc_profile))rgb_image = ImageCms.profileToProfile(rgb_image,source_profile,- ImageCms.createProfile("sRGB"),+ _srgb_profile(),outputMode="RGB",)except (ImageCms.PyCMSError, OSError, ValueError) as exc:warnings.append(f"Embedded color profile could not be applied ({exc}); assumed sRGB.")+ rgb = np.asarray(rgb_image, dtype=np.uint8)+ rgba = np.empty((*rgb.shape[:2], 4), dtype=np.uint8)+ rgba[..., :3] = rgb+ if alpha_image is None:+ rgba[..., 3] = 255+ alpha = None+ else:+ alpha = np.ascontiguousarray(np.asarray(alpha_image, dtype=np.uint8))+ rgba[..., 3] = alpha+ else:+ # Common sRGB paths need only one Pillow conversion and one contiguous+ # NumPy allocation, instead of RGB + alpha + RGBA intermediates.+ rgba_image = image.convert("RGBA")+ rgba = np.array(rgba_image, dtype=np.uint8, copy=True)+ rgba_image.close()+ alpha = np.ascontiguousarray(rgba[..., 3]) if has_alpha else None- rgb = np.asarray(rgb_image, dtype=np.uint8)- alpha = np.asarray(alpha_image, dtype=np.uint8)- rgba = np.empty((*rgb.shape[:2], 4), dtype=np.uint8)- rgba[..., :3] = rgb- rgba[..., 3] = alphaexif = image.getexif()if exif:exif[274] = 1@@ -128,17 +158,123 @@ def decode_image(path: str | os.PathLike[str]) -> _DecodedImage:"dpi": info.get("dpi"),"xmp": info.get("xmp"),"source_mode": original_mode,- "source_format": image.format or source.suffix.lstrip(".").upper(),+ "source_format": source_format,"frame_count": frame_count,}- return _DecodedImage(- np.ascontiguousarray(rgba),- np.ascontiguousarray(alpha),- decoder,- metadata,- warnings,- )+ return _DecodedImage(rgba, alpha, decoder, metadata, warnings)+ finally:+ image.close()+ if opened_image is not image:+ opened_image.close()+++def decode_image_preview(+ path: str | os.PathLike[str], max_size: tuple[int, int] | None = (1200, 900)+) -> Image.Image:+ """Decode a UI preview without constructing the full render-time NumPy payload.++ ``max_size=None`` preserves the decoded source dimensions. This is used only+ by the opt-in full-size Gradio preview path.+ """+ source = Path(path).resolve()+ suffix = source.suffix.lower()+ if suffix in RAW_EXTENSIONS:+ with rawpy.imread(str(source)) as raw:+ array = raw.postprocess(+ use_camera_wb=True,+ output_color=rawpy.ColorSpace.sRGB,+ output_bps=8,+ half_size=max_size is not None,+ )+ image = Image.fromarray(array, mode="RGB")+ if max_size is not None:+ image.thumbnail(max_size, Image.Resampling.BILINEAR)+ return image.convert("RGBA")++ image, _decoder = _open_pillow_source(source)+ opened_image = image+ try:+ if int(getattr(image, "n_frames", 1) or 1) > 1:+ image.seek(0)+ if max_size is not None:+ # JPEG can ask its decoder for a reduced-resolution IDCT before loading.+ with __import__("contextlib").suppress(Exception):+ image.draft("RGB", max_size)+ info = dict(image.info)+ image = ImageOps.exif_transpose(image)+ if max_size is not None:+ image.thumbnail(max_size, Image.Resampling.BILINEAR)+ icc_profile = info.get("icc_profile")+ if isinstance(icc_profile, bytes) and icc_profile:+ has_alpha = _has_alpha(image, info)+ alpha = image.convert("RGBA").getchannel("A") if has_alpha else None+ rgb = image.convert("RGB")+ try:+ rgb = ImageCms.profileToProfile(+ rgb,+ ImageCms.ImageCmsProfile(io.BytesIO(icc_profile)),+ _srgb_profile(),+ outputMode="RGB",+ )+ except (ImageCms.PyCMSError, OSError, ValueError):+ pass+ result = rgb.convert("RGBA")+ if alpha is not None:+ result.putalpha(alpha)+ return result+ return image.convert("RGBA")finally:image.close()if opened_image is not image:opened_image.close()+++def _direct_browser_image(source: Path) -> bool:+ suffix = source.suffix.lower()+ if suffix == ".svg":+ return True+ if suffix not in _BROWSER_IMAGE_EXTENSIONS:+ return False+ try:+ with Image.open(source) as image:+ return int(getattr(image, "n_frames", 1) or 1) <= 1+ except Exception:+ return False+++def full_size_image_preview_path(path: str | os.PathLike[str]) -> str:+ """Return a browser-displayable full-resolution path for an image.++ Browser-native, single-frame formats are served directly so no pixels are+ copied or re-encoded. RAW/HEIF/TIFF/animated/other Pillow-readable sources+ are converted once to a lossless full-resolution PNG in the app-local+ Gradio temp directory.+ """+ source = Path(path).resolve()+ if not source.is_file():+ raise FileNotFoundError(source)+ if _direct_browser_image(source):+ return str(source)++ stat = source.stat()+ identity = f"{source}|{stat.st_dev}|{stat.st_ino}|{stat.st_size}|{stat.st_mtime_ns}"+ digest = hashlib.sha256(identity.encode("utf-8", "surrogatepass")).hexdigest()[:24]+ GRADIO_TEMP.mkdir(parents=True, exist_ok=True)+ destination = GRADIO_TEMP / f"dlss5-fullsize-source-{digest}.png"+ if destination.is_file():+ return str(destination.resolve())++ image = decode_image_preview(source, None)+ handle, raw_temp = tempfile.mkstemp(+ prefix="dlss5-fullsize-source-write-", suffix=".png", dir=GRADIO_TEMP+ )+ os.close(handle)+ temporary = Path(raw_temp)+ try:+ image.save(temporary, format="PNG", optimize=False, compress_level=1)+ os.replace(temporary, destination)+ finally:+ image.close()+ temporary.unlink(missing_ok=True)+ return str(destination.resolve())+diff --git a/src/neural_rendering/image/encoder.py b/src/neural_rendering/image/encoder.pyindex 7bb6056..3a8b02a 100644--- a/src/neural_rendering/image/encoder.py+++ b/src/neural_rendering/image/encoder.py@@ -1,6 +1,8 @@from __future__ import annotations+import atexitimport os+import tempfileimport threadingimport timeimport warnings as python_warnings@@ -12,45 +14,171 @@ from PIL import Imagefrom .models import ImageConversionOptionsfrom ...core.jobs import Cancelled+from ...core.paths import GRADIO_TEMPfrom ...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()+# Keep UI previews bounded in RAM. Older previews spill as already-downscaled PNGs+# so large batches never have to reopen and resize their full-resolution outputs.+_PREVIEW_CACHE_BYTES = 256 * 1024 * 1024+_preview_cache: OrderedDict[str, tuple[Image.Image, int]] = OrderedDict()+_preview_spill: dict[str, Path] = {}+_preview_cache_bytes = 0_preview_cache_lock = threading.Lock()++def _clear_preview_cache() -> None:+ global _preview_cache_bytes+ with _preview_cache_lock:+ while _preview_cache:+ _key, (image, _size) = _preview_cache.popitem()+ image.close()+ for path in _preview_spill.values():+ _delete_spill(path)+ _preview_spill.clear()+ _preview_cache_bytes = 0+++def _delete_spill(path: Path | None) -> None:+ if path is not None:+ path.unlink(missing_ok=True)+++atexit.register(_clear_preview_cache)++def take_image_preview(output_path: str | os.PathLike[str]) -> Image.Image | None:- """Return and remove a UI thumbnail created from the rendered frame in memory."""+ """Return and remove a pre-generated UI thumbnail for a rendered output."""+ global _preview_cache_byteskey = str(Path(output_path).resolve())with _preview_cache_lock:- return _preview_cache.pop(key, None)+ cached = _preview_cache.pop(key, None)+ spill = _preview_spill.pop(key, None)+ if cached is not None:+ image, size = cached+ _preview_cache_bytes -= size+ return image+ if spill is None:+ return None+ try:+ with Image.open(spill) as image:+ image.load()+ return image.copy()+ finally:+ _delete_spill(spill)-def _remember_image_preview(- output_path: Path, rgba: np.ndarray, output_format: str-) -> None:+def _spill_preview(key: str, image: Image.Image) -> None:+ GRADIO_TEMP.mkdir(parents=True, exist_ok=True)+ handle, raw_path = tempfile.mkstemp(+ prefix="dlss5-preview-", suffix=".png", dir=GRADIO_TEMP+ )+ os.close(handle)+ path = Path(raw_path)+ try:+ image.save(path, format="PNG", optimize=False, compress_level=1)+ except Exception:+ path.unlink(missing_ok=True)+ return+ previous = _preview_spill.pop(key, None)+ _delete_spill(previous)+ _preview_spill[key] = path+++def make_image_preview(+ rgba: np.ndarray, output_format: str, has_transparency: bool | None = None,+) -> Image.Image:+ """Build the same gallery thumbnail used by a completed image render.++ The preview is derived directly from the processed RGBA pixels, before file+ encoding. JPEG is the one format-specific exception: production JPEG output+ composites transparency over white, so the gallery preview mirrors that.+ """image = Image.fromarray(rgba, mode="RGBA")try:if output_format == "JPEG":- background = Image.new("RGBA", image.size, (255, 255, 255, 255))- background.alpha_composite(image)- preview = background+ if has_transparency is None:+ has_transparency = bool(np.any(rgba[..., 3] != 255))+ if has_transparency:+ preview = Image.new("RGBA", image.size, (255, 255, 255, 255))+ preview.alpha_composite(image)+ else:+ preview = image.copy()else:preview = image.copy()- preview.thumbnail((1600, 1200), Image.Resampling.LANCZOS)+ # Preview pixels are not production output; bilinear is dramatically+ # cheaper than Lanczos and is visually sufficient at gallery scale.+ preview.thumbnail((1200, 900), Image.Resampling.BILINEAR)+ return preview+ finally:+ image.close()+++def save_full_size_image_preview(+ rgba: np.ndarray, output_format: str, has_transparency: bool | None = None,+) -> str:+ """Write processed preview pixels as a full-resolution browser-safe PNG.++ This avoids handing a full-size PIL object to Gradio (which would create an+ additional cache encode). JPEG preview semantics still composite transparent+ pixels over white, matching the production JPEG path.+ """+ GRADIO_TEMP.mkdir(parents=True, exist_ok=True)+ handle, raw_path = tempfile.mkstemp(+ prefix="dlss5-fullsize-processed-", suffix=".png", dir=GRADIO_TEMP+ )+ os.close(handle)+ path = Path(raw_path)+ source = Image.fromarray(rgba, mode="RGBA")+ display = source+ try:+ if output_format == "JPEG":+ if has_transparency is None:+ has_transparency = bool(np.any(rgba[..., 3] != 255))+ if has_transparency:+ display = Image.new("RGBA", source.size, (255, 255, 255, 255))+ display.alpha_composite(source)+ display.save(path, format="PNG", optimize=False, compress_level=1)+ return str(path.resolve())+ except Exception:+ path.unlink(missing_ok=True)+ raise+ finally:+ if display is not source:+ display.close()+ source.close()+++def _remember_image_preview(+ output_path: Path, rgba: np.ndarray, output_format: str,+ has_transparency: bool | None = None,+) -> None:+ global _preview_cache_bytes+ preview = make_image_preview(rgba, output_format, has_transparency)+ try:key = str(output_path.resolve())+ size = preview.width * preview.height * 4with _preview_cache_lock:previous = _preview_cache.pop(key, None)if previous is not None:- previous.close()- _preview_cache[key] = preview- while len(_preview_cache) > _PREVIEW_CACHE_LIMIT:- _unused_key, unused = _preview_cache.popitem(last=False)- unused.close()+ old, old_size = previous+ _preview_cache_bytes -= old_size+ old.close()+ old_spill = _preview_spill.pop(key, None)+ _delete_spill(old_spill)+ _preview_cache[key] = (preview, size)+ _preview_cache_bytes += size+ preview = None+ while _preview_cache_bytes > _PREVIEW_CACHE_BYTES and _preview_cache:+ old_key, (old_image, old_size) = _preview_cache.popitem(last=False)+ _preview_cache_bytes -= old_size+ _spill_preview(old_key, old_image)+ old_image.close()finally:- image.close()+ if preview is not None:+ preview.close()def _encode_image(@@ -59,18 +187,31 @@ def _encode_image(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,+ render_note: str | None = None, metadata_diagnostics: dict | None = None,+ controller=None, has_transparency: bool | None = None, timings: dict[str, float] | None = None,) -> list[str]:- warnings = save_image(output, rgba, options, metadata, render_note=render_note,- metadata_diagnostics=metadata_diagnostics, controller=controller)+ started = time.monotonic()+ warnings = save_image(+ output, rgba, options, metadata, render_note=render_note,+ metadata_diagnostics=metadata_diagnostics, controller=controller,+ has_transparency=has_transparency, timings=timings,+ )+ if timings is not None:+ timings["encode"] = time.monotonic() - startedtry:if generate_preview:- _remember_image_preview(preview_path or output, rgba, options.output_format)+ preview_started = time.monotonic()+ _remember_image_preview(+ preview_path or output, rgba, options.output_format, has_transparency+ )+ if timings is not None:+ timings["preview"] = time.monotonic() - preview_startedexcept Exception:# A UI convenience must never invalidate an otherwise correct output.passreturn warnings+def _metadata_save_args(metadata: dict[str, object], output_format: str) -> dict[str, object]:args: dict[str, object] = {}if metadata.get("icc_profile"):@@ -89,32 +230,43 @@ def save_image(rgba: np.ndarray,options: ImageConversionOptions,metadata: dict[str, object],- *, render_note: str | None = None, metadata_diagnostics: dict | None = None, controller=None,+ *, render_note: str | None = None, metadata_diagnostics: dict | None = None,+ controller=None, has_transparency: bool | None = None, timings: dict[str, float] | None = None,) -> list[str]:output_format = options.output_format- image = Image.fromarray(rgba, mode="RGBA")+ source_image = Image.fromarray(rgba, mode="RGBA")+ image = source_imagewarnings: list[str] = []args = _metadata_save_args(metadata, output_format) if options.preserve_metadata else {}if output_format == "PNG":- args.update(optimize=False, compress_level=6)+ # PNG compression effort changes only file size/CPU time, never pixels.+ args.update(optimize=False, compress_level=1)elif output_format == "TIFF":- args.update(compression="tiff_deflate")+ # LZW is lossless and materially lighter on CPU than Deflate here.+ args.update(compression="tiff_lzw")elif output_format == "JPEG":- background = Image.new("RGBA", image.size, (255, 255, 255, 255))- background.alpha_composite(image)- image = background.convert("RGB")+ if has_transparency is None:+ has_transparency = bool(np.any(rgba[..., 3] != 255))+ if has_transparency:+ background = Image.new("RGBA", source_image.size, (255, 255, 255, 255))+ background.alpha_composite(source_image)+ image = background.convert("RGB")+ background.close()+ warnings.append("Transparency was composited over white for JPEG output.")+ else:+ image = source_image.convert("RGB")args.update(quality=int(options.quality),subsampling=0,optimize=False,progressive=False,)- if np.any(rgba[..., 3] != 255):- warnings.append("Transparency was composited over white for JPEG output.")elif output_format == "WebP":- args.update(quality=int(options.quality), method=6)+ # Preserve the requested quality while avoiding maximum encoder effort.+ args.update(quality=int(options.quality), method=4)elif output_format == "AVIF":- args.update(quality=int(options.quality), speed=4)+ # Preserve the requested quality; speed controls encoder effort.+ args.update(quality=int(options.quality), speed=6)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@@ -139,9 +291,13 @@ def save_image(check_cancelled(controller)image.save(temporary, format=output_format, **args)check_cancelled(controller)- # Note-free legacy calls retain their existing validation behavior.+ # Header/container validation deliberately avoids saved.load(), which+ # would decode every output pixel a second time immediately after save.if render_note is not None or metadata_diagnostics is not None:+ verify_started = time.monotonic()_verify_image(temporary, image.size, description if note_requested else None)+ if timings is not None:+ timings["output_verification"] = timings.get("output_verification", 0.0) + time.monotonic() - verify_startedexcept Cancelled:raiseexcept (ValueError, TypeError, KeyError, SyntaxError, OSError, RuntimeError) as exc:@@ -152,14 +308,19 @@ def save_image(warnings.append(embedding_warning(metadata_diagnostics, exc))image.save(temporary, format=output_format, **original_args)check_cancelled(controller)+ verify_started = time.monotonic()_verify_image(temporary, image.size, None)+ if timings is not None:+ timings["output_verification"] = timings.get("output_verification", 0.0) + time.monotonic() - verify_startednote_requested = Falsecheck_cancelled(controller)os.replace(temporary, output)if note_requested:record_embedding(metadata_diagnostics, "embedded", field="EXIF.ImageDescription")finally:- image.close()+ if image is not source_image:+ image.close()+ source_image.close()if temporary.exists():temporary.unlink()return warnings@@ -197,8 +358,9 @@ def _exif_fields(exif: Image.Exif) -> dict:def _verify_image(path: Path, size: tuple[int, int], description: str | None) -> None:+ # Image.open() parses the container header lazily. Accessing size and EXIF does+ # not decode the raster payload, which avoids a redundant full-image pass.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:diff --git a/src/neural_rendering/image/models.py b/src/neural_rendering/image/models.pyindex 16548ff..c1d59e5 100644--- a/src/neural_rendering/image/models.py+++ b/src/neural_rendering/image/models.py@@ -17,19 +17,25 @@ class ImageConversionOptions:ai_gpu_uuid: str = "auto"nr_style: str = "Default"nr_intensity: float = 1.0+ nr_passes: int = 1local_tone_strength: float = 1.0local_structure_strength: float = 1.0skin_structure_strength: float = -1.0+ nr_color_strength: float = 1.0+ tone_preservation: float = 0.0+ face_skin_protection: float = 0.0+ grain_preservation: float = 0.0+ mask_feather: int = 0+ nr_mask: object | None = Noneupscaling_factor: float = 1.0output_format: str = "PNG"quality: int = 95preserve_metadata: bool = Truewarmup_frames: int = 0- nr_preset: str = "Default"automatic_mask: bool = False+ nr_gpu_mode: bool = Truerename_mode: str = "Auto"- custom_suffix: str = "_DLSS5"- dlss_model_preset: str = "Default"+ custom_suffix: str = "_Neural_Rendering"def neural_options(self) -> "ImageConversionOptions":# Image already carries every shared neural-rendering field needed by core.runtime.@@ -50,11 +56,13 @@ class ImageConversionResult:output_width: intoutput_height: intupscaling_factor: float- dlss_mode: stroutput_format: str- dlss_model_preset: str = "Default"- applied_dlss_model_preset: int = 0+ neural_dimensions: dict[str, int] | None = None+ resize_method: str = "none"+ memory_path: str = "host_staging"+ bridge_status: dict | None = Nonewarnings: list[str] = field(default_factory=list)+ timings: dict[str, float] = field(default_factory=dict)@dataclass(slots=True)diff --git a/src/neural_rendering/image/preview.py b/src/neural_rendering/image/preview.pynew file mode 100644index 0000000..a5d80a5--- /dev/null+++ b/src/neural_rendering/image/preview.py@@ -0,0 +1,148 @@+from __future__ import annotations++import time+from contextlib import suppress+from dataclasses import replace+from pathlib import Path+from typing import Callable++import cv2+import numpy as np+from PIL import Image++from ...core.gpu_selection import resolve_runtime_ai_gpu+from ...core.jobs import Cancelled, JobController, active_job+from ...core.runtime import (+ DLSSFrameSession,+ prepare_runtime,+ resize_fit,+ resolve_native_settings,+ resolve_output_size,+ resolve_upscaling_mode,+ verify_feature_18,+)+from .batch import _validate_options+from .decoder import decode_image+from .encoder import make_image_preview, save_full_size_image_preview+from .models import ImageConversionOptions+++def _update(controller: JobController, progress: Callable[[float, str], None] | None,+ value: float, message: str) -> None:+ if controller.cancel.is_set():+ raise Cancelled("Image preview stopped by user.")+ if progress is not None:+ progress(value, message)+++def render_image_preview(+ input_path: str | Path,+ options: ImageConversionOptions | None = None,+ progress: Callable[[float, str], None] | None = None,+ *,+ controller: JobController | None = None,+ full_size_preview: bool = False,+) -> tuple[Image.Image | str, str]:+ """Run the real single-image feature-18 path without publishing any files."""+ options = _validate_options(replace(options) if options else ImageConversionOptions())+ source = Path(input_path).resolve()+ if not source.is_file():+ raise FileNotFoundError(source)++ started = time.monotonic()+ session: DLSSFrameSession | None = None+ processed = None+ render_rgba = None+ with active_job(controller) as controller:+ try:+ _update(controller, progress, .01, "Decoding image")+ decoded = decode_image(source)+ height, width = decoded.rgba.shape[:2]+ if width < 64 or height < 64:+ raise ValueError(+ f"{source.name} is {width}×{height}; DLSS requires at least 64×64."+ )++ output_width, output_height = resolve_output_size(+ width, height, options.upscaling_factor,+ )+ prepared = prepare_runtime()+ gpu = resolve_runtime_ai_gpu(+ prepared.gpus, prepared.runtime_bundle, options.ai_gpu_uuid,+ )+ factor, mode = resolve_upscaling_mode(options.upscaling_factor)+ native = resolve_native_settings(options)++ _update(controller, progress, .12, "Starting D3D12/NGX bridge")+ 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,+ composition_mask=options.nr_mask,+ gpu=gpu,+ runtime_bundle=prepared.runtime_bundle,+ controller=controller,+ )+ _update(controller, progress, .28, "Processing image")+ render_rgba = resize_fit(+ decoded.rgba, session.render_width, session.render_height,+ )+ processed, _pts = session.process(+ index=0, rgba=render_rgba, reset=True, pts=0,+ )++ _update(controller, progress, .72, "Verifying feature 18")+ verify_feature_18(session.bridge_logs, session.structured_status())++ if decoded.alpha is None:+ processed[..., 3] = 255+ elif decoded.alpha.shape == (output_height, output_width):+ processed[..., 3] = decoded.alpha+ else:+ processed[..., 3] = cv2.resize(+ decoded.alpha,+ (output_width, output_height),+ interpolation=cv2.INTER_LANCZOS4,+ )++ render_width = session.render_width+ render_height = session.render_height+ resize_method = "none" if factor == 1.0 else "lanczos"+ memory_path = session.diagnostics.memory_path+ gpu_name = str(gpu["display_name"])++ _update(controller, progress, .88, "Finalizing Neural Rendering preview")+ session.close()+ session = None++ _update(controller, progress, .94, "Preparing preview")+ if full_size_preview:+ preview = save_full_size_image_preview(+ processed, options.output_format, decoded.alpha is not None,+ )+ else:+ preview = make_image_preview(+ processed, options.output_format, decoded.alpha is not None,+ )+ elapsed = time.monotonic() - started+ status = (+ f"Preview complete: {source.name} | {width}×{height} → "+ f"{output_width}×{output_height} | render {render_width}×{render_height} | "+ f"{resize_method} resize | {memory_path} | GPU: {gpu_name} | "+ f"{elapsed:.2f}s. Feature-18 execution verified.\n"+ "Preview only — no production output image was saved."+ )+ _update(controller, progress, 1.0, "Preview ready")+ return preview, status+ finally:+ if session is not None and not session.closed:+ with suppress(Exception):+ session.abort()+ processed = None+ render_rgba = Nonediff --git a/src/neural_rendering/image/reports.py b/src/neural_rendering/image/reports.pyindex aee59ad..8a33b7b 100644--- a/src/neural_rendering/image/reports.py+++ b/src/neural_rendering/image/reports.py@@ -1,19 +1,19 @@from __future__ import annotations-import json+import osimport zipfile-from dataclasses import asdict, dataclass+from dataclasses import dataclassfrom pathlib import Path-from PIL import TiffImagePlugin--from ...core.paths import LOGS, OUTPUTS+from ...core import app_log+from ...core.paths import OUTPUTSfrom ...core.disk_paths import OutputFilefrom ...core.jobs import Cancelled-from ...core.runtime import DLSSFrameSession, DLSS_MODEL_PRESETS+from ...core.runtime import DLSSFrameSessionfrom .decoder import _DecodedImagefrom .models import ImageConversionFailure, ImageConversionOptions, ImageConversionResult+@dataclass(slots=True)class _ImageReportData:decoder: str@@ -21,84 +21,143 @@ class _ImageReportData:warnings: list[str]+@dataclass(frozen=True, slots=True)+class _SessionReportData:+ minimum_width: int+ minimum_height: int+ maximum_width: int+ maximum_height: int+ setup_result: int+ native_settings: dict[str, int | float]+ runtime_bundle: dict[str, object]+ bridge_status: dict[str, object]+ bridge_log: tuple[str, ...]++def _report_data(decoded: _DecodedImage) -> _ImageReportData:- """Keep diagnostics without retaining the decoded pixel and alpha arrays."""- return _ImageReportData(decoded.decoder, decoded.metadata, decoded.warnings)---def _json_safe_metadata(value: object) -> object:- """Create a report-only JSON-safe copy without changing save metadata."""- if value is None or isinstance(value, (bool, int, float, str)):- return value- if isinstance(value, TiffImagePlugin.IFDRational):- return float(value)- if isinstance(value, dict):- return {str(key): _json_safe_metadata(item) for key, item in value.items()}- if isinstance(value, (list, tuple, set)):- return [_json_safe_metadata(item) for item in value]- if isinstance(value, bytes):- return {"type": "bytes", "length": len(value)}- return str(value)+ """Keep diagnostics without retaining decoded pixel/alpha arrays."""+ return _ImageReportData(decoded.decoder, decoded.metadata, list(decoded.warnings))+++def snapshot_session(session: DLSSFrameSession) -> _SessionReportData:+ """Detach structured report inputs from the live bridge session."""+ return _SessionReportData(+ minimum_width=session.minimum_width,+ minimum_height=session.minimum_height,+ maximum_width=session.maximum_width,+ maximum_height=session.maximum_height,+ setup_result=session.setup_result,+ native_settings=dict(session.native_settings),+ runtime_bundle=dict(session.runtime_bundle),+ bridge_status=session.structured_status(),+ bridge_log=tuple(session.bridge_logs),+ )+def _write_report(result: ImageConversionResult,options: ImageConversionOptions,decoded: _ImageReportData,gpu: dict,- session: DLSSFrameSession,+ session: _SessionReportData,evidence: dict[str, object],*, metadata_diagnostics: dict | None = None,) -> str:- report = {- "status": "success",- "input": result.input_path,- "output": result.output_path,- "options": asdict(options),- "gpu": gpu,- "decoder": decoded.decoder,- "source_metadata": {- key: _json_safe_metadata(value)- for key, value in decoded.metadata.items()- 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": {- "minimum": {"width": session.minimum_width, "height": session.minimum_height},- "maximum": {"width": session.maximum_width, "height": session.maximum_height},- },- "output_dimensions": {"width": result.output_width, "height": result.output_height},- "dlss_mode": result.dlss_mode,- "requested_upscaling_factor": result.upscaling_factor,- "dlss_model_preset": result.dlss_model_preset,- "requested_dlss_model_preset": result.dlss_model_preset,- "requested_dlss_model_preset_code": DLSS_MODEL_PRESETS[- result.dlss_model_preset- ],- "applied_dlss_model_preset": result.applied_dlss_model_preset,- "applied_dlss_model_preset_name": next(- name- for name, code in DLSS_MODEL_PRESETS.items()- if code == result.applied_dlss_model_preset- ),- "pipeline": "renodx-dlssnr-feature18-image",- "feature_id": 18,- "feature_18_confirmed": True,- "ngx_setup_result": f"0x{session.setup_result:08X}",- "carrier_create_result": evidence["carrier_create_result"],- "native_settings": session.native_settings,- "addon_release": session.runtime_bundle["addon"]["release"],- "dlssnr_runtime": session.runtime_bundle["neural_runtime"],- "worker_log": session.worker_logs,- "worker_log_dropped_lines": session.worker_log_dropped_lines,- "dlssnr_evidence": evidence["evidence"],- "elapsed_seconds": result.elapsed_seconds,- }- report_path = LOGS / f"{Path(result.output_path).name}.report.json"- report_path.write_text(json.dumps(report, indent=2), encoding="utf-8")- return str(report_path)+ app_log.info(+ "image-render",+ f"done src={Path(result.input_path).name} out={Path(result.output_path).name} "+ f"elapsed={result.elapsed_seconds:.1f}s",+ )+ return app_log.session_path()+++def update_report_performance(result: ImageConversionResult) -> None:+ """No-op: per-job report files no longer exist (see session log)."""+ return+++class IncrementalImageArchive:+ """Build the uncompressed batch ZIP while later images are on the GPU."""++ def __init__(self, path: Path, controller=None):+ self.path = path+ self.controller = controller+ self.output_file = OutputFile(path)+ self.archive = zipfile.ZipFile(+ self.output_file.temporary, "w", compression=zipfile.ZIP_STORED, allowZip64=True,+ )+ self.closed = False+ self.error: str | None = None+ self.count = 0++ def add(self, source_path: str | os.PathLike[str]) -> None:+ if self.closed or self.error:+ return+ source = Path(source_path)+ try:+ info = zipfile.ZipInfo.from_file(source, arcname=source.name)+ info.compress_type = zipfile.ZIP_STORED+ with source.open("rb") as input_stream, self.archive.open(info, "w", force_zip64=True) as output_stream:+ while True:+ if self.controller is not None and self.controller.cancel.is_set():+ raise Cancelled("ZIP creation cancelled.")+ chunk = input_stream.read(1024 * 1024)+ if not chunk:+ break+ output_stream.write(chunk)+ self.count += 1+ except Cancelled:+ raise+ except Exception as exc:+ # ZIP is a convenience artifact; never turn an already valid image into+ # a failed render because archive I/O failed.+ self.error = str(exc)++ def finish(self, *, cancelled: bool) -> str | None:+ if self.closed:+ return None+ self.closed = True+ try:+ try:+ self.archive.close()+ except Exception as exc:+ self.error = self.error or str(exc)+ if not self.count or cancelled or self.error or (self.controller is not None and self.controller.cancel.is_set()):+ return None+ self.output_file.publish()+ return str(self.path)+ finally:+ self.output_file.cleanup()++ def abort(self) -> None:+ if self.closed:+ return+ self.closed = True+ try:+ try:+ self.archive.close()+ except Exception:+ pass+ finally:+ self.output_file.cleanup(rollback=True)+++def _build_manifest(+ stamp: str,+ options: ImageConversionOptions,+ successes: list[ImageConversionResult],+ failures: list[ImageConversionFailure],+ cancelled: bool,+ *, output_dir: Path | None = None, batch_diagnostics: dict | None = None,+ archive_error: str | None = None,+) -> str:+ status = "cancelled" if cancelled else ("partial" if failures else "success")+ app_log.info(+ "image-batch",+ f"{status} ok={len(successes)} failed={len(failures)}"+ + (f" archive_warning={archive_error}" if archive_error else ""),+ )+ return app_log.session_path()def _build_manifest_and_zip(@@ -110,41 +169,29 @@ def _build_manifest_and_zip(*, 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")+ """Compatibility helper for callers that do not use IncrementalImageArchive."""+ manifest_path = _build_manifest(+ stamp, options, successes, failures, cancelled,+ output_dir=output_dir, batch_diagnostics=batch_diagnostics,+ )if not successes or not create_zip:- return str(manifest_path), None+ return manifest_path, Nonezip_path = (output_dir or OUTPUTS) / f"DLSS5_IMAGE_BATCH_{stamp}.zip"- output_file = OutputFile(zip_path)+ archive = IncrementalImageArchive(zip_path, controller)try:- with zipfile.ZipFile(output_file.temporary, "w", compression=zipfile.ZIP_STORED) as archive:- for item in successes:- 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()+ for item in successes:+ archive.add(item.output_path)+ result = archive.finish(cancelled=cancelled)+ if archive.error:+ manifest_path = _build_manifest(+ stamp, options, successes, failures, cancelled,+ output_dir=output_dir, batch_diagnostics=batch_diagnostics,+ archive_error=archive.error,+ )+ return manifest_path, resultexcept 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:- output_file.cleanup()- return str(manifest_path), str(zip_path)+ archive.abort()+ return _build_manifest(+ stamp, options, successes, failures, True,+ output_dir=output_dir, batch_diagnostics=batch_diagnostics,+ ), Nonediff --git a/src/neural_rendering/image/ui.py b/src/neural_rendering/image/ui.pyindex f39ab4b..7b9c967 100644--- a/src/neural_rendering/image/ui.py+++ b/src/neural_rendering/image/ui.py@@ -7,18 +7,20 @@ from pathlib import Pathimport gradio as grfrom ...core.batch_ui import (BATCH_HEADERS, bind_batch_ui, build_media_clear_button, build_media_select_button,- build_path_controls,+ build_path_controls, build_save_controls,)from PIL import Imagefrom ...core.naming import RENAME_MODES-from ...core.runtime import DLSS_MODEL_PRESETS, NR_PRESETS, NR_STYLES, UPSCALING_MODES+from ...core.runtime import NR_STYLES, UPSCALING_MODESfrom ...settings.models import AUTOMATIC_MASK_CHOICES, UISettings, automatic_mask_choice, parse_automatic_mask-from ...settings.storage import processing_gpu_settings-from .decoder import decode_image+from ...settings.storage import full_size_image_previews_enabled, processing_gpu_settings+from .decoder import decode_image_preview, full_size_image_preview_pathfrom .encoder import take_image_previewfrom .batch import convert_images+from .preview import render_image_previewfrom .models import IMAGE_FORMATS, RAW_EXTENSIONS, ImageConversionOptions+from ..composition_ui import CompositionWidgets, build_composition_sliders, build_composition_widgetsdef rename_suffix_update(mode: str):@@ -29,112 +31,107 @@ UPSCALING_CHOICES = tuple((mode["label"], factor) for factor, mode in UPSCALING_def build_neural_controls(settings: UISettings):- nr_preset = gr.Dropdown(- list(NR_PRESETS), value=settings.nr_preset, label="NR Preset",- info="Experimental content-dependent neural-rendering model hint. Default is recommended."- )nr_style = gr.Radio(list(NR_STYLES), value=settings.nr_style, label="NR Style",- info="Selects the native neural-rendering style.")upscaling_factor = gr.Dropdown(choices=list(UPSCALING_CHOICES), value=settings.upscaling_factor,- label="Upscaling factor", info="Uses NVIDIA's fixed DLSS modes. DLAA keeps source resolution."+ label="Scale",)- with gr.Row():- nr_intensity = gr.Slider(- 0.0, 2.0, value=settings.nr_intensity, step=0.05, precision=2,- label="NR Intensity", info="Overall neural-rendering strength.", buttons=["reset"]- )- local_tone_strength = gr.Slider(- 0.0, 2.0, value=settings.local_tone_strength, step=0.05, precision=2,- label="Local Tone Strength", info="Local tone and contrast enhancement.", buttons=["reset"]- )- with gr.Row():- local_structure_strength = gr.Slider(- 0.0, 2.0, value=settings.local_structure_strength, step=0.05, precision=2,- label="Local Structure Strength", info="Local detail and texture structure.", buttons=["reset"]- )- skin_structure_strength = gr.Slider(- -1.0, 2.0, value=settings.skin_structure_strength, step=0.05, precision=2,- label="Skin Structure Strength", info="Skin-specific structure; -1.00 is the native default.",- buttons=["reset"]- )+ # Each control is created directly in the parent Column (no gr.Row), so+ # every slider spans the full width in one vertical stack. Creation order+ # sets the visual order; the returned list keeps the canonical positional+ # order consumed by render/persist/settings-mirror code.+ nr_intensity = gr.Slider(+ 0.0, 2.0, value=settings.nr_intensity, step=0.05, precision=2,+ label="NR Intensity", buttons=["reset"],+ )+ nr_passes = gr.Slider(+ 1, 4, value=settings.nr_passes, step=1, precision=0,+ label="NR Passes", buttons=["reset"],+ )+ local_tone_strength = gr.Slider(+ 0.0, 2.0, value=settings.local_tone_strength, step=0.05, precision=2,+ label="Local Tone Strength", buttons=["reset"]+ )+ local_structure_strength = gr.Slider(+ 0.0, 2.0, value=settings.local_structure_strength, step=0.05, precision=2,+ label="Local Structure Strength", buttons=["reset"]+ )+ skin_structure_strength = gr.Slider(+ -1.0, 2.0, value=settings.skin_structure_strength, step=0.05, precision=2,+ label="Skin Structure Strength",+ buttons=["reset"],+ )+ composition = build_composition_sliders(settings)automatic_mask = gr.Radio(choices=AUTOMATIC_MASK_CHOICES,value=automatic_mask_choice(settings.automatic_mask),label="Automatic Mask",- info=(- "Experimental runtime-generated mask that changes where Neural Rendering is "- "applied; it may cause flicker or inconsistent results."- ),)return [- nr_preset, nr_style, nr_intensity, local_tone_strength, local_structure_strength,+ nr_style, nr_intensity, nr_passes, local_tone_strength, local_structure_strength,skin_structure_strength, upscaling_factor, automatic_mask- ]-+ ] + composition-def build_dlss_model_control(settings: UISettings):- return gr.Dropdown(- choices=list(DLSS_MODEL_PRESETS),- value=settings.dlss_model_preset,- label="DLSS Model Preset",- info=(- "Default lets NVIDIA select its normal mode-specific presets. "- "J, K, L, or M forces that model preset for every DLSS scaling mode."- ),- )def preview_input_images(paths: list[str] | str | None):if not paths:return []if isinstance(paths, str):paths = [paths]+ full_size = full_size_image_previews_enabled()previews = []for raw_path in paths:try:- decoded = decode_image(raw_path)- image = Image.fromarray(decoded.rgba, mode="RGBA")- image.thumbnail((1200, 900), Image.Resampling.LANCZOS)+ image = (+ full_size_image_preview_path(raw_path)+ if full_size+ else decode_image_preview(raw_path, (1200, 900))+ )previews.append((image, Path(raw_path).name))except Exception:continuereturn previews-def render_image_batch(- input_paths: list[str] | str | None,- nr_preset: str,+def _image_options(nr_style: str,nr_intensity: float,+ nr_passes: float,local_tone_strength: float,local_structure_strength: float,skin_structure_strength: float,upscaling_factor: float,automatic_mask: str,- dlss_model_preset: str,+ nr_color_strength: float,+ tone_preservation: float,+ face_skin_protection: float,+ grain_preservation: float,+ mask_feather: float,+ nr_mask: object | None,+ nr_gpu_mode: bool,image_format: str,image_quality: float,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.")- if isinstance(input_paths, str):- input_paths = [input_paths]- options = ImageConversionOptions(+) -> ImageConversionOptions:+ return ImageConversionOptions(ai_gpu_uuid=processing_gpu_settings()[0],- nr_preset=nr_preset,nr_style=nr_style,nr_intensity=nr_intensity,+ nr_passes=int(nr_passes),local_tone_strength=local_tone_strength,local_structure_strength=local_structure_strength,skin_structure_strength=skin_structure_strength,automatic_mask=parse_automatic_mask(automatic_mask),- dlss_model_preset=dlss_model_preset,+ nr_color_strength=float(nr_color_strength),+ tone_preservation=float(tone_preservation),+ face_skin_protection=float(face_skin_protection),+ grain_preservation=float(grain_preservation),+ mask_feather=int(mask_feather),+ nr_mask=nr_mask,+ nr_gpu_mode=nr_gpu_mode,upscaling_factor=upscaling_factor,output_format=image_format,quality=int(image_quality),@@ -142,13 +139,51 @@ def render_image_batch(custom_suffix=custom_suffix,)++def render_image_batch(+ input_paths: list[str] | str | None,+ nr_style: str,+ nr_intensity: float,+ nr_passes: float,+ local_tone_strength: float,+ local_structure_strength: float,+ skin_structure_strength: float,+ upscaling_factor: float,+ automatic_mask: str,+ nr_color_strength: float,+ tone_preservation: float,+ face_skin_protection: float,+ grain_preservation: float,+ mask_feather: float,+ nr_mask: object | None,+ nr_gpu_mode: bool,+ image_format: str,+ image_quality: float,+ 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.")+ if isinstance(input_paths, str):+ input_paths = [input_paths]+ options = _image_options(+ nr_style, nr_intensity, nr_passes, local_tone_strength, local_structure_strength,+ skin_structure_strength, upscaling_factor, automatic_mask,+ nr_color_strength, tone_preservation, face_skin_protection, grain_preservation,+ mask_feather, nr_mask, nr_gpu_mode,+ image_format, image_quality, rename_mode, custom_suffix,+ )+ full_size = full_size_image_previews_enabled()+def report(value: float, message: str) -> None:progress(value, desc=message)try: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)+ generate_previews=not direct_disk and not full_size, create_zip=False)except Exception as exc:traceback.print_exc()if on_item_update is not None:@@ -157,11 +192,19 @@ def render_image_batch(gallery = []for item in ([] if direct_disk else result.successes):- preview = take_image_preview(item.output_path)+ preview = None+ if full_size:+ try:+ preview = full_size_image_preview_path(item.output_path)+ except Exception:+ # Preview display must never invalidate a successful render.+ preview = None+ else:+ preview = take_image_preview(item.output_path)if preview is None:with Image.open(item.output_path) as output:preview = output.convert("RGBA")- preview.thumbnail((1600, 1200), Image.Resampling.LANCZOS)+ preview.thumbnail((1200, 900), Image.Resampling.BILINEAR)preview = preview.copy()gallery.append((preview, Path(item.output_path).name))rows = [@@ -178,7 +221,54 @@ def render_image_batch()if result.failures:status += f"\nFirst error: {result.failures[0].error}"- return gallery, result.zip_path, rows, status+ return gallery, [item.output_path for item in result.successes], rows, status+++def preview_rendered_image(+ input_paths: list[str] | str | None,+ nr_style: str,+ nr_intensity: float,+ nr_passes: float,+ local_tone_strength: float,+ local_structure_strength: float,+ skin_structure_strength: float,+ upscaling_factor: float,+ automatic_mask: str,+ nr_color_strength: float,+ tone_preservation: float,+ face_skin_protection: float,+ grain_preservation: float,+ mask_feather: float,+ nr_mask: object | None,+ nr_gpu_mode: bool,+ image_format: str,+ image_quality: float,+ rename_mode: str,+ custom_suffix: str,+ progress=gr.Progress(track_tqdm=False),+ *, output_dir=None, controller=None, ephemeral_preview=False,+):+ del output_dir, ephemeral_preview+ paths = [input_paths] if isinstance(input_paths, str) else list(input_paths or [])+ if len(paths) != 1:+ raise gr.Error("Choose exactly one image to preview.")+ options = _image_options(+ nr_style, nr_intensity, nr_passes, local_tone_strength, local_structure_strength,+ skin_structure_strength, upscaling_factor, automatic_mask,+ nr_color_strength, tone_preservation, face_skin_protection, grain_preservation,+ mask_feather, nr_mask, nr_gpu_mode,+ image_format, image_quality, rename_mode, custom_suffix,+ )+ full_size = full_size_image_previews_enabled()+ image, status = render_image_preview(+ paths[0], options, progress=lambda value, message: progress(value, desc=message),+ controller=controller, full_size_preview=full_size,+ )+ return (+ gr.update(value=[(image, f"Preview — {Path(paths[0]).name}")], visible=True),+ status,+ )+@dataclass(slots=True)class ImageTab:@@ -188,15 +278,20 @@ class ImageTab:select_source: objectclear_source: objectneural: list[object]- model_preset: object+ composition: CompositionWidgets+ mask_state: object+ gpu_mode: objectoutput_format: objectquality: objectrename_mode: objectcustom_suffix: objectrender: objectstop: object+ preview: objectreset: objectoutput_gallery: object+ save_download: object+ zip_button: objectzip_download: objectstatus: objectresults: object@@ -206,14 +301,18 @@ class ImageTab:@propertydef render_inputs(self) -> list[object]:- return [self.sources, *self.neural, self.model_preset, self.output_format, self.quality, self.rename_mode, self.custom_suffix]+ return [self.sources, *self.neural, self.mask_state, self.gpu_mode, self.output_format, self.quality, self.rename_mode, self.custom_suffix]++ @property+ def preview_inputs(self) -> list[object]:+ return self.render_inputs@propertydef settings_inputs(self) -> list[object]:- return [*self.neural, self.model_preset, self.output_format, self.quality, self.rename_mode, self.custom_suffix]+ return [*self.neural, self.output_format, self.quality, self.rename_mode, self.custom_suffix]-def build_image_tab(settings: UISettings) -> ImageTab:+def build_image_tab(settings: UISettings, gpu_mode_state: object, mask_state: object) -> ImageTab:upload_types = ["image", ".svg", ".heic", ".heif", *sorted(RAW_EXTENSIONS)]with gr.Row():with gr.Column(scale=3):@@ -224,7 +323,7 @@ def build_image_tab(settings: UISettings) -> ImageTab:)input_gallery = gr.Gallery(label="Input preview", columns=3, height=520, object_fit="contain",- interactive=False, visible=False, buttons=["fullscreen"], elem_id="image-input-preview",+ interactive=False, visible="hidden", buttons=["fullscreen"], elem_id="image-input-preview",)with gr.Row(visible=False, elem_id="image-input-actions",@@ -234,40 +333,38 @@ def build_image_tab(settings: UISettings) -> ImageTab:"Choose Images", upload_types, "image-select-input",)clear_source = build_media_clear_button("image-clear-input")- input_path, output_path = build_path_controls()- with gr.Accordion("DLSS 5 Neural Rendering Settings", open=True):+ with gr.Row():+ render = gr.Button("Render image(s)", variant="primary")+ stop = gr.Button("Stop", variant="stop")+ preview = gr.Button("Preview", visible=False)+ reset = gr.Button("Reset settings")+ with gr.Column(elem_classes=["neural-controls-unified"]):neural = build_neural_controls(settings)- with gr.Accordion("DLSS 5 Settings", open=True):- model_preset = build_dlss_model_control(settings)+ composition = build_composition_widgets()+ input_path, output_path = build_path_controls()with gr.Row():output_format = gr.Dropdown(list(IMAGE_FORMATS), value=settings.image_format, label="Output format",- info="PNG and TIFF are lossless; JPEG composites transparency over white.",)quality = gr.Slider(1, 100, value=settings.image_quality, step=1, precision=0,- label="Lossy quality", info="Used by JPEG, WebP, and AVIF; ignored by PNG/TIFF.",+ label="Lossy quality",)with gr.Row():rename_mode = gr.Radio(RENAME_MODES, value=settings.image_rename_mode, label="Rename",- info="Auto adds the current DLSS5 timestamp; Copy keeps the original base name; Custom appends your suffix.",)custom_suffix = gr.Textbox(- value=settings.image_custom_suffix, label="Custom suffix", placeholder="_DLSS5",+ value=settings.image_custom_suffix, label="Custom suffix", placeholder="_Neural_Rendering",interactive=settings.image_rename_mode == "Custom",)- with gr.Row():- render = gr.Button("Render image(s)", variant="primary")- stop = gr.Button("Stop", variant="stop")- reset = gr.Button("Reset settings")with gr.Column(scale=3):output_gallery = gr.Gallery(label="Enhanced previews", columns=2, height=520, object_fit="contain",interactive=False, buttons=["download", "download_all", "fullscreen"],elem_id="image-output-preview",)- zip_download = gr.DownloadButton("Save as ZIP", visible=False)+ save_download, zip_button, zip_download = build_save_controls("image", "nr-image")status = gr.Textbox(label="Status", interactive=False, lines=5, max_lines=12)results = gr.Dataframe(headers=BATCH_HEADERS,@@ -275,8 +372,8 @@ def build_image_tab(settings: UISettings) -> ImageTab:label="Batch results", wrap=True,)tab = ImageTab(- sources, input_gallery, input_actions, select_source, clear_source, neural, model_preset, output_format, quality, rename_mode,- custom_suffix, render, stop, reset, output_gallery, zip_download, status, results+ sources, input_gallery, input_actions, select_source, clear_source, neural, composition, mask_state, gpu_mode_state, output_format, quality, rename_mode,+ custom_suffix, render, stop, preview, reset, output_gallery, save_download, zip_button, zip_download, status, results)tab.input_path, tab.output_path = input_path, output_pathbind_image_events(tab)@@ -284,7 +381,13 @@ def build_image_tab(settings: UISettings) -> ImageTab:def bind_image_events(tab: ImageTab) -> None:- bind_batch_ui(tab, render_image_batch, kind="image", preview_mode=preview_input_images)+ bind_batch_ui(+ tab, render_image_batch, kind="image", preview_mode=preview_input_images,+ archive_prefix="DLSS5_IMAGE_BATCH",+ preview_actions=[(tab.preview, preview_rendered_image)],+ realtime_preview=preview_rendered_image,+ realtime_components=tab.neural,+ )tab.rename_mode.change(rename_suffix_update, inputs=tab.rename_mode, outputs=tab.custom_suffix, queue=False,)diff --git a/src/neural_rendering/ui.py b/src/neural_rendering/ui.pyindex b6c3f65..ad9e1f1 100644--- a/src/neural_rendering/ui.py+++ b/src/neural_rendering/ui.py@@ -4,6 +4,7 @@ from dataclasses import dataclassimport gradio as gr+from ..core.batch_ui import bind_input_surface_reactivationfrom ..settings.models import UISettingsfrom .image.ui import ImageTab, build_image_tabfrom .video.ui import VideoTab, build_video_tab@@ -12,10 +13,31 @@ from .video.ui import VideoTab, build_video_tabMODE_CHOICES = ("Image", "Video")+def _stable_visible(show: bool):+ # ``hidden`` keeps the component tree mounted while taking no layout space.+ return True if show else "hidden"++def mode_visibility(mode: str) -> tuple[dict, dict]:- """Show one workflow while keeping both component trees alive."""+ """Show one workflow while keeping both stateful component trees mounted."""show_video = mode == "Video"- return gr.update(visible=not show_video), gr.update(visible=show_video)+ return (+ gr.update(visible=_stable_visible(not show_video)),+ gr.update(visible=_stable_visible(show_video)),+ )+++_MODE_GUARD_JS = """(mode) => {+ document.documentElement.dataset.neuralRenderingMode = mode;+ const hiddenPanel = document.getElementById(+ mode === 'Video' ? 'neural-rendering-image' : 'neural-rendering-video'+ );+ if (hiddenPanel) {+ hiddenPanel.querySelectorAll('video').forEach((node) => {+ try { node.pause(); } catch (_) {}+ });+ }+}"""@dataclass(slots=True)@@ -27,24 +49,46 @@ class NeuralRenderingTab:video_panel: object-def build_neural_rendering_tab(settings: UISettings) -> NeuralRenderingTab:+def build_neural_rendering_tab(+ settings: UISettings, gpu_mode_state: object, mask_state: object,+) -> NeuralRenderingTab:mode = gr.Radio(choices=list(MODE_CHOICES),value="Image",- label="Mode",+ label=None,+ show_label=False,+ container=False,interactive=True,elem_id="neural-rendering-mode",)- with gr.Column(visible=True, elem_id="neural-rendering-image") as image_panel:- image_tab = build_image_tab(settings)- with gr.Column(visible=False, elem_id="neural-rendering-video") as video_panel:- video_tab = build_video_tab(settings)+ with gr.Column(+ visible=True, elem_id="neural-rendering-image", elem_classes=["workflow-mode-panel"],+ ) as image_panel:+ image_tab = build_image_tab(settings, gpu_mode_state, mask_state)+ with gr.Column(+ visible="hidden", elem_id="neural-rendering-video", elem_classes=["workflow-mode-panel"],+ ) as video_panel:+ video_tab = build_video_tab(settings, gpu_mode_state, mask_state)- mode.change(+ # First hide the old panel synchronously in the browser. The backend update+ # then makes the requested panel authoritative. If requests overlap, the+ # latest client guard still prevents an older response from exposing the+ # wrong panel; at worst the correct panel stays blank until the latest reply.+ transition = mode.change(+ None, inputs=mode, js=_MODE_GUARD_JS, queue=False, show_progress="hidden",+ trigger_mode="always_last",+ )+ visibility = transition.then(mode_visibility,inputs=mode,outputs=[image_panel, video_panel],queue=False,show_progress="hidden",+ trigger_mode="always_last",)+ # Re-assert the upload/preview invariant after the mode panel is restored.+ # This is non-destructive: it only corrects visibility and never regenerates+ # media or clears output/download state.+ bind_input_surface_reactivation(visibility, image_tab, kind="image")+ bind_input_surface_reactivation(visibility, video_tab, kind="video")return NeuralRenderingTab(mode, image_tab, video_tab, image_panel, video_panel)diff --git a/src/neural_rendering/video/__init__.py b/src/neural_rendering/video/__init__.pyindex bc7fb6a..ef74d94 100644--- a/src/neural_rendering/video/__init__.py+++ b/src/neural_rendering/video/__init__.py@@ -1,6 +1,6 @@from .batch import convert_videosfrom .models import (- ConversionOptions, ConversionResult, DLSS_MODEL_PRESETS, NR_PRESETS, NR_STYLES,+ ConversionOptions, ConversionResult, NR_STYLES,UPSCALING_MODES, VideoBatchResult, VideoConversionFailure, VideoConversionSuccess,)from .processor import convert_video@@ -11,8 +11,8 @@ from .sizing import ()__all__ = [- "AUTO_BITRATE_DIVISORS", "ConversionOptions", "ConversionResult", "DLSS_MODEL_PRESETS",- "ENCODING_QUALITIES", "NR_PRESETS", "NR_STYLES", "UPSCALING_CHOICES", "UPSCALING_MODES",+ "AUTO_BITRATE_DIVISORS", "ConversionOptions", "ConversionResult",+ "ENCODING_QUALITIES", "NR_STYLES", "UPSCALING_CHOICES", "UPSCALING_MODES","VideoBatchResult", "VideoConversionFailure", "VideoConversionSuccess","calculate_auto_bitrate_kbps", "convert_video", "convert_videos", "probe_video","resolve_encoding_quality", "resolve_native_settings", "resolve_output_size",diff --git a/src/neural_rendering/video/batch.py b/src/neural_rendering/video/batch.pyindex 38ef354..be7df50 100644--- a/src/neural_rendering/video/batch.py+++ b/src/neural_rendering/video/batch.py@@ -7,9 +7,8 @@ from pathlib import Pathfrom typing import Callable, Iterablefrom ...core.batch_progress import BatchItemUpdate, BatchProgress-from ...core.disk_paths import prepare_output_dir+from ...core.disk_paths import StreamingMediaArchive, prepare_output_dirfrom ...core.jobs import Cancelled, JobController, active_job-from ...core.paths import LOGSfrom ...core.runtime import prepare_runtimefrom .models import ConversionOptions, VideoBatchResult, VideoConversionFailure, VideoConversionSuccessfrom . import processor@@ -23,6 +22,7 @@ def convert_videos(*, output_dir: str | os.PathLike[str] | None = None,controller: JobController | None = None,on_item_update: Callable[[BatchItemUpdate], None] | None = None,+ create_archive: bool = False,) -> VideoBatchResult:"""Render in input order, publishing a completed row before starting the next file."""options = replace(options) if options else ConversionOptions()@@ -32,9 +32,16 @@ def convert_videos(controller = controller or JobController()reporter = BatchProgress(paths, on_item_update, progress)successes, failures = [], []+ archive: StreamingMediaArchive | None = None+ archive_path: str | None = None+ archive_error = ""stamp = time.strftime("%Y%m%d-%H%M%S") + f"-{time.time_ns() % 1_000_000:06d}"try:destination = prepare_output_dir(output_dir, default=processor.OUTPUTS)+ if create_archive and len(paths) > 1:+ archive = StreamingMediaArchive(+ destination, "DLSS5_VIDEO_BATCH", controller=controller+ )with active_job(controller):if controller.cancel.is_set():raise Cancelled("Stopped before rendering.")@@ -61,6 +68,9 @@ def convert_videos(else:successes.append(VideoConversionSuccess(index, str(path), result))reporter.complete(index, result.output_path, f"{result.frames} frames; report: {result.report_path}")+ if archive is not None and not archive.add(result.output_path):+ if archive.error is not None and not archive_error:+ archive_error = str(archive.error)finally:del processor._BATCH_CONTEXT.controllerdel processor._BATCH_CONTEXT.prepared_runtime@@ -70,11 +80,28 @@ def convert_videos(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))+ if archive is not None:+ try:+ archive_path = archive.close(publish=len(successes) > 1)+ except Exception as exc:+ archive_error = str(exc)+ archive_path = None+ diagnostics = reporter.diagnostics(final=True)+ if archive_error:+ diagnostics["archive_error"] = archive_error+ manifest = _write_video_batch_manifest(+ stamp, options, successes, failures, cancelled,+ batch_diagnostics=diagnostics, output_dir=str(destination)+ )reporter.finish(cancelled=cancelled, manifest_path=manifest)- return VideoBatchResult(successes, failures, cancelled, manifest)+ return VideoBatchResult(+ successes, failures, cancelled, manifest, archive_path, archive_error+ )except BaseException as exc:+ if archive is not None:+ try:+ archive.abort()+ except Exception:+ passreporter.finish(cancelled=controller.cancel.is_set(), error=str(exc))raisediff --git a/src/neural_rendering/video/cuda_pipeline.py b/src/neural_rendering/video/cuda_pipeline.pynew file mode 100644index 0000000..013457f--- /dev/null+++ b/src/neural_rendering/video/cuda_pipeline.py@@ -0,0 +1,478 @@+from __future__ import annotations++"""GPU-resident offline Neural Rendering video pipeline."""++import gc+import math+import os+import shutil+import time+from contextlib import suppress+from fractions import Fraction+from pathlib import Path+from typing import Any, Callable++import av+import numpy as np+from av.codec.hwaccel import HWAccel++from ...core import app_log, ffmpeg+from ...core.disk_paths import OutputFile, prepare_output_dir+from ...core.gpu_selection import resolve_runtime_ai_gpu+from ...core.jobs import Cancelled+from ...core.naming import output_filename+from ...core.paths import JOBS, OUTPUTS+from ...core.render_metadata import prepare_render_note+from ...core.runtime import (+ DLSSFrameSession,+ resize_fit,+ rotate_frame,+)+from .guides import TemporalGuideGenerator+from .models import ConversionOptions, ConversionResult+from .sizing import resolve_native_settings, resolve_output_size, resolve_upscaling_mode+++_NVENC_CODEC = {+ "H.264 (NVIDIA NVENC)": "h264_nvenc",+ "H.265 (NVIDIA NVENC)": "hevc_nvenc",+ "AV1 (NVIDIA NVENC)": "av1_nvenc",+}+++def _matrix_code(metadata: dict[str, Any]) -> int:+ value = str(metadata.get("color_space") or "").casefold()+ if "2020" in value:+ return 2+ if value in {"bt470bg", "smpte170m", "smpte240m", "fcc"}:+ return 0+ return 1+++def _range_code(metadata: dict[str, Any]) -> int:+ return int(str(metadata.get("color_range") or "").casefold() in {"pc", "jpeg", "full"})+++def _set_color_properties(codec_context: Any, metadata: dict[str, Any], hdr: bool) -> None:+ primaries = {"bt709": 1, "bt2020": 9}.get(str(metadata.get("color_primaries")), 2)+ transfer = {+ "bt709": 1,+ "smpte2084": 16,+ "arib-std-b67": 18,+ }.get(str(metadata.get("color_transfer")), 2)+ colorspace = {+ "bt709": 1,+ "bt470bg": 5,+ "smpte170m": 6,+ "bt2020nc": 9,+ "bt2020c": 10,+ }.get(str(metadata.get("color_space")), 2)+ codec_context.color_primaries = primaries+ codec_context.color_trc = transfer+ codec_context.colorspace = colorspace+ codec_context.color_range = 1 if hdr or not _range_code(metadata) else 2+++def _check_cancel(controller: Any) -> None:+ if controller.cancel.is_set():+ raise Cancelled("Render stopped by user.")+++def _progress(+ callback: Callable[[float, str], None] | None, value: float, message: str+) -> None:+ if callback is None:+ return+ try:+ callback(max(0.0, min(1.0, float(value))), message)+ except Exception:+ pass+++def convert_video_cuda_nvenc(+ source: Path,+ options: ConversionOptions,+ *,+ preview_seconds: float | None,+ preview_frames: int | None,+ compat_preview: bool,+ prepared_runtime: Any,+ controller: Any,+ progress: Callable[[float, str], None] | None,+ output_dir: str | os.PathLike[str] | None,+) -> ConversionResult:+ """Decode with PyAV, evaluate feature 18, and encode directly from CUDA."""+ started = time.perf_counter()+ runtime_bundle = prepared_runtime.runtime_bundle+ gpu: dict[str, Any] | None = None+ video_gpu: dict[str, Any] | None = None+ session: DLSSFrameSession | None = None+ output_file: OutputFile | None = None+ job_dir: Path | None = None+ delivered = 0+ encoded_container: Any | None = None+ timings: dict[str, float] = {}+ frame_accounting: dict[str, Any] = {}+ decode_backends: set[str] = set()+ try:+ _check_cancel(controller)+ probe_started = time.perf_counter()+ metadata = ffmpeg.probe_video(source, count_mode="metadata", controller=controller)+ timings["probe_seconds"] = time.perf_counter() - probe_started+ declared_frames = int(metadata.get("frames") or 0)+ estimated_frames = declared_frames or max(+ 1, int(math.ceil(float(metadata["duration"]) * float(metadata["fps"])))+ )+ if preview_frames is not None:+ estimated_frames = min(estimated_frames, preview_frames) if declared_frames else preview_frames+ elif preview_seconds is not None:+ estimated_frames = min(+ estimated_frames,+ max(1, int(math.ceil(preview_seconds * float(metadata["fps"])))),+ )+ factor, mode = resolve_upscaling_mode(options.upscaling_factor)+ input_width, input_height = int(metadata["width"]), int(metadata["height"])+ output_width, output_height = resolve_output_size(input_width, input_height, factor)+ effective_hdr = bool(options.preserve_hdr and not compat_preview)+ output_p010 = effective_hdr+ codec_name = _NVENC_CODEC[ffmpeg._normalize_codec(options.codec)]+ gpu = resolve_runtime_ai_gpu(+ prepared_runtime.gpus, prepared_runtime.runtime_bundle, options.ai_gpu_uuid+ )+ video_gpu = ffmpeg.resolve_video_gpu(+ prepared_runtime.gpus,+ str(gpu["uuid"]),+ options.codec,+ output_width,+ output_height,+ )+ if video_gpu is None:+ raise RuntimeError("The selected AI GPU cannot initialize the requested NVENC codec.")+ ordinal = int(gpu.get("cuda_ordinal", gpu.get("index", 0)))+ if int(video_gpu["cuda_ordinal"]) != ordinal:+ raise RuntimeError("NVENC did not resolve to the Neural Rendering CUDA adapter.")++ destination = prepare_output_dir(output_dir, default=OUTPUTS)+ JOBS.mkdir(exist_ok=True)+ app_log.info("video-render-cuda", f"start src={source.name}")+ stamp = time.strftime("%Y%m%d-%H%M%S") + f"-{time.time_ns() % 1_000_000:06d}"+ extension = {"MP4": ".mp4", "MKV": ".mkv", "MOV": ".mov"}[options.container]+ is_preview = preview_seconds is not None or preview_frames is not None+ output_kind = "DLSS5" if not is_preview else (+ "DLSS5_PREVIEW_FRAME" if preview_frames is not None else "DLSS5_PREVIEW"+ )+ output = destination / output_filename(+ source,+ extension,+ options.rename_mode,+ options.custom_suffix,+ f"{source.stem}_{output_kind}_{stamp}",+ )+ output_file = OutputFile(output)+ job_dir = JOBS / f"{source.stem}-{stamp}-{os.getpid()}-cuda"+ job_dir.mkdir(parents=True, exist_ok=False)+ intermediate_extension = ".mkv" if options.container == "MKV" else ".mp4"+ temp_video = job_dir / f"processed-video{intermediate_extension}"+ metadata_diagnostics: dict[str, Any] = {}+ render_note = prepare_render_note(options, metadata_diagnostics)+ native = resolve_native_settings(options)+ matrix = _matrix_code(metadata)+ color_range = _range_code(metadata)++ # FFmpeg and the bridge deliberately share CUDA's primary context.+ # Open the decoder first because some driver versions reject creating a+ # new FFmpeg CUDA hwdevice while another library has it current.+ decode_device = HWAccel(+ "cuda",+ device=str(ordinal),+ allow_software_fallback=True,+ options={"primary_ctx": "1"},+ is_hw_owned=True,+ )+ _progress(progress, 0.01, f"Starting feature 18 on {gpu['display_name']}")+ with av.open(str(source), hwaccel=decode_device) as decoded:+ input_stream = decoded.streams.video[0]+ input_stream.thread_type = "AUTO"+ # Create both FFmpeg CUDA hardware contexts before NGX initializes.+ # This also covers codecs which intentionally fall back to software+ # decoding and therefore do not create a decoder CUDA context.+ encode_device = HWAccel(+ "cuda",+ device=str(ordinal),+ options={"primary_ctx": "1"},+ is_hw_owned=True,+ )+ quality = ffmpeg.resolve_encoding_quality(+ options.quality,+ options.codec,+ output_width,+ output_height,+ float(metadata["fps"]),+ hdr_mode=effective_hdr,+ )+ encoded_container = av.open(str(temp_video), mode="w")+ try:+ output_stream = encoded_container.add_stream(+ codec_name, rate=metadata["rate"], hwaccel=encode_device+ )+ output_stream.width = output_width+ output_stream.height = output_height+ output_stream.pix_fmt = "cuda"+ output_stream.codec_context.sw_format = "p010le" if output_p010 else "nv12"+ output_stream.time_base = input_stream.time_base+ output_stream.codec_context.time_base = input_stream.time_base+ encoder_options = {"preset": "p6", "rc": "vbr", "gpu": str(ordinal)}+ if codec_name != "av1_nvenc":+ encoder_options["tune"] = "hq"+ if quality["mode"] == "constant-quality":+ encoder_options["cq"] = "0"+ else:+ output_stream.codec_context.bit_rate = int(quality["target_bitrate_kbps"]) * 1000+ output_stream.codec_context.options = encoder_options+ _set_color_properties(output_stream.codec_context, metadata, effective_hdr)+ except BaseException:+ encoded_container.close()+ raise+ session_started = time.perf_counter()+ session = DLSSFrameSession(+ input_width=input_width,+ input_height=input_height,+ output_width=output_width,+ output_height=output_height,+ frame_count=None,+ warmup_frames=options.warmup_frames,+ factor=factor,+ mode=mode,+ native_settings=native,+ composition_mask=options.nr_mask,+ gpu=gpu,+ runtime_bundle=runtime_bundle,+ controller=controller,+ cuda_video=True,+ )+ timings["native_setup_seconds"] = time.perf_counter() - session_started+ encode_started = time.perf_counter()+ with encoded_container as encoded:+ software_guides: TemporalGuideGenerator | None = None+ first_time: float | None = None+ preview_pts_origin: int | None = None+ default_duration = max(+ 1, round(Fraction(1, 1) / metadata["rate"] / input_stream.time_base)+ )+ decode_seconds = 0.0+ prepare_seconds = 0.0+ scene_seconds = 0.0+ evaluate_seconds = 0.0+ encode_seconds = 0.0+ completion_reason = "eof"+ decoder = iter(decoded.decode(input_stream))+ while True:+ _check_cancel(controller)+ if preview_frames is not None and delivered >= preview_frames:+ completion_reason = "preview_limit"+ break+ decode_started = time.perf_counter()+ try:+ frame = next(decoder)+ except StopIteration:+ decode_seconds += time.perf_counter() - decode_started+ break+ decode_seconds += time.perf_counter() - decode_started+ if frame.is_corrupt:+ raise RuntimeError(f"The video decoder marked source frame {delivered} as corrupt.")+ pts = int(frame.pts if frame.pts is not None else delivered * default_duration)+ frame_time_base = frame.time_base or input_stream.time_base+ timestamp = float(Fraction(pts) * frame_time_base)+ if first_time is None:+ first_time = timestamp+ if preview_seconds is not None and delivered and timestamp - first_time >= preview_seconds:+ completion_reason = "preview_limit"+ break+ duration = int(getattr(frame, "duration", None) or default_duration)++ if frame.format.name == "cuda":+ decode_backends.add("nvdec")+ scene_started = time.perf_counter()+ scene_score, reset = session.score_cuda_frame(+ frame, color_matrix=matrix, color_range=color_range+ )+ scene_seconds += time.perf_counter() - scene_started+ evaluate_started = time.perf_counter()+ processed, output_pts = session.process_video_frame(+ index=delivered,+ frame=frame,+ reset=reset,+ scene_score=scene_score,+ pts=pts,+ duration=duration,+ time_base=frame_time_base,+ color_matrix=matrix,+ color_range=color_range,+ rotation=int(metadata["rotation"]),+ output_p010=output_p010,+ )+ else:+ decode_backends.add("software")+ prepare_started = time.perf_counter()+ rgba = rotate_frame(+ frame.to_ndarray(format="rgba"), int(metadata["rotation"])+ )+ if rgba.shape[:2] != (output_height, output_width):+ rgba = resize_fit(rgba, output_width, output_height)+ rgba = np.ascontiguousarray(rgba, dtype=np.uint8)+ prepare_seconds += time.perf_counter() - prepare_started+ if software_guides is None:+ software_guides = TemporalGuideGenerator(output_width, output_height)+ scene_started = time.perf_counter()+ guide = software_guides.process(rgba)+ scene_seconds += time.perf_counter() - scene_started+ scene_score, reset = guide.scene_score, guide.reset+ evaluate_started = time.perf_counter()+ processed, output_pts = session.process_video_frame(+ index=delivered,+ rgba=rgba,+ reset=reset,+ scene_score=scene_score,+ pts=pts,+ duration=duration,+ time_base=frame_time_base,+ color_matrix=matrix,+ color_range=color_range,+ output_p010=output_p010,+ )+ evaluate_seconds += time.perf_counter() - evaluate_started+ if output_pts != pts:+ raise RuntimeError("The feature-18 frame ABI changed a video timestamp.")+ if is_preview:+ if preview_pts_origin is None:+ preview_pts_origin = output_pts+ processed.pts = output_pts - preview_pts_origin+ encode_frame_started = time.perf_counter()+ for packet in output_stream.encode(processed):+ encoded.mux(packet)+ encode_seconds += time.perf_counter() - encode_frame_started+ del processed, frame+ delivered += 1+ _progress(+ progress,+ 0.04 + 0.84 * min(1.0, delivered / estimated_frames),+ f"Neural Rendering frame {delivered} (estimated {estimated_frames})",+ )+ if not delivered:+ raise RuntimeError("The input video contains no decodable frames.")+ flush_started = time.perf_counter()+ for packet in output_stream.encode():+ encoded.mux(packet)+ encode_seconds += time.perf_counter() - flush_started+ timings.update(+ source_decode_seconds=decode_seconds,+ frame_prepare_seconds=prepare_seconds,+ scene_score_seconds=scene_seconds,+ dlss_seconds=evaluate_seconds,+ nvenc_seconds=encode_seconds,+ )+ frame_accounting.update(+ declared_frames=declared_frames,+ decoded_frames=delivered,+ processed_frames=delivered,+ encoded_frames=delivered,+ completion_reason=completion_reason,+ source_verification="clean_decoder_eof" if completion_reason == "eof" else "preview_limit",+ )+ timings["encoding_stage_seconds"] = time.perf_counter() - encode_started+ assert session is not None+ session.close()+ frame_accounting["bridge_completed_frames"] = session.completed_frames+ if session.completed_frames != delivered:+ raise RuntimeError("Bridge completion does not match the processed frame count.")+ gc.collect()++ mux_started = time.perf_counter()+ if preview_frames is not None:+ shutil.copyfile(temp_video, output_file.temporary)+ metadata_diagnostics.update(status="skipped", reason="preview_fast_path")+ else:+ _progress(progress, 0.91, "Muxing original audio, chapters, and metadata")+ 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+ _check_cancel(controller)+ verified = ffmpeg.probe_video(+ output_file.temporary, count_mode="metadata", controller=controller+ )+ if (int(verified["width"]), int(verified["height"])) != (output_width, output_height):+ raise RuntimeError("Saved output dimensions do not match the neural dimensions.")+ declared_output = int(verified.get("frames") or 0)+ if declared_output and declared_output != delivered:+ raise RuntimeError(+ f"Output container reports {declared_output} frames instead of {delivered}."+ )+ if effective_hdr and (not verified.get("hdr") or verified.get("color_space") != "bt2020nc"):+ raise RuntimeError("Saved HDR output did not retain BT.2020 HDR signaling.")++ status = session.structured_status()+ decode_backend = "+".join(sorted(decode_backends)) or "unknown"+ status["decode_backend"] = decode_backend+ status["encode_backend"] = codec_name+ resize_method = "none" if factor == 1.0 else "lanczos"+ elapsed = time.perf_counter() - started+ report_path = app_log.session_path()+ app_log.info(+ "video-render-cuda",+ f"done src={source.name} out={output.name} frames={delivered} "+ f"elapsed={elapsed:.0f}s avg={delivered / max(elapsed, 1e-9):.1f}fps",+ )+ output_file.publish()+ _progress(progress, 1.0, "Complete — feature 18 confirmed")+ return ConversionResult(+ output_path=str(output),+ report_path=str(report_path),+ frames=delivered,+ nr_count_evidence=delivered,+ elapsed_seconds=elapsed,+ gpu=str(gpu["display_name"]),+ input_width=input_width,+ input_height=input_height,+ render_width=output_width,+ render_height=output_height,+ output_width=output_width,+ output_height=output_height,+ upscaling_factor=factor,+ neural_dimensions={"width": output_width, "height": output_height},+ resize_method=resize_method,+ memory_path="cuda_d3d12_shared",+ bridge_status=status,+ )+ except BaseException as exc:+ if session is not None and not session.closed:+ with suppress(Exception):+ session.abort()+ if output_file is not None:+ output_file.cleanup(rollback=True)+ if controller.cancel.is_set() and not isinstance(exc, Cancelled):+ raise Cancelled("Render stopped by user.") from exc+ if isinstance(exc, Cancelled):+ app_log.info("video-render-cuda", f"cancelled src={source.name}")+ raise+ tails: dict[str, object] = {}+ if session is not None:+ with suppress(Exception):+ tails["bridge"] = session.bridge_logs[-20:]+ tails["decode"] = "+".join(sorted(decode_backends)) or "unknown"+ failure = app_log.fail("video-render-cuda", f"video-render-cuda-{source.stem}", exc, tails)+ raise RuntimeError(f"{exc}\nDetails: {failure}") from exc+ finally:+ if encoded_container is not None:+ with suppress(Exception):+ encoded_container.close()+ if output_file is not None:+ output_file.cleanup()+ if job_dir is not None and job_dir.exists():+ shutil.rmtree(job_dir, ignore_errors=True)diff --git a/src/neural_rendering/video/guides.py b/src/neural_rendering/video/guides.pyindex 8f690b3..449e834 100644--- a/src/neural_rendering/video/guides.py+++ b/src/neural_rendering/video/guides.py@@ -5,60 +5,58 @@ from dataclasses import dataclassimport cv2import numpy as np++SCENE_CUT_THRESHOLD = 0.24++@dataclass(slots=True)class GuideFrame:- motion: np.ndarrayreset: boolscene_score: floatclass TemporalGuideGenerator:- """Estimate the guide buffers an encoded video does not contain."""+ """Reduced-luma scene reset detector that retains history between cuts."""def __init__(self, width: int, height: int, flow_width: int = 640) -> None:- self.width = width- self.height = height- scale = min(1.0, flow_width / width)- self.flow_width = max(64, int(round(width * scale / 2) * 2))- self.flow_height = max(64, int(round(height * scale / 2) * 2))+ scale = min(1.0, flow_width / max(1, width))+ self.luma_width = max(32, int(round(width * scale / 2) * 2))+ self.luma_height = max(32, int(round(height * scale / 2) * 2))+ self._small_rgba = np.empty((self.luma_height, self.luma_width, 4), dtype=np.uint8)+ self._gray_a = np.empty((self.luma_height, self.luma_width), dtype=np.uint8)+ self._gray_b = np.empty_like(self._gray_a)+ self._difference = np.empty_like(self._gray_a)+ self._current_gray = self._gray_aself.previous_gray: np.ndarray | None = None- self.zero_motion = np.zeros((height, width, 2), dtype=np.float16)- self.dis = cv2.DISOpticalFlow_create(cv2.DISOPTICAL_FLOW_PRESET_MEDIUM)- self.dis.setUseSpatialPropagation(True)- self.dis.setFinestScale(1)+ self.duplicate_frames = 0def _small_gray(self, rgba: np.ndarray) -> np.ndarray:- gray = cv2.cvtColor(rgba, cv2.COLOR_RGBA2GRAY)- return cv2.resize(- gray,- (self.flow_width, self.flow_height),+ cv2.resize(+ rgba,+ (self.luma_width, self.luma_height),+ dst=self._small_rgba,interpolation=cv2.INTER_AREA,)+ cv2.cvtColor(self._small_rgba, cv2.COLOR_RGBA2GRAY, dst=self._current_gray)+ return self._current_gray- def process(self, rgba: np.ndarray) -> GuideFrame:+ def process(+ self, rgba: np.ndarray, *, motion_buffer: np.ndarray | None = None+ ) -> GuideFrame:+ del motion_buffercurrent = self._small_gray(rgba)if self.previous_gray is None:- motion = self.zero_motionreset = Truescene_score = 1.0else:- scene_score = float(np.mean(cv2.absdiff(current, self.previous_gray))) / 255.0- reset = scene_score > 0.24- if reset:- motion = self.zero_motion+ cv2.absdiff(current, self.previous_gray, dst=self._difference)+ if cv2.countNonZero(self._difference) == 0:+ self.duplicate_frames += 1+ scene_score = 0.0else:- motion = self.dis.calc(current, self.previous_gray, None)- motion = cv2.resize(- motion,- (self.width, self.height),- interpolation=cv2.INTER_LINEAR,- )- motion[..., 0] *= self.width / self.flow_width- motion[..., 1] *= self.height / self.flow_height- motion = np.ascontiguousarray(motion.astype(np.float16))+ scene_score = float(np.mean(self._difference)) / 255.0+ reset = scene_score > SCENE_CUT_THRESHOLD+self.previous_gray = current- return GuideFrame(- motion=motion,- reset=reset,- scene_score=scene_score,- )+ self._current_gray = self._gray_b if current is self._gray_a else self._gray_a+ return GuideFrame(reset=reset, scene_score=scene_score)diff --git a/src/neural_rendering/video/models.py b/src/neural_rendering/video/models.pyindex 36668e6..ce5411a 100644--- a/src/neural_rendering/video/models.py+++ b/src/neural_rendering/video/models.py@@ -2,7 +2,7 @@ from __future__ import annotationsfrom dataclasses import dataclass-from ...core.runtime import DLSS_MODEL_PRESETS, NR_PRESETS, NR_STYLES, UPSCALING_MODES+from ...core.runtime import NR_STYLES, UPSCALING_MODES@dataclass(slots=True)@@ -11,22 +11,29 @@ class ConversionOptions:video_gpu_uuid: str = "auto"nr_style: str = "Default"nr_intensity: float = 1.0+ nr_passes: int = 1local_tone_strength: float = 1.0local_structure_strength: float = 1.0skin_structure_strength: float = -1.0+ nr_color_strength: float = 1.0+ tone_preservation: float = 0.0+ face_skin_protection: float = 0.0+ grain_preservation: float = 0.0+ shimmer_suppression: float = 0.70+ mask_feather: int = 0+ nr_mask: object | None = Noneupscaling_factor: float = 1.0- codec: str = "H.264"+ codec: str = "H.264 (NVIDIA NVENC)"container: str = "MP4"quality: str = "Auto (Default)"preserve_hdr: bool = Falsewarmup_frames: int = 0preview_seconds: float | None = Nonepreview_frames: int | None = None- nr_preset: str = "Default"automatic_mask: bool = False+ nr_gpu_mode: bool = Truerename_mode: str = "Auto"- custom_suffix: str = "_DLSS5"- dlss_model_preset: str = "Default"+ custom_suffix: str = "_Neural_Rendering"# True = truncated preview uses the forced H.264 SDR path (current behavior).# False = truncated preview uses the user's codec/container (HDR preserved).preview_compat: bool = True@@ -46,9 +53,10 @@ class ConversionResult:output_width: intoutput_height: intupscaling_factor: float- dlss_mode: str- dlss_model_preset: str = "Default"- applied_dlss_model_preset: int = 0+ neural_dimensions: dict[str, int] | None = None+ resize_method: str = "none"+ memory_path: str = "host_staging"+ bridge_status: dict | None = None@dataclass(slots=True)@@ -72,3 +80,5 @@ class VideoBatchResult:failures: list[VideoConversionFailure]cancelled: boolmanifest_path: str+ archive_path: str | None = None+ archive_error: str = ""diff --git a/src/neural_rendering/video/preview.py b/src/neural_rendering/video/preview.pyindex 05da2bf..2ebcf43 100644--- a/src/neural_rendering/video/preview.py+++ b/src/neural_rendering/video/preview.py@@ -5,6 +5,7 @@ from pathlib import Pathimport gradio as gr+from ...core.jobs import Cancelledfrom ...core.ffmpeg.preview import (is_browser_playable, make_browser_preview, normalize_preview_encoding,resolve_final_preview, resolve_preview_codec, wants_compat_preview,@@ -18,15 +19,22 @@ PREVIEW_SECONDS = 3.0def _process_video(input_path: str | None,- nr_preset: str,nr_style: str,nr_intensity: float,+ nr_passes: float,local_tone_strength: float,local_structure_strength: float,skin_structure_strength: float,upscaling_factor: float,automatic_mask: str,- dlss_model_preset: str,+ nr_color_strength: float,+ tone_preservation: float,+ face_skin_protection: float,+ grain_preservation: float,+ mask_feather: float,+ shimmer_suppression: float,+ nr_mask: object | None,+ nr_gpu_mode: bool,codec: str,container: str,quality: str,@@ -34,6 +42,10 @@ def _process_video(progress,preview_seconds: float | None,preview_frames: int | None,+ *,+ output_dir=None,+ controller=None,+ ephemeral_preview: bool = False,) -> tuple[str | None, str]:if not input_path:raise gr.Error("Choose a video first.")@@ -60,14 +72,21 @@ def _process_video(options = ConversionOptions(ai_gpu_uuid=ai_gpu_uuid,video_gpu_uuid=video_gpu_uuid,- nr_preset=nr_preset,nr_style=nr_style,nr_intensity=nr_intensity,+ nr_passes=int(nr_passes),local_tone_strength=local_tone_strength,local_structure_strength=local_structure_strength,skin_structure_strength=skin_structure_strength,automatic_mask=parse_automatic_mask(automatic_mask),- dlss_model_preset=dlss_model_preset,+ nr_color_strength=float(nr_color_strength),+ tone_preservation=float(tone_preservation),+ face_skin_protection=float(face_skin_protection),+ grain_preservation=float(grain_preservation),+ shimmer_suppression=float(shimmer_suppression),+ mask_feather=int(mask_feather),+ nr_mask=nr_mask,+ nr_gpu_mode=nr_gpu_mode,upscaling_factor=upscaling_factor,codec=effective_codec,container=effective_container,@@ -82,10 +101,26 @@ def _process_video(progress(value, desc=message)try:- result = convert_video(input_path, options, progress=report)+ result = convert_video(+ input_path, options, progress=report, output_dir=output_dir,+ controller=controller,+ )+ except Cancelled:+ return None, "Preview cancelled."except Exception as exc:traceback.print_exc()return None, f"Failed: {exc}"++ def finish(media_path: str | None, status: str) -> tuple[str | None, str]:+ if ephemeral_preview:+ # report_path now points at the shared session log (or a kept+ # .err file): only remove legacy per-job report JSONs.+ if Path(result.report_path).name.endswith(".report.json"):+ Path(result.report_path).unlink(missing_ok=True)+ if media_path and Path(media_path).resolve() != Path(result.output_path).resolve():+ Path(result.output_path).unlink(missing_ok=True)+ return media_path, status+source_name = Path(input_path).nameif is_preview:# Truncated previews normally show the encoded file directly. In Auto@@ -100,40 +135,42 @@ def _process_video(playable = Falseif not playable:try:- output_preview = make_browser_preview(result.output_path)+ output_preview = make_browser_preview(+ result.output_path, dest_dir=output_dir, controller=controller,+ )derived_note = " (browser preview transcoded to H.264)"except Exception:output_preview = result.output_pathif preview_frames is not None:- return output_preview, (+ return finish(output_preview, (f"One-frame preview complete for {source_name} on {result.gpu} "f"in {result.elapsed_seconds:.1f}s. "- f"DLSS {result.dlss_mode}: {result.render_width}×{result.render_height} → "- f"{result.output_width}×{result.output_height}. Signed feature 18 confirmed."+ f"Neural dimensions {result.render_width}×{result.render_height}; "+ f"{result.resize_method}, {result.memory_path}. Feature 18 confirmed."f"{derived_note}"- )- return output_preview, (+ ))+ return finish(output_preview, (f"Preview complete for {source_name}: {result.frames} frames from the first "f"{PREVIEW_SECONDS:g} seconds processed "- f"on {result.gpu} in {result.elapsed_seconds:.1f}s. DLSS {result.dlss_mode}: "- f"{result.render_width}×{result.render_height} → {result.output_width}×{result.output_height}. "- "All frames returned success with signed feature 18 confirmed."+ f"on {result.gpu} in {result.elapsed_seconds:.1f}s. Neural dimensions "+ f"{result.render_width}×{result.render_height}; {result.resize_method}, "+ f"{result.memory_path}. All frames returned feature-18 success."f"{derived_note}"- )+ ))output_preview, used_derivative = resolve_final_preview(- result.output_path, preview_mode+ result.output_path, preview_mode, bounded_proxy=True)status = (f"Complete: {result.frames} frames processed on {result.gpu} in {result.elapsed_seconds:.1f}s. "- f"All {result.nr_count_evidence} frames returned success with signed feature 18 confirmed. "- f"DLSS {result.dlss_mode}: {result.render_width}×{result.render_height} → "- f"{result.output_width}×{result.output_height}."+ f"All {result.nr_count_evidence} frames returned feature-18 success. "+ f"Neural dimensions {result.render_width}×{result.render_height}; "+ f"{result.resize_method}, {result.memory_path}.")if used_derivative:- status += " Browser preview transcoded to H.264; the original file is unchanged."+ status += " A short H.264 browser proxy was created; the complete original output is unchanged."elif output_preview is None:status += f" {effective_container} output was created successfully, but browser preview is unavailable."- return output_preview, status+ return finish(output_preview, status)def normalize_video_paths(paths: list[str] | str | None) -> list[str]:if not paths:@@ -153,7 +190,7 @@ def update_video_preview_mode(paths: list[str] | str | None):input_value = normalized[0] if available else Noneinput_label = "Input video preview" if len(normalized) <= 1 else f"Input video preview (first of {len(normalized)})"return (- gr.update(value=input_value, visible=available, label=input_label),+ gr.update(value=input_value, visible=True if available else "hidden", label=input_label),gr.update(value=None, visible=True),gr.update(visible=single),gr.update(visible=single),@@ -161,15 +198,22 @@ def update_video_preview_mode(paths: list[str] | str | None):def preview_video(input_path: list[str] | str | None,- nr_preset: str,nr_style: str,nr_intensity: float,+ nr_passes: float,local_tone_strength: float,local_structure_strength: float,skin_structure_strength: float,upscaling_factor: float,automatic_mask: str,- dlss_model_preset: str,+ nr_color_strength: float,+ tone_preservation: float,+ face_skin_protection: float,+ grain_preservation: float,+ mask_feather: float,+ shimmer_suppression: float,+ nr_mask: object | None,+ nr_gpu_mode: bool,codec: str,container: str,quality: str,@@ -178,8 +222,10 @@ def preview_video():selected = first_video_path(input_path)return _process_video(- selected, nr_preset, nr_style, nr_intensity, local_tone_strength, local_structure_strength,- skin_structure_strength, upscaling_factor, automatic_mask, dlss_model_preset,+ selected, nr_style, nr_intensity, nr_passes, local_tone_strength, local_structure_strength,+ skin_structure_strength, upscaling_factor, automatic_mask,+ nr_color_strength, tone_preservation, face_skin_protection, grain_preservation,+ mask_feather, shimmer_suppression, nr_mask, nr_gpu_mode,codec, container, quality, hdr_mode,progress, PREVIEW_SECONDS, None)@@ -187,25 +233,36 @@ def preview_video(def preview_one_frame(input_path: list[str] | str | None,- nr_preset: str,nr_style: str,nr_intensity: float,+ nr_passes: float,local_tone_strength: float,local_structure_strength: float,skin_structure_strength: float,upscaling_factor: float,automatic_mask: str,- dlss_model_preset: str,+ nr_color_strength: float,+ tone_preservation: float,+ face_skin_protection: float,+ grain_preservation: float,+ mask_feather: float,+ shimmer_suppression: float,+ nr_mask: object | None,+ nr_gpu_mode: bool,codec: str,container: str,quality: str,hdr_mode: bool = False,progress=gr.Progress(track_tqdm=False),+ *, output_dir=None, controller=None, ephemeral_preview=False,):selected = first_video_path(input_path)return _process_video(- selected, nr_preset, nr_style, nr_intensity, local_tone_strength, local_structure_strength,- skin_structure_strength, upscaling_factor, automatic_mask, dlss_model_preset,+ selected, nr_style, nr_intensity, nr_passes, local_tone_strength, local_structure_strength,+ skin_structure_strength, upscaling_factor, automatic_mask,+ nr_color_strength, tone_preservation, face_skin_protection, grain_preservation,+ mask_feather, shimmer_suppression, nr_mask, nr_gpu_mode,codec, container, quality, hdr_mode,- progress, None, 1+ progress, None, 1, output_dir=output_dir, controller=controller,+ ephemeral_preview=ephemeral_preview,)diff --git a/src/neural_rendering/video/processor.py b/src/neural_rendering/video/processor.pyindex b52d5be..50f4d28 100644--- a/src/neural_rendering/video/processor.py+++ b/src/neural_rendering/video/processor.py@@ -1,6 +1,5 @@from __future__ import annotations-import jsonimport mathimport osimport queue@@ -9,27 +8,27 @@ import subprocessimport threadingimport timefrom contextlib import nullcontext, suppress-from dataclasses import asdict, replace+from dataclasses import replacefrom fractions import Fractionfrom pathlib import Pathfrom typing import Callableimport avimport numpy as np+from av.codec.hwaccel import HWAccel-from ...core import ffmpeg+from ...core import app_log, ffmpegfrom ...core.gpu_selection import resolve_runtime_ai_gpufrom ...core.jobs import Cancelled, active_jobfrom ...core.naming import output_filename, validate_renamefrom ...core.disk_paths import OutputFile, prepare_output_dirfrom ...core.render_metadata import prepare_render_note-from ...core.paths import JOBS, LOGS, OUTPUTS+from ...core.paths import JOBS, OUTPUTSfrom ...core.runtime import (- DLSSFrameSession, prepare_runtime, resize_fit, rotate_frame, verify_feature_18,- write_failure_report,+ DLSSFrameSession, prepare_runtime, resize_fit, rotate_frame,)-from .guides import TemporalGuideGenerator-from .models import ConversionOptions, ConversionResult, DLSS_MODEL_PRESETS+from .guides import GuideFrame, TemporalGuideGenerator+from .models import ConversionOptions, ConversionResultfrom .sizing import resolve_native_settings, resolve_output_size, resolve_upscaling_modevalidate_codec_container = ffmpeg.validate_codec_container@@ -95,6 +94,20 @@ def convert_video(with job_context as controller:assert controller is not None+ if options.nr_gpu_mode and ffmpeg._is_nvenc_codec(options.codec):+ from .cuda_pipeline import convert_video_cuda_nvenc++ return convert_video_cuda_nvenc(+ source,+ options,+ preview_seconds=preview_seconds,+ preview_frames=preview_frames,+ compat_preview=compat_preview,+ prepared_runtime=prepared_runtime,+ controller=controller,+ progress=progress,+ output_dir=output_dir,+ )started = time.perf_counter()def _report_progress(value: float, desc: str) -> None:@@ -147,6 +160,13 @@ def convert_video(try:stage_started = time.perf_counter()metadata = ffmpeg.probe_video(source, count_mode="metadata", controller=controller)+ color_space = str(metadata.get("color_space") or "").casefold()+ color_matrix = 2 if "2020" in color_space else (+ 0 if color_space in {"bt470bg", "smpte170m", "smpte240m", "fcc"} else 1+ )+ color_range = int(+ str(metadata.get("color_range") or "").casefold() in {"pc", "jpeg", "full"}+ )declared_frames = int(metadata["frames"])estimated_frames = declared_frames or max(1, int(math.ceil(float(metadata["duration"]) * float(metadata["fps"])))@@ -168,13 +188,6 @@ def convert_video(output_width, output_height = resolve_output_size(input_width, input_height, factor)- video_gpu = ffmpeg.resolve_video_gpu(- prepared_runtime.gpus,- options.video_gpu_uuid,- "H.264" if compat_preview else options.codec,- output_width,- output_height,- )# HDR metadata to copy – 10-bit path when HDR Mode is onhdr_metadata = Noneeffective_hdr = hdr_requested and (not is_preview or not compat_preview)@@ -186,8 +199,8 @@ def convert_video("hdr": bool(metadata.get("hdr", False)),}destination = prepare_output_dir(output_dir, default=OUTPUTS)- LOGS.mkdir(exist_ok=True)JOBS.mkdir(exist_ok=True)+ app_log.info("video-render", f"start src={source.name}")stamp = time.strftime("%Y%m%d-%H%M%S") + f"-{time.time_ns() % 1_000_000:06d}"job_dir = JOBS / f"{source.stem}-{stamp}-{os.getpid()}"job_dir.mkdir(parents=True, exist_ok=False)@@ -213,19 +226,54 @@ def convert_video(f"{source.stem}_{output_kind}_{stamp}",)output_file = OutputFile(output)- temp_video = job_dir / "processed-video.mkv"+ temp_video = job_dir / (f"processed-video{extension}" if preview_frames is not None else "processed-video.mkv")native = resolve_native_settings(options)+ metadata_diagnostics: dict = {}+ render_note = prepare_render_note(options, metadata_diagnostics)+ video_duration = float(metadata.get("video_stream_duration") or 0.0)+ direct_mux = (+ options.container in {"MP4", "MOV"}+ and preview_frames is None+ and (preview_seconds is not None or video_duration > 0.0)+ )+ direct_comment = (+ ffmpeg.prepare_mux_comment(+ source, options.container, render_note, metadata_diagnostics, controller+ )+ if direct_mux and not is_preview else None+ )+ if direct_mux and is_preview:+ metadata_diagnostics.update(status="skipped", reason="preview_fast_path")+ encoder_target = output_file.temporary if direct_mux else temp_video+ audio_duration = (+ float(preview_seconds)+ if preview_seconds is not None+ else (video_duration if video_duration > 0.0 else None)+ )_report_progress(0.01, f"Starting feature 18 on {gpu['display_name']}")encoder_setup: list[tuple] = []encoding_stage_started = time.perf_counter()def prepare_encoder() -> None:+ nonlocal video_gpuencoder_started = time.perf_counter()try:+ # GPU ON co-locates NVENC with feature 18 on the AI GPU.+ # GPU OFF retains the separate Video Processing selection.+ encoder_gpu_uuid = (+ str(gpu["uuid"]) if options.nr_gpu_mode else options.video_gpu_uuid+ )+ video_gpu = ffmpeg.resolve_video_gpu(+ prepared_runtime.gpus,+ encoder_gpu_uuid,+ "H.264" if compat_preview else options.codec,+ output_width,+ output_height,+ )encoder_setup.append(ffmpeg.start_encoder(- temp_video,+ encoder_target,options.codec,options.quality,controller,@@ -237,6 +285,12 @@ def convert_video(hdr_mode=effective_hdr,hdr_metadata=hdr_metadata,preserve_timestamps=not metadata["cfr"],+ speed_profile="preview" if compat_preview else "neural",+ source_audio=source if direct_mux else None,+ direct_container=options.container if direct_mux else None,+ comment=direct_comment,+ audio_duration=audio_duration if direct_mux else None,+ include_source_metadata=not is_preview,))except BaseException as exc:@@ -250,6 +304,21 @@ def convert_video(target=prepare_encoder, name="dlss5-encoder-setup", daemon=True)encoder_setup_thread.start()+ preopened_decoder = None+ # Final-residual stabilization needs the original RGBA frame after+ # composition. Keep decode on the host for this non-NVENC path;+ # feature 18 and optical flow still execute on the selected GPU.+ if options.nr_gpu_mode and float(options.shimmer_suppression) <= 0.0:+ # FFmpeg's CUDA hwdevice must be created before NGX retains the+ # CUDA primary context on affected Windows driver versions.+ decode_device = HWAccel(+ "cuda",+ device=str(int(gpu.get("cuda_ordinal", gpu.get("index", 0)))),+ allow_software_fallback=True,+ options={"primary_ctx": "1"},+ is_hw_owned=True,+ )+ preopened_decoder = av.open(str(source), hwaccel=decode_device)session_started = time.perf_counter()session = DLSSFrameSession(input_width=input_width,@@ -261,6 +330,7 @@ def convert_video(factor=factor,mode=mode,native_settings=native,+ composition_mask=options.nr_mask,gpu=gpu,runtime_bundle=runtime_bundle,controller=controller,@@ -298,7 +368,7 @@ def convert_video() = encoder_setup[0]assert encoder.stdin is not None- prepared_bytes = render_width * render_height * 8+ prepared_bytes = render_width * render_height * 4rendered_bytes = output_width * output_height * 4queue_slots = max(1,@@ -306,6 +376,9 @@ def convert_video()prepared_frames: queue.Queue[object] = queue.Queue(maxsize=queue_slots)rendered_frames: queue.Queue[object] = queue.Queue(maxsize=queue_slots)+ output_pool: queue.LifoQueue[np.ndarray] = queue.LifoQueue(maxsize=queue_slots + 1)+ for _ in range(queue_slots + 1):+ output_pool.put(np.empty((output_height, output_width, 4), dtype=np.uint8))stop_marker = object()def put_pipeline(target: queue.Queue[object], item: object) -> bool:@@ -319,59 +392,123 @@ def convert_video(continuereturn False+ def get_reusable(target: queue.LifoQueue[np.ndarray]) -> np.ndarray | None:+ while not pipeline_stop.is_set():+ if controller.cancel.is_set():+ return None+ try:+ return target.get(timeout=0.1)+ except queue.Empty:+ if not pipeline_errors.empty():+ return None+ continue+ return None+def produce_frames() -> None:producer_started = time.perf_counter()decoded = 0container = None+ decode_seconds = 0.0+ prepare_seconds = 0.0+ guide_seconds = 0.0+ queue_wait_seconds = 0.0+ hardware_frames = 0+ software_frames = 0try:- container = av.open(str(source))+ if preopened_decoder is not None:+ container = preopened_decoder+ else:+ container = av.open(str(source))stream = container.streams.video[0]stream.thread_type = "AUTO"guides = TemporalGuideGenerator(render_width, render_height)first_time: float | None = Nonerate = float(stream.average_rate or 30)- for index, frame in enumerate(container.decode(stream)):+ default_duration = max(+ 1,+ round(Fraction(1, 1) / metadata["rate"] / metadata["time_base"]),+ )+ decoder = iter(container.decode(stream))+ while True: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:+ # Check a frame-count preview before asking the decoder for+ # another frame. The old for-loop decoded one unnecessary+ # frame for every one-frame preview.+ if preview_frames is not None and decoded >= preview_frames:producer_stats["completion_reason"] = "preview_limit"break+ decode_started = time.perf_counter()+ try:+ frame = next(decoder)+ except StopIteration:+ decode_seconds += time.perf_counter() - decode_started+ producer_stats["completion_reason"] = "eof"+ break+ decode_seconds += time.perf_counter() - decode_started+ index = decoded+ pts = int(+ frame.pts if frame.pts is not None else decoded * default_duration+ )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- )+ timestamp = float(Fraction(pts) * metadata["time_base"])if first_time is None:first_time = timestampif decoded and timestamp - first_time >= preview_seconds:producer_stats["completion_reason"] = "preview_limit"breakif 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"]+ raise RuntimeError(+ f"The video decoder marked source frame {index} as corrupt."+ )+ if frame.format.name == "cuda":+ hardware_frames += 1+ guide_started = time.perf_counter()+ scene_score, reset = session.score_cuda_frame(+ frame, color_matrix=color_matrix, color_range=color_range+ )+ guide = GuideFrame(reset=reset, scene_score=scene_score)+ guide_seconds += time.perf_counter() - guide_started+ prepared = frame+ else:+ software_frames += 1+ prepare_started = time.perf_counter()+ rgba = rotate_frame(+ frame.to_ndarray(format="rgba"), metadata["rotation"]+ )+ if rgba.shape[1] != render_width or rgba.shape[0] != render_height:+ rgba = resize_fit(rgba, render_width, render_height)+ rgba = np.ascontiguousarray(rgba, dtype=np.uint8)+ prepare_seconds += time.perf_counter() - prepare_started+ guide_started = time.perf_counter()+ guide = guides.process(rgba)+ guide_seconds += time.perf_counter() - guide_started+ prepared = rgba+ duration = int(getattr(frame, "duration", None) or default_duration)+ queue_started = time.perf_counter()+ queued = put_pipeline(+ prepared_frames,+ (index, prepared, guide, pts, duration),)- if rgba.shape[1] != render_width or rgba.shape[0] != render_height:- rgba = resize_fit(rgba, render_width, render_height)- rgba = np.ascontiguousarray(rgba, dtype=np.uint8)- guide = guides.process(rgba)- pts = int(frame.pts if frame.pts is not None else index)- if not put_pipeline(- prepared_frames, (index, rgba, guide, pts)- ):+ queue_wait_seconds += time.perf_counter() - queue_started+ if not queued:returndecoded += 1- else:- producer_stats["completion_reason"] = "eof"put_pipeline(prepared_frames, stop_marker)+ producer_stats["duplicate_frames"] = guides.duplicate_framesexcept BaseException as exc:record_pipeline_error(exc)put_pipeline(prepared_frames, stop_marker)finally:producer_stats["decoded_frames"] = decoded+ producer_stats["decode_seconds"] = decode_seconds+ producer_stats["frame_prepare_seconds"] = prepare_seconds+ producer_stats["guide_seconds"] = guide_seconds+ producer_stats["queue_wait_seconds"] = queue_wait_seconds+ producer_stats["hardware_frames"] = hardware_frames+ producer_stats["software_frames"] = software_framesif container is not None:with suppress(Exception):container.close()@@ -380,42 +517,46 @@ def convert_video(def write_frames() -> None:writer_started = time.perf_counter()written = 0+ queue_wait_seconds = 0.0+ mux_seconds = 0.0nut = Nonetry:- nut = av.open(encoder.stdin, mode="w", format="nut")- raw_stream = nut.add_stream("rawvideo", rate=metadata["rate"])- raw_stream.width = output_width- 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"]+ nut = ffmpeg.RawVideoPacketMuxer(+ encoder.stdin,+ width=output_width,+ height=output_height,+ rate=metadata["rate"],+ time_base=metadata["time_base"],+ )while not pipeline_stop.is_set():if controller.cancel.is_set():raise Cancelled("Render stopped by user.")+ queue_started = time.perf_counter()try:item = rendered_frames.get(timeout=0.1)except queue.Empty:+ queue_wait_seconds += time.perf_counter() - queue_startedcontinue+ queue_wait_seconds += time.perf_counter() - queue_startedif item is stop_marker:break- processed, output_pts = item- output_frame = av.VideoFrame.from_ndarray(processed, format="rgba")- output_frame.pts = output_pts- output_frame.time_base = metadata["time_base"]- for packet in raw_stream.encode(output_frame):- nut.mux(packet)- written += 1+ processed, output_pts, duration = item+ try:+ mux_started = time.perf_counter()+ nut.write(processed, output_pts, duration)+ mux_seconds += time.perf_counter() - mux_started+ written += 1+ finally:+ output_pool.put(processed)if not pipeline_stop.is_set():- for packet in raw_stream.encode():- nut.mux(packet)nut.close()nut = Noneexcept BaseException as exc:record_pipeline_error(exc)finally:writer_stats["written_frames"] = written+ writer_stats["queue_wait_seconds"] = queue_wait_seconds+ writer_stats["raw_mux_seconds"] = mux_secondsif nut is not None:with suppress(Exception):nut.close()@@ -445,22 +586,45 @@ def convert_video(continueif item is stop_marker:break- index, rgba, guide, pts = item+ index, prepared, guide, pts, duration = itemscene_resets += int(guide.reset and index != 0)+ output_buffer = get_reusable(output_pool)+ if output_buffer is None:+ if not pipeline_errors.empty():+ raise pipeline_errors.get_nowait()+ raise Cancelled("Render stopped by user.")dlss_started = time.perf_counter()- processed, out_pts = session.process(- index=index,- rgba=rgba,- motion=guide.motion,- reset=guide.reset,- pts=pts,- )+ try:+ if isinstance(prepared, av.VideoFrame) and prepared.format.name == "cuda":+ processed, out_pts = session.process_cuda_frame_to_host(+ index=index,+ frame=prepared,+ reset=guide.reset,+ scene_score=guide.scene_score,+ pts=pts,+ color_matrix=color_matrix,+ color_range=color_range,+ rotation=int(metadata["rotation"]),+ output_buffer=output_buffer,+ )+ else:+ processed, out_pts = session.process(+ index=index,+ rgba=prepared,+ reset=guide.reset,+ pts=pts,+ output_buffer=output_buffer,+ )+ except BaseException:+ output_pool.put(output_buffer)+ raisedlss_seconds += time.perf_counter() - dlss_startedif is_preview:if preview_pts_origin is None:preview_pts_origin = out_ptsout_pts -= preview_pts_origin- if not put_pipeline(rendered_frames, (processed, out_pts)):+ if not put_pipeline(rendered_frames, (processed, out_pts, duration)):+ output_pool.put(processed)if not pipeline_errors.empty():raise pipeline_errors.get_nowait()raise Cancelled("Render stopped by user.")@@ -497,14 +661,23 @@ def convert_video(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["source_decode_seconds"] = float(producer_stats.get("decode_seconds", 0.0))+ timings["frame_prepare_seconds"] = float(producer_stats.get("frame_prepare_seconds", 0.0))+ timings["guide_seconds"] = float(producer_stats.get("guide_seconds", 0.0))+ timings["producer_queue_wait_seconds"] = float(producer_stats.get("queue_wait_seconds", 0.0))timings["dlss_seconds"] = dlss_secondstimings["encoder_feed_seconds"] = float(writer_stats.get("seconds", 0.0))+ timings["raw_nut_mux_seconds"] = float(writer_stats.get("raw_mux_seconds", 0.0))+ timings["writer_queue_wait_seconds"] = float(writer_stats.get("queue_wait_seconds", 0.0))if encoder.stdin and not encoder.stdin.closed:encoder.stdin.close()session.close()- frame_accounting["worker_completed_frames"] = session.completed_frames+ for name, value in session.process_timings.items():+ timings[f"native_{name}"] = float(value)+ frame_accounting["bridge_completed_frames"] = session.completed_frames+ frame_accounting["duplicate_frames"] = int(producer_stats.get("duplicate_frames", 0))if session.completed_frames != delivered:- raise RuntimeError("Native worker completion does not match the processed frame count.")+ raise RuntimeError("Bridge completion does not match the processed frame count.")encoder_code = encoder.wait(timeout=120)encoder_log_thread.join(timeout=2)controller.unregister(encoder)@@ -514,59 +687,75 @@ 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)+ # Reaching clean EOF in the decoder, successful one-for-one pipeline+ # accounting, the native END acknowledgement, and a successful encoder+ # exit are authoritative. Do not decode the complete source a second+ # time just because its container omitted or misreported nb_frames.+ if producer_stats["completion_reason"] == "eof":+ frame_accounting["source_verification"] = "clean_decoder_eof"+ frame_accounting["metadata_corrected"] = bool(+ declared_frames and declared_frames != delivered+ )else:- timings["source_verification_seconds"] = 0.0+ frame_accounting["source_verification"] = "preview_limit"+ timings["source_verification_seconds"] = 0.0- feature_evidence = verify_feature_18(- session.worker_logs, session.reshade_log_text()+ hardware_frames = int(producer_stats.get("hardware_frames", 0))+ software_frames = int(producer_stats.get("software_frames", 0))+ session.diagnostics.decode_backend = (+ "nvdec" if hardware_frames and not software_frames else+ "software" if software_frames and not hardware_frames else+ "nvdec+software")+ session.diagnostics.encode_backend = selected_encodernr_count = delivered- nr_upscaling_requested = factor > 1.0- nr_upscaling_active = bool(feature_evidence["nr_upscaling_active"])- nr_native_fallback = bool(feature_evidence["nr_native_fallback"])- carrier_create_result = str(feature_evidence["carrier_create_result"])- _report_progress(0.91, "Muxing original audio and metadata")+ resize_method = "none" if factor == 1.0 else "lanczos"+ memory_path = session.diagnostics.memory_pathmux_started = time.perf_counter()- 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+ if direct_mux:+ # MP4/MOV are encoded together with source audio/metadata in the+ # same FFmpeg pass, eliminating the complete intermediate-video+ # write/read/remux cycle.+ _report_progress(0.93, "Finalizing direct audio/video mux")+ ffmpeg.verify_mux_comment(+ output_file.temporary, direct_comment, metadata_diagnostics, controller+ )+ timings["final_mux_seconds"] = 0.0+ elif preview_frames is not None:+ # A frame preview is a visual test, not a production master. The+ # encoder already wrote the requested container; avoid reopening the+ # source for audio/chapters/metadata and remuxing a one-frame file.+ _report_progress(0.93, "Finalizing one-frame preview")+ shutil.copyfile(temp_video, output_file.temporary)+ metadata_diagnostics.update(status="skipped", reason="preview_fast_path")+ timings["final_mux_seconds"] = 0.0+ else:+ _report_progress(0.91, "Muxing original audio and metadata")+ 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_startedtimings["muxing_seconds"] = timings["final_mux_seconds"]++ # Header/stream verification is enough on the normal success path. The+ # application already counted every decoded, DLSS-returned and written+ # frame; scanning or decoding the finished file again is pure O(file) I/O.verify_started = time.perf_counter()_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:+ verified = ffmpeg.probe_video(+ output_file.temporary, count_mode="metadata", controller=controller+ )+ declared_output_frames = int(verified.get("frames") or 0)+ frame_accounting["verified_output_frames"] = (+ declared_output_frames if declared_output_frames else delivered+ )+ frame_accounting["output_verification"] = (+ "container_frame_count" if declared_output_frames else "pipeline_accounting"+ )+ if declared_output_frames and declared_output_frames != delivered:raise RuntimeError(- f"Output verification found {verified['frames']} frames instead of "+ f"Output container reports {declared_output_frames} frames instead of "f"{delivered}.")if (verified["width"], verified["height"]) != (@@ -580,110 +769,27 @@ def convert_video(timings["verification_seconds"] = time.perf_counter() - verify_startedelapsed = 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),- "input_metadata": {- key: str(value) if isinstance(value, Fraction) else value- for key, value in metadata.items()- },- "output_metadata": {- key: str(value) if isinstance(value, Fraction) else value- for key, value in verified.items()- },- "gpu": gpu,- "ai_gpu": gpu,- "video_gpu": video_gpu,- "encoder": selected_encoder,- "encoding_quality": encoding_quality,- "frames_processed": delivered,- "frame_accounting": frame_accounting,- "render_mode": (- "full"- if not is_preview- else ("preview-frame" if preview_frames is not None else "preview")- ),- "dlss_mode": mode["name"],- "dlss_model_preset": options.dlss_model_preset,- "requested_dlss_model_preset": options.dlss_model_preset,- "requested_dlss_model_preset_code": native["dlss_model_preset"],- "applied_dlss_model_preset": session.applied_dlss_model_preset,- "applied_dlss_model_preset_name": next(- name- for name, code in DLSS_MODEL_PRESETS.items()- if code == session.applied_dlss_model_preset- ),- "requested_upscaling_factor": factor,- "input_dimensions": {"width": input_width, "height": input_height},- "negotiated_render_dimensions": {- "width": render_width,- "height": render_height,- },- "negotiated_render_range": {- "minimum": {"width": minimum_width, "height": minimum_height},- "maximum": {"width": maximum_width, "height": maximum_height},- },- "output_dimensions": {"width": output_width, "height": output_height},- "effective_factor": {- "width": output_width / input_width,- "height": output_height / input_height,- },- "nr_upscaling_requested": nr_upscaling_requested,- "nr_upscaling_active": nr_upscaling_active,- "nr_native_fallback": nr_native_fallback,- "ngx_setup_result": f"0x{setup_result:08X}",- "scene_resets": scene_resets,- "pipeline": "renodx-dlssnr-feature18",- "feature_id": 18,- "feature_18_confirmed": True,- "carrier_create_result": carrier_create_result,- "successful_neural_rendering_frames": nr_count,- "addon_release": runtime_bundle["addon"]["release"],- "dlssnr_runtime": runtime_bundle["neural_runtime"],- "loaded_module_inventory": [- "host/nvngx.dll (standalone worker image)",- "host/dxgi.dll (ReShade carrier)",- "dlssnr/renodx-dlss5.addon64",- "dlss/nvngx_dlss.dll",- "dlssnr/nvngx_dlssnr.dll",- "system D3D12/DXGI/NGX core",- ],- "native_settings": native,- "elapsed_seconds": elapsed,- "average_fps": delivered / elapsed,- "timings": timings,- "worker_log": session.worker_logs,- "worker_log_dropped_lines": session.worker_log_dropped_lines,- "encoder_log": encoder_logs,- "dlssnr_evidence": feature_evidence["evidence"],- }- report_path = LOGS / f"{output.name}.report.json"- report_path.write_text(json.dumps(report, indent=2), encoding="utf-8")+ report_path = app_log.session_path()if controller.cancel.is_set():+ app_log.info("video-render", f"cancelled src={source.name} frames={delivered}")raise Cancelled("Render stopped by user.")+ app_log.info(+ "video-render",+ f"done src={source.name} out={output.name} frames={delivered} "+ f"elapsed={elapsed:.0f}s avg={delivered / max(elapsed, 1e-9):.1f}fps",+ )output_file.publish()_report_progress(1.0, "Complete — feature 18 confirmed")return ConversionResult(- str(output),- str(report_path),- delivered,- nr_count,- elapsed,- gpu["display_name"],- input_width,- input_height,- render_width,- render_height,- output_width,- output_height,- factor,- str(mode["name"]),- options.dlss_model_preset,- session.applied_dlss_model_preset,+ output_path=str(output), report_path=report_path, frames=delivered,+ nr_count_evidence=nr_count, elapsed_seconds=elapsed,+ gpu=gpu["display_name"], input_width=input_width,+ input_height=input_height, render_width=render_width,+ render_height=render_height, output_width=output_width,+ output_height=output_height, upscaling_factor=factor,+ neural_dimensions={"width": render_width, "height": render_height},+ resize_method=resize_method, memory_path=memory_path,+ bridge_status=session.structured_status(),)except Exception as exc:was_cancelled = controller.cancel.is_set()@@ -711,25 +817,16 @@ def convert_video(if was_cancelled and not isinstance(exc, Cancelled):raise Cancelled("Render stopped by user.") from excif isinstance(exc, Cancelled):+ app_log.info("video-render", f"cancelled src={source.name}")raise- worker_logs = session.worker_logs if session is not None else []- reshade_lines = session.reshade_diagnostics() if session is not None else []- worker_code = session.worker.poll() if session is not None else None- report_path = write_failure_report(- operation="video-render",- source=str(source),- error=exc,- gpu=gpu,- runtime_bundle=runtime_bundle,- 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+ tails: dict[str, object] = {}+ if session is not None:+ with suppress(Exception):+ tails["bridge"] = session.bridge_logs[-20:]+ if "encoder_logs" in locals() and encoder_logs:+ tails["ffmpeg"] = list(encoder_logs)[-40:]+ report_path = app_log.fail("video-render", f"video-render-{source.stem}", exc, tails or None)+ raise RuntimeError(f"{exc}\nDetails: {report_path}") from excfinally:if output_file is not None:output_file.cleanup()@@ -754,3 +851,6 @@ def convert_video(stream.close()if job_dir and job_dir.exists():shutil.rmtree(job_dir, ignore_errors=True)+ if "preopened_decoder" in locals() and preopened_decoder is not None:+ with suppress(Exception):+ preopened_decoder.close()diff --git a/src/neural_rendering/video/reports.py b/src/neural_rendering/video/reports.pyindex 0886adb..3ef926a 100644--- a/src/neural_rendering/video/reports.py+++ b/src/neural_rendering/video/reports.py@@ -1,11 +1,9 @@from __future__ import annotations-import json-from dataclasses import asdict--from ...core.paths import LOGS+from ...core import app_logfrom .models import ConversionOptions, VideoConversionFailure, VideoConversionSuccess+def _write_video_batch_manifest(stamp: str,options: ConversionOptions,@@ -14,15 +12,6 @@ def _write_video_batch_manifest(cancelled: bool,*, batch_diagnostics: dict | None = None, output_dir: str | None = None,) -> str:- LOGS.mkdir(exist_ok=True)- 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": output_dir,- }- manifest_path = LOGS / f"DLSS5_VIDEO_BATCH_{stamp}.manifest.json"- manifest_path.write_text(json.dumps(manifest, indent=2), encoding="utf-8")- return str(manifest_path)+ status = "cancelled" if cancelled else ("partial" if failures else "success")+ app_log.info("video-batch", f"{status} ok={len(successes)} failed={len(failures)}")+ return app_log.session_path()diff --git a/src/neural_rendering/video/ui.py b/src/neural_rendering/video/ui.pyindex cbf292f..d4a3965 100644--- a/src/neural_rendering/video/ui.py+++ b/src/neural_rendering/video/ui.py@@ -7,14 +7,12 @@ from pathlib import Pathimport gradio as grfrom ...core.batch_ui import (BATCH_HEADERS, bind_batch_ui, build_media_clear_button, build_media_select_button,- build_path_controls,+ build_path_controls, build_save_controls,)-from ...core.disk_paths import create_media_archive-from ...core.ffmpeg import hdr_mode_supportedfrom ...core.ffmpeg.preview import normalize_preview_encoding, resolve_final_previewfrom ...core.naming import RENAME_MODES-from ...core.runtime import DLSS_MODEL_PRESETS, NR_PRESETS, NR_STYLES, UPSCALING_MODES+from ...core.runtime import NR_STYLES, UPSCALING_MODESfrom ...settings.models import (AUTOMATIC_MASK_CHOICES, CODEC_CHOICES, CONTAINER_CHOICES, QUALITY_CHOICES, UISettings,automatic_mask_choice, coerce_hdr_mode, parse_automatic_mask,@@ -25,6 +23,7 @@ from .models import ConversionOptionsfrom .preview import (_process_video, normalize_video_paths, preview_one_frame, preview_video, update_video_preview_mode,)+from ..composition_ui import CompositionWidgets, build_composition_sliders, build_composition_widgetsdef hdr_mode_update(codec: str):@@ -42,74 +41,73 @@ UPSCALING_CHOICES = tuple((mode["label"], factor) for factor, mode in UPSCALING_def build_neural_controls(settings: UISettings):- nr_preset = gr.Dropdown(- list(NR_PRESETS), value=settings.nr_preset, label="NR Preset",- info="Experimental content-dependent neural-rendering model hint. Default is recommended."- )nr_style = gr.Radio(list(NR_STYLES), value=settings.nr_style, label="NR Style",- info="Selects the native neural-rendering style.")upscaling_factor = gr.Dropdown(choices=list(UPSCALING_CHOICES), value=settings.upscaling_factor,- label="Upscaling factor", info="Uses NVIDIA's fixed DLSS modes. DLAA keeps source resolution."+ label="Scale",+ )+ # Each control is created directly in the parent Column (no gr.Row), so+ # every slider spans the full width in one vertical stack. Creation order+ # sets the visual order; the returned list keeps the canonical positional+ # order consumed by render/persist/settings-mirror code.+ nr_intensity = gr.Slider(+ 0.0, 2.0, value=settings.nr_intensity, step=0.05, precision=2,+ label="NR Intensity", buttons=["reset"],+ )+ nr_passes = gr.Slider(+ 1, 4, value=settings.nr_passes, step=1, precision=0,+ label="NR Passes", buttons=["reset"],+ )+ local_tone_strength = gr.Slider(+ 0.0, 2.0, value=settings.local_tone_strength, step=0.05, precision=2,+ label="Local Tone Strength", buttons=["reset"]+ )+ local_structure_strength = gr.Slider(+ 0.0, 2.0, value=settings.local_structure_strength, step=0.05, precision=2,+ label="Local Structure Strength", buttons=["reset"],+ )+ skin_structure_strength = gr.Slider(+ -1.0, 2.0, value=settings.skin_structure_strength, step=0.05, precision=2,+ label="Skin Structure Strength",+ buttons=["reset"],+ )+ composition = build_composition_sliders(settings)+ shimmer_suppression = gr.Slider(+ 0.0, 1.0, value=settings.shimmer_suppression, step=0.05, precision=2,+ label="Shimmer Suppression", buttons=["reset"],)- with gr.Row():- nr_intensity = gr.Slider(- 0.0, 2.0, value=settings.nr_intensity, step=0.05, precision=2,- label="NR Intensity", info="Overall neural-rendering strength.", buttons=["reset"]- )- local_tone_strength = gr.Slider(- 0.0, 2.0, value=settings.local_tone_strength, step=0.05, precision=2,- label="Local Tone Strength", info="Local tone and contrast enhancement.", buttons=["reset"]- )- with gr.Row():- local_structure_strength = gr.Slider(- 0.0, 2.0, value=settings.local_structure_strength, step=0.05, precision=2,- label="Local Structure Strength", info="Local detail and texture structure.", buttons=["reset"]- )- skin_structure_strength = gr.Slider(- -1.0, 2.0, value=settings.skin_structure_strength, step=0.05, precision=2,- label="Skin Structure Strength", info="Skin-specific structure; -1.00 is the native default.",- buttons=["reset"]- )automatic_mask = gr.Radio(choices=AUTOMATIC_MASK_CHOICES,value=automatic_mask_choice(settings.automatic_mask),label="Automatic Mask",- info=(- "Experimental runtime-generated mask that changes where Neural Rendering is "- "applied; it may cause flicker or inconsistent results."- ),)- return [- nr_preset, nr_style, nr_intensity, local_tone_strength, local_structure_strength,+ controls = [+ nr_style, nr_intensity, nr_passes, local_tone_strength, local_structure_strength,skin_structure_strength, upscaling_factor, automatic_mask- ]-+ ] + composition+ return [*controls, shimmer_suppression]-def build_dlss_model_control(settings: UISettings):- return gr.Dropdown(- choices=list(DLSS_MODEL_PRESETS),- value=settings.dlss_model_preset,- label="DLSS Model Preset",- info=(- "Default lets NVIDIA select its normal mode-specific presets. "- "J, K, L, or M forces that model preset for every DLSS scaling mode."- ),- )def render_video(input_path: str,- nr_preset: str,nr_style: str,nr_intensity: float,+ nr_passes: float,local_tone_strength: float,local_structure_strength: float,skin_structure_strength: float,upscaling_factor: float,automatic_mask: str,- dlss_model_preset: str,+ nr_color_strength: float,+ tone_preservation: float,+ face_skin_protection: float,+ grain_preservation: float,+ mask_feather: float,+ shimmer_suppression: float,+ nr_mask: object | None,+ nr_gpu_mode: bool,codec: str,container: str,quality: str,@@ -117,23 +115,32 @@ def render_video(progress=gr.Progress(track_tqdm=False),):return _process_video(- input_path, nr_preset, nr_style, nr_intensity, local_tone_strength, local_structure_strength,- skin_structure_strength, upscaling_factor, automatic_mask, dlss_model_preset,+ input_path, nr_style, nr_intensity, nr_passes, local_tone_strength, local_structure_strength,+ skin_structure_strength, upscaling_factor, automatic_mask,+ nr_color_strength, tone_preservation, face_skin_protection, grain_preservation,+ mask_feather, shimmer_suppression, nr_mask, nr_gpu_mode,codec, container, quality, hdr_mode,progress, None, None)def render_video_batch(input_paths: list[str] | str | None,- nr_preset: str,nr_style: str,nr_intensity: float,+ nr_passes: float,local_tone_strength: float,local_structure_strength: float,skin_structure_strength: float,upscaling_factor: float,automatic_mask: str,- dlss_model_preset: str,+ nr_color_strength: float,+ tone_preservation: float,+ face_skin_protection: float,+ grain_preservation: float,+ mask_feather: float,+ shimmer_suppression: float,+ nr_mask: object | None,+ nr_gpu_mode: bool,codec: str,container: str,quality: str,@@ -142,7 +149,7 @@ def render_video_batch(custom_suffix: str,progress=gr.Progress(track_tqdm=False),*, output_dir=None, controller=None, on_item_update=None, direct_disk=False,-) -> tuple[object, str | None, list[list[str]], str]:+) -> tuple[object, list[str], list[list[str]], str]:paths = normalize_video_paths(input_paths)if not paths:raise gr.Error("Choose at least one video first.")@@ -150,14 +157,21 @@ def render_video_batch(options = ConversionOptions(ai_gpu_uuid=processing_gpu_settings()[0],video_gpu_uuid=processing_gpu_settings()[1],- nr_preset=nr_preset,nr_style=nr_style,nr_intensity=nr_intensity,+ nr_passes=int(nr_passes),local_tone_strength=local_tone_strength,local_structure_strength=local_structure_strength,skin_structure_strength=skin_structure_strength,automatic_mask=parse_automatic_mask(automatic_mask),- dlss_model_preset=dlss_model_preset,+ nr_color_strength=float(nr_color_strength),+ tone_preservation=float(tone_preservation),+ face_skin_protection=float(face_skin_protection),+ grain_preservation=float(grain_preservation),+ shimmer_suppression=float(shimmer_suppression),+ mask_feather=int(mask_feather),+ nr_mask=nr_mask,+ nr_gpu_mode=nr_gpu_mode,upscaling_factor=upscaling_factor,codec=codec,container=container,@@ -171,8 +185,10 @@ def render_video_batch(progress(value, desc=message)try:- result = convert_videos(paths, options, progress=report, output_dir=output_dir,- controller=controller, on_item_update=on_item_update)+ result = convert_videos(+ paths, options, progress=report, output_dir=output_dir, controller=controller,+ on_item_update=on_item_update, create_archive=False,+ )except Exception as exc:traceback.print_exc()if on_item_update is not None:@@ -184,9 +200,8 @@ def render_video_batch(conversion = item.resultdetails = (f"{conversion.frames} frames in {conversion.elapsed_seconds:.1f}s; "- f"DLSS {conversion.dlss_mode}: "- f"{conversion.render_width}×{conversion.render_height} → "- f"{conversion.output_width}×{conversion.output_height}; "+ f"Neural {conversion.render_width}×{conversion.render_height}; "+ f"{conversion.resize_method}, {conversion.memory_path}; "f"report: {conversion.report_path}")ordered_rows.append(@@ -217,7 +232,7 @@ def render_video_batch(if not direct_disk and len(paths) == 1 and result.successes and not result.cancelled:candidate = result.successes[0].result.output_pathoutput_preview, used_derivative = resolve_final_preview(- candidate, preview_mode, controller=controller+ candidate, preview_mode, controller=controller, bounded_proxy=True)failed_count = sum(not item.cancelled for item in result.failures)cancelled_count = sum(@@ -236,16 +251,9 @@ def render_video_batch(if result.failures:status += f"\nFirst error: {result.failures[0].error}"if used_derivative:- status += "\nBrowser preview transcoded to H.264; the original file is unchanged."- archive_path = None- if not direct_disk and not result.cancelled:- archive_path = create_media_archive(- (item.result.output_path for item in result.successes),- output_dir,- "DLSS5_VIDEO_BATCH",- controller=controller,- )- return gr.update(value=output_preview, visible=not direct_disk), archive_path, rows, status+ status += "\nA short H.264 browser proxy was created; the complete original output is unchanged."+ files = [item.result.output_path for item in result.successes]+ return gr.update(value=output_preview, visible=not direct_disk), files, rows, status@dataclass(slots=True)class VideoTab:@@ -255,7 +263,9 @@ class VideoTab:select_source: objectclear_source: objectneural: list[object]- model_preset: object+ composition: CompositionWidgets+ mask_state: object+ gpu_mode: objectquality: objectcodec: objectcontainer: object@@ -268,6 +278,8 @@ class VideoTab:stop: objectreset: objectoutput_video: object+ save_download: object+ zip_button: objectzip_download: objectstatus: objectresults: object@@ -278,26 +290,26 @@ class VideoTab:@propertydef render_inputs(self) -> list[object]:return [- self.sources, *self.neural, self.model_preset, self.codec, self.container,+ self.sources, *self.neural, self.mask_state, self.gpu_mode, self.codec, self.container,self.quality, self.hdr_mode, self.rename_mode, self.custom_suffix,]@propertydef preview_inputs(self) -> list[object]:return [- self.sources, *self.neural, self.model_preset, self.codec, self.container,+ self.sources, *self.neural, self.mask_state, self.gpu_mode, self.codec, self.container,self.quality, self.hdr_mode,]@propertydef settings_inputs(self) -> list[object]:return [- *self.neural, self.model_preset, self.codec, self.container, self.quality,+ *self.neural, self.codec, self.container, self.quality,self.hdr_mode, self.rename_mode, self.custom_suffix,]-def build_video_tab(settings: UISettings) -> VideoTab:+def build_video_tab(settings: UISettings, gpu_mode_state: object, mask_state: object) -> VideoTab:with gr.Row():with gr.Column(scale=3):sources = gr.File(@@ -305,7 +317,7 @@ def build_video_tab(settings: UISettings) -> VideoTab:type="filepath", allow_reordering=True, elem_id="video-upload-list",elem_classes=["media-upload-surface"],)- input_preview = gr.Video(label="Input video preview", interactive=False, visible=False)+ input_preview = gr.Video(label="Input video preview", interactive=False, visible="hidden")with gr.Row(visible=False, elem_id="video-input-actions",elem_classes=["media-input-actions"],@@ -314,47 +326,42 @@ def build_video_tab(settings: UISettings) -> VideoTab:"Choose Videos", ["video"], "video-select-input",)clear_source = build_media_clear_button("video-clear-input")- input_path, output_path = build_path_controls()- with gr.Accordion("DLSS 5 Neural Rendering Settings", open=True):+ with gr.Row():+ render = gr.Button("Render video(s)", variant="primary")+ stop = gr.Button("Stop", variant="stop")+ preview_frame = gr.Button("Preview 1 frame", visible=False)+ preview = gr.Button("Preview 3 sec", visible=False)+ reset = gr.Button("Reset settings")+ with gr.Column(elem_classes=["neural-controls-unified"]):neural = build_neural_controls(settings)- with gr.Accordion("DLSS 5 Settings", open=True):- model_preset = build_dlss_model_control(settings)+ composition = build_composition_widgets()+ input_path, output_path = build_path_controls()quality = gr.Radio(QUALITY_CHOICES, value=settings.quality, label="Encoding quality",- info="Auto uses output resolution, FPS, and codec. Good = Auto×2, Best = Auto×4, Max = CQ 0.",)with gr.Row():codec = gr.Dropdown(CODEC_CHOICES, value=settings.codec, label="Video codec",- info="Plain = CPU (libx264 / libx265 / libsvtav1). Suffixed (NVIDIA NVENC) = GPU. ProRes Proxy uses 10-bit 4:2:2 and requires MOV or MKV.",)container = gr.Dropdown(CONTAINER_CHOICES, value=settings.container, label="Container")with gr.Row():rename_mode = gr.Radio(RENAME_MODES, value=settings.video_rename_mode, label="Rename",- info="Auto adds the current DLSS5 timestamp; Copy keeps the original base name; Custom appends your suffix.",)custom_suffix = gr.Textbox(- value=settings.video_custom_suffix, label="Custom suffix", placeholder="_DLSS5",+ value=settings.video_custom_suffix, label="Custom suffix", placeholder="_Neural_Rendering",interactive=settings.video_rename_mode == "Custom",)hdr_mode = gr.Checkbox(value=settings.hdr_mode and hdr_mode_supported(settings.codec),label="HDR Mode",- info="When on: 10-bit output, copies input colorspace; keeps HDR if input is HDR. Only for H.265 / AV1 / ProRes.",interactive=hdr_mode_supported(settings.codec),)- with gr.Row():- preview_frame = gr.Button("Preview 1 frame", visible=False)- preview = gr.Button("Preview 3 sec", visible=False)- render = gr.Button("Render video(s)", variant="primary")- stop = gr.Button("Stop", variant="stop")- reset = gr.Button("Reset settings")with gr.Column(scale=3):output_video = gr.Video(label="Output video", interactive=False, visible=True, height=520,)- zip_download = gr.DownloadButton("Save as ZIP", visible=False)+ save_download, zip_button, zip_download = build_save_controls("video", "nr-video")status = gr.Textbox(label="Status", interactive=False, lines=5, max_lines=12)results = gr.Dataframe(headers=BATCH_HEADERS,@@ -362,9 +369,9 @@ def build_video_tab(settings: UISettings) -> VideoTab:label="Batch results", wrap=True,)tab = VideoTab(- sources, input_preview, input_actions, select_source, clear_source, neural, model_preset, quality, codec, container, rename_mode,+ sources, input_preview, input_actions, select_source, clear_source, neural, composition, mask_state, gpu_mode_state, quality, codec, container, rename_mode,custom_suffix, hdr_mode, preview_frame, preview, render, stop, reset, output_video,- zip_download, status, results+ save_download, zip_button, zip_download, status, results)tab.input_path, tab.output_path = input_path, output_pathbind_video_events(tab)@@ -372,7 +379,12 @@ def build_video_tab(settings: UISettings) -> VideoTab:def bind_video_events(tab: VideoTab) -> None:- 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)])+ bind_batch_ui(+ tab, render_video_batch, kind="video", preview_mode=update_video_preview_mode,+ archive_prefix="DLSS5_VIDEO_BATCH",+ preview_actions=[(tab.preview_frame, preview_one_frame), (tab.preview, preview_video)],+ realtime_preview=preview_one_frame,+ realtime_components=tab.neural,+ )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)diff --git a/src/settings/models.py b/src/settings/models.pyindex d43daef..1649da0 100644--- a/src/settings/models.py+++ b/src/settings/models.py@@ -1,208 +1,238 @@-from __future__ import annotations--from dataclasses import dataclass, replace-+from __future__ import annotations++from dataclasses import dataclass, replace+from ..core.ffmpeg import CODEC_CHOICES as FFMPEG_CODEC_CHOICES, ENCODING_QUALITIES, HDR_ALLOWED_CODECS, hdr_mode_supported-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-from ..upscale.video.models import options_from_settings-from ..upscale.image.models import options_from_settings as image_upscale_options--QUALITY_CHOICES = ENCODING_QUALITIES-CODEC_CHOICES = FFMPEG_CODEC_CHOICES-CONTAINER_CHOICES = ("MP4", "MKV", "MOV")-IMAGE_FORMAT_CHOICES = ("PNG", "JPEG", "WebP", "AVIF", "TIFF")-CONFIG_SECTION = "Settings"-PRESET_FORMAT = "dlss5-visual-enhancer-settings-preset"-PRESET_SCHEMA_VERSION = 1-MAX_PRESET_BYTES = 1024 * 1024--AUTOMATIC_MASK_CHOICES = ("Off", "On")-+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+from ..upscale.video.models import options_from_settings+from ..upscale.image.models import options_from_settings as image_upscale_options++QUALITY_CHOICES = ENCODING_QUALITIES+CODEC_CHOICES = FFMPEG_CODEC_CHOICES+CONTAINER_CHOICES = ("MP4", "MKV", "MOV")+IMAGE_FORMAT_CHOICES = ("PNG", "JPEG", "WebP", "AVIF", "TIFF")+CONFIG_SECTION = "Settings"+PRESET_FORMAT = "dlss5-visual-enhancer-settings-preset"+PRESET_SCHEMA_VERSION = 6+MAX_PRESET_BYTES = 1024 * 1024++AUTOMATIC_MASK_CHOICES = ("Off", "On")+PREVIEW_ENCODING_CHOICES = ("Auto", "Always H.264", "Disabled")UPSCALE_MODE_CHOICES = ("Image", "Video")---def coerce_hdr_mode(codec: str, enabled: bool) -> bool:- return bool(enabled) and hdr_mode_supported(codec)---def automatic_mask_choice(enabled: bool) -> str:- return "On" if enabled else "Off"---def parse_automatic_mask(value: str) -> bool:- if value not in AUTOMATIC_MASK_CHOICES:- choices = ", ".join(AUTOMATIC_MASK_CHOICES)- raise ValueError(f"Automatic Mask must be one of: {choices}.")- return value == "On"--@dataclass(frozen=True, slots=True)+++def coerce_hdr_mode(codec: str, enabled: bool) -> bool:+ return bool(enabled) and hdr_mode_supported(codec)+++def automatic_mask_choice(enabled: bool) -> str:+ return "On" if enabled else "Off"+++def parse_automatic_mask(value: str) -> bool:+ if value not in AUTOMATIC_MASK_CHOICES:+ choices = ", ".join(AUTOMATIC_MASK_CHOICES)+ raise ValueError(f"Automatic Mask must be one of: {choices}.")+ return value == "On"++@dataclass(frozen=True, slots=True)class UISettings:- ai_gpu_uuid: str = "auto"- video_gpu_uuid: str = "auto"- 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- upscaling_factor: float = 1.0- codec: str = "H.264"- container: str = "MP4"- quality: str = "Auto (Default)"- hdr_mode: bool = False- image_format: str = "PNG"- image_quality: int = 95- nr_preset: str = "Default"- automatic_mask: bool = False- image_rename_mode: str = "Auto"- image_custom_suffix: str = "_DLSS5"- video_rename_mode: str = "Auto"- video_custom_suffix: str = "_DLSS5"- dlss_model_preset: str = "Default"- frame_interpolation_target_fps: str = "60"- frame_interpolation_engine: str = "Auto"- frame_interpolation_codec: str = "H.264"- frame_interpolation_container: str = "MP4"- frame_interpolation_quality: str = "Auto (Default)"- frame_interpolation_hdr_mode: bool = False- frame_interpolation_rename_mode: str = "Auto"- frame_interpolation_custom_suffix: str = "_DLSSFG"+ ai_gpu_uuid: str = "auto"+ video_gpu_uuid: str = "auto"+ nr_style: str = "Default"+ nr_intensity: float = 1.0+ nr_passes: int = 1+ local_tone_strength: float = 1.0+ local_structure_strength: float = 1.0+ skin_structure_strength: float = -1.0+ nr_color_strength: float = 1.0+ tone_preservation: float = 0.0+ face_skin_protection: float = 0.0+ grain_preservation: float = 0.0+ # Video/Live temporal residual stabilization. Image rendering always+ # remains reset-based and does not consume this value.+ shimmer_suppression: float = 0.70+ mask_feather: int = 0+ # Validated Gradio upload identity; intentionally omitted from config/presets.+ nr_mask: object | None = None+ upscaling_factor: float = 1.0+ # Factory default only. Existing saved codec values are loaded unchanged.+ codec: str = "H.264 (NVIDIA NVENC)"+ container: str = "MP4"+ quality: str = "Auto (Default)"+ hdr_mode: bool = False+ image_format: str = "PNG"+ image_quality: int = 95+ automatic_mask: bool = False+ image_rename_mode: str = "Auto"+ image_custom_suffix: str = "_Neural_Rendering"+ video_rename_mode: str = "Auto"+ video_custom_suffix: str = "_Neural_Rendering"+ nr_gpu_mode: bool = True+ frame_interpolation_target_fps: str = "60"+ frame_interpolation_engine: str = "Auto"+ frame_interpolation_codec: str = "H.264"+ frame_interpolation_container: str = "MP4"+ frame_interpolation_quality: str = "Auto (Default)"+ frame_interpolation_hdr_mode: bool = False+ frame_interpolation_rename_mode: str = "Auto"+ frame_interpolation_custom_suffix: str = "_Frame_Interpolation"preview_encoding: str = "Auto"+ full_size_image_previews: bool = Falseupscale_mode: str = "Image"upscale_image_vsr_quality: int = 4- upscale_image_size_mode: str = "Scale factor"- upscale_image_scale_factor: float = 2.0- upscale_image_width: int = 3840- upscale_image_height: int = 2160- upscale_image_aspect_lock: bool = True- upscale_image_output_format: str = "PNG"- upscale_image_quality: int = 95- upscale_image_preserve_metadata: bool = True- upscale_image_rename_mode: str = "Auto"- upscale_image_custom_suffix: str = "_RTXIMAGE"- upscale_vsr_enabled: bool = True- upscale_vsr_quality: int = 4- upscale_size_mode: str = "Scale factor"- upscale_scale_factor: float = 2.0- upscale_width: int = 3840- upscale_height: int = 2160- upscale_aspect_lock: bool = True- upscale_hdr_enabled: bool = False- upscale_hdr_contrast: int = 100- upscale_hdr_saturation: int = 100- upscale_hdr_middle_gray: int = 50- upscale_hdr_peak_luminance: int = 1000- upscale_hdr_precision: str = "Packed 10-bit"- upscale_codec: str = "H.265 (NVIDIA NVENC)"- upscale_container: str = "MP4"- upscale_quality: str = "Auto (Default)"- upscale_rename_mode: str = "Auto"- upscale_custom_suffix: str = "_RTXVIDEO"-- def component_values(- self,- ) -> tuple[str, str, float, float, float, float, float, bool, str, str, str, str]:- return (- self.nr_preset,- self.nr_style,- self.nr_intensity,- self.local_tone_strength,- self.local_structure_strength,- self.skin_structure_strength,- self.upscaling_factor,- self.automatic_mask,- self.codec,- self.container,- self.quality,- self.dlss_model_preset,- )---DEFAULT_SETTINGS = UISettings()---def _validate(settings: UISettings) -> UISettings:- options_from_settings(settings).validate(for_render=False)- image_upscale_options(settings).validate(for_render=False)- for label, value in (- ("AI Processing GPU", settings.ai_gpu_uuid),- ("Video Processing GPU", settings.video_gpu_uuid),- ):- if not isinstance(value, str) or not value.strip() or len(value) > 160:- raise ValueError(f"{label} selection must be Automatic or a valid GPU UUID.")- resolve_native_settings(settings)- resolve_upscaling_mode(settings.upscaling_factor)- if not isinstance(settings.automatic_mask, bool):- raise ValueError("Automatic Mask must be a boolean value.")- if not isinstance(settings.hdr_mode, bool):- raise ValueError("HDR Mode must be a boolean value.")- if not isinstance(settings.frame_interpolation_hdr_mode, bool):- raise ValueError("Frame Interpolation HDR Mode must be a boolean value.")- # Migrate old codec names before validation- migrated_codec = _migrate_codec(settings.codec)- migrated_fi_codec = _migrate_codec(settings.frame_interpolation_codec)- if migrated_codec != settings.codec or migrated_fi_codec != settings.frame_interpolation_codec:- settings = replace(settings, codec=migrated_codec, frame_interpolation_codec=migrated_fi_codec)- # HDR Mode is only allowed for 10-bit capable codecs; if enabled with H.264, auto-disable for old configs- # For strict validation (presets), raise if mismatched – caller can decide; here we raise for explicit mismatch- _HDR_ALLOWED = HDR_ALLOWED_CODECS-- if settings.hdr_mode and settings.codec not in _HDR_ALLOWED:- raise ValueError(- f"HDR Mode is only available for {', '.join(sorted(_HDR_ALLOWED))}; current codec is {settings.codec!r}."- )- if settings.frame_interpolation_hdr_mode and settings.frame_interpolation_codec not in _HDR_ALLOWED:- raise ValueError(- f"Frame Interpolation HDR Mode is only available for {', '.join(sorted(_HDR_ALLOWED))}; current codec is {settings.frame_interpolation_codec!r}."- )- allowed = {- "Video codec": (settings.codec, CODEC_CHOICES),- "Container": (settings.container, CONTAINER_CHOICES),- "Encoding quality": (settings.quality, QUALITY_CHOICES),- "Image format": (settings.image_format, IMAGE_FORMAT_CHOICES),- "Frame Interpolation FPS": (- settings.frame_interpolation_target_fps,- FPS_CHOICES,- ),- "Frame Interpolation engine": (- settings.frame_interpolation_engine,- ENGINE_CHOICES,- ),- "Frame Interpolation codec": (- settings.frame_interpolation_codec,- CODEC_CHOICES,- ),- "Frame Interpolation container": (- settings.frame_interpolation_container,- CONTAINER_CHOICES,- ),- "Frame Interpolation quality": (- settings.frame_interpolation_quality,- QUALITY_CHOICES,- ),- "Preview encoding": (- settings.preview_encoding,- PREVIEW_ENCODING_CHOICES,- ),+ upscale_image_size_mode: str = "Scale factor"+ upscale_image_scale_factor: float = 2.0+ upscale_image_width: int = 3840+ upscale_image_height: int = 2160+ upscale_image_aspect_lock: bool = True+ upscale_image_output_format: str = "PNG"+ upscale_image_quality: int = 95+ upscale_image_preserve_metadata: bool = True+ upscale_image_rename_mode: str = "Auto"+ upscale_image_custom_suffix: str = "_Upscale"+ upscale_vsr_enabled: bool = True+ upscale_vsr_quality: int = 4+ upscale_size_mode: str = "Scale factor"+ upscale_scale_factor: float = 2.0+ upscale_width: int = 3840+ upscale_height: int = 2160+ upscale_aspect_lock: bool = True+ upscale_hdr_enabled: bool = False+ upscale_hdr_contrast: int = 100+ upscale_hdr_saturation: int = 100+ upscale_hdr_middle_gray: int = 50+ upscale_hdr_peak_luminance: int = 1000+ upscale_hdr_precision: str = "Packed 10-bit"+ upscale_codec: str = "H.265 (NVIDIA NVENC)"+ upscale_container: str = "MP4"+ upscale_quality: str = "Auto (Default)"+ upscale_rename_mode: str = "Auto"+ upscale_custom_suffix: str = "_Upscale"++ def component_values(+ self,+ ) -> tuple[str, float, int, float, float, float, float, float, float, float, float, int, float, bool, bool, str, str, str]:+ return (+ self.nr_style,+ self.nr_intensity,+ self.nr_passes,+ self.local_tone_strength,+ self.local_structure_strength,+ self.skin_structure_strength,+ self.nr_color_strength,+ self.tone_preservation,+ self.face_skin_protection,+ self.grain_preservation,+ self.shimmer_suppression,+ self.mask_feather,+ self.upscaling_factor,+ self.automatic_mask,+ self.nr_gpu_mode,+ self.codec,+ self.container,+ self.quality,+ )+++DEFAULT_SETTINGS = UISettings()+++def _validate(settings: UISettings) -> UISettings:+ options_from_settings(settings).validate(for_render=False)+ image_upscale_options(settings).validate(for_render=False)+ for label, value in (+ ("AI Processing GPU", settings.ai_gpu_uuid),+ ("Video Processing GPU", settings.video_gpu_uuid),+ ):+ if not isinstance(value, str) or not value.strip() or len(value) > 160:+ raise ValueError(f"{label} selection must be Automatic or a valid GPU UUID.")+ resolve_native_settings(settings)+ if isinstance(settings.nr_passes, bool) or not isinstance(settings.nr_passes, int):+ raise ValueError("NR Passes must be an integer from 1 to 4.")+ if not 1 <= settings.nr_passes <= 4:+ raise ValueError("NR Passes must be between 1 and 4.")+ if isinstance(settings.mask_feather, bool) or not isinstance(settings.mask_feather, int):+ raise ValueError("Mask Feather must be an integer from 0 to 128.")+ if not 0 <= settings.mask_feather <= 128:+ raise ValueError("Mask Feather must be between 0 and 128 pixels.")+ resolve_upscaling_mode(settings.upscaling_factor)+ if not isinstance(settings.automatic_mask, bool):+ raise ValueError("Automatic Mask must be a boolean value.")+ if not isinstance(settings.nr_gpu_mode, bool):+ raise ValueError("Neural Rendering GPU mode must be a boolean value.")+ if not isinstance(settings.hdr_mode, bool):+ raise ValueError("HDR Mode must be a boolean value.")+ if not isinstance(settings.frame_interpolation_hdr_mode, bool):+ raise ValueError("Frame Interpolation HDR Mode must be a boolean value.")+ if not isinstance(settings.full_size_image_previews, bool):+ raise ValueError("Full size quality preview must be a boolean value.")+ # Migrate old codec names before validation+ migrated_codec = _migrate_codec(settings.codec)+ migrated_fi_codec = _migrate_codec(settings.frame_interpolation_codec)+ if migrated_codec != settings.codec or migrated_fi_codec != settings.frame_interpolation_codec:+ settings = replace(settings, codec=migrated_codec, frame_interpolation_codec=migrated_fi_codec)+ # HDR Mode is only allowed for 10-bit capable codecs; if enabled with H.264, auto-disable for old configs+ # For strict validation (presets), raise if mismatched – caller can decide; here we raise for explicit mismatch+ _HDR_ALLOWED = HDR_ALLOWED_CODECS++ if settings.hdr_mode and settings.codec not in _HDR_ALLOWED:+ raise ValueError(+ f"HDR Mode is only available for {', '.join(sorted(_HDR_ALLOWED))}; current codec is {settings.codec!r}."+ )+ if settings.frame_interpolation_hdr_mode and settings.frame_interpolation_codec not in _HDR_ALLOWED:+ raise ValueError(+ f"Frame Interpolation HDR Mode is only available for {', '.join(sorted(_HDR_ALLOWED))}; current codec is {settings.frame_interpolation_codec!r}."+ )+ allowed = {+ "Video codec": (settings.codec, CODEC_CHOICES),+ "Container": (settings.container, CONTAINER_CHOICES),+ "Encoding quality": (settings.quality, QUALITY_CHOICES),+ "Image format": (settings.image_format, IMAGE_FORMAT_CHOICES),+ "Frame Interpolation FPS": (+ settings.frame_interpolation_target_fps,+ FPS_CHOICES,+ ),+ "Frame Interpolation engine": (+ settings.frame_interpolation_engine,+ ENGINE_CHOICES,+ ),+ "Frame Interpolation codec": (+ settings.frame_interpolation_codec,+ CODEC_CHOICES,+ ),+ "Frame Interpolation container": (+ settings.frame_interpolation_container,+ CONTAINER_CHOICES,+ ),+ "Frame Interpolation quality": (+ settings.frame_interpolation_quality,+ QUALITY_CHOICES,+ ),+ "Preview encoding": (+ settings.preview_encoding,+ PREVIEW_ENCODING_CHOICES,+ ),"Upscale mode": (settings.upscale_mode,UPSCALE_MODE_CHOICES,),- }- for label, (value, choices) in allowed.items():- if value not in choices:- raise ValueError(f"Unknown {label}: {value!r}.")- if isinstance(settings.image_quality, bool) or not 1 <= int(settings.image_quality) <= 100:- raise ValueError("Image quality must be an integer from 1 to 100.")- if int(settings.image_quality) != settings.image_quality:- raise ValueError("Image quality must be an integer from 1 to 100.")- validate_rename(settings.image_rename_mode, settings.image_custom_suffix)- validate_rename(settings.video_rename_mode, settings.video_custom_suffix)- validate_rename(- settings.frame_interpolation_rename_mode,- settings.frame_interpolation_custom_suffix,- )- return settings+ }+ for label, (value, choices) in allowed.items():+ if value not in choices:+ raise ValueError(f"Unknown {label}: {value!r}.")+ if isinstance(settings.image_quality, bool) or not 1 <= int(settings.image_quality) <= 100:+ raise ValueError("Image quality must be an integer from 1 to 100.")+ if int(settings.image_quality) != settings.image_quality:+ raise ValueError("Image quality must be an integer from 1 to 100.")+ validate_rename(settings.image_rename_mode, settings.image_custom_suffix)+ validate_rename(settings.video_rename_mode, settings.video_custom_suffix)+ validate_rename(+ settings.frame_interpolation_rename_mode,+ settings.frame_interpolation_custom_suffix,+ )+ return settingsdiff --git a/src/settings/presets.py b/src/settings/presets.pyindex e805380..c2a29d5 100644--- a/src/settings/presets.py+++ b/src/settings/presets.py@@ -9,6 +9,7 @@ from dataclasses import asdict, fields, replacefrom pathlib import Pathfrom typing import Any+from ..core.paths import GRADIO_TEMPfrom .models import (DEFAULT_SETTINGS, MAX_PRESET_BYTES, PRESET_FORMAT, PRESET_SCHEMA_VERSION, UISettings, _validate,)@@ -51,11 +52,13 @@ def preset_document(name: str, settings: UISettings) -> dict[str, Any]:"""Build the versioned user-facing preset document."""display_name = _preset_name(name)_validate(settings)+ values = asdict(settings)+ values.pop("nr_mask", None)return {"format": PRESET_FORMAT,"schema_version": PRESET_SCHEMA_VERSION,"name": display_name,- "settings": asdict(settings),+ "settings": values,}@@ -63,7 +66,8 @@ def export_settings_preset(name: str, settings: UISettings) -> Path:"""Write a validated preset to an isolated temporary download directory."""document = preset_document(name, settings)filename = preset_filename(document["name"])- directory = Path(tempfile.mkdtemp(prefix="dlss5-settings-preset-"))+ GRADIO_TEMP.mkdir(parents=True, exist_ok=True)+ directory = Path(tempfile.mkdtemp(prefix="dlss5-settings-preset-", dir=GRADIO_TEMP))path = directory / filenamepath.write_text(json.dumps(document, indent=2, ensure_ascii=False) + "\n",@@ -99,7 +103,7 @@ def _coerce_preset_value(field_name: str, value: Any, current: UISettings) -> Andef import_settings_preset(path: str | os.PathLike[str], current: UISettings) -> tuple[str, UISettings]:- """Load, compatibly merge, and atomically validate a version-1 preset."""+ """Load and migrate schema-v1 through v6 presets, then validate atomically."""preset_path = Path(path)if preset_path.suffix.casefold() != ".json":raise ValueError("Choose a JSON preset file.")@@ -120,7 +124,7 @@ def import_settings_preset(version = document.get("schema_version")if isinstance(version, bool) or not isinstance(version, int):raise ValueError("Preset schema_version must be an integer.")- if version != PRESET_SCHEMA_VERSION:+ if version not in range(1, PRESET_SCHEMA_VERSION + 1):direction = "newer" if version > PRESET_SCHEMA_VERSION else "unsupported"raise ValueError(f"Preset schema version {version} is {direction}; this build supports version "@@ -135,8 +139,29 @@ def import_settings_preset(changes = {key: _coerce_preset_value(key, value, current)for key, value in imported.items()- if key in known_names+ if key in known_names and key != "nr_mask"}+ if version == 1:+ # v1's DLSS model-preset field was never applied by feature 18. Ignore+ # it and default the newly introduced shared GPU staging switch to ON.+ changes.pop("dlss_model_preset", None)+ changes["nr_gpu_mode"] = DEFAULT_SETTINGS.nr_gpu_mode+ if version < 3:+ changes["nr_color_strength"] = DEFAULT_SETTINGS.nr_color_strength+ changes["tone_preservation"] = DEFAULT_SETTINGS.tone_preservation+ changes["mask_feather"] = DEFAULT_SETTINGS.mask_feather+ if version < 4:+ changes["face_skin_protection"] = DEFAULT_SETTINGS.face_skin_protection+ changes["grain_preservation"] = DEFAULT_SETTINGS.grain_preservation+ if version < 5:+ changes["nr_passes"] = DEFAULT_SETTINGS.nr_passes+ if version < 6:+ changes["shimmer_suppression"] = DEFAULT_SETTINGS.shimmer_suppression+ # NR Preset was removed entirely (non-functional). Old preset files still+ # carry it; ignore so imports from previous builds keep working.+ # (Unknown keys are already filtered above; this covers any edge case where+ # the field still exists on older UISettings shapes.)+ changes.pop("nr_preset", None)# Presets created before the Upscale tab get its defaults independently of# whichever RTX settings happen to be selected when the preset is imported.for key in known_names:diff --git a/src/settings/storage.py b/src/settings/storage.pyindex 5076e14..61af7c0 100644--- a/src/settings/storage.py+++ b/src/settings/storage.py@@ -1,320 +1,355 @@-from __future__ import annotations--import configparser-import math-import os-import threading-from dataclasses import replace-from pathlib import Path--from ..core.ffmpeg import HDR_ALLOWED_CODECS-from ..core.paths import CONFIG_PATH-from ..core.naming import RENAME_MODES, validate_rename-from ..core.runtime import DLSS_MODEL_PRESETS, NR_PRESETS, NR_STYLES, resolve_upscaling_mode-from ..frame_interpolation.models import ENGINE_CHOICES, FPS_CHOICES-from .migration import _migrate_codec+from __future__ import annotations++import configparser+import math+import os+import threading+from dataclasses import replace+from pathlib import Path++from ..core.ffmpeg import HDR_ALLOWED_CODECS+from ..core.paths import CONFIG_PATH+from ..core.naming import RENAME_MODES, validate_rename+from ..core.runtime import NR_STYLES, resolve_upscaling_mode+from ..frame_interpolation.models import ENGINE_CHOICES, FPS_CHOICES+from .migration import _migrate_codecfrom .models import (CODEC_CHOICES, CONFIG_SECTION, CONTAINER_CHOICES, DEFAULT_SETTINGS, IMAGE_FORMAT_CHOICES,PREVIEW_ENCODING_CHOICES, QUALITY_CHOICES, UPSCALE_MODE_CHOICES, UISettings, _validate,)from ..upscale.video.models import SETTING_FIELDS, options_from_settings-from ..upscale.image.models import SETTING_FIELDS as IMAGE_UPSCALE_FIELDS, options_from_settings as image_upscale_options--def load_settings(path: str | os.PathLike[str]) -> UISettings:- config_path = Path(path)- parser = configparser.ConfigParser()- try:- with config_path.open("r", encoding="utf-8") as stream:- parser.read_file(stream)- except (OSError, configparser.Error):- return DEFAULT_SETTINGS-- section = parser[CONFIG_SECTION] if parser.has_section(CONFIG_SECTION) else {}-- def choice(key: str, choices: tuple[str, ...], default: str) -> str:- value = section.get(key, default)- # Migrate old HEVC naming before validation- if key in ("codec", "frame_interpolation_codec"):- migrated = _migrate_codec(value)- if migrated in choices:- return migrated- return value if value in choices else default-- def codec_choice(key: str, default: str) -> str:- raw = section.get(key, default)- migrated = _migrate_codec(raw)- if migrated in CODEC_CHOICES:- return migrated- return default-- def number(key: str, minimum: float, maximum: float, default: float) -> float:- try:- value = float(section.get(key, str(default)))- except (TypeError, ValueError):- return default- return value if math.isfinite(value) and minimum <= value <= maximum else default-- def upscaling_factor() -> float:- try:- return resolve_upscaling_mode(float(section.get("upscaling_factor", "1.0")))[0]- except (TypeError, ValueError):- return DEFAULT_SETTINGS.upscaling_factor-- def image_quality() -> int:- value = number("image_quality", 1, 100, DEFAULT_SETTINGS.image_quality)- return int(value) if float(value).is_integer() else DEFAULT_SETTINGS.image_quality-- def boolean(key: str, default: bool) -> bool:- raw_value = section.get(key)- if raw_value is None:- return default- parsed = configparser.ConfigParser.BOOLEAN_STATES.get(str(raw_value).casefold())- return parsed if parsed is not None else default-- def hdr_mode_value() -> bool:- # Support both new 'hdr_mode' and legacy 'preserve_hdr' keys- raw = section.get("hdr_mode")- if raw is None:- raw = section.get("preserve_hdr")- if raw is None:- return DEFAULT_SETTINGS.hdr_mode- parsed = configparser.ConfigParser.BOOLEAN_STATES.get(str(raw).casefold())- return parsed if parsed is not None else DEFAULT_SETTINGS.hdr_mode-- def fi_hdr_mode_value() -> bool:- raw = section.get("frame_interpolation_hdr_mode")- if raw is None:- # Check legacy if ever used- raw = section.get("frame_interpolation_preserve_hdr")- if raw is None:- return DEFAULT_SETTINGS.frame_interpolation_hdr_mode- parsed = configparser.ConfigParser.BOOLEAN_STATES.get(str(raw).casefold())- return parsed if parsed is not None else DEFAULT_SETTINGS.frame_interpolation_hdr_mode-- def frame_interpolation_engine() -> str:- value = section.get(- "frame_interpolation_engine",- DEFAULT_SETTINGS.frame_interpolation_engine,- )- if value == "Experimental Cascade":- return "Cascade"- return value if value in ENGINE_CHOICES else DEFAULT_SETTINGS.frame_interpolation_engine-- image_rename_mode = choice(- "image_rename_mode", RENAME_MODES, DEFAULT_SETTINGS.image_rename_mode- )- image_custom_suffix = section.get(- "image_custom_suffix", DEFAULT_SETTINGS.image_custom_suffix- )- video_rename_mode = choice(- "video_rename_mode", RENAME_MODES, DEFAULT_SETTINGS.video_rename_mode- )- video_custom_suffix = section.get(- "video_custom_suffix", DEFAULT_SETTINGS.video_custom_suffix- )- frame_interpolation_rename_mode = choice(- "frame_interpolation_rename_mode",- RENAME_MODES,- DEFAULT_SETTINGS.frame_interpolation_rename_mode,- )- frame_interpolation_custom_suffix = section.get(- "frame_interpolation_custom_suffix",- DEFAULT_SETTINGS.frame_interpolation_custom_suffix,- )- try:- validate_rename(image_rename_mode, image_custom_suffix)- except ValueError:- image_rename_mode = DEFAULT_SETTINGS.image_rename_mode- image_custom_suffix = DEFAULT_SETTINGS.image_custom_suffix- try:- validate_rename(video_rename_mode, video_custom_suffix)- except ValueError:- video_rename_mode = DEFAULT_SETTINGS.video_rename_mode- video_custom_suffix = DEFAULT_SETTINGS.video_custom_suffix- try:- validate_rename(- frame_interpolation_rename_mode,- frame_interpolation_custom_suffix,- )- except ValueError:- frame_interpolation_rename_mode = DEFAULT_SETTINGS.frame_interpolation_rename_mode- frame_interpolation_custom_suffix = DEFAULT_SETTINGS.frame_interpolation_custom_suffix-- settings = UISettings(- ai_gpu_uuid=section.get("ai_gpu_uuid", DEFAULT_SETTINGS.ai_gpu_uuid).strip()- or DEFAULT_SETTINGS.ai_gpu_uuid,- video_gpu_uuid=section.get("video_gpu_uuid", DEFAULT_SETTINGS.video_gpu_uuid).strip()- or DEFAULT_SETTINGS.video_gpu_uuid,- nr_preset=choice("nr_preset", tuple(NR_PRESETS), DEFAULT_SETTINGS.nr_preset),- nr_style=choice("nr_style", tuple(NR_STYLES), DEFAULT_SETTINGS.nr_style),- nr_intensity=number("nr_intensity", 0.0, 2.0, DEFAULT_SETTINGS.nr_intensity),- local_tone_strength=number(- "local_tone_strength", 0.0, 2.0, DEFAULT_SETTINGS.local_tone_strength- ),- local_structure_strength=number(- "local_structure_strength", 0.0, 2.0, DEFAULT_SETTINGS.local_structure_strength- ),- skin_structure_strength=number(- "skin_structure_strength", -1.0, 2.0, DEFAULT_SETTINGS.skin_structure_strength- ),- automatic_mask=boolean("automatic_mask", DEFAULT_SETTINGS.automatic_mask),- upscaling_factor=upscaling_factor(),- codec=codec_choice("codec", DEFAULT_SETTINGS.codec),- container=choice("container", CONTAINER_CHOICES, DEFAULT_SETTINGS.container),- quality=choice("quality", QUALITY_CHOICES, DEFAULT_SETTINGS.quality),- hdr_mode=hdr_mode_value(),- image_format=choice(- "image_format", IMAGE_FORMAT_CHOICES, DEFAULT_SETTINGS.image_format- ),- image_quality=image_quality(),- dlss_model_preset=choice(- "dlss_model_preset",- tuple(DLSS_MODEL_PRESETS),- DEFAULT_SETTINGS.dlss_model_preset,- ),- image_rename_mode=image_rename_mode,- image_custom_suffix=image_custom_suffix,- video_rename_mode=video_rename_mode,- video_custom_suffix=video_custom_suffix,- frame_interpolation_target_fps=choice(- "frame_interpolation_target_fps",- FPS_CHOICES,- DEFAULT_SETTINGS.frame_interpolation_target_fps,- ),- frame_interpolation_engine=frame_interpolation_engine(),- frame_interpolation_codec=codec_choice(- "frame_interpolation_codec",- DEFAULT_SETTINGS.frame_interpolation_codec,- ),- frame_interpolation_container=choice(- "frame_interpolation_container",- CONTAINER_CHOICES,- DEFAULT_SETTINGS.frame_interpolation_container,- ),- frame_interpolation_quality=choice(- "frame_interpolation_quality",- QUALITY_CHOICES,- DEFAULT_SETTINGS.frame_interpolation_quality,- ),- frame_interpolation_hdr_mode=fi_hdr_mode_value(),- frame_interpolation_rename_mode=frame_interpolation_rename_mode,- frame_interpolation_custom_suffix=frame_interpolation_custom_suffix,- preview_encoding=choice(- "preview_encoding",- PREVIEW_ENCODING_CHOICES,- DEFAULT_SETTINGS.preview_encoding,- ),+from ..upscale.image.models import SETTING_FIELDS as IMAGE_UPSCALE_FIELDS, options_from_settings as image_upscale_options++def load_settings(path: str | os.PathLike[str]) -> UISettings:+ config_path = Path(path)+ parser = configparser.ConfigParser()+ try:+ with config_path.open("r", encoding="utf-8") as stream:+ parser.read_file(stream)+ except (OSError, configparser.Error):+ return DEFAULT_SETTINGS++ section = parser[CONFIG_SECTION] if parser.has_section(CONFIG_SECTION) else {}++ def choice(key: str, choices: tuple[str, ...], default: str) -> str:+ value = section.get(key, default)+ # Migrate old HEVC naming before validation+ if key in ("codec", "frame_interpolation_codec"):+ migrated = _migrate_codec(value)+ if migrated in choices:+ return migrated+ return value if value in choices else default++ def codec_choice(key: str, default: str) -> str:+ raw = section.get(key, default)+ migrated = _migrate_codec(raw)+ if migrated in CODEC_CHOICES:+ return migrated+ return default++ def number(key: str, minimum: float, maximum: float, default: float) -> float:+ try:+ value = float(section.get(key, str(default)))+ except (TypeError, ValueError):+ return default+ return value if math.isfinite(value) and minimum <= value <= maximum else default++ def upscaling_factor() -> float:+ try:+ return resolve_upscaling_mode(float(section.get("upscaling_factor", "1.0")))[0]+ except (TypeError, ValueError):+ return DEFAULT_SETTINGS.upscaling_factor++ def image_quality() -> int:+ value = number("image_quality", 1, 100, DEFAULT_SETTINGS.image_quality)+ return int(value) if float(value).is_integer() else DEFAULT_SETTINGS.image_quality++ def integer(key: str, minimum: int, maximum: int, default: int) -> int:+ value = number(key, minimum, maximum, default)+ return int(value) if float(value).is_integer() else default++ def boolean(key: str, default: bool) -> bool:+ raw_value = section.get(key)+ if raw_value is None:+ return default+ parsed = configparser.ConfigParser.BOOLEAN_STATES.get(str(raw_value).casefold())+ return parsed if parsed is not None else default++ def hdr_mode_value() -> bool:+ # Support both new 'hdr_mode' and legacy 'preserve_hdr' keys+ raw = section.get("hdr_mode")+ if raw is None:+ raw = section.get("preserve_hdr")+ if raw is None:+ return DEFAULT_SETTINGS.hdr_mode+ parsed = configparser.ConfigParser.BOOLEAN_STATES.get(str(raw).casefold())+ return parsed if parsed is not None else DEFAULT_SETTINGS.hdr_mode++ def fi_hdr_mode_value() -> bool:+ raw = section.get("frame_interpolation_hdr_mode")+ if raw is None:+ # Check legacy if ever used+ raw = section.get("frame_interpolation_preserve_hdr")+ if raw is None:+ return DEFAULT_SETTINGS.frame_interpolation_hdr_mode+ parsed = configparser.ConfigParser.BOOLEAN_STATES.get(str(raw).casefold())+ return parsed if parsed is not None else DEFAULT_SETTINGS.frame_interpolation_hdr_mode++ def frame_interpolation_engine() -> str:+ value = section.get(+ "frame_interpolation_engine",+ DEFAULT_SETTINGS.frame_interpolation_engine,+ )+ if value == "Experimental Cascade":+ return "Cascade"+ return value if value in ENGINE_CHOICES else DEFAULT_SETTINGS.frame_interpolation_engine++ image_rename_mode = choice(+ "image_rename_mode", RENAME_MODES, DEFAULT_SETTINGS.image_rename_mode+ )+ image_custom_suffix = section.get(+ "image_custom_suffix", DEFAULT_SETTINGS.image_custom_suffix+ )+ video_rename_mode = choice(+ "video_rename_mode", RENAME_MODES, DEFAULT_SETTINGS.video_rename_mode+ )+ video_custom_suffix = section.get(+ "video_custom_suffix", DEFAULT_SETTINGS.video_custom_suffix+ )+ frame_interpolation_rename_mode = choice(+ "frame_interpolation_rename_mode",+ RENAME_MODES,+ DEFAULT_SETTINGS.frame_interpolation_rename_mode,+ )+ frame_interpolation_custom_suffix = section.get(+ "frame_interpolation_custom_suffix",+ DEFAULT_SETTINGS.frame_interpolation_custom_suffix,+ )+ try:+ validate_rename(image_rename_mode, image_custom_suffix)+ except ValueError:+ image_rename_mode = DEFAULT_SETTINGS.image_rename_mode+ image_custom_suffix = DEFAULT_SETTINGS.image_custom_suffix+ try:+ validate_rename(video_rename_mode, video_custom_suffix)+ except ValueError:+ video_rename_mode = DEFAULT_SETTINGS.video_rename_mode+ video_custom_suffix = DEFAULT_SETTINGS.video_custom_suffix+ try:+ validate_rename(+ frame_interpolation_rename_mode,+ frame_interpolation_custom_suffix,+ )+ except ValueError:+ frame_interpolation_rename_mode = DEFAULT_SETTINGS.frame_interpolation_rename_mode+ frame_interpolation_custom_suffix = DEFAULT_SETTINGS.frame_interpolation_custom_suffix++ settings = UISettings(+ ai_gpu_uuid=section.get("ai_gpu_uuid", DEFAULT_SETTINGS.ai_gpu_uuid).strip()+ or DEFAULT_SETTINGS.ai_gpu_uuid,+ video_gpu_uuid=section.get("video_gpu_uuid", DEFAULT_SETTINGS.video_gpu_uuid).strip()+ or DEFAULT_SETTINGS.video_gpu_uuid,+ nr_style=choice("nr_style", tuple(NR_STYLES), DEFAULT_SETTINGS.nr_style),+ nr_intensity=number("nr_intensity", 0.0, 2.0, DEFAULT_SETTINGS.nr_intensity),+ nr_passes=integer("nr_passes", 1, 4, DEFAULT_SETTINGS.nr_passes),+ local_tone_strength=number(+ "local_tone_strength", 0.0, 2.0, DEFAULT_SETTINGS.local_tone_strength+ ),+ local_structure_strength=number(+ "local_structure_strength", 0.0, 2.0, DEFAULT_SETTINGS.local_structure_strength+ ),+ skin_structure_strength=number(+ "skin_structure_strength", -1.0, 2.0, DEFAULT_SETTINGS.skin_structure_strength+ ),+ nr_color_strength=number(+ "nr_color_strength", 0.0, 1.0, DEFAULT_SETTINGS.nr_color_strength+ ),+ tone_preservation=number(+ "tone_preservation", 0.0, 1.0, DEFAULT_SETTINGS.tone_preservation+ ),+ face_skin_protection=number(+ "face_skin_protection", 0.0, 1.0, DEFAULT_SETTINGS.face_skin_protection+ ),+ grain_preservation=number(+ "grain_preservation", 0.0, 1.0, DEFAULT_SETTINGS.grain_preservation+ ),+ shimmer_suppression=number(+ "shimmer_suppression", 0.0, 1.0, DEFAULT_SETTINGS.shimmer_suppression+ ),+ mask_feather=int(number(+ "mask_feather", 0, 128, DEFAULT_SETTINGS.mask_feather+ )),+ automatic_mask=boolean("automatic_mask", DEFAULT_SETTINGS.automatic_mask),+ nr_gpu_mode=boolean("nr_gpu_mode", DEFAULT_SETTINGS.nr_gpu_mode),+ upscaling_factor=upscaling_factor(),+ codec=codec_choice("codec", DEFAULT_SETTINGS.codec),+ container=choice("container", CONTAINER_CHOICES, DEFAULT_SETTINGS.container),+ quality=choice("quality", QUALITY_CHOICES, DEFAULT_SETTINGS.quality),+ hdr_mode=hdr_mode_value(),+ image_format=choice(+ "image_format", IMAGE_FORMAT_CHOICES, DEFAULT_SETTINGS.image_format+ ),+ image_quality=image_quality(),+ image_rename_mode=image_rename_mode,+ image_custom_suffix=image_custom_suffix,+ video_rename_mode=video_rename_mode,+ video_custom_suffix=video_custom_suffix,+ frame_interpolation_target_fps=choice(+ "frame_interpolation_target_fps",+ FPS_CHOICES,+ DEFAULT_SETTINGS.frame_interpolation_target_fps,+ ),+ frame_interpolation_engine=frame_interpolation_engine(),+ frame_interpolation_codec=codec_choice(+ "frame_interpolation_codec",+ DEFAULT_SETTINGS.frame_interpolation_codec,+ ),+ frame_interpolation_container=choice(+ "frame_interpolation_container",+ CONTAINER_CHOICES,+ DEFAULT_SETTINGS.frame_interpolation_container,+ ),+ frame_interpolation_quality=choice(+ "frame_interpolation_quality",+ QUALITY_CHOICES,+ DEFAULT_SETTINGS.frame_interpolation_quality,+ ),+ frame_interpolation_hdr_mode=fi_hdr_mode_value(),+ frame_interpolation_rename_mode=frame_interpolation_rename_mode,+ frame_interpolation_custom_suffix=frame_interpolation_custom_suffix,+ preview_encoding=choice(+ "preview_encoding",+ PREVIEW_ENCODING_CHOICES,+ DEFAULT_SETTINGS.preview_encoding,+ ),+ full_size_image_previews=boolean(+ "full_size_image_previews", DEFAULT_SETTINGS.full_size_image_previews+ ),upscale_mode=choice("upscale_mode",UPSCALE_MODE_CHOICES,DEFAULT_SETTINGS.upscale_mode,),- )- # Auto-disable HDR Mode if codec does not support it (e.g. H.264)- try:- if settings.hdr_mode and settings.codec not in HDR_ALLOWED_CODECS:- settings = replace(settings, hdr_mode=False)- if settings.frame_interpolation_hdr_mode and settings.frame_interpolation_codec not in HDR_ALLOWED_CODECS:- settings = replace(settings, frame_interpolation_hdr_mode=False)- except Exception:- pass- upscale_values = {}- for prefix, names, factory in (("upscale_", SETTING_FIELDS, options_from_settings),- ("upscale_image_", IMAGE_UPSCALE_FIELDS, image_upscale_options)):- for name in names:- key = prefix + name- default = getattr(DEFAULT_SETTINGS, key)- raw = section.get(key)- if raw is None:- continue- try:- if isinstance(default, bool):- value = boolean(key, default)- elif isinstance(default, int):- value = int(raw)- elif isinstance(default, float):- value = float(raw)- else:- value = str(raw)- if name == "vsr_quality" and value == 0:- value = default- candidate = replace(settings, **upscale_values, **{key: value})- factory(candidate).validate(for_render=False)- upscale_values[key] = value- except (ValueError, TypeError, OverflowError):- continue- return replace(settings, **upscale_values)---def save_settings(path: str | os.PathLike[str], settings: UISettings) -> None:- settings = _validate(settings)- config_path = Path(path)- config_path.parent.mkdir(parents=True, exist_ok=True)- parser = configparser.ConfigParser()- parser[CONFIG_SECTION] = {+ )+ # Auto-disable HDR Mode if codec does not support it (e.g. H.264)+ try:+ if settings.hdr_mode and settings.codec not in HDR_ALLOWED_CODECS:+ settings = replace(settings, hdr_mode=False)+ if settings.frame_interpolation_hdr_mode and settings.frame_interpolation_codec not in HDR_ALLOWED_CODECS:+ settings = replace(settings, frame_interpolation_hdr_mode=False)+ except Exception:+ pass+ upscale_values = {}+ for prefix, names, factory in (("upscale_", SETTING_FIELDS, options_from_settings),+ ("upscale_image_", IMAGE_UPSCALE_FIELDS, image_upscale_options)):+ for name in names:+ key = prefix + name+ default = getattr(DEFAULT_SETTINGS, key)+ raw = section.get(key)+ if raw is None:+ continue+ try:+ if isinstance(default, bool):+ value = boolean(key, default)+ elif isinstance(default, int):+ value = int(raw)+ elif isinstance(default, float):+ value = float(raw)+ else:+ value = str(raw)+ if name == "vsr_quality" and value == 0:+ value = default+ candidate = replace(settings, **upscale_values, **{key: value})+ factory(candidate).validate(for_render=False)+ upscale_values[key] = value+ except (ValueError, TypeError, OverflowError):+ continue+ return replace(settings, **upscale_values)+++def save_settings(path: str | os.PathLike[str], settings: UISettings) -> None:+ settings = _validate(settings)+ config_path = Path(path)+ config_path.parent.mkdir(parents=True, exist_ok=True)+ parser = configparser.ConfigParser()+ parser[CONFIG_SECTION] = {**{"upscale_image_" + name: str(getattr(settings, "upscale_image_" + name)) for name in IMAGE_UPSCALE_FIELDS},**{"upscale_" + name: str(getattr(settings, "upscale_" + name)) for name in SETTING_FIELDS},"upscale_mode": settings.upscale_mode,"ai_gpu_uuid": settings.ai_gpu_uuid,- "video_gpu_uuid": settings.video_gpu_uuid,- "nr_preset": settings.nr_preset,- "nr_style": settings.nr_style,- "nr_intensity": f"{settings.nr_intensity:.2f}",- "local_tone_strength": f"{settings.local_tone_strength:.2f}",- "local_structure_strength": f"{settings.local_structure_strength:.2f}",- "skin_structure_strength": f"{settings.skin_structure_strength:.2f}",- "automatic_mask": str(settings.automatic_mask).lower(),- "upscaling_factor": f"{settings.upscaling_factor:g}",- "codec": settings.codec,- "container": settings.container,- "quality": settings.quality,- "hdr_mode": str(settings.hdr_mode).lower(),- "image_format": settings.image_format,- "image_quality": str(settings.image_quality),- "image_rename_mode": settings.image_rename_mode,- "image_custom_suffix": settings.image_custom_suffix,- "video_rename_mode": settings.video_rename_mode,- "video_custom_suffix": settings.video_custom_suffix,- "dlss_model_preset": settings.dlss_model_preset,- "frame_interpolation_target_fps": settings.frame_interpolation_target_fps,- "frame_interpolation_engine": settings.frame_interpolation_engine,- "frame_interpolation_codec": settings.frame_interpolation_codec,- "frame_interpolation_container": settings.frame_interpolation_container,- "frame_interpolation_quality": settings.frame_interpolation_quality,- "frame_interpolation_hdr_mode": str(settings.frame_interpolation_hdr_mode).lower(),- "frame_interpolation_rename_mode": settings.frame_interpolation_rename_mode,- "frame_interpolation_custom_suffix": settings.frame_interpolation_custom_suffix,- "preview_encoding": settings.preview_encoding,+ "video_gpu_uuid": settings.video_gpu_uuid,+ "nr_style": settings.nr_style,+ "nr_intensity": f"{settings.nr_intensity:.2f}",+ "nr_passes": str(settings.nr_passes),+ "local_tone_strength": f"{settings.local_tone_strength:.2f}",+ "local_structure_strength": f"{settings.local_structure_strength:.2f}",+ "skin_structure_strength": f"{settings.skin_structure_strength:.2f}",+ "nr_color_strength": f"{settings.nr_color_strength:.2f}",+ "tone_preservation": f"{settings.tone_preservation:.2f}",+ "face_skin_protection": f"{settings.face_skin_protection:.2f}",+ "grain_preservation": f"{settings.grain_preservation:.2f}",+ "shimmer_suppression": f"{settings.shimmer_suppression:.2f}",+ "mask_feather": str(settings.mask_feather),+ "automatic_mask": str(settings.automatic_mask).lower(),+ "nr_gpu_mode": str(settings.nr_gpu_mode).lower(),+ "upscaling_factor": f"{settings.upscaling_factor:g}",+ "codec": settings.codec,+ "container": settings.container,+ "quality": settings.quality,+ "hdr_mode": str(settings.hdr_mode).lower(),+ "image_format": settings.image_format,+ "image_quality": str(settings.image_quality),+ "image_rename_mode": settings.image_rename_mode,+ "image_custom_suffix": settings.image_custom_suffix,+ "video_rename_mode": settings.video_rename_mode,+ "video_custom_suffix": settings.video_custom_suffix,+ "frame_interpolation_target_fps": settings.frame_interpolation_target_fps,+ "frame_interpolation_engine": settings.frame_interpolation_engine,+ "frame_interpolation_codec": settings.frame_interpolation_codec,+ "frame_interpolation_container": settings.frame_interpolation_container,+ "frame_interpolation_quality": settings.frame_interpolation_quality,+ "frame_interpolation_hdr_mode": str(settings.frame_interpolation_hdr_mode).lower(),+ "frame_interpolation_rename_mode": settings.frame_interpolation_rename_mode,+ "frame_interpolation_custom_suffix": settings.frame_interpolation_custom_suffix,+ "preview_encoding": settings.preview_encoding,+ "full_size_image_previews": str(settings.full_size_image_previews).lower(),}-- temporary = config_path.with_name(f".{config_path.name}.tmp")- try:- with temporary.open("w", encoding="utf-8", newline="") as stream:- parser.write(stream)- os.replace(temporary, config_path)- finally:- if temporary.exists():- temporary.unlink()---class _SettingsState:- def __init__(self) -> None:- self.lock = threading.Lock()- self.current: UISettings | None = None---SETTINGS_STATE = _SettingsState()---def processing_gpu_settings() -> tuple[str, str]:- with SETTINGS_STATE.lock:- settings = SETTINGS_STATE.current or load_settings(CONFIG_PATH)- return settings.ai_gpu_uuid, settings.video_gpu_uuid---def current_preview_encoding() -> str:- from ..core.ffmpeg.preview import normalize_preview_encoding-- with SETTINGS_STATE.lock:- settings = SETTINGS_STATE.current or load_settings(CONFIG_PATH)- return normalize_preview_encoding(settings.preview_encoding)++ temporary = config_path.with_name(f".{config_path.name}.tmp")+ try:+ with temporary.open("w", encoding="utf-8", newline="") as stream:+ parser.write(stream)+ os.replace(temporary, config_path)+ finally:+ if temporary.exists():+ temporary.unlink()+++class _SettingsState:+ def __init__(self) -> None:+ self.lock = threading.Lock()+ self.current: UISettings | None = None+++SETTINGS_STATE = _SettingsState()+++def processing_gpu_settings() -> tuple[str, str]:+ with SETTINGS_STATE.lock:+ settings = SETTINGS_STATE.current or load_settings(CONFIG_PATH)+ return settings.ai_gpu_uuid, settings.video_gpu_uuid+++def current_preview_encoding() -> str:+ from ..core.ffmpeg.preview import normalize_preview_encoding++ with SETTINGS_STATE.lock:+ settings = SETTINGS_STATE.current or load_settings(CONFIG_PATH)+ return normalize_preview_encoding(settings.preview_encoding)+++def full_size_image_previews_enabled() -> bool:+ """Return the saved image-preview quality mode for one operation snapshot."""+ with SETTINGS_STATE.lock:+ settings = SETTINGS_STATE.current or load_settings(CONFIG_PATH)+ return bool(settings.full_size_image_previews)diff --git a/src/settings/ui.py b/src/settings/ui.pyindex 3995830..1fb83d6 100644--- a/src/settings/ui.py+++ b/src/settings/ui.py@@ -1,239 +1,370 @@-from __future__ import annotations--from dataclasses import dataclass, replace--import gradio as gr-+from __future__ import annotations++from dataclasses import dataclass, replace++import gradio as gr+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 UPSCALING_MODES, prepare_runtime-from ..core.ffmpeg.preview import normalize_preview_encoding+from ..core.gpu_selection import gpu_choice_label+from ..core.paths import CONFIG_PATH+from ..core.runtime import UPSCALING_MODES, prepare_runtime+from ..core.ffmpeg.preview import normalize_preview_encodingfrom .models import (DEFAULT_SETTINGS, PREVIEW_ENCODING_CHOICES, UPSCALE_MODE_CHOICES, UISettings,automatic_mask_choice, coerce_hdr_mode, parse_automatic_mask,)-from .presets import export_settings_preset, import_settings_preset, preset_filename-from .storage import SETTINGS_STATE, load_settings, save_settings-from ..upscale.video.models import SETTING_FIELDS, UpscaleOptions-from ..upscale.image.models import SETTING_FIELDS as IMAGE_UPSCALE_FIELDS, ImageUpscaleOptions--_CONFIG_LOCK = SETTINGS_STATE.lock-UPSCALING_CHOICES = tuple((mode["label"], factor) for factor, mode in UPSCALING_MODES.items())--def _neural_values(- settings: UISettings,-) -> tuple[str, str, float, float, float, float, float, str]:- return (- settings.nr_preset,- settings.nr_style,- settings.nr_intensity,- settings.local_tone_strength,- settings.local_structure_strength,- settings.skin_structure_strength,- settings.upscaling_factor,- automatic_mask_choice(settings.automatic_mask),- )---def _shared_dlss_values(- settings: UISettings,-) -> tuple[str, str, float, float, float, float, float, str, str]:- return (*_neural_values(settings), settings.dlss_model_preset)---def _mirrored_dlss_values(settings: UISettings) -> tuple:- """Shared 9-tuple twice: feeds the Image and Video mode 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,- 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,- image_format: str,- image_quality: float,- rename_mode: str,- custom_suffix: 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,- image_format=image_format,- image_quality=int(image_quality),- image_rename_mode=rename_mode,- image_custom_suffix=custom_suffix,- )- if settings != current:- save_settings(CONFIG_PATH, settings)- SETTINGS_STATE.current = settings- _notify_live_effects(settings)- return _mirrored_dlss_values(settings)---def persist_video_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,- codec: str,- container: str,- quality: str,- hdr_mode: bool,- rename_mode: str,- custom_suffix: str,-) -> tuple:- with _CONFIG_LOCK:- current = SETTINGS_STATE.current or load_settings(CONFIG_PATH)- coerced_hdr = coerce_hdr_mode(codec, hdr_mode)- 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,- codec=codec,- container=container,- quality=quality,- hdr_mode=coerced_hdr,- video_rename_mode=rename_mode,- video_custom_suffix=custom_suffix,- )- if settings != current:- save_settings(CONFIG_PATH, settings)- SETTINGS_STATE.current = 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(- target_fps: str,- engine: str,- codec: str,- container: str,- quality: str,- hdr_mode: bool,- rename_mode: str,- custom_suffix: str,-) -> None:- with _CONFIG_LOCK:- current = SETTINGS_STATE.current or load_settings(CONFIG_PATH)- coerced_hdr = coerce_hdr_mode(codec, hdr_mode)- settings = replace(- current,- frame_interpolation_target_fps=target_fps,- frame_interpolation_engine=engine,- frame_interpolation_codec=codec,- frame_interpolation_container=container,- frame_interpolation_quality=quality,- frame_interpolation_hdr_mode=coerced_hdr,- frame_interpolation_rename_mode=rename_mode,- frame_interpolation_custom_suffix=custom_suffix,- )- if settings != current:- save_settings(CONFIG_PATH, settings)- SETTINGS_STATE.current = settings---def persist_upscale_settings(*values) -> None:- chosen = dict(zip(SETTING_FIELDS, values))- if not hdr_mode_supported(chosen["codec"]):- chosen["hdr_enabled"] = False- options = UpscaleOptions(**chosen)- options.validate(for_render=False)- defaults = UpscaleOptions()- for name in SETTING_FIELDS:- if type(getattr(defaults, name)) is int:- chosen[name] = int(chosen[name])- chosen["scale_factor"] = float(chosen["scale_factor"])- with _CONFIG_LOCK:- current = SETTINGS_STATE.current or load_settings(CONFIG_PATH)- settings = replace(current, **{"upscale_" + name: value for name, value in chosen.items()})- if settings != current:- save_settings(CONFIG_PATH, settings)- SETTINGS_STATE.current = settings--+from .presets import export_settings_preset, import_settings_preset, preset_filename+from .storage import SETTINGS_STATE, load_settings, save_settings+from ..upscale.video.models import SETTING_FIELDS, UpscaleOptions+from ..upscale.image.models import SETTING_FIELDS as IMAGE_UPSCALE_FIELDS, ImageUpscaleOptions++_CONFIG_LOCK = SETTINGS_STATE.lock+UPSCALING_CHOICES = tuple((mode["label"], factor) for factor, mode in UPSCALING_MODES.items())+_SKIN_STRUCTURE_INDEX = 5+_AUTOMATIC_MASK_INDEX = 7+_NR_COLOR_INDEX = 8+_TONE_PRESERVATION_INDEX = 9+PROCESSING_ENGINE_CHOICES = ("VRAM", "RAM")+++def processing_engine_choice(enabled: bool) -> str:+ """Map the stored bool path to its visible VRAM/RAM label."""+ return "VRAM" if enabled else "RAM"+++def parse_processing_engine(value: object) -> bool:+ """Map the visible VRAM/RAM selection back to its stored bool path."""+ if value not in PROCESSING_ENGINE_CHOICES:+ choices = ", ".join(PROCESSING_ENGINE_CHOICES)+ raise ValueError(f"Processing Engine Path must be one of: {choices}.")+ return value == "VRAM"+++def automatic_mask_for_skin_input(+ skin_structure_strength: float, automatic_mask: str,+) -> str:+ """Make an explicit Skin adjustment effective without overriding later Mask Off."""+ try:+ skin = float(skin_structure_strength)+ except (TypeError, ValueError) as exc:+ raise gr.Error("Skin Structure Strength must be between -1 and 2.") from exc+ if not -1.0 <= skin <= 2.0:+ raise gr.Error("Skin Structure Strength must be between -1 and 2.")+ return "On" if skin > -1.0 else automatic_mask+++def _persist_skin_input(persist, values: tuple) -> tuple:+ effective = list(values)+ effective[_AUTOMATIC_MASK_INDEX] = automatic_mask_for_skin_input(+ effective[_SKIN_STRUCTURE_INDEX], effective[_AUTOMATIC_MASK_INDEX]+ )+ mirrored = persist(*effective)+ return (effective[_AUTOMATIC_MASK_INDEX], *mirrored)+++def _neural_values(+ settings: UISettings,+) -> tuple[str, float, int, float, float, float, float, str, float, float, float, float, int]:+ return (+ settings.nr_style,+ settings.nr_intensity,+ settings.nr_passes,+ settings.local_tone_strength,+ settings.local_structure_strength,+ settings.skin_structure_strength,+ settings.upscaling_factor,+ automatic_mask_choice(settings.automatic_mask),+ settings.nr_color_strength,+ settings.tone_preservation,+ settings.face_skin_protection,+ settings.grain_preservation,+ settings.mask_feather,+ )+++def _mirrored_dlss_values(settings: UISettings) -> tuple:+ """Image controls followed by temporal Video/Live controls."""+ shared = _neural_values(settings)+ temporal = (*shared, settings.shimmer_suppression)+ return (*shared, *temporal)+++def _video_live_values(settings: UISettings) -> tuple:+ return (*_neural_values(settings), settings.shimmer_suppression)+++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_style: str,+ nr_intensity: float,+ nr_passes: float,+ local_tone_strength: float,+ local_structure_strength: float,+ skin_structure_strength: float,+ upscaling_factor: float,+ automatic_mask: str,+ nr_color_strength: float,+ tone_preservation: float,+ face_skin_protection: float,+ grain_preservation: float,+ mask_feather: float,+ image_format: str,+ image_quality: float,+ rename_mode: str,+ custom_suffix: str,+) -> tuple:+ with _CONFIG_LOCK:+ current = SETTINGS_STATE.current or load_settings(CONFIG_PATH)+ settings = replace(+ current,+ nr_style=nr_style,+ nr_intensity=nr_intensity,+ nr_passes=int(nr_passes),+ 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),+ nr_color_strength=float(nr_color_strength),+ tone_preservation=float(tone_preservation),+ face_skin_protection=float(face_skin_protection),+ grain_preservation=float(grain_preservation),+ mask_feather=int(mask_feather),+ image_format=image_format,+ image_quality=int(image_quality),+ image_rename_mode=rename_mode,+ image_custom_suffix=custom_suffix,+ )+ if settings != current:+ save_settings(CONFIG_PATH, settings)+ SETTINGS_STATE.current = settings+ _notify_live_effects(settings)+ temporal = _video_live_values(settings)+ return (*temporal, *temporal)+++def persist_video_settings(+ nr_style: str,+ nr_intensity: float,+ nr_passes: float,+ local_tone_strength: float,+ local_structure_strength: float,+ skin_structure_strength: float,+ upscaling_factor: float,+ automatic_mask: str,+ nr_color_strength: float,+ tone_preservation: float,+ face_skin_protection: float,+ grain_preservation: float,+ mask_feather: float,+ shimmer_suppression: float,+ codec: str,+ container: str,+ quality: str,+ hdr_mode: bool,+ rename_mode: str,+ custom_suffix: str,+) -> tuple:+ with _CONFIG_LOCK:+ current = SETTINGS_STATE.current or load_settings(CONFIG_PATH)+ coerced_hdr = coerce_hdr_mode(codec, hdr_mode)+ settings = replace(+ current,+ nr_style=nr_style,+ nr_intensity=nr_intensity,+ nr_passes=int(nr_passes),+ 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),+ nr_color_strength=float(nr_color_strength),+ tone_preservation=float(tone_preservation),+ face_skin_protection=float(face_skin_protection),+ grain_preservation=float(grain_preservation),+ mask_feather=int(mask_feather),+ shimmer_suppression=float(shimmer_suppression),+ codec=codec,+ container=container,+ quality=quality,+ hdr_mode=coerced_hdr,+ video_rename_mode=rename_mode,+ video_custom_suffix=custom_suffix,+ )+ if settings != current:+ save_settings(CONFIG_PATH, settings)+ SETTINGS_STATE.current = settings+ _notify_live_effects(settings)+ return (*_neural_values(settings), *_video_live_values(settings))+++def persist_live_settings(+ nr_style: str,+ nr_intensity: float,+ nr_passes: float,+ local_tone_strength: float,+ local_structure_strength: float,+ skin_structure_strength: float,+ upscaling_factor: float,+ automatic_mask: str,+ nr_color_strength: float,+ tone_preservation: float,+ face_skin_protection: float,+ grain_preservation: float,+ mask_feather: float,+ shimmer_suppression: float,+) -> tuple:+ with _CONFIG_LOCK:+ current = SETTINGS_STATE.current or load_settings(CONFIG_PATH)+ settings = replace(+ current,+ nr_style=nr_style,+ nr_intensity=nr_intensity,+ nr_passes=int(nr_passes),+ 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),+ nr_color_strength=float(nr_color_strength),+ tone_preservation=float(tone_preservation),+ face_skin_protection=float(face_skin_protection),+ grain_preservation=float(grain_preservation),+ mask_feather=int(mask_feather),+ shimmer_suppression=float(shimmer_suppression),+ )+ if settings != current:+ save_settings(CONFIG_PATH, settings)+ SETTINGS_STATE.current = settings+ _notify_live_effects(settings)+ return (*_neural_values(settings), *_video_live_values(settings))+++def persist_image_skin_settings(*values) -> tuple:+ return _persist_skin_input(persist_image_settings, values)+++def persist_video_skin_settings(*values) -> tuple:+ return _persist_skin_input(persist_video_settings, values)+++def persist_live_skin_settings(*values) -> tuple:+ return _persist_skin_input(persist_live_settings, values)+++def persist_nr_mask(selection: object | None) -> None:+ """Update session-only mask state without writing its temporary path to disk."""+ from ..core.nr_composition import mask_selection++ selected = mask_selection(selection)+ with _CONFIG_LOCK:+ current = SETTINGS_STATE.current or load_settings(CONFIG_PATH)+ settings = replace(current, nr_mask=selected)+ SETTINGS_STATE.current = settings+ _notify_live_effects(settings)+++def apply_detail_only_settings() -> tuple:+ """Apply the editable Detail-Only preset to every mirrored interface."""+ with _CONFIG_LOCK:+ current = SETTINGS_STATE.current or load_settings(CONFIG_PATH)+ settings = replace(current, nr_color_strength=0.0, tone_preservation=1.0)+ if settings != current:+ save_settings(CONFIG_PATH, settings)+ SETTINGS_STATE.current = settings+ _notify_live_effects(settings)+ shared = _neural_values(settings)+ temporal = _video_live_values(settings)+ return (*shared, *temporal, *temporal)+++def persist_nr_gpu_mode(selection: object) -> bool:+ """Persist the global Processing Engine Path switch and update Live effects."""+ if isinstance(selection, bool):+ enabled = selection+ else:+ try:+ enabled = parse_processing_engine(selection)+ except ValueError as exc:+ raise gr.Error(str(exc)) from exc+ with _CONFIG_LOCK:+ current = SETTINGS_STATE.current or load_settings(CONFIG_PATH)+ settings = replace(current, nr_gpu_mode=enabled)+ if settings != current:+ save_settings(CONFIG_PATH, settings)+ SETTINGS_STATE.current = settings+ _notify_live_effects(settings)+ return enabled+++def persist_frame_interpolation_settings(+ target_fps: str,+ engine: str,+ codec: str,+ container: str,+ quality: str,+ hdr_mode: bool,+ rename_mode: str,+ custom_suffix: str,+) -> None:+ with _CONFIG_LOCK:+ current = SETTINGS_STATE.current or load_settings(CONFIG_PATH)+ coerced_hdr = coerce_hdr_mode(codec, hdr_mode)+ settings = replace(+ current,+ frame_interpolation_target_fps=target_fps,+ frame_interpolation_engine=engine,+ frame_interpolation_codec=codec,+ frame_interpolation_container=container,+ frame_interpolation_quality=quality,+ frame_interpolation_hdr_mode=coerced_hdr,+ frame_interpolation_rename_mode=rename_mode,+ frame_interpolation_custom_suffix=custom_suffix,+ )+ if settings != current:+ save_settings(CONFIG_PATH, settings)+ SETTINGS_STATE.current = settings+++def persist_upscale_settings(*values) -> None:+ chosen = dict(zip(SETTING_FIELDS, values))+ if not hdr_mode_supported(chosen["codec"]):+ chosen["hdr_enabled"] = False+ options = UpscaleOptions(**chosen)+ options.validate(for_render=False)+ defaults = UpscaleOptions()+ for name in SETTING_FIELDS:+ if type(getattr(defaults, name)) is int:+ chosen[name] = int(chosen[name])+ chosen["scale_factor"] = float(chosen["scale_factor"])+ with _CONFIG_LOCK:+ current = SETTINGS_STATE.current or load_settings(CONFIG_PATH)+ settings = replace(current, **{"upscale_" + name: value for name, value in chosen.items()})+ if settings != current:+ save_settings(CONFIG_PATH, settings)+ SETTINGS_STATE.current = settings++def persist_image_upscale_settings(*values) -> None:- chosen = dict(zip(IMAGE_UPSCALE_FIELDS, values))- ImageUpscaleOptions(**chosen).validate()- defaults = ImageUpscaleOptions()- for name in IMAGE_UPSCALE_FIELDS:- if type(getattr(defaults, name)) is int:- chosen[name] = int(chosen[name])- chosen["scale_factor"] = float(chosen["scale_factor"])- with _CONFIG_LOCK:- current = SETTINGS_STATE.current or load_settings(CONFIG_PATH)- settings = replace(current, **{"upscale_image_" + n: v for n, v in chosen.items()})- if settings != current:- save_settings(CONFIG_PATH, settings)+ chosen = dict(zip(IMAGE_UPSCALE_FIELDS, values))+ ImageUpscaleOptions(**chosen).validate()+ defaults = ImageUpscaleOptions()+ for name in IMAGE_UPSCALE_FIELDS:+ if type(getattr(defaults, name)) is int:+ chosen[name] = int(chosen[name])+ chosen["scale_factor"] = float(chosen["scale_factor"])+ with _CONFIG_LOCK:+ current = SETTINGS_STATE.current or load_settings(CONFIG_PATH)+ settings = replace(current, **{"upscale_image_" + n: v for n, v in chosen.items()})+ if settings != current:+ save_settings(CONFIG_PATH, settings)SETTINGS_STATE.current = settings@@ -246,353 +377,403 @@ def persist_upscale_mode(mode: str) -> None:if settings != current:save_settings(CONFIG_PATH, settings)SETTINGS_STATE.current = settings---def _settings_component_values(settings: UISettings) -> tuple:- shared = _shared_dlss_values(settings)- return (- *shared,- *shared,- settings.image_format,- settings.image_quality,- settings.image_rename_mode,- gr.update(- value=settings.image_custom_suffix,- interactive=settings.image_rename_mode == "Custom",- ),- settings.codec,- settings.container,- settings.quality,- gr.update(value=settings.hdr_mode, interactive=hdr_mode_supported(settings.codec)),- settings.video_rename_mode,- gr.update(- value=settings.video_custom_suffix,- interactive=settings.video_rename_mode == "Custom",- ),- settings.frame_interpolation_target_fps,- settings.frame_interpolation_engine,- settings.frame_interpolation_codec,- settings.frame_interpolation_container,- settings.frame_interpolation_quality,- gr.update(value=settings.frame_interpolation_hdr_mode, interactive=hdr_mode_supported(settings.frame_interpolation_codec)),- settings.frame_interpolation_rename_mode,- gr.update(- value=settings.frame_interpolation_custom_suffix,- interactive=settings.frame_interpolation_rename_mode == "Custom",- ),- settings.ai_gpu_uuid,- settings.video_gpu_uuid,- settings.preview_encoding,+++def _settings_component_values(settings: UISettings) -> tuple:+ shared = _neural_values(settings)+ temporal = _video_live_values(settings)+ return (*shared,+ *temporal,+ settings.image_format,+ settings.image_quality,+ settings.image_rename_mode,+ gr.update(+ value=settings.image_custom_suffix,+ interactive=settings.image_rename_mode == "Custom",+ ),+ settings.codec,+ settings.container,+ settings.quality,+ gr.update(value=settings.hdr_mode, interactive=hdr_mode_supported(settings.codec)),+ settings.video_rename_mode,+ gr.update(+ value=settings.video_custom_suffix,+ interactive=settings.video_rename_mode == "Custom",+ ),+ settings.frame_interpolation_target_fps,+ settings.frame_interpolation_engine,+ settings.frame_interpolation_codec,+ settings.frame_interpolation_container,+ settings.frame_interpolation_quality,+ gr.update(value=settings.frame_interpolation_hdr_mode, interactive=hdr_mode_supported(settings.frame_interpolation_codec)),+ settings.frame_interpolation_rename_mode,+ gr.update(+ value=settings.frame_interpolation_custom_suffix,+ interactive=settings.frame_interpolation_rename_mode == "Custom",+ ),+ settings.ai_gpu_uuid,+ settings.video_gpu_uuid,+ settings.preview_encoding,+ settings.full_size_image_previews,+ processing_engine_choice(settings.nr_gpu_mode),+ settings.nr_gpu_mode,+ *temporal,settings.upscale_mode,*(getattr(settings, "upscale_" + name) for name in SETTING_FIELDS),- *(getattr(settings, "upscale_image_" + name) for name in IMAGE_UPSCALE_FIELDS),- )---def _gpu_choices(prepared) -> tuple[list[tuple[str, str]], list[tuple[str, str]]]:- automatic = [("Auto", "auto")]- ai = []- for gpu in prepared.gpus:- if not gpu.get("ai_compatible"):- continue- ai.append((gpu_choice_label(gpu), str(gpu["uuid"])))- video = [- (gpu_choice_label(gpu), str(gpu["uuid"]))- for gpu in prepared.gpus- if gpu.get("cuda_ordinal") is not None- and probe_nvenc_codecs(int(gpu["cuda_ordinal"]))- ]- return automatic + ai, automatic + video---def _normalize_gpu_settings(settings: UISettings, prepared) -> tuple[UISettings, str]:- ai_choices, video_choices = _gpu_choices(prepared)- ai_values = {value for _label, value in ai_choices}- video_values = {value for _label, value in video_choices}- warnings = []- ai_uuid = settings.ai_gpu_uuid- video_uuid = settings.video_gpu_uuid- if ai_uuid not in ai_values:- warnings.append("Saved AI Processing GPU is unavailable; using Auto.")- ai_uuid = "auto"- if video_uuid not in video_values:- warnings.append("Saved Video Processing GPU is unavailable; using Auto.")- video_uuid = "auto"- return replace(settings, ai_gpu_uuid=ai_uuid, video_gpu_uuid=video_uuid), " ".join(warnings)---def persist_preview_encoding(preview_encoding: str) -> None:- normalized = normalize_preview_encoding(preview_encoding)- if not isinstance(preview_encoding, str) or preview_encoding.strip() not in PREVIEW_ENCODING_CHOICES:- raise gr.Error(- f"Preview Encoding must be one of: {', '.join(PREVIEW_ENCODING_CHOICES)}."- )- with _CONFIG_LOCK:- current = SETTINGS_STATE.current or load_settings(CONFIG_PATH)- settings = replace(current, preview_encoding=normalized)- if settings != current:- save_settings(CONFIG_PATH, settings)- SETTINGS_STATE.current = settings--+ *(getattr(settings, "upscale_image_" + name) for name in IMAGE_UPSCALE_FIELDS),+ )+++def _gpu_choices(prepared) -> tuple[list[tuple[str, str]], list[tuple[str, str]]]:+ automatic = [("Auto", "auto")]+ ai = []+ for gpu in prepared.gpus:+ if not gpu.get("ai_compatible"):+ continue+ ai.append((gpu_choice_label(gpu), str(gpu["uuid"])))+ video = [+ (gpu_choice_label(gpu), str(gpu["uuid"]))+ for gpu in prepared.gpus+ if gpu.get("cuda_ordinal") is not None+ and probe_nvenc_codecs(int(gpu["cuda_ordinal"]))+ ]+ return automatic + ai, automatic + video+++def _normalize_gpu_settings(settings: UISettings, prepared) -> tuple[UISettings, str]:+ ai_choices, video_choices = _gpu_choices(prepared)+ ai_values = {value for _label, value in ai_choices}+ video_values = {value for _label, value in video_choices}+ warnings = []+ ai_uuid = settings.ai_gpu_uuid+ video_uuid = settings.video_gpu_uuid+ if ai_uuid not in ai_values:+ warnings.append("Saved AI Processing GPU is unavailable; using Auto.")+ ai_uuid = "auto"+ if video_uuid not in video_values:+ warnings.append("Saved Video Processing GPU is unavailable; using Auto.")+ video_uuid = "auto"+ return replace(settings, ai_gpu_uuid=ai_uuid, video_gpu_uuid=video_uuid), " ".join(warnings)+++def persist_preview_encoding(preview_encoding: str) -> None:+ normalized = normalize_preview_encoding(preview_encoding)+ if not isinstance(preview_encoding, str) or preview_encoding.strip() not in PREVIEW_ENCODING_CHOICES:+ raise gr.Error(+ f"Preview Encoding must be one of: {', '.join(PREVIEW_ENCODING_CHOICES)}."+ )+ with _CONFIG_LOCK:+ current = SETTINGS_STATE.current or load_settings(CONFIG_PATH)+ settings = replace(current, preview_encoding=normalized)+ if settings != current:+ save_settings(CONFIG_PATH, settings)+ SETTINGS_STATE.current = settings+++def persist_full_size_image_previews(enabled: bool) -> None:+ if not isinstance(enabled, bool):+ raise gr.Error("Full size quality preview must be enabled or disabled.")+ with _CONFIG_LOCK:+ current = SETTINGS_STATE.current or load_settings(CONFIG_PATH)+ settings = replace(current, full_size_image_previews=enabled)+ if settings != current:+ save_settings(CONFIG_PATH, settings)+ SETTINGS_STATE.current = settings++def persist_gpu_settings(ai_gpu_uuid: str, video_gpu_uuid: str) -> str:- prepared = prepare_runtime()- ai_choices, video_choices = _gpu_choices(prepared)- if ai_gpu_uuid not in {value for _label, value in ai_choices}:- raise gr.Error("Choose an available AI Processing GPU.")- if video_gpu_uuid not in {value for _label, value in video_choices}:- raise gr.Error("Choose an available Video Processing GPU.")- with _CONFIG_LOCK:- current = SETTINGS_STATE.current or load_settings(CONFIG_PATH)- settings = replace(- current, ai_gpu_uuid=ai_gpu_uuid, video_gpu_uuid=video_gpu_uuid- )- if settings != current:- save_settings(CONFIG_PATH, settings)- SETTINGS_STATE.current = settings- ai_name = "Auto" if ai_gpu_uuid == "auto" else next(- label for label, value in ai_choices if value == ai_gpu_uuid- )- video_name = "Auto" if video_gpu_uuid == "auto" else next(- label for label, value in video_choices if value == video_gpu_uuid- )- return f"AI Processing: {ai_name}\n\nVideo Processing: {video_name}"---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 Neural Rendering, Upscale, and Frame Interpolation settings were reset to defaults."- return (- *_settings_component_values(DEFAULT_SETTINGS),- message,- message,- message,- message,- message,- )---def settings_preset_download(name: str) -> str | None:- if not isinstance(name, str) or not name.strip():- return None- with _CONFIG_LOCK:- current = SETTINGS_STATE.current or load_settings(CONFIG_PATH)- try:- path = export_settings_preset(name, current)- except ValueError:- return None- return str(path)---def settings_preset_export_status(name: str) -> str:- try:- filename = preset_filename(name)- except ValueError as exc:- return f"Export failed: {exc}"- return f"Exported preset {name.strip()!r} as {filename}."---def apply_settings_preset(- uploaded_path: str | None, current_name: str-) -> tuple:- with _CONFIG_LOCK:- current = SETTINGS_STATE.current or load_settings(CONFIG_PATH)- try:- if not uploaded_path:- raise ValueError("Choose a JSON preset file before importing.")- name, imported = import_settings_preset(uploaded_path, current)- imported, gpu_warning = _normalize_gpu_settings(- imported, prepare_runtime()- )- save_settings(CONFIG_PATH, imported)- except (OSError, ValueError) as exc:- return (- current_name,- *_settings_component_values(current),- f"Import failed: {exc}",- )- SETTINGS_STATE.current = imported- _notify_live_effects(imported)- return (- name,- *_settings_component_values(imported),- f"Imported preset {name!r}; all modes, tabs, and saved startup settings were updated."- + (f" {gpu_warning}" if gpu_warning else ""),- )--@dataclass(slots=True)+ prepared = prepare_runtime()+ ai_choices, video_choices = _gpu_choices(prepared)+ if ai_gpu_uuid not in {value for _label, value in ai_choices}:+ raise gr.Error("Choose an available AI Processing GPU.")+ if video_gpu_uuid not in {value for _label, value in video_choices}:+ raise gr.Error("Choose an available Video Processing GPU.")+ with _CONFIG_LOCK:+ current = SETTINGS_STATE.current or load_settings(CONFIG_PATH)+ settings = replace(+ current, ai_gpu_uuid=ai_gpu_uuid, video_gpu_uuid=video_gpu_uuid+ )+ if settings != current:+ save_settings(CONFIG_PATH, settings)+ SETTINGS_STATE.current = settings+ ai_name = "Auto" if ai_gpu_uuid == "auto" else next(+ label for label, value in ai_choices if value == ai_gpu_uuid+ )+ video_name = "Auto" if video_gpu_uuid == "auto" else next(+ label for label, value in video_choices if value == video_gpu_uuid+ )+ return f"AI Processing: {ai_name}\n\nVideo Processing: {video_name}"+++def reset_saved_settings() -> tuple:+ with _CONFIG_LOCK:+ current = SETTINGS_STATE.current or load_settings(CONFIG_PATH)+ # Custom NR Mask is session-scoped, so resetting persisted controls must+ # not silently detach the mask that remains visible in all three tabs.+ settings = replace(DEFAULT_SETTINGS, nr_mask=current.nr_mask)+ save_settings(CONFIG_PATH, settings)+ SETTINGS_STATE.current = settings+ _notify_live_effects(settings)+ message = "All Neural Rendering, Upscale, and Frame Interpolation settings were reset to defaults."+ return (+ *_settings_component_values(settings),+ message,+ message,+ message,+ message,+ message,+ )+++def settings_preset_download(name: str) -> str | None:+ if not isinstance(name, str) or not name.strip():+ return None+ with _CONFIG_LOCK:+ current = SETTINGS_STATE.current or load_settings(CONFIG_PATH)+ try:+ path = export_settings_preset(name, current)+ except ValueError:+ return None+ return str(path)+++def settings_preset_export_status(name: str) -> str:+ try:+ filename = preset_filename(name)+ except ValueError as exc:+ return f"Export failed: {exc}"+ return f"Exported preset {name.strip()!r} as {filename}."+++def apply_settings_preset(+ uploaded_path: str | None, current_name: str+) -> tuple:+ with _CONFIG_LOCK:+ current = SETTINGS_STATE.current or load_settings(CONFIG_PATH)+ try:+ if not uploaded_path:+ raise ValueError("Choose a JSON preset file before importing.")+ name, imported = import_settings_preset(uploaded_path, current)+ imported, gpu_warning = _normalize_gpu_settings(+ imported, prepare_runtime()+ )+ save_settings(CONFIG_PATH, imported)+ except (OSError, ValueError) as exc:+ return (+ current_name,+ *_settings_component_values(current),+ f"Import failed: {exc}",+ )+ SETTINGS_STATE.current = imported+ _notify_live_effects(imported)+ return (+ name,+ *_settings_component_values(imported),+ f"Imported preset {name!r}; all modes, tabs, and saved startup settings were updated."+ + (f" {gpu_warning}" if gpu_warning else ""),+ )++@dataclass(slots=True)class SettingsTab:ai_gpu_selector: objectvideo_gpu_selector: objectpreview_encoding_selector: object+ full_size_image_previews: object+ gpu_mode: object+ processing_engine_state: objectpreset_name: object- preset_export: object- preset_import: object- preset_status: object---def initialize_settings(prepared) -> tuple[UISettings, str, list[tuple[str, str]], list[tuple[str, str]]]:- settings = SETTINGS_STATE.current or load_settings(CONFIG_PATH)- normalized, warning = _normalize_gpu_settings(settings, prepared)- if normalized != settings:- save_settings(CONFIG_PATH, normalized)- SETTINGS_STATE.current = normalized- ai_choices, video_choices = _gpu_choices(prepared)- return normalized, warning, ai_choices, video_choices---def current_settings() -> UISettings:- with _CONFIG_LOCK:- return SETTINGS_STATE.current or load_settings(CONFIG_PATH)---def build_settings_tab(- settings: UISettings,- ai_gpu_choices: list[tuple[str, str]],- video_gpu_choices: list[tuple[str, str]],-) -> SettingsTab:- gr.Markdown(- "## GPU Selection\n"- "AI Processing controls DLSS, RTX Video, and Frame Generation. Video Processing "- "controls only codecs suffixed (NVIDIA NVENC); plain H.264/H.265/AV1 and ProRes remain CPU-based."- )- with gr.Row():- ai_gpu_selector = gr.Dropdown(- choices=ai_gpu_choices,- value=settings.ai_gpu_uuid,- label="AI Processing GPU",- info="Used for DLSS Neural Rendering, RTX Video, and Frame Generation.",- )- video_gpu_selector = gr.Dropdown(- choices=video_gpu_choices,- value=settings.video_gpu_uuid,- label="Video Processing GPU",- info="Used only for codecs suffixed (NVIDIA NVENC); plain H.264/H.265/AV1 and ProRes stay on CPU.",- )- gr.Markdown(- "## Preview Encoding\n"- "Controls how in-app video previews are produced for Neural Rendering's Video mode, "- "Upscale, and Frame Interpolation (preview buttons and final-render player)."- )- preview_encoding_selector = gr.Radio(- choices=list(PREVIEW_ENCODING_CHOICES),- value=normalize_preview_encoding(settings.preview_encoding),- label="Preview Encoding",- info=(- "Auto uses the result directly when the browser can play it (MP4 + H.264, "- "verified by probe); otherwise creates an H.264 preview. "- "Always H.264 always generates a browser-compatible preview. "- "Disabled never creates one and sends the actual file to the browser. "- "Non-H.264 previews can be slower and larger."- ),- )- gr.Markdown(- "## Settings Presets\n"- "Export every adjustable option from Neural Rendering's Image and Video modes, "- "Upscale, and Frame Interpolation, or import a preset to apply it everywhere."- )- preset_name = gr.Textbox(- label="Preset name",- placeholder="Cinematic 2",- info="The exported file uses this name; the original name is stored in JSON.",- )- with gr.Row():- preset_export = gr.DownloadButton(- "Export preset",- value=settings_preset_download,- inputs=preset_name,- variant="primary",- scale=1,- )- preset_import = gr.UploadButton(- "Import preset",- file_count="single",- file_types=[".json"],- type="filepath",- variant="primary",- scale=1,- )- preset_status = gr.Markdown("", elem_id="preset-status")+ preset_export: object+ preset_import: object+ preset_status: object+++def initialize_settings(prepared) -> tuple[UISettings, str, list[tuple[str, str]], list[tuple[str, str]]]:+ settings = SETTINGS_STATE.current or load_settings(CONFIG_PATH)+ normalized, warning = _normalize_gpu_settings(settings, prepared)+ if normalized != settings:+ save_settings(CONFIG_PATH, normalized)+ SETTINGS_STATE.current = normalized+ ai_choices, video_choices = _gpu_choices(prepared)+ return normalized, warning, ai_choices, video_choices+++def current_settings() -> UISettings:+ with _CONFIG_LOCK:+ return SETTINGS_STATE.current or load_settings(CONFIG_PATH)+++def build_settings_tab(+ settings: UISettings,+ ai_gpu_choices: list[tuple[str, str]],+ video_gpu_choices: list[tuple[str, str]],+ processing_engine_state: object,+) -> SettingsTab:+ gr.Markdown("## GPU Selection")+ with gr.Row():+ ai_gpu_selector = gr.Dropdown(+ choices=ai_gpu_choices,+ value=settings.ai_gpu_uuid,+ label="AI Processing GPU",+ )+ video_gpu_selector = gr.Dropdown(+ choices=video_gpu_choices,+ value=settings.video_gpu_uuid,+ label="Video Processing GPU",+ )+ gr.Markdown("## Processing Engine Path")+ gpu_mode = gr.Radio(+ choices=list(PROCESSING_ENGINE_CHOICES),+ value=processing_engine_choice(settings.nr_gpu_mode),+ label="Processing Engine Path",+ show_label=False,+ )+ gr.Markdown("## Preview Encoding")+ preview_encoding_selector = gr.Radio(+ choices=list(PREVIEW_ENCODING_CHOICES),+ value=normalize_preview_encoding(settings.preview_encoding),+ label="Preview Encoding",+ show_label=False,+ )+ gr.Markdown("## Image Preview Quality")+ full_size_image_previews = gr.Checkbox(+ value=settings.full_size_image_previews,+ label="Full size quality preview",+ )+ gr.Markdown("## Settings Presets")+ preset_name = gr.Textbox(+ label="Preset name",+ placeholder="Preset 13",+ )+ with gr.Row():+ preset_export = gr.DownloadButton(+ "Export preset",+ value=settings_preset_download,+ inputs=preset_name,+ variant="primary",+ scale=1,+ )+ preset_import = gr.UploadButton(+ "Import preset",+ file_count="single",+ file_types=[".json"],+ type="filepath",+ variant="primary",+ scale=1,+ )+ preset_status = gr.Markdown("", elem_id="preset-status")return SettingsTab(- ai_gpu_selector, video_gpu_selector, preview_encoding_selector,- preset_name, preset_export, preset_import, preset_status- )----def settings_component_outputs(image_tab, video_tab, frame_tab, settings_tab, live_tab, upscale_tab) -> list[object]:- return [- *image_tab.neural,- image_tab.model_preset,- *video_tab.neural,- video_tab.model_preset,- image_tab.output_format,- image_tab.quality,- image_tab.rename_mode,- image_tab.custom_suffix,- video_tab.codec,- video_tab.container,- video_tab.quality,- video_tab.hdr_mode,- video_tab.rename_mode,- video_tab.custom_suffix,- frame_tab.target_fps,- frame_tab.engine,- frame_tab.codec,- frame_tab.container,- frame_tab.quality,- frame_tab.hdr_mode,- frame_tab.rename_mode,- frame_tab.custom_suffix,- settings_tab.ai_gpu_selector,+ ai_gpu_selector, video_gpu_selector, preview_encoding_selector, full_size_image_previews,+ gpu_mode, processing_engine_state, preset_name, preset_export, preset_import, preset_status+ )++++def settings_component_outputs(image_tab, video_tab, frame_tab, settings_tab, live_tab, upscale_tab) -> list[object]:+ return [+ *image_tab.neural,+ *video_tab.neural,+ image_tab.output_format,+ image_tab.quality,+ image_tab.rename_mode,+ image_tab.custom_suffix,+ video_tab.codec,+ video_tab.container,+ video_tab.quality,+ video_tab.hdr_mode,+ video_tab.rename_mode,+ video_tab.custom_suffix,+ frame_tab.target_fps,+ frame_tab.engine,+ frame_tab.codec,+ frame_tab.container,+ frame_tab.quality,+ frame_tab.hdr_mode,+ frame_tab.rename_mode,+ frame_tab.custom_suffix,+ settings_tab.ai_gpu_selector,settings_tab.video_gpu_selector,settings_tab.preview_encoding_selector,+ settings_tab.full_size_image_previews,+ settings_tab.gpu_mode,+ settings_tab.processing_engine_state,*live_tab.neural,- live_tab.model_preset,upscale_tab.mode,*upscale_tab.video.settings_inputs,- *upscale_tab.image.settings_inputs,- ]---def bind_settings_events(settings_tab, image_tab, video_tab, frame_tab, live_tab, upscale_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_mirror, *live_mirror],- queue=False,- )- for component in video_tab.settings_inputs:- component.input(- persist_video_settings,- inputs=video_tab.settings_inputs,- 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:- component.input(- persist_frame_interpolation_settings,- inputs=frame_tab.settings_inputs,- queue=False,- )-- for component in upscale_tab.video.settings_inputs:- component.change(persist_upscale_settings, inputs=upscale_tab.video.settings_inputs, queue=False, show_progress="hidden")+ *upscale_tab.image.settings_inputs,+ ]+++def bind_settings_events(settings_tab, image_tab, video_tab, frame_tab, live_tab, upscale_tab) -> None:+ video_mirror = [*video_tab.neural]+ image_mirror = [*image_tab.neural]+ live_mirror = [*live_tab.neural]+ for component in image_tab.settings_inputs:+ if component is image_tab.neural[_SKIN_STRUCTURE_INDEX]:+ continue+ component.input(+ persist_image_settings,+ inputs=image_tab.settings_inputs,+ outputs=[*video_mirror, *live_mirror],+ queue=False,+ )+ for component in video_tab.settings_inputs:+ if component is video_tab.neural[_SKIN_STRUCTURE_INDEX]:+ continue+ component.input(+ persist_video_settings,+ inputs=video_tab.settings_inputs,+ outputs=[*image_mirror, *live_mirror],+ queue=False,+ )+ for component in live_tab.settings_inputs:+ if component is live_tab.neural[_SKIN_STRUCTURE_INDEX]:+ continue+ component.input(+ persist_live_settings,+ inputs=live_tab.settings_inputs,+ outputs=[*image_mirror, *video_mirror],+ queue=False,+ )+ image_tab.neural[_SKIN_STRUCTURE_INDEX].input(+ persist_image_skin_settings,+ inputs=image_tab.settings_inputs,+ outputs=[image_tab.neural[_AUTOMATIC_MASK_INDEX], *video_mirror, *live_mirror],+ queue=False,+ )+ video_tab.neural[_SKIN_STRUCTURE_INDEX].input(+ persist_video_skin_settings,+ inputs=video_tab.settings_inputs,+ outputs=[video_tab.neural[_AUTOMATIC_MASK_INDEX], *image_mirror, *live_mirror],+ queue=False,+ )+ live_tab.neural[_SKIN_STRUCTURE_INDEX].input(+ persist_live_skin_settings,+ inputs=live_tab.settings_inputs,+ outputs=[live_tab.neural[_AUTOMATIC_MASK_INDEX], *image_mirror, *video_mirror],+ queue=False,+ )+ detail_outputs = [*image_tab.neural, *video_tab.neural, *live_tab.neural]+ for button in (+ image_tab.composition.detail_only,+ video_tab.composition.detail_only,+ live_tab.composition.detail_only,+ ):+ button.click(+ apply_detail_only_settings,+ outputs=detail_outputs,+ queue=False,+ show_progress="hidden",+ )+ for component in frame_tab.settings_inputs:+ component.input(+ persist_frame_interpolation_settings,+ inputs=frame_tab.settings_inputs,+ queue=False,+ )++ for component in upscale_tab.video.settings_inputs:+ component.change(persist_upscale_settings, inputs=upscale_tab.video.settings_inputs, queue=False, show_progress="hidden")for component in upscale_tab.image.settings_inputs:component.change(persist_image_upscale_settings, inputs=upscale_tab.image.settings_inputs, queue=False, show_progress="hidden")upscale_tab.mode.input(@@ -601,33 +782,44 @@ def bind_settings_events(settings_tab, image_tab, video_tab, frame_tab, live_tabqueue=False,show_progress="hidden",)- outputs = settings_component_outputs(image_tab, video_tab, frame_tab, settings_tab, live_tab, upscale_tab)- settings_tab.preset_export.click(- settings_preset_export_status,- inputs=settings_tab.preset_name,- outputs=settings_tab.preset_status,- queue=False,- )- settings_tab.preset_import.upload(- apply_settings_preset,- inputs=[settings_tab.preset_import, settings_tab.preset_name],- outputs=[settings_tab.preset_name, *outputs, settings_tab.preset_status],- queue=False,- )- for selector in [settings_tab.ai_gpu_selector, settings_tab.video_gpu_selector]:- selector.input(- persist_gpu_settings,- inputs=[settings_tab.ai_gpu_selector, settings_tab.video_gpu_selector],- queue=False,- )- settings_tab.preview_encoding_selector.input(- persist_preview_encoding,- inputs=settings_tab.preview_encoding_selector,- queue=False,- )+ outputs = settings_component_outputs(image_tab, video_tab, frame_tab, settings_tab, live_tab, upscale_tab)+ settings_tab.preset_export.click(+ settings_preset_export_status,+ inputs=settings_tab.preset_name,+ outputs=settings_tab.preset_status,+ queue=False,+ )+ settings_tab.preset_import.upload(+ apply_settings_preset,+ inputs=[settings_tab.preset_import, settings_tab.preset_name],+ outputs=[settings_tab.preset_name, *outputs, settings_tab.preset_status],+ queue=False,+ )+ for selector in [settings_tab.ai_gpu_selector, settings_tab.video_gpu_selector]:+ selector.input(+ persist_gpu_settings,+ inputs=[settings_tab.ai_gpu_selector, settings_tab.video_gpu_selector],+ queue=False,+ )+ settings_tab.preview_encoding_selector.input(+ persist_preview_encoding,+ inputs=settings_tab.preview_encoding_selector,+ queue=False,+ )+ settings_tab.full_size_image_previews.input(+ persist_full_size_image_previews,+ inputs=settings_tab.full_size_image_previews,+ queue=False,+ )+ settings_tab.gpu_mode.input(+ persist_nr_gpu_mode,+ inputs=settings_tab.gpu_mode,+ outputs=settings_tab.processing_engine_state,+ queue=False,+ )reset_outputs = [*outputs, image_tab.status, video_tab.status, frame_tab.status, upscale_tab.video.status, upscale_tab.image.status]- image_tab.reset.click(reset_saved_settings, outputs=reset_outputs, queue=False)- video_tab.reset.click(reset_saved_settings, outputs=reset_outputs, queue=False)- frame_tab.reset.click(reset_saved_settings, outputs=reset_outputs, queue=False)- upscale_tab.video.reset.click(reset_saved_settings, outputs=reset_outputs, queue=False)- upscale_tab.image.reset.click(reset_saved_settings, outputs=reset_outputs, queue=False)+ image_tab.reset.click(reset_saved_settings, outputs=reset_outputs, queue=False)+ video_tab.reset.click(reset_saved_settings, outputs=reset_outputs, queue=False)+ frame_tab.reset.click(reset_saved_settings, outputs=reset_outputs, queue=False)+ upscale_tab.video.reset.click(reset_saved_settings, outputs=reset_outputs, queue=False)+ upscale_tab.image.reset.click(reset_saved_settings, outputs=reset_outputs, queue=False)diff --git a/src/upscale/image/batch.py b/src/upscale/image/batch.pyindex 33d4387..9a91303 100644--- a/src/upscale/image/batch.py+++ b/src/upscale/image/batch.py@@ -1,15 +1,12 @@from __future__ import annotations-import json-import time-import uuid-from dataclasses import asdict, replace+from dataclasses import replacefrom pathlib import Path+from ...core import app_logfrom ...core.batch_progress import BatchProgressfrom ...core.disk_paths import prepare_output_dirfrom ...core.jobs import Cancelled, JobController, active_job-from ...core.paths import LOGSfrom ..video.native import probe_capabilitiesfrom .models import ImageUpscaleOptions, ImageUpscaleBatchResult, ImageUpscaleFailurefrom .processor import upscale_image@@ -54,12 +51,11 @@ def upscale_images(input_paths, options=None, progress=None, *, output_dir=None,if item.state == "Queued":failures.append(ImageUpscaleFailure(item.index, item.input_path, "Cancelled before rendering.", True))reporter.skip_from(0)- LOGS.mkdir(exist_ok=True)- manifest = LOGS / f"upscale-image-batch-{time.strftime('%Y%m%d-%H%M%S')}-{uuid.uuid4().hex[:8]}.json"- result = ImageUpscaleBatchResult(successes, failures, controller.cancel.is_set(), str(manifest))- manifest.write_text(json.dumps({**asdict(result), "options": asdict(options),- "progress": reporter.diagnostics(final=True)}, indent=2), encoding="utf-8")- reporter.finish(cancelled=result.cancelled, manifest_path=str(manifest))+ status = "cancelled" if controller.cancel.is_set() else ("partial" if failures else "success")+ app_log.info("upscale-image-batch", f"{status} ok={len(successes)} failed={len(failures)}")+ manifest = app_log.session_path()+ result = ImageUpscaleBatchResult(successes, failures, controller.cancel.is_set(), manifest)+ reporter.finish(cancelled=result.cancelled, manifest_path=manifest)return resultexcept BaseException as exc:reporter.finish(cancelled=controller.cancel.is_set(), error=str(exc))diff --git a/src/upscale/image/models.py b/src/upscale/image/models.pyindex 4e41030..fb0a505 100644--- a/src/upscale/image/models.py+++ b/src/upscale/image/models.py@@ -20,7 +20,7 @@ class ImageUpscaleOptions:quality: int = 95preserve_metadata: bool = Truerename_mode: str = "Auto"- custom_suffix: str = "_RTXIMAGE"+ custom_suffix: str = "_Upscale"ai_gpu_uuid: str = "auto"def validate(self, *, for_render: bool = True):diff --git a/src/upscale/image/processor.py b/src/upscale/image/processor.pyindex 5e5d03d..557b765 100644--- a/src/upscale/image/processor.py+++ b/src/upscale/image/processor.py@@ -1,118 +1,173 @@-from __future__ import annotations--import json-import time-import uuid-from contextlib import nullcontext, suppress-from dataclasses import asdict, replace-from pathlib import Path--import numpy as np-from PIL import Image--from ..video.native import RTXVideoSession, probe_capabilities-from ...core.disk_paths import OutputFile, prepare_output_dir-from ...core.jobs import Cancelled, active_job-from ...core.naming import output_filename-from ...core.paths import LOGS-from ...neural_rendering.image.decoder import decode_image-from ...neural_rendering.image.encoder import _encode_image-from ...neural_rendering.image.models import ImageConversionOptions-from .models import IMAGE_EXTENSIONS, ImageUpscaleOptions, ImageUpscaleResult, output_size---def srgb_to_worker(rgba):- """The video pipeline uses BT.709 primaries with BT.470M (gamma 2.2) RGB."""- rgb = rgba[..., :3].astype(np.float32) / 255- linear = np.where(rgb <= .04045, rgb / 12.92, ((rgb + .055) / 1.055) ** 2.4)- packed = np.empty_like(rgba)- packed[..., :3] = np.rint(np.clip(linear, 0, 1) ** (1 / 2.2) * 255).astype(np.uint8)- packed[..., 3] = 255- return np.ascontiguousarray(packed)---def worker_to_srgb(data, width, height, alpha):- rgba = np.frombuffer(data, dtype=np.uint8).reshape(height, width, 4).copy()- linear = (rgba[..., :3].astype(np.float32) / 255) ** 2.2- rgb = np.where(linear <= .0031308, linear * 12.92, 1.055 * linear ** (1 / 2.4) - .055)- rgba[..., :3] = np.rint(np.clip(rgb, 0, 1) * 255).astype(np.uint8)- with Image.fromarray(alpha) as mask:- with mask.resize((width, height), Image.Resampling.LANCZOS) as scaled:- rgba[..., 3] = np.asarray(scaled)- return rgba---def upscale_image(input_path, options=None, progress=None, *, output_dir=None, controller=None,- generate_previews=True, _owns_slot=False, _capabilities=None):- options = replace(options) if options else ImageUpscaleOptions()- options.validate()- source = Path(input_path).resolve()- if not source.is_file():- raise FileNotFoundError(source)- with nullcontext(controller) if _owns_slot else active_job(controller) as controller:- return _process(source, options, progress, output_dir, controller, generate_previews, _capabilities)---def _process(source, options, progress, output_dir, controller, generate_previews, capabilities):- started = time.monotonic()- stamp = time.strftime("%Y%m%d-%H%M%S") + "-" + uuid.uuid4().hex[:8]- LOGS.mkdir(exist_ok=True)- report_path = LOGS / f"upscale-image-{stamp}.json"- report = {"input": str(source), "options": asdict(options), "pipeline": "RTX VSR still image",- "color": "sRGB → gamma 2.2 RGB → RTX VSR → sRGB"}- destination_file = None- session = None-- def update(value, message):- if controller.cancel.is_set():- raise Cancelled("Image upscale stopped by user.")- if progress:- progress(value, message)-- try:- update(.01, "Decoding image")- decoded = decode_image(source)- height, width = decoded.rgba.shape[:2]- ow, oh = output_size(width, height, options)- caps = capabilities or probe_capabilities(options.ai_gpu_uuid, controller=controller)- output = prepare_output_dir(output_dir) / output_filename(- source, IMAGE_EXTENSIONS[options.output_format], options.rename_mode, options.custom_suffix,- f"{source.stem}_RTXIMAGE_{stamp}")- destination_file = OutputFile(output)- report.update(input_dimensions=[width, height], output_dimensions=[ow, oh],- capabilities=asdict(caps), decoder=decoded.decoder, warnings=decoded.warnings)- update(.15, f"RTX VSR: {width}×{height} → {ow}×{oh}")- with RTXVideoSession(width, height, ow, oh, options.native_options(), 1, caps, controller) as session:- processed = worker_to_srgb(session.process_frame(srgb_to_worker(decoded.rgba)), ow, oh, decoded.alpha)- if session.completed_frames != 1:- raise RuntimeError("RTX VSR did not process exactly one image.")- update(.80, "Saving image")- export = ImageConversionOptions(output_format=options.output_format, quality=int(options.quality),- preserve_metadata=options.preserve_metadata)- warnings = list(decoded.warnings)- warnings.extend(_encode_image(destination_file.temporary, processed, export, decoded.metadata,- generate_preview=generate_previews, preview_path=output, controller=controller))- with Image.open(destination_file.temporary) as saved:- saved.load()- if saved.size != (ow, oh):- raise RuntimeError("Saved image dimensions do not match the VSR output.")- update(.98, "Verifying output")- report.update(status="success", output=str(output), warnings=warnings, sdk_evaluations=1,- last_ngx_results=session.last_results, worker_log=list(session.logs),- elapsed_seconds=time.monotonic() - started)- report_path.write_text(json.dumps(report, indent=2, default=str), encoding="utf-8")- destination_file.publish()- return ImageUpscaleResult(str(source), str(output), str(report_path), ow, oh,- time.monotonic() - started, warnings)- except BaseException as exc:- cancelled = controller.cancel.is_set() or isinstance(exc, Cancelled)- report.update(status="cancelled" if cancelled else "failed", error=str(exc),- worker_log=list(session.logs) if session else [], elapsed_seconds=time.monotonic() - started)- with suppress(OSError):- report_path.write_text(json.dumps(report, indent=2, default=str), encoding="utf-8")- if cancelled and not isinstance(exc, Cancelled):- raise Cancelled("Image upscale stopped by user.") from exc- raise- finally:- if destination_file:- destination_file.cleanup()+from __future__ import annotations++import time+import uuid+from contextlib import nullcontext+from dataclasses import replace+from pathlib import Path++import numpy as np+from PIL import Image++from ..video.native import RTXVideoSession, probe_capabilities+from ...core import app_log+from ...core.disk_paths import OutputFile, prepare_output_dir+from ...core.jobs import Cancelled, active_job+from ...core.naming import output_filename+from ...neural_rendering.image.decoder import decode_image+from ...neural_rendering.image.encoder import (+ _encode_image, make_image_preview, save_full_size_image_preview,+)+from ...neural_rendering.image.models import ImageConversionOptions+from .models import IMAGE_EXTENSIONS, ImageUpscaleOptions, ImageUpscaleResult, output_size+++def srgb_to_worker(rgba):+ """The video pipeline uses BT.709 primaries with BT.470M (gamma 2.2) RGB."""+ rgb = rgba[..., :3].astype(np.float32) / 255+ linear = np.where(rgb <= .04045, rgb / 12.92, ((rgb + .055) / 1.055) ** 2.4)+ packed = np.empty_like(rgba)+ packed[..., :3] = np.rint(np.clip(linear, 0, 1) ** (1 / 2.2) * 255).astype(np.uint8)+ packed[..., 3] = 255+ return np.ascontiguousarray(packed)+++def worker_to_srgb(data, width, height, alpha):+ rgba = np.frombuffer(data, dtype=np.uint8).reshape(height, width, 4).copy()+ linear = (rgba[..., :3].astype(np.float32) / 255) ** 2.2+ rgb = np.where(linear <= .0031308, linear * 12.92, 1.055 * linear ** (1 / 2.4) - .055)+ rgba[..., :3] = np.rint(np.clip(rgb, 0, 1) * 255).astype(np.uint8)+ if alpha is None:+ rgba[..., 3] = 255+ else:+ with Image.fromarray(alpha) as mask:+ with mask.resize((width, height), Image.Resampling.LANCZOS) as scaled:+ rgba[..., 3] = np.asarray(scaled)+ return rgba+++def preview_upscale_image(input_path, options=None, progress=None, *, controller=None, full_size_preview=False):+ """Run the real RTX VSR still-image path without publishing output/report files."""+ options = replace(options) if options else ImageUpscaleOptions()+ options.validate()+ source = Path(input_path).resolve()+ if not source.is_file():+ raise FileNotFoundError(source)+ started = time.monotonic()++ with active_job(controller) as controller:+ def update(value, message):+ if controller.cancel.is_set():+ raise Cancelled("Image upscale preview stopped by user.")+ if progress:+ progress(value, message)++ update(.01, "Decoding image")+ decoded = decode_image(source)+ height, width = decoded.rgba.shape[:2]+ ow, oh = output_size(width, height, options)++ update(.12, "Checking RTX Video capabilities")+ caps = probe_capabilities(options.ai_gpu_uuid, controller=controller)+ update(.24, f"RTX VSR: {width}×{height} → {ow}×{oh}")+ with RTXVideoSession(+ width, height, ow, oh, options.native_options(), 1, caps, controller+ ) as session:+ processed = worker_to_srgb(+ session.process_frame(srgb_to_worker(decoded.rgba)), ow, oh, decoded.alpha+ )+ if session.completed_frames != 1:+ raise RuntimeError("RTX VSR did not process exactly one image.")+ last_results = tuple(session.last_results)++ update(.88, "Preparing preview")+ if full_size_preview:+ preview = save_full_size_image_preview(+ processed, options.output_format, decoded.alpha is not None+ )+ else:+ preview = make_image_preview(+ processed, options.output_format, decoded.alpha is not None+ )+ elapsed = time.monotonic() - started+ gpu_name = str(caps.gpu.get("name") or caps.gpu.get("display_name") or "NVIDIA GPU")+ status = (+ f"Preview complete: {source.name} | {width}×{height} → {ow}×{oh} | "+ f"RTX VSR quality {int(options.vsr_quality)} | GPU: {gpu_name} | "+ f"SDK result VSR=0x{int(last_results[0]):08X} | {elapsed:.2f}s.\n"+ "Preview only — no production output image was saved."+ )+ update(1.0, "Preview ready")+ return preview, status+++def upscale_image(input_path, options=None, progress=None, *, output_dir=None, controller=None,+ generate_previews=True, _owns_slot=False, _capabilities=None):+ options = replace(options) if options else ImageUpscaleOptions()+ options.validate()+ source = Path(input_path).resolve()+ if not source.is_file():+ raise FileNotFoundError(source)+ with nullcontext(controller) if _owns_slot else active_job(controller) as controller:+ return _process(source, options, progress, output_dir, controller, generate_previews, _capabilities)+++def _process(source, options, progress, output_dir, controller, generate_previews, capabilities):+ started = time.monotonic()+ stamp = time.strftime("%Y%m%d-%H%M%S") + "-" + uuid.uuid4().hex[:8]+ report_path = app_log.session_path()+ app_log.info("upscale-image", f"start src={source.name}")+ destination_file = None+ session = None++ def update(value, message):+ if controller.cancel.is_set():+ raise Cancelled("Image upscale stopped by user.")+ if progress:+ progress(value, message)++ try:+ update(.01, "Decoding image")+ decoded = decode_image(source)+ height, width = decoded.rgba.shape[:2]+ ow, oh = output_size(width, height, options)+ caps = capabilities or probe_capabilities(options.ai_gpu_uuid, controller=controller)+ output = prepare_output_dir(output_dir) / output_filename(+ source, IMAGE_EXTENSIONS[options.output_format], options.rename_mode, options.custom_suffix,+ f"{source.stem}_RTXIMAGE_{stamp}")+ destination_file = OutputFile(output)+ update(.15, f"RTX VSR: {width}×{height} → {ow}×{oh}")+ with RTXVideoSession(width, height, ow, oh, options.native_options(), 1, caps, controller) as session:+ processed = worker_to_srgb(session.process_frame(srgb_to_worker(decoded.rgba)), ow, oh, decoded.alpha)+ if session.completed_frames != 1:+ raise RuntimeError("RTX VSR did not process exactly one image.")+ update(.80, "Saving image")+ export = ImageConversionOptions(output_format=options.output_format, quality=int(options.quality),+ preserve_metadata=options.preserve_metadata)+ warnings = list(decoded.warnings)+ warnings.extend(_encode_image(destination_file.temporary, processed, export, decoded.metadata,+ generate_preview=generate_previews, preview_path=output, controller=controller,+ has_transparency=decoded.alpha is not None))+ with Image.open(destination_file.temporary) as saved:+ saved.load()+ if saved.size != (ow, oh):+ raise RuntimeError("Saved image dimensions do not match the VSR output.")+ update(.98, "Verifying output")+ elapsed = time.monotonic() - started+ app_log.info("upscale-image", f"done src={source.name} out={output.name} elapsed={elapsed:.1f}s")+ destination_file.publish()+ return ImageUpscaleResult(str(source), str(output), report_path, ow, oh,+ time.monotonic() - started, warnings)+ except BaseException as exc:+ cancelled = controller.cancel.is_set() or isinstance(exc, Cancelled)+ if not cancelled:+ tails = {"worker": list(session.logs)[-40:]} if session else None+ report_path = str(app_log.fail("upscale-image", f"upscale-image-{source.stem}", exc, tails))+ else:+ app_log.info("upscale-image", f"cancelled src={source.name}")+ if cancelled and not isinstance(exc, Cancelled):+ raise Cancelled("Image upscale stopped by user.") from exc+ raise+ finally:+ if destination_file:+ destination_file.cleanup()diff --git a/src/upscale/image/ui.py b/src/upscale/image/ui.pyindex 8be6088..a3bf33c 100644--- a/src/upscale/image/ui.py+++ b/src/upscale/image/ui.py@@ -4,17 +4,19 @@ from pathlib import Pathfrom types import SimpleNamespaceimport gradio as gr+from PIL import Imagefrom ...core.batch_ui import (BATCH_HEADERS, bind_batch_ui, build_media_clear_button,- build_media_select_button, build_path_controls)-from ...core.disk_paths import create_media_archive, resolve_inputs+ build_media_select_button, build_path_controls, build_save_controls)+from ...core.disk_paths import resolve_inputsfrom ...core.naming import RENAME_MODES-from ...neural_rendering.image.decoder import decode_image+from ...neural_rendering.image.decoder import decode_image, full_size_image_preview_pathfrom ...neural_rendering.image.encoder import take_image_previewfrom ...neural_rendering.image.models import RAW_EXTENSIONSfrom ...neural_rendering.image.ui import preview_input_images-from ...settings.storage import processing_gpu_settings+from ...settings.storage import full_size_image_previews_enabled, processing_gpu_settingsfrom .batch import upscale_images+from .processor import preview_upscale_imagefrom .models import (IMAGE_FORMATS, SCALE_FACTORS, SETTING_FIELDS, SIZE_MODES, VSR_QUALITIES,ImageUpscaleOptions, options_from_settings, output_size)@@ -27,23 +29,50 @@ def options_from_values(values):def render_image_batch(paths, *values, progress=None, output_dir=None, controller=None,on_item_update=None, direct_disk=False):- result = upscale_images(paths, options_from_values(values), progress, output_dir=output_dir,- controller=controller, on_item_update=on_item_update, generate_previews=not direct_disk)+ full_size = full_size_image_previews_enabled()+ result = upscale_images(+ paths, options_from_values(values), progress, output_dir=output_dir,+ controller=controller, on_item_update=on_item_update,+ generate_previews=not direct_disk and not full_size,+ )gallery = []if not direct_disk:for item in result.successes:- preview = take_image_preview(item.output_path)- if preview is not None:- gallery.append((preview, Path(item.output_path).name))- archive_path = None- if not direct_disk and not result.cancelled:- archive_path = create_media_archive(- (item.output_path for item in result.successes),- output_dir,- "RTXVIDEO_IMAGE_BATCH",- controller=controller,- )- return gallery, archive_path, [], str(result.manifest_path)+ preview = None+ if full_size:+ try:+ preview = full_size_image_preview_path(item.output_path)+ except Exception:+ preview = None+ else:+ preview = take_image_preview(item.output_path)+ if preview is None:+ try:+ with Image.open(item.output_path) as output:+ preview = output.convert("RGBA")+ preview.thumbnail((1200, 900), Image.Resampling.BILINEAR)+ preview = preview.copy()+ except Exception:+ continue+ gallery.append((preview, Path(item.output_path).name))+ files = [item.output_path for item in result.successes]+ return gallery, files, [], str(result.manifest_path)+++def preview_image(paths, *values, progress=gr.Progress(track_tqdm=False)):+ selected = [paths] if isinstance(paths, str) else list(paths or [])+ if len(selected) != 1:+ raise gr.Error("Choose exactly one image to preview.")+ full_size = full_size_image_previews_enabled()+ image, status = preview_upscale_image(+ selected[0], options_from_values(values),+ progress=lambda value, message: progress(value, desc=message),+ full_size_preview=full_size,+ )+ return (+ gr.update(value=[(image, f"Preview — {Path(selected[0]).name}")], visible=True),+ status,+ )def describe_size(paths, input_path, *values):@@ -77,7 +106,7 @@ def build_image_tab(settings):elem_classes=["media-upload-surface"])input_gallery = gr.Gallery(label="Input images", columns=3, height=520, object_fit="contain",- visible=False, interactive=False, type="pil", buttons=["fullscreen"],+ visible="hidden", interactive=False, type="pil", buttons=["fullscreen"],elem_id="upscale-image-input-preview",)with gr.Row(visible=False, elem_classes=["media-input-actions"]) as input_actions:@@ -85,8 +114,12 @@ def build_image_tab(settings):"Choose Images", upload_types, "upscale-image-select-input",)clear_source = build_media_clear_button("upscale-image-clear-input")- input_path, output_path = build_path_controls()- with gr.Accordion("RTX Video Super Resolution", open=True, elem_id="upscale-image-vsr-box"):+ with gr.Row():+ render = gr.Button("Upscale image(s)", variant="primary")+ stop = gr.Button("Stop", variant="stop")+ preview = gr.Button("Preview", visible=False)+ reset = gr.Button("Reset settings")+ with gr.Column(elem_id="upscale-image-vsr-box"):c["vsr_quality"] = gr.Dropdown(VSR_QUALITIES, value=opts.vsr_quality, label="VSR quality")c["size_mode"] = gr.Radio(SIZE_MODES, value=opts.size_mode, label="Output sizing")c["scale_factor"] = gr.Dropdown(@@ -98,18 +131,15 @@ def build_image_tab(settings):) as custom_dimensions_row:c["width"] = gr.Number(value=opts.width, minimum=1, maximum=16384, precision=0, label="Output width")c["height"] = gr.Number(value=opts.height, minimum=1, maximum=16384, precision=0, label="Output height")- c["aspect_lock"] = gr.Checkbox(value=opts.aspect_lock, label="Lock aspect ratio", info="Custom width determines height for each image.")+ c["aspect_lock"] = gr.Checkbox(value=opts.aspect_lock, label="Lock aspect ratio")dimensions = gr.Markdown(visible=False, elem_id="upscale-image-dimensions")+ input_path, output_path = build_path_controls()c["output_format"] = gr.Dropdown(IMAGE_FORMATS, value=opts.output_format, label="Output format")- c["quality"] = gr.Slider(1, 100, value=opts.quality, step=1, precision=0, label="Image quality", info="Used for JPEG, WebP, and AVIF.")+ c["quality"] = gr.Slider(1, 100, value=opts.quality, step=1, precision=0, label="Image quality")c["preserve_metadata"] = gr.Checkbox(value=opts.preserve_metadata, label="Preserve metadata")with gr.Row():c["rename_mode"] = gr.Radio(RENAME_MODES, value=opts.rename_mode, label="Rename")c["custom_suffix"] = gr.Textbox(value=opts.custom_suffix, label="Custom suffix", interactive=opts.rename_mode == "Custom")- with gr.Row():- render = gr.Button("Upscale image(s)", variant="primary")- stop = gr.Button("Stop", variant="stop")- reset = gr.Button("Reset settings")with gr.Column(scale=3):output_gallery = gr.Gallery(label="Upscaled images", columns=2, height=520, object_fit="contain",@@ -117,17 +147,22 @@ def build_image_tab(settings):buttons=["download", "download_all", "fullscreen"],elem_id="upscale-image-output-preview",)- zip_download = gr.DownloadButton("Save as ZIP", visible=False)+ save_download, zip_button, zip_download = build_save_controls("image", "upscale-image")status = gr.Textbox(label="Status", interactive=False, lines=5)results = gr.Dataframe(headers=BATCH_HEADERS, datatype=["str"] * len(BATCH_HEADERS), interactive=False,label="Batch results", wrap=True)tab = SimpleNamespace(sources=sources, input_gallery=input_gallery, input_actions=input_actions,select_source=select_source, clear_source=clear_source, input_path=input_path,- output_path=output_path, render=render, stop=stop, reset=reset, output_gallery=output_gallery,- zip_download=zip_download, status=status, results=results,+ output_path=output_path, render=render, stop=stop, preview=preview, reset=reset, output_gallery=output_gallery,+ save_download=save_download, zip_button=zip_button, zip_download=zip_download, status=status, results=results,controls=c, settings_inputs=[c[n] for n in SETTING_FIELDS])tab.render_inputs = [sources, *tab.settings_inputs]- bind_batch_ui(tab, render_image_batch, kind="image", preview_mode=preview_input_images)+ tab.preview_inputs = list(tab.render_inputs)+ bind_batch_ui(+ tab, render_image_batch, kind="image", preview_mode=preview_input_images,+ archive_prefix="RTXVIDEO_IMAGE_BATCH",+ preview_actions=[(tab.preview, preview_image)],+ )c["rename_mode"].change(lambda mode: gr.update(interactive=mode == "Custom"), inputs=c["rename_mode"], outputs=c["custom_suffix"], queue=False)def sizing_controls(mode, lock):diff --git a/src/upscale/ui.py b/src/upscale/ui.pyindex 0d272c1..1420efb 100644--- a/src/upscale/ui.py+++ b/src/upscale/ui.py@@ -2,13 +2,34 @@ from dataclasses import dataclassimport gradio as gr+from ..core.batch_ui import bind_input_surface_reactivationfrom ..settings.models import UPSCALE_MODE_CHOICESfrom .image.ui import build_image_tabfrom .video.ui import build_upscale_tab as build_video_tab+def _stable_visible(show: bool):+ return True if show else "hidden"++def mode_visibility(mode):- return gr.update(visible=mode == "Image"), gr.update(visible=mode == "Video")+ return (+ gr.update(visible=_stable_visible(mode == "Image")),+ gr.update(visible=_stable_visible(mode == "Video")),+ )+++_MODE_GUARD_JS = """(mode) => {+ document.documentElement.dataset.upscaleMode = mode;+ const hiddenPanel = document.getElementById(+ mode === 'Video' ? 'upscale-image' : 'upscale-video'+ );+ if (hiddenPanel) {+ hiddenPanel.querySelectorAll('video').forEach((node) => {+ try { node.pause(); } catch (_) {}+ });+ }+}"""@dataclass@@ -23,11 +44,34 @@ class UpscaleTab:def build_upscale_tab(settings):selected_mode = settings.upscale_modemode = gr.Radio(- list(UPSCALE_MODE_CHOICES), value=selected_mode, label="Mode", elem_id="upscale-mode"+ list(UPSCALE_MODE_CHOICES),+ value=selected_mode,+ label=None,+ show_label=False,+ container=False,+ elem_id="upscale-mode",)- with gr.Column(visible=selected_mode == "Image", elem_id="upscale-image") as image_panel:+ with gr.Column(+ visible=_stable_visible(selected_mode == "Image"),+ elem_id="upscale-image",+ elem_classes=["workflow-mode-panel"],+ ) as image_panel:image = build_image_tab(settings)- with gr.Column(visible=selected_mode == "Video", elem_id="upscale-video") as video_panel:+ with gr.Column(+ visible=_stable_visible(selected_mode == "Video"),+ elem_id="upscale-video",+ elem_classes=["workflow-mode-panel"],+ ) as video_panel:video = build_video_tab(settings)- mode.change(mode_visibility, inputs=mode, outputs=[image_panel, video_panel], queue=False, show_progress="hidden")++ transition = mode.change(+ None, inputs=mode, js=_MODE_GUARD_JS, queue=False, show_progress="hidden",+ trigger_mode="always_last",+ )+ visibility = transition.then(+ mode_visibility, inputs=mode, outputs=[image_panel, video_panel],+ queue=False, show_progress="hidden", trigger_mode="always_last",+ )+ bind_input_surface_reactivation(visibility, image, kind="image")+ bind_input_surface_reactivation(visibility, video, kind="video")return UpscaleTab(mode, image, video, image_panel, video_panel)diff --git a/src/upscale/video/batch.py b/src/upscale/video/batch.pyindex 588f274..c8ed17e 100644--- a/src/upscale/video/batch.py+++ b/src/upscale/video/batch.py@@ -1,15 +1,12 @@from __future__ import annotations-import json-import time-import uuid-from dataclasses import asdict, replace+from dataclasses import replacefrom pathlib import Path+from ...core import app_logfrom ...core.batch_progress import BatchProgressfrom ...core.disk_paths import prepare_output_dirfrom ...core.jobs import Cancelled, JobController, active_job-from ...core.paths import LOGSfrom .models import UpscaleOptions, UpscaleBatchResult, UpscaleSuccess, UpscaleFailurefrom .native import probe_capabilitiesfrom .processor import upscale_video@@ -51,12 +48,11 @@ def upscale_videos(input_paths, options=None, progress=None, *, output_dir=None,if item.state == "Queued":failures.append(UpscaleFailure(item.index, item.input_path, "Cancelled before rendering.", True))reporter.skip_from(0)- LOGS.mkdir(exist_ok=True)- manifest = LOGS / f"upscale-batch-{time.strftime('%Y%m%d-%H%M%S')}-{uuid.uuid4().hex[:8]}.json"- result = UpscaleBatchResult(successes, failures, controller.cancel.is_set(), str(manifest))- manifest.write_text(json.dumps({**asdict(result), "options": asdict(options),- "progress": reporter.diagnostics(final=True)}, indent=2), encoding="utf-8")- reporter.finish(cancelled=result.cancelled, manifest_path=str(manifest))+ status = "cancelled" if controller.cancel.is_set() else ("partial" if failures else "success")+ app_log.info("upscale-batch", f"{status} ok={len(successes)} failed={len(failures)}")+ manifest = app_log.session_path()+ result = UpscaleBatchResult(successes, failures, controller.cancel.is_set(), manifest)+ reporter.finish(cancelled=result.cancelled, manifest_path=manifest)return resultexcept BaseException as exc:reporter.finish(cancelled=controller.cancel.is_set(), error=str(exc))diff --git a/src/upscale/video/media.py b/src/upscale/video/media.pyindex c0414ca..27322c9 100644--- a/src/upscale/video/media.py+++ b/src/upscale/video/media.py@@ -10,6 +10,7 @@ from pathlib import Pathimport avimport numpy as np+from ...core import app_logfrom ...core.ffmpeg.probe import _run_jsonfrom ...core.jobs import BoundedLogBuffer, Cancelled, drain_bounded_textfrom ...core.paths import FFMPEG, FFPROBE@@ -191,6 +192,7 @@ def finish_process(process, thread, logs, controller, *, expected_stop=False):raise Cancelled("Upscale stopped by user.")if process.returncode and not expected_stop:detail = logs.snapshot() if hasattr(logs, "snapshot") else logs[-60:]+ app_log.error("ffmpeg", "video subprocess failed", "\n".join(detail)[-500:])raise RuntimeError("Video subprocess failed:\n" + "\n".join(detail))finally:controller.unregister(process)diff --git a/src/upscale/video/models.py b/src/upscale/video/models.pyindex 81c5be4..eb84e88 100644--- a/src/upscale/video/models.py+++ b/src/upscale/video/models.py@@ -34,7 +34,7 @@ class UpscaleOptions:container: str = "MP4"quality: str = "Auto (Default)"rename_mode: str = "Auto"- custom_suffix: str = "_RTXVIDEO"+ custom_suffix: str = "_Upscale"ai_gpu_uuid: str = "auto"video_gpu_uuid: str = "auto"preview_seconds: float | None = Nonediff --git a/src/upscale/video/native.py b/src/upscale/video/native.pyindex 2cedeb3..69ce5d6 100644--- a/src/upscale/video/native.py+++ b/src/upscale/video/native.py@@ -10,6 +10,7 @@ import timefrom contextlib import nullcontext, suppressfrom functools import lru_cache+from ...core import app_logfrom ...core.gpu_detection import detect_gpusfrom ...core.gpu_selection import resolve_ai_gpufrom ...core.jobs import Cancelled, JobController, current_job_controller, use_job_controller@@ -147,6 +148,7 @@ class RTXVideoSession:raise Cancelled("Upscale stopped by user.")self._reader.join(timeout=.2)detail = "timed out" if self.timed_out else "closed unexpectedly"+ app_log.error("rtx-worker", f"RTX Video worker {detail}", "\n".join(self.logs)[-500:])raise RuntimeError(f"RTX Video worker {detail}.\n" + "\n".join(self.logs)[-4000:])def _write(self, data):@@ -185,6 +187,7 @@ class RTXVideoSession:if (magic, index) != (FRAME, self.completed_frames):raise RuntimeError("RTX Video frame identity mismatch.")if status:+ app_log.error("rtx-worker", f"RTX Video evaluation failed: VSR=0x{vsr:08X}, HDR=0x{hdr:08X}")raise RuntimeError(f"RTX Video evaluation failed: VSR=0x{vsr:08X}, HDR=0x{hdr:08X}.")if size != self.output_bytes:raise RuntimeError("RTX Video output frame byte count mismatch.")@@ -216,6 +219,7 @@ class RTXVideoSession:for stream in (self.process.stdout, self.process.stderr):stream.close()if not abort and self.process.returncode:+ app_log.error("rtx-worker", "RTX Video worker failed during shutdown", "\n".join(self.logs)[-500:])raise RuntimeError("RTX Video worker failed during shutdown.\n" + "\n".join(self.logs)[-3000:])def __enter__(self):diff --git a/src/upscale/video/preview.py b/src/upscale/video/preview.pyindex 89a6b3f..f2e53d2 100644--- a/src/upscale/video/preview.py+++ b/src/upscale/video/preview.py@@ -49,5 +49,5 @@ def preview_mode(paths):available = bool(paths)single = len(paths) == 1label = "Input video preview" if len(paths) <= 1 else f"Input video preview (first of {len(paths)})"- return (gr.update(value=paths[0] if available else None, visible=available, label=label),+ return (gr.update(value=paths[0] if available else None, visible=True if available else "hidden", label=label),gr.update(value=None, visible=True), gr.update(visible=single), gr.update(visible=single))diff --git a/src/upscale/video/processor.py b/src/upscale/video/processor.pyindex 95a5e07..920da62 100644--- a/src/upscale/video/processor.py+++ b/src/upscale/video/processor.py@@ -1,23 +1,22 @@from __future__ import annotations-import jsonimport mathimport tempfileimport timeimport uuidfrom contextlib import ExitStack, nullcontext, suppress-from dataclasses import asdict, replace+from dataclasses import replacefrom fractions import Fractionfrom pathlib import Pathimport av-from ...core import ffmpeg+from ...core import app_log, ffmpegfrom ...core.disk_paths import OutputFile, prepare_output_dirfrom ...core.gpu_detection import detect_gpusfrom ...core.jobs import Cancelled, active_jobfrom ...core.naming import output_filename-from ...core.paths import JOBS, LOGS+from ...core.paths import JOBSfrom .media import PipeReader, PipeWriter, close_process, finish_process, inspect_video, output_filter, packed_bytes, result_frame, start_decoderfrom .models import UpscaleOptions, UpscaleResult, output_sizefrom .native import RTXVideoSession, probe_capabilities@@ -38,9 +37,9 @@ def upscale_video(input_path, options: UpscaleOptions | None = None, progress=Nodef _process(source, options, progress, output_dir, controller, capabilities):started = time.monotonic()stamp = time.strftime("%Y%m%d-%H%M%S") + "-" + uuid.uuid4().hex[:8]- LOGS.mkdir(exist_ok=True); JOBS.mkdir(exist_ok=True)- report_path = LOGS / f"upscale-{stamp}.json"- report = {"input": str(source), "options": asdict(options), "status": "running", "pipeline": "RTX Video SDK / D3D11"}+ JOBS.mkdir(exist_ok=True)+ report_path = app_log.session_path()+ app_log.info("upscale", f"start src={source.name}")processes = []destination_file = Nonesession = None@@ -59,8 +58,6 @@ def _process(source, options, progress, output_dir, controller, capabilities):ow, oh, rounding = output_size(width, height, options)caps = capabilities or probe_capabilities(options.ai_gpu_uuid, controller=controller)video_gpu = ffmpeg.resolve_video_gpu(detect_gpus(), options.video_gpu_uuid, options.codec, ow, oh)- report.update(capabilities=asdict(caps), video_gpu=video_gpu, input_metadata=meta,- output_dimensions=[ow, oh], rounding=rounding)destination = prepare_output_dir(output_dir)extension = {"MP4": ".mp4", "MKV": ".mkv", "MOV": ".mov"}[options.container]preview = options.preview_frames is not None or options.preview_seconds is not None@@ -89,16 +86,6 @@ def _process(source, options, progress, output_dir, controller, capabilities):decoder, dec_thread, dec_logs, assumptions = start_decoder(source, meta, controller)processes.append(decoder)stack.callback(close_process, decoder, controller, dec_thread)- report.update(encoder=selected, encoding_quality=quality, warnings=assumptions,- input_format=input_format, output_format=session.output_format, color=colors,- applied_settings={"vsr_enabled": options.vsr_enabled,- "vsr_quality": options.vsr_quality if options.vsr_enabled else None,- "hdr_enabled": options.hdr_enabled,- "hdr": {name: getattr(options, name) for name in- ("hdr_contrast", "hdr_saturation", "hdr_middle_gray",- "hdr_peak_luminance", "hdr_precision")} if options.hdr_enabled else None,- "input_dimensions": [width, height], "output_dimensions": [ow, oh],- "input_color": "Full-range BT.709 RGB, gamma 2.2", "full_frame": True})decoded = stack.enter_context(av.open(PipeReader(decoder.stdout), mode="r", format="nut"))source_stream = decoded.streams.video[0]tb = source_stream.time_base@@ -179,20 +166,27 @@ def _process(source, options, progress, output_dir, controller, capabilities):if not options.hdr_enabled and saved["hdr"]:raise RuntimeError("SDR output unexpectedly contains HDR signaling.")check_cancel()- report.update(status="success", output=str(output), frames=delivered, verified_output=saved,- sdk_evaluations=delivered, last_ngx_results=session.last_results,- timestamp_fallback_frames=timestamps_fallback,- timings={"total_seconds": time.monotonic()-started, "ngx_evaluation_seconds": eval_seconds},- worker_log=list(session.logs), decoder_log=dec_logs.snapshot(), encoder_log=enc_logs.snapshot())- report_path.write_text(json.dumps(report, indent=2, default=str), encoding="utf-8")+ elapsed = time.monotonic() - started+ app_log.info(+ "upscale",+ f"done src={source.name} out={output.name} frames={delivered} "+ f"elapsed={elapsed:.0f}s",+ )destination_file.publish()- return UpscaleResult(str(output), str(report_path), delivered, ow, oh, options.hdr_enabled, time.monotonic()-started)+ return UpscaleResult(str(output), report_path, delivered, ow, oh, options.hdr_enabled, time.monotonic()-started)except BaseException as exc:- report.update(status="cancelled" if controller.cancel.is_set() or isinstance(exc, Cancelled) else "failed",- error=str(exc), frames=delivered, elapsed_seconds=time.monotonic()-started,- worker_log=list(session.logs) if session else [])- with suppress(OSError):- report_path.write_text(json.dumps(report, indent=2, default=str), encoding="utf-8")+ cancelled = controller.cancel.is_set() or isinstance(exc, Cancelled)+ if not cancelled:+ tails: dict[str, object] = {}+ if session is not None:+ tails["worker"] = list(session.logs)[-40:]+ with suppress(Exception):+ tails["decoder"] = dec_logs.snapshot()[-40:]+ with suppress(Exception):+ tails["encoder"] = enc_logs.snapshot()[-40:]+ report_path = str(app_log.fail("upscale", f"upscale-{source.stem}", exc, tails))+ else:+ app_log.info("upscale", f"cancelled src={source.name}")if controller.cancel.is_set() and not isinstance(exc, Cancelled):raise Cancelled("Upscale stopped by user.") from excraisediff --git a/src/upscale/video/ui.py b/src/upscale/video/ui.pyindex 1abdc02..3c1192e 100644--- a/src/upscale/video/ui.py+++ b/src/upscale/video/ui.py@@ -1,134 +1,135 @@-from __future__ import annotations--from dataclasses import dataclass-from pathlib import Path--import gradio as gr--from ...core.batch_ui import (- BATCH_HEADERS, bind_batch_ui, build_media_clear_button, build_media_select_button,- build_path_controls,-)-from ...core.disk_paths import create_media_archive, resolve_inputs-from ...core.ffmpeg import CODEC_CHOICES, ENCODING_QUALITIES, hdr_mode_supported-from ...core.naming import RENAME_MODES-from ...settings.storage import processing_gpu_settings-from .batch import upscale_videos-from .components import ManagedVideo+from __future__ import annotations++from dataclasses import dataclass+from pathlib import Path++import gradio as gr++from ...core.batch_ui import (+ BATCH_HEADERS, bind_batch_ui, build_media_clear_button, build_media_select_button,+ build_path_controls, build_save_controls,+)+from ...core.disk_paths import resolve_inputs+from ...core.ffmpeg import CODEC_CHOICES, ENCODING_QUALITIES, hdr_mode_supported+from ...core.naming import RENAME_MODES+from ...settings.storage import processing_gpu_settings+from .batch import upscale_videos+from .components import ManagedVideofrom .media import inspect_videofrom .models import (HDR_PRECISION_CHOICES, SCALE_FACTORS, SETTING_FIELDS, SIZE_MODES,VSR_QUALITIES, UpscaleOptions, options_from_settings, output_size)from .preview import display_result, preview_mode, preview_upscale---def options_from_values(values):- ai, video = processing_gpu_settings()- return UpscaleOptions(**dict(zip(SETTING_FIELDS, values)), ai_gpu_uuid=ai, video_gpu_uuid=video)---def render_upscale_batch(paths, *values, progress=None, output_dir=None, controller=None, on_item_update=None, direct_disk=False):- options = options_from_values(values)- result = upscale_videos(paths, options, progress, output_dir=output_dir, controller=controller, on_item_update=on_item_update)- files = [item.result.output_path for item in result.successes]- output, detail = None, ""- if not direct_disk and len(paths) == 1 and result.successes and not result.cancelled:- output, detail = display_result(result.successes[0].result, options, controller)- status = f"{'Cancelled' if result.cancelled else 'Complete'}: {len(files)} completed; {len(result.failures)} failed/skipped.\n{detail}\nReport: {result.manifest_path}"+++def options_from_values(values):+ ai, video = processing_gpu_settings()+ return UpscaleOptions(**dict(zip(SETTING_FIELDS, values)), ai_gpu_uuid=ai, video_gpu_uuid=video)+++def render_upscale_batch(paths, *values, progress=None, output_dir=None, controller=None, on_item_update=None, direct_disk=False):+ options = options_from_values(values)+ result = upscale_videos(paths, options, progress, output_dir=output_dir, controller=controller, on_item_update=on_item_update)+ files = [item.result.output_path for item in result.successes]+ output, detail = None, ""+ if not direct_disk and len(paths) == 1 and result.successes and not result.cancelled:+ output, detail = display_result(result.successes[0].result, options, controller)+ status = f"{'Cancelled' if result.cancelled else 'Complete'}: {len(files)} completed; {len(result.failures)} failed/skipped.\n{detail}\nReport: {result.manifest_path}"if result.failures:status += "\n" + result.failures[0].error- archive_path = None- if not direct_disk and not result.cancelled:- archive_path = create_media_archive(- files, output_dir, "RTXVIDEO_VIDEO_BATCH", controller=controller,- )- return gr.update(value=output, visible=not direct_disk, label="SDR tone-mapped preview (download original for HDR)" if options.hdr_enabled and detail.startswith("SDR") else "Output video"), archive_path, [], status---def preview_frame(paths, *values, progress=gr.Progress(track_tqdm=False)):- return preview_upscale(paths, options_from_values(values), one_frame=True, progress=lambda v,m: progress(v, desc=m))---def preview_clip(paths, *values, progress=gr.Progress(track_tqdm=False)):- return preview_upscale(paths, options_from_values(values), progress=lambda v,m: progress(v, desc=m))--+ return gr.update(value=output, visible=not direct_disk, label="SDR tone-mapped preview (download original for HDR)" if options.hdr_enabled and detail.startswith("SDR") else "Output video"), files, [], status+++def preview_frame(paths, *values, progress=gr.Progress(track_tqdm=False)):+ return preview_upscale(paths, options_from_values(values), one_frame=True, progress=lambda v,m: progress(v, desc=m))+++def preview_clip(paths, *values, progress=gr.Progress(track_tqdm=False)):+ return preview_upscale(paths, options_from_values(values), progress=lambda v,m: progress(v, desc=m))++def describe_size(paths, input_path, *values):if not paths and not str(input_path or "").strip():return ""- try:- options = options_from_values(values)- sources = resolve_inputs(input_path, paths, "video")- lines = []- for path in sources[:8]:- m = inspect_video(Path(path), reject_hdr=True)- w, h, note = output_size(m["width"], m["height"], options)- lines.append(f"**{Path(path).name}**: {m['source_width']}×{m['source_height']} → **{w}×{h}**{note}")- if len(sources) > 8:- lines.append(f"{len(sources)-8} more files; dimensions are calculated independently for each source.")- return "\n\n".join(lines)- except Exception as exc:- return str(exc)--+ try:+ options = options_from_values(values)+ sources = resolve_inputs(input_path, paths, "video")+ lines = []+ for path in sources[:8]:+ m = inspect_video(Path(path), reject_hdr=True)+ w, h, note = output_size(m["width"], m["height"], options)+ lines.append(f"**{Path(path).name}**: {m['source_width']}×{m['source_height']} → **{w}×{h}**{note}")+ if len(sources) > 8:+ lines.append(f"{len(sources)-8} more files; dimensions are calculated independently for each source.")+ return "\n\n".join(lines)+ except Exception as exc:+ return str(exc)++@dataclassclass UpscaleTab:- sources: object- input_preview: object- input_actions: object- select_source: object- clear_source: object- controls: dict- preview_frame: object- preview: object- render: object- stop: object+ sources: object+ input_preview: object+ input_actions: object+ select_source: object+ clear_source: object+ controls: dict+ preview_frame: object+ preview: object+ render: object+ stop: objectreset: objectoutput_video: object+ save_download: object+ zip_button: objectzip_download: objectstatus: objectresults: objectinput_path: objectoutput_path: objectjob_state: object = None-- @property- def settings_inputs(self):- return [self.controls[n] for n in SETTING_FIELDS]-- @property- def render_inputs(self):- return [self.sources, *self.settings_inputs]-- @property- def preview_inputs(self):- return self.render_inputs---def build_upscale_tab(settings):- opts = options_from_settings(settings)- c = {}- with gr.Row():- with gr.Column(scale=3):++ @property+ def settings_inputs(self):+ return [self.controls[n] for n in SETTING_FIELDS]++ @property+ def render_inputs(self):+ return [self.sources, *self.settings_inputs]++ @property+ def preview_inputs(self):+ return self.render_inputs+++def build_upscale_tab(settings):+ opts = options_from_settings(settings)+ c = {}+ with gr.Row():+ with gr.Column(scale=3):sources = gr.File(label="Input video(s)", file_count="multiple", file_types=["video"], type="filepath",allow_reordering=True, elem_id="upscale-upload-list",elem_classes=["media-upload-surface"])- input_preview = ManagedVideo(label="Input video preview", interactive=False, visible=False)- with gr.Row(- visible=False, elem_id="upscale-input-actions",- elem_classes=["media-input-actions"],+ input_preview = ManagedVideo(label="Input video preview", interactive=False, visible="hidden")+ with gr.Row(+ visible=False, elem_id="upscale-input-actions",+ elem_classes=["media-input-actions"],) as input_actions:select_source = build_media_select_button("Choose Videos", ["video"], "upscale-select-input",)clear_source = build_media_clear_button("upscale-clear-input")- input_path, output_path = build_path_controls()- with gr.Accordion("RTX Video Super Resolution", open=True):- c["vsr_enabled"] = gr.Checkbox(value=opts.vsr_enabled, label="RTX Video Super Resolution")- c["vsr_quality"] = gr.Dropdown(VSR_QUALITIES, value=opts.vsr_quality, label="VSR quality", info="Ultra gives the highest quality.")+ with gr.Row():+ render = gr.Button("Upscale video(s)", variant="primary")+ stop = gr.Button("Stop", variant="stop")+ preview_frame_button = gr.Button("Preview 1 frame", visible=False)+ preview_button = gr.Button("Preview 3 sec", visible=False)+ reset = gr.Button("Reset settings")+ with gr.Column():+ c["vsr_enabled"] = gr.Checkbox(value=opts.vsr_enabled, label="Enable")+ c["vsr_quality"] = gr.Dropdown(VSR_QUALITIES, value=opts.vsr_quality, label="VSR quality")c["size_mode"] = gr.Radio(SIZE_MODES, value=opts.size_mode, label="Output sizing")c["scale_factor"] = gr.Dropdown(SCALE_FACTORS, value=opts.scale_factor, label="Scale factor",- info="1× enhances at original resolution. Other factors increase width and height.",)with gr.Row(visible=opts.size_mode == "Custom dimensions",@@ -136,11 +137,11 @@ def build_upscale_tab(settings):) as custom_dimensions_row:c["width"] = gr.Number(value=opts.width, minimum=2, maximum=16384, precision=0, label="Output width")c["height"] = gr.Number(value=opts.height, minimum=2, maximum=16384, precision=0, label="Output height")- c["aspect_lock"] = gr.Checkbox(value=opts.aspect_lock, label="Lock aspect ratio", info="Custom width determines height for each source.")+ c["aspect_lock"] = gr.Checkbox(value=opts.aspect_lock, label="Lock aspect ratio")dimensions = gr.Markdown(visible=False, elem_id="upscale-video-dimensions")+ input_path, output_path = build_path_controls()with gr.Accordion("RTX Video HDR", open=True):- c["hdr_enabled"] = gr.Checkbox(value=opts.hdr_enabled, label="Convert SDR to HDR", interactive=hdr_mode_supported(opts.codec),- info="Creates HDR highlights and colors. Requires H.265, AV1, or ProRes output.")+ c["hdr_enabled"] = gr.Checkbox(value=opts.hdr_enabled, label="Convert SDR to HDR", interactive=hdr_mode_supported(opts.codec))with gr.Column(visible=opts.hdr_enabled) as hdr_controls:with gr.Row():c["hdr_contrast"] = gr.Slider(0, 200, value=opts.hdr_contrast, step=1, precision=0, label="HDR contrast")@@ -148,36 +149,32 @@ def build_upscale_tab(settings):with gr.Row():c["hdr_middle_gray"] = gr.Slider(10, 100, value=opts.hdr_middle_gray, step=1, precision=0, label="HDR middle gray")c["hdr_peak_luminance"] = gr.Slider(400, 2000, value=opts.hdr_peak_luminance, step=1, precision=0, label="HDR peak luminance (nits)")- c["hdr_precision"] = gr.Radio(HDR_PRECISION_CHOICES, value=opts.hdr_precision, label="HDR processing precision",- info="FP16 uses more memory. HDR video exports use 10-bit color in both modes.")- c["quality"] = gr.Radio(ENCODING_QUALITIES, value=opts.quality, label="Encoding quality")- with gr.Row():- c["codec"] = gr.Dropdown(CODEC_CHOICES, value=opts.codec, label="Video codec", info="Plain = CPU; NVIDIA NVENC = GPU. ProRes requires MOV or MKV.")- c["container"] = gr.Dropdown(("MP4", "MKV", "MOV"), value=opts.container, label="Container")- with gr.Row():- c["rename_mode"] = gr.Radio(RENAME_MODES, value=opts.rename_mode, label="Rename")- c["custom_suffix"] = gr.Textbox(value=opts.custom_suffix, label="Custom suffix", interactive=opts.rename_mode == "Custom")- with gr.Row():- preview_frame_button = gr.Button("Preview 1 frame", visible=False)- preview_button = gr.Button("Preview 3 sec", visible=False)- render = gr.Button("Upscale video(s)", variant="primary")- stop = gr.Button("Stop", variant="stop")- reset = gr.Button("Reset settings")- with gr.Column(scale=3):+ c["hdr_precision"] = gr.Radio(HDR_PRECISION_CHOICES, value=opts.hdr_precision, label="HDR processing precision")+ c["quality"] = gr.Radio(ENCODING_QUALITIES, value=opts.quality, label="Encoding quality")+ with gr.Row():+ c["codec"] = gr.Dropdown(CODEC_CHOICES, value=opts.codec, label="Video codec")+ c["container"] = gr.Dropdown(("MP4", "MKV", "MOV"), value=opts.container, label="Container")+ with gr.Row():+ c["rename_mode"] = gr.Radio(RENAME_MODES, value=opts.rename_mode, label="Rename")+ c["custom_suffix"] = gr.Textbox(value=opts.custom_suffix, label="Custom suffix", interactive=opts.rename_mode == "Custom")+ with gr.Column(scale=3):output_video = ManagedVideo(label="Output video", interactive=False, visible=True, height=520,)- zip_download = gr.DownloadButton("Save as ZIP", visible=False)+ save_download, zip_button, zip_download = build_save_controls("video", "upscale-video")status = gr.Textbox(label="Status", interactive=False, lines=5, max_lines=12)results = gr.Dataframe(headers=BATCH_HEADERS, datatype=["str"]*len(BATCH_HEADERS), interactive=False, label="Batch results", wrap=True)tab = UpscaleTab(sources, input_preview, input_actions, select_source, clear_source, c, preview_frame_button, preview_button, render, stop, reset,- output_video, zip_download, status, results, input_path, output_path)- bind_batch_ui(tab, render_upscale_batch, kind="video", preview_mode=preview_mode,- preview_actions=[(tab.preview_frame, preview_frame), (tab.preview, preview_clip)])- c["hdr_enabled"].change(lambda enabled: gr.update(visible=enabled), inputs=c["hdr_enabled"], outputs=hdr_controls, queue=False)- c["codec"].change(lambda codec: gr.update(interactive=True) if hdr_mode_supported(codec) else gr.update(value=False, interactive=False),- inputs=c["codec"], outputs=c["hdr_enabled"], queue=False)- c["rename_mode"].change(lambda mode: gr.update(interactive=mode == "Custom"), inputs=c["rename_mode"], outputs=c["custom_suffix"], queue=False)+ output_video, save_download, zip_button, zip_download, status, results, input_path, output_path)+ bind_batch_ui(+ tab, render_upscale_batch, kind="video", preview_mode=preview_mode,+ archive_prefix="RTXVIDEO_VIDEO_BATCH",+ preview_actions=[(tab.preview_frame, preview_frame), (tab.preview, preview_clip)],+ )+ c["hdr_enabled"].change(lambda enabled: gr.update(visible=enabled), inputs=c["hdr_enabled"], outputs=hdr_controls, queue=False)+ c["codec"].change(lambda codec: gr.update(interactive=True) if hdr_mode_supported(codec) else gr.update(value=False, interactive=False),+ inputs=c["codec"], outputs=c["hdr_enabled"], queue=False)+ c["rename_mode"].change(lambda mode: gr.update(interactive=mode == "Custom"), inputs=c["rename_mode"], outputs=c["custom_suffix"], queue=False)def sizing_controls(enabled, mode, lock):custom = mode == "Custom dimensions"return [gr.update(interactive=enabled), gr.update(interactive=enabled),@@ -187,41 +184,41 @@ def build_upscale_tab(settings):gr.update(visible=custom, interactive=enabled)]sizing_outputs = [c["vsr_quality"], c["size_mode"], c["scale_factor"], custom_dimensions_row,c["width"], c["height"], c["aspect_lock"]]- for name in ("vsr_enabled", "size_mode", "aspect_lock"):- c[name].change(sizing_controls, inputs=[c["vsr_enabled"], c["size_mode"], c["aspect_lock"]], outputs=sizing_outputs, queue=False)- def initialize_custom(paths, path, factor, mode):- if mode != "Custom dimensions":- return gr.skip(), gr.skip()- try:- src = resolve_inputs(path, paths, "video")[0]- m = inspect_video(Path(src), reject_hdr=True)- w,h,_ = output_size(m["width"], m["height"], UpscaleOptions(scale_factor=factor))- return w,h- except Exception:- return gr.skip(), gr.skip()- c["size_mode"].input(initialize_custom, inputs=[sources, input_path, c["scale_factor"], c["size_mode"]], outputs=[c["width"], c["height"]], queue=False)- def locked_height(paths, path, width, lock):- if not lock:- return gr.skip()- try:- src = resolve_inputs(path, paths, "video")[0]- m = inspect_video(Path(src), reject_hdr=True)- _, h, _ = output_size(m["width"], m["height"], UpscaleOptions(size_mode="Custom dimensions", width=width))- return h- except Exception:- return gr.skip()- for name in ("width", "aspect_lock"):- c[name].input(locked_height, inputs=[sources, input_path, c["width"], c["aspect_lock"]], outputs=c["height"], queue=False)+ for name in ("vsr_enabled", "size_mode", "aspect_lock"):+ c[name].change(sizing_controls, inputs=[c["vsr_enabled"], c["size_mode"], c["aspect_lock"]], outputs=sizing_outputs, queue=False)+ def initialize_custom(paths, path, factor, mode):+ if mode != "Custom dimensions":+ return gr.skip(), gr.skip()+ try:+ src = resolve_inputs(path, paths, "video")[0]+ m = inspect_video(Path(src), reject_hdr=True)+ w,h,_ = output_size(m["width"], m["height"], UpscaleOptions(scale_factor=factor))+ return w,h+ except Exception:+ return gr.skip(), gr.skip()+ c["size_mode"].input(initialize_custom, inputs=[sources, input_path, c["scale_factor"], c["size_mode"]], outputs=[c["width"], c["height"]], queue=False)+ def locked_height(paths, path, width, lock):+ if not lock:+ return gr.skip()+ try:+ src = resolve_inputs(path, paths, "video")[0]+ m = inspect_video(Path(src), reject_hdr=True)+ _, h, _ = output_size(m["width"], m["height"], UpscaleOptions(size_mode="Custom dimensions", width=width))+ return h+ except Exception:+ return gr.skip()+ for name in ("width", "aspect_lock"):+ c[name].input(locked_height, inputs=[sources, input_path, c["width"], c["aspect_lock"]], outputs=c["height"], queue=False)for component in (sources, input_path, *tab.settings_inputs):component.change(lambda *args: gr.update(value=(text := describe_size(*args)), visible=bool(text)),inputs=[sources, input_path, *tab.settings_inputs], outputs=dimensions,queue=False, show_progress="hidden", trigger_mode="always_last",)- # Initialize state directly; events handle subsequent preset/reset changes.- initial = sizing_controls(opts.vsr_enabled, opts.size_mode, opts.aspect_lock)- for component, update in zip(sizing_outputs, initial):- for key in ("visible", "interactive"):- if key in update:- setattr(component, key, update[key])- return tab+ # Initialize state directly; events handle subsequent preset/reset changes.+ initial = sizing_controls(opts.vsr_enabled, opts.size_mode, opts.aspect_lock)+ for component, update in zip(sizing_outputs, initial):+ for key in ("visible", "interactive"):+ if key in update:+ setattr(component, key, update[key])+ return tabdiff --git a/start.bat b/start.batindex ad56b73..01e6038 100644--- a/start.bat+++ b/start.bat@@ -8,5 +8,7 @@ set "PYTHONNOUSERSITE=1"set "PYTHONDONTWRITEBYTECODE=1"set "PYTHONIOENCODING=utf-8"set "GRADIO_ANALYTICS_ENABLED=False"+set "GRADIO_TEMP_DIR=%~dp0temp\gradio"+if not exist "%GRADIO_TEMP_DIR%" mkdir "%GRADIO_TEMP_DIR%""%~dp0bin\python-3.13.15-embed-amd64\python.exe" "%~dp0app.py"if errorlevel 1 pause