yading@10: /* yading@10: * Generic buffer queue yading@10: * Copyright (c) 2012 Nicolas George yading@10: * yading@10: * This file is part of FFmpeg. yading@10: * yading@10: * FFmpeg is free software; you can redistribute it and/or yading@10: * modify it under the terms of the GNU Lesser General Public yading@10: * License as published by the Free Software Foundation; either yading@10: * version 2.1 of the License, or (at your option) any later version. yading@10: * yading@10: * FFmpeg is distributed in the hope that it will be useful, yading@10: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@10: * Lesser General Public License for more details. yading@10: * yading@10: * You should have received a copy of the GNU Lesser General Public yading@10: * License along with FFmpeg; if not, write to the Free Software yading@10: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@10: */ yading@10: yading@10: #ifndef AVFILTER_BUFFERQUEUE_H yading@10: #define AVFILTER_BUFFERQUEUE_H yading@10: yading@10: /** yading@10: * FFBufQueue: simple AVFrame queue API yading@10: * yading@10: * Note: this API is not thread-safe. Concurrent access to the same queue yading@10: * must be protected by a mutex or any synchronization mechanism. yading@10: */ yading@10: yading@10: /** yading@10: * Maximum size of the queue. yading@10: * yading@10: * This value can be overridden by definying it before including this yading@10: * header. yading@10: * Powers of 2 are recommended. yading@10: */ yading@10: #ifndef FF_BUFQUEUE_SIZE yading@10: #define FF_BUFQUEUE_SIZE 32 yading@10: #endif yading@10: yading@10: #include "avfilter.h" yading@10: #include "libavutil/avassert.h" yading@10: yading@10: /** yading@10: * Structure holding the queue yading@10: */ yading@10: struct FFBufQueue { yading@10: AVFrame *queue[FF_BUFQUEUE_SIZE]; yading@10: unsigned short head; yading@10: unsigned short available; /**< number of available buffers */ yading@10: }; yading@10: yading@10: #define BUCKET(i) queue->queue[(queue->head + (i)) % FF_BUFQUEUE_SIZE] yading@10: yading@10: /** yading@10: * Test if a buffer queue is full. yading@10: */ yading@10: static inline int ff_bufqueue_is_full(struct FFBufQueue *queue) yading@10: { yading@10: return queue->available == FF_BUFQUEUE_SIZE; yading@10: } yading@10: yading@10: /** yading@10: * Add a buffer to the queue. yading@10: * yading@10: * If the queue is already full, then the current last buffer is dropped yading@10: * (and unrefed) with a warning before adding the new buffer. yading@10: */ yading@10: static inline void ff_bufqueue_add(void *log, struct FFBufQueue *queue, yading@10: AVFrame *buf) yading@10: { yading@10: if (ff_bufqueue_is_full(queue)) { yading@10: av_log(log, AV_LOG_WARNING, "Buffer queue overflow, dropping.\n"); yading@10: av_frame_free(&BUCKET(--queue->available)); yading@10: } yading@10: BUCKET(queue->available++) = buf; yading@10: } yading@10: yading@10: /** yading@10: * Get a buffer from the queue without altering it. yading@10: * yading@10: * Buffer with index 0 is the first buffer in the queue. yading@10: * Return NULL if the queue has not enough buffers. yading@10: */ yading@10: static inline AVFrame *ff_bufqueue_peek(struct FFBufQueue *queue, yading@10: unsigned index) yading@10: { yading@10: return index < queue->available ? BUCKET(index) : NULL; yading@10: } yading@10: yading@10: /** yading@10: * Get the first buffer from the queue and remove it. yading@10: * yading@10: * Do not use on an empty queue. yading@10: */ yading@10: static inline AVFrame *ff_bufqueue_get(struct FFBufQueue *queue) yading@10: { yading@10: AVFrame *ret = queue->queue[queue->head]; yading@10: av_assert0(queue->available); yading@10: queue->available--; yading@10: queue->queue[queue->head] = NULL; yading@10: queue->head = (queue->head + 1) % FF_BUFQUEUE_SIZE; yading@10: return ret; yading@10: } yading@10: yading@10: /** yading@10: * Unref and remove all buffers from the queue. yading@10: */ yading@10: static inline void ff_bufqueue_discard_all(struct FFBufQueue *queue) yading@10: { yading@10: while (queue->available) { yading@10: AVFrame *buf = ff_bufqueue_get(queue); yading@10: av_frame_free(&buf); yading@10: } yading@10: } yading@10: yading@10: #undef BUCKET yading@10: yading@10: #endif /* AVFILTER_BUFFERQUEUE_H */