Browse code

lavfi: add realtime filter.

Similar to the -re option in ffmpeg that only works for input files.
Can be used at any place in the filter graph.

Nicolas George authored on 2015/10/26 01:31:58
Showing 5 changed files
... ...
@@ -31,6 +31,7 @@ version <next>:
31 31
 - ADPCM AICA decoder
32 32
 - Interplay ACM demuxer and audio decoder
33 33
 - XMA1 & XMA2 decoder
34
+- realtime filter
34 35
 
35 36
 
36 37
 version 2.8:
... ...
@@ -13068,6 +13068,22 @@ following one, the permission might not be received as expected in that
13068 13068
 following filter. Inserting a @ref{format} or @ref{aformat} filter before the
13069 13069
 perms/aperms filter can avoid this problem.
13070 13070
 
13071
+@section realtime, arealtime
13072
+
13073
+Slow down filtering to match real time approximatively.
13074
+
13075
+These filters will pause the filtering for a variable amount of time to
13076
+match the output rate with the input timestamps.
13077
+They are similar to the @option{re} option to @code{ffmpeg}.
13078
+
13079
+They accept the following options:
13080
+
13081
+@table @option
13082
+@item limit
13083
+Time limit for the pauses. Any pause longer than that will be considered
13084
+a timestamp discontinuity and reset the timer. Default is 2 seconds.
13085
+@end table
13086
+
13071 13087
 @section select, aselect
13072 13088
 
13073 13089
 Select frames to pass in output.
... ...
@@ -39,6 +39,7 @@ OBJS-$(CONFIG_ANULL_FILTER)                  += af_anull.o
39 39
 OBJS-$(CONFIG_APAD_FILTER)                   += af_apad.o
40 40
 OBJS-$(CONFIG_APERMS_FILTER)                 += f_perms.o
41 41
 OBJS-$(CONFIG_APHASER_FILTER)                += af_aphaser.o generate_wave_table.o
42
+OBJS-$(CONFIG_AREALTIME_FILTER)              += f_realtime.o
42 43
 OBJS-$(CONFIG_ARESAMPLE_FILTER)              += af_aresample.o
43 44
 OBJS-$(CONFIG_AREVERSE_FILTER)               += f_reverse.o
44 45
 OBJS-$(CONFIG_ASELECT_FILTER)                += f_select.o
... ...
@@ -197,6 +198,7 @@ OBJS-$(CONFIG_PSNR_FILTER)                   += vf_psnr.o dualinput.o framesync.
197 197
 OBJS-$(CONFIG_PULLUP_FILTER)                 += vf_pullup.o
198 198
 OBJS-$(CONFIG_QP_FILTER)                     += vf_qp.o
199 199
 OBJS-$(CONFIG_RANDOM_FILTER)                 += vf_random.o
200
+OBJS-$(CONFIG_REALTIME_FILTER)               += f_realtime.o
200 201
 OBJS-$(CONFIG_REMOVEGRAIN_FILTER)            += vf_removegrain.o
201 202
 OBJS-$(CONFIG_REMOVELOGO_FILTER)             += bbox.o lswsutils.o lavfutils.o vf_removelogo.o
202 203
 OBJS-$(CONFIG_REPEATFIELDS_FILTER)           += vf_repeatfields.o
... ...
@@ -61,6 +61,7 @@ void avfilter_register_all(void)
61 61
     REGISTER_FILTER(APAD,           apad,           af);
62 62
     REGISTER_FILTER(APERMS,         aperms,         af);
63 63
     REGISTER_FILTER(APHASER,        aphaser,        af);
64
+    REGISTER_FILTER(AREALTIME,      arealtime,      af);
64 65
     REGISTER_FILTER(ARESAMPLE,      aresample,      af);
65 66
     REGISTER_FILTER(AREVERSE,       areverse,       af);
66 67
     REGISTER_FILTER(ASELECT,        aselect,        af);
... ...
@@ -218,6 +219,7 @@ void avfilter_register_all(void)
218 218
     REGISTER_FILTER(PULLUP,         pullup,         vf);
219 219
     REGISTER_FILTER(QP,             qp,             vf);
220 220
     REGISTER_FILTER(RANDOM,         random,         vf);
221
+    REGISTER_FILTER(REALTIME,       realtime,       vf);
221 222
     REGISTER_FILTER(REMOVEGRAIN,    removegrain,    vf);
222 223
     REGISTER_FILTER(REMOVELOGO,     removelogo,     vf);
223 224
     REGISTER_FILTER(REPEATFIELDS,   repeatfields,   vf);
224 225
new file mode 100644
... ...
@@ -0,0 +1,132 @@
0
+/*
1
+ * Copyright (c) 2015 Nicolas George
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public License
7
+ * as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
+ * GNU Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public License
16
+ * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
17
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+#include "libavutil/opt.h"
21
+#include "libavutil/time.h"
22
+#include "avfilter.h"
23
+#include "internal.h"
24
+
25
+typedef struct RealtimeContext {
26
+    const AVClass *class;
27
+    int64_t delta;
28
+    int64_t limit;
29
+    unsigned inited;
30
+} RealtimeContext;
31
+
32
+static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
33
+{
34
+    AVFilterContext *ctx = inlink->dst;
35
+    RealtimeContext *s = ctx->priv;
36
+
37
+    if (frame->pts != AV_NOPTS_VALUE) {
38
+        int64_t pts = av_rescale_q(frame->pts, inlink->time_base, AV_TIME_BASE_Q);
39
+        int64_t now = av_gettime_relative();
40
+        int64_t sleep = pts - now + s->delta;
41
+        if (!s->inited) {
42
+            s->inited = 1;
43
+            sleep = 0;
44
+            s->delta = now - pts;
45
+        }
46
+        if (sleep > s->limit || sleep < -s->limit) {
47
+            av_log(ctx, AV_LOG_WARNING,
48
+                   "time discontinuity detected: %"PRIi64" us, resetting\n",
49
+                   sleep);
50
+            sleep = 0;
51
+            s->delta = now - pts;
52
+        }
53
+        if (sleep > 0) {
54
+            av_log(ctx, AV_LOG_DEBUG, "sleeping %"PRIi64" us\n", sleep);
55
+            for (; sleep > 600000000; sleep -= 600000000)
56
+                av_usleep(600000000);
57
+            av_usleep(sleep);
58
+        }
59
+    }
60
+    return ff_filter_frame(inlink->dst->outputs[0], frame);
61
+}
62
+
63
+#define OFFSET(x) offsetof(RealtimeContext, x)
64
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
65
+static const AVOption options[] = {
66
+    { "limit", "sleep time limit", OFFSET(limit), AV_OPT_TYPE_DURATION, { .i64 = 2000000 }, 0, INT64_MAX, FLAGS },
67
+    { NULL }
68
+};
69
+
70
+#if CONFIG_REALTIME_FILTER
71
+#define realtime_options options
72
+AVFILTER_DEFINE_CLASS(realtime);
73
+
74
+static const AVFilterPad avfilter_vf_realtime_inputs[] = {
75
+    {
76
+        .name         = "default",
77
+        .type         = AVMEDIA_TYPE_VIDEO,
78
+        .filter_frame = filter_frame,
79
+    },
80
+    { NULL }
81
+};
82
+
83
+static const AVFilterPad avfilter_vf_realtime_outputs[] = {
84
+    {
85
+        .name = "default",
86
+        .type = AVMEDIA_TYPE_VIDEO,
87
+    },
88
+    { NULL }
89
+};
90
+
91
+AVFilter ff_vf_realtime = {
92
+    .name        = "realtime",
93
+    .description = NULL_IF_CONFIG_SMALL("Slow down filtering to match realtime"),
94
+    .priv_size   = sizeof(RealtimeContext),
95
+    .priv_class  = &realtime_class,
96
+    .inputs      = avfilter_vf_realtime_inputs,
97
+    .outputs     = avfilter_vf_realtime_outputs,
98
+};
99
+#endif /* CONFIG_REALTIME_FILTER */
100
+
101
+#if CONFIG_AREALTIME_FILTER
102
+
103
+#define arealtime_options options
104
+AVFILTER_DEFINE_CLASS(arealtime);
105
+
106
+static const AVFilterPad arealtime_inputs[] = {
107
+    {
108
+        .name         = "default",
109
+        .type         = AVMEDIA_TYPE_AUDIO,
110
+        .filter_frame = filter_frame,
111
+    },
112
+    { NULL }
113
+};
114
+
115
+static const AVFilterPad arealtime_outputs[] = {
116
+    {
117
+        .name = "default",
118
+        .type = AVMEDIA_TYPE_AUDIO,
119
+    },
120
+    { NULL }
121
+};
122
+
123
+AVFilter ff_af_arealtime = {
124
+    .name        = "arealtime",
125
+    .description = NULL_IF_CONFIG_SMALL("Slow down filtering to match realtime"),
126
+    .priv_size   = sizeof(RealtimeContext),
127
+    .priv_class  = &arealtime_class,
128
+    .inputs      = arealtime_inputs,
129
+    .outputs     = arealtime_outputs,
130
+};
131
+#endif /* CONFIG_AREALTIME_FILTER */