[FFmpeg-devel] lavfi: Add fps filter.

Robert Nagy ronag89 at gmail.com
Fri May 4 14:47:14 CEST 2012


>
> libavcore doesn't exist anymore; please build this with the latest FFmpeg.
> Also since the patch is missing Makefile entry in order to test it, it's
> hard to do a proper review.


Unfortunately I do not have a Linux environment to build ffmpeg in.

>From d19e321a08189ef152c78b59ccb53cafcb896b9d Mon Sep 17 00:00:00 2001
From: Robert Nagy <ronag89 at gmail.com>
Date: Fri, 4 May 2012 10:42:34 +0200
Subject: [PATCH] lavfi: Add fps filter.

---
 libavfilter/vf_fps.c |  126
++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 126 insertions(+), 0 deletions(-)
 create mode 100644 libavfilter/vf_fps.c

diff --git a/libavfilter/vf_fps.c b/libavfilter/vf_fps.c
new file mode 100644
index 0000000..1b201f8
--- /dev/null
+++ b/libavfilter/vf_fps.c
@@ -0,0 +1,126 @@
+/*
+ * 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 framerate modification filter
+ */
+
+#include "avfilter.h"
+#include "inttypes.h"
+#include "libavutil/parseutils.h"
+#include "libavutil/mathematics.h"
+
+typedef struct {
+    AVRational         time_base;
+    int64_t            pts;
+    AVFilterBufferRef* pic;
+} FPSContext;
+
+static av_cold int init(AVFilterContext *ctx, const char *args, void
*opaque)
+{
+    FPSContext *fps = ctx->priv;
+
+    AVRational rate;
+
+    if (!args || (av_parse_video_rate(&rate, args) < 0))
+        return -1;
+
+    fps->time_base.den = rate.num;
+    fps->time_base.num = rate.den;
+
+    return 0;
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+    FPSContext *fps = ctx->priv;
+    avfilter_unref_bufferp(&fps->pic);
+}
+
+static void start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
+{
+    FPSContext *fps = link->dst->priv;
+
+    if (!fps->pic)
+        fps->pts = picref->pts;
+
+    avfilter_unref_bufferp(&fps->pic);
+    fps->pic = picref;
+}
+
+static void end_frame(AVFilterLink *link)
+{
+}
+
+static int request_frame(AVFilterLink *link)
+{
+    FPSContext *fps = link->src->priv;
+
+    AVRational in_time_base = link->src->inputs[0]->time_base;
+
+    while (!fps->pic || av_rescale(fps->pic->pts*in_time_base.num,
AV_TIME_BASE, in_time_base.den) < fps->pts){
+        int ret = avfilter_request_frame(link->src->inputs[0]);
+
+        if (ret == AVERROR_EOF)
+            avfilter_unref_bufferp(&fps->pic);
+
+        if (ret < 0)
+            return ret;
+    }
+
+    avfilter_start_frame(link, avfilter_ref_buffer(fps->pic, ~0));
+    avfilter_draw_slice (link, 0, fps->pic->video->h, 1);
+    avfilter_end_frame  (link);
+
+    fps->pts += av_rescale(fps->time_base.num, AV_TIME_BASE,
fps->time_base.den);
+
+    return 0;
+}
+
+static int out_config_props(AVFilterLink* link)
+{
+    FPSContext *fps = link->src->priv;
+
+    link->time_base = fps->time_base;
+
+    return 0;
+}
+
+AVFilter avfilter_vf_fps = {
+    .name      = "fps",
+
+    .init      = init,
+    .uninit    = uninit,
+
+    .priv_size = sizeof(FPSContext),
+
+    .inputs    = (AVFilterPad[]) {{ .name            = "default",
+                                    .type            = AVMEDIA_TYPE_VIDEO,
+                                    .get_video_buffer=
avfilter_null_get_video_buffer,
+                                    .start_frame     = start_frame,
+                                    .end_frame       = end_frame, },
+                                  { .name = NULL}},
+    .outputs   = (AVFilterPad[]) {{ .name            = "default",
+                                    .type            = AVMEDIA_TYPE_VIDEO,
+                                    .request_frame   = request_frame,
+                                    .config_props    = out_config_props},
+                                  { .name = NULL}},
+};
-- 
1.7.6.msysgit.0


More information about the ffmpeg-devel mailing list