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.

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