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.

892 lines
28KB

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