[FFmpeg-devel] [PATCH 2/3] lavfi: add a generic API for buffer queues.

Nicolas George nicolas.george at normalesup.org
Fri Apr 27 17:38:41 CEST 2012


Signed-off-by: Nicolas George <nicolas.george at normalesup.org>
---
 libavfilter/bufferqueue.h |   73 +++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 73 insertions(+), 0 deletions(-)
 create mode 100644 libavfilter/bufferqueue.h

diff --git a/libavfilter/bufferqueue.h b/libavfilter/bufferqueue.h
new file mode 100644
index 0000000..6ad00ee
--- /dev/null
+++ b/libavfilter/bufferqueue.h
@@ -0,0 +1,73 @@
+/*
+ * Generic buffer queue
+ * Copyright (c) 2012 Nicolas George
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef AVFILTER_BUFFERQUEUE_H
+#define AVFILTER_BUFFERQUEUE_H
+
+#ifndef FF_BUFQUEUE_SIZE
+#define FF_BUFQUEUE_SIZE 32
+#endif
+
+#include "avfilter.h"
+#include "libavutil/avassert.h"
+
+struct ff_bufqueue {
+    AVFilterBufferRef *queue[FF_BUFQUEUE_SIZE];
+    unsigned short head;
+    unsigned short available;
+};
+
+#define BUCKET(i) queue->queue[(queue->head + (i)) % FF_BUFQUEUE_SIZE]
+
+static inline void ff_bufqueue_add(void *log, struct ff_bufqueue *queue,
+                                   AVFilterBufferRef *buf)
+{
+    if (queue->available == FF_BUFQUEUE_SIZE) {
+        av_log(log, AV_LOG_WARNING, "Buffer queue overflow, dropping.\n");
+        avfilter_unref_buffer(BUCKET(--queue->available));
+    }
+    BUCKET(queue->available++) = buf;
+}
+
+static inline AVFilterBufferRef *ff_bufqueue_get(struct ff_bufqueue *queue,
+                                                 unsigned index)
+{
+    return index < queue->available ? BUCKET(index) : NULL;
+}
+
+static inline AVFilterBufferRef *ff_bufqueue_take(struct ff_bufqueue *queue)
+{
+    AVFilterBufferRef *ret = queue->queue[queue->head];
+    queue->available--;
+    queue->queue[queue->head] = NULL;
+    queue->head = (queue->head + 1) % FF_BUFQUEUE_SIZE;
+    return ret;
+}
+
+static inline void ff_bufqueue_discard_all(struct ff_bufqueue *queue)
+{
+    while (queue->available)
+        avfilter_unref_buffer(ff_bufqueue_take(queue));
+}
+
+#undef BUCKET
+
+#endif /* AVFILTER_BUFFERQUEUE_H */
-- 
1.7.2.5



More information about the ffmpeg-devel mailing list