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