
    /j;                         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dS )    N)AnyOptionalTypeVarUnion   )tqdmTc                  L    	 dd l } n# t          $ r t          d          w xY w| S )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    e/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/torchvision/datasets/video_utils.py_get_torchcodecr      sQ    
 
 
 
J
 
 	

 s    !tensorsizestepdilationreturnc                 X   |                                  dk    r$t          d|                                             |                     d          }|                                 }||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TfjjllTTUUU}}QHLLNNE/8h#67J(dQh/!34=A4HH{Qt9FHj999    c                   r    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dS )	_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                     || _         d S 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        }t          t          |                    |fS r$   )	r   decodersVideoDecoderr"   metadata
num_framesaverage_fpslistrange)r&   r.   r   decoderr3   fpss         r   __getitem__z#_VideoTimestampsDataset.__getitem__<   s\    $&&
%2243CC3HII%0
*E*%%&&++r   )__name__
__module____qualname____doc__r5   strr'   intr,   tupler   floatr9    r   r   r!   r!   -   s         'DI '$ ' ' ' '% % % % %,s ,uT#Y-G'H , , , , , ,r   r!   xc                     | S )zH
    Dummy collate function to be used with _VideoTimestampsDataset
    rB   )rC   s    r   _collate_fnrE   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	 d0d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d0d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 dS )1
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".
       r   Nr   THWCr"   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	        dvrt          d| d          ||                                  n|                     |           |                     |||           d S )N)rI   TCHWz5output_format should be either 'THWC' or 'TCHW', got .)r"   rN   rO   rP   rQ   rR   rS   rT   upperrU   r   _compute_frame_pts_init_from_metadatacompute_clips)r&   r"   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   s                 r   r'   zVideoClips.__init__f   s    " '& )*$8!$8!,.*0022%555eUbeeefff (##%%%%$$%:;;;02F
SSSSSr   c                    g | _         g | _        dd lj        j                            t          | j                  d| j        t                    }t          t          |                    5 }|D ]r}|                    d           t          t          |           \  }}fd|D             }| j                             |           | j                            |           s	 d d d            d S # 1 swxY w Y   d S )Nr   rH   )
batch_sizerN   
collate_fn)totalr   c                 >    g | ]} j         |j                   S )dtype)	as_tensorlong).0ptsr   s     r   
<listcomp>z1VideoClips._compute_frame_pts.<locals>.<listcomp>   s,    YYY_U_S
CCCYYYr   )	video_pts	video_fpstorch.utils.datautilsdata
DataLoaderr!   r"   rN   rE   r   r*   updater5   zipextend)r&   dlpbarbatch	batch_pts	batch_fpsr   s         @r   rZ   zVideoClips._compute_frame_pts   sW   &( 	 */+*:*E*E#D$455("	 +F +
 +
 B    		1D 1 1A'+CK'8'8$	9 ZYYYyYYY	%%i000%%i00001		1 		1 		1 		1 		1 		1 		1 		1 		1 		1 		1 		1 		1 		1 		1 		1 		1 		1s   0A6C44C8;C8r2   c                    |d         | _         t          | j                   t          |d                   k    sJ |d         | _        t          | j                   t          |d                   k    sJ |d         | _        d S )Nr"   ri   rj   )r"   r*   ri   rj   )r&   r2   s     r   r[   zVideoClips._init_from_metadata   s}    #M24#$$H[,A(B(BBBBB!+.4#$$H[,A(B(BBBBB!+.r   c                 0    | j         | j        | j        d}|S )Nr"   ri   rj   ry   )r&   	_metadatas     r   r2   zVideoClips.metadata   s(      +
 
	
 r   indicesc                      fd|D             } fd|D             } fd|D             }|||d} t                     | j         j         j        | j         j         j         j         j         j	         j
         j                  S )Nc                 *    g | ]}j         |         S rB   r%   rf   ir&   s     r   rh   z%VideoClips.subset.<locals>.<listcomp>   s!    <<<qt'*<<<r   c                 *    g | ]}j         |         S rB   )ri   r~   s     r   rh   z%VideoClips.subset.<locals>.<listcomp>        8881T^A&888r   c                 *    g | ]}j         |         S rB   )rj   r~   s     r   rh   z%VideoClips.subset.<locals>.<listcomp>   r   r   ry   )rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   )typer3   r   rL   rN   rO   rP   rQ   rR   rS   rT   rU   )r&   r{   r"   ri   rj   r2   s   `     r   subsetzVideoClips.subset   s    <<<<G<<<8888888	8888888	&""
 

 tDzz"&/!%"*(*,!%!:!%!:. 0,
 
 
 	
r   ri   r3   r   r8   c                    |d}||}t          |           |z  |z  }t                              t          t	          j        |                    ||          }| |         } t          | ||          }|                                st          j	        d           t          |t                    r|gt          |          z  }n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*   rG   _resample_video_idxr?   mathfloorr   r   warningswarn
isinstanceslice)	ri   r3   r   r8   rL   total_frames_idxsclipsidxss	            r   compute_clips_for_videoz"VideoClips.compute_clips_for_video   s     ; CJ9~~
2S8..s4:l3K3K/L/LcS]^^e$	y*d33{{}} 	M\  
 eU## 	37SZZ'DD%T22Dd{r   c                    || _         || _        || _        g | _        g | _        t          | j        | j                  D ]U\  }}|                     |||||          \  }}| j        	                    |           | j        	                    |           Vt          j        d | j        D                       }|                    d                                          | _        dS )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
        c                 ,    g | ]}t          |          S rB   r*   rf   vs     r   rh   z,VideoClips.compute_clips.<locals>.<listcomp>   s    'C'C'C1A'C'C'Cr   r   N)r3   r   rL   r   resampling_idxsrp   ri   rj   r   appendr   rd   cumsumtolistcumulative_sizes)	r&   r3   r   rL   ri   r8   r   r   clip_lengthss	            r   r\   zVideoClips.compute_clips   s     %	$
!!$.$.AA 	. 	.NIs66y*dTWYcddKE4Je$$$ ''----'C'C
'C'C'CDD , 3 3A 6 6 = = ? ?r   c                 *    |                                  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                 v    t          j        | j        |          }|dk    r|}n|| 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  sJ    
 '(=sCC	>>HHT29q=AAH(""r   original_fpsnew_fpsc                     ||z  }|                                 r t          |          }t          d d |          S t          j        | t          j                  |z  }|                                                    t          j                  }|S )Nrb   )	
is_integerr?   r   r   arangefloat32r   toint64)r3   r   r   r   r   s        r   r   zVideoClips._resample_video_idx  sx    g%?? 	+ t99DtT***|Jem<<<tCzz||u{++r   c           	         ||                                  k    r(t          d| d|                                   d          |                     |          \  }}| j        |         }| j        |         |         }t          |d                                                   }t          |d                                                   }t                      }| j        dk    rdnd}	|j	        
                    ||		          }
|
                    t          t          ||d
z                                 j        }|
j        j        }||z  }|d
z   |z  }	 |j	                            |          }|                    ||          }|j        }n0# t&          $ r# t)          j        dt(          j                  }Y nw xY wd|i}| j        J| j        |         |         }t3          |t(          j                  r||d         z
  }||         }| j        |d<   t7          |          | j        k    sJ |j         d| j                     ||||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   r   rI   NHWCNCHW)dimension_orderr   )r{   )start_secondsstop_seconds)r   r   rb   rj   Nz x )r   
IndexErrorr   r"   r   r?   itemr   rU   r0   r1   get_frames_atr5   r6   rm   r2   r4   AudioDecoderget_samples_played_in_range	Exceptionr   emptyr   rL   r   r   Tensorr*   r3   shape)r&   r.   r   r   
video_pathclip_pts	start_idxend_idxr   r   r7   videor8   	start_secend_secaudio_decoderaudio_samplesaudioinforesampling_idxs                       r   get_clipzVideoClips.get_clip#  sZ    $..""""]c]]$..:J:J]]]^^^"44S99	8%i0
:i(2((**++	hrl''))**$&&
$($6&$@$@&&f%22:2__%%d5GaK3P3P.Q.Q%RRW *O	Q;#%	=&/<<ZHHM)EET]lsEttM!&EE 	= 	= 	=Kem<<<EEE	= S!?&!1)<XFN.%,77 D!/.2C!C.)E $D5zzT_,,,.R.R.R.R,,,eT9,,s   8F *F98F9c                     d | j         D             }d | j         D             }|r(t          j        |          }|                                }| j                                        }||d<   ||d<   |d= |d= |d= d|d	<   |S )
Nc                 ,    g | ]}t          |          S rB   r   r   s     r   rh   z+VideoClips.__getstate__.<locals>.<listcomp>W  s    :::a3q66:::r   c                 L    g | ]!}|                     t          j                  "S rB   )r   r   r   )rf   rC   s     r   rh   z+VideoClips.__getstate__.<locals>.<listcomp>\  s&    ???1QTT%+&&???r   video_pts_sizesri   r   r   r      _version)ri   r   catnumpy__dict__copy)r&   r   ri   ds       r   __getstate__zVideoClips.__getstate__V  s    ::4>:::
 @????	 	*	),,I "))I M  .
"+ gJ  ! *r   r   c                    d|vr	|| _         d S t          j        |d         t          j                  }t          j        ||d         d          }|d= ||d<   || _         |                     | j        | j        | j                   d S )Nr   ri   rb   r   r   )r   )	r   r   rd   r   splitr\   r3   r   rL   )r&   r   ri   s      r   __setstate__zVideoClips.__setstate__r  s    QDMFOAkN%+FFF	K	1->+?QGGG	 "+4?DItGGGGGr   )rH   r   NNr   r   r   r   r   r   r   rI   )r   Nr$   )!r:   r;   r<   r=   r5   r>   r?   r   rA   dictr   r'   rZ   r[   propertyr2   r   staticmethodr   r   r@   r   r   r   r\   r,   r   r   r   r   r   r   r   rB   r   r   rG   rG   K   s        : &($%&*:>$%$% ##T #T#Y#T  ##T "	#T
 UO#T  (S#X7#T #T #T #T "#T "#T #T #T #T 
#T #T #T #TJ1 1 1 14/DcN /t / / / / $sCx.    X
d3i 
L 
 
 
 
2 qu <-08;BJ5/_ghm_n	u|U4;#<==	>   \2@ @ @3 @HUO @_c @ @ @ @.         %C % % % %)3 ) ) ) )
#S 
#U38_ 
# 
# 
# 
# 	 	5 	5 	UZ[`bgbn[nUo 	 	 	 \	1-C 1-E%,d3PS8nVY*Y$Z 1- 1- 1- 1-fd38n    8Hd38n H H H H H H Hr   rG   )r   )r   r   r   typingr   r   r   r   r   rl   r   r	   r   r   r?   r   r!   rE   rG   rB   r   r   <module>r      sK      0 0 0 0 0 0 0 0 0 0 0 0       GCLL	 	 	: :5< :s :# : :U\ : : : :(, , , , , , , ,.1     uH uH uH uH uH uH uH uH uH uHr   