00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00029 #include "avutil.h"
00030 #include "eval.h"
00031 #include "log.h"
00032
00033 typedef struct Parser {
00034 const AVClass *class;
00035 int stack_index;
00036 char *s;
00037 const double *const_values;
00038 const char * const *const_names;
00039 double (* const *funcs1)(void *, double a);
00040 const char * const *func1_names;
00041 double (* const *funcs2)(void *, double a, double b);
00042 const char * const *func2_names;
00043 void *opaque;
00044 int log_offset;
00045 void *log_ctx;
00046 #define VARS 10
00047 double *var;
00048 } Parser;
00049
00050 static const AVClass class = { "Eval", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT, offsetof(Parser,log_offset), offsetof(Parser,log_ctx) };
00051
00052 static const int8_t si_prefixes['z' - 'E' + 1] = {
00053 ['y'-'E']= -24,
00054 ['z'-'E']= -21,
00055 ['a'-'E']= -18,
00056 ['f'-'E']= -15,
00057 ['p'-'E']= -12,
00058 ['n'-'E']= - 9,
00059 ['u'-'E']= - 6,
00060 ['m'-'E']= - 3,
00061 ['c'-'E']= - 2,
00062 ['d'-'E']= - 1,
00063 ['h'-'E']= 2,
00064 ['k'-'E']= 3,
00065 ['K'-'E']= 3,
00066 ['M'-'E']= 6,
00067 ['G'-'E']= 9,
00068 ['T'-'E']= 12,
00069 ['P'-'E']= 15,
00070 ['E'-'E']= 18,
00071 ['Z'-'E']= 21,
00072 ['Y'-'E']= 24,
00073 };
00074
00075 static const struct {
00076 const char *name;
00077 double value;
00078 } constants[] = {
00079 { "E", M_E },
00080 { "PI", M_PI },
00081 { "PHI", M_PHI },
00082 };
00083
00084 double av_strtod(const char *numstr, char **tail)
00085 {
00086 double d;
00087 char *next;
00088 if(numstr[0]=='0' && (numstr[1]|0x20)=='x') {
00089 d = strtoul(numstr, &next, 16);
00090 } else
00091 d = strtod(numstr, &next);
00092
00093 if (next!=numstr) {
00094 if (*next >= 'E' && *next <= 'z') {
00095 int e= si_prefixes[*next - 'E'];
00096 if (e) {
00097 if (next[1] == 'i') {
00098 d*= pow( 2, e/0.3);
00099 next+=2;
00100 } else {
00101 d*= pow(10, e);
00102 next++;
00103 }
00104 }
00105 }
00106
00107 if (*next=='B') {
00108 d*=8;
00109 next++;
00110 }
00111 }
00112
00113
00114 if (tail)
00115 *tail = next;
00116 return d;
00117 }
00118
00119 #define IS_IDENTIFIER_CHAR(c) ((c) - '0' <= 9U || (c) - 'a' <= 25U || (c) - 'A' <= 25U || (c) == '_')
00120
00121 static int strmatch(const char *s, const char *prefix)
00122 {
00123 int i;
00124 for (i=0; prefix[i]; i++) {
00125 if (prefix[i] != s[i]) return 0;
00126 }
00127
00128 return !IS_IDENTIFIER_CHAR(s[i]);
00129 }
00130
00131 struct AVExpr {
00132 enum {
00133 e_value, e_const, e_func0, e_func1, e_func2,
00134 e_squish, e_gauss, e_ld, e_isnan,
00135 e_mod, e_max, e_min, e_eq, e_gt, e_gte,
00136 e_pow, e_mul, e_div, e_add,
00137 e_last, e_st, e_while, e_floor, e_ceil, e_trunc,
00138 e_sqrt, e_not, e_random, e_hypot, e_gcd,
00139 e_if, e_ifnot,
00140 } type;
00141 double value;
00142 union {
00143 int const_index;
00144 double (*func0)(double);
00145 double (*func1)(void *, double);
00146 double (*func2)(void *, double, double);
00147 } a;
00148 struct AVExpr *param[2];
00149 double *var;
00150 };
00151
00152 static double eval_expr(Parser *p, AVExpr *e)
00153 {
00154 switch (e->type) {
00155 case e_value: return e->value;
00156 case e_const: return e->value * p->const_values[e->a.const_index];
00157 case e_func0: return e->value * e->a.func0(eval_expr(p, e->param[0]));
00158 case e_func1: return e->value * e->a.func1(p->opaque, eval_expr(p, e->param[0]));
00159 case e_func2: return e->value * e->a.func2(p->opaque, eval_expr(p, e->param[0]), eval_expr(p, e->param[1]));
00160 case e_squish: return 1/(1+exp(4*eval_expr(p, e->param[0])));
00161 case e_gauss: { double d = eval_expr(p, e->param[0]); return exp(-d*d/2)/sqrt(2*M_PI); }
00162 case e_ld: return e->value * p->var[av_clip(eval_expr(p, e->param[0]), 0, VARS-1)];
00163 case e_isnan: return e->value * !!isnan(eval_expr(p, e->param[0]));
00164 case e_floor: return e->value * floor(eval_expr(p, e->param[0]));
00165 case e_ceil : return e->value * ceil (eval_expr(p, e->param[0]));
00166 case e_trunc: return e->value * trunc(eval_expr(p, e->param[0]));
00167 case e_sqrt: return e->value * sqrt (eval_expr(p, e->param[0]));
00168 case e_not: return e->value * (eval_expr(p, e->param[0]) == 0);
00169 case e_if: return e->value * ( eval_expr(p, e->param[0]) ? eval_expr(p, e->param[1]) : 0);
00170 case e_ifnot: return e->value * (!eval_expr(p, e->param[0]) ? eval_expr(p, e->param[1]) : 0);
00171 case e_random:{
00172 int idx= av_clip(eval_expr(p, e->param[0]), 0, VARS-1);
00173 uint64_t r= isnan(p->var[idx]) ? 0 : p->var[idx];
00174 r= r*1664525+1013904223;
00175 p->var[idx]= r;
00176 return e->value * (r * (1.0/UINT64_MAX));
00177 }
00178 case e_while: {
00179 double d = NAN;
00180 while (eval_expr(p, e->param[0]))
00181 d=eval_expr(p, e->param[1]);
00182 return d;
00183 }
00184 default: {
00185 double d = eval_expr(p, e->param[0]);
00186 double d2 = eval_expr(p, e->param[1]);
00187 switch (e->type) {
00188 case e_mod: return e->value * (d - floor(d/d2)*d2);
00189 case e_gcd: return e->value * av_gcd(d,d2);
00190 case e_max: return e->value * (d > d2 ? d : d2);
00191 case e_min: return e->value * (d < d2 ? d : d2);
00192 case e_eq: return e->value * (d == d2 ? 1.0 : 0.0);
00193 case e_gt: return e->value * (d > d2 ? 1.0 : 0.0);
00194 case e_gte: return e->value * (d >= d2 ? 1.0 : 0.0);
00195 case e_pow: return e->value * pow(d, d2);
00196 case e_mul: return e->value * (d * d2);
00197 case e_div: return e->value * (d / d2);
00198 case e_add: return e->value * (d + d2);
00199 case e_last:return e->value * d2;
00200 case e_st : return e->value * (p->var[av_clip(d, 0, VARS-1)]= d2);
00201 case e_hypot:return e->value * (sqrt(d*d + d2*d2));
00202 }
00203 }
00204 }
00205 return NAN;
00206 }
00207
00208 static int parse_expr(AVExpr **e, Parser *p);
00209
00210 void av_expr_free(AVExpr *e)
00211 {
00212 if (!e) return;
00213 av_expr_free(e->param[0]);
00214 av_expr_free(e->param[1]);
00215 av_freep(&e->var);
00216 av_freep(&e);
00217 }
00218
00219 static int parse_primary(AVExpr **e, Parser *p)
00220 {
00221 AVExpr *d = av_mallocz(sizeof(AVExpr));
00222 char *next = p->s, *s0 = p->s;
00223 int ret, i;
00224
00225 if (!d)
00226 return AVERROR(ENOMEM);
00227
00228
00229 d->value = av_strtod(p->s, &next);
00230 if (next != p->s) {
00231 d->type = e_value;
00232 p->s= next;
00233 *e = d;
00234 return 0;
00235 }
00236 d->value = 1;
00237
00238
00239 for (i=0; p->const_names && p->const_names[i]; i++) {
00240 if (strmatch(p->s, p->const_names[i])) {
00241 p->s+= strlen(p->const_names[i]);
00242 d->type = e_const;
00243 d->a.const_index = i;
00244 *e = d;
00245 return 0;
00246 }
00247 }
00248 for (i = 0; i < FF_ARRAY_ELEMS(constants); i++) {
00249 if (strmatch(p->s, constants[i].name)) {
00250 p->s += strlen(constants[i].name);
00251 d->type = e_value;
00252 d->value = constants[i].value;
00253 *e = d;
00254 return 0;
00255 }
00256 }
00257
00258 p->s= strchr(p->s, '(');
00259 if (p->s==NULL) {
00260 av_log(p, AV_LOG_ERROR, "Undefined constant or missing '(' in '%s'\n", s0);
00261 p->s= next;
00262 av_expr_free(d);
00263 return AVERROR(EINVAL);
00264 }
00265 p->s++;
00266 if (*next == '(') {
00267 av_freep(&d);
00268 if ((ret = parse_expr(&d, p)) < 0)
00269 return ret;
00270 if (p->s[0] != ')') {
00271 av_log(p, AV_LOG_ERROR, "Missing ')' in '%s'\n", s0);
00272 av_expr_free(d);
00273 return AVERROR(EINVAL);
00274 }
00275 p->s++;
00276 *e = d;
00277 return 0;
00278 }
00279 if ((ret = parse_expr(&(d->param[0]), p)) < 0) {
00280 av_expr_free(d);
00281 return ret;
00282 }
00283 if (p->s[0]== ',') {
00284 p->s++;
00285 parse_expr(&d->param[1], p);
00286 }
00287 if (p->s[0] != ')') {
00288 av_log(p, AV_LOG_ERROR, "Missing ')' or too many args in '%s'\n", s0);
00289 av_expr_free(d);
00290 return AVERROR(EINVAL);
00291 }
00292 p->s++;
00293
00294 d->type = e_func0;
00295 if (strmatch(next, "sinh" )) d->a.func0 = sinh;
00296 else if (strmatch(next, "cosh" )) d->a.func0 = cosh;
00297 else if (strmatch(next, "tanh" )) d->a.func0 = tanh;
00298 else if (strmatch(next, "sin" )) d->a.func0 = sin;
00299 else if (strmatch(next, "cos" )) d->a.func0 = cos;
00300 else if (strmatch(next, "tan" )) d->a.func0 = tan;
00301 else if (strmatch(next, "atan" )) d->a.func0 = atan;
00302 else if (strmatch(next, "asin" )) d->a.func0 = asin;
00303 else if (strmatch(next, "acos" )) d->a.func0 = acos;
00304 else if (strmatch(next, "exp" )) d->a.func0 = exp;
00305 else if (strmatch(next, "log" )) d->a.func0 = log;
00306 else if (strmatch(next, "abs" )) d->a.func0 = fabs;
00307 else if (strmatch(next, "squish")) d->type = e_squish;
00308 else if (strmatch(next, "gauss" )) d->type = e_gauss;
00309 else if (strmatch(next, "mod" )) d->type = e_mod;
00310 else if (strmatch(next, "max" )) d->type = e_max;
00311 else if (strmatch(next, "min" )) d->type = e_min;
00312 else if (strmatch(next, "eq" )) d->type = e_eq;
00313 else if (strmatch(next, "gte" )) d->type = e_gte;
00314 else if (strmatch(next, "gt" )) d->type = e_gt;
00315 else if (strmatch(next, "lte" )) { AVExpr *tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gte; }
00316 else if (strmatch(next, "lt" )) { AVExpr *tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gt; }
00317 else if (strmatch(next, "ld" )) d->type = e_ld;
00318 else if (strmatch(next, "isnan" )) d->type = e_isnan;
00319 else if (strmatch(next, "st" )) d->type = e_st;
00320 else if (strmatch(next, "while" )) d->type = e_while;
00321 else if (strmatch(next, "floor" )) d->type = e_floor;
00322 else if (strmatch(next, "ceil" )) d->type = e_ceil;
00323 else if (strmatch(next, "trunc" )) d->type = e_trunc;
00324 else if (strmatch(next, "sqrt" )) d->type = e_sqrt;
00325 else if (strmatch(next, "not" )) d->type = e_not;
00326 else if (strmatch(next, "pow" )) d->type = e_pow;
00327 else if (strmatch(next, "random")) d->type = e_random;
00328 else if (strmatch(next, "hypot" )) d->type = e_hypot;
00329 else if (strmatch(next, "gcd" )) d->type = e_gcd;
00330 else if (strmatch(next, "if" )) d->type = e_if;
00331 else if (strmatch(next, "ifnot" )) d->type = e_ifnot;
00332 else {
00333 for (i=0; p->func1_names && p->func1_names[i]; i++) {
00334 if (strmatch(next, p->func1_names[i])) {
00335 d->a.func1 = p->funcs1[i];
00336 d->type = e_func1;
00337 *e = d;
00338 return 0;
00339 }
00340 }
00341
00342 for (i=0; p->func2_names && p->func2_names[i]; i++) {
00343 if (strmatch(next, p->func2_names[i])) {
00344 d->a.func2 = p->funcs2[i];
00345 d->type = e_func2;
00346 *e = d;
00347 return 0;
00348 }
00349 }
00350
00351 av_log(p, AV_LOG_ERROR, "Unknown function in '%s'\n", s0);
00352 av_expr_free(d);
00353 return AVERROR(EINVAL);
00354 }
00355
00356 *e = d;
00357 return 0;
00358 }
00359
00360 static AVExpr *new_eval_expr(int type, int value, AVExpr *p0, AVExpr *p1)
00361 {
00362 AVExpr *e = av_mallocz(sizeof(AVExpr));
00363 if (!e)
00364 return NULL;
00365 e->type =type ;
00366 e->value =value ;
00367 e->param[0] =p0 ;
00368 e->param[1] =p1 ;
00369 return e;
00370 }
00371
00372 static int parse_pow(AVExpr **e, Parser *p, int *sign)
00373 {
00374 *sign= (*p->s == '+') - (*p->s == '-');
00375 p->s += *sign&1;
00376 return parse_primary(e, p);
00377 }
00378
00379 static int parse_factor(AVExpr **e, Parser *p)
00380 {
00381 int sign, sign2, ret;
00382 AVExpr *e0, *e1, *e2;
00383 if ((ret = parse_pow(&e0, p, &sign)) < 0)
00384 return ret;
00385 while(p->s[0]=='^'){
00386 e1 = e0;
00387 p->s++;
00388 if ((ret = parse_pow(&e2, p, &sign2)) < 0) {
00389 av_expr_free(e1);
00390 return ret;
00391 }
00392 e0 = new_eval_expr(e_pow, 1, e1, e2);
00393 if (!e0) {
00394 av_expr_free(e1);
00395 av_expr_free(e2);
00396 return AVERROR(ENOMEM);
00397 }
00398 if (e0->param[1]) e0->param[1]->value *= (sign2|1);
00399 }
00400 if (e0) e0->value *= (sign|1);
00401
00402 *e = e0;
00403 return 0;
00404 }
00405
00406 static int parse_term(AVExpr **e, Parser *p)
00407 {
00408 int ret;
00409 AVExpr *e0, *e1, *e2;
00410 if ((ret = parse_factor(&e0, p)) < 0)
00411 return ret;
00412 while (p->s[0]=='*' || p->s[0]=='/') {
00413 int c= *p->s++;
00414 e1 = e0;
00415 if ((ret = parse_factor(&e2, p)) < 0) {
00416 av_expr_free(e1);
00417 return ret;
00418 }
00419 e0 = new_eval_expr(c == '*' ? e_mul : e_div, 1, e1, e2);
00420 if (!e0) {
00421 av_expr_free(e1);
00422 av_expr_free(e2);
00423 return AVERROR(ENOMEM);
00424 }
00425 }
00426 *e = e0;
00427 return 0;
00428 }
00429
00430 static int parse_subexpr(AVExpr **e, Parser *p)
00431 {
00432 int ret;
00433 AVExpr *e0, *e1, *e2;
00434 if ((ret = parse_term(&e0, p)) < 0)
00435 return ret;
00436 while (*p->s == '+' || *p->s == '-') {
00437 e1 = e0;
00438 if ((ret = parse_term(&e2, p)) < 0) {
00439 av_expr_free(e1);
00440 return ret;
00441 }
00442 e0 = new_eval_expr(e_add, 1, e1, e2);
00443 if (!e0) {
00444 av_expr_free(e1);
00445 av_expr_free(e2);
00446 return AVERROR(ENOMEM);
00447 }
00448 };
00449
00450 *e = e0;
00451 return 0;
00452 }
00453
00454 static int parse_expr(AVExpr **e, Parser *p)
00455 {
00456 int ret;
00457 AVExpr *e0, *e1, *e2;
00458 if (p->stack_index <= 0)
00459 return AVERROR(EINVAL);
00460 p->stack_index--;
00461
00462 if ((ret = parse_subexpr(&e0, p)) < 0)
00463 return ret;
00464 while (*p->s == ';') {
00465 p->s++;
00466 e1 = e0;
00467 if ((ret = parse_subexpr(&e2, p)) < 0) {
00468 av_expr_free(e1);
00469 return ret;
00470 }
00471 e0 = new_eval_expr(e_last, 1, e1, e2);
00472 if (!e0) {
00473 av_expr_free(e1);
00474 av_expr_free(e2);
00475 return AVERROR(ENOMEM);
00476 }
00477 };
00478
00479 p->stack_index++;
00480 *e = e0;
00481 return 0;
00482 }
00483
00484 static int verify_expr(AVExpr *e)
00485 {
00486 if (!e) return 0;
00487 switch (e->type) {
00488 case e_value:
00489 case e_const: return 1;
00490 case e_func0:
00491 case e_func1:
00492 case e_squish:
00493 case e_ld:
00494 case e_gauss:
00495 case e_isnan:
00496 case e_floor:
00497 case e_ceil:
00498 case e_trunc:
00499 case e_sqrt:
00500 case e_not:
00501 case e_random:
00502 return verify_expr(e->param[0]);
00503 default: return verify_expr(e->param[0]) && verify_expr(e->param[1]);
00504 }
00505 }
00506
00507 int av_expr_parse(AVExpr **expr, const char *s,
00508 const char * const *const_names,
00509 const char * const *func1_names, double (* const *funcs1)(void *, double),
00510 const char * const *func2_names, double (* const *funcs2)(void *, double, double),
00511 int log_offset, void *log_ctx)
00512 {
00513 Parser p = { 0 };
00514 AVExpr *e = NULL;
00515 char *w = av_malloc(strlen(s) + 1);
00516 char *wp = w;
00517 const char *s0 = s;
00518 int ret = 0;
00519
00520 if (!w)
00521 return AVERROR(ENOMEM);
00522
00523 while (*s)
00524 if (!isspace(*s++)) *wp++ = s[-1];
00525 *wp++ = 0;
00526
00527 p.class = &class;
00528 p.stack_index=100;
00529 p.s= w;
00530 p.const_names = const_names;
00531 p.funcs1 = funcs1;
00532 p.func1_names = func1_names;
00533 p.funcs2 = funcs2;
00534 p.func2_names = func2_names;
00535 p.log_offset = log_offset;
00536 p.log_ctx = log_ctx;
00537
00538 if ((ret = parse_expr(&e, &p)) < 0)
00539 goto end;
00540 if (*p.s) {
00541 av_expr_free(e);
00542 av_log(&p, AV_LOG_ERROR, "Invalid chars '%s' at the end of expression '%s'\n", p.s, s0);
00543 ret = AVERROR(EINVAL);
00544 goto end;
00545 }
00546 if (!verify_expr(e)) {
00547 av_expr_free(e);
00548 ret = AVERROR(EINVAL);
00549 goto end;
00550 }
00551 e->var= av_mallocz(sizeof(double) *VARS);
00552 *expr = e;
00553 end:
00554 av_free(w);
00555 return ret;
00556 }
00557
00558 double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
00559 {
00560 Parser p = { 0 };
00561 p.var= e->var;
00562
00563 p.const_values = const_values;
00564 p.opaque = opaque;
00565 return eval_expr(&p, e);
00566 }
00567
00568 int av_expr_parse_and_eval(double *d, const char *s,
00569 const char * const *const_names, const double *const_values,
00570 const char * const *func1_names, double (* const *funcs1)(void *, double),
00571 const char * const *func2_names, double (* const *funcs2)(void *, double, double),
00572 void *opaque, int log_offset, void *log_ctx)
00573 {
00574 AVExpr *e = NULL;
00575 int ret = av_expr_parse(&e, s, const_names, func1_names, funcs1, func2_names, funcs2, log_offset, log_ctx);
00576
00577 if (ret < 0) {
00578 *d = NAN;
00579 return ret;
00580 }
00581 *d = av_expr_eval(e, const_values, opaque);
00582 av_expr_free(e);
00583 return isnan(*d) ? AVERROR(EINVAL) : 0;
00584 }
00585
00586 #if FF_API_OLD_EVAL_NAMES
00587
00588 int av_parse_expr(AVExpr **expr, const char *s,
00589 const char * const *const_names,
00590 const char * const *func1_names, double (* const *funcs1)(void *, double),
00591 const char * const *func2_names, double (* const *funcs2)(void *, double, double),
00592 int log_offset, void *log_ctx)
00593 {
00594 return av_expr_parse(expr, s, const_names, func1_names, funcs1, func2_names, funcs2,
00595 log_offset, log_ctx);
00596 }
00597
00598 double av_eval_expr(AVExpr *e, const double *const_values, void *opaque)
00599 {
00600 return av_expr_eval(e, const_values, opaque);
00601 }
00602
00603 int av_parse_and_eval_expr(double *res, const char *s,
00604 const char * const *const_names, const double *const_values,
00605 const char * const *func1_names, double (* const *funcs1)(void *, double),
00606 const char * const *func2_names, double (* const *funcs2)(void *, double, double),
00607 void *opaque, int log_offset, void *log_ctx)
00608 {
00609 return av_expr_parse_and_eval(res, s, const_names, const_values, func1_names, funcs1, func2_names, funcs2,
00610 opaque, log_offset, log_ctx);
00611 }
00612
00613 void av_free_expr(AVExpr *e)
00614 {
00615 av_expr_free(e);
00616 }
00617
00618 #endif
00619
00620 #ifdef TEST
00621
00622 #undef printf
00623 #include <string.h>
00624
00625 static double const_values[] = {
00626 M_PI,
00627 M_E,
00628 0
00629 };
00630
00631 static const char *const_names[] = {
00632 "PI",
00633 "E",
00634 0
00635 };
00636
00637 int main(int argc, char **argv)
00638 {
00639 int i;
00640 double d;
00641 const char **expr, *exprs[] = {
00642 "",
00643 "1;2",
00644 "-20",
00645 "-PI",
00646 "+PI",
00647 "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
00648 "80G/80Gi",
00649 "1k",
00650 "1Gi",
00651 "1gi",
00652 "1GiFoo",
00653 "1k+1k",
00654 "1Gi*3foo",
00655 "foo",
00656 "foo(",
00657 "foo()",
00658 "foo)",
00659 "sin",
00660 "sin(",
00661 "sin()",
00662 "sin)",
00663 "sin 10",
00664 "sin(1,2,3)",
00665 "sin(1 )",
00666 "1",
00667 "1foo",
00668 "bar + PI + E + 100f*2 + foo",
00669 "13k + 12f - foo(1, 2)",
00670 "1gi",
00671 "1Gi",
00672 "st(0, 123)",
00673 "st(1, 123); ld(1)",
00674
00675 "st(0, 1); while(lte(ld(0), 100), st(1, ld(1)+ld(0));st(0, ld(0)+1)); ld(1)",
00676
00677 "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)",
00678 "while(0, 10)",
00679 "st(0, 1); while(lte(ld(0),100), st(1, ld(1)+ld(0)); st(0, ld(0)+1))",
00680 "isnan(1)",
00681 "isnan(NAN)",
00682 "floor(NAN)",
00683 "floor(123.123)",
00684 "floor(-123.123)",
00685 "trunc(123.123)",
00686 "trunc(-123.123)",
00687 "ceil(123.123)",
00688 "ceil(-123.123)",
00689 "sqrt(1764)",
00690 "isnan(sqrt(-1))",
00691 "not(1)",
00692 "not(NAN)",
00693 "not(0)",
00694 "pow(0,1.23)",
00695 "pow(PI,1.23)",
00696 "PI^1.23",
00697 "pow(-1,1.23)",
00698 "if(1, 2)",
00699 "ifnot(0, 23)",
00700 "ifnot(1, NaN) + if(0, 1)",
00701 NULL
00702 };
00703
00704 for (expr = exprs; *expr; expr++) {
00705 printf("Evaluating '%s'\n", *expr);
00706 av_expr_parse_and_eval(&d, *expr,
00707 const_names, const_values,
00708 NULL, NULL, NULL, NULL, NULL, 0, NULL);
00709 if(isnan(d)){
00710 printf("'%s' -> nan\n\n", *expr);
00711 }else{
00712 printf("'%s' -> %f\n\n", *expr, d);
00713 }
00714 }
00715
00716 av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
00717 const_names, const_values,
00718 NULL, NULL, NULL, NULL, NULL, 0, NULL);
00719 printf("%f == 12.7\n", d);
00720 av_expr_parse_and_eval(&d, "80G/80Gi",
00721 const_names, const_values,
00722 NULL, NULL, NULL, NULL, NULL, 0, NULL);
00723 printf("%f == 0.931322575\n", d);
00724
00725 if (argc > 1 && !strcmp(argv[1], "-t")) {
00726 for (i = 0; i < 1050; i++) {
00727 START_TIMER;
00728 av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
00729 const_names, const_values,
00730 NULL, NULL, NULL, NULL, NULL, 0, NULL);
00731 STOP_TIMER("av_expr_parse_and_eval");
00732 }
00733 }
00734
00735 return 0;
00736 }
00737
00738 #endif