[FFmpeg-devel] [PATCH] vf_unsharp: extend unsharp syntax, for supporting named options

Stefano Sabatini stefasab at gmail.com
Mon Feb 6 15:42:43 CET 2012


From: Stefano Sabatini <stefano.sabatini-lala at poste.it>

Make setting params less awkward.

The old syntax is kept for backward compatibility.
---
 doc/filters.texi         |    9 ++++++-
 libavfilter/vf_unsharp.c |   60 +++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 67 insertions(+), 2 deletions(-)

diff --git a/doc/filters.texi b/doc/filters.texi
index bc64e1c..8f2dc11 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -2661,9 +2661,14 @@ Sharpen or blur the input video.
 It accepts the following parameters:
 @var{luma_msize_x}:@var{luma_msize_y}:@var{luma_amount}:@var{chroma_msize_x}:@var{chroma_msize_y}:@var{chroma_amount}
 
+Alternatively it is possible to specify separately the luma or chroma
+component through a string of the form:
+"@var{msize_x}x at var{msize_y}[-+x]@var{amount} using the
+ at var{luma}/@var{l} and @var{chroma}/@var{c} options.
+
 Negative values for the amount will blur the input video, while positive
 values will sharpen. All parameters are optional and default to the
-equivalent of the string '5:5:1.0:5:5:0.0'.
+equivalent of the string 'l=5x5+1:c=5x5+0.0'.
 
 @table @option
 
@@ -2696,9 +2701,11 @@ and 5.0, default value is 0.0.
 @example
 # Strong luma sharpen effect parameters
 unsharp=7:7:2.5
+unsharp=l=7x7+2.5
 
 # Strong blur of both luma and chroma parameters
 unsharp=7:7:-2:7:7:-2
+unsharp=l=7x7-2:c=7x7-2
 
 # Use the default values with @command{ffmpeg}
 ffmpeg -i in.avi -vf "unsharp" out.mp4
diff --git a/libavfilter/vf_unsharp.c b/libavfilter/vf_unsharp.c
index 57ec5d9..9435fdd 100644
--- a/libavfilter/vf_unsharp.c
+++ b/libavfilter/vf_unsharp.c
@@ -39,6 +39,7 @@
 #include "avfilter.h"
 #include "libavutil/common.h"
 #include "libavutil/mem.h"
+#include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 
 #define MIN_MATRIX_SIZE 3
@@ -59,11 +60,34 @@ typedef struct FilterParam {
 } FilterParam;
 
 typedef struct {
+    const AVClass *class;
     FilterParam luma;   ///< luma parameters (width, height, amount)
     FilterParam chroma; ///< chroma parameters (width, height, amount)
     int hsub, vsub;
+    char *luma_str, *chroma_str;
 } UnsharpContext;
 
+#define OFFSET(x) offsetof(UnsharpContext, x)
+
+static const AVOption unsharp_options[] = {
+    {"luma",   "set luma parameters",        OFFSET(luma_str),   FF_OPT_TYPE_STRING, {.str="5x5+1"}, CHAR_MIN, CHAR_MAX },
+    {"l",      "set luma parameters",        OFFSET(luma_str),   FF_OPT_TYPE_STRING, {.str="5x5+1"}, CHAR_MIN, CHAR_MAX },
+    {"chroma", "set chroma parameters file", OFFSET(chroma_str), FF_OPT_TYPE_STRING, {.str="5x5+0"},    CHAR_MIN, CHAR_MAX },
+    {"c",      "set chroma parameters file", OFFSET(chroma_str), FF_OPT_TYPE_STRING, {.str="5x5+0"},    CHAR_MIN, CHAR_MAX },
+    {NULL}
+};
+
+static const char *unsharp_get_name(void *ctx)
+{
+    return "unsharp";
+}
+
+static const AVClass unsharp_class = {
+    "UnsharpContext",
+    unsharp_get_name,
+    unsharp_options
+};
+
 static void apply_unsharp(      uint8_t *dst, int dst_stride,
                           const uint8_t *src, int src_stride,
                           int width, int height, FilterParam *fp)
@@ -129,17 +153,48 @@ static void set_filter_param(FilterParam *fp, int msize_x, int msize_y, double a
     fp->halfscale = 1 << (fp->scalebits - 1);
 }
 
+static int parse_component_params(int *msize_x, int *msize_y, double *amount,
+                                 char *params, void *log_ctx)
+{
+    int n;
+    char sign;
+
+    n = sscanf(params, "%dx%d%c%lf", msize_x, msize_y, &sign, amount);
+    if (n != 4 || (sign != '+' && sign != '-' && sign != 'x')) {
+        av_log(log_ctx, AV_LOG_ERROR,
+               "Invalid component parameters '%s', must be of the form "
+               "'XxY[x+-]A'\n", params);
+        return AVERROR(EINVAL);
+    }
+    if (sign == '-')
+        *amount *= -1;
+
+    return 0;
+}
+
 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
 {
     UnsharpContext *unsharp = ctx->priv;
     int lmsize_x = 5, cmsize_x = 5;
     int lmsize_y = 5, cmsize_y = 5;
     double lamount = 1.0f, camount = 0.0f;
-    int val;
+    int ret, val;
+
+    unsharp->class = &unsharp_class;
+    av_opt_set_defaults(unsharp);
 
     if (args)
+        ret =
         sscanf(args, "%d:%d:%lf:%d:%d:%lf", &lmsize_x, &lmsize_y, &lamount,
                                             &cmsize_x, &cmsize_y, &camount);
+    if (!ret) {
+        if ((ret = av_set_options_string(unsharp, args, "=", ":")) < 0 ||
+            (ret = parse_component_params(&lmsize_x, &lmsize_y, &lamount,
+                                          unsharp->luma_str, ctx)) < 0 ||
+            (ret = parse_component_params(&cmsize_x, &cmsize_y, &camount,
+                                          unsharp->chroma_str, ctx)) < 0)
+            return ret;
+    }
 
 #define CHECK_SIZE(lc, xy, lc_str)                                      \
     val = lc##msize_##xy;                                               \
@@ -214,6 +269,9 @@ static av_cold void uninit(AVFilterContext *ctx)
 {
     UnsharpContext *unsharp = ctx->priv;
 
+    av_freep(&unsharp->luma_str);
+    av_freep(&unsharp->chroma_str);
+
     free_filter_param(&unsharp->luma);
     free_filter_param(&unsharp->chroma);
 }
-- 
1.7.5.4



More information about the ffmpeg-devel mailing list