fifo.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 fifo.h
  20. * A very simple circular buffer FIFO implementation.
  21. */
  22. #ifndef AVUTIL_FIFO_H
  23. #define AVUTIL_FIFO_H
  24. #include <stdint.h>
  25. #include "common.h"
  26. typedef struct AVFifoBuffer {
  27. uint8_t *buffer;
  28. uint8_t *rptr, *wptr, *end;
  29. } AVFifoBuffer;
  30. /**
  31. * Initializes an AVFifoBuffer.
  32. * @param *f AVFifoBuffer to initialize
  33. * @param size of FIFO
  34. * @return <0 for failure >=0 otherwise
  35. */
  36. int av_fifo_init(AVFifoBuffer *f, unsigned int size);
  37. /**
  38. * Frees an AVFifoBuffer.
  39. * @param *f AVFifoBuffer to free
  40. */
  41. void av_fifo_free(AVFifoBuffer *f);
  42. /**
  43. * Returns the amount of data in bytes in the AVFifoBuffer, that is the
  44. * amount of data you can read from it.
  45. * @param *f AVFifoBuffer to read from
  46. * @return size
  47. */
  48. int av_fifo_size(AVFifoBuffer *f);
  49. /**
  50. * Reads data from an AVFifoBuffer.
  51. * @param *f AVFifoBuffer to read from
  52. * @param *buf data destination
  53. * @param buf_size number of bytes to read
  54. */
  55. int av_fifo_read(AVFifoBuffer *f, uint8_t *buf, int buf_size);
  56. /**
  57. * Feeds data from an AVFifoBuffer to a user supplied callback.
  58. * @param *f AVFifoBuffer to read from
  59. * @param buf_size number of bytes to read
  60. * @param *func generic read function
  61. * @param *dest data destination
  62. */
  63. int av_fifo_generic_read(AVFifoBuffer *f, int buf_size, void (*func)(void*, void*, int), void* dest);
  64. /**
  65. * Writes data into an AVFifoBuffer.
  66. * @param *f AVFifoBuffer to write to
  67. * @param *buf data source
  68. * @param size data size
  69. */
  70. attribute_deprecated void av_fifo_write(AVFifoBuffer *f, const uint8_t *buf, int size);
  71. /**
  72. * Feeds data from a user supplied callback to an AVFifoBuffer.
  73. * @param *f AVFifoBuffer to write to
  74. * @param *src data source
  75. * @param size number of bytes to write
  76. * @param *func generic write function. First parameter is src,
  77. * second is dest_buf, third is dest_buf_size.
  78. * func must return the number of bytes written to dest_buf, or <= 0 to
  79. * indicate no more data available to write.
  80. * If func is NULL, src is interpreted as a simple byte array for source data.
  81. * @return the number of bytes written to the fifo.
  82. */
  83. int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size, int (*func)(void*, void*, int));
  84. #if LIBAVUTIL_VERSION_MAJOR < 50
  85. /**
  86. * Resizes an AVFifoBuffer.
  87. * @param *f AVFifoBuffer to resize
  88. * @param size new AVFifoBuffer size in bytes
  89. * @see av_fifo_realloc2()
  90. */
  91. attribute_deprecated void av_fifo_realloc(AVFifoBuffer *f, unsigned int size);
  92. #endif
  93. /**
  94. * Resizes an AVFifoBuffer.
  95. * @param *f AVFifoBuffer to resize
  96. * @param size new AVFifoBuffer size in bytes
  97. * @return <0 for failure >=0 otherwise
  98. */
  99. int av_fifo_realloc2(AVFifoBuffer *f, unsigned int size);
  100. /**
  101. * Reads and discards the specified amount of data from an AVFifoBuffer.
  102. * @param *f AVFifoBuffer to read from
  103. * @param size amount of data to read in bytes
  104. */
  105. void av_fifo_drain(AVFifoBuffer *f, int size);
  106. static inline uint8_t av_fifo_peek(AVFifoBuffer *f, int offs)
  107. {
  108. uint8_t *ptr = f->rptr + offs;
  109. if (ptr >= f->end)
  110. ptr -= f->end - f->buffer;
  111. return *ptr;
  112. }
  113. #endif /* AVUTIL_FIFO_H */