fifo.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 <stdint.h>
  25. typedef struct AVFifoBuffer {
  26. uint8_t *buffer;
  27. uint8_t *rptr, *wptr, *end;
  28. uint32_t rndx, wndx;
  29. } AVFifoBuffer;
  30. /**
  31. * Initialize an AVFifoBuffer.
  32. * @param size of FIFO
  33. * @return AVFifoBuffer or NULL in case of memory allocation failure
  34. */
  35. AVFifoBuffer *av_fifo_alloc(unsigned int size);
  36. /**
  37. * Free an AVFifoBuffer.
  38. * @param *f AVFifoBuffer to free
  39. */
  40. void av_fifo_free(AVFifoBuffer *f);
  41. /**
  42. * Reset the AVFifoBuffer to the state right after av_fifo_alloc, in particular it is emptied.
  43. * @param *f AVFifoBuffer to reset
  44. */
  45. void av_fifo_reset(AVFifoBuffer *f);
  46. /**
  47. * Return the amount of data in bytes in the AVFifoBuffer, that is the
  48. * amount of data you can read from it.
  49. * @param *f AVFifoBuffer to read from
  50. * @return size
  51. */
  52. int av_fifo_size(AVFifoBuffer *f);
  53. /**
  54. * Return the amount of space in bytes in the AVFifoBuffer, that is the
  55. * amount of data you can write into it.
  56. * @param *f AVFifoBuffer to write into
  57. * @return size
  58. */
  59. int av_fifo_space(AVFifoBuffer *f);
  60. /**
  61. * Feed data from an AVFifoBuffer to a user-supplied callback.
  62. * @param *f AVFifoBuffer to read from
  63. * @param buf_size number of bytes to read
  64. * @param *func generic read function
  65. * @param *dest data destination
  66. */
  67. int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size, void (*func)(void*, void*, int));
  68. /**
  69. * Feed data from a user-supplied callback to an AVFifoBuffer.
  70. * @param *f AVFifoBuffer to write to
  71. * @param *src data source; non-const since it may be used as a
  72. * modifiable context by the function defined in func
  73. * @param size number of bytes to write
  74. * @param *func generic write function; the first parameter is src,
  75. * the second is dest_buf, the third is dest_buf_size.
  76. * func must return the number of bytes written to dest_buf, or <= 0 to
  77. * indicate no more data available to write.
  78. * If func is NULL, src is interpreted as a simple byte array for source data.
  79. * @return the number of bytes written to the FIFO
  80. */
  81. int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size, int (*func)(void*, void*, int));
  82. /**
  83. * Resize an AVFifoBuffer.
  84. * @param *f AVFifoBuffer to resize
  85. * @param size new AVFifoBuffer size in bytes
  86. * @return <0 for failure, >=0 otherwise
  87. */
  88. int av_fifo_realloc2(AVFifoBuffer *f, unsigned int size);
  89. /**
  90. * Read and discard the specified amount of data from an AVFifoBuffer.
  91. * @param *f AVFifoBuffer to read from
  92. * @param size amount of data to read in bytes
  93. */
  94. void av_fifo_drain(AVFifoBuffer *f, int size);
  95. static inline uint8_t av_fifo_peek(AVFifoBuffer *f, int offs)
  96. {
  97. uint8_t *ptr = f->rptr + offs;
  98. if (ptr >= f->end)
  99. ptr -= f->end - f->buffer;
  100. return *ptr;
  101. }
  102. #endif /* AVUTIL_FIFO_H */