3
Á3W^•+  ã               @   sL  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZddl	m
Z
 dZy>ejjejjeƒdƒZejdegƒ\ZZZejjeƒ dZW n eefk
r¦   Y nX y4d dlZejjejjƒ eejjjdƒsÚed	ƒZW n ek
rø   ed
ƒZY nX dd„ Z dd„ Z!d a"da#ddd„Z$dd„ Z%dd„ Z&ddd„Z'dd„ Z(d dd„Z)dS )!é    Né   )Ú
_video_optFz..Zvideo_readerTÚ	pict_typea+  Your version of PyAV is too old for the necessary video operations in torchvision.
If you are on Python 3.5, you will have to build from source (the conda-forge
packages are not up-to-date).  See
https://github.com/mikeboers/PyAV#installation for instructions on how to
install PyAV on your system.
z¼PyAV is not installed, and is necessary for the video operations in torchvision.
See https://github.com/mikeboers/PyAV#installation for instructions on how to
install PyAV on your system.
c               C   s   t ttƒrt‚d S )N)Ú
isinstanceÚavÚ	Exception© r   r   ú;/tmp/pip-build-7wfi2gtz/torchvision/torchvision/io/video.pyÚ_check_av_available+   s    
r
   c               C   s   t ttƒ S )N)r   r   r   r   r   r   r	   Ú_av_available0   s    r   é
   Úlibx264c       
      C   sÔ   t ƒ  tj|tjd�jƒ }tj| dd�}|j||d�}|jd |_	|jd |_
|dkrZdnd	|_|pfi |_x@|D ]8}tjj|d	d
�}d|_x|j|ƒD ]}	|j|	ƒ q–W qpW x|jƒ D ]}	|j|	ƒ q¶W |jƒ  dS )aJ  
    Writes a 4d tensor in [T, H, W, C] format in a video file

    Parameters
    ----------
    filename : str
        path where the video will be saved
    video_array : Tensor[T, H, W, C]
        tensor containing the individual frames, as a uint8 tensor in [T, H, W, C] format
    fps : Number
        frames per second
    )ÚdtypeÚw)Úmode)Úrateé   r   Z
libx264rgbZyuv420pZrgb24)ÚformatÚNONEN)r
   ÚtorchÚ	as_tensorÚuint8Únumpyr   ÚopenZ
add_streamÚshapeÚwidthÚheightZpix_fmtÚoptionsÚ
VideoFrameZfrom_ndarrayr   ÚencodeZmuxÚclose)
ÚfilenameZvideo_arrayZfpsZvideo_codecr   Ú	containerÚstreamÚimgÚframeÚpacketr   r   r	   Úwrite_video9   s     

r'   c                s*  t d7 a t t td kr tjƒ  |dkrfttjˆd|j  ƒƒ‰ˆ tdƒkrpttj	ˆ d|j  ƒƒ‰ n
t
jdƒ i ‰d}d}|jdkræ|jj}|ræd	|kræ|jd	ƒ}	||	d … }
tjd
|
ƒ}|d krÐtjd|
ƒ}|d k	ræ|jdƒdk}ˆ}t|d dƒ}|�rt|| dƒ}y| j|dd|d� W n tjk
�r:   g S X d}yXxRt| jf |ŽƒD ]>\}}|ˆ|j< |jˆ k�rT|�rŽ||k �rŽ|d7 }�qTP �qTW W n tjk
�r°   Y nX ‡ ‡‡fdd„tˆƒD ƒ}tˆƒdk�r&ˆdk�r&ˆˆk�r&‡fdd„ˆD ƒ}t|ƒdk�r&t|ƒ}|jdˆ| ƒ |S )Nr   ÚsecÚinfz@The pts_unit 'pts' gives wrong results and will be removed in a z-follow-up version. Please use pts_unit 'sec'.Fé   Úvideos   DivXs   DivX(\d+)Build(\d+)(\w)s   DivX(\d+)b(\d+)(\w)é   ó   pr   T)Z	any_frameZbackwardr#   c                s2   g | ]*}ˆˆ| j   ko ˆ kn  rˆ| ‘qS r   )Úpts)Ú.0Úi)Ú
end_offsetÚframesÚstart_offsetr   r	   ú
<listcomp>›   s    z%_read_from_stream.<locals>.<listcomp>c                s   g | ]}|ˆ k r|‘qS r   r   )r/   r0   )r3   r   r	   r4       s    zmThe pts_unit 'pts' gives wrong results and will be removed in a follow-up version. Please use pts_unit 'sec'.)Ú_CALLED_TIMESÚ_GC_COLLECTION_INTERVALÚgcZcollectÚintÚmathÚfloorÚ	time_baseÚfloatÚceilÚwarningsÚwarnÚtypeÚcodec_contextÚ	extradataÚfindÚreÚsearchÚgroupÚmaxÚseekr   ÚAVErrorÚ	enumerateÚdecoder.   ÚsortedÚlenÚinsert)r"   r3   r1   Úpts_unitr#   Ústream_nameZshould_bufferZmax_buffer_sizerB   ÚposÚdÚoZseek_offsetZbuffer_countÚidxr%   ÚresultZpreceding_framesZfirst_frame_ptsr   )r1   r2   r3   r	   Ú_read_from_stream_   s`    


"rV   c       
      C   s|   |d j |d j  }}| jd }|| d | }d}|}	||k rPt|| | ƒ}||krht|| | ƒ}	| d d …||	…f S )Nr   r   éÿÿÿÿ)r.   r   r8   )
ÚaframesÚaudio_framesZ	ref_startZref_endÚstartÚendZtotal_aframesZstep_per_aframeZs_idxZe_idxr   r   r	   Ú_align_audio_frames§   s    
r\   r.   c             C   sž  ddl m} |ƒ dkr&tj| |||ƒS tƒ  |dkr<tdƒ}||k rTtdj||ƒƒ‚i }g }g }ytj	| dd�}W n tj
k
rˆ   Y nŒX |jjrÒt|||||jjd d	diƒ}|jjd j}	|	dk	rÒt|	ƒ|d
< |jj�rt|||||jjd ddiƒ}|jjd j|d< |jƒ  dd„ |D ƒ}
dd„ |D ƒ}|
�rHtjtj|
ƒƒ}
ntjdtjd�}
|�r„tj|dƒ}tj|ƒ}t||||ƒ}ntjdtjd�}|
||fS )a¾  
    Reads a video from a file, returning both the video frames as well as
    the audio frames

    Parameters
    ----------
    filename : str
        path to the video file
    start_pts : int if pts_unit = 'pts', optional
        float / Fraction if pts_unit = 'sec', optional
        the start presentation time of the video
    end_pts : int if pts_unit = 'pts', optional
        float / Fraction if pts_unit = 'sec', optional
        the end presentation time
    pts_unit : str, optional
        unit in which start_pts and end_pts values will be interpreted, either 'pts' or 'sec'. Defaults to 'pts'.

    Returns
    -------
    vframes : Tensor[T, H, W, C]
        the `T` video frames
    aframes : Tensor[K, L]
        the audio frames, where `K` is the number of channels and `L` is the
        number of points
    info : Dict
        metadata for the video and audio. Can contain the fields video_fps (float)
        and audio_fps (int)
    r   )Úget_video_backendÚpyavNr)   zHend_pts should be larger than start_pts, got start_pts={} and end_pts={}Úignore)Úmetadata_errorsr+   Ú	video_fpsÚaudioZ	audio_fpsc             S   s   g | ]}|j ƒ jƒ ‘qS r   )Zto_rgbÚ
to_ndarray)r/   r%   r   r   r	   r4   ø   s    zread_video.<locals>.<listcomp>c             S   s   g | ]}|j ƒ ‘qS r   )rc   )r/   r%   r   r   r	   r4   ù   s    r   r,   )r   )r   r   r   r,   )r   r   )Útorchvisionr]   r   Z_read_videor
   r<   Ú
ValueErrorr   r   r   rI   Ústreamsr+   rV   Úaverage_raterb   r   r    r   r   ÚnpÚstackÚemptyr   Zconcatenater\   Zfloat32)r!   Z	start_ptsZend_ptsrO   r]   ÚinfoÚvideo_framesrY   r"   ra   ZvframesrX   r   r   r	   Ú
read_video´   sL    





rm   c             C   s*   | j d jj}|d krdS d|kr&dS dS )Nr   Fs   LavcT)rf   rA   rB   )r"   rB   r   r   r	   Ú!_can_read_timestamps_from_packets
  s    rn   c                sð   ddl m} |ƒ dkr"tj| |ƒS tƒ  g }d}ytj| dd�}W n tjk
rX   Y nhX |jj	r¸|jj	d }|j
‰ t|ƒr”dd„ |jdd	�D ƒ}nt|dtd
ƒ||ddiƒ}t|jƒ}|jƒ  dd„ |D ƒ}|dkrè‡ fdd„|D ƒ}||fS )a.  
    List the video frames timestamps.

    Note that the function decodes the whole video frame-by-frame.

    Parameters
    ----------
    filename : str
        path to the video file
    pts_unit : str, optional
        unit in which timestamp values will be returned either 'pts' or 'sec'. Defaults to 'pts'.

    Returns
    -------
    pts : List[int] if pts_unit = 'pts'
        List[Fraction] if pts_unit = 'sec'
        presentation timestamps for each one of the frames in the video.
    video_fps : int
        the frame rate for the video

    r   )r]   r^   Nr_   )r`   c             S   s   g | ]}|j d k	r|‘qS )N)r.   )r/   Úxr   r   r	   r4   =  s    z)read_video_timestamps.<locals>.<listcomp>)r+   r)   r+   c             S   s   g | ]
}|j ‘qS r   )r.   )r/   ro   r   r   r	   r4   D  s    r(   c                s   g | ]}|ˆ  ‘qS r   r   )r/   ro   )Úvideo_time_baser   r	   r4   G  s    )rd   r]   r   Z_read_video_timestampsr
   r   r   rI   rf   r+   r;   rn   ZdemuxrV   r<   rg   r    )r!   rO   r]   rl   ra   r"   Zvideo_streamr.   r   )rp   r	   Úread_video_timestamps  s.    

rq   )r   N)r   Nr.   )r.   )*rD   Úimpr7   Úosr   r   rh   r9   r>   Ú r   Z_HAS_VIDEO_OPTÚpathÚjoinÚdirnameÚ__file__Úlib_dirÚfind_moduleÚ_ÚdescriptionÚopsZload_libraryÚImportErrorÚOSErrorr   ÚloggingZ	set_levelÚERRORÚhasattrr+   r%   r   r
   r   r5   r6   r'   rV   r\   rm   rn   rq   r   r   r   r	   Ú<module>   sF   

&H
V	