yading@11: /* yading@11: * This file is part of FFmpeg. yading@11: * yading@11: * FFmpeg is free software; you can redistribute it and/or yading@11: * modify it under the terms of the GNU Lesser General Public yading@11: * License as published by the Free Software Foundation; either yading@11: * version 2.1 of the License, or (at your option) any later version. yading@11: * yading@11: * FFmpeg is distributed in the hope that it will be useful, yading@11: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@11: * Lesser General Public License for more details. yading@11: * yading@11: * You should have received a copy of the GNU Lesser General Public yading@11: * License along with FFmpeg; if not, write to the Free Software yading@11: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@11: */ yading@11: yading@11: /** yading@11: * @file yading@11: * a very simple circular buffer FIFO implementation yading@11: */ yading@11: yading@11: #ifndef AVUTIL_FIFO_H yading@11: #define AVUTIL_FIFO_H yading@11: yading@11: #include yading@11: #include "avutil.h" yading@11: #include "attributes.h" yading@11: yading@11: typedef struct AVFifoBuffer { yading@11: uint8_t *buffer; yading@11: uint8_t *rptr, *wptr, *end; yading@11: uint32_t rndx, wndx; yading@11: } AVFifoBuffer; yading@11: yading@11: /** yading@11: * Initialize an AVFifoBuffer. yading@11: * @param size of FIFO yading@11: * @return AVFifoBuffer or NULL in case of memory allocation failure yading@11: */ yading@11: AVFifoBuffer *av_fifo_alloc(unsigned int size); yading@11: yading@11: /** yading@11: * Free an AVFifoBuffer. yading@11: * @param f AVFifoBuffer to free yading@11: */ yading@11: void av_fifo_free(AVFifoBuffer *f); yading@11: yading@11: /** yading@11: * Reset the AVFifoBuffer to the state right after av_fifo_alloc, in particular it is emptied. yading@11: * @param f AVFifoBuffer to reset yading@11: */ yading@11: void av_fifo_reset(AVFifoBuffer *f); yading@11: yading@11: /** yading@11: * Return the amount of data in bytes in the AVFifoBuffer, that is the yading@11: * amount of data you can read from it. yading@11: * @param f AVFifoBuffer to read from yading@11: * @return size yading@11: */ yading@11: int av_fifo_size(AVFifoBuffer *f); yading@11: yading@11: /** yading@11: * Return the amount of space in bytes in the AVFifoBuffer, that is the yading@11: * amount of data you can write into it. yading@11: * @param f AVFifoBuffer to write into yading@11: * @return size yading@11: */ yading@11: int av_fifo_space(AVFifoBuffer *f); yading@11: yading@11: /** yading@11: * Feed data from an AVFifoBuffer to a user-supplied callback. yading@11: * @param f AVFifoBuffer to read from yading@11: * @param buf_size number of bytes to read yading@11: * @param func generic read function yading@11: * @param dest data destination yading@11: */ yading@11: int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size, void (*func)(void*, void*, int)); yading@11: yading@11: /** yading@11: * Feed data from a user-supplied callback to an AVFifoBuffer. yading@11: * @param f AVFifoBuffer to write to yading@11: * @param src data source; non-const since it may be used as a yading@11: * modifiable context by the function defined in func yading@11: * @param size number of bytes to write yading@11: * @param func generic write function; the first parameter is src, yading@11: * the second is dest_buf, the third is dest_buf_size. yading@11: * func must return the number of bytes written to dest_buf, or <= 0 to yading@11: * indicate no more data available to write. yading@11: * If func is NULL, src is interpreted as a simple byte array for source data. yading@11: * @return the number of bytes written to the FIFO yading@11: */ yading@11: int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size, int (*func)(void*, void*, int)); yading@11: yading@11: /** yading@11: * Resize an AVFifoBuffer. yading@11: * In case of reallocation failure, the old FIFO is kept unchanged. yading@11: * yading@11: * @param f AVFifoBuffer to resize yading@11: * @param size new AVFifoBuffer size in bytes yading@11: * @return <0 for failure, >=0 otherwise yading@11: */ yading@11: int av_fifo_realloc2(AVFifoBuffer *f, unsigned int size); yading@11: yading@11: /** yading@11: * Enlarge an AVFifoBuffer. yading@11: * In case of reallocation failure, the old FIFO is kept unchanged. yading@11: * The new fifo size may be larger than the requested size. yading@11: * yading@11: * @param f AVFifoBuffer to resize yading@11: * @param additional_space the amount of space in bytes to allocate in addition to av_fifo_size() yading@11: * @return <0 for failure, >=0 otherwise yading@11: */ yading@11: int av_fifo_grow(AVFifoBuffer *f, unsigned int additional_space); yading@11: yading@11: /** yading@11: * Read and discard the specified amount of data from an AVFifoBuffer. yading@11: * @param f AVFifoBuffer to read from yading@11: * @param size amount of data to read in bytes yading@11: */ yading@11: void av_fifo_drain(AVFifoBuffer *f, int size); yading@11: yading@11: /** yading@11: * Return a pointer to the data stored in a FIFO buffer at a certain offset. yading@11: * The FIFO buffer is not modified. yading@11: * yading@11: * @param f AVFifoBuffer to peek at, f must be non-NULL yading@11: * @param offs an offset in bytes, its absolute value must be less yading@11: * than the used buffer size or the returned pointer will yading@11: * point outside to the buffer data. yading@11: * The used buffer size can be checked with av_fifo_size(). yading@11: */ yading@11: static inline uint8_t *av_fifo_peek2(const AVFifoBuffer *f, int offs) yading@11: { yading@11: uint8_t *ptr = f->rptr + offs; yading@11: if (ptr >= f->end) yading@11: ptr = f->buffer + (ptr - f->end); yading@11: else if (ptr < f->buffer) yading@11: ptr = f->end - (f->buffer - ptr); yading@11: return ptr; yading@11: } yading@11: yading@11: #endif /* AVUTIL_FIFO_H */