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.

868 lines
27KB

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