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.

467 lines
14KB

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