eval.c 22 KB

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