
    q	h94                     t   S SK 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  SSKJr  S S	KJr   " S
 S5      rS\S\S\S\R$                  R&                  4S jrS\R*                  R,                  S\S\S\4S jrS\S\R*                  R,                  S\S\S\\R$                  R&                     4
S jrg)    N)Union   )video_frame_pb2)ffi_pb2)ListOptional)	FfiClient	FfiHandle)get_address)Anyc                      \ rS rSrSrS\S\S\R                  R                  S\	\
\\4   SS4
S	 jr\S\4S
 j5       r\S\4S j5       r\S\R                  R                  4S j5       r\S\4S j5       r\S\R(                  SS 4S j5       rS\R,                  4S jrS\S\\   4S jrSS.S\R                  R                  S\SS 4S jjrS\4S jr\S\4S j5       r Sr!g)
VideoFrame   z
Represents a video frame with associated metadata and pixel data.

This class provides methods to access video frame properties such as width, height,
and pixel format, as well as methods for manipulating and converting video frames.
widthheighttypedatareturnNc                 H    Xl         X l        X0l        [        U5      U l        g)a~  
Initializes a new VideoFrame instance.

Args:
    width (int): The width of the video frame in pixels.
    height (int): The height of the video frame in pixels.
    type (proto_video.VideoBufferType.ValueType): The format type of the video frame data
        (e.g., RGBA, BGRA, RGB24, etc.).
    data (Union[bytes, bytearray, memoryview]): The raw pixel data for the video frame.
N)_width_height_type	bytearray_data)selfr   r   r   r   s        I/var/www/html/env/lib/python3.13/site-packages/livekit/rtc/video_frame.py__init__VideoFrame.__init__"   s    " 
t_
    c                     U R                   $ )zb
Returns the width of the video frame in pixels.

Returns:
    int: The width of the video frame.
)r   r   s    r   r   VideoFrame.width8   s     {{r   c                     U R                   $ zd
Returns the height of the video frame in pixels.

Returns:
    int: The height of the video frame.
)r   r!   s    r   r   VideoFrame.heightB   s     ||r   c                     U R                   $ r$   )r   r!   s    r   r   VideoFrame.typeL   s     zzr   c                 ,    [        U R                  5      $ )z
Returns a memoryview of the raw pixel data for the video frame.

Returns:
    memoryview: The raw pixel data of the video frame as a memoryview object.
)
memoryviewr   r!   s    r   r   VideoFrame.dataV   s     $**%%r   
owned_infoc                 t   U R                   n[        UR                  UR                  UR                  5      n[
        R                  U-  R                  UR                  5      n[        U5      n[        UR                  UR                  UR                  US9n[        U R                  R                  5        U$ )Nr   r   r   r   )info_get_plane_lengthr   r   r   ctypesc_uint8from_addressdata_ptrr   r   r
   handleid)r+   r.   data_lencdatar   frames         r   _from_owned_infoVideoFrame._from_owned_info`   s    $TYY

DKKH(*88G**;;	
 	*##&&'r   c                    [         R                  " 5       n[        U R                  5      nUR                  R                  [        X R                  U R                  U R                  5      5        U R                  Ul        U R                  Ul	        U R                  Ul        X!l
        SUl        U R                  [         R                  R                  [         R                  R                  [         R                  R                  [         R                  R                   4;   a  U R                  S-  Ul        U$ U R                  [         R                  R"                  :X  a  U R                  S-  Ul        U$ )Nr         )proto_videoVideoBufferInfor   r   
componentsextend_get_plane_infosr   r   r   r3   strideVideoBufferTypeARGBABGRRGBABGRARGB24)r   r.   addrs      r   _proto_infoVideoFrame._proto_infoo   s   **,499%/iiT[[YZZZ
kkII	99'',,'',,'',,'',,	
 
 **q.DK  YY+55;;;**q.DKr   	plane_nthc                 *   [        [        U R                  5      U R                  U R                  U R
                  5      nU[        U5      :  a  gX!   n[        R                  UR                  -  R                  UR                  5      n[        U5      $ )a  
Returns the memoryview of a specific plane in the video frame, based on its index.

Some video formats (e.g., I420, NV12) contain multiple planes (Y, U, V channels).
This method allows access to individual planes by index.

Args:
    plane_nth (int): The index of the plane to retrieve (starting from 0).

Returns:
    Optional[memoryview]: A memoryview of the specified plane's data, or None if
    the index is out of bounds for the format.
N)rB   r   r   r   r   r   lenr0   r1   sizer2   r3   r)   )r   rM   plane_infos
plane_infor7   s        r   	get_planeVideoFrame.get_plane   sr     '{499'=tyy$**VZVaVabK(( +
*//1??
@S@ST%  r   F)flip_yrU   c                   [         R                  " 5       nX#R                  l        XR                  l        UR                  R
                  R                  U R                  5       5        [        R                  R                  U5      nUR                  R                  (       a  [        UR                  R                  5      e[        R                  UR                  R
                  5      $ )a  
Converts the current video frame to a different format type, optionally flipping
the frame vertically.

Args:
    type (proto_video.VideoBufferType.ValueType): The target format type to convert to
        (e.g., RGBA, I420).
    flip_y (bool, optional): If True, the frame will be flipped vertically. Defaults to False.

Returns:
    VideoFrame: A new VideoFrame object in the specified format.

Raises:
    Exception: If the conversion isn't supported.

Example:
    Convert a frame from RGBA to I420 format:

    >>> frame = VideoFrame(width=1920, height=1080, type=proto_video.VideoBufferType.RGBA, data=raw_data)
    >>> converted_frame = frame.convert(proto_video.VideoBufferType.I420)
    >>> print(converted_frame.type)
    VideoBufferType.I420

Example:
    Convert a frame from BGRA to RGB24 format and flip it vertically:

    >>> frame = VideoFrame(width=1280, height=720, type=proto_video.VideoBufferType.BGRA, data=raw_data)
    >>> converted_frame = frame.convert(proto_video.VideoBufferType.RGB24, flip_y=True)
    >>> print(converted_frame.type)
    VideoBufferType.RGB24
    >>> print(converted_frame.width, converted_frame.height)
    1280 720
)proto
FfiRequestvideo_convertrU   dst_typebufferCopyFromrK   r	   instancerequesterror	Exceptionr   r9   )r   r   rU   reqresps        r   convertVideoFrame.convert   s    H  #) %)"  ))$*:*:*<=!!))#.##D..4455**4+=+=+D+DEEr   c                     SU R                    SU R                   S[        R                  R	                  U R
                  5       S3$ )Nzrtc.VideoFrame(width=z	, height=z, type=))r   r   r>   rD   Namer   r!   s    r   __repr__VideoFrame.__repr__   s@    &tzzl)DKK=P[PkPkPpPpquqzqzP{O||}~~r   _c                   ^ SSK Jn  SS KmS[        SS4U4S jjnUR	                  UR                  UR                  UR                  UR                  5       5      UR                  UR                  5       5      UR                  UR                  5       5      UR                  UR                  5       5      S.5      UR                  U5      /5      UR                  U5      UR                  U4S j5      S	9$ )
Nr   )core_schemavaluer   r   c           	      .  > [        U [        5      (       a  U $ [        U [        5      (       a  U S   n [        U [        5      (       aD  [        U S   U S   [        R
                  R                  U S   5      TR                  U S   5      S9$ [        S5      e)Nr   r   r   r   r   r-   zInvalid type for VideoFrame)	
isinstancer   tupledictr>   rD   Value	b64decode	TypeError)rm   base64s    r   validate_video_frameEVideoFrame.__get_pydantic_core_schema__.<locals>.validate_video_frame   s    %,,%''a%&&!. ?$44::5=I))%-8	  9::r   r-   c                    > U R                   U R                  [        R                  R	                  U R
                  5      TR                  U R                  5      R                  S5      S.$ )Nzutf-8r-   )	r   r   r>   rD   rg   r   	b64encoder   decode)r]   ru   s    r   <lambda>9VideoFrame.__get_pydantic_core_schema__.<locals>.<lambda>   sK    %^^&oo'77<<X]]K",,X]];BB7K	"r   )json_schemapython_schemaserialization)pydantic_corerl   ru   r   json_or_python_schemachain_schemamodel_fields_schemamodel_field
int_schema
str_schema no_info_plain_validator_function$plain_serializer_function_ser_schema)clsrj   rl   rv   ru   s       @r   __get_pydantic_core_schema__'VideoFrame.__get_pydantic_core_schema__   s    -	; 	; 	;" 00#0033%0%<%<[=S=S=U%V&1&=&=k>T>T>V&W$/$;$;K<R<R<T$U$/$;$;K<R<R<T$U	  @@AUV
 &FFG[\%JJ 1 
 	
r   )r   r   r   r   )"__name__
__module____qualname____firstlineno____doc__intr>   rD   	ValueTyper   bytesr   r)   r   propertyr   r   r   r   staticmethodOwnedVideoBufferr9   r?   rK   r   rS   boolrc   strrh   classmethodr   r   __static_attributes__ r   r   r   r      s|   %% % ))33	%
 E9j01% 
%, s      k11;;   &j & & [%A%A l  [88 ,!3 !8J+? !. NS,F//99,FFJ,F	,F\#  ,
c ,
 ,
r   r   r3   rC   rP   r   c                 f    [         R                  R                  5       nXl        Xl        X#l        U$ )N)r>   r?   ComponentInfor3   rC   rP   )r3   rC   rP   cmpts       r   _component_infor      s,     &&446DMKIKr   r   r   r   c                    U [         R                  R                  [         R                  R                  [         R                  R                  [         R                  R
                  4;   a  X-  S-  $ U [         R                  R                  :X  a  X-  S-  $ U [         R                  R                  :X  a  US-   S-  nUS-   S-  nX-  X4-  S-  -   $ U [         R                  R                  :X  a  US-   S-  nX-  S-  X3-  S-  -   $ U [         R                  R                  :X  a  US-   S-  nX-  X2-  S-  -   $ U [         R                  R                  :X  a  X-  S-  $ U [         R                  R                  :X  a  US-   S-  nUS-   S-  nX-  S-  X4-  S-  -   $ U [         R                  R                  :X  a  US-   S-  nUS-   S-  nX-  X3-  S-  -   $ [        SU  35      e)zj
Return the size in bytes of a participant video buffer type based on its size (This ignores the strides)
r<   r=   r      zunsupported video buffer type: )r>   rD   rE   rF   rG   rH   rI   I420I420AI422I444I010NV12r`   )r   r   r   chroma_widthchroma_heights        r   r/   r/     s    ##((##((##((##((	  ~!!	,,22	2~!!	,,11	1	a'!)~ <q @@@	,,22	2	a'~!L$?!$CCC	,,11	1	a'~ 5 999	,,11	1~!!	,,11	1	a'!)~!L$@1$DDD	,,11	1	a'!)~ ;a ???
5dV<
==r   rJ   c                    U[         R                  R                  :X  an  US-   S-  nUS-   S-  n[        XX#-  5      n[        UR                  UR
                  -   XDU-  5      n[        UR                  UR
                  -   XDU-  5      nXgU/$ U[         R                  R                  :X  a  US-   S-  nUS-   S-  n[        XX#-  5      n[        UR                  UR
                  -   XDU-  5      n[        UR                  UR
                  -   XDU-  5      n[        UR                  UR
                  -   X"U-  5      n	XgX/$ U[         R                  R                  :X  as  US-   S-  n[        XX#-  5      n[        UR                  UR
                  -   XDU-  5      n[        UR                  UR
                  -   UR
                  -   XDU-  5      nXgU/$ U[         R                  R                  :X  a^  [        XX#-  5      n[        UR                  UR
                  -   X"U-  5      n[        UR                  UR
                  -   X"U-  5      nXgU/$ U[         R                  R                  :X  a  US-   S-  nUS-   S-  n[        XS-  X#-  S-  5      n[        UR                  UR
                  -   US-  XE-  S-  5      n[        UR                  UR
                  -   US-  XE-  S-  5      nXgU/$ U[         R                  R                  :X  aM  US-   S-  nUS-   S-  n[        XX#-  5      n[        UR                  UR
                  -   US-  XE-  S-  5      n
Xj/$ / $ )Nr   r   )r>   rD   r   r   r3   rP   r   r   r   r   r   )rJ   r   r   r   r   r   yuvauvs              r   rB   rB   +  s*    {**///	a'!)D8AJJ/m?[\AJJ/m?[\ay	,,22	2	a'!)D8AJJ/m?[\AJJ/m?[\AJJ/Ga|	,,11	1	a'D8AJJ/f?TUAJJ/!&&8,W]H]^ay	,,11	1D8AJJ/GAJJ/Gay	,,11	1	a'!)D!)U^a-?@AJJ/1A<C_bcCcdAJJ/1A<C_bcCcday	,,11	1	a'!)D8JJ!1<3ORS3S
 wIr   )r0   typingr   _protor   r>   r   rW   r   r   _ffi_clientr	   r
   _utilsr   r   r   r   r?   r   r   rD   r   r/   rB   r   r   r   <module>r      s      2 $ ! -  _
 _
D&)  ..">K77AA "># ">WZ ">_b ">J-
- 00::-CF-PS-	+
%
%
3
34-r   