00001 /* 00002 * Generic buffer queue 00003 * Copyright (c) 2012 Nicolas George 00004 * 00005 * This file is part of FFmpeg. 00006 * 00007 * FFmpeg is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * FFmpeg is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with FFmpeg; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00022 #ifndef AVFILTER_BUFFERQUEUE_H 00023 #define AVFILTER_BUFFERQUEUE_H 00024 00039 #ifndef FF_BUFQUEUE_SIZE 00040 #define FF_BUFQUEUE_SIZE 32 00041 #endif 00042 00043 #include "avfilter.h" 00044 #include "libavutil/avassert.h" 00045 00049 struct FFBufQueue { 00050 AVFilterBufferRef *queue[FF_BUFQUEUE_SIZE]; 00051 unsigned short head; 00052 unsigned short available; 00053 }; 00054 00055 #define BUCKET(i) queue->queue[(queue->head + (i)) % FF_BUFQUEUE_SIZE] 00056 00063 static inline void ff_bufqueue_add(void *log, struct FFBufQueue *queue, 00064 AVFilterBufferRef *buf) 00065 { 00066 if (queue->available == FF_BUFQUEUE_SIZE) { 00067 av_log(log, AV_LOG_WARNING, "Buffer queue overflow, dropping.\n"); 00068 avfilter_unref_buffer(BUCKET(--queue->available)); 00069 } 00070 BUCKET(queue->available++) = buf; 00071 } 00072 00079 static inline AVFilterBufferRef *ff_bufqueue_peek(struct FFBufQueue *queue, 00080 unsigned index) 00081 { 00082 return index < queue->available ? BUCKET(index) : NULL; 00083 } 00084 00090 static inline AVFilterBufferRef *ff_bufqueue_get(struct FFBufQueue *queue) 00091 { 00092 AVFilterBufferRef *ret = queue->queue[queue->head]; 00093 av_assert0(queue->available); 00094 queue->available--; 00095 queue->queue[queue->head] = NULL; 00096 queue->head = (queue->head + 1) % FF_BUFQUEUE_SIZE; 00097 return ret; 00098 } 00099 00103 static inline void ff_bufqueue_discard_all(struct FFBufQueue *queue) 00104 { 00105 while (queue->available) 00106 avfilter_unref_buffer(ff_bufqueue_get(queue)); 00107 } 00108 00109 #undef BUCKET 00110 00111 #endif /* AVFILTER_BUFFERQUEUE_H */