2
0

fifo.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * a very simple circular buffer FIFO implementation
  3. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
  4. * Copyright (c) 2006 Roman Shaposhnik
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "avassert.h"
  23. #include "common.h"
  24. #include "fifo.h"
  25. static AVFifoBuffer *fifo_alloc_common(void *buffer, size_t size)
  26. {
  27. AVFifoBuffer *f;
  28. if (!buffer)
  29. return NULL;
  30. f = av_mallocz(sizeof(AVFifoBuffer));
  31. if (!f) {
  32. av_free(buffer);
  33. return NULL;
  34. }
  35. f->buffer = buffer;
  36. f->end = f->buffer + size;
  37. av_fifo_reset(f);
  38. return f;
  39. }
  40. AVFifoBuffer *av_fifo_alloc(unsigned int size)
  41. {
  42. void *buffer = av_malloc(size);
  43. return fifo_alloc_common(buffer, size);
  44. }
  45. AVFifoBuffer *av_fifo_alloc_array(size_t nmemb, size_t size)
  46. {
  47. void *buffer = av_malloc_array(nmemb, size);
  48. return fifo_alloc_common(buffer, nmemb * size);
  49. }
  50. void av_fifo_free(AVFifoBuffer *f)
  51. {
  52. if (f) {
  53. av_freep(&f->buffer);
  54. av_free(f);
  55. }
  56. }
  57. void av_fifo_freep(AVFifoBuffer **f)
  58. {
  59. if (f) {
  60. av_fifo_free(*f);
  61. *f = NULL;
  62. }
  63. }
  64. void av_fifo_reset(AVFifoBuffer *f)
  65. {
  66. f->wptr = f->rptr = f->buffer;
  67. f->wndx = f->rndx = 0;
  68. }
  69. int av_fifo_size(const AVFifoBuffer *f)
  70. {
  71. return (uint32_t)(f->wndx - f->rndx);
  72. }
  73. int av_fifo_space(const AVFifoBuffer *f)
  74. {
  75. return f->end - f->buffer - av_fifo_size(f);
  76. }
  77. int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size)
  78. {
  79. unsigned int old_size = f->end - f->buffer;
  80. if (old_size < new_size) {
  81. int len = av_fifo_size(f);
  82. AVFifoBuffer *f2 = av_fifo_alloc(new_size);
  83. if (!f2)
  84. return AVERROR(ENOMEM);
  85. av_fifo_generic_read(f, f2->buffer, len, NULL);
  86. f2->wptr += len;
  87. f2->wndx += len;
  88. av_free(f->buffer);
  89. *f = *f2;
  90. av_free(f2);
  91. }
  92. return 0;
  93. }
  94. int av_fifo_grow(AVFifoBuffer *f, unsigned int size)
  95. {
  96. unsigned int old_size = f->end - f->buffer;
  97. if(size + (unsigned)av_fifo_size(f) < size)
  98. return AVERROR(EINVAL);
  99. size += av_fifo_size(f);
  100. if (old_size < size)
  101. return av_fifo_realloc2(f, FFMAX(size, 2*old_size));
  102. return 0;
  103. }
  104. /* src must NOT be const as it can be a context for func that may need
  105. * updating (like a pointer or byte counter) */
  106. int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size,
  107. int (*func)(void *, void *, int))
  108. {
  109. int total = size;
  110. uint32_t wndx= f->wndx;
  111. uint8_t *wptr= f->wptr;
  112. do {
  113. int len = FFMIN(f->end - wptr, size);
  114. if (func) {
  115. len = func(src, wptr, len);
  116. if (len <= 0)
  117. break;
  118. } else {
  119. memcpy(wptr, src, len);
  120. src = (uint8_t *)src + len;
  121. }
  122. // Write memory barrier needed for SMP here in theory
  123. wptr += len;
  124. if (wptr >= f->end)
  125. wptr = f->buffer;
  126. wndx += len;
  127. size -= len;
  128. } while (size > 0);
  129. f->wndx= wndx;
  130. f->wptr= wptr;
  131. return total - size;
  132. }
  133. int av_fifo_generic_peek_at(AVFifoBuffer *f, void *dest, int offset, int buf_size, void (*func)(void*, void*, int))
  134. {
  135. uint8_t *rptr = f->rptr;
  136. av_assert2(offset >= 0);
  137. /*
  138. * *ndx are indexes modulo 2^32, they are intended to overflow,
  139. * to handle *ndx greater than 4gb.
  140. */
  141. av_assert2(buf_size + (unsigned)offset <= f->wndx - f->rndx);
  142. if (offset >= f->end - rptr)
  143. rptr += offset - (f->end - f->buffer);
  144. else
  145. rptr += offset;
  146. while (buf_size > 0) {
  147. int len;
  148. if (rptr >= f->end)
  149. rptr -= f->end - f->buffer;
  150. len = FFMIN(f->end - rptr, buf_size);
  151. if (func)
  152. func(dest, rptr, len);
  153. else {
  154. memcpy(dest, rptr, len);
  155. dest = (uint8_t *)dest + len;
  156. }
  157. buf_size -= len;
  158. rptr += len;
  159. }
  160. return 0;
  161. }
  162. int av_fifo_generic_peek(AVFifoBuffer *f, void *dest, int buf_size,
  163. void (*func)(void *, void *, int))
  164. {
  165. // Read memory barrier needed for SMP here in theory
  166. uint8_t *rptr = f->rptr;
  167. do {
  168. int len = FFMIN(f->end - rptr, buf_size);
  169. if (func)
  170. func(dest, rptr, len);
  171. else {
  172. memcpy(dest, rptr, len);
  173. dest = (uint8_t *)dest + len;
  174. }
  175. // memory barrier needed for SMP here in theory
  176. rptr += len;
  177. if (rptr >= f->end)
  178. rptr -= f->end - f->buffer;
  179. buf_size -= len;
  180. } while (buf_size > 0);
  181. return 0;
  182. }
  183. int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size,
  184. void (*func)(void *, void *, int))
  185. {
  186. // Read memory barrier needed for SMP here in theory
  187. do {
  188. int len = FFMIN(f->end - f->rptr, buf_size);
  189. if (func)
  190. func(dest, f->rptr, len);
  191. else {
  192. memcpy(dest, f->rptr, len);
  193. dest = (uint8_t *)dest + len;
  194. }
  195. // memory barrier needed for SMP here in theory
  196. av_fifo_drain(f, len);
  197. buf_size -= len;
  198. } while (buf_size > 0);
  199. return 0;
  200. }
  201. /** Discard data from the FIFO. */
  202. void av_fifo_drain(AVFifoBuffer *f, int size)
  203. {
  204. av_assert2(av_fifo_size(f) >= size);
  205. f->rptr += size;
  206. if (f->rptr >= f->end)
  207. f->rptr -= f->end - f->buffer;
  208. f->rndx += size;
  209. }