
    dj-                         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 d d
lmZmZ e j        defd            Ze j         G d de                      ZdS )    N)CodecContextwrap_codec_context)StreamContainer)
Dictionary)	err_check)Packet)Streamwrap_stream)avdict_to_dict)int64_t)freemallocselfc                    t                      | _        t          j        5  | j        dz  rAt
                              t          j        | j                             | xj        dz  c_        d d d            d S # 1 swxY w Y   d S )N   )	r   streamscythonnogil_myflaglibavformat_close_inputaddressptrr   s    M/home/agentuser/manim-venv/lib/python3.11/site-packages/av/container/input.pyclose_inputr      s    "$$DL	  <! 	$$V^DH%=%=>>>LLBLL	                 s   AA99A= A=c                       e Zd Zd Zd Zed             Zed             Zed             Zed             Z	d Z
d Zd	 Zd
ddddddej        dej        dedz  dej        dej        f
dZej        d             ZdS )InputContainerc           	         t           j        }| j        s| j        rzt	          | j                  }t          j        t           j        t           j        t          j                          t          | j
        j        t          j        t           j                  z                      }t          | j
        j                  D ]}t           j        ||<   |t          | j                  k     ru| j        rn|                                }|                    | j        |                    t                              t          j        ||                   |j
        d           t                              t          j        ||                   |j
        d           |                     | j                   |                                  t           j        5  t                              | j
        |          }d d d            n# 1 swxY w Y   |                     d            |                     |           |r]t          | j
        j                  D ]4}t                              t          j        ||                              5t7          |           d}t9                      | _        t          | j
        j                  D ]}| j
        j        |         }	t                              |	j        j                   }
|
rst          !                    |
          }t3          t          "                    ||	j                             |	j#        |_$        tK          ||
| j&                  }|j'        rd}nd }| j        (                    tS          | |	|                     | j&        r| j&        j*        s|stW          d          tY          | j
        j-        | j.        | j/                  | _-        d S )Nr   FTzAHardware accelerated decode requested but no stream is compatible)0r   NULLoptionsstream_optionsr   castpointerr   AVDictionaryr   r   
nb_streams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	base_dictistream_dictret at_least_one_accelerated_contextstreamcodeccodec_contextpy_codec_contexts                r   	__cinit__zInputContainer.__cinit__   s    GMk	 < 	U4. 	U"4<00Iv~c.>?@tx*V]6=-I-IIJJ I 48.// U U%{	!s4.////D4G/"+.."2"2K&&t':1'=>>>$$V^IaL%A%A;?TUVVVV$$V^IaL%A%A9=RSTTTT*+++\ 	E 	E//)DDC	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	Es  	48.// ? ?  	!!=!=>>>>OOO+0(&((tx*++ 	Q 	QAX%a(F,,V_-EFFE ( # : :5 A A55mV_UU   .4-=*#5!5$,$ $  $. <7;4 $( L##Kf>N$O$OPPPP L	L8	 5	
 S   'Ht5t7K
 
s   !G==HHc                 $    t          |            d S Nr   r   s    r   __dealloc__zInputContainer.__dealloc__d       D    c                 z    |                                   | j        j        t          j        k    r| j        j        S d S rQ   )_assert_openr   
start_timer   AV_NOPTS_VALUEr   s    r   rX   zInputContainer.start_timeg   s:    8#"4448&& 54rU   c                 z    |                                   | j        j        t          j        k    r| j        j        S d S rQ   )rW   r   durationr   rY   r   s    r   r[   zInputContainer.durationm   s:    8 2228$$ 32rU   c                 B    |                                   | j        j        S rQ   )rW   r   bit_rater   s    r   r]   zInputContainer.bit_rates   s    x  rU   c                 r    |                                   t                              | j        j                  S rQ   )rW   r   	avio_sizer   pbr   s    r   sizezInputContainer.sizex   s*    }}TX[)))rU   c                 $    t          |            d S rQ   rR   r   s    r   closezInputContainer.close}   rT   rU   c           	   /   0  K   |                                    | j        j        |i |}t          j        t          j        t          j                 t          | j        j	        t          j
        t                    z                      }|t          j        k    rt                      |                     | j                   	 t          | j        j	                  D ]}d||<   |D ]1}|j        }|| j        j	        k    rt#          d| d          d||<   2t          j        5  t&                                          }ddd           n# 1 swxY w Y   |t          j        k    rt          d          	 t          j        5  t&                              |           ddd           n# 1 swxY w Y   	 |                                  t          j        5  t&                              | j        |          }ddd           n# 1 swxY w Y   |                     |           n# t2          $ r Y nw xY w||j                 r|j        t7          | j                  k     rt9                      }	t          j        5  t&                              |	j        |           ddd           n# 1 swxY w Y   | j        |	j        j                 |	_        |	j        j        j        |	j        _        |	V  vt          | j        j	                  D ]I}||         r?t9                      }	| j        |         |	_        |	j        j        j        |	j        _        |	V  J	 |                     d           tA          |           |t          j        k    r.t&          !                    t          j"        |                     dS dS # |                     d           tA          |           |t          j        k    r-t&          !                    t          j"        |                     w w xY 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.

        Fzstream index z out of rangeTNzCould not allocate packet)#rW   r   getr   r$   r%   bintr   r   r'   r(   r!   MemoryErrorr/   read_timeoutr*   index
ValueErrorr   r   av_packet_allocav_packet_unrefr1   av_read_framer   EOFErrorstream_indexr+   r   av_packet_move_ref_streamr9   r   av_packet_freer   )
r   rC   rD   r   include_streamrG   rK   read_packetrI   packets
             r   demuxzInputContainer.demux   s       	 0 0$ A& A A6<kN6;'48&t)<)<<==7
 7
 V[((-- 	*+++7	@48.// * *$)q!!! ) )L+++$%EQ%E%E%EFFF$(q!!  4 4!11334 4 4 4 4 4 4 4 4 4 4 4 4 4 4fk))!"=>>>%\ 5 5''4445 5 5 5 5 5 5 5 5 5 5 5 5 5 5&&((( G G!//+FFG G G G G G G G G G G G G G GNN3''''   E "+":; %
 #/#dl2C2CCC "(#\ L L226:{KKKL L L L L L L L L L L L L L L)-fj6M)N/5~/A/K
,$5%: 48.// ! !!!$ !#XXF%)\!_FN+1>+=+GFJ( LLL! T"""   fk))""6>+#>#>????? *) T"""   fk))""6>+#>#>???? *s   A!N2 #E	=N2 	EN2 E/N2  F'N2 'F++N2 .F+/N2 3 H! !H 4H!  HH! HH!  N2 !
H.+N2 -H..AN2 5!J"N2 "J&&N2 )J&*B#N2 2A#Pc              /      K   |                                    | j        |i |D ]}|                                D ]}|V  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)rW   rv   decode)r   rC   rD   ru   frames        r   rx   zInputContainer.decode   sk       	 dj$1&11 	 	F  	 	rU   TFN)backward	any_framerK   unsupported_frame_offsetunsupported_byte_offsetrz   r{   rK   r|   r}   c                
   |                                   t          |t                    st          dt	          |                    |}d}|r|t
          j        z  }|r|t
          j        z  }|r|t
          j        z  }|r|t
          j	        z  }|r|j
        nd}	t          j        5  t
                              | j        |	||          }
ddd           n# 1 swxY w 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)rW   
isinstanceint	TypeErrortyper   AVSEEK_FLAG_BACKWARDAVSEEK_FLAG_ANYAVSEEK_FLAG_FRAMEAVSEEK_FLAG_BYTEri   r   r   av_seek_framer   r   flush_buffers)r   offsetrz   r{   rK   r|   r}   c_offsetflagsro   rI   s              r   seekzInputContainer.seek   s`   J 	&#&& 	YI4PV<<XXX"  	.S--E 	)S((E $ 	+S**E" 	*S))E39#A6<<r\ 	M 	M##DHlHeLLC	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M 	M#s   &#CCCc                 |    |                                   | j        D ]}|j        }|r|                                  d S rQ   )rW   r   rM   r   )r   rK   rM   s      r   r   zInputContainer.flush_buffers*  sT    
 l 	. 	.F"0M .++---	. 	.rU   )__name__
__module____qualname__rO   rS   propertyrX   r[   r]   ra   rc   rv   rx   r   rf   r	   r   cfuncr    rU   r   r   r      sa       I
 I
 I
V   ' ' X'
 % % X%
 ! ! X! * * X*  W@ W@ W@r  * !%!& $05/4> > > +	>
 ;> > #)+> "(> > > >@ \	. 	. \	. 	. 	.rU   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   cython.cimports.libc.stdlibr   r   r   r   r   cclass	Containerr   rU   r   <module>r      s7    M M M M M M M M @ @ @ @ @ @ 4 4 4 4 4 4 . . . . . . , , , , , , 9 9 9 9 9 9 9 9 3 3 3 3 3 3 / / / / / / 4 4 4 4 4 4 4 4 n     \. \. \. \. \.Y \. \. \. \. \.rU   