3 * Copyright (c) 2012 Nicolas George
5 * This file is part of FFmpeg.
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.
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.
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
22 #ifndef AVFILTER_BUFFERQUEUE_H
23 #define AVFILTER_BUFFERQUEUE_H
26 * FFBufQueue: simple AVFilterBufferRef queue API
28 * Note: this API is not thread-safe. Concurrent access to the same queue
29 * must be protected by a mutex or any synchronization mechanism.
33 * Maximum size of the queue.
35 * This value can be overridden by definying it before including this
37 * Powers of 2 are recommended.
39 #ifndef FF_BUFQUEUE_SIZE
40 #define FF_BUFQUEUE_SIZE 32
44 #include "libavutil/avassert.h"
47 * Structure holding the queue
50 AVFilterBufferRef *queue[FF_BUFQUEUE_SIZE];
52 unsigned short available; /**< number of available buffers */
55 #define BUCKET(i) queue->queue[(queue->head + (i)) % FF_BUFQUEUE_SIZE]
58 * Add a buffer to the queue.
60 * If the queue is already full, then the current last buffer is dropped
61 * (and unrefed) with a warning before adding the new buffer.
63 static inline void ff_bufqueue_add(void *log, struct FFBufQueue *queue,
64 AVFilterBufferRef *buf)
66 if (queue->available == FF_BUFQUEUE_SIZE) {
67 av_log(log, AV_LOG_WARNING, "Buffer queue overflow, dropping.\n");
68 avfilter_unref_buffer(BUCKET(--queue->available));
70 BUCKET(queue->available++) = buf;
74 * Get a buffer from the queue without altering it.
76 * Buffer with index 0 is the first buffer in the queue.
77 * Return NULL if the queue has not enough buffers.
79 static inline AVFilterBufferRef *ff_bufqueue_peek(struct FFBufQueue *queue,
82 return index < queue->available ? BUCKET(index) : NULL;
86 * Get the first buffer from the queue and remove it.
88 * Do not use on an empty queue.
90 static inline AVFilterBufferRef *ff_bufqueue_get(struct FFBufQueue *queue)
92 AVFilterBufferRef *ret = queue->queue[queue->head];
93 av_assert0(queue->available);
95 queue->queue[queue->head] = NULL;
96 queue->head = (queue->head + 1) % FF_BUFQUEUE_SIZE;
101 * Unref and remove all buffers from the queue.
103 static inline void ff_bufqueue_discard_all(struct FFBufQueue *queue)
105 while (queue->available)
106 avfilter_unref_buffer(ff_bufqueue_get(queue));
111 #endif /* AVFILTER_BUFFERQUEUE_H */