2 * Copyright (c) 2013 Nicolas George
4 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavutil/avassert.h"
22 #include "libavutil/opt.h"
25 #include "framesync.h"
28 #define OFFSET(member) offsetof(FFFrameSync, member)
29 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
31 static const char *framesync_name(void *ptr)
36 static const AVOption framesync_options[] = {
37 { "eof_action", "Action to take when encountering EOF from secondary input ",
38 OFFSET(opt_eof_action), AV_OPT_TYPE_INT, { .i64 = EOF_ACTION_REPEAT },
39 EOF_ACTION_REPEAT, EOF_ACTION_PASS, .flags = FLAGS, "eof_action" },
40 { "repeat", "Repeat the previous frame.", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_REPEAT }, .flags = FLAGS, "eof_action" },
41 { "endall", "End both streams.", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_ENDALL }, .flags = FLAGS, "eof_action" },
42 { "pass", "Pass through the main input.", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_PASS }, .flags = FLAGS, "eof_action" },
43 { "shortest", "force termination when the shortest input terminates", OFFSET(opt_shortest), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
44 { "repeatlast", "extend last frame of secondary streams beyond EOF", OFFSET(opt_repeatlast), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, FLAGS },
47 static const AVClass framesync_class = {
48 .version = LIBAVUTIL_VERSION_INT,
49 .class_name = "framesync",
50 .item_name = framesync_name,
51 .category = AV_CLASS_CATEGORY_FILTER,
52 .option = framesync_options,
53 .parent_log_context_offset = OFFSET(parent),
62 static int consume_from_fifos(FFFrameSync *fs);
64 const AVClass *ff_framesync_get_class(void)
66 return &framesync_class;
69 void ff_framesync_preinit(FFFrameSync *fs)
73 fs->class = &framesync_class;
74 av_opt_set_defaults(fs);
77 int ff_framesync_init(FFFrameSync *fs, AVFilterContext *parent, unsigned nb_in)
79 /* For filters with several outputs, we will not be able to assume which
80 output is relevant for ff_outlink_frame_wanted() and
81 ff_outlink_set_status(). To be designed when needed. */
82 av_assert0(parent->nb_outputs == 1);
84 ff_framesync_preinit(fs);
88 fs->in = av_calloc(nb_in, sizeof(*fs->in));
90 return AVERROR(ENOMEM);
94 static void framesync_eof(FFFrameSync *fs)
98 ff_outlink_set_status(fs->parent->outputs[0], AVERROR_EOF, AV_NOPTS_VALUE);
101 static void framesync_sync_level_update(FFFrameSync *fs)
103 unsigned i, level = 0;
105 for (i = 0; i < fs->nb_in; i++)
106 if (fs->in[i].state != STATE_EOF)
107 level = FFMAX(level, fs->in[i].sync);
108 av_assert0(level <= fs->sync_level);
109 if (level < fs->sync_level)
110 av_log(fs, AV_LOG_VERBOSE, "Sync level %u\n", level);
112 fs->sync_level = level;
117 int ff_framesync_configure(FFFrameSync *fs)
122 if (!fs->opt_repeatlast || fs->opt_eof_action == EOF_ACTION_PASS) {
123 fs->opt_repeatlast = 0;
124 fs->opt_eof_action = EOF_ACTION_PASS;
126 if (fs->opt_shortest || fs->opt_eof_action == EOF_ACTION_ENDALL) {
127 fs->opt_shortest = 1;
128 fs->opt_eof_action = EOF_ACTION_ENDALL;
130 if (!fs->opt_repeatlast) {
131 for (i = 1; i < fs->nb_in; i++) {
132 fs->in[i].after = EXT_NULL;
136 if (fs->opt_shortest) {
137 for (i = 0; i < fs->nb_in; i++)
138 fs->in[i].after = EXT_STOP;
141 if (!fs->time_base.num) {
142 for (i = 0; i < fs->nb_in; i++) {
143 if (fs->in[i].sync) {
144 if (fs->time_base.num) {
145 gcd = av_gcd(fs->time_base.den, fs->in[i].time_base.den);
146 lcm = (fs->time_base.den / gcd) * fs->in[i].time_base.den;
147 if (lcm < AV_TIME_BASE / 2) {
148 fs->time_base.den = lcm;
149 fs->time_base.num = av_gcd(fs->time_base.num,
150 fs->in[i].time_base.num);
152 fs->time_base.num = 1;
153 fs->time_base.den = AV_TIME_BASE;
157 fs->time_base = fs->in[i].time_base;
161 if (!fs->time_base.num) {
162 av_log(fs, AV_LOG_ERROR, "Impossible to set time base\n");
163 return AVERROR(EINVAL);
165 av_log(fs, AV_LOG_VERBOSE, "Selected %d/%d time base\n",
166 fs->time_base.num, fs->time_base.den);
169 for (i = 0; i < fs->nb_in; i++)
170 fs->in[i].pts = fs->in[i].pts_next = AV_NOPTS_VALUE;
171 fs->sync_level = UINT_MAX;
172 framesync_sync_level_update(fs);
177 static int framesync_advance(FFFrameSync *fs)
183 while (!(fs->frame_ready || fs->eof)) {
184 ret = consume_from_fifos(fs);
189 for (i = 0; i < fs->nb_in; i++)
190 if (fs->in[i].have_next && fs->in[i].pts_next < pts)
191 pts = fs->in[i].pts_next;
192 if (pts == INT64_MAX) {
196 for (i = 0; i < fs->nb_in; i++) {
197 if (fs->in[i].pts_next == pts ||
198 (fs->in[i].before == EXT_INFINITY &&
199 fs->in[i].state == STATE_BOF)) {
200 av_frame_free(&fs->in[i].frame);
201 fs->in[i].frame = fs->in[i].frame_next;
202 fs->in[i].pts = fs->in[i].pts_next;
203 fs->in[i].frame_next = NULL;
204 fs->in[i].pts_next = AV_NOPTS_VALUE;
205 fs->in[i].have_next = 0;
206 fs->in[i].state = fs->in[i].frame ? STATE_RUN : STATE_EOF;
207 if (fs->in[i].sync == fs->sync_level && fs->in[i].frame)
209 if (fs->in[i].state == STATE_EOF &&
210 fs->in[i].after == EXT_STOP)
215 for (i = 0; i < fs->nb_in; i++)
216 if ((fs->in[i].state == STATE_BOF &&
217 fs->in[i].before == EXT_STOP))
224 static int64_t framesync_pts_extrapolate(FFFrameSync *fs, unsigned in,
227 /* Possible enhancement: use the link's frame rate */
231 static void framesync_inject_frame(FFFrameSync *fs, unsigned in, AVFrame *frame)
235 av_assert0(!fs->in[in].have_next);
237 pts = av_rescale_q(frame->pts, fs->in[in].time_base, fs->time_base);
239 fs->in[in].frame_next = frame;
240 fs->in[in].pts_next = pts;
241 fs->in[in].have_next = 1;
244 static void framesync_inject_status(FFFrameSync *fs, unsigned in, int status, int64_t pts)
246 av_assert0(!fs->in[in].have_next);
247 pts = fs->in[in].state != STATE_RUN || fs->in[in].after == EXT_INFINITY
248 ? INT64_MAX : framesync_pts_extrapolate(fs, in, fs->in[in].pts);
250 framesync_sync_level_update(fs);
251 fs->in[in].frame_next = NULL;
252 fs->in[in].pts_next = pts;
253 fs->in[in].have_next = 1;
256 int ff_framesync_get_frame(FFFrameSync *fs, unsigned in, AVFrame **rframe,
260 unsigned need_copy = 0, i;
264 if (!fs->in[in].frame) {
268 frame = fs->in[in].frame;
270 /* Find out if we need to copy the frame: is there another sync
271 stream, and do we know if its current frame will outlast this one? */
272 pts_next = fs->in[in].have_next ? fs->in[in].pts_next : INT64_MAX;
273 for (i = 0; i < fs->nb_in && !need_copy; i++)
274 if (i != in && fs->in[i].sync &&
275 (!fs->in[i].have_next || fs->in[i].pts_next < pts_next))
278 if (!(frame = av_frame_clone(frame)))
279 return AVERROR(ENOMEM);
280 if ((ret = av_frame_make_writable(frame)) < 0) {
281 av_frame_free(&frame);
285 fs->in[in].frame = NULL;
293 void ff_framesync_uninit(FFFrameSync *fs)
297 for (i = 0; i < fs->nb_in; i++) {
298 av_frame_free(&fs->in[i].frame);
299 av_frame_free(&fs->in[i].frame_next);
305 static int consume_from_fifos(FFFrameSync *fs)
307 AVFilterContext *ctx = fs->parent;
308 AVFrame *frame = NULL;
310 unsigned i, nb_active, nb_miss;
313 nb_active = nb_miss = 0;
314 for (i = 0; i < fs->nb_in; i++) {
315 if (fs->in[i].have_next || fs->in[i].state == STATE_EOF)
318 ret = ff_inlink_consume_frame(ctx->inputs[i], &frame);
323 framesync_inject_frame(fs, i, frame);
325 ret = ff_inlink_acknowledge_status(ctx->inputs[i], &status, &pts);
327 framesync_inject_status(fs, i, status, pts);
334 if (nb_miss == nb_active && !ff_outlink_frame_wanted(ctx->outputs[0]))
335 return FFERROR_NOT_READY;
336 for (i = 0; i < fs->nb_in; i++)
337 if (!fs->in[i].have_next && fs->in[i].state != STATE_EOF)
338 ff_inlink_request_frame(ctx->inputs[i]);
344 int ff_framesync_activate(FFFrameSync *fs)
348 ret = framesync_advance(fs);
351 if (fs->eof || !fs->frame_ready)
353 ret = fs->on_event(fs);
361 int ff_framesync_init_dualinput(FFFrameSync *fs, AVFilterContext *parent)
365 ret = ff_framesync_init(fs, parent, 2);
368 fs->in[0].time_base = parent->inputs[0]->time_base;
369 fs->in[1].time_base = parent->inputs[1]->time_base;
371 fs->in[0].before = EXT_STOP;
372 fs->in[0].after = EXT_INFINITY;
374 fs->in[1].before = EXT_NULL;
375 fs->in[1].after = EXT_INFINITY;
379 int ff_framesync_dualinput_get(FFFrameSync *fs, AVFrame **f0, AVFrame **f1)
381 AVFilterContext *ctx = fs->parent;
382 AVFrame *mainpic = NULL, *secondpic = NULL;
385 if ((ret = ff_framesync_get_frame(fs, 0, &mainpic, 1)) < 0 ||
386 (ret = ff_framesync_get_frame(fs, 1, &secondpic, 0)) < 0) {
387 av_frame_free(&mainpic);
391 mainpic->pts = av_rescale_q(fs->pts, fs->time_base, ctx->outputs[0]->time_base);
392 if (ctx->is_disabled)
399 int ff_framesync_dualinput_get_writable(FFFrameSync *fs, AVFrame **f0, AVFrame **f1)
403 ret = ff_framesync_dualinput_get(fs, f0, f1);
406 ret = ff_inlink_make_frame_writable(fs->parent->inputs[0], f0);