[FFmpeg-devel] [PATCH 3/4] lavfi: add pp filter.

Clément Bœsch ubitux at gmail.com
Fri Dec 21 22:31:15 CET 2012


Ported from MPlayer. Original author is A'rpi, with various
contributions from Michael Niedermayer. The original documentation was
mostly written by Diego Biurrun. See the MPlayer history for full
credits.

The filter is under GPL like the original filter, even if it differs
quite a lot. There is not much point in making it LGPL since pp is under
GPL.
---
 Changelog                 |   1 +
 LICENSE                   |   1 +
 configure                 |   1 +
 doc/filters.texi          | 168 +++++++++++++++++++++++++++++++++++++++++++++
 libavfilter/Makefile      |   1 +
 libavfilter/allfilters.c  |   1 +
 libavfilter/version.h     |   2 +-
 libavfilter/vf_pp.c       | 169 ++++++++++++++++++++++++++++++++++++++++++++++
 tests/lavfi-regression.sh |  12 ++--
 9 files changed, 349 insertions(+), 7 deletions(-)
 create mode 100644 libavfilter/vf_pp.c

diff --git a/Changelog b/Changelog
index 9dcc65e..04e2092 100644
--- a/Changelog
+++ b/Changelog
@@ -47,6 +47,7 @@ version <next>:
 - Silicon Graphics Movie demuxer
 - apad filter
 - adaptive frame-level multithreading for H.264
+- pp (postproc) filter ported from MPlayer
 
 
 version 1.0:
diff --git a/LICENSE b/LICENSE
index a1204f4..d083e56 100644
--- a/LICENSE
+++ b/LICENSE
@@ -34,6 +34,7 @@ Specifically, the GPL parts of FFmpeg are
     - vf_hqdn3d.c
     - vf_hue.c
     - vf_mp.c
+    - vf_pp.c
     - vf_smartblur.c
     - vf_super2xsai.c
     - vf_tinterlace.c
diff --git a/configure b/configure
index da6e511..71b39f0 100755
--- a/configure
+++ b/configure
@@ -1990,6 +1990,7 @@ negate_filter_deps="lut_filter"
 resample_filter_deps="avresample"
 ocv_filter_deps="libopencv"
 pan_filter_deps="swresample"
+pp_filter_deps="gpl postproc"
 removelogo_filter_deps="avcodec avformat swscale"
 scale_filter_deps="swscale"
 smartblur_filter_deps="gpl swscale"
diff --git a/doc/filters.texi b/doc/filters.texi
index 17e2af7..1b4031f 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -3384,6 +3384,174 @@ format=monow, pixdesctest
 
 can be used to test the monowhite pixel format descriptor definition.
 
+ at section pp
+
+Enable the specified chain of postprocessing subfilters using libpostproc. This
+library should be automatically selected with a GPL build (@code{--enable-gpl}).
+Subfilters must be separated by '/' and can be disabled by prepending a '-'.
+Each subfilter and some options have a short and a long name that can be used
+interchangeably, i.e. dr/dering are the same.
+
+All subfilters share common options to determine their scope:
+
+ at table @option
+ at item a/autoq
+Honor the quality commands for this subfilter.
+
+ at item c/chrom
+Do chrominance filtering, too (default).
+
+ at item y/nochrom
+Do luminance filtering only (no chrominance).
+
+ at item n/noluma
+Do chrominance filtering only (no luminance).
+ at end table
+
+These options can be appended after the subfilter name, separated by a ':'.
+
+Available subfilters are:
+
+ at table @option
+ at item hb/hdeblock[:difference[:flatness]]
+Horizontal deblocking filter
+ at table @option
+ at item difference
+Difference factor where higher values mean more deblocking (default: @code{32}).
+ at item flatness
+Flatness threshold where lower values mean more deblocking (default: @code{39}).
+ at end table
+
+ at item vb/vdeblock[:difference[:flatness]]
+Vertical deblocking filter
+ at table @option
+ at item difference
+Difference factor where higher values mean more deblocking (default: @code{32}).
+ at item flatness
+Flatness threshold where lower values mean more deblocking (default: @code{39}).
+ at end table
+
+ at item ha/hadeblock[:difference[:flatness]]
+Accurate horizontal deblocking filter
+ at table @option
+ at item difference
+Difference factor where higher values mean more deblocking (default: @code{32}).
+ at item flatness
+Flatness threshold where lower values mean more deblocking (default: @code{39}).
+ at end table
+
+ at item va/vadeblock[:difference[:flatness]]
+Accurate vertical deblocking filter
+ at table @option
+ at item difference
+Difference factor where higher values mean more deblocking (default: @code{32}).
+ at item flatness
+Flatness threshold where lower values mean more deblocking (default: @code{39}).
+ at end table
+ at end table
+
+The horizontal and vertical deblocking filters share the difference and
+flatness values so you cannot set different horizontal and vertical
+thresholds.
+
+ at table @option
+ at item h1/x1hdeblock
+Experimental horizontal deblocking filter
+
+ at item v1/x1vdeblock
+Experimental vertical deblocking filter
+
+ at item dr/dering
+Deringing filter
+
+ at item tn/tmpnoise[:threshold1[:threshold2[:threshold3]]], temporal noise reducer
+ at table @option
+ at item threshold1
+larger -> stronger filtering
+ at item threshold2
+larger -> stronger filtering
+ at item threshold3
+larger -> stronger filtering
+ at end table
+
+ at item al/autolevels[:f/fullyrange], automatic brightness / contrast correction
+ at table @option
+ at item f/fullyrange
+Stretch luminance to @code{0-255}.
+ at end table
+
+ at item lb/linblenddeint
+Linear blend deinterlacing filter that deinterlaces the given block by
+filtering all lines with a @code{(1 2 1)} filter.
+
+ at item li/linipoldeint
+Linear interpolating deinterlacing filter that deinterlaces the given block by
+linearly interpolating every second line.
+
+ at item ci/cubicipoldeint
+Cubic interpolating deinterlacing filter deinterlaces the given block by
+cubically interpolating every second line.
+
+ at item md/mediandeint
+Median deinterlacing filter that deinterlaces the given block by applying a
+median filter to every second line.
+
+ at item fd/ffmpegdeint
+FFmpeg deinterlacing filter that deinterlaces the given block by filtering every
+second line with a @code{(-1 4 2 4 -1)} filter.
+
+ at item l5/lowpass5
+Vertically applied FIR lowpass deinterlacing filter that deinterlaces the given
+block by filtering all lines with a @code{(-1 2 6 2 -1)} filter.
+
+ at item fq/forceQuant[:quantizer]
+Overrides the quantizer table from the input with the constant quantizer you
+specify.
+ at table @option
+ at item quantizer
+Quantizer to use
+ at end table
+
+ at item de/default
+Default pp filter combination (@code{hb:a,vb:a,dr:a})
+
+ at item fa/fast
+Fast pp filter combination (@code{h1:a,v1:a,dr:a})
+
+ at item ac
+High quality pp filter combination (@code{ha:a:128:7,va:a,dr:a})
+ at end table
+
+ at subsection Examples
+
+ at itemize
+ at item
+Apply horizontal and vertical deblocking, deringing and automatic
+brightness/contrast:
+ at example
+pp=hb/vb/dr/al
+ at end example
+
+ at item
+Apply default filters without brightness/contrast correction:
+ at example
+pp=de/-al
+ at end example
+
+ at item
+Apply default filters and temporal denoiser:
+ at example
+pp=default/tmpnoise:1:2:3
+ at end example
+
+ at item
+Apply deblocking on luminance only, and switch vertical deblocking on or off
+automatically depending on available CPU time:
+ at example
+pp=hb:y/vb:a
+ at end example
+ at end itemize
+
 @section removelogo
 
 Suppress a TV station logo, using an image file to determine which
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 1061444..0e7fa1f 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -123,6 +123,7 @@ OBJS-$(CONFIG_OCV_FILTER)                    += vf_libopencv.o
 OBJS-$(CONFIG_OVERLAY_FILTER)                += vf_overlay.o
 OBJS-$(CONFIG_PAD_FILTER)                    += vf_pad.o
 OBJS-$(CONFIG_PIXDESCTEST_FILTER)            += vf_pixdesctest.o
+OBJS-$(CONFIG_PP_FILTER)                     += vf_pp.o
 OBJS-$(CONFIG_REMOVELOGO_FILTER)             += bbox.o lswsutils.o lavfutils.o vf_removelogo.o
 OBJS-$(CONFIG_SCALE_FILTER)                  += vf_scale.o
 OBJS-$(CONFIG_SELECT_FILTER)                 += f_select.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index fc96809..b842add 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -116,6 +116,7 @@ void avfilter_register_all(void)
     REGISTER_FILTER (OVERLAY,     overlay,     vf);
     REGISTER_FILTER (PAD,         pad,         vf);
     REGISTER_FILTER (PIXDESCTEST, pixdesctest, vf);
+    REGISTER_FILTER (PP,          pp,          vf);
     REGISTER_FILTER (REMOVELOGO,  removelogo,  vf);
     REGISTER_FILTER (SCALE,       scale,       vf);
     REGISTER_FILTER (SELECT,      select,      vf);
diff --git a/libavfilter/version.h b/libavfilter/version.h
index d8061de..a83ddb4 100644
--- a/libavfilter/version.h
+++ b/libavfilter/version.h
@@ -29,7 +29,7 @@
 #include "libavutil/avutil.h"
 
 #define LIBAVFILTER_VERSION_MAJOR  3
-#define LIBAVFILTER_VERSION_MINOR  29
+#define LIBAVFILTER_VERSION_MINOR  30
 #define LIBAVFILTER_VERSION_MICRO 100
 
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
diff --git a/libavfilter/vf_pp.c b/libavfilter/vf_pp.c
new file mode 100644
index 0000000..7db2508
--- /dev/null
+++ b/libavfilter/vf_pp.c
@@ -0,0 +1,169 @@
+/*
+ * Copyright (c) 2002 A'rpi
+ * Copyright (C) 2012 Clément Bœsch
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU 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
+ * libpostproc filter, ported from MPlayer.
+ */
+
+#include "libavutil/avassert.h"
+#include "libavutil/opt.h"
+#include "internal.h"
+
+#include "libpostproc/postprocess.h"
+
+typedef struct {
+    int mode_id;
+    pp_mode *modes[PP_QUALITY_MAX + 1];
+    void *pp_ctx;
+} PPFilterContext;
+
+static av_cold int pp_init(AVFilterContext *ctx, const char *args)
+{
+    int i;
+    PPFilterContext *pp = ctx->priv;
+
+    if (!args || !*args)
+        args = "de";
+
+    for (i = 0; i <= PP_QUALITY_MAX; i++) {
+        pp->modes[i] = pp_get_mode_by_name_and_quality(args, i);
+        if (!pp->modes[i])
+            return AVERROR_EXTERNAL;
+    }
+    pp->mode_id = PP_QUALITY_MAX;
+    return 0;
+}
+
+static int pp_process_command(AVFilterContext *ctx, const char *cmd, const char *args,
+                              char *res, int res_len, int flags)
+{
+    PPFilterContext *pp = ctx->priv;
+
+    if (!strcmp(cmd, "quality")) {
+        pp->mode_id = av_clip(strtol(args, NULL, 10), 0, PP_QUALITY_MAX);
+        return 0;
+    }
+    return AVERROR(ENOSYS);
+}
+
+static int pp_query_formats(AVFilterContext *ctx)
+{
+    static const enum PixelFormat pix_fmts[] = {
+        AV_PIX_FMT_YUV420P,
+        AV_PIX_FMT_YUV422P,
+        AV_PIX_FMT_YUV411P,
+        AV_PIX_FMT_YUV444P,
+        AV_PIX_FMT_NONE
+    };
+    ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
+    return 0;
+}
+
+static int pp_config_props(AVFilterLink *inlink)
+{
+    int flags = PP_CPU_CAPS_AUTO;
+    PPFilterContext *pp = inlink->dst->priv;
+
+    switch (inlink->format) {
+    case AV_PIX_FMT_YUV420P: flags |= PP_FORMAT_420; break;
+    case AV_PIX_FMT_YUV422P: flags |= PP_FORMAT_422; break;
+    case AV_PIX_FMT_YUV411P: flags |= PP_FORMAT_411; break;
+    case AV_PIX_FMT_YUV444P: flags |= PP_FORMAT_444; break;
+    default: av_assert0(0);
+    }
+
+    pp->pp_ctx = pp_get_context(inlink->w, inlink->h, flags);
+    if (!pp->pp_ctx)
+        return AVERROR(ENOMEM);
+    return 0;
+}
+
+static int pp_filter_frame(AVFilterLink *inlink, AVFilterBufferRef *inbuf)
+{
+    AVFilterContext *ctx = inlink->dst;
+    PPFilterContext *pp = ctx->priv;
+    AVFilterLink *outlink = ctx->outputs[0];
+    const int aligned_w = FFALIGN(outlink->w, 8);
+    const int aligned_h = FFALIGN(outlink->h, 8);
+    AVFilterBufferRef *outbuf;
+
+    outbuf = ff_get_video_buffer(outlink, AV_PERM_WRITE, aligned_w, aligned_h);
+    if (!outbuf) {
+        avfilter_unref_buffer(inbuf);
+        return AVERROR(ENOMEM);
+    }
+    avfilter_copy_buffer_ref_props(outbuf, inbuf);
+
+    pp_postprocess((const uint8_t **)inbuf->data, inbuf->linesize,
+                   outbuf->data,                 outbuf->linesize,
+                   aligned_w, outlink->h,
+                   outbuf->video->qp_table,
+                   outbuf->video->qp_table_linesize,
+                   pp->modes[pp->mode_id],
+                   pp->pp_ctx,
+                   outbuf->video->pict_type);
+
+    avfilter_unref_buffer(inbuf);
+    return ff_filter_frame(outlink, outbuf);
+}
+
+static av_cold void pp_uninit(AVFilterContext *ctx)
+{
+    int i;
+    PPFilterContext *pp = ctx->priv;
+
+    for (i = 0; i <= PP_QUALITY_MAX; i++)
+        pp_free_mode(pp->modes[i]);
+    if (pp->pp_ctx)
+        pp_free_context(pp->pp_ctx);
+}
+
+static const AVFilterPad pp_inputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_VIDEO,
+        .config_props = pp_config_props,
+        .filter_frame = pp_filter_frame,
+        .min_perms    = AV_PERM_READ,
+    },
+    { NULL }
+};
+
+static const AVFilterPad pp_outputs[] = {
+    {
+        .name = "default",
+        .type = AVMEDIA_TYPE_VIDEO,
+    },
+    { NULL }
+};
+
+AVFilter avfilter_vf_pp = {
+    .name            = "pp",
+    .description     = NULL_IF_CONFIG_SMALL("Filter video using libpostproc."),
+    .priv_size       = sizeof(PPFilterContext),
+    .init            = pp_init,
+    .uninit          = pp_uninit,
+    .query_formats   = pp_query_formats,
+    .inputs          = pp_inputs,
+    .outputs         = pp_outputs,
+    .process_command = pp_process_command,
+};
diff --git a/tests/lavfi-regression.sh b/tests/lavfi-regression.sh
index 6899799..6278f0a 100755
--- a/tests/lavfi-regression.sh
+++ b/tests/lavfi-regression.sh
@@ -49,12 +49,12 @@ do_lavfi "idet"               "idet"
 do_lavfi "null"               "null"
 do_lavfi "overlay"            "split[m],scale=88:72,pad=96:80:4:4[o2];[m]fifo[o1],[o1][o2]overlay=240:16"
 do_lavfi "pad"                "pad=iw*1.5:ih*1.5:iw*0.3:ih*0.2"
-do_lavfi "pp"                 "mp=pp=be/hb/vb/tn/l5/al"
-do_lavfi "pp2"                "mp=pp=be/fq:16/h1/v1/lb"
-do_lavfi "pp3"                "mp=pp=be/fq:8/ha:128:7/va/li"
-do_lavfi "pp4"                "mp=pp=be/ci"
-do_lavfi "pp5"                "mp=pp=md"
-do_lavfi "pp6"                "mp=pp=be/fd"
+do_lavfi "pp"                 "pp=be/hb/vb/tn/l5/al"
+do_lavfi "pp2"                "pp=be/fq:16/h1/v1/lb"
+do_lavfi "pp3"                "pp=be/fq:8/ha:128:7/va/li"
+do_lavfi "pp4"                "pp=be/ci"
+do_lavfi "pp5"                "pp=md"
+do_lavfi "pp6"                "pp=be/fd"
 do_lavfi "scale200"           "scale=200:200"
 do_lavfi "scale500"           "scale=500:500"
 do_lavfi "select"             "select=not(eq(mod(n\,2)\,0)+eq(mod(n\,3)\,0))"
-- 
1.8.0.2



More information about the ffmpeg-devel mailing list