3
Á3W^ó/  ã               @   s€   d dl Z d dlmZ d dlZd dlZd dlmZmZmZ d dlm	Z	m
Z
 ddlmZ ejfdd„Zdd	d
„ZG dd„ deƒZdS )é    N)ÚFraction)Ú _read_video_timestamps_from_fileÚ_read_video_from_fileÚ_probe_video_from_file)Úread_video_timestampsÚ
read_videoé   )Útqdmc             C   s   t | dƒ| | }||ƒS )zèconvert 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   )ZptsZtimebase_fromZtimebase_toZ
round_funcZnew_pts© r
   úG/tmp/pip-build-7wfi2gtz/torchvision/torchvision/datasets/video_utils.pyÚpts_convert   s    r   c             C   st   | j ƒ dkst‚| jdƒ}| jƒ }|| || f}|||d  d  | d |f}|d dk rfd|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   r   )ZdimÚAssertionErrorZstrideÚnumelÚtorchZ
as_strided)ZtensorÚsizeÚstepZdilationZo_strider   Z
new_strideZnew_sizer
   r
   r   Úunfold   s    	
 r   c            	   @   sˆ   e Zd ZdZd dd„Zdd	„ Zd
d„ Zedd„ ƒZdd„ Z	e
dd„ ƒZd!dd„Zdd„ Zdd„ Zdd„ Zdd„ Ze
dd„ ƒZdd„ ZdS )"Ú
VideoClipsaÙ  
    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.

    Arguments:
        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 (int, 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)
    é   r   Nr   c             C   sR   || _ || _|| _|| _|	| _|
| _|d kr6| jƒ  n
| j|ƒ | j|||ƒ d S )N)	Úvideo_pathsÚnum_workersÚ_video_widthÚ_video_heightÚ_video_min_dimensionÚ_audio_samplesÚ_compute_frame_ptsÚ_init_from_metadataÚcompute_clips)Úselfr   Zclip_length_in_framesZframes_between_clipsÚ
frame_rateÚ_precomputed_metadatar   r   r   r   r   r
   r
   r   Ú__init__G   s    

zVideoClips.__init__c                s¶   g | _ g | _G dd„ dtƒ}dd l‰ ˆ jjj|| jƒd| jdd„ d�}t	t
|ƒd��X}xP|D ]H}|jd	ƒ tt|Ž ƒ\}}‡ fd
d„|D ƒ}| j j|ƒ | jj|ƒ q\W W d Q R X d S )Nc               @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )z)VideoClips._compute_frame_pts.<locals>.DSc             S   s
   || _ d S )N)Úx)r   r"   r
   r
   r   r!   b   s    z2VideoClips._compute_frame_pts.<locals>.DS.__init__c             S   s
   t | jƒS )N)Úlenr"   )r   r
   r
   r   Ú__len__e   s    z1VideoClips._compute_frame_pts.<locals>.DS.__len__c             S   s   t | j| ƒS )N)r   r"   )r   Úidxr
   r
   r   Ú__getitem__h   s    z5VideoClips._compute_frame_pts.<locals>.DS.__getitem__N)Ú__name__Ú
__module__Ú__qualname__r!   r$   r&   r
   r
   r
   r   ÚDSa   s   r*   r   r   c             S   s   | S )Nr
   )r"   r
   r
   r   Ú<lambda>p   s    z/VideoClips._compute_frame_pts.<locals>.<lambda>)Z
batch_sizer   Z
collate_fn)Útotalr   c                s   g | ]}ˆ j |ƒ‘qS r
   )Ú	as_tensor)Ú.0Úc)r   r
   r   ú
<listcomp>v   s    z1VideoClips._compute_frame_pts.<locals>.<listcomp>)Ú	video_ptsÚ	video_fpsÚobjectZtorch.utils.dataÚutilsÚdataZ
DataLoaderr   r   r	   r#   ÚupdateÚlistÚzipÚextend)r   r*   ÚdlZpbarÚbatchÚclipsÚfpsr
   )r   r   r   [   s     


zVideoClips._compute_frame_ptsc             C   sV   |d | _ t| j ƒt|d ƒks$t‚|d | _t| j ƒt|d ƒksHt‚|d | _d S )Nr   r1   r2   )r   r#   r   r1   r2   )r   Úmetadatar
   r
   r   r   z   s
    

zVideoClips._init_from_metadatac             C   s   | j | j| jdœ}|S )N)r   r1   r2   )r   r1   r2   )r   Z	_metadatar
   r
   r   r>   �   s    
zVideoClips.metadatac                sr   ‡ fdd„|D ƒ}‡ fdd„|D ƒ}‡ fdd„|D ƒ}|||dœ}t ˆ ƒ|ˆ jˆ jˆ j|ˆ jˆ jˆ jˆ jˆ jd�
S )Nc                s   g | ]}ˆ j | ‘qS r
   )r   )r.   Úi)r   r
   r   r0   ‹   s    z%VideoClips.subset.<locals>.<listcomp>c                s   g | ]}ˆ j | ‘qS r
   )r1   )r.   r?   )r   r
   r   r0   Œ   s    c                s   g | ]}ˆ j | ‘qS r
   )r2   )r.   r?   )r   r
   r   r0   �   s    )r   r1   r2   )r    r   r   r   r   r   )	ÚtypeÚ
num_framesr   r   r   r   r   r   r   )r   Úindicesr   r1   r2   r>   r
   )r   r   ÚsubsetŠ   s    zVideoClips.subsetc             C   s†   |d krd}|d kr|}t | ƒt|ƒ|  }tjttj|ƒƒ||ƒ}| | } t| ||ƒ}t|t	ƒrr|gt |ƒ }nt|||ƒ}||fS )Nr   )
r#   Úfloatr   Ú_resample_video_idxÚintÚmathÚfloorr   Ú
isinstanceÚslice)r1   rA   r   r=   r   Ztotal_framesÚidxsr<   r
   r
   r   Úcompute_clips_for_videoš   s    
z"VideoClips.compute_clips_for_videoc       	      C   s’   || _ || _|| _g | _g | _xHt| j| jƒD ]6\}}| j|||||ƒ\}}| jj	|ƒ | jj	|ƒ q.W t
jdd„ | jD ƒƒ}|jdƒjƒ | _dS )aR  
        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.

        Arguments:
            num_frames (int): number of frames for the clip
            step (int): distance between two clips
        c             S   s   g | ]}t |ƒ‘qS r
   )r#   )r.   Úvr
   r
   r   r0   ¿   s    z,VideoClips.compute_clips.<locals>.<listcomp>r   N)rA   r   r   r<   Úresampling_idxsr8   r1   r2   rL   Úappendr   r-   ZcumsumÚtolistÚcumulative_sizes)	r   rA   r   r   r1   r=   r<   rK   Zclip_lengthsr
   r
   r   r   ¬   s    
zVideoClips.compute_clipsc             C   s   | j ƒ S )N)Ú	num_clips)r   r
   r
   r   r$   Â   s    zVideoClips.__len__c             C   s
   t | jƒS )N)r#   r   )r   r
   r
   r   Ú
num_videosÅ   s    zVideoClips.num_videosc             C   s
   | j d S )zJ
        Number of subclips that are available in the video list.
        r   éÿÿÿÿ)rQ   )r   r
   r
   r   rR   È   s    zVideoClips.num_clipsc             C   s6   t j| j|ƒ}|dkr|}n|| j|d   }||fS )zw
        Converts a flattened representation of the indices into a video_idx, clip_idx
        representation.
        r   r   )ÚbisectÚbisect_rightrQ   )r   r%   Ú	video_idxÚclip_idxr
   r
   r   Úget_clip_locationÎ   s
    zVideoClips.get_clip_locationc             C   sP   t |ƒ| }|jƒ r(t|ƒ}td d |ƒS tj| tjd�| }|jƒ jtj	ƒ}|S )N)Zdtype)
rD   Ú
is_integerrF   rJ   r   ZarangeZfloat32rH   ZtoZint64)rA   Zoriginal_fpsZnew_fpsr   rK   r
   r
   r   rE   Ú   s    zVideoClips._resample_video_idxc             C   s(  || j ƒ kr tdj|| j ƒ ƒƒ‚| j|ƒ\}}| j| }| j| | }ddlm} |ƒ }|dkr¨| jdkrrt	dƒ‚| j
dkr„t	dƒ‚| jdkr–t	dƒ‚| jdkr¨t	dƒ‚|dkrÜ|d jƒ }|d jƒ }	t|||	ƒ\}
}}nÖt|ƒ}|d
 }d}|d jƒ }|d jƒ }d\}}tdd	ƒ}d|k�rd|d }t||d |d tjƒ}t||d |d tjƒ}|d }t|| j| j
| j||f|d | j||f|d�	\}
}}d
|i}|dk	�r²||d< | jdk	�rø| j| | }t|tjƒ�ræ||d  }|
| }
| j|d
< t|
ƒ| jk�stdj|
j| jƒƒ‚|
|||fS )a<  
        Gets a subclip from a list of videos.

        Arguments:
            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 {} out of range ({} number of clips)r   )Úget_video_backendZpyavz.pyav backend doesn't support _video_width != 0z/pyav backend doesn't support _video_height != 0z6pyav backend doesn't support _video_min_dimension != 0z0pyav backend doesn't support _audio_samples != 0r   r2   NÚaudio_timebaseÚvideo_timebaseZaudio_sample_rate)Zvideo_widthZvideo_heightZvideo_min_dimensionZvideo_pts_ranger]   Zaudio_samplesZaudio_pts_ranger\   Ú	audio_fpsz{} x {}rT   rT   rT   )r   rT   )rR   Ú
IndexErrorÚformatrY   r   r<   Ztorchvisionr[   r   Ú
ValueErrorr   r   r   Úitemr   r   r   r   rG   rH   Úceilr   r   rN   rI   r   ZTensorr#   rA   r   Úshape)r   r%   rW   rX   Z
video_pathZclip_ptsr[   ÚbackendZ	start_ptsZend_ptsÚvideoÚaudioÚinfor2   r^   Zvideo_start_ptsZvideo_end_ptsZaudio_start_ptsZaudio_end_ptsr\   Zresampling_idxr
   r
   r   Úget_clipæ   s|    








$zVideoClips.get_clip)	r   r   NNr   r   r   r   r   )N)r'   r(   r)   Ú__doc__r!   r   r   Úpropertyr>   rC   ÚstaticmethodrL   r   r$   rS   rR   rY   rE   ri   r
   r
   r
   r   r   .   s"      
	
r   )r   )rU   Z	fractionsr   rG   r   Ztorchvision.ior   r   r   r   r   r4   r	   rH   r   r   r3   r   r
   r
   r
   r   Ú<module>   s   
