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.

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