
    \jV;                        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 jB                  ejD                        dejD                  fd              Z#e jH                  e jJ                   G d de                    Z&y)    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k7  r%||   |j
                  k(  r||   S |dz  }||   dk7  r%|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is       H/root/.hermes/venv/lib/python3.12/site-packages/av/video/codeccontext.py_get_hw_formatr%      s     8>{{)*CJJ8L A
1+
A;,777A;	Q 1+

 '>>DGDWDW    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e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j4                  fd       Zed        Zej.                  dej4                  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!        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d0        Z+e+j.                  d1        Z+ed2        Z,e,j.                  d3        Z,ed4        Z-e-j.                  d5        Z-ed6        Z.e.j.                  d7        Z.ed8        Z/e/j.                  d9        Z/y):VideoCodecContextptrcodechwaccelNc                    t        j                  | |||       |y | j                  r[|j                  | j                  d      | _        t        j                  | j
                  j                        | j                  _	        y 	 |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                  _        y # t*        $ r
 d | _        Y y w xY w)NT)for_encoding)r   _init
is_encoder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+   sK    	4eW5???
  '~~djjt~LD%(%6%6t7G7G7K7K%LDHH"	$&~~djj9D%(%6%6t7G7G7K7K%LDHH"#//66::BBDHH"0DHH262B2B2I2I2M2M2U2UD/44 6 %nnT-?-?@DHHO" 		$  $D		$s   D2F7 7G
	G
vframer   c           
         t        j                  t         j                  t        j                     | j
                  j                  j                        }|j
                  j                  |j                  k(  r|S |j
                  j                  |j                  k7  sF|j
                  j                  |j                  k7  s#|j
                  j                  |j                  k7  r| j                  st               | _        | j                  j                  ||j                  |j                  t        |j                  |j                  |j                        | j
                  j                         }t#               }t%        t        j&                  | j
                  j                  |j
                  d             t%        t        j(                  |j
                  |j
                  d             |j+                  |d       |j-                          |j
                  j.                  t        j0                  k(  r%| j
                  j2                  |j
                  _        |S )Nthreadsr   Fdata_layout)r   r   r   r    AVHWFramesContextr)   hw_frames_ctxdataformat	sw_formatwidthheightreformatterr   reformatr   thread_countr   r   av_hwframe_get_bufferav_hwframe_transfer_data_copy_internal_attributes_init_user_attributesptsAV_NOPTS_VALUE	frame_num)r:   r;   
frames_ctxhwframes       r$   _encode_upload_framez&VideoCodecContext._encode_upload_frameU   s    =CKKNN30014883I3I3N3N=


 ::
 1 11M
 JJ!5!55zz:#3#33zz  J$5$55###3#5 %%..  !! ((**:*:J<M<M -- / F 01#++DHH,B,BGKKQRST#..w{{FJJJK))&e)D%%';;??c000"hh00GKKOr&   inputc                    ||sd gS |}| j                   j                  t        j                  k7  r| j	                  |      gS |j
                  j                  | j                  k7  sF|j                  | j                   j                  k7  s#|j                  | j                   j                  k7  r| j                  st               | _	        | j                  j                  || j                   j                  | j                   j                  | j
                  | j                   j                        }|j                   j                  t        j                  k(  r%| j                   j                   |j                   _        |gS )Nr=   )r)   rB   r   NULLrT   rD   r5   rF   rG   rH   r   rI   rJ   rO   r    rP   rQ   )r:   rU   r;   s      r$   _prepare_frames_for_encodez,VideoCodecContext._prepare_frames_for_encode~   s   =6M" 88!!V[[0--f566 MM!!T\\1||txx~~-}}/###3#5 %%..-- / F ::>>S///!XX//FJJNxr&   c                     t               S N)r   r:   s    r$   _alloc_next_framez#VideoCodecContext._alloc_next_frame   s     ""r&   framepacketc                 V    t        j                  | ||       |}|j                          y rZ   )r   _setup_decoded_framerN   )r:   r]   r^   r;   s       r$   r`   z&VideoCodecContext._setup_decoded_frame   s%    ))$v>"$$&r&   c                    | j                   |S |j                  j                  | j                   j                  j                  j                  k7  r|S | j                   j
                  r5| j                   j                  t        j                  t        |      _
        |S | j                         }t        t        j                  |j                  |j                  d             |j                  |d       |S )Nr   Fr?   )r1   r)   rD   r4   r5   is_hw_owned	device_idr   r   r   
_device_idr\   r   r    rL   rM   )r:   r]   frame_sws      r$   _transfer_hwframez#VideoCodecContext._transfer_hwframe   s    #L99t//66::BBB L''8<8H8H8R8RFKK
E*5L002#..x||UYYJK**5e*Dr&   c                     t        t        j                  t        j                  | j
                  j                        | j
                  j                  | j
                  j                        S rZ   )	r   r   r   r    AVPixelFormatr)   r5   rF   rG   r[   s    r$   rD   zVideoCodecContext.format   sC    KK))488+;+;<HHNNHHOO
 	
r&   rD   c                     |j                   | j                  _         |j                  | j                  _        |j                  | j                  _        y rZ   )r5   r)   rF   rG   )r:   rD   s     r$   rD   zVideoCodecContext.format   s0    !>> --r&   c                 h    | j                   t        j                  u ry| j                   j                  S Nr   )r)   r   rW   rF   r[   s    r$   rF   zVideoCodecContext.width   s#    88v{{"xx~~r&   valuec                 &    || j                   _        y rZ   )r)   rF   r:   rl   s     r$   rF   zVideoCodecContext.width   s    r&   c                 h    | j                   t        j                  u ry| j                   j                  S rk   )r)   r   rW   rG   r[   s    r$   rG   zVideoCodecContext.height   s#    88v{{"xxr&   c                 &    || j                   _        y rZ   )r)   rG   rn   s     r$   rG   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_sampler[   s    r$   rr   z'VideoCodecContext.bits_per_coded_sample   s     xx---r&   c                 T    | j                   rt        d      || j                  _        y )NzNot supported for encoders)r/   
ValueErrorr)   rr   rn   s     r$   rr   z'VideoCodecContext.bits_per_coded_sample   s!    ??9::).&r&   c                    t        j                  t        j                  t         j                  | j
                  j                              }|t        j                  k(  ryt        j                  t        |j                        S )zE
        The pixel format's name.

        :type: str | None
        N)
r    av_pix_fmt_desc_getr   r   rh   r)   r5   rW   strname)r:   descs     r$   r5   zVideoCodecContext.pix_fmt   sX     ##FKK0A0A488CSCS$TU 	 6;;{{3		**r&   c                 8    t        |      | j                  _        y rZ   )r   r)   r5   rn   s     r$   r5   zVideoCodecContext.pix_fmt   s    &u-r&   c                 x   | j                   j                  s| j                   j                  t        j                  k7  rkt        t        j                  t        j                  | j                   j                        | j                   j                  | j                   j                        S yt        j                  t        j                  t        j                     | j                   j                  j                        }t        |j                  | j                   j                  | j                   j                        S )z
        For a hardware context (e.g. ``format.name == "cuda"``), the underlying
        software pixel format (``nv12``, ``p010le``, ...). ``None`` for a regular
        software context.

        :type: VideoFormat | None
        N)r)   rB   r   r    r!   r   r   r   rh   rF   rG   r   rA   rC   rE   )r:   rR   s     r$   rE   zVideoCodecContext.sw_format   s     xx%%xx""c&9&99'KK 1 14883F3FGHHNNHHOO 
 <BKKNN30014883I3I3N3N=

  
 4 4dhhnndhhooVVr&   c                 8    t        |      | j                  _        y rZ   )r   r)   r   rn   s     r$   rE   zVideoCodecContext.sw_format  s    )%0r&   c                 f    t        t        j                  | j                  j                              S )zZ
        The frame rate, in frames per second.

        :type: fractions.Fraction
        )r   r   r8   r)   	framerater[   s    r$   r~   zVideoCodecContext.framerate  s"     &fnnTXX5G5G&HIIr&   c                 j    t        |t        j                  | j                  j                               y rZ   )r	   r   r8   r)   r~   rn   s     r$   r~   zVideoCodecContext.framerate#  s    eV^^DHH,>,>?@r&   c                     | j                   S )z#Another name for :attr:`framerate`.r~   r[   s    r$   ratezVideoCodecContext.rate'  s     ~~r&   c                     || _         y rZ   r   rn   s     r$   r   zVideoCodecContext.rate,  s	    r&   c                 \    | 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_sizer[   s    r$   r   zVideoCodecContext.gop_size0  s'     ??FGGxx   r&   c                 T    | j                   rt        d      || j                  _        y )Nr   r   rn   s     r$   r   zVideoCodecContext.gop_size;  s!    ??FGG!r&   c                 f    t        t        j                  | j                  j                              S rZ   )r   r   r8   r)   sample_aspect_ratior[   s    r$   r   z%VideoCodecContext.sample_aspect_ratioA  s     %fnnTXX5Q5Q&RSSr&   c                 j    t        |t        j                  | j                  j                               y rZ   )r	   r   r8   r)   r   rn   s     r$   r   z%VideoCodecContext.sample_aspect_ratioE  s    eV^^DHH,H,HI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   r8   numdenr)   rF   r   rG   r   )r:   dars     r$   display_aspect_ratioz&VideoCodecContext.display_aspect_ratioI  s     	NN377#NN377#HHNNTXX99===HHOOdhh::>>>	
 &fnnS&9::r&   c                 @    t        | j                  j                        S )z
        :type: bool
        )boolr)   has_b_framesr[   s    r$   r   zVideoCodecContext.has_b_framesV  s    
 DHH))**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   r[   s    r$   reorder_depthzVideoCodecContext.reorder_depth]  s     xx$$$r&   c                 &    || j                   _        y rZ   r   rn   s     r$   r   zVideoCodecContext.reorder_depthg       %r&   c                 .    | j                   j                  S z
        :type: int
        )r)   coded_widthr[   s    r$   r   zVideoCodecContext.coded_widthk  s    
 xx###r&   c                 .    | j                   j                  S r   )r)   coded_heightr[   s    r$   r   zVideoCodecContext.coded_heightr  s    
 xx$$$r&   c                 .    | j                   j                  S )z
        Describes the signal range of the colorspace.

        Wraps :ffmpeg:`AVFrame.color_range`.

        :type: int
        r)   color_ranger[   s    r$   r   zVideoCodecContext.color_rangey  s     xx###r&   c                 &    || j                   _        y rZ   r   rn   s     r$   r   zVideoCodecContext.color_range  s    $r&   c                 .    | j                   j                  S )z
        Describes the RGB/XYZ matrix of the colorspace.

        Wraps :ffmpeg:`AVFrame.color_primaries`.

        :type: int
        r)   color_primariesr[   s    r$   r   z!VideoCodecContext.color_primaries  s     xx'''r&   c                 &    || j                   _        y rZ   r   rn   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_trcr[   s    r$   r   zVideoCodecContext.color_trc  s     xx!!!r&   c                 &    || j                   _        y rZ   r   rn   s     r$   r   zVideoCodecContext.color_trc  s    "r&   c                 .    | j                   j                  S )z
        Describes the YUV/RGB transformation matrix of the colorspace.

        Wraps :ffmpeg:`AVFrame.colorspace`.

        :type: int
        r)   
colorspacer[   s    r$   r   zVideoCodecContext.colorspace  s     xx"""r&   c                 &    || j                   _        y rZ   r   rn   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_framesr[   s    r$   r   zVideoCodecContext.max_b_frames  s     xx$$$r&   c                 &    || j                   _        y rZ   r   rn   s     r$   r   zVideoCodecContext.max_b_frames  r   r&   c                 .    | j                   j                  S )z
        The minimum quantiser value of an encoded stream.

        Wraps :ffmpeg:`AVCodecContext.qmin`.

        :type: int
        r)   qminr[   s    r$   r   zVideoCodecContext.qmin       xx}}r&   c                 &    || j                   _        y rZ   r   rn   s     r$   r   zVideoCodecContext.qmin      r&   c                 .    | j                   j                  S )z
        The maximum quantiser value of an encoded stream.

        Wraps :ffmpeg:`AVCodecContext.qmax`.

        :type: int
        r)   qmaxr[   s    r$   r   zVideoCodecContext.qmax  r   r&   c                 &    || j                   _        y rZ   r   rn   s     r$   r   zVideoCodecContext.qmax  r   r&   )0__name__
__module____qualname__r   cfuncr   r    AVCodecContextconstAVCodecr   r.   r   rT   r   listrX   r\   r   r`   rf   propertyrD   setterr
   rF   uintrG   rr   intr5   rE   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r&   r$   r(   r(   (   s    \\'$^^C../'$ ~~fll3;;78'$ 4	'$ '$R \\&: &* & &P \\   > \\#5 # # \\'% ' ' '
 \\u  " 
 
 ]]([ ( (
  
 \\6;;    
 ]] FKK     . . !!/6:: / "/ + + ^^. . W W* 1 1 J J A A   
[[  ! ! __"fjj " "
 T T K  K 
; 
; + + % % &6:: & & $ $ % % $ $ % % ( ( ) ) " " # # # # $ $ % % & &   
[[    
[[ 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   rh   r%   finalcclassr(   r   r&   r$   <module>r      s     # # 9 4 . * , J V V H A / 	**	+nnV\\#*;*;<= 	  . t t  tr&   