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