FFmpeg
Loading...
Searching...
No Matches
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 "config_components.h"
22
23#include "libavutil/opt.h"
24#include "libavutil/time.h"
25#include "audio.h"
26#include "avfilter.h"
27#include "filters.h"
28#include "video.h"
29#include <float.h>
30
31typedef struct RealtimeContext {
32 const AVClass *class;
35 double speed;
36 unsigned inited;
38
40{
41 AVFilterContext *ctx = inlink->dst;
42 RealtimeContext *s = ctx->priv;
43
44 if (frame->pts != AV_NOPTS_VALUE) {
45 int64_t pts = av_rescale_q(frame->pts, inlink->time_base, AV_TIME_BASE_Q) / s->speed;
47 int64_t sleep = pts - now + s->delta;
48 if (!s->inited) {
49 s->inited = 1;
50 sleep = 0;
51 s->delta = now - pts;
52 }
53 if (FFABS(sleep) > s->limit / s->speed) {
55 "time discontinuity detected: %"PRIi64" us, resetting\n",
56 sleep);
57 sleep = 0;
58 s->delta = now - pts;
59 }
60 if (sleep > 0) {
61 av_log(ctx, AV_LOG_DEBUG, "sleeping %"PRIi64" us\n", sleep);
62 for (; sleep > 600000000; sleep -= 600000000)
63 av_usleep(600000000);
64 av_usleep(sleep);
65 }
66 }
67 return ff_filter_frame(inlink->dst->outputs[0], frame);
68}
69
70#define OFFSET(x) offsetof(RealtimeContext, x)
71#define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_RUNTIME_PARAM
72static const AVOption options[] = {
73 { "limit", "sleep time limit", OFFSET(limit), AV_OPT_TYPE_DURATION, { .i64 = 2000000 }, 0, INT64_MAX, FLAGS },
74 { "speed", "speed factor", OFFSET(speed), AV_OPT_TYPE_DOUBLE, { .dbl = 1.0 }, DBL_MIN, DBL_MAX, FLAGS },
75 { NULL }
76};
77
78AVFILTER_DEFINE_CLASS_EXT(realtime, "(a)realtime", options);
79
80#if CONFIG_REALTIME_FILTER
81
82static const AVFilterPad avfilter_vf_realtime_inputs[] = {
83 {
84 .name = "default",
85 .type = AVMEDIA_TYPE_VIDEO,
86 .filter_frame = filter_frame,
87 },
88};
89
91 .p.name = "realtime",
92 .p.description = NULL_IF_CONFIG_SMALL("Slow down filtering to match realtime."),
93 .p.priv_class = &realtime_class,
95 .priv_size = sizeof(RealtimeContext),
96 FILTER_INPUTS(avfilter_vf_realtime_inputs),
98 .process_command = ff_filter_process_command,
99};
100#endif /* CONFIG_REALTIME_FILTER */
101
102#if CONFIG_AREALTIME_FILTER
103
104static const AVFilterPad arealtime_inputs[] = {
105 {
106 .name = "default",
107 .type = AVMEDIA_TYPE_AUDIO,
108 .filter_frame = filter_frame,
109 },
110};
111
113 .p.name = "arealtime",
114 .p.description = NULL_IF_CONFIG_SMALL("Slow down filtering to match realtime."),
115 .p.priv_class = &realtime_class,
117 .priv_size = sizeof(RealtimeContext),
118 FILTER_INPUTS(arealtime_inputs),
120 .process_command = ff_filter_process_command,
121};
122#endif /* CONFIG_AREALTIME_FILTER */
const FFFilter ff_af_arealtime
const FFFilter ff_vf_realtime
const AVFilterPad ff_audio_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_AUDIO.
Definition audio.c:34
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
int ff_filter_process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Generic processing of user supplied commands that are set in the same way as the filter options.
Definition avfilter.c:906
Main libavfilter public API header.
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:598
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition common.h:74
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVFrame * frame
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition f_realtime.c:39
#define OFFSET(x)
Definition f_realtime.c:70
@ AV_OPT_TYPE_DURATION
Underlying C type is int64_t.
Definition opt.h:318
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
Definition opt.h:266
#define AVFILTER_FLAG_METADATA_ONLY
The filter is a "metadata" filter - it does not modify the frame data in any way.
Definition avfilter.h:182
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition avutil.h:263
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
#define AVFILTER_DEFINE_CLASS_EXT(name, desc, options)
Definition filters.h:470
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
AVOptions.
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
AVFilterLink ** outputs
array of pointers to output links
Definition avfilter.h:285
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
AVOption.
Definition opt.h:428
unsigned inited
Definition f_realtime.c:36
#define av_log(a,...)
static AVFormatContext * ctx
Definition movenc.c:49
int av_usleep(unsigned usec)
Sleep for a period of time.
Definition time.c:93
int64_t av_gettime_relative(void)
Get the current time in microseconds since some unspecified starting point.
Definition time.c:57
static int64_t pts
static double limit(double x)
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
Definition video.c:37