
    ePi'                        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dS )    N)PyBuffer_FillInfoPyBytes_FromString)int64_tuint64_t)memcpystrlenc                       e Zd Zd ZdS )SubtitleProxyc                 h    t                               t          j        | j                             d S N)libavsubtitle_freecythonaddressstructselfs    i/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/av/subtitles/subtitle.py__dealloc__zSubtitleProxy.__dealloc__	   s(    FN4;7788888    N)__name__
__module____qualname__r    r   r   r
   r
      s#        9 9 9 9 9r   r
   c                   X   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dS )SubtitleSetzm
    A :class:`SubtitleSet` can contain many :class:`Subtitle` objects.

    Wraps :ffmpeg:`AVSubtitle`.
    Nc                      |t           u rt                       _        d _        d S t	          |t                    rE| _        t           fdt           j        j        j                  D                        _        d S t          d          )Nr   c              3   8   K   | ]}t          |          V  d S r   )build_subtitle.0ir   s     r   	<genexpr>z(SubtitleSet.__cinit__.<locals>.<genexpr>    s>        ,-tQ''     r   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__   s     666&DJDJJJ)=99 		*DJ    16tz7H7R1S1S    DJJJ R  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                              t          j        t          j        t          j                                               |j        j        _        |j        j        j        t          j        k    rt%          d          t          j
        t          j        t          j                 t                              t          j        t          j                                      }|t          j        k    rSt                              |j        j        j                   t          j        |j        j        _        t%          d          ||j        j        j        d<   d|_        d|_        d|_        d|_        d|_        t          j        |_        t          j        |_        d|_        t;          |           }t          j
        t          j        t                              |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
        r.   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_timer2   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/   r0   r1   r2   r3   subsetrecttext_lens           r   createzSubtitleSet.create(   s.   6 **@AA%4"16./2,"%()%$*KN6>#*<=>NN6=8J)KLLMM%
 %
! <$33GHHH39;N3-.NN6=);<<==4
 4
 6;KK+1222(.FL%@AAA'+!!$$	K	
&)$ii;v}cmmHqL.I.IJJ8v{""@AAAtxV]D998DD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   s8    X)XXDN,CXX2d88XXXX	
r   c                 $    | j         j        j        S r   r%   r   r6   r   s    r   r6   zSubtitleSet.formatv   s    z ''r   valuec                 (    || j         j        _        d S r   r[   r   r\   s     r   r6   zSubtitleSet.formatz   s    #(
   r   c                 $    | j         j        j        S r   r%   r   r7   r   s    r   r7   zSubtitleSet.start_display_time~   s    z 33r   c                 (    || j         j        _        d S r   r`   r^   s     r   r7   zSubtitleSet.start_display_time   s    /4
,,,r   c                 $    | j         j        j        S r   r%   r   r8   r   s    r   r8   zSubtitleSet.end_display_time   s    z 11r   c                 (    || j         j        _        d S r   rc   r^   s     r   r8   zSubtitleSet.end_display_time   s    -2
***r   c                 $    | j         j        j        S )z$Same as packet pts, in av.time_base.r%   r   r2   r   s    r   r2   zSubtitleSet.pts   s     z $$r   c                 (    || j         j        _        d S r   rf   r^   s     r   r2   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    DJr   c                     | j         |         S r   )r&   r   r"   s     r   __getitem__zSubtitleSet.__getitem__   s    z!}r   r   )r   r.   )r   r   r   __doc__r-   staticmethodbytesintr   rQ   rY   propertyr6   setterr7   r8   r2   ri   rl   ro   r   r   r   r   r      s             
  F FFF F 	F
 F 
F F F \FP
 
 

 ( ( X( ])C ) ) ) ]) 4 4 X4 5 5 5 5 5 2 2 X2 3c 3 3 3 3 % % X% 	Z& & & & Z&           r   r   subtitleindexr4   c                    |dk     s2t          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     qyyFKU33x~7L7VVV;<<<.6n.C.I%.PC
x3&&&h...
x3###sx33D'D'D8U+++
/#(:
;
;;r   c                   2    e Zd ZdZdedej        fdZd ZdS )Subtitlezg
    An abstract base class for each concrete type of subtitle.
    Wraps :ffmpeg:`AVSubtitleRect`
    rv   rw   c                 0   |dk     s2t          j        t           j        |          |j        j        j        k    rt          d          |j        | _        | j        j        j        |         | _        | j        j	        t          j        k    r	d| _	        d S | j        j	        t          j        k    r	d| _	        d S | j        j	        t          j        k    r	d| _	        d S | j        j	        t          j        k    r	d| _	        d S 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    AII{6;..(.2G2QQQ?@@@^
:$*518=C---DIIIX]c111!DIIIX]c///DIIIX]c...DIIIGdhmGGHHHr   c                 D    d| j         j         dt          |           ddS )Nz<av.rU   rA   rV   )rW   r   rX   r   s    r   rY   zSubtitle.__repr__   s)    Bdn-BBRXXBBBBBr   N)	r   r   r   rp   r   r   rs   r-   rY   r   r   r   r   r      s\         
I+ Ifj I I I I(C C C C 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dS )r}   rv   rw   c                 b     t           fdt          d          D                        _        d S )Nc              3   \   K   | ]&}j         j        |         t          |          V  'd S r   )r   linesizeBitmapSubtitlePlaner    s     r   r#   z+BitmapSubtitle.__cinit__.<locals>.<genexpr>   sQ       
 
-.dh>OPQ>R
a((
 
 
 
 
 
r      )r(   r)   planesr   s   `  r   r-   zBitmapSubtitle.__cinit__   sD     
 
 
 
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__   s    S) S SDN,C S SzS S KS S-1VS S6:fS SEGXXRS S S	
r   c                     | j         j        S r   )r   rA   r   s    r   rA   zBitmapSubtitle.x       xz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    x!!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    DK   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        
+ 
fj 
 
 
 


 
 
   X   X   X   X " " X"     ! ! !    r   r}   c                   X    e Zd Zdedej        fdZdej        e         dej        fdZ	dS )r   rv   rw   c                 2   |dk    rt          d          |j        j        |         st          d          || _        || _        |j        j        |j        j        z  | _        t          j	        t          j
        |j        j        |                   | _        d S )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B|$U+ 	53444 
#<>HLN:{6=(,2CE2JKKr   viewrH   c                 B    t          || | j        | j        d|           d S )Nr   )r   r   r   )r   r   rH   s      r   __getbuffer__z!BitmapSubtitlePlane.__getbuffer__  s%    $dlD4DaOOOOOr   N)
r   r   r   r}   r   rs   r-   r:   	Py_bufferr   r   r   r   r   r      sq        	L. 	L 	L 	L 	L 	LP&."; PFJ P P P P P Pr   r   c                   Z    e Zd ZdZd Zed             Zed             Zed             ZdS )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&    F$-FFDFFFFFr   c                 h    | j         j        t          j        urt	          | j         j                  S dS )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,    
 8<v{**%dhl333sr   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     "||k     r||         }|dz   |k    rdn
||dz            }|dk    r!|dk    r|                    d           |d	z  }K|s%|d
k    r	|dk    rd}n|                    |           n|dk    rd}|dz  }||k     t          |          S )zK
        Extract the dialogue from the ass format. Strip comments.
        r   F   ,   r.   \   N   
      {   T}   )rL   r   declarechar	bytearrayrI   appendrr   )	r   comma_countr"   stateass_textr   	next_charresultrP   s	            r   r   zAssSubtitle.dialogue"  sV   
 %&"( .55i%KK&)(mmAoo!h,,{g%%q FA Aoo!h,,
 (llA;DUh..HQUOIxI$8$8h'''Q 7??yH'<'< EEMM$''''FA! (ll$ V}}r   c                 h    | j         j        t          j        urt	          | j         j                  S dS )zN
        Rarely used attribute. You're probably looking for dialogue.
        r   )r   r/   r   r>   r   r   s    r   r/   zAssSubtitle.textH  s,    
 8=++%dhm444sr   N)	r   r   r   rp   rY   rt   rL   r   r/   r   r   r   rM   rM     s         G G G   X # # X#J   X  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 I I I I I I I 9 9 9 9 9 9 9 9 6 6 6 6 6 6 6 6 9 9 9 9 9 9 9 9
    M M M M M M M M` <[ < < < < < <$ C C C C C C C C< ' ' ' ' 'X ' ' 'T P P P P P P P P" > > > > >( > > > > >r   