FFmpeg  4.0
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 
26 typedef struct RealtimeContext {
27  const AVClass *class;
28  int64_t delta;
29  int64_t limit;
30  unsigned inited;
32 
33 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
34 {
35  AVFilterContext *ctx = inlink->dst;
36  RealtimeContext *s = ctx->priv;
37 
38  if (frame->pts != AV_NOPTS_VALUE) {
39  int64_t pts = av_rescale_q(frame->pts, inlink->time_base, AV_TIME_BASE_Q);
40  int64_t now = av_gettime_relative();
41  int64_t sleep = pts - now + s->delta;
42  if (!s->inited) {
43  s->inited = 1;
44  sleep = 0;
45  s->delta = now - pts;
46  }
47  if (sleep > s->limit || sleep < -s->limit) {
49  "time discontinuity detected: %"PRIi64" us, resetting\n",
50  sleep);
51  sleep = 0;
52  s->delta = now - pts;
53  }
54  if (sleep > 0) {
55  av_log(ctx, AV_LOG_DEBUG, "sleeping %"PRIi64" us\n", sleep);
56  for (; sleep > 600000000; sleep -= 600000000)
57  av_usleep(600000000);
58  av_usleep(sleep);
59  }
60  }
61  return ff_filter_frame(inlink->dst->outputs[0], frame);
62 }
63 
64 #define OFFSET(x) offsetof(RealtimeContext, x)
65 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
66 static const AVOption options[] = {
67  { "limit", "sleep time limit", OFFSET(limit), AV_OPT_TYPE_DURATION, { .i64 = 2000000 }, 0, INT64_MAX, FLAGS },
68  { NULL }
69 };
70 
71 #if CONFIG_REALTIME_FILTER
72 #define realtime_options options
73 AVFILTER_DEFINE_CLASS(realtime);
74 
75 static const AVFilterPad avfilter_vf_realtime_inputs[] = {
76  {
77  .name = "default",
78  .type = AVMEDIA_TYPE_VIDEO,
79  .filter_frame = filter_frame,
80  },
81  { NULL }
82 };
83 
84 static const AVFilterPad avfilter_vf_realtime_outputs[] = {
85  {
86  .name = "default",
87  .type = AVMEDIA_TYPE_VIDEO,
88  },
89  { NULL }
90 };
91 
93  .name = "realtime",
94  .description = NULL_IF_CONFIG_SMALL("Slow down filtering to match realtime."),
95  .priv_size = sizeof(RealtimeContext),
96  .priv_class = &realtime_class,
97  .inputs = avfilter_vf_realtime_inputs,
98  .outputs = avfilter_vf_realtime_outputs,
99 };
100 #endif /* CONFIG_REALTIME_FILTER */
101 
102 #if CONFIG_AREALTIME_FILTER
103 
104 #define arealtime_options options
105 AVFILTER_DEFINE_CLASS(arealtime);
106 
107 static const AVFilterPad arealtime_inputs[] = {
108  {
109  .name = "default",
110  .type = AVMEDIA_TYPE_AUDIO,
111  .filter_frame = filter_frame,
112  },
113  { NULL }
114 };
115 
116 static const AVFilterPad arealtime_outputs[] = {
117  {
118  .name = "default",
119  .type = AVMEDIA_TYPE_AUDIO,
120  },
121  { NULL }
122 };
123 
125  .name = "arealtime",
126  .description = NULL_IF_CONFIG_SMALL("Slow down filtering to match realtime."),
127  .priv_size = sizeof(RealtimeContext),
128  .priv_class = &arealtime_class,
129  .inputs = arealtime_inputs,
130  .outputs = arealtime_outputs,
131 };
132 #endif /* CONFIG_AREALTIME_FILTER */
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
This structure describes decoded (raw) audio or video data.
Definition: frame.h:218
AVOption.
Definition: opt.h:246
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
Main libavfilter public API header.
#define OFFSET(x)
Definition: f_realtime.c:64
int av_usleep(unsigned usec)
Sleep for a period of time.
Definition: time.c:84
const char * name
Pad name.
Definition: internal.h:60
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1080
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: f_realtime.c:33
AVOptions.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:311
static AVFrame * frame
#define av_log(a,...)
A filter pad used for either input or output.
Definition: internal.h:54
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
#define FLAGS
Definition: f_realtime.c:65
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
void * priv
private data for use by the filter
Definition: avfilter.h:353
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
AVFilter ff_vf_realtime
AVFilter ff_af_arealtime
AVFormatContext * ctx
Definition: movenc.c:48
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:260
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
int64_t limit
Definition: f_realtime.c:29
const char * name
Filter name.
Definition: avfilter.h:148
unsigned inited
Definition: f_realtime.c:30
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
static int64_t pts
int64_t av_gettime_relative(void)
Get the current time in microseconds since some unspecified starting point.
Definition: time.c:56
static const AVOption options[]
Definition: f_realtime.c:66
#define AVFILTER_DEFINE_CLASS(fname)
Definition: internal.h:334
An instance of a filter.
Definition: avfilter.h:338
int64_t delta
Definition: f_realtime.c:28
internal API functions
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248