eval.c 26 KB

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