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.

846 lines
26KB

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