eval.c 19 KB

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