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.

709 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, e_hypot, e_gcd,
  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_gcd: return e->value * av_gcd(d,d2);
  167. case e_max: return e->value * (d > d2 ? d : d2);
  168. case e_min: return e->value * (d < d2 ? d : d2);
  169. case e_eq: return e->value * (d == d2 ? 1.0 : 0.0);
  170. case e_gt: return e->value * (d > d2 ? 1.0 : 0.0);
  171. case e_gte: return e->value * (d >= d2 ? 1.0 : 0.0);
  172. case e_pow: return e->value * pow(d, d2);
  173. case e_mul: return e->value * (d * d2);
  174. case e_div: return e->value * (d / d2);
  175. case e_add: return e->value * (d + d2);
  176. case e_last:return e->value * d2;
  177. case e_st : return e->value * (p->var[av_clip(d, 0, VARS-1)]= d2);
  178. case e_hypot:return e->value * (sqrt(d*d + d2*d2));
  179. }
  180. }
  181. }
  182. return NAN;
  183. }
  184. static int parse_expr(AVExpr **e, Parser *p);
  185. void av_expr_free(AVExpr *e)
  186. {
  187. if (!e) return;
  188. av_expr_free(e->param[0]);
  189. av_expr_free(e->param[1]);
  190. av_freep(&e->var);
  191. av_freep(&e);
  192. }
  193. static int parse_primary(AVExpr **e, Parser *p)
  194. {
  195. AVExpr *d = av_mallocz(sizeof(AVExpr));
  196. char *next = p->s, *s0 = p->s;
  197. int ret, i;
  198. if (!d)
  199. return AVERROR(ENOMEM);
  200. /* number */
  201. d->value = av_strtod(p->s, &next);
  202. if (next != p->s) {
  203. d->type = e_value;
  204. p->s= next;
  205. *e = d;
  206. return 0;
  207. }
  208. d->value = 1;
  209. /* named constants */
  210. for (i=0; p->const_names && p->const_names[i]; i++) {
  211. if (strmatch(p->s, p->const_names[i])) {
  212. p->s+= strlen(p->const_names[i]);
  213. d->type = e_const;
  214. d->a.const_index = i;
  215. *e = d;
  216. return 0;
  217. }
  218. }
  219. p->s= strchr(p->s, '(');
  220. if (p->s==NULL) {
  221. av_log(p, AV_LOG_ERROR, "Undefined constant or missing '(' in '%s'\n", s0);
  222. p->s= next;
  223. av_expr_free(d);
  224. return AVERROR(EINVAL);
  225. }
  226. p->s++; // "("
  227. if (*next == '(') { // special case do-nothing
  228. av_freep(&d);
  229. if ((ret = parse_expr(&d, p)) < 0)
  230. return ret;
  231. if (p->s[0] != ')') {
  232. av_log(p, AV_LOG_ERROR, "Missing ')' in '%s'\n", s0);
  233. av_expr_free(d);
  234. return AVERROR(EINVAL);
  235. }
  236. p->s++; // ")"
  237. *e = d;
  238. return 0;
  239. }
  240. if ((ret = parse_expr(&(d->param[0]), p)) < 0) {
  241. av_expr_free(d);
  242. return ret;
  243. }
  244. if (p->s[0]== ',') {
  245. p->s++; // ","
  246. parse_expr(&d->param[1], p);
  247. }
  248. if (p->s[0] != ')') {
  249. av_log(p, AV_LOG_ERROR, "Missing ')' or too many args in '%s'\n", s0);
  250. av_expr_free(d);
  251. return AVERROR(EINVAL);
  252. }
  253. p->s++; // ")"
  254. d->type = e_func0;
  255. if (strmatch(next, "sinh" )) d->a.func0 = sinh;
  256. else if (strmatch(next, "cosh" )) d->a.func0 = cosh;
  257. else if (strmatch(next, "tanh" )) d->a.func0 = tanh;
  258. else if (strmatch(next, "sin" )) d->a.func0 = sin;
  259. else if (strmatch(next, "cos" )) d->a.func0 = cos;
  260. else if (strmatch(next, "tan" )) d->a.func0 = tan;
  261. else if (strmatch(next, "atan" )) d->a.func0 = atan;
  262. else if (strmatch(next, "asin" )) d->a.func0 = asin;
  263. else if (strmatch(next, "acos" )) d->a.func0 = acos;
  264. else if (strmatch(next, "exp" )) d->a.func0 = exp;
  265. else if (strmatch(next, "log" )) d->a.func0 = log;
  266. else if (strmatch(next, "abs" )) d->a.func0 = fabs;
  267. else if (strmatch(next, "squish")) d->type = e_squish;
  268. else if (strmatch(next, "gauss" )) d->type = e_gauss;
  269. else if (strmatch(next, "mod" )) d->type = e_mod;
  270. else if (strmatch(next, "max" )) d->type = e_max;
  271. else if (strmatch(next, "min" )) d->type = e_min;
  272. else if (strmatch(next, "eq" )) d->type = e_eq;
  273. else if (strmatch(next, "gte" )) d->type = e_gte;
  274. else if (strmatch(next, "gt" )) d->type = e_gt;
  275. else if (strmatch(next, "lte" )) { AVExpr *tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gt; }
  276. else if (strmatch(next, "lt" )) { AVExpr *tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gte; }
  277. else if (strmatch(next, "ld" )) d->type = e_ld;
  278. else if (strmatch(next, "isnan" )) d->type = e_isnan;
  279. else if (strmatch(next, "st" )) d->type = e_st;
  280. else if (strmatch(next, "while" )) d->type = e_while;
  281. else if (strmatch(next, "floor" )) d->type = e_floor;
  282. else if (strmatch(next, "ceil" )) d->type = e_ceil;
  283. else if (strmatch(next, "trunc" )) d->type = e_trunc;
  284. else if (strmatch(next, "sqrt" )) d->type = e_sqrt;
  285. else if (strmatch(next, "not" )) d->type = e_not;
  286. else if (strmatch(next, "pow" )) d->type = e_pow;
  287. else if (strmatch(next, "random")) d->type = e_random;
  288. else if (strmatch(next, "hypot" )) d->type = e_hypot;
  289. else if (strmatch(next, "gcd" )) d->type = e_gcd;
  290. else {
  291. for (i=0; p->func1_names && p->func1_names[i]; i++) {
  292. if (strmatch(next, p->func1_names[i])) {
  293. d->a.func1 = p->funcs1[i];
  294. d->type = e_func1;
  295. *e = d;
  296. return 0;
  297. }
  298. }
  299. for (i=0; p->func2_names && p->func2_names[i]; i++) {
  300. if (strmatch(next, p->func2_names[i])) {
  301. d->a.func2 = p->funcs2[i];
  302. d->type = e_func2;
  303. *e = d;
  304. return 0;
  305. }
  306. }
  307. av_log(p, AV_LOG_ERROR, "Unknown function in '%s'\n", s0);
  308. av_expr_free(d);
  309. return AVERROR(EINVAL);
  310. }
  311. *e = d;
  312. return 0;
  313. }
  314. static AVExpr *new_eval_expr(int type, int value, AVExpr *p0, AVExpr *p1)
  315. {
  316. AVExpr *e = av_mallocz(sizeof(AVExpr));
  317. if (!e)
  318. return NULL;
  319. e->type =type ;
  320. e->value =value ;
  321. e->param[0] =p0 ;
  322. e->param[1] =p1 ;
  323. return e;
  324. }
  325. static int parse_pow(AVExpr **e, Parser *p, int *sign)
  326. {
  327. *sign= (*p->s == '+') - (*p->s == '-');
  328. p->s += *sign&1;
  329. return parse_primary(e, p);
  330. }
  331. static int parse_factor(AVExpr **e, Parser *p)
  332. {
  333. int sign, sign2, ret;
  334. AVExpr *e0, *e1, *e2;
  335. if ((ret = parse_pow(&e0, p, &sign)) < 0)
  336. return ret;
  337. while(p->s[0]=='^'){
  338. e1 = e0;
  339. p->s++;
  340. if ((ret = parse_pow(&e2, p, &sign2)) < 0) {
  341. av_expr_free(e1);
  342. return ret;
  343. }
  344. e0 = new_eval_expr(e_pow, 1, e1, e2);
  345. if (!e0) {
  346. av_expr_free(e1);
  347. av_expr_free(e2);
  348. return AVERROR(ENOMEM);
  349. }
  350. if (e0->param[1]) e0->param[1]->value *= (sign2|1);
  351. }
  352. if (e0) e0->value *= (sign|1);
  353. *e = e0;
  354. return 0;
  355. }
  356. static int parse_term(AVExpr **e, Parser *p)
  357. {
  358. int ret;
  359. AVExpr *e0, *e1, *e2;
  360. if ((ret = parse_factor(&e0, p)) < 0)
  361. return ret;
  362. while (p->s[0]=='*' || p->s[0]=='/') {
  363. int c= *p->s++;
  364. e1 = e0;
  365. if ((ret = parse_factor(&e2, p)) < 0) {
  366. av_expr_free(e1);
  367. return ret;
  368. }
  369. e0 = new_eval_expr(c == '*' ? e_mul : e_div, 1, e1, e2);
  370. if (!e0) {
  371. av_expr_free(e1);
  372. av_expr_free(e2);
  373. return AVERROR(ENOMEM);
  374. }
  375. }
  376. *e = e0;
  377. return 0;
  378. }
  379. static int parse_subexpr(AVExpr **e, Parser *p)
  380. {
  381. int ret;
  382. AVExpr *e0, *e1, *e2;
  383. if ((ret = parse_term(&e0, p)) < 0)
  384. return ret;
  385. while (*p->s == '+' || *p->s == '-') {
  386. e1 = e0;
  387. if ((ret = parse_term(&e2, p)) < 0) {
  388. av_expr_free(e1);
  389. return ret;
  390. }
  391. e0 = new_eval_expr(e_add, 1, e1, e2);
  392. if (!e0) {
  393. av_expr_free(e1);
  394. av_expr_free(e2);
  395. return AVERROR(ENOMEM);
  396. }
  397. };
  398. *e = e0;
  399. return 0;
  400. }
  401. static int parse_expr(AVExpr **e, Parser *p)
  402. {
  403. int ret;
  404. AVExpr *e0, *e1, *e2;
  405. if (p->stack_index <= 0) //protect against stack overflows
  406. return AVERROR(EINVAL);
  407. p->stack_index--;
  408. if ((ret = parse_subexpr(&e0, p)) < 0)
  409. return ret;
  410. while (*p->s == ';') {
  411. p->s++;
  412. e1 = e0;
  413. if ((ret = parse_subexpr(&e2, p)) < 0) {
  414. av_expr_free(e1);
  415. return ret;
  416. }
  417. e0 = new_eval_expr(e_last, 1, e1, e2);
  418. if (!e0) {
  419. av_expr_free(e1);
  420. av_expr_free(e2);
  421. return AVERROR(ENOMEM);
  422. }
  423. };
  424. p->stack_index++;
  425. *e = e0;
  426. return 0;
  427. }
  428. static int verify_expr(AVExpr *e)
  429. {
  430. if (!e) return 0;
  431. switch (e->type) {
  432. case e_value:
  433. case e_const: return 1;
  434. case e_func0:
  435. case e_func1:
  436. case e_squish:
  437. case e_ld:
  438. case e_gauss:
  439. case e_isnan:
  440. case e_floor:
  441. case e_ceil:
  442. case e_trunc:
  443. case e_sqrt:
  444. case e_not:
  445. case e_random:
  446. return verify_expr(e->param[0]);
  447. default: return verify_expr(e->param[0]) && verify_expr(e->param[1]);
  448. }
  449. }
  450. int av_expr_parse(AVExpr **expr, const char *s,
  451. const char * const *const_names,
  452. const char * const *func1_names, double (* const *funcs1)(void *, double),
  453. const char * const *func2_names, double (* const *funcs2)(void *, double, double),
  454. int log_offset, void *log_ctx)
  455. {
  456. Parser p = { 0 };
  457. AVExpr *e = NULL;
  458. char *w = av_malloc(strlen(s) + 1);
  459. char *wp = w;
  460. const char *s0 = s;
  461. int ret = 0;
  462. if (!w)
  463. return AVERROR(ENOMEM);
  464. while (*s)
  465. if (!isspace(*s++)) *wp++ = s[-1];
  466. *wp++ = 0;
  467. p.class = &class;
  468. p.stack_index=100;
  469. p.s= w;
  470. p.const_names = const_names;
  471. p.funcs1 = funcs1;
  472. p.func1_names = func1_names;
  473. p.funcs2 = funcs2;
  474. p.func2_names = func2_names;
  475. p.log_offset = log_offset;
  476. p.log_ctx = log_ctx;
  477. if ((ret = parse_expr(&e, &p)) < 0)
  478. goto end;
  479. if (*p.s) {
  480. av_expr_free(e);
  481. av_log(&p, AV_LOG_ERROR, "Invalid chars '%s' at the end of expression '%s'\n", p.s, s0);
  482. ret = AVERROR(EINVAL);
  483. goto end;
  484. }
  485. if (!verify_expr(e)) {
  486. av_expr_free(e);
  487. ret = AVERROR(EINVAL);
  488. goto end;
  489. }
  490. e->var= av_mallocz(sizeof(double) *VARS);
  491. *expr = e;
  492. end:
  493. av_free(w);
  494. return ret;
  495. }
  496. double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
  497. {
  498. Parser p = { 0 };
  499. p.var= e->var;
  500. p.const_values = const_values;
  501. p.opaque = opaque;
  502. return eval_expr(&p, e);
  503. }
  504. int av_expr_parse_and_eval(double *d, const char *s,
  505. const char * const *const_names, const double *const_values,
  506. const char * const *func1_names, double (* const *funcs1)(void *, double),
  507. const char * const *func2_names, double (* const *funcs2)(void *, double, double),
  508. void *opaque, int log_offset, void *log_ctx)
  509. {
  510. AVExpr *e = NULL;
  511. int ret = av_expr_parse(&e, s, const_names, func1_names, funcs1, func2_names, funcs2, log_offset, log_ctx);
  512. if (ret < 0) {
  513. *d = NAN;
  514. return ret;
  515. }
  516. *d = av_expr_eval(e, const_values, opaque);
  517. av_expr_free(e);
  518. return isnan(*d) ? AVERROR(EINVAL) : 0;
  519. }
  520. #if FF_API_OLD_EVAL_NAMES
  521. int av_parse_expr(AVExpr **expr, const char *s,
  522. const char * const *const_names,
  523. const char * const *func1_names, double (* const *funcs1)(void *, double),
  524. const char * const *func2_names, double (* const *funcs2)(void *, double, double),
  525. int log_offset, void *log_ctx)
  526. {
  527. return av_expr_parse(expr, s, const_names, func1_names, funcs1, func2_names, funcs2,
  528. log_offset, log_ctx);
  529. }
  530. double av_eval_expr(AVExpr *e, const double *const_values, void *opaque)
  531. {
  532. return av_expr_eval(e, const_values, opaque);
  533. }
  534. int av_parse_and_eval_expr(double *res, const char *s,
  535. const char * const *const_names, const double *const_values,
  536. const char * const *func1_names, double (* const *funcs1)(void *, double),
  537. const char * const *func2_names, double (* const *funcs2)(void *, double, double),
  538. void *opaque, int log_offset, void *log_ctx)
  539. {
  540. return av_expr_parse_and_eval(res, s, const_names, const_values, func1_names, funcs1, func2_names, funcs2,
  541. opaque, log_offset, log_ctx);
  542. }
  543. void av_free_expr(AVExpr *e)
  544. {
  545. av_expr_free(e);
  546. }
  547. #endif /* FF_API_OLD_EVAL_NAMES */
  548. #ifdef TEST
  549. #undef printf
  550. #include <string.h>
  551. static double const_values[] = {
  552. M_PI,
  553. M_E,
  554. 0
  555. };
  556. static const char *const_names[] = {
  557. "PI",
  558. "E",
  559. 0
  560. };
  561. int main(int argc, char **argv)
  562. {
  563. int i;
  564. double d;
  565. const char **expr, *exprs[] = {
  566. "",
  567. "1;2",
  568. "-20",
  569. "-PI",
  570. "+PI",
  571. "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
  572. "80G/80Gi",
  573. "1k",
  574. "1Gi",
  575. "1gi",
  576. "1GiFoo",
  577. "1k+1k",
  578. "1Gi*3foo",
  579. "foo",
  580. "foo(",
  581. "foo()",
  582. "foo)",
  583. "sin",
  584. "sin(",
  585. "sin()",
  586. "sin)",
  587. "sin 10",
  588. "sin(1,2,3)",
  589. "sin(1 )",
  590. "1",
  591. "1foo",
  592. "bar + PI + E + 100f*2 + foo",
  593. "13k + 12f - foo(1, 2)",
  594. "1gi",
  595. "1Gi",
  596. "st(0, 123)",
  597. "st(1, 123); ld(1)",
  598. /* compute 1+2+...+N */
  599. "st(0, 1); while(lte(ld(0), 100), st(1, ld(1)+ld(0));st(0, ld(0)+1)); ld(1)",
  600. /* compute Fib(N) */
  601. "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)",
  602. "while(0, 10)",
  603. "st(0, 1); while(lte(ld(0),100), st(1, ld(1)+ld(0)); st(0, ld(0)+1))",
  604. "isnan(1)",
  605. "isnan(NAN)",
  606. "floor(NAN)",
  607. "floor(123.123)",
  608. "floor(-123.123)",
  609. "trunc(123.123)",
  610. "trunc(-123.123)",
  611. "ceil(123.123)",
  612. "ceil(-123.123)",
  613. "sqrt(1764)",
  614. "isnan(sqrt(-1))",
  615. "not(1)",
  616. "not(NAN)",
  617. "not(0)",
  618. "pow(0,1.23)",
  619. "pow(PI,1.23)",
  620. "PI^1.23",
  621. "pow(-1,1.23)",
  622. NULL
  623. };
  624. for (expr = exprs; *expr; expr++) {
  625. printf("Evaluating '%s'\n", *expr);
  626. av_expr_parse_and_eval(&d, *expr,
  627. const_names, const_values,
  628. NULL, NULL, NULL, NULL, NULL, 0, NULL);
  629. if(isnan(d)){
  630. printf("'%s' -> nan\n\n", *expr);
  631. }else{
  632. printf("'%s' -> %f\n\n", *expr, d);
  633. }
  634. }
  635. av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
  636. const_names, const_values,
  637. NULL, NULL, NULL, NULL, NULL, 0, NULL);
  638. printf("%f == 12.7\n", d);
  639. av_expr_parse_and_eval(&d, "80G/80Gi",
  640. const_names, const_values,
  641. NULL, NULL, NULL, NULL, NULL, 0, NULL);
  642. printf("%f == 0.931322575\n", d);
  643. if (argc > 1 && !strcmp(argv[1], "-t")) {
  644. for (i = 0; i < 1050; i++) {
  645. START_TIMER;
  646. av_expr_parse_and_eval(&d, "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
  647. const_names, const_values,
  648. NULL, NULL, NULL, NULL, NULL, 0, NULL);
  649. STOP_TIMER("av_expr_parse_and_eval");
  650. }
  651. }
  652. return 0;
  653. }
  654. #endif