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.

829 lines
25KB

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