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.

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