FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "avfilter.h"
31 #include "internal.h"
32 #include "audio.h"
33 #include "video.h"
34 
35 static const char *const var_names[] = {
36  "FRAME_RATE", ///< defined only for constant frame-rate video
37  "INTERLACED", ///< tell if the current frame is interlaced
38  "N", ///< frame number (starting at zero)
39  "NB_CONSUMED_SAMPLES", ///< number of samples consumed by the filter (only audio)
40  "NB_SAMPLES", ///< number of samples in the current frame (only audio)
41  "POS", ///< original position in the file of the frame
42  "PREV_INPTS", ///< previous input PTS
43  "PREV_INT", ///< previous input time in seconds
44  "PREV_OUTPTS", ///< previous output PTS
45  "PREV_OUTT", ///< previous output time in seconds
46  "PTS", ///< original pts in the file of the frame
47  "SAMPLE_RATE", ///< sample rate (only audio)
48  "STARTPTS", ///< PTS at start of movie
49  "STARTT", ///< time at start of movie
50  "T", ///< original time in the file of the frame
51  "TB", ///< timebase
52  NULL
53 };
54 
55 enum var_name {
73 };
74 
75 typedef struct {
77  double var_values[VAR_VARS_NB];
80 
81 static av_cold int init(AVFilterContext *ctx, const char *args)
82 {
83  SetPTSContext *setpts = ctx->priv;
84  int ret;
85 
86  if ((ret = av_expr_parse(&setpts->expr, args ? args : "PTS",
87  var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
88  av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", args);
89  return ret;
90  }
91 
92  setpts->var_values[VAR_N ] = 0.0;
93  setpts->var_values[VAR_PREV_INPTS ] = setpts->var_values[VAR_PREV_INT ] = NAN;
95  setpts->var_values[VAR_STARTPTS ] = setpts->var_values[VAR_STARTT ] = NAN;
96  return 0;
97 }
98 
99 static int config_input(AVFilterLink *inlink)
100 {
101  AVFilterContext *ctx = inlink->dst;
102  SetPTSContext *setpts = ctx->priv;
103 
104  setpts->type = inlink->type;
105  setpts->var_values[VAR_TB] = av_q2d(inlink->time_base);
106 
107  setpts->var_values[VAR_SAMPLE_RATE] =
108  setpts->type == AVMEDIA_TYPE_AUDIO ? inlink->sample_rate : NAN;
109 
110  setpts->var_values[VAR_FRAME_RATE] = inlink->frame_rate.num && inlink->frame_rate.den ?
111  av_q2d(inlink->frame_rate) : NAN;
112 
113  av_log(inlink->src, AV_LOG_VERBOSE, "TB:%f FRAME_RATE:%f SAMPLE_RATE:%f\n",
114  setpts->var_values[VAR_TB],
115  setpts->var_values[VAR_FRAME_RATE],
116  setpts->var_values[VAR_SAMPLE_RATE]);
117  return 0;
118 }
119 
120 #define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
121 #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
122 #define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts)*av_q2d(tb))
123 
124 #define BUF_SIZE 64
125 
126 static inline char *double2int64str(char *buf, double v)
127 {
128  if (isnan(v)) snprintf(buf, BUF_SIZE, "nan");
129  else snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)v);
130  return buf;
131 }
132 
133 #define d2istr(v) double2int64str((char[BUF_SIZE]){0}, v)
134 
136 {
137  SetPTSContext *setpts = inlink->dst->priv;
138  int64_t in_pts = frame->pts;
139  double d;
140 
141  if (isnan(setpts->var_values[VAR_STARTPTS])) {
142  setpts->var_values[VAR_STARTPTS] = TS2D(frame->pts);
143  setpts->var_values[VAR_STARTT ] = TS2T(frame->pts, inlink->time_base);
144  }
145  setpts->var_values[VAR_PTS ] = TS2D(frame->pts);
146  setpts->var_values[VAR_T ] = TS2T(frame->pts, inlink->time_base);
147  setpts->var_values[VAR_POS ] = frame->pos == -1 ? NAN : frame->pos;
148 
149  switch (inlink->type) {
150  case AVMEDIA_TYPE_VIDEO:
151  setpts->var_values[VAR_INTERLACED] = frame->video->interlaced;
152  break;
153 
154  case AVMEDIA_TYPE_AUDIO:
155  setpts->var_values[VAR_NB_SAMPLES] = frame->audio->nb_samples;
156  break;
157  }
158 
159  d = av_expr_eval(setpts->expr, setpts->var_values, NULL);
160 
161  av_log(inlink->dst, AV_LOG_DEBUG,
162  "N:%"PRId64" PTS:%s T:%f POS:%s",
163  (int64_t)setpts->var_values[VAR_N],
164  d2istr(setpts->var_values[VAR_PTS]),
165  setpts->var_values[VAR_T],
166  d2istr(setpts->var_values[VAR_POS]));
167  switch (inlink->type) {
168  case AVMEDIA_TYPE_VIDEO:
169  av_log(inlink->dst, AV_LOG_DEBUG, " INTERLACED:%"PRId64,
170  (int64_t)setpts->var_values[VAR_INTERLACED]);
171  break;
172  case AVMEDIA_TYPE_AUDIO:
173  av_log(inlink->dst, AV_LOG_DEBUG, " NB_SAMPLES:%"PRId64" NB_CONSUMED_SAMPLES:%"PRId64,
174  (int64_t)setpts->var_values[VAR_NB_SAMPLES],
175  (int64_t)setpts->var_values[VAR_NB_CONSUMED_SAMPLES]);
176  break;
177  }
178  av_log(inlink->dst, AV_LOG_DEBUG, " -> PTS:%s T:%f\n", d2istr(d), TS2T(d, inlink->time_base));
179 
180  frame->pts = D2TS(d);
181 
182  setpts->var_values[VAR_PREV_INPTS ] = TS2D(in_pts);
183  setpts->var_values[VAR_PREV_INT ] = TS2T(in_pts, inlink->time_base);
184  setpts->var_values[VAR_PREV_OUTPTS] = TS2D(frame->pts);
185  setpts->var_values[VAR_PREV_OUTT] = TS2T(frame->pts, inlink->time_base);
186  setpts->var_values[VAR_N] += 1.0;
187  if (setpts->type == AVMEDIA_TYPE_AUDIO) {
189  }
190  return ff_filter_frame(inlink->dst->outputs[0], frame);
191 }
192 
193 static av_cold void uninit(AVFilterContext *ctx)
194 {
195  SetPTSContext *setpts = ctx->priv;
196  av_expr_free(setpts->expr);
197  setpts->expr = NULL;
198 }
199 
200 #if CONFIG_ASETPTS_FILTER
201 static const AVFilterPad avfilter_af_asetpts_inputs[] = {
202  {
203  .name = "default",
204  .type = AVMEDIA_TYPE_AUDIO,
205  .get_audio_buffer = ff_null_get_audio_buffer,
206  .config_props = config_input,
207  .filter_frame = filter_frame,
208  },
209  { NULL }
210 };
211 
212 static const AVFilterPad avfilter_af_asetpts_outputs[] = {
213  {
214  .name = "default",
215  .type = AVMEDIA_TYPE_AUDIO,
216  },
217  { NULL }
218 };
219 
220 AVFilter avfilter_af_asetpts = {
221  .name = "asetpts",
222  .description = NULL_IF_CONFIG_SMALL("Set PTS for the output audio frame."),
223  .init = init,
224  .uninit = uninit,
225  .priv_size = sizeof(SetPTSContext),
226  .inputs = avfilter_af_asetpts_inputs,
227  .outputs = avfilter_af_asetpts_outputs,
228 };
229 #endif /* CONFIG_ASETPTS_FILTER */
230 
231 #if CONFIG_SETPTS_FILTER
232 static const AVFilterPad avfilter_vf_setpts_inputs[] = {
233  {
234  .name = "default",
235  .type = AVMEDIA_TYPE_VIDEO,
236  .get_video_buffer = ff_null_get_video_buffer,
237  .config_props = config_input,
238  .filter_frame = filter_frame,
239  },
240  { NULL }
241 };
242 
243 static const AVFilterPad avfilter_vf_setpts_outputs[] = {
244  {
245  .name = "default",
246  .type = AVMEDIA_TYPE_VIDEO,
247  },
248  { NULL }
249 };
250 
251 AVFilter avfilter_vf_setpts = {
252  .name = "setpts",
253  .description = NULL_IF_CONFIG_SMALL("Set PTS for the output video frame."),
254  .init = init,
255  .uninit = uninit,
256 
257  .priv_size = sizeof(SetPTSContext),
258 
259  .inputs = avfilter_vf_setpts_inputs,
260  .outputs = avfilter_vf_setpts_outputs,
261 };
262 #endif /* CONFIG_SETPTS_FILTER */