Ë
    (täi¡   ã                   ó`   — d dl Z d dlZd dlZd dlZd dlmc mZ ddl	m
Z
mZmZ  G d„ de
«      Zy)é    Né   )ÚVaeImageProcessorÚis_valid_imageÚis_valid_image_imagelistc                   óJ  — e Zd ZdZddedz  dedz  dej                  fd„Z	 ddej                  dede	j                  ej                  z  eej                  j                     z  fd	„Zededed
edeeef   fd„«       Zedej                  dededej                  fd„«       Zy)ÚVideoProcessorzSimple video processor.NÚheightÚwidthÚreturnc                 ó¨  — t        |t        «      r`t        |d   t        j                  «      rC|d   j                  dk(  r1t        j                  dt        «       t        j                  |d¬«      }t        |t        «      r`t        |d   t        j                  «      rC|d   j                  dk(  r1t        j                  dt        «       t        j                  |d¬«      }t        |t        j                  t        j                  f«      r|j                  dk(  rt        |«      }nYt        |t        «      rt        |d   «      st        |«      r|g}n,t        |t        «      rt        |d   «      r|}nt        d«      ‚t        j                  |D �cg c]  } | j                   |f||dœ|¤Ž‘Œ c}d¬«      }|j#                  dd	d
dd«      }|S c c}w )a  
        Preprocesses input video(s). Keyword arguments will be forwarded to `VaeImageProcessor.preprocess`.

        Args:
            video (`list[PIL.Image]`, `list[list[PIL.Image]]`, `torch.Tensor`, `np.array`, `list[torch.Tensor]`, `list[np.array]`):
                The input video. It can be one of the following:
                * list of the PIL images.
                * list of list of PIL images.
                * 4D Torch tensors (expected shape for each tensor `(num_frames, num_channels, height, width)`).
                * 4D NumPy arrays (expected shape for each array `(num_frames, height, width, num_channels)`).
                * list of 4D Torch tensors (expected shape for each tensor `(num_frames, num_channels, height,
                  width)`).
                * list of 4D NumPy arrays (expected shape for each array `(num_frames, height, width, num_channels)`).
                * 5D NumPy arrays: expected shape for each array `(batch_size, num_frames, height, width,
                  num_channels)`.
                * 5D Torch tensors: expected shape for each array `(batch_size, num_frames, num_channels, height,
                  width)`.
            height (`int`, *optional*, defaults to `None`):
                The height in preprocessed frames of the video. If `None`, will use the `get_default_height_width()` to
                get default height.
            width (`int`, *optional*`, defaults to `None`):
                The width in preprocessed frames of the video. If `None`, will use get_default_height_width()` to get
                the default width.

        Returns:
            `torch.Tensor` of shape `(batch_size, num_channels, num_frames, height, width)`:
                A 5D tensor holding the batched channels-first video(s).
        r   é   z”Passing `video` as a list of 5d np.ndarray is deprecated.Please concatenate the list along the batch dimension and pass it as a single 5d np.ndarray)Úaxisz˜Passing `video` as a list of 5d torch.Tensor is deprecated.Please concatenate the list along the batch dimension and pass it as a single 5d torch.TensorzeInput is in incorrect format. Currently, we only support numpy.ndarray, torch.Tensor, PIL.Image.Image)r	   r
   )Údimé   r   é   é   )Ú
isinstanceÚlistÚnpÚndarrayÚndimÚwarningsÚwarnÚFutureWarningÚconcatenateÚtorchÚTensorÚcatr   r   Ú
ValueErrorÚstackÚ
preprocessÚpermute)ÚselfÚvideor	   r
   ÚkwargsÚimgs         úh/Volumes/fast/ai/experiments/MLX_z-image/.venv/lib/python3.12/site-packages/diffusers/video_processor.pyÚpreprocess_videozVideoProcessor.preprocess_video   s‡  € ô: �eœTÔ"¤z°%¸±(¼B¿J¹JÔ'GÈEÐRSÉHÏMÉMÐ]^ÒL^Ü�M‰Mðnäôô
 —N‘N 5¨qÔ1ˆEÜ�eœTÔ"¤z°%¸±(¼E¿L¹LÔ'IÈeÐTUÉhÏmÉmÐ_`ÒN`Ü�M‰Mðpäôô
 —I‘I˜e¨!Ô,ˆEô
 �eœbŸj™j¬%¯,©,Ð7Ô8¸U¿Z¹ZÈ1º_Ü˜“K‰EÜ˜œtÔ$¬¸¸a¹Ô)AÔE]Ð^cÔEdØ�G‰EÜ˜œtÔ$Ô)AÀ%ÈÁ(Ô)KØ‰EäØwóð ô —‘ÑchÓiÑchÐ\_˜_˜TŸ_™_¨SÐW¸ÀuÑWÐPVÓWÐchÑiÐopÔqˆð —‘˜a  A q¨!Ó,ˆàˆùò js   ÆGr$   Úoutput_typec                 óV  — |j                   d   }g }t        |«      D ]>  }||   j                  dddd«      } | j                  ||fi |¤Ž}|j	                  |«       Œ@ |dk(  rt        j                  |«      }|S |dk(  rt        j                  |«      }|S |dk(  st        |› d�«      ‚|S )	aI  
        Converts a video tensor to a list of frames for export. Keyword arguments will be forwarded to
        `VaeImageProcessor.postprocess`.

        Args:
            video (`torch.Tensor`): The video as a tensor.
            output_type (`str`, defaults to `"np"`): Output type of the postprocessed `video` tensor.
        r   r   r   r   r   ÚptÚpilz9 does not exist. Please choose one of ['np', 'pt', 'pil'])	ÚshapeÚranger"   ÚpostprocessÚappendr   r    r   r   )	r#   r$   r)   r%   Ú
batch_sizeÚoutputsÚ	batch_idxÚ	batch_vidÚbatch_outputs	            r'   Úpostprocess_videoz VideoProcessor.postprocess_video]   sÅ   € ð —[‘[ ‘^ˆ
ØˆÜ˜zÖ*ˆIØ˜iÑ(×0Ñ0°°A°q¸!Ó<ˆIØ+˜4×+Ñ+¨I°{ÑMÀfÑMˆLØ�N‰N˜<Õ(ð +ð
 ˜$ÒÜ—h‘h˜wÓ'ˆGð ˆð ˜DÒ Ü—k‘k 'Ó*ˆGð ˆð  Ò%Ü ˜}Ð,eÐfÓgÐgàˆó    Úratiosc                 óž   ‡— t        | |z  «      Št        |j                  «       ˆfd„¬«      }||   }t        |d   «      t        |d   «      fS )aŽ  
        Returns the binned height and width based on the aspect ratio.

        Args:
            height (`int`): The height of the image.
            width (`int`): The width of the image.
            ratios (`dict`): A dictionary where keys are aspect ratios and values are tuples of (height, width).

        Returns:
            `tuple[int, int]`: The closest binned height and width.
        c                 ó2   •— t        t        | «      ‰z
  «      S )N)ÚabsÚfloat)ÚratioÚars    €r'   Ú<lambda>z:VideoProcessor.classify_height_width_bin.<locals>.<lambda>†   s   ø€ ¼SÄÀuÃÐPRÑARÔ=Sr7   )Úkeyr   r   )r<   ÚminÚkeysÚint)r	   r
   r8   Úclosest_ratioÚ
default_hwr>   s        @r'   Úclassify_height_width_binz(VideoProcessor.classify_height_width_binx   sM   ø€ ô �6˜E‘>Ó"ˆÜ˜FŸK™K›MÓ/SÔTˆØ˜MÑ*ˆ
Ü�:˜a‘=Ó!¤3 z°!¡}Ó#5Ð5Ð5r7   ÚsamplesÚ	new_widthÚ
new_heightc                 ó  — | j                   d   | j                   d   }}||k7  s||k7  rØt        ||z  ||z  «      }t        ||z  «      }t        ||z  «      }| j                   \  }}	}
}}| j                  ddddd«      j	                  ||
z  |	||«      } t        j                  | ||fdd¬«      } ||z
  dz  }||z   }||z
  dz  }||z   }| d	d	…d	d	…||…||…f   } | j	                  ||
|	||«      j                  ddddd«      } | S )
a4  
        Resizes and crops a tensor of videos to the specified dimensions.

        Args:
            samples (`torch.Tensor`):
                A tensor of shape (N, C, T, H, W) where N is the batch size, C is the number of channels, T is the
                number of frames, H is the height, and W is the width.
            new_width (`int`): The desired width of the output videos.
            new_height (`int`): The desired height of the output videos.

        Returns:
            `torch.Tensor`: A tensor containing the resized and cropped videos.
        r   r   r   r   r   ÚbilinearF)ÚsizeÚmodeÚalign_cornersN)r-   ÚmaxrC   r"   ÚreshapeÚFÚinterpolate)rG   rH   rI   Úorig_heightÚ
orig_widthr=   Úresized_widthÚresized_heightÚnÚcÚtÚhÚwÚstart_xÚend_xÚstart_yÚend_ys                    r'   Úresize_and_crop_tensorz%VideoProcessor.resize_and_crop_tensorŠ   sF  € ð #*§-¡-°Ñ"2°G·M±MÀ!Ñ4D�Zˆð ˜*Ò$¨
°iÒ(?Ü˜
 [Ñ0°)¸jÑ2HÓIˆEÜ 
¨UÑ 2Ó3ˆMÜ  ¨uÑ!4Ó5ˆNð $ŸM™M‰MˆAˆq�!�Q˜Ø—o‘o a¨¨A¨q°!Ó4×<Ñ<¸QÀ¹UÀAÀqÈ!ÓLˆGô —m‘mØ˜~¨}Ð=ÀJÐ^côˆGð
 % yÑ0°QÑ6ˆGØ˜iÑ'ˆEØ%¨
Ñ2°qÑ8ˆGØ˜jÑ(ˆEØša¢ G¨E M°7¸5°=Ð@ÑAˆGð —o‘o a¨¨A¨z¸9ÓE×MÑMÈaÐQRÐTUÐWXÐZ[Ó\ˆGàˆr7   )NN)r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__rC   r   r   r(   Ústrr   r   r   ÚPILÚImager6   ÚstaticmethodÚdictÚtuplerF   r`   © r7   r'   r   r      së   „ Ù"ñ?¨c°D©jð ?ÈÈdÉ
ð ?Ðhm×htÑhtó ?ðD 7;ñØ—\‘\ðØ03ðà	�‰�e—l‘lÑ	" T¨#¯)©)¯/©/Ñ%:Ñ	:óð6 ð6¨#ð 6°cð 6À4ð 6ÈEÐRUÐWZÐRZÉOò 6ó ð6ð" ð)¨¯©ð )Àð )ÐRUð )ÐZ_×ZfÑZfò )ó ñ)r7   r   )r   Únumpyr   rf   r   Útorch.nn.functionalÚnnÚ
functionalrQ   Úimage_processorr   r   r   r   rk   r7   r'   Ú<module>rq      s.   ðó ã Û 
Û ß Ð ç XÑ Xô[Ð&õ [r7   