eval.c 25 KB

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