
    \j                       d dl Z d dlmZ 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mZ d dlmZmZmZmZ d d	lmZ d d
lmZmZmZ d dlmZm Z  d dl!m"Z"m#Z# ejH                  ejJ                   G d d                    Z&ejN                  de(de(dejR                  e   fd       Z*ejN                  ejV                   ejX                  d      dejZ                  dejR                  e#   dej\                  fd                     Z/ejN                  ejV                   ejX                  d      dejZ                  dejR                  e#   dej\                  fd                     Z0 ejb                  e( e(             Z2h dZ3 ejb                  e4e5e6ejn                  e5f   f   i ddddddddd dd!dd"d#d$d#d%d#d&d#d'd#d(d#d)d#d*d#d+d,d-d.d/d.i d0dd1dd2d3d4d3d5dd6d#d7d#d8d#d9d#d:d#d;d#d<d#d=d#d>d?d@d?dAddBd#i dCd#dDd#dEd#dFd#dGd#dHd#dId#dJd#dKd#dLd?dMd?dNddOd#dPd#dQd#dRd#dSd#i dTd#dUd#dVd#dWddXd#dYd#dZd?d[d?d\d,d]d.d^d.d_dd`ddad3dbd3dcdddedddfdfdgdgdd#d#d#d#ddhdi      Z8ejN                  de9fdj       Z: G dk dle      Z; ejb                  ejx                  e jz                  dmk(        Z>ejN                  ej~                  dnejx                  fdo              Z@ejN                  dpedqejn                  drejx                  dsejx                  fdt       ZAejN                  dpedqejn                  fdu       ZBejN                  ej~                  	 ddpedqejn                  dve5fdw              ZCejN                  dxe(dyejx                  fdz       ZDejH                  ejJ                   G d{ d|eE                    Z9y)}    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)int64_tuint8_tc            
       D   e Zd Zddej                  dej
                  fdZd Zej                  dej                  e
j                     fd       Zej                  de
j                  dej                  d	ej                  dej                  e
j                     fd
       Zy)CudaContext	device_idprimary_ctxc                 X    || _         || _        t        j                  | _        i | _        y N)r   r   cythonNULL_device_ref_frames_cache)selfr   r   s      A/root/.hermes/venv/lib/python3.12/site-packages/av/video/frame.py	__cinit__zCudaContext.__cinit__   s%    "&!;;    c                 .   | j                   j                         D ]  }t        j                  t        j                  t
        j                     t        j                  t        j                  |            }t        j                  t        j                  |              | j                   j                          | j                  }|t        j                  k7  r>t        j                  t        j                  |             t        j                  | _        y y r   )r    valuesr   castpointerlibAVBufferRefsize_tav_buffer_unrefaddressclearr   r   )r!   vrefs      r"   __dealloc__zCudaContext.__dealloc__   s     ##**, 	5A++s/FMM1-C s 34	5 	  "&++s 34%{{D r$   returnc           	         | j                   }|t        j                  k7  r|S t        j                  }| j                   j	                         }|}t        d| j                  rdndi      }t        t        j                  t        j                  |      t        j                  ||j                  d             || _         |S )Nr   10r   )r   r   r   r   encoder   r   r   r)   av_hwdevice_ctx_creater-   AV_HWDEVICE_TYPE_CUDAptr)r!   
device_refdevice_bytesc_deviceoptionss        r"   _get_device_refzCudaContext._get_device_ref/   s    6:6F6F
$[[
..)113".(4#3#3C=
 	&&z*))	
 &r$   sw_fmtwidthheightc                    t        |      t        |      t        |      f}| j                  j                  |      }|t        j                  t        j
                  t        j                     t        j                  t        j                  |            }t        j                  |      }|t        j                  k(  rt        d      |S | j                         }t        j                  |      }	|	t        j                  k(  rt        d      	 t        j                  t        j
                  t        j                     |	j                        }
t!        d      |
_        ||
_        t        |      |
_        t        |      |
_        t+        t        j,                  |	             t        j                  |	      }|t        j                  k(  r3t        j0                  t        j2                  |	             t        d      t        j                  t        j                  |	      | j                  |<   |S # t.        $ r* t        j0                  t        j2                  |	              w xY w)Nzav_buffer_ref() failedzav_hwframe_ctx_alloc() failed   cuda)intr    getr   r'   r(   r)   r*   r+   av_buffer_refr   MemoryErrorr>   av_hwframe_ctx_allocAVHWFramesContextdatar	   format	sw_formatr@   rA   r   av_hwframe_ctx_init	Exceptionr,   r-   )r!   r?   r@   rA   keycached
cached_refout_refr:   
frames_ref
frames_ctxs              r"   get_frames_ctxzCudaContext.get_frames_ctxG   s    6{CJF4##'', s/FMM62J ''
3G&++%!":;;N))+
696N6N7

 $=>>	@Fs445zAJ !,G 4J#)J "5zJ #FJc--j9:
 ##J/fkk!z :;677"(++fmmZ"H3  	z :;	s   BH0 03I#N)r   T)__name__
__module____qualname__r   rD   bintr#   r1   cfuncr(   r)   r*   r>   AVPixelFormatrU    r$   r"   r   r      s     6::    +  \\!@  . \\0!!0 zz0 

	0
 
	(0 0r$   r   objstreamr2   c                    t        | d      r%|| j                         n| j                  |      }n| }t        |d      st                t	        d      t        j                  t
        j                  t           t        |d            }|t
        j                  k(  rt        d      t        |d      dk7  rt        d      |S )	N
__dlpack__)r^   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   	TypeErrorr   r'   r(   r   r   r   
ValueErrorr   RuntimeError)r]   r^   capsulemanageds       r"   _consume_dlpackrg   {   s    
 sL!&,n#.."#..PV.:WWk2L
 	
 kk'Wk2G &++=>>"23q8566Nr$   F)checkopaquerJ   c                     t        j                  t         j                  t           |       }|t         j                  k7  r|j                  |       y y r   )r   r'   r(   r   r   deleter)ri   rJ   rf   s      r"   _dlpack_avbuffer_freerl      s?     06{{'0G &++  r$   c                     | t         j                  k7  r=t         j                  5  t        t        j                  t
        |              d d d        y y # 1 sw Y   y xY wr   )r   r   gilr   r'   object)ri   rJ   s     r"   _numpy_avbuffer_freerp      sI     ZZ 	3fkk&&12	3 	3 	3 	3s   $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_rggb16lerq   )   uint8rr   r   )   r   r   r   r   r   )   uint16r   r   r   r   r   r   r   r{   )   r   r   )   r   r   rs   rt   r   )   r   r   r|   r   r   r   r   r   r   r   r   r   )r   float32r   ru   r   r   r   r   r   r   r   r   r   r   r   r   rv   r   r   r   r   r   r   r   r   r}   r   r   r   r   r~   r   r   ry   rz   r   r   r   )r   float16r   )   r   )   r   )r   r   )r   r   r   r   r   r   r   r   r   r   r   c                       t        t              S )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   e  s     ,--r$   c                       e Zd Zej                  Zej                  Zej                  Z	ej                  Zej                  Zej                  Zej                   Zej$                  Zy)PictureTypeN)rV   rW   rX   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   p  sZ    ##DAAAA			B			B			Br$   r   big
big_endianc                 8    t         |k7  r| j                         S | S r   )_is_big_endianbyteswap)arrayr   s     r"   byteswap_arrayr   ~  s     #~~Lr$   planebytes_per_pixelflip_horizontalflip_verticalc                    | }d}|j                   |z  }|}d}	t        |j                        }
t        j                  t        j
                        \  }}}|r|j                  dz
  }d}d}nd}|j                  }d}t        |||      D ]Y  }||z  }|r=t        d||      D ],  }t        |      D ]  }|||z   |z
  |z
  |z      ||	|z   |z   <    . n||||z    ||	|	|z    |	|
z  }	[ y )Nr   r   )r@   abs	line_sizer   declarerD   rA   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IwLL1$		,,Y. h1h8 / A+0(1,>B,E%!)a-( /4EEH<L.ME%%(*+r$   c                 B    | j                         }t        |||dd       y )NF)tobytesr  )r   r   r   imgbytess       r"   copy_array_to_planer
    s    mmoH%%Gr$   dtypec                    ddl }|j                  |      }| j                  j                  j                  | j
                     }t        |      }|j                  }||z  }|dk(  r| j                  | j                  f}	||f}
n| j                  | j                  |f}	|||f}
|dk  r2| j                  dz
  |z  }| g|
dd }
|j                  |	|| ||
      S |j                  |	|| |
      S )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  framer9   linesizeindexr   itemsizerA   r@   ndarray)r   r   r  np	dtype_objr   total_line_sizer  channelsshaper  r  s               r"   useful_arrayr    s     I((5I)nO!!H(*H1}u{{+"H-u{{H5"OX>1},,"o5##2gabk2zz5  
 	
 ::e9UG:LLr$   r   okc                 :    |st        d| j                   d      y )NzUnexpected numpy array shape ``)rc   r  )r   r  s     r"   check_ndarray_shaper    s#    9%++aHII r$   c                   `   e Zd Zd,dZej
                  dej                  dej                  dej                  fd       Z	ej
                  d        Z
d Zed        Zd	 Zed
        Zed        Ze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d        Zej*                  d        Zd Zd Zej8                  defd       Zd Z ej@                  d      d-d       Z!d  Z"e#d!        Z$e#d.d"       Z%d# Z&e# ej@                  d      d/d$              Z'e#	 	 	 d0d%e(de)de)fd&       Z*e#	 	 	 	 	 	 	 d1de+de)de)d(e)d'z  d)e,f
d*       Z-e#d+        Z.y')2r   c                 R    |t         u ry t        |      }| j                  |||       y r   )r   r	   _init)r!   r@   rA   rK   c_formats        r"   r#   zVideoFrame.__cinit__  s(    **&1&&9

8UF+r$   rK   r@   rA   c                 :   d}t         j                  5  || j                  _        || j                  _        || j                  _        |r"|r t        j                  | j                  d      }d d d        |rt        |       | j                          y # 1 sw Y   'xY w)Nr   r   )
r   nogilr9   r@   rA   rK   r)   av_frame_get_bufferr   _init_user_attributes)r!   rK   r@   rA   ress        r"   r"  zVideoFrame._init  sy    \\ 	<"DHHN$DHHO$DHHO --dhh;	< cN""$	< 	<s   ABBc                     t        t        j                  t        j                  | j
                  j                        | j
                  j                  | j
                  j                        | _        y r   )	r
   r   r'   r)   r[   r9   rK   r@   rA   r!   s    r"   r'  z VideoFrame._init_user_attributes  s@    &KK))488??;HHNNHHOO
r$   c                 B    t        j                  | j                         y r   )r)   av_frame_unrefr9   r*  s    r"   r1   zVideoFrame.__dealloc__  s    488$r$   c                 T   | j                   j                  syt        j                  t        j                  t
        j                     | j                   j                  j                        }t        |j                  | j                   j                  | j                   j                        S )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)r9   hw_frames_ctxr   r'   r(   r)   rI   rJ   r
   rL   r@   rA   )r!   rT   s     r"   rL   zVideoFrame.sw_format  sm     xx%%<BKKNN30014883I3I3N3N=

  
 4 4dhhnndhhooVVr$   c                     d| j                   j                   d| j                   d| j                  j                   d| j
                   d| j                   dt        |       ddS )Nz<av.z, pts= xz at 0x>)	__class__rV   ptsrK   namer@   rA   idr*  s    r"   __repr__zVideoFrame.__repr__  s\    4>>**+6$((1T[[=M=M<Nazzl!DKK=r$xl!=	
r$   c           	      B   | j                   }| j                  j                  rt        j                  t        j
                  t        j                     | j                  j                  j                        }t        |j                  | j                  j                  | j                  j                        }d}t        |j                  j                        D ]0  }|j                  j                  |   j                   dz   }||k  s/|}2 |j"                  dk(  rd}d}||k  r=| j                  j$                  |   r$|dz  }||k  r| j                  j$                  |   r$|dk(  rt'        | d      fS t)        t        |      D cg c]  }t'        | |       c}      S c c}w )z:
        A tuple of :class:`.VideoPlane` objects.
        r   r   rx   r   )rK   r9   r.  r   r'   r(   r)   rI   rJ   r
   rL   r@   rA   r   nb_componentscompr   r5  extended_datar   tuple)r!   fmtrT   max_plane_countr  countplane_counts          r"   planeszVideoFrame.planes  sW    kk88!!@Fs445txx7M7M7R7RAJ #$$dhhnndhhooC '(sww,,- 	(AGGLLO))A-E&"'	( 88vO"#O+0F0F{0S1K O+0F0F{0S!tQ'))53EFajq)FGGFs   Fc                 .    | j                   j                  S )zWidth of the image, in pixels.)r9   r@   r*  s    r"   r@   zVideoFrame.width=  s     xx~~r$   c                 .    | j                   j                  S )zHeight of the image, in pixels.)r9   rA   r*  s    r"   rA   zVideoFrame.heightB  s     xxr$   c                     t        |       S )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   r*  s    r"   rotationzVideoFrame.rotationG  s     $D))r$   c                 b    t        | j                  j                  t        j                  z        S )z+Is this frame an interlaced or progressive?)boolr9   flagsr)   AV_FRAME_FLAG_INTERLACEDr*  s    r"   interlaced_framezVideoFrame.interlaced_frameQ  s"     DHHNNS%A%AABBr$   c                 .    | j                   j                  S )zReturns an integer that corresponds to the PictureType enum.

        Wraps :ffmpeg:`AVFrame.pict_type`

        :type: int
        r9   	pict_typer*  s    r"   rM  zVideoFrame.pict_typeW  s     xx!!!r$   c                 &    || j                   _        y r   rL  r!   values     r"   rM  zVideoFrame.pict_typea      "r$   c                 .    | j                   j                  S )zKColorspace of frame.

        Wraps :ffmpeg:`AVFrame.colorspace`.

        r9   
colorspacer*  s    r"   rT  zVideoFrame.colorspacee  s     xx"""r$   c                 &    || j                   _        y r   rS  rO  s     r"   rT  zVideoFrame.colorspacen  s    #r$   c                 .    | j                   j                  S )zMColor range of frame.

        Wraps :ffmpeg:`AVFrame.color_range`.

        r9   color_ranger*  s    r"   rX  zVideoFrame.color_ranger  s     xx###r$   c                 &    || j                   _        y r   rW  rO  s     r"   rX  zVideoFrame.color_range{  s    $r$   c                 .    | j                   j                  S )zWTransfer characteristic of frame.

        Wraps :ffmpeg:`AVFrame.color_trc`.

        r9   	color_trcr*  s    r"   r\  zVideoFrame.color_trc  s     xx!!!r$   c                 &    || j                   _        y r   r[  rO  s     r"   r\  zVideoFrame.color_trc  rQ  r$   c                 .    | j                   j                  S )zUColor primaries of frame.

        Wraps :ffmpeg:`AVFrame.color_primaries`.

        r9   color_primariesr*  s    r"   r`  zVideoFrame.color_primaries  s     xx'''r$   c                 &    || j                   _        y r   r_  rO  s     r"   r`  zVideoFrame.color_primaries  s    #( r$   c                 x    | j                   st               | _          | j                   j                  | g|i |S )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"   re  zVideoFrame.reformat  s;     /1D(t((????r$   c                 *     | j                   dddi|S )a
  Get 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'

        rK   r~   r\   )re  )r!   rg  s     r"   to_rgbzVideoFrame.to_rgb  s     t}}6G6v66r$   filepathc                    |j                  d      rd}n0|j                  d      s|j                  d      rd}nt        d      |rdnd}|rd	nd
}ddlm}  ||dddi      5 }|j	                  ||      }| j
                  |_        | j                  |_        |j                  |j                  | j                  |                   |j                  |j                  d             ddd       y# 1 sw Y   yxY w)zPSave a VideoFrame as a JPG or PNG.

        :param filepath: str | Path
        z.pngFz.jpgz.jpegTz"filepath must end with png or jpg.mjpegpngr   r~   r   )openwupdater4   )r=   )pix_fmt)rK   N)
endswithrc   av.container.corern  
add_streamr@   rA   muxr6   re  )r!   rj  is_jpgencoderrq  rn  outputoutput_streams           r"   savezVideoFrame.save  s     V$Fv&(*;*;G*DFABB"(we%+z*(C(C9 	3V"--gw-GM"&**M#';;M JJ}++DMMM,IJKJJ}++D12	3 	3 	3s   $BC33C<c           	         ddl m}  | j                  d	ddi|j                  d   }|}|j                  }t        |      }d}|j                  dz  }|j                  |z  }	t        |	      }
|dk  r|j                  dz
  |z  nd}||	k  r)||||z    |
|||z    |dk  r||z  }n||z  }||z  }||	k  r)|j                  d|j                  |j                  ft        |
      dddd      S )
zGet an RGB ``PIL.Image`` of this frame.

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

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

        r   )ImagerK   r~   r   r   RGBrawr\   )PILr|  re  rA  r   r   r@   rA   	bytearray	frombytesbytes)r!   rg  r|  r   r   r   r   r   r   o_sizer   r   s               r"   to_imagezVideoFrame.to_image  s    	)DMMCCFCJJ1M*/ %	"%i. "'++/ %x 7$V, AJAq 0H<STfn.3EEH<L.ME%%(*+1}!!XE fn EKK.eeUAq
 	
r$   Tc           	      
   | j                   j                  rd|vrt        j                  t        j                  t
        j                     | j                   j                  j                        }t        |      }t        |j                  | j                   j                  | j                   j                        j                  |d<   t        |      dkD  r | j                  di |n| }|j                   j                  rt!        d      ddl}|j$                  j                  }|j&                  }|t(        v rH|dk(  rF|j                   j                  dz  dk(  sJ d       |j                   j                  dz  dk(  sJ d       t(        |   \  }}	t        |      }
|
d	k(  rt+        |d   ||	      }n|j-                  |j                   j                  |j                   j                  |
f|	
      }|j/                  d      rddd	gt1        d|
      }nt1        |
      }t3        |      D ]  \  }}t+        ||   ||	      |dddd|f<   ! t5        ||j7                  d            }|s|dv r|j9                  |dd      }|S |dv rA|j                   j                  dz  dk(  sJ d       |j                   j                  dz  dk(  sJ d       |dk(  }|rdnd	}|rdnd}	|j                   j                  dk(  s|j                   j                  dk(  r)|j-                  d|j                   j                  f|	
      S |j;                  t+        |d   ||	      j=                  d      t+        |d	   ||	      j=                  d      t+        |d   ||	      j=                  d      g      j=                  d|j                   j                        S |dk(  r|j                   j                  dz  dk(  sJ d       |j                   j                  dz  dk(  sJ d       t+        |d   dd      }t+        |d	   dd      }t+        |d   dd      }|j?                  |dd	      }|j?                  |dd	      }|r|jA                  |||gd      S |jA                  |||gd      S |dk(  r_t+        |d         }|jC                  |d	   d      jE                  d      j=                  dd	      jG                  |jH                        }||fS |dk(  rn|j;                  t+        |d         j=                  d      t+        |d	   d      j=                  d      g      j=                  d|j                   j                        S t!        d| d      )aI  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.

        rK   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   axisrx   i4>i4rw   z'Conversion to numpy array with format `` is not yet supportedr\   )%r9   r.  r   r'   r(   r)   rI   rJ   dictr
   rL   r@   rA   r5  lenre  rc   r  rK   rA  _np_pix_fmt_dtypesr  empty
startswithr   	enumerater   rr  moveaxishstackreshaperepeatstack
frombufferastypeviewr   )r!   channel_lastrg  rT   r  r  format_namerA  r  r  
num_planesr   plane_indicesr  p_idxis_10bityur/   u_fullv_fullimagepalettes                          r"   
to_ndarrayzVideoFrame.to_ndarray  s    . 88!!hf&<@Fs445txx7M7M7R7RAJ &\F/$$dhhnndhhoo d 8 8;6{QMDMM3F3D99""QRR ll'').,,i'yy*a/S1SS/yy''!+q0U2UU00=OHe(+FJQ$VAY%@YY%%uyy
C5 !  ))%0%&1$DuQ
/C$DM$)*$5M )- 8 RHAu%1&-5%QE!Q'NR"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 He<DDRH He<DDRH He<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    |j                   dk7  r|j                  d      }t        || j                  d   d       y)z6
        Update content from a ``PIL.Image``.
        r}  r   r   N)modeconvertr
  rA  )r!   imgs     r"   	set_imagezVideoFrame.set_imagei  s2     88u++e$CCQ3r$   c                 v    t        | j                  d   | j                  d   d      }|j                  |        |S )z9
        Construct a frame from a ``PIL.Image``.
        r   r   r~   )r   sizer  )r  r  s     r"   
from_imagezVideoFrame.from_imager  s3    
 'sxx{CHHQKIr$   c                 *   ddl }| j                  d   }|s| j                  d   }|dv rXt        | dd       t        | | j                  d   dk(         | j                  dd dk7  rt        d	      | j                  d   f}n|d
v rXt        | dd       t        | | j                  d   dk(         | j                  dd dk7  rt        d	      | j                  d   f}n|dv rXt        | dd       t        | | j                  d   dk(         | j                  dd dk7  rt        d	      | j                  d   f}nA|dv rXt        | dd       t        | | j                  d   dk(         | j                  dd dk7  rt        d	      | j                  d   f}n|dv rXt        | dd       t        | | j                  d   dk(         | j                  dd dk7  rt        d	      | j                  d   f}n|dv rXt        | dd       t        | | j                  d   dk(         | j                  dd dk7  rt        d	      | j                  d   f}n-|dv rXt        | dd       t        | | j                  d   dk(         | j                  dd dk7  rt        d	      | j                  d   f}n|dv r<t        | dd       | j                  d   dk7  rt        d	      | j                  d   f}n|dv r<t        | dd       | j                  d   dk7  rt        d	      | j                  d   f}nQ|dv r<t        | dd       | j                  d   dk7  rt        d	      | j                  d   f}n|dv r}t        | dd       t        | | j                  d   dk(         | j                  dd dk7  rt        d	      | j                  d   dz  | j                  d   dz  | j                  d   dz  f}n|dv r}t        | dd       t        | | j                  d   dk(         | j                  dd dk7  rt        d	      | j                  d   dz  | j                  d   dz  | j                  d   dz  f}n|dv r}t        | dd       t        | | j                  d   dk(         | j                  dd dk7  rt        d	      | j                  d   dz  | j                  d   dz  | j                  d   dz  f}n|dv rt        | dd       t        | | j                  d   dk(         | j                  dd dk7  rt        d	      | j                  d   dz  | j                  d   dz  | j                  d   dz  | j                  d   dz  f}n|d v rt        | dd       t        | | j                  d   dk(         | j                  dd dk7  rt        d	      | j                  d   dz  | j                  d   dz  | j                  d   dz  | j                  d   dz  f}nj|d!v rt        | dd       t        | | j                  d   dk(         | j                  dd dk7  rt        d	      | j                  d   dz  | j                  d   dz  | j                  d   dz  | j                  d   dz  f}n|d"v rt        | dd       t        | | j                  d   dz  dk(         t        | | j                  d   dz  dk(         |d#z  dz  }| j                  d   dk7  rt        d	      |d$v r3| j                  d   | j                  d   dz  | j                  d   dz  f}n.| j                  d   | j                  d   f}nt        d%| d&      |j                  d'      r*|j                  |j                  | d(g d)f   d*d            } n:|j                  d+      r)|j                  |j                  | d(g d,f   d*d            } t        t              }|j                  | ||||       |S )-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   >   rq   rr   rt   rz   r   )r   r      r   r   r   r   )r   r      r   r   r      r   r   )r   r   >   rs   rv   ry   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   >   ru   >
   r   r   r   r   r   r   r   r   r   r   >   r   r   >   r|   >   r   r   r   r   r   r   r   r   >   r   r   >   rw   r   r   r      r   r   )Conversion from numpy array with format `r  r|   .)r   r   r   r   r   ru   )r   r   r   )r  r  r   r  r  rc   r  ascontiguousarrayr  r   r   _image_fill_pointers_numpy)r   rK   r@   r  rA   	linesizesr  s          r"   from_numpy_bufferzVideoFrame.from_numpy_buffer|  sf     	QKKNE''%!,u{{1~':;}}QR F* !QRRq)+ICC%1-u{{1~':;}}QR F* !QRRq)+I//%A.u{{1~':;}}QR G+ !QRRq)+I77%!,u{{1~':;}}QR F* !QRRq)+IGG%1-u{{1~':;}}QR F* !QRRq)+I11%A.u{{1~':;}}QR F* !QRRq)+I11%A.u{{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%!,u{{1~':;}}QR F* !QRRa A%a A%a A%I
  
 
 %1-u{{1~':;}}QR F* !QRRa A%a A%a A%I
 11%A.u{{1~':;}}QR G+ !QRRa A%a A%a A%I
 y %!,u{{1~':;}}QR F* !QRRa A%a A%a A%a A%	I  	
 	
 %1-u{{1~':;}}QR F* !QRRa A%a A%a A%a A%	I 33%A.u{{1~':;}}QR G+ !QRRa A%a A%a A%a A%	I 66%!,u{{1~'9Q'>?u{{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((vy&Qr$   c           	      V   |j                   j                  }t        j                  t        j                  t
           |      }t        |      }t        j                  | j                         || j                  _
        || j                  _        || j                  _        t        |      D ]  \  }	}
|
| j                  j                  |	<     t        t        j                   | j                  j                  t        j                  t        j"                  | j                  j                        | j                  j                  || j                  j                              }t        j                  t$        |      }t'        |       t        j(                  ||t*        t        j                  t        j,                  |      d      | j                  j.                  d<   | j                  j.                  d   t        j0                  k(  rt3        |       t5        d      | j7                          y )Nr   zav_buffer_create failed)ctypesrJ   r   r'   r(   r   r	   r)   r,  r9   rK   r@   rA   r  r  r   av_image_fill_pointersr[   ro   r   av_buffer_createrp   p_voidbufr   r   rG   r'  )r!   r  r@   rA   r  rK   c_datac_ptrr#  r  r  requiredpy_bufs                r"   r  z%VideoFrame._image_fill_pointers_numpyY  s   " %+MM$6$6)/V^^G5Lf)U&1&&9488$ # $Y/ 	,KAx#+DHHa 	, &&C--txx?!!
 VV,&.. KKv.
Q 88<<?fkk)f788""$r$   c           	         ddl }i ddddddddd	dd
dddddddddddddddddddddddi ddddddddddddddd d!d"d#d$d#d%d#d&d#d'd#d(d#d)d#d*d#d+d#i d,d#d-d.d/d.d0dd1dd2dd3dd4dd5dd6dd7dd8dd9dd:dd;dd<d=d>d=dd!d#d#ddd!d?j                  |d@      \  }}}|4| j                  dAk(  r| dddddf   } t        | |dB       |s|dCv r|j	                  | ddA      } t        | | j                  dA   |k(         t        | |j                  dD            } t        | j                  dE   | j                  d   |      }|j                  j                  j                  dF      r:|j                  | dddddEdBf   | ddddddEf   | dddddBdf   gdAG      } t        |      D ]&  }t        | dddd|f   |j                   |   |       ( |S |dHk(  r| \  } }	t        | dIdA       t        |	dIdA       t        |	|	j                  dJk(         t        | j                  dE   | j                  d   |      }t        | |j                   d   dE       |j                   dE   j#                  |	j%                  dK      j'                  dL      j)                                |S |dMv r#t        | dIdA       t        | | j                  d   dBz  dk(         t        | | j                  dE   dAz  dk(         t        | j                  dE   | j                  d   dAz  dBz  |      }|j*                  dk(  s|j,                  dk(  r|S |j*                  |j,                  z  }
dN|
z  dOz  }| j/                  dP      }t        |d|
 |j                   d   dE       t        ||
| |j                   dE   dE       t        ||d |j                   dA   dE       |S |dQk(  r#t        | dRdA       t        | | j                  d   dBz  dk(         t        | | j                  dE   dAz  dk(         t        | j                  dE   | j                  d   dAz  dBz  |      }|j*                  dk(  s|j,                  dk(  r|S |j*                  |j,                  z  }
dN|
z  dOz  }| j/                  dP      }t        |d|
 |j                   d   dA       t        ||
| |j                   dE   dA       t        ||d |j                   dA   dA       |S |dSk(  r t        | dIdA       t        | | j                  d   dOz  dk(         t        | | j                  dE   dAz  dk(         t        | j                  dE   | j                  d   dAz  |      }|j*                  dk(  s|j,                  dk(  r|S |j*                  |j,                  z  }
|
|
dAz  z   }| j/                  dP      }t        |d|
 |j                   d   dE       t        ||
| |j                   dE   dE       t        ||d |j                   dA   dE       |S |dTk(  rBt1        | |j2                        r| j4                  |j6                  k7  rt9        dU      |r&| j                  dA   dBk(  r|j	                  | dAd      } n| j                  d   dBk(  st9        dV      | j                  dEd \  }}|dAz  dk7  s|dAz  dk7  rt9        dW      t        |||      }t        | d   |j                   d   dA       | dEdddddAf   j;                         }| dAdddddAf   j;                         }t        ||j                   dE   dA       t        ||j                   dA   dA       |S |dXk(  rit        | dIdB       t        | | j                  d   dAz  dk(         t        | | j                  dE   dAz  dk(         t        | | j                  dA   dAk(         n|dYv r+t        | dIdB       t        | | j                  dA   dBk(         n|dZv r+t        | dIdB       t        | | j                  dA   dOk(         n|d[v rt        | dRdB       t        | | j                  dA   dBk(         t        | j                  dE   | j                  d   |      }t        t        | |j                  dD            |j                   d   d\       |S |d]v rt        | d^dB       t        | | j                  dA   dBk(         t        | j                  dE   | j                  d   |      }t        t        | |j                  dD            |j                   d   d_       |S |d`v rt        | dRdB       t        | | j                  dA   dOk(         t        | j                  dE   | j                  d   |      }t        t        | |j                  dD            |j                   d   da       |S |dbv rt        | dcdB       t        | | j                  dA   dOk(         t        | j                  dE   | j                  d   |      }t        t        | |j                  dD            |j                   d   da       |S |ddv rt        | d^dB       t        | | j                  dA   dOk(         t        | j                  dE   | j                  d   |      }t        t        | |j                  dD            |j                   d   de       |S |dfk(  rt        | dIdA       t        | | j                  d   dBz  dk(         t        | | j                  dE   dAz  dk(         t        | j                  dE   | j                  d   dAz  dBz  |      }|j*                  |j,                  z  }| j/                  dP      }t        |d| |j                   d   dE       t        ||d |j                   dE   dA       |S t9        dg| dh      t        | j                  dE   | j                  d   |      }t        | |j                   d   | j                  dAk(  rdE       |S | j                  dA          |S )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   rs   r|   )r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   ru   )r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   rv   r   r   r   r   r   r   r   r   r}   r   r   r   )r   r   r   r   )ry   r   r   r   r   r   r   )NNNr   r   r  r  r   r  r  rx   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  >   rq   rr   rt   rz   r  r   r  r   r   r  r   r  r   r  r   rw   r  r  )r  rE   ndimr   r  r  r  r   rr  r   rK   r5  r  concatenater   r
  rA  rp  r  r  r  r@   rA   r  
isinstancer  r  r   rc   copy)r   rK   r  r  r  r  r  r  r  r  u_startv_startflatrA   r@   r  r/   uv_starts                     r"   from_ndarrayzVideoFrame.from_ndarray  s   $ 	;%
.;%
.;%
 ?;%
 .	;%

 .;%
 ?;%
 .;%
 .;%
 ?;%
 .;%
 .;%
 ?;%
 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u{{1~'AB"5&//$*?@Eu{{1~u{{1~vFE||  ++E2aAaCi(aAaCi(aABh
  '  8_ O#E!Q'NELLOXNOL V"NE7%!,'7A.()BCu{{1~u{{1~vFEu||A:LLO""7<<#6#=#=d#C#K#K#MNL..%!,u{{1~'9Q'>?u{{1~'9Q'>?u{{1~A0Bq/H&QE{{a5<<1#4kkELL0G'kQ&G==$DQwa!DWW 5u||AJWXQCL}$%1-u{{1~'9Q'>?u{{1~'9Q'>?u{{1~A0Bq/H&QE{{a5<<1#4kkELL0G'kQ&G==$DQwa!DWW 5u||AJWXQCLy %!,u{{1~'9Q'>?u{{1~'9Q'>?u{{1~u{{1~/BFKE{{a5<<1#4kkELL0G1,G==$DQwa!DWW 5u||AJWXQCL}$eRZZ0EKK2994L !<== A! 3E1a0kk!n) T  "KKOMFEqyA~!q !@AAuff5Ea%,,q/1=aCaCi %%'AaCaCi %%'A5<<?A65<<?A6Ly %!,u{{1~'9Q'>?u{{1~'9Q'>?u{{1~':;))%!,u{{1~':;77%!,u{{1~':;CC%1-u{{1~':;u{{1~u{{1~vFEufood&;<ell1oq L//%A.u{{1~':;u{{1~u{{1~vFEufood&;<ell1or LGG%1-u{{1~':;u{{1~u{{1~vFEufood&;<ell1oq L11%A.u{{1~':;u{{1~u{{1~vFEufood&;<ell1oq L11%A.u{{1~':;u{{1~u{{1~vFEufood&;<ell1or Lv%!,u{{1~'9Q'>?u{{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5<<?qA	
  ?Dkk!n	
 r$   r   c                     t        |||      }|dk(  rt        | |j                  d   d||       |S |dv r1t        | |j                  d   |j                  d      rdnd||       |S t	        d| d	      )
Nrz   r   r   >   r   r   r   r   r   r   r   r   r   r   r   r   8r   r   zFormat 'z' is not supported.)r   r  rA  rr  NotImplementedError)r   r@   rA   rK   r   r   r  s          r"   
from_byteszVideoFrame.from_bytes  s     5&&1V5<<?A4 /  
 
  Q__S)q  &8K&LMMr$   Nr   r   c           	      >   t        | t        t        f      s| f} t        |      }t        d      }	t        d      }
t        d      }||	|
|fvrt        j                  | ||||||      S t        |       dk7  rt        d      ||	k(  rdnd}|dk(  rdnd}t        j                  }t        j                  }d }	 t        | d	   |      }t        | d   |      }|j                  j                  }|j                  j                  }||k7  rt        d
      |t        t        fvrt        d      |j                  j                   }|j                  j                   }||k7  rt        d      |t        k(  r||}n!||k7  rt        d      |dvrt        d      d	}|t        k(  r|d	k7  s|d	k7  rt        d      |j                  j"                  j$                  dk7  sF|j                  j"                  j&                  |k7  s#|j                  j"                  j(                  dk7  rt+        d      |j                  j"                  j$                  dk7  sF|j                  j"                  j&                  |k7  s#|j                  j"                  j(                  dk7  rt+        d      |j                  j,                  dk7  rt        d      t        j.                  t0        |j                  j2                  d	         }t        j.                  t0        |j                  j2                  d         }|d	k(  r:|d	k(  r5t        j.                  t4        |      }t        j.                  t4        |      }n*|d	k(  s|d	k(  rt        d      ||k7  s||k7  rt        d      |dz  s|dz  rt        d      |j                  j6                  t        j                  k7  rY|j                  j6                  d   dk7  rt        d      t        j.                  t0        |j                  j6                  d	         }nt        j.                  t0        |      }t        j.                  t4        ||z        }t        j.                  t4        ||z        }|j                  j,                  }|dz  }|dk(  rt        j.                  t4        |j                  j2                  d	         }t        j.                  t4        |j                  j2                  d         }||k7  s||k7  rt        d      |j                  j6                  t        j                  k7  rZ|j                  j6                  d   dk7  rt        d      t        j.                  t0        |j                  j6                  d	         }nt        j.                  t0        |      }nx|dk(  rgt        j.                  t4        |j                  j2                  d	         }t        j.                  t4        |j                  j2                  d         }t        j.                  t4        |j                  j2                  d         } ||k7  s||dz  k7  s| dk7  rt        d      |j                  j6                  t        j                  k7  ru|j                  j6                  d   dk7  s|j                  j6                  d   dk7  rt        d      t        j.                  t0        |j                  j6                  d	         }n&t        j.                  t0        |      }nt        d      t        j.                  t4        ||z        }!t        j.                  t4        |!|dz  z        }"t9               }||j:                  _        ||j:                  _        |t        k(  r|tA        ||      }#nt        |t@              st+        d      t5        |j                         t5        |      k7  rt        d       tC        |jD                        tC        |      k7  rt        d!      t        j.                  t@        |      }#|#jG                  |||      }$t        d"      |j:                  _$        |$|j:                  _%        ||_&        |#|_'        n||j:                  _$        t        j.                  t        jP                  tR           |j                  jT                        t        j.                  t        jV                  |j                  jX                        z   }%t        j.                  t        jP                  tR           |j                  jT                        t        j.                  t        jV                  |j                  jX                        z   }&t[        j\                  |%|t^        t        j.                  t        j`                  |      d	      |j:                  jb                  d	<   |j:                  jb                  d	   t        j                  k(  rte        d#      |%|j:                  jT                  d	<   ||j:                  jf                  d	<   t        j                  }t[        j\                  |&|"t^        t        j.                  t        j`                  |      d	      |j:                  jb                  d<   |j:                  jb                  d   t        j                  k(  rte        d$      |&|j:                  jT                  d<   |!|j:                  jf                  d<   t        j                  }|ji                          |S # tj        $ rk |t[        jl                  |j:                         |t        j                  k7  r|jo                  |       |t        j                  k7  r|jo                  |        w xY w)%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   r   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_ctxrC   z#av_buffer_create failed for plane 0z#av_buffer_create failed for plane 1)8r  r<  listr	   r   _from_dlpack_planarr  rc   r   r   rg   	dl_tensordevice_typer   r   r  r   r  codebitslanesrb   r  r'   r   r  rD   r  r   r9   r@   rA   r   rG  r   rU   rK   r.  
_device_id	_cuda_ctxr(   r   rJ   r+   byte_offsetr)   r  rl   r  r  rG   r  r'  rN   r,  rk   )'rA  rK   r@   rA   r^   r   r   cuda_contextr?   rw   p010lep016leexpected_bitsr  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ctxrS   y_ptruv_ptrs'                                          r"   from_dlpackzVideoFrame.from_dlpack  s,	    &5$-0YF$/$77#Y'Y'$//11vvy  v;!Z  $t^%*1.4kk.4kk p	 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!  '%	{SC%lK@'(LMM<112c)nD(_  L445k9JJ(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   6i1l( (A4nc           
         t        j                  |      }|t        j                  k(  rt	        d|      |j
                  t         j                  t         j                  z  t         j                  z  z  rt	        d|d      d}t        |j                        D ]`  }	t        j                  t        j                  |j                  |	   j                        dz   |kD  sE|j                  |	   j                  dz   }b dg|z  }
t        |      D ]{  }d}t        |j                        D ]K  }	t        j                  t        j                  |j                  |	   j                        |k(  sB|	|
|<   |dz  }M |dk7  snt	        d|d       t        |       |k7  rt!        |d| d	t        |              |j                  d   j"                  }|dk7  r|d
k7  rt	        d      |d
k(  r(|j
                  t         j$                  z  rt	        d      |dz  }|dvrt!        d      |j&                  }|j(                  }d }t        j                  }	 t+               }||j,                  _        t        |      D ]y  }t1        | |   |      }|j2                  j4                  t6        k7  rt	        d      |j2                  j8                  dk7  rt!        d      |j2                  j:                  j<                  dk7  sF|j2                  j:                  j>                  |k7  s#|j2                  j:                  j@                  dk7  rtC        d|       |j2                  jD                  d
k7  rt!        d| d      t        j                  tF        |j2                  jH                  d         }t        j                  tF        |j2                  jH                  d         }|dk(  r|dk(  r:|dk(  r5t        j                  t        |      }t        j                  t        |      }n*|dk(  s|dk(  rt!        d      ||k7  s||k7  rt!        d      |r|d
z  s|r|d
z  rt!        d|      ||j,                  _%        ||j,                  _&        |
|   }|dk(  xs |d
k(  xr |xs |}|r| |z	   n|}|r| |z	   n|}||k7  s||k7  rt!        d| d| d| d      |j2                  jN                  t        j                  k7  r]|j2                  jN                  d   dk7  rt!        d| d      t        j                  tF        |j2                  jN                  d         }nt        j                  tF        |      }t        j                  t        ||z        }t        j                  t        ||z        }t        j                  t        jP                  tR           |j2                  jT                        t        j                  t        jV                  |j2                  jX                        z   }t        jZ                  ||t\        t        j                  t        j^                  |      d      |j,                  j`                  |<   |j,                  j`                  |   t        j                  k(  rtc        d|       ||j,                  jT                  |<   ||j,                  jd                  |<   t        j                  }| |jg                          |S # th        $ rG |t        jj                  |j,                         |t        j                  k7  r|jm                  |        w xY w)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  rH  AV_PIX_FMT_FLAG_BITSTREAMAV_PIX_FMT_FLAG_PALAV_PIX_FMT_FLAG_BAYERr   r9  r'   rD   r:  r   r  rc   r  AV_PIX_FMT_FLAG_BElog2_chroma_wlog2_chroma_hr   r9   rK   rg   r  r  r   r   r  r  r  r  rb   r  r   r  r@   rA   r  r(   r   rJ   r+   r  r  rl   r  r  rG   r  r'  rN   r,  rk   )rA  r?   rK   r@   rA   r^   r   desc	nb_planesr  comp_of_planepr?  r  r  log2_wlog2_hr  mphpwcomp_idx	is_chromaexp_wexp_hpitch_elemsr  r  r9   s                                r"   r  zVideoFrame._from_dlpack_planar  s    ##F+ 	 6;;%(=fZ&HII::))%%&''(

 &":Q(  !"	t))* 	3A{{6::tyy|'9'9:Q>J IIaL..2		3 iy! 
	AE4--. ;;vzz499Q<+=+=>!C'(M!$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% C #F1Iv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C J '')L 	 ""599-FKK		!	s   R4\ A])r   r   r   )F)r~   r   )r~   F)rz   FF)rw   r   r   NNTN)/rV   rW   rX   r#   r   rZ   r)   r[   uintr"  r'  r1   propertyrL   r7  rA  r@   rA   rE  rJ  rM  setterrT  rX  r\  r`  re  ri  ccallro   rz  r  	cdivisionr  r  staticmethodr  r  r  r  r  rD   r  strrG  r  r  r\   r$   r"   r   r     s5   , \\%C-- %fkk %6;; % %$ \\
 
% W W
 H H>     * * C C
 " " # # # # $ $ $ $ % % " " # # ( ( ) )
@7 \\3V 3 36#
J Vdr
 r
h4   Z Zx5%n Vd}  }~ 
 $$$ $ $L   $ SS S 	S :S S Sj R Rr$   r   )F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.libc.stdintr   r   finalcclassr   rZ   ro   r(   rg   r%  	exceptvalr  voidrl   rp   r   r   supported_np_pix_fmtsr  r"  r<  r  r  r   r   r   rY   	byteorderr   inliner   r  r
  r  r  Framer\   r$   r"   <module>r:     s>   
   # # 4 . E 2 I S S 3 
 = 8 ` `  `F   FNN?4S  8 !MM!
..
!! [[!   ! 3MM3
..
!3 [[3   3 (9 V t $V^^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
 @ @  @r$   