
    +j;                         d dl Z d dlmZ d dlZd dlZd dlZd dlmZm	Z	 ddZ
 G d de	j                  Z G d d	e          Zd
efdZdS )    N)override)ComfyExtensionio   c                    | j         d         | j         d         }}t          |t          ||          z  d          }t          |t          ||z  |z            |z            }t          |t          ||z  |z            |z            }t          j                            | ddddddddf                             dd          ||dd                              dd          S )	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>qA3q!99$c**E	VU1u9v-..7	8	8B	VU1u9v-..7	8	8B;%%eAAAqqq!!!RaRK&8&@&@Q&G&GRQWYcddllmnprsss    c                   R    e Zd ZdZed             Ze	 ddej        fd            ZdS )BerniniConditioninga(  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                 J   t          j        ddddt           j                            d          t           j                            d          t           j                            d          t           j                            dd	d
dd
          t           j                            ddd
dd
          t           j                            ddddd          t           j                            dddd          t           j                            ddd          t           j                            ddd          t           j                            ddt           j                            t           j                            dd          d d!d"#          $          t           j                            d%d&d
dd
dd'(          gt           j        	                    d)          t           j        	                    d)          t           j
        	                    d*)          g+          S ),Nr"   zBernini Conditioningzconditioning/video_modelsa  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@  r   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).)r1   reference_image_r      )inputprefixr   r   )r0   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)   r0   r1   )display_namelatent)node_idr<   categorydescriptioninputsoutputs)r   SchemaConditioningInputVaeIntImageAutogrowTemplatePrefixOutputLatent)clss    r   define_schemaz!BerniniConditioning.define_schema%   s   y)/0t %%j11%%j11U##Wcrt"MMXs2NNXrqdKK\1!FF~q  t t04D  G G!!"4t[77 hnn->Z - ] ]1qa 8 A A " B B
 ^SbdQS^b S  U U#* &&J&??&&J&??	  h 775
 
 
 	
r    Nr;   returnc                    t          j        |d|dz
  dz  dz   |dz  |dz  gt          j                                                  }g }|t          j                            |d |d d d d d df                             dd          ||dd	                              dd          }|                    |	                    |d d d d d d d df                              |	Rt          |	d |         |          }|                    |	                    |d d d d d d d df                              |
rt          |
          D ]}|
|         }|t          |j        d
                   D ]W}t          |||dz            |          }|                    |	                    |d d d d d d d df                              X|r.t          j        |d|i          }t          j        |d|i          }t!          j        ||d|i          S )Nr   r   r-   r6   )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)rM   r$   r%   r&   r'   r*   r+   r.   r/   r2   r3   r:   r=   contextvidref_vidnameimgsiimgs                       r   executezBerniniConditioning.executeH   sy    j"
q/@A.EvQR{TY]^T^_$)$:$N$N$P$PR R R #+,,\'6'111aaa!:K-L-T-TUWYZ-[-[]bdjlrt|}}  F  F  GH  JL  M  MCNN3::c!!!QQQ2A2+&677888&'(@,OOGNN3::gaaaAAArrk&:;;<<<  	A/00 A A'-<tz!}-- A AA+D1q5M<HHCNN3::c!!!QQQ2A2+.>#?#?@@@@A  	d#;HGXZaFbccH#;HGXZaFbccH}Xx)V1DEEEr    )NNNr;   )	__name__
__module____qualname____doc__classmethodrN   r   r_   rg    r    r   r"   r"      su         "  
  
 [ 
D ]`F FegerF F F [F F Fr    r"   c                   L    e Zd Zedeeej                          fd            ZdS )BerniniExtensionrO   c                    K   t           gS N)r"   )selfs    r   get_node_listzBerniniExtension.get_node_listk   s        
 	
r    N)	rh   ri   rj   r   listtyper   	ComfyNoders   rm   r    r   ro   ro   j   sE        
T$r|*<%= 
 
 
 X
 
 
r    ro   rO   c                  "   K   t                      S rq   )ro   rm   r    r   comfy_entrypointrx   r   s      r    )r   )rU   typing_extensionsr   comfy.model_managementr   comfy.utilsr]   comfy_api.latestr   r   r   rv   r"   ro   rx   rm   r    r   <module>r}      s    & & & & & &             / / / / / / / /t t t tTF TF TF TF TF", TF TF TFn
 
 
 
 
~ 
 
 
 0      r    