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.

239 lines
5.8KB

  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*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(AVFifoBuffer *f, void *dest, int buf_size,
  134. void (*func)(void *, void *, int))
  135. {
  136. // Read memory barrier needed for SMP here in theory
  137. uint8_t *rptr = f->rptr;
  138. uint32_t rndx = f->rndx;
  139. do {
  140. int len = FFMIN(f->end - f->rptr, buf_size);
  141. if (func)
  142. func(dest, f->rptr, len);
  143. else {
  144. memcpy(dest, f->rptr, len);
  145. dest = (uint8_t *)dest + len;
  146. }
  147. // memory barrier needed for SMP here in theory
  148. av_fifo_drain(f, len);
  149. buf_size -= len;
  150. } while (buf_size > 0);
  151. f->rptr = rptr;
  152. f->rndx = rndx;
  153. return 0;
  154. }
  155. int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size,
  156. void (*func)(void *, void *, int))
  157. {
  158. // Read memory barrier needed for SMP here in theory
  159. do {
  160. int len = FFMIN(f->end - f->rptr, buf_size);
  161. if (func)
  162. func(dest, f->rptr, len);
  163. else {
  164. memcpy(dest, f->rptr, len);
  165. dest = (uint8_t *)dest + len;
  166. }
  167. // memory barrier needed for SMP here in theory
  168. av_fifo_drain(f, len);
  169. buf_size -= len;
  170. } while (buf_size > 0);
  171. return 0;
  172. }
  173. /** Discard data from the FIFO. */
  174. void av_fifo_drain(AVFifoBuffer *f, int size)
  175. {
  176. av_assert2(av_fifo_size(f) >= size);
  177. f->rptr += size;
  178. if (f->rptr >= f->end)
  179. f->rptr -= f->end - f->buffer;
  180. f->rndx += size;
  181. }
  182. #ifdef TEST
  183. int main(void)
  184. {
  185. /* create a FIFO buffer */
  186. AVFifoBuffer *fifo = av_fifo_alloc(13 * sizeof(int));
  187. int i, j, n;
  188. /* fill data */
  189. for (i = 0; av_fifo_space(fifo) >= sizeof(int); i++)
  190. av_fifo_generic_write(fifo, &i, sizeof(int), NULL);
  191. /* peek at FIFO */
  192. n = av_fifo_size(fifo) / sizeof(int);
  193. for (i = -n + 1; i < n; i++) {
  194. int *v = (int *)av_fifo_peek2(fifo, i * sizeof(int));
  195. printf("%d: %d\n", i, *v);
  196. }
  197. printf("\n");
  198. /* read data */
  199. for (i = 0; av_fifo_size(fifo) >= sizeof(int); i++) {
  200. av_fifo_generic_read(fifo, &j, sizeof(int), NULL);
  201. printf("%d ", j);
  202. }
  203. printf("\n");
  204. av_fifo_free(fifo);
  205. return 0;
  206. }
  207. #endif