eval.c 27 KB

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