[FFmpeg-devel] [PATCH] lavu/eval: add ifelse function

Stefano Sabatini stefasab at gmail.com
Wed Jan 23 00:03:03 CET 2013


TODO: bump micro
---
 doc/eval.texi       |    4 ++++
 libavutil/eval.c    |    9 ++++++++-
 tests/ref/fate/eval |    6 ++++++
 3 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/doc/eval.texi b/doc/eval.texi
index 6c3eaef..3520532 100644
--- a/doc/eval.texi
+++ b/doc/eval.texi
@@ -155,6 +155,10 @@ the evaluation of @var{y}, return 0 otherwise.
 Evaluate @var{x}, and if the result is zero return the result of the
 evaluation of @var{y}, return 0 otherwise.
 
+ at item ifelse(x, y, z)
+Evaluate @var{x}, and if the result is zero return the result of the
+evaluation of @var{y}, return the evalution of @var{z} otherwise.
+
 @item taylor(expr, x)
 @item taylor(expr, x, id)
 Evaluate a Taylor series at @var{x}.
diff --git a/libavutil/eval.c b/libavutil/eval.c
index 6687b64..565421a 100644
--- a/libavutil/eval.c
+++ b/libavutil/eval.c
@@ -143,7 +143,7 @@ struct AVExpr {
         e_pow, e_mul, e_div, e_add,
         e_last, e_st, e_while, e_taylor, e_root, e_floor, e_ceil, e_trunc,
         e_sqrt, e_not, e_random, e_hypot, e_gcd,
-        e_if, e_ifnot,
+        e_if, e_ifnot, e_ifelse,
     } type;
     double value; // is sign in other types
     union {
@@ -176,6 +176,7 @@ static double eval_expr(Parser *p, AVExpr *e)
         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_ifelse: return e->value * ( eval_expr(p, e->param[0]) ? eval_expr(p, e->param[1]) : eval_expr(p, e->param[2]));
         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];
@@ -405,6 +406,7 @@ static int parse_primary(AVExpr **e, Parser *p)
     else if (strmatch(next, "gcd"   )) d->type = e_gcd;
     else if (strmatch(next, "if"    )) d->type = e_if;
     else if (strmatch(next, "ifnot" )) d->type = e_ifnot;
+    else if (strmatch(next, "ifelse")) d->type = e_ifelse;
     else {
         for (i=0; p->func1_names && p->func1_names[i]; i++) {
             if (strmatch(next, p->func1_names[i])) {
@@ -595,6 +597,9 @@ static int verify_expr(AVExpr *e)
         case e_taylor:
             return verify_expr(e->param[0]) && verify_expr(e->param[1])
                    && (!e->param[2] || verify_expr(e->param[2]));
+        case e_ifelse:
+            return verify_expr(e->param[0]) && verify_expr(e->param[1])
+                   && (!e->param[2] || verify_expr(e->param[2]));
         default: return verify_expr(e->param[0]) && verify_expr(e->param[1]) && !e->param[2];
     }
 }
@@ -772,6 +777,8 @@ int main(int argc, char **argv)
         "if(1, 2)",
         "ifnot(0, 23)",
         "ifnot(1, NaN) + if(0, 1)",
+        "ifelse(1, 1, 2)",
+        "ifelse(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)",
diff --git a/tests/ref/fate/eval b/tests/ref/fate/eval
index 8dda06b..015f17f 100644
--- a/tests/ref/fate/eval
+++ b/tests/ref/fate/eval
@@ -211,6 +211,12 @@ Evaluating 'ifnot(0, 23)'
 Evaluating 'ifnot(1, NaN) + if(0, 1)'
 'ifnot(1, NaN) + if(0, 1)' -> 0.000000
 
+Evaluating 'ifelse(1, 1, 2)'
+'ifelse(1, 1, 2)' -> 1.000000
+
+Evaluating 'ifelse(0, 1, 2)'
+'ifelse(0, 1, 2)' -> 2.000000
+
 Evaluating 'taylor(1, 1)'
 'taylor(1, 1)' -> 2.718282
 
-- 
1.7.9.5



More information about the ffmpeg-devel mailing list