On Sat, Jan 31, 2015 at 3:55 PM, Stefano Sabatini <stefa...@gmail.com>
wrote:

> On date Friday 2015-01-30 23:17:33 +0530, Arwa Arif encoded:
> > I have tried to add process_command in vf_eq.c. I have attached the
> patch.
>
> > From 1d65e493a8eb247d86b0db324cb740579662706d Mon Sep 17 00:00:00 2001
> > From: Arwa Arif <arwaarif1...@gmail.com>
> > Date: Fri, 30 Jan 2015 23:06:50 +0530
> > Subject: [PATCH] Add support to process_command in vf_eq.c
> >
> > ---
> >  libavfilter/vf_eq.c |   53
> ++++++++++++++++++++++++++++++++++++++-------------
> >  1 file changed, 40 insertions(+), 13 deletions(-)
>
> Missing doc updates.
>
> >
> > diff --git a/libavfilter/vf_eq.c b/libavfilter/vf_eq.c
> > index ad2a37e..5899abb 100644
> > --- a/libavfilter/vf_eq.c
> > +++ b/libavfilter/vf_eq.c
> > @@ -27,11 +27,6 @@
> >   * very simple video equalizer
> >   */
> >
> > -/**
> > - * TODO:
> > - * - Add support to process_command
> > - */
> > -
> >  #include "libavfilter/internal.h"
> >  #include "libavutil/common.h"
> >  #include "libavutil/imgutils.h"
> > @@ -217,6 +212,37 @@ static int filter_frame(AVFilterLink *inlink,
> AVFrame *in)
> >      av_frame_free(&in);
> >      return ff_filter_frame(outlink, out);
> >  }
> > +
> > +static int process_command(AVFilterContext *ctx, const char *cmd, const
> char *args,
> > +                           char *res, int res_len, int flags)
> > +{
> > +    EQContext *eq = ctx->priv;
> > +
> > +    if (!strcmp(cmd, "contrast"))
>
> > +        eq->contrast     = av_clipf(strtod(args, NULL), -2.0,  2.0);
>
> All this parameters are set through the av_opt_ interface. Try to use
> it, since otherwise range checks and parsing is inconsistent. In
> particular, the parsing performed by av_opt_ may be different from
> strtod(). Also you want to want the user in case of out-of-range values.
>
> > +    if (!strcmp(cmd, "brightness"))
> > +        eq->brightness   = av_clipf(strtod(args, NULL), -1.0,  1.0);
> > +    if (!strcmp(cmd, "saturation"))
> > +        eq->saturation   = av_clipf(strtod(args, NULL),  0.0,  3.0);
> > +    if (!strcmp(cmd, "gamma"))
> > +        eq->gamma        = av_clipf(strtod(args, NULL),  0.1, 10.0);
> > +    if (!strcmp(cmd, "gamma_r"))
> > +        eq->gamma_r      = av_clipf(strtod(args, NULL),  0.1, 10.0);
> > +    if (!strcmp(cmd, "gamma_g"))
> > +        eq->gamma_g      = av_clipf(strtod(args, NULL),  0.1, 10.0);
> > +    if (!strcmp(cmd, "gamma_b"))
> > +        eq->gamma_b      = av_clipf(strtod(args, NULL),  0.1, 10.0);
> > +    if (!strcmp(cmd, "gamma_weight"))
> > +        eq->gamma_weight = av_clipf(strtod(args, NULL),  0.0,  1.0);
> > +
> > +    set_gamma(eq);
> > +    set_contrast(eq);
> > +    set_brightness(eq);
> > +    set_saturation(eq);
> > +
> > +    return 0;
> > +}
>
> if (!strcmp(cmd, "contrast) {
>    set contrast
>    set_contrast(eq);
> } else if (...)
>
> } else...
>
> This wauy you don't need to set the other parameters if they are not
> changed.
>
>
> Also it should return AVERROR(ENOSYS) in case of unrecognized command.
>
> > +
> >  static const AVFilterPad eq_inputs[] = {
> >      {
> >          .name = "default",
> > @@ -260,12 +286,13 @@ static const AVOption eq_options[] = {
> >  AVFILTER_DEFINE_CLASS(eq);
> >
> >  AVFilter ff_vf_eq = {
> > -    .name          = "eq",
> > -    .description   = NULL_IF_CONFIG_SMALL("Adjust brightness, contrast,
> gamma, and saturation."),
> > -    .priv_size     = sizeof(EQContext),
> > -    .priv_class    = &eq_class,
> > -    .inputs        = eq_inputs,
> > -    .outputs       = eq_outputs,
> > -    .query_formats = query_formats,
> > -    .init          = initialize,
>
> > +    .name            = "eq",
> > +    .description     = NULL_IF_CONFIG_SMALL("Adjust brightness,
> contrast, gamma, and saturation."),
> > +    .priv_size       = sizeof(EQContext),
> > +    .priv_class      = &eq_class,
> > +    .inputs          = eq_inputs,
> > +    .outputs         = eq_outputs,
> > +    .process_command = process_command,
> > +    .query_formats   = query_formats,
> > +    .init            = initialize,
>
> avoid cosmetic changes, you can vertically realign in a separate
> (cosmetical) commit.
> --
> FFmpeg = Fostering Faithless Mournful Puristic Ecletic Generator
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>


I have looked at various codes which use process_command and tried to
update the patch accordingly.
From 656071669ed14f8b273cd288526702406c6b22f9 Mon Sep 17 00:00:00 2001
From: Arwa Arif <arwaarif1...@gmail.com>
Date: Thu, 19 Feb 2015 01:26:44 +0530
Subject: [PATCH] Add process_command to eq.

---
 doc/filters.texi    |   35 +++++++++++
 libavfilter/vf_eq.c |  160 ++++++++++++++++++++++++++++++++++++---------------
 libavfilter/vf_eq.h |   56 ++++++++++++++++--
 3 files changed, 199 insertions(+), 52 deletions(-)

diff --git a/doc/filters.texi b/doc/filters.texi
index 191b52f..e5bf3a2 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -4402,6 +4402,41 @@ Default is @code{1.0}.
 
 @end table
 
+@subsection Commands
+The filter supports the following commands:
+
+@table @option
+@item contrast
+Set the contrast expression.
+
+@item brightness
+Set the brightness expression.
+
+@item saturation
+Set the saturation expression.
+
+@item gamma
+Set the gamma expression.
+
+@item gamma_r
+Set the gamma_r expression.
+
+@item gamma_g
+Set gamma_g expression.
+
+@item gamma_b
+Set gamma_b expression.
+
+@item gamma_weight
+Set gamma_weight expression.
+
+The command accepts the same syntax of the corresponding option.
+
+If the specified expression is not valid, it is kept at its current
+value.
+
+@end table
+
 @section extractplanes
 
 Extract color channel components from input video stream into
diff --git a/libavfilter/vf_eq.c b/libavfilter/vf_eq.c
index 5899abb..2b957d3 100644
--- a/libavfilter/vf_eq.c
+++ b/libavfilter/vf_eq.c
@@ -106,14 +106,16 @@ static void check_values(EQParameters *param, EQContext *eq)
 
 static void set_contrast(EQContext *eq)
 {
-    eq->param[0].contrast = eq->contrast;
+    eq->var_values[VAR_CONTRAST] = av_clipf(av_strtod(eq->contrast_expr, NULL), -2.0, 2.0);
+    eq->param[0].contrast = eq->var_values[VAR_CONTRAST];
     eq->param[0].lut_clean = 0;
     check_values(&eq->param[0], eq);
 }
 
 static void set_brightness(EQContext *eq)
 {
-    eq->param[0].brightness = eq->brightness;
+    eq->var_values[VAR_BRIGHTNESS] =  av_clipf(av_strtod(eq->brightness_expr, NULL), -1.0, 1.0);
+    eq->param[0].brightness = eq->var_values[VAR_BRIGHTNESS];
     eq->param[0].lut_clean = 0;
     check_values(&eq->param[0], eq);
 }
@@ -121,12 +123,19 @@ static void set_brightness(EQContext *eq)
 static void set_gamma(EQContext *eq)
 {
     int i;
-    eq->param[0].gamma = eq->gamma * eq->gamma_g;
-    eq->param[1].gamma = sqrt(eq->gamma_b / eq->gamma_g);
-    eq->param[2].gamma = sqrt(eq->gamma_r / eq->gamma_g);
+
+    eq->var_values[VAR_GAMMA]        =  av_clipf(av_strtod(eq->gamma_expr,        NULL),  0.1, 10.0);
+    eq->var_values[VAR_GAMMA_R]      =  av_clipf(av_strtod(eq->gamma_r_expr,      NULL),  0.1, 10.0);
+    eq->var_values[VAR_GAMMA_G]      =  av_clipf(av_strtod(eq->gamma_g_expr,      NULL),  0.1, 10.0);
+    eq->var_values[VAR_GAMMA_B]      =  av_clipf(av_strtod(eq->gamma_b_expr,      NULL),  0.1, 10.0);
+    eq->var_values[VAR_GAMMA_WEIGHT] =  av_clipf(av_strtod(eq->gamma_weight_expr, NULL),  0.0,  1.0);
+
+    eq->param[0].gamma = eq->var_values[VAR_GAMMA] * eq->var_values[VAR_GAMMA_G];
+    eq->param[1].gamma = sqrt(eq->var_values[VAR_GAMMA_B] / eq->var_values[VAR_GAMMA_G]);
+    eq->param[2].gamma = sqrt(eq->var_values[VAR_GAMMA_R] / eq->var_values[VAR_GAMMA_G]);
 
     for (i = 0; i < 3; i++) {
-        eq->param[i].gamma_weight = eq->gamma_weight;
+        eq->param[i].gamma_weight = eq->var_values[VAR_GAMMA_WEIGHT];
         eq->param[i].lut_clean = 0;
         check_values(&eq->param[i], eq);
     }
@@ -135,8 +144,11 @@ static void set_gamma(EQContext *eq)
 static void set_saturation(EQContext *eq)
 {
     int i;
+
+    eq->var_values[VAR_SATURATION] = av_clipf(av_strtod(eq->saturation_expr, NULL), 0.0, 3.0);
+
     for (i = 1; i < 3; i++) {
-        eq->param[i].contrast = eq->saturation;
+        eq->param[i].contrast = eq->var_values[VAR_SATURATION];
         eq->param[i].lut_clean = 0;
         check_values(&eq->param[i], eq);
     }
@@ -159,6 +171,20 @@ static int initialize(AVFilterContext *ctx)
     return 0;
 }
 
+static void uninit(AVFilterContext *ctx)
+{
+    EQContext *eq = ctx->priv;
+
+    av_expr_free(eq->contrast_pexpr);     eq->contrast_pexpr     = NULL;
+    av_expr_free(eq->brightness_pexpr);   eq->brightness_pexpr   = NULL;
+    av_expr_free(eq->saturation_pexpr);   eq->saturation_pexpr   = NULL;
+    av_expr_free(eq->gamma_pexpr);        eq->gamma_pexpr        = NULL;
+    av_expr_free(eq->gamma_weight_pexpr); eq->gamma_weight_pexpr = NULL;
+    av_expr_free(eq->gamma_r_pexpr);      eq->gamma_r_pexpr      = NULL;
+    av_expr_free(eq->gamma_g_pexpr);      eq->gamma_g_pexpr      = NULL;
+    av_expr_free(eq->gamma_b_pexpr);      eq->gamma_b_pexpr      = NULL;
+}
+
 static int query_formats(AVFilterContext *ctx)
 {
     static const enum AVPixelFormat pixel_fmts_eq[] = {
@@ -176,6 +202,27 @@ static int query_formats(AVFilterContext *ctx)
     return 0;
 }
 
+static int set_expr(AVExpr **pexpr, const char *expr, const char *option, void *log_ctx)
+{
+    int ret;
+    AVExpr *old = NULL;
+
+    if (*pexpr)
+        old = *pexpr;
+    ret = av_expr_parse(pexpr, expr, var_names,
+                        NULL, NULL, NULL, NULL, 0, log_ctx);
+    if (ret < 0) {
+        av_log(log_ctx, AV_LOG_ERROR,
+               "Error when evaluating the expression '%s' for %s\n",
+               expr, option);
+        *pexpr = old;
+        return ret;
+    }
+
+    av_expr_free(old);
+    return 0;
+}
+
 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
 {
     AVFilterContext *ctx = inlink->dst;
@@ -217,30 +264,50 @@ static int process_command(AVFilterContext *ctx, const char *cmd, const char *ar
                            char *res, int res_len, int flags)
 {
     EQContext *eq = ctx->priv;
+    int ret;
 
-    if (!strcmp(cmd, "contrast"))
-        eq->contrast     = av_clipf(strtod(args, NULL), -2.0,  2.0);
-    if (!strcmp(cmd, "brightness"))
-        eq->brightness   = av_clipf(strtod(args, NULL), -1.0,  1.0);
-    if (!strcmp(cmd, "saturation"))
-        eq->saturation   = av_clipf(strtod(args, NULL),  0.0,  3.0);
-    if (!strcmp(cmd, "gamma"))
-        eq->gamma        = av_clipf(strtod(args, NULL),  0.1, 10.0);
-    if (!strcmp(cmd, "gamma_r"))
-        eq->gamma_r      = av_clipf(strtod(args, NULL),  0.1, 10.0);
-    if (!strcmp(cmd, "gamma_g"))
-        eq->gamma_g      = av_clipf(strtod(args, NULL),  0.1, 10.0);
-    if (!strcmp(cmd, "gamma_b"))
-        eq->gamma_b      = av_clipf(strtod(args, NULL),  0.1, 10.0);
-    if (!strcmp(cmd, "gamma_weight"))
-        eq->gamma_weight = av_clipf(strtod(args, NULL),  0.0,  1.0);
-
-    set_gamma(eq);
-    set_contrast(eq);
-    set_brightness(eq);
-    set_saturation(eq);
-
-    return 0;
+    if (!strcmp(cmd, "contrast")) {
+        ret = set_expr(&eq->contrast_pexpr, args, cmd, ctx);
+        set_contrast(eq);
+        return ret;
+    }
+    else if (!strcmp(cmd, "brightness")) {
+        ret = set_expr(&eq->brightness_pexpr, args, cmd, ctx);
+        set_brightness(eq);
+        return ret;
+    }
+    else if (!strcmp(cmd, "saturation")) {
+        ret = set_expr(&eq->saturation_pexpr, args, cmd, ctx);
+        set_saturation(eq);
+        return ret;
+    }
+    else if (!strcmp(cmd, "gamma")) {
+        ret = set_expr(&eq->gamma_pexpr, args, cmd, ctx);
+        set_gamma(eq);
+        return ret;
+    }
+    else if (!strcmp(cmd, "gamma_r")) {
+        ret = set_expr(&eq->gamma_r_pexpr, args, cmd, ctx);
+        set_gamma(eq);
+        return ret;
+    }
+    else if (!strcmp(cmd, "gamma_g")) {
+        ret = set_expr(&eq->gamma_g_pexpr, args, cmd, ctx);
+        set_gamma(eq);
+        return ret;
+    }
+    else if (!strcmp(cmd, "gamma_b")) {
+        ret = set_expr(&eq->gamma_b_pexpr, args, cmd, ctx);
+        set_gamma(eq);
+        return ret;
+    }
+    else if (!strcmp(cmd, "gamma_weight")) {
+        ret = set_expr(&eq->gamma_weight_pexpr, args, cmd, ctx);
+        set_gamma(eq);
+        return ret;
+    }
+    else
+        return AVERROR(ENOSYS);
 }
 
 static const AVFilterPad eq_inputs[] = {
@@ -265,34 +332,35 @@ static const AVFilterPad eq_outputs[] = {
 
 static const AVOption eq_options[] = {
     { "contrast",     "set the contrast adjustment, negative values give a negative image",
-        OFFSET(contrast),     AV_OPT_TYPE_DOUBLE, {.dbl = 1.0}, -2.0, 2.0, FLAGS },
+        OFFSET(contrast_expr),     AV_OPT_TYPE_STRING, {.str = "1.0"}, CHAR_MIN, CHAR_MAX, FLAGS },
     { "brightness",   "set the brightness adjustment",
-        OFFSET(brightness),   AV_OPT_TYPE_DOUBLE, {.dbl = 0.0}, -1.0, 1.0, FLAGS },
+        OFFSET(brightness_expr),   AV_OPT_TYPE_STRING, {.str = "0.0"}, CHAR_MIN, CHAR_MAX, FLAGS },
     { "saturation",   "set the saturation adjustment",
-        OFFSET(saturation),   AV_OPT_TYPE_DOUBLE, {.dbl = 1.0},  0.0, 3.0, FLAGS },
+        OFFSET(saturation_expr),   AV_OPT_TYPE_STRING, {.str = "1.0"}, CHAR_MIN, CHAR_MAX, FLAGS },
     { "gamma",        "set the initial gamma value",
-        OFFSET(gamma),        AV_OPT_TYPE_DOUBLE, {.dbl = 1.0},  0.1, 10.0, FLAGS },
+        OFFSET(gamma_expr),        AV_OPT_TYPE_STRING, {.str = "1.0"}, CHAR_MIN, CHAR_MAX, FLAGS },
     { "gamma_r",      "gamma value for red",
-        OFFSET(gamma_r),      AV_OPT_TYPE_DOUBLE, {.dbl = 1.0},  0.1, 10.0, FLAGS },
+        OFFSET(gamma_r_expr),      AV_OPT_TYPE_STRING, {.str = "1.0"}, CHAR_MIN, CHAR_MAX, FLAGS },
     { "gamma_g",      "gamma value for green",
-        OFFSET(gamma_g),      AV_OPT_TYPE_DOUBLE, {.dbl = 1.0},  0.1, 10.0, FLAGS },
+        OFFSET(gamma_g_expr),      AV_OPT_TYPE_STRING, {.str = "1.0"}, CHAR_MIN, CHAR_MAX, FLAGS },
     { "gamma_b",      "gamma value for blue",
-        OFFSET(gamma_b),      AV_OPT_TYPE_DOUBLE, {.dbl = 1.0},  0.1, 10.0, FLAGS },
+        OFFSET(gamma_b_expr),      AV_OPT_TYPE_STRING, {.str = "1.0"}, CHAR_MIN, CHAR_MAX, FLAGS },
     { "gamma_weight", "set the gamma weight which reduces the effect of gamma on bright areas",
-        OFFSET(gamma_weight), AV_OPT_TYPE_DOUBLE, {.dbl = 1.0},  0.0, 1.0, FLAGS },
+        OFFSET(gamma_weight_expr), AV_OPT_TYPE_STRING, {.str = "1.0"}, CHAR_MIN, CHAR_MAX, FLAGS },
     { NULL }
 };
 
 AVFILTER_DEFINE_CLASS(eq);
 
 AVFilter ff_vf_eq = {
-    .name            = "eq",
-    .description     = NULL_IF_CONFIG_SMALL("Adjust brightness, contrast, gamma, and saturation."),
-    .priv_size       = sizeof(EQContext),
-    .priv_class      = &eq_class,
-    .inputs          = eq_inputs,
-    .outputs         = eq_outputs,
+    .name          = "eq",
+    .description   = NULL_IF_CONFIG_SMALL("Adjust brightness, contrast, gamma, and saturation."),
+    .priv_size     = sizeof(EQContext),
+    .priv_class    = &eq_class,
+    .inputs        = eq_inputs,
+    .outputs       = eq_outputs,
     .process_command = process_command,
-    .query_formats   = query_formats,
-    .init            = initialize,
+    .query_formats = query_formats,
+    .init          = initialize,
+    .uninit        = uninit,
 };
diff --git a/libavfilter/vf_eq.h b/libavfilter/vf_eq.h
index 7e7c054..fe9c09c 100644
--- a/libavfilter/vf_eq.h
+++ b/libavfilter/vf_eq.h
@@ -26,6 +26,31 @@
 #define AVFILTER_EQ_H
 
 #include "avfilter.h"
+#include "libavutil/eval.h"
+
+static const char * const var_names[] = {
+    "contrast",
+    "brightness",
+    "saturation",
+    "gamma",
+    "gamma_weight",
+    "gamma_r",
+    "gamma_g",
+    "gamma_b",
+    NULL
+};
+
+enum var_name {
+    VAR_CONTRAST ,
+    VAR_BRIGHTNESS ,
+    VAR_SATURATION ,
+    VAR_GAMMA ,
+    VAR_GAMMA_WEIGHT ,
+    VAR_GAMMA_R ,
+    VAR_GAMMA_G ,
+    VAR_GAMMA_B ,
+    VAR_VARS_NB ,
+};
 
 typedef struct EQParameters {
     void (*adjust)(struct EQParameters *eq, uint8_t *dst, int dst_stride,
@@ -35,6 +60,7 @@ typedef struct EQParameters {
 
     double brightness, contrast, gamma, gamma_weight;
     int lut_clean;
+
 } EQParameters;
 
 typedef struct {
@@ -42,13 +68,31 @@ typedef struct {
 
     EQParameters param[3];
 
-    double contrast;
-    double brightness;
-    double saturation;
+    char   *contrast_expr;
+    AVExpr *contrast_pexpr;
+
+    char   *brightness_expr;
+    AVExpr *brightness_pexpr;
+
+    char   *saturation_expr;
+    AVExpr *saturation_pexpr;
+
+    char   *gamma_expr;
+    AVExpr *gamma_pexpr;
+
+    char   *gamma_weight_expr;
+    AVExpr *gamma_weight_pexpr;
+
+    char   *gamma_r_expr;
+    AVExpr *gamma_r_pexpr;
+
+    char   *gamma_g_expr;
+    AVExpr *gamma_g_pexpr;
+
+    char   *gamma_b_expr;
+    AVExpr *gamma_b_pexpr;
 
-    double gamma;
-    double gamma_weight;
-    double gamma_r, gamma_g, gamma_b;
+    double var_values[VAR_VARS_NB];
 
     void (*process)(struct EQParameters *par, uint8_t *dst, int dst_stride,
                     const uint8_t *src, int src_stride, int w, int h);
-- 
1.7.9.5

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel

Reply via email to