Libav 0.7.1
|
00001 /* 00002 * Copyright (c) 2002-2006 Michael Niedermayer <michaelni@gmx.at> 00003 * Copyright (c) 2006 Oded Shimon <ods15@ods15.dyndns.org> 00004 * 00005 * This file is part of Libav. 00006 * 00007 * Libav is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * Libav is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with Libav; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00029 #include "avutil.h" 00030 #include "eval.h" 00031 00032 typedef struct Parser { 00033 const AVClass *class; 00034 int stack_index; 00035 char *s; 00036 const double *const_values; 00037 const char * const *const_names; // NULL terminated 00038 double (* const *funcs1)(void *, double a); // NULL terminated 00039 const char * const *func1_names; // NULL terminated 00040 double (* const *funcs2)(void *, double a, double b); // NULL terminated 00041 const char * const *func2_names; // NULL terminated 00042 void *opaque; 00043 int log_offset; 00044 void *log_ctx; 00045 #define VARS 10 00046 double var[VARS]; 00047 } Parser; 00048 00049 static const AVClass class = { "Eval", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT, offsetof(Parser,log_offset), offsetof(Parser,log_ctx) }; 00050 00051 static const int8_t si_prefixes['z' - 'E' + 1] = { 00052 ['y'-'E']= -24, 00053 ['z'-'E']= -21, 00054 ['a'-'E']= -18, 00055 ['f'-'E']= -15, 00056 ['p'-'E']= -12, 00057 ['n'-'E']= - 9, 00058 ['u'-'E']= - 6, 00059 ['m'-'E']= - 3, 00060 ['c'-'E']= - 2, 00061 ['d'-'E']= - 1, 00062 ['h'-'E']= 2, 00063 ['k'-'E']= 3, 00064 ['K'-'E']= 3, 00065 ['M'-'E']= 6, 00066 ['G'-'E']= 9, 00067 ['T'-'E']= 12, 00068 ['P'-'E']= 15, 00069 ['E'-'E']= 18, 00070 ['Z'-'E']= 21, 00071 ['Y'-'E']= 24, 00072 }; 00073 00074 double av_strtod(const char *numstr, char **tail) 00075 { 00076 double d; 00077 char *next; 00078 d = strtod(numstr, &next); 00079 /* if parsing succeeded, check for and interpret postfixes */ 00080 if (next!=numstr) { 00081 if (*next >= 'E' && *next <= 'z') { 00082 int e= si_prefixes[*next - 'E']; 00083 if (e) { 00084 if (next[1] == 'i') { 00085 d*= pow( 2, e/0.3); 00086 next+=2; 00087 } else { 00088 d*= pow(10, e); 00089 next++; 00090 } 00091 } 00092 } 00093 00094 if (*next=='B') { 00095 d*=8; 00096 next++; 00097 } 00098 } 00099 /* if requested, fill in tail with the position after the last parsed 00100 character */ 00101 if (tail) 00102 *tail = next; 00103 return d; 00104 } 00105 00106 #define IS_IDENTIFIER_CHAR(c) ((c) - '0' <= 9U || (c) - 'a' <= 25U || (c) - 'A' <= 25U || (c) == '_') 00107 00108 static int strmatch(const char *s, const char *prefix) 00109 { 00110 int i; 00111 for (i=0; prefix[i]; i++) { 00112 if (prefix[i] != s[i]) return 0; 00113 } 00114 /* return 1 only if the s identifier is terminated */ 00115 return !IS_IDENTIFIER_CHAR(s[i]); 00116 } 00117 00118 struct AVExpr { 00119 enum { 00120 e_value, e_const, e_func0, e_func1, e_func2, 00121 e_squish, e_gauss, e_ld, e_isnan, 00122 e_mod, e_max, e_min, e_eq, e_gt, e_gte, 00123 e_pow, e_mul, e_div, e_add, 00124 e_last, e_st, e_while, e_floor, e_ceil, e_trunc, 00125 } type; 00126 double value; // is sign in other types 00127 union { 00128 int const_index; 00129 double (*func0)(double); 00130 double (*func1)(void *, double); 00131 double (*func2)(void *, double, double); 00132 } a; 00133 struct AVExpr *param[2]; 00134 }; 00135 00136 static double eval_expr(Parser *p, AVExpr *e) 00137 { 00138 switch (e->type) { 00139 case e_value: return e->value; 00140 case e_const: return e->value * p->const_values[e->a.const_index]; 00141 case e_func0: return e->value * e->a.func0(eval_expr(p, e->param[0])); 00142 case e_func1: return e->value * e->a.func1(p->opaque, eval_expr(p, e->param[0])); 00143 case e_func2: return e->value * e->a.func2(p->opaque, eval_expr(p, e->param[0]), eval_expr(p, e->param[1])); 00144 case e_squish: return 1/(1+exp(4*eval_expr(p, e->param[0]))); 00145 case e_gauss: { double d = eval_expr(p, e->param[0]); return exp(-d*d/2)/sqrt(2*M_PI); } 00146 case e_ld: return e->value * p->var[av_clip(eval_expr(p, e->param[0]), 0, VARS-1)]; 00147 case e_isnan: return e->value * !!isnan(eval_expr(p, e->param[0])); 00148 case e_floor: return e->value * floor(eval_expr(p, e->param[0])); 00149 case e_ceil : return e->value * ceil (eval_expr(p, e->param[0])); 00150 case e_trunc: return e->value * trunc(eval_expr(p, e->param[0])); 00151 case e_while: { 00152 double d = NAN; 00153 while (eval_expr(p, e->param[0])) 00154 d=eval_expr(p, e->param[1]); 00155 return d; 00156 } 00157 default: { 00158 double d = eval_expr(p, e->param[0]); 00159 double d2 = eval_expr(p, e->param[1]); 00160 switch (e->type) { 00161 case e_mod: return e->value * (d - floor(d/d2)*d2); 00162 case e_max: return e->value * (d > d2 ? d : d2); 00163 case e_min: return e->value * (d < d2 ? d : d2); 00164 case e_eq: return e->value * (d == d2 ? 1.0 : 0.0); 00165 case e_gt: return e->value * (d > d2 ? 1.0 : 0.0); 00166 case e_gte: return e->value * (d >= d2 ? 1.0 : 0.0); 00167 case e_pow: return e->value * pow(d, d2); 00168 case e_mul: return e->value * (d * d2); 00169 case e_div: return e->value * (d / d2); 00170 case e_add: return e->value * (d + d2); 00171 case e_last:return e->value * d2; 00172 case e_st : return e->value * (p->var[av_clip(d, 0, VARS-1)]= d2); 00173 } 00174 } 00175 } 00176 return NAN; 00177 } 00178 00179 static int parse_expr(AVExpr **e, Parser *p); 00180 00181 void av_expr_free(AVExpr *e) 00182 { 00183 if (!e) return; 00184 av_expr_free(e->param[0]); 00185 av_expr_free(e->param[1]); 00186 av_freep(&e); 00187 } 00188 00189 static int parse_primary(AVExpr **e, Parser *p) 00190 { 00191 AVExpr *d = av_mallocz(sizeof(AVExpr)); 00192 char *next = p->s, *s0 = p->s; 00193 int ret, i; 00194 00195 if (!d) 00196 return AVERROR(ENOMEM); 00197 00198 /* number */ 00199 d->value = av_strtod(p->s, &next); 00200 if (next != p->s) { 00201 d->type = e_value; 00202 p->s= next; 00203 *e = d; 00204 return 0; 00205 } 00206 d->value = 1; 00207 00208 /* named constants */ 00209 for (i=0; p->const_names && p->const_names[i]; i++) { 00210 if (strmatch(p->s, p->const_names[i])) { 00211 p->s+= strlen(p->const_names[i]); 00212 d->type = e_const; 00213 d->a.const_index = i; 00214 *e = d; 00215 return 0; 00216 } 00217 } 00218 00219 p->s= strchr(p->s, '('); 00220 if (p->s==NULL) { 00221 av_log(p, AV_LOG_ERROR, "Undefined constant or missing '(' in '%s'\n", s0); 00222 p->s= next; 00223 av_expr_free(d); 00224 return AVERROR(EINVAL); 00225 } 00226 p->s++; // "(" 00227 if (*next == '(') { // special case do-nothing 00228 av_freep(&d); 00229 if ((ret = parse_expr(&d, p)) < 0) 00230 return ret; 00231 if (p->s[0] != ')') { 00232 av_log(p, AV_LOG_ERROR, "Missing ')' in '%s'\n", s0); 00233 av_expr_free(d); 00234 return AVERROR(EINVAL); 00235 } 00236 p->s++; // ")" 00237 *e = d; 00238 return 0; 00239 } 00240 if ((ret = parse_expr(&(d->param[0]), p)) < 0) { 00241 av_expr_free(d); 00242 return ret; 00243 } 00244 if (p->s[0]== ',') { 00245 p->s++; // "," 00246 parse_expr(&d->param[1], p); 00247 } 00248 if (p->s[0] != ')') { 00249 av_log(p, AV_LOG_ERROR, "Missing ')' or too many args in '%s'\n", s0); 00250 av_expr_free(d); 00251 return AVERROR(EINVAL); 00252 } 00253 p->s++; // ")" 00254 00255 d->type = e_func0; 00256 if (strmatch(next, "sinh" )) d->a.func0 = sinh; 00257 else if (strmatch(next, "cosh" )) d->a.func0 = cosh; 00258 else if (strmatch(next, "tanh" )) d->a.func0 = tanh; 00259 else if (strmatch(next, "sin" )) d->a.func0 = sin; 00260 else if (strmatch(next, "cos" )) d->a.func0 = cos; 00261 else if (strmatch(next, "tan" )) d->a.func0 = tan; 00262 else if (strmatch(next, "atan" )) d->a.func0 = atan; 00263 else if (strmatch(next, "asin" )) d->a.func0 = asin; 00264 else if (strmatch(next, "acos" )) d->a.func0 = acos; 00265 else if (strmatch(next, "exp" )) d->a.func0 = exp; 00266 else if (strmatch(next, "log" )) d->a.func0 = log; 00267 else if (strmatch(next, "abs" )) d->a.func0 = fabs; 00268 else if (strmatch(next, "squish")) d->type = e_squish; 00269 else if (strmatch(next, "gauss" )) d->type = e_gauss; 00270 else if (strmatch(next, "mod" )) d->type = e_mod; 00271 else if (strmatch(next, "max" )) d->type = e_max; 00272 else if (strmatch(next, "min" )) d->type = e_min; 00273 else if (strmatch(next, "eq" )) d->type = e_eq; 00274 else if (strmatch(next, "gte" )) d->type = e_gte; 00275 else if (strmatch(next, "gt" )) d->type = e_gt; 00276 else if (strmatch(next, "lte" )) { AVExpr *tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gt; } 00277 else if (strmatch(next, "lt" )) { AVExpr *tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gte; } 00278 else if (strmatch(next, "ld" )) d->type = e_ld; 00279 else if (strmatch(next, "isnan" )) d->type = e_isnan; 00280 else if (strmatch(next, "st" )) d->type = e_st; 00281 else if (strmatch(next, "while" )) d->type = e_while; 00282 else if (strmatch(next, "floor" )) d->type = e_floor; 00283 else if (strmatch(next, "ceil" )) d->type = e_ceil; 00284 else if (strmatch(next, "trunc" )) d->type = e_trunc; 00285 else { 00286 for (i=0; p->func1_names && p->func1_names[i]; i++) { 00287 if (strmatch(next, p->func1_names[i])) { 00288 d->a.func1 = p->funcs1[i]; 00289 d->type = e_func1; 00290 *e = d; 00291 return 0; 00292 } 00293 } 00294 00295 for (i=0; p->func2_names && p->func2_names[i]; i++) { 00296 if (strmatch(next, p->func2_names[i])) { 00297 d->a.func2 = p->funcs2[i]; 00298 d->type = e_func2; 00299 *e = d; 00300 return 0; 00301 } 00302 } 00303 00304 av_log(p, AV_LOG_ERROR, "Unknown function in '%s'\n", s0); 00305 av_expr_free(d); 00306 return AVERROR(EINVAL); 00307 } 00308 00309 *e = d; 00310 return 0; 00311 } 00312 00313 static AVExpr *new_eval_expr(int type, int value, AVExpr *p0, AVExpr *p1) 00314 { 00315 AVExpr *e = av_mallocz(sizeof(AVExpr)); 00316 if (!e) 00317 return NULL; 00318 e->type =type ; 00319 e->value =value ; 00320 e->param[0] =p0 ; 00321 e->param[1] =p1 ; 00322 return e; 00323 } 00324 00325 static int parse_pow(AVExpr **e, Parser *p, int *sign) 00326 { 00327 *sign= (*p->s == '+') - (*p->s == '-'); 00328 p->s += *sign&1; 00329 return parse_primary(e, p); 00330 } 00331 00332 static int parse_factor(AVExpr **e, Parser *p) 00333 { 00334 int sign, sign2, ret; 00335 AVExpr *e0, *e1, *e2; 00336 if ((ret = parse_pow(&e0, p, &sign)) < 0) 00337 return ret; 00338 while(p->s[0]=='^'){ 00339 e1 = e0; 00340 p->s++; 00341 if ((ret = parse_pow(&e2, p, &sign2)) < 0) { 00342 av_expr_free(e1); 00343 return ret; 00344 } 00345 e0 = new_eval_expr(e_pow, 1, e1, e2); 00346 if (!e0) { 00347 av_expr_free(e1); 00348 av_expr_free(e2); 00349 return AVERROR(ENOMEM); 00350 } 00351 if (e0->param[1]) e0->param[1]->value *= (sign2|1); 00352 } 00353 if (e0) e0->value *= (sign|1); 00354 00355 *e = e0; 00356 return 0; 00357 } 00358 00359 static int parse_term(AVExpr **e, Parser *p) 00360 { 00361 int ret; 00362 AVExpr *e0, *e1, *e2; 00363 if ((ret = parse_factor(&e0, p)) < 0) 00364 return ret; 00365 while (p->s[0]=='*' || p->s[0]=='/') { 00366 int c= *p->s++; 00367 e1 = e0; 00368 if ((ret = parse_factor(&e2, p)) < 0) { 00369 av_expr_free(e1); 00370 return ret; 00371 } 00372 e0 = new_eval_expr(c == '*' ? e_mul : e_div, 1, e1, e2); 00373 if (!e0) { 00374 av_expr_free(e1); 00375 av_expr_free(e2); 00376 return AVERROR(ENOMEM); 00377 } 00378 } 00379 *e = e0; 00380 return 0; 00381 } 00382 00383 static int parse_subexpr(AVExpr **e, Parser *p) 00384 { 00385 int ret; 00386 AVExpr *e0, *e1, *e2; 00387 if ((ret = parse_term(&e0, p)) < 0) 00388 return ret; 00389 while (*p->s == '+' || *p->s == '-') { 00390 e1 = e0; 00391 if ((ret = parse_term(&e2, p)) < 0) { 00392 av_expr_free(e1); 00393 return ret; 00394 } 00395 e0 = new_eval_expr(e_add, 1, e1, e2); 00396 if (!e0) { 00397 av_expr_free(e1); 00398 av_expr_free(e2); 00399 return AVERROR(ENOMEM); 00400 } 00401 }; 00402 00403 *e = e0; 00404 return 0; 00405 } 00406 00407 static int parse_expr(AVExpr **e, Parser *p) 00408 { 00409 int ret; 00410 AVExpr *e0, *e1, *e2; 00411 if (p->stack_index <= 0) //protect against stack overflows 00412 return AVERROR(EINVAL); 00413 p->stack_index--; 00414 00415 if ((ret = parse_subexpr(&e0, p)) < 0) 00416 return ret; 00417 while (*p->s == ';') { 00418 p->s++; 00419 e1 = e0; 00420 if ((ret = parse_subexpr(&e2, p)) < 0) { 00421 av_expr_free(e1); 00422 return ret; 00423 } 00424 e0 = new_eval_expr(e_last, 1, e1, e2); 00425 if (!e0) { 00426 av_expr_free(e1); 00427 av_expr_free(e2); 00428 return AVERROR(ENOMEM); 00429 } 00430 }; 00431 00432 p->stack_index++; 00433 *e = e0; 00434 return 0; 00435 } 00436 00437 static int verify_expr(AVExpr *e) 00438 { 00439 if (!e) return 0; 00440 switch (e->type) { 00441 case e_value: 00442 case e_const: return 1; 00443 case e_func0: 00444 case e_func1: 00445 case e_squish: 00446 case e_ld: 00447 case e_gauss: 00448 case e_isnan: 00449 case e_floor: 00450 case e_ceil: 00451 case e_trunc: 00452 return verify_expr(e->param[0]); 00453 default: return verify_expr(e->param[0]) && verify_expr(e->param[1]); 00454 } 00455 } 00456 00457 int av_expr_parse(AVExpr **expr, const char *s, 00458 const char * const *const_names, 00459 const char * const *func1_names, double (* const *funcs1)(void *, double), 00460 const char * const *func2_names, double (* const *funcs2)(void *, double, double), 00461 int log_offset, void *log_ctx) 00462 { 00463 Parser p; 00464 AVExpr *e = NULL; 00465 char *w = av_malloc(strlen(s) + 1); 00466 char *wp = w; 00467 const char *s0 = s; 00468 int ret = 0; 00469 00470 if (!w) 00471 return AVERROR(ENOMEM); 00472 00473 while (*s) 00474 if (!isspace(*s++)) *wp++ = s[-1]; 00475 *wp++ = 0; 00476 00477 p.class = &class; 00478 p.stack_index=100; 00479 p.s= w; 00480 p.const_names = const_names; 00481 p.funcs1 = funcs1; 00482 p.func1_names = func1_names; 00483 p.funcs2 = funcs2; 00484 p.func2_names = func2_names; 00485 p.log_offset = log_offset; 00486 p.log_ctx = log_ctx; 00487 00488 if ((ret = parse_expr(&e, &p)) < 0) 00489 goto end; 00490 if (*p.s) { 00491 av_expr_free(e); 00492 av_log(&p, AV_LOG_ERROR, "Invalid chars '%s' at the end of expression '%s'\n", p.s, s0); 00493 ret = AVERROR(EINVAL); 00494 goto end; 00495 } 00496 if (!verify_expr(e)) { 00497 av_expr_free(e); 00498 ret = AVERROR(EINVAL); 00499 goto end; 00500 } 00501 *expr = e; 00502 end: 00503 av_free(w); 00504 return ret; 00505 } 00506 00507 double av_expr_eval(AVExpr *e, const double *const_values, void *opaque) 00508 { 00509 Parser p; 00510 00511 p.const_values = const_values; 00512 p.opaque = opaque; 00513 return eval_expr(&p, e); 00514 } 00515 00516 int av_expr_parse_and_eval(double *d, const char *s, 00517 const char * const *const_names, const double *const_values, 00518 const char * const *func1_names, double (* const *funcs1)(void *, double), 00519 const char * const *func2_names, double (* const *funcs2)(void *, double, double), 00520 void *opaque, int log_offset, void *log_ctx) 00521 { 00522 AVExpr *e = NULL; 00523 int ret = av_expr_parse(&e, s, const_names, func1_names, funcs1, func2_names, funcs2, log_offset, log_ctx); 00524 00525 if (ret < 0) { 00526 *d = NAN; 00527 return ret; 00528 } 00529 *d = av_expr_eval(e, const_values, opaque); 00530 av_expr_free(e); 00531 return isnan(*d) ? AVERROR(EINVAL) : 0; 00532 } 00533 00534 #ifdef TEST 00535 #undef printf 00536 static double const_values[] = { 00537 M_PI, 00538 M_E, 00539 0 00540 }; 00541 00542 static const char *const_names[] = { 00543 "PI", 00544 "E", 00545 0 00546 }; 00547 00548 int main(void) 00549 { 00550 int i; 00551 double d; 00552 const char **expr, *exprs[] = { 00553 "", 00554 "1;2", 00555 "-20", 00556 "-PI", 00557 "+PI", 00558 "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", 00559 "80G/80Gi" 00560 "1k", 00561 "1Gi", 00562 "1gi", 00563 "1GiFoo", 00564 "1k+1k", 00565 "1Gi*3foo", 00566 "foo", 00567 "foo(", 00568 "foo()", 00569 "foo)", 00570 "sin", 00571 "sin(", 00572 "sin()", 00573 "sin)", 00574 "sin 10", 00575 "sin(1,2,3)", 00576 "sin(1 )", 00577 "1", 00578 "1foo", 00579 "bar + PI + E + 100f*2 + foo", 00580 "13k + 12f - foo(1, 2)", 00581 "1gi", 00582 "1Gi", 00583 "st(0, 123)", 00584 "st(1, 123); ld(1)", 00585 /* compute 1+2+...+N */ 00586 "st(0, 1); while(lte(ld(0), 100), st(1, ld(1)+ld(0));st(0, ld(0)+1)); ld(1)", 00587 /* compute Fib(N) */ 00588 "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)", 00589 "while(0, 10)", 00590 "st(0, 1); while(lte(ld(0),100), st(1, ld(1)+ld(0)); st(0, ld(0)+1))", 00591 "isnan(1)", 00592 "isnan(NAN)", 00593 "floor(NAN)", 00594 "floor(123.123)", 00595 "floor(-123.123)", 00596 "trunc(123.123)", 00597 "trunc(-123.123)", 00598 "ceil(123.123)", 00599 "ceil(-123.123)", 00600 NULL 00601 }; 00602 00603 for (expr = exprs; *expr; expr++) { 00604 printf("Evaluating '%s'\n", *expr); 00605 av_expr_parse_and_eval(&d, *expr, 00606 const_names, const_values, 00607 NULL, NULL, NULL, NULL, NULL, 0, NULL); 00608 printf("'%s' -> %f\n\n", *expr, d); 00609 } 00610 00611 av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", 00612 const_names, const_values, 00613 NULL, NULL, NULL, NULL, NULL, 0, NULL); 00614 printf("%f == 12.7\n", d); 00615 av_expr_parse_and_eval(&d, "80G/80Gi", 00616 const_names, const_values, 00617 NULL, NULL, NULL, NULL, NULL, 0, NULL); 00618 printf("%f == 0.931322575\n", d); 00619 00620 for (i=0; i<1050; i++) { 00621 START_TIMER 00622 av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", 00623 const_names, const_values, 00624 NULL, NULL, NULL, NULL, NULL, 0, NULL); 00625 STOP_TIMER("av_expr_parse_and_eval") 00626 } 00627 return 0; 00628 } 00629 #endif