[FFmpeg-devel] [PATCH] avfilter: add feedback video filter

Paul B Mahol onemda at gmail.com
Tue Apr 12 21:18:02 EEST 2022


On Tue, Apr 12, 2022 at 7:17 PM Michael Niedermayer <michael at niedermayer.cc>
wrote:

> On Mon, Apr 11, 2022 at 07:24:46PM +0200, Paul B Mahol wrote:
> > Signed-off-by: Paul B Mahol <onemda at gmail.com>
> > ---
> >  doc/filters.texi          |  38 +++++
> >  libavfilter/Makefile      |   1 +
> >  libavfilter/allfilters.c  |   1 +
> >  libavfilter/vf_feedback.c | 312 ++++++++++++++++++++++++++++++++++++++
> >  4 files changed, 352 insertions(+)
> >  create mode 100644 libavfilter/vf_feedback.c
> >
> > diff --git a/doc/filters.texi b/doc/filters.texi
> > index 0300a4e2e3..de2b674ba2 100644
> > --- a/doc/filters.texi
> > +++ b/doc/filters.texi
> > @@ -12214,6 +12214,44 @@ fade=t=in:st=5.5:d=0.5
> >
> >  @end itemize
> >
> > + at section feedback
> > +Apply feedback video filter.
> > +
> > +This filter pass cropped input frames to 2nd output.
> > +From there it can be filtered with other video filters.
> > +After filter receives frame from 2nd input, that frame
> > +is combined on top of original frame from 1st input and passed
> > +to 1st output.
> > +
> > +The typical usage is filter only part of frame.
> > +
> > +The filter accepts the following options:
> > + at table @option
> > + at item x
> > + at item y
> > +Set the top left crop position.
> > +
> > + at item w
> > + at item h
> > +Set the crop size.
> > + at end table
> > +
> > + at subsection Examples
> > +
> > + at itemize
> > + at item
> > +Blur only top left rectangular part of video frame size 100x100 with
> gblur filter.
> > + at example
> >
> +[in][blurin]feedback=x=0:y=0:w=100:h=100[out][blurout];[blurout]gblur=8[blurin]
> > + at end example
> > +
> > + at item
> > +Draw black box on top left part of video frame of size 100x100 with
> drawbox filter.
> > + at example
> >
> +[in][blurin]feedback=x=0:y=0:w=100:h=100[out][blurout];[blurout]drawbox=x=0:y=0:w=100:h=100:t=100[blurin]
> > + at end example
> > + at end itemize
> > +
> >  @section fftdnoiz
> >  Denoise frames using 3D FFT (frequency domain filtering).
> >
> > diff --git a/libavfilter/Makefile b/libavfilter/Makefile
> > index d69bd59bb6..bdfdfdc04a 100644
> > --- a/libavfilter/Makefile
> > +++ b/libavfilter/Makefile
> > @@ -279,6 +279,7 @@ OBJS-$(CONFIG_ESTDIF_FILTER)                 +=
> vf_estdif.o
> >  OBJS-$(CONFIG_EXPOSURE_FILTER)               += vf_exposure.o
> >  OBJS-$(CONFIG_EXTRACTPLANES_FILTER)          += vf_extractplanes.o
> >  OBJS-$(CONFIG_FADE_FILTER)                   += vf_fade.o
> > +OBJS-$(CONFIG_FEEDBACK_FILTER)               += vf_feedback.o
> >  OBJS-$(CONFIG_FFTDNOIZ_FILTER)               += vf_fftdnoiz.o
> >  OBJS-$(CONFIG_FFTFILT_FILTER)                += vf_fftfilt.o
> >  OBJS-$(CONFIG_FIELD_FILTER)                  += vf_field.o
> > diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
> > index abd1fe2367..44fac46521 100644
> > --- a/libavfilter/allfilters.c
> > +++ b/libavfilter/allfilters.c
> > @@ -261,6 +261,7 @@ extern const AVFilter ff_vf_estdif;
> >  extern const AVFilter ff_vf_exposure;
> >  extern const AVFilter ff_vf_extractplanes;
> >  extern const AVFilter ff_vf_fade;
> > +extern const AVFilter ff_vf_feedback;
> >  extern const AVFilter ff_vf_fftdnoiz;
> >  extern const AVFilter ff_vf_fftfilt;
> >  extern const AVFilter ff_vf_field;
> > diff --git a/libavfilter/vf_feedback.c b/libavfilter/vf_feedback.c
> > new file mode 100644
> > index 0000000000..268aff0ebd
> > --- /dev/null
> > +++ b/libavfilter/vf_feedback.c
> > @@ -0,0 +1,312 @@
> > +/*
> > + * 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
> > + * feedback video filter
> > + */
> > +
> > +#include "libavutil/fifo.h"
> > +#include "libavutil/imgutils.h"
> > +#include "libavutil/opt.h"
> > +#include "libavutil/internal.h"
> > +#include "avfilter.h"
> > +#include "filters.h"
> > +#include "internal.h"
> > +#include "video.h"
> > +
> > +typedef struct FeedbackContext {
> > +    const AVClass *class;
> > +
> > +    int x, y;
> > +    int w, h;
> > +
> > +    int max_step[4];
> > +    int hsub, vsub;
> > +
> > +    AVFrame *feed;
> > +
> > +    AVFifo *fifo;
> > +} FeedbackContext;
> > +
> > +static void adjust_pos(AVFilterContext *ctx, FeedbackContext *s)
> > +{
> > +    if (s->x + s->w > ctx->inputs[0]->w)
> > +        s->x = ctx->inputs[0]->w - s->w;
> > +    if (s->y + s->h > ctx->inputs[0]->h)
> > +        s->y = ctx->inputs[0]->h - s->h;
> > +}
> > +
> > +static void adjust_parameters(AVFilterContext *ctx, FeedbackContext *s)
> > +{
> > +    if (s->x >= ctx->inputs[0]->w)
> > +        s->x = 0;
> > +    if (s->y >= ctx->inputs[0]->h)
> > +        s->y = 0;
> > +
> > +    if (s->w <= 0)
> > +        s->w = ctx->inputs[0]->w - s->x;
> > +    if (s->h <= 0)
> > +        s->h = ctx->inputs[0]->h - s->y;
> > +
> > +    if (s->w > ctx->inputs[0]->w)
> > +        s->w = ctx->inputs[0]->w;
> > +    if (s->h > ctx->inputs[0]->h)
> > +        s->h = ctx->inputs[0]->h;
> > +
> > +    adjust_pos(ctx, s);
> > +}
> > +
> > +static int config_input(AVFilterLink *inlink)
> > +{
> > +    AVFilterContext *ctx = inlink->dst;
> > +    const AVPixFmtDescriptor *pix_desc =
> av_pix_fmt_desc_get(inlink->format);
> > +    FeedbackContext *s = ctx->priv;
> > +
> > +    s->hsub = pix_desc->log2_chroma_w;
> > +    s->vsub = pix_desc->log2_chroma_h;
> > +
> > +    av_image_fill_max_pixsteps(s->max_step, NULL, pix_desc);
> > +
> > +    adjust_parameters(ctx, s);
> > +
> > +    ctx->inputs[1]->w = s->w;
> > +    ctx->inputs[1]->h = s->h;
> > +
> > +    return 0;
> > +}
> > +
> > +static int config_output(AVFilterLink *outlink)
> > +{
> > +    AVFilterContext *ctx = outlink->src;
> > +    FeedbackContext *s = ctx->priv;
> > +
> > +    adjust_parameters(ctx, s);
> > +
> > +    ctx->outputs[0]->w = ctx->inputs[0]->w;
> > +    ctx->outputs[0]->h = ctx->inputs[0]->h;
> > +    ctx->outputs[1]->w = s->w;
> > +    ctx->outputs[1]->h = s->h;
> > +
> > +    return 0;
> > +}
> > +
> > +static int query_formats(AVFilterContext *ctx)
> > +{
> > +    return ff_set_common_formats(ctx, ff_formats_pixdesc_filter(0,
> AV_PIX_FMT_FLAG_BITSTREAM |
> > +
>  AV_PIX_FMT_FLAG_HWACCEL |
> > +
>  AV_PIX_FMT_FLAG_PAL));
> > +}
> > +
> > +static int activate(AVFilterContext *ctx)
> > +{
> > +    FeedbackContext *s = ctx->priv;
> > +    int status, ret;
> > +    int64_t pts;
> > +
> > +    adjust_pos(ctx, s);
> > +
> > +    for (int i = 0; i < ctx->nb_outputs; i++)
> > +        FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[i], ctx);
> > +
> > +    if (!s->feed) {
> > +        ret = ff_inlink_consume_frame(ctx->inputs[1], &s->feed);
> > +        if (ret < 0)
> > +            return ret;
> > +    }
> > +
> > +    if (s->feed && av_fifo_can_read(s->fifo)) {
> > +        AVFrame *src = s->feed;
> > +        AVFrame *dst = NULL;
> > +
> > +        av_fifo_read(s->fifo, &dst, 1);
> > +        if (!dst)
> > +            return AVERROR_BUG;
> > +
>
> putting a av_frame_make_writable(dst); here makes the output look quite a
> bit
> less jittery so i think this filter corrupted the inputs frame
>

Fixed locally. Thanks. I always forgot that frames can be read only.


>
> thx
>
>
> [...]
> --
> Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB
>
> Its not that you shouldnt use gotos but rather that you should write
> readable code and code with gotos often but not always is less readable
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel at ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request at ffmpeg.org with subject "unsubscribe".
>


More information about the ffmpeg-devel mailing list