o
    پi/                     @   s   d Z ddlmZ ddlZ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 ejZG dd deZe
d dS )zVideo Loader.    )absolute_importN   )c_arrayc_str)	_init_api)DECORDError)ndarray)DECORDContext)
bridge_outc                   @   sJ   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dS )VideoLoaderaZ  Multiple video loader with advanced shuffling and batching methods.

    Parameters
    ----------
    uris : list of str
        List of video paths.
    ctx : decord.Context or list of Context
        The context to decode the video file, can be decord.cpu() or decord.gpu().
        If ctx is a list, videos will be evenly split over many ctxs.
    shape : tuple
        Returned shape of the batch images, e.g., (2, 320, 240, 3) as (Batch, H, W, 3)
    interval : int
        Intra-batch frame interval.
    skip : int
        Inter-batch frame interval.
    shuffle : int
        Shuffling strategy. Can be
        `0`:  all sequential, no seeking, following initial filename order
        `1`:  random filename order, no random access for each video, very efficient
        `2`:  random order
        `3`:  random frame access in each video only.

    r   c                 C   s
  d | _ t|ttfsJ t|dksJ ddd |D }t|tr&|g}|D ]	}	t|	ts1J q(tdd |D }
tdd |D }t|ttfsOJ t|dks\J d		|t
||
||d |d
 |d |d ||||| _ | j d uszJ t| j | _d| _d S )Nr   ,c                 s   s    | ]}|  V  qd S N)strip.0x r   G/home/ubuntu/.local/lib/python3.10/site-packages/decord/video_loader.py	<genexpr>-   s    z'VideoLoader.__init__.<locals>.<genexpr>c                 S      g | ]}|j qS r   )device_typer   r   r   r   
<listcomp>2       z(VideoLoader.__init__.<locals>.<listcomp>c                 S   r   r   )	device_idr   r   r   r   r   3   r      z0expected shape: [bs, height, width, 3], given {}r         )_handle
isinstancelisttuplelenjoinr	   _ndarrayformat_CAPI_VideoLoaderGetVideoLoader_CAPI_VideoLoaderLength_len_curr)selfurisctxshapeintervalskipshuffleprefetchuri_ctxdevice_types
device_idsr   r   r   __init__)   s$   
&
zVideoLoader.__init__c                 C   s   | j r
t| j  d S d S r   )r   _CAPI_VideoLoaderFreer*   r   r   r   __del__<   s   zVideoLoader.__del__c                 C   s   | j S )zGet number of batches in each epoch.

        Returns
        -------
        int
            number of batches in each epoch.

        )r(   r8   r   r   r   __len__@   s   	zVideoLoader.__len__c                 C   s"   | j dusJ d| _t| j  dS )z&Reset loader for next epoch.

        Nr   )r   r)   _CAPI_VideoLoaderResetr8   r   r   r   resetK   s   zVideoLoader.resetc                 C   sZ   | j dusJ | j| jkrtt| j  t| j }t| j }|  jd7  _t|t|fS )a  Get the next batch.

        Returns
        -------
        ndarray, ndarray
            Frame data and corresponding indices in videos.
            Indices are [(n0, k0), (n1, k1)...] where n0 is the index of video, k0 is the index
            of frame in video n0.

        Nr   )r   r)   r(   StopIteration_CAPI_VideoLoaderNext_CAPI_VideoLoaderNextData_CAPI_VideoLoaderNextIndicesr
   )r*   dataindicesr   r   r   __next__S   s   


zVideoLoader.__next__c                 C   s   |   S )z(Alias of __next__ for python2.

        )rC   r8   r   r   r   nexth   s   zVideoLoader.nextc                 C   s   | j d usJ | S r   )r   r8   r   r   r   __iter__n   s   zVideoLoader.__iter__N)r   )__name__
__module____qualname____doc__r6   r9   r:   r<   rC   rD   rE   r   r   r   r   r      s    
r   zdecord.video_loader)rI   
__future__r   ctypesnumpynp	_ffi.baser   r   _ffi.functionr   baser    r   r#   r	   bridger
   c_void_pVideoLoaderHandleobjectr   r   r   r   r   <module>   s    h