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.

705 lines
21KB

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