eval: add random()

Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
This commit is contained in:
Michael Niedermayer 2011-10-08 03:09:16 +02:00
parent 8fd56f7b12
commit ac7cda9e14
1 changed files with 10 additions and 1 deletions

View File

@ -126,7 +126,7 @@ struct AVExpr {
e_mod, e_max, e_min, e_eq, e_gt, e_gte,
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_sqrt, e_not, e_random,
} type;
double value; // is sign in other types
union {
@ -156,6 +156,13 @@ 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_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];
r= r*1664525+1013904223;
p->var[idx]= r;
return e->value * (r * (1.0/UINT64_MAX));
}
case e_while: {
double d = NAN;
while (eval_expr(p, e->param[0]))
@ -294,6 +301,7 @@ static int parse_primary(AVExpr **e, Parser *p)
else if (strmatch(next, "sqrt" )) d->type = e_sqrt;
else if (strmatch(next, "not" )) d->type = e_not;
else if (strmatch(next, "pow" )) d->type = e_pow;
else if (strmatch(next, "random")) d->type = e_random;
else {
for (i=0; p->func1_names && p->func1_names[i]; i++) {
if (strmatch(next, p->func1_names[i])) {
@ -463,6 +471,7 @@ static int verify_expr(AVExpr *e)
case e_trunc:
case e_sqrt:
case e_not:
case e_random:
return verify_expr(e->param[0]);
default: return verify_expr(e->param[0]) && verify_expr(e->param[1]);
}