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.

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