[FFmpeg-devel] [PATCH 3/4] Add setpts filter by Victor Paesa.

Stefano Sabatini stefano.sabatini-lala
Wed Jun 9 00:17:35 CEST 2010


---
 doc/libavfilter.texi     |   46 +++++++++++++++++
 libavfilter/Makefile     |    1 +
 libavfilter/allfilters.c |    1 +
 libavfilter/vf_setpts.c  |  127 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 175 insertions(+), 0 deletions(-)
 create mode 100644 libavfilter/vf_setpts.c

diff --git a/doc/libavfilter.texi b/doc/libavfilter.texi
index 1de27d9..cf53a93 100644
--- a/doc/libavfilter.texi
+++ b/doc/libavfilter.texi
@@ -237,6 +237,52 @@ ratio of the input image.
 
 The default value of @var{width} and @var{height} is 0.
 
+ at section setpts
+
+Change the PTS (presentation timestamp) of the input video frames.
+
+Accepts in input an expression evaluated through the eval API, which
+can contain the following constants:
+
+ at table
+ at item PTS
+the presentation timestamp in input
+
+ at item PI
+Greek PI
+
+ at item E
+Euler number
+
+ at item AVTB
+the FFmpeg Time Base
+
+ at item N
+the count of the input frame, starting from 0.
+
+ at item STARTPTS
+the PTS of the first video frame
+ at end table
+
+Follows some example:
+
+ at example
+# Start counting PTS from zero
+setpts=PTS-STARTPTS
+
+# Fast motion
+setpts=0.5*PTS
+
+# Slow motion
+setpts=2.0*PTS
+
+# Fixed rate 25 fps
+setpts=N*AVTB/25
+
+# Fixed rate 25 fps with some jitter
+setpts='AVTB/25 * (N+0.05 * sin (N*2*PI/25))'
+ at end example
+
 @section slicify
 
 Pass the images of input video on to next video filter as multiple
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 2ddf217..e96d4da 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -22,6 +22,7 @@ OBJS-$(CONFIG_NULL_FILTER)                   += vf_null.o
 OBJS-$(CONFIG_PAD_FILTER)                    += vf_pad.o
 OBJS-$(CONFIG_PIXELASPECT_FILTER)            += vf_aspect.o
 OBJS-$(CONFIG_SCALE_FILTER)                  += vf_scale.o
+OBJS-$(CONFIG_SETPTS_FILTER)                 += vf_setpts.o
 OBJS-$(CONFIG_SLICIFY_FILTER)                += vf_slicify.o
 OBJS-$(CONFIG_UNSHARP_FILTER)                += vf_unsharp.o
 OBJS-$(CONFIG_VFLIP_FILTER)                  += vf_vflip.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index e85485c..375670c 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -42,6 +42,7 @@ void avfilter_register_all(void)
     REGISTER_FILTER (PAD,         pad,         vf);
     REGISTER_FILTER (PIXELASPECT, pixelaspect, vf);
     REGISTER_FILTER (SCALE,       scale,       vf);
+    REGISTER_FILTER (SETPTS,      setpts,      vf);
     REGISTER_FILTER (SLICIFY,     slicify,     vf);
     REGISTER_FILTER (UNSHARP,     unsharp,     vf);
     REGISTER_FILTER (VFLIP,       vflip,       vf);
diff --git a/libavfilter/vf_setpts.c b/libavfilter/vf_setpts.c
new file mode 100644
index 0000000..af7c9b5
--- /dev/null
+++ b/libavfilter/vf_setpts.c
@@ -0,0 +1,127 @@
+/*
+ * copyright (c) 2008 Victor Paesa
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * video presentation timestamp (PTS) modification filter
+ *
+ * @todo Add multiple inputs/outputs to filter
+ * @todo Add functions of pin number: pts(), startpts()
+ */
+
+#include "libavutil/eval.h"
+#include "avfilter.h"
+
+static const char *const_names[] = {
+    "PI",
+    "E",
+    "AVTB",       ///< AV_TIME_BASE
+    "N",          ///< frame number (starting at zero)
+    "PTS",        ///< original pts
+    "STARTPTS",   ///< pts at start of movie
+    NULL
+};
+
+enum ConstName {
+    PI,
+    E,
+    AVTB,
+    N,
+    PTS,
+    STARTPTS,
+    CONSTS_NB
+};
+
+typedef struct {
+    AVExpr *expr;
+    double const_values[CONSTS_NB];
+} SetPTSContext;
+
+static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
+{
+    SetPTSContext *setpts = ctx->priv;
+    int ret;
+
+    ret = av_parse_expr(&setpts->expr, args ? args : "PTS",
+                        const_names, NULL, NULL, NULL, NULL, 0, ctx);
+    if (ret < 0) {
+        av_log(ctx, AV_LOG_ERROR,
+               "Error while parsing expression '%s'\n", args);
+        return ret;
+    }
+
+    setpts->const_values[PI      ] = M_PI;
+    setpts->const_values[E       ] = M_E;
+    setpts->const_values[AVTB    ] = AV_TIME_BASE;
+    setpts->const_values[N       ] = 0.0;
+    setpts->const_values[STARTPTS] = AV_NOPTS_VALUE;
+    return 0;
+}
+
+static AVFilterPicRef *get_video_buffer(AVFilterLink *link, int perms,
+                                        int w, int h)
+{
+    return avfilter_get_video_buffer(link->dst->outputs[0], perms, w, h);
+}
+
+static void start_frame(AVFilterLink *link, AVFilterPicRef *picref)
+{
+    SetPTSContext *setpts = link->dst->priv;
+    AVFilterPicRef *picref2 = avfilter_ref_pic(picref, ~0);
+
+    if (setpts->const_values[STARTPTS] == AV_NOPTS_VALUE)
+        setpts->const_values[STARTPTS] = picref2->pts;
+
+    setpts->const_values[PTS] = picref2->pts;
+
+    picref2->pts = (int64_t)av_eval_expr(setpts->expr, setpts->const_values, setpts);
+    av_log(link->dst, AV_LOG_DEBUG, "n:%"PRId64" ppts:%"PRId64" -> pts:%"PRId64"\n",
+           (int64_t)setpts->const_values[N  ],
+           (int64_t)setpts->const_values[PTS],
+           picref2->pts);
+
+    setpts->const_values[N] += 1.0;
+    avfilter_start_frame(link->dst->outputs[0], picref2);
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+    SetPTSContext *setpts = ctx->priv;
+    av_free_expr(setpts->expr);
+    setpts->expr = NULL;
+}
+
+AVFilter avfilter_vf_setpts = {
+    .name      = "setpts",
+    .description = "Set PTS for the output video frame.",
+    .init      = init,
+    .uninit    = uninit,
+
+    .priv_size = sizeof(SetPTSContext),
+
+    .inputs    = (AVFilterPad[]) {{ .name            = "default",
+                                    .type            = AVMEDIA_TYPE_VIDEO,
+                                    .get_video_buffer= get_video_buffer,
+                                    .start_frame     = start_frame, },
+                                  { .name = NULL }},
+    .outputs   = (AVFilterPad[]) {{ .name            = "default",
+                                    .type            = AVMEDIA_TYPE_VIDEO, },
+                                  { .name = NULL}},
+};
-- 
1.7.1




More information about the ffmpeg-devel mailing list