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.

759 lines
23KB

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