
    Z j;                         S SK r S SKrS SKrS SKJrJrJrJr  S SKrSSK	J
r
  \" S5      rS rSS\R                  S\S	\S
\S\R                  4
S jjr " S S5      rS\S\4S jr " S S5      rg)    N)AnyOptionalTypeVarUnion   )tqdmTc                  B     SS K n U $ ! [         a    [        S5      ef = f)Nr   zVideo decoding capabilities were removed from torchvision and migrated to TorchCodec. Please install TorchCodec following instructions at https://github.com/pytorch/torchcodec#installing-torchcodec)
torchcodecImportError)r   s    q/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/torchvision/datasets/video_utils.py_get_torchcodecr      s6    
   
J
 	

s    tensorsizestepdilationreturnc                 &   U R                  5       S:w  a  [        SU R                  5        35      eU R                  S5      nU R                  5       nX$-  X4-  4nXSUS-
  -  S-   -
  U-  S-   U4nUS   S:  a  SU4n[        R
                  " XU5      $ )z
similar to tensor.unfold, but with the dilation
and specialized for 1d tensors

Returns all consecutive windows of `size` elements, with
`step` between windows. The distance between each element
in a window is given by `dilation`.
r   z*tensor should have 1 dimension instead of r   )dim
ValueErrorstridenumeltorch
as_strided)r   r   r   r   o_strider   
new_stridenew_sizes           r   unfoldr      s     zz|qEfjjl^TUU}}QHLLNE/8#67JdQh/!34=A4HH{Qt9Fj99    c                   f    \ rS rSrSrS\\   SS4S jrS\4S jr	S\S\
\\   \\   4   4S	 jrS
rg)_VideoTimestampsDataset-   z
Dataset used to parallelize the reading of the timestamps
of a list of videos, given their paths in the filesystem.

Used in VideoClips and defined at top level, so it can be
pickled when forking.
video_pathsr   Nc                     Xl         g Nr#   )selfr#   s     r   __init__ _VideoTimestampsDataset.__init__6   s    &r   c                 ,    [        U R                  5      $ r%   lenr#   r'   s    r   __len___VideoTimestampsDataset.__len__9       4##$$r   idxc                     [        5       nUR                  R                  U R                  U   5      nUR                  R
                  nUR                  R                  n[        [        U5      5      U4$ r%   )	r   decodersVideoDecoderr#   metadata
num_framesaverage_fpslistrange)r'   r1   r   decoderr6   fpss         r   __getitem__#_VideoTimestampsDataset.__getitem__<   sb    $&
%%2243C3CC3HI%%00
**E*%&++r   r&   )__name__
__module____qualname____firstlineno____doc__r8   strr(   intr.   tupler   floatr<   __static_attributes__ r   r   r!   r!   -   sO    'DI '$ '% %,s ,uT#Y-G'H ,r   r!   xc                     U $ )z@
Dummy collate function to be used with _VideoTimestampsDataset
rH   )rI   s    r   _collate_fnrK   D   s	     Hr   c                      \ rS rSrSr            S,S\\   S\S\S\\	   S\\
\\4      S	\S
\S\S\S\S\S\S\SS4S jjrS-S jrS\
\\4   SS4S jr\S\
\\4   4S j5       rS\\   SS 4S jr\ S.S\R(                  S\S\S\\	   S\\	   S\\R(                  \\\   \R(                  4   4   4S jj5       rS.S\S\S\\	   SS4S jjrS\4S jrS\4S  jrS\4S! jrS"\S\\\4   4S# jr\S\S$\	S%\	S\\\R(                  4   4S& j5       rS"\S\\R(                  \R(                  \
\\4   \4   4S' jrS\
\\4   4S( jr S)\
\\4   SS4S* jr!S+r"g)/
VideoClipsK   a  
Given a list of video files, computes all consecutive subvideos of size
`clip_length_in_frames`, where the distance between each subvideo in the
same video is defined by `frames_between_clips`.
If `frame_rate` is specified, it will also resample all the videos to have
the same frame rate, and the clips will refer to this frame rate.

Creating this instance the first time is time-consuming, as it needs to
decode all the videos in `video_paths`. It is recommended that you
cache the results after instantiation of the class.

Recreating the clips for different clip lengths is fast, and can be done
with the `compute_clips` method.

Args:
    video_paths (List[str]): paths to the video files
    clip_length_in_frames (int): size of a clip in number of frames
    frames_between_clips (int): step (in frames) between each clip
    frame_rate (float, optional): if specified, it will resample the video
        so that it has `frame_rate`, and then the clips will be defined
        on the resampled video
    num_workers (int): how many subprocesses to use for data loading.
        0 means that the data will be loaded in the main process. (default: 0)
    output_format (str): The format of the output video tensors. Can be either "THWC" (default) or "TCHW".
Nr#   clip_length_in_framesframes_between_clips
frame_rate_precomputed_metadatanum_workers_video_width_video_height_video_min_dimension_video_max_dimension_audio_samples_audio_channelsoutput_formatr   c                 :   Xl         X`l        Xpl        Xl        Xl        Xl        Xl        Xl        UR                  5       U l	        U R                  S;  a  [        SU S35      eUc  U R                  5         OU R                  U5        U R                  X#U5        g )N)THWCTCHWz5output_format should be either 'THWC' or 'TCHW', got .)r#   rS   rT   rU   rV   rW   rX   rY   upperrZ   r   _compute_frame_pts_init_from_metadatacompute_clips)r'   r#   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   s                 r   r(   VideoClips.__init__f   s    " '& )*$8!$8!,.*002%55TUbTccdeff (##%$$%:;0
Sr   c           
      0   / U l         / U l        SS KnUR                  R                  R                  [        U R                  5      SU R                  [        S9n[        [        U5      S9 nU H  nUR                  S5        [        [        U6 5      u  pVU Vs/ s H  oqR                  " XqR                   S9PM     nnU R                   R#                  U5        U R                  R#                  U5        M     S S S 5        g s  snf ! , (       d  f       g = f)Nr      )
batch_sizerS   
collate_fn)totalr   dtype)	video_pts	video_fpstorch.utils.datautilsdata
DataLoaderr!   r#   rS   rK   r   r,   updater8   zip	as_tensorlongextend)r'   r   dlpbarbatch	batch_pts	batch_fpsptss           r   r`   VideoClips._compute_frame_pts   s    &( 	 */++*:*:*E*E#D$4$45(("	 +F +
 B DA'+CK'8$	 PYYy__S

Cy	Y%%i0%%i0  !  Z ! s   ,.D#D=<DD
Dr5   c                     US   U l         [        U R                   5      [        US   5      :X  d   eUS   U l        [        U R                   5      [        US   5      :X  d   eUS   U l        g )Nr#   rk   rl   )r#   r,   rk   rl   )r'   r5   s     r   ra   VideoClips._init_from_metadata   sm    #M24##$H[,A(BBBB!+.4##$H[,A(BBBB!+.r   c                 N    U R                   U R                  U R                  S.nU$ )Nr#   rk   rl   r   )r'   	_metadatas     r   r5   VideoClips.metadata   s*      ++
	
 r   indicesc                    U Vs/ s H  o R                   U   PM     nnU Vs/ s H  o R                  U   PM     nnU Vs/ s H  o R                  U   PM     nnUUUS.n[        U 5      " UU R                  U R
                  U R                  UU R                  U R                  U R                  U R                  U R                  U R                  U R                  U R                  S9$ s  snf s  snf s  snf )Nr   )rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   )r#   rk   rl   typer6   r   rQ   rS   rT   rU   rV   rW   rX   rY   rZ   )r'   r   ir#   rk   rl   r5   s          r   subsetVideoClips.subset   s    4;<Gq''*G<0781^^A&	80781^^A&	8&""

 Dz"&//!%"*((**,,!%!:!:!%!:!:.. 00,,
 	
 =88s   C*C/C4rk   r6   r   r;   c                    Uc  SnUc  Un[        U 5      U-  U-  n[        R                  [        [        R
                  " U5      5      X45      nX   n [        XU5      nUR                  5       (       d  [        R                  " S5        [        U[        5      (       a  U/[        U5      -  nXx4$ [        XaU5      nXx4$ )Nr   zThere aren't enough frames in the current video to get a clip for the given clip length and frames between clips. The video (and potentially others) will be skipped.)r,   rM   _resample_video_idxrD   mathfloorr   r   warningswarn
isinstanceslice)	rk   r6   r   r;   rQ   total_frames_idxsclipsidxss	            r   compute_clips_for_video"VideoClips.compute_clips_for_video   s     ; CJ9~
2S8..s4::l3K/Lc^$	yd3{{}}MM\
 eU##7SZ'D { %T2D{r   c                    Xl         X l        X0l        / U l        / U l        [        U R                  U R                  5       HP  u  pEU R                  XAX%U5      u  pgU R                  R                  U5        U R                  R                  U5        MR     [        R                  " U R                   Vs/ s H  n[        U5      PM     sn5      n	U	R                  S5      R                  5       U l        gs  snf )aD  
Compute all consecutive sequences of clips from video_pts.
Always returns clips of size `num_frames`, meaning that the
last few frames in a video can potentially be dropped.

Args:
    num_frames (int): number of frames for the clip
    step (int): distance between two clips
    frame_rate (int, optional): The frame rate
r   N)r6   r   rQ   r   resampling_idxsrr   rk   rl   r   appendr   rs   r,   cumsumtolistcumulative_sizes)
r'   r6   r   rQ   rk   r;   r   r   vclip_lengthss
             r   rb   VideoClips.compute_clips   s     %	$
!!$..$..ANI66ydYcdKEJJe$  ''- B 

'C
1A
'CD , 3 3A 6 = = ? (Ds   3C4c                 "    U R                  5       $ r%   )	num_clipsr-   s    r   r.   VideoClips.__len__   s    ~~r   c                 ,    [        U R                  5      $ r%   r+   r-   s    r   
num_videosVideoClips.num_videos  r0   r   c                      U R                   S   $ )z:
Number of subclips that are available in the video list.
)r   r-   s    r   r   VideoClips.num_clips  s     $$R((r   r1   c                     [         R                  " U R                  U5      nUS:X  a  UnX#4$ XR                  US-
     -
  nX#4$ )z_
Converts a flattened representation of the indices into a video_idx, clip_idx
representation.
r   r   )bisectbisect_rightr   )r'   r1   	video_idxclip_idxs       r   get_clip_locationVideoClips.get_clip_location  sU    
 ''(=(=sC	>H "" 229q=AAH""r   original_fpsnew_fpsc                    X-  nUR                  5       (       a  [        U5      n[        S S U5      $ [        R                  " U [        R
                  S9U-  nUR                  5       R                  [        R                  5      nU$ )Nri   )	
is_integerrD   r   r   arangefloat32r   toint64)r6   r   r   r   r   s        r   r   VideoClips._resample_video_idx  sg    %?? t9DtT**||Jemm<tCzz|u{{+r   c           	      \   XR                  5       :  a   [        SU SU R                  5        S35      eU R                  U5      u  p#U R                  U   nU R                  U   U   n[        US   R                  5       5      n[        US   R                  5       5      n[        5       nU R                  S:X  a  SOSn	UR                  R                  XIS	9n
U
R                  [        [        XgS
-   5      5      S9R                  nU
R                  R                   nXl-  nUS
-   U-  n UR                  R#                  U5      nUR%                  XS9nUR                  nSU0nU R.                  bM  U R0                  U   U   n[3        U[(        R4                  5      (       a  UUS   -
  nUU   nU R.                  US'   [7        U5      U R8                  :X  d    UR:                   SU R8                   35       eUUUU4$ ! [&         a&    [(        R*                  " S[(        R,                  S9n Nf = f)z
Gets a subclip from a list of videos.

Args:
    idx (int): index of the subclip. Must be between 0 and num_clips().

Returns:
    video (Tensor)
    audio (Tensor)
    info (Dict)
    video_idx (int): index of the video in `video_paths`
zIndex z out of range (z number of clips)r   r   r\   NHWCNCHW)dimension_orderr   )r   )start_secondsstop_seconds)r   r   ri   rl   z x )r   
IndexErrorr   r#   r   rD   itemr   rZ   r3   r4   get_frames_atr8   r9   ro   r5   r7   AudioDecoderget_samples_played_in_range	Exceptionr   emptyr   rQ   r   r   Tensorr,   r6   shape)r'   r1   r   r   
video_pathclip_pts	start_idxend_idxr   r   r:   videor;   	start_secend_secaudio_decoderaudio_samplesaudioinforesampling_idxs                       r   get_clipVideoClips.get_clip#  s    ..""vcU/$..:J9KK\]^^"44S9	%%i0
::i(2((*+	hrl'')*$&
$($6$6&$@&f%%22:2_%%d5aK3P.Q%RWW **O	Q;#%	=&//<<ZHM)EET]EtM!&&E S!??&!11)<XFN.%,,77!/.2C!C.)E $D5zT__,RS@Q.RR,eT9,,  	=KKemm<E	=s   (6G; ;-H+*H+c                    U R                    Vs/ s H  n[        U5      PM     nnU R                    Vs/ s H!  o3R                  [        R                  5      PM#     nnU(       a&  [        R
                  " U5      nUR                  5       nU R                  R                  5       nX%S'   XES'   US	 US	 US	 SUS'   U$ s  snf s  snf )Nvideo_pts_sizesrk   r   r   r      _version)	rk   r,   r   r   r   catnumpy__dict__copy)r'   r   r   rI   rk   ds         r   __getstate__VideoClips.__getstate__V  s    +/>>:>a3q6>:
 15?1TT%++&	?		),I ")I MM .
"+ gJ  ! *3 ;
 @s
   B=(Cr   c                    SU;  a  Xl         g [        R                  " US   [        R                  S9n[        R                  " X!S   SS9nUS	 X!S'   Xl         U R                  U R                  U R                  U R                  5        g )Nr   rk   ri   r   r   )r   )	r   r   rs   r   splitrb   r6   r   rQ   )r'   r   rk   s      r   __setstate__VideoClips.__setstate__r  ss    QMOOAkN%++F	KK	->+?QG	 "+4??DIItGr   )r   rY   rX   rU   rW   rV   rT   r   r   rQ   r6   rS   rZ   r   r   rl   r#   rk   )re   r   NNr   r   r   r   r   r   r   r\   )r   Nr%   )#r>   r?   r@   rA   rB   r8   rC   rD   r   rF   dictr   r(   r`   ra   propertyr5   r   staticmethodr   r   rE   r   r   r   rb   r.   r   r   r   r   r   r   r   rG   rH   r   r   rM   rM   K   s   : &($%&*:>$%$% ##T#Y#T  ##T "	#T
 UO#T  (S#X7#T #T #T #T "#T "#T #T #T #T 
#TJ14/DcN /t / $sCx.  
d3i 
L 
2 qu<<-08;BJ5/_ghm_n	u||U4;#<==	> 2@ @3 @HUO @_c @.   %C %)3 )
#S 
#U38_ 
# 	 	5 	5 	UZ[`bgbnbn[nUo 	 	1-C 1-E%,,d3PS8nVY*Y$Z 1-fd38n 8Hd38n H Hr   rM   )r   )r   r   r   typingr   r   r   r   r   rn   r   r	   r   r   rD   r   r!   rK   rM   rH   r   r   <module>r      s       0 0  CL	:5<< :s :# : :U\\ :(, ,.1  uH uHr   