Browse code

lavfi: add an asetpts filter

Anton Khirnov authored on 2013/04/10 23:28:38
Showing 7 changed files
... ...
@@ -12,6 +12,7 @@ version 10:
12 12
 - uniform options syntax across all filters
13 13
 - new interlace filter
14 14
 - JPEG 2000 decoder
15
+- new asetpts filter (same as setpts, but for audio)
15 16
 
16 17
 
17 18
 version 9:
... ...
@@ -192,6 +192,75 @@ stream ends. The default value is 2 seconds.
192 192
 
193 193
 Pass the audio source unchanged to the output.
194 194
 
195
+@section asetpts
196
+
197
+Change the PTS (presentation timestamp) of the input audio frames.
198
+
199
+This filter accepts the following options:
200
+
201
+@table @option
202
+
203
+@item expr
204
+The expression which is evaluated for each frame to construct its timestamp.
205
+
206
+@end table
207
+
208
+The expression is evaluated through the eval API and can contain the following
209
+constants:
210
+
211
+@table @option
212
+@item PTS
213
+the presentation timestamp in input
214
+
215
+@item PI
216
+Greek PI
217
+
218
+@item PHI
219
+golden ratio
220
+
221
+@item E
222
+Euler number
223
+
224
+@item N
225
+Number of the audio samples pass through the filter so far, starting at 0.
226
+
227
+@item S
228
+Number of the audio samples in the current frame.
229
+
230
+@item SR
231
+Audio sample rate.
232
+
233
+@item STARTPTS
234
+the PTS of the first frame
235
+
236
+@item PREV_INPTS
237
+previous input PTS
238
+
239
+@item PREV_OUTPTS
240
+previous output PTS
241
+
242
+@item RTCTIME
243
+wallclock (RTC) time in microseconds
244
+
245
+@item RTCSTART
246
+wallclock (RTC) time at the start of the movie in microseconds
247
+
248
+@end table
249
+
250
+Some examples follow:
251
+
252
+@example
253
+# start counting PTS from zero
254
+asetpts=expr=PTS-STARTPTS
255
+
256
+#generate timestamps by counting samples
257
+asetpts=expr=N/SR/TB
258
+
259
+# generate timestamps from a "live source" and rebase onto the current timebase
260
+asetpts='(RTCTIME - RTCSTART) / (TB * 1000000)"
261
+@end example
262
+
263
+
195 264
 @section ashowinfo
196 265
 
197 266
 Show a line containing various information for each input audio frame.
... ...
@@ -27,6 +27,7 @@ OBJS = allfilters.o                                                     \
27 27
 OBJS-$(CONFIG_AFORMAT_FILTER)                += af_aformat.o
28 28
 OBJS-$(CONFIG_AMIX_FILTER)                   += af_amix.o
29 29
 OBJS-$(CONFIG_ANULL_FILTER)                  += af_anull.o
30
+OBJS-$(CONFIG_ASETPTS_FILTER)                += setpts.o
30 31
 OBJS-$(CONFIG_ASHOWINFO_FILTER)              += af_ashowinfo.o
31 32
 OBJS-$(CONFIG_ASPLIT_FILTER)                 += split.o
32 33
 OBJS-$(CONFIG_ASYNCTS_FILTER)                += af_asyncts.o
... ...
@@ -70,7 +71,7 @@ OBJS-$(CONFIG_PIXDESCTEST_FILTER)            += vf_pixdesctest.o
70 70
 OBJS-$(CONFIG_SCALE_FILTER)                  += vf_scale.o
71 71
 OBJS-$(CONFIG_SELECT_FILTER)                 += vf_select.o
72 72
 OBJS-$(CONFIG_SETDAR_FILTER)                 += vf_aspect.o
73
-OBJS-$(CONFIG_SETPTS_FILTER)                 += vf_setpts.o
73
+OBJS-$(CONFIG_SETPTS_FILTER)                 += setpts.o
74 74
 OBJS-$(CONFIG_SETSAR_FILTER)                 += vf_aspect.o
75 75
 OBJS-$(CONFIG_SETTB_FILTER)                  += vf_settb.o
76 76
 OBJS-$(CONFIG_SHOWINFO_FILTER)               += vf_showinfo.o
... ...
@@ -47,6 +47,7 @@ void avfilter_register_all(void)
47 47
     REGISTER_FILTER(AFORMAT,        aformat,        af);
48 48
     REGISTER_FILTER(AMIX,           amix,           af);
49 49
     REGISTER_FILTER(ANULL,          anull,          af);
50
+    REGISTER_FILTER(ASETPTS,        asetpts,        af);
50 51
     REGISTER_FILTER(ASHOWINFO,      ashowinfo,      af);
51 52
     REGISTER_FILTER(ASPLIT,         asplit,         af);
52 53
     REGISTER_FILTER(ASYNCTS,        asyncts,        af);
53 54
new file mode 100644
... ...
@@ -0,0 +1,261 @@
0
+/*
1
+ * Copyright (c) 2010 Stefano Sabatini
2
+ * Copyright (c) 2008 Victor Paesa
3
+ *
4
+ * This file is part of Libav.
5
+ *
6
+ * Libav is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * Libav is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with Libav; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+/**
22
+ * @file
23
+ * video presentation timestamp (PTS) modification filter
24
+ */
25
+
26
+/* #define DEBUG */
27
+
28
+#include "libavutil/eval.h"
29
+#include "libavutil/internal.h"
30
+#include "libavutil/mathematics.h"
31
+#include "libavutil/opt.h"
32
+#include "libavutil/time.h"
33
+
34
+#include "audio.h"
35
+#include "avfilter.h"
36
+#include "internal.h"
37
+#include "video.h"
38
+
39
+#include "config.h"
40
+
41
+static const char *const var_names[] = {
42
+    "E",           ///< Euler number
43
+    "INTERLACED",  ///< tell if the current frame is interlaced
44
+    "N",           ///< frame / sample number (starting at zero)
45
+    "PHI",         ///< golden ratio
46
+    "PI",          ///< greek pi
47
+    "PREV_INPTS",  ///< previous  input PTS
48
+    "PREV_OUTPTS", ///< previous output PTS
49
+    "PTS",         ///< original pts in the file of the frame
50
+    "STARTPTS",    ///< PTS at start of movie
51
+    "TB",          ///< timebase
52
+    "RTCTIME",     ///< wallclock (RTC) time in micro seconds
53
+    "RTCSTART",    ///< wallclock (RTC) time at the start of the movie in micro seconds
54
+    "S",           //   Number of samples in the current frame
55
+    "SR",          //   Audio sample rate
56
+    NULL
57
+};
58
+
59
+enum var_name {
60
+    VAR_E,
61
+    VAR_INTERLACED,
62
+    VAR_N,
63
+    VAR_PHI,
64
+    VAR_PI,
65
+    VAR_PREV_INPTS,
66
+    VAR_PREV_OUTPTS,
67
+    VAR_PTS,
68
+    VAR_STARTPTS,
69
+    VAR_TB,
70
+    VAR_RTCTIME,
71
+    VAR_RTCSTART,
72
+    VAR_S,
73
+    VAR_SR,
74
+    VAR_VARS_NB
75
+};
76
+
77
+typedef struct {
78
+    const AVClass *class;
79
+    char *expr_str;
80
+    AVExpr *expr;
81
+    double var_values[VAR_VARS_NB];
82
+} SetPTSContext;
83
+
84
+static av_cold int init(AVFilterContext *ctx)
85
+{
86
+    SetPTSContext *setpts = ctx->priv;
87
+    int ret;
88
+
89
+    if ((ret = av_expr_parse(&setpts->expr, setpts->expr_str,
90
+                             var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
91
+        av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", setpts->expr_str);
92
+        return ret;
93
+    }
94
+
95
+    setpts->var_values[VAR_E]           = M_E;
96
+    setpts->var_values[VAR_N]           = 0.0;
97
+    setpts->var_values[VAR_S]           = 0.0;
98
+    setpts->var_values[VAR_PHI]         = M_PHI;
99
+    setpts->var_values[VAR_PI]          = M_PI;
100
+    setpts->var_values[VAR_PREV_INPTS]  = NAN;
101
+    setpts->var_values[VAR_PREV_OUTPTS] = NAN;
102
+    setpts->var_values[VAR_STARTPTS]    = NAN;
103
+    return 0;
104
+}
105
+
106
+static int config_input(AVFilterLink *inlink)
107
+{
108
+    SetPTSContext *setpts = inlink->dst->priv;
109
+
110
+    setpts->var_values[VAR_TB] = av_q2d(inlink->time_base);
111
+    setpts->var_values[VAR_RTCSTART] = av_gettime();
112
+
113
+    if (inlink->type == AVMEDIA_TYPE_AUDIO) {
114
+        setpts->var_values[VAR_SR] = inlink->sample_rate;
115
+    }
116
+
117
+    av_log(inlink->src, AV_LOG_VERBOSE, "TB:%f\n", setpts->var_values[VAR_TB]);
118
+    return 0;
119
+}
120
+
121
+#define D2TS(d)  (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
122
+#define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
123
+
124
+static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
125
+{
126
+    SetPTSContext *setpts = inlink->dst->priv;
127
+    int64_t in_pts = frame->pts;
128
+    double d;
129
+
130
+    if (isnan(setpts->var_values[VAR_STARTPTS]))
131
+        setpts->var_values[VAR_STARTPTS] = TS2D(frame->pts);
132
+
133
+    setpts->var_values[VAR_PTS       ] = TS2D(frame->pts);
134
+    setpts->var_values[VAR_RTCTIME   ] = av_gettime();
135
+
136
+    if (inlink->type == AVMEDIA_TYPE_VIDEO) {
137
+        setpts->var_values[VAR_INTERLACED] = frame->interlaced_frame;
138
+    } else {
139
+        setpts->var_values[VAR_S] = frame->nb_samples;
140
+    }
141
+
142
+    d = av_expr_eval(setpts->expr, setpts->var_values, NULL);
143
+    frame->pts = D2TS(d);
144
+
145
+#ifdef DEBUG
146
+    av_log(inlink->dst, AV_LOG_DEBUG,
147
+           "n:%"PRId64" interlaced:%d pts:%"PRId64" t:%f -> pts:%"PRId64" t:%f\n",
148
+           (int64_t)setpts->var_values[VAR_N],
149
+           (int)setpts->var_values[VAR_INTERLACED],
150
+           in_pts, in_pts * av_q2d(inlink->time_base),
151
+           frame->pts, frame->pts * av_q2d(inlink->time_base));
152
+#endif
153
+
154
+
155
+    if (inlink->type == AVMEDIA_TYPE_VIDEO) {
156
+        setpts->var_values[VAR_N] += 1.0;
157
+    } else {
158
+        setpts->var_values[VAR_N] += frame->nb_samples;
159
+    }
160
+
161
+    setpts->var_values[VAR_PREV_INPTS ] = TS2D(in_pts);
162
+    setpts->var_values[VAR_PREV_OUTPTS] = TS2D(frame->pts);
163
+    return ff_filter_frame(inlink->dst->outputs[0], frame);
164
+}
165
+
166
+static av_cold void uninit(AVFilterContext *ctx)
167
+{
168
+    SetPTSContext *setpts = ctx->priv;
169
+    av_expr_free(setpts->expr);
170
+    setpts->expr = NULL;
171
+}
172
+
173
+#define OFFSET(x) offsetof(SetPTSContext, x)
174
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_AUDIO_PARAM
175
+static const AVOption options[] = {
176
+    { "expr", "Expression determining the frame timestamp", OFFSET(expr_str), AV_OPT_TYPE_STRING, { .str = "PTS" }, .flags = FLAGS },
177
+    { NULL },
178
+};
179
+
180
+#if CONFIG_SETPTS_FILTER
181
+static const AVClass setpts_class = {
182
+    .class_name = "setpts",
183
+    .item_name  = av_default_item_name,
184
+    .option     = options,
185
+    .version    = LIBAVUTIL_VERSION_INT,
186
+};
187
+
188
+static const AVFilterPad avfilter_vf_setpts_inputs[] = {
189
+    {
190
+        .name             = "default",
191
+        .type             = AVMEDIA_TYPE_VIDEO,
192
+        .get_video_buffer = ff_null_get_video_buffer,
193
+        .config_props     = config_input,
194
+        .filter_frame     = filter_frame,
195
+    },
196
+    { NULL }
197
+};
198
+
199
+static const AVFilterPad avfilter_vf_setpts_outputs[] = {
200
+    {
201
+        .name = "default",
202
+        .type = AVMEDIA_TYPE_VIDEO,
203
+    },
204
+    { NULL }
205
+};
206
+
207
+AVFilter avfilter_vf_setpts = {
208
+    .name      = "setpts",
209
+    .description = NULL_IF_CONFIG_SMALL("Set PTS for the output video frame."),
210
+    .init      = init,
211
+    .uninit    = uninit,
212
+
213
+    .priv_size = sizeof(SetPTSContext),
214
+    .priv_class = &setpts_class,
215
+
216
+    .inputs    = avfilter_vf_setpts_inputs,
217
+    .outputs   = avfilter_vf_setpts_outputs,
218
+};
219
+#endif
220
+
221
+#if CONFIG_ASETPTS_FILTER
222
+static const AVClass asetpts_class = {
223
+    .class_name = "asetpts",
224
+    .item_name  = av_default_item_name,
225
+    .option     = options,
226
+    .version    = LIBAVUTIL_VERSION_INT,
227
+};
228
+
229
+static const AVFilterPad asetpts_inputs[] = {
230
+    {
231
+        .name             = "default",
232
+        .type             = AVMEDIA_TYPE_AUDIO,
233
+        .get_audio_buffer = ff_null_get_audio_buffer,
234
+        .config_props     = config_input,
235
+        .filter_frame     = filter_frame,
236
+    },
237
+    { NULL }
238
+};
239
+
240
+static const AVFilterPad asetpts_outputs[] = {
241
+    {
242
+        .name = "default",
243
+        .type = AVMEDIA_TYPE_AUDIO,
244
+    },
245
+    { NULL }
246
+};
247
+
248
+AVFilter avfilter_af_asetpts = {
249
+    .name        = "asetpts",
250
+    .description = NULL_IF_CONFIG_SMALL("Set PTS for the output audio frame."),
251
+    .init        = init,
252
+    .uninit      = uninit,
253
+
254
+    .priv_size  = sizeof(SetPTSContext),
255
+    .priv_class = &asetpts_class,
256
+
257
+    .inputs    = asetpts_inputs,
258
+    .outputs   = asetpts_outputs,
259
+};
260
+#endif
... ...
@@ -29,7 +29,7 @@
29 29
 #include "libavutil/avutil.h"
30 30
 
31 31
 #define LIBAVFILTER_VERSION_MAJOR  3
32
-#define LIBAVFILTER_VERSION_MINOR  8
32
+#define LIBAVFILTER_VERSION_MINOR  9
33 33
 #define LIBAVFILTER_VERSION_MICRO  0
34 34
 
35 35
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
36 36
deleted file mode 100644
... ...
@@ -1,195 +0,0 @@
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 "libavutil/opt.h"
33
-#include "libavutil/time.h"
34
-#include "avfilter.h"
35
-#include "internal.h"
36
-#include "video.h"
37
-
38
-static const char *const var_names[] = {
39
-    "E",           ///< Euler number
40
-    "INTERLACED",  ///< tell if the current frame is interlaced
41
-    "N",           ///< frame number (starting at zero)
42
-    "PHI",         ///< golden ratio
43
-    "PI",          ///< greek pi
44
-    "PREV_INPTS",  ///< previous  input PTS
45
-    "PREV_OUTPTS", ///< previous output PTS
46
-    "PTS",         ///< original pts in the file of the frame
47
-    "STARTPTS",   ///< PTS at start of movie
48
-    "TB",          ///< timebase
49
-    "RTCTIME",     ///< wallclock (RTC) time in micro seconds
50
-    "RTCSTART",    ///< wallclock (RTC) time at the start of the movie in micro seconds
51
-    NULL
52
-};
53
-
54
-enum var_name {
55
-    VAR_E,
56
-    VAR_INTERLACED,
57
-    VAR_N,
58
-    VAR_PHI,
59
-    VAR_PI,
60
-    VAR_PREV_INPTS,
61
-    VAR_PREV_OUTPTS,
62
-    VAR_PTS,
63
-    VAR_STARTPTS,
64
-    VAR_TB,
65
-    VAR_RTCTIME,
66
-    VAR_RTCSTART,
67
-    VAR_VARS_NB
68
-};
69
-
70
-typedef struct {
71
-    const AVClass *class;
72
-    char *expr_str;
73
-    AVExpr *expr;
74
-    double var_values[VAR_VARS_NB];
75
-} SetPTSContext;
76
-
77
-static av_cold int init(AVFilterContext *ctx)
78
-{
79
-    SetPTSContext *setpts = ctx->priv;
80
-    int ret;
81
-
82
-    if ((ret = av_expr_parse(&setpts->expr, setpts->expr_str,
83
-                             var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
84
-        av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", setpts->expr_str);
85
-        return ret;
86
-    }
87
-
88
-    setpts->var_values[VAR_E]           = M_E;
89
-    setpts->var_values[VAR_N]           = 0.0;
90
-    setpts->var_values[VAR_PHI]         = M_PHI;
91
-    setpts->var_values[VAR_PI]          = M_PI;
92
-    setpts->var_values[VAR_PREV_INPTS]  = NAN;
93
-    setpts->var_values[VAR_PREV_OUTPTS] = NAN;
94
-    setpts->var_values[VAR_STARTPTS]    = NAN;
95
-    return 0;
96
-}
97
-
98
-static int config_input(AVFilterLink *inlink)
99
-{
100
-    SetPTSContext *setpts = inlink->dst->priv;
101
-
102
-    setpts->var_values[VAR_TB] = av_q2d(inlink->time_base);
103
-    setpts->var_values[VAR_RTCSTART] = av_gettime();
104
-
105
-    av_log(inlink->src, AV_LOG_VERBOSE, "TB:%f\n", setpts->var_values[VAR_TB]);
106
-    return 0;
107
-}
108
-
109
-#define D2TS(d)  (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
110
-#define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
111
-
112
-static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
113
-{
114
-    SetPTSContext *setpts = inlink->dst->priv;
115
-    int64_t in_pts = frame->pts;
116
-    double d;
117
-
118
-    if (isnan(setpts->var_values[VAR_STARTPTS]))
119
-        setpts->var_values[VAR_STARTPTS] = TS2D(frame->pts);
120
-
121
-    setpts->var_values[VAR_INTERLACED] = frame->interlaced_frame;
122
-    setpts->var_values[VAR_PTS       ] = TS2D(frame->pts);
123
-    setpts->var_values[VAR_RTCTIME   ] = av_gettime();
124
-
125
-    d = av_expr_eval(setpts->expr, setpts->var_values, NULL);
126
-    frame->pts = D2TS(d);
127
-
128
-#ifdef DEBUG
129
-    av_log(inlink->dst, AV_LOG_DEBUG,
130
-           "n:%"PRId64" interlaced:%d pts:%"PRId64" t:%f -> pts:%"PRId64" t:%f\n",
131
-           (int64_t)setpts->var_values[VAR_N],
132
-           (int)setpts->var_values[VAR_INTERLACED],
133
-           in_pts, in_pts * av_q2d(inlink->time_base),
134
-           frame->pts, frame->pts * av_q2d(inlink->time_base));
135
-#endif
136
-
137
-
138
-    setpts->var_values[VAR_N] += 1.0;
139
-    setpts->var_values[VAR_PREV_INPTS ] = TS2D(in_pts);
140
-    setpts->var_values[VAR_PREV_OUTPTS] = TS2D(frame->pts);
141
-    return ff_filter_frame(inlink->dst->outputs[0], frame);
142
-}
143
-
144
-static av_cold void uninit(AVFilterContext *ctx)
145
-{
146
-    SetPTSContext *setpts = ctx->priv;
147
-    av_expr_free(setpts->expr);
148
-    setpts->expr = NULL;
149
-}
150
-
151
-#define OFFSET(x) offsetof(SetPTSContext, x)
152
-#define FLAGS AV_OPT_FLAG_VIDEO_PARAM
153
-static const AVOption options[] = {
154
-    { "expr", "Expression determining the frame timestamp", OFFSET(expr_str), AV_OPT_TYPE_STRING, { .str = "PTS" }, .flags = FLAGS },
155
-    { NULL },
156
-};
157
-
158
-static const AVClass setpts_class = {
159
-    .class_name = "setpts",
160
-    .item_name  = av_default_item_name,
161
-    .option     = options,
162
-    .version    = LIBAVUTIL_VERSION_INT,
163
-};
164
-
165
-static const AVFilterPad avfilter_vf_setpts_inputs[] = {
166
-    {
167
-        .name             = "default",
168
-        .type             = AVMEDIA_TYPE_VIDEO,
169
-        .get_video_buffer = ff_null_get_video_buffer,
170
-        .config_props     = config_input,
171
-        .filter_frame     = filter_frame,
172
-    },
173
-    { NULL }
174
-};
175
-
176
-static const AVFilterPad avfilter_vf_setpts_outputs[] = {
177
-    {
178
-        .name = "default",
179
-        .type = AVMEDIA_TYPE_VIDEO,
180
-    },
181
-    { NULL }
182
-};
183
-
184
-AVFilter avfilter_vf_setpts = {
185
-    .name      = "setpts",
186
-    .description = NULL_IF_CONFIG_SMALL("Set PTS for the output video frame."),
187
-    .init      = init,
188
-    .uninit    = uninit,
189
-
190
-    .priv_size = sizeof(SetPTSContext),
191
-    .priv_class = &setpts_class,
192
-
193
-    .inputs    = avfilter_vf_setpts_inputs,
194
-    .outputs   = avfilter_vf_setpts_outputs,
195
-};