FFmpeg
bufferqueue.h
1 /*
2  * Generic buffer queue
3  * Copyright (c) 2012 Nicolas George
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21 
22 #ifndef AVFILTER_BUFFERQUEUE_H
23 #define AVFILTER_BUFFERQUEUE_H
24 
39 #ifndef FF_BUFQUEUE_SIZE
40 #define FF_BUFQUEUE_SIZE 64
41 #endif
42 
43 #include "avfilter.h"
44 #include "libavutil/avassert.h"
45 
49 struct FFBufQueue {
50  AVFrame *queue[FF_BUFQUEUE_SIZE];
51  unsigned short head;
52  unsigned short available;
53 };
54 
55 #define BUCKET(i) queue->queue[(queue->head + (i)) % FF_BUFQUEUE_SIZE]
56 
60 static inline int ff_bufqueue_is_full(struct FFBufQueue *queue)
61 {
62  return queue->available == FF_BUFQUEUE_SIZE;
63 }
64 
71 static inline void ff_bufqueue_add(void *log, struct FFBufQueue *queue,
72  AVFrame *buf)
73 {
74  if (ff_bufqueue_is_full(queue)) {
75  av_log(log, AV_LOG_WARNING, "Buffer queue overflow, dropping.\n");
76  av_frame_free(&BUCKET(--queue->available));
77  }
78  BUCKET(queue->available++) = buf;
79 }
80 
87 static inline AVFrame *ff_bufqueue_peek(struct FFBufQueue *queue,
88  unsigned index)
89 {
90  return index < queue->available ? BUCKET(index) : NULL;
91 }
92 
98 static inline AVFrame *ff_bufqueue_get(struct FFBufQueue *queue)
99 {
100  AVFrame *ret = queue->queue[queue->head];
101  av_assert0(queue->available);
102  queue->available--;
103  queue->queue[queue->head] = NULL;
104  queue->head = (queue->head + 1) % FF_BUFQUEUE_SIZE;
105  return ret;
106 }
107 
111 static inline void ff_bufqueue_discard_all(struct FFBufQueue *queue)
112 {
113  while (queue->available) {
114  AVFrame *buf = ff_bufqueue_get(queue);
115  av_frame_free(&buf);
116  }
117 }
118 
119 #undef BUCKET
120 
121 #endif /* AVFILTER_BUFFERQUEUE_H */
This structure describes decoded (raw) audio or video data.
Definition: frame.h:218
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
Main libavfilter public API header.
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
Structure holding the queue.
Definition: bufferqueue.h:49
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
simple assert() macros that are a bit more flexible than ISO C assert().
unsigned short available
number of available buffers
Definition: bufferqueue.h:52