Libav
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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
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 
35 #include "audio.h"
36 #include "avfilter.h"
37 #include "internal.h"
38 #include "video.h"
39 
40 #include "config.h"
41 
42 static const char *const var_names[] = {
43  "E",
44  "FRAME_RATE",
45  "INTERLACED",
46  "N",
47  "PHI",
48  "PI",
49  "PREV_INPTS",
50  "PREV_OUTPTS",
51  "PTS",
52  "STARTPTS",
53  "TB",
54  "RTCTIME",
55  "RTCSTART",
56  "S", // Number of samples in the current frame
57  "SR", // Audio sample rate
58  NULL
59 };
60 
61 enum var_name {
78 };
79 
80 typedef struct SetPTSContext {
81  const AVClass *class;
82  char *expr_str;
86 
88 {
89  SetPTSContext *setpts = ctx->priv;
90  int ret;
91 
92  if ((ret = av_expr_parse(&setpts->expr, setpts->expr_str,
93  var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
94  av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", setpts->expr_str);
95  return ret;
96  }
97 
98  setpts->var_values[VAR_E] = M_E;
99  setpts->var_values[VAR_N] = 0.0;
100  setpts->var_values[VAR_S] = 0.0;
101  setpts->var_values[VAR_PHI] = M_PHI;
102  setpts->var_values[VAR_PI] = M_PI;
103  setpts->var_values[VAR_PREV_INPTS] = NAN;
104  setpts->var_values[VAR_PREV_OUTPTS] = NAN;
105  setpts->var_values[VAR_STARTPTS] = NAN;
106  return 0;
107 }
108 
109 static int config_input(AVFilterLink *inlink)
110 {
111  SetPTSContext *setpts = inlink->dst->priv;
112 
113  setpts->var_values[VAR_TB] = av_q2d(inlink->time_base);
114  setpts->var_values[VAR_RTCSTART] = av_gettime();
115 
116  if (inlink->type == AVMEDIA_TYPE_AUDIO) {
117  setpts->var_values[VAR_SR] = inlink->sample_rate;
118  }
119 
120  setpts->var_values[VAR_FRAME_RATE] = inlink->frame_rate.num &&
121  inlink->frame_rate.den ?
122  av_q2d(inlink->frame_rate) : NAN;
123 
124  // Indicate the output can be variable framerate.
125  inlink->frame_rate = (AVRational){1, 0};
126 
127  av_log(inlink->src, AV_LOG_VERBOSE, "TB:%f\n", setpts->var_values[VAR_TB]);
128  return 0;
129 }
130 
131 #define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
132 #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
133 
134 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
135 {
136  SetPTSContext *setpts = inlink->dst->priv;
137  int64_t in_pts = frame->pts;
138  double d;
139 
140  if (isnan(setpts->var_values[VAR_STARTPTS]))
141  setpts->var_values[VAR_STARTPTS] = TS2D(frame->pts);
142 
143  setpts->var_values[VAR_PTS ] = TS2D(frame->pts);
144  setpts->var_values[VAR_RTCTIME ] = av_gettime();
145 
146  if (inlink->type == AVMEDIA_TYPE_VIDEO) {
147  setpts->var_values[VAR_INTERLACED] = frame->interlaced_frame;
148  } else {
149  setpts->var_values[VAR_S] = frame->nb_samples;
150  }
151 
152  d = av_expr_eval(setpts->expr, setpts->var_values, NULL);
153  frame->pts = D2TS(d);
154 
155  av_log(inlink->dst, AV_LOG_TRACE,
156  "n:%"PRId64" interlaced:%d pts:%"PRId64" t:%f -> pts:%"PRId64" t:%f\n",
157  (int64_t)setpts->var_values[VAR_N],
158  (int)setpts->var_values[VAR_INTERLACED],
159  in_pts, in_pts * av_q2d(inlink->time_base),
160  frame->pts, frame->pts * av_q2d(inlink->time_base));
161 
162  if (inlink->type == AVMEDIA_TYPE_VIDEO) {
163  setpts->var_values[VAR_N] += 1.0;
164  } else {
165  setpts->var_values[VAR_N] += frame->nb_samples;
166  }
167 
168  setpts->var_values[VAR_PREV_INPTS ] = TS2D(in_pts);
169  setpts->var_values[VAR_PREV_OUTPTS] = TS2D(frame->pts);
170  return ff_filter_frame(inlink->dst->outputs[0], frame);
171 }
172 
174 {
175  SetPTSContext *setpts = ctx->priv;
176  av_expr_free(setpts->expr);
177  setpts->expr = NULL;
178 }
179 
180 #define OFFSET(x) offsetof(SetPTSContext, x)
181 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_AUDIO_PARAM
182 static const AVOption options[] = {
183  { "expr", "Expression determining the frame timestamp", OFFSET(expr_str), AV_OPT_TYPE_STRING, { .str = "PTS" }, .flags = FLAGS },
184  { NULL },
185 };
186 
187 #if CONFIG_SETPTS_FILTER
188 static const AVClass setpts_class = {
189  .class_name = "setpts",
190  .item_name = av_default_item_name,
191  .option = options,
192  .version = LIBAVUTIL_VERSION_INT,
193 };
194 
195 static const AVFilterPad avfilter_vf_setpts_inputs[] = {
196  {
197  .name = "default",
198  .type = AVMEDIA_TYPE_VIDEO,
199  .get_video_buffer = ff_null_get_video_buffer,
200  .config_props = config_input,
201  .filter_frame = filter_frame,
202  },
203  { NULL }
204 };
205 
206 static const AVFilterPad avfilter_vf_setpts_outputs[] = {
207  {
208  .name = "default",
209  .type = AVMEDIA_TYPE_VIDEO,
210  },
211  { NULL }
212 };
213 
214 AVFilter ff_vf_setpts = {
215  .name = "setpts",
216  .description = NULL_IF_CONFIG_SMALL("Set PTS for the output video frame."),
217  .init = init,
218  .uninit = uninit,
219 
220  .priv_size = sizeof(SetPTSContext),
221  .priv_class = &setpts_class,
222 
223  .inputs = avfilter_vf_setpts_inputs,
224  .outputs = avfilter_vf_setpts_outputs,
225 };
226 #endif
227 
228 #if CONFIG_ASETPTS_FILTER
229 static const AVClass asetpts_class = {
230  .class_name = "asetpts",
231  .item_name = av_default_item_name,
232  .option = options,
233  .version = LIBAVUTIL_VERSION_INT,
234 };
235 
236 static const AVFilterPad asetpts_inputs[] = {
237  {
238  .name = "default",
239  .type = AVMEDIA_TYPE_AUDIO,
240  .get_audio_buffer = ff_null_get_audio_buffer,
241  .config_props = config_input,
242  .filter_frame = filter_frame,
243  },
244  { NULL }
245 };
246 
247 static const AVFilterPad asetpts_outputs[] = {
248  {
249  .name = "default",
250  .type = AVMEDIA_TYPE_AUDIO,
251  },
252  { NULL }
253 };
254 
255 AVFilter ff_af_asetpts = {
256  .name = "asetpts",
257  .description = NULL_IF_CONFIG_SMALL("Set PTS for the output audio frame."),
258  .init = init,
259  .uninit = uninit,
260 
261  .priv_size = sizeof(SetPTSContext),
262  .priv_class = &asetpts_class,
263 
264  .inputs = asetpts_inputs,
265  .outputs = asetpts_outputs,
266 };
267 #endif
static const char *const var_names[]
Definition: setpts.c:42
Definition: setpts.c:62
This structure describes decoded (raw) audio or video data.
Definition: frame.h:140
AVOption.
Definition: opt.h:234
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:259
Main libavfilter public API header.
static int config_input(AVFilterLink *inlink)
Definition: setpts.c:109
int num
numerator
Definition: rational.h:44
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
AVFrame * ff_null_get_video_buffer(AVFilterLink *link, int w, int h)
Definition: video.c:30
double var_values[VAR_VARS_NB]
Definition: setpts.c:84
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:492
const char * name
Pad name.
Definition: internal.h:41
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:39
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:747
#define av_cold
Definition: attributes.h:66
AVOptions.
static av_always_inline av_const int isnan(float x)
Definition: libm.h:85
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:150
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:211
Definition: eval.c:128
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:69
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:140
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:263
Definition: setpts.c:65
A filter pad used for either input or output.
Definition: internal.h:35
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
Definition: setpts.c:72
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
void * priv
private data for use by the filter
Definition: avfilter.h:277
common internal API header
static const AVOption options[]
Definition: setpts.c:182
Definition: setpts.c:75
#define M_E
Definition: ratecontrol.c:42
char * expr_str
Definition: setpts.c:82
AVFormatContext * ctx
Definition: movenc.c:48
#define D2TS(d)
Definition: setpts.c:131
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:26
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
static av_cold void uninit(AVFilterContext *ctx)
Definition: setpts.c:173
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:40
NULL
Definition: eval.c:55
void av_expr_free(AVExpr *e)
Free a parsed expression previously created with av_expr_parse().
Definition: eval.c:195
av_default_item_name
Definition: dnxhdenc.c:55
Describe the class of an AVClass context structure.
Definition: log.h:34
Filter definition.
Definition: avfilter.h:120
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:248
rational number numerator/denominator
Definition: rational.h:43
#define TS2D(ts)
Definition: setpts.c:132
#define M_PHI
Definition: mathematics.h:34
const char * name
Filter name.
Definition: avfilter.h:124
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:274
Definition: setpts.c:67
Definition: setpts.c:76
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: setpts.c:134
int den
denominator
Definition: rational.h:45
#define OFFSET(x)
Definition: setpts.c:180
#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:542
An instance of a filter.
Definition: avfilter.h:262
AVExpr * expr
Definition: setpts.c:83
#define FLAGS
Definition: setpts.c:181
Definition: setpts.c:66
internal API functions
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:184
static av_cold int init(AVFilterContext *ctx)
Definition: setpts.c:87
Definition: setpts.c:70
var_name
Definition: setpts.c:61
simple arithmetic expression evaluator