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.

458 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 libavcodec/eval.c
  23. * simple arithmetic expression evaluator.
  24. *
  25. * see http://joe.hotchkiss.com/programming/eval/eval.html
  26. */
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <math.h>
  31. #include "libavutil/mathematics.h"
  32. #include "avcodec.h"
  33. #include "eval.h"
  34. typedef struct Parser{
  35. int stack_index;
  36. char *s;
  37. const double *const_value;
  38. const char * const *const_name; // NULL terminated
  39. double (**func1)(void *, double a); // NULL terminated
  40. const char **func1_name; // NULL terminated
  41. double (**func2)(void *, double a, double b); // NULL terminated
  42. const char **func2_name; // NULL terminated
  43. void *opaque;
  44. const char **error;
  45. #define VARS 10
  46. double var[VARS];
  47. } Parser;
  48. static const int8_t si_prefixes['z' - 'E' + 1]={
  49. ['y'-'E']= -24,
  50. ['z'-'E']= -21,
  51. ['a'-'E']= -18,
  52. ['f'-'E']= -15,
  53. ['p'-'E']= -12,
  54. ['n'-'E']= - 9,
  55. ['u'-'E']= - 6,
  56. ['m'-'E']= - 3,
  57. ['c'-'E']= - 2,
  58. ['d'-'E']= - 1,
  59. ['h'-'E']= 2,
  60. ['k'-'E']= 3,
  61. ['K'-'E']= 3,
  62. ['M'-'E']= 6,
  63. ['G'-'E']= 9,
  64. ['T'-'E']= 12,
  65. ['P'-'E']= 15,
  66. ['E'-'E']= 18,
  67. ['Z'-'E']= 21,
  68. ['Y'-'E']= 24,
  69. };
  70. double av_strtod(const char *numstr, char **tail) {
  71. double d;
  72. char *next;
  73. d = strtod(numstr, &next);
  74. /* if parsing succeeded, check for and interpret postfixes */
  75. if (next!=numstr) {
  76. if(*next >= 'E' && *next <= 'z'){
  77. int e= si_prefixes[*next - 'E'];
  78. if(e){
  79. if(next[1] == 'i'){
  80. d*= pow( 2, e/0.3);
  81. next+=2;
  82. }else{
  83. d*= pow(10, e);
  84. next++;
  85. }
  86. }
  87. }
  88. if(*next=='B') {
  89. d*=8;
  90. next++;
  91. }
  92. }
  93. /* if requested, fill in tail with the position after the last parsed
  94. character */
  95. if (tail)
  96. *tail = next;
  97. return d;
  98. }
  99. static int strmatch(const char *s, const char *prefix){
  100. int i;
  101. for(i=0; prefix[i]; i++){
  102. if(prefix[i] != s[i]) return 0;
  103. }
  104. return 1;
  105. }
  106. struct AVExpr {
  107. enum {
  108. e_value, e_const, e_func0, e_func1, e_func2,
  109. e_squish, e_gauss, e_ld,
  110. e_mod, e_max, e_min, e_eq, e_gt, e_gte,
  111. e_pow, e_mul, e_div, e_add,
  112. e_last, e_st, e_while,
  113. } type;
  114. double value; // is sign in other types
  115. union {
  116. int const_index;
  117. double (*func0)(double);
  118. double (*func1)(void *, double);
  119. double (*func2)(void *, double, double);
  120. } a;
  121. struct AVExpr *param[2];
  122. };
  123. static double eval_expr(Parser * p, AVExpr * e) {
  124. switch (e->type) {
  125. case e_value: return e->value;
  126. case e_const: return e->value * p->const_value[e->a.const_index];
  127. case e_func0: return e->value * e->a.func0(eval_expr(p, e->param[0]));
  128. case e_func1: return e->value * e->a.func1(p->opaque, eval_expr(p, e->param[0]));
  129. case e_func2: return e->value * e->a.func2(p->opaque, eval_expr(p, e->param[0]), eval_expr(p, e->param[1]));
  130. case e_squish: return 1/(1+exp(4*eval_expr(p, e->param[0])));
  131. case e_gauss: { double d = eval_expr(p, e->param[0]); return exp(-d*d/2)/sqrt(2*M_PI); }
  132. case e_ld: return e->value * p->var[av_clip(eval_expr(p, e->param[0]), 0, VARS-1)];
  133. case e_while: {
  134. double d = NAN;
  135. while(eval_expr(p, e->param[0]))
  136. d=eval_expr(p, e->param[1]);
  137. return d;
  138. }
  139. default: {
  140. double d = eval_expr(p, e->param[0]);
  141. double d2 = eval_expr(p, e->param[1]);
  142. switch (e->type) {
  143. case e_mod: return e->value * (d - floor(d/d2)*d2);
  144. case e_max: return e->value * (d > d2 ? d : d2);
  145. case e_min: return e->value * (d < d2 ? d : d2);
  146. case e_eq: return e->value * (d == d2 ? 1.0 : 0.0);
  147. case e_gt: return e->value * (d > d2 ? 1.0 : 0.0);
  148. case e_gte: return e->value * (d >= d2 ? 1.0 : 0.0);
  149. case e_pow: return e->value * pow(d, d2);
  150. case e_mul: return e->value * (d * d2);
  151. case e_div: return e->value * (d / d2);
  152. case e_add: return e->value * (d + d2);
  153. case e_last:return e->value * d2;
  154. case e_st : return e->value * (p->var[av_clip(d, 0, VARS-1)]= d2);
  155. }
  156. }
  157. }
  158. return NAN;
  159. }
  160. static AVExpr * parse_expr(Parser *p);
  161. void ff_free_expr(AVExpr * e) {
  162. if (!e) return;
  163. ff_free_expr(e->param[0]);
  164. ff_free_expr(e->param[1]);
  165. av_freep(&e);
  166. }
  167. static AVExpr * parse_primary(Parser *p) {
  168. AVExpr * d = av_mallocz(sizeof(AVExpr));
  169. char *next= p->s;
  170. int i;
  171. if (!d)
  172. return NULL;
  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_free_expr(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_free_expr(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_free_expr(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" ) ) { AVExpr * tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gt; }
  242. else if( strmatch(next, "lt" ) ) { AVExpr * 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_free_expr(d);
  263. return NULL;
  264. }
  265. return d;
  266. }
  267. static AVExpr * new_eval_expr(int type, int value, AVExpr *p0, AVExpr *p1){
  268. AVExpr * e = av_mallocz(sizeof(AVExpr));
  269. if (!e)
  270. return NULL;
  271. e->type =type ;
  272. e->value =value ;
  273. e->param[0] =p0 ;
  274. e->param[1] =p1 ;
  275. return e;
  276. }
  277. static AVExpr * parse_pow(Parser *p, int *sign){
  278. *sign= (*p->s == '+') - (*p->s == '-');
  279. p->s += *sign&1;
  280. return parse_primary(p);
  281. }
  282. static AVExpr * parse_factor(Parser *p){
  283. int sign, sign2;
  284. AVExpr * e = parse_pow(p, &sign);
  285. while(p->s[0]=='^'){
  286. p->s++;
  287. e= new_eval_expr(e_pow, 1, e, parse_pow(p, &sign2));
  288. if (!e)
  289. return NULL;
  290. if (e->param[1]) e->param[1]->value *= (sign2|1);
  291. }
  292. if (e) e->value *= (sign|1);
  293. return e;
  294. }
  295. static AVExpr * parse_term(Parser *p){
  296. AVExpr * e = parse_factor(p);
  297. while(p->s[0]=='*' || p->s[0]=='/'){
  298. int c= *p->s++;
  299. e= new_eval_expr(c == '*' ? e_mul : e_div, 1, e, parse_factor(p));
  300. if (!e)
  301. return NULL;
  302. }
  303. return e;
  304. }
  305. static AVExpr * parse_subexpr(Parser *p) {
  306. AVExpr * e = parse_term(p);
  307. while(*p->s == '+' || *p->s == '-') {
  308. e= new_eval_expr(e_add, 1, e, parse_term(p));
  309. if (!e)
  310. return NULL;
  311. };
  312. return e;
  313. }
  314. static AVExpr * parse_expr(Parser *p) {
  315. AVExpr * e;
  316. if(p->stack_index <= 0) //protect against stack overflows
  317. return NULL;
  318. p->stack_index--;
  319. e = parse_subexpr(p);
  320. while(*p->s == ';') {
  321. p->s++;
  322. e= new_eval_expr(e_last, 1, e, parse_subexpr(p));
  323. if (!e)
  324. return NULL;
  325. };
  326. p->stack_index++;
  327. return e;
  328. }
  329. static int verify_expr(AVExpr * e) {
  330. if (!e) return 0;
  331. switch (e->type) {
  332. case e_value:
  333. case e_const: return 1;
  334. case e_func0:
  335. case e_func1:
  336. case e_squish:
  337. case e_ld:
  338. case e_gauss: return verify_expr(e->param[0]);
  339. default: return verify_expr(e->param[0]) && verify_expr(e->param[1]);
  340. }
  341. }
  342. AVExpr * ff_parse(const char *s, const char * const *const_name,
  343. double (**func1)(void *, double), const char **func1_name,
  344. double (**func2)(void *, double, double), const char **func2_name,
  345. const char **error){
  346. Parser p;
  347. AVExpr *e = NULL;
  348. char *w = av_malloc(strlen(s) + 1);
  349. char *wp = w;
  350. if (!w)
  351. goto end;
  352. while (*s)
  353. if (!isspace(*s++)) *wp++ = s[-1];
  354. *wp++ = 0;
  355. p.stack_index=100;
  356. p.s= w;
  357. p.const_name = const_name;
  358. p.func1 = func1;
  359. p.func1_name = func1_name;
  360. p.func2 = func2;
  361. p.func2_name = func2_name;
  362. p.error= error;
  363. e = parse_expr(&p);
  364. if (!verify_expr(e)) {
  365. ff_free_expr(e);
  366. e = NULL;
  367. }
  368. end:
  369. av_free(w);
  370. return e;
  371. }
  372. double ff_eval_expr(AVExpr * e, const double *const_value, void *opaque) {
  373. Parser p;
  374. p.const_value= const_value;
  375. p.opaque = opaque;
  376. return eval_expr(&p, e);
  377. }
  378. double ff_eval2(const char *s, const double *const_value, const char * const *const_name,
  379. double (**func1)(void *, double), const char **func1_name,
  380. double (**func2)(void *, double, double), const char **func2_name,
  381. void *opaque, const char **error){
  382. AVExpr * e = ff_parse(s, const_name, func1, func1_name, func2, func2_name, error);
  383. double d;
  384. if (!e) return NAN;
  385. d = ff_eval_expr(e, const_value, opaque);
  386. ff_free_expr(e);
  387. return d;
  388. }
  389. #ifdef TEST
  390. #undef printf
  391. static double const_values[]={
  392. M_PI,
  393. M_E,
  394. 0
  395. };
  396. static const char *const_names[]={
  397. "PI",
  398. "E",
  399. 0
  400. };
  401. int main(void){
  402. int i;
  403. 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));
  404. printf("%f == 0.931322575\n", ff_eval2("80G/80Gi", const_values, const_names, NULL, NULL, NULL, NULL, NULL, NULL));
  405. for(i=0; i<1050; i++){
  406. START_TIMER
  407. 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);
  408. STOP_TIMER("ff_eval2")
  409. }
  410. return 0;
  411. }
  412. #endif