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