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.

670 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,
  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_while: {
  146. double d = NAN;
  147. while (eval_expr(p, e->param[0]))
  148. d=eval_expr(p, e->param[1]);
  149. return d;
  150. }
  151. default: {
  152. double d = eval_expr(p, e->param[0]);
  153. double d2 = eval_expr(p, e->param[1]);
  154. switch (e->type) {
  155. case e_mod: return e->value * (d - floor(d/d2)*d2);
  156. case e_max: return e->value * (d > d2 ? d : d2);
  157. case e_min: return e->value * (d < d2 ? d : d2);
  158. case e_eq: return e->value * (d == d2 ? 1.0 : 0.0);
  159. case e_gt: return e->value * (d > d2 ? 1.0 : 0.0);
  160. case e_gte: return e->value * (d >= d2 ? 1.0 : 0.0);
  161. case e_pow: return e->value * pow(d, d2);
  162. case e_mul: return e->value * (d * d2);
  163. case e_div: return e->value * (d / d2);
  164. case e_add: return e->value * (d + d2);
  165. case e_last:return e->value * d2;
  166. case e_st : return e->value * (p->var[av_clip(d, 0, VARS-1)]= d2);
  167. }
  168. }
  169. }
  170. return NAN;
  171. }
  172. static int parse_expr(AVExpr **e, Parser *p);
  173. void av_expr_free(AVExpr *e)
  174. {
  175. if (!e) return;
  176. av_expr_free(e->param[0]);
  177. av_expr_free(e->param[1]);
  178. av_freep(&e);
  179. }
  180. static int parse_primary(AVExpr **e, Parser *p)
  181. {
  182. AVExpr *d = av_mallocz(sizeof(AVExpr));
  183. char *next = p->s, *s0 = p->s;
  184. int ret, i;
  185. if (!d)
  186. return AVERROR(ENOMEM);
  187. /* number */
  188. d->value = av_strtod(p->s, &next);
  189. if (next != p->s) {
  190. d->type = e_value;
  191. p->s= next;
  192. *e = d;
  193. return 0;
  194. }
  195. d->value = 1;
  196. /* named constants */
  197. for (i=0; p->const_names && p->const_names[i]; i++) {
  198. if (strmatch(p->s, p->const_names[i])) {
  199. p->s+= strlen(p->const_names[i]);
  200. d->type = e_const;
  201. d->a.const_index = i;
  202. *e = d;
  203. return 0;
  204. }
  205. }
  206. p->s= strchr(p->s, '(');
  207. if (p->s==NULL) {
  208. av_log(p, AV_LOG_ERROR, "Undefined constant or missing '(' in '%s'\n", s0);
  209. p->s= next;
  210. av_expr_free(d);
  211. return AVERROR(EINVAL);
  212. }
  213. p->s++; // "("
  214. if (*next == '(') { // special case do-nothing
  215. av_freep(&d);
  216. if ((ret = parse_expr(&d, p)) < 0)
  217. return ret;
  218. if (p->s[0] != ')') {
  219. av_log(p, AV_LOG_ERROR, "Missing ')' in '%s'\n", s0);
  220. av_expr_free(d);
  221. return AVERROR(EINVAL);
  222. }
  223. p->s++; // ")"
  224. *e = d;
  225. return 0;
  226. }
  227. if ((ret = parse_expr(&(d->param[0]), p)) < 0) {
  228. av_expr_free(d);
  229. return ret;
  230. }
  231. if (p->s[0]== ',') {
  232. p->s++; // ","
  233. parse_expr(&d->param[1], p);
  234. }
  235. if (p->s[0] != ')') {
  236. av_log(p, AV_LOG_ERROR, "Missing ')' or too many args in '%s'\n", s0);
  237. av_expr_free(d);
  238. return AVERROR(EINVAL);
  239. }
  240. p->s++; // ")"
  241. d->type = e_func0;
  242. if (strmatch(next, "sinh" )) d->a.func0 = sinh;
  243. else if (strmatch(next, "cosh" )) d->a.func0 = cosh;
  244. else if (strmatch(next, "tanh" )) d->a.func0 = tanh;
  245. else if (strmatch(next, "sin" )) d->a.func0 = sin;
  246. else if (strmatch(next, "cos" )) d->a.func0 = cos;
  247. else if (strmatch(next, "tan" )) d->a.func0 = tan;
  248. else if (strmatch(next, "atan" )) d->a.func0 = atan;
  249. else if (strmatch(next, "asin" )) d->a.func0 = asin;
  250. else if (strmatch(next, "acos" )) d->a.func0 = acos;
  251. else if (strmatch(next, "exp" )) d->a.func0 = exp;
  252. else if (strmatch(next, "log" )) d->a.func0 = log;
  253. else if (strmatch(next, "abs" )) d->a.func0 = fabs;
  254. else if (strmatch(next, "squish")) d->type = e_squish;
  255. else if (strmatch(next, "gauss" )) d->type = e_gauss;
  256. else if (strmatch(next, "mod" )) d->type = e_mod;
  257. else if (strmatch(next, "max" )) d->type = e_max;
  258. else if (strmatch(next, "min" )) d->type = e_min;
  259. else if (strmatch(next, "eq" )) d->type = e_eq;
  260. else if (strmatch(next, "gte" )) d->type = e_gte;
  261. else if (strmatch(next, "gt" )) d->type = e_gt;
  262. else if (strmatch(next, "lte" )) { AVExpr *tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gt; }
  263. else if (strmatch(next, "lt" )) { AVExpr *tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gte; }
  264. else if (strmatch(next, "ld" )) d->type = e_ld;
  265. else if (strmatch(next, "isnan" )) d->type = e_isnan;
  266. else if (strmatch(next, "st" )) d->type = e_st;
  267. else if (strmatch(next, "while" )) d->type = e_while;
  268. else if (strmatch(next, "floor" )) d->type = e_floor;
  269. else if (strmatch(next, "ceil" )) d->type = e_ceil;
  270. else if (strmatch(next, "trunc" )) d->type = e_trunc;
  271. else if (strmatch(next, "sqrt" )) d->type = e_sqrt;
  272. else {
  273. for (i=0; p->func1_names && p->func1_names[i]; i++) {
  274. if (strmatch(next, p->func1_names[i])) {
  275. d->a.func1 = p->funcs1[i];
  276. d->type = e_func1;
  277. *e = d;
  278. return 0;
  279. }
  280. }
  281. for (i=0; p->func2_names && p->func2_names[i]; i++) {
  282. if (strmatch(next, p->func2_names[i])) {
  283. d->a.func2 = p->funcs2[i];
  284. d->type = e_func2;
  285. *e = d;
  286. return 0;
  287. }
  288. }
  289. av_log(p, AV_LOG_ERROR, "Unknown function in '%s'\n", s0);
  290. av_expr_free(d);
  291. return AVERROR(EINVAL);
  292. }
  293. *e = d;
  294. return 0;
  295. }
  296. static AVExpr *new_eval_expr(int type, int value, AVExpr *p0, AVExpr *p1)
  297. {
  298. AVExpr *e = av_mallocz(sizeof(AVExpr));
  299. if (!e)
  300. return NULL;
  301. e->type =type ;
  302. e->value =value ;
  303. e->param[0] =p0 ;
  304. e->param[1] =p1 ;
  305. return e;
  306. }
  307. static int parse_pow(AVExpr **e, Parser *p, int *sign)
  308. {
  309. *sign= (*p->s == '+') - (*p->s == '-');
  310. p->s += *sign&1;
  311. return parse_primary(e, p);
  312. }
  313. static int parse_factor(AVExpr **e, Parser *p)
  314. {
  315. int sign, sign2, ret;
  316. AVExpr *e0, *e1, *e2;
  317. if ((ret = parse_pow(&e0, p, &sign)) < 0)
  318. return ret;
  319. while(p->s[0]=='^'){
  320. e1 = e0;
  321. p->s++;
  322. if ((ret = parse_pow(&e2, p, &sign2)) < 0) {
  323. av_expr_free(e1);
  324. return ret;
  325. }
  326. e0 = new_eval_expr(e_pow, 1, e1, e2);
  327. if (!e0) {
  328. av_expr_free(e1);
  329. av_expr_free(e2);
  330. return AVERROR(ENOMEM);
  331. }
  332. if (e0->param[1]) e0->param[1]->value *= (sign2|1);
  333. }
  334. if (e0) e0->value *= (sign|1);
  335. *e = e0;
  336. return 0;
  337. }
  338. static int parse_term(AVExpr **e, Parser *p)
  339. {
  340. int ret;
  341. AVExpr *e0, *e1, *e2;
  342. if ((ret = parse_factor(&e0, p)) < 0)
  343. return ret;
  344. while (p->s[0]=='*' || p->s[0]=='/') {
  345. int c= *p->s++;
  346. e1 = e0;
  347. if ((ret = parse_factor(&e2, p)) < 0) {
  348. av_expr_free(e1);
  349. return ret;
  350. }
  351. e0 = new_eval_expr(c == '*' ? e_mul : e_div, 1, e1, e2);
  352. if (!e0) {
  353. av_expr_free(e1);
  354. av_expr_free(e2);
  355. return AVERROR(ENOMEM);
  356. }
  357. }
  358. *e = e0;
  359. return 0;
  360. }
  361. static int parse_subexpr(AVExpr **e, Parser *p)
  362. {
  363. int ret;
  364. AVExpr *e0, *e1, *e2;
  365. if ((ret = parse_term(&e0, p)) < 0)
  366. return ret;
  367. while (*p->s == '+' || *p->s == '-') {
  368. e1 = e0;
  369. if ((ret = parse_term(&e2, p)) < 0) {
  370. av_expr_free(e1);
  371. return ret;
  372. }
  373. e0 = new_eval_expr(e_add, 1, e1, e2);
  374. if (!e0) {
  375. av_expr_free(e1);
  376. av_expr_free(e2);
  377. return AVERROR(ENOMEM);
  378. }
  379. };
  380. *e = e0;
  381. return 0;
  382. }
  383. static int parse_expr(AVExpr **e, Parser *p)
  384. {
  385. int ret;
  386. AVExpr *e0, *e1, *e2;
  387. if (p->stack_index <= 0) //protect against stack overflows
  388. return AVERROR(EINVAL);
  389. p->stack_index--;
  390. if ((ret = parse_subexpr(&e0, p)) < 0)
  391. return ret;
  392. while (*p->s == ';') {
  393. p->s++;
  394. e1 = e0;
  395. if ((ret = parse_subexpr(&e2, p)) < 0) {
  396. av_expr_free(e1);
  397. return ret;
  398. }
  399. e0 = new_eval_expr(e_last, 1, e1, e2);
  400. if (!e0) {
  401. av_expr_free(e1);
  402. av_expr_free(e2);
  403. return AVERROR(ENOMEM);
  404. }
  405. };
  406. p->stack_index++;
  407. *e = e0;
  408. return 0;
  409. }
  410. static int verify_expr(AVExpr *e)
  411. {
  412. if (!e) return 0;
  413. switch (e->type) {
  414. case e_value:
  415. case e_const: return 1;
  416. case e_func0:
  417. case e_func1:
  418. case e_squish:
  419. case e_ld:
  420. case e_gauss:
  421. case e_isnan:
  422. case e_floor:
  423. case e_ceil:
  424. case e_trunc:
  425. case e_sqrt:
  426. return verify_expr(e->param[0]);
  427. default: return verify_expr(e->param[0]) && verify_expr(e->param[1]);
  428. }
  429. }
  430. int av_expr_parse(AVExpr **expr, const char *s,
  431. const char * const *const_names,
  432. const char * const *func1_names, double (* const *funcs1)(void *, double),
  433. const char * const *func2_names, double (* const *funcs2)(void *, double, double),
  434. int log_offset, void *log_ctx)
  435. {
  436. Parser p;
  437. AVExpr *e = NULL;
  438. char *w = av_malloc(strlen(s) + 1);
  439. char *wp = w;
  440. const char *s0 = s;
  441. int ret = 0;
  442. if (!w)
  443. return AVERROR(ENOMEM);
  444. while (*s)
  445. if (!isspace(*s++)) *wp++ = s[-1];
  446. *wp++ = 0;
  447. p.class = &class;
  448. p.stack_index=100;
  449. p.s= w;
  450. p.const_names = const_names;
  451. p.funcs1 = funcs1;
  452. p.func1_names = func1_names;
  453. p.funcs2 = funcs2;
  454. p.func2_names = func2_names;
  455. p.log_offset = log_offset;
  456. p.log_ctx = log_ctx;
  457. if ((ret = parse_expr(&e, &p)) < 0)
  458. goto end;
  459. if (*p.s) {
  460. av_log(&p, AV_LOG_ERROR, "Invalid chars '%s' at the end of expression '%s'\n", p.s, s0);
  461. ret = AVERROR(EINVAL);
  462. goto end;
  463. }
  464. if (!verify_expr(e)) {
  465. av_expr_free(e);
  466. ret = AVERROR(EINVAL);
  467. goto end;
  468. }
  469. *expr = e;
  470. end:
  471. av_free(w);
  472. return ret;
  473. }
  474. double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
  475. {
  476. Parser p;
  477. p.const_values = const_values;
  478. p.opaque = opaque;
  479. return eval_expr(&p, e);
  480. }
  481. int av_expr_parse_and_eval(double *d, const char *s,
  482. const char * const *const_names, const double *const_values,
  483. const char * const *func1_names, double (* const *funcs1)(void *, double),
  484. const char * const *func2_names, double (* const *funcs2)(void *, double, double),
  485. void *opaque, int log_offset, void *log_ctx)
  486. {
  487. AVExpr *e = NULL;
  488. int ret = av_expr_parse(&e, s, const_names, func1_names, funcs1, func2_names, funcs2, log_offset, log_ctx);
  489. if (ret < 0) {
  490. *d = NAN;
  491. return ret;
  492. }
  493. *d = av_expr_eval(e, const_values, opaque);
  494. av_expr_free(e);
  495. return isnan(*d) ? AVERROR(EINVAL) : 0;
  496. }
  497. #if FF_API_OLD_EVAL_NAMES
  498. int av_parse_expr(AVExpr **expr, const char *s,
  499. const char * const *const_names,
  500. const char * const *func1_names, double (* const *funcs1)(void *, double),
  501. const char * const *func2_names, double (* const *funcs2)(void *, double, double),
  502. int log_offset, void *log_ctx)
  503. {
  504. return av_expr_parse(expr, s, const_names, func1_names, funcs1, func2_names, funcs2,
  505. log_offset, log_ctx);
  506. }
  507. double av_eval_expr(AVExpr *e, const double *const_values, void *opaque)
  508. {
  509. return av_expr_eval(e, const_values, opaque);
  510. }
  511. int av_parse_and_eval_expr(double *res, const char *s,
  512. const char * const *const_names, const double *const_values,
  513. const char * const *func1_names, double (* const *funcs1)(void *, double),
  514. const char * const *func2_names, double (* const *funcs2)(void *, double, double),
  515. void *opaque, int log_offset, void *log_ctx)
  516. {
  517. return av_expr_parse_and_eval(res, s, const_names, const_values, func1_names, funcs1, func2_names, funcs2,
  518. opaque, log_offset, log_ctx);
  519. }
  520. void av_free_expr(AVExpr *e)
  521. {
  522. av_expr_free(e);
  523. }
  524. #endif /* FF_API_OLD_EVAL_NAMES */
  525. #ifdef TEST
  526. #undef printf
  527. static double const_values[] = {
  528. M_PI,
  529. M_E,
  530. 0
  531. };
  532. static const char *const_names[] = {
  533. "PI",
  534. "E",
  535. 0
  536. };
  537. int main(void)
  538. {
  539. int i;
  540. double d;
  541. const char **expr, *exprs[] = {
  542. "",
  543. "1;2",
  544. "-20",
  545. "-PI",
  546. "+PI",
  547. "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
  548. "80G/80Gi"
  549. "1k",
  550. "1Gi",
  551. "1gi",
  552. "1GiFoo",
  553. "1k+1k",
  554. "1Gi*3foo",
  555. "foo",
  556. "foo(",
  557. "foo()",
  558. "foo)",
  559. "sin",
  560. "sin(",
  561. "sin()",
  562. "sin)",
  563. "sin 10",
  564. "sin(1,2,3)",
  565. "sin(1 )",
  566. "1",
  567. "1foo",
  568. "bar + PI + E + 100f*2 + foo",
  569. "13k + 12f - foo(1, 2)",
  570. "1gi",
  571. "1Gi",
  572. "st(0, 123)",
  573. "st(1, 123); ld(1)",
  574. /* compute 1+2+...+N */
  575. "st(0, 1); while(lte(ld(0), 100), st(1, ld(1)+ld(0));st(0, ld(0)+1)); ld(1)",
  576. /* compute Fib(N) */
  577. "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)",
  578. "while(0, 10)",
  579. "st(0, 1); while(lte(ld(0),100), st(1, ld(1)+ld(0)); st(0, ld(0)+1))",
  580. "isnan(1)",
  581. "isnan(NAN)",
  582. "floor(NAN)",
  583. "floor(123.123)",
  584. "floor(-123.123)",
  585. "trunc(123.123)",
  586. "trunc(-123.123)",
  587. "ceil(123.123)",
  588. "ceil(-123.123)",
  589. "sqrt(1764)",
  590. "sqrt(-1)",
  591. NULL
  592. };
  593. for (expr = exprs; *expr; expr++) {
  594. printf("Evaluating '%s'\n", *expr);
  595. av_expr_parse_and_eval(&d, *expr,
  596. const_names, const_values,
  597. NULL, NULL, NULL, NULL, NULL, 0, NULL);
  598. printf("'%s' -> %f\n\n", *expr, d);
  599. }
  600. av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
  601. const_names, const_values,
  602. NULL, NULL, NULL, NULL, NULL, 0, NULL);
  603. printf("%f == 12.7\n", d);
  604. av_expr_parse_and_eval(&d, "80G/80Gi",
  605. const_names, const_values,
  606. NULL, NULL, NULL, NULL, NULL, 0, NULL);
  607. printf("%f == 0.931322575\n", d);
  608. for (i=0; i<1050; i++) {
  609. START_TIMER
  610. av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
  611. const_names, const_values,
  612. NULL, NULL, NULL, NULL, NULL, 0, NULL);
  613. STOP_TIMER("av_expr_parse_and_eval")
  614. }
  615. return 0;
  616. }
  617. #endif