1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 /**
20  * @file
21  * a very simple circular buffer FIFO implementation
22  */
23 module ffmpeg.libavutil.fifo;
24 extern (C) @nogc nothrow:
25 
26 struct AVFifoBuffer
27 {
28     ubyte* buffer;
29     ubyte* rptr;
30     ubyte* wptr;
31     ubyte* end;
32     uint rndx;
33     uint wndx;
34 }
35 
36 /**
37  * Initialize an AVFifoBuffer.
38  * @param size of FIFO
39  * @return AVFifoBuffer or NULL in case of memory allocation failure
40  */
41 AVFifoBuffer* av_fifo_alloc (uint size);
42 
43 /**
44  * Initialize an AVFifoBuffer.
45  * @param nmemb number of elements
46  * @param size  size of the single element
47  * @return AVFifoBuffer or NULL in case of memory allocation failure
48  */
49 AVFifoBuffer* av_fifo_alloc_array (size_t nmemb, size_t size);
50 
51 /**
52  * Free an AVFifoBuffer.
53  * @param f AVFifoBuffer to free
54  */
55 void av_fifo_free (AVFifoBuffer* f);
56 
57 /**
58  * Free an AVFifoBuffer and reset pointer to NULL.
59  * @param f AVFifoBuffer to free
60  */
61 void av_fifo_freep (AVFifoBuffer** f);
62 
63 /**
64  * Reset the AVFifoBuffer to the state right after av_fifo_alloc, in particular it is emptied.
65  * @param f AVFifoBuffer to reset
66  */
67 void av_fifo_reset (AVFifoBuffer* f);
68 
69 /**
70  * Return the amount of data in bytes in the AVFifoBuffer, that is the
71  * amount of data you can read from it.
72  * @param f AVFifoBuffer to read from
73  * @return size
74  */
75 int av_fifo_size (const(AVFifoBuffer)* f);
76 
77 /**
78  * Return the amount of space in bytes in the AVFifoBuffer, that is the
79  * amount of data you can write into it.
80  * @param f AVFifoBuffer to write into
81  * @return size
82  */
83 int av_fifo_space (const(AVFifoBuffer)* f);
84 
85 /**
86  * Feed data at specific position from an AVFifoBuffer to a user-supplied callback.
87  * Similar as av_fifo_gereric_read but without discarding data.
88  * @param f AVFifoBuffer to read from
89  * @param offset offset from current read position
90  * @param buf_size number of bytes to read
91  * @param func generic read function
92  * @param dest data destination
93  */
94 int av_fifo_generic_peek_at (AVFifoBuffer* f, void* dest, int offset, int buf_size, void function (void*, void*, int) func);
95 
96 /**
97  * Feed data from an AVFifoBuffer to a user-supplied callback.
98  * Similar as av_fifo_gereric_read but without discarding data.
99  * @param f AVFifoBuffer to read from
100  * @param buf_size number of bytes to read
101  * @param func generic read function
102  * @param dest data destination
103  */
104 int av_fifo_generic_peek (AVFifoBuffer* f, void* dest, int buf_size, void function (void*, void*, int) func);
105 
106 /**
107  * Feed data from an AVFifoBuffer to a user-supplied callback.
108  * @param f AVFifoBuffer to read from
109  * @param buf_size number of bytes to read
110  * @param func generic read function
111  * @param dest data destination
112  */
113 int av_fifo_generic_read (AVFifoBuffer* f, void* dest, int buf_size, void function (void*, void*, int) func);
114 
115 /**
116  * Feed data from a user-supplied callback to an AVFifoBuffer.
117  * @param f AVFifoBuffer to write to
118  * @param src data source; non-const since it may be used as a
119  * modifiable context by the function defined in func
120  * @param size number of bytes to write
121  * @param func generic write function; the first parameter is src,
122  * the second is dest_buf, the third is dest_buf_size.
123  * func must return the number of bytes written to dest_buf, or <= 0 to
124  * indicate no more data available to write.
125  * If func is NULL, src is interpreted as a simple byte array for source data.
126  * @return the number of bytes written to the FIFO
127  */
128 int av_fifo_generic_write (AVFifoBuffer* f, void* src, int size, int function (void*, void*, int) func);
129 
130 /**
131  * Resize an AVFifoBuffer.
132  * In case of reallocation failure, the old FIFO is kept unchanged.
133  *
134  * @param f AVFifoBuffer to resize
135  * @param size new AVFifoBuffer size in bytes
136  * @return <0 for failure, >=0 otherwise
137  */
138 int av_fifo_realloc2 (AVFifoBuffer* f, uint size);
139 
140 /**
141  * Enlarge an AVFifoBuffer.
142  * In case of reallocation failure, the old FIFO is kept unchanged.
143  * The new fifo size may be larger than the requested size.
144  *
145  * @param f AVFifoBuffer to resize
146  * @param additional_space the amount of space in bytes to allocate in addition to av_fifo_size()
147  * @return <0 for failure, >=0 otherwise
148  */
149 int av_fifo_grow (AVFifoBuffer* f, uint additional_space);
150 
151 /**
152  * Read and discard the specified amount of data from an AVFifoBuffer.
153  * @param f AVFifoBuffer to read from
154  * @param size amount of data to read in bytes
155  */
156 void av_fifo_drain (AVFifoBuffer* f, int size);
157 
158 /**
159  * Return a pointer to the data stored in a FIFO buffer at a certain offset.
160  * The FIFO buffer is not modified.
161  *
162  * @param f    AVFifoBuffer to peek at, f must be non-NULL
163  * @param offs an offset in bytes, its absolute value must be less
164  *             than the used buffer size or the returned pointer will
165  *             point outside to the buffer data.
166  *             The used buffer size can be checked with av_fifo_size().
167  */
168 ubyte* av_fifo_peek2 (const(AVFifoBuffer)* f, int offs);
169 
170 /* AVUTIL_FIFO_H */