eval.c 25 KB

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