o
    VhG                     @   s  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 dlmZm	Z	m
Z
mZmZmZ d dlZd dlZddlmZ ddlmZ ddlmZ z,d dlZejejj eejjjd	s]ed
Zzej Z W n e!yn   ej"Z Y nw W n ey}   edZY nw d=ddZ#de$fddZ%d a&da'						d>de(dej)de*de(dee	e(ef  deej) dee* dee( dee	e(ef  ddfddZ+ddd e*d!e*d"e(d#d$d%e	e(eee,ee,d&f e
e, f  f de
d' fd(d)Z-d*ej)d+e
d' d,e,d-e*dej)f
d.d/Z.	 		0	1d?de(d2ee*ef d3eee*ef  d"e(d4e(deej)ej)e	e(ef f fd5d6Z/ddde$fd7d8Z0ddde
e, fd9d:Z1d@de(d"e(dee
e, ee* f fd;d<Z2dS )A    N)Fraction)AnyDictListOptionalTupleUnion   )_log_api_usage_once   )
_video_opt) _raise_video_deprecation_warning	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.
zPyAV 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.
returnc                   C   s   t ttrtd S N
isinstanceav	Exception r   r   H/var/www/vscode/kcb/lib/python3.10/site-packages/torchvision/io/video.py_check_av_available,   s   
r   c                   C   s   t tt S r   r   r   r   r   r   _av_available1   s   r   
   libx264filenamevideo_arrayfpsvideo_codecoptionsaudio_array	audio_fpsaudio_codecaudio_optionsc	                 C   s  t   tj stj stt t  tj|tj	dj
dd}t|tr*t|}tj| dd}	|	j||d}
|jd |
_|jd |
_|d	krKd
nd|
_|pQi |
_|durddddddddddd
}|	j||d}|pni |_|jd }|dkr{dnd}|	jjd jj}t|| }t|j
dd|}tjj|||d}||_| |D ]}|	!| q|  D ]}|	!| q|D ]0}tj"j|dd}zd|_#W n t$y   ddl%m&} |j'|_#Y nw |
 |D ]}|	!| qq|
  D ]}|	!| qW d   dS 1 sw   Y  dS )a~  
    [DEPRECATED] Writes a 4d tensor in [T, H, W, C] format in a video file.

    .. warning::

        DEPRECATED: All the video decoding and encoding capabilities of torchvision
        are deprecated from version 0.22 and will be removed in version 0.24.  We
        recommend that you migrate to
        `TorchCodec <https://github.com/pytorch/torchcodec>`__, where we'll
        consolidate the future decoding/encoding capabilities of PyTorch

    This function relies on PyAV (therefore, ultimately FFmpeg) to encode
    videos, you can get more fine-grained control by referring to the other
    options at your disposal within `the FFMpeg wiki
    <http://trac.ffmpeg.org/wiki#Encoding>`_.

    Args:
        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): video frames per second
        video_codec (str): the name of the video codec, i.e. "libx264", "h264", etc.
        options (Dict): dictionary containing options to be passed into the PyAV video stream.
            The list of options is codec-dependent and can all
            be found from `the FFMpeg wiki <http://trac.ffmpeg.org/wiki#Encoding>`_.
        audio_array (Tensor[C, N]): tensor containing the audio, where C is the number of channels
            and N is the number of samples
        audio_fps (Number): audio sample rate, typically 44100 or 48000
        audio_codec (str): the name of the audio codec, i.e. "mp3", "aac", etc.
        audio_options (Dict): dictionary containing options to be passed into the PyAV audio stream.
            The list of options is codec-dependent and can all
            be found from `the FFMpeg wiki <http://trac.ffmpeg.org/wiki#Encoding>`_.

    Examples::
        >>> # Creating libx264 video with CRF 17, for visually lossless footage:
        >>>
        >>> from torchvision.io import write_video
        >>> # 1000 frames of 100x100, 3-channel image.
        >>> vid = torch.randn(1000, 100, 100, 3, dtype = torch.uint8)
        >>> write_video("video.mp4", options = {"crf": "17"})

    dtypeT)forcew)mode)rater	   r   
libx264rgbyuv420prgb24Nz<f8z<f4z<i2z<i4u1)
dbldblpfltfltps16s16ps32s32pu8u8pr   stereomono)formatlayout)r:   NONE)PictureType)(r   torchjitis_scripting
is_tracingr
   write_videor   	as_tensoruint8numpyr   floatnproundr   open
add_streamshapewidthheightpix_fmtr   streamsaudior:   namer%   astype
AudioFramefrom_ndarraysample_rateencodemux
VideoFramer   	TypeErrorav.video.framer=   r<   )r   r   r   r   r   r    r!   r"   r#   	containerstreamaudio_format_dtypesa_streamnum_channelsaudio_layoutaudio_sample_fmtformat_dtypeframepacketimgr=   r   r   r   rB   :   sj   5





$rB   r[   zav.container.Containerstart_offset
end_offsetpts_unitr\   zav.stream.Streamstream_name.zav.frame.Framec                    s  t d7 a t t td krt  |dkr3ttd|j   tdkr2tt	 d|j   nt
d i d}d}|jdkrs|jj}|rsd|v rs|d}	||	d  }
td	|
}|d u rhtd
|
}|d urs|ddk}}t|d d}|rt|| d}z| j|dd|d W n ty   g  Y S w d}z(t| jdi |D ]\}}||j< |j kr|r||k r|d7 }q nqW n	 ty   Y nw  fddtD }tdkr
dkr
vr
fddD }t|dkr
t|}|d|  |S )Nr   secinfzBThe pts_unit 'pts' gives wrong results. Please use pts_unit 'sec'.T   videos   DivXs   DivX(\d+)Build(\d+)(\w)s   DivX(\d+)b(\d+)(\w)      pr   F)	any_framebackwardr\   c                    s2   g | ]}| j   kr krn n| qS r   pts.0irg   framesrf   r   r   
<listcomp>   s   2 z%_read_from_stream.<locals>.<listcomp>c                    s   g | ]}| k r|qS r   r   rt   )rf   r   r   ry          r   )_CALLED_TIMES_GC_COLLECTION_INTERVALgccollectintmathfloor	time_baserF   ceilwarningswarntypecodec_context	extradatafindresearchgroupmaxseekFFmpegError	enumeratedecoders   sortedleninsert)r[   rf   rg   rh   r\   ri   should_buffermax_buffer_sizer   posdoseek_offsetbuffer_count_idxrc   resultpreceding_framesfirst_frame_ptsr   rw   r   _read_from_stream   sh   	




"r   aframesaudio_frames	ref_startref_endc           
      C   s|   |d j |d j }}| jd }|| d | }d}|}	||k r(t|| | }||kr4t|| | }	| d d ||	f S )Nr   r   )rs   rK   r   )
r   r   r   r   startendtotal_aframesstep_per_aframes_idxe_idxr   r   r   _align_audio_frames  s   
r   rs   THWC	start_ptsend_ptsoutput_formatc              	   C   s  t   tj stj stt | }|dvr!td| dddl	m
} | dkrEtj| s9td|  t| |||\}}}nt  |du rPtd	}||k r^td
| d| i }g }	g }
tj}zdtj| ddS}|jjr{|jjd j}|jjrt|||||jjd ddi}	|jjd j}|durt||d< |jjrt|||||jjd ddi}
|jjd j|d< W d   n1 sw   Y  W n	 ty   Y nw dd |	D }dd |
D }|rtt |}ntj!dtj"d}|r.t#|d}t|}|dkr&t$t%&|d|  }|td	kr&t$t%'|d|  }t(||
||}ntj!dtj)d}|dkrC|*dddd}|||fS )a  [DEPRECATED] Reads a video from a file, returning both the video frames and the audio frames

    .. warning::

        DEPRECATED: All the video decoding and encoding capabilities of torchvision
        are deprecated from version 0.22 and will be removed in version 0.24.  We
        recommend that you migrate to
        `TorchCodec <https://github.com/pytorch/torchcodec>`__, where we'll
        consolidate the future decoding/encoding capabilities of PyTorch

    Args:
        filename (str): path to the video file. If using the pyav backend, this can be whatever ``av.open`` accepts.
        start_pts (int if pts_unit = 'pts', float / Fraction if pts_unit = 'sec', optional):
            The start presentation time of the video
        end_pts (int if pts_unit = 'pts', 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'.
        output_format (str, optional): The format of the output video tensors. Can be either "THWC" (default) or "TCHW".

    Returns:
        vframes (Tensor[T, H, W, C] or Tensor[T, C, H, W]): 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   TCHWz5output_format should be either 'THWC' or 'TCHW', got .r   get_video_backendpyavzFile not found: Nrk   z7end_pts should be larger than start_pts, got start_pts=z and end_pts=ignoremetadata_errorsrm   	video_fpsrP   r!   c                 S   s   g | ]}|   qS r   )to_rgb
to_ndarrayru   rc   r   r   r   ry   q  rz   zread_video.<locals>.<listcomp>c                 S   s   g | ]}|  qS r   )r   r   r   r   r   ry   r      )r   r   r   rn   r$   r   rj   )r   r   r   rn   r	   )+r   r>   r?   r@   rA   r
   
read_videoupper
ValueErrortorchvisionr   ospathexistsRuntimeErrorr   _read_videor   rF   default_timebaser   rI   rO   rP   r   rm   r   average_rater)   r   rC   rG   stackemptyrD   concatenater   r   r   r   r   float32permute)r   r   r   rh   r   r   vframesr   infovideo_framesr   audio_timebaser[   r   vframes_listaframes_listr   r   r   r     s    






r   c                 C   s*   | j d jj}|d u rdS d|v rdS dS )Nr   Fs   LavcT)rO   r   r   )r[   r   r   r   r   !_can_read_timestamps_from_packets  s   r   c                 C   s4   t | rdd | jddD S dd | jddD S )Nc                 S      g | ]
}|j d ur|j qS r   rr   ru   xr   r   r   ry         z,_decode_video_timestamps.<locals>.<listcomp>r   )rm   c                 S   r   r   rr   r   r   r   r   ry     r   )r   demuxr   )r[   r   r   r   _decode_video_timestamps  s   r   c           	   
      sD  t   tj stj stt ddlm} | dkr"t	
| |S t  d}g }zCtj| dd2}|jjr\|jjd }|j zt|}W n tyV   td|   Y nw t|j}W d   n1 sfw   Y  W n  ty } zd|  d	| }t|t W Y d}~nd}~ww |  |d
kr fdd|D }||fS )a  [DEPREACTED] List the video frames timestamps.

    .. warning::

        DEPRECATED: All the video decoding and encoding capabilities of torchvision
        are deprecated from version 0.22 and will be removed in version 0.24.  We
        recommend that you migrate to
        `TorchCodec <https://github.com/pytorch/torchcodec>`__, where we'll
        consolidate the future decoding/encoding capabilities of PyTorch

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

    Args:
        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 (float, optional): the frame rate for the video

    r   r   r   Nr   r   z Failed decoding frames for file zFailed to open container for z; Caught error: rj   c                    s   g | ]}|  qS r   r   r   video_time_baser   r   ry     r   z)read_video_timestamps.<locals>.<listcomp>)r   r>   r?   r@   rA   r
   read_video_timestampsr   r   r   _read_video_timestampsr   r   rI   rO   rm   r   r   r   r   r   rF   r   RuntimeWarningsort)	r   rh   r   r   rs   r[   video_streamemsgr   r   r   r     s@   

	r   )r   N)r   NNNNN)r   Nrs   r   rr   )3r}   r   r   r   r   	fractionsr   typingr   r   r   r   r   r   rE   rG   r>   utilsr
    r   _video_deprecation_warningr   r   logging	set_levelERRORhasattrrm   rc   rX   ImportErrorr   AttributeErrorAVErrorr   boolr   r{   r|   strTensorrF   rB   r   r   r   r   r   r   r   r   r   r   r   <module>   s     	



	

y$
P


y	,