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.

888 lines
27KB

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