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.

443 lines
13KB

  1. /*
  2. * simple arithmetic expression evaluator
  3. *
  4. * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  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. char **error;
  53. } Parser;
  54. static int8_t si_prefixes['z' - 'E' + 1]={
  55. ['y'-'E']= -24,
  56. ['z'-'E']= -21,
  57. ['a'-'E']= -18,
  58. ['f'-'E']= -15,
  59. ['p'-'E']= -12,
  60. ['n'-'E']= - 9,
  61. ['u'-'E']= - 6,
  62. ['m'-'E']= - 3,
  63. ['c'-'E']= - 2,
  64. ['d'-'E']= - 1,
  65. ['h'-'E']= 2,
  66. ['k'-'E']= 3,
  67. ['K'-'E']= 3,
  68. ['M'-'E']= 6,
  69. ['G'-'E']= 9,
  70. ['T'-'E']= 12,
  71. ['P'-'E']= 15,
  72. ['E'-'E']= 18,
  73. ['Z'-'E']= 21,
  74. ['Y'-'E']= 24,
  75. };
  76. /** strtod() function extended with 'k', 'M', 'G', 'ki', 'Mi', 'Gi' and 'B'
  77. * postfixes. This allows using f.e. kB, MiB, G and B as a postfix. This
  78. * function assumes that the unit of numbers is bits not bytes.
  79. */
  80. static double av_strtod(const char *name, char **tail) {
  81. double d;
  82. char *next;
  83. d = strtod(name, &next);
  84. /* if parsing succeeded, check for and interpret postfixes */
  85. if (next!=name) {
  86. if(*next >= 'E' && *next <= 'z'){
  87. int e= si_prefixes[*next - 'E'];
  88. if(e){
  89. if(next[1] == 'i'){
  90. d*= pow( 2, e/0.3);
  91. next+=2;
  92. }else{
  93. d*= pow(10, e);
  94. next++;
  95. }
  96. }
  97. }
  98. if(*next=='B') {
  99. d*=8;
  100. *next++;
  101. }
  102. }
  103. /* if requested, fill in tail with the position after the last parsed
  104. character */
  105. if (tail)
  106. *tail = next;
  107. return d;
  108. }
  109. static int strmatch(const char *s, const char *prefix){
  110. int i;
  111. for(i=0; prefix[i]; i++){
  112. if(prefix[i] != s[i]) return 0;
  113. }
  114. return 1;
  115. }
  116. struct ff_expr_s {
  117. enum {
  118. e_value, e_const, e_func0, e_func1, e_func2,
  119. e_squish, e_gauss,
  120. e_mod, e_max, e_min, e_eq, e_gt, e_gte,
  121. e_pow, e_mul, e_div, e_add,
  122. } type;
  123. double value; // is sign in other types
  124. union {
  125. int const_index;
  126. double (*func0)(double);
  127. double (*func1)(void *, double);
  128. double (*func2)(void *, double, double);
  129. } a;
  130. AVEvalExpr * param[2];
  131. };
  132. static double eval_expr(Parser * p, AVEvalExpr * e) {
  133. switch (e->type) {
  134. case e_value: return e->value;
  135. case e_const: return e->value * p->const_value[e->a.const_index];
  136. case e_func0: return e->value * e->a.func0(eval_expr(p, e->param[0]));
  137. case e_func1: return e->value * e->a.func1(p->opaque, eval_expr(p, e->param[0]));
  138. case e_func2: return e->value * e->a.func2(p->opaque, eval_expr(p, e->param[0]), eval_expr(p, e->param[1]));
  139. case e_squish: return 1/(1+exp(4*eval_expr(p, e->param[0])));
  140. case e_gauss: { double d = eval_expr(p, e->param[0]); return exp(-d*d/2)/sqrt(2*M_PI); }
  141. default: {
  142. double d = eval_expr(p, e->param[0]);
  143. double d2 = eval_expr(p, e->param[1]);
  144. switch (e->type) {
  145. case e_mod: return e->value * (d - floor(d/d2)*d2);
  146. case e_max: return e->value * (d > d2 ? d : d2);
  147. case e_min: return e->value * (d < d2 ? d : d2);
  148. case e_eq: return e->value * (d == d2 ? 1.0 : 0.0);
  149. case e_gt: return e->value * (d > d2 ? 1.0 : 0.0);
  150. case e_gte: return e->value * (d >= d2 ? 1.0 : 0.0);
  151. case e_pow: return e->value * pow(d, d2);
  152. case e_mul: return e->value * (d * d2);
  153. case e_div: return e->value * (d / d2);
  154. case e_add: return e->value * (d + d2);
  155. }
  156. }
  157. }
  158. return NAN;
  159. }
  160. static AVEvalExpr * parse_expr(Parser *p);
  161. void ff_eval_free(AVEvalExpr * e) {
  162. if (!e) return;
  163. ff_eval_free(e->param[0]);
  164. ff_eval_free(e->param[1]);
  165. av_freep(&e);
  166. }
  167. static AVEvalExpr * parse_primary(Parser *p) {
  168. AVEvalExpr * d = av_mallocz(sizeof(AVEvalExpr));
  169. char *next= p->s;
  170. int i;
  171. /* number */
  172. d->value = av_strtod(p->s, &next);
  173. if(next != p->s){
  174. d->type = e_value;
  175. p->s= next;
  176. return d;
  177. }
  178. d->value = 1;
  179. /* named constants */
  180. for(i=0; p->const_name && p->const_name[i]; i++){
  181. if(strmatch(p->s, p->const_name[i])){
  182. p->s+= strlen(p->const_name[i]);
  183. d->type = e_const;
  184. d->a.const_index = i;
  185. return d;
  186. }
  187. }
  188. p->s= strchr(p->s, '(');
  189. if(p->s==NULL){
  190. *p->error = "missing (";
  191. p->s= next;
  192. ff_eval_free(d);
  193. return NULL;
  194. }
  195. p->s++; // "("
  196. if (*next == '(') { // special case do-nothing
  197. av_freep(&d);
  198. d = parse_expr(p);
  199. if(p->s[0] != ')'){
  200. *p->error = "missing )";
  201. ff_eval_free(d);
  202. return NULL;
  203. }
  204. p->s++; // ")"
  205. return d;
  206. }
  207. d->param[0] = parse_expr(p);
  208. if(p->s[0]== ','){
  209. p->s++; // ","
  210. d->param[1] = parse_expr(p);
  211. }
  212. if(p->s[0] != ')'){
  213. *p->error = "missing )";
  214. ff_eval_free(d);
  215. return NULL;
  216. }
  217. p->s++; // ")"
  218. d->type = e_func0;
  219. if( strmatch(next, "sinh" ) ) d->a.func0 = sinh;
  220. else if( strmatch(next, "cosh" ) ) d->a.func0 = cosh;
  221. else if( strmatch(next, "tanh" ) ) d->a.func0 = tanh;
  222. else if( strmatch(next, "sin" ) ) d->a.func0 = sin;
  223. else if( strmatch(next, "cos" ) ) d->a.func0 = cos;
  224. else if( strmatch(next, "tan" ) ) d->a.func0 = tan;
  225. else if( strmatch(next, "atan" ) ) d->a.func0 = atan;
  226. else if( strmatch(next, "asin" ) ) d->a.func0 = asin;
  227. else if( strmatch(next, "acos" ) ) d->a.func0 = acos;
  228. else if( strmatch(next, "exp" ) ) d->a.func0 = exp;
  229. else if( strmatch(next, "log" ) ) d->a.func0 = log;
  230. else if( strmatch(next, "abs" ) ) d->a.func0 = fabs;
  231. else if( strmatch(next, "squish") ) d->type = e_squish;
  232. else if( strmatch(next, "gauss" ) ) d->type = e_gauss;
  233. else if( strmatch(next, "mod" ) ) d->type = e_mod;
  234. else if( strmatch(next, "max" ) ) d->type = e_max;
  235. else if( strmatch(next, "min" ) ) d->type = e_min;
  236. else if( strmatch(next, "eq" ) ) d->type = e_eq;
  237. else if( strmatch(next, "gt" ) ) d->type = e_gt;
  238. else if( strmatch(next, "gte" ) ) d->type = e_gte;
  239. else if( strmatch(next, "lt" ) ) { AVEvalExpr * tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gte; }
  240. else if( strmatch(next, "lte" ) ) { AVEvalExpr * tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gt; }
  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_eval_free(d);
  258. return NULL;
  259. }
  260. return d;
  261. }
  262. static AVEvalExpr * parse_pow(Parser *p, int *sign){
  263. *sign= (*p->s == '+') - (*p->s == '-');
  264. p->s += *sign&1;
  265. return parse_primary(p);
  266. }
  267. static AVEvalExpr * parse_factor(Parser *p){
  268. int sign, sign2;
  269. AVEvalExpr * e = parse_pow(p, &sign);
  270. while(p->s[0]=='^'){
  271. AVEvalExpr * tmp = av_mallocz(sizeof(AVEvalExpr));
  272. p->s++;
  273. tmp->type = e_pow;
  274. tmp->value = 1.;
  275. tmp->param[0] = e;
  276. tmp->param[1] = parse_pow(p, &sign2);
  277. if (tmp->param[1]) tmp->param[1]->value *= (sign2|1);
  278. e = tmp;
  279. }
  280. if (e) e->value *= (sign|1);
  281. return e;
  282. }
  283. static AVEvalExpr * parse_term(Parser *p){
  284. AVEvalExpr * e = parse_factor(p);
  285. while(p->s[0]=='*' || p->s[0]=='/'){
  286. AVEvalExpr * tmp = av_mallocz(sizeof(AVEvalExpr));
  287. if(*p->s++ == '*') tmp->type = e_mul;
  288. else tmp->type = e_div;
  289. tmp->value = 1.;
  290. tmp->param[0] = e;
  291. tmp->param[1] = parse_factor(p);
  292. e = tmp;
  293. }
  294. return e;
  295. }
  296. static AVEvalExpr * parse_expr(Parser *p) {
  297. AVEvalExpr * e;
  298. if(p->stack_index <= 0) //protect against stack overflows
  299. return NULL;
  300. p->stack_index--;
  301. e = parse_term(p);
  302. while(*p->s == '+' || *p->s == '-') {
  303. AVEvalExpr * tmp = av_mallocz(sizeof(AVEvalExpr));
  304. tmp->type = e_add;
  305. tmp->value = 1.;
  306. tmp->param[0] = e;
  307. tmp->param[1] = parse_term(p);
  308. e = tmp;
  309. };
  310. p->stack_index++;
  311. return e;
  312. }
  313. static int verify_expr(AVEvalExpr * e) {
  314. if (!e) return 0;
  315. switch (e->type) {
  316. case e_value:
  317. case e_const: return 1;
  318. case e_func0:
  319. case e_func1:
  320. case e_squish:
  321. case e_gauss: return verify_expr(e->param[0]);
  322. default: return verify_expr(e->param[0]) && verify_expr(e->param[1]);
  323. }
  324. }
  325. AVEvalExpr * ff_parse(char *s, const char **const_name,
  326. double (**func1)(void *, double), const char **func1_name,
  327. double (**func2)(void *, double, double), char **func2_name,
  328. char **error){
  329. Parser p;
  330. AVEvalExpr * e;
  331. p.stack_index=100;
  332. p.s= s;
  333. p.const_name = const_name;
  334. p.func1 = func1;
  335. p.func1_name = func1_name;
  336. p.func2 = func2;
  337. p.func2_name = func2_name;
  338. p.error= error;
  339. e = parse_expr(&p);
  340. if (!verify_expr(e)) {
  341. ff_eval_free(e);
  342. return NULL;
  343. }
  344. return e;
  345. }
  346. double ff_parse_eval(AVEvalExpr * e, double *const_value, void *opaque) {
  347. Parser p;
  348. p.const_value= const_value;
  349. p.opaque = opaque;
  350. return eval_expr(&p, e);
  351. }
  352. double ff_eval2(char *s, double *const_value, const char **const_name,
  353. double (**func1)(void *, double), const char **func1_name,
  354. double (**func2)(void *, double, double), char **func2_name,
  355. void *opaque, char **error){
  356. AVEvalExpr * e = ff_parse(s, const_name, func1, func1_name, func2, func2_name, error);
  357. double d;
  358. if (!e) return NAN;
  359. d = ff_parse_eval(e, const_value, opaque);
  360. ff_eval_free(e);
  361. return d;
  362. }
  363. #if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0)
  364. attribute_deprecated double ff_eval(char *s, double *const_value, const char **const_name,
  365. double (**func1)(void *, double), const char **func1_name,
  366. double (**func2)(void *, double, double), char **func2_name,
  367. void *opaque){
  368. char *error=NULL;
  369. double ret;
  370. ret = ff_eval2(s, const_value, const_name, func1, func1_name, func2, func2_name, opaque, &error);
  371. if (error)
  372. av_log(NULL, AV_LOG_ERROR, "Error evaluating \"%s\": %s\n", s, error);
  373. return ret;
  374. }
  375. #endif
  376. #ifdef TEST
  377. #undef printf
  378. static double const_values[]={
  379. M_PI,
  380. M_E,
  381. 0
  382. };
  383. static const char *const_names[]={
  384. "PI",
  385. "E",
  386. 0
  387. };
  388. main(){
  389. int i;
  390. 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));
  391. printf("%f == 0.931322575\n", ff_eval("80G/80Gi", const_values, const_names, NULL, NULL, NULL, NULL, NULL));
  392. for(i=0; i<1050; i++){
  393. START_TIMER
  394. 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);
  395. STOP_TIMER("ff_eval")
  396. }
  397. }
  398. #endif