You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
3.8KB

  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 "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. /**
  66. * Writes data into an AVFifoBuffer.
  67. * @param *f AVFifoBuffer to write to
  68. * @param *buf data source
  69. * @param size data size
  70. */
  71. attribute_deprecated void av_fifo_write(AVFifoBuffer *f, const uint8_t *buf, int size);
  72. /**
  73. * Feeds data from a user supplied callback to an AVFifoBuffer.
  74. * @param *f AVFifoBuffer to write to
  75. * @param *src data source
  76. * @param size number of bytes to write
  77. * @param *func generic write function. First parameter is src,
  78. * second is dest_buf, third is dest_buf_size.
  79. * func must return the number of bytes written to dest_buf, or <= 0 to
  80. * indicate no more data available to write.
  81. * If func is NULL, src is interpreted as a simple byte array for source data.
  82. * @return the number of bytes written to the fifo.
  83. */
  84. int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size, int (*func)(void*, void*, int));
  85. #if LIBAVUTIL_VERSION_MAJOR < 50
  86. /**
  87. * Resizes an AVFifoBuffer.
  88. * @param *f AVFifoBuffer to resize
  89. * @param size new AVFifoBuffer size in bytes
  90. * @see av_fifo_realloc2()
  91. */
  92. attribute_deprecated void av_fifo_realloc(AVFifoBuffer *f, unsigned int size);
  93. #endif
  94. /**
  95. * Resizes an AVFifoBuffer.
  96. * @param *f AVFifoBuffer to resize
  97. * @param size new AVFifoBuffer size in bytes
  98. * @return <0 for failure >=0 otherwise
  99. */
  100. int av_fifo_realloc2(AVFifoBuffer *f, unsigned int size);
  101. /**
  102. * Reads and discards the specified amount of data from an AVFifoBuffer.
  103. * @param *f AVFifoBuffer to read from
  104. * @param size amount of data to read in bytes
  105. */
  106. void av_fifo_drain(AVFifoBuffer *f, int size);
  107. static inline uint8_t av_fifo_peek(AVFifoBuffer *f, int offs)
  108. {
  109. uint8_t *ptr = f->rptr + offs;
  110. if (ptr >= f->end)
  111. ptr -= f->end - f->buffer;
  112. return *ptr;
  113. }
  114. #endif /* AVUTIL_FIFO_H */