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.

893 lines
28KB

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