o
    2j /                     @   s   d dl 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 d dlmZmZ d dlmZ d d	lmZmZ d d
lmZmZ e jdefddZe je jG dd deZdS )    N)CodecContextwrap_codec_context)StreamContainer)
Dictionary)	err_check)Packet)Streamwrap_stream)avdict_to_dict)int64_tuint8_t)freemallocselfc                 C   sn   t  | _tj& | jd@ r%tt| j |  jdM  _W d    d S W d    d S 1 s0w   Y  d S )N   )	r   streamscythonnogil_myflaglibavformat_close_inputaddressptrr    r   M/var/www/html/whisper/venv/lib/python3.10/site-packages/av/container/input.pyclose_input   s   
"r   c                   @   s   e Zd Zdd Z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ddddddejdejdedB dejdejf
ddZejdd ZdS ) InputContainerc                 O   s<  t j}| jj}| jr|dkrtd| js| jrs|dkrst| j}t t j	t j	t
j  t|t t j }t|D ]8}t j||< |t| jk re| jre| }|| j|  t
t || |jd q:t
t || |jd q:| | j |   t j t
| j|}W d    n1 sw   Y  | d  | | |rt|D ]}t
t ||  qt| d}	t | _t| jjD ]<}| jj| }
t
|
j j!}|rt
"|}tt
#||
j  |
j$|_%t&||| j'}|j(rd}	nd }| j)t*| |
| q| j'r| j'j+s|	st,dt-| jj.| j/| j0| _.d S )Nr   zstream_options were provided, but this format does not expose its streams before avformat_find_stream_info (e.g. MPEG). Per-stream options cannot be applied.FTzAHardware accelerated decode requested but no stream is compatible)1r   NULLr   
nb_streamsstream_options
ValueErroroptionsr   castpointerr   AVDictionaryr   sizeofp_voidrangelencopyupdateav_dict_copyr   set_timeoutopen_timeoutstart_timeoutr   avformat_find_stream_infor   av_dict_freer   r   r   avcodec_find_decodercodecparcodec_idavcodec_alloc_context3avcodec_parameters_to_context	time_basepkt_timebaser   hwaccel
is_hwaccel
add_streamr	   allow_software_fallbackRuntimeErrorr
   metadatametadata_encodingmetadata_errors)r   argskwargs	c_optionsnb_streams_before	base_dictistream_dictret at_least_one_accelerated_contextstreamcodeccodec_contextpy_codec_contextr   r   r   	__cinit__   sz   	





zInputContainer.__cinit__c                 C      t |  d S Nr   r   r   r   r   __dealloc__l      zInputContainer.__dealloc__c                 C   "   |    | jjtjkr| jjS d S rQ   )_assert_openr   
start_timer   AV_NOPTS_VALUEr   r   r   r   rW   o      zInputContainer.start_timec                 C   rU   rQ   )rV   r   durationr   rX   r   r   r   r   rZ   u   rY   zInputContainer.durationc                 C   s   |    | jjS rQ   )rV   r   bit_rater   r   r   r   r[   {   s   zInputContainer.bit_ratec                 C   s   |    t| jjS rQ   )rV   r   	avio_sizer   pbr   r   r   r   size   s   zInputContainer.sizec                 C   rP   rQ   rR   r   r   r   r   close   rT   zInputContainer.closec           
      o   s   |    | jj|i |}| jjdkrdS ttjt t	| jjt
t }|tjkr0t | | j zt| jjD ]}d||< q>|D ]}|j}|| jjkrZtd| dd||< qGtj t }W d   n1 sqw   Y  |tjkrtd	 tj t| W d   n1 sw   Y  z%|   tj t| j|}W d   n1 sw   Y  | | W n	 ty   Y n@w ||j r|jt| jk rt }	tj t|	j| W d   n1 sw   Y  | j|	jj |	_|	jjj|	j_|	V  qt| jjD ]}|| r&t }	| j| |	_|	jjj|	j_|	V  qW | d t | |tjkrBt!t"| dS dS | d t | |tjkr\t!t"| w w )a0  demux(streams=None, video=None, audio=None, subtitles=None, data=None)

        Yields a series of :class:`.Packet` from the given set of :class:`.Stream`::

            for packet in container.demux():
                # Do something with `packet`, often:
                for frame in packet.decode():
                    # Do something with `frame`.

        .. seealso:: :meth:`.StreamContainer.get` for the interpretation of
            the arguments.

        .. note:: The last packets are dummy packets that when decoded will flush the buffers.

        r   Nzstream index z out of range   zCould not allocate packet)#rV   r   getr   r    r   r$   r%   r   r   r'   r   MemoryErrorr.   read_timeoutr)   indexr"   r   r   av_packet_allocav_packet_unrefr0   av_read_framer   EOFErrorstream_indexr*   r   av_packet_move_ref_streamr8   r   av_packet_freer   )
r   rB   rC   r   include_streamrG   rK   read_packetrI   packetr   r   r   demux   s   







zInputContainer.demuxc                 o   s8    |    | j|i |D ]}| D ]}|V  qqdS )a`  decode(streams=None, video=None, audio=None, subtitles=None, data=None)

        Yields a series of :class:`.Frame` from the given set of streams::

            for frame in container.decode():
                # Do something with `frame`.

        .. seealso:: :meth:`.StreamContainer.get` for the interpretation of
            the arguments.

        N)rV   rp   decode)r   rB   rC   ro   framer   r   r   rq      s   zInputContainer.decodeTFN)backward	any_framerK   unsupported_frame_offsetunsupported_byte_offsetrs   rt   rK   ru   rv   c                C   s   |    t|tstdt||}d}|r|tjO }|r"|tjO }|r)|tjO }|r0|tj	O }|r5|j
nd}	tj t| j|	||}
W d   n1 sNw   Y  t|
 |   dS )a)  seek(offset, *, backward=True, any_frame=False, stream=None)

        Seek to a (key)frame nearest to the given timestamp.

        :param int offset: Time to seek to, expressed in``stream.time_base`` if ``stream``
            is given, otherwise in :data:`av.time_base`.
        :param bool backward: If there is not a (key)frame at the given offset,
            look backwards for it.
        :param bool any_frame: Seek to any frame, not just a keyframe.
        :param Stream stream: The stream who's ``time_base`` the ``offset`` is in.

        :param bool unsupported_frame_offset: ``offset`` is a frame
            index instead of a time; not supported by any known format.
        :param bool unsupported_byte_offset: ``offset`` is a byte
            location in the file; not supported by any known format.

        After seeking, packets that you demux should correspond (roughly) to
        the position you requested.

        In most cases, the defaults of ``backwards = True`` and ``any_frame = False``
        are the best course of action, followed by you demuxing/decoding to
        the position that you want. This is because to properly decode video frames
        you need to start from the previous keyframe.

        .. seealso:: :ffmpeg:`avformat_seek_file` for discussion of the flags.

        z+Container.seek only accepts integer offset.r   N)rV   
isinstanceint	TypeErrortyper   AVSEEK_FLAG_BACKWARDAVSEEK_FLAG_ANYAVSEEK_FLAG_FRAMEAVSEEK_FLAG_BYTErd   r   r   av_seek_framer   r   flush_buffers)r   offsetrs   rt   rK   ru   rv   c_offsetflagsri   rI   r   r   r   seek   s&   %




zInputContainer.seekc                 C   s*   |    | jD ]}|j}|r|  qd S rQ   )rV   r   rM   r   )r   rK   rM   r   r   r   r   4  s   
zInputContainer.flush_buffers)__name__
__module____qualname__rO   rS   propertyrW   rZ   r[   r^   r_   rp   rq   r   bintr   r   cfuncr   r   r   r   r   r      s@    R



[
@r   )r    cython.cimports.av.codec.contextr   r   $cython.cimports.av.container.streamsr   cython.cimports.av.dictionaryr   cython.cimports.av.errorr   cython.cimports.av.packetr   cython.cimports.av.streamr   r	   cython.cimports.av.utilsr
   cython.cimports.libc.stdintr   r   cython.cimports.libc.stdlibr   r   r   r   r   finalcclass	Containerr   r   r   r   <module>   s    	