
    dj87                        d dl mZmZ 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mZmZ d d	lmZmZ  ej        e e                      Zej        d
ej        ej        ej                          defd            Z G d de          Z G d de          Z  G d de!          Z"ej#         G d d                      Zej        d             Z$ e$            Z% e ej&                              Z'd Z(d Z)ddZ*dS )    )FlagIntEnumN)libavget_audio_format)wrap_hwconfig)wrap_avclass)avrational_to_fraction)VideoFormatget_pix_fmtget_video_format)freemallocptrreturnc                     t          t                    }| |_        t          j        |           |_        |                                 |S N)Codec_cinit_sentinelr   libav_codec_is_encoder
is_encoder_init)r   codecs     I/home/agentuser/manim-venv/lib/python3.11/site-packages/av/codec/codec.py
wrap_codecr      s;    ))EEI.s33E	KKMMML    c                   f    e Zd ZdZej        Zej        Zej	        Z
ej        Zej        Zej        ZdS )
Propertiesr   N)__name__
__module____qualname__NONEr   AV_CODEC_PROP_INTRA_ONLY
INTRA_ONLYAV_CODEC_PROP_LOSSYLOSSYAV_CODEC_PROP_LOSSLESSLOSSLESSAV_CODEC_PROP_REORDERREORDERAV_CODEC_PROP_BITMAP_SUB
BITMAP_SUBAV_CODEC_PROP_TEXT_SUBTEXT_SUB r   r   r   r      sC        D-J#E)H'G-J)HHHr   r   c                       e Zd ZdZej        Zej        ZdZ	ej
        Zej        ZdZej        Zej        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#dZ$dZ%dZ&dS )	Capabilitiesr         i   i   i    i  @ N)'r    r!   r"   noner   AV_CODEC_CAP_DRAW_HORIZ_BANDdraw_horiz_bandAV_CODEC_CAP_DR1dr1hwaccelAV_CODEC_CAP_DELAYdelayAV_CODEC_CAP_SMALL_LAST_FRAMEsmall_last_framehwaccel_vdpauAV_CODEC_CAP_EXPERIMENTALexperimentalAV_CODEC_CAP_CHANNEL_CONFchannel_confneg_linesizesAV_CODEC_CAP_FRAME_THREADSframe_threadsAV_CODEC_CAP_SLICE_THREADSslice_threadsAV_CODEC_CAP_PARAM_CHANGEparam_changeAV_CODEC_CAP_OTHER_THREADSauto_threads AV_CODEC_CAP_VARIABLE_FRAME_SIZEvariable_frame_sizeAV_CODEC_CAP_AVOID_PROBINGavoid_probingAV_CODEC_CAP_HARDWAREhardwareAV_CODEC_CAP_HYBRIDhybridencoder_reordered_opaqueencoder_flushencoder_recon_framer0   r   r   r2   r2   "   s        D6O

CG"E8M0L0LM2M2M0L1L>2M(H$F&M!r   r2   c                       e Zd ZdS )UnknownCodecErrorN)r    r!   r"   r0   r   r   rY   rY   :   s        Dr   rY   c                   D   e Zd ZdZddZej        d d            Zd Zd dZ	e
d             Ze
d	             Ze
d
             Ze
d             Ze
d             Ze
d             Ze
d             Ze
d             Ze
d             Ze
d             Ze
d             Ze
d             Ze
d             Ze
d             Ze
d             Ze
d             Ze
d             Ze
d             Ze
d             Ze
d             Ze
d             Ze
d             Z e
d             Z!dS )!r   a  Codec(name, mode='r')

    :param str name: The codec name.
    :param str mode: ``'r'`` for decoding or ``'w'`` for encoding.

    This object exposes information about an available codec, and an avenue to
    create a :class:`.CodecContext` to encode/decode directly.

    ::

        >>> codec = Codec('mpeg4', 'r')
        >>> codec.name
        'mpeg4'
        >>> codec.type
        'video'
        >>> codec.is_encoder
        False

    rc                 N   |t           u rd S |dk    rdt          j        |          | _        | j        sCt          j        |          | _        | j        r#t          j        | j        j                  | _        nz|dk    rdt          j        |          | _        | j        sCt          j        |          | _        | j        r#t          j	        | j        j                  | _        nt          d|          |                     |           |dk    | j        k    rt          d||          d S )Nwr[   z!Invalid mode; must be "r" or "w".z Found codec does not match mode.)r   r   avcodec_find_encoder_by_namer   avcodec_descriptor_get_by_namedescavcodec_find_encoderidavcodec_find_decoder_by_nameavcodec_find_decoder
ValueErrorr   r   RuntimeError)selfnamemodes      r   	__cinit__zCodec.__cinit__T   s   ?""F3;;7==DH8 F>tDD	9 F"7	EEDHS[[7==DH8 F>tDD	9 F"7	EEDH @$GGG

4 CKDO++A4NNN ,+r   Nc                 V   | j         st          |          | j        s<t          j        | j         j                  | _        | j        st          d|z            t          j        | j                   | _        | j        r(t          j	        | j                   rt          d          d S d S )NzNo codec descriptor for %r.z%s is both encoder and decoder.)
r   rY   r`   r   avcodec_descriptor_getrb   rf   r   r   av_codec_is_decoder)rg   rh   s     r   r   zCodec._inito   s    x 	*#D)))y 	I248;??DI9 I"#@4#GHHH1$(;; ? 	Bs6tx@@ 	B@AAA	B 	B 	B 	Br   c                 F    | j         }d| j        j         d| j         d|dS )Nz<av. z mode=>)ri   	__class__r    rh   )rg   ri   s     r   __repr__zCodec.__repr__   s1    yDdn-DD	DDDDDDDr   c                 8    ddl m} |                    |           S )zCreate a :class:`.CodecContext` for this codec.

        :param str kind: Gives a hint to static type checkers for what exact CodecContext is used.
           )CodecContext)contextru   create)rg   kindru   s      r   rw   zCodec.create   s*    
 	*)))))""4(((r   c                     | j         rdndS )Nr]   r[   r   rg   s    r   ri   z
Codec.mode   s    o.ss3.r   c                     | j          S r   rz   r{   s    r   
is_decoderzCodec.is_decoder   s    ?""r   c                 4    t          | j        j                  S r   )r	   r   
priv_classr{   s    r   
descriptorzCodec.descriptor   s    DH/000r   c                     | j         j        pdS N )r   rh   r{   s    r   rh   z
Codec.name   s    x}""r   c                 >    t          j        | j        j                  S )zH
        Returns the name of the codec, not a specific encoder.
        )r   avcodec_get_namer   rb   r{   s    r   canonical_namezCodec.canonical_name   s    
 #DHK000r   c                     | j         j        pdS r   )r   	long_namer{   s    r   r   zCodec.long_name   s    x!'R'r   c                 f    t          j        | j        j                  }|t          j        k    rdn|S )zh
        The media type of this codec.

        E.g: ``'audio'``, ``'video'``, ``'subtitle'``.

        unknown)r   av_get_media_type_stringr   typecythonNULL)rg   
media_types     r   r   z
Codec.type   s.     1$(-@@
&&+55yy:Er   c                     | j         j        S r   )r   rb   r{   s    r   rb   zCodec.id   s    x{r   c           
      h   t           j        }d}t          j        t           j        | j        t          j        dt          j        |          t          j        |                     |sdS t          j        t           j        t          j	                 |          fdt          |          D             S )zKA list of supported frame rates (:class:`fractions.Fraction`), or ``None``.r   Nc                 ^    g | ])}t          t          j        |                             *S r0   )r
   r   address.0iratess     r   
<listcomp>z%Codec.frame_rates.<locals>.<listcomp>   s0    UUUQ&v~eAh'?'?@@UUUr   )r   r   r   avcodec_get_supported_configr   AV_CODEC_CONFIG_FRAME_RATEr   castpointer
AVRationalrangerg   outnumr   s      @r   frame_rateszCodec.frame_rates   s     :@(KH*N3N3	
 	
 	
  	FFN3>:C@@UUUU%PS**UUUUr   c           
      h   t           j        }d}t          j        t           j        | j        t          j        dt          j        |          t          j        |                     |sdS t          j        t           j        t           j	                 |          fdt          |          D             S )z>A list of supported audio sample rates (``int``), or ``None``.r   Nc                      g | ]
}|         S r0   r0   r   s     r   r   z%Codec.audio_rates.<locals>.<listcomp>   s    ---Qa---r   )r   r   r   r   r   AV_CODEC_CONFIG_SAMPLE_RATEr   r   r   intr   r   s      @r   audio_rateszCodec.audio_rates   s     :@(KH+N3N3	
 	
 	
  	FFN6:6<<----%**----r   c           
      h   t           j        }d}t          j        t           j        | j        t          j        dt          j        |          t          j        |                     |sdS t          j        t           j        t          j	                 |          fdt          |          D             S )z7A list of supported :class:`.VideoFormat`, or ``None``.r   Nc                 >    g | ]}t          |         d d           S )r   )r   r   r   fmtss     r   r   z'Codec.video_formats.<locals>.<listcomp>   s*    DDDA a!Q//DDDr   )r   r   r   r   r   AV_CODEC_CONFIG_PIX_FORMATr   r   r   AVPixelFormatr   rg   r   r   r   s      @r   video_formatszCodec.video_formats   s     :@(KH*N3N3	
 	
 	
  	F{6>#*;<cBBDDDDsDDDDr   c           
      h   t           j        }d}t          j        t           j        | j        t          j        dt          j        |          t          j        |                     |sdS t          j        t           j        t          j	                 |          fdt          |          D             S )z7A list of supported :class:`.AudioFormat`, or ``None``.r   Nc                 :    g | ]}t          |                   S r0   r   r   s     r   r   z'Codec.audio_formats.<locals>.<listcomp>   s&    >>>a a))>>>r   )r   r   r   r   r   AV_CODEC_CONFIG_SAMPLE_FORMATr   r   r   AVSampleFormatr   r   s      @r   audio_formatszCodec.audio_formats   s     :@(KH-N3N3	
 	
 	
  	F{6>#*<=sCC>>>>5::>>>>r   c                     | j         r| j         S g }d}	 t          j        | j        |          }|sn(|                    t          |                     |dz  }Et          |          | _         | j         S )Nr   Trt   )_hardware_configsr   avcodec_get_hw_configr   appendr   tuple)rg   retr   r   s       r   hardware_configszCodec.hardware_configs   s    ! 	*))	+DHa88C JJ}S))***FA	 "'s%%r   c                     | j         j        S r   )r`   propsr{   s    r   
propertieszCodec.properties  s    yr   c                 N    t          | j        j        t          j        z            S r   )boolr`   r   r   r$   r{   s    r   
intra_onlyzCodec.intra_only      DIOc&BBCCCr   c                 N    t          | j        j        t          j        z            S r   )r   r`   r   r   r&   r{   s    r   lossyzCodec.lossy  s    DIOc&==>>>r   c                 N    t          | j        j        t          j        z            S r   )r   r`   r   r   r(   r{   s    r   losslesszCodec.lossless      DIOc&@@AAAr   c                 N    t          | j        j        t          j        z            S r   )r   r`   r   r   r*   r{   s    r   reorderzCodec.reorder  s    DIOc&??@@@r   c                 N    t          | j        j        t          j        z            S r   )r   r`   r   r   r,   r{   s    r   
bitmap_subzCodec.bitmap_sub"  r   r   c                 N    t          | j        j        t          j        z            S r   )r   r`   r   r   r.   r{   s    r   text_subzCodec.text_sub&  r   r   c                     | j         j        S )a  
        Get the capabilities bitmask of the codec.

        This method returns an integer representing the codec capabilities bitmask,
        which can be used to check specific codec features by performing bitwise
        operations with the Capabilities enum values.

        :example:

        .. code-block:: python

            from av.codec import Codec, Capabilities

            codec = Codec("h264", "w")

            # Check if the codec can be fed a final frame with a smaller size.
            # This can be used to prevent truncation of the last audio samples.
            small_last_frame = bool(codec.capabilities & Capabilities.small_last_frame)

        :rtype: int
        )r   capabilitiesr{   s    r   r   zCodec.capabilities*  s    . x$$r   c                 N    t          | j        j        t          j        z            S )z
        Check if codec is experimental and is thus avoided in favor of non experimental encoders.

        :rtype: bool
        )r   r   r   r   r@   r{   s    r   rA   zCodec.experimentalC  s     DH)C,IIJJJr   c                 N    t          | j        j        t          j        z            S )z
        If true, encoder or decoder requires flushing with `None` at the end in order to give the complete and correct output.

        :rtype: bool
        )r   r   r   r   r;   r{   s    r   r<   zCodec.delayL  s     DH)C,BBCCCr   )r[   r   )"r    r!   r"   __doc__rj   r   cfuncr   rr   rw   propertyri   r}   r   rh   r   r   r   rb   r   r   r   r   r   r   r   r   r   r   r   r   r   rA   r<   r0   r   r   r   r   >   s        (O O O O6 \B B B \BE E E) ) ) ) / / X/ # # X# 1 1 X1 # # X# 1 1 X1 ( ( X( F F XF   X V V XV" . . X." E E XE" ? ? X?" & & X&   X D D XD ? ? X? B B XB A A XA D D XD B B XB % % X%0 K K XK D D XD D Dr   r   c                  B   t                      } t          j        t          j        t          j        t
          j                                    }t          j        }	 t          j        t          j	        |                    }|r| 
                    |j                   nnE| S r   )setr   declarer   constr   AVCodecr   av_codec_iterater   addrh   )namesr   opaques      r   get_codec_namesr   V  s~    E
.S[(AB
C
CC"KF"6>&#9#9:: 	IIch Lr   c                  v   t          d           t          t                    D ]} 	 t          | d          }n# t          $ r d}Y nw xY w	 t          | d          }n# t          $ r d}Y nw xY w|p|}	 t          ddt          |                   dt          |                   |j        d                                         d	|j                 d
|j	                 d|j
                 d|j        dd|j                   # t          $ r&}t          d|j        dd|            Y d}~d}~ww xY wdS )z)Print information about available codecs.zCodecs:
 D..... = Decoding supported
 .E.... = Encoding supported
 ..V... = Video codec
 ..A... = Audio codec
 ..S... = Subtitle codec
 ...I.. = Intra frame-only codec
 ....L. = Lossy compression
 .....S = Lossless compression
 ------r]   Nr[   ro   z.Dz.Er   z.Iz.Lz.S18z...... z<18z ERROR: )printsortedcodecs_availabler   re   r   r   upperr   r   r   rh   r   	Exception)rh   e_codecd_codecr   es        r   dump_codecsr   h  s    
		   '(( 9 9	D#&&GG 	 	 	GGG		D#&&GG 	 	 	GGG	 "7	9E g''g''JqM''))))**%%(((JJJJOO
     	9 	9 	97EJ777A7788888888	999 9s:   9AAAA,+A,4BD
D6D11D6c                      t          d           t          t                    D ]X} 	 t          | d          }n# t          $ r Y  w xY w|j        }|s.t          d|j                   |D ]}t          d|           Yd S )NzHardware configs:r[   z   z       )r   r   r   r   re   r   rh   )rh   r   configsconfigs       r   dump_hwconfigsr     s    	
'(( % %	$$$EE 	 	 	H	 ( 	eUZ    	% 	%F)V$$$$	%% %s   8
AAFc           	      V   t           j        }| t          d          t          |           } | sdS t	          |t
                    r t          j        t
          |          j        }n't          t          j        t          |                    }t          |           }t          j        t           j        t          j                 t          |dz   t          j        t          j                  z                      }|t           j        k    rt!                      	 t#          |          D ]l}| |         }t	          |t
                    r#t          j        t
          |          j        ||<   Bt          t          j        t          |                    ||<   mt          j        ||<   d}t          j        |||rdndt          j        |                    }	t+          |	dd          |f|t           j        k    rt-          |           S S # |t           j        k    rt-          |           w w xY w)a  
    Find the best pixel format to convert to given a source format.

    Wraps :ffmpeg:`avcodec_find_best_pix_fmt_of_list`.

    :param pix_fmts: Iterable of pixel formats to choose from (str or VideoFormat).
    :param src_pix_fmt: Source pixel format (str or VideoFormat).
    :param bool has_alpha: Whether the source alpha channel is used.
    :return: (best_format, loss)
    :rtype: (VideoFormat | None, int)
    Nzpix_fmts must not be None)Nr   rt   r   )r   r   	TypeErrorr   
isinstancer   r   pix_fmtr   strlenr   r   r   r   sizeofMemoryErrorr   AV_PIX_FMT_NONE!avcodec_find_best_pix_fmt_of_listr   r   r   )
pix_fmtssrc_pix_fmt	has_alphac_listsrcnr   itemc_lossbests
             r   find_best_pix_fmt_of_listr    s    17F 3444XH w+{++ 9k+{33;&+c;7788HA[s()As'8999:: F mmq 	@ 	@AA;D$,, @"KT::Bq		'C(>(>??q		'q	4Ci.Qv0F0F
 
  a++V3V[  LLLL !6V[  LLLL !s   CH "H()F)+enumr   r   r   cython.cimportsr   r   cython.cimports.av.audio.formatr    cython.cimports.av.codec.hwaccelr   cython.cimports.av.descriptorr	   cython.cimports.av.utilsr
   cython.cimports.av.video.formatr   r   r   cython.cimports.libc.stdlibr   r   r   objectr   r   r   r   r   r   r   r   r2   re   rY   cclassr   r   avcodec_get_classcodec_descriptorr   r   r  r0   r   r   <module>r     sZ            ( ( ( ( ( ( < < < < < < : : : : : : 6 6 6 6 6 6 ; ; ; ; ; ; V V V V V V V V V V 4 4 4 4 4 4 4 4 &.22 FN6<#<= %    * * * * * * * *" " " " "7 " " "0	 	 	 	 	
 	 	 	 TD TD TD TD TD TD TD TDn 
 
 
 #?$$ < 5 5 7 788 -9 -9 -9`% % %"8 8 8 8 8 8r   