]> git.sesse.net Git - ffmpeg/blob - libavfilter/f_setpts.c
lavfi: rename vf_setpts.c to f_setpts.c
[ffmpeg] / libavfilter / f_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/internal.h"
31 #include "libavutil/mathematics.h"
32 #include "avfilter.h"
33 #include "internal.h"
34 #include "audio.h"
35 #include "video.h"
36
37 static const char *const var_names[] = {
38     "INTERLACED",  ///< tell if the current frame is interlaced
39     "N",           ///< frame number (starting at zero)
40     "NB_CONSUMED_SAMPLES", ///< number of samples consumed by the filter (only audio)
41     "NB_SAMPLES",  ///< number of samples in the current frame (only audio)
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     "SAMPLE_RATE", ///< sample rate (only audio)
47     "STARTPTS",    ///< PTS at start of movie
48     "TB",          ///< timebase
49     NULL
50 };
51
52 enum var_name {
53     VAR_INTERLACED,
54     VAR_N,
55     VAR_NB_CONSUMED_SAMPLES,
56     VAR_NB_SAMPLES,
57     VAR_POS,
58     VAR_PREV_INPTS,
59     VAR_PREV_OUTPTS,
60     VAR_PTS,
61     VAR_SAMPLE_RATE,
62     VAR_STARTPTS,
63     VAR_TB,
64     VAR_VARS_NB
65 };
66
67 typedef struct {
68     AVExpr *expr;
69     double var_values[VAR_VARS_NB];
70     enum AVMediaType type;
71 } SetPTSContext;
72
73 static av_cold int init(AVFilterContext *ctx, const char *args)
74 {
75     SetPTSContext *setpts = ctx->priv;
76     int ret;
77
78     if ((ret = av_expr_parse(&setpts->expr, args ? args : "PTS",
79                              var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
80         av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", args);
81         return ret;
82     }
83
84     setpts->var_values[VAR_N          ] = 0.0;
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     AVFilterContext *ctx = inlink->dst;
94     SetPTSContext *setpts = ctx->priv;
95
96     setpts->type = inlink->type;
97     setpts->var_values[VAR_TB] = av_q2d(inlink->time_base);
98
99     if (setpts->type == AVMEDIA_TYPE_AUDIO)
100         setpts->var_values[VAR_SAMPLE_RATE] = inlink->sample_rate;
101
102     av_log(inlink->src, AV_LOG_VERBOSE, "TB:%f SAMPLE_RATE:%f\n",
103            setpts->var_values[VAR_TB], setpts->var_values[VAR_SAMPLE_RATE]);
104     return 0;
105 }
106
107 #define D2TS(d)  (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
108 #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
109
110 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
111 {
112     SetPTSContext *setpts = inlink->dst->priv;
113     double d;
114     AVFilterBufferRef *outpicref = avfilter_ref_buffer(inpicref, ~0);
115
116     if (!outpicref)
117         return AVERROR(ENOMEM);
118
119     if (isnan(setpts->var_values[VAR_STARTPTS]))
120         setpts->var_values[VAR_STARTPTS] = TS2D(inpicref->pts);
121     setpts->var_values[VAR_PTS       ] = TS2D(inpicref->pts);
122     setpts->var_values[VAR_POS       ] = inpicref->pos == -1 ? NAN : inpicref->pos;
123
124     switch (inlink->type) {
125     case AVMEDIA_TYPE_VIDEO:
126         setpts->var_values[VAR_INTERLACED] = inpicref->video->interlaced;
127         break;
128
129     case AVMEDIA_TYPE_AUDIO:
130         setpts->var_values[VAR_NB_SAMPLES] = inpicref->audio->nb_samples;
131         break;
132     }
133
134     d = av_expr_eval(setpts->expr, setpts->var_values, NULL);
135     outpicref->pts = D2TS(d);
136
137 #ifdef DEBUG
138     av_log(inlink->dst, AV_LOG_DEBUG,
139            "n:%"PRId64" interlaced:%d nb_samples:%d nb_consumed_samples:%d "
140            "pos:%"PRId64" pts:%"PRId64" t:%f -> pts:%"PRId64" t:%f\n",
141            (int64_t)setpts->var_values[VAR_N],
142            (int)setpts->var_values[VAR_INTERLACED],
143            (int)setpts->var_values[VAR_NB_SAMPLES],
144            (int)setpts->var_values[VAR_NB_CONSUMED_SAMPLES],
145            inpicref ->pos,
146            inpicref ->pts, inpicref ->pts * av_q2d(inlink->time_base),
147            outpicref->pts, outpicref->pts * av_q2d(inlink->time_base));
148
149 #endif
150
151     setpts->var_values[VAR_N] += 1.0;
152     setpts->var_values[VAR_PREV_INPTS ] = TS2D(inpicref ->pts);
153     setpts->var_values[VAR_PREV_OUTPTS] = TS2D(outpicref->pts);
154
155     if (setpts->type == AVMEDIA_TYPE_AUDIO) {
156         setpts->var_values[VAR_NB_CONSUMED_SAMPLES] += inpicref->audio->nb_samples;
157         return ff_filter_samples(inlink->dst->outputs[0], outpicref);
158     } else
159         return ff_start_frame   (inlink->dst->outputs[0], outpicref);
160 }
161
162 static av_cold void uninit(AVFilterContext *ctx)
163 {
164     SetPTSContext *setpts = ctx->priv;
165     av_expr_free(setpts->expr);
166     setpts->expr = NULL;
167 }
168
169 #if CONFIG_ASETPTS_FILTER
170 AVFilter avfilter_af_asetpts = {
171     .name      = "asetpts",
172     .description = NULL_IF_CONFIG_SMALL("Set PTS for the output audio frame."),
173     .init      = init,
174     .uninit    = uninit,
175
176     .priv_size = sizeof(SetPTSContext),
177
178     .inputs = (const AVFilterPad[]) {
179         {
180             .name             = "default",
181             .type             = AVMEDIA_TYPE_AUDIO,
182             .get_audio_buffer = ff_null_get_audio_buffer,
183             .config_props     = config_input,
184             .filter_samples   = filter_frame,
185         },
186         { .name = NULL }
187     },
188     .outputs = (const AVFilterPad[]) {
189         {
190             .name             = "default",
191             .type             = AVMEDIA_TYPE_AUDIO,
192         },
193         { .name = NULL }
194     },
195 };
196 #endif /* CONFIG_ASETPTS_FILTER */
197
198 #if CONFIG_SETPTS_FILTER
199 AVFilter avfilter_vf_setpts = {
200     .name      = "setpts",
201     .description = NULL_IF_CONFIG_SMALL("Set PTS for the output video frame."),
202     .init      = init,
203     .uninit    = uninit,
204
205     .priv_size = sizeof(SetPTSContext),
206
207     .inputs    = (const AVFilterPad[]) {{ .name             = "default",
208                                           .type             = AVMEDIA_TYPE_VIDEO,
209                                           .get_video_buffer = ff_null_get_video_buffer,
210                                           .config_props     = config_input,
211                                           .start_frame      = filter_frame, },
212                                         { .name = NULL }},
213     .outputs   = (const AVFilterPad[]) {{ .name             = "default",
214                                           .type             = AVMEDIA_TYPE_VIDEO, },
215                                         { .name = NULL}},
216 };
217 #endif /* CONFIG_SETPTS_FILTER */