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.

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