
;c]c           @` s  d  Z  d d l m Z m Z m Z d d l Z d d l Z d d l Z d d l	 Z	 d d l
 Z
 d d l m Z d d l m Z m Z m Z m Z m Z e j e  Z i d d 6d	 d
 6d d 6d d 6d d 6Z d Z d Z d   Z d Z d Z d Z e j Z d Z d Z  d Z! d Z" d Z# e j Z$ d Z% d Z& d Z' d Z( d Z) d Z* d Z+ d Z, e j Z- e j. Z/ e j. Z0 e j1 Z2 d e j3 f d     YZ4 d e j3 f d      YZ5 d! e j3 f d"     YZ6 d# e j7 f d$     YZ8 d% e j3 f d&     YZ9 d' e j3 f d(     YZ: e j; e< e j= e j e j=  Z> d)   Z? e< e@ d*  ZA d+   ZB d, e f d-     YZC eC d. d/ d0 d1  ZD e jE eD  d S(2   s    Plugin for reading videos via AvBin

Would be nice if we could capture webcam with this, but unfortunately,
avbin does not currently support this.
i    (   t   absolute_importt   print_functiont   divisionNi   (   t   formats(   t   Formatt   get_platformt   get_remote_filet   InternetNotAllowedErrort   NeedDownloadErrors   libavbin-10-osx.dylibt   osx64s   avbin-10-win32.dllt   win32s   avbin-10-win64.dllt   win64s   libavbin-10-linux32.sot   linux32s   libavbin-10-linux64.sot   linux64ic         C` s#   |  t  k r t d |    n  |  S(   Ns   AVBin returned error code %i(   t   AVBIN_RESULT_OKt   RuntimeError(   t   x(    (    s4   lib/python2.7/site-packages/imageio/plugins/avbin.pyt   AVbinResult-   s    i   i   i   ii   i   i   i    i(   i0   t   AVbinFileInfoc           B` s   e  Z d  e j f d e j f d e f d e f d e j d f d e j d f d e j d f d e j d f d	 e j d f d
 e j f d e j f d e j d f g Z RS(   t   structure_sizet	   n_streamst
   start_timet   durationt   titlei   t   authort	   copyrightt   commentt   albumt   yeart   trackt   genrei    (   t   __name__t
   __module__t   ctypest   c_size_tt   c_intt	   Timestampt   c_chart   _fields_(    (    (    s4   lib/python2.7/site-packages/imageio/plugins/avbin.pyR   O   s   		t   _AVbinStreamInfoVideo8c           B` sV   e  Z d  e j f d e j f d e j f d e j f d e j f d e j f g Z RS(   t   widtht   heightt   sample_aspect_numt   sample_aspect_dent   frame_rate_numt   frame_rate_den(   R   R    R!   t   c_uintR&   (    (    (    s4   lib/python2.7/site-packages/imageio/plugins/avbin.pyR'   `   s   t   _AVbinStreamInfoAudio8c           B` s>   e  Z d  e j f d e j f d e j f d e j f g Z RS(   t   sample_formatt   sample_ratet   sample_bitst   channels(   R   R    R!   R#   R.   R&   (    (    (    s4   lib/python2.7/site-packages/imageio/plugins/avbin.pyR/   k   s   t   _AVbinStreamInfoUnion8c           B` s    e  Z d  e f d e f g Z RS(   t   videot   audio(   R   R    R'   R/   R&   (    (    (    s4   lib/python2.7/site-packages/imageio/plugins/avbin.pyR4   t   s   t   AVbinStreamInfo8c           B` s/   e  Z d  e j f d e j f d e f g Z RS(   R   t   typet   u(   R   R    R!   R"   R#   R4   R&   (    (    (    s4   lib/python2.7/site-packages/imageio/plugins/avbin.pyR7   x   s   t   AVbinPacketc           B` sP   e  Z d  e j f d e f d e j f d e j e j  f d e j f g Z RS(   R   t	   timestampt   stream_indext   datat   size(	   R   R    R!   R"   R$   R#   t   POINTERt   c_uint8R&   (    (    (    s4   lib/python2.7/site-packages/imageio/plugins/avbin.pyR:      s
   	c         C` s   t  |   d S(   Ni@B (   t   float(   R;   (    (    s4   lib/python2.7/site-packages/imageio/plugins/avbin.pyt   timestamp_from_avbin   s    c         C` sY   t    } | o | t k s. t d |   n  d t | } t d | d |  d |  d S(   s   Download the avbin library to your computer.

    Parameters
    ----------
    directory : str | None
        The directory where the file will be cached if a download was
        required to obtain the file. By default, the appdata directory
        is used. This is also the first directory that is checked for
        a local version of the file.
    force_download : bool | str
        If True, the file will be downloaded even if a local copy exists
        (and this copy will be overwritten). Can also be a YYYY-MM-DD date
        to ensure a file is up-to-date (modified date of a file on disk,
        if present, is checked).
    s*   AVBIN lib is not available for platform %ss   avbin/t   fnamet	   directoryt   force_downloadN(   R   t   FNAME_PER_PLATFORMR   R   (   RD   RE   t   platRC   (    (    s4   lib/python2.7/site-packages/imageio/plugins/avbin.pyt   download   s
    	c          C` s   t  j d d  }  |  r |  St   } y t | }  Wn! t k
 rV t d |   n Xy t d |  d t SWnE t	 k
 r t	 d   n) t
 k
 r } t d t |    n Xd S(   s    Get avbin .dll/.dylib/.so
    t   IMAGEIO_AVBIN_LIBs,   Avbin plugin is not supported on platform %ss   avbin/t   autos   Need avbin library. You can obtain it with either:
  - download using the command: imageio_download_bin avbin
  - download by calling (in Python): imageio.plugins.avbin.download()
s    Could not download avbin lib:
%sN(   t   ost   getenvt   NoneR   RF   t   KeyErrorR   R   t   FalseR   R   t   IOErrort   str(   t   libt   platformt   err(    (    s4   lib/python2.7/site-packages/imageio/plugins/avbin.pyt   get_avbin_lib   s    	t   AvBinFormatc           B` sN   e  Z d  Z d   Z d   Z d   Z d d  Z d e j	 f d     YZ	 RS(   s   
    The AvBinFormat uses the AvBin library (based on libav) to read
    video files.
    
    This plugin is more efficient than the ffmpeg plugin, because it
    uses ctypes (rather than a pipe like the ffmpeg plugin does).
    Further, it supports reading images into a given numpy array.
    
    The limitations of this plugin are that seeking, writing and camera
    feeds are not supported. See the ffmpeg format for these features.

    The avbin plugin requires an `avbin` binary. If this binary is
    not available on the system, it can be downloaded manually from
    <https://github.com/imageio/imageio-binaries> by either
    
    - the command line script ``imageio_download_bin avbin``
    - the Python method ``imageio.plugins.avbin.download()``.

    Parameters for reading
    ----------------------
    loop : bool
        If True, the video will rewind as soon as a frame is requested
        beyond the last frame. Otherwise, IndexError is raised. Default False.
    stream : int
        Specifies which video stream to read. Default 0.
    videoformat : str | None
        Specifies the video format (e.g. 'avi', or 'mp4'). If this is None
        (default) the format is auto-detected.
    
    Parameters for get_data
    -----------------------
    out : np.ndarray
        destination for the data retrieved. This can be used to save 
        time-consuming memory allocations when reading multiple image
        sequntially. The shape of out must be (width, height, 3), the
        dtype must be np.uint8 and it must be C-contiguous.
        
        Use the create_empty_image() method of the reader object
        to create an array that is suitable for get_data.
    c         O` s    d  |  _ t j |  | |  d  S(   N(   RM   t   _avbinR   t   __init__(   t   selft   argst   kwargs(    (    s4   lib/python2.7/site-packages/imageio/plugins/avbin.pyRX      s    	c         C` s7   | j  d |  j d k r3 | j |  j k r3 t Sn  d  S(   Ni   t   ?(   t   modet   modest	   extensiont
   extensionst   True(   RY   t   request(    (    s4   lib/python2.7/site-packages/imageio/plugins/avbin.pyt	   _can_read   s    c         C` s   t  S(   N(   RO   (   RY   Rb   (    (    s4   lib/python2.7/site-packages/imageio/plugins/avbin.pyt
   _can_write   s    c         C` sj  |  j  d  k	 r" | d  k r" |  j  St j d  | d  k rG t   } n  t j j |  |  _  } t j | j	 _
 t j | j _
 t j | j _
 t j | j _
 t j g | j _ t | j _
 t | j _
 t g | j _ t g | j _ t | j _
 t j g | j _ t | j _
 t j t j g | j _ t g | j _ t t g | j _ t t j t  g | j _ t t j t j t   g | j! _ t j" | j# _
 t t j g | j# _ t$ g | j% _ t t j t&  g | j' _ t | j' _
 t j | j( _
 t$ t j" t j t j" t j t j  g | j( _ t j | j) _
 t$ t j" t j t j" g | j) _ | j   | j t*  |  j  S(   Nsj   The imageio avbin plugin is deprecated and will be removed in a future version. Please use ffmpeg instead.(+   RW   RM   t   loggert   warningRU   R!   t   cdllt   LoadLibraryR#   t   avbin_get_versiont   restypet   avbin_get_ffmpeg_revisionR"   t   avbin_get_audio_buffer_sizet   avbin_have_featuret   c_char_pt   argtypesR   t
   avbin_initt   avbin_set_log_levelt   AVbinLogLevelt   AVbinLogCallbackt   avbin_set_log_callbackt
   AVbinFilePt   avbin_open_filenamet   avbin_open_filename_with_formatt   avbin_close_fileR$   t   avbin_seek_fileR?   R   t   avbin_file_infoR7   t   avbin_stream_infot   c_void_pt   avbin_open_streamt   AVbinStreamPt   avbin_close_streamR:   t
   avbin_readt   avbin_decode_audiot   avbin_decode_videot   AVBIN_LOG_QUIET(   RY   t   libpatht   avbin(    (    s4   lib/python2.7/site-packages/imageio/plugins/avbin.pyt   avbinlib   s\    
t   Readerc           B` sV   e  Z e d  d e d  Z d   Z d   Z d   Z d   Z d d  Z	 d   Z
 RS(	   i    c         C` so   t  |  |  _ t |  |  _ | |  _ t  |  |  _ |  j j   |  _ d  |  _
 i d d 6|  _ |  j   d  S(   NR   t   plugin(   t   boolt	   _arg_loopt   intt   _arg_streamt   _arg_videoformatt   _arg_skipemptyRb   t   get_local_filenamet	   _filenameRM   t   _filet   _metat   _init_video(   RY   t   loopt   streamt   videoformatt	   skipempty(    (    s4   lib/python2.7/site-packages/imageio/plugins/avbin.pyt   _openG  s    		c         C` s   |  j  j   } |  j j t j    } |  j d  k	 rZ | j | |  j j d   |  _	 n | j
 |  |  _	 |  j	 s t d |  j   n  t   |  _ t j |  j  |  j _ | j |  j	 t j |  j   t | j    |  j d <|  j j j d  |  j d <|  j j j d  |  j d <t |  j j  |  j d <d } xt |  j j  D]^} t   } t j |  | _ | j |  j	 | |  | j t  k rqQn  | |  j! k r| d	 7} qQn  | j" |  j	 |  |  _# | |  _$ | j% j& j' |  _( | j% j& j) |  _* |  j( |  j* f |  j d
 <|  j( |  j* f |  j d <t+ | j% j& j,  t+ | j% j& j-  |  j d <|  j d c d |  j d 8<t. |  j d |  j d  |  j d <| |  _/ PqQWt d |  j! |  j f   t0   |  _1 t j |  j1  |  j1 _ d |  _2 d  S(   Nt   asciis   Could not open "%s"t   avbin_versions   utf-8R   R   R   i    i   R>   t   source_sizet   fpsg      ?t   nframess   Stream #%d not found in %r(3   t   formatR   R   t   encodet   syst   getfilesystemencodingR   RM   Rw   R   Rv   RP   R   t   _infoR!   t   sizeofR   Rz   t   byrefRQ   Ri   R   R   t   decodeR   RB   R   t   rangeR   R7   R{   R8   t   AVBIN_STREAM_TYPE_VIDEOR   R}   t   _streamt   _stream_infoR9   R5   R(   t   _widthR)   t   _heightRA   R,   R-   R   t   _stream_indexR:   t   _packett   _framecounter(   RY   R   t   filename_bytest   video_stream_countert   it   info(    (    s4   lib/python2.7/site-packages/imageio/plugins/avbin.pyR   V  sT    		
	%	c         C` s>   |  j  d  k	 r: |  j j   } | j |  j   d  |  _  n  d  S(   N(   R   RM   R   R   Rx   (   RY   R   (    (    s4   lib/python2.7/site-packages/imageio/plugins/avbin.pyt   _close  s    c         C` s   |  j  d S(   NR   (   R   (   RY   (    (    s4   lib/python2.7/site-packages/imageio/plugins/avbin.pyt   _get_length  s    c         C` s%   t  j |  j |  j d f d t  j S(   Ni   t   dtype(   t   npt   zerosR   R   t   uint8(   RY   (    (    s4   lib/python2.7/site-packages/imageio/plugins/avbin.pyt   create_empty_image  s    c         C` sC  |  j  j   } |  j d rU |  j d t d  k  rU |  j rU | |  j d } qU n  | d k  rp t d   nm | |  j d k r t d   nK | |  j k r | d k r |  j   |  j   |  j	 d  St d   n  |  j d 7_ | d  k r|  j   } n  | j t j k rC| j j rC| j |  j |  j d f k sIt  x t r)y# | j |  j t j |  j   Wn t k
 rt d	   n X|  j j |  j k rqLn  y | j j } Wn" t k
 r| j  d
 d } n X| j! |  j" |  j j |  j j# |  } |  j$ s"| d k rLPqLqLW| t% d |  j j&  f S(   NR   t   infi    s   Frame index must be > 0s   Reached end of videos   Avbin format cannot seeki   i   s   Reached end of video too soonR=   iR;   ('   R   R   R   RA   R   t
   IndexErrorR   R   R   t	   _get_dataRM   R   R   R   R   t   flagst   c_contiguoust   shapeR   R   t   AssertionErrorRa   R   R   R!   R   R   R   R<   R   R=   t	   Exceptiont   __array_interface__R   R   R>   R   t   dictR;   (   RY   t   indext   outR   t   ptrt   result(    (    s4   lib/python2.7/site-packages/imageio/plugins/avbin.pyR     sH    &	

$	#!c         C` s   |  j  S(   N(   R   (   RY   R   (    (    s4   lib/python2.7/site-packages/imageio/plugins/avbin.pyt   _get_meta_data  s    N(   R   R    RO   RM   R   R   R   R   R   R   R   (    (    (    s4   lib/python2.7/site-packages/imageio/plugins/avbin.pyR   F  s   	I			<N(
   R   R    t   __doc__RX   Rc   Rd   RM   R   R   R   (    (    (    s4   lib/python2.7/site-packages/imageio/plugins/avbin.pyRV      s   (			GR   s2   Many video formats (via AvBin, i.e. libav library)s   mov avi mp4 mpg mpeg mkvt   I(F   R   t
   __future__R    R   R   t   numpyR   t   loggingR!   R   RK   t    R   t   coreR   R   R   R   R   t	   getLoggerR   Re   RF   t   AVBIN_RESULT_ERRORR   R   t   AVBIN_STREAM_TYPE_UNKNOWNR   t   AVBIN_STREAM_TYPE_AUDIOR#   t   AVbinStreamTypet   AVBIN_SAMPLE_FORMAT_U8t   AVBIN_SAMPLE_FORMAT_S16t   AVBIN_SAMPLE_FORMAT_S24t   AVBIN_SAMPLE_FORMAT_S32t   AVBIN_SAMPLE_FORMAT_FLOATt   AVbinSampleFormatR   t   AVBIN_LOG_PANICt   AVBIN_LOG_FATALt   AVBIN_LOG_ERRORt   AVBIN_LOG_WARNINGt   AVBIN_LOG_INFOt   AVBIN_LOG_VERBOSEt   AVBIN_LOG_DEBUGRr   R|   Ru   R~   t   c_int64R$   t	   StructureR   R'   R/   t   UnionR4   R7   R:   t	   CFUNCTYPERM   Rn   Rs   RB   RO   RH   RU   RV   R   t
   add_format(    (    (    s4   lib/python2.7/site-packages/imageio/plugins/avbin.pyt   <module>   sv   (	
								
		 '	