annotate ffmpeg/libavfilter/bufferqueue.h @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
rev   line source
yading@10 1 /*
yading@10 2 * Generic buffer queue
yading@10 3 * Copyright (c) 2012 Nicolas George
yading@10 4 *
yading@10 5 * This file is part of FFmpeg.
yading@10 6 *
yading@10 7 * FFmpeg is free software; you can redistribute it and/or
yading@10 8 * modify it under the terms of the GNU Lesser General Public
yading@10 9 * License as published by the Free Software Foundation; either
yading@10 10 * version 2.1 of the License, or (at your option) any later version.
yading@10 11 *
yading@10 12 * FFmpeg is distributed in the hope that it will be useful,
yading@10 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 15 * Lesser General Public License for more details.
yading@10 16 *
yading@10 17 * You should have received a copy of the GNU Lesser General Public
yading@10 18 * License along with FFmpeg; if not, write to the Free Software
yading@10 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 20 */
yading@10 21
yading@10 22 #ifndef AVFILTER_BUFFERQUEUE_H
yading@10 23 #define AVFILTER_BUFFERQUEUE_H
yading@10 24
yading@10 25 /**
yading@10 26 * FFBufQueue: simple AVFrame queue API
yading@10 27 *
yading@10 28 * Note: this API is not thread-safe. Concurrent access to the same queue
yading@10 29 * must be protected by a mutex or any synchronization mechanism.
yading@10 30 */
yading@10 31
yading@10 32 /**
yading@10 33 * Maximum size of the queue.
yading@10 34 *
yading@10 35 * This value can be overridden by definying it before including this
yading@10 36 * header.
yading@10 37 * Powers of 2 are recommended.
yading@10 38 */
yading@10 39 #ifndef FF_BUFQUEUE_SIZE
yading@10 40 #define FF_BUFQUEUE_SIZE 32
yading@10 41 #endif
yading@10 42
yading@10 43 #include "avfilter.h"
yading@10 44 #include "libavutil/avassert.h"
yading@10 45
yading@10 46 /**
yading@10 47 * Structure holding the queue
yading@10 48 */
yading@10 49 struct FFBufQueue {
yading@10 50 AVFrame *queue[FF_BUFQUEUE_SIZE];
yading@10 51 unsigned short head;
yading@10 52 unsigned short available; /**< number of available buffers */
yading@10 53 };
yading@10 54
yading@10 55 #define BUCKET(i) queue->queue[(queue->head + (i)) % FF_BUFQUEUE_SIZE]
yading@10 56
yading@10 57 /**
yading@10 58 * Test if a buffer queue is full.
yading@10 59 */
yading@10 60 static inline int ff_bufqueue_is_full(struct FFBufQueue *queue)
yading@10 61 {
yading@10 62 return queue->available == FF_BUFQUEUE_SIZE;
yading@10 63 }
yading@10 64
yading@10 65 /**
yading@10 66 * Add a buffer to the queue.
yading@10 67 *
yading@10 68 * If the queue is already full, then the current last buffer is dropped
yading@10 69 * (and unrefed) with a warning before adding the new buffer.
yading@10 70 */
yading@10 71 static inline void ff_bufqueue_add(void *log, struct FFBufQueue *queue,
yading@10 72 AVFrame *buf)
yading@10 73 {
yading@10 74 if (ff_bufqueue_is_full(queue)) {
yading@10 75 av_log(log, AV_LOG_WARNING, "Buffer queue overflow, dropping.\n");
yading@10 76 av_frame_free(&BUCKET(--queue->available));
yading@10 77 }
yading@10 78 BUCKET(queue->available++) = buf;
yading@10 79 }
yading@10 80
yading@10 81 /**
yading@10 82 * Get a buffer from the queue without altering it.
yading@10 83 *
yading@10 84 * Buffer with index 0 is the first buffer in the queue.
yading@10 85 * Return NULL if the queue has not enough buffers.
yading@10 86 */
yading@10 87 static inline AVFrame *ff_bufqueue_peek(struct FFBufQueue *queue,
yading@10 88 unsigned index)
yading@10 89 {
yading@10 90 return index < queue->available ? BUCKET(index) : NULL;
yading@10 91 }
yading@10 92
yading@10 93 /**
yading@10 94 * Get the first buffer from the queue and remove it.
yading@10 95 *
yading@10 96 * Do not use on an empty queue.
yading@10 97 */
yading@10 98 static inline AVFrame *ff_bufqueue_get(struct FFBufQueue *queue)
yading@10 99 {
yading@10 100 AVFrame *ret = queue->queue[queue->head];
yading@10 101 av_assert0(queue->available);
yading@10 102 queue->available--;
yading@10 103 queue->queue[queue->head] = NULL;
yading@10 104 queue->head = (queue->head + 1) % FF_BUFQUEUE_SIZE;
yading@10 105 return ret;
yading@10 106 }
yading@10 107
yading@10 108 /**
yading@10 109 * Unref and remove all buffers from the queue.
yading@10 110 */
yading@10 111 static inline void ff_bufqueue_discard_all(struct FFBufQueue *queue)
yading@10 112 {
yading@10 113 while (queue->available) {
yading@10 114 AVFrame *buf = ff_bufqueue_get(queue);
yading@10 115 av_frame_free(&buf);
yading@10 116 }
yading@10 117 }
yading@10 118
yading@10 119 #undef BUCKET
yading@10 120
yading@10 121 #endif /* AVFILTER_BUFFERQUEUE_H */