[FFmpeg-devel] lavfi: Add fps filter.

Robert Nagy ronag89 at gmail.com
Fri May 4 15:51:27 CEST 2012


Handle some corner cases:
> 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->src->priv;
> +    AVRational in_time_base = link->src->inputs[0]->time_base;
> +
> +    if (!fps->pic || fps->pts == AV_NOPTS_VALUE)
> +        fps->pts = picref->pts == AV_NOPTS_VALUE ? picref->pts :
av_rescale(picref->pts, in_time_base.num*AV_TIME_BASE, in_time_base.den);
> +
> +    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;
> +
> +    int ret = 0;
> +    while (!fps->pic || fps->pts == AV_NOPTS_VALUE ||
av_rescale(fps->pic->pts*in_time_base.num, AV_TIME_BASE, in_time_base.den)
< fps->pts){
> +        ret = avfilter_request_frame(link->src->inputs[0]);
> +        if (ret < 0)
> +            break;
> +    }
> +
> +    if (ret == AVERROR_EOF)
> +       avfilter_unref_bufferp(&fps->pic);
> +
> +    if (fps->pts != AV_NOPTS_VALUE)
> +       fps->pts += av_rescale(fps->time_base.num, AV_TIME_BASE,
fps->time_base.den);
> +
> +    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);
> +
> +    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}},
> +};


More information about the ffmpeg-devel mailing list