FFmpeg
f_realtime.c
Go to the documentation of this file.
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;
34 
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) {
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 AVFILTER_DEFINE_CLASS_EXT(realtime, "(a)realtime", options);
75 
76 #if CONFIG_REALTIME_FILTER
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 };
85 
86 static const AVFilterPad avfilter_vf_realtime_outputs[] = {
87  {
88  .name = "default",
89  .type = AVMEDIA_TYPE_VIDEO,
90  },
91 };
92 
93 const AVFilter ff_vf_realtime = {
94  .name = "realtime",
95  .description = NULL_IF_CONFIG_SMALL("Slow down filtering to match realtime."),
96  .priv_size = sizeof(RealtimeContext),
97  .priv_class = &realtime_class,
99  FILTER_INPUTS(avfilter_vf_realtime_inputs),
100  FILTER_OUTPUTS(avfilter_vf_realtime_outputs),
101 };
102 #endif /* CONFIG_REALTIME_FILTER */
103 
104 #if CONFIG_AREALTIME_FILTER
105 
106 static const AVFilterPad arealtime_inputs[] = {
107  {
108  .name = "default",
109  .type = AVMEDIA_TYPE_AUDIO,
110  .filter_frame = filter_frame,
111  },
112 };
113 
114 static const AVFilterPad arealtime_outputs[] = {
115  {
116  .name = "default",
117  .type = AVMEDIA_TYPE_AUDIO,
118  },
119 };
120 
121 const AVFilter ff_af_arealtime = {
122  .name = "arealtime",
123  .description = NULL_IF_CONFIG_SMALL("Slow down filtering to match realtime."),
124  .priv_class = &realtime_class,
125  .priv_size = sizeof(RealtimeContext),
127  FILTER_INPUTS(arealtime_inputs),
128  FILTER_OUTPUTS(arealtime_outputs),
129 };
130 #endif /* CONFIG_AREALTIME_FILTER */
av_gettime_relative
int64_t av_gettime_relative(void)
Get the current time in microseconds since some unspecified starting point.
Definition: time.c:56
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
RealtimeContext::speed
double speed
Definition: f_realtime.c:31
opt.h
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1018
RealtimeContext::limit
int64_t limit
Definition: f_realtime.c:30
AV_TIME_BASE_Q
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:260
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:317
AVOption
AVOption.
Definition: opt.h:247
AV_OPT_TYPE_DURATION
@ AV_OPT_TYPE_DURATION
Definition: opt.h:238
float.h
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:169
options
static const AVOption options[]
Definition: f_realtime.c:68
RealtimeContext::delta
int64_t delta
Definition: f_realtime.c:29
pts
static int64_t pts
Definition: transcode_aac.c:653
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:50
s
#define s(width, name)
Definition: cbs_vp9.c:257
AV_OPT_TYPE_DOUBLE
@ AV_OPT_TYPE_DOUBLE
Definition: opt.h:226
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
ctx
AVFormatContext * ctx
Definition: movenc.c:48
av_rescale_q
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:141
RealtimeContext::inited
unsigned inited
Definition: f_realtime.c:32
av_usleep
int av_usleep(unsigned usec)
Sleep for a period of time.
Definition: time.c:84
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:191
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:65
RealtimeContext
Definition: f_realtime.c:27
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
time.h
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: f_realtime.c:35
internal.h
FLAGS
#define FLAGS
Definition: f_realtime.c:67
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:56
OFFSET
#define OFFSET(x)
Definition: f_realtime.c:66
limit
static double limit(double x)
Definition: vf_pseudocolor.c:128
AVFilter
Filter definition.
Definition: avfilter.h:165
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
avfilter.h
AVFILTER_FLAG_METADATA_ONLY
#define AVFILTER_FLAG_METADATA_ONLY
The filter is a "metadata" filter - it does not modify the frame data in any way.
Definition: avfilter.h:137
AVFilterContext
An instance of a filter.
Definition: avfilter.h:402
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:192
ff_af_arealtime
const AVFilter ff_af_arealtime
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVFILTER_DEFINE_CLASS_EXT
AVFILTER_DEFINE_CLASS_EXT(realtime, "(a)realtime", options)
ff_vf_realtime
const AVFilter ff_vf_realtime