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.

447 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. /* number */
  174. d->value = av_strtod(p->s, &next);
  175. if(next != p->s){
  176. d->type = e_value;
  177. p->s= next;
  178. return d;
  179. }
  180. d->value = 1;
  181. /* named constants */
  182. for(i=0; p->const_name && p->const_name[i]; i++){
  183. if(strmatch(p->s, p->const_name[i])){
  184. p->s+= strlen(p->const_name[i]);
  185. d->type = e_const;
  186. d->a.const_index = i;
  187. return d;
  188. }
  189. }
  190. p->s= strchr(p->s, '(');
  191. if(p->s==NULL){
  192. *p->error = "undefined constant or missing (";
  193. p->s= next;
  194. ff_eval_free(d);
  195. return NULL;
  196. }
  197. p->s++; // "("
  198. if (*next == '(') { // special case do-nothing
  199. av_freep(&d);
  200. d = parse_expr(p);
  201. if(p->s[0] != ')'){
  202. *p->error = "missing )";
  203. ff_eval_free(d);
  204. return NULL;
  205. }
  206. p->s++; // ")"
  207. return d;
  208. }
  209. d->param[0] = parse_expr(p);
  210. if(p->s[0]== ','){
  211. p->s++; // ","
  212. d->param[1] = parse_expr(p);
  213. }
  214. if(p->s[0] != ')'){
  215. *p->error = "missing )";
  216. ff_eval_free(d);
  217. return NULL;
  218. }
  219. p->s++; // ")"
  220. d->type = e_func0;
  221. if( strmatch(next, "sinh" ) ) d->a.func0 = sinh;
  222. else if( strmatch(next, "cosh" ) ) d->a.func0 = cosh;
  223. else if( strmatch(next, "tanh" ) ) d->a.func0 = tanh;
  224. else if( strmatch(next, "sin" ) ) d->a.func0 = sin;
  225. else if( strmatch(next, "cos" ) ) d->a.func0 = cos;
  226. else if( strmatch(next, "tan" ) ) d->a.func0 = tan;
  227. else if( strmatch(next, "atan" ) ) d->a.func0 = atan;
  228. else if( strmatch(next, "asin" ) ) d->a.func0 = asin;
  229. else if( strmatch(next, "acos" ) ) d->a.func0 = acos;
  230. else if( strmatch(next, "exp" ) ) d->a.func0 = exp;
  231. else if( strmatch(next, "log" ) ) d->a.func0 = log;
  232. else if( strmatch(next, "abs" ) ) d->a.func0 = fabs;
  233. else if( strmatch(next, "squish") ) d->type = e_squish;
  234. else if( strmatch(next, "gauss" ) ) d->type = e_gauss;
  235. else if( strmatch(next, "mod" ) ) d->type = e_mod;
  236. else if( strmatch(next, "max" ) ) d->type = e_max;
  237. else if( strmatch(next, "min" ) ) d->type = e_min;
  238. else if( strmatch(next, "eq" ) ) d->type = e_eq;
  239. else if( strmatch(next, "gte" ) ) d->type = e_gte;
  240. else if( strmatch(next, "gt" ) ) d->type = e_gt;
  241. else if( strmatch(next, "lte" ) ) { AVEvalExpr * tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gt; }
  242. else if( strmatch(next, "lt" ) ) { AVEvalExpr * tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gte; }
  243. else if( strmatch(next, "ld" ) ) d->type = e_ld;
  244. else if( strmatch(next, "st" ) ) d->type = e_st;
  245. else if( strmatch(next, "while" ) ) d->type = e_while;
  246. else {
  247. for(i=0; p->func1_name && p->func1_name[i]; i++){
  248. if(strmatch(next, p->func1_name[i])){
  249. d->a.func1 = p->func1[i];
  250. d->type = e_func1;
  251. return d;
  252. }
  253. }
  254. for(i=0; p->func2_name && p->func2_name[i]; i++){
  255. if(strmatch(next, p->func2_name[i])){
  256. d->a.func2 = p->func2[i];
  257. d->type = e_func2;
  258. return d;
  259. }
  260. }
  261. *p->error = "unknown function";
  262. ff_eval_free(d);
  263. return NULL;
  264. }
  265. return d;
  266. }
  267. static AVEvalExpr * new_eval_expr(int type, int value, AVEvalExpr *p0, AVEvalExpr *p1){
  268. AVEvalExpr * e = av_mallocz(sizeof(AVEvalExpr));
  269. e->type =type ;
  270. e->value =value ;
  271. e->param[0] =p0 ;
  272. e->param[1] =p1 ;
  273. return e;
  274. }
  275. static AVEvalExpr * parse_pow(Parser *p, int *sign){
  276. *sign= (*p->s == '+') - (*p->s == '-');
  277. p->s += *sign&1;
  278. return parse_primary(p);
  279. }
  280. static AVEvalExpr * parse_factor(Parser *p){
  281. int sign, sign2;
  282. AVEvalExpr * e = parse_pow(p, &sign);
  283. while(p->s[0]=='^'){
  284. p->s++;
  285. e= new_eval_expr(e_pow, 1, e, parse_pow(p, &sign2));
  286. if (e->param[1]) e->param[1]->value *= (sign2|1);
  287. }
  288. if (e) e->value *= (sign|1);
  289. return e;
  290. }
  291. static AVEvalExpr * parse_term(Parser *p){
  292. AVEvalExpr * e = parse_factor(p);
  293. while(p->s[0]=='*' || p->s[0]=='/'){
  294. int c= *p->s++;
  295. e= new_eval_expr(c == '*' ? e_mul : e_div, 1, e, parse_factor(p));
  296. }
  297. return e;
  298. }
  299. static AVEvalExpr * parse_subexpr(Parser *p) {
  300. AVEvalExpr * e = parse_term(p);
  301. while(*p->s == '+' || *p->s == '-') {
  302. e= new_eval_expr(e_add, 1, e, parse_term(p));
  303. };
  304. return e;
  305. }
  306. static AVEvalExpr * parse_expr(Parser *p) {
  307. AVEvalExpr * e;
  308. if(p->stack_index <= 0) //protect against stack overflows
  309. return NULL;
  310. p->stack_index--;
  311. e = parse_subexpr(p);
  312. while(*p->s == ';') {
  313. p->s++;
  314. e= new_eval_expr(e_last, 1, e, parse_subexpr(p));
  315. };
  316. p->stack_index++;
  317. return e;
  318. }
  319. static int verify_expr(AVEvalExpr * e) {
  320. if (!e) return 0;
  321. switch (e->type) {
  322. case e_value:
  323. case e_const: return 1;
  324. case e_func0:
  325. case e_func1:
  326. case e_squish:
  327. case e_ld:
  328. case e_gauss: return verify_expr(e->param[0]);
  329. default: return verify_expr(e->param[0]) && verify_expr(e->param[1]);
  330. }
  331. }
  332. AVEvalExpr * ff_parse(const char *s, const char * const *const_name,
  333. double (**func1)(void *, double), const char **func1_name,
  334. double (**func2)(void *, double, double), const char **func2_name,
  335. const char **error){
  336. Parser p;
  337. AVEvalExpr *e = NULL;
  338. char *w = av_malloc(strlen(s) + 1);
  339. char *wp = w;
  340. if (!w)
  341. goto end;
  342. while (*s)
  343. if (!isspace(*s++)) *wp++ = s[-1];
  344. *wp++ = 0;
  345. p.stack_index=100;
  346. p.s= w;
  347. p.const_name = const_name;
  348. p.func1 = func1;
  349. p.func1_name = func1_name;
  350. p.func2 = func2;
  351. p.func2_name = func2_name;
  352. p.error= error;
  353. e = parse_expr(&p);
  354. if (!verify_expr(e)) {
  355. ff_eval_free(e);
  356. e = NULL;
  357. }
  358. end:
  359. av_free(w);
  360. return e;
  361. }
  362. double ff_parse_eval(AVEvalExpr * e, const double *const_value, void *opaque) {
  363. Parser p;
  364. p.const_value= const_value;
  365. p.opaque = opaque;
  366. return eval_expr(&p, e);
  367. }
  368. double ff_eval2(const char *s, const double *const_value, const char * const *const_name,
  369. double (**func1)(void *, double), const char **func1_name,
  370. double (**func2)(void *, double, double), const char **func2_name,
  371. void *opaque, const char **error){
  372. AVEvalExpr * e = ff_parse(s, const_name, func1, func1_name, func2, func2_name, error);
  373. double d;
  374. if (!e) return NAN;
  375. d = ff_parse_eval(e, const_value, opaque);
  376. ff_eval_free(e);
  377. return d;
  378. }
  379. #ifdef TEST
  380. #undef printf
  381. static double const_values[]={
  382. M_PI,
  383. M_E,
  384. 0
  385. };
  386. static const char *const_names[]={
  387. "PI",
  388. "E",
  389. 0
  390. };
  391. int main(void){
  392. int i;
  393. 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));
  394. printf("%f == 0.931322575\n", ff_eval2("80G/80Gi", const_values, const_names, NULL, NULL, NULL, NULL, NULL, NULL));
  395. for(i=0; i<1050; i++){
  396. START_TIMER
  397. 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);
  398. STOP_TIMER("ff_eval2")
  399. }
  400. return 0;
  401. }
  402. #endif