eval.c 25 KB

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