fifo.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. * @file
  20. * @ingroup lavu_fifo
  21. * A generic FIFO API
  22. */
  23. #ifndef AVUTIL_FIFO_H
  24. #define AVUTIL_FIFO_H
  25. #include <stddef.h>
  26. #include <stdint.h>
  27. #include "attributes.h"
  28. #include "version.h"
  29. /**
  30. * @defgroup lavu_fifo AVFifo
  31. * @ingroup lavu_data
  32. *
  33. * @{
  34. * A generic FIFO API
  35. */
  36. typedef struct AVFifo AVFifo;
  37. /**
  38. * Callback for writing or reading from a FIFO, passed to (and invoked from) the
  39. * av_fifo_*_cb() functions. It may be invoked multiple times from a single
  40. * av_fifo_*_cb() call and may process less data than the maximum size indicated
  41. * by nb_elems.
  42. *
  43. * @param opaque the opaque pointer provided to the av_fifo_*_cb() function
  44. * @param buf the buffer for reading or writing the data, depending on which
  45. * av_fifo_*_cb function is called
  46. * @param nb_elems On entry contains the maximum number of elements that can be
  47. * read from / written into buf. On success, the callback should
  48. * update it to contain the number of elements actually written.
  49. *
  50. * @return 0 on success, a negative error code on failure (will be returned from
  51. * the invoking av_fifo_*_cb() function)
  52. */
  53. typedef int AVFifoCB(void *opaque, void *buf, size_t *nb_elems);
  54. /**
  55. * Automatically resize the FIFO on writes, so that the data fits. This
  56. * automatic resizing happens up to a limit that can be modified with
  57. * av_fifo_auto_grow_limit().
  58. */
  59. #define AV_FIFO_FLAG_AUTO_GROW (1 << 0)
  60. /**
  61. * Allocate and initialize an AVFifo with a given element size.
  62. *
  63. * @param elems initial number of elements that can be stored in the FIFO
  64. * @param elem_size Size in bytes of a single element. Further operations on
  65. * the returned FIFO will implicitly use this element size.
  66. * @param flags a combination of AV_FIFO_FLAG_*
  67. *
  68. * @return newly-allocated AVFifo on success, a negative error code on failure
  69. */
  70. AVFifo *av_fifo_alloc2(size_t elems, size_t elem_size,
  71. unsigned int flags);
  72. /**
  73. * @return Element size for FIFO operations. This element size is set at
  74. * FIFO allocation and remains constant during its lifetime
  75. */
  76. size_t av_fifo_elem_size(const AVFifo *f);
  77. /**
  78. * Set the maximum size (in elements) to which the FIFO can be resized
  79. * automatically. Has no effect unless AV_FIFO_FLAG_AUTO_GROW is used.
  80. */
  81. void av_fifo_auto_grow_limit(AVFifo *f, size_t max_elems);
  82. /**
  83. * @return number of elements available for reading from the given FIFO.
  84. */
  85. size_t av_fifo_can_read(const AVFifo *f);
  86. /**
  87. * @return Number of elements that can be written into the given FIFO without
  88. * growing it.
  89. *
  90. * In other words, this number of elements or less is guaranteed to fit
  91. * into the FIFO. More data may be written when the
  92. * AV_FIFO_FLAG_AUTO_GROW flag was specified at FIFO creation, but this
  93. * may involve memory allocation, which can fail.
  94. */
  95. size_t av_fifo_can_write(const AVFifo *f);
  96. /**
  97. * Enlarge an AVFifo.
  98. *
  99. * On success, the FIFO will be large enough to hold exactly
  100. * inc + av_fifo_can_read() + av_fifo_can_write()
  101. * elements. In case of failure, the old FIFO is kept unchanged.
  102. *
  103. * @param f AVFifo to resize
  104. * @param inc number of elements to allocate for, in addition to the current
  105. * allocated size
  106. * @return a non-negative number on success, a negative error code on failure
  107. */
  108. int av_fifo_grow2(AVFifo *f, size_t inc);
  109. /**
  110. * Write data into a FIFO.
  111. *
  112. * In case nb_elems > av_fifo_can_write(f) and the AV_FIFO_FLAG_AUTO_GROW flag
  113. * was not specified at FIFO creation, nothing is written and an error
  114. * is returned.
  115. *
  116. * Calling function is guaranteed to succeed if nb_elems <= av_fifo_can_write(f).
  117. *
  118. * @param f the FIFO buffer
  119. * @param buf Data to be written. nb_elems * av_fifo_elem_size(f) bytes will be
  120. * read from buf on success.
  121. * @param nb_elems number of elements to write into FIFO
  122. *
  123. * @return a non-negative number on success, a negative error code on failure
  124. */
  125. int av_fifo_write(AVFifo *f, const void *buf, size_t nb_elems);
  126. /**
  127. * Write data from a user-provided callback into a FIFO.
  128. *
  129. * @param f the FIFO buffer
  130. * @param read_cb Callback supplying the data to the FIFO. May be called
  131. * multiple times.
  132. * @param opaque opaque user data to be provided to read_cb
  133. * @param nb_elems Should point to the maximum number of elements that can be
  134. * written. Will be updated to contain the number of elements
  135. * actually written.
  136. *
  137. * @return non-negative number on success, a negative error code on failure
  138. */
  139. int av_fifo_write_from_cb(AVFifo *f, AVFifoCB read_cb,
  140. void *opaque, size_t *nb_elems);
  141. /**
  142. * Read data from a FIFO.
  143. *
  144. * In case nb_elems > av_fifo_can_read(f), nothing is read and an error
  145. * is returned.
  146. *
  147. * @param f the FIFO buffer
  148. * @param buf Buffer to store the data. nb_elems * av_fifo_elem_size(f) bytes
  149. * will be written into buf on success.
  150. * @param nb_elems number of elements to read from FIFO
  151. *
  152. * @return a non-negative number on success, a negative error code on failure
  153. */
  154. int av_fifo_read(AVFifo *f, void *buf, size_t nb_elems);
  155. /**
  156. * Feed data from a FIFO into a user-provided callback.
  157. *
  158. * @param f the FIFO buffer
  159. * @param write_cb Callback the data will be supplied to. May be called
  160. * multiple times.
  161. * @param opaque opaque user data to be provided to write_cb
  162. * @param nb_elems Should point to the maximum number of elements that can be
  163. * read. Will be updated to contain the total number of elements
  164. * actually sent to the callback.
  165. *
  166. * @return non-negative number on success, a negative error code on failure
  167. */
  168. int av_fifo_read_to_cb(AVFifo *f, AVFifoCB write_cb,
  169. void *opaque, size_t *nb_elems);
  170. /**
  171. * Read data from a FIFO without modifying FIFO state.
  172. *
  173. * Returns an error if an attempt is made to peek to nonexistent elements
  174. * (i.e. if offset + nb_elems is larger than av_fifo_can_read(f)).
  175. *
  176. * @param f the FIFO buffer
  177. * @param buf Buffer to store the data. nb_elems * av_fifo_elem_size(f) bytes
  178. * will be written into buf.
  179. * @param nb_elems number of elements to read from FIFO
  180. * @param offset number of initial elements to skip.
  181. *
  182. * @return a non-negative number on success, a negative error code on failure
  183. */
  184. int av_fifo_peek(const AVFifo *f, void *buf, size_t nb_elems, size_t offset);
  185. /**
  186. * Feed data from a FIFO into a user-provided callback.
  187. *
  188. * @param f the FIFO buffer
  189. * @param write_cb Callback the data will be supplied to. May be called
  190. * multiple times.
  191. * @param opaque opaque user data to be provided to write_cb
  192. * @param nb_elems Should point to the maximum number of elements that can be
  193. * read. Will be updated to contain the total number of elements
  194. * actually sent to the callback.
  195. * @param offset number of initial elements to skip; offset + *nb_elems must not
  196. * be larger than av_fifo_can_read(f).
  197. *
  198. * @return a non-negative number on success, a negative error code on failure
  199. */
  200. int av_fifo_peek_to_cb(const AVFifo *f, AVFifoCB write_cb, void *opaque,
  201. size_t *nb_elems, size_t offset);
  202. /**
  203. * Discard the specified amount of data from an AVFifo.
  204. * @param size number of elements to discard, MUST NOT be larger than
  205. * av_fifo_can_read(f)
  206. */
  207. void av_fifo_drain2(AVFifo *f, size_t size);
  208. /*
  209. * Empty the AVFifo.
  210. * @param f AVFifo to reset
  211. */
  212. void av_fifo_reset2(AVFifo *f);
  213. /**
  214. * Free an AVFifo and reset pointer to NULL.
  215. * @param f Pointer to an AVFifo to free. *f == NULL is allowed.
  216. */
  217. void av_fifo_freep2(AVFifo **f);
  218. /**
  219. * @}
  220. */
  221. #endif /* AVUTIL_FIFO_H */