eval.c 20 KB

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