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.

452 lines
13KB

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