* see http://joe.hotchkiss.com/programming/eval/eval.html
*/
+#include "attributes.h"
#include "avutil.h"
+#include "common.h"
#include "eval.h"
#include "log.h"
+#include "mathematics.h"
+#include "avstring.h"
+#include "timer.h"
typedef struct Parser {
const AVClass *class;
d = strtod(numstr, &next);
/* if parsing succeeded, check for and interpret postfixes */
if (next!=numstr) {
- if (*next >= 'E' && *next <= 'z') {
+ if (next[0] == 'd' && next[1] == 'B') {
+ /* treat dB as decibels instead of decibytes */
+ d = pow(10, d / 20);
+ next += 2;
+ } else if (*next >= 'E' && *next <= 'z') {
int e= si_prefixes[*next - 'E'];
if (e) {
if (next[1] == 'i') {
struct AVExpr {
enum {
e_value, e_const, e_func0, e_func1, e_func2,
- e_squish, e_gauss, e_ld, e_isnan,
+ e_squish, e_gauss, e_ld, e_isnan, e_isinf,
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,
case e_gauss: { double d = eval_expr(p, e->param[0]); return exp(-d*d/2)/sqrt(2*M_PI); }
case e_ld: return e->value * p->var[av_clip(eval_expr(p, e->param[0]), 0, VARS-1)];
case e_isnan: return e->value * !!isnan(eval_expr(p, e->param[0]));
+ case e_isinf: return e->value * !!isinf(eval_expr(p, e->param[0]));
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]));
}
p->s= strchr(p->s, '(');
- if (p->s==NULL) {
+ if (!p->s) {
av_log(p, AV_LOG_ERROR, "Undefined constant or missing '(' in '%s'\n", s0);
p->s= next;
av_expr_free(d);
else if (strmatch(next, "eq" )) d->type = e_eq;
else if (strmatch(next, "gte" )) d->type = e_gte;
else if (strmatch(next, "gt" )) d->type = e_gt;
- else if (strmatch(next, "lte" )) { AVExpr *tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gt; }
- else if (strmatch(next, "lt" )) { AVExpr *tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gte; }
+ else if (strmatch(next, "lte" )) { AVExpr *tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gte; }
+ else if (strmatch(next, "lt" )) { AVExpr *tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gt; }
else if (strmatch(next, "ld" )) d->type = e_ld;
else if (strmatch(next, "isnan" )) d->type = e_isnan;
+ else if (strmatch(next, "isinf" )) d->type = e_isinf;
else if (strmatch(next, "st" )) d->type = e_st;
else if (strmatch(next, "while" )) d->type = e_while;
else if (strmatch(next, "floor" )) d->type = e_floor;
return parse_primary(e, p);
}
+static int parse_dB(AVExpr **e, Parser *p, int *sign)
+{
+ /* do not filter out the negative sign when parsing a dB value.
+ for example, -3dB is not the same as -(3dB) */
+ if (*p->s == '-') {
+ char *next;
+ double av_unused ignored = strtod(p->s, &next);
+ if (next != p->s && next[0] == 'd' && next[1] == 'B') {
+ *sign = 0;
+ return parse_primary(e, p);
+ }
+ }
+ return parse_pow(e, p, sign);
+}
+
static int parse_factor(AVExpr **e, Parser *p)
{
int sign, sign2, ret;
AVExpr *e0, *e1, *e2;
- if ((ret = parse_pow(&e0, p, &sign)) < 0)
+ if ((ret = parse_dB(&e0, p, &sign)) < 0)
return ret;
while(p->s[0]=='^'){
e1 = e0;
p->s++;
- if ((ret = parse_pow(&e2, p, &sign2)) < 0) {
+ if ((ret = parse_dB(&e2, p, &sign2)) < 0) {
av_expr_free(e1);
return ret;
}
case e_ld:
case e_gauss:
case e_isnan:
+ case e_isinf:
case e_floor:
case e_ceil:
case e_trunc:
return AVERROR(ENOMEM);
while (*s)
- if (!isspace(*s++)) *wp++ = s[-1];
+ if (!av_isspace(*s++)) *wp++ = s[-1];
*wp++ = 0;
p.class = &class;
av_expr_free(e);
return isnan(*d) ? AVERROR(EINVAL) : 0;
}
-
-#ifdef TEST
-#undef printf
-#include <string.h>
-
-static double const_values[] = {
- M_PI,
- M_E,
- 0
-};
-
-static const char *const_names[] = {
- "PI",
- "E",
- 0
-};
-
-int main(int argc, char **argv)
-{
- int i;
- double d;
- const char **expr, *exprs[] = {
- "",
- "1;2",
- "-20",
- "-PI",
- "+PI",
- "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
- "80G/80Gi",
- "1k",
- "1Gi",
- "1gi",
- "1GiFoo",
- "1k+1k",
- "1Gi*3foo",
- "foo",
- "foo(",
- "foo()",
- "foo)",
- "sin",
- "sin(",
- "sin()",
- "sin)",
- "sin 10",
- "sin(1,2,3)",
- "sin(1 )",
- "1",
- "1foo",
- "bar + PI + E + 100f*2 + foo",
- "13k + 12f - foo(1, 2)",
- "1gi",
- "1Gi",
- "st(0, 123)",
- "st(1, 123); ld(1)",
- /* compute 1+2+...+N */
- "st(0, 1); while(lte(ld(0), 100), st(1, ld(1)+ld(0));st(0, ld(0)+1)); ld(1)",
- /* compute Fib(N) */
- "st(1, 1); st(2, 2); st(0, 1); while(lte(ld(0),10), st(3, ld(1)+ld(2)); st(1, ld(2)); st(2, ld(3)); st(0, ld(0)+1)); ld(3)",
- "while(0, 10)",
- "st(0, 1); while(lte(ld(0),100), st(1, ld(1)+ld(0)); st(0, ld(0)+1))",
- "isnan(1)",
- "isnan(NAN)",
- "floor(NAN)",
- "floor(123.123)",
- "floor(-123.123)",
- "trunc(123.123)",
- "trunc(-123.123)",
- "ceil(123.123)",
- "ceil(-123.123)",
- "sqrt(1764)",
- "sqrt(-1)",
- "not(1)",
- "not(NAN)",
- "not(0)",
- NULL
- };
-
- for (expr = exprs; *expr; expr++) {
- printf("Evaluating '%s'\n", *expr);
- av_expr_parse_and_eval(&d, *expr,
- const_names, const_values,
- NULL, NULL, NULL, NULL, NULL, 0, NULL);
- printf("'%s' -> %f\n\n", *expr, d);
- }
-
- av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
- const_names, const_values,
- NULL, NULL, NULL, NULL, NULL, 0, NULL);
- printf("%f == 12.7\n", d);
- av_expr_parse_and_eval(&d, "80G/80Gi",
- const_names, const_values,
- NULL, NULL, NULL, NULL, NULL, 0, NULL);
- printf("%f == 0.931322575\n", d);
-
- if (argc > 1 && !strcmp(argv[1], "-t")) {
- for (i = 0; i < 1050; i++) {
- START_TIMER;
- av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
- const_names, const_values,
- NULL, NULL, NULL, NULL, NULL, 0, NULL);
- STOP_TIMER("av_expr_parse_and_eval");
- }
- }
-
- return 0;
-}
-#endif