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.

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