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 
24 module ffmpeg.libavutil.fifo;
25 
26 extern (C):
27 import ffmpeg; @nogc nothrow:
28 
29 struct AVFifoBuffer
30 {
31     ubyte* buffer;
32     ubyte* rptr;
33     ubyte* wptr;
34     ubyte* end;
35     uint rndx;
36     uint wndx;
37 }
38 
39 /**
40  * Initialize an AVFifoBuffer.
41  * @param size of FIFO
42  * @return AVFifoBuffer or NULL in case of memory allocation failure
43  */
44 AVFifoBuffer* av_fifo_alloc (uint size);
45 
46 /**
47  * Initialize an AVFifoBuffer.
48  * @param nmemb number of elements
49  * @param size  size of the single element
50  * @return AVFifoBuffer or NULL in case of memory allocation failure
51  */
52 AVFifoBuffer* av_fifo_alloc_array (size_t nmemb, size_t size);
53 
54 /**
55  * Free an AVFifoBuffer.
56  * @param f AVFifoBuffer to free
57  */
58 void av_fifo_free (AVFifoBuffer* f);
59 
60 /**
61  * Free an AVFifoBuffer and reset pointer to NULL.
62  * @param f AVFifoBuffer to free
63  */
64 void av_fifo_freep (AVFifoBuffer** f);
65 
66 /**
67  * Reset the AVFifoBuffer to the state right after av_fifo_alloc, in particular it is emptied.
68  * @param f AVFifoBuffer to reset
69  */
70 void av_fifo_reset (AVFifoBuffer* f);
71 
72 /**
73  * Return the amount of data in bytes in the AVFifoBuffer, that is the
74  * amount of data you can read from it.
75  * @param f AVFifoBuffer to read from
76  * @return size
77  */
78 int av_fifo_size (const(AVFifoBuffer)* f);
79 
80 /**
81  * Return the amount of space in bytes in the AVFifoBuffer, that is the
82  * amount of data you can write into it.
83  * @param f AVFifoBuffer to write into
84  * @return size
85  */
86 int av_fifo_space (const(AVFifoBuffer)* f);
87 
88 /**
89  * Feed data at specific position from an AVFifoBuffer to a user-supplied callback.
90  * Similar as av_fifo_gereric_read but without discarding data.
91  * @param f AVFifoBuffer to read from
92  * @param offset offset from current read position
93  * @param buf_size number of bytes to read
94  * @param func generic read function
95  * @param dest data destination
96  */
97 int av_fifo_generic_peek_at (AVFifoBuffer* f, void* dest, int offset, int buf_size, void function (void*, void*, int) func);
98 
99 /**
100  * Feed data from an AVFifoBuffer to a user-supplied callback.
101  * Similar as av_fifo_gereric_read but without discarding data.
102  * @param f AVFifoBuffer to read from
103  * @param buf_size number of bytes to read
104  * @param func generic read function
105  * @param dest data destination
106  */
107 int av_fifo_generic_peek (AVFifoBuffer* f, void* dest, int buf_size, void function (void*, void*, int) func);
108 
109 /**
110  * Feed data from an AVFifoBuffer to a user-supplied callback.
111  * @param f AVFifoBuffer to read from
112  * @param buf_size number of bytes to read
113  * @param func generic read function
114  * @param dest data destination
115  */
116 int av_fifo_generic_read (AVFifoBuffer* f, void* dest, int buf_size, void function (void*, void*, int) func);
117 
118 /**
119  * Feed data from a user-supplied callback to an AVFifoBuffer.
120  * @param f AVFifoBuffer to write to
121  * @param src data source; non-const since it may be used as a
122  * modifiable context by the function defined in func
123  * @param size number of bytes to write
124  * @param func generic write function; the first parameter is src,
125  * the second is dest_buf, the third is dest_buf_size.
126  * func must return the number of bytes written to dest_buf, or <= 0 to
127  * indicate no more data available to write.
128  * If func is NULL, src is interpreted as a simple byte array for source data.
129  * @return the number of bytes written to the FIFO
130  */
131 int av_fifo_generic_write (AVFifoBuffer* f, void* src, int size, int function (void*, void*, int) func);
132 
133 /**
134  * Resize an AVFifoBuffer.
135  * In case of reallocation failure, the old FIFO is kept unchanged.
136  *
137  * @param f AVFifoBuffer to resize
138  * @param size new AVFifoBuffer size in bytes
139  * @return <0 for failure, >=0 otherwise
140  */
141 int av_fifo_realloc2 (AVFifoBuffer* f, uint size);
142 
143 /**
144  * Enlarge an AVFifoBuffer.
145  * In case of reallocation failure, the old FIFO is kept unchanged.
146  * The new fifo size may be larger than the requested size.
147  *
148  * @param f AVFifoBuffer to resize
149  * @param additional_space the amount of space in bytes to allocate in addition to av_fifo_size()
150  * @return <0 for failure, >=0 otherwise
151  */
152 int av_fifo_grow (AVFifoBuffer* f, uint additional_space);
153 
154 /**
155  * Read and discard the specified amount of data from an AVFifoBuffer.
156  * @param f AVFifoBuffer to read from
157  * @param size amount of data to read in bytes
158  */
159 void av_fifo_drain (AVFifoBuffer* f, int size);
160 
161 /**
162  * Return a pointer to the data stored in a FIFO buffer at a certain offset.
163  * The FIFO buffer is not modified.
164  *
165  * @param f    AVFifoBuffer to peek at, f must be non-NULL
166  * @param offs an offset in bytes, its absolute value must be less
167  *             than the used buffer size or the returned pointer will
168  *             point outside to the buffer data.
169  *             The used buffer size can be checked with av_fifo_size().
170  */
171 ubyte* av_fifo_peek2 (const(AVFifoBuffer)* f, int offs);
172 
173 /* AVUTIL_FIFO_H */