1 /*
2 * Copyright (c) 2015 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 "config_components.h"
22
23 #include "libavutil/opt.h"
24 #include "libavutil/time.h"
25 #include "avfilter.h"
26 #include "internal.h"
27 #include <float.h>
28
29 typedef struct RealtimeContext {
30 const AVClass *class;
31 int64_t delta;
32 int64_t limit;
33 double speed;
34 unsigned inited;
35 } RealtimeContext;
36
filter_frame(AVFilterLink * inlink,AVFrame * frame)37 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
38 {
39 AVFilterContext *ctx = inlink->dst;
40 RealtimeContext *s = ctx->priv;
41
42 if (frame->pts != AV_NOPTS_VALUE) {
43 int64_t pts = av_rescale_q(frame->pts, inlink->time_base, AV_TIME_BASE_Q) / s->speed;
44 int64_t now = av_gettime_relative();
45 int64_t sleep = pts - now + s->delta;
46 if (!s->inited) {
47 s->inited = 1;
48 sleep = 0;
49 s->delta = now - pts;
50 }
51 if (FFABS(sleep) > s->limit / s->speed) {
52 av_log(ctx, AV_LOG_WARNING,
53 "time discontinuity detected: %"PRIi64" us, resetting\n",
54 sleep);
55 sleep = 0;
56 s->delta = now - pts;
57 }
58 if (sleep > 0) {
59 av_log(ctx, AV_LOG_DEBUG, "sleeping %"PRIi64" us\n", sleep);
60 for (; sleep > 600000000; sleep -= 600000000)
61 av_usleep(600000000);
62 av_usleep(sleep);
63 }
64 }
65 return ff_filter_frame(inlink->dst->outputs[0], frame);
66 }
67
68 #define OFFSET(x) offsetof(RealtimeContext, x)
69 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_RUNTIME_PARAM
70 static const AVOption options[] = {
71 { "limit", "sleep time limit", OFFSET(limit), AV_OPT_TYPE_DURATION, { .i64 = 2000000 }, 0, INT64_MAX, FLAGS },
72 { "speed", "speed factor", OFFSET(speed), AV_OPT_TYPE_DOUBLE, { .dbl = 1.0 }, DBL_MIN, DBL_MAX, FLAGS },
73 { NULL }
74 };
75
76 AVFILTER_DEFINE_CLASS_EXT(realtime, "(a)realtime", options);
77
78 #if CONFIG_REALTIME_FILTER
79
80 static const AVFilterPad avfilter_vf_realtime_inputs[] = {
81 {
82 .name = "default",
83 .type = AVMEDIA_TYPE_VIDEO,
84 .filter_frame = filter_frame,
85 },
86 };
87
88 static const AVFilterPad avfilter_vf_realtime_outputs[] = {
89 {
90 .name = "default",
91 .type = AVMEDIA_TYPE_VIDEO,
92 },
93 };
94
95 const AVFilter ff_vf_realtime = {
96 .name = "realtime",
97 .description = NULL_IF_CONFIG_SMALL("Slow down filtering to match realtime."),
98 .priv_size = sizeof(RealtimeContext),
99 .priv_class = &realtime_class,
100 .flags = AVFILTER_FLAG_METADATA_ONLY,
101 FILTER_INPUTS(avfilter_vf_realtime_inputs),
102 FILTER_OUTPUTS(avfilter_vf_realtime_outputs),
103 .process_command = ff_filter_process_command,
104 };
105 #endif /* CONFIG_REALTIME_FILTER */
106
107 #if CONFIG_AREALTIME_FILTER
108
109 static const AVFilterPad arealtime_inputs[] = {
110 {
111 .name = "default",
112 .type = AVMEDIA_TYPE_AUDIO,
113 .filter_frame = filter_frame,
114 },
115 };
116
117 static const AVFilterPad arealtime_outputs[] = {
118 {
119 .name = "default",
120 .type = AVMEDIA_TYPE_AUDIO,
121 },
122 };
123
124 const AVFilter ff_af_arealtime = {
125 .name = "arealtime",
126 .description = NULL_IF_CONFIG_SMALL("Slow down filtering to match realtime."),
127 .priv_class = &realtime_class,
128 .priv_size = sizeof(RealtimeContext),
129 .flags = AVFILTER_FLAG_METADATA_ONLY,
130 FILTER_INPUTS(arealtime_inputs),
131 FILTER_OUTPUTS(arealtime_outputs),
132 .process_command = ff_filter_process_command,
133 };
134 #endif /* CONFIG_AREALTIME_FILTER */
135