[FFmpeg-devel] [PATCH] lavfi/hue: factorize setting options logic

Stefano Sabatini stefasab at gmail.com
Mon Sep 3 14:45:04 CEST 2012


---
 libavfilter/vf_hue.c |   85 +++++++++++++++++++++++---------------------------
 1 files changed, 39 insertions(+), 46 deletions(-)

diff --git a/libavfilter/vf_hue.c b/libavfilter/vf_hue.c
index dd36d8e..a6522cc 100644
--- a/libavfilter/vf_hue.c
+++ b/libavfilter/vf_hue.c
@@ -35,9 +35,6 @@
 #include "internal.h"
 #include "video.h"
 
-#define HUE_DEFAULT_VAL 0
-#define SAT_DEFAULT_VAL 1
-
 typedef struct {
     const    AVClass *class;
     float    hue_deg; /* hue expressed in degrees */
@@ -53,11 +50,11 @@ typedef struct {
 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
 static const AVOption hue_options[] = {
     { "h", "set the hue angle degrees", OFFSET(hue_deg), AV_OPT_TYPE_FLOAT,
-      { -FLT_MAX }, -FLT_MAX, FLT_MAX, FLAGS },
+      { 0 }, -FLT_MAX, FLT_MAX, FLAGS },
     { "H", "set the hue angle radians", OFFSET(hue), AV_OPT_TYPE_FLOAT,
-      { -FLT_MAX }, -FLT_MAX, FLT_MAX, FLAGS },
+      { 0 }, -FLT_MAX, FLT_MAX, FLAGS },
     { "s", "set the saturation value", OFFSET(saturation), AV_OPT_TYPE_FLOAT,
-      { SAT_DEFAULT_VAL }, -10, 10, FLAGS },
+      { 1 }, -10, 10, FLAGS },
     { NULL }
 };
 
@@ -70,11 +67,17 @@ static inline int set_options(AVFilterContext *ctx, const char *args)
     char c1 = 0, c2 = 0;
     char *equal;
 
+    float hue_old        = hue->hue;
+    float hue_deg_old    = hue->hue_deg;
+    float saturation_old = hue->saturation;
+
+    hue->hue        = -FLT_MAX;
+    hue->hue_deg    = -FLT_MAX;
+    hue->saturation = -FLT_MAX;
+
     if (args) {
         /* named options syntax */
         if (equal = strchr(args, '=')) {
-            hue->hue     = -FLT_MAX;
-            hue->hue_deg = -FLT_MAX;
 
             if ((ret = av_set_options_string(hue, args, "=", ":")) < 0)
                 return ret;
@@ -103,31 +106,41 @@ static inline int set_options(AVFilterContext *ctx, const char *args)
         }
     }
 
+    /* set hue and hue_deg interdipendent values */
+     if (hue->hue_deg != -FLT_MAX)
+        hue->hue = hue->hue_deg * M_PI / 180;
+     if (hue->hue != -FLT_MAX)
+         hue->hue_deg = hue->hue*180/M_PI;
+
+    if (hue->hue == -FLT_MAX)
+        hue->hue = hue_old;
+    if (hue->hue_deg == -FLT_MAX)
+        hue->hue_deg = hue_deg_old;
+    if (hue->saturation == -FLT_MAX)
+        hue->saturation = saturation_old;
+
+    /*
+     * Scale the value to the norm of the resulting (U,V) vector, that is
+     * the saturation.
+     * This will be useful in the process_chrominance function.
+     */
+    hue->hue_sin = rint(sin(hue->hue) * (1 << 16) * hue->saturation);
+    hue->hue_cos = rint(cos(hue->hue) * (1 << 16) * hue->saturation);
+
+    av_log(ctx, AV_LOG_VERBOSE, "hue:%f*PI hue_deg:%f saturation:%f\n",
+           hue->hue/M_PI, hue->hue_deg, hue->saturation);
+
     return 0;
 }
 
 static av_cold int init(AVFilterContext *ctx, const char *args)
 {
     HueContext *hue = ctx->priv;
-    int ret;
 
     hue->class = &hue_class;
     av_opt_set_defaults(hue);
 
-    if ((ret = set_options(ctx, args)) < 0)
-        return ret;
-
-    if (hue->saturation == -FLT_MAX)
-        hue->hue = SAT_DEFAULT_VAL;
-    if (hue->hue == -FLT_MAX)
-        hue->hue = HUE_DEFAULT_VAL;
-    if (hue->hue_deg != -FLT_MAX)
-        /* Convert angle from degrees to radians */
-        hue->hue = hue->hue_deg * M_PI / 180;
-
-    av_log(ctx, AV_LOG_VERBOSE, "hue:%f*PI hue_deg:%f saturation:%f\n",
-           hue->hue/M_PI, hue->hue*180/M_PI, hue->saturation);
-    return 0;
+    return set_options(ctx, args);
 }
 
 static av_cold void uninit(AVFilterContext *ctx)
@@ -159,13 +172,6 @@ static int config_props(AVFilterLink *inlink)
 
     hue->hsub = desc->log2_chroma_w;
     hue->vsub = desc->log2_chroma_h;
-    /*
-     * Scale the value to the norm of the resulting (U,V) vector, that is
-     * the saturation.
-     * This will be useful in the process_chrominance function.
-     */
-    hue->hue_sin = rint(sin(hue->hue) * (1 << 16) * hue->saturation);
-    hue->hue_cos = rint(cos(hue->hue) * (1 << 16) * hue->saturation);
 
     return 0;
 }
@@ -240,23 +246,10 @@ static int draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
                            char *res, int res_len, int flags)
 {
-    HueContext *hue = ctx->priv;
-    int ret;
-
-    if (!strcmp(cmd, "reinit")) {
-        if ((ret = set_options(ctx, args)) < 0)
-            return ret;
-
-        if (hue->hue_deg != -FLT_MAX)
-            /* Convert angle from degrees to radians */
-            hue->hue = hue->hue_deg * M_PI / 180;
-
-        hue->hue_sin = rint(sin(hue->hue) * (1 << 16) * hue->saturation);
-        hue->hue_cos = rint(cos(hue->hue) * (1 << 16) * hue->saturation);
-    } else
+    if (!strcmp(cmd, "reinit"))
+        return set_options(ctx, args);
+    else
         return AVERROR(ENOSYS);
-
-    return 0;
 }
 
 AVFilter avfilter_vf_hue = {
-- 
1.7.5.4



More information about the ffmpeg-devel mailing list