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.

448 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 "avcodec.h"
  30. #include "eval.h"
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <math.h>
  35. #ifndef NAN
  36. #define NAN 0.0/0.0
  37. #endif
  38. #ifndef M_PI
  39. #define M_PI 3.14159265358979323846
  40. #endif
  41. typedef struct Parser{
  42. int stack_index;
  43. char *s;
  44. const double *const_value;
  45. const char * const *const_name; // NULL terminated
  46. double (**func1)(void *, double a); // NULL terminated
  47. const char **func1_name; // NULL terminated
  48. double (**func2)(void *, double a, double b); // NULL terminated
  49. const char **func2_name; // NULL terminated
  50. void *opaque;
  51. const char **error;
  52. #define VARS 10
  53. double var[VARS];
  54. } Parser;
  55. static const int8_t si_prefixes['z' - 'E' + 1]={
  56. ['y'-'E']= -24,
  57. ['z'-'E']= -21,
  58. ['a'-'E']= -18,
  59. ['f'-'E']= -15,
  60. ['p'-'E']= -12,
  61. ['n'-'E']= - 9,
  62. ['u'-'E']= - 6,
  63. ['m'-'E']= - 3,
  64. ['c'-'E']= - 2,
  65. ['d'-'E']= - 1,
  66. ['h'-'E']= 2,
  67. ['k'-'E']= 3,
  68. ['K'-'E']= 3,
  69. ['M'-'E']= 6,
  70. ['G'-'E']= 9,
  71. ['T'-'E']= 12,
  72. ['P'-'E']= 15,
  73. ['E'-'E']= 18,
  74. ['Z'-'E']= 21,
  75. ['Y'-'E']= 24,
  76. };
  77. double av_strtod(const char *numstr, char **tail) {
  78. double d;
  79. char *next;
  80. d = strtod(numstr, &next);
  81. /* if parsing succeeded, check for and interpret postfixes */
  82. if (next!=numstr) {
  83. if(*next >= 'E' && *next <= 'z'){
  84. int e= si_prefixes[*next - 'E'];
  85. if(e){
  86. if(next[1] == 'i'){
  87. d*= pow( 2, e/0.3);
  88. next+=2;
  89. }else{
  90. d*= pow(10, e);
  91. next++;
  92. }
  93. }
  94. }
  95. if(*next=='B') {
  96. d*=8;
  97. next++;
  98. }
  99. }
  100. /* if requested, fill in tail with the position after the last parsed
  101. character */
  102. if (tail)
  103. *tail = next;
  104. return d;
  105. }
  106. static int strmatch(const char *s, const char *prefix){
  107. int i;
  108. for(i=0; prefix[i]; i++){
  109. if(prefix[i] != s[i]) return 0;
  110. }
  111. return 1;
  112. }
  113. struct ff_expr_s {
  114. enum {
  115. e_value, e_const, e_func0, e_func1, e_func2,
  116. e_squish, e_gauss, e_ld,
  117. e_mod, e_max, e_min, e_eq, e_gt, e_gte,
  118. e_pow, e_mul, e_div, e_add,
  119. e_last, e_st, e_while,
  120. } type;
  121. double value; // is sign in other types
  122. union {
  123. int const_index;
  124. double (*func0)(double);
  125. double (*func1)(void *, double);
  126. double (*func2)(void *, double, double);
  127. } a;
  128. AVEvalExpr * param[2];
  129. };
  130. static double eval_expr(Parser * p, AVEvalExpr * e) {
  131. switch (e->type) {
  132. case e_value: return e->value;
  133. case e_const: return e->value * p->const_value[e->a.const_index];
  134. case e_func0: return e->value * e->a.func0(eval_expr(p, e->param[0]));
  135. case e_func1: return e->value * e->a.func1(p->opaque, eval_expr(p, e->param[0]));
  136. case e_func2: return e->value * e->a.func2(p->opaque, eval_expr(p, e->param[0]), eval_expr(p, e->param[1]));
  137. case e_squish: return 1/(1+exp(4*eval_expr(p, e->param[0])));
  138. case e_gauss: { double d = eval_expr(p, e->param[0]); return exp(-d*d/2)/sqrt(2*M_PI); }
  139. case e_ld: return e->value * p->var[av_clip(eval_expr(p, e->param[0]), 0, VARS-1)];
  140. case e_while: {
  141. double d = NAN;
  142. while(eval_expr(p, e->param[0]))
  143. d=eval_expr(p, e->param[1]);
  144. return d;
  145. }
  146. default: {
  147. double d = eval_expr(p, e->param[0]);
  148. double d2 = eval_expr(p, e->param[1]);
  149. switch (e->type) {
  150. case e_mod: return e->value * (d - floor(d/d2)*d2);
  151. case e_max: return e->value * (d > d2 ? d : d2);
  152. case e_min: return e->value * (d < d2 ? d : d2);
  153. case e_eq: return e->value * (d == d2 ? 1.0 : 0.0);
  154. case e_gt: return e->value * (d > d2 ? 1.0 : 0.0);
  155. case e_gte: return e->value * (d >= d2 ? 1.0 : 0.0);
  156. case e_pow: return e->value * pow(d, d2);
  157. case e_mul: return e->value * (d * d2);
  158. case e_div: return e->value * (d / d2);
  159. case e_add: return e->value * (d + d2);
  160. case e_last:return e->value * d2;
  161. case e_st : return e->value * (p->var[av_clip(d, 0, VARS-1)]= d2);
  162. }
  163. }
  164. }
  165. return NAN;
  166. }
  167. static AVEvalExpr * parse_expr(Parser *p);
  168. void ff_eval_free(AVEvalExpr * e) {
  169. if (!e) return;
  170. ff_eval_free(e->param[0]);
  171. ff_eval_free(e->param[1]);
  172. av_freep(&e);
  173. }
  174. static AVEvalExpr * parse_primary(Parser *p) {
  175. AVEvalExpr * d = av_mallocz(sizeof(AVEvalExpr));
  176. char *next= p->s;
  177. int i;
  178. /* number */
  179. d->value = av_strtod(p->s, &next);
  180. if(next != p->s){
  181. d->type = e_value;
  182. p->s= next;
  183. return d;
  184. }
  185. d->value = 1;
  186. /* named constants */
  187. for(i=0; p->const_name && p->const_name[i]; i++){
  188. if(strmatch(p->s, p->const_name[i])){
  189. p->s+= strlen(p->const_name[i]);
  190. d->type = e_const;
  191. d->a.const_index = i;
  192. return d;
  193. }
  194. }
  195. p->s= strchr(p->s, '(');
  196. if(p->s==NULL){
  197. *p->error = "undefined constant or missing (";
  198. p->s= next;
  199. ff_eval_free(d);
  200. return NULL;
  201. }
  202. p->s++; // "("
  203. if (*next == '(') { // special case do-nothing
  204. av_freep(&d);
  205. d = parse_expr(p);
  206. if(p->s[0] != ')'){
  207. *p->error = "missing )";
  208. ff_eval_free(d);
  209. return NULL;
  210. }
  211. p->s++; // ")"
  212. return d;
  213. }
  214. d->param[0] = parse_expr(p);
  215. if(p->s[0]== ','){
  216. p->s++; // ","
  217. d->param[1] = parse_expr(p);
  218. }
  219. if(p->s[0] != ')'){
  220. *p->error = "missing )";
  221. ff_eval_free(d);
  222. return NULL;
  223. }
  224. p->s++; // ")"
  225. d->type = e_func0;
  226. if( strmatch(next, "sinh" ) ) d->a.func0 = sinh;
  227. else if( strmatch(next, "cosh" ) ) d->a.func0 = cosh;
  228. else if( strmatch(next, "tanh" ) ) d->a.func0 = tanh;
  229. else if( strmatch(next, "sin" ) ) d->a.func0 = sin;
  230. else if( strmatch(next, "cos" ) ) d->a.func0 = cos;
  231. else if( strmatch(next, "tan" ) ) d->a.func0 = tan;
  232. else if( strmatch(next, "atan" ) ) d->a.func0 = atan;
  233. else if( strmatch(next, "asin" ) ) d->a.func0 = asin;
  234. else if( strmatch(next, "acos" ) ) d->a.func0 = acos;
  235. else if( strmatch(next, "exp" ) ) d->a.func0 = exp;
  236. else if( strmatch(next, "log" ) ) d->a.func0 = log;
  237. else if( strmatch(next, "abs" ) ) d->a.func0 = fabs;
  238. else if( strmatch(next, "squish") ) d->type = e_squish;
  239. else if( strmatch(next, "gauss" ) ) d->type = e_gauss;
  240. else if( strmatch(next, "mod" ) ) d->type = e_mod;
  241. else if( strmatch(next, "max" ) ) d->type = e_max;
  242. else if( strmatch(next, "min" ) ) d->type = e_min;
  243. else if( strmatch(next, "eq" ) ) d->type = e_eq;
  244. else if( strmatch(next, "gte" ) ) d->type = e_gte;
  245. else if( strmatch(next, "gt" ) ) d->type = e_gt;
  246. else if( strmatch(next, "lte" ) ) { AVEvalExpr * tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gt; }
  247. else if( strmatch(next, "lt" ) ) { AVEvalExpr * tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gte; }
  248. else if( strmatch(next, "ld" ) ) d->type = e_ld;
  249. else if( strmatch(next, "st" ) ) d->type = e_st;
  250. else if( strmatch(next, "while" ) ) d->type = e_while;
  251. else {
  252. for(i=0; p->func1_name && p->func1_name[i]; i++){
  253. if(strmatch(next, p->func1_name[i])){
  254. d->a.func1 = p->func1[i];
  255. d->type = e_func1;
  256. return d;
  257. }
  258. }
  259. for(i=0; p->func2_name && p->func2_name[i]; i++){
  260. if(strmatch(next, p->func2_name[i])){
  261. d->a.func2 = p->func2[i];
  262. d->type = e_func2;
  263. return d;
  264. }
  265. }
  266. *p->error = "unknown function";
  267. ff_eval_free(d);
  268. return NULL;
  269. }
  270. return d;
  271. }
  272. static AVEvalExpr * new_eval_expr(int type, int value, AVEvalExpr *p0, AVEvalExpr *p1){
  273. AVEvalExpr * e = av_mallocz(sizeof(AVEvalExpr));
  274. e->type =type ;
  275. e->value =value ;
  276. e->param[0] =p0 ;
  277. e->param[1] =p1 ;
  278. return e;
  279. }
  280. static AVEvalExpr * parse_pow(Parser *p, int *sign){
  281. *sign= (*p->s == '+') - (*p->s == '-');
  282. p->s += *sign&1;
  283. return parse_primary(p);
  284. }
  285. static AVEvalExpr * parse_factor(Parser *p){
  286. int sign, sign2;
  287. AVEvalExpr * e = parse_pow(p, &sign);
  288. while(p->s[0]=='^'){
  289. p->s++;
  290. e= new_eval_expr(e_pow, 1, e, parse_pow(p, &sign2));
  291. if (e->param[1]) e->param[1]->value *= (sign2|1);
  292. }
  293. if (e) e->value *= (sign|1);
  294. return e;
  295. }
  296. static AVEvalExpr * parse_term(Parser *p){
  297. AVEvalExpr * e = parse_factor(p);
  298. while(p->s[0]=='*' || p->s[0]=='/'){
  299. int c= *p->s++;
  300. e= new_eval_expr(c == '*' ? e_mul : e_div, 1, e, parse_factor(p));
  301. }
  302. return e;
  303. }
  304. static AVEvalExpr * parse_subexpr(Parser *p) {
  305. AVEvalExpr * e = parse_term(p);
  306. while(*p->s == '+' || *p->s == '-') {
  307. e= new_eval_expr(e_add, 1, e, parse_term(p));
  308. };
  309. return e;
  310. }
  311. static AVEvalExpr * parse_expr(Parser *p) {
  312. AVEvalExpr * e;
  313. if(p->stack_index <= 0) //protect against stack overflows
  314. return NULL;
  315. p->stack_index--;
  316. e = parse_subexpr(p);
  317. while(*p->s == ';') {
  318. p->s++;
  319. e= new_eval_expr(e_last, 1, e, parse_subexpr(p));
  320. };
  321. p->stack_index++;
  322. return e;
  323. }
  324. static int verify_expr(AVEvalExpr * e) {
  325. if (!e) return 0;
  326. switch (e->type) {
  327. case e_value:
  328. case e_const: return 1;
  329. case e_func0:
  330. case e_func1:
  331. case e_squish:
  332. case e_ld:
  333. case e_gauss: return verify_expr(e->param[0]);
  334. default: return verify_expr(e->param[0]) && verify_expr(e->param[1]);
  335. }
  336. }
  337. AVEvalExpr * ff_parse(const char *s, const char * const *const_name,
  338. double (**func1)(void *, double), const char **func1_name,
  339. double (**func2)(void *, double, double), const char **func2_name,
  340. const char **error){
  341. Parser p;
  342. AVEvalExpr * e;
  343. char w[strlen(s) + 1], * wp = w;
  344. while (*s)
  345. if (!isspace(*s++)) *wp++ = s[-1];
  346. *wp++ = 0;
  347. p.stack_index=100;
  348. p.s= w;
  349. p.const_name = const_name;
  350. p.func1 = func1;
  351. p.func1_name = func1_name;
  352. p.func2 = func2;
  353. p.func2_name = func2_name;
  354. p.error= error;
  355. e = parse_expr(&p);
  356. if (!verify_expr(e)) {
  357. ff_eval_free(e);
  358. return NULL;
  359. }
  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