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