]> git.sesse.net Git - ffmpeg/commitdiff
lavu/eval: extend if/ifnot functions to accept a third parameter
authorStefano Sabatini <stefasab@gmail.com>
Tue, 22 Jan 2013 23:02:36 +0000 (00:02 +0100)
committerStefano Sabatini <stefasab@gmail.com>
Thu, 24 Jan 2013 11:19:01 +0000 (12:19 +0100)
Add support to an if/else construct, simplify logic in expressions.

doc/eval.texi
libavutil/eval.c
libavutil/version.h
tests/ref/fate/eval

index a1faff6e1624b326cb9034cf5cbc0697e7096806..f691821d1a1bcf34d2d0013c143a0f3805ffb6ed 100644 (file)
@@ -151,10 +151,18 @@ Return the greatest common divisor of @var{x} and @var{y}. If both @var{x} and
 Evaluate @var{x}, and if the result is non-zero return the result of
 the evaluation of @var{y}, return 0 otherwise.
 
+@item if(x, y, z)
+Evaluate @var{x}, and if the result is non-zero return the evaluation
+result of @var{y}, otherwise the evaluation result of @var{z}.
+
 @item ifnot(x, y)
 Evaluate @var{x}, and if the result is zero return the result of the
 evaluation of @var{y}, return 0 otherwise.
 
+@item ifnot(x, y, z)
+Evaluate @var{x}, and if the result is zero return the evaluation
+result of @var{y}, otherwise the evaluation result of @var{z}.
+
 @item taylor(expr, x) taylor(expr, x, id)
 Evaluate a taylor series at x.
 expr represents the LD(id)-th derivates of f(x) at 0. If id is not specified
index 6ea7504ff1b2f4f3401413274dccfe462297d46f..22b491fef372947faa708a5facdbdc17e70ca6f2 100644 (file)
@@ -180,8 +180,10 @@ static double eval_expr(Parser *p, AVExpr *e)
         case e_trunc:  return e->value * trunc(eval_expr(p, e->param[0]));
         case e_sqrt:   return e->value * sqrt (eval_expr(p, e->param[0]));
         case e_not:    return e->value * (eval_expr(p, e->param[0]) == 0);
-        case e_if:     return e->value * ( eval_expr(p, e->param[0]) ? eval_expr(p, e->param[1]) : 0);
-        case e_ifnot:  return e->value * (!eval_expr(p, e->param[0]) ? eval_expr(p, e->param[1]) : 0);
+        case e_if:     return e->value * (eval_expr(p, e->param[0]) ? eval_expr(p, e->param[1]) :
+                                          e->param[2] ? eval_expr(p, e->param[2]) : 0);
+        case e_ifnot:  return e->value * (!eval_expr(p, e->param[0]) ? eval_expr(p, e->param[1]) :
+                                          e->param[2] ? eval_expr(p, e->param[2]) : 0);
         case e_random:{
             int idx= av_clip(eval_expr(p, e->param[0]), 0, VARS-1);
             uint64_t r= isnan(p->var[idx]) ? 0 : p->var[idx];
@@ -599,6 +601,8 @@ static int verify_expr(AVExpr *e)
         case e_not:
         case e_random:
             return verify_expr(e->param[0]) && !e->param[1];
+        case e_if:
+        case e_ifnot:
         case e_taylor:
             return verify_expr(e->param[0]) && verify_expr(e->param[1])
                    && (!e->param[2] || verify_expr(e->param[2]));
@@ -777,8 +781,12 @@ int main(int argc, char **argv)
         "PI^1.23",
         "pow(-1,1.23)",
         "if(1, 2)",
+        "if(1, 1, 2)",
+        "if(0, 1, 2)",
         "ifnot(0, 23)",
         "ifnot(1, NaN) + if(0, 1)",
+        "ifnot(1, 1, 2)",
+        "ifnot(0, 1, 2)",
         "taylor(1, 1)",
         "taylor(eq(mod(ld(1),4),1)-eq(mod(ld(1),4),3), PI/2, 1)",
         "root(sin(ld(0))-1, 2)",
index 6fb794357e7d655af160daae3bb304a645a9825f..8e678fe287ee0bbff6720ad4ca78cc8eb9eed7ac 100644 (file)
@@ -76,7 +76,7 @@
 
 #define LIBAVUTIL_VERSION_MAJOR  52
 #define LIBAVUTIL_VERSION_MINOR  15
-#define LIBAVUTIL_VERSION_MICRO 101
+#define LIBAVUTIL_VERSION_MICRO 102
 
 #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
                                                LIBAVUTIL_VERSION_MINOR, \
index 8dda06b9982214eef4d6e1e9d6a0ca89b1f7616d..91a8abf171002988601310b56c32cac1a457871d 100644 (file)
@@ -205,12 +205,24 @@ Evaluating 'pow(-1,1.23)'
 Evaluating 'if(1, 2)'
 'if(1, 2)' -> 2.000000
 
+Evaluating 'if(1, 1, 2)'
+'if(1, 1, 2)' -> 1.000000
+
+Evaluating 'if(0, 1, 2)'
+'if(0, 1, 2)' -> 2.000000
+
 Evaluating 'ifnot(0, 23)'
 'ifnot(0, 23)' -> 23.000000
 
 Evaluating 'ifnot(1, NaN) + if(0, 1)'
 'ifnot(1, NaN) + if(0, 1)' -> 0.000000
 
+Evaluating 'ifnot(1, 1, 2)'
+'ifnot(1, 1, 2)' -> 2.000000
+
+Evaluating 'ifnot(0, 1, 2)'
+'ifnot(0, 1, 2)' -> 1.000000
+
 Evaluating 'taylor(1, 1)'
 'taylor(1, 1)' -> 2.718282