U
    ­Ê¹j¦O  ã                   @   s�  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	 d dl
Z
ddlmZ zedƒ dZW n eefk
rx   dZY nX ed d	ƒZG d
d„ dƒZG dd„ dƒZeeef ddœdd„Ze
je
je
je
je
je
jedœdd„Ze
je
jeeef e
jdœdd„Zddd d d d dedd d defeeeeeeeeeef eeeeeeef eee
je
jef dœdd„Zeeee ee ef dœdd„Zeedœdd „Zd0e
jeeeeeeeeef eeeeeeeef eeee
je
jf d!œd"d#„Ze
jeee ee ef d$œd%d&„Ze
jed$œd'd(„Z d1ee	eef ee	eef  eee
je
jeeef f d*œd+d,„Z!d2eeee	ee ee f ee f d-œd.d/„Z"dS )3é    N)ÚFraction)ÚDictÚListÚOptionalÚTupleÚUnioné   )Ú_load_libraryÚvideo_readerTFé   c                   @   s0   e Zd ZeedœZddgZeeddœdd„ZdS )ÚTimebase©Ú	numeratorÚdenominatorr   r   N)r   r   Úreturnc                 C   s   || _ || _d S )Nr   )Úselfr   r   © r   úX/var/www/html/bwave/python/venv/lib/python3.8/site-packages/torchvision/io/_video_opt.pyÚ__init__   s    zTimebase.__init__)Ú__name__Ú
__module__Ú__qualname__ÚintÚ__annotations__Ú	__slots__r   r   r   r   r   r      s   
ür   c                	   @   sD   e Zd ZeeeeeeeedœZdddddddd	gZd
dœdd„Zd
S )ÚVideoMetaData)Ú	has_videoÚvideo_timebaseÚvideo_durationÚ	video_fpsÚ	has_audioÚaudio_timebaseÚaudio_durationÚaudio_sample_rater   r   r   r   r    r!   r"   r#   N)r   c                 C   s@   d| _ tddƒ| _d| _d| _d| _tddƒ| _d| _d| _d S )NFr   r   g        )	r   r   r   r   r   r    r!   r"   r#   )r   r   r   r   r   9   s    zVideoMetaData.__init__)	r   r   r   Úboolr   Úfloatr   r   r   r   r   r   r   r   #   s&   øør   )Ú	pts_ranger   c                 C   s@   | d | d   krdkr<n nt d| d › d| d › �ƒ‚d S )Nr   r   z=Start pts should not be smaller than end pts, got start pts: z and end pts: )Ú
ValueError)r&   r   r   r   Ú_validate_ptsD   s     ÿr(   )Ú	vtimebaseÚvfpsÚ	vdurationÚ	atimebaseÚasample_rateÚ	adurationr   c                 C   s$  t ƒ }|  ¡ dkrvtt| d  ¡ ƒt| d  ¡ ƒƒ|_| d  ¡ t| d  ¡ ƒ }| ¡ dkrvd|_t| ¡ ƒ| |_| ¡ dkr�t| ¡ ƒ|_	| ¡ dk�rtt|d  ¡ ƒt|d  ¡ ƒƒ|_
|d  ¡ t|d  ¡ ƒ }| ¡ dk�rd|_t| ¡ ƒ| |_| ¡ dk�r t| ¡ ƒ|_|S )zE
    Build update VideoMetaData struct with info about the video
    r   r   T)r   Únumelr   r   Úitemr   r%   r   r   r   r!   r    r"   r#   )r)   r*   r+   r,   r-   r.   ÚmetaZtimebaser   r   r   Ú
_fill_infoL   s$    $$r2   )ÚaframesÚ
aframe_ptsÚaudio_pts_ranger   c           	      C   sœ   |d |d  }}|   d¡}t|| d ƒt|ƒ }d}|}||d k r\t|d | | ƒ}|d dkrˆ||d krˆt|d | | ƒ}| ||…d d …f S )Nr   éÿÿÿÿr   )Úsizer%   r   )	r3   r4   r5   ÚstartÚendZnum_samplesZstep_per_aframeZs_idxZe_idxr   r   r   Ú_align_audio_framesl   s    
r:   ç      Ð?©r   r6   )ÚfilenameÚseek_frame_marginÚread_video_streamÚvideo_widthÚvideo_heightÚvideo_min_dimensionÚvideo_max_dimensionÚvideo_pts_ranger   Úread_audio_streamÚaudio_samplesÚaudio_channelsr5   r!   r   c                 C   s¦   t |ƒ t |ƒ tjj | |d||||||d |d |j|j|	|
||d |d |j|j¡}|\
}}}}}}}}}}t||||||ƒ}| ¡ dkrœt	|||ƒ}|||fS )ab  
    Reads a video from a file, returning both the video frames and the audio frames

    Args:
    filename (str): path to the video file
    seek_frame_margin (double, optional): seeking frame in the stream is imprecise. Thus,
        when video_start_pts is specified, we seek the pts earlier by seek_frame_margin seconds
    read_video_stream (int, optional): whether read video stream. If yes, set to 1. Otherwise, 0
    video_width/video_height/video_min_dimension/video_max_dimension (int): together decide
        the size of decoded frames:

            - When video_width = 0, video_height = 0, video_min_dimension = 0,
                and video_max_dimension = 0, keep the original frame resolution
            - When video_width = 0, video_height = 0, video_min_dimension != 0,
                and video_max_dimension = 0, keep the aspect ratio and resize the
                frame so that shorter edge size is video_min_dimension
            - When video_width = 0, video_height = 0, video_min_dimension = 0,
                and video_max_dimension != 0, keep the aspect ratio and resize
                the frame so that longer edge size is video_max_dimension
            - When video_width = 0, video_height = 0, video_min_dimension != 0,
                and video_max_dimension != 0, resize the frame so that shorter
                edge size is video_min_dimension, and longer edge size is
                video_max_dimension. The aspect ratio may not be preserved
            - When video_width = 0, video_height != 0, video_min_dimension = 0,
                and video_max_dimension = 0, keep the aspect ratio and resize
                the frame so that frame video_height is $video_height
            - When video_width != 0, video_height == 0, video_min_dimension = 0,
                and video_max_dimension = 0, keep the aspect ratio and resize
                the frame so that frame video_width is $video_width
            - When video_width != 0, video_height != 0, video_min_dimension = 0,
                and video_max_dimension = 0, resize the frame so that frame
                video_width and  video_height are set to $video_width and
                $video_height, respectively
    video_pts_range (list(int), optional): the start and end presentation timestamp of video stream
    video_timebase (Fraction, optional): a Fraction rational number which denotes timebase in video stream
    read_audio_stream (int, optional): whether read audio stream. If yes, set to 1. Otherwise, 0
    audio_samples (int, optional): audio sampling rate
    audio_channels (int optional): audio channels
    audio_pts_range (list(int), optional): the start and end presentation timestamp of audio stream
    audio_timebase (Fraction, optional): a Fraction rational number which denotes time base in audio stream

    Returns
        vframes (Tensor[T, H, W, C]): the `T` video frames
        aframes (Tensor[L, K]): the audio frames, where `L` is the number of points and
            `K` is the number of audio_channels
        info (Dict): metadata for the video and audio. Can contain the fields video_fps (float)
            and audio_fps (int)
    r   r   )
r(   ÚtorchÚopsr
   Úread_video_from_filer   r   r2   r/   r:   )r=   r>   r?   r@   rA   rB   rC   rD   r   rE   rF   rG   r5   r!   ÚresultÚvframesÚ_vframe_ptsr)   r*   r+   r3   r4   r,   r-   r.   Úinfor   r   r   Ú_read_video_from_file{   s8    @írO   )r=   r   c                 C   s~   t jj | dddddddddddddddddd¡}|\
}}}}}}}}	}
}t||||	|
|ƒ}| ¡  ¡ }| ¡  ¡ }|||fS )zÉ
    Decode all video- and audio frames in the video. Only pts
    (presentation timestamp) is returned. The actual frame pixel data is not
    copied. Thus, it is much faster than read_video(...)
    r   r   r6   )rH   rI   r
   rJ   r2   ÚnumpyÚtolist)r=   rK   Ú_vframesÚ
vframe_ptsr)   r*   r+   Ú_aframesr4   r,   r-   r.   rN   r   r   r   Ú _read_video_timestamps_from_fileÛ   s4    írU   c           	      C   s4   t jj | ¡}|\}}}}}}t||||||ƒ}|S )zO
    Probe a video file and return VideoMetaData with info about the video
    )rH   rI   r
   Zprobe_video_from_filer2   )	r=   rK   r)   r*   r+   r,   r-   r.   rN   r   r   r   Ú_probe_video_from_fileþ   s    rV   )Ú
video_datar>   r?   r@   rA   rB   rC   rD   Úvideo_timebase_numeratorÚvideo_timebase_denominatorrE   rF   rG   r5   Úaudio_timebase_numeratorÚaudio_timebase_denominatorr   c                 C   sÈ   t |ƒ t |ƒ t| tjƒsNt ¡ �$ tjddd� tj| tjd�} W 5 Q R X tj	j
 | |d||||||d |d ||	|
|||d |d ||¡}|\
}}}}}}}}}}| ¡ dkrÀt|||ƒ}||fS )aÏ  
    Reads a video from memory, returning both the video frames as the audio frames
    This function is torchscriptable.

    Args:
    video_data (data type could be 1) torch.Tensor, dtype=torch.int8 or 2) python bytes):
        compressed video content stored in either 1) torch.Tensor 2) python bytes
    seek_frame_margin (double, optional): seeking frame in the stream is imprecise.
        Thus, when video_start_pts is specified, we seek the pts earlier by seek_frame_margin seconds
    read_video_stream (int, optional): whether read video stream. If yes, set to 1. Otherwise, 0
    video_width/video_height/video_min_dimension/video_max_dimension (int): together decide
        the size of decoded frames:

            - When video_width = 0, video_height = 0, video_min_dimension = 0,
                and video_max_dimension = 0, keep the original frame resolution
            - When video_width = 0, video_height = 0, video_min_dimension != 0,
                and video_max_dimension = 0, keep the aspect ratio and resize the
                frame so that shorter edge size is video_min_dimension
            - When video_width = 0, video_height = 0, video_min_dimension = 0,
                and video_max_dimension != 0, keep the aspect ratio and resize
                the frame so that longer edge size is video_max_dimension
            - When video_width = 0, video_height = 0, video_min_dimension != 0,
                and video_max_dimension != 0, resize the frame so that shorter
                edge size is video_min_dimension, and longer edge size is
                video_max_dimension. The aspect ratio may not be preserved
            - When video_width = 0, video_height != 0, video_min_dimension = 0,
                and video_max_dimension = 0, keep the aspect ratio and resize
                the frame so that frame video_height is $video_height
            - When video_width != 0, video_height == 0, video_min_dimension = 0,
                and video_max_dimension = 0, keep the aspect ratio and resize
                the frame so that frame video_width is $video_width
            - When video_width != 0, video_height != 0, video_min_dimension = 0,
                and video_max_dimension = 0, resize the frame so that frame
                video_width and  video_height are set to $video_width and
                $video_height, respectively
    video_pts_range (list(int), optional): the start and end presentation timestamp of video stream
    video_timebase_numerator / video_timebase_denominator (float, optional): a rational
        number which denotes timebase in video stream
    read_audio_stream (int, optional): whether read audio stream. If yes, set to 1. Otherwise, 0
    audio_samples (int, optional): audio sampling rate
    audio_channels (int optional): audio audio_channels
    audio_pts_range (list(int), optional): the start and end presentation timestamp of audio stream
    audio_timebase_numerator / audio_timebase_denominator (float, optional):
        a rational number which denotes time base in audio stream

    Returns:
        vframes (Tensor[T, H, W, C]): the `T` video frames
        aframes (Tensor[L, K]): the audio frames, where `L` is the number of points and
            `K` is the number of channels
    Úignoreú The given buffer is not writable©Úmessage©Zdtyper   r   )r(   Ú
isinstancerH   ÚTensorÚwarningsÚcatch_warningsÚfilterwarningsÚ
frombufferÚuint8rI   r
   Úread_video_from_memoryr/   r:   )rW   r>   r?   r@   rA   rB   rC   rD   rX   rY   rE   rF   rG   r5   rZ   r[   rK   rL   rM   r)   r*   r+   r3   r4   r,   r-   r.   r   r   r   Ú_read_video_from_memory  s>    E
íri   )rW   r   c                 C   s¼   t | tjƒs>t ¡ �$ tjddd� tj| tjd�} W 5 Q R X tjj	 
| dddddddddddddddddd¡}|\
}}}}}}}}	}
}t||||	|
|ƒ}| ¡  ¡ }| ¡  ¡ }|||fS )zÐ
    Decode all frames in the video. Only pts (presentation timestamp) is returned.
    The actual frame pixel data is not copied. Thus, read_video_timestamps(...)
    is much faster than read_video(...)
    r\   r]   r^   r`   r   r   r6   )ra   rH   rb   rc   rd   re   rf   rg   rI   r
   rh   r2   rP   rQ   )rW   rK   rR   rS   r)   r*   r+   rT   r4   r,   r-   r.   rN   r   r   r   Ú"_read_video_timestamps_from_memoryu  s<    
írj   c           	   	   C   sr   t | tjƒs>t ¡ �$ tjddd� tj| tjd�} W 5 Q R X tjj	 
| ¡}|\}}}}}}t||||||ƒ}|S )zy
    Probe a video in memory and return VideoMetaData with info about the video
    This function is torchscriptable
    r\   r]   r^   r`   )ra   rH   rb   rc   rd   re   rf   rg   rI   r
   Zprobe_video_from_memoryr2   )	rW   rK   r)   r*   r+   r,   r-   r.   rN   r   r   r   Ú_probe_video_from_memoryŸ  s    
rk   Úpts)r=   Ú	start_ptsÚend_ptsÚpts_unitr   c              	      sØ   ˆ d krt dƒ‰ ˆdkr"t d¡ t| ƒ}|j}|j}‡ ‡‡fdd„}d}t}	|rlt|jj	|jj
ƒ}	||	ƒ}d}
t}|r’t|jj	|jj
ƒ}||ƒ}
t| d||	d|
|d�\}}}i }|rÀ|j|d	< |rÎ|j|d
< |||fS )NÚinfrl   úmThe pts_unit 'pts' gives wrong results and will be removed in a follow-up version. Please use pts_unit 'sec'.c                    s`   ˆ}ˆ }ˆdkrHt t ˆd|   ¡ƒ}|tdƒkrHt t ˆ d|   ¡ƒ}|tdƒkrXd}||fS )NÚsecr   rp   r6   )r   ÚmathÚfloorr%   Úceil)Z	time_baseZstart_offsetÚ
end_offset©rn   ro   rm   r   r   Úget_ptsÅ  s    z_read_video.<locals>.get_ptsr<   T)r?   rD   r   rE   r5   r!   r   Z	audio_fps)r%   rc   ÚwarnrV   r   r    Údefault_timebaser   r   r   r   r!   rO   r   r#   )r=   rm   rn   ro   rN   r   r    rx   rD   r   r5   r!   rL   r3   Ú_infor   rw   r   Ú_read_video±  sF    ÿù	

r|   )r=   ro   r   c                    sd   |dkrt  d¡ t| ƒ\}}}|dkrLt|jj|jjƒ‰ ‡ fdd„|D ƒ}|jrX|jnd }||fS )Nrl   rq   rr   c                    s   g | ]}|ˆ  ‘qS r   r   )Ú.0Úx©Zvideo_time_baser   r   Ú
<listcomp>ü  s     z*_read_video_timestamps.<locals>.<listcomp>)	rc   ry   rU   r   r   r   r   r   r   )r=   ro   rl   Ú_rN   r   r   r   r   Ú_read_video_timestampsî  s    ÿr‚   )r;   r   r   r   r   r   r<   r   r   r   r   r   r<   r   r   )r   Nrl   )rl   )#rs   rc   Ú	fractionsr   Útypingr   r   r   r   r   rH   Ú	extensionr	   Z_HAS_VIDEO_OPTÚImportErrorÚOSErrorrz   r   r   r   r(   rb   r2   r:   Ústrr%   r$   rO   rU   rV   ri   rj   rk   r|   r‚   r   r   r   r   Ú<module>   sæ   

!	ù!  
þò

ñ`"#               ð

ïnþ+þ   ü
û> ÿ þ