[FFmpeg-devel] [PATCH] avfilter: add setfps filter

Paul B Mahol onemda at gmail.com
Tue Sep 29 23:39:10 CEST 2015


Signed-off-by: Paul B Mahol <onemda at gmail.com>
---
 libavfilter/Makefile     |   1 +
 libavfilter/allfilters.c |   1 +
 libavfilter/vf_setfps.c  | 119 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 121 insertions(+)
 create mode 100644 libavfilter/vf_setfps.c

diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 9125443..b1a0f9d 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -208,6 +208,7 @@ OBJS-$(CONFIG_SELECT_FILTER)                 += f_select.o
 OBJS-$(CONFIG_SENDCMD_FILTER)                += f_sendcmd.o
 OBJS-$(CONFIG_SETDAR_FILTER)                 += vf_aspect.o
 OBJS-$(CONFIG_SETFIELD_FILTER)               += vf_setfield.o
+OBJS-$(CONFIG_SETFPS_FILTER)                 += vf_setfps.o
 OBJS-$(CONFIG_SETPTS_FILTER)                 += setpts.o
 OBJS-$(CONFIG_SETSAR_FILTER)                 += vf_aspect.o
 OBJS-$(CONFIG_SETTB_FILTER)                  += settb.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 612fc99..17748db 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -229,6 +229,7 @@ void avfilter_register_all(void)
     REGISTER_FILTER(SEPARATEFIELDS, separatefields, vf);
     REGISTER_FILTER(SETDAR,         setdar,         vf);
     REGISTER_FILTER(SETFIELD,       setfield,       vf);
+    REGISTER_FILTER(SETFPS,         setfps,         vf);
     REGISTER_FILTER(SETPTS,         setpts,         vf);
     REGISTER_FILTER(SETSAR,         setsar,         vf);
     REGISTER_FILTER(SETTB,          settb,          vf);
diff --git a/libavfilter/vf_setfps.c b/libavfilter/vf_setfps.c
new file mode 100644
index 0000000..4360998
--- /dev/null
+++ b/libavfilter/vf_setfps.c
@@ -0,0 +1,119 @@
+/*
+ * Copyright (c) 2015 The FFmpeg Project
+ *
+ * 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
+ * a filter changing framerate and updating pts
+ */
+
+#include <float.h>
+#include <stdint.h>
+
+#include "libavutil/common.h"
+#include "libavutil/fifo.h"
+#include "libavutil/mathematics.h"
+#include "libavutil/opt.h"
+#include "libavutil/parseutils.h"
+
+#include "avfilter.h"
+#include "internal.h"
+#include "video.h"
+
+typedef struct SetFPSContext {
+    const AVClass *class;
+
+    AVRational framerate;
+    int64_t first_pts;
+} SetFPSContext;
+
+#define OFFSET(x) offsetof(SetFPSContext, x)
+#define V AV_OPT_FLAG_VIDEO_PARAM
+#define F AV_OPT_FLAG_FILTERING_PARAM
+static const AVOption setfps_options[] = {
+    { "fps", "A string describing desired output framerate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, { .str = "25" }, .flags = V|F },
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(setfps);
+
+static int config_props(AVFilterLink *outlink)
+{
+    AVFilterContext *ctx = outlink->src;
+    SetFPSContext *s = ctx->priv;
+    AVFilterLink *inlink = ctx->inputs[0];
+
+    outlink->time_base  = av_inv_q(s->framerate);
+    outlink->frame_rate = s->framerate;
+    s->first_pts = AV_NOPTS_VALUE;
+
+    av_log(outlink->src, AV_LOG_VERBOSE, "fps:%d/%d -> fps:%d/%d\n",
+           inlink ->frame_rate.num, inlink ->frame_rate.den,
+           outlink->frame_rate.num, outlink->frame_rate.den);
+
+    return 0;
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
+{
+    AVFilterContext *ctx = inlink->dst;
+    SetFPSContext *s = ctx->priv;
+    AVFilterLink *outlink = ctx->outputs[0];
+
+    if (av_cmp_q(inlink->time_base, outlink->time_base) && ((s->first_pts != AV_NOPTS_VALUE) || (frame->pts != AV_NOPTS_VALUE))) {
+        int64_t orig_pts = frame->pts;
+
+        if (s->first_pts == AV_NOPTS_VALUE)
+            s->first_pts = frame->pts;
+
+        frame->pts = av_rescale_q(s->first_pts, inlink->time_base, outlink->time_base) + outlink->frame_count;
+        av_log(ctx, AV_LOG_DEBUG, "tb:%d/%d pts:%"PRId64" -> tb:%d/%d pts:%"PRId64"\n",
+               inlink ->time_base.num, inlink ->time_base.den, orig_pts,
+               outlink->time_base.num, outlink->time_base.den, frame->pts);
+    }
+
+    return ff_filter_frame(outlink, frame);
+}
+
+static const AVFilterPad setfps_inputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_VIDEO,
+        .filter_frame = filter_frame,
+    },
+    { NULL }
+};
+
+static const AVFilterPad setfps_outputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_VIDEO,
+        .config_props = config_props
+    },
+    { NULL }
+};
+
+AVFilter ff_vf_setfps = {
+    .name        = "setfps",
+    .description = NULL_IF_CONFIG_SMALL("Set framerate for video output link."),
+    .priv_size   = sizeof(SetFPSContext),
+    .priv_class  = &setfps_class,
+    .inputs      = setfps_inputs,
+    .outputs     = setfps_outputs,
+};
-- 
1.9.1



More information about the ffmpeg-devel mailing list