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.

533 lines
15KB

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