
    khBQ              %          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 ddlmZ 	  ed       d	ZeZ ed d      Z G d d      Z G d d      Zdeeef   ddfdZde
j0                  de
j0                  de
j0                  de
j0                  de
j0                  de
j0                  defdZde
j0                  de
j0                  deeef   de
j0                  fdZdd	d d d d ded	d d defded ed!ed"ed#ed$ed%ed&eeef   d'ed(ed)ed*edeeef   d+edee
j0                  e
j0                  ef   fd,Zdedeee   ee   ef   fd-Zdedefd.Z 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d<d/e
j0                  d ed!ed"ed#ed$ed%ed&eeef   d0ed1ed(ed)ed*edeeef   d2ed3edee
j0                  e
j0                  f   f"d4Z!d/e
j0                  deee   ee   ef   fd5Z"d/e
j0                  defd6Z#	 	 	 d=ded7e	eef   d8ee	eef      d9edee
j0                  e
j0                  eeef   f   f
d:Z$	 d>ded9edee	ee   ee   f   ee   f   fd;Z%y# eef$ r d
ZY w xY w)?    N)Fraction)DictListOptionalTupleUnion   )_load_library   ) _raise_video_deprecation_warningvideo_readerTFc                   2    e Zd ZeedZddgZdededdfdZy)Timebase	numeratordenominatorr   r   returnNc                      || _         || _        y )Nr   )selfr   r   s      U/var/www/teggl/fontify/venv/lib/python3.12/site-packages/torchvision/io/_video_opt.py__init__zTimebase.__init__   s    
 #&    )__name__
__module____qualname__int__annotations__	__slots__r    r   r   r   r      s7    $'<Om,I'' ' 
	'r   r   c            	       2    e Zd ZeeeeeeeedZg dZddZy)VideoMetaData)	has_videovideo_timebasevideo_duration	video_fps	has_audioaudio_timebaseaudio_durationaudio_sample_rateNc                     d| _         t        dd      | _        d| _        d| _        d| _        t        dd      | _        d| _        d| _        y )NFr   r   g        )	r"   r   r#   r$   r%   r&   r'   r(   r)   )r   s    r   r   zVideoMetaData.__init__;   sK    &q!n!&q!n!!$r   )r   N)	r   r   r   boolr   floatr   r   r   r   r   r   r!   r!   %   s-    """	O	I%r   r!   	pts_ranger   c                 Z    | d   | d   cxkD  rdkD  rn y t        d| d    d| d          y )Nr   r   z=Start pts should not be smaller than end pts, got start pts: z and end pts: )
ValueError)r-   s    r   _validate_ptsr0   F   sG    |il&Q&KIVWL>Yghqrshtguv
 	
 'r   	vtimebasevfps	vduration	atimebaseasample_rate	adurationc                    t               }| j                         dkD  rt        t        | d   j	                               t        | d   j	                                     |_        | d   j	                         t        | d   j	                               z  }|j                         dkD  r(d|_        t        |j	                               |z  |_        |j                         dkD  rt        |j	                               |_	        |j                         dkD  rt        t        |d   j	                               t        |d   j	                                     |_
        |d   j	                         t        |d   j	                               z  }|j                         dkD  r(d|_        t        |j	                               |z  |_        |j                         dkD  rt        |j	                               |_        |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)   )r1   r2   r3   r4   r5   r6   metatimebases           r   
_fill_infor<   N   s    ?D1&s9Q<+<+<+>'?Yq\EVEVEXAYZQ<$$&y|/@/@/B)CC??q !DN"'	(8"9H"DDzz|atyy{+1&s9Q<+<+<+>'?Yq\EVEVEXAYZQ<$$&y|/@/@/B)CC??q !DN"'	(8"9H"DDa!&|'8'8':!;Kr   aframes
aframe_ptsaudio_pts_rangec                    |d   |d   }}| j                  d      }t        ||z
  dz         t        |      z  }d}|}||d   k  rt        |d   |z
  |z        }|d   dk7  r||d   kD  rt        |d   |z
  |z        }| ||d d f   S )Nr   r   )sizer,   r   )	r=   r>   r?   startendnum_samplesstep_per_aframes_idxe_idxs	            r   _align_audio_framesrI   n   s     A
23E,,q/KC%K!O,u[/AAOEEq!!_Q'%/?BCqRC/!*<$<_Q'#-@A5;>""r         ?r   rA   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_channelsr'   c                    t                t        |       t        |       t        j                  j                  j                  | |d||||||d   |d   |j                  |j                  |	|
||d   |d   |j                  |j                        }|\
  }}}}}}}}}}t        ||||||      }|j                         dkD  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   r0   torchopsr   read_video_from_filer   r   r<   r8   rI   )rL   rM   rN   rO   rP   rQ   rR   rS   r#   rT   rU   rV   r?   r'   resultvframes_vframe_ptsr1   r2   r3   r=   r>   r4   r5   r6   infos                             r   _read_video_from_filer_   }   s    @ %&/"/"YY##88	  ""  ""'F* qwmG[)T9gz9Vbdmiy)\9UD}}%gz?KGT!!r   c                 4   t         j                  j                  j                  | dddddddddddddddddd      }|\
  }}}}}}}}	}
}t	        ||||	|
|      }|j                         j                         }|j                         j                         }|||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   rA   )rX   rY   r   rZ   r<   numpytolist)rL   r[   _vframes
vframe_ptsr1   r2   r3   _aframesr>   r4   r5   r6   r^   s                r    _read_video_timestamps_from_filerf      s     YY##88								
						
		'F* rxnHj)T9h
IWceniy)\9UD!!#**,J!!#**,Jz4''r   c                     t                t        j                  j                  j	                  |       }|\  }}}}}}t        ||||||      }|S )zO
    Probe a video file and return VideoMetaData with info about the video
    )r   rX   rY   r   probe_video_from_filer<   )	rL   r[   r1   r2   r3   r4   r5   r6   r^   s	            r   _probe_video_from_fileri     sO     %&YY##99(CFEKBItY	<iy)\9UDKr   
video_datavideo_timebase_numeratorvideo_timebase_denominatoraudio_timebase_numeratoraudio_timebase_denominatorc                 0   t                t        |       t        |       t        | t        j                        sYt        j                         5  t        j                  dd       t        j                  | t        j                        } ddd       t        j                  j                  j                  | |d||||||d   |d   ||	|
|||d   |d   ||      }|\
  }}}}}}}}}}|j                         dkD  rt        |||      }||fS # 1 sw Y   xY w)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dtypeNr   r   )r   r0   
isinstancerX   Tensorwarningscatch_warningsfilterwarnings
frombufferuint8rY   r   read_video_from_memoryr8   rI   )rj   rM   rN   rO   rP   rQ   rR   rS   rk   rl   rT   rU   rV   r?   rm   rn   r[   r\   r]   r1   r2   r3   r=   r>   r4   r5   r6   s                              r   _read_video_from_memoryr~     s&   J %&/"/"j%,,/$$& 	I##H6XY))*EKKHJ	I
 YY##::	 " "'F, qwmG[)T9gz9Vbdm}}%gz?KGC	I 	Is   =DDc                 F   t        | t        j                        sYt        j                         5  t        j
                  dd       t        j                  | t        j                        } ddd       t        j                  j                  j                  | dddddddddddddddddd      }t                |\
  }}}}}}}}	}
}t        ||||	|
|      }|j                         j                         }|j                         j                         }|||fS # 1 sw Y   xY w)	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(...)
    rp   rq   rr   rt   Nr   r   rA   )rv   rX   rw   rx   ry   rz   r{   r|   rY   r   r}   r   r<   ra   rb   )rj   r[   rc   rd   r1   r2   r3   re   r>   r4   r5   r6   r^   s                r   "_read_video_timestamps_from_memoryr   z  s&    j%,,/$$& 	I##H6XY))*EKKHJ	I YY##::								
						
		'F* %&qwnHj)T9h
IWceniy)\9UD!!#**,J!!#**,Jz4''?	I 	Is   =DD c                    t                t        | t        j                        sYt	        j
                         5  t	        j                  dd       t        j                  | t        j                        } ddd       t        j                  j                  j                  |       }|\  }}}}}}t        ||||||      }|S # 1 sw Y   MxY w)zy
    Probe a video in memory and return VideoMetaData with info about the video
    This function is torchscriptable
    rp   rq   rr   rt   N)r   rv   rX   rw   rx   ry   rz   r{   r|   rY   r   probe_video_from_memoryr<   )	rj   r[   r1   r2   r3   r4   r5   r6   r^   s	            r   _probe_video_from_memoryr     s     %&j%,,/$$& 	I##H6XY))*EKKHJ	I YY##;;JGFEKBItY	<iy)\9UDK	I 	Is   =CC	start_ptsend_ptspts_unitc           	      T   t                t        d      dk(  rt        j                  d       t	        |       }|j
                  }|j                  }fd}d}t        }	|r<t        |j                  j                  |j                  j                        }	 ||	      }d}
t        }|r<t        |j                  j                  |j                  j                        } ||      }
t        | d||	d|
|      \  }}}i }|r|j                  |d<   |r|j                  |d	<   |||fS )
NinfptsmThe pts_unit 'pts' gives wrong results and will be removed in a follow-up version. Please use pts_unit 'sec'.c                     }}dk(  rVt        t        j                  d| z  z              }|t        d      k7  r$t        t        j                  d| z  z              }|t        d      k(  rd}||fS )Nsecr   r   rA   )r   mathfloorr,   ceil)	time_basestart_offset
end_offsetr   r   r   s      r   get_ptsz_read_video.<locals>.get_pts  su     
utzz)q9}*EFGLU5\) 7a)m+D!EF
u%JZ''r   rK   T)rN   rS   r#   rT   r?   r'   r%   	audio_fps)r   r,   rx   warnri   r"   r&   default_timebaser   r#   r   r   r'   r_   r%   r)   )rL   r   r   r   r^   r"   r&   r   rS   r#   r?   r'   r\   r=   _infos    ```           r   _read_videor     s+    %&,5>	

 "(+DII	( O%N!$"5"5"?"?ATATA`A`a!.1O%N!$"5"5"?"?ATATA`A`a!.12'%'%GWd E!^^k!33kGU""r   c                 D   t                |dk(  rt        j                  d       t        |       \  }}}|dk(  rFt	        |j
                  j                  |j
                  j                        }|D cg c]  }||z  	 }}|j                  r|j                  nd }||fS c c}w )Nr   r   r   )
r   rx   r   rf   r   r#   r   r   r"   r%   )rL   r   r   _r^   video_time_basexr%   s           r   _read_video_timestampsr     s     %&5>	
 4H=LCD5"4#6#6#@#@$BUBUBaBab,/0qq?"00"&..dI	>	 1s   1B)rJ   r   r   r   r   r   rK   r   r   r   r   r   rK   r   r   )r   Nr   )r   )&r   rx   	fractionsr   typingr   r   r   r   r   rX   	extensionr
   _video_deprecation_warningr   _HAS_CPU_VIDEO_DECODERImportErrorOSError_HAS_VIDEO_OPTr   r   r!   r   r0   rw   r<   rI   strr,   r+   r_   rf   ri   r~   r   r   r   r   r   r   r   <module>r      sj      5 5  % H#.!! (Aq> 

' 
'% %B
U38_ 
 
||
,, || ||	
 ,, || @#\\#',||#FKCQTHo#
\\#"  $"  './"'./^"^"^" ^" 	^"
 ^" ^" ^" 38_^" ^" ^" ^" ^" 38_^" ^" 5<<}45^"B (s  (uT#YS	S`=`7a  (FS ]   $  '.$%&''.$%&'!kkk k 	k
 k k k 38_k "k !$k k k k 38_k "k  !$!k" 5<<%&#k\((((
49d3i./((V* )*04	;#;#UH_%;# eE8O,-;# 	;#
 5<<tCJ'778;#~ $) 
5cDN*+Xe_<=O 	W #"#s   
I 	II