libavutil/fifo.h
f5a90186
 /*
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
1faf7dc8
 /**
ba87f080
  * @file
89c9ff50
  * a very simple circular buffer FIFO implementation
1faf7dc8
  */
 
98790382
 #ifndef AVUTIL_FIFO_H
 #define AVUTIL_FIFO_H
f5a478f6
 
99545457
 #include <stdint.h>
 
f5a478f6
 typedef struct AVFifoBuffer {
     uint8_t *buffer;
     uint8_t *rptr, *wptr, *end;
0a71e78c
     uint32_t rndx, wndx;
f5a478f6
 } AVFifoBuffer;
 
1faf7dc8
 /**
49bd8e4b
  * Initialize an AVFifoBuffer.
1faf7dc8
  * @param size of FIFO
b9bd6589
  * @return AVFifoBuffer or NULL in case of memory allocation failure
1faf7dc8
  */
41dd680d
 AVFifoBuffer *av_fifo_alloc(unsigned int size);
1faf7dc8
 
 /**
49bd8e4b
  * Free an AVFifoBuffer.
82ffe191
  * @param *f AVFifoBuffer to free
1faf7dc8
  */
f5a478f6
 void av_fifo_free(AVFifoBuffer *f);
1faf7dc8
 
 /**
49bd8e4b
  * Reset the AVFifoBuffer to the state right after av_fifo_alloc, in particular it is emptied.
32b936d0
  * @param *f AVFifoBuffer to reset
  */
 void av_fifo_reset(AVFifoBuffer *f);
 
 /**
49bd8e4b
  * Return the amount of data in bytes in the AVFifoBuffer, that is the
82ffe191
  * amount of data you can read from it.
  * @param *f AVFifoBuffer to read from
1faf7dc8
  * @return size
  */
f5a478f6
 int av_fifo_size(AVFifoBuffer *f);
1faf7dc8
 
 /**
49bd8e4b
  * Return the amount of space in bytes in the AVFifoBuffer, that is the
7b09db35
  * amount of data you can write into it.
  * @param *f AVFifoBuffer to write into
  * @return size
  */
 int av_fifo_space(AVFifoBuffer *f);
 
 /**
49bd8e4b
  * Feed data from an AVFifoBuffer to a user-supplied callback.
82ffe191
  * @param *f AVFifoBuffer to read from
  * @param buf_size number of bytes to read
1faf7dc8
  * @param *func generic read function
  * @param *dest data destination
  */
3898eed8
 int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size, void (*func)(void*, void*, int));
1faf7dc8
 
49cec199
 /**
49bd8e4b
  * Feed data from a user-supplied callback to an AVFifoBuffer.
49cec199
  * @param *f AVFifoBuffer to write to
33147993
  * @param *src data source; non-const since it may be used as a
  * modifiable context by the function defined in func
49cec199
  * @param size number of bytes to write
89c9ff50
  * @param *func generic write function; the first parameter is src,
  * the second is dest_buf, the third is dest_buf_size.
49cec199
  * func must return the number of bytes written to dest_buf, or <= 0 to
  * indicate no more data available to write.
95c76e11
  * If func is NULL, src is interpreted as a simple byte array for source data.
89c9ff50
  * @return the number of bytes written to the FIFO
49cec199
  */
95c76e11
 int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size, int (*func)(void*, void*, int));
1faf7dc8
 
 /**
49bd8e4b
  * Resize an AVFifoBuffer.
8257b835
  * @param *f AVFifoBuffer to resize
  * @param size new AVFifoBuffer size in bytes
89c9ff50
  * @return <0 for failure, >=0 otherwise
8257b835
  */
 int av_fifo_realloc2(AVFifoBuffer *f, unsigned int size);
 
 /**
49bd8e4b
  * Read and discard the specified amount of data from an AVFifoBuffer.
82ffe191
  * @param *f AVFifoBuffer to read from
  * @param size amount of data to read in bytes
1faf7dc8
  */
f5a478f6
 void av_fifo_drain(AVFifoBuffer *f, int size);
 
 static inline uint8_t av_fifo_peek(AVFifoBuffer *f, int offs)
 {
     uint8_t *ptr = f->rptr + offs;
     if (ptr >= f->end)
         ptr -= f->end - f->buffer;
     return *ptr;
 }
98790382
 #endif /* AVUTIL_FIFO_H */