
    \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mZ d d
lmZmZ e j.                  defd       Ze j4                  e j6                   G d de                    Zy)    N)CodecContextwrap_codec_context)StreamContainer)
Dictionary)	err_check)Packet)Streamwrap_stream)avdict_to_dict)int64_tuint8_t)freemallocselfc                    t               | _        t        j                  5  | j                  dz  rGt
        j                  t        j                  | j                               | xj                  dz  c_        d d d        y # 1 sw Y   y xY w)N   )	r   streamscythonnogil_myflaglibavformat_close_inputaddressptrr   s    E/root/.hermes/venv/lib/python3.12/site-packages/av/container/input.pyclose_inputr      s]    "$DL	 <<!$$V^^DHH%=>LLBL	  s   AB  B	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y)InputContainerc           	      R   t         j                  }| j                  j                  }| j                  r|dk(  rt        d      | j                  s| j                  rr|dkD  rlt        | j                        }t        j                  t         j                  t         j                  t        j                        t        |t        j                  t         j                        z              }t        |      D ]  }t         j                  ||<   |t!        | j                        k  rr| j                  rf|j#                         }|j%                  | j                  |          t        j'                  t        j(                  ||         |j                  d       t        j'                  t        j(                  ||         |j                  d        | j+                  | j,                         | j/                          t         j0                  5  t        j3                  | j                  |      }d d d        | j+                  d        | j5                         |rFt        |      D ]-  }t        j7                  t        j(                  ||                / t9        |       d}	t;               | _        t        | j                  j                        D ]  }| j                  j<                  |   }
t        j?                  |
j@                  jB                        }|rut        jE                  |      }t5        t        jG                  ||
j@                               |
jH                  |_%        tM        ||| jN                        }|jP                  rd}	nd }| j<                  jS                  tU        | |
|              | jN                  r#| jN                  jV                  s|	stY        d      t[        | j                  j\                  | j^                  | j`                        | _1        y # 1 sw Y   xY w)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)2r   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	_metadata)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_contexts                 r   	__cinit__zInputContainer.__cinit__   sF    GMkk	 *.)<)<#4#98  LLD//5F5J"4<<0Iv~~c.>.>?@(6==+GGHI ,- U%{{	!s4..//D4G4G"+.."2K&&t':':1'=>$$V^^IaL%A;??TUV$$V^^IaL%A9==RSTU 	**+\\ 	E//)DC	Es,- ?  	!!=>?O+0(&(txx**+ 	QAXX%%a(F,,V__-E-EFE # : :5 A55mV__U .4-=-=*#5!5$,,$  $..7;4 $( LL##Kf>N$OP#	Q( LLLL884S  (HHt55t7K7K
S	E 	Es   	!PP&c                     t        |        y Nr   r   s    r   __dealloc__zInputContainer.__dealloc__l   
    D    c                     | j                          | j                  j                  t        j                  k7  r| j                  j                  S y rU   )_assert_openr   
start_timer   AV_NOPTS_VALUEr   s    r   r\   zInputContainer.start_timeo   s;    88#"4"4488&&& 5rY   c                     | j                          | j                  j                  t        j                  k7  r| j                  j                  S y rU   )r[   r   durationr   r]   r   s    r   r_   zInputContainer.durationu   s;    88 2 2288$$$ 3rY   c                 N    | j                          | j                  j                  S rU   )r[   r   bit_rater   s    r   ra   zInputContainer.bit_rate{   s    xx   rY   c                 t    | j                          t        j                  | j                  j                        S rU   )r[   r   	avio_sizer   pbr   s    r   sizezInputContainer.size   s%    }}TXX[[))rY   c                     t        |        y rU   rV   r   s    r   closezInputContainer.close   rX   rY   c           	   /     K   | j                           | j                  j                  |i |}| j                  j                  dk(  ryt        j                  t
        j                  t           t        | j                  j                  t        j                  t              z              }|t
        j                  k(  r
t               | j                  | j                         	 t        | j                  j                        D ]  }d||<   	 |D ];  }|j                   }|| j                  j                  k\  rt#        d| d      d||<   = t
        j$                  5  t&        j)                         }ddd       t
        j                  k(  rt        d      	 t
        j$                  5  t&        j+                  |       ddd       	 | j-                          t
        j$                  5  t&        j/                  | j                  |      }ddd       | j1                         ||j4                     r|j4                  t7        | j                        k  rt9               }	t
        j$                  5  t&        j;                  |	j                  |       ddd       | j                  |	j                  j4                     |	_        |	j<                  j                  j>                  |	j                  _        |	 Z# 1 sw Y   xY w# 1 sw Y   CxY w# 1 sw Y   xY w# t2        $ r Y nw xY w# 1 sw Y   xY wt        | j                  j                        D ]Y  }||   s	t9               }	| j                  |   |	_        |	j<                  j                  j>                  |	j                  _        |	 [ 	 | j                  d       tA        |       |t
        j                  k7  r)t&        jC                  t        jD                  |             yy# | j                  d       tA        |       t
        j                  k7  r)t&        jC                  t        jD                  |             w w xY 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)#r[   r   getr   r#   r   r'   r(   r   r   r*   r"   MemoryErrorr1   read_timeoutr,   indexr%   r   r   av_packet_allocav_packet_unrefr3   av_read_framer   EOFErrorstream_indexr-   r   av_packet_move_ref_streamr;   r   av_packet_freer   )
r   rF   rG   r   include_streamrK   rO   read_packetrM   packets
             r   demuxzInputContainer.demux   s}      	 0 0 0$ A& A88!#28++NN7#488&&w)??@3
 V[[(- 	**+7	@488../ &$%q!&! &LL+++$}QC}%EFF$%q!	&  4!1134fkk)!"=>>\\ 5''45&&( G!//+FGNN3' "+":":;
 #//#dll2CC "(#\\ L226::{KL)-fjj6M6M)N/5~~/A/A/K/K

,$5 4 45 5G G   L L 488../ !!!$#XF%)\\!_FN+1>>+=+=+G+GFJJ( L! T" fkk)""6>>+#>? * T" fkk)""6>>+#>? *s   C!Q'$A9P
 K327P
 )L ?P
  L (!L	L "AP
 -!L)A%P
 3K=8P
  L
P
 LL 	L&#P
 %L&&P
 )L2..P
 AP
 1AQ'
AQ$$Q'c              /      K   | j                           | j                  |i |D ]  }|j                         E d{     y7 w)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)r[   ry   decode)r   rF   rG   rx   s       r   r{   zInputContainer.decode   sE      	 djj$1&1 	'F}}&&	'&s   9AAATFN)backward	any_framerO   unsupported_frame_offsetunsupported_byte_offsetr|   r}   rO   r~   r   c                   | j                          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                  | j                  |	||      }
ddd       t        
       | j!                          y# 1 sw Y   %xY w)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)r[   
isinstanceint	TypeErrortyper   AVSEEK_FLAG_BACKWARDAVSEEK_FLAG_ANYAVSEEK_FLAG_FRAMEAVSEEK_FLAG_BYTErm   r   r   av_seek_framer   r   flush_buffers)r   offsetr|   r}   rO   r~   r   c_offsetflagsrr   rM   s              r   seekzInputContainer.seek   s    J 	&#&I4PV<XX" S---ES(((E $S***E"S)))E396<<r\\ 	M##DHHlHeLC	M#		M 	Ms   .#C55C>c                     | j                          | j                  D ]!  }|j                  }|s|j                          # y rU   )r[   r   rQ   r   )r   rO   rQ   s      r   r   zInputContainer.flush_buffers3  s=    
 ll 	.F"00M++-	.rY   )__name__
__module____qualname__rS   rW   propertyr\   r_   ra   re   rg   ry   r{   r   bintr	   r   cfuncr    rY   r   r    r       s    P
d ' '
 % %
 ! ! * *Y@v'( !%!& $05/4> ++	>
 ;;> > #)++> "(>@ \\	. 	.rY   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   rY   r   <module>r      sp     M @ 4 . , 9 3 8 4 n   d.Y d.  d.rY   