eval.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  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(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. }