o
    ]i'                     @   s   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 dZe Ze j
G dd dZe jd	ed
e jdefddZe j
G dd dZe j
G dd deZe j
G dd dZe j
G dd deZdS )    N)PyBuffer_FillInfoPyBytes_FromString)int64_tuint64_t)memcpystrlenc                   @   s   e Zd Zdd ZdS )SubtitleProxyc                 C   s   t t| j d S N)libavsubtitle_freecythonaddressstructself r   I/home/ubuntu/.local/lib/python3.10/site-packages/av/subtitles/subtitle.py__dealloc__	   s   zSubtitleProxy.__dealloc__N)__name__
__module____qualname__r   r   r   r   r   r      s    r   c                   @   s   e Zd ZdZd$ddZe		d%deded	ed
ededefddZ	dd Z
edd ZejdefddZedd ZejdefddZedd ZejdefddZedd ZejdefddZdd Zd d! Zd"d# ZdS )&SubtitleSetzm
    A :class:`SubtitleSet` can contain many :class:`Subtitle` objects.

    Wraps :ffmpeg:`AVSubtitle`.
    Nc                    sX   |t u rt  _d _d S t|tr(| _t fddt jjjD  _d S t	d)Nr   c                 3   s    | ]}t  |V  qd S r	   )build_subtitle.0ir   r   r   	<genexpr>    s    

z(SubtitleSet.__cinit__.<locals>.<genexpr>z@SubtitleSet requires a SubtitleProxy or use SubtitleSet.create())
_cinit_bypass_sentinelr   proxyrects
isinstancetupleranger   	num_rects	TypeError)r   proxy_or_sentinelr   r   r   	__cinit__   s   

zSubtitleSet.__cinit__r      textstartendptssubtitle_formatreturnc              	   C   s  t t}||jj_||jj_||jj_||jj_d|jj_t	
t	jt	jtj  tt	t	jtj |jj_|jjjt	jkrCtdt	
t	jtj tt	tj}|t	jkrkt|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	
t	jt|d |_ |j t	jkrtdt!|j t	
t	j| | d|j |< t"|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_timer+   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)   r*   r+   r,   subsetrecttext_lenr   r   r   create(   sJ   








zSubtitleSet.createc                 C   s&   d| j j d| j j dt| ddS )N<. at 0xr9   >)	__class__r   r   idr   r   r   r   __repr__q   s   $zSubtitleSet.__repr__c                 C   
   | j jjS r	   r   r   r.   r   r   r   r   r.   v      
zSubtitleSet.formatvaluec                 C      || j j_d S r	   rR   r   rT   r   r   r   r.   z      c                 C   rQ   r	   r   r   r/   r   r   r   r   r/   ~   rS   zSubtitleSet.start_display_timec                 C   rU   r	   rX   rV   r   r   r   r/      rW   c                 C   rQ   r	   r   r   r0   r   r   r   r   r0      rS   zSubtitleSet.end_display_timec                 C   rU   r	   rY   rV   r   r   r   r0      rW   c                 C   rQ   )z$Same as packet pts, in av.time_base.r   r   r+   r   r   r   r   r+      s   
zSubtitleSet.ptsc                 C   rU   r	   rZ   rV   r   r   r   r+      rW   c                 C   
   t | jS r	   )rA   r   r   r   r   r   __len__      
zSubtitleSet.__len__c                 C   r[   r	   )iterr   r   r   r   r   __iter__   r]   zSubtitleSet.__iter__c                 C   
   | j | S r	   )r   r   r   r   r   r   __getitem__   r]   zSubtitleSet.__getitem__r	   )r   r'   )r   r   r   __doc__r&   staticmethodbytesintr   rI   rP   propertyr.   setterr/   r0   r+   r\   r_   rb   r   r   r   r   r      sN    
H



r   subtitleindexr-   c                 C   s|   |dk st t j|| jjjkrtd| jjj| }|jt	j
kr&t| |S |jt	jks2|jt	jkr7t| |S td|j )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   r1   uintr   r   r#   
ValueErrorr   r?   r
   SUBTITLE_BITMAPBitmapSubtitler>   SUBTITLE_TEXTrE   )ri   rj   ptrr   r   r   r      s    

r   c                   @   s,   e Zd ZdZdedejfddZdd ZdS )	Subtitlezg
    An abstract base class for each concrete type of subtitle.
    Wraps :ffmpeg:`AVSubtitleRect`
    ri   rj   c                 C   s   |dk st 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r8d| _	d S | jj	t
jkrDd| _	d S | jj	t
jkrPd| _	d S td| jj	)Nr   rk   s   nones   bitmaps   texts   asszunknown subtitle type )r   r1   rl   r   r   r#   rm   r   rq   r?   r
   SUBTITLE_NONErn   rp   r>   r   ri   rj   r   r   r   r&      s   



zSubtitle.__cinit__c                 C   s   d| j j dt| ddS )Nz<av.rL   r9   rM   )rN   r   rO   r   r   r   r   rP      s   zSubtitle.__repr__N)	r   r   r   rc   r   r   rf   r&   rP   r   r   r   r   rr      s    rr   c                   @   s|   e Zd ZdedejfddZdd Zedd Z	ed	d
 Z
edd Zedd Zedd Zdd Zdd Zdd ZdS )ro   ri   rj   c                    s    t  fddtdD  _d S )Nc                 3   s&    | ]} j j| rt |V  qd S r	   )rq   linesizeBitmapSubtitlePlaner   r   r   r   r      s    
z+BitmapSubtitle.__cinit__.<locals>.<genexpr>   )r!   r"   planesrt   r   r   r   r&      s   zBitmapSubtitle.__cinit__c                 C   sF   d| j j d| j j d| j d| j d| j d| j dt| ddS )	NrJ   rK    r9   z at ,z; at 0xrM   )rN   r   r   widthheightr9   r:   rO   r   r   r   r   rP      s   zBitmapSubtitle.__repr__c                 C      | j jS r	   )rq   r9   r   r   r   r   r9         zBitmapSubtitle.xc                 C   r}   r	   )rq   r:   r   r   r   r   r:      r~   zBitmapSubtitle.yc                 C   r}   r	   )rq   r;   r   r   r   r   r{      r~   zBitmapSubtitle.widthc                 C   r}   r	   )rq   r<   r   r   r   r   r|      r~   zBitmapSubtitle.heightc                 C   r}   r	   )rq   r=   r   r   r   r   r=      r~   zBitmapSubtitle.nb_colorsc                 C   r[   r	   )rA   rx   r   r   r   r   r\      r]   zBitmapSubtitle.__len__c                 C   r[   r	   )r^   rx   r   r   r   r   r_      r]   zBitmapSubtitle.__iter__c                 C   r`   r	   )rx   ra   r   r   r   rb      r]   zBitmapSubtitle.__getitem__N)r   r   r   r   r   rf   r&   rP   rg   r9   r:   r{   r|   r=   r\   r_   rb   r   r   r   r   ro      s     




ro   c                   @   s:   e Zd ZdedejfddZdeje dejfddZ	d	S )
rv   ri   rj   c                 C   s^   |dkrt d|jj| st d|| _|| _|jj|jj | _t	tj
|jj| | _d S )Nrw   z"BitmapSubtitles have only 4 planeszplane does not exist)rm   rq   ru   ri   rj   r;   r<   buffer_sizer   r1   p_voiddata_bufferrt   r   r   r   r&      s   zBitmapSubtitlePlane.__cinit__viewr@   c                 C   s   t || | j| jd| d S )Nr   )r   r   r   )r   r   r@   r   r   r   __getbuffer__     z!BitmapSubtitlePlane.__getbuffer__N)
r   r   r   ro   r   rf   r&   r2   	Py_bufferr   r   r   r   r   rv      s    rv   c                   @   s<   e Zd ZdZdd Zedd Zedd Zedd	 Zd
S )rE   zY
    Represents an ASS/Text subtitle format, as opposed to a bitmap Subtitle format.
    c                 C   s   d| j dt| ddS )Nz<av.AssSubtitle rL   r9   rM   )dialoguerO   r   r   r   r   rP     r   zAssSubtitle.__repr__c                 C      | 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.
            )rq   rD   r   r6   r   r   r   r   r   rD        zAssSubtitle.assc           	      C   s  d}d}d}| j }ttj\}}t }t|}|dk r6||k r6|| dkr*|d7 }|d7 }|dk r6||k s ||k r|| }|d |krFdn||d  }|dkr^|dkr^|d |d	7 }q6|sq|d
krk|dkrkd}n|| n|dkrwd}|d7 }||k s:t|S )zK
        Extract the dialogue from the ass format. Strip comments.
        r   F   ,   r'   \   N   
      {   T}   )rD   r   declarechar	bytearrayrA   appendre   )	r   comma_countr   stateass_textr   	next_charresultrH   r   r   r   r   "  s8   
zAssSubtitle.dialoguec                 C   r   )zN
        Rarely used attribute. You're probably looking for dialogue.
        r   )rq   r(   r   r6   r   r   r   r   r   r(   H  r   zAssSubtitle.textN)	r   r   r   rc   rP   rg   rD   r   r(   r   r   r   r   rE     s    

%rE   )r   cython.cimports.cpythonr   r   cython.cimports.libc.stdintr   r   cython.cimports.libc.stringr   r   cclassr   objectr   r   cfuncrf   rr   r   ro   rv   rE   r   r   r   r   <module>   s(     *