framequeue.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Generic frame queue
  3. * Copyright (c) 2016 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 License
  9. * 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
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #ifndef AVFILTER_FRAMEQUEUE_H
  22. #define AVFILTER_FRAMEQUEUE_H
  23. /**
  24. * FFFrameQueue: simple AVFrame 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. #include "libavutil/frame.h"
  30. typedef struct FFFrameBucket {
  31. AVFrame *frame;
  32. } FFFrameBucket;
  33. /**
  34. * Structure to hold global options and statistics for frame queues.
  35. *
  36. * This structure is intended to allow implementing global control of the
  37. * frame queues, including memory consumption caps.
  38. *
  39. * It is currently empty.
  40. */
  41. typedef struct FFFrameQueueGlobal {
  42. char dummy; /* C does not allow empty structs */
  43. } FFFrameQueueGlobal;
  44. /**
  45. * Queue of AVFrame pointers.
  46. */
  47. typedef struct FFFrameQueue {
  48. /**
  49. * Array of allocated buckets, used as a circular buffer.
  50. */
  51. FFFrameBucket *queue;
  52. /**
  53. * Size of the array of buckets.
  54. */
  55. size_t allocated;
  56. /**
  57. * Tail of the queue.
  58. * It is the index in the array of the next frame to take.
  59. */
  60. size_t tail;
  61. /**
  62. * Number of currently queued frames.
  63. */
  64. size_t queued;
  65. /**
  66. * Pre-allocated bucket for queues of size 1.
  67. */
  68. FFFrameBucket first_bucket;
  69. /**
  70. * Total number of frames entered in the queue.
  71. */
  72. uint64_t total_frames_head;
  73. /**
  74. * Total number of frames dequeued from the queue.
  75. * queued = total_frames_head - total_frames_tail
  76. */
  77. uint64_t total_frames_tail;
  78. /**
  79. * Total number of samples entered in the queue.
  80. */
  81. uint64_t total_samples_head;
  82. /**
  83. * Total number of samples dequeued from the queue.
  84. * queued_samples = total_samples_head - total_samples_tail
  85. */
  86. uint64_t total_samples_tail;
  87. /**
  88. * Indicate that samples are skipped
  89. */
  90. int samples_skipped;
  91. } FFFrameQueue;
  92. /**
  93. * Init a global structure.
  94. */
  95. void ff_framequeue_global_init(FFFrameQueueGlobal *fqg);
  96. /**
  97. * Init a frame queue and attach it to a global structure.
  98. */
  99. void ff_framequeue_init(FFFrameQueue *fq, FFFrameQueueGlobal *fqg);
  100. /**
  101. * Free the queue and all queued frames.
  102. */
  103. void ff_framequeue_free(FFFrameQueue *fq);
  104. /**
  105. * Add a frame.
  106. * @return >=0 or an AVERROR code.
  107. */
  108. int ff_framequeue_add(FFFrameQueue *fq, AVFrame *frame);
  109. /**
  110. * Take the first frame in the queue.
  111. * Must not be used with empty queues.
  112. */
  113. AVFrame *ff_framequeue_take(FFFrameQueue *fq);
  114. /**
  115. * Access a frame in the queue, without removing it.
  116. * The first frame is numbered 0; the designated frame must exist.
  117. */
  118. AVFrame *ff_framequeue_peek(FFFrameQueue *fq, size_t idx);
  119. /**
  120. * Get the number of queued frames.
  121. */
  122. static inline size_t ff_framequeue_queued_frames(const FFFrameQueue *fq)
  123. {
  124. return fq->queued;
  125. }
  126. /**
  127. * Get the number of queued samples.
  128. */
  129. static inline uint64_t ff_framequeue_queued_samples(const FFFrameQueue *fq)
  130. {
  131. return fq->total_samples_head - fq->total_samples_tail;
  132. }
  133. /**
  134. * Update the statistics after a frame accessed using ff_framequeue_peek()
  135. * was modified.
  136. * Currently used only as a marker.
  137. */
  138. static inline void ff_framequeue_update_peeked(FFFrameQueue *fq, size_t idx)
  139. {
  140. }
  141. /**
  142. * Skip samples from the first frame in the queue.
  143. *
  144. * This function must be used when the first frame was accessed using
  145. * ff_framequeue_peek() and samples were consumed from it.
  146. * It adapts the data pointers and timestamps of the head frame to account
  147. * for the skipped samples.
  148. */
  149. void ff_framequeue_skip_samples(FFFrameQueue *fq, size_t samples, AVRational time_base);
  150. #endif /* AVFILTER_FRAMEQUEUE_H */