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.

138 lines
3.4KB

  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 "common.h"
  23. #include "fifo.h"
  24. int av_fifo_init(AVFifoBuffer *f, int size)
  25. {
  26. f->buffer = av_malloc(size);
  27. if (!f->buffer)
  28. return -1;
  29. f->end = f->buffer + size;
  30. f->wptr = f->rptr = f->buffer;
  31. return 0;
  32. }
  33. void av_fifo_free(AVFifoBuffer *f)
  34. {
  35. av_free(f->buffer);
  36. }
  37. int av_fifo_size(AVFifoBuffer *f)
  38. {
  39. int size = f->wptr - f->rptr;
  40. if (size < 0)
  41. size += f->end - f->buffer;
  42. return size;
  43. }
  44. /**
  45. * Get data from the fifo (returns -1 if not enough data).
  46. */
  47. int av_fifo_read(AVFifoBuffer *f, uint8_t *buf, int buf_size)
  48. {
  49. int len;
  50. int size = f->wptr - f->rptr;
  51. if (size < 0)
  52. size += f->end - f->buffer;
  53. if (size < buf_size)
  54. return -1;
  55. while (buf_size > 0) {
  56. len = FFMIN(f->end - f->rptr, buf_size);
  57. memcpy(buf, f->rptr, len);
  58. buf += len;
  59. f->rptr += len;
  60. if (f->rptr >= f->end)
  61. f->rptr = f->buffer;
  62. buf_size -= len;
  63. }
  64. return 0;
  65. }
  66. /**
  67. * Resizes a FIFO.
  68. */
  69. void av_fifo_realloc(AVFifoBuffer *f, unsigned int new_size) {
  70. unsigned int old_size= f->end - f->buffer;
  71. if(old_size < new_size){
  72. uint8_t *old= f->buffer;
  73. f->buffer= av_realloc(f->buffer, new_size);
  74. f->rptr += f->buffer - old;
  75. f->wptr += f->buffer - old;
  76. if(f->wptr < f->rptr){
  77. memmove(f->rptr + new_size - old_size, f->rptr, f->buffer + old_size - f->rptr);
  78. f->rptr += new_size - old_size;
  79. }
  80. f->end= f->buffer + new_size;
  81. }
  82. }
  83. void av_fifo_write(AVFifoBuffer *f, const uint8_t *buf, int size)
  84. {
  85. int len;
  86. while (size > 0) {
  87. len = FFMIN(f->end - f->wptr, size);
  88. memcpy(f->wptr, buf, len);
  89. f->wptr += len;
  90. if (f->wptr >= f->end)
  91. f->wptr = f->buffer;
  92. buf += len;
  93. size -= len;
  94. }
  95. }
  96. /* get data from the fifo (return -1 if not enough data) */
  97. int av_fifo_generic_read(AVFifoBuffer *f, int buf_size, void (*func)(void*, void*, int), void* dest)
  98. {
  99. int len;
  100. int size = f->wptr - f->rptr;
  101. if (size < 0)
  102. size += f->end - f->buffer;
  103. if (size < buf_size)
  104. return -1;
  105. while (buf_size > 0) {
  106. len = FFMIN(f->end - f->rptr, buf_size);
  107. func(dest, f->rptr, len);
  108. f->rptr += len;
  109. if (f->rptr >= f->end)
  110. f->rptr = f->buffer;
  111. buf_size -= len;
  112. }
  113. return 0;
  114. }
  115. /* discard data from the fifo */
  116. void av_fifo_drain(AVFifoBuffer *f, int size)
  117. {
  118. f->rptr += size;
  119. if (f->rptr >= f->end)
  120. f->rptr -= f->end - f->buffer;
  121. }