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.

115 lines
2.9KB

  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->wptr = f->rptr =
  27. f->buffer = av_malloc(size);
  28. f->end = f->buffer + size;
  29. if (!f->buffer)
  30. return -1;
  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. return av_fifo_generic_read(f, buf_size, NULL, buf);
  50. }
  51. /**
  52. * Resizes a FIFO.
  53. */
  54. void av_fifo_realloc(AVFifoBuffer *f, unsigned int new_size) {
  55. unsigned int old_size= f->end - f->buffer;
  56. if(old_size < new_size){
  57. int len= av_fifo_size(f);
  58. AVFifoBuffer f2;
  59. av_fifo_init(&f2, new_size);
  60. av_fifo_read(f, f2.buffer, len);
  61. f2.wptr += len;
  62. av_free(f->buffer);
  63. *f= f2;
  64. }
  65. }
  66. void av_fifo_write(AVFifoBuffer *f, const uint8_t *buf, int size)
  67. {
  68. do {
  69. int len = FFMIN(f->end - f->wptr, size);
  70. memcpy(f->wptr, buf, len);
  71. f->wptr += len;
  72. if (f->wptr >= f->end)
  73. f->wptr = f->buffer;
  74. buf += len;
  75. size -= len;
  76. } while (size > 0);
  77. }
  78. /** get data from the fifo (return -1 if not enough data) */
  79. int av_fifo_generic_read(AVFifoBuffer *f, int buf_size, void (*func)(void*, void*, int), void* dest)
  80. {
  81. int size = av_fifo_size(f);
  82. if (size < buf_size)
  83. return -1;
  84. do {
  85. int len = FFMIN(f->end - f->rptr, buf_size);
  86. if(func) func(dest, f->rptr, len);
  87. else{
  88. memcpy(dest, f->rptr, len);
  89. dest = (uint8_t*)dest + len;
  90. }
  91. av_fifo_drain(f, len);
  92. buf_size -= len;
  93. } while (buf_size > 0);
  94. return 0;
  95. }
  96. /** discard data from the fifo */
  97. void av_fifo_drain(AVFifoBuffer *f, int size)
  98. {
  99. f->rptr += size;
  100. if (f->rptr >= f->end)
  101. f->rptr -= f->end - f->buffer;
  102. }