
    QЦiO              %          S SK r S SKrS SKJr  S SKJrJrJrJrJ	r	  S SK
r
SSKJr   \" S5        Sr\r\" S S	5      r " S
 S5      r " S S5      rS\\\4   SS4S jrS\
R,                  S\
R,                  S\
R,                  S\
R,                  S\
R,                  S\
R,                  S\4S jrS\
R,                  S\
R,                  S\\\4   S\
R,                  4S jrSSS S S S S\SS S S\4S\S\S \S!\S"\S#\S$\S%\\\4   S&\S'\S(\S)\S\\\4   S*\S\\
R,                  \
R,                  \4   4S+ jjrS\S\\\   \\   \4   4S, jrS\S\4S- jr               S;S.\
R,                  S\S \S!\S"\S#\S$\S%\\\4   S/\S0\S'\S(\S)\S\\\4   S1\S2\S\\
R,                  \
R,                  4   4"S3 jjrS.\
R,                  S\\\   \\   \4   4S4 jr S.\
R,                  S\4S5 jr!   S<S\S6\	\\4   S7\\	\\4      S8\S\\
R,                  \
R,                  \\\4   4   4
S9 jjr" S=S\S8\S\\	\\   \\   4   \\   4   4S: jjr#g! \\4 a    Sr GN&f = f)>    N)Fraction)DictListOptionalTupleUnion   )_load_libraryvideo_readerTF   c                   <    \ rS rSr\\S.rSS/rS\S\SS4S jrSrg)	Timebase   	numeratordenominatorr   r   returnNc                     Xl         X l        g )Nr   )selfr   r   s      X/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/torchvision/io/_video_opt.py__init__Timebase.__init__   s    
 #&    )r   r   )	__name__
__module____qualname____firstlineno__int__annotations__	__slots__r   __static_attributes__ r   r   r   r      s7    $'<Om,I'' ' 
	'r   r   c            	       <    \ rS rSr\\\\\\\\S.r/ SQrSS jr	Sr
g)VideoMetaData$   )	has_videovideo_timebasevideo_duration	video_fps	has_audioaudio_timebaseaudio_durationaudio_sample_rateNc                     SU l         [        SS5      U l        SU l        SU l        SU l        [        SS5      U l        SU l        SU l        g )NFr   r   g        )	r&   r   r'   r(   r)   r*   r+   r,   r-   )r   s    r   r   VideoMetaData.__init__:   sK    &q!n!&q!n!!$r   )r,   r-   r+   r*   r&   r(   r)   r'   )r   N)r   r   r   r   boolr   floatr   r    r   r!   r"   r   r   r$   r$   $   s-    """	O	I%r   r$   	pts_ranger   c                 ^    U S   U S   s=:  a  S:  a  O  g [        SU S    SU S    35      eg )Nr   r   z=Start pts should not be smaller than end pts, got start pts: z and end pts: )
ValueError)r2   s    r   _validate_ptsr5   E   sG    |il&Q&KIVWL>Yghqrshtguv
 	
 'r   	vtimebasevfps	vduration	atimebaseasample_rate	adurationc                    [        5       nU R                  5       S:  a  [        [        U S   R	                  5       5      [        U S   R	                  5       5      5      Ul        U S   R	                  5       [        U S   R	                  5       5      -  nUR                  5       S:  a(  SUl        [        UR	                  5       5      U-  Ul        UR                  5       S:  a  [        UR	                  5       5      Ul	        UR                  5       S:  a  [        [        US   R	                  5       5      [        US   R	                  5       5      5      Ul
        US   R	                  5       [        US   R	                  5       5      -  nUR                  5       S:  a(  SUl        [        UR	                  5       5      U-  Ul        UR                  5       S:  a  [        UR	                  5       5      Ul        U$ )z=
Build update VideoMetaData struct with info about the video
r   r   T)r$   numelr   r   itemr'   r1   r&   r(   r)   r+   r*   r,   r-   )r6   r7   r8   r9   r:   r;   metatimebases           r   
_fill_inforA   M   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                    US   US   pCU R                  S5      n[        XC-
  S-   5      [        U5      -  nSnUnX2S   :  a  [        US   U-
  U-  5      nUS   S:w  a  XBS   :  a  [        US   U-
  U-  5      nXU2S S 24   $ )Nr   r   )sizer1   r   )	rB   rC   rD   startendnum_samplesstep_per_aframes_idxe_idxs	            r   _align_audio_framesrN   m   s     A
23,,q/KCK!O,u[/AAOEEq!!_Q'%/?BCqRC!*<$<_Q'#-@A;>""r         ?r   rF   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                    [        U5        [        U5        [        R                  R                  R	                  U USUUUUUUS   US   UR
                  UR                  U	U
UUS   US   UR
                  UR                  5      nUu
  nnnnnnnnnn[        UUUUUU5      nUR                  5       S:  a  [        UUU5      nUUU4$ )a  
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   )
r5   torchopsr   read_video_from_filer   r   rA   r=   rN   )rQ   rR   rS   rT   rU   rV   rW   rX   r'   rY   rZ   r[   rD   r+   resultvframes_vframe_ptsr6   r7   r8   rB   rC   r9   r:   r;   infos                             r   _read_video_from_filerd   |   s    @ /"/"YY##88	  ""  ""'F* qwmG[)T9gz9Vbdmiy)\9UD}}%gz?KGT!!r   c                 "   [         R                  R                  R                  U SSSSSSSSSSSSSSSSSS5      nUu
  p#pEpgpp[	        XEXiX5      nUR                  5       R                  5       nUR                  5       R                  5       nX8U4$ )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   rF   )r]   r^   r   r_   rA   numpytolist)rQ   r`   _vframes
vframe_ptsr6   r7   r8   _aframesrC   r9   r:   r;   rc   s                r    _read_video_timestamps_from_filerk      s     YY##88								
						
		'F* rxnH)9
Wciy\UD!!#**,J!!#**,J4''r   c                 ~    [         R                  R                  R                  U 5      nUu  p#pEpg[	        X#XEXg5      nU$ )zG
Probe a video file and return VideoMetaData with info about the video
)r]   r^   r   probe_video_from_filerA   )	rQ   r`   r6   r7   r8   r9   r:   r;   rc   s	            r   _probe_video_from_filern      s=     YY##99(CFEKBIY<iy\UDKr   
video_datavideo_timebase_numeratorvideo_timebase_denominatoraudio_timebase_numeratoraudio_timebase_denominatorc                 ,   [        U5        [        U5        [        U [        R                  5      (       dV  [        R
                  " 5          [        R                  " SSS9  [        R                  " U [        R                  S9n SSS5        [        R                  R                  R                  U USUUUUUUS   US   UU	U
UUUS   US   UU5      nUu
  nnnnnnnnnnUR                  5       S:  a  [        UUU5      nUU4$ ! , (       d  f       N= f)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   )r5   
isinstancer]   Tensorwarningscatch_warningsfilterwarnings
frombufferuint8r^   r   read_video_from_memoryr=   rN   )ro   rR   rS   rT   rU   rV   rW   rX   rp   rq   rY   rZ   r[   rD   rr   rs   r`   ra   rb   r6   r7   r8   rB   rC   r9   r:   r;   s                              r   _read_video_from_memoryr   	  s   J /"/"j%,,//$$&##H6XY))*EKKHJ '
 YY##::	 " "'F, qwmG[)T9gz9Vbdm}}%gz?KGC '&s   9D
Dc                 .   [        U [        R                  5      (       dV  [        R                  " 5          [        R
                  " SSS9  [        R                  " U [        R                  S9n SSS5        [        R                  R                  R                  U SSSSSSSSSSSSSSSSSS5      nUu
  p#pEpgpp[        XEXiX5      nUR                  5       R                  5       nUR                  5       R                  5       nX8U4$ ! , (       d  f       N= f)	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(...)
ru   rv   rw   ry   Nr   r   rF   )r{   r]   r|   r}   r~   r   r   r   r^   r   r   rA   rf   rg   )ro   r`   rh   ri   r6   r7   r8   rj   rC   r9   r:   r;   rc   s                r   "_read_video_timestamps_from_memoryr   v  s    j%,,//$$&##H6XY))*EKKHJ ' YY##::								
						
		'F* rxnH)9
Wciy\UD!!#**,J!!#**,J4''= '&s   9D
Dc                    [        U [        R                  5      (       dV  [        R                  " 5          [        R
                  " SSS9  [        R                  " U [        R                  S9n SSS5        [        R                  R                  R                  U 5      nUu  p#pEpg[        X#XEXg5      nU$ ! , (       d  f       NL= f)zm
Probe a video in memory and return VideoMetaData with info about the video
This function is torchscriptable
ru   rv   rw   ry   N)r{   r]   r|   r}   r~   r   r   r   r^   r   probe_video_from_memoryrA   )	ro   r`   r6   r7   r8   r9   r:   r;   rc   s	            r   _probe_video_from_memoryr     s     j%,,//$$&##H6XY))*EKKHJ ' YY##;;JGFEKBIY<iy\UDK '&s   9B44
C	start_ptsend_ptspts_unitc           
      f  ^^^ Tc  [        S5      mTS:X  a  [        R                  " S5        [        U 5      nUR                  nUR
                  nUUU4S jnSn[        n	U(       a<  [        UR                  R                  UR                  R                  5      n	U" U	5      nSn
[        nU(       a<  [        UR                  R                  UR                  R                  5      nU" U5      n
[        U SUU	SU
US9u  pn0 nU(       a  UR                  US'   U(       a  UR                  US	'   XU4$ )
NinfptsmThe pts_unit 'pts' gives wrong results and will be removed in a follow-up version. Please use pts_unit 'sec'.c                    > TnTnTS:X  aY  [        [        R                  " TSU -  -  5      5      nU[        S5      :w  a%  [        [        R                  " TSU -  -  5      5      nU[        S5      :X  a  SnX4$ )Nsecr   r   rF   )r   mathfloorr1   ceil)	time_basestart_offset
end_offsetr   r   r   s      r   get_pts_read_video.<locals>.get_pts  ss     
utzz)q9}*EFGLU5\) 7a)m+D!EF
u%J''r   rP   T)rS   rX   r'   rY   rD   r+   r)   	audio_fps)r1   r}   warnrn   r&   r*   default_timebaser   r'   r   r   r+   rd   r)   r-   )rQ   r   r   r   rc   r&   r*   r   rX   r'   rD   r+   ra   rB   _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'%'%Gd E!^^k!33kU""r   c                 @   US:X  a  [         R                  " S5        [        U 5      u  p#nUS:X  aH  [        UR                  R
                  UR                  R                  5      nU Vs/ s H  ofU-  PM	     nnUR                  (       a  UR                  OS nX'4$ s  snf )Nr   r   r   )	r}   r   rk   r   r'   r   r   r&   r)   )rQ   r   r   _rc   video_time_basexr)   s           r   _read_video_timestampsr     s     5>	
 4H=LCD5"4#6#6#@#@$BUBUBaBab,/0Cq?"C0"&..dI>	 1s   )B)rO   r   r   r   r   r   rP   r   r   r   r   r   rP   r   r   )r   Nr   )r   )$r   r}   	fractionsr   typingr   r   r   r   r   r]   	extensionr
   _HAS_CPU_VIDEO_DECODERImportErrorOSError_HAS_VIDEO_OPTr   r   r$   r   r5   r|   rA   rN   strr1   r0   rd   rk   rn   r   r   r   r   r   r"   r   r   <module>r      sg      5 5  %#.!! (Aq> 

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