
    3j                         S SK r S SKJr  S SKrS SKrS SKrS SKJrJ	r	  SS jr
 " S S\	R                  5      r " S S\5      rS	\4S
 jrg)    N)override)ComfyExtensionioc                    U R                   S   U R                   S   pC[        U[        X45      -  S5      n[        U[        X5-  U-  5      U-  5      n[        U[        XE-  U-  5      U-  5      n[        R
                  R                  U SS2SS2SS2SS24   R                  SS5      XvSS5      R                  SS5      $ )	zVResize (preserve aspect) so the long edge <= max_size, then snap each side to `stride`      g      ?N   areadisabled)shapeminmaxroundcomfyutilscommon_upscalemovedim)imagemax_sizestridehwscalenhnws           :/home/wildlama/comfy/ComfyUI/comfy_extras/nodes_bernini.py_resize_long_edger   
   s    ;;q>5;;q>q3q9$c*E	VU19v-.7	8B	VU19v-.7	8B;;%%eAq!RaRK&8&@&@Q&GQWYcdllmnprss    c                   X    \ rS rSrSr\S 5       r\SS\R                  4S jj5       r	Sr
g)	BerniniConditioning   a  Bernini in-context conditioning for a Wan2.2-A14B model.

Attaches the VAE-encoded source video / reference images to the conditioning
source video first, then each reference image

The task is inferred from which inputs are connected:
  (nothing)                  -> t2v (text-to-video)
  source_video               -> v2v (video-to-video)
  source_video + ref_images  -> rv2v (reference-guided video editing)
  ref_images only            -> r2v (reference-to-video)
  source_video + ref_video   -> ads2v (insert image/video into video)

source_video is the edit base / canvas (resized to width x height).
reference_video is moving content to composite in.
Streams are ordered source_video, reference_video, then reference_images -> source_id (1, 2, 3, ...).
c                    [         R                  " SSSS[         R                  R                  S5      [         R                  R                  S5      [         R                  R                  S5      [         R
                  R                  SS	S
SS
S9[         R
                  R                  SSS
SS
S9[         R
                  R                  SSSSSS9[         R
                  R                  SSSSS9[         R                  R                  SSSS9[         R                  R                  SSSS9[         R                  R                  SS[         R                  R                  [         R                  R                  SSS9S S!S"S#9S$9[         R
                  R                  S%S&S
SS
SS'S(9/[         R                  R                  SS)9[         R                  R                  SS)9[         R                  R                  S*S)9/S+9$ ),Nr!   zBernini Conditioningzmodel/conditioning/berninia  Conditioning node for Bernini in-context video/image conditioning. It can be used for the following tasks: t2v (text-to-video), v2v (video-to-video), rv2v (reference-guided video editing), r2v (reference-to-video), ads2v (insert image/video into video).Reference images injected as in-context tokens (r2v, rv2v) are encoded independently at their own native aspect ratio (long edge capped at ref_max_size)positivenegativevaewidthi@     i    )defaultr   r   stepheighti  lengthQ   r      
batch_sizei   )r)   r   r   source_videoTz[Source video to edit or restyle (v2v, rv2v). Resized to width/height and trimmed to length.)optionaltooltipreference_videoz.Video to insert into the source video (ads2v).reference_imagesreference_imagez<Reference image injected as an in-context token (r2v, rv2v).)r2   reference_image_r      )inputprefixr   r   )r1   templateref_max_sizeP  z|Max size for the long edge of reference_video and reference_images. Resized with preserved aspect ratio and snapped to 16px.)r)   r   r   r*   r1   r2   )display_namelatent)node_idr=   categorydescriptioninputsoutputs)r   SchemaConditioningInputVaeIntImageAutogrowTemplatePrefixOutputLatent)clss    r   define_schema!BerniniConditioning.define_schema%   s   yy)/1k %%j1%%j1U#Wcrt"MXs2NXrqdK\1!F~  He  g04Jz|!!"4t[[77 hhnn->  JHn  J1qa 8 A " B ^SbdQS^b S  U$ &&J&?&&J&?		  h 7/
 	
r   Nreturnc                    [         R                  " USUS-
  S-  S-   US-  US-  /[        R                  R	                  5       S9n/ nUb  [        R
                  R                  US U2S S 2S S 2S S24   R                  SS5      XESS	5      R                  SS5      nUR                  UR                  US S 2S S 2S S 2S S24   5      5        U	b>  [        U	S U U5      nUR                  UR                  US S 2S S 2S S 2S S24   5      5        U
(       a|  [        U
5       Hm  nU
U   nUc  M  [        UR                  S
   5       HD  n[        UUUS-    U5      nUR                  UR                  US S 2S S 2S S 2S S24   5      5        MF     Mo     U(       a2  [        R                  " USU05      n[        R                  " USU05      n[         R"                  " XSU05      $ )Nr(   r   r.   r7   )devicer	   r
   r   centerr   context_latentssamples)torchzerosr   model_managementintermediate_devicer   r   r   appendencoder   sortedranger   node_helpersconditioning_set_valuesr   
NodeOutput)rN   r$   r%   r&   r'   r+   r,   r/   r0   r3   r4   r;   r>   contextvidref_vidnameimgsiimgs                       r   executeBerniniConditioning.executeE   s   j"
q/@A.EvQR{TY]^T^_hmh~h~  iS  iS  iU  V #++,,\'6'1a!:K-L-T-TUWYZ-[]blrt|}  F  F  GH  JL  MCNN3::c!Q2A2+&678&'(@,OGNN3::gaArrk&:;< /0'-<tzz!}-A+D1q5M<HCNN3::c!Q2A2+.>#?@ .	 1 #;;HGXZaFbcH#;;HGXZaFbcH}}X)V1DEEr    )NNNr<   )__name__
__module____qualname____firstlineno____doc__classmethodrO   r   ra   ri   __static_attributes__rk   r   r   r!   r!      sL    " 
 
> F  gi  gt  gt F Fr   r!   c                   L    \ rS rSr\S\\\R                        4S j5       r	Sr
g)BerniniExtensione   rQ   c                    #    [         /$ 7fN)r!   )selfs    r   get_node_listBerniniExtension.get_node_listf   s     #%%s   
rk   N)rl   rm   rn   ro   r   listtyper   	ComfyNodery   rr   rk   r   r   rt   rt   e   s)    &T$r||*<%= & &r   rt   rQ   c                     #    [        5       $ 7frw   )rt   rk   r   r   comfy_entrypointr   k   s     s   )r(   )rW   typing_extensionsr   comfy.model_managementr   comfy.utilsr_   comfy_api.latestr   r   r   r}   r!   rt   r   rk   r   r   <module>r      sK     &    /tOF",, OFd&~ & 0 r   