You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

815 lines
25KB

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