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.

197 lines
4.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. AVFifoBuffer *av_fifo_alloc(unsigned int size)
  26. {
  27. AVFifoBuffer *f = av_mallocz(sizeof(AVFifoBuffer));
  28. if (!f)
  29. return NULL;
  30. f->buffer = av_malloc(size);
  31. f->end = f->buffer + size;
  32. av_fifo_reset(f);
  33. if (!f->buffer)
  34. av_freep(&f);
  35. return f;
  36. }
  37. void av_fifo_free(AVFifoBuffer *f)
  38. {
  39. if (f) {
  40. av_freep(&f->buffer);
  41. av_free(f);
  42. }
  43. }
  44. void av_fifo_freep(AVFifoBuffer **f)
  45. {
  46. if (f) {
  47. av_fifo_free(*f);
  48. *f = NULL;
  49. }
  50. }
  51. void av_fifo_reset(AVFifoBuffer *f)
  52. {
  53. f->wptr = f->rptr = f->buffer;
  54. f->wndx = f->rndx = 0;
  55. }
  56. int av_fifo_size(FF_CONST_AVUTIL53 AVFifoBuffer *f)
  57. {
  58. return (uint32_t)(f->wndx - f->rndx);
  59. }
  60. int av_fifo_space(FF_CONST_AVUTIL53 AVFifoBuffer *f)
  61. {
  62. return f->end - f->buffer - av_fifo_size(f);
  63. }
  64. int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size)
  65. {
  66. unsigned int old_size = f->end - f->buffer;
  67. if (old_size < new_size) {
  68. int len = av_fifo_size(f);
  69. AVFifoBuffer *f2 = av_fifo_alloc(new_size);
  70. if (!f2)
  71. return AVERROR(ENOMEM);
  72. av_fifo_generic_read(f, f2->buffer, len, NULL);
  73. f2->wptr += len;
  74. f2->wndx += len;
  75. av_free(f->buffer);
  76. *f = *f2;
  77. av_free(f2);
  78. }
  79. return 0;
  80. }
  81. int av_fifo_grow(AVFifoBuffer *f, unsigned int size)
  82. {
  83. unsigned int old_size = f->end - f->buffer;
  84. if(size + (unsigned)av_fifo_size(f) < size)
  85. return AVERROR(EINVAL);
  86. size += av_fifo_size(f);
  87. if (old_size < size)
  88. return av_fifo_realloc2(f, FFMAX(size, 2*size));
  89. return 0;
  90. }
  91. /* src must NOT be const as it can be a context for func that may need
  92. * updating (like a pointer or byte counter) */
  93. int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size,
  94. int (*func)(void *, void *, int))
  95. {
  96. int total = size;
  97. uint32_t wndx= f->wndx;
  98. uint8_t *wptr= f->wptr;
  99. do {
  100. int len = FFMIN(f->end - wptr, size);
  101. if (func) {
  102. if (func(src, wptr, len) <= 0)
  103. break;
  104. } else {
  105. memcpy(wptr, src, len);
  106. src = (uint8_t *)src + len;
  107. }
  108. // Write memory barrier needed for SMP here in theory
  109. wptr += len;
  110. if (wptr >= f->end)
  111. wptr = f->buffer;
  112. wndx += len;
  113. size -= len;
  114. } while (size > 0);
  115. f->wndx= wndx;
  116. f->wptr= wptr;
  117. return total - size;
  118. }
  119. int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size,
  120. void (*func)(void *, void *, int))
  121. {
  122. // Read memory barrier needed for SMP here in theory
  123. do {
  124. int len = FFMIN(f->end - f->rptr, buf_size);
  125. if (func)
  126. func(dest, f->rptr, len);
  127. else {
  128. memcpy(dest, f->rptr, len);
  129. dest = (uint8_t *)dest + len;
  130. }
  131. // memory barrier needed for SMP here in theory
  132. av_fifo_drain(f, len);
  133. buf_size -= len;
  134. } while (buf_size > 0);
  135. return 0;
  136. }
  137. /** Discard data from the FIFO. */
  138. void av_fifo_drain(AVFifoBuffer *f, int size)
  139. {
  140. av_assert2(av_fifo_size(f) >= size);
  141. f->rptr += size;
  142. if (f->rptr >= f->end)
  143. f->rptr -= f->end - f->buffer;
  144. f->rndx += size;
  145. }
  146. #ifdef TEST
  147. int main(void)
  148. {
  149. /* create a FIFO buffer */
  150. AVFifoBuffer *fifo = av_fifo_alloc(13 * sizeof(int));
  151. int i, j, n;
  152. /* fill data */
  153. for (i = 0; av_fifo_space(fifo) >= sizeof(int); i++)
  154. av_fifo_generic_write(fifo, &i, sizeof(int), NULL);
  155. /* peek at FIFO */
  156. n = av_fifo_size(fifo) / sizeof(int);
  157. for (i = -n + 1; i < n; i++) {
  158. int *v = (int *)av_fifo_peek2(fifo, i * sizeof(int));
  159. printf("%d: %d\n", i, *v);
  160. }
  161. printf("\n");
  162. /* read data */
  163. for (i = 0; av_fifo_size(fifo) >= sizeof(int); i++) {
  164. av_fifo_generic_read(fifo, &j, sizeof(int), NULL);
  165. printf("%d ", j);
  166. }
  167. printf("\n");
  168. av_fifo_free(fifo);
  169. return 0;
  170. }
  171. #endif