o
    2j`                  	   @   s(  d dl mZmZ d dlZd dlmZ d dlmZm	Z	 d dl
mZ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 d dlmZ d dlmZ eee Zejdej ej! dej ej"ej#  de$de%fddZ&G dd deZ'G dd deZ(G dd deZ)ej*G dd dZ%dS )    )FlagIntEnumN)libav)
ByteSource
bytesource)Codec
wrap_codec)
Dictionary)	err_check)Packet)avrational_to_fractionto_avrational)EAGAIN)uint8_t)memcpyc_ctxc_codechwaccelreturnc                 C   s   | j tjkrddlm} |t}n&| j tjkr"ddlm} |t}n| j tj	kr3ddl
m} |t}ntt}|| || |S )z8Build an bv.CodecContext for an existing AVCodecContext.r   )VideoCodecContext)AudioCodecContext)SubtitleCodecContext)
codec_typelibAVMEDIA_TYPE_VIDEOav.video.codeccontextr   _cinit_sentinelAVMEDIA_TYPE_AUDIOav.audio.codeccontextr   AVMEDIA_TYPE_SUBTITLEav.subtitles.codeccontextr   CodecContext_init)r   r   r   r   py_ctxr   r    r$   K/var/www/html/whisper/venv/lib/python3.10/site-packages/av/codec/context.pywrap_codec_context   s   	


r&   c                   @   sB   e Zd ZU dZejZded< ejZ	ded< ejejB Z
ded< dS )	
ThreadTyper   z"Decode more than one frame at onceFRAMEz3Decode more than one part of a single frame at onceSLICEz*Decode using both FRAME and SLICE methods.AUTON)__name__
__module____qualname__NONEr   FF_THREAD_FRAMEr(   __annotations__FF_THREAD_SLICEr)   r*   r$   r$   r$   r%   r'   /   s   
 
r'   c                   @   s   e Zd ZejZejZejZ	ej
ZejZejZejZejZejZejZejZejZejZejZej Z!ej"Z#ej$Z%ej&Z'ej(Z)ej*Z+dS )FlagsN),r+   r,   r-   r   AV_CODEC_FLAG_UNALIGNED	unalignedAV_CODEC_FLAG_QSCALEqscaleAV_CODEC_FLAG_4MVfour_mvAV_CODEC_FLAG_OUTPUT_CORRUPToutput_corruptAV_CODEC_FLAG_QPELqpelAV_CODEC_FLAG_RECON_FRAMErecon_frameAV_CODEC_FLAG_COPY_OPAQUEcopy_opaqueAV_CODEC_FLAG_FRAME_DURATIONframe_durationAV_CODEC_FLAG_PASS1pass1AV_CODEC_FLAG_PASS2pass2AV_CODEC_FLAG_LOOP_FILTERloop_filterAV_CODEC_FLAG_GRAYgrayAV_CODEC_FLAG_PSNRpsnrAV_CODEC_FLAG_INTERLACED_DCTinterlaced_dctAV_CODEC_FLAG_LOW_DELAY	low_delayAV_CODEC_FLAG_GLOBAL_HEADERglobal_headerAV_CODEC_FLAG_BITEXACTbitexactAV_CODEC_FLAG_AC_PREDac_predAV_CODEC_FLAG_INTERLACED_MEinterlaced_meAV_CODEC_FLAG_CLOSED_GOP
closed_gopr$   r$   r$   r%   r2   8   s*    
r2   c                   @   sB   e Zd ZejZejZejZ	ej
ZejZejZejZejZejZdS )Flags2N)r+   r,   r-   r   AV_CODEC_FLAG2_FASTfastAV_CODEC_FLAG2_NO_OUTPUT	no_outputAV_CODEC_FLAG2_LOCAL_HEADERlocal_headerAV_CODEC_FLAG2_CHUNKSchunksAV_CODEC_FLAG2_IGNORE_CROPignore_cropAV_CODEC_FLAG2_SHOW_ALLshow_allAV_CODEC_FLAG2_EXPORT_MVS
export_mvsAV_CODEC_FLAG2_SKIP_MANUALskip_manualAV_CODEC_FLAG2_RO_FLUSH_NOOPro_flush_noopr$   r$   r$   r%   r[   O   s    
r[   c                   @   s  e Zd ZedoddZdpddZejdeje	j
 dejej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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dd Zedd Zedd  Zejdqd"ejfd#d$Zd%d& Zd'd( Zdpd)d*Zed+d, Z d-e!dB fd.d/Z"ejd-e!dB d0e#fd1d2Z$ejd0e!fd3d4Z%ejd5d6 Z&ejd-e!fd7d8Z'ejd9d: Z(ejd-e!fd;d<Z)ejdpd-e!dB fd=d>Z*dpd-e!dB fd?d@Z+ejdAe,fdBdCZ-ejdpdAe,dB fdDdEZ.ejdFdG Z/ejd-e!dAe,dB fdHdIZ0edJdK Z1edLdM Z2edNdO Z3edPdQ Z4e4jdRdQ Z4edSdT Z5e5jdUdT Z5edVdW Z6e6jdXdW Z6ee7d!dYdZ Z8e8jdejfd[dZZ8ed\d] Z9e9jdejfd^d]Z9ed_d` Z:edadb Z;e;jdejfdcdbZ;eddde Z<e<jdejfdfdeZ<edgdh Z=e=jdidh Z=edjdk Z>e>jdldk Z>edmdn Z?dS )rr!   Nc                 C   s2   t | tr| nt| |}t|j}t||j|S N)
isinstancer   r   avcodec_alloc_context3ptrr&   )codecmoder   cy_codecr   r$   r$   r%   create]   s
   zCodecContext.createc                 O   s&   |t urtdi | _d| _d| _d S )NzCannot instantiate CodecContextF)r   RuntimeErroroptionsstream_indexis_open)selfsentinelargskwargsr$   r$   r%   	__cinit__e   s
   
zCodecContext.__cinit__rq   rr   r   c                 C   s\   || _ | j jr|r| j j|krtdt|tjkr|n| j j| _|| _d| j _d| j _d S )Nz,Wrapping CodecContext with mismatched codec.r      )	rq   rr   rw   r   cythonNULLr   thread_countthread_type)r{   rq   rr   r   r$   r$   r%   r"   m   s   zCodecContext._initc                 C      | j jS )zU
        Get and set the flags bitmask of CodecContext.

        :rtype: int
        rq   flagsr{   r$   r$   r%   r   ~      zCodecContext.flagsvaluec                 C      || j _d S rn   r   r{   r   r$   r$   r%   r         c                 C      t | jjtj@ S )z9
        Use fixed qscale.

        :rtype: bool
        )boolrq   r   r   r5   r   r$   r$   r%   r6      s   zCodecContext.qscalec                 C   2   |r| j  jtjO  _d S | j  jtj M  _d S rn   )rq   r   r   r5   r   r$   r$   r%   r6         c                 C   r   rn   )r   rq   r   r   r?   r   r$   r$   r%   r@         zCodecContext.copy_opaquec                 C   r   rn   )rq   r   r   r?   r   r$   r$   r%   r@      r   c                 C   r   )zV
        Get and set the flags2 bitmask of CodecContext.

        :rtype: int
        rq   flags2r   r$   r$   r%   r      r   zCodecContext.flags2c                 C   r   rn   r   r   r$   r$   r%   r      r   c                 C   sJ   | j tju rd S | j jdkr#ttttjt | j jd | j j S d S Nr   )	rq   r   r   extradata_sizecastbytespointerr   	extradatar   r$   r$   r%   r      s   
zCodecContext.extradatac                 C   s   |d u rt t| jj d| j_d S t|}ttj	t
 t | jj|jt j | j_| jjs5tdt| jj|j|j |j| j_d S )Nr   zCannot allocate extradata)r   av_freepr   addressrq   r   r   r   r   r   r   
av_realloclengthAV_INPUT_BUFFER_PADDING_SIZEMemoryErrorr   )r{   datasourcer$   r$   r%   r      s   c                 C   r   rn   )rq   r   r   r$   r$   r%   r         zCodecContext.extradata_sizec                 C      | j tju rdS t| j jS NF)rq   r   r   r   av_codec_is_encoderrr   r   r$   r$   r%   
is_encoder      zCodecContext.is_encoderc                 C   r   r   )rq   r   r   r   av_codec_is_decoderrr   r   r$   r$   r%   
is_decoder   r   zCodecContext.is_decoderTstrictc                 C   s   | j r|r	tdd S t }|| jpi  | jjjsV| jrV| j	dkr9| jj
jp)d| jj_| jj
jp4tj| jj_n| j	dkrKd| jj_| jj| jj_nd| jj_tj| jj_tt| j| jjt|jd| jj d| j d d| _ t|| _d S )	NzCodecContext is already open.video   audiozavcodec_open2("z", )T)rz   
ValueErrorr	   updaterx   rq   	time_basenumr   type	frameratedenr   AV_TIME_BASEsample_rater
   avcodec_open2rr   r   r   namedict)r{   r   rx   r$   r$   r%   open   s*   



zCodecContext.openc                 C   sF   | j rtt| j j tt| j  | jr!t| j d S d S rn   )	rq   r   r   r   r   r   avcodec_free_contextparserav_parser_closer   r$   r$   r%   __dealloc__   s   zCodecContext.__dealloc__c              	   C   s<   | j pd}| jp	d}d| jj d| d| dt| dd	S )	Nz<notype>z	<nocodec>z<av. /z at 0xx>)r   r   	__class__r+   id)r{   _typer   r$   r$   r%   __repr__  s   

(zCodecContext.__repr__c           
      C   sj  | j st| jjj| _ | j std| jj t|dd}|dur%|jnt	j
}|dur/|jnd}d}g }	 t	j t| j | jt	|t	|||tjtjd	}	W d   n1 s[w   Y  t|	 |rt|}t|jj|| | j j|j_| j j|j_| j j|j_| j jr| j j|j_| j jdkr|j jtjO  _|| |s	 |S ||	7 }||	8 }|s	 |S q6)aN  Split up a byte stream into list of :class:`.Packet`.

        This is only effectively splitting up a byte stream, and does no
        actual interpretation of the data.

        It will return all packets that are fully contained within the given
        input, and will buffer partial packets until they are complete.

        Any timing information the parser is able to infer (``pts``, ``dts``,
        ``duration``, ``pos`` and the keyframe flag) is assigned onto the
        returned packets. Fields the parser cannot determine are left unset.

        :param ByteSource raw_input: A chunk of a byte-stream to process.
            Anything that can be turned into a :class:`.ByteSource` is fine.
            ``None`` or empty inputs will flush the parser's buffers.

        :return: ``list`` of :class:`.Packet` newly available.

        zNo parser for T)
allow_noneNr   r   )r   r   av_parser_initrr   rq   r   r   r   r   r   r   r   nogilav_parser_parse2r   AV_NOPTS_VALUEr
   r   r   r   ptsdtsposduration	key_framer   AV_PKT_FLAG_KEYappend)
r{   	raw_inputr   in_datain_sizepacketpacketsout_dataout_sizeconsumedr$   r$   r%   parse
  sX   
zCodecContext.parsec                 C   s
   | j duS )zf
        Returns ``True`` if this codec context is hardware accelerated, ``False`` otherwise.
        N)hwaccel_ctxr   r$   r$   r%   
is_hwaccelf  s   
zCodecContext.is_hwaccelframec                 c   sv    t j t| j|d ur|jnt j}W d    n1 sw   Y  t|d |  }|r9|V  |  }|s.d S d S )Nzavcodec_send_frame())r   r   r   avcodec_send_framerq   r   r
   _recv_packetr{   r   resr   r$   r$   r%   _send_frame_and_recvm  s   
z!CodecContext._send_frame_and_recvr   c                 C   s   |gS rn   r$   r{   r   r$   r$   r%   _prepare_frames_for_encode{  s   z'CodecContext._prepare_frames_for_encodec                 C   s   t d)Nz Base CodecContext cannot decode.)NotImplementedErrorr   r$   r$   r%   _alloc_next_frame  r   zCodecContext._alloc_next_framec                 C   s   | j s|  | _ | j }tj t| j|j}W d    n1 s!w   Y  |t ks0|tjkr2d S t	|d | 
|}|sCd | _ |S d S )Nzavcodec_receive_frame())_next_framer   r   r   r   avcodec_receive_framerq   r   AVERROR_EOFr
   _transfer_hwframe)r{   r   r   r$   r$   r%   _recv_frame  s   


zCodecContext._recv_framec                 C   s   |S rn   r$   r   r$   r$   r%   r     s   zCodecContext._transfer_hwframec                 C   sj   t  }tj t| j|j}W d    n1 sw   Y  |t ks(|tjkr*d S t|d |s3|S d S )Nzavcodec_receive_packet())	r   r   r   r   avcodec_receive_packetrq   r   r   r
   )r{   r   r   r$   r$   r%   r     s   
zCodecContext._recv_packetc                 C   sV   | j jtjtjfvrtd| jdd | |}|D ]}|d ur(|| j j	 q|S )Nz/Encoding is only supported for audio and video.Fr   )
rq   r   r   r   r   r   r   r   _rebase_timer   )r{   r   framesr$   r$   r%   *_prepare_and_time_rebase_frames_for_encode  s   
z7CodecContext._prepare_and_time_rebase_frames_for_encodec                 C   s<   g }|  |D ]}| |D ]}| | || qq|S )zAEncode a list of :class:`.Packet` from the given :class:`.Frame`.)r   r   _setup_encoded_packetr   r   r$   r$   r%   encode  s   
zCodecContext.encodec                 c   s6    |  |D ]}| |D ]
}| | |V  qqd S rn   )r   r   r   r{   r   r   r$   r$   r%   encode_lazy  s   
zCodecContext.encode_lazyr   c                 C   s   | j j|j _d S rn   )rq   r   )r{   r   r$   r$   r%   r     s   	z"CodecContext._setup_encoded_packetc                 C   s   | j jstd| jdd tj t| j|dur|jntj}W d   n1 s+w   Y  t	|d g }| 
 }|rN| || || | 
 }|s=|S )a  Decode a list of :class:`.Frame` from the given :class:`.Packet`.

        If the packet is None, the buffers will be flushed. This is useful if
        you do not want the library to automatically re-order frames for you
        (if they are encoded with a codec that has B-frames).

        .. warning::

            This method is **not thread-safe**. Calling :meth:`decode` concurrently
            from multiple threads on the same :class:`CodecContext` will corrupt
            internal FFmpeg state and likely cause a crash (segfault). FFmpeg 8.1
            enforces this more strictly than earlier releases. If you need to decode
            from multiple threads, give each thread its own :class:`CodecContext`.

        zcannot decode unknown codecFr   Nzavcodec_send_packet())rr   rq   r   r   r   r   r   avcodec_send_packetr   r
   r   _setup_decoded_framer   )r{   r   r   outr   r$   r$   r%   decode  s"   

zCodecContext.decodec                 C   sB   | j rtj t| j W d   dS 1 sw   Y  dS dS )zReset the internal codec state and discard all internal buffers.

        Should be called before you start decoding from a new position e.g.
        when seeking or when switching to a different stream.

        N)rz   r   r   r   avcodec_flush_buffersrq   r   r$   r$   r%   flush_buffers  s
   "zCodecContext.flush_buffersc                 C   s   |d ur|j j|_d S d S rn   )rq   r   
_time_baser   r$   r$   r%   r     s   z!CodecContext._setup_decoded_framec                 C   r   rn   )rr   r   r   r$   r$   r%   r     r   zCodecContext.namec                 C   r   rn   )rr   r   r   r$   r$   r%   r     r   zCodecContext.typec                 C   sp   g }| j jr| jjr| jjjs|S | jj}d}|j| jtjkr6||j| j |d7 }|j| jtjks |S )zX
        List the available profiles for this stream.

        :type: list[str]
        r   r   )	rq   rr   descprofilesprofiler   AV_PROFILE_UNKNOWNr   r   )r{   retr   ir$   r$   r%   r     s   zCodecContext.profilesc                 C   s~   | j jr| jjr| jjjsd S | jj}d}|j| jtjkr=|j| j| j jkr.|j| jS |d7 }|j| jtjksd S d S Nr   r   )rq   rr   r   r   r  r   r  r   )r{   r   r  r$   r$   r%   r  /  s   zCodecContext.profilec                 C   s   | j r| j jr| j jjsd S | j j}d}|j| jtjkr>|j| j|kr/|j| j| j_d S |d7 }|j| jtjksd S d S r  )rr   r   r   r  r   r  r   rq   )r{   r   r   r  r$   r$   r%   r  =  s   c                 C   s    | j rtdtt| jjS Nz&Cannot access 'time_base' as a decoder)r   rw   r   r   r   rq   r   r   r$   r$   r%   r   L  s   zCodecContext.time_basec                 C   s&   | j rtdt|t| jj d S r  )r   rw   r   r   r   rq   r   r   r$   r$   r%   r   R  s   c                 C   s   | j jjddddjddS )N   littleF	byteordersignedasciiencoding)rq   	codec_tagto_bytesr   r   r$   r$   r%   r  X  s   zCodecContext.codec_tagc                 C   s>   t |trt|dkrtj|jddddd| j_d S td)Nr  r  r  r  Fr	  z)Codec tag should be a 4 character string.)	ro   strlenint
from_bytesr   rq   r  r   r   r$   r$   r%   r  ^  s
   c                 C   s   | j jtj S )zGlobal quality for codecs which cannot change it per frame.

        Stored internally in lambda units; this property converts to/from
        QP units using ``FF_QP2LAMBDA``.

        Wraps :ffmpeg:`AVCodecContext.global_quality`.

        )rq   global_qualityr   FF_QP2LAMBDAr   r$   r$   r%   r  g  s   zCodecContext.global_qualityc                 C   s   |t j | j_d S rn   )r   r  rq   r  r   r$   r$   r%   r  t  r   c                 C      | j jdkr
| j jS d S r   rq   bit_rater   r$   r$   r%   r  x  s   zCodecContext.bit_ratec                 C   r   rn   r  r   r$   r$   r%   r  |  r   c                 C   r  r   )rq   rc_max_rater   r$   r$   r%   max_bit_rate  s   zCodecContext.max_bit_ratec                 C   s   | j j d S rn   rq   bit_rate_tolerancer   r$   r$   r%   r    r   zCodecContext.bit_rate_tolerancec                 C   r   rn   r  r   r$   r$   r%   r    r   c                 C   r   )zfHow many threads to use; 0 means auto.

        Wraps :ffmpeg:`AVCodecContext.thread_count`.

        )rq   r   r   r$   r$   r%   r     r   zCodecContext.thread_countc                 C   s   | j rtd|| j_d S )Nz/Cannot change thread_count after codec is open.)rz   rw   rq   r   r   r$   r$   r%   r     s   c                 C   s   t | jjS )z[One of :class:`.ThreadType`.

        Wraps :ffmpeg:`AVCodecContext.thread_type`.

        )r'   rq   r   r   r$   r$   r%   r     s   zCodecContext.thread_typec                 C   sR   | j rtdt|tu r|| j_d S t|tu r"t| j| j_d S |j| j_d S )Nz.Cannot change thread_type after codec is open.)	rz   rw   r   r  rq   r   r  r'   r   r   r$   r$   r%   r     s   c                 C   sp   | j j}|tjkrdS |tjkrdS |tjkrdS |tjkr dS |tjkr'dS |tjkr.dS |tj	kr5dS | S )a  Returns one of the following str literals:

        "NONE" Discard nothing
        "DEFAULT" Discard useless packets like 0 size packets in AVI
        "NONREF" Discard all non reference
        "BIDIR" Discard all bidirectional frames
        "NONINTRA" Discard all non intra frames
        "NONKEY Discard all frames except keyframes
        "ALL" Discard all

        Wraps :ffmpeg:`AVCodecContext.skip_frame`.
        r.   DEFAULTNONREFBIDIRNONINTRANONKEYALL)
rq   
skip_framer   AVDISCARD_NONEAVDISCARD_DEFAULTAVDISCARD_NONREFAVDISCARD_BIDIRAVDISCARD_NONINTRAAVDISCARD_NONKEYAVDISCARD_ALLr   r$   r$   r%   r$    s    






zCodecContext.skip_framec                 C   s   |dkrt j| j_d S |dkrt j| j_d S |dkr!t j| j_d S |dkr,t j| j_d S |dkr7t j| j_d S |dkrBt j| j_d S |dkrMt j	| j_d S t
d)	Nr.   r  r  r   r!  r"  r#  zInvalid skip_frame type)r   r%  rq   r$  r&  r'  r(  r)  r*  r+  r   r   r$   r$   r%   r$    s   c                 C   r   )zECodec delay.

        Wraps :ffmpeg:`AVCodecContext.delay`.

        )rq   delayr   r$   r$   r%   r,    r   zCodecContext.delay)NNrn   )T)@r+   r,   r-   staticmethodru   r   r   cfuncr   r   AVCodecContextconstAVCodecHWAccelr"   propertyr   setterr  r6   r@   r   r   r   r   r   ccallbintr   r   r   r   r   Framer   listr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r   r  	cdivisionr  r  r  r  r   r   r$  r,  r$   r$   r$   r%   r!   [   s    













\


	
$
	

















r!   )+enumr   r   r   cython.cimportsr   r   cython.cimports.av.bufferr   r   cython.cimports.av.codec.codecr   r   cython.cimports.av.dictionaryr	   cython.cimports.av.errorr
   cython.cimports.av.packetr   cython.cimports.av.utilsr   r   cython.cimports.libc.errnor   cython.cimports.libc.stdintr   cython.cimports.libc.stringr   declareobjectr   r.  r   r/  r0  r1  r2  r!   r&   r'   r2   r[   cclassr$   r$   r$   r%   <module>   s8    
	