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>
323b9306
 #include "avutil.h"
1d9c2dc8
 #include "attributes.h"
99545457
 
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.
0138a8de
  * @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.
0138a8de
  * @param f AVFifoBuffer to reset
32b936d0
  */
 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.
0138a8de
  * @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.
0138a8de
  * @param f AVFifoBuffer to write into
7b09db35
  * @return size
  */
 int av_fifo_space(AVFifoBuffer *f);
 
 /**
49bd8e4b
  * Feed data from an AVFifoBuffer to a user-supplied callback.
0138a8de
  * @param f AVFifoBuffer to read from
82ffe191
  * @param buf_size number of bytes to read
0138a8de
  * @param func generic read function
  * @param dest data destination
1faf7dc8
  */
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.
0138a8de
  * @param f AVFifoBuffer to write to
  * @param src data source; non-const since it may be used as a
33147993
  * modifiable context by the function defined in func
49cec199
  * @param size number of bytes to write
0138a8de
  * @param func generic write function; the first parameter is src,
89c9ff50
  * 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.
bf917e26
  * In case of reallocation failure, the old FIFO is kept unchanged.
  *
0138a8de
  * @param f AVFifoBuffer to resize
8257b835
  * @param size new AVFifoBuffer size in bytes
89c9ff50
  * @return <0 for failure, >=0 otherwise
8257b835
  */
 int av_fifo_realloc2(AVFifoBuffer *f, unsigned int size);
 
 /**
f8196759
  * Enlarge an AVFifoBuffer.
  * In case of reallocation failure, the old FIFO is kept unchanged.
  * The new fifo size may be larger than the requested size.
  *
  * @param f AVFifoBuffer to resize
  * @param additional_space the amount of space in bytes to allocate in addition to av_fifo_size()
  * @return <0 for failure, >=0 otherwise
  */
 int av_fifo_grow(AVFifoBuffer *f, unsigned int additional_space);
 
 /**
49bd8e4b
  * Read and discard the specified amount of data from an AVFifoBuffer.
0138a8de
  * @param f AVFifoBuffer to read from
82ffe191
  * @param size amount of data to read in bytes
1faf7dc8
  */
f5a478f6
 void av_fifo_drain(AVFifoBuffer *f, int size);
 
323b9306
 /**
  * Return a pointer to the data stored in a FIFO buffer at a certain offset.
  * The FIFO buffer is not modified.
  *
fa19c5c2
  * @param f    AVFifoBuffer to peek at, f must be non-NULL
323b9306
  * @param offs an offset in bytes, its absolute value must be less
  *             than the used buffer size or the returned pointer will
  *             point outside to the buffer data.
  *             The used buffer size can be checked with av_fifo_size().
  */
 static inline uint8_t *av_fifo_peek2(const AVFifoBuffer *f, int offs)
f5a478f6
 {
     uint8_t *ptr = f->rptr + offs;
     if (ptr >= f->end)
323b9306
         ptr = f->buffer + (ptr - f->end);
     else if (ptr < f->buffer)
         ptr = f->end - (f->buffer - ptr);
     return ptr;
f5a478f6
 }
323b9306
 
98790382
 #endif /* AVUTIL_FIFO_H */