From 482566ccc3fdcdbaf0f1e78309bf8ea9ddbce66b Mon Sep 17 00:00:00 2001 From: Kevin Mark Date: Tue, 6 Jun 2017 00:43:13 -0400 Subject: [PATCH] libavutil/eval: Add round function to expression parser We have floor, ceil, and trunc. Let's add round. Signed-off-by: Kevin Mark Signed-off-by: Michael Niedermayer --- doc/utils.texi | 3 +++ libavutil/eval.c | 5 ++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/doc/utils.texi b/doc/utils.texi index 1734439459..d65bdf0b0e 100644 --- a/doc/utils.texi +++ b/doc/utils.texi @@ -914,6 +914,9 @@ various input values that the expression can access through @code{ld(0)}. When the expression evaluates to 0 then the corresponding input value will be returned. +@item round(expr) +Round the value of expression @var{expr} to the nearest integer. For example, "round(1.5)" is "2.0". + @item sin(x) Compute sine of @var{x}. diff --git a/libavutil/eval.c b/libavutil/eval.c index 7e866155db..638259adef 100644 --- a/libavutil/eval.c +++ b/libavutil/eval.c @@ -153,7 +153,7 @@ struct AVExpr { e_squish, e_gauss, e_ld, e_isnan, e_isinf, e_mod, e_max, e_min, e_eq, e_gt, e_gte, e_lte, e_lt, 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_last, e_st, e_while, e_taylor, e_root, e_floor, e_ceil, e_trunc, e_round, e_sqrt, e_not, e_random, e_hypot, e_gcd, e_if, e_ifnot, e_print, e_bitand, e_bitor, e_between, e_clip, e_atan2 } type; @@ -189,6 +189,7 @@ static double eval_expr(Parser *p, AVExpr *e) case e_floor: return e->value * floor(eval_expr(p, e->param[0])); case e_ceil : return e->value * ceil (eval_expr(p, e->param[0])); case e_trunc: return e->value * trunc(eval_expr(p, e->param[0])); + case e_round: return e->value * round(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]) : @@ -440,6 +441,7 @@ static int parse_primary(AVExpr **e, Parser *p) else if (strmatch(next, "floor" )) d->type = e_floor; else if (strmatch(next, "ceil" )) d->type = e_ceil; else if (strmatch(next, "trunc" )) d->type = e_trunc; + else if (strmatch(next, "round" )) d->type = e_round; 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; @@ -637,6 +639,7 @@ static int verify_expr(AVExpr *e) case e_floor: case e_ceil: case e_trunc: + case e_round: case e_sqrt: case e_not: case e_random: