
    kh=C                     B   d dl Z d dlZd dlZd dlmZ d dlmZmZmZm	Z	m
Z
mZmZmZmZ d dlZd dlmZmZmZmZ ddlmZ  ed      Zej0                  fded	ed
ededef
dZddej6                  dedededej6                  f
dZ G d d      ZdedefdZ G d d      Zy)    NFraction)	AnyCallablecastDictListOptionalTupleTypeVarUnion)_probe_video_from_file_read_video_from_file
read_videoread_video_timestamps   )tqdmTptstimebase_fromtimebase_to
round_funcreturnc                 6    t        | d      |z  |z  } ||      S )zconvert pts between different time bases
    Args:
        pts: presentation timestamp, float
        timebase_from: original timebase. Fraction
        timebase_to: new timebase. Fraction
        round_func: rounding function.
    r   r   )r   r   r   r   new_ptss        \/var/www/teggl/fontify/venv/lib/python3.12/site-packages/torchvision/datasets/video_utils.pypts_convertr      s%     sA.<Gg    tensorsizestepdilationc                 (   | j                         dk7  rt        d| j                                | j                  d      }| j                         }||z  ||z  f}|||dz
  z  dz   z
  |z  dz   |f}|d   dk  rd|f}t	        j
                  | ||      S )a   
    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/8h#67J(dQh/!34=A4HH{Qt9FHj99r   c                   X    e Zd ZdZdee   ddfdZdefdZdede	ee   e
e   f   fdZy)	_VideoTimestampsDatasetz
    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                     || _         y N)r0   )selfr0   s     r   __init__z _VideoTimestampsDataset.__init__8   s
    &r   c                 ,    t        | j                        S r2   lenr0   r3   s    r   __len__z_VideoTimestampsDataset.__len__;       4##$$r   idxc                 2    t        | j                  |         S r2   )r   r0   )r3   r;   s     r   __getitem__z#_VideoTimestampsDataset.__getitem__>   s    $T%5%5c%:;;r   )__name__
__module____qualname____doc__r	   strr4   intr9   r   r
   floatr=    r   r   r/   r/   /   sO    'DI '$ '% %<s <uT#Y-G'H <r   r/   xc                     | S )zH
    Dummy collate function to be used with _VideoTimestampsDataset
    rE   )rF   s    r   _collate_fnrH   B   s	     Hr   c                      e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 	 d*dee   dededee   dee	ee
f      ded	ed
ededededededdfdZd+dZde	ee
f   ddfdZede	ee
f   fd       Zdee   dd fdZe	 d,dej&                  dededee   dee   deej&                  eee   ej&                  f   f   fd       Zd,dededee   ddfdZdefdZdefdZdefd Zd!edeeef   fd"Zeded#ed$edeeej&                  f   fd%       Zd!edeej&                  ej&                  e	ee
f   ef   fd&Zde	ee
f   fd'Zd(e	ee
f   ddfd)Z y)-
VideoClipsaE  
    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".
    Nr0   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                 F   || _         || _        || _        || _        |	| _        |
| _        || _        || _        |j                         | _	        | j                  dvrt        d| d      || j                          n| j                  |       | j                  |||       y )N)THWCTCHWz5output_format should be either 'THWC' or 'TCHW', got .)r0   rO   rP   rQ   rR   rS   rT   rU   upperrV   r%   _compute_frame_pts_init_from_metadatacompute_clips)r3   r0   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   s                 r   r4   zVideoClips.__init__d   s    " '& )*$8!$8!,.*002%55TUbTccdeff (##%$$%:;02F
Sr   c           	      6   g | _         g | _        dd l}|j                  j                  j                  t        | j                        d| j                  t              }t        t        |            5 }|D ]  }|j                  d       t        t        |       \  }}|D cg c]   } |j                  ||j                         " }}| j                   j#                  |       | j                  j#                  |        	 d d d        y c c}w # 1 sw Y   y xY w)Nr      )
batch_sizerO   
collate_fn)totalr   dtype)	video_pts	video_fpstorch.utils.datautilsdata
DataLoaderr/   r0   rO   rH   r   r7   updatelistzip	as_tensorlongextend)r3   r(   dlpbarbatch	batch_pts	batch_fpsr   s           r   r\   zVideoClips._compute_frame_pts   s    &( 	 */++*:*:*E*E#D$4$45(("	 +F +
 B  		1D 1A'+CK'8$	9 PYY_U__S

CY	Y%%i0%%i01		1 		1 Z		1 		1s   2/D!%D
:D
DDmetadatac                     |d   | _         t        | j                         t        |d         k(  sJ |d   | _        t        | j                         t        |d         k(  sJ |d   | _        y )Nr0   rf   rg   )r0   r7   rf   rg   )r3   rw   s     r   r]   zVideoClips._init_from_metadata   sm    #M24##$H[,A(BBBB!+.4##$H[,A(BBBB!+.r   c                 N    | j                   | j                  | j                  d}|S )Nr0   rf   rg   rz   )r3   	_metadatas     r   rw   zVideoClips.metadata   s*      ++
	
 r   indicesc                    |D cg c]  }| j                   |    }}|D cg c]  }| j                  |    }}|D cg c]  }| j                  |    }}|||d} t        |       || j                  | j
                  | j                  || j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                        S c c}w c c}w c c}w )Nrz   )rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   )r0   rf   rg   type
num_framesr!   rM   rO   rP   rQ   rR   rS   rT   rU   rV   )r3   r|   ir0   rf   rg   rw   s          r   subsetzVideoClips.subset   s    4;<qt''*<<0781T^^A&8	80781T^^A&8	8&""

 tDz"&//!%"*((**,,!%!:!:!%!:!:.. 00,,
 	
 =88s   C'C,C1rf   r   r!   fpsc                 p   |d}||}t        |       |z  |z  }t        j                  t        t	        j
                  |            ||      }| |   } t        | ||      }|j                         st        j                  d       t        |t              r|gt        |      z  }||fS t        |||      }||fS )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.)r7   rJ   _resample_video_idxrC   mathfloorr-   r'   warningswarn
isinstanceslice)	rf   r   r!   r   rM   total_frames_idxsclipsidxss	            r   compute_clips_for_videoz"VideoClips.compute_clips_for_video   s     ; CJ9~
2S8..s4::l3K/LcS]^e$	y*d3{{}MM\
 eU#7SZ'D d{ %T2Dd{r   c                    || _         || _        || _        g | _        g | _        t        | j                  | j                        D ]S  \  }}| j                  |||||      \  }}| j                  j                  |       | j                  j                  |       U t        j                  | j                  D cg c]  }t        |       c}      }	|	j                  d      j                         | _        yc c}w )a  
        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)r   r!   rM   r   resampling_idxsrn   rf   rg   r   appendr(   ro   r7   cumsumtolistcumulative_sizes)
r3   r   r!   rM   rf   r   r   r   vclip_lengthss
             r   r^   zVideoClips.compute_clips   s     %	$
!!$..$..A 	.NIs66y*dTWYcdKE4JJe$  ''-	. 

'C1A'CD , 3 3A 6 = = ? (Ds   7C5c                 "    | j                         S r2   )	num_clipsr8   s    r   r9   zVideoClips.__len__   s    ~~r   c                 ,    t        | j                        S r2   r6   r8   s    r   
num_videoszVideoClips.num_videos   r:   r   c                      | j                   d   S )zJ
        Number of subclips that are available in the video list.
        )r   r8   s    r   r   zVideoClips.num_clips  s     $$R((r   r;   c                     t        j                  | j                  |      }|dk(  r|}||fS || j                  |dz
     z
  }||fS )zw
        Converts a flattened representation of the indices into a video_idx, clip_idx
        representation.
        r   r   )bisectbisect_rightr   )r3   r;   	video_idxclip_idxs       r   get_clip_locationzVideoClips.get_clip_location	  s[    
 ''(=(=sC	>H ("" T229q=AAH(""r   original_fpsnew_fpsc                 
   ||z  }|j                         rt        |      }t        d d |      S t        j                  | t        j
                        |z  }|j                         j                  t        j                        }|S )Nrd   )	
is_integerrC   r   r(   arangefloat32r   toint64)r   r   r   r!   r   s        r   r   zVideoClips._resample_video_idx  sf    g%?? t9DtT**||Jemm<tCzz|u{{+r   c                    || j                         k\  r t        d| d| j                          d      | j                  |      \  }}| j                  |   }| j                  |   |   }ddlm}  |       }|dk(  r| j                  dk7  rt        d      | j                  dk7  rt        d      | j                  dk7  rt        d	      | j                  dk7  rt        d
      | j                  dk7  rt        d      |dk(  r9|d   j                         }|d   j                         }	t        |||	      \  }
}}nt        |      }|j                   }d}t#        t$        |d   j                               }t#        t$        |d   j                               }d\  }}t'        dd      }t'        |j(                  j*                  |j(                  j,                        }|j.                  rxt'        |j0                  j*                  |j0                  j,                        }t3        |||t4        j6                        }t3        |||t4        j8                        }|j:                  }t=        || j                  | j                  | j                  | j                  ||f|| j                  | j>                  ||f|      \  }
}}d|i}|||d<   | j@                  H| jB                  |   |   }tE        |tF        jH                        r||d   z
  }|
|   }
| j@                  |d<   tK        |
      | jL                  k(  s J |
jN                   d| jL                          | jP                  dk(  r|
jS                  dddd      }
|
|||fS )a7  
        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   )get_video_backendpyavz.pyav backend doesn't support _video_width != 0z/pyav backend doesn't support _video_height != 0z6pyav backend doesn't support _video_min_dimension != 0z6pyav backend doesn't support _video_max_dimension != 0z0pyav backend doesn't support _audio_samples != 0r   N)r   r   r   )
video_widthvideo_heightvideo_min_dimensionvideo_max_dimensionvideo_pts_rangevideo_timebaseaudio_samplesaudio_channelsaudio_pts_rangeaudio_timebaserg   	audio_fpsz x rY         )*r   
IndexErrorr   r0   r   torchvisionr   rP   r%   rQ   rR   rS   rT   itemr   r   rg   r   rC   r   r   	numeratordenominator	has_audior   r   r   r   ceilaudio_sample_rater   rU   rM   r   r   r(   Tensorr7   r   shaperV   permute)r3   r;   r   r   
video_pathclip_ptsr   backend	start_ptsend_ptsvideoaudioinfo_inforg   r   video_start_ptsvideo_end_ptsaudio_start_ptsaudio_end_ptsr   r   _resampling_idxs                           r   get_clipzVideoClips.get_clip!  sZ    $..""vcU/$..:J9KK\]^^"44S9	8%%i0
::i(21#%f  A% !QRR!!Q& !RSS((A- !YZZ((A- !YZZ""a' !STTf ((*Irl'')G!+J	7!KE5$*:6EII"3(8(8(:;O hrl&7&7&9:M-2*O]%a^N%e&:&:&D&DeFZFZFfFfgN!)%*>*>*H*H%J^J^JjJj!k"-o~~_c_i_i"j +M>>[_[d[d e!33	3 --!//$($=$=$($=$=!0- @-"11#33!0- @-OE5!  +D$$-[!??&!11)<XFN.%,,7!/.2C!C.)E $D5zT__,RS@Q.RR,'MM!Q1-EeT9,,r   c                 |   | j                   D cg c]  }t        |       }}| j                   D cg c]!  }|j                  t        j                        # }}|r%t        j
                  |      }|j                         }| j                  j                         }||d<   ||d<   |d= |d= |d= d|d<   |S c c}w c c}w )Nvideo_pts_sizesrf   r   r   r   r   _version)	rf   r7   r   r(   r   catnumpy__dict__copy)r3   r   r   rF   rf   ds         r   __getstate__zVideoClips.__getstate__y  s    +/>>:a3q6::
 15?1QTT%++&?	?		),I ")I MM .
"+ gJ  ! *3 ;
 @s
   B4&B9r   c                 "   d|vr|| _         y t        j                  |d   t        j                        }t        j                  ||d   d      }|d= ||d<   || _         | j                  | j                  | j                  | j                         y )Nr   rf   rd   r   r   )r$   )	r   r(   ro   r   splitr^   r   r!   rM   )r3   r   rf   s      r   __setstate__zVideoClips.__setstate__  s{    QDMOOAkN%++F	KK	1->+?QG	 "+4??DIItGr   )r`   r   NNr   r   r   r   r   r   r   rX   )r   Nr2   )!r>   r?   r@   rA   r	   rB   rC   r
   rD   r   r   r4   r\   r]   propertyrw   r   staticmethodr(   r   r   r   r   r   r^   r9   r   r   r   r   r   r   r   rE   r   r   rJ   rJ   I   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 	 	V-C V-E%,,d3PS8nVY*Y$Z V-pd38n 8Hd38n H Hr   rJ   )r   ) r   r   r   	fractionsr   typingr   r   r   r   r	   r
   r   r   r   r(   torchvision.ior   r   r   r   ri   r   r   r   rC   r   r   r-   r/   rH   rJ   rE   r   r   <module>r      s        S S S  k k CL bfakak 	S 	 	 	V^ 	ps 	:5<< :s :# : :U\\ :(< <&1  ZH ZHr   