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.

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