eval.c 27 KB

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