
    Vji,                        d dl mZmZ d dlZd dlmZ d dlmZ d dlm	Z	m
Z
mZmZmZ d Zd Zd Zd	ej        d
ededede
dee         fdZded         deded
edededee         ded         defdZdddd ddddeded
edededee         ded         defdZdddd ddddeded
edededee         ded         defdZde	 dZd e de_        d!e de_        dS )"    )LiteralOptionalN)
FrameBatch)VideoDecoder)_FRAMEBATCH_RETURN_DOCS_POLICY_FUNCTION_TYPE_POLICY_FUNCTIONS_reshape_4d_framebatch_into_5d_validate_common_paramsc                 j    | dk    rt          d|  d          |dk    rt          d| d          d S )Nr   znum_clips (z) must be > 0znum_indices_between_frames (z) must be strictly positive)
ValueError	num_clipsnum_indices_between_framess     Z/root/voice-cloning/.venv/lib/python3.11/site-packages/torchcodec/samplers/_index_based.py_validate_params_index_basedr      sY    A~~?y???@@@!Q&&b+Ebbb
 
 	
 '&    c                 R   |dk     r||z   }||k    rt          d| d| d          t          | |          }|3t          ||z
  dz   d          }||k    rt          d| d| d          n7|dk     r||z   }t          ||          }||k    rt          d| d	| d          ||fS )
Nr   zsampling_range_start (z-) must be smaller than the number of frames (z).r   num_frames_per_clip   z0We determined that sampling_range_end should be z;, but it is smaller than or equal to sampling_range_start (z+) must be smaller than sampling_range_end ()r   _get_clip_spanmaxmin)r   r   sampling_range_startsampling_range_endnum_frames_in_video	clip_spans         r   $_validate_sampling_range_index_basedr      s^    a25II222=%9 = =%8= = =
 
 	

 #=/  I
 ! !4y!@1!DaHH#555-CU - -(- - -   6 !!!47I!I !35HII#555>)= > >'9> > >  
  !333r   c                     | |dz
  z  dz   S )a  Return the span of a clip, i.e. the number of frames (or indices)
    between the first and last frame in the clip, both included.

    This isn't the same as the number of frames in a clip!
    Example: f means a frame in the clip, x means a frame excluded from the clip
    num_frames_per_clip = 4
    num_indices_between_frames = 1, clip = ffff      , span = 4
    num_indices_between_frames = 2, clip = fxfxfxf   , span = 7
    num_indices_between_frames = 3, clip = fxxfxxfxxf, span = 10
    r    r   s     r   r   r   F   s     &)<q)@AAEEr   clip_start_indicesr   r   r   
policy_funreturnc                     g }t          ||          }| D ]W}t          ||z   |          }t          t          |||                    }	t	          |	          |k     r ||	|          }	||	z  }X|S )Nr   )r   r   listrangelen)
r"   r   r   r   r#   all_clips_indicesr   start_indexframe_index_upper_boundframe_indicess
             r   _build_all_clips_indicesr-   T   s    $ $&#=/  I
 * + +"%kI&=?R"S"S+68RSS
 
 } 333&J}6IJJM]*r   kind)randomregulardecoderr   r   r   policy)repeat_lastwraperrorc          	         t          |||           t          ||           t          ||||t          |                    \  }}| dk    rt	          j        |||f          }n%t	          j        ||dz
  |t          j                  }t          |||t          |          t          |                   }	|
                    |		          }
t          |
||
          S )N)r1   r   r2   r   )r   r   r   r   r   r/   )lowhighsizer   )stepsdtype)r"   r   r   r   r#   )indices)framesr   r   )r   r   r   r(   torchrandintlinspaceintr-   r	   get_frames_atr
   )r.   r1   r   r   r   r   r   r2   r"   r)   r=   s              r   _generic_index_based_samplerrC   x   s=    /   
 !#=   
 0T/#=1-LL0 0 0,, x"]$+=YL
 
 
 #^ ")	
 
 
 1-/#=LL$V,   ""+<"==F)/   r   r   r3   )r   r   r   r   r   r2   c          
      n    t           j                            d           t          d| ||||||          S )Nz+torchcodec.samplers.clips_at_random_indicesr/   r.   r1   r   r   r   r   r   r2   r>   _C_log_api_usage_oncerC   r1   r   r   r   r   r   r2   s          r   clips_at_random_indicesrJ      sJ     
H  !NOOO'/#=1-	 	 	 	r   c          
      n    t           j                            d           t          d| ||||||          S )Nz,torchcodec.samplers.clips_at_regular_indicesr0   rE   rF   rI   s          r   clips_at_regular_indicesrL      sJ     
H  !OPPP'/#=1-	 	 	 	r   a	  
    Args:
        decoder (VideoDecoder): The :class:`~torchcodec.decoders.VideoDecoder`
            instance to sample clips from.
        num_clips (int, optional): The number of clips to return. Default: 1.
        num_frames_per_clip (int, optional): The number of frames per clips. Default: 1.
        num_indices_between_frames(int, optional): The number of indices between
            the frames *within* a clip. Default: 1, which means frames are
            consecutive. This is sometimes refered-to as "dilation".
        sampling_range_start (int, optional): The start of the sampling range,
            which defines the first index that a clip may *start* at. Default:
            0, i.e. the start of the video.
        sampling_range_end (int or None, optional): The end of the sampling
            range, which defines the last index that a clip may *start* at. This
            value is exclusive, i.e. a clip may only start within
            [``sampling_range_start``, ``sampling_range_end``). If None
            (default), the value is set automatically such that the clips never
            span beyond the end of the video. For example if the last valid
            index in a video is 99 and the clips span 10 frames, this value is
            set to 99 - 10 + 1 = 90. Negative values are accepted and are
            equivalent to ``len(video) - val``. When a clip spans beyond the end
            of the video, the ``policy`` parameter defines how to construct such
            clip.
        policy (str, optional): Defines how to construct clips that span beyond
            the end of the video. This is best described with an example:
            assuming the last valid index in a video is 99, and a clip was
            sampled to start at index 95, with ``num_frames_per_clip=5`` and
            ``num_indices_between_frames=2``, the indices of the frames in the
            clip are supposed to be [95, 97, 99, 101, 103]. But 101 and 103 are
            invalid indices, so the ``policy`` parameter defines how to replace
            those frames, with valid indices:

            - "repeat_last": repeats the last valid frame of the clip. We would
              get [95, 97, 99, 99, 99].
            - "wrap": wraps around to the beginning of the clip. We would get
              [95, 97, 99, 95, 97].
            - "error": raises an error.

            Default is "repeat_last". Note that when ``sampling_range_end=None``
            (default), this policy parameter is unlikely to be relevant.

    
z(Sample :term:`clips` at random indices.
z:Sample :term:`clips` at regular (equally-spaced) indices.
)typingr   r   r>   
torchcodecr   torchcodec.decodersr   torchcodec.samplers._commonr   r   r	   r
   r   r   r   r   TensorrA   r&   r-   rC   rJ   rL   _COMMON_DOCS__doc__r!   r   r   <module>rU      s   $ $ $ $ $ $ $ $  ! ! ! ! ! ! , , , , , ,             
 
 
)4 )4 )4XF F F!! ! !$	!
 ! &! 
#Y! ! ! !HA
%
&AA 	A
 A !$A A !A 23A A A A AN  &' !(,6C    	
 !$  ! 23    6  &' !(,6C    	
 !$  ! 23    0*R S* * *X## # #  
$$ $ $       r   