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.

676 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 "libavutil/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 = strtol(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 {
  275. for (i=0; p->func1_names && p->func1_names[i]; i++) {
  276. if (strmatch(next, p->func1_names[i])) {
  277. d->a.func1 = p->funcs1[i];
  278. d->type = e_func1;
  279. *e = d;
  280. return 0;
  281. }
  282. }
  283. for (i=0; p->func2_names && p->func2_names[i]; i++) {
  284. if (strmatch(next, p->func2_names[i])) {
  285. d->a.func2 = p->funcs2[i];
  286. d->type = e_func2;
  287. *e = d;
  288. return 0;
  289. }
  290. }
  291. av_log(p, AV_LOG_ERROR, "Unknown function in '%s'\n", s0);
  292. av_expr_free(d);
  293. return AVERROR(EINVAL);
  294. }
  295. *e = d;
  296. return 0;
  297. }
  298. static AVExpr *new_eval_expr(int type, int value, AVExpr *p0, AVExpr *p1)
  299. {
  300. AVExpr *e = av_mallocz(sizeof(AVExpr));
  301. if (!e)
  302. return NULL;
  303. e->type =type ;
  304. e->value =value ;
  305. e->param[0] =p0 ;
  306. e->param[1] =p1 ;
  307. return e;
  308. }
  309. static int parse_pow(AVExpr **e, Parser *p, int *sign)
  310. {
  311. *sign= (*p->s == '+') - (*p->s == '-');
  312. p->s += *sign&1;
  313. return parse_primary(e, p);
  314. }
  315. static int parse_factor(AVExpr **e, Parser *p)
  316. {
  317. int sign, sign2, ret;
  318. AVExpr *e0, *e1, *e2;
  319. if ((ret = parse_pow(&e0, p, &sign)) < 0)
  320. return ret;
  321. while(p->s[0]=='^'){
  322. e1 = e0;
  323. p->s++;
  324. if ((ret = parse_pow(&e2, p, &sign2)) < 0) {
  325. av_expr_free(e1);
  326. return ret;
  327. }
  328. e0 = new_eval_expr(e_pow, 1, e1, e2);
  329. if (!e0) {
  330. av_expr_free(e1);
  331. av_expr_free(e2);
  332. return AVERROR(ENOMEM);
  333. }
  334. if (e0->param[1]) e0->param[1]->value *= (sign2|1);
  335. }
  336. if (e0) e0->value *= (sign|1);
  337. *e = e0;
  338. return 0;
  339. }
  340. static int parse_term(AVExpr **e, Parser *p)
  341. {
  342. int ret;
  343. AVExpr *e0, *e1, *e2;
  344. if ((ret = parse_factor(&e0, p)) < 0)
  345. return ret;
  346. while (p->s[0]=='*' || p->s[0]=='/') {
  347. int c= *p->s++;
  348. e1 = e0;
  349. if ((ret = parse_factor(&e2, p)) < 0) {
  350. av_expr_free(e1);
  351. return ret;
  352. }
  353. e0 = new_eval_expr(c == '*' ? e_mul : e_div, 1, e1, e2);
  354. if (!e0) {
  355. av_expr_free(e1);
  356. av_expr_free(e2);
  357. return AVERROR(ENOMEM);
  358. }
  359. }
  360. *e = e0;
  361. return 0;
  362. }
  363. static int parse_subexpr(AVExpr **e, Parser *p)
  364. {
  365. int ret;
  366. AVExpr *e0, *e1, *e2;
  367. if ((ret = parse_term(&e0, p)) < 0)
  368. return ret;
  369. while (*p->s == '+' || *p->s == '-') {
  370. e1 = e0;
  371. if ((ret = parse_term(&e2, p)) < 0) {
  372. av_expr_free(e1);
  373. return ret;
  374. }
  375. e0 = new_eval_expr(e_add, 1, e1, e2);
  376. if (!e0) {
  377. av_expr_free(e1);
  378. av_expr_free(e2);
  379. return AVERROR(ENOMEM);
  380. }
  381. };
  382. *e = e0;
  383. return 0;
  384. }
  385. static int parse_expr(AVExpr **e, Parser *p)
  386. {
  387. int ret;
  388. AVExpr *e0, *e1, *e2;
  389. if (p->stack_index <= 0) //protect against stack overflows
  390. return AVERROR(EINVAL);
  391. p->stack_index--;
  392. if ((ret = parse_subexpr(&e0, p)) < 0)
  393. return ret;
  394. while (*p->s == ';') {
  395. p->s++;
  396. e1 = e0;
  397. if ((ret = parse_subexpr(&e2, p)) < 0) {
  398. av_expr_free(e1);
  399. return ret;
  400. }
  401. e0 = new_eval_expr(e_last, 1, e1, e2);
  402. if (!e0) {
  403. av_expr_free(e1);
  404. av_expr_free(e2);
  405. return AVERROR(ENOMEM);
  406. }
  407. };
  408. p->stack_index++;
  409. *e = e0;
  410. return 0;
  411. }
  412. static int verify_expr(AVExpr *e)
  413. {
  414. if (!e) return 0;
  415. switch (e->type) {
  416. case e_value:
  417. case e_const: return 1;
  418. case e_func0:
  419. case e_func1:
  420. case e_squish:
  421. case e_ld:
  422. case e_gauss:
  423. case e_isnan:
  424. case e_floor:
  425. case e_ceil:
  426. case e_trunc:
  427. case e_sqrt:
  428. case e_not:
  429. return verify_expr(e->param[0]);
  430. default: return verify_expr(e->param[0]) && verify_expr(e->param[1]);
  431. }
  432. }
  433. int av_expr_parse(AVExpr **expr, const char *s,
  434. const char * const *const_names,
  435. const char * const *func1_names, double (* const *funcs1)(void *, double),
  436. const char * const *func2_names, double (* const *funcs2)(void *, double, double),
  437. int log_offset, void *log_ctx)
  438. {
  439. Parser p;
  440. AVExpr *e = NULL;
  441. char *w = av_malloc(strlen(s) + 1);
  442. char *wp = w;
  443. const char *s0 = s;
  444. int ret = 0;
  445. if (!w)
  446. return AVERROR(ENOMEM);
  447. while (*s)
  448. if (!isspace(*s++)) *wp++ = s[-1];
  449. *wp++ = 0;
  450. p.class = &class;
  451. p.stack_index=100;
  452. p.s= w;
  453. p.const_names = const_names;
  454. p.funcs1 = funcs1;
  455. p.func1_names = func1_names;
  456. p.funcs2 = funcs2;
  457. p.func2_names = func2_names;
  458. p.log_offset = log_offset;
  459. p.log_ctx = log_ctx;
  460. if ((ret = parse_expr(&e, &p)) < 0)
  461. goto end;
  462. if (*p.s) {
  463. av_log(&p, AV_LOG_ERROR, "Invalid chars '%s' at the end of expression '%s'\n", p.s, s0);
  464. ret = AVERROR(EINVAL);
  465. goto end;
  466. }
  467. if (!verify_expr(e)) {
  468. av_expr_free(e);
  469. ret = AVERROR(EINVAL);
  470. goto end;
  471. }
  472. *expr = e;
  473. end:
  474. av_free(w);
  475. return ret;
  476. }
  477. double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
  478. {
  479. Parser p;
  480. p.const_values = const_values;
  481. p.opaque = opaque;
  482. return eval_expr(&p, e);
  483. }
  484. int av_expr_parse_and_eval(double *d, const char *s,
  485. const char * const *const_names, const double *const_values,
  486. const char * const *func1_names, double (* const *funcs1)(void *, double),
  487. const char * const *func2_names, double (* const *funcs2)(void *, double, double),
  488. void *opaque, int log_offset, void *log_ctx)
  489. {
  490. AVExpr *e = NULL;
  491. int ret = av_expr_parse(&e, s, const_names, func1_names, funcs1, func2_names, funcs2, log_offset, log_ctx);
  492. if (ret < 0) {
  493. *d = NAN;
  494. return ret;
  495. }
  496. *d = av_expr_eval(e, const_values, opaque);
  497. av_expr_free(e);
  498. return isnan(*d) ? AVERROR(EINVAL) : 0;
  499. }
  500. #if FF_API_OLD_EVAL_NAMES
  501. int av_parse_expr(AVExpr **expr, const char *s,
  502. const char * const *const_names,
  503. const char * const *func1_names, double (* const *funcs1)(void *, double),
  504. const char * const *func2_names, double (* const *funcs2)(void *, double, double),
  505. int log_offset, void *log_ctx)
  506. {
  507. return av_expr_parse(expr, s, const_names, func1_names, funcs1, func2_names, funcs2,
  508. log_offset, log_ctx);
  509. }
  510. double av_eval_expr(AVExpr *e, const double *const_values, void *opaque)
  511. {
  512. return av_expr_eval(e, const_values, opaque);
  513. }
  514. int av_parse_and_eval_expr(double *res, const char *s,
  515. const char * const *const_names, const double *const_values,
  516. const char * const *func1_names, double (* const *funcs1)(void *, double),
  517. const char * const *func2_names, double (* const *funcs2)(void *, double, double),
  518. void *opaque, int log_offset, void *log_ctx)
  519. {
  520. return av_expr_parse_and_eval(res, s, const_names, const_values, func1_names, funcs1, func2_names, funcs2,
  521. opaque, log_offset, log_ctx);
  522. }
  523. void av_free_expr(AVExpr *e)
  524. {
  525. av_expr_free(e);
  526. }
  527. #endif /* FF_API_OLD_EVAL_NAMES */
  528. #ifdef TEST
  529. #undef printf
  530. static double const_values[] = {
  531. M_PI,
  532. M_E,
  533. 0
  534. };
  535. static const char *const_names[] = {
  536. "PI",
  537. "E",
  538. 0
  539. };
  540. int main(void)
  541. {
  542. int i;
  543. double d;
  544. const char **expr, *exprs[] = {
  545. "",
  546. "1;2",
  547. "-20",
  548. "-PI",
  549. "+PI",
  550. "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
  551. "80G/80Gi"
  552. "1k",
  553. "1Gi",
  554. "1gi",
  555. "1GiFoo",
  556. "1k+1k",
  557. "1Gi*3foo",
  558. "foo",
  559. "foo(",
  560. "foo()",
  561. "foo)",
  562. "sin",
  563. "sin(",
  564. "sin()",
  565. "sin)",
  566. "sin 10",
  567. "sin(1,2,3)",
  568. "sin(1 )",
  569. "1",
  570. "1foo",
  571. "bar + PI + E + 100f*2 + foo",
  572. "13k + 12f - foo(1, 2)",
  573. "1gi",
  574. "1Gi",
  575. "st(0, 123)",
  576. "st(1, 123); ld(1)",
  577. /* compute 1+2+...+N */
  578. "st(0, 1); while(lte(ld(0), 100), st(1, ld(1)+ld(0));st(0, ld(0)+1)); ld(1)",
  579. /* compute Fib(N) */
  580. "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)",
  581. "while(0, 10)",
  582. "st(0, 1); while(lte(ld(0),100), st(1, ld(1)+ld(0)); st(0, ld(0)+1))",
  583. "isnan(1)",
  584. "isnan(NAN)",
  585. "floor(NAN)",
  586. "floor(123.123)",
  587. "floor(-123.123)",
  588. "trunc(123.123)",
  589. "trunc(-123.123)",
  590. "ceil(123.123)",
  591. "ceil(-123.123)",
  592. "sqrt(1764)",
  593. "sqrt(-1)",
  594. "not(1)",
  595. "not(NAN)",
  596. "not(0)",
  597. NULL
  598. };
  599. for (expr = exprs; *expr; expr++) {
  600. printf("Evaluating '%s'\n", *expr);
  601. av_expr_parse_and_eval(&d, *expr,
  602. const_names, const_values,
  603. NULL, NULL, NULL, NULL, NULL, 0, NULL);
  604. printf("'%s' -> %f\n\n", *expr, d);
  605. }
  606. av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
  607. const_names, const_values,
  608. NULL, NULL, NULL, NULL, NULL, 0, NULL);
  609. printf("%f == 12.7\n", d);
  610. av_expr_parse_and_eval(&d, "80G/80Gi",
  611. const_names, const_values,
  612. NULL, NULL, NULL, NULL, NULL, 0, NULL);
  613. printf("%f == 0.931322575\n", d);
  614. for (i=0; i<1050; i++) {
  615. START_TIMER
  616. av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
  617. const_names, const_values,
  618. NULL, NULL, NULL, NULL, NULL, 0, NULL);
  619. STOP_TIMER("av_expr_parse_and_eval")
  620. }
  621. return 0;
  622. }
  623. #endif