eval.c 22 KB

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