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