
    EjT.                        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       K/var/www/html/qr/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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j0                  fd       Zed        Zej*                  dej0                  fd       Zed        Zej*                  dej6                  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j6                  fd       Zed         Z e j*                  d!        Z ed"        Z!ed#        Z"ed$        Z#e#j*                  dej6                  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,y)6VideoCodecContextptrcodechwaccelNc                    t        j                  | |||       |4	 |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 y # t(        $ r
 d | _        Y y w xY w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(#*>>$**#= ),):):4;K;K;O;O)P&#'#3#3#:#:#>#>#F#F &4#$$++//77 ""3 LL88 "": #)..1C1C"D  ' 	( $( 	(s   D2E E$#E$inputr   c                    ||sd 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 )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_encodeL   s    =6M"MM!!T\\1||txx~~-}}/###3#5 %%..-- / F ::>>S///!XX//FJJNxr&   c                     t               S r-   )r   r9   s    r$   _alloc_next_framez#VideoCodecContext._alloc_next_frameg   s     ""r&   framepacketc                 V    t        j                  | ||       |}|j                          y r-   )r   _setup_decoded_frame_init_user_attributes)r9   rK   rL   rF   s       r$   rN   z&VideoCodecContext._setup_decoded_framek   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                  |_        |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_hwframeq   s    #L99t//66::BBB L''8<8H8H8R8RFKK
E*5L002#..x||UYYJKyy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   sC    KK))488+;+;<HHNNHHOO
 	
r&   r=   c                     |j                   | j                  _         |j                  | j                  _        |j                  | j                  _        y r-   )r4   r)   r>   r?   )r9   r=   s     r$   r=   zVideoCodecContext.format   s0    !>> --r&   c                 h    | j                   t        j                  u ry| j                   j                  S rQ   )r)   r   NULLr>   rI   s    r$   r>   zVideoCodecContext.width   s#    88v{{"xx~~r&   valuec                 &    || j                   _        y r-   )r)   r>   r9   r]   s     r$   r>   zVideoCodecContext.width   s    r&   c                 h    | j                   t        j                  u ry| j                   j                  S rQ   )r)   r   r\   r?   rI   s    r$   r?   zVideoCodecContext.height   s#    88v{{"xxr&   c                 &    || j                   _        y 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     xx---r&   c                 T    | j                   rt        d      || j                  _        y )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   sI     ##FKK0A0A488CSCS$TU 	 {{3		**r&   c                 8    t        |      | j                  _        y r-   )r   r)   r4   r_   s     r$   r4   zVideoCodecContext.pix_fmt   s    &u-r&   c                 f    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"     &fnnTXX5G5G&HIIr&   c                 j    t        |t        j                  | j                  j                               y r-   )r	   r   r7   r)   rn   r_   s     r$   rn   zVideoCodecContext.framerate   s    eV^^DHH,>,>?@r&   c                     | j                   S )z#Another name for :attr:`framerate`.rn   rI   s    r$   ratezVideoCodecContext.rate   s     ~~r&   c                     || _         y r-   rq   r_   s     r$   rr   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_sizerI   s    r$   ry   zVideoCodecContext.gop_size   s'     ??FGGxx   r&   c                 T    | j                   rt        d      || j                  _        y )Nru   rv   r_   s     r$   ry   zVideoCodecContext.gop_size   s!    ??FGG!r&   c                 f    t        t        j                  | j                  j                              S r-   )r   r   r7   r)   sample_aspect_ratiorI   s    r$   r|   z%VideoCodecContext.sample_aspect_ratio   s     %fnnTXX5Q5Q&RSSr&   c                 j    t        |t        j                  | j                  j                               y r-   )r	   r   r7   r)   r|   r_   s     r$   r|   z%VideoCodecContext.sample_aspect_ratio   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   r7   numdenr)   r>   r|   r?   r   )r9   dars     r$   display_aspect_ratioz&VideoCodecContext.display_aspect_ratio   s     	NN377#NN377#HHNNTXX99===HHOOdhh::>>>	
 &fnnS&9::r&   c                 @    t        | j                  j                        S )z
        :type: bool
        )boolr)   has_b_framesrI   s    r$   r   zVideoCodecContext.has_b_frames  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   rI   s    r$   reorder_depthzVideoCodecContext.reorder_depth  s     xx$$$r&   c                 &    || j                   _        y 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    
 xx###r&   c                 .    | j                   j                  S r   )r)   coded_heightrI   s    r$   r   zVideoCodecContext.coded_height   s    
 xx$$$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     xx###r&   c                 &    || j                   _        y r-   r   r_   s     r$   r   zVideoCodecContext.color_range2  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_primaries6  s     xx'''r&   c                 &    || j                   _        y r-   r   r_   s     r$   r   z!VideoCodecContext.color_primariesA  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_trcE  s     xx!!!r&   c                 &    || j                   _        y r-   r   r_   s     r$   r   zVideoCodecContext.color_trcP  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.colorspaceT  s     xx"""r&   c                 &    || j                   _        y 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_framesc  s     xx$$$r&   c                 &    || j                   _        y r-   r   r_   s     r$   r   zVideoCodecContext.max_b_framesl  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.qminp       xx}}r&   c                 &    || j                   _        y r-   r   r_   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)   qmaxrI   s    r$   r   zVideoCodecContext.qmax  r   r&   c                 &    || j                   _        y 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../( ~~fll3;;78( 4	( (@ \\   4 \\#5 # # \\'% ' ' '
 \\u  $ 
 
 ]]([ ( (
  
 \\6;;    
 ]] FKK     . . !!/6:: / "/ 	+ 	+ ^^. . 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   rY   r%   finalcclassr(   r   r&   r$   <module>r      s     # # 9 4 . * , J V V H A / 	**	+nnV\\#*;*;<= 	  . b b  br&   