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 "libavutil/opt.h"
23 #include "avfilter.h"
24 #include "filters.h"
25 #include "framesync.h"
26 #include "internal.h"
27
28 #define OFFSET(member) offsetof(FFFrameSync, member)
29 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
30
framesync_name(void * ptr)31 static const char *framesync_name(void *ptr)
32 {
33 return "framesync";
34 }
35
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 },
45 { NULL }
46 };
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),
54 };
55
56 enum {
57 STATE_BOF,
58 STATE_RUN,
59 STATE_EOF,
60 };
61
62 static int consume_from_fifos(FFFrameSync *fs);
63
ff_framesync_get_class(void)64 const AVClass *ff_framesync_get_class(void)
65 {
66 return &framesync_class;
67 }
68
ff_framesync_preinit(FFFrameSync * fs)69 void ff_framesync_preinit(FFFrameSync *fs)
70 {
71 if (fs->class)
72 return;
73 fs->class = &framesync_class;
74 av_opt_set_defaults(fs);
75 }
76
ff_framesync_init(FFFrameSync * fs,AVFilterContext * parent,unsigned nb_in)77 int ff_framesync_init(FFFrameSync *fs, AVFilterContext *parent, unsigned nb_in)
78 {
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);
83
84 ff_framesync_preinit(fs);
85 fs->parent = parent;
86 fs->nb_in = nb_in;
87
88 fs->in = av_calloc(nb_in, sizeof(*fs->in));
89 if (!fs->in)
90 return AVERROR(ENOMEM);
91 return 0;
92 }
93
framesync_eof(FFFrameSync * fs)94 static void framesync_eof(FFFrameSync *fs)
95 {
96 fs->eof = 1;
97 fs->frame_ready = 0;
98 ff_outlink_set_status(fs->parent->outputs[0], AVERROR_EOF, AV_NOPTS_VALUE);
99 }
100
framesync_sync_level_update(FFFrameSync * fs)101 static void framesync_sync_level_update(FFFrameSync *fs)
102 {
103 unsigned i, level = 0;
104
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);
111 if (level)
112 fs->sync_level = level;
113 else
114 framesync_eof(fs);
115 }
116
ff_framesync_configure(FFFrameSync * fs)117 int ff_framesync_configure(FFFrameSync *fs)
118 {
119 unsigned i;
120
121 if (!fs->opt_repeatlast || fs->opt_eof_action == EOF_ACTION_PASS) {
122 fs->opt_repeatlast = 0;
123 fs->opt_eof_action = EOF_ACTION_PASS;
124 }
125 if (fs->opt_shortest || fs->opt_eof_action == EOF_ACTION_ENDALL) {
126 fs->opt_shortest = 1;
127 fs->opt_eof_action = EOF_ACTION_ENDALL;
128 }
129 if (!fs->opt_repeatlast) {
130 for (i = 1; i < fs->nb_in; i++) {
131 fs->in[i].after = EXT_NULL;
132 fs->in[i].sync = 0;
133 }
134 }
135 if (fs->opt_shortest) {
136 for (i = 0; i < fs->nb_in; i++)
137 fs->in[i].after = EXT_STOP;
138 }
139
140 if (!fs->time_base.num) {
141 for (i = 0; i < fs->nb_in; i++) {
142 if (fs->in[i].sync) {
143 if (fs->time_base.num) {
144 fs->time_base = av_gcd_q(fs->time_base, fs->in[i].time_base,
145 AV_TIME_BASE / 2, AV_TIME_BASE_Q);
146 } else {
147 fs->time_base = fs->in[i].time_base;
148 }
149 }
150 }
151 if (!fs->time_base.num) {
152 av_log(fs, AV_LOG_ERROR, "Impossible to set time base\n");
153 return AVERROR(EINVAL);
154 }
155 av_log(fs, AV_LOG_VERBOSE, "Selected %d/%d time base\n",
156 fs->time_base.num, fs->time_base.den);
157 }
158
159 for (i = 0; i < fs->nb_in; i++)
160 fs->in[i].pts = fs->in[i].pts_next = AV_NOPTS_VALUE;
161 fs->sync_level = UINT_MAX;
162 framesync_sync_level_update(fs);
163
164 return 0;
165 }
166
framesync_advance(FFFrameSync * fs)167 static int framesync_advance(FFFrameSync *fs)
168 {
169 unsigned i;
170 int64_t pts;
171 int ret;
172
173 while (!(fs->frame_ready || fs->eof)) {
174 ret = consume_from_fifos(fs);
175 if (ret <= 0)
176 return ret;
177
178 pts = INT64_MAX;
179 for (i = 0; i < fs->nb_in; i++)
180 if (fs->in[i].have_next && fs->in[i].pts_next < pts)
181 pts = fs->in[i].pts_next;
182 if (pts == INT64_MAX) {
183 framesync_eof(fs);
184 break;
185 }
186 for (i = 0; i < fs->nb_in; i++) {
187 if (fs->in[i].pts_next == pts ||
188 (fs->in[i].before == EXT_INFINITY &&
189 fs->in[i].state == STATE_BOF)) {
190 av_frame_free(&fs->in[i].frame);
191 fs->in[i].frame = fs->in[i].frame_next;
192 fs->in[i].pts = fs->in[i].pts_next;
193 fs->in[i].frame_next = NULL;
194 fs->in[i].pts_next = AV_NOPTS_VALUE;
195 fs->in[i].have_next = 0;
196 fs->in[i].state = fs->in[i].frame ? STATE_RUN : STATE_EOF;
197 if (fs->in[i].sync == fs->sync_level && fs->in[i].frame)
198 fs->frame_ready = 1;
199 if (fs->in[i].state == STATE_EOF &&
200 fs->in[i].after == EXT_STOP)
201 framesync_eof(fs);
202 }
203 }
204 if (fs->frame_ready)
205 for (i = 0; i < fs->nb_in; i++)
206 if ((fs->in[i].state == STATE_BOF &&
207 fs->in[i].before == EXT_STOP))
208 fs->frame_ready = 0;
209 fs->pts = pts;
210 }
211 return 0;
212 }
213
framesync_pts_extrapolate(FFFrameSync * fs,unsigned in,int64_t pts)214 static int64_t framesync_pts_extrapolate(FFFrameSync *fs, unsigned in,
215 int64_t pts)
216 {
217 /* Possible enhancement: use the link's frame rate */
218 return pts + 1;
219 }
220
framesync_inject_frame(FFFrameSync * fs,unsigned in,AVFrame * frame)221 static void framesync_inject_frame(FFFrameSync *fs, unsigned in, AVFrame *frame)
222 {
223 int64_t pts;
224
225 av_assert0(!fs->in[in].have_next);
226 av_assert0(frame);
227 pts = av_rescale_q(frame->pts, fs->in[in].time_base, fs->time_base);
228 frame->pts = pts;
229 fs->in[in].frame_next = frame;
230 fs->in[in].pts_next = pts;
231 fs->in[in].have_next = 1;
232 }
233
framesync_inject_status(FFFrameSync * fs,unsigned in,int status,int64_t pts)234 static void framesync_inject_status(FFFrameSync *fs, unsigned in, int status, int64_t pts)
235 {
236 av_assert0(!fs->in[in].have_next);
237 pts = fs->in[in].state != STATE_RUN || fs->in[in].after == EXT_INFINITY
238 ? INT64_MAX : framesync_pts_extrapolate(fs, in, fs->in[in].pts);
239 fs->in[in].sync = 0;
240 framesync_sync_level_update(fs);
241 fs->in[in].frame_next = NULL;
242 fs->in[in].pts_next = pts;
243 fs->in[in].have_next = 1;
244 }
245
ff_framesync_get_frame(FFFrameSync * fs,unsigned in,AVFrame ** rframe,unsigned get)246 int ff_framesync_get_frame(FFFrameSync *fs, unsigned in, AVFrame **rframe,
247 unsigned get)
248 {
249 AVFrame *frame;
250 unsigned need_copy = 0, i;
251 int64_t pts_next;
252 int ret;
253
254 if (!fs->in[in].frame) {
255 *rframe = NULL;
256 return 0;
257 }
258 frame = fs->in[in].frame;
259 if (get) {
260 /* Find out if we need to copy the frame: is there another sync
261 stream, and do we know if its current frame will outlast this one? */
262 pts_next = fs->in[in].have_next ? fs->in[in].pts_next : INT64_MAX;
263 for (i = 0; i < fs->nb_in && !need_copy; i++)
264 if (i != in && fs->in[i].sync &&
265 (!fs->in[i].have_next || fs->in[i].pts_next < pts_next))
266 need_copy = 1;
267 if (need_copy) {
268 if (!(frame = av_frame_clone(frame)))
269 return AVERROR(ENOMEM);
270 if ((ret = av_frame_make_writable(frame)) < 0) {
271 av_frame_free(&frame);
272 return ret;
273 }
274 } else {
275 fs->in[in].frame = NULL;
276 }
277 fs->frame_ready = 0;
278 }
279 *rframe = frame;
280 return 0;
281 }
282
ff_framesync_uninit(FFFrameSync * fs)283 void ff_framesync_uninit(FFFrameSync *fs)
284 {
285 unsigned i;
286
287 for (i = 0; i < fs->nb_in; i++) {
288 av_frame_free(&fs->in[i].frame);
289 av_frame_free(&fs->in[i].frame_next);
290 }
291
292 av_freep(&fs->in);
293 }
294
consume_from_fifos(FFFrameSync * fs)295 static int consume_from_fifos(FFFrameSync *fs)
296 {
297 AVFilterContext *ctx = fs->parent;
298 AVFrame *frame = NULL;
299 int64_t pts;
300 unsigned i, nb_active, nb_miss;
301 int ret, status;
302
303 nb_active = nb_miss = 0;
304 for (i = 0; i < fs->nb_in; i++) {
305 if (fs->in[i].have_next || fs->in[i].state == STATE_EOF)
306 continue;
307 nb_active++;
308 ret = ff_inlink_consume_frame(ctx->inputs[i], &frame);
309 if (ret < 0)
310 return ret;
311 if (ret) {
312 av_assert0(frame);
313 framesync_inject_frame(fs, i, frame);
314 } else {
315 ret = ff_inlink_acknowledge_status(ctx->inputs[i], &status, &pts);
316 if (ret > 0) {
317 framesync_inject_status(fs, i, status, pts);
318 } else if (!ret) {
319 nb_miss++;
320 }
321 }
322 }
323 if (nb_miss) {
324 if (nb_miss == nb_active && !ff_outlink_frame_wanted(ctx->outputs[0]))
325 return FFERROR_NOT_READY;
326 for (i = 0; i < fs->nb_in; i++)
327 if (!fs->in[i].have_next && fs->in[i].state != STATE_EOF)
328 ff_inlink_request_frame(ctx->inputs[i]);
329 return 0;
330 }
331 return 1;
332 }
333
ff_framesync_activate(FFFrameSync * fs)334 int ff_framesync_activate(FFFrameSync *fs)
335 {
336 int ret;
337
338 ret = framesync_advance(fs);
339 if (ret < 0)
340 return ret;
341 if (fs->eof || !fs->frame_ready)
342 return 0;
343 ret = fs->on_event(fs);
344 if (ret < 0)
345 return ret;
346 fs->frame_ready = 0;
347
348 return 0;
349 }
350
ff_framesync_init_dualinput(FFFrameSync * fs,AVFilterContext * parent)351 int ff_framesync_init_dualinput(FFFrameSync *fs, AVFilterContext *parent)
352 {
353 int ret;
354
355 ret = ff_framesync_init(fs, parent, 2);
356 if (ret < 0)
357 return ret;
358 fs->in[0].time_base = parent->inputs[0]->time_base;
359 fs->in[1].time_base = parent->inputs[1]->time_base;
360 fs->in[0].sync = 2;
361 fs->in[0].before = EXT_STOP;
362 fs->in[0].after = EXT_INFINITY;
363 fs->in[1].sync = 1;
364 fs->in[1].before = EXT_NULL;
365 fs->in[1].after = EXT_INFINITY;
366 return 0;
367 }
368
ff_framesync_dualinput_get(FFFrameSync * fs,AVFrame ** f0,AVFrame ** f1)369 int ff_framesync_dualinput_get(FFFrameSync *fs, AVFrame **f0, AVFrame **f1)
370 {
371 AVFilterContext *ctx = fs->parent;
372 AVFrame *mainpic = NULL, *secondpic = NULL;
373 int ret;
374
375 if ((ret = ff_framesync_get_frame(fs, 0, &mainpic, 1)) < 0 ||
376 (ret = ff_framesync_get_frame(fs, 1, &secondpic, 0)) < 0) {
377 av_frame_free(&mainpic);
378 return ret;
379 }
380 av_assert0(mainpic);
381 mainpic->pts = av_rescale_q(fs->pts, fs->time_base, ctx->outputs[0]->time_base);
382 if (ctx->is_disabled)
383 secondpic = NULL;
384 *f0 = mainpic;
385 *f1 = secondpic;
386 return 0;
387 }
388
ff_framesync_dualinput_get_writable(FFFrameSync * fs,AVFrame ** f0,AVFrame ** f1)389 int ff_framesync_dualinput_get_writable(FFFrameSync *fs, AVFrame **f0, AVFrame **f1)
390 {
391 int ret;
392
393 ret = ff_framesync_dualinput_get(fs, f0, f1);
394 if (ret < 0)
395 return ret;
396 ret = ff_inlink_make_frame_writable(fs->parent->inputs[0], f0);
397 if (ret < 0) {
398 av_frame_free(f0);
399 *f1 = NULL;
400 return ret;
401 }
402 return 0;
403 }
404