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