| """Preceding / overlap logic — extracted from the original process_iteration(). |
| |
| Implements P1 mode (iter 0: first frame is the single preceding frame) and |
| P9 mode (iter > 0: last N generated frames are the preceding frames), and |
| renders matching preceding scene/fg projections. |
| |
| We deliberately mirror the original semantics so overlap-aware iterative |
| inference behaves identically. |
| """ |
| from __future__ import annotations |
|
|
| from typing import List, Optional, Tuple |
|
|
| import numpy as np |
| import torch |
|
|
| from .projection import blend_fg_over_scene, render_scene_proj_latent |
| from .fg import encode_first_frame_fg_to_latent |
| from .precomputed import encode_proj_frames_to_latent, subset_video_frames |
|
|
|
|
| def build_preceding_p1(state, |
| options, |
| output_size: Tuple[int, int], |
| t0: int, |
| pipeline, |
| use_fg_proj: bool) -> Tuple[Optional[np.ndarray], |
| Optional[torch.Tensor], |
| Optional[torch.Tensor]]: |
| """P1 preceding: first frame + scene_proj at first frame pose + (optional) fg. |
| |
| Returns |
| ------- |
| preceding_frames : (1, H, W, 3) uint8 or None |
| preceding_scene_proj : [16, 1, h, w] or None |
| preceding_fg_proj : [16, 1, h, w] or None |
| """ |
| max_preceding = max(0, int(options.max_preceding_frames_first_iter)) |
| if max_preceding < 1: |
| return None, None, None |
|
|
| first_frame_np = np.array(state.current_first_frame) |
| preceding_frames = first_frame_np[np.newaxis, ...] |
|
|
| use_mp4_scene = bool(state.__dict__.get("use_mp4_scene", False)) |
| use_mp4_fg = bool(state.__dict__.get("use_mp4_fg", use_mp4_scene)) |
| blend_fg_into_scene = bool(state.__dict__.get("blend_fg_into_scene", False)) |
| scene_frames = state.__dict__.get("scene_proj_frames") |
| fg_frames = state.__dict__.get("fg_proj_frames") |
|
|
| preceding_scene_proj = None |
| preceding_fg_proj = None |
|
|
| fg_blend_frames = None |
| if blend_fg_into_scene and fg_frames is not None: |
| fg_blend_frames = subset_video_frames(fg_frames, [t0], output_size) |
|
|
| if use_mp4_scene and scene_frames is not None: |
| |
| if fg_blend_frames is not None: |
| scene_sub = subset_video_frames(scene_frames, [t0], output_size) |
| blended = blend_fg_over_scene(scene_sub, fg_blend_frames) |
| preceding_scene_proj = encode_proj_frames_to_latent( |
| frames_all=blended, |
| frame_indices=list(range(len(blended))), |
| output_size=output_size, |
| vae=pipeline.vae, device=pipeline.device, dtype=pipeline.dtype, |
| ) |
| else: |
| preceding_scene_proj = encode_proj_frames_to_latent( |
| frames_all=scene_frames, |
| frame_indices=[t0], |
| output_size=output_size, |
| vae=pipeline.vae, device=pipeline.device, dtype=pipeline.dtype, |
| ) |
|
|
| if not use_mp4_scene and state.points_world is not None: |
| preceding_scene_proj = render_scene_proj_latent( |
| points_world=state.points_world, |
| colors=state.colors, |
| colored_mask=state.colored_mask, |
| target_frame_indices=[t0], |
| poses_c2w=state.poses_c2w, |
| intrinsics=state.intrinsics, |
| intrinsics_size=state.intrinsics_size, |
| output_size=output_size, |
| vae=pipeline.vae, |
| device=pipeline.device, |
| dtype=pipeline.dtype, |
| renderer=state.__dict__.get("scene_proj_renderer", "splat2d"), |
| splat_rect_size=state.__dict__.get("scene_proj_splat_rect_size", 10), |
| fg_blend_frames=fg_blend_frames, |
| ) |
|
|
| if use_fg_proj and use_mp4_fg and fg_frames is not None: |
| |
| |
| preceding_fg_proj = encode_proj_frames_to_latent( |
| frames_all=fg_frames, |
| frame_indices=[t0], |
| output_size=output_size, |
| vae=pipeline.vae, device=pipeline.device, dtype=pipeline.dtype, |
| ) |
| elif use_fg_proj and state.initial_fg_only_mask is not None: |
| preceding_fg_proj = encode_first_frame_fg_to_latent( |
| first_frame_rgb=first_frame_np, |
| fg_mask=state.initial_fg_only_mask, |
| output_size=output_size, |
| vae=pipeline.vae, |
| device=pipeline.device, |
| dtype=pipeline.dtype, |
| ) |
|
|
| return preceding_frames, preceding_scene_proj, preceding_fg_proj |
|
|
|
|
| def build_preceding_p9(state, |
| options, |
| output_size: Tuple[int, int], |
| target_frame_indices: List[int], |
| pipeline) -> Tuple[Optional[np.ndarray], |
| Optional[torch.Tensor], |
| Optional[torch.Tensor]]: |
| """P9 preceding: last N generated frames + scene_proj at their poses. |
| |
| fg_proj follows the configured source independently from scene_proj, so |
| hybrid scene-from-pointcloud / fg-from-mp4 conditioning can keep P9 fg. |
| """ |
| max_preceding = max(0, int(options.max_preceding_frames_other_iter)) |
| if max_preceding < 1 or len(state.all_generated_frames) == 0: |
| return None, None, None |
|
|
| num_use = min(max_preceding, len(state.all_generated_frames)) |
| preceding_start_idx = len(state.all_generated_frames) - num_use |
| preceding_frames = np.stack(state.all_generated_frames[preceding_start_idx:], axis=0) |
|
|
| use_mp4_scene = bool(state.__dict__.get("use_mp4_scene", False)) |
| use_mp4_fg = bool(state.__dict__.get("use_mp4_fg", use_mp4_scene)) |
| blend_fg_into_scene = bool(state.__dict__.get("blend_fg_into_scene", False)) |
| scene_frames = state.__dict__.get("scene_proj_frames") |
| fg_frames = state.__dict__.get("fg_proj_frames") |
|
|
| preceding_pose_start = target_frame_indices[0] - len(preceding_frames) |
| preceding_pose_start = max(0, preceding_pose_start) |
| preceding_pose_indices = list(range(preceding_pose_start, target_frame_indices[0])) |
|
|
| preceding_scene_proj = None |
| preceding_fg_proj = None |
|
|
| fg_blend_frames = None |
| if (blend_fg_into_scene and fg_frames is not None |
| and preceding_pose_indices): |
| fg_blend_frames = subset_video_frames( |
| fg_frames, preceding_pose_indices, output_size, |
| ) |
|
|
| if use_mp4_scene and scene_frames is not None: |
| if preceding_pose_indices: |
| if fg_blend_frames is not None: |
| scene_sub = subset_video_frames( |
| scene_frames, preceding_pose_indices, output_size, |
| ) |
| blended = blend_fg_over_scene(scene_sub, fg_blend_frames) |
| preceding_scene_proj = encode_proj_frames_to_latent( |
| frames_all=blended, |
| frame_indices=list(range(len(blended))), |
| output_size=output_size, |
| vae=pipeline.vae, device=pipeline.device, dtype=pipeline.dtype, |
| ) |
| else: |
| preceding_scene_proj = encode_proj_frames_to_latent( |
| frames_all=scene_frames, |
| frame_indices=preceding_pose_indices, |
| output_size=output_size, |
| vae=pipeline.vae, device=pipeline.device, dtype=pipeline.dtype, |
| ) |
|
|
| if not use_mp4_scene and state.points_world is not None and preceding_pose_indices: |
| preceding_scene_proj = render_scene_proj_latent( |
| points_world=state.points_world, |
| colors=state.colors, |
| colored_mask=state.colored_mask, |
| target_frame_indices=preceding_pose_indices, |
| poses_c2w=state.poses_c2w, |
| intrinsics=state.intrinsics, |
| intrinsics_size=state.intrinsics_size, |
| output_size=output_size, |
| vae=pipeline.vae, |
| device=pipeline.device, |
| dtype=pipeline.dtype, |
| renderer=state.__dict__.get("scene_proj_renderer", "splat2d"), |
| splat_rect_size=state.__dict__.get("scene_proj_splat_rect_size", 10), |
| fg_blend_frames=fg_blend_frames, |
| ) |
|
|
| if use_mp4_fg and fg_frames is not None and preceding_pose_indices: |
| preceding_fg_proj = encode_proj_frames_to_latent( |
| frames_all=fg_frames, |
| frame_indices=preceding_pose_indices, |
| output_size=output_size, |
| vae=pipeline.vae, device=pipeline.device, dtype=pipeline.dtype, |
| ) |
|
|
| return preceding_frames, preceding_scene_proj, preceding_fg_proj |
|
|