Path: blob/master/dep/ffmpeg/include/libavutil/fifo.h
4216 views
/*1* This file is part of FFmpeg.2*3* FFmpeg is free software; you can redistribute it and/or4* modify it under the terms of the GNU Lesser General Public5* License as published by the Free Software Foundation; either6* version 2.1 of the License, or (at your option) any later version.7*8* FFmpeg is distributed in the hope that it will be useful,9* but WITHOUT ANY WARRANTY; without even the implied warranty of10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU11* Lesser General Public License for more details.12*13* You should have received a copy of the GNU Lesser General Public14* License along with FFmpeg; if not, write to the Free Software15* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA16*/1718/**19* @file20* @ingroup lavu_fifo21* A generic FIFO API22*/2324#ifndef AVUTIL_FIFO_H25#define AVUTIL_FIFO_H2627#include <stddef.h>2829/**30* @defgroup lavu_fifo AVFifo31* @ingroup lavu_data32*33* @{34* A generic FIFO API35*/3637typedef struct AVFifo AVFifo;3839/**40* Callback for writing or reading from a FIFO, passed to (and invoked from) the41* av_fifo_*_cb() functions. It may be invoked multiple times from a single42* av_fifo_*_cb() call and may process less data than the maximum size indicated43* by nb_elems.44*45* @param opaque the opaque pointer provided to the av_fifo_*_cb() function46* @param buf the buffer for reading or writing the data, depending on which47* av_fifo_*_cb function is called48* @param nb_elems On entry contains the maximum number of elements that can be49* read from / written into buf. On success, the callback should50* update it to contain the number of elements actually written.51*52* @return 0 on success, a negative error code on failure (will be returned from53* the invoking av_fifo_*_cb() function)54*/55typedef int AVFifoCB(void *opaque, void *buf, size_t *nb_elems);5657/**58* Automatically resize the FIFO on writes, so that the data fits. This59* automatic resizing happens up to a limit that can be modified with60* av_fifo_auto_grow_limit().61*/62#define AV_FIFO_FLAG_AUTO_GROW (1 << 0)6364/**65* Allocate and initialize an AVFifo with a given element size.66*67* @param elems initial number of elements that can be stored in the FIFO68* @param elem_size Size in bytes of a single element. Further operations on69* the returned FIFO will implicitly use this element size.70* @param flags a combination of AV_FIFO_FLAG_*71*72* @return newly-allocated AVFifo on success, a negative error code on failure73*/74AVFifo *av_fifo_alloc2(size_t elems, size_t elem_size,75unsigned int flags);7677/**78* @return Element size for FIFO operations. This element size is set at79* FIFO allocation and remains constant during its lifetime80*/81size_t av_fifo_elem_size(const AVFifo *f);8283/**84* Set the maximum size (in elements) to which the FIFO can be resized85* automatically. Has no effect unless AV_FIFO_FLAG_AUTO_GROW is used.86*/87void av_fifo_auto_grow_limit(AVFifo *f, size_t max_elems);8889/**90* @return number of elements available for reading from the given FIFO.91*/92size_t av_fifo_can_read(const AVFifo *f);9394/**95* @return Number of elements that can be written into the given FIFO without96* growing it.97*98* In other words, this number of elements or less is guaranteed to fit99* into the FIFO. More data may be written when the100* AV_FIFO_FLAG_AUTO_GROW flag was specified at FIFO creation, but this101* may involve memory allocation, which can fail.102*/103size_t av_fifo_can_write(const AVFifo *f);104105/**106* Enlarge an AVFifo.107*108* On success, the FIFO will be large enough to hold exactly109* inc + av_fifo_can_read() + av_fifo_can_write()110* elements. In case of failure, the old FIFO is kept unchanged.111*112* @param f AVFifo to resize113* @param inc number of elements to allocate for, in addition to the current114* allocated size115* @return a non-negative number on success, a negative error code on failure116*/117int av_fifo_grow2(AVFifo *f, size_t inc);118119/**120* Write data into a FIFO.121*122* In case nb_elems > av_fifo_can_write(f) and the AV_FIFO_FLAG_AUTO_GROW flag123* was not specified at FIFO creation, nothing is written and an error124* is returned.125*126* Calling function is guaranteed to succeed if nb_elems <= av_fifo_can_write(f).127*128* @param f the FIFO buffer129* @param buf Data to be written. nb_elems * av_fifo_elem_size(f) bytes will be130* read from buf on success.131* @param nb_elems number of elements to write into FIFO132*133* @return a non-negative number on success, a negative error code on failure134*/135int av_fifo_write(AVFifo *f, const void *buf, size_t nb_elems);136137/**138* Write data from a user-provided callback into a FIFO.139*140* @param f the FIFO buffer141* @param read_cb Callback supplying the data to the FIFO. May be called142* multiple times.143* @param opaque opaque user data to be provided to read_cb144* @param nb_elems Should point to the maximum number of elements that can be145* written. Will be updated to contain the number of elements146* actually written.147*148* @return non-negative number on success, a negative error code on failure149*/150int av_fifo_write_from_cb(AVFifo *f, AVFifoCB read_cb,151void *opaque, size_t *nb_elems);152153/**154* Read data from a FIFO.155*156* In case nb_elems > av_fifo_can_read(f), nothing is read and an error157* is returned.158*159* @param f the FIFO buffer160* @param buf Buffer to store the data. nb_elems * av_fifo_elem_size(f) bytes161* will be written into buf on success.162* @param nb_elems number of elements to read from FIFO163*164* @return a non-negative number on success, a negative error code on failure165*/166int av_fifo_read(AVFifo *f, void *buf, size_t nb_elems);167168/**169* Feed data from a FIFO into a user-provided callback.170*171* @param f the FIFO buffer172* @param write_cb Callback the data will be supplied to. May be called173* multiple times.174* @param opaque opaque user data to be provided to write_cb175* @param nb_elems Should point to the maximum number of elements that can be176* read. Will be updated to contain the total number of elements177* actually sent to the callback.178*179* @return non-negative number on success, a negative error code on failure180*/181int av_fifo_read_to_cb(AVFifo *f, AVFifoCB write_cb,182void *opaque, size_t *nb_elems);183184/**185* Read data from a FIFO without modifying FIFO state.186*187* Returns an error if an attempt is made to peek to nonexistent elements188* (i.e. if offset + nb_elems is larger than av_fifo_can_read(f)).189*190* @param f the FIFO buffer191* @param buf Buffer to store the data. nb_elems * av_fifo_elem_size(f) bytes192* will be written into buf.193* @param nb_elems number of elements to read from FIFO194* @param offset number of initial elements to skip.195*196* @return a non-negative number on success, a negative error code on failure197*/198int av_fifo_peek(const AVFifo *f, void *buf, size_t nb_elems, size_t offset);199200/**201* Feed data from a FIFO into a user-provided callback.202*203* @param f the FIFO buffer204* @param write_cb Callback the data will be supplied to. May be called205* multiple times.206* @param opaque opaque user data to be provided to write_cb207* @param nb_elems Should point to the maximum number of elements that can be208* read. Will be updated to contain the total number of elements209* actually sent to the callback.210* @param offset number of initial elements to skip; offset + *nb_elems must not211* be larger than av_fifo_can_read(f).212*213* @return a non-negative number on success, a negative error code on failure214*/215int av_fifo_peek_to_cb(const AVFifo *f, AVFifoCB write_cb, void *opaque,216size_t *nb_elems, size_t offset);217218/**219* Discard the specified amount of data from an AVFifo.220* @param size number of elements to discard, MUST NOT be larger than221* av_fifo_can_read(f)222*/223void av_fifo_drain2(AVFifo *f, size_t size);224225/*226* Empty the AVFifo.227* @param f AVFifo to reset228*/229void av_fifo_reset2(AVFifo *f);230231/**232* Free an AVFifo and reset pointer to NULL.233* @param f Pointer to an AVFifo to free. *f == NULL is allowed.234*/235void av_fifo_freep2(AVFifo **f);236237/**238* @}239*/240241#endif /* AVUTIL_FIFO_H */242243244