
    ybj                       S SK r S SKJr  S SKrS SKJs  Jr  S SKJ	r	  S SK
Jr  S SKJr  S SKJr  S SKJrJr  S SKJrJrJrJr  S S	KJr  S S
KJrJrJr  S SKJrJ r   S SK!J"r"J#r#  S SK$J%r%J&r&J'r'  \RP                  \RR                  \RT                  " SS9S\RV                  \RX                     S\RZ                  4S j5       5       5       r.\R^                  \R`                   " S S5      5       5       r1\RP                  S\2S\2S\RV                  \   4S j5       r3\RP                  \RR                  \RT                  " SS9S\Rh                  S\RV                  \&   S\RZ                  4S j5       5       5       r5\RP                  \RR                  \RT                  " SS9S\Rh                  S\RV                  \&   S\RZ                  4S j5       5       5       r6\Rn                  " \2\2" 5       5      r81 Skr9\Rn                  " \:\;\<\Rz                  \;4   4   0 SS_SS_S S!_S"S!_S#S!_S$S!_S%S&_S'S&_S(S&_S)S&_S*S&_S+S&_S,S&_S-S&_S.S/_S0S1_S2S1_0 S3S!_S4S_S5S6_S7S6_S8S!_S9S&_S:S&_S;S&_S<S&_S=S&_S>S&_S?S&_S@S&_SASB_SCSB_SDS!_SES&_E0 SFS&_SGS&_SHS&_SIS&_SJS&_SKS&_SLS&_SMS&_SNS&_SOSB_SPSB_SQS!_SRS&_SSS&_STS&_SUS&_SVS&_E0 SWS&_SXS&_SYS&_SZS!_S[S&_S\S&_S]SB_S^SB_S_S/_S`S1_SaS1_SbS!_ScS_SdS6_SeS6_SfSg_ShSg_ESiSiSjSjS!S&S&S&S&S!SkSl.E5      r>\RP                  S\?4Sm j5       r@ " Sn So\5      rA\Rn                  " \R                  \ R                  Sp:H  5      rD\RP                  \R                  Sq\R                  4Sr j5       5       rF\RP                  Ss\St\Rz                  Su\R                  Sv\R                  4Sw j5       rG\RP                  Ss\St\Rz                  4Sx j5       rH\RP                  \R                   S!Ss\St\Rz                  Sy\;4Sz jj5       5       rI\RP                  S{\2S|\R                  4S} j5       rJ\R^                  \R`                   " S~ S\K5      5       5       r?g)    N)IntEnum)
Dictionary)	err_checkget_display_rotation)check_ndarray)get_pix_fmtget_video_format)DLManagedTensor
VideoPlanekCPUkCuda)PyErr_Clear)PyCapsule_GetPointerPyCapsule_IsValidPyCapsule_SetName)	Py_DECREF	Py_INCREF)AVCUDADeviceContextCUstream)int64_tuint8_t	uintptr_tF)check
device_ctxreturnc                    [         R                  " [         R                  [        R                     U R
                  5      n[        R                  " [         R                  " U5      5        [         R                  U l        g N)	cythoncastpointerlibAVBufferRefuser_opaqueav_buffer_unrefaddressNULL)r   	owner_refs     ڊ/tmp/claude-0/-home-danvics-docker-quiz/c1e0577a-e42c-4a3d-b1ea-3edd61103a4e/scratchpad/stt/lib/python3.13/site-packages/av/video/frame.py_cuda_device_ctx_freer*      sO     28s')?)?2I y12#[[J    c            
       |   \ rS rSrSr    SS\R                  S\R                  S\R                  S\4S jjr	S	 r
\R                  S
\R                  \R                     4S j5       r\R                  S\R                   S\R                  S\R                  S
\R                  \R                     4S j5       rSrg)CudaContext$   a  A reusable FFmpeg CUDA context for frames imported with DLPack.

:param int device_id: CUDA device ordinal.
:param bool primary_ctx: Retain the device's primary CUDA context.
:param bool current_ctx: Wrap the CUDA context current on the calling thread
    when this object is first used. This is mutually exclusive with
    ``primary_ctx``.
:param int cuda_stream: Optional raw ``CUstream`` pointer for FFmpeg CUDA
    operations, including NVENC input and output. The caller owns the
    stream and must keep it alive as long as this context can be used.
    Nonzero stream pointers are currently supported only for logical CUDA
    device 0. To use another physical GPU, expose it as logical device 0
    via ``CUDA_VISIBLE_DEVICES``.
N	device_idprimary_ctxcurrent_ctxcuda_streamc                    Xl         X l        X0l        SU l        [        R
                  U l        0 U l        U(       a  U(       a  [        S5      eUbQ  [        U[        5      (       d  [        S5      eUS:  a  [        S5      e[        R                  " [        U5      U l        U R                  (       a  U R                   S:w  a  [        S5      eg g )Nr   z2primary_ctx and current_ctx are mutually exclusivez&cuda_stream must be an integer or Nonez cuda_stream must be non-negativezxcuda_stream currently requires device_id 0; expose the desired physical GPU as logical device 0 via CUDA_VISIBLE_DEVICES)r/   r0   r1   r2   r   r'   _device_ref_frames_cache
ValueError
isinstanceint	TypeErrorr    r   )selfr/   r0   r1   r2   s        r)   	__cinit__CudaContext.__cinit__6   s     #&&!;;;QRR"k3// HIIQ !CDD%{{9kBD! 3L  !4r+   c                 @   U R                   R                  5        H  n[        R                  " [        R                  [
        R                     [        R                  " [        R                  U5      5      n[
        R                  " [        R                  " U5      5        M     U R                   R                  5         U R                  nU[        R                  :w  a@  [
        R                  " [        R                  " U5      5        [        R                  U l        g g r   )r5   valuesr   r    r!   r"   r#   size_tr%   r&   clearr4   r'   )r:   vrefs      r)   __dealloc__CudaContext.__dealloc__Q   s     ##**,A++s/FMM1-C s 34 - 	  "&++s 34%{{D r+   r   c           	         U R                   nU[        R                  :w  a  U$ [        R                  nU R                   R	                  5       nUnSU R
                  (       a  SOS0nU R                  (       a  SUS'   [        U5      n[        [        R                  " [        R                  " U5      [        R                  UUR                  S5      5        U R                  (       d  X l         U$ [        R                  " [        R                  5      nU[        R                  :X  a5  [        R                   " [        R                  " U5      5        [#        S5      e [        R$                  " [        R&                  [        R(                     UR*                  5      n[        R$                  " [        R&                  [,           UR.                  5      n[        R$                  " [        R&                  [        R(                     UR*                  5      n	[        R$                  " [        R&                  [,           U	R.                  5      n
UR0                  U
l        [        R$                  " [2        U R                  5      U
l        [6        U	l        [        R$                  " [        R:                  U5      U	l        [        R                  n[        [        R>                  " U5      5        Xl         U$ ! [@         aV    [        R                   " [        R                  " U5      5        [        R                   " [        R                  " U5      5        e f = f)Nr0   10r1   r   zav_hwdevice_ctx_alloc() failed)!r4   r   r'   r/   encoder0   r1   r   r   r"   av_hwdevice_ctx_creater&   AV_HWDEVICE_TYPE_CUDAptrr2   av_hwdevice_ctx_allocr%   MemoryErrorr    r!   AVHWDeviceContextdatar   hwctxcuda_ctxr   streamr*   freep_voidr$   av_hwdevice_ctx_init	Exception)r:   
device_refr(   device_bytesc_deviceoptions_dictoptionsowner_device_ctxowner_cuda_ctxr   rQ   s              r)   _get_device_refCudaContext._get_device_refa   s`   6:6F6F
$KK	..)113".%d.>.>sCH*-L'(6&&y)))	
 (..s/H/HI
$y 9:>??	FLkks445y~~G CI++235E5K5KCN AGs445zAJ =CKK23Z5E5E=H !/ 7 7H$kk(D4D4DEHO3JO%+[[	%JJ"Ic..z:; &  	z :;y 9:	s   FK1 1A Msw_fmtwidthheightc                    [        U5      [        U5      [        U5      4nU R                  R                  U5      nUb  [        R                  " [        R
                  [        R                     [        R                  " [        R                  U5      5      n[        R                  " U5      nU[        R                  :X  a  [        S5      eU$ U R                  5       n[        R                  " U5      n	U	[        R                  :X  a  [        S5      e [        R                  " [        R
                  [        R                     U	R                  5      n
[!        S5      U
l        Xl        [        U5      U
l        [        U5      U
l        [+        [        R,                  " U	5      5        [        R                  " U	5      nU[        R                  :X  a5  [        R0                  " [        R2                  " U	5      5        [        S5      e[        R                  " [        R                  U	5      U R                  U'   U$ ! [.         a,    [        R0                  " [        R2                  " U	5      5        e f = f)Nzav_buffer_ref() failedzav_hwframe_ctx_alloc() failed   cuda)r8   r5   getr   r    r!   r"   r#   r?   av_buffer_refr'   rM   r^   av_hwframe_ctx_allocAVHWFramesContextrO   r	   format	sw_formatra   rb   r   av_hwframe_ctx_initrV   r%   r&   )r:   r`   ra   rb   keycached
cached_refout_refrW   
frames_ref
frames_ctxs              r)   get_frames_ctxCudaContext.get_frames_ctx   s    6{CJF4##'', s/FMM62J ''
3G&++%!":;;N))+
696N6N7

 $=>>	@Fs445zAJ !,G 4J#) "5zJ #FJc--j9:
 ##J/fkk!z :;677"(++fmmZ"H3  	z :;	s   BH= =6I3)r4   r5   r2   r1   r/   r0   )r   TFN)__name__
__module____qualname____firstlineno____doc__r   r8   bintobjectr;   rC   cfuncr!   r"   r#   r^   AVPixelFormatrr   __static_attributes__ r+   r)   r-   r-   $   s    " !"#'#(":: [[ [[	
 6+  \\8!@ 8 8t \\0!!0 zz0 

	0
 
	(0 0r+   r-   objrR   c                    [        U S5      (       a#  Uc  U R                  5       OU R                  US9nOU n[        US5      (       d  [        5         [	        S5      e[
        R                  " [
        R                  [           [        US5      5      nU[
        R                  :X  a  [        S5      e[        US5      S:w  a  [        S5      eU$ )	N
__dlpack__)rR   s   dltensorz>expected a DLPack capsule or an object implementing __dlpack__z"PyCapsule_GetPointer returned NULLs   used_dltensorr   zPyCapsule_SetName failed)hasattrr   r   r   r9   r   r    r!   r   r   r'   r6   r   RuntimeError)r   rR   capsulemanageds       r)   _consume_dlpackr      s    
 sL!!&,n#.."#..PV.:WWk22L
 	
 kk'Wk2G &++=>>"23q8566Nr+   opaquerO   c                     [         R                  " [         R                  [           U 5      nU[         R                  :w  a  UR                  U5        g g r   )r   r    r!   r   r'   deleter)r   rO   r   s      r)   _dlpack_avbuffer_freer      s?     06{{'0G &++  r+   c                     U [         R                  :w  a>  [         R                     [        [         R                  " [
        U 5      5        S S S 5        g g ! , (       d  f       g = fr   )r   r'   gilr   r    rz   )r   rO   s     r)   _numpy_avbuffer_freer      s<     ZZfkk&&12 Z Zs   %A
A">U   abgrargbbgr8bgragbrpgraynv12pal8rgb8rgbabgr24gbrapgray8rgb24bgr48bebgr48legbrp9begbrp9legray9begray9lergb48bergb48leyuv420pyuv444pyuyv422bgra64bebgra64legbrp10begbrp10legbrp12begbrp12legbrp14begbrp14legbrp16begbrp16legray10begray10legray12begray12legray14begray14legray16begray16lergba64bergba64lergbf32bergbf32leyuvj420pyuvj444p	gbrap10be	gbrap10le	gbrap12be	gbrap12le	gbrap14be	gbrap14le	gbrap16be	gbrap16le	gbrpf32be	gbrpf32le	grayf32be	grayf32le	rgbaf16be	rgbaf16le	rgbaf32be	rgbaf32le
gbrapf32be
gbrapf32lebayer_bggr8bayer_gbrg8bayer_grbg8bayer_rggb8yuv420p10leyuv422p10leyuv444p16beyuv444p16leyuva444p16beyuva444p16lebayer_bggr16bebayer_bggr16lebayer_gbrg16bebayer_gbrg16lebayer_grbg16bebayer_grbg16lebayer_rggb16bebayer_rggb16ler   )   uint8r   r   )   r   r   r   r   r   )   uint16r   r   r   r   r   r   r   r   )   r   r   )   r   r   r   r   r   )   r   r   r   r   r   r   r   r   r   r   r   r   )r   float32r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   float16r   )   r   )   r   )r   r   )r   r   r   r   r   r   r   r   r   r   r   c                       [        [        5      $ )zGet a mostly uninitialized VideoFrame.

You MUST call VideoFrame._init(...) or VideoFrame._init_user_attributes()
before exposing to the user.

)
VideoFrame_cinit_bypass_sentinelr~   r+   r)   alloc_video_framer     s     ,--r+   c                       \ rS rSr\R
                  r\R                  r\R                  r
\R                  r\R                  r\R                  r\R"                  r\R&                  rSrg)PictureTypei  r~   N)rt   ru   rv   rw   r"   AV_PICTURE_TYPE_NONENONEAV_PICTURE_TYPE_IIAV_PICTURE_TYPE_PPAV_PICTURE_TYPE_BBAV_PICTURE_TYPE_SSAV_PICTURE_TYPE_SISIAV_PICTURE_TYPE_SPSPAV_PICTURE_TYPE_BIBIr}   r~   r+   r)   r   r     sZ    ##DAAAA			B			B			Br+   r   big
big_endianc                 :    [         U:w  a  U R                  5       $ U $ r   )_is_big_endianbyteswap)arrayr  s     r)   byteswap_arrayr    s     #~~Lr+   planebytes_per_pixelflip_horizontalflip_verticalc                    U nSnUR                   U-  nUnSn	[        UR                  5      n
[        R                  " [        R
                  5      u  pnU(       a  UR                  S-
  nSnSnOSnUR                  nSn[        XU5       HZ  nX-  nU(       a>  [        SXr5       H-  n[        U5       H  nUXg-   U-
  U-
  U-      XU-   U-   '   M     M/     O
XVXg-    XX-   & X-  n	M\     g )Nr   r   )ra   abs	line_sizer   declarer8   rb   range)	img_bytesr  r  r  r  i_bufi_posi_strideo_bufo_poso_stride	start_rowend_rowsteprowijs                    r)   copy_bytes_to_planer#    s     '0EE#kkO;HEE!%//2H%~~fjj9ILL1$		,,Y.1h8/A+0(1,>B,E!)a-( 0 9 /4E<L.ME%*+ /r+   c                 @    U R                  5       n[        X1USS5        g )NF)tobytesr#  )r
  r  r  imgbytess       r)   copy_array_to_planer'    s    mmoH%Gr+   dtypec                    SSK nUR                  U5      nU R                  R                  R                  U R
                     n[        U5      nUR                  nX-  nUS:X  a  U R                  U R                  4n	Xg4n
OU R                  U R                  U4n	XaU4n
US:  a-  U R                  S-
  U-  nU* /U
SS Q7n
UR                  XXU
S9$ UR                  XX
S9$ )z
Return the useful part of the VideoPlane as a strided array.

We are simply creating a view that discards any padding which was added for
alignment.
r   Nr   )r(  bufferoffsetstrides)r(  r*  r,  )numpyr(  framerK   linesizeindexr  itemsizerb   ra   ndarray)r  r  r(  np	dtype_objr  total_line_sizer1  channelsshaper,  r+  s               r)   useful_arrayr8  
  s     I((5I)nO!!H*H1}u{{+"-u{{H5"X>1},,"o5##2gabk2zz5  
 	
 ::eU:LLr+   r
  okc                 D    U(       d  [        SU R                   S35      eg )NzUnexpected numpy array shape ``)r6   r7  )r
  r9  s     r)   check_ndarray_shaper<  .  s#    9%++aHII r+   c                   p   \ rS rSrS/S jr\R                  S\R                  S\R                  S\R                  4S j5       r
\R                  S 5       rS r\S	 5       rS
 r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\R,                  S 5       r\S 5       r\R,                  S 5       r\S 5       r\R,                  S 5       r\S 5       r\R,                  S 5       r\S 5       r\R,                  S 5       rS rS rS\4S jrS r\R@                  " S5      S0S  j5       r!S! r"\#S" 5       r$\#S1S# j5       r%S$ r&\#\R@                  " S5      S2S% j5       5       r'\#   S3S&\(S\)S\)4S' jj5       r*\#        S4S\+S\)S\)S)\)S(-  S*\,S(-  S+\,S(-  4S, jj5       r-\#S- 5       r.S.r/g()5r   i4  c                 R    U[         L a  g [        U5      nU R                  XAU5        g r   )r   r	   _init)r:   ra   rb   ri   c_formats        r)   r;   VideoFrame.__cinit__7  s&    **&1&&9

8F+r+   ri   ra   rb   c                 ^   Sn[         R                     X R                  l        X0R                  l        XR                  l        U(       a(  U(       a!  [        R                  " U R                  S5      nS S S 5        U(       a  [        U5        U R                  5         g ! , (       d  f       N1= f)Nr   r   )
r   nogilrK   ra   rb   ri   r"   av_frame_get_bufferr   _init_user_attributes)r:   ri   ra   rb   ress        r)   r?  VideoFrame._init>  sk    \\"HHN$HHO$HHO --dhh;  cN""$ \s   A B
B,c                     [        [        R                  " [        R                  U R
                  R                  5      U R
                  R                  U R
                  R                  5      U l        g r   )	r
   r   r    r"   r|   rK   ri   ra   rb   r:   s    r)   rE   VideoFrame._init_user_attributesQ  s@    &KK))488??;HHNNHHOO
r+   c                 D    [         R                  " U R                  5        g r   )r"   av_frame_unrefrK   rI  s    r)   rC   VideoFrame.__dealloc__Y  s    488$r+   c                 `   U R                   R                  (       d  g[        R                  " [        R                  [
        R                     U R                   R                  R                  5      n[        UR                  U R                   R                  U R                   R                  5      $ )z
For a hardware frame (e.g. ``format.name == "cuda"``), the underlying
software pixel format (``nv12``, ``yuv444p``, ``p010le``, ...). ``None``
for a regular software frame.

:type: VideoFormat | None
N)rK   hw_frames_ctxr   r    r!   r"   rh   rO   r
   rj   ra   rb   )r:   rq   s     r)   rj   VideoFrame.sw_format\  sm     xx%%<BKKNN30014883I3I3N3N=

  
 4 4dhhnndhhooVVr+   c                     SU R                   R                   SU R                   SU R                  R                   SU R
                   SU R                   S[        U 5      S S3$ )Nz<av.z, pts= xz at 0x>)	__class__rt   ptsri   namera   rb   idrI  s    r)   __repr__VideoFrame.__repr__l  s\    4>>**+6$((1T[[=M=M<Nazzl!DKK=r$xl!=	
r+   c           	      r   U R                   nU R                  R                  (       a  [        R                  " [        R
                  [        R                     U R                  R                  R                  5      n[        UR                  U R                  R                  U R                  R                  5      nSn[        UR                  R                  5       H2  nUR                  R                  U   R                   S-   nX5:  d  M0  UnM4     UR"                  S:X  a  SnSnXc:  aH  U R                  R$                  U   (       a*  US-  nXc:  a   U R                  R$                  U   (       a  M*  US:X  a  ['        U S5      4$ [)        [        U5       Vs/ s H  n['        X5      PM     sn5      $ s  snf )z*
A tuple of :class:`.VideoPlane` objects.
r   r   r   r   )ri   rK   rO  r   r    r!   r"   rh   rO   r
   rj   ra   rb   r  nb_componentscompr  rW  extended_datar   tuple)r:   fmtrq   max_plane_countr!  countplane_counts          r)   planesVideoFrame.planesr  sS    kk88!!@Fs445txx7M7M7R7RAJ #$$dhhnndhhooC '(sww,,-AGGLLO))A-E&"' . 88vO"#+0F0F{0S1K +0F0F{0S0S!tQ'))53EF3Eaj)3EFGGFs   F4c                 .    U R                   R                  $ )zWidth of the image, in pixels.)rK   ra   rI  s    r)   ra   VideoFrame.width  s     xx~~r+   c                 .    U R                   R                  $ )zHeight of the image, in pixels.)rK   rb   rI  s    r)   rb   VideoFrame.height  s     xxr+   c                     [        U 5      $ )zThe rotation component of the `DISPLAYMATRIX` transformation matrix.

Returns:
    int: The angle (in degrees) by which the transformation rotates the frame
        counterclockwise. The angle will be in range [-180, 180].
r   rI  s    r)   rotationVideoFrame.rotation  s     $D))r+   c                 b    [        U R                  R                  [        R                  -  5      $ )z+Is this frame an interlaced or progressive?)boolrK   flagsr"   AV_FRAME_FLAG_INTERLACEDrI  s    r)   interlaced_frameVideoFrame.interlaced_frame  s"     DHHNNS%A%AABBr+   c                 .    U R                   R                  $ )zlReturns an integer that corresponds to the PictureType enum.

Wraps :ffmpeg:`AVFrame.pict_type`

:type: int
rK   	pict_typerI  s    r)   ru  VideoFrame.pict_type  s     xx!!!r+   c                 $    XR                   l        g r   rt  r:   values     r)   ru  rv        "r+   c                 .    U R                   R                  $ )z;Colorspace of frame.

Wraps :ffmpeg:`AVFrame.colorspace`.

rK   
colorspacerI  s    r)   r}  VideoFrame.colorspace  s     xx"""r+   c                 $    XR                   l        g r   r|  rx  s     r)   r}  r~    s    #r+   c                 .    U R                   R                  $ )z=Color range of frame.

Wraps :ffmpeg:`AVFrame.color_range`.

rK   color_rangerI  s    r)   r  VideoFrame.color_range  s     xx###r+   c                 $    XR                   l        g r   r  rx  s     r)   r  r    s    $r+   c                 .    U R                   R                  $ )zGTransfer characteristic of frame.

Wraps :ffmpeg:`AVFrame.color_trc`.

rK   	color_trcrI  s    r)   r  VideoFrame.color_trc  s     xx!!!r+   c                 $    XR                   l        g r   r  rx  s     r)   r  r    rz  r+   c                 .    U R                   R                  $ )zEColor primaries of frame.

Wraps :ffmpeg:`AVFrame.color_primaries`.

rK   color_primariesrI  s    r)   r  VideoFrame.color_primaries  s     xx'''r+   c                 $    XR                   l        g r   r  rx  s     r)   r  r    s    #( r+   c                     U R                   (       d  [        5       U l         U R                   R                  " U /UQ70 UD6$ )a  reformat(width=None, height=None, format=None, src_colorspace=None, dst_colorspace=None, interpolation=None, threads=None)

Create a new :class:`VideoFrame` with the given width/height/format/colorspace.

.. seealso:: :meth:`.VideoReformatter.reformat` for arguments.

)reformatterVideoReformatterreformat)r:   argskwargss      r)   r  VideoFrame.reformat  s9     /1D((????r+   c                 *    U R                   " SSS0UD6$ )zGet an RGB version of this frame.

Any ``**kwargs`` are passed to :meth:`.VideoReformatter.reformat`.

>>> frame = VideoFrame(1920, 1080)
>>> frame.format.name
'yuv420p'
>>> frame.to_rgb().format.name
'rgb24'

ri   r   r~   )r  )r:   r  s     r)   to_rgbVideoFrame.to_rgb  s     }}6G6v66r+   filepathc                    [        U5      nUR                  S5      (       a  SnO:UR                  S5      (       d  UR                  S5      (       a  SnO[        S5      eU(       a  SOSnU(       a  S	OS
nSSKJn  U" USSS0S9 nUR                  UUUR                  5        V	V
s0 s H  u  pU	[        U
5      _M     sn
n	S9nU R                  Ul        U R                  Ul        UR                  UR                  U R                  US95      5        UR                  UR                  S5      5        SSS5        gs  sn
n	f ! , (       d  f       g= f)a?  Save a VideoFrame as a JPG or PNG.

:param filepath: str | Path
:param \**options: Encoder options, e.g. ``pred="none"`` or
    ``compression_level=1`` for PNG, ``qscale=2`` for JPG. Values are
    coerced to ``str``. The PNG defaults favor file size over speed;
    ``pred="none"`` is roughly 3x faster and 2x larger.
z.pngFz.jpgz.jpegTz"filepath must end with png or jpg.mjpegpngr   r   r   )openwupdaterF   )r[   )pix_fmtr[   )ri   N)strendswithr6   av.container.corer  
add_streamitemsra   rb   muxrH   r  )r:   r  r[   rW  is_jpgencoderr  r  outputkrA   output_streams               r)   saveVideoFrame.save  s    M==  F]]6""dmmG&<&<FABB"(we%+z*(C(C9V"--/6}}?tqCF? . M
 #'**M#';;M JJ}++DMMM,IJKJJ}++D12 :9 @	 :9s   !E&D<>A5E<E
Ec           	         SSK Jn  U R                  " S	SS0UD6R                  S   nUnUR                  n[        U5      nSnUR                  S-  nUR                  U-  n	[        U	5      n
US:  a  UR                  S-
  U-  OSnXy:  a$  XKX-    XXx-   & US:  a  X-  nOX-  nXx-  nXy:  a  M$  UR                  SUR                  UR                  4[        U
5      SSSS5      $ )
zGet an RGB ``PIL.Image`` of this frame.

Any ``**kwargs`` are passed to :meth:`.VideoReformatter.reformat`.

.. note:: PIL or Pillow must be installed.

r   )Imageri   r   r   r   RGBrawr~   )PILr  r  rd  r  r  ra   rb   	bytearray	frombytesbytes)r:   r  r  r  r  r  r  r  r  o_sizer  r  s               r)   to_imageVideoFrame.to_image,  s     	 MMCCFCJJ1M*/ %	"%i. "'++/ %x 7$V, AJAq 0H<STn.3E<L.ME%*+1}!!E n EKK.eeUAq
 	
r+   Tc           	      :   U R                   R                  (       a  SU;  a  [        R                  " [        R                  [
        R                     U R                   R                  R                  5      n[        U5      n[        UR                  U R                   R                  U R                   R                  5      R                  US'   [        U5      S:  a  U R                  " S0 UD6OU nUR                   R                  (       a  [!        S5      eSSKnUR$                  R                  nUR&                  nU[(        ;   GaQ  US:X  aH  UR                   R                  S-  S:X  d   S5       eUR                   R                  S-  S:X  d   S5       e[(        U   u  p[        U5      n
U
S	:X  a  [+        US   X5      nOUR-                  UR                   R                  UR                   R                  U
4U	S
9nUR/                  S5      (       a  SSS	/[1        SU
5      Q7nO[1        U
5      n[3        U5       H  u  p[+        X~   X5      USS2SS2U4'   M     [5        XR7                  S5      5      nU(       d  US;   a  UR9                  USS5      nU$ US;   GaI  UR                   R                  S-  S:X  d   S5       eUR                   R                  S-  S:X  d   S5       eUS:H  nU(       a  SOS	nU(       a  SOSn	UR                   R                  S:X  d  UR                   R                  S:X  a&  UR-                  SUR                   R                  4U	S
9$ UR;                  [+        US   X5      R=                  S5      [+        US	   X5      R=                  S5      [+        US   X5      R=                  S5      /5      R=                  SUR                   R                  5      $ US:X  a  UR                   R                  S-  S:X  d   S5       eUR                   R                  S-  S:X  d   S5       e[+        US   SS5      n[+        US	   SS5      n[+        US   SS5      nUR?                  USS	S9nUR?                  USS	S9nU(       a  URA                  UUU/SS9$ URA                  UUU/SS9$ US:X  a_  [+        US   5      nURC                  US	   S5      RE                  S5      R=                  SS	5      RG                  URH                  5      nUU4$ US:X  an  UR;                  [+        US   5      R=                  S5      [+        US	   S5      R=                  S5      /5      R=                  SUR                   R                  5      $ [!        SU S35      e)a  Get a numpy array of this frame.

Any ``**kwargs`` are passed to :meth:`.VideoReformatter.reformat`.

The array returned is generally of dimension (height, width, channels).

:param bool channel_last: If True, the shape of array will be
    (height, width, channels) rather than (channels, height, width) for
    the "yuv444p" and "yuvj444p" formats.

.. note:: Numpy must be installed.

.. note:: For formats which return an array of ``uint16``, ``float16`` or ``float32``,
    the samples will be in the system's native byte order.

.. note:: For ``pal8``, an ``(image, palette)`` tuple will be returned,
    with the palette being in ARGB (PyAV will swap bytes if needed).

.. note:: For ``gbrp`` formats, channels are flipped to RGB order.

ri   r   z2Cannot convert a hardware frame to numpy directly.Nr   r   z width has to be even for yuyv422z!height has to be even for yuyv422r   )r(  gbrr   be   r   r   >   r   yuv422pr   r   z$width has to be even for this formatz%height has to be even for this formatr   r   r   r  r   axisr   i4>i4r   z'Conversion to numpy array with format `` is not yet supportedr~   )%rK   rO  r   r    r!   r"   rh   rO   dictr
   rj   ra   rb   rW  lenr  r6   r-  ri   rd  _np_pix_fmt_dtypesr8  empty
startswithr  	enumerater  r  moveaxishstackreshaperepeatstack
frombufferastypeviewr   )r:   channel_lastr  rq   r.  r3  format_namerd  r1  r(  
num_planesr
  plane_indicesr!  p_idxis_10bityurA   u_fullv_fullimagepalettes                          r)   
to_ndarrayVideoFrame.to_ndarrayQ  s   . 88!!hf&<@Fs445txx7M7M7R7RAJ &\F/$$dhhnndhhoo d 8 8;6{QDMM3F3D99""QRR ll'').,,i'yy*a/S1SS/yy''!+q0U2UU00=OH(+FJQ$VAY@YY%%uyy
C5 !  ))%00%&1$DuQ
/C$DM$)*$5M )- 8HA%1&-%QE!Q'N !9"5*>*>t*DEEK3J$JE1a0L KK99??Q&!+S-SS+99##a'1,U.UU,$/=$@H$q!H (HgEyy!#uyy'7'71'<xxEIIOO 4ExBB99 H<DDRH H<DDRH H<DDRH gb%))//*+ -'99??Q&!+S-SS+99##a'1,U.UU,VAY84AVAY84AVAY84A YYq!!Y,FYYq!!Y,FxxFF 3!x<<88Q/a888&  +EfQi.Qbhh	  '>!& 99 +33B7 A.66r:
 gb%))//*+ 5k]BXY
 	
r+   c                 x    UR                   S:w  a  UR                  S5      n[        XR                  S   S5        g)z&
Update content from a ``PIL.Image``.
r  r   r   N)modeconvertr'  rd  )r:   imgs     r)   	set_imageVideoFrame.set_image  s0     88u++e$CCQ3r+   c                 v    [        U R                  S   U R                  S   S5      nUR                  U 5        U$ )z)
Construct a frame from a ``PIL.Image``.
r   r   r   )r   sizer  )r  r.  s     r)   
from_imageVideoFrame.from_image  s3    
 'sxx{CHHQKIr+   c                    SSK nU R                  S   nU(       d  U R                  S   nUS;   aX  [        U SS5        [        X R                  S   S:H  5        U R                  SS S:w  a  [        S	5      eU R                  S   4nGOUS
;   aX  [        U SS5        [        X R                  S   S:H  5        U R                  SS S:w  a  [        S	5      eU R                  S   4nGOUS;   aX  [        U SS5        [        X R                  S   S:H  5        U R                  SS S:w  a  [        S	5      eU R                  S   4nGOaUS;   aX  [        U SS5        [        X R                  S   S:H  5        U R                  SS S:w  a  [        S	5      eU R                  S   4nGOUS;   aX  [        U SS5        [        X R                  S   S:H  5        U R                  SS S:w  a  [        S	5      eU R                  S   4nGOUS;   aX  [        U SS5        [        X R                  S   S:H  5        U R                  SS S:w  a  [        S	5      eU R                  S   4nGOGUS;   aX  [        U SS5        [        X R                  S   S:H  5        U R                  SS S:w  a  [        S	5      eU R                  S   4nGOUS;   a=  [        U SS5        U R                  S   S:w  a  [        S	5      eU R                  S   4nGOUS;   a=  [        U SS5        U R                  S   S:w  a  [        S	5      eU R                  S   4nGOcUS;   a=  [        U SS5        U R                  S   S:w  a  [        S	5      eU R                  S   4nGO US;   a}  [        U SS5        [        X R                  S   S:H  5        U R                  SS S:w  a  [        S	5      eU R                  S   S-  U R                  S   S-  U R                  S   S-  4nGOUS;   a}  [        U SS5        [        X R                  S   S:H  5        U R                  SS S:w  a  [        S	5      eU R                  S   S-  U R                  S   S-  U R                  S   S-  4nGOUS;   a}  [        U SS5        [        X R                  S   S:H  5        U R                  SS S:w  a  [        S	5      eU R                  S   S-  U R                  S   S-  U R                  S   S-  4nGOUS;   a  [        U SS5        [        X R                  S   S:H  5        U R                  SS S:w  a  [        S	5      eU R                  S   S-  U R                  S   S-  U R                  S   S-  U R                  S   S-  4nGOUS ;   a  [        U SS5        [        X R                  S   S:H  5        U R                  SS S:w  a  [        S	5      eU R                  S   S-  U R                  S   S-  U R                  S   S-  U R                  S   S-  4nGOoUS!;   a  [        U SS5        [        X R                  S   S:H  5        U R                  SS S:w  a  [        S	5      eU R                  S   S-  U R                  S   S-  U R                  S   S-  U R                  S   S-  4nOUS";   a  [        U SS5        [        X R                  S   S-  S:H  5        [        X R                  S   S-  S:H  5        US#-  S-  nU R                  S   S:w  a  [        S	5      eUS$;   a3  U R                  S   U R                  S   S-  U R                  S   S-  4nO.U R                  S   U R                  S   4nO[        S%U S&35      eUR                  S'5      (       a*  UR                  UR                  U S(/ S)Q4   S*S5      5      n O?UR                  S+5      (       a)  UR                  UR                  U S(/ S,Q4   S*S5      5      n [        [        5      nUR                  XXEU5        U$ )-a  
Construct a frame from a numpy buffer.

:param int width: optional width of actual image, if different from the array width.

.. note:: For formats which expect an array of ``uint16``, ``float16`` or ``float32``,
    the samples must be in the system's native byte order.

.. note:: for ``gbrp`` formats, channels are assumed to be given in RGB order.

.. note:: For formats where width of the array is not the same as the width of the image,
for example with yuv420p images the UV rows at the bottom have padding bytes in the middle of the
row as well as at the end. To cope with these, callers need to be able to pass the actual width.
r   Nr      r   r   r   r   r   )r   r   z.provided array does not have C_CONTIGUOUS rows   r   r   r   r   r   )r   r      r   r   r   )r   r   >   r   r   r   r   r   )r   r      r   r   r   r   )r   r      r   r   r      r   r   )r   r   >   r   r   r   r   r   r   r   r   >   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   >   r   r   >   r   >
   r   r   r   r   r   r   r   r   r   r   >   r   r   >   r   >   r   r   r   r   r   r   r   r   >   r   r   >   r   r   r   r      r   r   )Conversion from numpy array with format `r  r   .)r   r   r   r   r  r   )r   r   r   )r-  r7  r   r<  r,  r6   r  ascontiguousarrayr  r   r   _image_fill_pointers_numpy)r
  ri   ra   r3  rb   	linesizesr.  s          r)   from_numpy_bufferVideoFrame.from_numpy_buffer  sJ     	QKKNE''%!,{{1~':;}}QR F* !QRRq)+ICC%1-{{1~':;}}QR F* !QRRq)+I//%A.{{1~':;}}QR G+ !QRRq)+I77%!,{{1~':;}}QR F* !QRRq)+IGG%1-{{1~':;}}QR F* !QRRq)+I11%A.{{1~':;}}QR F* !QRRq)+I11%A.{{1~':;}}QR G+ !QRRq)+I 	
 	
 %!,}}Q1$ !QRRq)+I 
 
( %1-}}Q1$ !QRRq)+I11%A.}}Q1$ !QRRq)+Ix%!,{{1~':;}}QR F* !QRRa A%a A%a A%I
  
 
 %1-{{1~':;}}QR F* !QRRa A%a A%a A%I
 11%A.{{1~':;}}QR G+ !QRRa A%a A%a A%I
 y %!,{{1~':;}}QR F* !QRRa A%a A%a A%a A%	I  	
 	
 %1-{{1~':;}}QR F* !QRRa A%a A%a A%a A%	I 33%A.{{1~':;}}QR G+ !QRRa A%a A%a A%a A%	I 66%!,{{1~'9Q'>?{{1~'9Q'>?q[1_F}}Q1$ !QRR00 MM!$MM!$)MM!$)	 #]]1-u}}Q/?@	;F8CYZ  W%%((U3;L5MrST)UVEv&&((U3	>5JBPQ)RSE12((v&Qr+   c           	      `   UR                   R                  n[        R                  " [        R                  [
           U5      n[        U5      n[        R                  " U R                  5        XR                  l
        X R                  l        X0R                  l        [        U5       H  u  pXR                  R                  U	'   M     [        [        R                   " U R                  R                  [        R                  " [        R"                  U R                  R                  5      U R                  R                  UU R                  R                  5      5      n[        R                  " [$        U5      n['        U5        [        R(                  " UU[*        [        R                  " [        R,                  U5      S5      U R                  R.                  S'   U R                  R.                  S   [        R0                  :X  a  [3        U5        [5        S5      eU R7                  5         g )Nr   zav_buffer_create failed)ctypesrO   r   r    r!   r   r	   r"   rL  rK   ri   ra   rb   r  r/  r   av_image_fill_pointersr|   rz   r   av_buffer_creater   rT   bufr'   r   rM   rE  )r:   r*  ra   rb   r  ri   c_datac_ptrr@  r!  r/  requiredpy_bufs                r)   r  %VideoFrame._image_fill_pointers_numpy  sr   " %+MM$6$6)/V^^G5Lf)U&1&&9488$ # $Y/KA#+HHa  0 &&C--txx?!!
 VV,&.. KKv.
Q 88<<?fkk)f788""$r+   c           	         SSK n0 SS_SS_SS_SS_S	S_S
S_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_0 SS_SS_SS_SS_SS_SS_SS_S S!_S"S#_S$S#_S%S#_S&S#_S'S#_S(S#_S)S#_S*S#_S+S#_E0 S,S#_S-S._S/S._S0S_S1S_S2S_S3S_S4S_S5S_S6S_S7S_S8S_S9S_S:S_S;S_S<S=_S>S=_ESS!S#S#SSS!S?.ER                  US@5      u  pEnUGb=  U R                  SA:X  a  U SS2SS2S4   n [        XSB5        U(       d  USC;   a  UR	                  U SSA5      n [        X R                  SA   U:H  5        [        XR                  SD5      5      n [        U R                  SE   U R                  S   U5      nUR                  R                  R                  SF5      (       a7  UR                  U SS2SS2SESB24   U SS2SS2SSE24   U SS2SS2SBS24   /SASG9n [        U5       H'  n[        U SS2SS2U4   UR                   U   U5        M)     U$ USH:X  a  U u  p	[        U SISA5        [        U	SISA5        [        XR                  SJ:H  5        [        U R                  SE   U R                  S   U5      n[        XR                   S   SE5        UR                   SE   R#                  U	R%                  SK5      R'                  SL5      R)                  5       5        U$ USM;   Ga!  [        U SISA5        [        X R                  S   SB-  S:H  5        [        X R                  SE   SA-  S:H  5        [        U R                  SE   U R                  S   SA-  SB-  U5      nUR*                  S:X  d  UR,                  S:X  a  U$ UR*                  UR,                  -  n
SNU
-  SO-  nU R/                  SP5      n[        USU
 UR                   S   SE5        [        XU UR                   SE   SE5        [        XS UR                   SA   SE5        U$ USQ:X  Ga!  [        U SRSA5        [        X R                  S   SB-  S:H  5        [        X R                  SE   SA-  S:H  5        [        U R                  SE   U R                  S   SA-  SB-  U5      nUR*                  S:X  d  UR,                  S:X  a  U$ UR*                  UR,                  -  n
SNU
-  SO-  nU R/                  SP5      n[        USU
 UR                   S   SA5        [        XU UR                   SE   SA5        [        XS UR                   SA   SA5        U$ USS:X  Ga  [        U SISA5        [        X R                  S   SO-  S:H  5        [        X R                  SE   SA-  S:H  5        [        U R                  SE   U R                  S   SA-  U5      nUR*                  S:X  d  UR,                  S:X  a  U$ UR*                  UR,                  -  n
XSA-  -   nU R/                  SP5      n[        USU
 UR                   S   SE5        [        XU UR                   SE   SE5        [        XS UR                   SA   SE5        U$ UST:X  GaM  [1        XR2                  5      (       a  U R4                  UR6                  :w  a  [9        SU5      eU(       a'  U R                  SA   SB:X  a  UR	                  U SAS5      n OU R                  S   SB:X  d  [9        SV5      eU R                  SES u  pUSA-  S:w  d	  USA-  S:w  a  [9        SW5      e[        XU5      n[        U S   UR                   S   SA5        U SESS2SSSA24   R;                  5       nU SASS2SSSA24   R;                  5       n[        XR                   SE   SA5        [        UUR                   SA   SA5        U$ USX:X  af  [        U SISB5        [        X R                  S   SA-  S:H  5        [        X R                  SE   SA-  S:H  5        [        X R                  SA   SA:H  5        GOUSY;   a*  [        U SISB5        [        X R                  SA   SB:H  5        GOUSZ;   a*  [        U SISB5        [        X R                  SA   SO:H  5        GOUS[;   a  [        U SRSB5        [        X R                  SA   SB:H  5        [        U R                  SE   U R                  S   U5      n[        [        XR                  SD5      5      UR                   S   S\5        U$ US];   a  [        U S^SB5        [        X R                  SA   SB:H  5        [        U R                  SE   U R                  S   U5      n[        [        XR                  SD5      5      UR                   S   S_5        U$ US`;   a  [        U SRSB5        [        X R                  SA   SO:H  5        [        U R                  SE   U R                  S   U5      n[        [        XR                  SD5      5      UR                   S   Sa5        U$ USb;   a  [        U ScSB5        [        X R                  SA   SO:H  5        [        U R                  SE   U R                  S   U5      n[        [        XR                  SD5      5      UR                   S   Sa5        U$ USd;   a  [        U S^SB5        [        X R                  SA   SO:H  5        [        U R                  SE   U R                  S   U5      n[        [        XR                  SD5      5      UR                   S   Se5        U$ USf:X  a  [        U SISA5        [        X R                  S   SB-  S:H  5        [        X R                  SE   SA-  S:H  5        [        U R                  SE   U R                  S   SA-  SB-  U5      nUR*                  UR,                  -  nU R/                  SP5      n[        USU UR                   S   SE5        [        UUS UR                   SE   SA5        U$ [9        SgU Sh35      e[        U R                  SE   U R                  S   U5      n[        XR                   S   U R                  SA:X  a  SE5        U$ U R                  SA   5        U$ )ia]  
Construct a frame from a numpy array.

:param bool channel_last: If False (default), the shape for the yuv444p and yuvj444p
    is given by (channels, height, width) rather than (height, width, channels).

.. note:: For formats which expect an array of ``uint16``, ``float16`` or ``float32``,
    the samples must be in the system's native byte order.

.. note:: for ``pal8``, an ``(image, palette)`` pair must be passed. `palette` must
    have shape (256, 4) and is given in ARGB format (PyAV will swap bytes if needed).

.. note:: for ``gbrp`` formats, channels are assumed to be given in RGB order.

r   Nr   )r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   )r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   )r   r   r   r   r   r   r   )NNNr   r   r  r  r   r  r  r   r   )   r   r  r  r     r   r  r   r   r  r   zArray must be uint16 typez>Array must have shape (3, height, width) or (height, width, 3)zWidth and height must be evenr   r  >   r   r   r   r   r  r   r  r   r   r  r   r  r   r  r   r   r  r  )r-  re   ndimr   r  r<  r7  r  r  r   ri   rW  r  concatenater  r'  rd  r  r  r  r%  ra   rb   r  r7   r2  r(  r   r6   copy)r
  ri   r  r3  r6  r1  r(  r.  r!  r  u_startv_startflatrb   ra   r  rA   uv_starts                     r)   from_ndarrayVideoFrame.from_ndarray  sL   $ 	;%
.;%
.;%
 ?;%
 .	;%

 .;%
 ?;%
 .;%
 .;%
 ?;%
 .;%
 .;%
 ?;%
 O;%
 _;%
 );%
  )!;%
" )#;%
$ )%;%
& )';%
( ));%
* )+;%
, )-;%
. +/;%
0 +1;%
2 O3;%
4 (5;%
6 (7;%
8 (9;%
: (;;%
< (=;%
> (?;%
@ (A;%
B (C;%
D 'E;%
F 'G;%
H *I;%
J *K;%
L OM;%
N (O;%
P (Q;%
R (S;%
T (U;%
V (W;%
X (Y;%
Z ([;%
\ (];%
^ __;%
` 'a;%
b 'c;%
d *e;%
f *g;%
h $&++,,'u;%
v #f(
)w 	"Ex zzQaDj)%*F.E$EE1a0{{1~'AB"5//$*?@Eu{{1~u{{1~vFE||  ++E22aAaCi(aAaCi(aABh
  '  8_#E!Q'NELLOXN %L V"NE%!,'7A.()BCu{{1~u{{1~vFE||A:LLO""7<<#6#=#=d#C#K#K#MNL..%!,{{1~'9Q'>?{{1~'9Q'>?u{{1~A0Bq/H&QE{{a5<<1#4kkELL0G'kQ&G==$DQwa!DW 5u||AJXQCL}$%1-{{1~'9Q'>?{{1~'9Q'>?u{{1~A0Bq/H&QE{{a5<<1#4kkELL0G'kQ&G==$DQwa!DW 5u||AJXQCLy %!,{{1~'9Q'>?{{1~'9Q'>?u{{1~u{{1~/BFKE{{a5<<1#4kkELL0G1,G==$DQwa!DW 5u||AJXQCL}$eZZ00EKK2994L !<== A! 3E1a0kk!n) T  "KKOMFqyA~!q !@AAuf5Ea%,,q/1=aCaCi %%'AaCaCi %%'A<<?A65<<?A6Ly %!,{{1~'9Q'>?{{1~'9Q'>?{{1~':;))%!,{{1~':;77%!,{{1~':;CC%1-{{1~':;u{{1~u{{1~vFEuood&;<ell1oq L//%A.{{1~':;u{{1~u{{1~vFEuood&;<ell1or LGG%1-{{1~':;u{{1~u{{1~vFEuood&;<ell1oq L11%A.{{1~':;u{{1~u{{1~vFEuood&;<ell1oq L11%A.{{1~':;u{{1~u{{1~vFEuood&;<ell1or Lv%!,{{1~'9Q'>?{{1~'9Q'>?u{{1~A0Bq/H&QE{{U\\1H==$DYha!DXYa!DL;F8CYZ  5;;q>5;;q>6B<<?qA	
  ?Dkk!n	
 r+   r  c                     [        XU5      nUS:X  a  [        XR                  S   SXE5        U$ US;   a6  [        U UR                  S   UR                  S5      (       a  SOSUU5        U$ [	        SU S	35      e)
Nr   r   r   >   r   r   r   r   r   r   r   r   r   r   r   r   8r   r   zFormat 'z' is not supported.)r   r#  rd  r  NotImplementedError)r  ra   rb   ri   r  r  r.  s          r)   
from_bytesVideoFrame.from_bytes  s     5&1V<<?A4 /  
 
  Q__S))q  &8K&LMMr+   Nr/   r0   r1   c	           	         [        U [        [        45      (       d  U 4n [        U5      n	[        S5      n
[        S5      n[        S5      nXX4;  a  [        R                  X	XX4U5      $ [        U 5      S:w  a  [        S5      eX:X  a  SOSnUS:X  a  SOSn[        R                  n[        R                  nS n [        U S	   U5      n[        U S   U5      nUR                  R                  nUR                  R                  nUU:w  a  [        S
5      eU[        [        4;  a  [        S5      eUR                  R                   nUR                  R                   nUU:w  a  [        S5      eU[        :X  a  Uc  UnO%UU:w  a  [        S5      eOUS;  a  [        S5      eS	nU[        :X  a  US	:w  d  US	:w  a  [        S5      eUR                  R"                  R$                  S:w  dH  UR                  R"                  R&                  U:w  d$  UR                  R"                  R(                  S:w  a  [+        S5      eUR                  R"                  R$                  S:w  dH  UR                  R"                  R&                  U:w  d$  UR                  R"                  R(                  S:w  a  [+        S5      eUR                  R,                  S:w  a  [        S5      e[        R.                  " [0        UR                  R2                  S	   5      n[        R.                  " [0        UR                  R2                  S   5      nUS	:X  a=  US	:X  a7  [        R.                  " [4        U5      n[        R.                  " [4        U5      nO.US	:X  d  US	:X  a  [        S5      eUU:w  d  UU:w  a  [        S5      eUS-  (       d
  US-  (       a  [        S5      eUR                  R6                  [        R                  :w  a[  UR                  R6                  S   S:w  a  [        S5      e[        R.                  " [0        UR                  R6                  S	   5      nO[        R.                  " [0        U5      n[        R.                  " [4        UU-  5      n[        R.                  " [4        UU-  5      nUR                  R,                  nUS-  nUS:X  Ga  [        R.                  " [4        UR                  R2                  S	   5      n[        R.                  " [4        UR                  R2                  S   5      nUU:w  d  UU:w  a  [        S5      eUR                  R6                  [        R                  :w  a\  UR                  R6                  S   S:w  a  [        S5      e[        R.                  " [0        UR                  R6                  S	   5      nGO[        R.                  " [0        U5      nGOUS:X  Gar  [        R.                  " [4        UR                  R2                  S	   5      n[        R.                  " [4        UR                  R2                  S   5      n [        R.                  " [4        UR                  R2                  S   5      n!UU:w  d  U US-  :w  d  U!S:w  a  [        S5      eUR                  R6                  [        R                  :w  ax  UR                  R6                  S   S:w  d  UR                  R6                  S   S:w  a  [        S5      e[        R.                  " [0        UR                  R6                  S	   5      nO'[        R.                  " [0        U5      nO[        S5      e[        R.                  " [4        UU-  5      n"[        R.                  " [4        U"US-  -  5      n#[9        5       nUUR:                  l        UUR:                  l        U[        :X  Ga6  Uc  [A        UUc  U(       + OU[C        U5      S9n$O[        U[@        5      (       d  [+        S5      e[5        UR                   5      [5        U5      :w  a  [        S 5      eUb-  [C        URD                  5      [C        U5      :w  a  [        S!5      eUb-  [C        URF                  5      [C        U5      :w  a  [        S"5      e[        R.                  " [@        U5      n$U$RI                  XU5      n%[        S#5      UR:                  l%        U%UR:                  l&        UUl'        U$Ul(        OU	UR:                  l%        [        R.                  " [        RR                  [T           UR                  RV                  5      [        R.                  " [        RX                  UR                  RZ                  5      -   n&[        R.                  " [        RR                  [T           UR                  RV                  5      [        R.                  " [        RX                  UR                  RZ                  5      -   n'[\        R^                  " U&U[`        [        R.                  " [        Rb                  U5      S	5      UR:                  Rd                  S	'   UR:                  Rd                  S	   [        R                  :X  a  [g        S$5      eU&UR:                  RV                  S	'   UUR:                  Rh                  S	'   [        R                  n[\        R^                  " U'U#[`        [        R.                  " [        Rb                  U5      S	5      UR:                  Rd                  S'   UR:                  Rd                  S   [        R                  :X  a  [g        S%5      eU'UR:                  RV                  S'   U"UR:                  Rh                  S'   [        R                  nURk                  5         U$ ! [l         ao    Ub   [\        Rn                  " UR:                  5        U[        R                  :w  a  URq                  U5        U[        R                  :w  a  URq                  U5        e f = f)&Ns   nv12s   p010les   p016ler   zHfrom_dlpack currently supports 2-plane formats only (nv12/p010le/p016le)r   r   r   r   z,plane tensors must have the same device_typez.only CPU and CUDA DLPack tensors are supportedz-plane tensors must be on the same CUDA devicez4device_id does not match the DLPack tensor device_idNr   #device_id must be 0 for CPU tensors+CPU DLPack tensors must have device_id == 0zunexpected dtype for plane 0zunexpected dtype for plane 1zplane 0 must be 2D (H, W)+either specify both width/height or neither)plane 0 shape does not match width/heightz0width/height must be even for nv12/p010le/p016lez0plane 0 must be contiguous in the last dimensionz*plane 1 must have shape (H/2, W) for 2D UVz0plane 1 must be contiguous in the last dimensionr   z/plane 1 must have shape (H/2, W/2, 2) for 3D UVz-unexpected UV plane strides for (H/2, W/2, 2)zplane 1 must be 2D or 3D)r/   r0   r1   z"cuda_context must be a CudaContextzAcuda_context.device_id does not match the DLPack tensor device_idz3cuda_context.primary_ctx does not match primary_ctxz3cuda_context.current_ctx does not match current_ctxrd   z#av_buffer_create failed for plane 0z#av_buffer_create failed for plane 1)9r7   r_  listr	   r   _from_dlpack_planarr  r6   r   r'   r   	dl_tensordevice_typer   r   r  r/   r(  codebitslanesr9   r  r    r   r7  r8   r,  r   rK   ra   rb   r-   rn  r0   r1   rr   ri   rO  
_device_id	_cuda_ctxr!   r   rO   r?   byte_offsetr"   r  r   rT   r  rM   r/  rE  rV   rL  r   )(rd  ri   ra   rb   rR   r/   r0   cuda_contextr1   r`   r   p010lep016leexpected_bitsr1  m0m1r.  	dev_type0	dev_type1dev0dev1y_hy_wy_pitch_elems
y_linesizey_sizeuv_ndimuv_h_expecteduv_huv_wuv_pitch_elemsuv_w2uv_cuv_linesizeuv_sizectxrp   y_ptruv_ptrs(                                           r)   from_dlpackVideoFrame.from_dlpack  s	    &5$-00YF$/$77#Y'Y'//11vy  v;!Z  $^%*1.4kk.4kk ~	 F3B F3B00I00II% !OPP-)D  <<))D<<))Dt| !PQQE!$ $I$&$N  '
 I-$%JKK	D dai419 !NOO ""''1,<<%%**m;<<%%++q0 >??""''1,<<%%**m;<<%%++q0 >??||  A% !<==++gr||'9'9!'<=C++gr||'9'9!'<=CzfkC-S#.!v{ !NOO%<3&=$%PQQqyFQJ !STT||##v{{2<<''*a/$%WXX &GR\\5I5I!5L M &GU ;S-(*BCJ[[j6&9:Fll''G"aKM!|{{3(:(:1(=>{{3(:(:1(=>=(DEM$%QRR<<''6;;6||++A.!3(N  &,[[",,:N:Nq:Q%RN%+[[$%?NA{{3(:(:1(=>C););A)>?{{3(:(:1(=>=(Eeqj,ATQY$%VWW<<''6;;6||++A.!3r||7K7KA7NRS7S(K  &,[[",,:N:Nq:Q%RN%+[[%%@N !;<< ++c>H+DEKkk#{fk'BCG%'E#EIIO%EIIE!  '%"+/:/BO$($5C &lK@@'(LMM<112c)nD(_  #.4$004k*4+ )Q  #.4$004k*4+ )Q  !++k<@C //vF
#.w#7		 *4		'#, "%#)		 KKw'):):FMM2<<+C+CDEE [[w'):):FMM2<<+C+CDEF  #33v4fkk&--QS6TVW EIIMM! yy}}Q6;;.!"GHH %EIINN1$.EIIq!B"33%FMM2. EIIMM! yy}}Q6;;.!"GHH &EIINN1$/EIIq!B'')L 	 ""599-V[[ 

2V[[ 

2	s   :lo	 	A9qc           
         [         R                  " U5      nU[        R                  :X  a  [	        SU< 35      eUR
                  [         R                  [         R                  -  [         R                  -  -  (       a  [	        SU< S35      eSn[        UR                  5       Hd  n	[        R                  " [        R                  UR                  U	   R                  5      S-   U:  d  MH  UR                  U	   R                  S-   nMf     S/U-  n
[        U5       H  nSn[        UR                  5       HN  n	[        R                  " [        R                  UR                  U	   R                  5      U:X  d  ME  XU'   US-  nMP     US:w  d  Mt  [	        SU< S35      e   [        U 5      U:w  a  [!        U< SU S	[        U 5       35      eUR                  S   R"                  nUS:w  a  US
:w  a  [	        S5      eUS
:X  a-  UR
                  [         R$                  -  (       a  [	        S5      eUS-  nUS;  a  [!        S5      eUR&                  nUR(                  nS n[        R                  n [+        5       nUUR,                  l        [        U5       GH  n[1        X   U5      nUR2                  R4                  [6        :w  a  [	        S5      eUR2                  R8                  S:w  a  [!        S5      eUR2                  R:                  R<                  S:w  dH  UR2                  R:                  R>                  U:w  d$  UR2                  R:                  R@                  S:w  a  [C        SU 35      eUR2                  RD                  S
:w  a  [!        SU S35      e[        R                  " [F        UR2                  RH                  S   5      n[        R                  " [F        UR2                  RH                  S   5      nUS:X  a  US:X  a=  US:X  a7  [        R                  " [        U5      n[        R                  " [        U5      nO.US:X  d  US:X  a  [!        S5      eUU:w  d  UU:w  a  [!        S5      eU(       a
  US
-  (       d  U(       a  US
-  (       a  [!        SU< 35      eUUR,                  l%        UUR,                  l&        X   nUS:H  =(       d    US
:H  =(       a    U=(       d    UnU(       a  U* U-	  * OUnU(       a  U* U-	  * OUnUU:w  d  UU:w  a  [!        SU SU SU S35      eUR2                  RN                  [        R                  :w  a_  UR2                  RN                  S   S:w  a  [!        SU S35      e[        R                  " [F        UR2                  RN                  S   5      nO[        R                  " [F        U5      n[        R                  " [        UU-  5      n[        R                  " [        UU-  5      n[        R                  " [        RP                  [R           UR2                  RT                  5      [        R                  " [        RV                  UR2                  RX                  5      -   n[         RZ                  " UU[\        [        R                  " [        R^                  U5      S5      UR,                  R`                  U'   UR,                  R`                  U   [        R                  :X  a  [c        SU 35      eUUR,                  RT                  U'   UUR,                  Rd                  U'   [        R                  nGM     URg                  5         U$ ! [h         aJ    Ub   [         Rj                  " UR,                  5        U[        R                  :w  a  URm                  U5        e f = f)Nzunknown pixel format zCfrom_dlpack does not support bitstream, palette, or Bayer formats ()r   r   zMfrom_dlpack supports nv12/p010le/p016le and planar single-component formats; z is not supportedz
 requires z plane(s), got r   z=only 8- and 16-bit components are supported for DLPack importz6big-endian formats are not supported for DLPack importr   r  r  z5only CPU DLPack tensors are supported for this formatr  zunexpected dtype for plane zplane z must be 2D (H, W)r  r  zwidth/height must be even for z must have shape (z, z) must be contiguous in the last dimensionz"av_buffer_create failed for plane )7r"   av_pix_fmt_desc_getr   r'   r  ro  AV_PIX_FMT_FLAG_BITSTREAMAV_PIX_FMT_FLAG_PALAV_PIX_FMT_FLAG_BAYERr  r\  r    r8   r]  r  r  r6   r  AV_PIX_FMT_FLAG_BElog2_chroma_wlog2_chroma_hr   rK   ri   r   r  r  r   r/   r(  r  r  r  r9   r  r   r7  ra   rb   r,  r!   r   rO   r?   r!  r  r   rT   r  rM   r/  rE  rV   rL  r   )rd  r`   ri   ra   rb   rR   r/   desc	nb_planesr!  comp_of_planeprb  r1  r%  log2_wlog2_hr.  mphpwcomp_idx	is_chromaexp_wexp_hpitch_elemsr/  r  rK   s                                r)   r  VideoFrame._from_dlpack_planar  s|    ##F+ 	 6;;%(=fZ&HII::))%%&''(

 &":Q(  !"	t))*A{{6::tyy|'9'9:Q>J IIaL..2	 + iy!AE4--.;;vzz499Q<+=+=>!C'(!$QJE / z)117
:KM  " v;)#*JykVN   $yy|00q=X]%O  q=TZZ#*@*@@%H  %-qLI%BCC!//!// -3[[Q	%'E%EII9%#FIv6;;**d2-O  ;;((A-$%RSS KK%%**a/{{((-->{{((..!3#&A!$EFF;;##q($vaS0B%CDD[[!++*;*;A*>?[[!++*;*;A*>?6zfk &C 4!'S"!5!v{()VWWuf()TUU519&VaZ(+I&)TUU&+EIIO'-EII$(+%];h!mS&BRF	3<UFv-.%4=VG./6;"+$vaS0B5'E7RS%TUU;;&&&++5{{**1-2($QC'PQ  #)++gq{{7J7J17M"NK"(++gu"=K!;;sK(,BC{{35(89kkNN7+Q[[-=-=KKq{{/F/FGH $'#7#74fkk&--QR6SUV$		a  99==#v{{2%(J1#&NOO$'		q!(0		""1%KKG &J '')L 	 ""599-FKK		!	s   4T ]5 5A_	)ri   r  )r   r   r   )F)r   r   )r   F)r   FF)r   r   r   NNNNN)0rt   ru   rv   rw   r;   r   r{   r"   r|   uintr?  rE  rC   propertyrj   rY  rd  ra   rb   rk  rq  ru  setterr}  r  r  r  r  r  rz   r  r  	cdivisionr  r  staticmethodr  r  r  r  r  r8   r  r  rn  r=  r  r}   r~   r+   r)   r   r   4  s5   , \\%C-- %fkk %6;; % %$ \\
 
% W W
 H H>     * * C C
 " " # # # # $ $ $ $ % % " " # # ( ( ) )
@7"3V "3H#
J dr
 r
h4   Z Zx5%n d}  }~ 
 $$$ $ $L   $#'#'bb b 	b :b D[b D[b bH R Rr+   r   )Lsysenumr   r   cython.cimports.libavcimportslibavr"   cython.cimports.av.dictionaryr   cython.cimports.av.errorr   $cython.cimports.av.sidedata.sidedatar   cython.cimports.av.utilsr   cython.cimports.av.video.formatr	   r
   cython.cimports.av.video.planer   r   r   r   cython.cimports.cpython.excr   !cython.cimports.cpython.pycapsuler   r   r   cython.cimports.cpython.refr   r   cython.cimports.hwcontext_cudar   r   cython.cimports.libc.stdintr   r   r   r{   rC  	exceptvalr!   rN   voidr*   finalcclassr-   rz   r   rT   r   r   r  r   supported_np_pix_fmtsr  r  r_  rW  r  r   r   r   ry   	byteorderr  inliner  r#  r'  r8  r<  Framer~   r+   r)   <module>rt     s   
   # # 4 . E 2 I S S 3 
 = H C C )s445)[[)   ) g g  gT   FNN?4S  8 !MM!
..
!! [[!   ! 3MM3
..
!3 [[3   3  9 V t ^^eFKK$%	%&PPP 	|P 	|	P
 	|P 	|P 	-P 	-P 	-P 	-P 	-P 	-P 	-P 	-P 	P  	=!P" 	=#P$ 	%P& 	'P( 	M)P* 	M+P, 	-P. 	]/P0 	]1P2 	]3P4 	]5P6 	]7P8 	]9P: 	];P< 	]=P> 	n?P@ 	nAPB 	CPD 	MEPF 	MGPH 	MIPJ 	MKPL 	MMPN 	MOPP 	MQPR 	MSPT 	=UPV 	=WPX 	^YPZ 	^[P\ 	]P^ 	M_P` 	MaPb 	McPd 	MePf 	MgPh 	MiPj 	MkPl 	MmPn 	oPp 	=qPr 	=sPt 	^uPv 	^wPx 	yPz 	={P| 	=}P~ 	P@ 	APB 	MCPD 	MEPF 	^GPH 	^IPJ %$##$$%% _PS l .: . . '   S]]e-CD fkk    %% [[% [[	%
 ;;% %P Hj H6;; H H
 FMMM(.M@CM  MD Jv J6;; J J
 W W  Wr+   