bufferqueue.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Generic buffer queue
  3. * Copyright (c) 2012 Nicolas George
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #ifndef AVFILTER_BUFFERQUEUE_H
  22. #define AVFILTER_BUFFERQUEUE_H
  23. /**
  24. * FFBufQueue: simple AVFilterBufferRef queue API
  25. *
  26. * Note: this API is not thread-safe. Concurrent access to the same queue
  27. * must be protected by a mutex or any synchronization mechanism.
  28. */
  29. /**
  30. * Maximum size of the queue.
  31. *
  32. * This value can be overridden by definying it before including this
  33. * header.
  34. * Powers of 2 are recommended.
  35. */
  36. #ifndef FF_BUFQUEUE_SIZE
  37. #define FF_BUFQUEUE_SIZE 32
  38. #endif
  39. #include "avfilter.h"
  40. #include "libavutil/avassert.h"
  41. /**
  42. * Structure holding the queue
  43. */
  44. struct FFBufQueue {
  45. AVFilterBufferRef *queue[FF_BUFQUEUE_SIZE];
  46. unsigned short head;
  47. unsigned short available; /**< number of available buffers */
  48. };
  49. #define BUCKET(i) queue->queue[(queue->head + (i)) % FF_BUFQUEUE_SIZE]
  50. /**
  51. * Add a buffer to the queue.
  52. *
  53. * If the queue is already full, then the current last buffer is dropped
  54. * (and unrefed) with a warning before adding the new buffer.
  55. */
  56. static inline void ff_bufqueue_add(void *log, struct FFBufQueue *queue,
  57. AVFilterBufferRef *buf)
  58. {
  59. if (queue->available == FF_BUFQUEUE_SIZE) {
  60. av_log(log, AV_LOG_WARNING, "Buffer queue overflow, dropping.\n");
  61. avfilter_unref_buffer(BUCKET(--queue->available));
  62. }
  63. BUCKET(queue->available++) = buf;
  64. }
  65. /**
  66. * Get a buffer from the queue without altering it.
  67. *
  68. * Buffer with index 0 is the first buffer in the queue.
  69. * Return NULL if the queue has not enough buffers.
  70. */
  71. static inline AVFilterBufferRef *ff_bufqueue_peek(struct FFBufQueue *queue,
  72. unsigned index)
  73. {
  74. return index < queue->available ? BUCKET(index) : NULL;
  75. }
  76. /**
  77. * Get the first buffer from the queue and remove it.
  78. *
  79. * Do not use on an empty queue.
  80. */
  81. static inline AVFilterBufferRef *ff_bufqueue_get(struct FFBufQueue *queue)
  82. {
  83. AVFilterBufferRef *ret = queue->queue[queue->head];
  84. av_assert0(queue->available);
  85. queue->available--;
  86. queue->queue[queue->head] = NULL;
  87. queue->head = (queue->head + 1) % FF_BUFQUEUE_SIZE;
  88. return ret;
  89. }
  90. /**
  91. * Unref and remove all buffers from the queue.
  92. */
  93. static inline void ff_bufqueue_discard_all(struct FFBufQueue *queue)
  94. {
  95. while (queue->available)
  96. avfilter_unref_buffer(ff_bufqueue_get(queue));
  97. }
  98. #undef BUCKET
  99. #endif /* AVFILTER_BUFFERQUEUE_H */