
    \j                         d dl Z d dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZ e j                   e j"                   G d	 d
e                    Zy)    N)libav)Packet)Stream)avrational_to_fraction)
VideoFrame)int32_t)memcpyc                   "   e Zd Zd Zd Zej                  ddedz  fd       Zej                  dde	dz  fd       Z
ej                  d        Zej                  d	        Zd
 ZddZed        Zed        Zed        Zed        Zed        Zy)VideoStreamc                 N   | j                   d| j                   dt        |       ddS d| j                   d| j                   d| j                  r| j                  j                  nd  d| j                   j
                   d| j                   j                   dt        |       ddS )Nz<av.VideoStream #z video/<nocodec> at 0xx> z, z at 0x)codec_contextindexidnameformatwidthheightselfs    B/root/.hermes/venv/lib/python3.12/site-packages/av/video/stream.py__repr__zVideoStream.__repr__   s    %&tzzl2HDRSTUVV

|1TYYKr#';;t{{D94;M;M;S;S:TTU!!(()4|1>	
    c                     |dv r%t        dt        |       j                   d| d      | j                  %t        dt        |       j                   d| d      t	        | j                  |      S )N)	frameraterate'z' object has no attribute ')AttributeErrortype__name__r   getattr)r   r   s     r   __getattr__zVideoStream.__getattr__   s    (( DJ''((CD6K  % DJ''((CD6K  t))400r   Nframec                     | j                   j                  |      }|D ].  }| |_        | j                  j                  |j                  _        0 |S )z
        Encode an :class:`.VideoFrame` and return a list of :class:`.Packet`.

        :rtype: list[Packet]

        .. seealso:: This is mostly a passthrough to :meth:`.CodecContext.encode`.
        )r   encode_streamptrr   stream_index)r   r%   packetspackets       r   r'   zVideoStream.encode"   sK     $$++E2 	5F!FN&*hhnnFJJ#	5 r   r,   c                 8    | j                   j                  |      S )z
        Decode a :class:`.Packet` and return a list of :class:`.VideoFrame`.

        :rtype: list[VideoFrame]

        .. seealso:: This is a passthrough to :meth:`.CodecContext.decode`.
        )r   decode)r   r,   s     r   r.   zVideoStream.decode3   s     !!((00r   c                     t        j                  |        | j                  | j                  r| j	                          y y y N)r   _finalize_for_outputr   _has_display_matrix_apply_display_matrixr   s    r   r1   z VideoStream._finalize_for_output>   s;    ##D) )d.F.F&&( /G)r   c                    dt        j                  t              z  }t        j                  t        j
                  | j                  j                  j                        t        j
                  | j                  j                  j                        t        j                  |d      }|t         j                  k(  rt        d      t        |j                  | j                  |       y )N	   r   z+could not allocate display matrix side data)cythonsizeofr   libav_packet_side_data_newaddressr)   codecparcoded_side_datanb_coded_side_dataAV_PKT_DATA_DISPLAYMATRIXNULLMemoryErrorr	   data_display_matrix)r   nsds      r   r3   z!VideoStream._apply_display_matrixF   s    FMM'22363N3NNN488,,<<=NN488,,??@))4
 KLLrww,,a0r   c                     |d| _         y|D cg c]  }t        |       }}t        |      dk7  rt        d      t	        d      D ]  }||   | j
                  |<    d| _         yc c}w )aB  Set the display matrix written to the container as coded side data.

        ``matrix`` is a sequence of 9 integers in FFmpeg's ``AV_PKT_DATA_DISPLAYMATRIX``
        layout, or ``None`` to clear. Must be called before the first frame is
        encoded. See :meth:`set_display_rotation` for a higher-level helper.
        NFr5   z+display matrix must have exactly 9 elementsT)r2   intlen
ValueErrorrangerB   )r   matrixvvalsis        r   set_display_matrixzVideoStream.set_display_matrixU   sv     >',D$ &'1A''t9>JKKq 	.A&*1gD  #	.#'  (s   A&c                     t        j                  | j                  t        |              t        j                  | j                  t        |      t        |             d| _        y)ah  Set the container display matrix from a rotation and optional flips.

        ``degrees`` is a counter-clockwise rotation (matching the value read back
        from :attr:`VideoFrame.rotation`); ``hflip`` / ``vflip`` mirror after it.
        Together these express all eight EXIF orientations. Must be called before
        the first frame is encoded.
        TN)r8   av_display_rotation_setrB   floatav_display_matrix_flipboolr2   )r   degreeshflipvflips       r   set_display_rotationz VideoStream.set_display_rotationh   sH     	##D$8$85>/J""4#7#7ed5kR#' r   c                 f    t        t        j                  | j                  j                              S )z
        The average frame rate of this video stream.

        This is calculated when the file is opened by looking at the first
        few frames and averaging their rate.

        :type: fractions.Fraction | None
        )r   r6   r:   r)   avg_frame_rater   s    r   average_ratezVideoStream.average_ratev   s"     &fnnTXX5L5L&MNNr   c                 f    t        t        j                  | j                  j                              S )a  
        The base frame rate of this stream.

        This is calculated as the lowest framerate at which the timestamps of
        frames can be represented accurately. See :ffmpeg:`AVStream.r_frame_rate`
        for more.

        :type: fractions.Fraction | None
        )r   r6   r:   r)   r_frame_rater   s    r   	base_ratezVideoStream.base_rate   s"     &fnnTXX5J5J&KLLr   c                     t        j                  t        j                  | j                  t        j                        }t        t        j                  |            S )zThe guessed frame rate of this stream.

        This is a wrapper around :ffmpeg:`av_guess_frame_rate`, and uses multiple
        heuristics to decide what is "the" frame rate.

        :type: fractions.Fraction | None
        )r8   av_guess_frame_rater6   r?   r)   r   r:   )r   vals     r   guessed_ratezVideoStream.guessed_rate   s=     "55KK6;;
 &fnnS&9::r   c                     t        j                  | j                  j                  | j                  t        j
                        }t        t	        j                  |            S )a  The guessed sample aspect ratio (SAR) of this stream.

        This is a wrapper around :ffmpeg:`av_guess_sample_aspect_ratio`, and uses multiple
        heuristics to decide what is "the" sample aspect ratio.

        :type: fractions.Fraction | None
        )r8   av_guess_sample_aspect_ratio	containerr)   r6   r?   r   r:   )r   sars     r   sample_aspect_ratiozVideoStream.sample_aspect_ratio   sC     ">>NN&++
 &fnnS&9::r   c                    t        j                  t        j                        }t        j                  t        j
                  |j                        t        j
                  |j                        | j                  j                  | j                  j                  z  | j                  j                  | j                  j                  z  d       t        t        j
                  |            S )zThe guessed display aspect ratio (DAR) of this stream.

        This is calculated from :meth:`.VideoStream.guessed_sample_aspect_ratio`.

        :type: fractions.Fraction | None
        i   )r6   declarer8   
AVRational	av_reducer:   numdenr   r   rf   r   r   )r   dars     r   display_aspect_ratioz VideoStream.display_aspect_ratio   s     nnS^^,NN377#NN377#KK 8 8 < <<KK!9!9!=!==	
 &fnnS&9::r   r0   )FF)r"   
__module____qualname__r   r$   r6   ccallr   r'   r   r.   cfuncr1   r3   rN   rW   propertyrZ   r]   ra   rf   rn    r   r   r   r      s    
	1 \\J-    \\1Vd] 1 1 \\) ) \\1 1(&( 	O 	O 
M 
M ; ; ; ; ; ;r   r   )r6   cython.cimportsr   r8   cython.cimports.av.packetr   cython.cimports.av.streamr   cython.cimports.av.utilsr   cython.cimports.av.video.framer   cython.cimports.libc.stdintr   cython.cimports.libc.stringr	   finalcclassr   rt   r   r   <module>r~      sK     ( , , ; 5 / . o;& o;  o;r   