[FFmpeg-devel] [PATCH] lavu/eval: add ifzero() and ifnonzero() expression

Stefano Sabatini stefasab at gmail.com
Sun Jan 15 23:08:55 CET 2012


They allow to implement the if/then/else logic, which cannot be
implemented otherwise.

For example the expression:
A*B + not(A)*C

always evaluates to NaN if B is NaN, even in the case where A is 0.
---
 doc/eval.texi    |   19 ++++++++++++-------
 libavutil/eval.c |    8 ++++++++
 2 files changed, 20 insertions(+), 7 deletions(-)

diff --git a/doc/eval.texi b/doc/eval.texi
index b325b37..4cc8ec9 100644
--- a/doc/eval.texi
+++ b/doc/eval.texi
@@ -100,6 +100,14 @@ Return the greatest common divisor of @var{x} and @var{y}. If both @var{x} and
 @var{y} are 0 or either or both are less than zero then behavior is undefined.
 @end table
 
+ at item ifzero(x, y)
+Evaluate @var{x}, and if the result is zero return the result of the
+evaluation of @var{y}, return 0 otherwise.
+
+ at item ifnonzero(x, y)
+Evaluate @var{x}, and if the result is non-zero return the result of
+the evaluation of @var{y}, return 0 otherwise.
+
 The following constants are available:
 @table @option
 @item PI
@@ -110,19 +118,16 @@ exp(1) (Euler's number), approximately 2.718
 golden ratio (1+sqrt(5))/2, approximately 1.618
 @end table
 
-Note that:
-
- at code{*} works like AND
-
- at code{+} works like OR
+Note that @code{+} can be used to OR several expressions togheter,
+assuming that an expression is considered "true" if it has a non-zero
+value, thus
 
-thus
 @example
 if A then B else C
 @end example
 is equivalent to
 @example
-A*B + not(A)*C
+ifnonzero(A,B) + ifzero(A,C)
 @end example
 
 In your C code, you can extend the list of unary and binary functions,
diff --git a/libavutil/eval.c b/libavutil/eval.c
index 1b8176c..f5ebe3d 100644
--- a/libavutil/eval.c
+++ b/libavutil/eval.c
@@ -136,6 +136,7 @@ struct AVExpr {
         e_pow, e_mul, e_div, e_add,
         e_last, e_st, e_while, e_floor, e_ceil, e_trunc,
         e_sqrt, e_not, e_random, e_hypot, e_gcd,
+        e_ifzero, e_ifnonzero,
     } type;
     double value; // is sign in other types
     union {
@@ -165,6 +166,8 @@ 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_ifzero:    return e->value * (eval_expr(p, e->param[0]) == 0 ? eval_expr(p, e->param[1]) : 0);
+        case e_ifnonzero: return e->value * (eval_expr(p, e->param[0]) != 0 ? eval_expr(p, e->param[1]) : 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];
@@ -324,6 +327,8 @@ static int parse_primary(AVExpr **e, Parser *p)
     else if (strmatch(next, "random")) d->type = e_random;
     else if (strmatch(next, "hypot" )) d->type = e_hypot;
     else if (strmatch(next, "gcd"   )) d->type = e_gcd;
+    else if (strmatch(next, "ifzero")) d->type = e_ifzero;
+    else if (strmatch(next, "ifnonzero")) d->type = e_ifnonzero;
     else {
         for (i=0; p->func1_names && p->func1_names[i]; i++) {
             if (strmatch(next, p->func1_names[i])) {
@@ -690,6 +695,9 @@ int main(int argc, char **argv)
         "pow(PI,1.23)",
         "PI^1.23",
         "pow(-1,1.23)",
+        "ifzero(1, 2)",
+        "ifnonzero(0, 23)",
+        "ifnonzero(1, NaN) + ifzero(0, 1)",
         NULL
     };
 
-- 
1.7.5.4



More information about the ffmpeg-devel mailing list