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.

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