History

lavfi: add an API to synchronize multiple video inputs.

Compared to dualinput, this API can handle more than two inputs and can generate frames synchronized to any or all input streams.

Nicolas George committed 13 years ago

Commit 53319d51 parent 3cfd4df2 files · +625 −0Browse files

Unified · Split

2 files
  1. libavfilter/framesync.c +329 −0
  2. libavfilter/framesync.h +296 −0
libavfilter/framesync.c+329−0
@@ -0,0 +1,329 @@
1/*
2 * Copyright (c) 2013 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 "avfilter.h"
23#include "bufferqueue.h"
24#include "framesync.h"
25#include "internal.h"
26
27#define OFFSET(member) offsetof(FFFrameSync, member)
28
29static const char *framesync_name(void *ptr)
30{
31 return "framesync";
32}
33
34static const AVClass framesync_class = {
35 .version = LIBAVUTIL_VERSION_INT,
36 .class_name = "framesync",
37 .item_name = framesync_name,
38 .category = AV_CLASS_CATEGORY_FILTER,
39 .option = NULL,
40 .parent_log_context_offset = OFFSET(parent),
41};
42
43enum {
44 STATE_BOF,
45 STATE_RUN,
46 STATE_EOF,
47};
48
49void ff_framesync_init(FFFrameSync *fs, void *parent, unsigned nb_in)
50{
51 fs->class = &framesync_class;
52 fs->parent = parent;
53 fs->nb_in = nb_in;
54}
55
56static void framesync_sync_level_update(FFFrameSync *fs)
57{
58 unsigned i, level = 0;
59
60 for (i = 0; i < fs->nb_in; i++)
61 if (fs->in[i].state != STATE_EOF)
62 level = FFMAX(level, fs->in[i].sync);
63 av_assert0(level <= fs->sync_level);
64 if (level < fs->sync_level)
65 av_log(fs, AV_LOG_VERBOSE, "Sync level %u\n", level);
66 if (level)
67 fs->sync_level = level;
68 else
69 fs->eof = 1;
70}
71
72int ff_framesync_configure(FFFrameSync *fs)
73{
74 unsigned i;
75 int64_t gcd, lcm;
76
77 if (!fs->time_base.num) {
78 for (i = 0; i < fs->nb_in; i++) {
79 if (fs->in[i].sync) {
80 if (fs->time_base.num) {
81 gcd = av_gcd(fs->time_base.den, fs->in[i].time_base.den);
82 lcm = (fs->time_base.den / gcd) * fs->in[i].time_base.den;
83 if (lcm < AV_TIME_BASE / 2) {
84 fs->time_base.den = lcm;
85 fs->time_base.num = av_gcd(fs->time_base.num,
86 fs->in[i].time_base.num);
87 } else {
88 fs->time_base.num = 1;
89 fs->time_base.den = AV_TIME_BASE;
90 break;
91 }
92 } else {
93 fs->time_base = fs->in[i].time_base;
94 }
95 }
96 }
97 if (!fs->time_base.num) {
98 av_log(fs, AV_LOG_ERROR, "Impossible to set time base\n");
99 return AVERROR(EINVAL);
100 }
101 av_log(fs, AV_LOG_VERBOSE, "Selected %d/%d time base\n",
102 fs->time_base.num, fs->time_base.den);
103 }
104
105 for (i = 0; i < fs->nb_in; i++)
106 fs->in[i].pts = fs->in[i].pts_next = AV_NOPTS_VALUE;
107 fs->sync_level = UINT_MAX;
108 framesync_sync_level_update(fs);
109
110 return 0;
111}
112
113static void framesync_advance(FFFrameSync *fs)
114{
115 int latest;
116 unsigned i;
117 int64_t pts;
118
119 if (fs->eof)
120 return;
121 while (!fs->frame_ready) {
122 latest = -1;
123 for (i = 0; i < fs->nb_in; i++) {
124 if (!fs->in[i].have_next) {
125 if (latest < 0 || fs->in[i].pts < fs->in[latest].pts)
126 latest = i;
127 }
128 }
129 if (latest >= 0) {
130 fs->in_request = latest;
131 break;
132 }
133
134 pts = fs->in[0].pts_next;
135 for (i = 1; i < fs->nb_in; i++)
136 if (fs->in[i].pts_next < pts)
137 pts = fs->in[i].pts_next;
138 if (pts == INT64_MAX) {
139 fs->eof = 1;
140 break;
141 }
142 for (i = 0; i < fs->nb_in; i++) {
143 if (fs->in[i].pts_next == pts ||
144 (fs->in[i].before == EXT_INFINITY &&
145 fs->in[i].state == STATE_BOF)) {
146 av_frame_free(&fs->in[i].frame);
147 fs->in[i].frame = fs->in[i].frame_next;
148 fs->in[i].pts = fs->in[i].pts_next;
149 fs->in[i].frame_next = NULL;
150 fs->in[i].pts_next = AV_NOPTS_VALUE;
151 fs->in[i].have_next = 0;
152 fs->in[i].state = fs->in[i].frame ? STATE_RUN : STATE_EOF;
153 if (fs->in[i].sync == fs->sync_level && fs->in[i].frame)
154 fs->frame_ready = 1;
155 if (fs->in[i].state == STATE_EOF &&
156 fs->in[i].after == EXT_STOP)
157 fs->eof = 1;
158 }
159 }
160 if (fs->eof)
161 fs->frame_ready = 0;
162 if (fs->frame_ready)
163 for (i = 0; i < fs->nb_in; i++)
164 if ((fs->in[i].state == STATE_BOF &&
165 fs->in[i].before == EXT_STOP))
166 fs->frame_ready = 0;
167 fs->pts = pts;
168 }
169}
170
171static int64_t framesync_pts_extrapolate(FFFrameSync *fs, unsigned in,
172 int64_t pts)
173{
174 /* Possible enhancement: use the link's frame rate */
175 return pts + 1;
176}
177
178static void framesync_inject_frame(FFFrameSync *fs, unsigned in, AVFrame *frame)
179{
180 int64_t pts;
181
182 av_assert0(!fs->in[in].have_next);
183 if (frame) {
184 pts = av_rescale_q(frame->pts, fs->in[in].time_base, fs->time_base);
185 frame->pts = pts;
186 } else {
187 pts = fs->in[in].state != STATE_RUN || fs->in[in].after == EXT_INFINITY
188 ? INT64_MAX : framesync_pts_extrapolate(fs, in, fs->in[in].pts);
189 fs->in[in].sync = 0;
190 framesync_sync_level_update(fs);
191 }
192 fs->in[in].frame_next = frame;
193 fs->in[in].pts_next = pts;
194 fs->in[in].have_next = 1;
195}
196
197int ff_framesync_add_frame(FFFrameSync *fs, unsigned in, AVFrame *frame)
198{
199 av_assert1(in < fs->nb_in);
200 if (!fs->in[in].have_next)
201 framesync_inject_frame(fs, in, frame);
202 else
203 ff_bufqueue_add(fs, &fs->in[in].queue, frame);
204 return 0;
205}
206
207void ff_framesync_next(FFFrameSync *fs)
208{
209 unsigned i;
210
211 av_assert0(!fs->frame_ready);
212 for (i = 0; i < fs->nb_in; i++)
213 if (!fs->in[i].have_next && fs->in[i].queue.available)
214 framesync_inject_frame(fs, i, ff_bufqueue_get(&fs->in[i].queue));
215 fs->frame_ready = 0;
216 framesync_advance(fs);
217}
218
219void ff_framesync_drop(FFFrameSync *fs)
220{
221 fs->frame_ready = 0;
222}
223
224int ff_framesync_get_frame(FFFrameSync *fs, unsigned in, AVFrame **rframe,
225 unsigned get)
226{
227 AVFrame *frame;
228 unsigned need_copy = 0, i;
229 int64_t pts_next;
230 int ret;
231
232 if (!fs->in[in].frame) {
233 *rframe = NULL;
234 return 0;
235 }
236 frame = fs->in[in].frame;
237 if (get) {
238 /* Find out if we need to copy the frame: is there another sync
239 stream, and do we know if its current frame will outlast this one? */
240 pts_next = fs->in[in].have_next ? fs->in[in].pts_next : INT64_MAX;
241 for (i = 0; i < fs->nb_in && !need_copy; i++)
242 if (i != in && fs->in[i].sync &&
243 (!fs->in[i].have_next || fs->in[i].pts_next < pts_next))
244 need_copy = 1;
245 if (need_copy) {
246 if (!(frame = av_frame_clone(frame)))
247 return AVERROR(ENOMEM);
248 if ((ret = av_frame_make_writable(frame)) < 0) {
249 av_frame_free(&frame);
250 return ret;
251 }
252 } else {
253 fs->in[in].frame = NULL;
254 }
255 fs->frame_ready = 0;
256 }
257 *rframe = frame;
258 return 0;
259}
260
261void ff_framesync_uninit(FFFrameSync *fs)
262{
263 unsigned i;
264
265 for (i = 0; i < fs->nb_in; i++) {
266 av_frame_free(&fs->in[i].frame);
267 av_frame_free(&fs->in[i].frame_next);
268 ff_bufqueue_discard_all(&fs->in[i].queue);
269 }
270}
271
272int ff_framesync_process_frame(FFFrameSync *fs, unsigned all)
273{
274 int ret, count = 0;
275
276 av_assert0(fs->on_event);
277 while (1) {
278 ff_framesync_next(fs);
279 if (fs->eof || !fs->frame_ready)
280 break;
281 if ((ret = fs->on_event(fs)) < 0)
282 return ret;
283 ff_framesync_drop(fs);
284 count++;
285 if (!all)
286 break;
287 }
288 if (!count && fs->eof)
289 return AVERROR_EOF;
290 return count;
291}
292
293int ff_framesync_filter_frame(FFFrameSync *fs, AVFilterLink *inlink,
294 AVFrame *in)
295{
296 int ret;
297
298 if ((ret = ff_framesync_process_frame(fs, 1)) < 0)
299 return ret;
300 if ((ret = ff_framesync_add_frame(fs, FF_INLINK_IDX(inlink), in)) < 0)
301 return ret;
302 if ((ret = ff_framesync_process_frame(fs, 0)) < 0)
303 return ret;
304 return 0;
305}
306
307int ff_framesync_request_frame(FFFrameSync *fs, AVFilterLink *outlink)
308{
309 AVFilterContext *ctx = outlink->src;
310 int input, ret;
311
312 if ((ret = ff_framesync_process_frame(fs, 0)) < 0)
313 return ret;
314 if (ret > 0)
315 return 0;
316 if (fs->eof)
317 return AVERROR_EOF;
318 outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
319 input = fs->in_request;
320 ret = ff_request_frame(ctx->inputs[input]);
321 if (ret == AVERROR_EOF) {
322 if ((ret = ff_framesync_add_frame(fs, input, NULL)) < 0)
323 return ret;
324 if ((ret = ff_framesync_process_frame(fs, 0)) < 0)
325 return ret;
326 ret = 0;
327 }
328 return ret;
329}
libavfilter/framesync.h+296−0
@@ -0,0 +1,296 @@
1/*
2 * Copyright (c) 2013 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_FRAMESYNC_H
22#define AVFILTER_FRAMESYNC_H
23
24#include "bufferqueue.h"
25
26/*
27 * TODO
28 * Callback-based API similar to dualinput.
29 * Export convenient options.
30 */
31
32/**
33 * This API is intended as a helper for filters that have several video
34 * input and need to combine them somehow. If the inputs have different or
35 * variable frame rate, getting the input frames to match requires a rather
36 * complex logic and a few user-tunable options.
37 *
38 * In this API, when a set of synchronized input frames is ready to be
39 * procesed is called a frame event. Frame event can be generated in
40 * response to input frames on any or all inputs and the handling of
41 * situations where some stream extend beyond the beginning or the end of
42 * others can be configured.
43 *
44 * The basic working of this API is the following:
45 *
46 * - When a frame is available on any input, add it using
47 * ff_framesync_add_frame().
48 *
49 * - When a frame event is ready to be processed (i.e. after adding a frame
50 * or when requested on input):
51 * - call ff_framesync_next();
52 * - if fs->frame_ready is true, process the frames;
53 * - call ff_framesync_drop().
54 */
55
56/**
57 * Stream extrapolation mode
58 *
59 * Describe how the frames of a stream are extrapolated before the first one
60 * and after EOF to keep sync with possibly longer other streams.
61 */
62enum FFFrameSyncExtMode {
63
64 /**
65 * Completely stop all streams with this one.
66 */
67 EXT_STOP,
68
69 /**
70 * Ignore this stream and continue processing the other ones.
71 */
72 EXT_NULL,
73
74 /**
75 * Extend the frame to infinity.
76 */
77 EXT_INFINITY,
78};
79
80/**
81 * Input stream structure
82 */
83typedef struct FFFrameSyncIn {
84
85 /**
86 * Queue of incoming AVFrame, and NULL to mark EOF
87 */
88 struct FFBufQueue queue;
89
90 /**
91 * Extrapolation mode for timestamps before the first frame
92 */
93 enum FFFrameSyncExtMode before;
94
95 /**
96 * Extrapolation mode for timestamps after the last frame
97 */
98 enum FFFrameSyncExtMode after;
99
100 /**
101 * Time base for the incoming frames
102 */
103 AVRational time_base;
104
105 /**
106 * Current frame, may be NULL before the first one or after EOF
107 */
108 AVFrame *frame;
109
110 /**
111 * Next frame, for internal use
112 */
113 AVFrame *frame_next;
114
115 /**
116 * PTS of the current frame
117 */
118 int64_t pts;
119
120 /**
121 * PTS of the next frame, for internal use
122 */
123 int64_t pts_next;
124
125 /**
126 * Boolean flagging the next frame, for internal use
127 */
128 uint8_t have_next;
129
130 /**
131 * State: before first, in stream or after EOF, for internal use
132 */
133 uint8_t state;
134
135 /**
136 * Synchronization level: frames on input at the highest sync level will
137 * generate output frame events.
138 *
139 * For example, if inputs #0 and #1 have sync level 2 and input #2 has
140 * sync level 1, then a frame on either input #0 or #1 will generate a
141 * frame event, but not a frame on input #2 until both inputs #0 and #1
142 * have reached EOF.
143 *
144 * If sync is 0, no frame event will be generated.
145 */
146 unsigned sync;
147
148} FFFrameSyncIn;
149
150/**
151 * Frame sync structure.
152 */
153typedef struct FFFrameSync {
154 const AVClass *class;
155 void *parent;
156
157 /**
158 * Number of input streams
159 */
160 unsigned nb_in;
161
162 /**
163 * Time base for the output events
164 */
165 AVRational time_base;
166
167 /**
168 * Timestamp of the current event
169 */
170 int64_t pts;
171
172 /**
173 * Callback called when a frame event is ready
174 */
175 int (*on_event)(struct FFFrameSync *fs);
176
177 /**
178 * Opaque pointer, not used by the API
179 */
180 void *opaque;
181
182 /**
183 * Index of the input that requires a request
184 */
185 unsigned in_request;
186
187 /**
188 * Synchronization level: only inputs with the same sync level are sync
189 * sources.
190 */
191 unsigned sync_level;
192
193 /**
194 * Flag indicating that a frame event is ready
195 */
196 uint8_t frame_ready;
197
198 /**
199 * Flag indicating that output has reached EOF.
200 */
201 uint8_t eof;
202
203 /**
204 * Array of inputs; all inputs must be in consecutive memory
205 */
206 FFFrameSyncIn in[1]; /* must be the last field */
207
208} FFFrameSync;
209
210/**
211 * Initialize a frame sync structure.
212 *
213 * The entire structure is expected to be already set to 0.
214 *
215 * @param fs frame sync structure to initialize
216 * @param parent parent object, used for logging
217 * @param nb_in number of inputs
218 */
219void ff_framesync_init(FFFrameSync *fs, void *parent, unsigned nb_in);
220
221/**
222 * Configure a frame sync structure.
223 *
224 * Must be called after all options are set but before all use.
225 *
226 * @return >= 0 for success or a negative error code
227 */
228int ff_framesync_configure(FFFrameSync *fs);
229
230/**
231 * Free all memory currently allocated.
232 */
233void ff_framesync_uninit(FFFrameSync *fs);
234
235/**
236 * Add a frame to an input
237 *
238 * Typically called from the filter_frame() method.
239 *
240 * @param fs frame sync structure
241 * @param in index of the input
242 * @param frame input frame, or NULL for EOF
243 */
244int ff_framesync_add_frame(FFFrameSync *fs, unsigned in, AVFrame *frame);
245
246/**
247 * Prepare the next frame event.
248 *
249 * The status of the operation can be found in fs->frame_ready and fs->eof.
250 */
251void ff_framesync_next(FFFrameSync *fs);
252
253/**
254 * Drop the current frame event.
255 */
256void ff_framesync_drop(FFFrameSync *fs);
257
258/**
259 * Get the current frame in an input.
260 *
261 * @param fs frame sync structure
262 * @param in index of the input
263 * @param rframe used to return the current frame (or NULL)
264 * @param get if not zero, the calling code needs to get ownership of
265 * the returned frame; the current frame will either be
266 * duplicated or removed from the framesync structure
267 */
268int ff_framesync_get_frame(FFFrameSync *fs, unsigned in, AVFrame **rframe,
269 unsigned get);
270
271/**
272 * Process one or several frame using the on_event callback.
273 *
274 * @return number of frames processed or negative error code
275 */
276int ff_framesync_process_frame(FFFrameSync *fs, unsigned all);
277
278
279/**
280 * Accept a frame on a filter input.
281 *
282 * This function can be the complete implementation of all filter_frame
283 * methods of a filter using framesync.
284 */
285int ff_framesync_filter_frame(FFFrameSync *fs, AVFilterLink *inlink,
286 AVFrame *in);
287
288/**
289 * Request a frame on the filter output.
290 *
291 * This function can be the complete implementation of all filter_frame
292 * methods of a filter using framesync if it has only one output.
293 */
294int ff_framesync_request_frame(FFFrameSync *fs, AVFilterLink *outlink);
295
296#endif /* AVFILTER_FRAMESYNC_H */

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t