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.

575 lines
17KB

  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 Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 "attributes.h"
  28. #include "avutil.h"
  29. #include "common.h"
  30. #include "eval.h"
  31. #include "log.h"
  32. #include "mathematics.h"
  33. #include "avstring.h"
  34. #include "timer.h"
  35. typedef struct Parser {
  36. const AVClass *class;
  37. int stack_index;
  38. char *s;
  39. const double *const_values;
  40. const char * const *const_names; // NULL terminated
  41. double (* const *funcs1)(void *, double a); // NULL terminated
  42. const char * const *func1_names; // NULL terminated
  43. double (* const *funcs2)(void *, double a, double b); // NULL terminated
  44. const char * const *func2_names; // NULL terminated
  45. void *opaque;
  46. int log_offset;
  47. void *log_ctx;
  48. #define VARS 10
  49. double var[VARS];
  50. } Parser;
  51. static const AVClass class = {
  52. .class_name = "Eval",
  53. .item_name = av_default_item_name,
  54. .option = NULL,
  55. .version = LIBAVUTIL_VERSION_INT,
  56. .log_level_offset_offset = offsetof(Parser, log_offset),
  57. .parent_log_context_offset = offsetof(Parser, log_ctx),
  58. };
  59. static const int8_t si_prefixes['z' - 'E' + 1] = {
  60. ['y'-'E']= -24,
  61. ['z'-'E']= -21,
  62. ['a'-'E']= -18,
  63. ['f'-'E']= -15,
  64. ['p'-'E']= -12,
  65. ['n'-'E']= - 9,
  66. ['u'-'E']= - 6,
  67. ['m'-'E']= - 3,
  68. ['c'-'E']= - 2,
  69. ['d'-'E']= - 1,
  70. ['h'-'E']= 2,
  71. ['k'-'E']= 3,
  72. ['K'-'E']= 3,
  73. ['M'-'E']= 6,
  74. ['G'-'E']= 9,
  75. ['T'-'E']= 12,
  76. ['P'-'E']= 15,
  77. ['E'-'E']= 18,
  78. ['Z'-'E']= 21,
  79. ['Y'-'E']= 24,
  80. };
  81. double av_strtod(const char *numstr, char **tail)
  82. {
  83. double d;
  84. char *next;
  85. d = strtod(numstr, &next);
  86. /* if parsing succeeded, check for and interpret postfixes */
  87. if (next!=numstr) {
  88. if (next[0] == 'd' && next[1] == 'B') {
  89. /* treat dB as decibels instead of decibytes */
  90. d = pow(10, d / 20);
  91. next += 2;
  92. } else if (*next >= 'E' && *next <= 'z') {
  93. int e= si_prefixes[*next - 'E'];
  94. if (e) {
  95. if (next[1] == 'i') {
  96. d*= pow( 2, e/0.3);
  97. next+=2;
  98. } else {
  99. d*= pow(10, e);
  100. next++;
  101. }
  102. }
  103. }
  104. if (*next=='B') {
  105. d*=8;
  106. next++;
  107. }
  108. }
  109. /* if requested, fill in tail with the position after the last parsed
  110. character */
  111. if (tail)
  112. *tail = next;
  113. return d;
  114. }
  115. #define IS_IDENTIFIER_CHAR(c) ((c) - '0' <= 9U || (c) - 'a' <= 25U || (c) - 'A' <= 25U || (c) == '_')
  116. static int strmatch(const char *s, const char *prefix)
  117. {
  118. int i;
  119. for (i=0; prefix[i]; i++) {
  120. if (prefix[i] != s[i]) return 0;
  121. }
  122. /* return 1 only if the s identifier is terminated */
  123. return !IS_IDENTIFIER_CHAR(s[i]);
  124. }
  125. struct AVExpr {
  126. enum {
  127. e_value, e_const, e_func0, e_func1, e_func2,
  128. e_squish, e_gauss, e_ld, e_isnan, e_isinf,
  129. e_mod, e_max, e_min, e_eq, e_gt, e_gte,
  130. e_pow, e_mul, e_div, e_add,
  131. e_last, e_st, e_while, e_floor, e_ceil, e_trunc,
  132. e_sqrt, e_not,
  133. } type;
  134. double value; // is sign in other types
  135. union {
  136. int const_index;
  137. double (*func0)(double);
  138. double (*func1)(void *, double);
  139. double (*func2)(void *, double, double);
  140. } a;
  141. struct AVExpr *param[2];
  142. };
  143. static double eval_expr(Parser *p, AVExpr *e)
  144. {
  145. switch (e->type) {
  146. case e_value: return e->value;
  147. case e_const: return e->value * p->const_values[e->a.const_index];
  148. case e_func0: return e->value * e->a.func0(eval_expr(p, e->param[0]));
  149. case e_func1: return e->value * e->a.func1(p->opaque, eval_expr(p, e->param[0]));
  150. case e_func2: return e->value * e->a.func2(p->opaque, eval_expr(p, e->param[0]), eval_expr(p, e->param[1]));
  151. case e_squish: return 1/(1+exp(4*eval_expr(p, e->param[0])));
  152. case e_gauss: { double d = eval_expr(p, e->param[0]); return exp(-d*d/2)/sqrt(2*M_PI); }
  153. case e_ld: return e->value * p->var[av_clip(eval_expr(p, e->param[0]), 0, VARS-1)];
  154. case e_isnan: return e->value * !!isnan(eval_expr(p, e->param[0]));
  155. case e_isinf: return e->value * !!isinf(eval_expr(p, e->param[0]));
  156. case e_floor: return e->value * floor(eval_expr(p, e->param[0]));
  157. case e_ceil : return e->value * ceil (eval_expr(p, e->param[0]));
  158. case e_trunc: return e->value * trunc(eval_expr(p, e->param[0]));
  159. case e_sqrt: return e->value * sqrt (eval_expr(p, e->param[0]));
  160. case e_not: return e->value * eval_expr(p, e->param[0]) == 0;
  161. case e_while: {
  162. double d = NAN;
  163. while (eval_expr(p, e->param[0]))
  164. d=eval_expr(p, e->param[1]);
  165. return d;
  166. }
  167. default: {
  168. double d = eval_expr(p, e->param[0]);
  169. double d2 = eval_expr(p, e->param[1]);
  170. switch (e->type) {
  171. case e_mod: return e->value * (d - floor(d/d2)*d2);
  172. case e_max: return e->value * (d > d2 ? d : d2);
  173. case e_min: return e->value * (d < d2 ? d : d2);
  174. case e_eq: return e->value * (d == d2 ? 1.0 : 0.0);
  175. case e_gt: return e->value * (d > d2 ? 1.0 : 0.0);
  176. case e_gte: return e->value * (d >= d2 ? 1.0 : 0.0);
  177. case e_pow: return e->value * pow(d, d2);
  178. case e_mul: return e->value * (d * d2);
  179. case e_div: return e->value * (d / d2);
  180. case e_add: return e->value * (d + d2);
  181. case e_last:return e->value * d2;
  182. case e_st : return e->value * (p->var[av_clip(d, 0, VARS-1)]= d2);
  183. }
  184. }
  185. }
  186. return NAN;
  187. }
  188. static int parse_expr(AVExpr **e, Parser *p);
  189. void av_expr_free(AVExpr *e)
  190. {
  191. if (!e) return;
  192. av_expr_free(e->param[0]);
  193. av_expr_free(e->param[1]);
  194. av_freep(&e);
  195. }
  196. static int parse_primary(AVExpr **e, Parser *p)
  197. {
  198. AVExpr *d = av_mallocz(sizeof(AVExpr));
  199. char *next = p->s, *s0 = p->s;
  200. int ret, i;
  201. if (!d)
  202. return AVERROR(ENOMEM);
  203. /* number */
  204. d->value = av_strtod(p->s, &next);
  205. if (next != p->s) {
  206. d->type = e_value;
  207. p->s= next;
  208. *e = d;
  209. return 0;
  210. }
  211. d->value = 1;
  212. /* named constants */
  213. for (i=0; p->const_names && p->const_names[i]; i++) {
  214. if (strmatch(p->s, p->const_names[i])) {
  215. p->s+= strlen(p->const_names[i]);
  216. d->type = e_const;
  217. d->a.const_index = i;
  218. *e = d;
  219. return 0;
  220. }
  221. }
  222. p->s= strchr(p->s, '(');
  223. if (!p->s) {
  224. av_log(p, AV_LOG_ERROR, "Undefined constant or missing '(' in '%s'\n", s0);
  225. p->s= next;
  226. av_expr_free(d);
  227. return AVERROR(EINVAL);
  228. }
  229. p->s++; // "("
  230. if (*next == '(') { // special case do-nothing
  231. av_freep(&d);
  232. if ((ret = parse_expr(&d, p)) < 0)
  233. return ret;
  234. if (p->s[0] != ')') {
  235. av_log(p, AV_LOG_ERROR, "Missing ')' in '%s'\n", s0);
  236. av_expr_free(d);
  237. return AVERROR(EINVAL);
  238. }
  239. p->s++; // ")"
  240. *e = d;
  241. return 0;
  242. }
  243. if ((ret = parse_expr(&(d->param[0]), p)) < 0) {
  244. av_expr_free(d);
  245. return ret;
  246. }
  247. if (p->s[0]== ',') {
  248. p->s++; // ","
  249. parse_expr(&d->param[1], p);
  250. }
  251. if (p->s[0] != ')') {
  252. av_log(p, AV_LOG_ERROR, "Missing ')' or too many args in '%s'\n", s0);
  253. av_expr_free(d);
  254. return AVERROR(EINVAL);
  255. }
  256. p->s++; // ")"
  257. d->type = e_func0;
  258. if (strmatch(next, "sinh" )) d->a.func0 = sinh;
  259. else if (strmatch(next, "cosh" )) d->a.func0 = cosh;
  260. else if (strmatch(next, "tanh" )) d->a.func0 = tanh;
  261. else if (strmatch(next, "sin" )) d->a.func0 = sin;
  262. else if (strmatch(next, "cos" )) d->a.func0 = cos;
  263. else if (strmatch(next, "tan" )) d->a.func0 = tan;
  264. else if (strmatch(next, "atan" )) d->a.func0 = atan;
  265. else if (strmatch(next, "asin" )) d->a.func0 = asin;
  266. else if (strmatch(next, "acos" )) d->a.func0 = acos;
  267. else if (strmatch(next, "exp" )) d->a.func0 = exp;
  268. else if (strmatch(next, "log" )) d->a.func0 = log;
  269. else if (strmatch(next, "abs" )) d->a.func0 = fabs;
  270. else if (strmatch(next, "squish")) d->type = e_squish;
  271. else if (strmatch(next, "gauss" )) d->type = e_gauss;
  272. else if (strmatch(next, "mod" )) d->type = e_mod;
  273. else if (strmatch(next, "max" )) d->type = e_max;
  274. else if (strmatch(next, "min" )) d->type = e_min;
  275. else if (strmatch(next, "eq" )) d->type = e_eq;
  276. else if (strmatch(next, "gte" )) d->type = e_gte;
  277. else if (strmatch(next, "gt" )) d->type = e_gt;
  278. else if (strmatch(next, "lte" )) { AVExpr *tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gte; }
  279. else if (strmatch(next, "lt" )) { AVExpr *tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gt; }
  280. else if (strmatch(next, "ld" )) d->type = e_ld;
  281. else if (strmatch(next, "isnan" )) d->type = e_isnan;
  282. else if (strmatch(next, "isinf" )) d->type = e_isinf;
  283. else if (strmatch(next, "st" )) d->type = e_st;
  284. else if (strmatch(next, "while" )) d->type = e_while;
  285. else if (strmatch(next, "floor" )) d->type = e_floor;
  286. else if (strmatch(next, "ceil" )) d->type = e_ceil;
  287. else if (strmatch(next, "trunc" )) d->type = e_trunc;
  288. else if (strmatch(next, "sqrt" )) d->type = e_sqrt;
  289. else if (strmatch(next, "not" )) d->type = e_not;
  290. else {
  291. for (i=0; p->func1_names && p->func1_names[i]; i++) {
  292. if (strmatch(next, p->func1_names[i])) {
  293. d->a.func1 = p->funcs1[i];
  294. d->type = e_func1;
  295. *e = d;
  296. return 0;
  297. }
  298. }
  299. for (i=0; p->func2_names && p->func2_names[i]; i++) {
  300. if (strmatch(next, p->func2_names[i])) {
  301. d->a.func2 = p->funcs2[i];
  302. d->type = e_func2;
  303. *e = d;
  304. return 0;
  305. }
  306. }
  307. av_log(p, AV_LOG_ERROR, "Unknown function in '%s'\n", s0);
  308. av_expr_free(d);
  309. return AVERROR(EINVAL);
  310. }
  311. *e = d;
  312. return 0;
  313. }
  314. static AVExpr *new_eval_expr(int type, int value, AVExpr *p0, AVExpr *p1)
  315. {
  316. AVExpr *e = av_mallocz(sizeof(AVExpr));
  317. if (!e)
  318. return NULL;
  319. e->type =type ;
  320. e->value =value ;
  321. e->param[0] =p0 ;
  322. e->param[1] =p1 ;
  323. return e;
  324. }
  325. static int parse_pow(AVExpr **e, Parser *p, int *sign)
  326. {
  327. *sign= (*p->s == '+') - (*p->s == '-');
  328. p->s += *sign&1;
  329. return parse_primary(e, p);
  330. }
  331. static int parse_dB(AVExpr **e, Parser *p, int *sign)
  332. {
  333. /* do not filter out the negative sign when parsing a dB value.
  334. for example, -3dB is not the same as -(3dB) */
  335. if (*p->s == '-') {
  336. char *next;
  337. double av_unused ignored = strtod(p->s, &next);
  338. if (next != p->s && next[0] == 'd' && next[1] == 'B') {
  339. *sign = 0;
  340. return parse_primary(e, p);
  341. }
  342. }
  343. return parse_pow(e, p, sign);
  344. }
  345. static int parse_factor(AVExpr **e, Parser *p)
  346. {
  347. int sign, sign2, ret;
  348. AVExpr *e0, *e1, *e2;
  349. if ((ret = parse_dB(&e0, p, &sign)) < 0)
  350. return ret;
  351. while(p->s[0]=='^'){
  352. e1 = e0;
  353. p->s++;
  354. if ((ret = parse_dB(&e2, p, &sign2)) < 0) {
  355. av_expr_free(e1);
  356. return ret;
  357. }
  358. e0 = new_eval_expr(e_pow, 1, e1, e2);
  359. if (!e0) {
  360. av_expr_free(e1);
  361. av_expr_free(e2);
  362. return AVERROR(ENOMEM);
  363. }
  364. if (e0->param[1]) e0->param[1]->value *= (sign2|1);
  365. }
  366. if (e0) e0->value *= (sign|1);
  367. *e = e0;
  368. return 0;
  369. }
  370. static int parse_term(AVExpr **e, Parser *p)
  371. {
  372. int ret;
  373. AVExpr *e0, *e1, *e2;
  374. if ((ret = parse_factor(&e0, p)) < 0)
  375. return ret;
  376. while (p->s[0]=='*' || p->s[0]=='/') {
  377. int c= *p->s++;
  378. e1 = e0;
  379. if ((ret = parse_factor(&e2, p)) < 0) {
  380. av_expr_free(e1);
  381. return ret;
  382. }
  383. e0 = new_eval_expr(c == '*' ? e_mul : e_div, 1, e1, e2);
  384. if (!e0) {
  385. av_expr_free(e1);
  386. av_expr_free(e2);
  387. return AVERROR(ENOMEM);
  388. }
  389. }
  390. *e = e0;
  391. return 0;
  392. }
  393. static int parse_subexpr(AVExpr **e, Parser *p)
  394. {
  395. int ret;
  396. AVExpr *e0, *e1, *e2;
  397. if ((ret = parse_term(&e0, p)) < 0)
  398. return ret;
  399. while (*p->s == '+' || *p->s == '-') {
  400. e1 = e0;
  401. if ((ret = parse_term(&e2, p)) < 0) {
  402. av_expr_free(e1);
  403. return ret;
  404. }
  405. e0 = new_eval_expr(e_add, 1, e1, e2);
  406. if (!e0) {
  407. av_expr_free(e1);
  408. av_expr_free(e2);
  409. return AVERROR(ENOMEM);
  410. }
  411. };
  412. *e = e0;
  413. return 0;
  414. }
  415. static int parse_expr(AVExpr **e, Parser *p)
  416. {
  417. int ret;
  418. AVExpr *e0, *e1, *e2;
  419. if (p->stack_index <= 0) //protect against stack overflows
  420. return AVERROR(EINVAL);
  421. p->stack_index--;
  422. if ((ret = parse_subexpr(&e0, p)) < 0)
  423. return ret;
  424. while (*p->s == ';') {
  425. p->s++;
  426. e1 = e0;
  427. if ((ret = parse_subexpr(&e2, p)) < 0) {
  428. av_expr_free(e1);
  429. return ret;
  430. }
  431. e0 = new_eval_expr(e_last, 1, e1, e2);
  432. if (!e0) {
  433. av_expr_free(e1);
  434. av_expr_free(e2);
  435. return AVERROR(ENOMEM);
  436. }
  437. };
  438. p->stack_index++;
  439. *e = e0;
  440. return 0;
  441. }
  442. static int verify_expr(AVExpr *e)
  443. {
  444. if (!e) return 0;
  445. switch (e->type) {
  446. case e_value:
  447. case e_const: return 1;
  448. case e_func0:
  449. case e_func1:
  450. case e_squish:
  451. case e_ld:
  452. case e_gauss:
  453. case e_isnan:
  454. case e_isinf:
  455. case e_floor:
  456. case e_ceil:
  457. case e_trunc:
  458. case e_sqrt:
  459. case e_not:
  460. return verify_expr(e->param[0]);
  461. default: return verify_expr(e->param[0]) && verify_expr(e->param[1]);
  462. }
  463. }
  464. int av_expr_parse(AVExpr **expr, const char *s,
  465. const char * const *const_names,
  466. const char * const *func1_names, double (* const *funcs1)(void *, double),
  467. const char * const *func2_names, double (* const *funcs2)(void *, double, double),
  468. int log_offset, void *log_ctx)
  469. {
  470. Parser p = { 0 };
  471. AVExpr *e = NULL;
  472. char *w = av_malloc(strlen(s) + 1);
  473. char *wp = w;
  474. const char *s0 = s;
  475. int ret = 0;
  476. if (!w)
  477. return AVERROR(ENOMEM);
  478. while (*s)
  479. if (!av_isspace(*s++)) *wp++ = s[-1];
  480. *wp++ = 0;
  481. p.class = &class;
  482. p.stack_index=100;
  483. p.s= w;
  484. p.const_names = const_names;
  485. p.funcs1 = funcs1;
  486. p.func1_names = func1_names;
  487. p.funcs2 = funcs2;
  488. p.func2_names = func2_names;
  489. p.log_offset = log_offset;
  490. p.log_ctx = log_ctx;
  491. if ((ret = parse_expr(&e, &p)) < 0)
  492. goto end;
  493. if (*p.s) {
  494. av_expr_free(e);
  495. av_log(&p, AV_LOG_ERROR, "Invalid chars '%s' at the end of expression '%s'\n", p.s, s0);
  496. ret = AVERROR(EINVAL);
  497. goto end;
  498. }
  499. if (!verify_expr(e)) {
  500. av_expr_free(e);
  501. ret = AVERROR(EINVAL);
  502. goto end;
  503. }
  504. *expr = e;
  505. end:
  506. av_free(w);
  507. return ret;
  508. }
  509. double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
  510. {
  511. Parser p = { 0 };
  512. p.const_values = const_values;
  513. p.opaque = opaque;
  514. return eval_expr(&p, e);
  515. }
  516. int av_expr_parse_and_eval(double *d, const char *s,
  517. const char * const *const_names, const double *const_values,
  518. const char * const *func1_names, double (* const *funcs1)(void *, double),
  519. const char * const *func2_names, double (* const *funcs2)(void *, double, double),
  520. void *opaque, int log_offset, void *log_ctx)
  521. {
  522. AVExpr *e = NULL;
  523. int ret = av_expr_parse(&e, s, const_names, func1_names, funcs1, func2_names, funcs2, log_offset, log_ctx);
  524. if (ret < 0) {
  525. *d = NAN;
  526. return ret;
  527. }
  528. *d = av_expr_eval(e, const_values, opaque);
  529. av_expr_free(e);
  530. return isnan(*d) ? AVERROR(EINVAL) : 0;
  531. }