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.

460 lines
13KB

  1. /*
  2. * simple arithmetic expression evaluator
  3. *
  4. * Copyright (c) 2002-2006 Michael Niedermayer <michaelni@gmx.at>
  5. * Copyright (c) 2006 Oded Shimon <ods15@ods15.dyndns.org>
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. /**
  24. * @file libavcodec/eval.c
  25. * simple arithmetic expression evaluator.
  26. *
  27. * see http://joe.hotchkiss.com/programming/eval/eval.html
  28. */
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <math.h>
  33. #include "libavutil/mathematics.h"
  34. #include "avcodec.h"
  35. #include "eval.h"
  36. typedef struct Parser{
  37. int stack_index;
  38. char *s;
  39. const double *const_value;
  40. const char * const *const_name; // NULL terminated
  41. double (**func1)(void *, double a); // NULL terminated
  42. const char **func1_name; // NULL terminated
  43. double (**func2)(void *, double a, double b); // NULL terminated
  44. const char **func2_name; // NULL terminated
  45. void *opaque;
  46. const char **error;
  47. #define VARS 10
  48. double var[VARS];
  49. } Parser;
  50. static const int8_t si_prefixes['z' - 'E' + 1]={
  51. ['y'-'E']= -24,
  52. ['z'-'E']= -21,
  53. ['a'-'E']= -18,
  54. ['f'-'E']= -15,
  55. ['p'-'E']= -12,
  56. ['n'-'E']= - 9,
  57. ['u'-'E']= - 6,
  58. ['m'-'E']= - 3,
  59. ['c'-'E']= - 2,
  60. ['d'-'E']= - 1,
  61. ['h'-'E']= 2,
  62. ['k'-'E']= 3,
  63. ['K'-'E']= 3,
  64. ['M'-'E']= 6,
  65. ['G'-'E']= 9,
  66. ['T'-'E']= 12,
  67. ['P'-'E']= 15,
  68. ['E'-'E']= 18,
  69. ['Z'-'E']= 21,
  70. ['Y'-'E']= 24,
  71. };
  72. double av_strtod(const char *numstr, char **tail) {
  73. double d;
  74. char *next;
  75. d = strtod(numstr, &next);
  76. /* if parsing succeeded, check for and interpret postfixes */
  77. if (next!=numstr) {
  78. if(*next >= 'E' && *next <= 'z'){
  79. int e= si_prefixes[*next - 'E'];
  80. if(e){
  81. if(next[1] == 'i'){
  82. d*= pow( 2, e/0.3);
  83. next+=2;
  84. }else{
  85. d*= pow(10, e);
  86. next++;
  87. }
  88. }
  89. }
  90. if(*next=='B') {
  91. d*=8;
  92. next++;
  93. }
  94. }
  95. /* if requested, fill in tail with the position after the last parsed
  96. character */
  97. if (tail)
  98. *tail = next;
  99. return d;
  100. }
  101. static int strmatch(const char *s, const char *prefix){
  102. int i;
  103. for(i=0; prefix[i]; i++){
  104. if(prefix[i] != s[i]) return 0;
  105. }
  106. return 1;
  107. }
  108. struct ff_expr_s {
  109. enum {
  110. e_value, e_const, e_func0, e_func1, e_func2,
  111. e_squish, e_gauss, e_ld,
  112. e_mod, e_max, e_min, e_eq, e_gt, e_gte,
  113. e_pow, e_mul, e_div, e_add,
  114. e_last, e_st, e_while,
  115. } type;
  116. double value; // is sign in other types
  117. union {
  118. int const_index;
  119. double (*func0)(double);
  120. double (*func1)(void *, double);
  121. double (*func2)(void *, double, double);
  122. } a;
  123. AVEvalExpr * param[2];
  124. };
  125. static double eval_expr(Parser * p, AVEvalExpr * e) {
  126. switch (e->type) {
  127. case e_value: return e->value;
  128. case e_const: return e->value * p->const_value[e->a.const_index];
  129. case e_func0: return e->value * e->a.func0(eval_expr(p, e->param[0]));
  130. case e_func1: return e->value * e->a.func1(p->opaque, eval_expr(p, e->param[0]));
  131. case e_func2: return e->value * e->a.func2(p->opaque, eval_expr(p, e->param[0]), eval_expr(p, e->param[1]));
  132. case e_squish: return 1/(1+exp(4*eval_expr(p, e->param[0])));
  133. case e_gauss: { double d = eval_expr(p, e->param[0]); return exp(-d*d/2)/sqrt(2*M_PI); }
  134. case e_ld: return e->value * p->var[av_clip(eval_expr(p, e->param[0]), 0, VARS-1)];
  135. case e_while: {
  136. double d = NAN;
  137. while(eval_expr(p, e->param[0]))
  138. d=eval_expr(p, e->param[1]);
  139. return d;
  140. }
  141. default: {
  142. double d = eval_expr(p, e->param[0]);
  143. double d2 = eval_expr(p, e->param[1]);
  144. switch (e->type) {
  145. case e_mod: return e->value * (d - floor(d/d2)*d2);
  146. case e_max: return e->value * (d > d2 ? d : d2);
  147. case e_min: return e->value * (d < d2 ? d : d2);
  148. case e_eq: return e->value * (d == d2 ? 1.0 : 0.0);
  149. case e_gt: return e->value * (d > d2 ? 1.0 : 0.0);
  150. case e_gte: return e->value * (d >= d2 ? 1.0 : 0.0);
  151. case e_pow: return e->value * pow(d, d2);
  152. case e_mul: return e->value * (d * d2);
  153. case e_div: return e->value * (d / d2);
  154. case e_add: return e->value * (d + d2);
  155. case e_last:return e->value * d2;
  156. case e_st : return e->value * (p->var[av_clip(d, 0, VARS-1)]= d2);
  157. }
  158. }
  159. }
  160. return NAN;
  161. }
  162. static AVEvalExpr * parse_expr(Parser *p);
  163. void ff_eval_free(AVEvalExpr * e) {
  164. if (!e) return;
  165. ff_eval_free(e->param[0]);
  166. ff_eval_free(e->param[1]);
  167. av_freep(&e);
  168. }
  169. static AVEvalExpr * parse_primary(Parser *p) {
  170. AVEvalExpr * d = av_mallocz(sizeof(AVEvalExpr));
  171. char *next= p->s;
  172. int i;
  173. if (!d)
  174. return NULL;
  175. /* number */
  176. d->value = av_strtod(p->s, &next);
  177. if(next != p->s){
  178. d->type = e_value;
  179. p->s= next;
  180. return d;
  181. }
  182. d->value = 1;
  183. /* named constants */
  184. for(i=0; p->const_name && p->const_name[i]; i++){
  185. if(strmatch(p->s, p->const_name[i])){
  186. p->s+= strlen(p->const_name[i]);
  187. d->type = e_const;
  188. d->a.const_index = i;
  189. return d;
  190. }
  191. }
  192. p->s= strchr(p->s, '(');
  193. if(p->s==NULL){
  194. *p->error = "undefined constant or missing (";
  195. p->s= next;
  196. ff_eval_free(d);
  197. return NULL;
  198. }
  199. p->s++; // "("
  200. if (*next == '(') { // special case do-nothing
  201. av_freep(&d);
  202. d = parse_expr(p);
  203. if(p->s[0] != ')'){
  204. *p->error = "missing )";
  205. ff_eval_free(d);
  206. return NULL;
  207. }
  208. p->s++; // ")"
  209. return d;
  210. }
  211. d->param[0] = parse_expr(p);
  212. if(p->s[0]== ','){
  213. p->s++; // ","
  214. d->param[1] = parse_expr(p);
  215. }
  216. if(p->s[0] != ')'){
  217. *p->error = "missing )";
  218. ff_eval_free(d);
  219. return NULL;
  220. }
  221. p->s++; // ")"
  222. d->type = e_func0;
  223. if( strmatch(next, "sinh" ) ) d->a.func0 = sinh;
  224. else if( strmatch(next, "cosh" ) ) d->a.func0 = cosh;
  225. else if( strmatch(next, "tanh" ) ) d->a.func0 = tanh;
  226. else if( strmatch(next, "sin" ) ) d->a.func0 = sin;
  227. else if( strmatch(next, "cos" ) ) d->a.func0 = cos;
  228. else if( strmatch(next, "tan" ) ) d->a.func0 = tan;
  229. else if( strmatch(next, "atan" ) ) d->a.func0 = atan;
  230. else if( strmatch(next, "asin" ) ) d->a.func0 = asin;
  231. else if( strmatch(next, "acos" ) ) d->a.func0 = acos;
  232. else if( strmatch(next, "exp" ) ) d->a.func0 = exp;
  233. else if( strmatch(next, "log" ) ) d->a.func0 = log;
  234. else if( strmatch(next, "abs" ) ) d->a.func0 = fabs;
  235. else if( strmatch(next, "squish") ) d->type = e_squish;
  236. else if( strmatch(next, "gauss" ) ) d->type = e_gauss;
  237. else if( strmatch(next, "mod" ) ) d->type = e_mod;
  238. else if( strmatch(next, "max" ) ) d->type = e_max;
  239. else if( strmatch(next, "min" ) ) d->type = e_min;
  240. else if( strmatch(next, "eq" ) ) d->type = e_eq;
  241. else if( strmatch(next, "gte" ) ) d->type = e_gte;
  242. else if( strmatch(next, "gt" ) ) d->type = e_gt;
  243. else if( strmatch(next, "lte" ) ) { AVEvalExpr * tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gt; }
  244. else if( strmatch(next, "lt" ) ) { AVEvalExpr * tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gte; }
  245. else if( strmatch(next, "ld" ) ) d->type = e_ld;
  246. else if( strmatch(next, "st" ) ) d->type = e_st;
  247. else if( strmatch(next, "while" ) ) d->type = e_while;
  248. else {
  249. for(i=0; p->func1_name && p->func1_name[i]; i++){
  250. if(strmatch(next, p->func1_name[i])){
  251. d->a.func1 = p->func1[i];
  252. d->type = e_func1;
  253. return d;
  254. }
  255. }
  256. for(i=0; p->func2_name && p->func2_name[i]; i++){
  257. if(strmatch(next, p->func2_name[i])){
  258. d->a.func2 = p->func2[i];
  259. d->type = e_func2;
  260. return d;
  261. }
  262. }
  263. *p->error = "unknown function";
  264. ff_eval_free(d);
  265. return NULL;
  266. }
  267. return d;
  268. }
  269. static AVEvalExpr * new_eval_expr(int type, int value, AVEvalExpr *p0, AVEvalExpr *p1){
  270. AVEvalExpr * e = av_mallocz(sizeof(AVEvalExpr));
  271. if (!e)
  272. return NULL;
  273. e->type =type ;
  274. e->value =value ;
  275. e->param[0] =p0 ;
  276. e->param[1] =p1 ;
  277. return e;
  278. }
  279. static AVEvalExpr * parse_pow(Parser *p, int *sign){
  280. *sign= (*p->s == '+') - (*p->s == '-');
  281. p->s += *sign&1;
  282. return parse_primary(p);
  283. }
  284. static AVEvalExpr * parse_factor(Parser *p){
  285. int sign, sign2;
  286. AVEvalExpr * e = parse_pow(p, &sign);
  287. while(p->s[0]=='^'){
  288. p->s++;
  289. e= new_eval_expr(e_pow, 1, e, parse_pow(p, &sign2));
  290. if (!e)
  291. return NULL;
  292. if (e->param[1]) e->param[1]->value *= (sign2|1);
  293. }
  294. if (e) e->value *= (sign|1);
  295. return e;
  296. }
  297. static AVEvalExpr * parse_term(Parser *p){
  298. AVEvalExpr * e = parse_factor(p);
  299. while(p->s[0]=='*' || p->s[0]=='/'){
  300. int c= *p->s++;
  301. e= new_eval_expr(c == '*' ? e_mul : e_div, 1, e, parse_factor(p));
  302. if (!e)
  303. return NULL;
  304. }
  305. return e;
  306. }
  307. static AVEvalExpr * parse_subexpr(Parser *p) {
  308. AVEvalExpr * e = parse_term(p);
  309. while(*p->s == '+' || *p->s == '-') {
  310. e= new_eval_expr(e_add, 1, e, parse_term(p));
  311. if (!e)
  312. return NULL;
  313. };
  314. return e;
  315. }
  316. static AVEvalExpr * parse_expr(Parser *p) {
  317. AVEvalExpr * e;
  318. if(p->stack_index <= 0) //protect against stack overflows
  319. return NULL;
  320. p->stack_index--;
  321. e = parse_subexpr(p);
  322. while(*p->s == ';') {
  323. p->s++;
  324. e= new_eval_expr(e_last, 1, e, parse_subexpr(p));
  325. if (!e)
  326. return NULL;
  327. };
  328. p->stack_index++;
  329. return e;
  330. }
  331. static int verify_expr(AVEvalExpr * e) {
  332. if (!e) return 0;
  333. switch (e->type) {
  334. case e_value:
  335. case e_const: return 1;
  336. case e_func0:
  337. case e_func1:
  338. case e_squish:
  339. case e_ld:
  340. case e_gauss: return verify_expr(e->param[0]);
  341. default: return verify_expr(e->param[0]) && verify_expr(e->param[1]);
  342. }
  343. }
  344. AVEvalExpr * ff_parse(const char *s, const char * const *const_name,
  345. double (**func1)(void *, double), const char **func1_name,
  346. double (**func2)(void *, double, double), const char **func2_name,
  347. const char **error){
  348. Parser p;
  349. AVEvalExpr *e = NULL;
  350. char *w = av_malloc(strlen(s) + 1);
  351. char *wp = w;
  352. if (!w)
  353. goto end;
  354. while (*s)
  355. if (!isspace(*s++)) *wp++ = s[-1];
  356. *wp++ = 0;
  357. p.stack_index=100;
  358. p.s= w;
  359. p.const_name = const_name;
  360. p.func1 = func1;
  361. p.func1_name = func1_name;
  362. p.func2 = func2;
  363. p.func2_name = func2_name;
  364. p.error= error;
  365. e = parse_expr(&p);
  366. if (!verify_expr(e)) {
  367. ff_eval_free(e);
  368. e = NULL;
  369. }
  370. end:
  371. av_free(w);
  372. return e;
  373. }
  374. double ff_parse_eval(AVEvalExpr * e, const double *const_value, void *opaque) {
  375. Parser p;
  376. p.const_value= const_value;
  377. p.opaque = opaque;
  378. return eval_expr(&p, e);
  379. }
  380. double ff_eval2(const char *s, const double *const_value, const char * const *const_name,
  381. double (**func1)(void *, double), const char **func1_name,
  382. double (**func2)(void *, double, double), const char **func2_name,
  383. void *opaque, const char **error){
  384. AVEvalExpr * e = ff_parse(s, const_name, func1, func1_name, func2, func2_name, error);
  385. double d;
  386. if (!e) return NAN;
  387. d = ff_parse_eval(e, const_value, opaque);
  388. ff_eval_free(e);
  389. return d;
  390. }
  391. #ifdef TEST
  392. #undef printf
  393. static double const_values[]={
  394. M_PI,
  395. M_E,
  396. 0
  397. };
  398. static const char *const_names[]={
  399. "PI",
  400. "E",
  401. 0
  402. };
  403. int main(void){
  404. int i;
  405. printf("%f == 12.7\n", ff_eval2("1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", const_values, const_names, NULL, NULL, NULL, NULL, NULL, NULL));
  406. printf("%f == 0.931322575\n", ff_eval2("80G/80Gi", const_values, const_names, NULL, NULL, NULL, NULL, NULL, NULL));
  407. for(i=0; i<1050; i++){
  408. START_TIMER
  409. ff_eval2("1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", const_values, const_names, NULL, NULL, NULL, NULL, NULL, NULL);
  410. STOP_TIMER("ff_eval2")
  411. }
  412. return 0;
  413. }
  414. #endif