Browse code

lavfi: add FFFrameQueue API.

Nicolas George authored on 2015/12/04 04:05:14
Showing 3 changed files
... ...
@@ -18,6 +18,7 @@ OBJS = allfilters.o                                                     \
18 18
        fifo.o                                                           \
19 19
        formats.o                                                        \
20 20
        framepool.o                                                      \
21
+       framequeue.o                                                     \
21 22
        graphdump.o                                                      \
22 23
        graphparser.o                                                    \
23 24
        opencl_allkernels.o                                              \
24 25
new file mode 100644
... ...
@@ -0,0 +1,123 @@
0
+/*
1
+ * Generic frame queue
2
+ * Copyright (c) 2016 Nicolas George
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public License
8
+ * as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
+ * GNU Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public License
17
+ * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
18
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include "libavutil/avassert.h"
22
+#include "framequeue.h"
23
+
24
+static inline FFFrameBucket *bucket(FFFrameQueue *fq, size_t idx)
25
+{
26
+    return &fq->queue[(fq->tail + idx) & (fq->allocated - 1)];
27
+}
28
+
29
+void ff_framequeue_global_init(FFFrameQueueGlobal *fqg)
30
+{
31
+}
32
+
33
+static void check_consistency(FFFrameQueue *fq)
34
+{
35
+#if ASSERT_LEVEL >= 2
36
+    uint64_t nb_samples = 0;
37
+    size_t i;
38
+
39
+    av_assert0(fq->queued == fq->total_frames_head - fq->total_frames_tail);
40
+    for (i = 0; i < fq->queued; i++)
41
+        nb_samples += bucket(fq, i)->frame->nb_samples;
42
+    av_assert0(nb_samples == fq->total_samples_head - fq->total_samples_tail);
43
+#endif
44
+}
45
+
46
+void ff_framequeue_init(FFFrameQueue *fq, FFFrameQueueGlobal *fqg)
47
+{
48
+    fq->queue = &fq->first_bucket;
49
+    fq->allocated = 1;
50
+}
51
+
52
+void ff_framequeue_free(FFFrameQueue *fq)
53
+{
54
+    while (fq->queued) {
55
+        AVFrame *frame = ff_framequeue_take(fq);
56
+        av_frame_free(&frame);
57
+    }
58
+    if (fq->queue != &fq->first_bucket)
59
+        av_freep(&fq->queue);
60
+}
61
+
62
+int ff_framequeue_add(FFFrameQueue *fq, AVFrame *frame)
63
+{
64
+    FFFrameBucket *b;
65
+
66
+    check_consistency(fq);
67
+    if (fq->queued == fq->allocated) {
68
+        if (fq->allocated == 1) {
69
+            size_t na = 8;
70
+            FFFrameBucket *nq = av_realloc_array(NULL, na, sizeof(*nq));
71
+            if (!nq)
72
+                return AVERROR(ENOMEM);
73
+            nq[0] = fq->queue[0];
74
+            fq->queue = nq;
75
+            fq->allocated = na;
76
+        } else {
77
+            size_t na = fq->allocated << 1;
78
+            FFFrameBucket *nq = av_realloc_array(fq->queue, na, sizeof(*nq));
79
+            if (!nq)
80
+                return AVERROR(ENOMEM);
81
+            if (fq->tail + fq->queued > fq->allocated)
82
+                memmove(nq + fq->allocated, nq,
83
+                        (fq->tail + fq->queued - fq->allocated) * sizeof(*nq));
84
+            fq->queue = nq;
85
+            fq->allocated = na;
86
+        }
87
+    }
88
+    b = bucket(fq, fq->queued);
89
+    b->frame = frame;
90
+    fq->queued++;
91
+    fq->total_frames_head++;
92
+    fq->total_samples_head += frame->nb_samples;
93
+    check_consistency(fq);
94
+    return 0;
95
+}
96
+
97
+AVFrame *ff_framequeue_take(FFFrameQueue *fq)
98
+{
99
+    FFFrameBucket *b;
100
+
101
+    check_consistency(fq);
102
+    av_assert1(fq->queued);
103
+    b = bucket(fq, 0);
104
+    fq->queued--;
105
+    fq->tail++;
106
+    fq->tail &= fq->allocated - 1;
107
+    fq->total_frames_tail++;
108
+    fq->total_samples_tail += b->frame->nb_samples;
109
+    check_consistency(fq);
110
+    return b->frame;
111
+}
112
+
113
+AVFrame *ff_framequeue_peek(FFFrameQueue *fq, size_t idx)
114
+{
115
+    FFFrameBucket *b;
116
+
117
+    check_consistency(fq);
118
+    av_assert1(idx < fq->queued);
119
+    b = bucket(fq, idx);
120
+    check_consistency(fq);
121
+    return b->frame;
122
+}
0 123
new file mode 100644
... ...
@@ -0,0 +1,173 @@
0
+/*
1
+ * Generic frame queue
2
+ * Copyright (c) 2016 Nicolas George
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public License
8
+ * as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
+ * GNU Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public License
17
+ * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
18
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#ifndef AVFILTER_FRAMEQUEUE_H
22
+#define AVFILTER_FRAMEQUEUE_H
23
+
24
+/**
25
+ * FFFrameQueue: simple AVFrame queue API
26
+ *
27
+ * Note: this API is not thread-safe. Concurrent access to the same queue
28
+ * must be protected by a mutex or any synchronization mechanism.
29
+ */
30
+
31
+#include "libavutil/frame.h"
32
+
33
+typedef struct FFFrameBucket {
34
+    AVFrame *frame;
35
+} FFFrameBucket;
36
+
37
+/**
38
+ * Structure to hold global options and statistics for frame queues.
39
+ *
40
+ * This structure is intended to allow implementing global control of the
41
+ * frame queues, including memory consumption caps.
42
+ *
43
+ * It is currently empty.
44
+ */
45
+typedef struct FFFrameQueueGlobal {
46
+} FFFrameQueueGlobal;
47
+
48
+/**
49
+ * Queue of AVFrame pointers.
50
+ */
51
+typedef struct FFFrameQueue {
52
+
53
+    /**
54
+     * Array of allocated buckets, used as a circular buffer.
55
+     */
56
+    FFFrameBucket *queue;
57
+
58
+    /**
59
+     * Size of the array of buckets.
60
+     */
61
+    size_t allocated;
62
+
63
+    /**
64
+     * Tail of the queue.
65
+     * It is the index in the array of the next frame to take.
66
+     */
67
+    size_t tail;
68
+
69
+    /**
70
+     * Number of currently queued frames.
71
+     */
72
+    size_t queued;
73
+
74
+    /**
75
+     * Pre-allocated bucket for queues of size 1.
76
+     */
77
+    FFFrameBucket first_bucket;
78
+
79
+    /**
80
+     * Total number of frames entered in the queue.
81
+     */
82
+    uint64_t total_frames_head;
83
+
84
+    /**
85
+     * Total number of frames dequeued from the queue.
86
+     * queued = total_frames_head - total_frames_tail
87
+     */
88
+    uint64_t total_frames_tail;
89
+
90
+    /**
91
+     * Total number of samples entered in the queue.
92
+     */
93
+    uint64_t total_samples_head;
94
+
95
+    /**
96
+     * Total number of samples dequeued from the queue.
97
+     * queued_samples = total_samples_head - total_samples_tail
98
+     */
99
+    uint64_t total_samples_tail;
100
+
101
+} FFFrameQueue;
102
+
103
+/**
104
+ * Init a global structure.
105
+ */
106
+void ff_framequeue_global_init(FFFrameQueueGlobal *fqg);
107
+
108
+/**
109
+ * Init a frame queue and attach it to a global structure.
110
+ */
111
+void ff_framequeue_init(FFFrameQueue *fq, FFFrameQueueGlobal *fqg);
112
+
113
+/**
114
+ * Free the queue and all queued frames.
115
+ */
116
+void ff_framequeue_free(FFFrameQueue *fq);
117
+
118
+/**
119
+ * Add a frame.
120
+ * @return  >=0 or an AVERROR code.
121
+ */
122
+int ff_framequeue_add(FFFrameQueue *fq, AVFrame *frame);
123
+
124
+/**
125
+ * Take the first frame in the queue.
126
+ * Must not be used with empty queues.
127
+ */
128
+AVFrame *ff_framequeue_take(FFFrameQueue *fq);
129
+
130
+/**
131
+ * Access a frame in the queue, without removing it.
132
+ * The first frame is numbered 0; the designated frame must exist.
133
+ */
134
+AVFrame *ff_framequeue_peek(FFFrameQueue *fq, size_t idx);
135
+
136
+/**
137
+ * Get the number of queued frames.
138
+ */
139
+static inline size_t ff_framequeue_queued_frames(const FFFrameQueue *fq)
140
+{
141
+    return fq->queued;
142
+}
143
+
144
+/**
145
+ * Get the number of queued samples.
146
+ */
147
+static inline uint64_t ff_framequeue_queued_samples(const FFFrameQueue *fq)
148
+{
149
+    return fq->total_samples_head - fq->total_samples_tail;
150
+}
151
+
152
+/**
153
+ * Update the statistics after a frame accessed using ff_framequeue_peek()
154
+ * was modified.
155
+ * Currently used only as a marker.
156
+ */
157
+static inline void ff_framequeue_update_peeked(FFFrameQueue *fq, size_t idx)
158
+{
159
+}
160
+
161
+/**
162
+ * Update the sample count in the queue.
163
+ *
164
+ * This function must be used when the first frame was accessed using
165
+ * ff_framequeue_peek() and samples were removed from it.
166
+ */
167
+static inline void ff_framequeue_skip_samples(FFFrameQueue *fq, size_t n)
168
+{
169
+    fq->total_samples_tail += n;
170
+}
171
+
172
+#endif /* AVFILTER_FRAMEQUEUE_H */