Skip to content

LTX-Video Support for ComfyUI

Automated PR - 2026-09-17

github-actions[bot]63a7f34e6412
diff --git a/README.md b/README.md
index 81b9e7e..919e99f 100644
--- a/README.md
+++ b/README.md
@@ -86,13 +86,13 @@ The union LoRA is trained to understand and respond to both control signals (dep
## HDR IC-LoRA
-We provide an **HDR IC-LoRA** that generates linear HDR video encoded in ARRI LogC3, enabling workflows that output high-dynamic-range content suitable for grading and EXR export.
+We provide an **HDR IC-LoRA** that upgrades SDR video into ACEScct HDR (same working space as pipelines `hdr_ic_lora`), suitable for grading, EXR export, and HLG masters. Use the **2.5** graph: [`LTX-2.5_ICLoRA_HDR_Distilled.json`](./example_workflows/2.5/LTX-2.5_ICLoRA_HDR_Distilled.json). The 2.3 workflow remains as a legacy LogC3 example.
### Key Features
- **Linear HDR output**: The LoRA produces frames in LogC3-compressed space; the `LTXVHDRDecodePostprocess` node decodes these back to linear HDR values.
- **SDR preview + raw HDR**: The node outputs both a Reinhard-tonemapped SDR preview and the raw linear HDR tensor for downstream use.
-- **EXR export**: Optionally writes the linear HDR frames as a 16/32-bit EXR image sequence. To enable EXR writing, set `OPENCV_IO_ENABLE_OPENEXR=1` in the environment before starting ComfyUI. The exported EXR sequence is best viewed in [DJV](https://github.com/grizzlypeak3d/DJV) (or [DJV for macOS](https://djv.en.uptodown.com/mac/download)).
+- **EXR export**: Optionally writes the linear HDR frames as a 16/32-bit EXR image sequence via **OpenImageIO** (`pip install openimageio`, also listed in `requirements.txt`). The exported EXR sequence is best viewed in [DJV](https://github.com/grizzlypeak3d/DJV) (or [DJV for macOS](https://djv.en.uptodown.com/mac/download)).
## Dub-It IC-LoRA
diff --git a/__init__.py b/__init__.py
index 8f921a3..236493e 100644
--- a/__init__.py
+++ b/__init__.py
@@ -14,7 +14,13 @@ from .gemma_api_conditioning import GemmaAPITextEncode
from .gemma_encoder import LTXVGemmaCLIPModelLoader, LTXVGemmaEnhancePrompt
from .guide import LTXVAddGuideAdvanced, LTXVAddGuideAdvancedAttention
from .guiders import GuiderParametersNode, MultimodalGuiderNode
-from .hdr import LTXVHDRDecodePostprocess
+from .hdr_nodes import (
+ LTXVHDRDecodePostprocess,
+ LTXVLoadEXRSequence,
+ LTXVSaveHLG,
+ LTXVSDRToHDRWorkingSpace,
+ LTXVVAEForceFloat32,
+)
from .iclora import (
LTXAddVideoICLoRAGuide,
LTXAddVideoICLoRAGuideAdvanced,
@@ -28,7 +34,6 @@ from .latent_norm import (
LTXVStatNormLatent,
)
from .latents import (
- LTXVAddLatentGuide,
LTXVImgToVideoConditionOnly,
LTXVSelectLatents,
LTXVSetVideoLatentNoiseMasks,
@@ -69,7 +74,6 @@ NODE_CLASS_MAPPINGS = {
"LTXVLinearOverlapLatentTransition": LinearOverlapLatentTransition,
"LTXVAddGuideAdvanced": LTXVAddGuideAdvanced,
"LTXVAddGuideAdvancedAttention": LTXVAddGuideAdvancedAttention,
- "LTXVAddLatentGuide": LTXVAddLatentGuide,
"LTXVAdainLatent": LTXVAdainLatent,
"LTXVImgToVideoConditionOnly": LTXVImgToVideoConditionOnly,
"LTXVPerStepAdainPatcher": LTXVPerStepAdainPatcher,
@@ -118,6 +122,10 @@ NODE_CLASS_MAPPINGS = {
"LTXVInpaintPreprocess": LTXVInpaintPreprocess,
"LTXVLaplacianPyramidBlend": LTXVLaplacianPyramidBlend,
"LTXVHDRDecodePostprocess": LTXVHDRDecodePostprocess,
+ "LTXVSDRToHDRWorkingSpace": LTXVSDRToHDRWorkingSpace,
+ "LTXVLoadEXRSequence": LTXVLoadEXRSequence,
+ "LTXVVAEForceFloat32": LTXVVAEForceFloat32,
+ "LTXVSaveHLG": LTXVSaveHLG,
"LTXVAudioOnlyModel": LTXVAudioOnlyModel,
"LTXVAudioOnlyEmptyVideoLatent": LTXVAudioOnlyEmptyVideoLatent,
}
diff --git a/conditioning_loader.py b/conditioning_loader.py
index 40d9be9..498158d 100644
--- a/conditioning_loader.py
+++ b/conditioning_loader.py
@@ -9,6 +9,76 @@ from comfy_api.latest import io
from .nodes_registry import comfy_node
+# Pipeline HDR IC-LoRA scene embeddings (`hdr_ic_lora._load_video_context`).
+_PIPELINE_VIDEO_KEYS = ("video_context", "video_prompt_embeds")
+_PIPELINE_AUDIO_KEYS = ("audio_context", "audio_prompt_embeds")
+# LTX-AV text encode layout: video 4096 + audio 2048 on the last dim.
+_VIDEO_CONTEXT_DIM = 4096
+_AUDIO_CONTEXT_DIM = 2048
+_AV_CONTEXT_DIM = _VIDEO_CONTEXT_DIM + _AUDIO_CONTEXT_DIM
+
+
+def _unsqueeze_batch(t: torch.Tensor, *, source: str, kind: str) -> torch.Tensor:
+ """Ensure conditioning tensors are ``[B, T, D]`` (pipeline files are often ``[T, D]``)."""
+ if t.ndim == 2:
+ return t.unsqueeze(0)
+ if t.ndim == 3:
+ return t
+ raise ValueError(
+ f"{source}: {kind} embedding must be 2D [T, D] or 3D [B, T, D]; "
+ f"got shape {tuple(t.shape)}"
+ )
+
+
+def _load_pipeline_scene_embedding(f: Any, source: str) -> list[list[Any]]:
+ """Load ``video_context`` (+ optional ``audio_context``) into Comfy CONDITIONING.
+
+ Matches ``ltx_pipelines.hdr_ic_lora``: the safetensors replaces text encoding.
+ For AV Comfy models, video (4096) and audio (2048) contexts are concatenated on
+ the last dim — same layout as a fully processed LTX-AV text encode.
+ """
+ keys = list(f.keys())
+ video_key = next((k for k in _PIPELINE_VIDEO_KEYS if k in keys), None)
+ if video_key is None:
+ return []
+
+ video = _unsqueeze_batch(f.get_tensor(video_key), source=source, kind=video_key)
+ video_dim = int(video.shape[-1])
+ audio_key = next((k for k in _PIPELINE_AUDIO_KEYS if k in keys), None)
+
+ if video_dim == _AV_CONTEXT_DIM:
+ # Already video∥audio on the last dim (rare export); use as-is.
+ tensor = video
+ elif video_dim == _VIDEO_CONTEXT_DIM:
+ if audio_key is None:
+ raise ValueError(
+ f"{source}: {video_key} last dim is {_VIDEO_CONTEXT_DIM} but no "
+ f"audio_context / audio_prompt_embeds found. AV conditioning needs "
+ f"{_VIDEO_CONTEXT_DIM}+{_AUDIO_CONTEXT_DIM}="
+ f"{_AV_CONTEXT_DIM} on the last dim."
+ )
+ audio = _unsqueeze_batch(f.get_tensor(audio_key), source=source, kind=audio_key)
+ audio_dim = int(audio.shape[-1])
+ if audio_dim != _AUDIO_CONTEXT_DIM:
+ raise ValueError(
+ f"{source}: {audio_key} last dim must be {_AUDIO_CONTEXT_DIM}; "
+ f"got {audio_dim} with shape {tuple(audio.shape)}"
+ )
+ if audio.shape[:-1] != video.shape[:-1]:
+ raise ValueError(
+ f"{source}: audio/video context shape mismatch "
+ f"(excluding last dim): {tuple(audio.shape)} vs {tuple(video.shape)}"
+ )
+ tensor = torch.cat([video, audio], dim=-1)
+ else:
+ raise ValueError(
+ f"{source}: {video_key} last dim must be {_VIDEO_CONTEXT_DIM} "
+ f"(or pre-concatenated {_AV_CONTEXT_DIM}); got {video_dim} with shape "
+ f"{tuple(video.shape)}. Check that video/audio keys are not swapped."
+ )
+
+ return [[tensor, {}]]
+
@comfy_node(name="LTXVLoadConditioning")
class LTXVLoadConditioning(io.ComfyNode):
@@ -21,6 +91,11 @@ class LTXVLoadConditioning(io.ComfyNode):
node_id="LTXVLoadConditioning",
display_name="🅛🅣🅧 LTXV Load Conditioning",
category="lightricks/LTXV",
+ description=(
+ "Load precomputed text conditioning from models/embeddings. "
+ "Supports Comfy conditioning_data_* files and pipeline scene "
+ "embeddings (video_context / audio_context), e.g. HDR IC-LoRA."
+ ),
inputs=[
io.Combo.Input("file_name", options=sorted(files)),
io.Combo.Input("device", options=["cpu", "gpu"]),
@@ -47,19 +122,25 @@ class LTXVLoadConditioning(io.ComfyNode):
) as f:
tensor_keys = [k for k in f.keys() if k.startswith("conditioning_data_")]
- for tensor_key in sorted(tensor_keys):
- idx = tensor_key.replace("conditioning_data_", "")
- tensor = f.get_tensor(tensor_key)
+ if tensor_keys:
+ for tensor_key in sorted(tensor_keys):
+ idx = tensor_key.replace("conditioning_data_", "")
+ tensor = f.get_tensor(tensor_key)
- options: dict[str, Any] = {}
- mask_key = f"attention_mask_{idx}"
- if mask_key in f.keys():
- options["attention_mask"] = f.get_tensor(mask_key)
+ options: dict[str, Any] = {}
+ mask_key = f"attention_mask_{idx}"
+ if mask_key in f.keys():
+ options["attention_mask"] = f.get_tensor(mask_key)
- conditioning.append([tensor, options])
+ conditioning.append([tensor, options])
+ else:
+ conditioning = _load_pipeline_scene_embedding(f, source=file_name)
if not conditioning:
- raise ValueError(f"No conditioning data found in file: {file_name}")
+ raise ValueError(
+ f"No conditioning data found in file: {file_name}. "
+ "Expected conditioning_data_* or video_context / video_prompt_embeds."
+ )
return io.NodeOutput(conditioning)
diff --git a/easy_samplers.py b/easy_samplers.py
index d44e77c..4c5ad86 100644
--- a/easy_samplers.py
+++ b/easy_samplers.py
@@ -12,7 +12,7 @@ from comfy_extras.nodes_lt import EmptyLTXVLatentVideo, LTXVAddGuide, LTXVCropGu
from .guide import blur_internal
from .latent_norm import LTXVAdainLatent
-from .latents import LTXVAddLatentGuide, LTXVSelectLatents
+from .latents import LTXVSelectLatents, add_latent_guide
from .nodes_registry import comfy_node
@@ -248,7 +248,7 @@ class LTXVBaseSampler:
positive,
negative,
latents,
- ) = LTXVAddLatentGuide().generate(
+ ) = add_latent_guide(
vae=vae,
positive=positive,
negative=negative,
@@ -446,7 +446,7 @@ class LTXVExtendSampler:
positive,
negative,
new_latents,
- ) = LTXVAddLatentGuide().generate(
+ ) = add_latent_guide(
vae=vae,
positive=positive,
negative=negative,
@@ -504,7 +504,7 @@ class LTXVExtendSampler:
positive,
negative,
new_latents,
- ) = LTXVAddLatentGuide().generate(
+ ) = add_latent_guide(
vae=vae,
positive=positive,
negative=negative,
@@ -519,7 +519,7 @@ class LTXVExtendSampler:
positive,
negative,
new_latents,
- ) = LTXVAddLatentGuide().generate(
+ ) = add_latent_guide(
vae=vae,
positive=positive,
negative=negative,
@@ -552,7 +552,7 @@ class LTXVExtendSampler:
positive,
negative,
denoised_output_latents,
- ) = LTXVAddLatentGuide().generate(
+ ) = add_latent_guide(
vae=vae,
positive=positive,
negative=negative,
@@ -756,7 +756,7 @@ class LTXVInContextSampler:
positive,
negative,
new_latents,
- ) = LTXVAddLatentGuide().generate(
+ ) = add_latent_guide(
vae=vae,
positive=positive,
negative=negative,
@@ -792,7 +792,7 @@ class LTXVInContextSampler:
positive,
negative,
new_latents,
- ) = LTXVAddLatentGuide().generate(
+ ) = add_latent_guide(
vae=vae,
positive=positive,
negative=negative,
diff --git a/example_workflows/2.3/LTX-2.3_ICLoRA_HDR_Distilled.json b/example_workflows/2.3/LTX-2.3_ICLoRA_HDR_Distilled.json
index e117a06..97b2b43 100644
--- a/example_workflows/2.3/LTX-2.3_ICLoRA_HDR_Distilled.json
+++ b/example_workflows/2.3/LTX-2.3_ICLoRA_HDR_Distilled.json
@@ -1119,7 +1119,7 @@
"outputs": [],
"properties": {},
"widgets_values": [
- "Set OPENCV_IO_ENABLE_OPENEXR=1 in your shell before launching ComfyUI, otherwise save_exr will fail"
+ "EXR export requires OpenImageIO (pip install openimageio). Same backend as ltx-pipelines."
],
"color": "#432",
"bgcolor": "#653"
@@ -1407,6 +1407,16 @@
"type": "IMAGE",
"link": 13664
},
+ {
+ "localized_name": "transfer",
+ "name": "transfer",
+ "shape": 7,
+ "type": "COMBO",
+ "widget": {
+ "name": "transfer"
+ },
+ "link": null
+ },
{
"localized_name": "exposure",
"name": "exposure",
@@ -1427,6 +1437,16 @@
},
"link": null
},
+ {
+ "localized_name": "exr_color_space",
+ "name": "exr_color_space",
+ "shape": 7,
+ "type": "COMBO",
+ "widget": {
+ "name": "exr_color_space"
+ },
+ "link": null
+ },
{
"localized_name": "output_dir",
"name": "output_dir",
@@ -1478,8 +1498,10 @@
"Node name for S&R": "LTXVHDRDecodePostprocess"
},
"widgets_values": [
+ "logc3",
7.1,
true,
+ "linear",
"output/hdr_exr3",
"frame",
true
@@ -3086,8 +3108,10 @@
},
"5114": {
"inputs": {
+ "transfer": "logc3",
"exposure": 7.1,
"save_exr": true,
+ "exr_color_space": "linear",
"output_dir": "output/hdr_exr3",
"filename_prefix": "frame",
"half_precision": true,
diff --git a/example_workflows/2.5/LTX-2.5_A2V_Two_Stage_Distilled.json b/example_workflows/2.5/LTX-2.5_A2V_Two_Stage_Distilled.json
index 20d3c18..82c96bf 100644
--- a/example_workflows/2.5/LTX-2.5_A2V_Two_Stage_Distilled.json
+++ b/example_workflows/2.5/LTX-2.5_A2V_Two_Stage_Distilled.json
@@ -7636,7 +7636,7 @@
"Node name for S&R": "ManualSigmas"
},
"widgets_values": [
- "0.85, 0.7250, 0.4219, 0.0"
+ "0.909375, 0.725, 0.421875, 0.0"
]
},
{
diff --git a/example_workflows/2.5/LTX-2.5_ICLoRA_Union_Control_Distilled.json b/example_workflows/2.5/LTX-2.5_ICLoRA_Union_Control_Distilled.json
index 2913b52..706c0f8 100644
--- a/example_workflows/2.5/LTX-2.5_ICLoRA_Union_Control_Distilled.json
+++ b/example_workflows/2.5/LTX-2.5_ICLoRA_Union_Control_Distilled.json
@@ -5314,7 +5314,7 @@
"ver": "0.14.1"
},
"widgets_values": [
- "0.85, 0.7250, 0.4219, 0.0"
+ "0.909375, 0.725, 0.421875, 0.0"
]
},
{
diff --git a/example_workflows/2.5/LTX-2.5_T2V_I2V_Two_Stage_Distilled.json b/example_workflows/2.5/LTX-2.5_T2V_I2V_Two_Stage_Distilled.json
index 6820fe9..e0569bb 100644
--- a/example_workflows/2.5/LTX-2.5_T2V_I2V_Two_Stage_Distilled.json
+++ b/example_workflows/2.5/LTX-2.5_T2V_I2V_Two_Stage_Distilled.json
@@ -6019,7 +6019,7 @@
"ver": "0.14.1"
},
"widgets_values": [
- "0.85, 0.7250, 0.4219, 0.0"
+ "0.909375, 0.725, 0.421875, 0.0"
]
},
{
diff --git a/example_workflows/2.5/README.md b/example_workflows/2.5/README.md
index e322366..f2c8d97 100644
--- a/example_workflows/2.5/README.md
+++ b/example_workflows/2.5/README.md
@@ -42,10 +42,13 @@ These keep the distilled 2.5 backbone and add an IC-LoRA so a guide (video, imag
| -------- | ------------ |
| [LTX-2.5_ICLoRA_Union_Control_Distilled.json](./LTX-2.5_ICLoRA_Union_Control_Distilled.json) | Video-to-video from a **depth / canny / pose** annotator on a reference clip. Depth is wired by default; switch the annotator inside the graph. |
| [LTX-2.5_V2V_ICLoRA_Single_Stage_Distilled.json](./LTX-2.5_V2V_ICLoRA_Single_Stage_Distilled.json) | Video-to-video using the source frames as IC-LoRA guides. Ships with the Instant Shave LoRA as the example; swap the LoRA for other identity/edit LoRAs. Original audio is frozen through. |
+| [LTX-2.5_ICLoRA_HDR_Distilled.json](./LTX-2.5_ICLoRA_HDR_Distilled.json) | **SDR→HDR**: SDR MP4 → ACEScct IDT → HDR IC-LoRA → EXR + HLG. |
| [LTX-2.5_ICLoRA_Ingredients_Single_Stage_Distilled.json](./LTX-2.5_ICLoRA_Ingredients_Single_Stage_Distilled.json) | Generate from a **reference sheet** (characters, props, wardrobe, location laid out in one image). Describe each element in the prompt by its place on the sheet. |
| [LTX-2.5_ICLoRA_Motion_Track_Distilled.json](./LTX-2.5_ICLoRA_Motion_Track_Distilled.json) | Image-to-video with **sparse motion tracks** you draw on the first frame. Optional still as the opening frame. |
| [LTX-2.5_ICLoRA_Inpaint_Two_Stage_Distilled.json](./LTX-2.5_ICLoRA_Inpaint_Two_Stage_Distilled.json) | Fill masked regions of a reference video (two-stage). Source audio can stay frozen. |
+| [LTX-2.5_ICLoRA_Inpaint_Native_HDR_Two_Stage_Distilled.json](./LTX-2.5_ICLoRA_Inpaint_Native_HDR_Two_Stage_Distilled.json) | Same inpaint, but **HDR→HDR**: EXR sequence + mask video → EXR + HLG out. |
| [LTX-2.5_ICLoRA_Outpaint_Two_Stage_Distilled.json](./LTX-2.5_ICLoRA_Outpaint_Two_Stage_Distilled.json) | Extend the canvas of a reference video (two-stage). Same in/outpaint LoRA as inpaint. |
+| [LTX-2.5_ICLoRA_Outpaint_Native_HDR_Two_Stage_Distilled.json](./LTX-2.5_ICLoRA_Outpaint_Native_HDR_Two_Stage_Distilled.json) | Same outpaint, but **HDR→HDR**: EXR sequence + target size → EXR + HLG out. |
## Which workflow should I use?
@@ -63,10 +66,14 @@ Do you have an existing video to edit or follow?
│ │ → LTX-2.5_ICLoRA_Union_Control_Distilled.json
│ ├─ Keep the footage, change appearance (identity / style LoRA)
│ │ → LTX-2.5_V2V_ICLoRA_Single_Stage_Distilled.json
+│ ├─ Upgrade SDR footage to HDR (ACEScct IC-LoRA)
+│ │ → LTX-2.5_ICLoRA_HDR_Distilled.json
│ ├─ Fill a masked region
-│ │ → LTX-2.5_ICLoRA_Inpaint_Two_Stage_Distilled.json
+│ │ → LTX-2.5_ICLoRA_Inpaint_Two_Stage_Distilled.json (SDR)
+│ │ → LTX-2.5_ICLoRA_Inpaint_Native_HDR_Two_Stage_Distilled.json (HDR EXR)
│ └─ Grow the frame / canvas
-│ → LTX-2.5_ICLoRA_Outpaint_Two_Stage_Distilled.json
+│ → LTX-2.5_ICLoRA_Outpaint_Two_Stage_Distilled.json (SDR)
+│ → LTX-2.5_ICLoRA_Outpaint_Native_HDR_Two_Stage_Distilled.json (HDR EXR)
│
Do you have stills rather than a video?
├─ YES → A sheet of characters / props / locations?
@@ -95,9 +102,19 @@ Two-stage is the better default when you care about spatial detail. Single-stage
| T2A single-stage | 1 | — | Text | Audio-only clips |
| Union Control | 1 | — | Reference video (depth / canny / pose) | Structure-following v2v |
| V2V IC-LoRA | 1 | — | Source video (+ frozen audio) | Appearance edits on existing footage |
+| [SDR→HDR IC-LoRA](./LTX-2.5_ICLoRA_HDR_Distilled.json) | 1 | — | SDR video (+ frozen audio) | SDR→ACEScct HDR; EXR + HLG out |
| Ingredients | 1 | — | Reference sheet | Cast / props / location from one image |
| Motion Track | 1 | — | Image + drawn tracks | Directed motion from a still |
| Inpaint two-stage | 2 | 2× spatial | Video + mask (+ frozen audio) | Replacing a region |
-| Outpaint two-stage | 2 | 2× spatial | Video + mask (+ frozen audio) | Extending the frame |
+| Outpaint two-stage | 2 | 2× spatial | Video + target size (+ frozen audio) | Extending the frame |
+| [Outpaint Native HDR two-stage](./LTX-2.5_ICLoRA_Outpaint_Native_HDR_Two_Stage_Distilled.json) | 2 | 2× spatial | EXR sequence + target size | HDR outpaint; EXR + HLG out |
+| [I2V Native HDR two-stage](./LTX-2.5_I2V_Native_HDR_Two_Stage_Distilled.json) | 2 | 2× spatial | EXR still (ACEScct) | HDR still → HDR video; EXR + HLG out |
+| [Inpaint Native HDR two-stage](./LTX-2.5_ICLoRA_Inpaint_Native_HDR_Two_Stage_Distilled.json) | 2 | 2× spatial | EXR sequence + mask video | HDR inpaint; EXR + HLG out |
+
+Native HDR I/O: `LTXVLoadEXRSequence` → float32 VAE → `LTXVHDRDecodePostprocess` (`transfer=acescct`, optional EXR) → `LTXVSaveHLG` from `hdr_linear`. Same idea as pipelines `--hdr` on Distilled / IC-LoRA.
+
+SDR→HDR IC-LoRA (separate from native EXR HDR): [LTX-2.5_ICLoRA_HDR_Distilled.json](./LTX-2.5_ICLoRA_HDR_Distilled.json) — `LoadVideo` → `LTXVSDRToHDRWorkingSpace` (`srgb_gamma`) → `Resize Image/Mask` (scale to multiple 32) → **fixed scene embeddings** (`LTXVLoadConditioning`, not a free-text prompt) → HDR IC-LoRA guide → float32 VAE decode → `LTXVHDRDecodePostprocess` (`transfer=acescct`, EXR=`acescct`) → HLG. Sampler **euler**; original audio frozen/muxed. Same idea as pipelines `hdr_ic_lora --text-embeddings`.
+
+For native HDR, set **`img_compression = 0`** (Python `--hdr` EXR conditioning skips JPEG/CRF; CRF on ACEScct darkens highlights). Turn **prompt enhancer off** when matching goldens. I2V stage-2 distilled sigmas should be `0.909375, 0.725, 0.421875, 0.0` (not `0.85, …`). Inpaint keeps its own 2-step stage-2 schedule.
Python / pipeline equivalents of these ideas live in [pipeline-selection.md](https://github.com/Lightricks/LTX-2/blob/main/packages/ltx-pipelines/docs/pipeline-selection.md) in the LTX-2 repo.
diff --git a/gemma_api_conditioning.py b/gemma_api_conditioning.py
index 1c964d5..edc82ae 100644
--- a/gemma_api_conditioning.py
+++ b/gemma_api_conditioning.py
@@ -4,6 +4,7 @@ Replaces the CLIP encoding step entirely using an external API.
"""
import io
+import json
import logging
import pickle
@@ -23,14 +24,18 @@ UPDATE_MESSAGE = (
INVALID_API_KEY_MESSAGE = (
"Invalid API key. Please generate a new API key at: https://console.ltx.video/"
)
-MISSING_MODEL_ID_MESSAGE = "Model ID cannot be identified from the provided model file"
+MISSING_MODEL_SELECTOR_MESSAGE = (
+ "Cannot identify the text encoder from the provided model file."
+)
+MODEL_ID_METADATA_KEY = "encrypted_wandb_properties"
+GEMMA_SOURCE_CHECKPOINT_METADATA_KEY = "gemma_source_checkpoint"
MODEL_FOLDERS = ("checkpoints", "diffusion_models")
def model_filename_list() -> list[str]:
- """Every model the node can read a model id from, in folder order and de-duplicated.
+ """Every model the node can read a text-encoder selector from, de-duplicated.
A name present in both folders is listed once; resolve_model_path resolves
it the same way round, so the entry always refers to the same file the
@@ -55,17 +60,49 @@ def resolve_model_path(ckpt_name: str) -> str:
)
-def extract_model_id(ckpt_name: str) -> str:
- model_id_key = "encrypted_wandb_properties"
+def parse_gemma_source_checkpoint(raw: str) -> dict[str, str]:
+ """Parse the ``gemma_source_checkpoint`` safetensors metadata JSON string."""
+ try:
+ obj = json.loads(raw)
+ except json.JSONDecodeError as exc:
+ raise ValueError(MISSING_MODEL_SELECTOR_MESSAGE) from exc
+ if not isinstance(obj, dict):
+ raise ValueError(MISSING_MODEL_SELECTOR_MESSAGE)
+ ltx_version, gemma_version = obj.get("ltx_version"), obj.get("gemma_version")
+ if not ltx_version or not gemma_version:
+ raise ValueError(MISSING_MODEL_SELECTOR_MESSAGE)
+ return {"ltx_version": str(ltx_version), "gemma_version": str(gemma_version)}
+
+
+def prompt_embedding_selector_from_metadata(
+ metadata: dict[str, str] | None,
+) -> dict[str, str | dict[str, str]]:
+ """Build the XOR selector for ``/v1/prompt-embedding``.
+
+ LTX-2.5+ checkpoints identify the text encoder via ``gemma_source_checkpoint``
+ (``ltx_version`` + ``gemma_version``). Pre-2.5 checkpoints use
+ ``encrypted_wandb_properties`` as ``model_id``. Prefer the 2.5+ selector when
+ both are present: OS 2.5 files may still carry an unusable ``model_id``.
+ """
+ if metadata:
+ raw_gemma = metadata.get(GEMMA_SOURCE_CHECKPOINT_METADATA_KEY)
+ if raw_gemma:
+ return {"model": parse_gemma_source_checkpoint(raw_gemma)}
+ model_id = metadata.get(MODEL_ID_METADATA_KEY)
+ if model_id:
+ return {"model_id": model_id}
+ raise ValueError(MISSING_MODEL_SELECTOR_MESSAGE)
+
+
+def extract_prompt_embedding_selector(
+ ckpt_name: str,
+) -> dict[str, str | dict[str, str]]:
with safe_open(
resolve_model_path(ckpt_name),
framework="pt",
device="cpu",
) as f:
- metadata = f.metadata()
- if not metadata or model_id_key not in metadata:
- raise ValueError(MISSING_MODEL_ID_MESSAGE)
- return metadata[model_id_key]
+ return prompt_embedding_selector_from_metadata(f.metadata())
@comfy_node(name="GemmaAPITextEncode")
@@ -74,8 +111,8 @@ class GemmaAPITextEncode:
Encodes text prompts using the LTX Video API, returning CONDITIONING for LTX-2 models.
This node replaces the local CLIP encoding step by sending the prompt to an external API
- for processing. It requires an API key and automatically extracts the model ID from the
- checkpoint file metadata.
+ for processing. It requires an API key and selects the text encoder from checkpoint
+ metadata: ``model`` (LTX-2.5+) or ``model_id`` (LTX-2.3).
Inputs:
- api_key: Authentication key for the LTX Video API
@@ -117,9 +154,8 @@ class GemmaAPITextEncode:
"ckpt_name": (
model_filename_list(),
{
- "tooltip": "The model to read the API model id from. "
- "Either a checkpoint or a diffusion model; it must "
- "carry the model id in its safetensors metadata."
+ "tooltip": "The model to read the API text-encoder selector from. "
+ "Either a checkpoint or a diffusion model."
},
),
},
@@ -142,14 +178,18 @@ class GemmaAPITextEncode:
if not ckpt_name or not ckpt_name.strip():
raise ValueError("Model path is required")
- model_id = extract_model_id(ckpt_name)
+ selector = extract_prompt_embedding_selector(ckpt_name)
payload = {
"prompt": prompt,
- "model_id": model_id,
"enhance_prompt": enhance_prompt,
+ **selector,
}
+ if "model_id" in selector:
+ selector_desc = f"model_id: {str(selector['model_id'])[:50]}..."
+ else:
+ selector_desc = f"model: {selector['model']}"
logger.info(
- f"Calling API to encode prompt: {prompt[:50]}... with model_id: {model_id[:50]}..."
+ f"Calling API to encode prompt: {prompt[:50]}... with {selector_desc}"
)
try:
response = requests.post(
diff --git a/hdr.py b/hdr.py
deleted file mode 100644
index 1e7608a..0000000
--- a/hdr.py
+++ /dev/null
@@ -1,245 +0,0 @@
-"""HDR utilities and ComfyUI node for HDR IC-LoRA inference.
-
-Provides HDR compression/decompression transforms and a single ComfyUI node:
-
-- LTXVHDRDecodePostprocess: Decompress VAE output, tonemap for preview,
- and optionally save raw linear HDR frames as EXR.
-"""
-
-import logging
-import os
-
-import torch
-from torch import Tensor
-
-from .nodes_registry import comfy_node
-
-logger = logging.getLogger("LTXVideo.hdr")
-
-
-# ---------------------------------------------------------------------------
-# HDR Transform Classes (ported from ltx_core/hdr.py)
-# ---------------------------------------------------------------------------
-
-
-class LogC3:
- """ARRI LogC3 (EI 800) HDR compression.
-
- Maps linear [0, inf) -> [0, 1] via the camera log curve, then scales to
- [-1, 1] for VAE input.
- """
-
- A = 5.555556
- B = 0.052272
- C = 0.247190
- D = 0.385537
- E = 5.367655
- F = 0.092809
- CUT = 0.010591
-
- def compress(self, hdr: Tensor) -> Tensor:
- x = torch.clamp(hdr, min=0.0)
- log_part = self.C * torch.log10(self.A * x + self.B) + self.D
- lin_part = self.E * x + self.F
- logc = torch.where(x >= self.CUT, log_part, lin_part)
- logc = torch.clamp(logc, 0.0, 1.0)
- return logc * 2.0 - 1.0
-
- def decompress(self, z: Tensor) -> Tensor:
- logc = torch.clamp((z + 1.0) / 2.0, 0.0, 1.0)
- cut_log = self.E * self.CUT + self.F
- lin_from_log = (torch.pow(10.0, (logc - self.D) / self.C) - self.B) / self.A
- lin_from_lin = (logc - self.F) / self.E
- return torch.where(logc >= cut_log, lin_from_log, lin_from_lin)
-
-
-# ---------------------------------------------------------------------------
-# Transform registry — singleton (stateless)
-# ---------------------------------------------------------------------------
-
-_LOGC3 = LogC3()
-
-
-def _hdr_decompress(decoded_01: Tensor) -> Tensor:
- """Decompress VAE-decoded image from [0,1] to linear HDR [0, inf).
-
- ComfyUI's VAE decode returns images in [0, 1] via ``(raw + 1) / 2``.
- This function reverses that and applies LogC3 HDR decompression.
- """
- raw = decoded_01.float() * 2.0 - 1.0
- return _LOGC3.decompress(raw)
-
-
-def _linear_to_srgb(x: Tensor) -> Tensor:
- """Convert linear [0, 1] to sRGB [0, 1]."""
- return torch.where(
- x <= 0.0031308,
- 12.92 * x,
- 1.055 * torch.pow(x.clamp(min=0.0031308), 1.0 / 2.4) - 0.055,
- ).clamp(0.0, 1.0)
-
-
-# ---------------------------------------------------------------------------
-# ComfyUI Node
-# ---------------------------------------------------------------------------
-
-
-@comfy_node(name="LTXVHDRDecodePostprocess")
-class LTXVHDRDecodePostprocess:
- """Decompress HDR from VAE output, tonemap for preview, optionally save EXR.
-
- Place after VAE Decode in an HDR IC-LoRA workflow. Recovers linear HDR
- values from the compressed latent space and tonemaps them to SDR for
- display. When ``save_exr`` is enabled, also writes the raw linear HDR
- frames as an EXR image sequence.
- make sure to set OPENCV_IO_ENABLE_OPENEXR=1 environment in the command line # Must be set before cv2 import
-
-
- Outputs:
- tonemapped: SDR preview [0, 1] after Reinhard tonemap + sRGB gamma.
- hdr_linear: Raw linear HDR values [0, inf) for further processing.
- """
-
- @classmethod
- def INPUT_TYPES(s):
- return {
- "required": {
- "image": ("IMAGE",),
- },
- "optional": {
- "exposure": (
- "FLOAT",
- {
- "default": 0.0,
- "min": -10.0,
- "max": 10.0,
- "step": 0.1,
- "display": "slider",
- "tooltip": (
- "Exposure in stops (EV). 0 = no change, "
- "+1 = 2x brighter, -1 = half brightness."
- ),
- },
- ),
- "save_exr": (
- "BOOLEAN",
- {
- "default": False,
- "tooltip": "Save raw linear HDR frames as EXR sequence.",
- },
- ),
- "output_dir": (
- "STRING",
- {
- "default": "output/hdr_exr",
- "tooltip": (
- "Directory for EXR frames (relative to ComfyUI "
- "output directory, or absolute path)."
- ),
- },
- ),
- "filename_prefix": (
- "STRING",
- {"default": "frame"},
- ),
- "half_precision": (
- "BOOLEAN",
- {
- "default": True,
- "tooltip": (
- "Save EXR as float16 (half). Smaller files, "
- "negligible quality loss for most workflows."
- ),
- },
- ),
- },
- }
-
- RETURN_TYPES = ("IMAGE", "IMAGE")
- RETURN_NAMES = ("tonemapped", "hdr_linear")
- OUTPUT_NODE = True
- FUNCTION = "postprocess"
- CATEGORY = "Lightricks/HDR"
- DESCRIPTION = (
- "Decompresses VAE-decoded output from HDR IC-LoRA (LogC3) and applies "
- "Reinhard tonemapping. Place after VAE Decode. 'tonemapped' is the "
- "SDR preview; 'hdr_linear' is raw linear HDR for downstream use. "
- "Enable 'save_exr' to write an EXR image sequence."
- "if save_exr is enabled, make sure to set OPENCV_IO_ENABLE_OPENEXR=1 environment in the command line"
- )
-
- def postprocess(
- self,
- image: torch.Tensor,
- exposure: float = 0.0,
- save_exr: bool = False,
- output_dir: str = "output/hdr_exr",
- filename_prefix: str = "frame",
- half_precision: bool = True,
- ) -> tuple:
-
- hdr = _hdr_decompress(image)
- hdr = torch.clamp(hdr, min=0.0, max=1e4)
-
- # Reinhard tonemap with exposure (stops → linear multiplier)
- exposure_mult = 2.0**exposure
- hdr_exposed = hdr * exposure_mult
- tonemapped_linear = (hdr_exposed / (1.0 + hdr_exposed)).clamp(0.0, 1.0)
- tonemapped = _linear_to_srgb(tonemapped_linear)
-
- if save_exr:
- assert os.environ.get("OPENCV_IO_ENABLE_OPENEXR") == "1", (
- "EXR output is enabled (save_exr = TRUE), but OpenCV does not support EXR by default. "
- "To enable it, set the environment variable OPENCV_IO_ENABLE_OPENEXR=1 before starting ComfyUI, then restart. "
- "Alternatively, disable EXR output or switch to PNG/JPG."
- )
- self._save_exr_frames(hdr, output_dir, filename_prefix, half_precision)
-
- return (tonemapped, hdr)
-
- @staticmethod
- def _save_exr_frames(
- hdr_image: torch.Tensor,
- output_dir: str,
- filename_prefix: str,
- half_precision: bool,
- ) -> None:
- try:
- import cv2
- except ImportError:
- logger.error(
- "opencv-python is required for EXR export. "
- "Install with: pip install opencv-python"
- )
- return
-
- import folder_paths
- import numpy as np
-
- if not os.path.isabs(output_dir):
- output_dir = os.path.join(folder_paths.get_output_directory(), output_dir)
-
- os.makedirs(output_dir, exist_ok=True)
-
- frames = hdr_image.cpu().numpy()
- exr_type = (
- cv2.IMWRITE_EXR_TYPE_HALF if half_precision else cv2.IMWRITE_EXR_TYPE_FLOAT
- )
- params = [
- cv2.IMWRITE_EXR_TYPE,
- exr_type,
- cv2.IMWRITE_EXR_COMPRESSION,
- cv2.IMWRITE_EXR_COMPRESSION_ZIP,
- ]
-
- for i in range(frames.shape[0]):
- frame_bgr = frames[i][:, :, ::-1].astype(np.float32).copy()
- path = os.path.join(output_dir, f"{filename_prefix}_{i:05d}.exr")
- cv2.imwrite(path, frame_bgr, params)
-
- logger.info(
- "Saved %d EXR frame(s) to %s (%s)",
- frames.shape[0],
- output_dir,
- "float16" if half_precision else "float32",
- )
diff --git a/hdr_color.py b/hdr_color.py
new file mode 100644
index 0000000..aa1a93d
--- /dev/null
+++ b/hdr_color.py
@@ -0,0 +1,251 @@
+"""ACEScct working-space + primaries math for HDR Comfy paths.
+
+Kept in lockstep with ``ltx_core.hdr`` / ``ltx_core.color.primaries`` on
+ltx-2-internal: ACEScct curve constants match ``ltx_core.hdr``, and primaries
+matrices / EXR chromaticities come from ``colour-science`` with Bradford CAT
+(same source OpenColorIO-Config-ACES uses for its utility CLFs).
+
+``colour-science`` is required (same as ``ltx_core.color.primaries``).
+
+Used by HDR Comfy nodes in ``hdr_nodes`` (EXR ↔ VAE, SDR→HDR IC-LoRA ingest
+via ``SDRColorSpace`` / sRGB EOTF → ACEScct). File I/O helpers live in ``hdr_io``.
+"""
+
+from __future__ import annotations
+
+import enum
+import logging
+
+import colour
+import torch
+from torch import Tensor
+
+logger = logging.getLogger("LTXVideo.hdr_color")
+
+# ACEScct (AMPAS S-2016-001) — same constants as ``ltx_core.hdr``
+_ACESCCT_A_LIN = 10.5402377416545
+_ACESCCT_B_LIN = 0.0729055341958355
+_ACESCCT_X_BRK = 0.0078125
+_ACESCCT_Y_BRK = 0.155251141552511
+_ACESCCT_LOG_M = 17.52
+_ACESCCT_LOG_B = 9.72
+
+_CAT = "Bradford"
+_CS_ACESCG = colour.RGB_COLOURSPACES["ACEScg"]
+_CS_REC709 = colour.RGB_COLOURSPACES["ITU-R BT.709"]
+_CS_REC2020 = colour.RGB_COLOURSPACES["ITU-R BT.2020"]
+
+
+def _rgb_to_rgb(src: colour.RGB_Colourspace, dst: colour.RGB_Colourspace) -> Tensor:
+ """Linear RGB→RGB matrix from colour-science, as float32."""
+ return torch.as_tensor(
+ colour.matrix_RGB_to_RGB(src, dst, chromatic_adaptation_transform=_CAT),
+ dtype=torch.float32,
+ )
+
+
+def _exr_chroma(cs: colour.RGB_Colourspace) -> tuple[float, ...]:
+ """EXR ``chromaticities`` order: Rxy, Gxy, Bxy, Wxy."""
+ r, g, b = cs.primaries
+ w = cs.whitepoint
+ return (
+ float(r[0]),
+ float(r[1]),
+ float(g[0]),
+ float(g[1]),
+ float(b[0]),
+ float(b[1]),
+ float(w[0]),
+ float(w[1]),
+ )
+
+
+_ACESCG_TO_SRGB = _rgb_to_rgb(_CS_ACESCG, _CS_REC709)
+_SRGB_TO_ACESCG = torch.linalg.inv(_ACESCG_TO_SRGB.double()).to(torch.float32)
+_REC709_TO_2020 = _rgb_to_rgb(_CS_REC709, _CS_REC2020)
+_ACESCG_TO_2020 = _rgb_to_rgb(_CS_ACESCG, _CS_REC2020)
+_CHROMA_REC709 = _exr_chroma(_CS_REC709)
+_CHROMA_ACESCG = _exr_chroma(_CS_ACESCG)
+_MATRICES_SOURCE = "colour-science"
+
+# Public aliases (parity with ``ltx_core.color.primaries`` exports).
+ACESCG_TO_SRGB = _ACESCG_TO_SRGB
+SRGB_TO_ACESCG = _SRGB_TO_ACESCG
+REC709_TO_2020 = _REC709_TO_2020
+ACESCG_TO_2020 = _ACESCG_TO_2020
+
+_CHROMATICITIES = {
+ "rec709": _CHROMA_REC709,
+ "acescg": _CHROMA_ACESCG,
+}
+
+
+class Primaries(enum.Enum):
+ """Authoring / working-space colour primaries (linear light)."""
+
+ REC709 = "rec709"
+ ACESCG = "acescg"
+
+ @property
+ def exr_chromaticities(self) -> tuple[float, ...]:
+ """EXR ``chromaticities`` header (R/G/B/W x,y)."""
+ return _CHROMATICITIES[self.value]
+
+ @property
+ def matrix_to_rec2020(self) -> Tensor:
+ """3x3 linear map from this basis to Rec.2020 (for HLG)."""
+ return _REC709_TO_2020 if self is Primaries.REC709 else _ACESCG_TO_2020
+
+ def to(self, target: Primaries, video: Tensor) -> Tensor:
+ """Convert linear light from this basis into ``target``. Identity when same."""
+ if self is target:
+ return video
+ if (self, target) == (Primaries.REC709, Primaries.ACESCG):
+ return _apply_matrix(video, _SRGB_TO_ACESCG)
+ if (self, target) == (Primaries.ACESCG, Primaries.REC709):
+ return _apply_matrix(video, _ACESCG_TO_SRGB)
+ raise ValueError(f"No primaries conversion from {self!r} to {target!r}.")
+
+ def to_rec2020(self, video: Tensor) -> Tensor:
+ """Linear light in this basis → Rec.2020 (HLG master space)."""
+ return _apply_matrix(video, self.matrix_to_rec2020)
+
+
+class HDRColorSpace(enum.Enum):
+ """Source / EXR authoring colour space (matches pipelines ``--hdr``)."""
+
+ SRGB_LINEAR = "srgb_linear"
+ ACESCG = "acescg"
+ ACESCCT = "acescct"
+
+ @property
+ def is_log_working(self) -> bool:
+ return self is HDRColorSpace.ACESCCT
+
+ @property
+ def source_primaries(self) -> Primaries:
+ if self in (HDRColorSpace.ACESCG, HDRColorSpace.ACESCCT):
+ return Primaries.ACESCG
+ return Primaries.REC709
+
+ def exr_output_tags(self) -> tuple[Primaries, str]:
+ """``(primaries, colorSpace tag)`` for EXR headers."""
+ if self is HDRColorSpace.ACESCCT:
+ return Primaries.ACESCG, "ACEScct"
+ if self is HDRColorSpace.ACESCG:
+ return Primaries.ACESCG, "ACEScg"
+ return Primaries.REC709, "sRGB"
+
+
+def _apply_matrix(video: Tensor, mat: Tensor) -> Tensor:
+ """Apply 3x3 on channel-last ``[..., H, W, 3]`` or channel-first ``[..., 3, H, W]``.
+
+ Comfy IMAGE tensors are channel-last; ``ltx_core`` often uses channel-first.
+ """
+ mat = mat.to(device=video.device, dtype=video.dtype)
+ if video.shape[-1] == 3:
+ return video @ mat.T
+ if video.shape[-3] == 3:
+ return torch.einsum("...chw,dc->...dhw", video, mat)
+ raise ValueError(f"Expected RGB in last or -3 dim; got shape {tuple(video.shape)}")
+
+
+# sRGB EOTF (IEC 61966-2-1) — same constants as ``ltx_core.hdr``.
+_SRGB_A = 0.055
+_SRGB_LINEAR_THRESHOLD = 0.04045
+_SRGB_LINEAR_SLOPE = 12.92
+_SRGB_GAMMA = 2.4
+
+
+def compress_acescct(hdr: Tensor) -> Tensor:
+ """Linear HDR [0, ∞) → ACEScct [0, 1]."""
+ x = torch.clamp(hdr, min=0.0)
+ log_part = (torch.log2(torch.clamp(x, min=1e-12)) + _ACESCCT_LOG_B) / _ACESCCT_LOG_M
+ lin_part = _ACESCCT_A_LIN * x + _ACESCCT_B_LIN
+ return torch.clamp(torch.where(x > _ACESCCT_X_BRK, log_part, lin_part), 0.0, 1.0)
+
+
+def decompress_acescct(ct: Tensor) -> Tensor:
+ """ACEScct [0, 1] → linear ACEScg [0, ∞)."""
+ ct = torch.clamp(ct, 0.0, 1.0)
+ lin_from_log = torch.pow(2.0, ct * _ACESCCT_LOG_M - _ACESCCT_LOG_B)
+ lin_from_lin = (ct - _ACESCCT_B_LIN) / _ACESCCT_A_LIN
+ return torch.where(ct > _ACESCCT_Y_BRK, lin_from_log, lin_from_lin)
+
+
+def srgb_eotf_to_linear(srgb_01: Tensor) -> Tensor:
+ """sRGB-encoded [0, 1] → display-linear Rec.709/sRGB (IEC 61966-2-1 EOTF)."""
+ x = torch.clamp(srgb_01.float(), 0.0, 1.0)
+ return torch.where(
+ x <= _SRGB_LINEAR_THRESHOLD,
+ x / _SRGB_LINEAR_SLOPE,
+ torch.pow((x + _SRGB_A) / (1.0 + _SRGB_A), _SRGB_GAMMA),
+ )
+
+
+def to_acescct_working_space(linear_rgb: Tensor, source_primaries: Primaries) -> Tensor:
+ """Scene-linear RGB → ACEScct [0, 1] (channel-last or channel-first)."""
+ native = source_primaries.to(Primaries.ACESCG, linear_rgb.float())
+ return compress_acescct(native.clamp(min=0.0))
+
+
+def acescct_to_linear(
+ working: Tensor, out_primaries: Primaries = Primaries.REC709
+) -> Tensor:
+ """ACEScct [0, 1] → scene-linear in ``out_primaries``."""
+ linear_acescg = decompress_acescct(working.float())
+ return Primaries.ACESCG.to(out_primaries, linear_acescg).clamp(min=0.0)
+
+
+class SDRColorSpace(enum.Enum):
+ """SDR ingest colour space for mapping float RGB into ACEScct.
+
+ Matches pipelines ``SDRColorSpace`` / ``hdr_ic_lora`` IDT:
+
+ * ``srgb_gamma`` — display-referred sRGB (apply EOTF, then ACEScct).
+ * ``srgb`` — scene-linear Rec.709 / sRGB primaries (no EOTF).
+ * ``acescg`` — scene-linear ACEScg → ACEScct.
+ * ``acescct`` — already ACEScct log codes; clamp passthrough.
+ """
+
+ SRGB = "srgb"
+ SRGB_GAMMA = "srgb_gamma"
+ ACESCG = "acescg"
+ ACESCCT = "acescct"
+
+ def to_working_space(self, video: Tensor) -> Tensor:
+ """Map SDR float RGB into ACEScct ``[0, 1]`` (channel-last or channel-first)."""
+ if not video.is_floating_point():
+ raise ValueError(
+ f"SDRColorSpace.to_working_space expects float RGB; got dtype={video.dtype}. "
+ "Normalize integer pixels before calling."
+ )
+ video = video.float()
+ if self is SDRColorSpace.ACESCCT:
+ return video.clamp(0.0, 1.0)
+ if self is SDRColorSpace.SRGB_GAMMA:
+ return to_acescct_working_space(
+ srgb_eotf_to_linear(video), Primaries.REC709
+ )
+ if self is SDRColorSpace.SRGB:
+ return to_acescct_working_space(video, Primaries.REC709)
+ if self is SDRColorSpace.ACESCG:
+ return to_acescct_working_space(video, Primaries.ACESCG)
+ raise ValueError(f"Unsupported SDR color space: {self!r}")
+
+
+def images_to_acescct(images: Tensor, color_space: HDRColorSpace) -> Tensor:
+ """Convert loaded EXR frames ``[F,H,W,3]`` into ACEScct ``[0,1]`` for the VAE."""
+ images = images.float()
+ if color_space.is_log_working:
+ return images.clamp(0.0, 1.0)
+ return to_acescct_working_space(images, color_space.source_primaries)
+
+
+def sdr_images_to_acescct(images: Tensor, color_space: SDRColorSpace) -> Tensor:
+ """Convert SDR frames ``[F,H,W,C]`` into ACEScct ``[0,1]`` for HDR IC-LoRA."""
+ rgb = images[..., :3]
+ working = color_space.to_working_space(rgb)
+ if images.shape[-1] > 3:
+ return torch.cat([working, images[..., 3:].float()], dim=-1)
+ return working
diff --git a/hdr_io.py b/hdr_io.py
new file mode 100644
index 0000000..173e32f
--- /dev/null
+++ b/hdr_io.py
@@ -0,0 +1,394 @@
+"""HDR file I/O helpers: EXR read/write, HLG encode, path utilities.
+
+Shared by Comfy HDR nodes (ingest + delivery). Color math lives in ``hdr_color``;
+node classes live in ``hdr_nodes``.
+"""
+
+from __future__ import annotations
+
+import logging
+import math
+import os
+import re
+from pathlib import Path
+
+import folder_paths
+import numpy as np
+import torch
+from torch import Tensor
+
+from .hdr_color import Primaries
+
+logger = logging.getLogger("LTXVideo.hdr_io")
+
+# BT.2020 / HLG (ARIB STD-B67) encode constants — from ltx_core.color.hlg
+_HLG_A, _HLG_B, _HLG_C = 0.17883277, 0.28466892, 0.55991073
+_AV_COLOR_PRIMARIES_BT2020 = 9
+_AV_COLOR_TRC_ARIB_STD_B67 = 18
+_AV_COLORSPACE_BT2020_NCL = 9
+_AV_COLOR_RANGE_MPEG = 1
+_KR_2020, _KG_2020, _KB_2020 = 0.2627, 0.6780, 0.0593
+_BT2020_RGB_TO_YUV = torch.tensor(
+ [
+ [_KR_2020, _KG_2020, _KB_2020],
+ [-_KR_2020 / 1.8814, -_KG_2020 / 1.8814, 0.5],
+ [0.5, -_KG_2020 / 1.4746, -_KB_2020 / 1.4746],
+ ],
+ dtype=torch.float32,
+)
+
+
+def _resolve_path(path: str) -> Path:
+ p = Path(path).expanduser()
+ if p.is_absolute() and p.exists():
+ return p
+ # Prefer Comfy input dir for relative paths.
+ candidate = Path(folder_paths.get_input_directory()) / path
+ if candidate.exists():
+ return candidate
+ if p.exists():
+ return p.resolve()
+ raise FileNotFoundError(
+ f"HDR path not found: {path!r} (tried {candidate} and {p.resolve()})"
+ )
+
+
+def _natural_sort_key(name: str) -> tuple:
+ """Sort key so ``frame_9.exr`` precedes ``frame_10.exr`` (not lexicographic)."""
+ parts = re.split(r"(\d+)", name)
+ key: list = []
+ for part in parts:
+ if part.isdigit():
+ key.append((0, int(part)))
+ else:
+ key.append((1, part.casefold()))
+ return tuple(key)
+
+
+def _list_exr_files(path: Path) -> list[Path]:
+ """Resolve a still ``.exr`` or an EXR folder to a sorted frame list."""
+ if path.is_file():
+ if path.suffix.lower() != ".exr":
+ raise ValueError(f"Expected a .exr file; got {path}")
+ return [path]
+ if path.is_dir():
+ # Case-insensitive match (Linux is case-sensitive; macOS usually is not).
+ files = sorted(
+ {p for p in path.iterdir() if p.is_file() and p.suffix.lower() == ".exr"},
+ key=lambda p: _natural_sort_key(p.name),
+ )
+ if not files:
+ raise RuntimeError(f"No .exr frames in {path}")
+ return files
+ raise ValueError(f"Path must be a .exr file or a directory of EXRs; got {path}")
+
+
+def _silent_audio(num_frames: int, frame_rate: float, sample_rate: int = 44100) -> dict:
+ """Stereo silence long enough for the LTX audio VAE mel path.
+
+ EXR plates have no soundtrack; V2V still freezes an audio latent. A 0-length
+ waveform crashes ``torchaudio.functional.resample`` / ``VAEEncodeAudio``.
+ """
+ if frame_rate <= 0:
+ raise ValueError(f"frame_rate must be > 0; got {frame_rate}")
+ duration_s = max(float(num_frames) / float(frame_rate), 1.0)
+ # Keep a comfortable floor for mel/STFT (typical n_fft is 1024–2048).
+ n_samples = max(int(math.ceil(duration_s * sample_rate)), sample_rate, 8192)
+ return {
+ "waveform": torch.zeros(1, 2, n_samples, dtype=torch.float32),
+ "sample_rate": int(sample_rate),
+ }
+
+
+def _read_exr(path: Path) -> Tensor:
+ """Read one EXR as float32 ``[H,W,3]`` (unbounded linear or log codes as stored).
+
+ Requires OpenImageIO (same as ``ltx_pipelines`` media I/O). No OpenCV fallback.
+ """
+ try:
+ import OpenImageIO as oiio
+ except ImportError as e:
+ raise RuntimeError(
+ "Reading EXR requires OpenImageIO. Install with: pip install openimageio"
+ ) from e
+
+ buf = oiio.ImageBuf(str(path))
+ pixels = buf.get_pixels(oiio.FLOAT)
+ if pixels is None or buf.has_error:
+ raise RuntimeError(f"OpenImageIO failed on '{path}': {buf.geterror()}")
+ arr = np.ascontiguousarray(np.asarray(pixels, dtype=np.float32))
+
+ if arr.ndim == 2:
+ arr = np.repeat(arr[..., None], 3, axis=-1)
+ return torch.from_numpy(np.ascontiguousarray(arr[..., :3]))
+
+
+def _save_exr_frame(
+ frame_hwc: Tensor,
+ path: Path,
+ *,
+ half_precision: bool,
+ primaries: Primaries | None,
+ color_space_tag: str | None,
+) -> None:
+ """Write one EXR via OpenImageIO (same backend as ``ltx_pipelines``).
+
+ Optional ``primaries`` / ``color_space_tag`` set EXR header attributes when
+ provided; ``exr_color_space=linear`` may omit them for legacy untagged plates.
+ """
+ try:
+ import OpenImageIO as oiio
+ except ImportError as e:
+ raise RuntimeError(
+ "Saving EXR requires OpenImageIO. Install with: pip install openimageio"
+ ) from e
+
+ rgb = frame_hwc.detach().float().cpu().numpy()
+ path.parent.mkdir(parents=True, exist_ok=True)
+ h, w, _ = rgb.shape
+ fmt = oiio.HALF if half_precision else oiio.FLOAT
+ spec = oiio.ImageSpec(w, h, 3, fmt)
+ spec.channelnames = ("R", "G", "B")
+ spec.attribute("compression", "zip")
+ if primaries is not None:
+ spec.attribute(
+ "chromaticities",
+ "float[8]",
+ primaries.exr_chromaticities,
+ )
+ if color_space_tag is not None:
+ spec.attribute("colorSpace", color_space_tag)
+ out = oiio.ImageOutput.create(str(path))
+ if out is None:
+ raise RuntimeError(
+ f"OpenImageIO cannot create writer for {path}. "
+ "Ensure OpenImageIO is built with OpenEXR support."
+ )
+ try:
+ if not out.open(str(path), spec):
+ raise RuntimeError(
+ f"Failed to access EXR output file at '{path}': {out.geterror()}"
+ )
+ # OIIO converts to HALF from float32 buffer when spec is HALF.
+ if not out.write_image(np.ascontiguousarray(rgb, dtype=np.float32)):
+ raise RuntimeError(f"Failed to write EXR '{path}': {out.geterror()}")
+ finally:
+ out.close()
+
+
+def _hlg_inverse_oetf_scalar(v: float) -> float:
+ return (
+ (v * v) / 3.0 if v <= 0.5 else (math.exp((v - _HLG_C) / _HLG_A) + _HLG_B) / 12.0
+ )
+
+
+def _linear_to_hlg_signal(
+ rgb_fhwc: Tensor,
+ primaries: Primaries = Primaries.REC709,
+ white_signal: float = 0.75,
+) -> Tensor:
+ """Scene-linear RGB ``[F,H,W,3]`` in ``primaries`` → HLG signal in BT.2020.
+
+ Maps with ``primaries.matrix_to_rec2020`` then clamps (same as
+ ``ltx_core.color.hlg.HlgGpuConverter``). Prefer ACEScg→Rec.2020 directly
+ over ACEScg→Rec.709→Rec.2020 so OOG negatives are not crushed early.
+ """
+ white_x = _hlg_inverse_oetf_scalar(white_signal)
+ roll_k = white_x / (1.0 - white_x)
+ mat = primaries.matrix_to_rec2020.to(device=rgb_fhwc.device, dtype=rgb_fhwc.dtype)
+ lin = torch.nan_to_num((rgb_fhwc @ mat.T).clamp(min=0.0), nan=0.0, neginf=0.0)
+ x = torch.where(
+ lin <= 1.0,
+ lin * white_x,
+ 1.0 - (1.0 - white_x) * torch.exp(-roll_k * (lin - 1.0)),
+ )
+ sig = torch.where(
+ x <= 1.0 / 12.0,
+ torch.sqrt((3.0 * x).clamp(min=0.0)),
+ _HLG_A * torch.log((12.0 * x - _HLG_B).clamp(min=1e-12)) + _HLG_C,
+ )
+ return sig.clamp(0.0, 1.0)
+
+
+def _rgb_hlg_to_yuv420p10(
+ rgb_fhwc: Tensor,
+) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
+ """HLG RGB ``[F,H,W,3]`` → planar uint16 Y,U,V (limited / BT.2020)."""
+ if rgb_fhwc.shape[-2] % 2 or rgb_fhwc.shape[-3] % 2:
+ raise ValueError(f"HLG encode needs even H and W; got {tuple(rgb_fhwc.shape)}")
+ rgb = rgb_fhwc.movedim(-1, -3).contiguous() # F,3,H,W
+ mat = _BT2020_RGB_TO_YUV.to(device=rgb.device, dtype=rgb.dtype)
+ pixels = rgb.movedim(-3, -1)
+ yuv = pixels.flatten(-3, -2) @ mat.T
+ yuv = yuv.unflatten(-2, rgb.shape[-2:]).movedim(-1, -3)
+ y = yuv[:, :1]
+ uv = torch.nn.functional.avg_pool2d(
+ yuv[:, 1:3].contiguous(), kernel_size=2, stride=2
+ )
+ # MPEG limited 10-bit
+ y = y.mul(219 * 4).add_(16 * 4)
+ uv = uv.mul(224 * 4).add_(128 * 4)
+ y16 = y[:, 0].round_().clamp_(0, 1023).to(torch.uint16).cpu().numpy()
+ u16 = uv[:, 0].round_().clamp_(0, 1023).to(torch.uint16).cpu().numpy()
+ v16 = uv[:, 1].round_().clamp_(0, 1023).to(torch.uint16).cpu().numpy()
+ return y16, u16, v16
+
+
+def _encode_hlg_mp4(
+ frames_linear: Tensor,
+ out_path: Path,
+ fps: float,
+ audio: dict | None = None,
+ *,
+ primaries: Primaries = Primaries.REC709,
+) -> None:
+ """Encode scene-linear ``[F,H,W,3]`` (in ``primaries``) to BT.2020/HLG/10-bit HEVC.
+
+ Optional Comfy ``AUDIO`` dict ``{waveform, sample_rate}`` is muxed as AAC.
+ The AAC stream is registered *before* video packets (PyAV requires this).
+ """
+ from fractions import Fraction
+
+ import av
+
+ hlg = _linear_to_hlg_signal(frames_linear.float(), primaries=primaries)
+ y, u, v = _rgb_hlg_to_yuv420p10(hlg)
+ f, height, width = int(y.shape[0]), int(y.shape[1]), int(y.shape[2])
+ out_path.parent.mkdir(parents=True, exist_ok=True)
+
+ container = av.open(str(out_path), mode="w", options={"movflags": "+faststart"})
+ audio_stream = None
+ try:
+ stream = container.add_stream(
+ "libx265", rate=Fraction(fps).limit_denominator(1000)
+ )
+ stream.width = width
+ stream.height = height
+ stream.pix_fmt = "yuv420p10le"
+ stream.codec_tag = "hvc1"
+ threads = max(1, min(os.cpu_count() or 8, 16))
+ stream.options = {
+ "crf": "12",
+ "preset": "medium",
+ "x265-params": (
+ "colorprim=bt2020:transfer=arib-std-b67:colormatrix=bt2020nc:range=limited:"
+ f"repeat-headers=1:info=0:pools={threads}:frame-threads=4"
+ ),
+ }
+ ctx = stream.codec_context
+ ctx.thread_count = threads
+ ctx.color_primaries = _AV_COLOR_PRIMARIES_BT2020
+ ctx.color_trc = _AV_COLOR_TRC_ARIB_STD_B67
+ ctx.colorspace = _AV_COLORSPACE_BT2020_NCL
+ ctx.color_range = _AV_COLOR_RANGE_MPEG
+
+ if audio is not None:
+ audio_stream = _prepare_aac_stream(container, audio)
+
+ for i in range(f):
+ frame = av.VideoFrame(width, height, "yuv420p10le")
+ for plane, src in zip(frame.planes, (y[i], u[i], v[i]), strict=True):
+ dest = np.frombuffer(plane, dtype=np.uint16).reshape(
+ plane.height, plane.line_size // 2
+ )
+ dest[:, : src.shape[1]] = src
+ frame.colorspace = _AV_COLORSPACE_BT2020_NCL
+ frame.color_range = _AV_COLOR_RANGE_MPEG
+ for packet in stream.encode(frame):
+ container.mux(packet)
+ for packet in stream.encode():
+ container.mux(packet)
+
+ if audio is not None and audio_stream is not None:
+ _mux_comfy_audio_aac(container, audio_stream, audio)
+ finally:
+ container.close()
+
+
+def _comfy_audio_stereo_n2(audio: dict) -> tuple[Tensor, int]:
+ """Return stereo int16-ready float samples ``[N, 2]`` and sample rate."""
+ waveform = audio.get("waveform")
+ sample_rate = int(audio.get("sample_rate", 0) or 0)
+ if waveform is None or sample_rate <= 0:
+ raise ValueError("AUDIO must include waveform and sample_rate")
+
+ samples = waveform.detach().float().cpu()
+ if samples.ndim == 3:
+ samples = samples[0]
+ if samples.ndim != 2:
+ raise ValueError(
+ f"Expected AUDIO waveform [C,N] or [B,C,N]; got {tuple(waveform.shape)}"
+ )
+ if samples.shape[0] == 2:
+ samples_cn = samples
+ elif samples.shape[1] == 2:
+ samples_cn = samples.T
+ elif samples.shape[0] == 1:
+ samples_cn = samples.repeat(2, 1)
+ else:
+ raise ValueError(
+ f"AUDIO must be mono or stereo; got shape {tuple(samples.shape)}"
+ )
+ return samples_cn.T.contiguous(), sample_rate
+
+
+def _prepare_aac_stream(container, audio: dict):
+ """Register a stereo AAC stream before any packets are muxed."""
+ from fractions import Fraction
+
+ _, sample_rate = _comfy_audio_stereo_n2(audio)
+ audio_stream = container.add_stream("aac", rate=sample_rate)
+ audio_stream.codec_context.sample_rate = sample_rate
+ audio_stream.codec_context.layout = "stereo"
+ audio_stream.codec_context.time_base = Fraction(1, sample_rate)
+ return audio_stream
+
+
+def _mux_comfy_audio_aac(container, audio_stream, audio: dict) -> None:
+ """Mux Comfy AUDIO onto an already-added AAC stream (after video flush)."""
+ import av
+
+ samples_n2, sample_rate = _comfy_audio_stereo_n2(audio)
+ samples_i16 = (samples_n2.clamp(-1.0, 1.0) * 32767.0).to(torch.int16)
+
+ frame_in = av.AudioFrame.from_ndarray(
+ samples_i16.reshape(1, -1).numpy(),
+ format="s16",
+ layout="stereo",
+ )
+ frame_in.sample_rate = sample_rate
+
+ cc = audio_stream.codec_context
+ resampler = av.audio.resampler.AudioResampler(
+ format=cc.format or "fltp",
+ layout=cc.layout or "stereo",
+ rate=cc.sample_rate or sample_rate,
+ )
+ next_pts = 0
+ for rframe in resampler.resample(frame_in):
+ if rframe.pts is None:
+ rframe.pts = next_pts
+ next_pts += rframe.samples
+ for packet in audio_stream.encode(rframe):
+ container.mux(packet)
+ for rframe in resampler.resample(None):
+ if rframe.pts is None:
+ rframe.pts = next_pts
+ next_pts += rframe.samples
+ for packet in audio_stream.encode(rframe):
+ container.mux(packet)
+ for packet in audio_stream.encode(None):
+ container.mux(packet)
+
+
+def _trim_to_8k1(images: Tensor) -> Tensor:
+ """Keep longest legal ``8k+1`` prefix (VAE temporal scale 8)."""
+ n = images.shape[0]
+ legal = ((n - 1) // 8) * 8 + 1
+ if legal < 1:
+ raise ValueError(f"Need at least 1 frame; got {n}")
+ if legal != n:
+ logger.warning(
+ "Trimmed EXR sequence from %d to %d frames (must be 8k+1)", n, legal
+ )
+ return images[:legal]
diff --git a/hdr_nodes.py b/hdr_nodes.py
new file mode 100644
index 0000000..747c118
--- /dev/null
+++ b/hdr_nodes.py
@@ -0,0 +1,675 @@
+"""ComfyUI HDR nodes: ingest, decode postprocess, and delivery.
+
+Stage-oriented layout (shared ACEScct working space):
+
+- Ingest: ``LTXVSDRToHDRWorkingSpace``, ``LTXVLoadEXRSequence``
+- Runtime: ``LTXVVAEForceFloat32``
+- Delivery: ``LTXVHDRDecodePostprocess``, ``LTXVSaveHLG``
+
+Color math: ``hdr_color``. File I/O helpers: ``hdr_io``.
+"""
+
+from __future__ import annotations
+
+import logging
+import os
+from pathlib import Path
+
+import folder_paths
+import torch
+from torch import Tensor
+
+from .hdr_color import (
+ HDRColorSpace,
+ Primaries,
+ SDRColorSpace,
+ compress_acescct,
+ images_to_acescct,
+ sdr_images_to_acescct,
+)
+from .hdr_io import (
+ _encode_hlg_mp4,
+ _list_exr_files,
+ _read_exr,
+ _resolve_path,
+ _save_exr_frame,
+ _silent_audio,
+ _trim_to_8k1,
+)
+from .nodes_registry import comfy_node
+
+logger = logging.getLogger("LTXVideo.hdr_nodes")
+
+_EXR_COLOR_SPACE_CHOICES = ["linear", "acescct", "srgb_linear", "acescg"]
+_SDR_COLOR_SPACE_CHOICES = [c.value for c in SDRColorSpace]
+_COLOR_SPACE_CHOICES = [c.value for c in HDRColorSpace]
+
+
+class LogC3:
+ """ARRI LogC3 (EI 800) HDR compression.
+
+ Maps linear [0, inf) -> [0, 1] via the camera log curve, then scales to
+ [-1, 1] for VAE input.
+ """
+
+ A = 5.555556
+ B = 0.052272
+ C = 0.247190
+ D = 0.385537
+ E = 5.367655
+ F = 0.092809
+ CUT = 0.010591
+
+ def compress(self, hdr: Tensor) -> Tensor:
+ x = torch.clamp(hdr, min=0.0)
+ log_part = self.C * torch.log10(self.A * x + self.B) + self.D
+ lin_part = self.E * x + self.F
+ logc = torch.where(x >= self.CUT, log_part, lin_part)
+ logc = torch.clamp(logc, 0.0, 1.0)
+ return logc * 2.0 - 1.0
+
+ def decompress(self, z: Tensor) -> Tensor:
+ logc = torch.clamp((z + 1.0) / 2.0, 0.0, 1.0)
+ cut_log = self.E * self.CUT + self.F
+ lin_from_log = (torch.pow(10.0, (logc - self.D) / self.C) - self.B) / self.A
+ lin_from_lin = (logc - self.F) / self.E
+ return torch.where(logc >= cut_log, lin_from_log, lin_from_lin)
+
+
+# ---------------------------------------------------------------------------
+# Transform registry — singleton (stateless)
+# ---------------------------------------------------------------------------
+
+_LOGC3 = LogC3()
+
+
+def _hdr_decompress(decoded_01: Tensor, transfer: str = "acescct") -> Tensor:
+ """Decompress VAE-decoded ``[0,1]`` image to linear HDR ``[0, inf)``.
+
+ ComfyUI's VAE decode returns images in ``[0, 1]`` via ``(raw + 1) / 2``.
+
+ * ``acescct`` — ACEScct working codes (SDR→HDR IC-LoRA + native HDR).
+ * ``logc3`` — legacy LogC3 SDR→HDR LoRA only.
+ """
+ transfer = (transfer or "acescct").lower()
+ if transfer == "acescct":
+ from .hdr_color import decompress_acescct
+
+ # Decoded IMAGE is already ACEScct working-space [0, 1].
+ return decompress_acescct(decoded_01.float())
+ if transfer != "logc3":
+ raise ValueError(
+ f"Unsupported HDR transfer {transfer!r}; use 'logc3' or 'acescct'."
+ )
+ raw = decoded_01.float() * 2.0 - 1.0
+ return _LOGC3.decompress(raw)
+
+
+def _linear_to_srgb(x: Tensor) -> Tensor:
+ """Convert linear [0, 1] to sRGB [0, 1]."""
+ return torch.where(
+ x <= 0.0031308,
+ 12.92 * x,
+ 1.055 * torch.pow(x.clamp(min=0.0031308), 1.0 / 2.4) - 0.055,
+ ).clamp(0.0, 1.0)
+
+
+# ---------------------------------------------------------------------------
+# ComfyUI Nodes
+# ---------------------------------------------------------------------------
+
+
+@comfy_node(name="LTXVSDRToHDRWorkingSpace")
+class LTXVSDRToHDRWorkingSpace:
+ """Map SDR float RGB into ACEScct ``[0, 1]`` for HDR IC-LoRA conditioning.
+ Place **before** resize when the source is display sRGB so
+ the EOTF is not applied to bilinear-filtered codes.
+
+ Default ``srgb_gamma`` matches typical ``LoadVideo`` / MP4 decode.
+ """
+
+ @classmethod
+ def INPUT_TYPES(s):
+ return {
+ "required": {
+ "image": ("IMAGE",),
+ "color_space": (
+ _SDR_COLOR_SPACE_CHOICES,
+ {
+ "default": "srgb_gamma",
+ "tooltip": (
+ "SDR ingest IDT. srgb_gamma = display sRGB (EOTF then "
+ "ACEScct); srgb = scene-linear Rec.709; acescg / acescct "
+ "for float EXR-style plates."
+ ),
+ },
+ ),
+ },
+ }
+
+ RETURN_TYPES = ("IMAGE",)
+ RETURN_NAMES = ("acescct",)
+ FUNCTION = "convert"
+ CATEGORY = "Lightricks/HDR"
+ DESCRIPTION = (
+ "SDR → ACEScct working space for HDR IC-LoRA (pipelines SDRColorSpace). "
+ "Wire LoadVideo images through this before resize / IC-LoRA guide."
+ )
+
+ def convert(self, image: torch.Tensor, color_space: str = "srgb_gamma") -> tuple:
+ space = SDRColorSpace(color_space)
+ return (sdr_images_to_acescct(image, space),)
+
+
+def _exr_payload_for_color_space(
+ image: Tensor,
+ hdr_linear: Tensor,
+ transfer: str,
+ exr_color_space: str,
+) -> tuple[Tensor, Primaries | None, str | None]:
+ """Build EXR RGB frames and optional primaries / colorSpace tags.
+
+ ``linear`` keeps the legacy 2.3 behaviour (raw decompressed buffer, no tags).
+ Other values match pipelines ``--hdr`` EXR authoring.
+ """
+ space = (exr_color_space or "linear").lower()
+ transfer = (transfer or "acescct").lower()
+ rgb_in = image[..., :3].float()
+ hdr = hdr_linear[..., :3].float()
+
+ if space == "linear":
+ return hdr, None, None
+
+ if space == "acescct":
+ if transfer == "acescct":
+ frames = rgb_in.clamp(0.0, 1.0)
+ else:
+ # LogC3 linear is treated as Rec.709 scene-linear for ACEScct authoring.
+ frames = compress_acescct(
+ Primaries.REC709.to(Primaries.ACESCG, hdr).clamp(min=0.0)
+ )
+ return frames, Primaries.ACESCG, "ACEScct"
+
+ if space == "acescg":
+ if transfer == "acescct":
+ frames = hdr # decompress_acescct → ACEScg linear
+ else:
+ frames = Primaries.REC709.to(Primaries.ACESCG, hdr).clamp(min=0.0)
+ return frames, Primaries.ACESCG, "ACEScg"
+
+ if space == "srgb_linear":
+ if transfer == "acescct":
+ frames = Primaries.ACESCG.to(Primaries.REC709, hdr).clamp(min=0.0)
+ else:
+ frames = hdr.clamp(min=0.0)
+ return frames, Primaries.REC709, "sRGB"
+
+ raise ValueError(
+ f"Unsupported exr_color_space {exr_color_space!r}; "
+ f"use one of {_EXR_COLOR_SPACE_CHOICES}."
+ )
+
+
+@comfy_node(name="LTXVHDRDecodePostprocess")
+class LTXVHDRDecodePostprocess:
+ """Decompress HDR from VAE output, tonemap for preview, optionally save EXR.
+
+ Place after VAE Decode. Recovers linear HDR from the VAE ``[0,1]`` image and
+ tonemaps to SDR for display. When ``save_exr`` is enabled, writes an EXR
+ sequence (legacy ``linear``, or tagged ``acescct`` / ``srgb_linear`` /
+ ``acescg``).
+
+ For BT.2020/HLG masters, wire ``hdr_linear`` into ``LTXVSaveHLG``.
+
+ Outputs:
+ tonemapped: SDR preview [0, 1] after Reinhard tonemap + sRGB gamma.
+ hdr_linear: Raw linear HDR values [0, inf) for further processing.
+ With ``transfer=acescct`` this is ACEScg scene-linear; with
+ ``logc3`` it is LogC3/Rec.709-style scene-linear.
+ """
+
+ @classmethod
+ def INPUT_TYPES(s):
+ return {
+ "required": {
+ "image": ("IMAGE",),
+ },
+ "optional": {
+ "transfer": (
+ ["acescct", "logc3"],
+ {
+ "default": "acescct",
+ "tooltip": (
+ "Working-space curve of the VAE decode. "
+ "Use acescct for SDR→HDR IC-LoRA (after "
+ "LTXVSDRToHDRWorkingSpace) and for native HDR EXR; "
+ "logc3 only for legacy LogC3 HDR LoRA checkpoints."
+ ),
+ },
+ ),
+ "exposure": (
+ "FLOAT",
+ {
+ "default": 0.0,
+ "min": -10.0,
+ "max": 10.0,
+ "step": 0.1,
+ "display": "slider",
+ "tooltip": (
+ "Exposure in stops (EV). 0 = no change, "
+ "+1 = 2x brighter, -1 = half brightness. "
+ "Affects the tonemapped preview only."
+ ),
+ },
+ ),
+ "save_exr": (
+ "BOOLEAN",
+ {
+ "default": False,
+ "tooltip": "Save HDR frames as an EXR sequence.",
+ },
+ ),
+ "exr_color_space": (
+ _EXR_COLOR_SPACE_CHOICES,
+ {
+ "default": "acescct",
+ "tooltip": (
+ "EXR contents when save_exr is on. "
+ "Default 'acescct' matches pipelines hdr_ic_lora "
+ "ACEScct log masters. 'acescg' / 'srgb_linear' are "
+ "tagged linear; 'linear' is legacy raw decompress "
+ "(untagged). All paths use OpenImageIO."
+ ),
+ },
+ ),
+ "output_dir": (
+ "STRING",
+ {
+ "default": "output/hdr_exr",
+ "tooltip": (
+ "Directory for EXR frames (relative to ComfyUI "
+ "output directory, or absolute path)."
+ ),
+ },
+ ),
+ "filename_prefix": (
+ "STRING",
+ {"default": "frame"},
+ ),
+ "half_precision": (
+ "BOOLEAN",
+ {
+ "default": True,
+ "tooltip": (
+ "Save EXR as float16 (half). Smaller files, "
+ "negligible quality loss for most workflows."
+ ),
+ },
+ ),
+ },
+ }
+
+ RETURN_TYPES = ("IMAGE", "IMAGE")
+ RETURN_NAMES = ("tonemapped", "hdr_linear")
+ OUTPUT_NODE = True
+ FUNCTION = "postprocess"
+ CATEGORY = "Lightricks/HDR"
+ DESCRIPTION = (
+ "Decompresses VAE-decoded HDR (ACEScct or legacy LogC3), tonemaps for "
+ "preview, and optionally writes EXR (linear / ACEScct / sRGB linear / "
+ "ACEScg). Wire hdr_linear into LTXVSaveHLG for a BT.2020/HLG master."
+ )
+
+ def postprocess(
+ self,
+ image: torch.Tensor,
+ transfer: str = "acescct",
+ exposure: float = 0.0,
+ save_exr: bool = False,
+ exr_color_space: str = "acescct",
+ output_dir: str = "output/hdr_exr",
+ filename_prefix: str = "frame",
+ half_precision: bool = True,
+ ) -> tuple:
+
+ hdr = _hdr_decompress(image, transfer=transfer)
+ hdr = torch.clamp(hdr, min=0.0, max=1e4)
+
+ # Reinhard tonemap with exposure (stops → linear multiplier)
+ exposure_mult = 2.0**exposure
+ hdr_exposed = hdr * exposure_mult
+ tonemapped_linear = (hdr_exposed / (1.0 + hdr_exposed)).clamp(0.0, 1.0)
+ tonemapped = _linear_to_srgb(tonemapped_linear)
+
+ if save_exr:
+ frames, primaries, tag = _exr_payload_for_color_space(
+ image, hdr, transfer, exr_color_space
+ )
+ self._save_exr_frames_tagged(
+ frames,
+ output_dir,
+ filename_prefix,
+ half_precision,
+ primaries=primaries,
+ color_space_tag=tag,
+ )
+
+ return (tonemapped, hdr)
+
+ @staticmethod
+ def _resolve_exr_dir(output_dir: str) -> str:
+ import folder_paths
+
+ if not os.path.isabs(output_dir):
+ output_dir = os.path.join(folder_paths.get_output_directory(), output_dir)
+ os.makedirs(output_dir, exist_ok=True)
+ return output_dir
+
+ @classmethod
+ def _save_exr_frames_tagged(
+ cls,
+ frames: torch.Tensor,
+ output_dir: str,
+ filename_prefix: str,
+ half_precision: bool,
+ *,
+ primaries: Primaries | None,
+ color_space_tag: str | None,
+ ) -> None:
+ output_dir = cls._resolve_exr_dir(output_dir)
+ for i, frame in enumerate(frames):
+ _save_exr_frame(
+ frame,
+ Path(output_dir) / f"{filename_prefix}_{i:05d}.exr",
+ half_precision=half_precision,
+ primaries=primaries,
+ color_space_tag=color_space_tag,
+ )
+ logger.info(
+ "Saved %d EXR frame(s) to %s (%s%s)",
+ frames.shape[0],
+ output_dir,
+ "float16" if half_precision else "float32",
+ f", {color_space_tag}" if color_space_tag else ", untagged linear",
+ )
+
+
+@comfy_node(name="LTXVLoadEXRSequence")
+class LTXVLoadEXRSequence:
+ """Load an EXR still or folder and convert to ACEScct ``[0,1]`` IMAGE for the VAE.
+
+ Declares the source colour space the same way as pipelines ``--hdr``:
+ ``srgb_linear`` / ``acescg`` (compress to ACEScct) or ``acescct`` (passthrough).
+ """
+
+ @classmethod
+ def INPUT_TYPES(cls):
+ return {
+ "required": {
+ "path": (
+ "STRING",
+ {
+ "default": "",
+ "tooltip": (
+ "Path to a .exr still or a directory of *.exr frames. "
+ "I2V: single still is fine. V2V: use a folder with 8k+1 frames. "
+ "Relative paths are resolved under ComfyUI's input folder."
+ ),
+ },
+ ),
+ "color_space": (_COLOR_SPACE_CHOICES, {"default": "srgb_linear"}),
+ "frame_rate": (
+ "FLOAT",
+ {
+ "default": 24.0,
+ "min": 1.0,
+ "max": 120.0,
+ "step": 0.001,
+ "tooltip": "Required for EXR folders (no container fps).",
+ },
+ ),
+ },
+ "optional": {
+ "frame_start": ("INT", {"default": 0, "min": 0, "max": 100000}),
+ "frame_cap": (
+ "INT",
+ {
+ "default": 0,
+ "min": 0,
+ "max": 100000,
+ "tooltip": "Max frames to load (0 = all). Still trimmed to 8k+1.",
+ },
+ ),
+ "trim_to_8k1": (
+ "BOOLEAN",
+ {
+ "default": True,
+ "tooltip": "Trim to longest 8k+1 prefix required by the video VAE.",
+ },
+ ),
+ },
+ }
+
+ RETURN_TYPES = ("IMAGE", "FLOAT", "INT", "INT", "INT", "AUDIO")
+ RETURN_NAMES = ("images", "frame_rate", "width", "height", "frame_count", "audio")
+ FUNCTION = "load"
+ CATEGORY = "Lightricks/HDR"
+ DESCRIPTION = (
+ "Native HDR input: load EXR → ACEScct [0,1] frames for IC-LoRA / VAE encode. "
+ "Also emits silent AUDIO matching duration so V2V graphs that freeze audio still wire. "
+ "Native EXR path only (not SDR→HDR IC-LoRA ingest). "
+ "Downstream: set LTXVPreprocess img_compression=0 — Python --hdr EXR conditioning "
+ "skips JPEG/CRF; compressing ACEScct codes darkens HDR highlights."
+ )
+
+ def load(
+ self,
+ path: str,
+ color_space: str,
+ frame_rate: float,
+ frame_start: int = 0,
+ frame_cap: int = 0,
+ trim_to_8k1: bool = True,
+ ):
+ root = _resolve_path(path)
+ files = _list_exr_files(root)
+ files = files[frame_start:]
+ if frame_cap > 0:
+ files = files[:frame_cap]
+ if not files:
+ raise ValueError("No EXR frames left after frame_start / frame_cap.")
+
+ cs = HDRColorSpace(color_space)
+ frames = [_read_exr(f) for f in files]
+ shapes = {tuple(f.shape) for f in frames}
+ if len(shapes) != 1:
+ raise ValueError(
+ "EXR frames must share the same H×W×C; got "
+ + ", ".join(str(s) for s in sorted(shapes))
+ )
+ images = torch.stack(frames, dim=0)
+ images = images_to_acescct(images, cs)
+ if trim_to_8k1:
+ images = _trim_to_8k1(images)
+
+ h, w = int(images.shape[1]), int(images.shape[2])
+ n = int(images.shape[0])
+ if n < 1:
+ raise ValueError(f"No EXR frames loaded from {root}")
+ audio = _silent_audio(n, float(frame_rate))
+ logger.info(
+ "Loaded %d EXR frame(s) from %s as %s → ACEScct (%dx%d @ %.3f fps); "
+ "silent audio %d samples @ %d Hz",
+ n,
+ root if root.is_dir() else (root.parent if root.is_file() else root),
+ cs.value,
+ w,
+ h,
+ frame_rate,
+ int(audio["waveform"].shape[-1]),
+ int(audio["sample_rate"]),
+ )
+ return (images, float(frame_rate), w, h, n, audio)
+
+
+@comfy_node(name="LTXVVAEForceFloat32")
+class LTXVVAEForceFloat32:
+ """Force the video VAE to encode/decode in float32 (native HDR requirement).
+
+ This node does **not** clone the VAE into a separate float32 copy for the
+ output. It mutates the same object that was passed in (``vae_dtype`` and
+ ``first_stage_model`` weights). The output socket is that same instance.
+ """
+
+ @classmethod
+ def INPUT_TYPES(cls):
+ return {
+ "required": {
+ "vae": ("VAE",),
+ }
+ }
+
+ RETURN_TYPES = ("VAE",)
+ FUNCTION = "patch"
+ CATEGORY = "Lightricks/HDR"
+ DESCRIPTION = (
+ "Forces float32 on the video VAE (vae_dtype + first_stage_model) for "
+ "native HDR. IMPORTANT: this does NOT clone a float32 VAE for the "
+ "output — it mutates the input object in place and returns the same "
+ "instance. If you also wire that loader's VAE (or this output) into "
+ "another branch, those nodes share the mutated model: they may see "
+ "float16 or float32 depending on execution order (a race with the "
+ "original dtype). Prefer a single chain VAELoader → this node → all "
+ "encode/decode / IC-LoRA guide uses."
+ )
+
+ def patch(self, vae):
+ # In-place: Comfy Module.to(dtype) mutates weights; a wrapper copy would
+ # still share first_stage_model and waste peak memory for no isolation.
+ vae.vae_dtype = torch.float32
+ model = getattr(vae, "first_stage_model", None)
+ if model is not None:
+ try:
+ model.to(dtype=torch.float32)
+ except Exception as exc:
+ raise RuntimeError(
+ "LTXVVAEForceFloat32 could not cast VAE weights to float32 "
+ f"({type(exc).__name__}: {exc}). Native HDR requires float32."
+ ) from exc
+ logger.info("VAE dtype forced to float32 for native HDR")
+ return (vae,)
+
+
+@comfy_node(name="LTXVSaveHLG")
+class LTXVSaveHLG:
+ """Encode scene-linear ``hdr_linear`` frames to a BT.2020 / HLG 10-bit HEVC master.
+
+ Wire the ``hdr_linear`` output of ``LTXVHDRDecodePostprocess`` here (not the
+ tonemapped preview). Matches pipelines ``encode_video`` HLG delivery.
+ """
+
+ @classmethod
+ def INPUT_TYPES(cls):
+ return {
+ "required": {
+ "hdr_linear": (
+ "IMAGE",
+ {
+ "tooltip": (
+ "Scene-linear HDR from LTXVHDRDecodePostprocess. "
+ "With transfer=acescct this is ACEScg linear — leave "
+ "linear_primaries=acescg. For logc3, use rec709."
+ ),
+ },
+ ),
+ "frame_rate": (
+ "FLOAT",
+ {"default": 24.0, "min": 1.0, "max": 120.0, "step": 0.001},
+ ),
+ "filename_prefix": ("STRING", {"default": "hdr/ltxv_hlg"}),
+ },
+ "optional": {
+ "linear_primaries": (
+ ["acescg", "rec709"],
+ {
+ "default": "acescg",
+ "tooltip": (
+ "Primaries of hdr_linear. ACEScct decode "
+ "(SDR→HDR IC-LoRA or native HDR) → acescg. "
+ "Legacy LogC3 → rec709."
+ ),
+ },
+ ),
+ "audio": (
+ "AUDIO",
+ {
+ "tooltip": (
+ "Optional track to mux into the HLG MP4 (e.g. Decode AUDIO). "
+ "Omit for video-only."
+ ),
+ },
+ ),
+ },
+ }
+
+ RETURN_TYPES = ()
+ OUTPUT_NODE = True
+ FUNCTION = "save"
+ CATEGORY = "Lightricks/HDR"
+ DESCRIPTION = (
+ "Writes a BT.2020/HLG 10-bit HEVC master from scene-linear hdr_linear "
+ "(from LTXVHDRDecodePostprocess). Maps linear_primaries → Rec.2020 "
+ "directly (no Rec.709 hop), then HLG. Optional AUDIO is muxed as AAC."
+ )
+
+ def save(
+ self,
+ hdr_linear: Tensor,
+ frame_rate: float,
+ filename_prefix: str,
+ linear_primaries: str = "acescg",
+ audio: dict | None = None,
+ ):
+ if hdr_linear.ndim != 4 or hdr_linear.shape[-1] < 3:
+ raise ValueError(f"Expected IMAGE [F,H,W,C]; got {tuple(hdr_linear.shape)}")
+
+ # ACEScg/Rec.709 → Rec.2020 inside HLG encode (Primaries.to_rec2020 /
+ # matrix_to_rec2020). No Rec.709 intermediate that would crush OOG early.
+ linear = hdr_linear[..., :3].float()
+ prim = (linear_primaries or "acescg").lower()
+ if prim == "acescg":
+ src_primaries = Primaries.ACESCG
+ elif prim == "rec709":
+ src_primaries = Primaries.REC709
+ else:
+ raise ValueError(
+ f"Unsupported linear_primaries {linear_primaries!r}; use acescg or rec709."
+ )
+
+ # Even dims for 4:2:0
+ h, w = linear.shape[1], linear.shape[2]
+ if h % 2 or w % 2:
+ linear = linear[:, : h - (h % 2), : w - (w % 2), :]
+
+ full_output_folder, filename, counter, _subfolder, _ = (
+ folder_paths.get_save_image_path(
+ filename_prefix,
+ folder_paths.get_output_directory(),
+ linear.shape[2],
+ linear.shape[1],
+ )
+ )
+ hlg_path = Path(full_output_folder) / f"{filename}_{counter:05d}_hlg.mp4"
+ _encode_hlg_mp4(
+ linear,
+ hlg_path,
+ fps=float(frame_rate),
+ audio=audio,
+ primaries=src_primaries,
+ )
+ logger.info(
+ "Wrote HLG 10-bit master to %s%s",
+ hlg_path,
+ " (with audio)" if audio is not None else "",
+ )
+ return {}
diff --git a/iclora.py b/iclora.py
index 33c541c..ca54698 100644
--- a/iclora.py
+++ b/iclora.py
@@ -13,6 +13,15 @@ from comfy_api.latest import io
from .latents import LTXVDilateLatent
from .nodes_registry import NODES_DISPLAY_NAME_PREFIX, comfy_node
+FRAME_IDX_TOOLTIP = (
+ "RoPE position of the reference, in pixel frames.\n"
+ "0 aligns the reference with the generated clip. Use for video references.\n"
+ ">0 places the reference at a specific pixel-frame position in the clip.\n"
+ "-1 places the reference outside the generated frame range, while still using it "
+ "for attention conditioning. Use for single-image references.\n"
+ "For multi-frame references, >0 values are rounded down to 1 mod 8."
+)
+
@comfy_node(name="LTXAddVideoICLoRAGuide")
class LTXAddVideoICLoRAGuide(io.ComfyNode):
@@ -44,12 +53,7 @@ class LTXAddVideoICLoRAGuide(io.ComfyNode):
default=0,
min=-9999,
max=9999,
- tooltip=(
- "Frame index to start the conditioning at. "
- "For single-frame videos, any frame_idx value is acceptable. "
- "For videos, frame_idx must be 1 modulo 8, otherwise it will be rounded "
- "down to the nearest 1 modulo 8. Negative values are counted from the end of the video."
- ),
+ tooltip=FRAME_IDX_TOOLTIP,
),
io.Float.Input(
"strength",
@@ -101,6 +105,35 @@ class LTXAddVideoICLoRAGuide(io.ComfyNode):
],
)
+ @classmethod
+ def resolve_frame_idx(
+ cls,
+ positive,
+ frame_idx,
+ latent_length,
+ image_length,
+ guide_latent,
+ scale_factors,
+ ):
+ """Map frame_idx to the position append_keyframe should use.
+
+ A negative value is a RoPE position before the clip, not an index counted back
+ from its end, so it bypasses get_latent_index. That is what lets a one-frame
+ reference sit at -1: adjacent to the first generated frame without landing in
+ its slot, which is the only position a one-frame guide can occupy that makes it
+ indistinguishable from the frame being generated. The length assert is
+ meaningless there -- the guide is outside the clip by construction.
+ """
+ if frame_idx < 0:
+ return frame_idx
+ frame_idx, latent_idx = nodes_lt.LTXVAddGuide.get_latent_index(
+ positive, latent_length, image_length, frame_idx, scale_factors
+ )
+ assert (
+ latent_idx + guide_latent.shape[2] <= latent_length
+ ), "Conditioning frames exceed the length of the latent sequence."
+ return frame_idx
+
@classmethod
def encode(
cls,
@@ -219,12 +252,9 @@ class LTXAddVideoICLoRAGuide(io.ComfyNode):
guide_latent.shape[2] * guide_latent.shape[3] * guide_latent.shape[4]
)
- frame_idx, latent_idx = nodes_lt.LTXVAddGuide.get_latent_index(
- positive, latent_length, len(image), frame_idx, scale_factors
+ frame_idx = cls.resolve_frame_idx(
+ positive, frame_idx, latent_length, len(image), guide_latent, scale_factors
)
- assert (
- latent_idx + guide_latent.shape[2] <= latent_length
- ), "Conditioning frames exceed the length of the latent sequence."
positive, negative, latent_image, noise_mask = (
nodes_lt.LTXVAddGuide.append_keyframe(
@@ -292,11 +322,7 @@ class LTXAddVideoICLoRAGuideAdvanced(LTXAddVideoICLoRAGuide):
default=0,
min=-9999,
max=9999,
- tooltip=(
- "Frame index to start the conditioning at. The value is rounded to the "
- "nearest frame and wrapped modulo the number of video frames. Negative "
- "values are counted from the end of the video before wrapping."
- ),
+ tooltip=FRAME_IDX_TOOLTIP,
),
io.Float.Input("strength", default=1.0, min=0.0, max=1.0, step=0.01),
io.Float.Input(
@@ -420,12 +446,9 @@ class LTXAddVideoICLoRAGuideAdvanced(LTXAddVideoICLoRAGuide):
guide_latent.shape[2] * guide_latent.shape[3] * guide_latent.shape[4]
)
- frame_idx, latent_idx = nodes_lt.LTXVAddGuide.get_latent_index(
- positive, latent_length, len(image), frame_idx, scale_factors
+ frame_idx = cls.resolve_frame_idx(
+ positive, frame_idx, latent_length, len(image), guide_latent, scale_factors
)
- assert (
- latent_idx + guide_latent.shape[2] <= latent_length
- ), "Conditioning frames exceed the length of the latent sequence."
positive, negative, latent_image, noise_mask = (
nodes_lt.LTXVAddGuide.append_keyframe(
diff --git a/iclora_attention.py b/iclora_attention.py
index 2a1e218..9c5af14 100644
--- a/iclora_attention.py
+++ b/iclora_attention.py
@@ -53,6 +53,19 @@ def append_guide_attention_entry(
return _set_guide_attention_entries(conditioning, entries)
+def set_last_guide_attention_strength(conditioning, strength):
+ """Override the attention strength of the most recently appended guide entry.
+
+ Separate from a guide's denoise strength: one says how clean the reference is
+ kept, the other how strongly the rest of the sequence may attend to it.
+ """
+ entries = _get_guide_attention_entries(conditioning)
+ if not entries:
+ return conditioning
+ updated = [*entries[:-1], {**entries[-1], "strength": strength}]
+ return _set_guide_attention_entries(conditioning, updated)
+
+
def normalize_mask(mask):
"""Normalize a ComfyUI MASK to (1, 1, F, H, W) for downstream processing.
diff --git a/latents.py b/latents.py
index 84cdfd2..5c92c42 100644
--- a/latents.py
+++ b/latents.py
@@ -7,6 +7,7 @@ import torch
from comfy.ldm.lightricks.vae.audio_vae import LATENT_DOWNSAMPLE_FACTOR
from comfy.nested_tensor import NestedTensor
+from .iclora_attention import set_last_guide_attention_strength
from .nodes_registry import comfy_node
@@ -397,102 +398,47 @@ class LTXVDilateLatent:
return (latent,)
-@comfy_node(name="LTXVAddLatentGuide")
-class LTXVAddLatentGuide:
- @classmethod
- def INPUT_TYPES(s):
- return {
- "required": {
- "vae": ("VAE",),
- "positive": ("CONDITIONING",),
- "negative": ("CONDITIONING",),
- "latent": ("LATENT",),
- "guiding_latent": ("LATENT",),
- "latent_idx": (
- "INT",
- {
- "default": 0,
- "min": -9999,
- "max": 9999,
- "step": 1,
- "tooltip": "Latent index to start the conditioning at. Can be negative to"
- "indicate that the conditioning is on the frames before the latent.",
- },
- ),
- "strength": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 1.0}),
- }
- }
-
- RETURN_TYPES = ("CONDITIONING", "CONDITIONING", "LATENT")
- RETURN_NAMES = ("positive", "negative", "latent")
-
- CATEGORY = "ltxtricks"
- FUNCTION = "generate"
-
- DESCRIPTION = "Adds a keyframe or a video segment at a specific frame index."
-
- def generate(
- self, vae, positive, negative, latent, guiding_latent, latent_idx, strength
- ):
- noise_mask = nodes_lt.get_noise_mask(latent)
- latent = latent["samples"]
- guide = guiding_latent["samples"]
-
- # Record original (pre-dilation) guide latent shape for spatial mask downsampling
- guide_orig_shape = list(guide.shape[2:]) # [F, H_small, W_small]
-
- assert (
- latent.shape[4] % guide.shape[4] == 0
- and latent.shape[3] % guide.shape[3] == 0
- ), "The ratio of the height and width of the latents and optional_guiding_latents must be an integer"
-
- guiding_latent = LTXVDilateLatent().dilate_latent(
- guiding_latent,
- horizontal_scale=latent.shape[4] // guide.shape[4],
- vertical_scale=latent.shape[3] // guide.shape[3],
- )[0]
-
- guide = guiding_latent["samples"]
- guide_mask = guiding_latent.get("noise_mask", None)
-
- # Pre-filter token count = product of dilated spatial dims
- # (before grid_mask filtering removes padding positions)
- iclora_tokens_added = guide.shape[2] * guide.shape[3] * guide.shape[4]
-
- scale_factors = vae.downscale_index_formula
-
- if latent_idx <= 0:
- frame_idx = latent_idx * scale_factors[0]
- else:
- frame_idx = 1 + (latent_idx - 1) * scale_factors[0]
-
- positive, negative, latent, noise_mask = nodes_lt.LTXVAddGuide.append_keyframe(
- positive=positive,
- negative=negative,
- frame_idx=frame_idx,
- latent_image=latent,
- noise_mask=noise_mask,
- guiding_latent=guide,
- strength=strength,
- scale_factors=scale_factors,
- guide_mask=guide_mask,
- )
-
- # Track this guide in guide_attention_entries for per-reference attention control.
- from .iclora_attention import append_guide_attention_entry
+def add_latent_guide(
+ *,
+ vae,
+ positive,
+ negative,
+ latent,
+ guiding_latent,
+ latent_idx,
+ strength,
+ attention_mask=None,
+):
+ """Pin an already-encoded latent as a guide, via ComfyUI's native node.
- positive = append_guide_attention_entry(
- positive, iclora_tokens_added, guide_orig_shape
- )
- negative = append_guide_attention_entry(
- negative, iclora_tokens_added, guide_orig_shape
- )
+ ComfyUI ships LTXVAddLatentGuide itself (Comfy-Org/ComfyUI#16176), so the
+ implementation lives there rather than being duplicated here; this is only the
+ call-shape adapter the pack's samplers want -- keyword arguments and a plain
+ tuple instead of a NodeOutput.
- return (
- positive,
- negative,
- {"samples": latent, "noise_mask": noise_mask},
- )
+ Deliberately does not forward pixel_frame_offset: that input exists only in
+ Lightricks' ComfyUI fork, and the pack has to keep working against a stock
+ install.
+ """
+ positive, negative, latent = nodes_lt.LTXVAddLatentGuide.execute(
+ positive=positive,
+ negative=negative,
+ vae=vae,
+ latent=latent,
+ guiding_latent=guiding_latent,
+ latent_idx=latent_idx,
+ strength=strength,
+ attention_mask=attention_mask,
+ )
+ if attention_mask is None:
+ # The core node records `strength` as the guide's attention strength too, so a
+ # weakly pinned guide would also become weakly attended. The implementation this
+ # replaced always left attention at full strength, and the samplers calling this
+ # pass strengths well below 1.0 (0.15 in the tiled sampler, 0.5 when extending),
+ # so keep the two decoupled rather than silently reweighting their attention.
+ positive = set_last_guide_attention_strength(positive, 1.0)
+ negative = set_last_guide_attention_strength(negative, 1.0)
+ return positive, negative, latent
@comfy_node(name="LTXVImgToVideoConditionOnly")
diff --git a/requirements.txt b/requirements.txt
index ec5155a..7607a86 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,6 +1,10 @@
+# Required for HDR primaries (same as ltx-core); no baked-matrix fallback.
+colour-science>=0.4.4
diffusers
einops
huggingface_hub>=0.25.2
kornia
ninja~=1.11.1.4
+# Required for HDR EXR read/write (same as ltx-pipelines); no OpenCV EXR fallback.
+openimageio
transformers[timm]>=4.50.0
diff --git a/tiled_sampler.py b/tiled_sampler.py
index b514220..ea620e4 100644
--- a/tiled_sampler.py
+++ b/tiled_sampler.py
@@ -5,7 +5,7 @@ import torch
from comfy_extras.nodes_custom_sampler import SamplerCustomAdvanced
from comfy_extras.nodes_lt import LTXVAddGuide, LTXVCropGuides
-from .latents import LTXVAddLatentGuide, LTXVSelectLatents
+from .latents import LTXVSelectLatents, add_latent_guide
from .nodes_registry import comfy_node
@@ -233,7 +233,7 @@ class LTXVTiledSampler:
tile_positive,
tile_negative,
tile_latents,
- ) = LTXVAddLatentGuide().generate(
+ ) = add_latent_guide(
vae=vae,
positive=tile_positive,
negative=tile_negative,
@@ -249,7 +249,7 @@ class LTXVTiledSampler:
tile_positive,
tile_negative,
tile_latents,
- ) = LTXVAddLatentGuide().generate(
+ ) = add_latent_guide(
vae=vae,
positive=tile_positive,
negative=tile_negative,
diff --git a/vanish_nodes.py b/vanish_nodes.py
index 2baa6ee..30b33a2 100644
--- a/vanish_nodes.py
+++ b/vanish_nodes.py
@@ -97,6 +97,11 @@ class LTXVInpaintPreprocess(io.ComfyNode):
"""Composites images with a green (#66FF00) background where mask is active.
If the mask has a single frame it is broadcast to match the video length.
+
+ The fill is always SDR display-encoded ``#66FF00`` (``_BG_COLOR_RGB``), even
+ when ``images`` are already ACEScct. Native HDR inpaint IC-LoRA was trained
+ with that pixel-space key; converting the green into ACEScct changes the
+ VAE latent cue and breaks inpainting.
"""
@classmethod
@@ -105,8 +110,10 @@ class LTXVInpaintPreprocess(io.ComfyNode):
node_id="LTXVInpaintPreprocess",
category="Lightricks/image_processing",
description=(
- "Composites images with a green background where mask is "
- "active, for inpainting conditioning."
+ "Composites images with a green (#66FF00) background where mask "
+ "is active, for inpainting conditioning. Always paints SDR "
+ "#66FF00 (not ACEScct-converted) — that key matches IC-LoRA "
+ "training even on native HDR / ACEScct plates."
),
inputs=[
io.Image.Input(