
    Vjiv9                     h   d dl mZmZ d dlZd dlmZ d dlmZmZm	Z	m
Z
mZ d Zd Zdej        ded	ed
ededee         fdZddded         dee         dee         ded	ee         de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e         dee         dee         ded         defdZdddddddeded	ee         dee         dee         ded         defdZdZdZd e                    d!ee"           d#e_        d$Zd%Zd&e                    eee"           d#e_        dS )'    )LiteralOptionalN)
FrameBatch)_FRAMEBATCH_RETURN_DOCS_POLICY_FUNCTION_TYPE_POLICY_FUNCTIONS_reshape_4d_framebatch_into_5d_validate_common_paramsc                 f   ||||t          d          ||dk    rt          d| d          ||dk    rt          d| d          | j        j        t          d          | j        j        t          d          d| j        j        z  }||}n|dk    rt          d| d	          |S )
Nz.This is internal only and should never happen.r   zseconds_between_clip_starts (z) must be > 0znum_clips (zZCould not infer average fps from video metadata. Try using an index-based sampler instead.zYCould not infer stream end from video metadata. Try using an index-based sampler instead.   z) must be > 0, got)
ValueErrormetadataaverage_fpsend_stream_seconds)decoder	num_clipsseconds_between_clip_startsseconds_between_framesaverage_frame_duration_secondss        Y/root/voice-cloning/.venv/lib/python3.11/site-packages/torchcodec/samplers/_time_based.py_validate_params_time_basedr      s&    	9A"="IIJJJ".3NRS3S3SV,GVVV
 
 	
 a?y???@@@#+8
 
 	
 *28
 
 	

 &')9)E%E"%!?	1	$	$[,G[[[
 
 	
 "!    c                 *   ||}n6||k     rt          d| d|           ||k    rt          d| d|           ||| dz
  |z  z
  }n||k    rt          d| d|           ||k    rt          d| d| d          t          ||          }||fS )Nzsampling_range_start (z) must be at least z) must be smaller than r   zsampling_range_end (z+) must be smaller than sampling_range_end ())r   minnum_frames_per_clipr   sampling_range_startsampling_range_endbegin_stream_secondsr   s         r   #_validate_sampling_range_time_basedr!   >   s.    #3"666h)=hhRfhh    #555j)=jjVhjj   !0 "5"9=S!SS 	 
3	3	3`#5``J^``
 
 	
 111{%9{{fx{{{
 
 	
 /1CDD!333r   clip_start_secondsr   r   r   
policy_funreturnc                     g }| D ]Dfdt          |          D             }t          |          |k     r |||          }||z  }E|S )Nc                 0    g | ]}|z  z   xk     S  r'   ).0ir   r   start_seconds	timestamps     r   
<listcomp>z/_build_all_clips_timestamps.<locals>.<listcomp>   sI     
 
 
*Q1G-GGG	 ! ! ! ! !r   )rangelen)	r"   r   r   r   r#   all_clips_timestampsclip_timestampsr*   r+   s	     ``   @@r   _build_all_clips_timestampsr1   }   s     )++ 
0 
0
 
 
 
 
 
 
.//
 
 
 "555(j:MNNO/r   repeat_last)policykind)randomregularr   r   r   r   r3   )r2   wraperrorc                   t          |||           t          ||||          }t          |||||j        j        |j        j                  \  }}| dk    r$|J ||z
  }	t          j        |          |	z  |z   }
nA|J t          j        |||          }
|
d         |k    r|
|
|k              }
t          |
          }t          |
|||j        j        t          |                   }|                    |          }t          |||          S )	N)r   r   r3   )r   r   r   r   r   r5   )r"   r   r   r   r#   )seconds)framesr   r   )r
   r   r!   r   r    r   torchrandaranger.   r1   r   get_frames_played_atr	   )r4   r   r   r   r   r   r   r   r3   sampling_range_widthr"   r/   r<   s                r   _generic_time_based_samplerrB      s   " /    9$?5	   0S/51-$-B"+>0 0 0,, x$$$14HH Jy!!$88;OO 	 +666"\ '
 
 b!%777!3"%77" *++	6-/5"+>$V,   ))2F)GGF)/   r   r   )r   r   r   r   r   r3   c                p    t           j                            d           t          d| |d |||||	  	        S )Nz.torchcodec.samplers.clips_at_random_timestampsr5   	r4   r   r   r   r   r   r   r   r3   r=   _C_log_api_usage_oncerB   )r   r   r   r   r   r   r3   s          r   clips_at_random_timestampsrH      sM     
H  !QRRR&$(/51-
 
 
 
r   )r   r   r   r   r3   c                p    t           j                            d           t          d| d ||||||	  	        S )Nz/torchcodec.samplers.clips_at_regular_timestampsr6   rD   rE   )r   r   r   r   r   r   r3   s          r   clips_at_regular_timestampsrJ      sM     
H  !RSSS&$?/51-
 
 
 
r   a  
    {maybe_note}

    Args:
        decoder (VideoDecoder): The :class:`~torchcodec.decoders.VideoDecoder`
            instance to sample clips from.
        {num_clips_or_seconds_between_clip_starts}
        num_frames_per_clip (int, optional): The number of frames per clips. Default: 1.
        seconds_between_frames (float or None, optional): The time (in seconds)
            between each frame within a clip. More accurately, this defines the
            time between the *frame sampling point*, i.e. the timestamps at
            which we sample the frames. Because frames span intervals in time ,
            the resulting start of frames within a clip may not be exactly
            spaced by ``seconds_between_frames`` - but on average, they will be.
            Default is None, which is set to the average frame duration
            (``1/average_fps``).
        sampling_range_start (float or None, optional): The start of the
            sampling range, which defines the first timestamp (in seconds) that
            a clip may *start* at. Default: None, which corresponds to the start
            of the video. (Note: some videos start at negative values, which is
            why the default is not 0).
        sampling_range_end (float or None, optional): The end of the sampling
            range, which defines the last timestamp (in seconds) 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, i.e. it is set to
            ``end_video_seconds - (num_frames_per_clip - 1) *
            seconds_between_frames``. 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 (seekable) timestamp in a video is 10.9, and
            a clip was sampled to start at timestamp 10.5, with
            ``num_frames_per_clip=5`` and ``seconds_between_frames=0.2``, the
            sampling timestamps of the frames in the clip are supposed to be
            [10.5, 10.7, 10.9, 11.1, 11.2]. But 11.1 and 11.2 are invalid
            timestamps, so the ``policy`` parameter defines how to replace those
            frames, with valid sampling timestamps:

            - "repeat_last": repeats the last valid frame of the clip. We would
              get frames sampled at timestamps [10.5, 10.7, 10.9, 10.9, 10.9].
            - "wrap": wraps around to the beginning of the clip. We would get
              frames sampled at timestamps [10.5, 10.7, 10.9, 10.5, 10.7].
            - "error": raises an error.

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

    {return_docs}
zO
        num_clips (int, optional): The number of clips to return. Default: 1.
z+Sample :term:`clips` at random timestamps.
 )
maybe_note(num_clips_or_seconds_between_clip_startsreturn_docs
zj
        seconds_between_clip_starts (float): The space (in seconds) between each
            clip start.
a  
    .. note::
        For consistency with existing sampling APIs (such as torchvision), this
        sampler takes a ``seconds_between_clip_starts`` parameter instead of
        ``num_clips``. If you find that supporting ``num_clips`` would be
        useful, please let us know by `opening a feature request
        <https://github.com/pytorch/torchcodec/issues?q=is:open+is:issue>`_.
z=Sample :term:`clips` at regular (equally-spaced) timestamps.
)typingr   r   r=   
torchcodecr   torchcodec.samplers._commonr   r   r   r	   r
   r   r!   Tensorintfloatlistr1   rB   rH   rJ   _COMMON_DOCS_NUM_CLIPS_DOCSformat__doc___SECONDS_BETWEEN_CLIP_STARTS
_NOTE_DOCSr'   r   r   <module>r]      sO   $ $ $ $ $ $ $ $  ! ! ! ! ! !             ," ," ,"^<4 <4 <4~     "	 
   &  
%[       H 7DM M M
%
&M }	M
 "*%M M %UOM #5/M !M 23M M M M Mf  .2,0*.6C    	
 %UO #5/ ! 23    <  !.2,0*.6C   "' 	
 %UO #5/ ! 23    42j&_  kB  C  C& & &  "
  

'
Uq  @W  X  X' ' '  # # #r   