
    djF.                        d dl Z d dlmc 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e j        ej                  de j        e j!        ej"                          dej"        fd                        Z#e j$         G 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                    t          j        t           j        t                   | j                  }d}||         dk    r*||         |j        k    r||         S |dz  }||         dk    *|j        r| j        n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is       P/home/agentuser/manim-venv/lib/python3.11/site-packages/av/video/codeccontext.py_get_hw_formatr%      s     8>{)*CJ8 8L A
1+

A;,777A;	Q 1+


 '>WCDW    c                      e Zd Zej        dej        ej                 dej        ej        ej	                          de
dz  fd            Zej        dedz  defd            Zej        defd	            Zej        d
edefd            Zej        d
efd            Zed             Zej        defd            Zed             Zej        dej        fd            Zed             Zej        dej        fd            Zed             Zej        dej        fd            Zed             Zej        d             Zed             Zej        d             Zed             Zej        d             Zed             Zej        dej        fd            Zed              Z e j        d!             Z ed"             Z!ed#             Z"ed$             Z#e#j        dej        fd%            Z#ed&             Z$ed'             Z%ed(             Z&e&j        d)             Z&ed*             Z'e'j        d+             Z'ed,             Z(e(j        d-             Z(ed.             Z)e)j        d/             Z)ed0             Z*e*j        d1             Z*ed2             Z+e+j        d3             Z+ed4             Z,e,j        d5             Z,dS )6VideoCodecContextptrcodechwaccelNc                    t          j        | |||           |	 |                    | j                  | _        t          j        | j        j                  | j        _        | j        j	        j        j
        | j        _
        t          | j        _        | j        j	        j        j
        | j        _        | j        j        | j        _        t#          j        | j                  | j        _        d S # t(          $ r d | _        Y d S w xY 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+   s       r$   r.   zVideoCodecContext._init*   s     	4eW555(#*>>$*#=#= ),):4;K;O)P)P&#'#3#:#>#F &4#$+/7 "3 L8 ": #).1C"D"D& 	( 	( 	( $(    	( s   CC. .DDinputr   c                    ||sd gS |}|j         j        | j        k    s*|j        | j        j        k    s|j        | j        j        k    r\| j        st                      | _        | j                            || j        j        | j        j        | j         | j        j                  }|j        j	        t          j        k    r| j        j        |j        _	        |gS )N)threads)formatr4   widthr)   heightreformatterr   reformatthread_countptsr    AV_NOPTS_VALUE	frame_num)r9   r:   vframes      r$   _prepare_frames_for_encodez,VideoCodecContext._prepare_frames_for_encodeK   s    ==6M"M!T\11|tx~--}//# 6#3#5#5 %..- /  F :>S///!X/FJNxr&   c                     t                      S r-   )r   r9   s    r$   _alloc_next_framez#VideoCodecContext._alloc_next_framef   s     """r&   framepacketc                 ^    t          j        | ||           |}|                                 d S r-   )r   _setup_decoded_frame_init_user_attributes)r9   rK   rL   rF   s       r$   rN   z&VideoCodecContext._setup_decoded_framej   s4    )$v>>>"$$&&&&&r&   c                 l   | j         |S |j        j        | j         j        j        j        k    r|S | j         j        r+| j         j        t          j        t          |          _
        |S |                                 }t          t          j        |j        |j        d                     |j        |_        |S Nr   )r0   r)   r=   r3   r4   is_hw_owned	device_idr   r   r   
_device_idrJ   r   r    av_hwframe_transfer_datarC   )r9   rK   frame_sws      r$   _transfer_hwframez#VideoCodecContext._transfer_hwframep   s    #L9t/6:BBB L' 	8<8H8RFK
E**5L0022#.x|UYJJKKKyr&   c                     t          t          j        t          j        | j        j                  | j        j        | j        j                  S r-   )	r   r   r   r    AVPixelFormatr)   r4   r>   r?   rI   s    r$   r=   zVideoCodecContext.format   s9    K)48+;<<HNHO
 
 	
r&   r=   c                 l    |j         | j        _         |j        | j        _        |j        | j        _        d S r-   )r4   r)   r>   r?   )r9   r=   s     r$   r=   zVideoCodecContext.format   s(    !> -r&   c                 D    | j         t          j        u rdS | j         j        S rQ   )r)   r   NULLr>   rI   s    r$   r>   zVideoCodecContext.width   s     8v{""1x~r&   valuec                     || j         _        d S r-   )r)   r>   r9   r]   s     r$   r>   zVideoCodecContext.width   s    r&   c                 D    | j         t          j        u rdS | j         j        S rQ   )r)   r   r\   r?   rI   s    r$   r?   zVideoCodecContext.height   s     8v{""1xr&   c                     || j         _        d S r-   )r)   r?   r_   s     r$   r?   zVideoCodecContext.height   s    r&   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_samplerI   s    r$   rc   z'VideoCodecContext.bits_per_coded_sample   s     x--r&   c                 J    | j         rt          d          || j        _        d S )NzNot supported for encoders)
is_encoder
ValueErrorr)   rc   r_   s     r$   rc   z'VideoCodecContext.bits_per_coded_sample   s+    ? 	;9:::).&&&r&   c                     t          j        t          j        t           j        | j        j                            }t          j        t          |j                  S )zE
        The pixel format's name.

        :type: str | None
        )	r    av_pix_fmt_desc_getr   r   rY   r)   r4   strname)r9   descs     r$   r4   zVideoCodecContext.pix_fmt   sA     #FK0A48CS$T$TUU 	 {3	***r&   c                 8    t          |          | j        _        d S r-   )r   r)   r4   r_   s     r$   r4   zVideoCodecContext.pix_fmt   s    &u--r&   c                 X    t          t          j        | j        j                            S )zZ
        The frame rate, in frames per second.

        :type: fractions.Fraction
        )r   r   r7   r)   	frameraterI   s    r$   rn   zVideoCodecContext.framerate   s!     &fnTX5G&H&HIIIr&   c                 ^    t          |t          j        | j        j                             d S r-   )r	   r   r7   r)   rn   r_   s     r$   rn   zVideoCodecContext.framerate   s'    eV^DH,>??@@@@@r&   c                     | j         S )z#Another name for :attr:`framerate`.rn   rI   s    r$   ratezVideoCodecContext.rate   s     ~r&   c                     || _         d S r-   rq   r_   s     r$   rr   zVideoCodecContext.rate   s    r&   c                 F    | 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_sizerI   s    r$   ry   zVideoCodecContext.gop_size   s)     ? 	HFGGGx  r&   c                 J    | j         rt          d          || j        _        d S )Nru   rv   r_   s     r$   ry   zVideoCodecContext.gop_size   s,    ? 	HFGGG!r&   c                 X    t          t          j        | j        j                            S r-   )r   r   r7   r)   sample_aspect_ratiorI   s    r$   r|   z%VideoCodecContext.sample_aspect_ratio   s    %fnTX5Q&R&RSSSr&   c                 ^    t          |t          j        | j        j                             d S r-   )r	   r   r7   r)   r|   r_   s     r$   r|   z%VideoCodecContext.sample_aspect_ratio   s'    eV^DH,HIIJJJJJr&   c                 @   t          j        t          j        |j                  t          j        |j                  | j        j        | j        j        j        z  | j        j	        | j        j        j        z  d           t          t          j        |                    S )Ni   )r    	av_reducer   r7   numdenr)   r>   r|   r?   r   )r9   dars     r$   display_aspect_ratioz&VideoCodecContext.display_aspect_ratio   s{     	N37##N37##HNTX9==HOdh:>>	
 	
 	
 &fnS&9&9:::r&   c                 4    t          | j        j                  S )z
        :type: bool
        )boolr)   has_b_framesrI   s    r$   r   zVideoCodecContext.has_b_frames  s    
 DH)***r&   c                     | j         j        S )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)   r   rI   s    r$   reorder_depthzVideoCodecContext.reorder_depth
  s     x$$r&   c                     || j         _        d S r-   r   r_   s     r$   r   zVideoCodecContext.reorder_depth       %r&   c                     | j         j        S z
        :type: int
        )r)   coded_widthrI   s    r$   r   zVideoCodecContext.coded_width  s    
 x##r&   c                     | j         j        S r   )r)   coded_heightrI   s    r$   r   zVideoCodecContext.coded_height  s    
 x$$r&   c                     | j         j        S )z
        Describes the signal range of the colorspace.

        Wraps :ffmpeg:`AVFrame.color_range`.

        :type: int
        r)   color_rangerI   s    r$   r   zVideoCodecContext.color_range&  s     x##r&   c                     || j         _        d S r-   r   r_   s     r$   r   zVideoCodecContext.color_range1  s    $r&   c                     | j         j        S )z
        Describes the RGB/XYZ matrix of the colorspace.

        Wraps :ffmpeg:`AVFrame.color_primaries`.

        :type: int
        r)   color_primariesrI   s    r$   r   z!VideoCodecContext.color_primaries5  s     x''r&   c                     || j         _        d S r-   r   r_   s     r$   r   z!VideoCodecContext.color_primaries@  s    #(   r&   c                     | j         j        S )z
        Describes the linearization function (a.k.a. transformation characteristics) of the colorspace.

        Wraps :ffmpeg:`AVFrame.color_trc`.

        :type: int
        r)   	color_trcrI   s    r$   r   zVideoCodecContext.color_trcD  s     x!!r&   c                     || j         _        d S r-   r   r_   s     r$   r   zVideoCodecContext.color_trcO  s    "r&   c                     | j         j        S )z
        Describes the YUV/RGB transformation matrix of the colorspace.

        Wraps :ffmpeg:`AVFrame.colorspace`.

        :type: int
        r)   
colorspacerI   s    r$   r   zVideoCodecContext.colorspaceS  s     x""r&   c                     || j         _        d S r-   r   r_   s     r$   r   zVideoCodecContext.colorspace^  s    #r&   c                     | j         j        S )zd
        The maximum run of consecutive B frames when encoding a video.

        :type: int
        r)   max_b_framesrI   s    r$   r   zVideoCodecContext.max_b_framesb  s     x$$r&   c                     || j         _        d S r-   r   r_   s     r$   r   zVideoCodecContext.max_b_framesk  r   r&   c                     | j         j        S )z
        The minimum quantiser value of an encoded stream.

        Wraps :ffmpeg:`AVCodecContext.qmin`.

        :type: int
        r)   qminrI   s    r$   r   zVideoCodecContext.qmino       x}r&   c                     || j         _        d S r-   r   r_   s     r$   r   zVideoCodecContext.qminz      r&   c                     | j         j        S )z
        The maximum quantiser value of an encoded stream.

        Wraps :ffmpeg:`AVCodecContext.qmax`.

        :type: int
        r)   qmaxrI   s    r$   r   zVideoCodecContext.qmax~  r   r&   c                     || j         _        d S r-   r   r_   s     r$   r   zVideoCodecContext.qmax  r   r&   )-__name__
__module____qualname__r   cfuncr   r    AVCodecContextconstAVCodecr   r.   r   listrG   rJ   r   rN   rW   propertyr=   setterr
   r>   uintr?   rc   intr4   rn   rr   ry   r|   r   r   r   r   r   r   r   r   r   r   r   r    r&   r$   r(   r(   (   s       \(^C./( ~fl3;78( 4	( ( ( \(@ \     \4 \#5 # # # \# \'% ' ' ' ' \'
 \u    \$ 
 
 X
 ]([ ( ( ( ](
   X
 \6;    \   X
 ] FK       ]  . . X. !/6: / / / "!/ 	+ 	+ X	+ ^. . ^. J J XJ A A A   X 
[  [ ! ! X! _"fj " " " _"
 T T XT K K  K 
; 
; X
; + + X+ % % X% &6: & & & & $ $ X$ % % X% $ $ X$ % % % ( ( X( ) ) ) " " X" # # # # # X# $ $ $ % % X% & & &   X 
[  [   X 
[  [  r&   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   rY   r%   cclassr(   r   r&   r$   <module>r      s    # # # # # # # # # 9 9 9 9 9 9 4 4 4 4 4 4 . . . . . . * * * * * * , , , , , , J J J J J J J J V V V V V V V V V V H H H H H H H H A A A A A A / / / / / / 	*	+nV\#*;<= 	    . b b b b b b b b b br&   