You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

681 lines
20KB

  1. /*
  2. * Copyright (c) 2002-2006 Michael Niedermayer <michaelni@gmx.at>
  3. * Copyright (c) 2006 Oded Shimon <ods15@ods15.dyndns.org>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * simple arithmetic expression evaluator.
  24. *
  25. * see http://joe.hotchkiss.com/programming/eval/eval.html
  26. */
  27. #include "avutil.h"
  28. #include "eval.h"
  29. typedef struct Parser {
  30. const AVClass *class;
  31. int stack_index;
  32. char *s;
  33. const double *const_values;
  34. const char * const *const_names; // NULL terminated
  35. double (* const *funcs1)(void *, double a); // NULL terminated
  36. const char * const *func1_names; // NULL terminated
  37. double (* const *funcs2)(void *, double a, double b); // NULL terminated
  38. const char * const *func2_names; // NULL terminated
  39. void *opaque;
  40. int log_offset;
  41. void *log_ctx;
  42. #define VARS 10
  43. double var[VARS];
  44. } Parser;
  45. static const AVClass class = { "Eval", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT, offsetof(Parser,log_offset), offsetof(Parser,log_ctx) };
  46. static const int8_t si_prefixes['z' - 'E' + 1] = {
  47. ['y'-'E']= -24,
  48. ['z'-'E']= -21,
  49. ['a'-'E']= -18,
  50. ['f'-'E']= -15,
  51. ['p'-'E']= -12,
  52. ['n'-'E']= - 9,
  53. ['u'-'E']= - 6,
  54. ['m'-'E']= - 3,
  55. ['c'-'E']= - 2,
  56. ['d'-'E']= - 1,
  57. ['h'-'E']= 2,
  58. ['k'-'E']= 3,
  59. ['K'-'E']= 3,
  60. ['M'-'E']= 6,
  61. ['G'-'E']= 9,
  62. ['T'-'E']= 12,
  63. ['P'-'E']= 15,
  64. ['E'-'E']= 18,
  65. ['Z'-'E']= 21,
  66. ['Y'-'E']= 24,
  67. };
  68. double av_strtod(const char *numstr, char **tail)
  69. {
  70. double d;
  71. char *next;
  72. if(numstr[0]=='0' && (numstr[1]|0x20)=='x') {
  73. d = strtoul(numstr, &next, 16);
  74. } else
  75. d = strtod(numstr, &next);
  76. /* if parsing succeeded, check for and interpret postfixes */
  77. if (next!=numstr) {
  78. if (*next >= 'E' && *next <= 'z') {
  79. int e= si_prefixes[*next - 'E'];
  80. if (e) {
  81. if (next[1] == 'i') {
  82. d*= pow( 2, e/0.3);
  83. next+=2;
  84. } else {
  85. d*= pow(10, e);
  86. next++;
  87. }
  88. }
  89. }
  90. if (*next=='B') {
  91. d*=8;
  92. next++;
  93. }
  94. }
  95. /* if requested, fill in tail with the position after the last parsed
  96. character */
  97. if (tail)
  98. *tail = next;
  99. return d;
  100. }
  101. #define IS_IDENTIFIER_CHAR(c) ((c) - '0' <= 9U || (c) - 'a' <= 25U || (c) - 'A' <= 25U || (c) == '_')
  102. static int strmatch(const char *s, const char *prefix)
  103. {
  104. int i;
  105. for (i=0; prefix[i]; i++) {
  106. if (prefix[i] != s[i]) return 0;
  107. }
  108. /* return 1 only if the s identifier is terminated */
  109. return !IS_IDENTIFIER_CHAR(s[i]);
  110. }
  111. struct AVExpr {
  112. enum {
  113. e_value, e_const, e_func0, e_func1, e_func2,
  114. e_squish, e_gauss, e_ld, e_isnan,
  115. e_mod, e_max, e_min, e_eq, e_gt, e_gte,
  116. e_pow, e_mul, e_div, e_add,
  117. e_last, e_st, e_while, e_floor, e_ceil, e_trunc,
  118. e_sqrt, e_not,
  119. } type;
  120. double value; // is sign in other types
  121. union {
  122. int const_index;
  123. double (*func0)(double);
  124. double (*func1)(void *, double);
  125. double (*func2)(void *, double, double);
  126. } a;
  127. struct AVExpr *param[2];
  128. };
  129. static double eval_expr(Parser *p, AVExpr *e)
  130. {
  131. switch (e->type) {
  132. case e_value: return e->value;
  133. case e_const: return e->value * p->const_values[e->a.const_index];
  134. case e_func0: return e->value * e->a.func0(eval_expr(p, e->param[0]));
  135. case e_func1: return e->value * e->a.func1(p->opaque, eval_expr(p, e->param[0]));
  136. case e_func2: return e->value * e->a.func2(p->opaque, eval_expr(p, e->param[0]), eval_expr(p, e->param[1]));
  137. case e_squish: return 1/(1+exp(4*eval_expr(p, e->param[0])));
  138. case e_gauss: { double d = eval_expr(p, e->param[0]); return exp(-d*d/2)/sqrt(2*M_PI); }
  139. case e_ld: return e->value * p->var[av_clip(eval_expr(p, e->param[0]), 0, VARS-1)];
  140. case e_isnan: return e->value * !!isnan(eval_expr(p, e->param[0]));
  141. case e_floor: return e->value * floor(eval_expr(p, e->param[0]));
  142. case e_ceil : return e->value * ceil (eval_expr(p, e->param[0]));
  143. case e_trunc: return e->value * trunc(eval_expr(p, e->param[0]));
  144. case e_sqrt: return e->value * sqrt (eval_expr(p, e->param[0]));
  145. case e_not: return e->value * eval_expr(p, e->param[0]) == 0;
  146. case e_while: {
  147. double d = NAN;
  148. while (eval_expr(p, e->param[0]))
  149. d=eval_expr(p, e->param[1]);
  150. return d;
  151. }
  152. default: {
  153. double d = eval_expr(p, e->param[0]);
  154. double d2 = eval_expr(p, e->param[1]);
  155. switch (e->type) {
  156. case e_mod: return e->value * (d - floor(d/d2)*d2);
  157. case e_max: return e->value * (d > d2 ? d : d2);
  158. case e_min: return e->value * (d < d2 ? d : d2);
  159. case e_eq: return e->value * (d == d2 ? 1.0 : 0.0);
  160. case e_gt: return e->value * (d > d2 ? 1.0 : 0.0);
  161. case e_gte: return e->value * (d >= d2 ? 1.0 : 0.0);
  162. case e_pow: return e->value * pow(d, d2);
  163. case e_mul: return e->value * (d * d2);
  164. case e_div: return e->value * (d / d2);
  165. case e_add: return e->value * (d + d2);
  166. case e_last:return e->value * d2;
  167. case e_st : return e->value * (p->var[av_clip(d, 0, VARS-1)]= d2);
  168. }
  169. }
  170. }
  171. return NAN;
  172. }
  173. static int parse_expr(AVExpr **e, Parser *p);
  174. void av_expr_free(AVExpr *e)
  175. {
  176. if (!e) return;
  177. av_expr_free(e->param[0]);
  178. av_expr_free(e->param[1]);
  179. av_freep(&e);
  180. }
  181. static int parse_primary(AVExpr **e, Parser *p)
  182. {
  183. AVExpr *d = av_mallocz(sizeof(AVExpr));
  184. char *next = p->s, *s0 = p->s;
  185. int ret, i;
  186. if (!d)
  187. return AVERROR(ENOMEM);
  188. /* number */
  189. d->value = av_strtod(p->s, &next);
  190. if (next != p->s) {
  191. d->type = e_value;
  192. p->s= next;
  193. *e = d;
  194. return 0;
  195. }
  196. d->value = 1;
  197. /* named constants */
  198. for (i=0; p->const_names && p->const_names[i]; i++) {
  199. if (strmatch(p->s, p->const_names[i])) {
  200. p->s+= strlen(p->const_names[i]);
  201. d->type = e_const;
  202. d->a.const_index = i;
  203. *e = d;
  204. return 0;
  205. }
  206. }
  207. p->s= strchr(p->s, '(');
  208. if (p->s==NULL) {
  209. av_log(p, AV_LOG_ERROR, "Undefined constant or missing '(' in '%s'\n", s0);
  210. p->s= next;
  211. av_expr_free(d);
  212. return AVERROR(EINVAL);
  213. }
  214. p->s++; // "("
  215. if (*next == '(') { // special case do-nothing
  216. av_freep(&d);
  217. if ((ret = parse_expr(&d, p)) < 0)
  218. return ret;
  219. if (p->s[0] != ')') {
  220. av_log(p, AV_LOG_ERROR, "Missing ')' in '%s'\n", s0);
  221. av_expr_free(d);
  222. return AVERROR(EINVAL);
  223. }
  224. p->s++; // ")"
  225. *e = d;
  226. return 0;
  227. }
  228. if ((ret = parse_expr(&(d->param[0]), p)) < 0) {
  229. av_expr_free(d);
  230. return ret;
  231. }
  232. if (p->s[0]== ',') {
  233. p->s++; // ","
  234. parse_expr(&d->param[1], p);
  235. }
  236. if (p->s[0] != ')') {
  237. av_log(p, AV_LOG_ERROR, "Missing ')' or too many args in '%s'\n", s0);
  238. av_expr_free(d);
  239. return AVERROR(EINVAL);
  240. }
  241. p->s++; // ")"
  242. d->type = e_func0;
  243. if (strmatch(next, "sinh" )) d->a.func0 = sinh;
  244. else if (strmatch(next, "cosh" )) d->a.func0 = cosh;
  245. else if (strmatch(next, "tanh" )) d->a.func0 = tanh;
  246. else if (strmatch(next, "sin" )) d->a.func0 = sin;
  247. else if (strmatch(next, "cos" )) d->a.func0 = cos;
  248. else if (strmatch(next, "tan" )) d->a.func0 = tan;
  249. else if (strmatch(next, "atan" )) d->a.func0 = atan;
  250. else if (strmatch(next, "asin" )) d->a.func0 = asin;
  251. else if (strmatch(next, "acos" )) d->a.func0 = acos;
  252. else if (strmatch(next, "exp" )) d->a.func0 = exp;
  253. else if (strmatch(next, "log" )) d->a.func0 = log;
  254. else if (strmatch(next, "abs" )) d->a.func0 = fabs;
  255. else if (strmatch(next, "squish")) d->type = e_squish;
  256. else if (strmatch(next, "gauss" )) d->type = e_gauss;
  257. else if (strmatch(next, "mod" )) d->type = e_mod;
  258. else if (strmatch(next, "max" )) d->type = e_max;
  259. else if (strmatch(next, "min" )) d->type = e_min;
  260. else if (strmatch(next, "eq" )) d->type = e_eq;
  261. else if (strmatch(next, "gte" )) d->type = e_gte;
  262. else if (strmatch(next, "gt" )) d->type = e_gt;
  263. else if (strmatch(next, "lte" )) { AVExpr *tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gt; }
  264. else if (strmatch(next, "lt" )) { AVExpr *tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gte; }
  265. else if (strmatch(next, "ld" )) d->type = e_ld;
  266. else if (strmatch(next, "isnan" )) d->type = e_isnan;
  267. else if (strmatch(next, "st" )) d->type = e_st;
  268. else if (strmatch(next, "while" )) d->type = e_while;
  269. else if (strmatch(next, "floor" )) d->type = e_floor;
  270. else if (strmatch(next, "ceil" )) d->type = e_ceil;
  271. else if (strmatch(next, "trunc" )) d->type = e_trunc;
  272. else if (strmatch(next, "sqrt" )) d->type = e_sqrt;
  273. else if (strmatch(next, "not" )) d->type = e_not;
  274. else if (strmatch(next, "pow" )) d->type = e_pow;
  275. else {
  276. for (i=0; p->func1_names && p->func1_names[i]; i++) {
  277. if (strmatch(next, p->func1_names[i])) {
  278. d->a.func1 = p->funcs1[i];
  279. d->type = e_func1;
  280. *e = d;
  281. return 0;
  282. }
  283. }
  284. for (i=0; p->func2_names && p->func2_names[i]; i++) {
  285. if (strmatch(next, p->func2_names[i])) {
  286. d->a.func2 = p->funcs2[i];
  287. d->type = e_func2;
  288. *e = d;
  289. return 0;
  290. }
  291. }
  292. av_log(p, AV_LOG_ERROR, "Unknown function in '%s'\n", s0);
  293. av_expr_free(d);
  294. return AVERROR(EINVAL);
  295. }
  296. *e = d;
  297. return 0;
  298. }
  299. static AVExpr *new_eval_expr(int type, int value, AVExpr *p0, AVExpr *p1)
  300. {
  301. AVExpr *e = av_mallocz(sizeof(AVExpr));
  302. if (!e)
  303. return NULL;
  304. e->type =type ;
  305. e->value =value ;
  306. e->param[0] =p0 ;
  307. e->param[1] =p1 ;
  308. return e;
  309. }
  310. static int parse_pow(AVExpr **e, Parser *p, int *sign)
  311. {
  312. *sign= (*p->s == '+') - (*p->s == '-');
  313. p->s += *sign&1;
  314. return parse_primary(e, p);
  315. }
  316. static int parse_factor(AVExpr **e, Parser *p)
  317. {
  318. int sign, sign2, ret;
  319. AVExpr *e0, *e1, *e2;
  320. if ((ret = parse_pow(&e0, p, &sign)) < 0)
  321. return ret;
  322. while(p->s[0]=='^'){
  323. e1 = e0;
  324. p->s++;
  325. if ((ret = parse_pow(&e2, p, &sign2)) < 0) {
  326. av_expr_free(e1);
  327. return ret;
  328. }
  329. e0 = new_eval_expr(e_pow, 1, e1, e2);
  330. if (!e0) {
  331. av_expr_free(e1);
  332. av_expr_free(e2);
  333. return AVERROR(ENOMEM);
  334. }
  335. if (e0->param[1]) e0->param[1]->value *= (sign2|1);
  336. }
  337. if (e0) e0->value *= (sign|1);
  338. *e = e0;
  339. return 0;
  340. }
  341. static int parse_term(AVExpr **e, Parser *p)
  342. {
  343. int ret;
  344. AVExpr *e0, *e1, *e2;
  345. if ((ret = parse_factor(&e0, p)) < 0)
  346. return ret;
  347. while (p->s[0]=='*' || p->s[0]=='/') {
  348. int c= *p->s++;
  349. e1 = e0;
  350. if ((ret = parse_factor(&e2, p)) < 0) {
  351. av_expr_free(e1);
  352. return ret;
  353. }
  354. e0 = new_eval_expr(c == '*' ? e_mul : e_div, 1, e1, e2);
  355. if (!e0) {
  356. av_expr_free(e1);
  357. av_expr_free(e2);
  358. return AVERROR(ENOMEM);
  359. }
  360. }
  361. *e = e0;
  362. return 0;
  363. }
  364. static int parse_subexpr(AVExpr **e, Parser *p)
  365. {
  366. int ret;
  367. AVExpr *e0, *e1, *e2;
  368. if ((ret = parse_term(&e0, p)) < 0)
  369. return ret;
  370. while (*p->s == '+' || *p->s == '-') {
  371. e1 = e0;
  372. if ((ret = parse_term(&e2, p)) < 0) {
  373. av_expr_free(e1);
  374. return ret;
  375. }
  376. e0 = new_eval_expr(e_add, 1, e1, e2);
  377. if (!e0) {
  378. av_expr_free(e1);
  379. av_expr_free(e2);
  380. return AVERROR(ENOMEM);
  381. }
  382. };
  383. *e = e0;
  384. return 0;
  385. }
  386. static int parse_expr(AVExpr **e, Parser *p)
  387. {
  388. int ret;
  389. AVExpr *e0, *e1, *e2;
  390. if (p->stack_index <= 0) //protect against stack overflows
  391. return AVERROR(EINVAL);
  392. p->stack_index--;
  393. if ((ret = parse_subexpr(&e0, p)) < 0)
  394. return ret;
  395. while (*p->s == ';') {
  396. p->s++;
  397. e1 = e0;
  398. if ((ret = parse_subexpr(&e2, p)) < 0) {
  399. av_expr_free(e1);
  400. return ret;
  401. }
  402. e0 = new_eval_expr(e_last, 1, e1, e2);
  403. if (!e0) {
  404. av_expr_free(e1);
  405. av_expr_free(e2);
  406. return AVERROR(ENOMEM);
  407. }
  408. };
  409. p->stack_index++;
  410. *e = e0;
  411. return 0;
  412. }
  413. static int verify_expr(AVExpr *e)
  414. {
  415. if (!e) return 0;
  416. switch (e->type) {
  417. case e_value:
  418. case e_const: return 1;
  419. case e_func0:
  420. case e_func1:
  421. case e_squish:
  422. case e_ld:
  423. case e_gauss:
  424. case e_isnan:
  425. case e_floor:
  426. case e_ceil:
  427. case e_trunc:
  428. case e_sqrt:
  429. case e_not:
  430. return verify_expr(e->param[0]);
  431. default: return verify_expr(e->param[0]) && verify_expr(e->param[1]);
  432. }
  433. }
  434. int av_expr_parse(AVExpr **expr, const char *s,
  435. const char * const *const_names,
  436. const char * const *func1_names, double (* const *funcs1)(void *, double),
  437. const char * const *func2_names, double (* const *funcs2)(void *, double, double),
  438. int log_offset, void *log_ctx)
  439. {
  440. Parser p;
  441. AVExpr *e = NULL;
  442. char *w = av_malloc(strlen(s) + 1);
  443. char *wp = w;
  444. const char *s0 = s;
  445. int ret = 0;
  446. if (!w)
  447. return AVERROR(ENOMEM);
  448. while (*s)
  449. if (!isspace(*s++)) *wp++ = s[-1];
  450. *wp++ = 0;
  451. p.class = &class;
  452. p.stack_index=100;
  453. p.s= w;
  454. p.const_names = const_names;
  455. p.funcs1 = funcs1;
  456. p.func1_names = func1_names;
  457. p.funcs2 = funcs2;
  458. p.func2_names = func2_names;
  459. p.log_offset = log_offset;
  460. p.log_ctx = log_ctx;
  461. if ((ret = parse_expr(&e, &p)) < 0)
  462. goto end;
  463. if (*p.s) {
  464. av_log(&p, AV_LOG_ERROR, "Invalid chars '%s' at the end of expression '%s'\n", p.s, s0);
  465. ret = AVERROR(EINVAL);
  466. goto end;
  467. }
  468. if (!verify_expr(e)) {
  469. av_expr_free(e);
  470. ret = AVERROR(EINVAL);
  471. goto end;
  472. }
  473. *expr = e;
  474. end:
  475. av_free(w);
  476. return ret;
  477. }
  478. double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
  479. {
  480. Parser p;
  481. p.const_values = const_values;
  482. p.opaque = opaque;
  483. return eval_expr(&p, e);
  484. }
  485. int av_expr_parse_and_eval(double *d, const char *s,
  486. const char * const *const_names, const double *const_values,
  487. const char * const *func1_names, double (* const *funcs1)(void *, double),
  488. const char * const *func2_names, double (* const *funcs2)(void *, double, double),
  489. void *opaque, int log_offset, void *log_ctx)
  490. {
  491. AVExpr *e = NULL;
  492. int ret = av_expr_parse(&e, s, const_names, func1_names, funcs1, func2_names, funcs2, log_offset, log_ctx);
  493. if (ret < 0) {
  494. *d = NAN;
  495. return ret;
  496. }
  497. *d = av_expr_eval(e, const_values, opaque);
  498. av_expr_free(e);
  499. return isnan(*d) ? AVERROR(EINVAL) : 0;
  500. }
  501. #if FF_API_OLD_EVAL_NAMES
  502. int av_parse_expr(AVExpr **expr, const char *s,
  503. const char * const *const_names,
  504. const char * const *func1_names, double (* const *funcs1)(void *, double),
  505. const char * const *func2_names, double (* const *funcs2)(void *, double, double),
  506. int log_offset, void *log_ctx)
  507. {
  508. return av_expr_parse(expr, s, const_names, func1_names, funcs1, func2_names, funcs2,
  509. log_offset, log_ctx);
  510. }
  511. double av_eval_expr(AVExpr *e, const double *const_values, void *opaque)
  512. {
  513. return av_expr_eval(e, const_values, opaque);
  514. }
  515. int av_parse_and_eval_expr(double *res, const char *s,
  516. const char * const *const_names, const double *const_values,
  517. const char * const *func1_names, double (* const *funcs1)(void *, double),
  518. const char * const *func2_names, double (* const *funcs2)(void *, double, double),
  519. void *opaque, int log_offset, void *log_ctx)
  520. {
  521. return av_expr_parse_and_eval(res, s, const_names, const_values, func1_names, funcs1, func2_names, funcs2,
  522. opaque, log_offset, log_ctx);
  523. }
  524. void av_free_expr(AVExpr *e)
  525. {
  526. av_expr_free(e);
  527. }
  528. #endif /* FF_API_OLD_EVAL_NAMES */
  529. #ifdef TEST
  530. #undef printf
  531. static double const_values[] = {
  532. M_PI,
  533. M_E,
  534. 0
  535. };
  536. static const char *const_names[] = {
  537. "PI",
  538. "E",
  539. 0
  540. };
  541. int main(void)
  542. {
  543. int i;
  544. double d;
  545. const char **expr, *exprs[] = {
  546. "",
  547. "1;2",
  548. "-20",
  549. "-PI",
  550. "+PI",
  551. "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
  552. "80G/80Gi"
  553. "1k",
  554. "1Gi",
  555. "1gi",
  556. "1GiFoo",
  557. "1k+1k",
  558. "1Gi*3foo",
  559. "foo",
  560. "foo(",
  561. "foo()",
  562. "foo)",
  563. "sin",
  564. "sin(",
  565. "sin()",
  566. "sin)",
  567. "sin 10",
  568. "sin(1,2,3)",
  569. "sin(1 )",
  570. "1",
  571. "1foo",
  572. "bar + PI + E + 100f*2 + foo",
  573. "13k + 12f - foo(1, 2)",
  574. "1gi",
  575. "1Gi",
  576. "st(0, 123)",
  577. "st(1, 123); ld(1)",
  578. /* compute 1+2+...+N */
  579. "st(0, 1); while(lte(ld(0), 100), st(1, ld(1)+ld(0));st(0, ld(0)+1)); ld(1)",
  580. /* compute Fib(N) */
  581. "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)",
  582. "while(0, 10)",
  583. "st(0, 1); while(lte(ld(0),100), st(1, ld(1)+ld(0)); st(0, ld(0)+1))",
  584. "isnan(1)",
  585. "isnan(NAN)",
  586. "floor(NAN)",
  587. "floor(123.123)",
  588. "floor(-123.123)",
  589. "trunc(123.123)",
  590. "trunc(-123.123)",
  591. "ceil(123.123)",
  592. "ceil(-123.123)",
  593. "sqrt(1764)",
  594. "sqrt(-1)",
  595. "not(1)",
  596. "not(NAN)",
  597. "not(0)",
  598. "pow(0,1.23)",
  599. "pow(PI,1.23)",
  600. "PI^1.23",
  601. "pow(-1,1.23)",
  602. NULL
  603. };
  604. for (expr = exprs; *expr; expr++) {
  605. printf("Evaluating '%s'\n", *expr);
  606. av_expr_parse_and_eval(&d, *expr,
  607. const_names, const_values,
  608. NULL, NULL, NULL, NULL, NULL, 0, NULL);
  609. printf("'%s' -> %f\n\n", *expr, d);
  610. }
  611. av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
  612. const_names, const_values,
  613. NULL, NULL, NULL, NULL, NULL, 0, NULL);
  614. printf("%f == 12.7\n", d);
  615. av_expr_parse_and_eval(&d, "80G/80Gi",
  616. const_names, const_values,
  617. NULL, NULL, NULL, NULL, NULL, 0, NULL);
  618. printf("%f == 0.931322575\n", d);
  619. for (i=0; i<1050; i++) {
  620. START_TIMER
  621. av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
  622. const_names, const_values,
  623. NULL, NULL, NULL, NULL, NULL, 0, NULL);
  624. STOP_TIMER("av_expr_parse_and_eval")
  625. }
  626. return 0;
  627. }
  628. #endif