fifo.h 3.8 KB

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