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