FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
setpts.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010 Stefano Sabatini
3  * Copyright (c) 2008 Victor Paesa
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * video presentation timestamp (PTS) modification filter
25  */
26 
27 #include <inttypes.h>
28 
29 #include "libavutil/eval.h"
30 #include "libavutil/internal.h"
31 #include "libavutil/mathematics.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/time.h"
34 #include "audio.h"
35 #include "avfilter.h"
36 #include "internal.h"
37 #include "video.h"
38 
39 static const char *const var_names[] = {
40  "FRAME_RATE", ///< defined only for constant frame-rate video
41  "INTERLACED", ///< tell if the current frame is interlaced
42  "N", ///< frame / sample number (starting at zero)
43  "NB_CONSUMED_SAMPLES", ///< number of samples consumed by the filter (only audio)
44  "NB_SAMPLES", ///< number of samples in the current frame (only audio)
45  "POS", ///< original position in the file of the frame
46  "PREV_INPTS", ///< previous input PTS
47  "PREV_INT", ///< previous input time in seconds
48  "PREV_OUTPTS", ///< previous output PTS
49  "PREV_OUTT", ///< previous output time in seconds
50  "PTS", ///< original pts in the file of the frame
51  "SAMPLE_RATE", ///< sample rate (only audio)
52  "STARTPTS", ///< PTS at start of movie
53  "STARTT", ///< time at start of movie
54  "T", ///< original time in the file of the frame
55  "TB", ///< timebase
56  "RTCTIME", ///< wallclock (RTC) time in micro seconds
57  "RTCSTART", ///< wallclock (RTC) time at the start of the movie in micro seconds
58  "S", // Number of samples in the current frame
59  "SR", // Audio sample rate
60  NULL
61 };
62 
63 enum var_name {
85 };
86 
87 typedef struct SetPTSContext {
88  const AVClass *class;
89  char *expr_str;
94 
96 {
97  SetPTSContext *setpts = ctx->priv;
98  int ret;
99 
100  if ((ret = av_expr_parse(&setpts->expr, setpts->expr_str,
101  var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
102  av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", setpts->expr_str);
103  return ret;
104  }
105 
106  setpts->var_values[VAR_N] = 0.0;
107  setpts->var_values[VAR_S] = 0.0;
108  setpts->var_values[VAR_PREV_INPTS] = NAN;
109  setpts->var_values[VAR_PREV_INT] = NAN;
110  setpts->var_values[VAR_PREV_OUTPTS] = NAN;
111  setpts->var_values[VAR_PREV_OUTT] = NAN;
112  setpts->var_values[VAR_STARTPTS] = NAN;
113  setpts->var_values[VAR_STARTT] = NAN;
114  return 0;
115 }
116 
117 static int config_input(AVFilterLink *inlink)
118 {
119  AVFilterContext *ctx = inlink->dst;
120  SetPTSContext *setpts = ctx->priv;
121 
122  setpts->type = inlink->type;
123  setpts->var_values[VAR_TB] = av_q2d(inlink->time_base);
124  setpts->var_values[VAR_RTCSTART] = av_gettime();
125 
126  setpts->var_values[VAR_SR] =
127  setpts->var_values[VAR_SAMPLE_RATE] =
128  setpts->type == AVMEDIA_TYPE_AUDIO ? inlink->sample_rate : NAN;
129 
130  setpts->var_values[VAR_FRAME_RATE] = inlink->frame_rate.num &&
131  inlink->frame_rate.den ?
132  av_q2d(inlink->frame_rate) : NAN;
133 
134  av_log(inlink->src, AV_LOG_VERBOSE, "TB:%f FRAME_RATE:%f SAMPLE_RATE:%f\n",
135  setpts->var_values[VAR_TB],
136  setpts->var_values[VAR_FRAME_RATE],
137  setpts->var_values[VAR_SAMPLE_RATE]);
138  return 0;
139 }
140 
141 #define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
142 #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
143 #define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts)*av_q2d(tb))
144 
145 #define BUF_SIZE 64
146 
147 static inline char *double2int64str(char *buf, double v)
148 {
149  if (isnan(v)) snprintf(buf, BUF_SIZE, "nan");
150  else snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)v);
151  return buf;
152 }
153 
154 #define d2istr(v) double2int64str((char[BUF_SIZE]){0}, v)
155 
156 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
157 {
158  SetPTSContext *setpts = inlink->dst->priv;
159  int64_t in_pts = frame->pts;
160  double d;
161 
162  if (isnan(setpts->var_values[VAR_STARTPTS])) {
163  setpts->var_values[VAR_STARTPTS] = TS2D(frame->pts);
164  setpts->var_values[VAR_STARTT ] = TS2T(frame->pts, inlink->time_base);
165  }
166  setpts->var_values[VAR_PTS ] = TS2D(frame->pts);
167  setpts->var_values[VAR_T ] = TS2T(frame->pts, inlink->time_base);
168  setpts->var_values[VAR_POS ] = av_frame_get_pkt_pos(frame) == -1 ? NAN : av_frame_get_pkt_pos(frame);
169  setpts->var_values[VAR_RTCTIME ] = av_gettime();
170 
171  if (inlink->type == AVMEDIA_TYPE_VIDEO) {
172  setpts->var_values[VAR_INTERLACED] = frame->interlaced_frame;
173  } else if (inlink->type == AVMEDIA_TYPE_AUDIO) {
174  setpts->var_values[VAR_S] = frame->nb_samples;
175  setpts->var_values[VAR_NB_SAMPLES] = frame->nb_samples;
176  }
177 
178  d = av_expr_eval(setpts->expr, setpts->var_values, NULL);
179  frame->pts = D2TS(d);
180 
181  av_log(inlink->dst, AV_LOG_TRACE,
182  "N:%"PRId64" PTS:%s T:%f POS:%s",
183  (int64_t)setpts->var_values[VAR_N],
184  d2istr(setpts->var_values[VAR_PTS]),
185  setpts->var_values[VAR_T],
186  d2istr(setpts->var_values[VAR_POS]));
187  switch (inlink->type) {
188  case AVMEDIA_TYPE_VIDEO:
189  av_log(inlink->dst, AV_LOG_TRACE, " INTERLACED:%"PRId64,
190  (int64_t)setpts->var_values[VAR_INTERLACED]);
191  break;
192  case AVMEDIA_TYPE_AUDIO:
193  av_log(inlink->dst, AV_LOG_TRACE, " NB_SAMPLES:%"PRId64" NB_CONSUMED_SAMPLES:%"PRId64,
194  (int64_t)setpts->var_values[VAR_NB_SAMPLES],
195  (int64_t)setpts->var_values[VAR_NB_CONSUMED_SAMPLES]);
196  break;
197  }
198  av_log(inlink->dst, AV_LOG_TRACE, " -> PTS:%s T:%f\n", d2istr(d), TS2T(d, inlink->time_base));
199 
200  if (inlink->type == AVMEDIA_TYPE_VIDEO) {
201  setpts->var_values[VAR_N] += 1.0;
202  } else {
203  setpts->var_values[VAR_N] += frame->nb_samples;
204  }
205 
206  setpts->var_values[VAR_PREV_INPTS ] = TS2D(in_pts);
207  setpts->var_values[VAR_PREV_INT ] = TS2T(in_pts, inlink->time_base);
208  setpts->var_values[VAR_PREV_OUTPTS] = TS2D(frame->pts);
209  setpts->var_values[VAR_PREV_OUTT] = TS2T(frame->pts, inlink->time_base);
210  if (setpts->type == AVMEDIA_TYPE_AUDIO) {
211  setpts->var_values[VAR_NB_CONSUMED_SAMPLES] += frame->nb_samples;
212  }
213  return ff_filter_frame(inlink->dst->outputs[0], frame);
214 }
215 
217 {
218  SetPTSContext *setpts = ctx->priv;
219  av_expr_free(setpts->expr);
220  setpts->expr = NULL;
221 }
222 
223 #define OFFSET(x) offsetof(SetPTSContext, x)
224 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
225 static const AVOption options[] = {
226  { "expr", "Expression determining the frame timestamp", OFFSET(expr_str), AV_OPT_TYPE_STRING, { .str = "PTS" }, .flags = FLAGS },
227  { NULL }
228 };
229 
230 #if CONFIG_SETPTS_FILTER
231 #define setpts_options options
232 AVFILTER_DEFINE_CLASS(setpts);
233 
234 static const AVFilterPad avfilter_vf_setpts_inputs[] = {
235  {
236  .name = "default",
237  .type = AVMEDIA_TYPE_VIDEO,
238  .config_props = config_input,
239  .filter_frame = filter_frame,
240  },
241  { NULL }
242 };
243 
244 static const AVFilterPad avfilter_vf_setpts_outputs[] = {
245  {
246  .name = "default",
247  .type = AVMEDIA_TYPE_VIDEO,
248  },
249  { NULL }
250 };
251 
252 AVFilter ff_vf_setpts = {
253  .name = "setpts",
254  .description = NULL_IF_CONFIG_SMALL("Set PTS for the output video frame."),
255  .init = init,
256  .uninit = uninit,
257 
258  .priv_size = sizeof(SetPTSContext),
259  .priv_class = &setpts_class,
260 
261  .inputs = avfilter_vf_setpts_inputs,
262  .outputs = avfilter_vf_setpts_outputs,
263 };
264 #endif /* CONFIG_SETPTS_FILTER */
265 
266 #if CONFIG_ASETPTS_FILTER
267 
268 #define asetpts_options options
269 AVFILTER_DEFINE_CLASS(asetpts);
270 
271 static const AVFilterPad asetpts_inputs[] = {
272  {
273  .name = "default",
274  .type = AVMEDIA_TYPE_AUDIO,
275  .config_props = config_input,
276  .filter_frame = filter_frame,
277  },
278  { NULL }
279 };
280 
281 static const AVFilterPad asetpts_outputs[] = {
282  {
283  .name = "default",
284  .type = AVMEDIA_TYPE_AUDIO,
285  },
286  { NULL }
287 };
288 
289 AVFilter ff_af_asetpts = {
290  .name = "asetpts",
291  .description = NULL_IF_CONFIG_SMALL("Set PTS for the output audio frame."),
292  .init = init,
293  .uninit = uninit,
294  .priv_size = sizeof(SetPTSContext),
295  .priv_class = &asetpts_class,
296  .inputs = asetpts_inputs,
297  .outputs = asetpts_outputs,
298 };
299 #endif /* CONFIG_ASETPTS_FILTER */
static const char *const var_names[]
Definition: setpts.c:39
#define NULL
Definition: coverity.c:32
Definition: setpts.c:78
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
AVOption.
Definition: opt.h:245
Main libavfilter public API header.
static int config_input(AVFilterLink *inlink)
Definition: setpts.c:117
int num
Numerator.
Definition: rational.h:59
enum AVMediaType type
Definition: setpts.c:92
double var_values[VAR_VARS_NB]
Definition: setpts.c:91
int av_expr_parse(AVExpr **expr, const char *s, const char *const *const_names, const char *const *func1_names, double(*const *funcs1)(void *, double), const char *const *func2_names, double(*const *funcs2)(void *, double, double), int log_offset, void *log_ctx)
Parse an expression.
Definition: eval.c:658
#define d2istr(v)
Definition: setpts.c:154
const char * name
Pad name.
Definition: internal.h:59
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1189
#define av_cold
Definition: attributes.h:82
AVOptions.
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:202
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:268
Definition: eval.c:149
static AVFrame * frame
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
Definition: setpts.c:69
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:322
Definition: setpts.c:66
#define av_log(a,...)
A filter pad used for either input or output.
Definition: internal.h:53
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
Definition: setpts.c:79
#define BUF_SIZE
Definition: setpts.c:145
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
void * priv
private data for use by the filter
Definition: avfilter.h:322
var_name
Definition: aeval.c:46
common internal API header
static const AVOption options[]
Definition: setpts.c:225
#define TS2T(ts, tb)
Definition: setpts.c:143
Definition: setpts.c:82
char * expr_str
Definition: setpts.c:89
AVFormatContext * ctx
Definition: movenc.c:48
static char * double2int64str(char *buf, double v)
Definition: setpts.c:147
static const AVFilterPad outputs[]
Definition: af_afftfilt.c:386
#define D2TS(d)
Definition: setpts.c:141
static av_cold void uninit(AVFilterContext *ctx)
Definition: setpts.c:216
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:39
static const AVFilterPad inputs[]
Definition: af_afftfilt.c:376
void av_expr_free(AVExpr *e)
Free a parsed expression previously created with av_expr_parse().
Definition: eval.c:318
void * buf
Definition: avisynth_c.h:690
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
#define isnan(x)
Definition: libm.h:340
#define TS2D(ts)
Definition: setpts.c:142
AVMediaType
Definition: avutil.h:193
const char * name
Filter name.
Definition: avfilter.h:148
#define snprintf
Definition: snprintf.h:34
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:319
Definition: setpts.c:83
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: setpts.c:156
int den
Denominator.
Definition: rational.h:60
#define OFFSET(x)
Definition: setpts.c:223
#define NAN
Definition: math.h:28
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
Evaluate a previously parsed expression.
Definition: eval.c:713
int64_t av_frame_get_pkt_pos(const AVFrame *frame)
#define AVFILTER_DEFINE_CLASS(fname)
Definition: internal.h:339
An instance of a filter.
Definition: avfilter.h:307
AVExpr * expr
Definition: setpts.c:90
#define FLAGS
Definition: setpts.c:224
internal API functions
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:241
static av_cold int init(AVFilterContext *ctx)
Definition: setpts.c:95
Definition: setpts.c:74
simple arithmetic expression evaluator