o
    2j                     @   s|   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
 d
eZdS )    N)libav)Packet)Stream)avrational_to_fraction)
VideoFrame)int32_t)memcpyc                   @   s   e Zd Zdd Zdd ZejddedB fddZejdd	e	dB fd
dZ
ejdd Zejdd Zdd Zd ddZedd Zedd Zedd Zedd Zedd ZdS )!VideoStreamc                 C   sn   | j d u r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self r   J/var/www/html/whisper/venv/lib/python3.10/site-packages/av/video/stream.py__repr__   s   
zVideoStream.__repr__c                 C   sV   |dv rt dt| j d| d| jd u r%t dt| j d| dt| j|S )N)	frameraterate'z' object has no attribute ')AttributeErrortype__name__r   getattr)r   r   r   r   r   __getattr__   s   
zVideoStream.__getattr__Nframec                 C   s,   | j |}|D ]}| |_| jj|j_q|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packetr   r   r   r"   "   s
   
zVideoStream.encoder'   c                 C   s   | 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'   r   r   r   r(   3   s   	zVideoStream.decodec                 C   s.   t |  | jd ur| jr|   d S d S d S N)r   _finalize_for_outputr   _has_display_matrix_apply_display_matrixr   r   r   r   r*   >   s   
z VideoStream._finalize_for_outputc                 C   s`   dt t }tt | jjjt | jjj	tj
|d}|t jkr&tdt|j| j| d S )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sdr   r   r   r,   F   s   
z!VideoStream._apply_display_matrixc                 C   sZ   |du r	d| _ dS dd |D }t|dkrtdtdD ]	}|| | j|< qd| _ dS )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.
        NFc                 S   s   g | ]}t |qS r   )int).0vr   r   r   
<listcomp>`   s    z2VideoStream.set_display_matrix.<locals>.<listcomp>r-   z+display matrix must have exactly 9 elementsT)r+   len
ValueErrorranger:   )r   matrixvalsir   r   r   set_display_matrixU   s   
zVideoStream.set_display_matrixFc                 C   s6   t | jt|  t | jt|t| d| _dS )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)r0   av_display_rotation_setr:   floatav_display_matrix_flipboolr+   )r   degreeshflipvflipr   r   r   set_display_rotationh   s   

z VideoStream.set_display_rotationc                 C      t t| 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   r.   r2   r$   avg_frame_rater   r   r   r   average_ratev   s   
zVideoStream.average_ratec                 C   rP   )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   r.   r2   r$   r_frame_rater   r   r   r   	base_rate   s   zVideoStream.base_ratec                 C   s"   t tj| jtj}tt|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
        )r0   av_guess_frame_rater.   r7   r$   r   r2   )r   valr   r   r   guessed_rate   s   	zVideoStream.guessed_ratec                 C   s$   t | jj| jtj}tt|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
        )r0   av_guess_sample_aspect_ratio	containerr$   r.   r7   r   r2   )r   sarr   r   r   sample_aspect_ratio   s   	zVideoStream.sample_aspect_ratioc                 C   sT   t tj}tt |jt |j| jj	| j
j | jj| j
j d tt |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   )r.   declarer0   
AVRational	av_reducer2   numdenr   r   r[   r   r   )r   darr   r   r   display_aspect_ratio   s   

z VideoStream.display_aspect_ratior)   )FF)r   
__module____qualname__r   r    r.   ccallr   r"   r   r(   cfuncr*   r,   rG   rO   propertyrR   rT   rW   r[   rb   r   r   r   r   r	      s.    	







r	   )r.   cython.cimportsr   r0   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	   r   r   r   r   <module>   s    