o
    2jT.                     @   s  d dl Z d dlm  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mZ d dlmZmZmZ d d	lmZmZ d d
lmZ d dlmZ e je jddde jej  de je j!ej"  dej"fddZ#e j$e j%G dd deZ&dS )    N)CodecContext)HWAccel)	err_check)Frame)Packet)avrational_to_fractionto_avrational)VideoFormatget_pix_fmtget_video_format)
VideoFramealloc_video_frame)VideoReformatter)int64_tF)checkctxpix_fmtsreturnc                 C   s`   t t jt | j}d}|| dkr'|| |jkr|| S |d7 }|| dks|jr-| jS tj	S )Nr      )
cythoncastpointerAVCodecPrivateDataopaquehardware_pix_fmtallow_software_fallback
sw_pix_fmtlibAV_PIX_FMT_NONE)r   r   private_datai r"   P/var/www/html/whisper/venv/lib/python3.10/site-packages/av/video/codeccontext.py_get_hw_format   s   
r$   c                   @   s  e Zd Zejdejej dejejej	  de
dB fddZejdedB defd	d
ZejdefddZejdedefddZejdefddZedd ZejdefddZedd ZejdejfddZedd ZejdejfddZedd Zejdejfd dZed!d" Zejd#d" Zed$d% Zejd&d% Zed'd( Zejd)d( Zed*d+ Zejdejfd,d+Zed-d. Z e jd/d. Z ed0d1 Z!ed2d3 Z"ed4d5 Z#e#jdejfd6d5Z#ed7d8 Z$ed9d: Z%ed;d< Z&e&jd=d< Z&ed>d? Z'e'jd@d? Z'edAdB Z(e(jdCdB Z(edDdE Z)e)jdFdE Z)edGdH Z*e*jdIdH Z*edJdK Z+e+jdLdK Z+edMdN Z,e,jdOdN Z,dS )PVideoCodecContextptrcodechwaccelNc                 C   s   t | ||| |d urOz5|| j| _t| jj| j_| jj	jj
| j_
t| j_| jj	jj
| j_| jj| j_t| j| j_W d S  tyN   d | _Y d S w d S N)r   _initcreater'   hwaccel_ctxr   av_buffer_refr&   hw_device_ctxconfigpix_fmtr$   
get_format_private_datar   r(   r   r   addressr   NotImplementedError)selfr&   r'   r(   r"   r"   r#   r*   +   s    
	zVideoCodecContext._initinputr   c                 C   s   |d u s|s	d gS |}|j j| jks |j| jjks |j| jjkr9| js't | _| jj|| jj| jj| j | jjd}|jj	t
jkrF| jj|j_	|gS )N)threads)formatr0   widthr&   heightreformatterr   reformatthread_countptsr   AV_NOPTS_VALUE	frame_num)r5   r6   vframer"   r"   r#   _prepare_frames_for_encodeL   s$   z,VideoCodecContext._prepare_frames_for_encodec                 C   s   t  S r)   )r   r5   r"   r"   r#   _alloc_next_frameg   s   z#VideoCodecContext._alloc_next_frameframepacketc                 C   s   t | || |}|  d S r)   )r   _setup_decoded_frame_init_user_attributes)r5   rE   rF   rA   r"   r"   r#   rG   k   s   z&VideoCodecContext._setup_decoded_framec                 C   sn   | j d u r|S |jj| j jjjkr|S | j jr"| j jtt	|_
|S |  }tt|j|jd |j|_|S Nr   )r,   r&   r8   r/   r0   is_hw_owned	device_idr   r   r   
_device_idrD   r   r   av_hwframe_transfer_datar>   )r5   rE   frame_swr"   r"   r#   _transfer_hwframeq   s   
z#VideoCodecContext._transfer_hwframec                 C   s"   t ttj| jj| jj| jjS r)   )	r   r   r   r   AVPixelFormatr&   r0   r9   r:   rC   r"   r"   r#   r8      s
   zVideoCodecContext.formatr8   c                 C   s"   |j | j_ |j| j_|j| j_d S r)   )r0   r&   r9   r:   )r5   r8   r"   r"   r#   r8      s   

c                 C      | j tju rdS | j jS rI   )r&   r   NULLr9   rC   r"   r"   r#   r9         zVideoCodecContext.widthvaluec                 C      || j _d S r)   )r&   r9   r5   rT   r"   r"   r#   r9         c                 C   rQ   rI   )r&   r   rR   r:   rC   r"   r"   r#   r:      rS   zVideoCodecContext.heightc                 C   rU   r)   )r&   r:   rV   r"   r"   r#   r:      rW   c                 C      | j jS )z
        The number of bits per sample in the codedwords. It's mandatory for this to be set for some formats to decode properly.

        Wraps :ffmpeg:`AVCodecContext.bits_per_coded_sample`.

        :type: int
        )r&   bits_per_coded_samplerC   r"   r"   r#   rY         	z'VideoCodecContext.bits_per_coded_samplec                 C      | j rtd|| j_d S )NzNot supported for encoders)
is_encoder
ValueErrorr&   rY   rV   r"   r"   r#   rY      s   c                 C   s&   t tt j| jj}tt|jS )zE
        The pixel format's name.

        :type: str | None
        )	r   av_pix_fmt_desc_getr   r   rP   r&   r0   strname)r5   descr"   r"   r#   r0      s   zVideoCodecContext.pix_fmtc                 C   s   t || j_d S r)   )r
   r&   r0   rV   r"   r"   r#   r0      s   c                 C      t t| jjS )zZ
        The frame rate, in frames per second.

        :type: fractions.Fraction
        )r   r   r3   r&   	frameraterC   r"   r"   r#   rc      s   zVideoCodecContext.frameratec                 C      t |t| jj d S r)   )r   r   r3   r&   rc   rV   r"   r"   r#   rc         c                 C   s   | j S )z#Another name for :attr:`framerate`.rc   rC   r"   r"   r#   rate   s   zVideoCodecContext.ratec                 C   s
   || _ d S r)   rf   rV   r"   r"   r#   rg      s   
c                 C   s   | j rtd| jjS )zj
        Sets the number of frames between keyframes. Used only for encoding.

        :type: int
        %Cannot access 'gop_size' as a decoder
is_decoderRuntimeErrorr&   gop_sizerC   r"   r"   r#   rl      s   zVideoCodecContext.gop_sizec                 C   r[   )Nrh   ri   rV   r"   r"   r#   rl      s   c                 C   rb   r)   )r   r   r3   r&   sample_aspect_ratiorC   r"   r"   r#   rm      s   z%VideoCodecContext.sample_aspect_ratioc                 C   rd   r)   )r   r   r3   r&   rm   rV   r"   r"   r#   rm      re   c                 C   sL   t t|jt|j| jj| jjj | jj	| jjj d t
t|S )Ni   )r   	av_reducer   r3   numdenr&   r9   rm   r:   r   )r5   darr"   r"   r#   display_aspect_ratio   s   

z&VideoCodecContext.display_aspect_ratioc                 C   s   t | jjS )z
        :type: bool
        )boolr&   has_b_framesrC   r"   r"   r#   rt     s   zVideoCodecContext.has_b_framesc                 C   rX   )a  Raw ``has_b_frames`` value from FFmpeg (int, not bool).

        After :meth:`flush_buffers`, FFmpeg may reset the internal reorder
        heuristic. Set this to the known reorder depth *after* seeking to
        avoid dropped hierarchical B-frames.
        r&   rt   rC   r"   r"   r#   reorder_depth  s   zVideoCodecContext.reorder_depthc                 C   rU   r)   ru   rV   r"   r"   r#   rv     rW   c                 C   rX   z
        :type: int
        )r&   coded_widthrC   r"   r"   r#   rx        zVideoCodecContext.coded_widthc                 C   rX   rw   )r&   coded_heightrC   r"   r"   r#   rz      ry   zVideoCodecContext.coded_heightc                 C   rX   )z
        Describes the signal range of the colorspace.

        Wraps :ffmpeg:`AVFrame.color_range`.

        :type: int
        r&   color_rangerC   r"   r"   r#   r|   '  rZ   zVideoCodecContext.color_rangec                 C   rU   r)   r{   rV   r"   r"   r#   r|   2  rW   c                 C   rX   )z
        Describes the RGB/XYZ matrix of the colorspace.

        Wraps :ffmpeg:`AVFrame.color_primaries`.

        :type: int
        r&   color_primariesrC   r"   r"   r#   r~   6  rZ   z!VideoCodecContext.color_primariesc                 C   rU   r)   r}   rV   r"   r"   r#   r~   A  rW   c                 C   rX   )z
        Describes the linearization function (a.k.a. transformation characteristics) of the colorspace.

        Wraps :ffmpeg:`AVFrame.color_trc`.

        :type: int
        r&   	color_trcrC   r"   r"   r#   r   E  rZ   zVideoCodecContext.color_trcc                 C   rU   r)   r   rV   r"   r"   r#   r   P  rW   c                 C   rX   )z
        Describes the YUV/RGB transformation matrix of the colorspace.

        Wraps :ffmpeg:`AVFrame.colorspace`.

        :type: int
        r&   
colorspacerC   r"   r"   r#   r   T  rZ   zVideoCodecContext.colorspacec                 C   rU   r)   r   rV   r"   r"   r#   r   _  rW   c                 C   rX   )zd
        The maximum run of consecutive B frames when encoding a video.

        :type: int
        r&   max_b_framesrC   r"   r"   r#   r   c  s   zVideoCodecContext.max_b_framesc                 C   rU   r)   r   rV   r"   r"   r#   r   l  rW   c                 C   rX   )z
        The minimum quantiser value of an encoded stream.

        Wraps :ffmpeg:`AVCodecContext.qmin`.

        :type: int
        r&   qminrC   r"   r"   r#   r   p  rZ   zVideoCodecContext.qminc                 C   rU   r)   r   rV   r"   r"   r#   r   {  rW   c                 C   rX   )z
        The maximum quantiser value of an encoded stream.

        Wraps :ffmpeg:`AVCodecContext.qmax`.

        :type: int
        r&   qmaxrC   r"   r"   r#   r     rZ   zVideoCodecContext.qmaxc                 C   rU   r)   r   rV   r"   r"   r#   r     rW   )-__name__
__module____qualname__r   cfuncr   r   AVCodecContextconstAVCodecr   r*   r   listrB   rD   r   rG   rO   propertyr8   setterr	   r9   uintr:   rY   intr0   rc   rg   rl   rm   rr   rt   rv   rx   rz   r|   r~   r   r   r   r   r   r"   r"   r"   r#   r%   (   s    
 

















	




















r%   )'r   cython.cimports.libavcimportslibavr    cython.cimports.av.codec.contextr    cython.cimports.av.codec.hwaccelr   cython.cimports.av.errorr   cython.cimports.av.framer   cython.cimports.av.packetr   cython.cimports.av.utilsr   r   cython.cimports.av.video.formatr	   r
   r   cython.cimports.av.video.framer   r   $cython.cimports.av.video.reformatterr   cython.cimports.libc.stdintr   r   	exceptvalr   r   r   rP   r$   finalcclassr%   r"   r"   r"   r#   <module>   s0    

