]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_setpts.c
6f8290d93db58c7ccf2a46699ff8f77fca2515dd
[ffmpeg] / libavfilter / vf_setpts.c
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
22 /**
23  * @file
24  * video presentation timestamp (PTS) modification filter
25  */
26
27 /* #define DEBUG */
28
29 #include "libavutil/eval.h"
30 #include "libavutil/mathematics.h"
31 #include "avfilter.h"
32 #include "internal.h"
33 #include "video.h"
34
35 static const char *const var_names[] = {
36     "E",           ///< Euler number
37     "INTERLACED",  ///< tell if the current frame is interlaced
38     "N",           ///< frame number (starting at zero)
39     "PHI",         ///< golden ratio
40     "PI",          ///< greek pi
41     "POS",         ///< original position in the file of the frame
42     "PREV_INPTS",  ///< previous  input PTS
43     "PREV_OUTPTS", ///< previous output PTS
44     "PTS",         ///< original pts in the file of the frame
45     "STARTPTS",   ///< PTS at start of movie
46     "TB",          ///< timebase
47     NULL
48 };
49
50 enum var_name {
51     VAR_E,
52     VAR_INTERLACED,
53     VAR_N,
54     VAR_PHI,
55     VAR_PI,
56     VAR_POS,
57     VAR_PREV_INPTS,
58     VAR_PREV_OUTPTS,
59     VAR_PTS,
60     VAR_STARTPTS,
61     VAR_TB,
62     VAR_VARS_NB
63 };
64
65 typedef struct {
66     AVExpr *expr;
67     double var_values[VAR_VARS_NB];
68 } SetPTSContext;
69
70 static av_cold int init(AVFilterContext *ctx, const char *args)
71 {
72     SetPTSContext *setpts = ctx->priv;
73     int ret;
74
75     if ((ret = av_expr_parse(&setpts->expr, args ? args : "PTS",
76                              var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
77         av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", args);
78         return ret;
79     }
80
81     setpts->var_values[VAR_E          ] = M_E;
82     setpts->var_values[VAR_N          ] = 0.0;
83     setpts->var_values[VAR_PHI        ] = M_PHI;
84     setpts->var_values[VAR_PI         ] = M_PI;
85     setpts->var_values[VAR_PREV_INPTS ] = NAN;
86     setpts->var_values[VAR_PREV_OUTPTS] = NAN;
87     setpts->var_values[VAR_STARTPTS   ] = NAN;
88     return 0;
89 }
90
91 static int config_input(AVFilterLink *inlink)
92 {
93     SetPTSContext *setpts = inlink->dst->priv;
94
95     setpts->var_values[VAR_TB] = av_q2d(inlink->time_base);
96
97     av_log(inlink->src, AV_LOG_INFO, "TB:%f\n", setpts->var_values[VAR_TB]);
98     return 0;
99 }
100
101 #define D2TS(d)  (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
102 #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
103
104 static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
105 {
106     SetPTSContext *setpts = inlink->dst->priv;
107     double d;
108     AVFilterBufferRef *outpicref = avfilter_ref_buffer(inpicref, ~0);
109
110     if (isnan(setpts->var_values[VAR_STARTPTS]))
111         setpts->var_values[VAR_STARTPTS] = TS2D(inpicref->pts);
112
113     setpts->var_values[VAR_INTERLACED] = inpicref->video->interlaced;
114     setpts->var_values[VAR_PTS       ] = TS2D(inpicref->pts);
115     setpts->var_values[VAR_POS       ] = inpicref->pos == -1 ? NAN : inpicref->pos;
116
117     d = av_expr_eval(setpts->expr, setpts->var_values, NULL);
118     outpicref->pts = D2TS(d);
119
120 #ifdef DEBUG
121     av_log(inlink->dst, AV_LOG_DEBUG,
122            "n:%"PRId64" interlaced:%d pos:%"PRId64" pts:%"PRId64" t:%f -> pts:%"PRId64" t:%f\n",
123            (int64_t)setpts->var_values[VAR_N],
124            (int)setpts->var_values[VAR_INTERLACED],
125            inpicref ->pos,
126            inpicref ->pts, inpicref ->pts * av_q2d(inlink->time_base),
127            outpicref->pts, outpicref->pts * av_q2d(inlink->time_base));
128 #endif
129
130     setpts->var_values[VAR_N] += 1.0;
131     setpts->var_values[VAR_PREV_INPTS ] = TS2D(inpicref ->pts);
132     setpts->var_values[VAR_PREV_OUTPTS] = TS2D(outpicref->pts);
133     ff_start_frame(inlink->dst->outputs[0], outpicref);
134 }
135
136 static av_cold void uninit(AVFilterContext *ctx)
137 {
138     SetPTSContext *setpts = ctx->priv;
139     av_expr_free(setpts->expr);
140     setpts->expr = NULL;
141 }
142
143 AVFilter avfilter_vf_setpts = {
144     .name      = "setpts",
145     .description = NULL_IF_CONFIG_SMALL("Set PTS for the output video frame."),
146     .init      = init,
147     .uninit    = uninit,
148
149     .priv_size = sizeof(SetPTSContext),
150
151     .inputs    = (AVFilterPad[]) {{ .name             = "default",
152                                     .type             = AVMEDIA_TYPE_VIDEO,
153                                     .get_video_buffer = ff_null_get_video_buffer,
154                                     .config_props     = config_input,
155                                     .start_frame      = start_frame, },
156                                   { .name = NULL }},
157     .outputs   = (AVFilterPad[]) {{ .name             = "default",
158                                     .type             = AVMEDIA_TYPE_VIDEO, },
159                                   { .name = NULL}},
160 };