lavu/eval: add if() and ifnot() eval functions

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.
release/0.10
Stefano Sabatini 13 years ago
parent a798c20a76
commit 999495734b
  1. 12
      doc/eval.texi
  2. 2
      libavutil/avutil.h
  3. 8
      libavutil/eval.c
  4. 9
      tests/ref/fate/eval

@ -98,6 +98,14 @@ point (@var{x}, @var{y}) from the origin.
@item gcd(x, y) @item gcd(x, y)
Return the greatest common divisor of @var{x} and @var{y}. If both @var{x} and 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. @var{y} are 0 or either or both are less than zero then behavior is undefined.
@item if(x, y)
Evaluate @var{x}, and if the result is non-zero return the result of
the evaluation of @var{y}, return 0 otherwise.
@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.
@end table @end table
The following constants are available: The following constants are available:
@ -116,13 +124,13 @@ Note that:
@code{+} works like OR @code{+} works like OR
thus and the construct:
@example @example
if A then B else C if A then B else C
@end example @end example
is equivalent to is equivalent to
@example @example
A*B + not(A)*C if(A,B) + ifnot(A,C)
@end example @end example
In your C code, you can extend the list of unary and binary functions, In your C code, you can extend the list of unary and binary functions,

@ -155,7 +155,7 @@
#define LIBAVUTIL_VERSION_MAJOR 51 #define LIBAVUTIL_VERSION_MAJOR 51
#define LIBAVUTIL_VERSION_MINOR 34 #define LIBAVUTIL_VERSION_MINOR 34
#define LIBAVUTIL_VERSION_MICRO 100 #define LIBAVUTIL_VERSION_MICRO 101
#define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \ #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
LIBAVUTIL_VERSION_MINOR, \ LIBAVUTIL_VERSION_MINOR, \

@ -136,6 +136,7 @@ struct AVExpr {
e_pow, e_mul, e_div, e_add, e_pow, e_mul, e_div, e_add,
e_last, e_st, e_while, e_floor, e_ceil, e_trunc, e_last, e_st, e_while, e_floor, e_ceil, e_trunc,
e_sqrt, e_not, e_random, e_hypot, e_gcd, e_sqrt, e_not, e_random, e_hypot, e_gcd,
e_if, e_ifnot,
} type; } type;
double value; // is sign in other types double value; // is sign in other types
union { 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_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_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_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_random:{ case e_random:{
int idx= av_clip(eval_expr(p, e->param[0]), 0, VARS-1); int idx= av_clip(eval_expr(p, e->param[0]), 0, VARS-1);
uint64_t r= isnan(p->var[idx]) ? 0 : p->var[idx]; 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, "random")) d->type = e_random;
else if (strmatch(next, "hypot" )) d->type = e_hypot; else if (strmatch(next, "hypot" )) d->type = e_hypot;
else if (strmatch(next, "gcd" )) d->type = e_gcd; 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 { else {
for (i=0; p->func1_names && p->func1_names[i]; i++) { for (i=0; p->func1_names && p->func1_names[i]; i++) {
if (strmatch(next, p->func1_names[i])) { if (strmatch(next, p->func1_names[i])) {
@ -690,6 +695,9 @@ int main(int argc, char **argv)
"pow(PI,1.23)", "pow(PI,1.23)",
"PI^1.23", "PI^1.23",
"pow(-1,1.23)", "pow(-1,1.23)",
"if(1, 2)",
"ifnot(0, 23)",
"ifnot(1, NaN) + if(0, 1)",
NULL NULL
}; };

@ -160,5 +160,14 @@ Evaluating 'PI^1.23'
Evaluating 'pow(-1,1.23)' Evaluating 'pow(-1,1.23)'
'pow(-1,1.23)' -> nan 'pow(-1,1.23)' -> nan
Evaluating 'if(1, 2)'
'if(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
12.700000 == 12.7 12.700000 == 12.7
0.931323 == 0.931322575 0.931323 == 0.931322575

Loading…
Cancel
Save