
    Hui'                        d dl Z d dlmZmZ d dlmZmZ d dlmZm	Z	 e j                   G d d             Z e       Ze j                   G d d             Ze j                  d	ed
e j                   defd       Ze j                   G d d             Ze j                   G d de             Ze j                   G d d             Ze j                   G d de             Zy)    N)PyBuffer_FillInfoPyBytes_FromString)int64_tuint64_t)memcpystrlenc                       e Zd Zd Zy)SubtitleProxyc                 h    t         j                  t        j                  | j                               y N)libavsubtitle_freecythonaddressstructselfs    G/opt/pipecat/venv/lib/python3.12/site-packages/av/subtitles/subtitle.py__dealloc__zSubtitleProxy.__dealloc__	   s    FNN4;;78    N)__name__
__module____qualname__r    r   r   r
   r
      s    9r   r
   c                   H   e Zd ZdZddZe	 	 ddededededed	efd
       Z	d Z
ed        Zej                  defd       Zed        Zej                  defd       Zed        Zej                  defd       Zed        Zej                  defd       Zd Zd Zd Zy)SubtitleSetzm
    A :class:`SubtitleSet` can contain many :class:`Subtitle` objects.

    Wraps :ffmpeg:`AVSubtitle`.
    Nc                     |t         u rt                _        d _        y t	        |t              rH| _        t         fdt         j                  j                  j                        D               _        y t        d      )Nr   c              3   6   K   | ]  }t        |        y wr   )build_subtitle.0ir   s     r   	<genexpr>z(SubtitleSet.__cinit__.<locals>.<genexpr>    s      ,-tQ's   z@SubtitleSet requires a SubtitleProxy or use SubtitleSet.create())
_cinit_bypass_sentinelr
   proxyrects
isinstancetupleranger   	num_rects	TypeError)r   proxy_or_sentinels   ` r   	__cinit__zSubtitleSet.__cinit__   sn     66&DJDJ)=9*DJ 16tzz7H7H7R7R1S DJ R r   textstartendptssubtitle_formatreturnc           	      >   t        t              }||j                  j                  _        ||j                  j                  _        ||j                  j                  _        ||j                  j                  _        d|j                  j                  _        t        j                  t        j                  t        j                  t        j                        t        j                  t        j                  t        j                  t        j                                       |j                  j                  _        |j                  j                  j                   t        j"                  k(  rt%        d      t        j                  t        j                  t        j                     t        j                  t        j                  t        j                                    }|t        j"                  k(  rgt        j'                  |j                  j                  j                          t        j"                  |j                  j                  _        t%        d      ||j                  j                  j                   d<   d|_        d|_        d|_        d|_        d|_        t        j2                  |_        t        j"                  |_        d|_        t;        |       }t        j                  t        j<                  t        j?                  |dz               |_         |j@                  t        j"                  k(  rt%        d      tC        |j@                  t        j                  t        j<                  |       |       d|j@                  |<   tE        |d      f|_        |S )a  
        Create a SubtitleSet for encoding.

        Args:
            text: The subtitle text in ASS dialogue format
                  (e.g. b"0,0,Default,,0,0,0,,Hello World")
            start: Start display time as offset from pts (typically 0)
            end: End display time as offset from pts (i.e., duration)
            pts: Presentation timestamp in stream time_base units
            subtitle_format: Subtitle format (default 1 for text)

        Note:
            All timing values should be in stream time_base units.
            For MKV (time_base=1/1000), units are milliseconds.
            For MP4 (time_base=1/1000000), units are microseconds.

        Returns:
            A SubtitleSet ready for encoding
           z'Failed to allocate subtitle rects arrayz Failed to allocate subtitle rectr   z Failed to allocate subtitle text)#r   r$   r%   r   formatstart_display_timeend_display_timer1   r*   r   castpointerr   AVSubtitleRect
av_malloczsizeofr&   NULLMemoryErrorav_freexywh	nb_colorsSUBTITLE_ASStyper.   flagslenp_char	av_mallocassr   AssSubtitle)r.   r/   r0   r1   r2   subsetrecttext_lens           r   createzSubtitleSet.create(   s[   6 **@A%4"16./2,"%()%$*KKNN6>>#*<*<=>NN6==8J8J)KLM%
! <<$$3GHH39;;NN3--.NN6==););<=4
 6;;KK++112(.FLL%@AA'+!!!$$$	KK	
&)$i;;v}}cmmHqL.IJ88v{{"@AAtxxV]]D98D#FA.0r   c                 ~    d| j                   j                   d| j                   j                   dt        |       ddS )N<. at 0xrA   >)	__class__r   r   idr   s    r   __repr__zSubtitleSet.__repr__q   s=    ))*!DNN,C,C+DF2d8TU,VWX	
r   c                 B    | j                   j                  j                  S r   r%   r   r6   r   s    r   r6   zSubtitleSet.formatv   s    zz  '''r   valuec                 :    || j                   j                  _        y r   r[   r   r\   s     r   r6   zSubtitleSet.formatz   s    #(

 r   c                 B    | j                   j                  j                  S r   r%   r   r7   r   s    r   r7   zSubtitleSet.start_display_time~   s    zz  333r   c                 :    || j                   j                  _        y r   r`   r^   s     r   r7   zSubtitleSet.start_display_time   s    /4

,r   c                 B    | j                   j                  j                  S r   r%   r   r8   r   s    r   r8   zSubtitleSet.end_display_time   s    zz  111r   c                 :    || j                   j                  _        y r   rc   r^   s     r   r8   zSubtitleSet.end_display_time   s    -2

*r   c                 B    | j                   j                  j                  S )z$Same as packet pts, in av.time_base.r%   r   r1   r   s    r   r1   zSubtitleSet.pts   s     zz  $$$r   c                 :    || j                   j                  _        y r   rf   r^   s     r   r1   zSubtitleSet.pts   s     %

r   c                 ,    t        | j                        S r   )rI   r&   r   s    r   __len__zSubtitleSet.__len__   s    4::r   c                 ,    t        | j                        S r   )iterr&   r   s    r   __iter__zSubtitleSet.__iter__   s    DJJr   c                      | j                   |   S r   )r&   r   r"   s     r   __getitem__zSubtitleSet.__getitem__   s    zz!}r   r   )r   r5   )r   r   r   __doc__r-   staticmethodbytesintr   rQ   rY   propertyr6   setterr7   r8   r1   ri   rl   ro   r   r   r   r   r      sM     
  FFF F 	F
 F 
F FP

 ( ( ]])C ) ) 4 4 5 5 5 2 2 3c 3 3 % % 	ZZ& & & r   r   subtitleindexr3   c                     |dk  sEt        j                  t         j                  |      | j                  j                  j
                  k\  rt        d      | j                  j                  j                  |   }|j                  t        j                  k(  rt        | |      S |j                  t        j                  k(  s|j                  t        j                  k(  rt        | |      S t        d|j                  z        )zBuild an av.Stream for an existing AVStream.

    The AVStream MUST be fully constructed and ready for use before this is called.
    r    subtitle rect index out of rangezunknown subtitle type %r)r   r9   uintr%   r   r*   
ValueErrorr&   rG   r   SUBTITLE_BITMAPBitmapSubtitlerF   SUBTITLE_TEXTrM   )rv   rw   ptrs      r   r   r      s     qyFKKU3x~~7L7L7V7VV;<<.6nn.C.C.I.I%.PC
xx3&&&h..
xx3###sxx33D3D'D8U++
/#((:
;;r   c                   :    e Zd ZdZdedej                  fdZd Zy)Subtitlezg
    An abstract base class for each concrete type of subtitle.
    Wraps :ffmpeg:`AVSubtitleRect`
    rv   rw   c                    |dk  sEt        j                  t         j                  |      |j                  j                  j
                  k\  rt        d      |j                  | _        | j                  j                  j                  |   | _        | j                  j                  t        j                  k(  rd| _	        y | j                  j                  t        j                  k(  rd| _	        y | j                  j                  t        j                  k(  rd| _	        y | j                  j                  t        j                  k(  rd| _	        y t        d| j                  j                        )Nr   ry   s   nones   bitmaps   texts   asszunknown subtitle type )r   r9   rz   r%   r   r*   r{   r&   r   rG   r   SUBTITLE_NONEr|   r~   rF   r   rv   rw   s      r   r-   zSubtitle.__cinit__   s    AI{{6;;.(..2G2G2Q2QQ?@@^^
::$$**5188==C---DIXX]]c111!DIXX]]c///DIXX]]c...DI5dhhmm5FGHHr   c                 P    d| j                   j                   dt        |       ddS )Nz<av.rU   rA   rV   )rW   r   rX   r   s    r   rY   zSubtitle.__repr__   s(    dnn--.fRXaLBBr   N)	r   r   r   rp   r   r   rs   r-   rY   r   r   r   r   r      s'    
I+ Ifjj I(Cr   r   c                       e Zd Zdedej
                  fdZd Zed        Z	ed        Z
ed        Zed        Zed	        Zd
 Zd Zd Zy)r}   rv   rw   c                 J     t         fdt        d      D               _        y )Nc              3   j   K   | ]*  }j                   j                  |   st        |       , y wr   )r   linesizeBitmapSubtitlePlaner    s     r   r#   z+BitmapSubtitle.__cinit__.<locals>.<genexpr>   s0      
-.dhh>O>OPQ>Ra(
s   33   )r(   r)   planesr   s   `  r   r-   zBitmapSubtitle.__cinit__   s      
27(
 
r   c                     d| j                   j                   d| j                   j                   d| j                   d| j                   d| j
                   d| j                   dt        |       ddS )	NrS   rT    rA   z at ,z; at 0xrV   )rW   r   r   widthheightrA   rB   rX   r   s    r   rY   zBitmapSubtitle.__repr__   sh    ))*!DNN,C,C+DAzzl!DKK=TVVHAdffXWRXaLPQS	
r   c                 .    | j                   j                  S r   )r   rA   r   s    r   rA   zBitmapSubtitle.x       xxzzr   c                 .    | j                   j                  S r   )r   rB   r   s    r   rB   zBitmapSubtitle.y   r   r   c                 .    | j                   j                  S r   )r   rC   r   s    r   r   zBitmapSubtitle.width   r   r   c                 .    | j                   j                  S r   )r   rD   r   s    r   r   zBitmapSubtitle.height   r   r   c                 .    | j                   j                  S r   )r   rE   r   s    r   rE   zBitmapSubtitle.nb_colors   s    xx!!!r   c                 ,    t        | j                        S r   )rI   r   r   s    r   ri   zBitmapSubtitle.__len__   s    4;;r   c                 ,    t        | j                        S r   )rk   r   r   s    r   rl   zBitmapSubtitle.__iter__   s    DKK  r   c                      | j                   |   S r   )r   rn   s     r   ro   zBitmapSubtitle.__getitem__   s    {{1~r   N)r   r   r   r   r   rs   r-   rY   rt   rA   rB   r   r   rE   ri   rl   ro   r   r   r   r}   r}      s    
+ 
fjj 


         " " !r   r}   c                   n    e Zd Zdedej
                  fdZdej                  e   dej
                  fdZ	y)r   rv   rw   c                 l   |dk\  rt        d      |j                  j                  |   st        d      || _        || _        |j                  j
                  |j                  j                  z  | _        t        j                  t        j                  |j                  j                  |         | _        y )Nr   z"BitmapSubtitles have only 4 planeszplane does not exist)r{   r   r   rv   rw   rC   rD   buffer_sizer   r9   p_voiddata_bufferr   s      r   r-   zBitmapSubtitlePlane.__cinit__   s    A:ABB||$$U+344 
#<<>>HLLNN:{{6==(,,2C2CE2JKr   viewrH   c                 L    t        || | j                  | j                  d|       y )Nr   )r   r   r   )r   r   rH   s      r   __getbuffer__z!BitmapSubtitlePlane.__getbuffer__  s    $dllD4D4DaOr   N)
r   r   r   r}   r   rs   r-   r:   	Py_bufferr   r   r   r   r   r      s?    	L. 	L 	LP&.."; PFJJ Pr   r   c                   F    e Zd ZdZd Zed        Zed        Zed        Zy)rM   zY
    Represents an ASS/Text subtitle format, as opposed to a bitmap Subtitle format.
    c                 <    d| j                   dt        |       ddS )Nz<av.AssSubtitle rU   rA   rV   )dialoguerX   r   s    r   rY   zAssSubtitle.__repr__  s"    !$--!2&D!AFFr   c                     | j                   j                  t        j                  urt	        | j                   j                        S y)zl
        Returns the subtitle in the ASS/SSA format. Used by the vast majority of subtitle formats.
        r   )r   rL   r   r>   r   r   s    r   rL   zAssSubtitle.ass  s.    
 88<<v{{*%dhhll33r   c                    d}d}d}| j                   }t        j                  t        j                        \  }}t	               }t        |      }|dk  r"||k  r||   dk(  r|dz  }|dz  }|dk  r||k  r||k  rk||   }|dz   |k\  rdn||dz      }|dk(  r|dk(  r|j                  d       |d	z  }=|s|d
k(  r|dk7  rd}n|j                  |       n|dk(  rd}|dz  }||k  rkt        |      S )zK
        Extract the dialogue from the ass format. Strip comments.
        r   F   ,   r5   \   N   
      {   T}   )rL   r   declarechar	bytearrayrI   appendrr   )	r   comma_countr"   stateass_textr   	next_charresultrP   s	            r   r   zAssSubtitle.dialogue"  s   
 %&"(( ..5i%K&)(mAo!h,{g%q FA Ao!h,
 (lA;DUh.HQUOIxI$8h'Q7?yH'< EMM$'FA! (l$ V}r   c                     | j                   j                  t        j                  urt	        | j                   j                        S y)zN
        Rarely used attribute. You're probably looking for dialogue.
        r   )r   r.   r   r>   r   r   s    r   r.   zAssSubtitle.textH  s.    
 88==+%dhhmm44r   N)	r   r   r   rp   rY   rt   rL   r   r.   r   r   r   rM   rM     sJ    G   # #J  r   rM   )r   cython.cimports.cpythonr   r   cython.cimports.libc.stdintr   r   cython.cimports.libc.stringr   r   cclassr
   objectr$   r   cfuncrs   r   r   r}   r   rM   r   r   r   <module>r      s	    I 9 6 9 9 9
    M M M` <[ < < < <$ C C C< 'X ' 'T P P P" >( > >r   