o
    2jr                     @   s   d dl Z d dlmZ d dlmZ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 d d	lmZ e je jG d
d deZdS )    N)libav)
ByteSource
bytesource)CodecContext)	err_check)Packet)SubtitleProxySubtitleSet)PyBytes_FromStringAndSize)memcpyc                   @   s   e Zd ZededB fddZejdedB ddfddZdddZd	ede	fd
dZ
ejdde	dB fddZejde	fddZdS )SubtitleCodecContextreturnNc                 C   s:   | j jtjks| j jdkrdS tttj| j j| j jS )zAGet the subtitle header data (ASS/SSA format for text subtitles).r   N)ptrsubtitle_headercythonNULLsubtitle_header_sizer
   castp_charself r   T/var/www/html/whisper/venv/lib/python3.10/site-packages/av/subtitles/codeccontext.pyr      s   z$SubtitleCodecContext.subtitle_headerdatac                 C   s   |du rt t| jj d| j_n.t|}ttj	t 
| jj|jt j | j_| jjs2tdt| jj|j|j |j| j_d| _dS )zSet the subtitle header data.Nr   zCannot allocate subtitle_headerT)libav_freepr   addressr   r   r   r   r   p_uchar
av_realloclengthAV_INPUT_BUFFER_PADDING_SIZEMemoryErrorr   subtitle_header_set)r   r   sourcer   r   r   r      s    



c                 C   s,   | j r| jrtt| j j d S d S d S N)r   r"   r   r   r   r   r   r   r   r   r   __dealloc__2   s   z SubtitleCodecContext.__dealloc__subtitlec                 C   s   | j jstd| jdd ttjtd}|tj	kr t
 t| j|dt|jj}|dk r<t| t|d t|}t|jj|| t| |jjj|j_|jjj|j_|jjj|jjj |j_|S )z
        Encode a SubtitleSet into a Packet.

        Args:
            subtitle: The SubtitleSet to encode

        Returns:
            A Packet containing the encoded subtitle data
        z Cannot encode with unknown codecFstricti   r   zavcodec_encode_subtitle())codecr   
ValueErroropenr   r   r   r   	av_mallocr   r!   avcodec_encode_subtitler   proxystructav_freer   r   r   r   ptsdtsend_display_timestart_display_timeduration)r   r&   bufretpacketr   r   r   encode_subtitle6   s2   




z$SubtitleCodecContext.encode_subtitler8   c                 C   sp   | j jstd|du rtd| jdd t }d}tt| jt	
|jt	
||j |r6tt|S g S )zDecode a subtitle packet, returning a list of :class:`.Subtitle` objects
        if a subtitle was decoded, or an empty list otherwise.cannot decode unknown codecNzpacket cannot be NoneFr'   r   )r)   r   r*   RuntimeErrorr+   r   r   r   avcodec_decode_subtitle2r   r   r/   listr	   r   r8   r.   	got_framer   r   r   decode_   s$   
	zSubtitleCodecContext.decodec                 C   s\   | j jstd| jdd t }d}tt| jt	|j
t	||j |r,t|S dS )z<
        Returns SubtitleSet if you really need it.
        r:   Fr'   r   N)r)   r   r*   r+   r   r   r   r<   r   r   r/   r	   r>   r   r   r   decode2z   s    
	zSubtitleCodecContext.decode2)r   Nr$   )__name__
__module____qualname__propertybytesr   setterr%   r	   r   r9   r   ccallr@   rA   r   r   r   r   r      s    
)r   )r   cython.cimportsr   r   cython.cimports.av.bufferr   r    cython.cimports.av.codec.contextr   cython.cimports.av.errorr   cython.cimports.av.packetr   %cython.cimports.av.subtitles.subtitler   r	   cython.cimports.cpython.bytesr
   cython.cimports.libc.stringr   finalcclassr   r   r   r   r   <module>   s    