f_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 "libavutil/eval.h"
28 #include "libavutil/internal.h"
29 #include "libavutil/mathematics.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/time.h"
32 #include "avfilter.h"
33 #include "internal.h"
34 #include "audio.h"
35 #include "video.h"
36 
37 static const char *const var_names[] = {
38  "FRAME_RATE", ///< defined only for constant frame-rate video
39  "INTERLACED", ///< tell if the current frame is interlaced
40  "N", ///< frame number (starting at zero)
41  "NB_CONSUMED_SAMPLES", ///< number of samples consumed by the filter (only audio)
42  "NB_SAMPLES", ///< number of samples in the current frame (only audio)
43  "POS", ///< original position in the file of the frame
44  "PREV_INPTS", ///< previous input PTS
45  "PREV_INT", ///< previous input time in seconds
46  "PREV_OUTPTS", ///< previous output PTS
47  "PREV_OUTT", ///< previous output time in seconds
48  "PTS", ///< original pts in the file of the frame
49  "SAMPLE_RATE", ///< sample rate (only audio)
50  "STARTPTS", ///< PTS at start of movie
51  "STARTT", ///< time at start of movie
52  "T", ///< original time in the file of the frame
53  "TB", ///< timebase
54  "RTCTIME", ///< wallclock (RTC) time in micro seconds
55  "RTCSTART", ///< wallclock (RTC) time at the start of the movie in micro seconds
56  NULL
57 };
58 
59 enum var_name {
79 };
80 
81 typedef struct {
82  const AVClass *class;
83  char *expr_str;
85  double var_values[VAR_VARS_NB];
88 
89 static av_cold int init(AVFilterContext *ctx)
90 {
91  SetPTSContext *setpts = ctx->priv;
92  int ret;
93 
94  if ((ret = av_expr_parse(&setpts->expr, setpts->expr_str,
95  var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
96  av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", setpts->expr_str);
97  return ret;
98  }
99 
100  setpts->var_values[VAR_N ] = 0.0;
101  setpts->var_values[VAR_PREV_INPTS ] = setpts->var_values[VAR_PREV_INT ] = NAN;
102  setpts->var_values[VAR_PREV_OUTPTS] = setpts->var_values[VAR_PREV_OUTT] = NAN;
103  setpts->var_values[VAR_STARTPTS ] = setpts->var_values[VAR_STARTT ] = NAN;
104  return 0;
105 }
106 
107 static int config_input(AVFilterLink *inlink)
108 {
109  AVFilterContext *ctx = inlink->dst;
110  SetPTSContext *setpts = ctx->priv;
111 
112  setpts->type = inlink->type;
113  setpts->var_values[VAR_TB] = av_q2d(inlink->time_base);
114  setpts->var_values[VAR_RTCSTART] = av_gettime();
115 
116  setpts->var_values[VAR_SAMPLE_RATE] =
117  setpts->type == AVMEDIA_TYPE_AUDIO ? inlink->sample_rate : NAN;
118 
119  setpts->var_values[VAR_FRAME_RATE] = inlink->frame_rate.num && inlink->frame_rate.den ?
120  av_q2d(inlink->frame_rate) : NAN;
121 
122  av_log(inlink->src, AV_LOG_VERBOSE, "TB:%f FRAME_RATE:%f SAMPLE_RATE:%f\n",
123  setpts->var_values[VAR_TB],
124  setpts->var_values[VAR_FRAME_RATE],
125  setpts->var_values[VAR_SAMPLE_RATE]);
126  return 0;
127 }
128 
129 #define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
130 #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
131 #define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts)*av_q2d(tb))
132 
133 #define BUF_SIZE 64
134 
135 static inline char *double2int64str(char *buf, double v)
136 {
137  if (isnan(v)) snprintf(buf, BUF_SIZE, "nan");
138  else snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)v);
139  return buf;
140 }
141 
142 #define d2istr(v) double2int64str((char[BUF_SIZE]){0}, v)
143 
144 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
145 {
146  SetPTSContext *setpts = inlink->dst->priv;
147  int64_t in_pts = frame->pts;
148  double d;
149 
150  if (isnan(setpts->var_values[VAR_STARTPTS])) {
151  setpts->var_values[VAR_STARTPTS] = TS2D(frame->pts);
152  setpts->var_values[VAR_STARTT ] = TS2T(frame->pts, inlink->time_base);
153  }
154  setpts->var_values[VAR_PTS ] = TS2D(frame->pts);
155  setpts->var_values[VAR_T ] = TS2T(frame->pts, inlink->time_base);
156  setpts->var_values[VAR_POS ] = av_frame_get_pkt_pos(frame) == -1 ? NAN : av_frame_get_pkt_pos(frame);
157  setpts->var_values[VAR_RTCTIME ] = av_gettime();
158 
159  switch (inlink->type) {
160  case AVMEDIA_TYPE_VIDEO:
161  setpts->var_values[VAR_INTERLACED] = frame->interlaced_frame;
162  break;
163 
164  case AVMEDIA_TYPE_AUDIO:
165  setpts->var_values[VAR_NB_SAMPLES] = frame->nb_samples;
166  break;
167  }
168 
169  d = av_expr_eval(setpts->expr, setpts->var_values, NULL);
170 
171  av_log(inlink->dst, AV_LOG_DEBUG,
172  "N:%"PRId64" PTS:%s T:%f POS:%s",
173  (int64_t)setpts->var_values[VAR_N],
174  d2istr(setpts->var_values[VAR_PTS]),
175  setpts->var_values[VAR_T],
176  d2istr(setpts->var_values[VAR_POS]));
177  switch (inlink->type) {
178  case AVMEDIA_TYPE_VIDEO:
179  av_log(inlink->dst, AV_LOG_DEBUG, " INTERLACED:%"PRId64,
180  (int64_t)setpts->var_values[VAR_INTERLACED]);
181  break;
182  case AVMEDIA_TYPE_AUDIO:
183  av_log(inlink->dst, AV_LOG_DEBUG, " NB_SAMPLES:%"PRId64" NB_CONSUMED_SAMPLES:%"PRId64,
184  (int64_t)setpts->var_values[VAR_NB_SAMPLES],
185  (int64_t)setpts->var_values[VAR_NB_CONSUMED_SAMPLES]);
186  break;
187  }
188  av_log(inlink->dst, AV_LOG_DEBUG, " -> PTS:%s T:%f\n", d2istr(d), TS2T(d, inlink->time_base));
189 
190  frame->pts = D2TS(d);
191 
192  setpts->var_values[VAR_PREV_INPTS ] = TS2D(in_pts);
193  setpts->var_values[VAR_PREV_INT ] = TS2T(in_pts, inlink->time_base);
194  setpts->var_values[VAR_PREV_OUTPTS] = TS2D(frame->pts);
195  setpts->var_values[VAR_PREV_OUTT] = TS2T(frame->pts, inlink->time_base);
196  setpts->var_values[VAR_N] += 1.0;
197  if (setpts->type == AVMEDIA_TYPE_AUDIO) {
198  setpts->var_values[VAR_NB_CONSUMED_SAMPLES] += frame->nb_samples;
199  }
200  return ff_filter_frame(inlink->dst->outputs[0], frame);
201 }
202 
203 static av_cold void uninit(AVFilterContext *ctx)
204 {
205  SetPTSContext *setpts = ctx->priv;
206  av_expr_free(setpts->expr);
207  setpts->expr = NULL;
208 }
209 
210 #if CONFIG_ASETPTS_FILTER
211 
212 #define OFFSET(x) offsetof(SetPTSContext, x)
213 #define AFLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
214 static const AVOption aoptions[] = {
215  { "expr", "Expression determining the frame timestamp", OFFSET(expr_str), AV_OPT_TYPE_STRING, { .str = "PTS" }, .flags = AFLAGS },
216  { NULL },
217 };
218 
219 static const AVClass asetpts_class = {
220  .class_name = "asetpts",
221  .item_name = av_default_item_name,
222  .option = aoptions,
223  .version = LIBAVUTIL_VERSION_INT,
224 };
225 
226 static const AVFilterPad avfilter_af_asetpts_inputs[] = {
227  {
228  .name = "default",
229  .type = AVMEDIA_TYPE_AUDIO,
230  .get_audio_buffer = ff_null_get_audio_buffer,
231  .config_props = config_input,
232  .filter_frame = filter_frame,
233  },
234  { NULL }
235 };
236 
237 static const AVFilterPad avfilter_af_asetpts_outputs[] = {
238  {
239  .name = "default",
240  .type = AVMEDIA_TYPE_AUDIO,
241  },
242  { NULL }
243 };
244 
245 AVFilter avfilter_af_asetpts = {
246  .name = "asetpts",
247  .description = NULL_IF_CONFIG_SMALL("Set PTS for the output audio frame."),
248  .init = init,
249  .uninit = uninit,
250  .priv_size = sizeof(SetPTSContext),
251  .priv_class= &asetpts_class,
252  .inputs = avfilter_af_asetpts_inputs,
253  .outputs = avfilter_af_asetpts_outputs,
254 };
255 #endif /* CONFIG_ASETPTS_FILTER */
256 
257 #if CONFIG_SETPTS_FILTER
258 
259 #define OFFSET(x) offsetof(SetPTSContext, x)
260 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
261 static const AVOption options[] = {
262  { "expr", "Expression determining the frame timestamp", OFFSET(expr_str), AV_OPT_TYPE_STRING, { .str = "PTS" }, .flags = FLAGS },
263  { NULL },
264 };
265 
266 static const AVClass setpts_class = {
267  .class_name = "setpts",
268  .item_name = av_default_item_name,
269  .option = options,
270  .version = LIBAVUTIL_VERSION_INT,
271 };
272 
273 static const AVFilterPad avfilter_vf_setpts_inputs[] = {
274  {
275  .name = "default",
276  .type = AVMEDIA_TYPE_VIDEO,
277  .get_video_buffer = ff_null_get_video_buffer,
278  .config_props = config_input,
279  .filter_frame = filter_frame,
280  },
281  { NULL }
282 };
283 
284 static const AVFilterPad avfilter_vf_setpts_outputs[] = {
285  {
286  .name = "default",
287  .type = AVMEDIA_TYPE_VIDEO,
288  },
289  { NULL }
290 };
291 
292 AVFilter avfilter_vf_setpts = {
293  .name = "setpts",
294  .description = NULL_IF_CONFIG_SMALL("Set PTS for the output video frame."),
295  .init = init,
296  .uninit = uninit,
297 
298  .priv_size = sizeof(SetPTSContext),
299  .priv_class = &setpts_class,
300 
301  .inputs = avfilter_vf_setpts_inputs,
302  .outputs = avfilter_vf_setpts_outputs,
303 };
304 #endif /* CONFIG_SETPTS_FILTER */
int64_t av_frame_get_pkt_pos(const AVFrame *s)
Definition: frame.c:36
#define TS2T(ts, tb)
Definition: f_setpts.c:131
static const char *const var_names[]
Definition: f_setpts.c:37
float v
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
#define FLAGS
Definition: cmdutils.c:482
AVOption.
Definition: opt.h:251
av_default_item_name
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:117
#define LIBAVUTIL_VERSION_INT
external API header
int num
numerator
Definition: rational.h:44
static av_cold void uninit(AVFilterContext *ctx)
Definition: f_setpts.c:203
enum AVMediaType type
Definition: f_setpts.c:86
AVFrame * ff_null_get_video_buffer(AVFilterLink *link, int w, int h)
Definition: video.c:35
double var_values[VAR_VARS_NB]
Definition: f_setpts.c:85
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:640
set threshold d
const char * name
Pad name.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:55
it can be given away to ff_start_frame *A reference passed to ff_filter_frame(or the deprecated ff_start_frame) is given away and must no longer be used.*A reference created with avfilter_ref_buffer belongs to the code that created it.*A reference obtained with ff_get_video_buffer or ff_get_audio_buffer belongs to the code that requested it.*A reference given as return value by the get_video_buffer or get_audio_buffer method is given away and must no longer be used.Link reference fields---------------------The AVFilterLink structure has a few AVFilterBufferRef fields.The cur_buf and out_buf were used with the deprecated start_frame/draw_slice/end_frame API and should no longer be used.src_buf
#define av_cold
Definition: attributes.h:78
AVOptions.
#define NAN
Definition: math.h:7
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:159
Definition: eval.c:140
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:69
static int config_input(AVFilterLink *inlink)
Definition: f_setpts.c:107
static char * double2int64str(char *buf, double v)
Definition: f_setpts.c:135
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:270
const OptionDef options[]
Definition: ffserver.c:4697
frame
Definition: stft.m:14
A filter pad used for either input or output.
Definition: f_setpts.c:74
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: f_setpts.c:144
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void * priv
private data for use by the filter
Definition: avfilter.h:545
void av_log(void *avcl, int level, const char *fmt,...)
Send the specified message to the log if the level is less than or equal to the current av_log_level...
Definition: log.c:246
#define D2TS(d)
Definition: f_setpts.c:129
#define TS2D(ts)
Definition: f_setpts.c:130
common internal API header
#define AV_LOG_VERBOSE
Definition: log.h:157
var_name
ret
Definition: avfilter.c:821
char * expr_str
Definition: f_setpts.c:83
AVFrame * ff_null_get_audio_buffer(AVFilterLink *link, int nb_samples)
get_audio_buffer() handler for filters which simply pass audio along
Definition: audio.c:36
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:39
static int isnan(float x)
Definition: libm.h:96
NULL
Definition: eval.c:55
void av_expr_free(AVExpr *e)
Free a parsed expression previously created with av_expr_parse().
Definition: eval.c:302
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
#define BUF_SIZE
Definition: f_setpts.c:133
void * buf
Definition: avisynth_c.h:594
Definition: f_setpts.c:62
Describe the class of an AVClass context structure.
Definition: log.h:50
Filter definition.
Definition: avfilter.h:436
AVMediaType
Definition: avutil.h:141
static av_cold int init(AVFilterContext *ctx)
Definition: f_setpts.c:89
const char * name
filter name
Definition: avfilter.h:437
#define snprintf
Definition: snprintf.h:34
#define type
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:539
#define OFFSET(x)
Definition: ffmpeg_opt.c:2553
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:162
int den
denominator
Definition: rational.h:45
#define d2istr(v)
Definition: f_setpts.c:142
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
Evaluate a previously parsed expression.
Definition: eval.c:691
An instance of a filter.
Definition: avfilter.h:524
AVExpr * expr
Definition: f_setpts.c:84
internal API functions
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 inputs
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:127
simple arithmetic expression evaluator