fifo.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. * a very simple circular buffer FIFO implementation
  21. */
  22. #ifndef AVUTIL_FIFO_H
  23. #define AVUTIL_FIFO_H
  24. #include <stddef.h>
  25. #include <stdint.h>
  26. #include "attributes.h"
  27. #include "version.h"
  28. typedef struct AVFifo AVFifo;
  29. /**
  30. * Callback for writing or reading from a FIFO, passed to (and invoked from) the
  31. * av_fifo_*_cb() functions. It may be invoked multiple times from a single
  32. * av_fifo_*_cb() call and may process less data than the maximum size indicated
  33. * by nb_elems.
  34. *
  35. * @param opaque the opaque pointer provided to the av_fifo_*_cb() function
  36. * @param buf the buffer for reading or writing the data, depending on which
  37. * av_fifo_*_cb function is called
  38. * @param nb_elems On entry contains the maximum number of elements that can be
  39. * read from / written into buf. On success, the callback should
  40. * update it to contain the number of elements actually written.
  41. *
  42. * @return 0 on success, a negative error code on failure (will be returned from
  43. * the invoking av_fifo_*_cb() function)
  44. */
  45. typedef int AVFifoCB(void *opaque, void *buf, size_t *nb_elems);
  46. /**
  47. * Automatically resize the FIFO on writes, so that the data fits. This
  48. * automatic resizing happens up to a limit that can be modified with
  49. * av_fifo_auto_grow_limit().
  50. */
  51. #define AV_FIFO_FLAG_AUTO_GROW (1 << 0)
  52. /**
  53. * Allocate and initialize an AVFifo with a given element size.
  54. *
  55. * @param elems initial number of elements that can be stored in the FIFO
  56. * @param elem_size Size in bytes of a single element. Further operations on
  57. * the returned FIFO will implicitly use this element size.
  58. * @param flags a combination of AV_FIFO_FLAG_*
  59. *
  60. * @return newly-allocated AVFifo on success, a negative error code on failure
  61. */
  62. AVFifo *av_fifo_alloc2(size_t elems, size_t elem_size,
  63. unsigned int flags);
  64. /**
  65. * @return Element size for FIFO operations. This element size is set at
  66. * FIFO allocation and remains constant during its lifetime
  67. */
  68. size_t av_fifo_elem_size(const AVFifo *f);
  69. /**
  70. * Set the maximum size (in elements) to which the FIFO can be resized
  71. * automatically. Has no effect unless AV_FIFO_FLAG_AUTO_GROW is used.
  72. */
  73. void av_fifo_auto_grow_limit(AVFifo *f, size_t max_elems);
  74. /**
  75. * @return number of elements available for reading from the given FIFO.
  76. */
  77. size_t av_fifo_can_read(const AVFifo *f);
  78. /**
  79. * @return number of elements that can be written into the given FIFO.
  80. */
  81. size_t av_fifo_can_write(const AVFifo *f);
  82. /**
  83. * Enlarge an AVFifo.
  84. *
  85. * On success, the FIFO will be large enough to hold exactly
  86. * inc + av_fifo_can_read() + av_fifo_can_write()
  87. * elements. In case of failure, the old FIFO is kept unchanged.
  88. *
  89. * @param f AVFifo to resize
  90. * @param inc number of elements to allocate for, in addition to the current
  91. * allocated size
  92. * @return a non-negative number on success, a negative error code on failure
  93. */
  94. int av_fifo_grow2(AVFifo *f, size_t inc);
  95. /**
  96. * Write data into a FIFO.
  97. *
  98. * In case nb_elems > av_fifo_can_write(f), nothing is written and an error
  99. * is returned.
  100. *
  101. * @param f the FIFO buffer
  102. * @param buf Data to be written. nb_elems * av_fifo_elem_size(f) bytes will be
  103. * read from buf on success.
  104. * @param nb_elems number of elements to write into FIFO
  105. *
  106. * @return a non-negative number on success, a negative error code on failure
  107. */
  108. int av_fifo_write(AVFifo *f, const void *buf, size_t nb_elems);
  109. /**
  110. * Write data from a user-provided callback into a FIFO.
  111. *
  112. * @param f the FIFO buffer
  113. * @param read_cb Callback supplying the data to the FIFO. May be called
  114. * multiple times.
  115. * @param opaque opaque user data to be provided to read_cb
  116. * @param nb_elems Should point to the maximum number of elements that can be
  117. * written. Will be updated to contain the number of elements
  118. * actually written.
  119. *
  120. * @return non-negative number on success, a negative error code on failure
  121. */
  122. int av_fifo_write_from_cb(AVFifo *f, AVFifoCB read_cb,
  123. void *opaque, size_t *nb_elems);
  124. /**
  125. * Read data from a FIFO.
  126. *
  127. * In case nb_elems > av_fifo_can_read(f), nothing is read and an error
  128. * is returned.
  129. *
  130. * @param f the FIFO buffer
  131. * @param buf Buffer to store the data. nb_elems * av_fifo_elem_size(f) bytes
  132. * will be written into buf on success.
  133. * @param nb_elems number of elements to read from FIFO
  134. *
  135. * @return a non-negative number on success, a negative error code on failure
  136. */
  137. int av_fifo_read(AVFifo *f, void *buf, size_t nb_elems);
  138. /**
  139. * Feed data from a FIFO into a user-provided callback.
  140. *
  141. * @param f the FIFO buffer
  142. * @param write_cb Callback the data will be supplied to. May be called
  143. * multiple times.
  144. * @param opaque opaque user data to be provided to write_cb
  145. * @param nb_elems Should point to the maximum number of elements that can be
  146. * read. Will be updated to contain the total number of elements
  147. * actually sent to the callback.
  148. *
  149. * @return non-negative number on success, a negative error code on failure
  150. */
  151. int av_fifo_read_to_cb(AVFifo *f, AVFifoCB write_cb,
  152. void *opaque, size_t *nb_elems);
  153. /**
  154. * Read data from a FIFO without modifying FIFO state.
  155. *
  156. * Returns an error if an attempt is made to peek to nonexistent elements
  157. * (i.e. if offset + nb_elems is larger than av_fifo_can_read(f)).
  158. *
  159. * @param f the FIFO buffer
  160. * @param buf Buffer to store the data. nb_elems * av_fifo_elem_size(f) bytes
  161. * will be written into buf.
  162. * @param nb_elems number of elements to read from FIFO
  163. * @param offset number of initial elements to skip.
  164. *
  165. * @return a non-negative number on success, a negative error code on failure
  166. */
  167. int av_fifo_peek(AVFifo *f, void *buf, size_t nb_elems, size_t offset);
  168. /**
  169. * Feed data from a FIFO into a user-provided callback.
  170. *
  171. * @param f the FIFO buffer
  172. * @param write_cb Callback the data will be supplied to. May be called
  173. * multiple times.
  174. * @param opaque opaque user data to be provided to write_cb
  175. * @param nb_elems Should point to the maximum number of elements that can be
  176. * read. Will be updated to contain the total number of elements
  177. * actually sent to the callback.
  178. * @param offset number of initial elements to skip; offset + *nb_elems must not
  179. * be larger than av_fifo_can_read(f).
  180. *
  181. * @return a non-negative number on success, a negative error code on failure
  182. */
  183. int av_fifo_peek_to_cb(AVFifo *f, AVFifoCB write_cb, void *opaque,
  184. size_t *nb_elems, size_t offset);
  185. /**
  186. * Discard the specified amount of data from an AVFifo.
  187. * @param size number of elements to discard, MUST NOT be larger than
  188. * av_fifo_can_read(f)
  189. */
  190. void av_fifo_drain2(AVFifo *f, size_t size);
  191. /*
  192. * Empty the AVFifo.
  193. * @param f AVFifo to reset
  194. */
  195. void av_fifo_reset2(AVFifo *f);
  196. /**
  197. * Free an AVFifo and reset pointer to NULL.
  198. * @param f Pointer to an AVFifo to free. *f == NULL is allowed.
  199. */
  200. void av_fifo_freep2(AVFifo **f);
  201. #if FF_API_FIFO_OLD_API
  202. typedef struct AVFifoBuffer {
  203. uint8_t *buffer;
  204. uint8_t *rptr, *wptr, *end;
  205. uint32_t rndx, wndx;
  206. } AVFifoBuffer;
  207. /**
  208. * Initialize an AVFifoBuffer.
  209. * @param size of FIFO
  210. * @return AVFifoBuffer or NULL in case of memory allocation failure
  211. * @deprecated use av_fifo_alloc2()
  212. */
  213. attribute_deprecated
  214. AVFifoBuffer *av_fifo_alloc(unsigned int size);
  215. /**
  216. * Initialize an AVFifoBuffer.
  217. * @param nmemb number of elements
  218. * @param size size of the single element
  219. * @return AVFifoBuffer or NULL in case of memory allocation failure
  220. * @deprecated use av_fifo_alloc2()
  221. */
  222. attribute_deprecated
  223. AVFifoBuffer *av_fifo_alloc_array(size_t nmemb, size_t size);
  224. /**
  225. * Free an AVFifoBuffer.
  226. * @param f AVFifoBuffer to free
  227. * @deprecated use the AVFifo API with av_fifo_freep2()
  228. */
  229. attribute_deprecated
  230. void av_fifo_free(AVFifoBuffer *f);
  231. /**
  232. * Free an AVFifoBuffer and reset pointer to NULL.
  233. * @param f AVFifoBuffer to free
  234. * @deprecated use the AVFifo API with av_fifo_freep2()
  235. */
  236. attribute_deprecated
  237. void av_fifo_freep(AVFifoBuffer **f);
  238. /**
  239. * Reset the AVFifoBuffer to the state right after av_fifo_alloc, in particular it is emptied.
  240. * @param f AVFifoBuffer to reset
  241. * @deprecated use av_fifo_reset2() with the new AVFifo-API
  242. */
  243. attribute_deprecated
  244. void av_fifo_reset(AVFifoBuffer *f);
  245. /**
  246. * Return the amount of data in bytes in the AVFifoBuffer, that is the
  247. * amount of data you can read from it.
  248. * @param f AVFifoBuffer to read from
  249. * @return size
  250. * @deprecated use av_fifo_can_read() with the new AVFifo-API
  251. */
  252. attribute_deprecated
  253. int av_fifo_size(const AVFifoBuffer *f);
  254. /**
  255. * Return the amount of space in bytes in the AVFifoBuffer, that is the
  256. * amount of data you can write into it.
  257. * @param f AVFifoBuffer to write into
  258. * @return size
  259. * @deprecated use av_fifo_can_write() with the new AVFifo-API
  260. */
  261. attribute_deprecated
  262. int av_fifo_space(const AVFifoBuffer *f);
  263. /**
  264. * Feed data at specific position from an AVFifoBuffer to a user-supplied callback.
  265. * Similar as av_fifo_gereric_read but without discarding data.
  266. * @param f AVFifoBuffer to read from
  267. * @param offset offset from current read position
  268. * @param buf_size number of bytes to read
  269. * @param func generic read function
  270. * @param dest data destination
  271. *
  272. * @return a non-negative number on success, a negative error code on failure
  273. *
  274. * @deprecated use the new AVFifo-API with av_fifo_peek() when func == NULL,
  275. * av_fifo_peek_to_cb() otherwise
  276. */
  277. attribute_deprecated
  278. int av_fifo_generic_peek_at(AVFifoBuffer *f, void *dest, int offset, int buf_size, void (*func)(void*, void*, int));
  279. /**
  280. * Feed data from an AVFifoBuffer to a user-supplied callback.
  281. * Similar as av_fifo_gereric_read but without discarding data.
  282. * @param f AVFifoBuffer to read from
  283. * @param buf_size number of bytes to read
  284. * @param func generic read function
  285. * @param dest data destination
  286. *
  287. * @return a non-negative number on success, a negative error code on failure
  288. *
  289. * @deprecated use the new AVFifo-API with av_fifo_peek() when func == NULL,
  290. * av_fifo_peek_to_cb() otherwise
  291. */
  292. attribute_deprecated
  293. int av_fifo_generic_peek(AVFifoBuffer *f, void *dest, int buf_size, void (*func)(void*, void*, int));
  294. /**
  295. * Feed data from an AVFifoBuffer to a user-supplied callback.
  296. * @param f AVFifoBuffer to read from
  297. * @param buf_size number of bytes to read
  298. * @param func generic read function
  299. * @param dest data destination
  300. *
  301. * @return a non-negative number on success, a negative error code on failure
  302. *
  303. * @deprecated use the new AVFifo-API with av_fifo_read() when func == NULL,
  304. * av_fifo_read_to_cb() otherwise
  305. */
  306. attribute_deprecated
  307. int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size, void (*func)(void*, void*, int));
  308. /**
  309. * Feed data from a user-supplied callback to an AVFifoBuffer.
  310. * @param f AVFifoBuffer to write to
  311. * @param src data source; non-const since it may be used as a
  312. * modifiable context by the function defined in func
  313. * @param size number of bytes to write
  314. * @param func generic write function; the first parameter is src,
  315. * the second is dest_buf, the third is dest_buf_size.
  316. * func must return the number of bytes written to dest_buf, or <= 0 to
  317. * indicate no more data available to write.
  318. * If func is NULL, src is interpreted as a simple byte array for source data.
  319. * @return the number of bytes written to the FIFO or a negative error code on failure
  320. *
  321. * @deprecated use the new AVFifo-API with av_fifo_write() when func == NULL,
  322. * av_fifo_write_from_cb() otherwise
  323. */
  324. attribute_deprecated
  325. int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size, int (*func)(void*, void*, int));
  326. /**
  327. * Resize an AVFifoBuffer.
  328. * In case of reallocation failure, the old FIFO is kept unchanged.
  329. *
  330. * @param f AVFifoBuffer to resize
  331. * @param size new AVFifoBuffer size in bytes
  332. * @return <0 for failure, >=0 otherwise
  333. *
  334. * @deprecated use the new AVFifo-API with av_fifo_grow2() to increase FIFO size,
  335. * decreasing FIFO size is not supported
  336. */
  337. attribute_deprecated
  338. int av_fifo_realloc2(AVFifoBuffer *f, unsigned int size);
  339. /**
  340. * Enlarge an AVFifoBuffer.
  341. * In case of reallocation failure, the old FIFO is kept unchanged.
  342. * The new fifo size may be larger than the requested size.
  343. *
  344. * @param f AVFifoBuffer to resize
  345. * @param additional_space the amount of space in bytes to allocate in addition to av_fifo_size()
  346. * @return <0 for failure, >=0 otherwise
  347. *
  348. * @deprecated use the new AVFifo-API with av_fifo_grow2(); note that unlike
  349. * this function it adds to the allocated size, rather than to the used size
  350. */
  351. attribute_deprecated
  352. int av_fifo_grow(AVFifoBuffer *f, unsigned int additional_space);
  353. /**
  354. * Read and discard the specified amount of data from an AVFifoBuffer.
  355. * @param f AVFifoBuffer to read from
  356. * @param size amount of data to read in bytes
  357. *
  358. * @deprecated use the new AVFifo-API with av_fifo_drain2()
  359. */
  360. attribute_deprecated
  361. void av_fifo_drain(AVFifoBuffer *f, int size);
  362. #if FF_API_FIFO_PEEK2
  363. /**
  364. * Return a pointer to the data stored in a FIFO buffer at a certain offset.
  365. * The FIFO buffer is not modified.
  366. *
  367. * @param f AVFifoBuffer to peek at, f must be non-NULL
  368. * @param offs an offset in bytes, its absolute value must be less
  369. * than the used buffer size or the returned pointer will
  370. * point outside to the buffer data.
  371. * The used buffer size can be checked with av_fifo_size().
  372. * @deprecated use the new AVFifo-API with av_fifo_peek() or av_fifo_peek_to_cb()
  373. */
  374. attribute_deprecated
  375. static inline uint8_t *av_fifo_peek2(const AVFifoBuffer *f, int offs)
  376. {
  377. uint8_t *ptr = f->rptr + offs;
  378. if (ptr >= f->end)
  379. ptr = f->buffer + (ptr - f->end);
  380. else if (ptr < f->buffer)
  381. ptr = f->end - (f->buffer - ptr);
  382. return ptr;
  383. }
  384. #endif
  385. #endif
  386. #endif /* AVUTIL_FIFO_H */