
    9j;                         d dl Z d dlZd dlZd dlmZmZmZmZ d dlZddl	m
Z
  ed      Zd Zddej                  ded	ed
edej                  f
dZ G d d      ZdedefdZ G d d      Zy)    N)AnyOptionalTypeVarUnion   )tqdmTc                  @    	 dd l } | S # t        $ r t        d      w xY w)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    `/media/conek/DATA/Code/OCR/venv/lib/python3.12/site-packages/torchvision/datasets/video_utils.py_get_torchcodecr      s6    
   
J
 	

s    tensorsizestepdilationreturnc                 (   | 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9    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)r"   )selfr"   s     r   __init__z _VideoTimestampsDataset.__init__6   s
    &r   c                 ,    t        | j                        S r$   lenr"   r%   s    r   __len__z_VideoTimestampsDataset.__len__9       4##$$r   idxc                     t               }|j                  j                  | j                  |         }|j                  j
                  }|j                  j                  }t        t        |            |fS r$   )	r   decodersVideoDecoderr"   metadata
num_framesaverage_fpslistrange)r%   r-   r   decoderr2   fpss         r   __getitem__z#_VideoTimestampsDataset.__getitem__<   sb    $&
%%2243C3CC3HI%%00
**E*%&++r   )__name__
__module____qualname____doc__r4   strr&   intr+   tupler   floatr8    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
    rA   )rB   s    r   _collate_fnrD   D   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".
    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                 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 .)r"   rK   rL   rM   rN   rO   rP   rQ   upperrR   r   _compute_frame_pts_init_from_metadatacompute_clips)r%   r"   rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   s                 r   r&   zVideoClips.__init__f   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_sizerK   
collate_fn)totalr   dtype)	video_pts	video_fpstorch.utils.datautilsdata
DataLoaderr!   r"   rK   rD   r   r)   updater4   zip	as_tensorlongextend)r%   r   dlpbarbatch	batch_pts	batch_fpsptss           r   rX   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r1   c                     |d   | _         t        | j                         t        |d         k(  sJ |d   | _        t        | j                         t        |d         k(  sJ |d   | _        y )Nr"   rb   rc   )r"   r)   rb   rc   )r%   r1   s     r   rY   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r"   rb   rc   ru   )r%   	_metadatas     r   r1   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 )Nru   )rG   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   )r"   rb   rc   typer2   r   rI   rK   rL   rM   rN   rO   rP   rQ   rR   )r%   rw   ir"   rb   rc   r1   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rb   r2   r   r7   c                 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.)r)   rF   _resample_video_idxr>   mathfloorr   r   warningswarn
isinstanceslice)	rb   r2   r   r7   rI   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)r2   r   rI   r   resampling_idxsri   rb   rc   r   appendr   rj   r)   cumsumtolistcumulative_sizes)
r%   r2   r   rI   rb   r7   r   r   vclip_lengthss
             r   rZ   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 r$   )	num_clipsr*   s    r   r+   zVideoClips.__len__   s    ~~r   c                 ,    t        | j                        S r$   r(   r*   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   r*   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   )r%   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 )Nr`   )	
is_integerr>   r   r   arangefloat32r   toint64)r2   r   r   r   r   s        r   r}   zVideoClips._resample_video_idx  sf    g%?? t9DtT**||Jemm<tCzz|u{{+r   c           	      j   || j                         k\  r t        d| d| j                          d      | j                  |      \  }}| j                  |   }| j                  |   |   }t        |d   j                               }t        |d   j                               }t               }| j                  dk(  rdnd}	|j                  j                  ||		      }
|
j                  t        t        ||d
z                     j                  }|
j                  j                   }||z  }|d
z   |z  }	 |j                  j#                  |      }|j%                  ||      }|j                  }d|i}| j.                  H| j0                  |   |   }t3        |t(        j4                        r||d   z
  }||   }| j.                  |d<   t7        |      | j8                  k(  s J |j:                   d| j8                          ||||fS # t&        $ r( t)        j*                  dt(        j,                        }Y w xY w)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   r   rT   NHWCNCHW)dimension_orderr   )rw   )start_secondsstop_seconds)r   r   r`   rc   z x )r   
IndexErrorr   r"   r   r>   itemr   rR   r/   r0   get_frames_atr4   r5   rf   r1   r3   AudioDecoderget_samples_played_in_range	Exceptionr   emptyr   rI   r   r   Tensorr)   r2   shape)r%   r-   r   r   
video_pathclip_pts	start_idxend_idxr   r   r6   videor7   	start_secend_secaudio_decoderaudio_samplesaudioinforesampling_idxs                       r   get_clipzVideoClips.get_clip#  s&    $..""vcU/$..:J9KK\]^^"44S9	8%%i0
::i(2((*+	hrl'')*$&
$($6$6&$@&f%%22:2_%%d5GaK3P.Q%RWW **O	Q;#%	=&//<<ZHM)EET]lsEtM!&&E S!??&!11)<XFN.%,,7!/.2C!C.)E $D5zT__,RS@Q.RR,eT9,,  	=KKemm<E	=s   1:H .H21H2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_sizesrb   r   r   r      _version)	rb   r)   r   r   r   catnumpy__dict__copy)r%   r   r   rB   rb   ds         r   __getstate__zVideoClips.__getstate__V  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   rb   r`   r   r   )r   )	r   r   rj   r   splitrZ   r2   r   rI   )r%   r   rb   s      r   __setstate__zVideoClips.__setstate__r  s{    QDMOOAkN%++F	KK	1->+?QG	 "+4??DIItGr   )r\   r   NNr   r   r   r   r   r   r   rT   )r   Nr$   )!r9   r:   r;   r<   r4   r=   r>   r   r@   dictr   r&   rX   rY   propertyr1   r{   staticmethodr   r   r?   r   r   r   rZ   r+   r   r   r   r}   r   r   r   rA   r   r   rF   rF   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   rF   )r   )r   r~   r   typingr   r   r   r   r   re   r   r	   r   r   r>   r   r!   rD   rF   rA   r   r   <module>r      s       0 0  CL	:5<< :s :# : :U\\ :(, ,.1  uH uHr   