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.

263 lines
7.0KB

  1. /*
  2. * simple arithmetic expression evaluator
  3. *
  4. * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. */
  21. /**
  22. * @file eval.c
  23. * simple arithmetic expression evaluator.
  24. *
  25. * see http://joe.hotchkiss.com/programming/eval/eval.html
  26. */
  27. #include "avcodec.h"
  28. #include "mpegvideo.h"
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <math.h>
  33. #ifndef NAN
  34. #define NAN 0
  35. #endif
  36. #ifndef M_PI
  37. #define M_PI 3.14159265358979323846
  38. #endif
  39. typedef struct Parser{
  40. int stack_index;
  41. char *s;
  42. double *const_value;
  43. const char **const_name; // NULL terminated
  44. double (**func1)(void *, double a); // NULL terminated
  45. const char **func1_name; // NULL terminated
  46. double (**func2)(void *, double a, double b); // NULL terminated
  47. char **func2_name; // NULL terminated
  48. void *opaque;
  49. } Parser;
  50. static double evalExpression(Parser *p);
  51. static int strmatch(const char *s, const char *prefix){
  52. int i;
  53. for(i=0; prefix[i]; i++){
  54. if(prefix[i] != s[i]) return 0;
  55. }
  56. return 1;
  57. }
  58. static int8_t si_prefixes['z' - 'E' + 1]={
  59. ['y'-'E']= -24,
  60. ['z'-'E']= -21,
  61. ['a'-'E']= -18,
  62. ['f'-'E']= -15,
  63. ['p'-'E']= -12,
  64. ['n'-'E']= - 9,
  65. ['u'-'E']= - 6,
  66. ['m'-'E']= - 3,
  67. ['c'-'E']= - 2,
  68. ['d'-'E']= - 1,
  69. ['h'-'E']= 2,
  70. ['k'-'E']= 3,
  71. ['K'-'E']= 3,
  72. ['M'-'E']= 6,
  73. ['G'-'E']= 9,
  74. ['T'-'E']= 12,
  75. ['P'-'E']= 15,
  76. ['E'-'E']= 18,
  77. ['Z'-'E']= 21,
  78. ['Y'-'E']= 24,
  79. };
  80. static double evalPrimary(Parser *p){
  81. double d, d2=NAN;
  82. char *next= p->s;
  83. int i;
  84. /* number */
  85. d= strtod(p->s, &next);
  86. if(next != p->s){
  87. if(*next >= 'E' && *next <= 'z'){
  88. int e= si_prefixes[*next - 'E'];
  89. if(e){
  90. if(next[1] == 'i'){
  91. d*= pow( 2, e/0.3);
  92. next+=2;
  93. }else{
  94. d*= pow(10, e);
  95. next++;
  96. }
  97. }
  98. }
  99. p->s= next;
  100. return d;
  101. }
  102. /* named constants */
  103. for(i=0; p->const_name && p->const_name[i]; i++){
  104. if(strmatch(p->s, p->const_name[i])){
  105. p->s+= strlen(p->const_name[i]);
  106. return p->const_value[i];
  107. }
  108. }
  109. p->s= strchr(p->s, '(');
  110. if(p->s==NULL){
  111. av_log(NULL, AV_LOG_ERROR, "Parser: missing ( in \"%s\"\n", next);
  112. return NAN;
  113. }
  114. p->s++; // "("
  115. d= evalExpression(p);
  116. if(p->s[0]== ','){
  117. p->s++; // ","
  118. d2= evalExpression(p);
  119. }
  120. if(p->s[0] != ')'){
  121. av_log(NULL, AV_LOG_ERROR, "Parser: missing ) in \"%s\"\n", next);
  122. return NAN;
  123. }
  124. p->s++; // ")"
  125. if( strmatch(next, "sinh" ) ) d= sinh(d);
  126. else if( strmatch(next, "cosh" ) ) d= cosh(d);
  127. else if( strmatch(next, "tanh" ) ) d= tanh(d);
  128. else if( strmatch(next, "sin" ) ) d= sin(d);
  129. else if( strmatch(next, "cos" ) ) d= cos(d);
  130. else if( strmatch(next, "tan" ) ) d= tan(d);
  131. else if( strmatch(next, "exp" ) ) d= exp(d);
  132. else if( strmatch(next, "log" ) ) d= log(d);
  133. else if( strmatch(next, "squish") ) d= 1/(1+exp(4*d));
  134. else if( strmatch(next, "gauss" ) ) d= exp(-d*d/2)/sqrt(2*M_PI);
  135. else if( strmatch(next, "abs" ) ) d= fabs(d);
  136. else if( strmatch(next, "max" ) ) d= d > d2 ? d : d2;
  137. else if( strmatch(next, "min" ) ) d= d < d2 ? d : d2;
  138. else if( strmatch(next, "gt" ) ) d= d > d2 ? 1.0 : 0.0;
  139. else if( strmatch(next, "gte" ) ) d= d >= d2 ? 1.0 : 0.0;
  140. else if( strmatch(next, "lt" ) ) d= d > d2 ? 0.0 : 1.0;
  141. else if( strmatch(next, "lte" ) ) d= d >= d2 ? 0.0 : 1.0;
  142. else if( strmatch(next, "eq" ) ) d= d == d2 ? 1.0 : 0.0;
  143. else if( strmatch(next, "(" ) ) d= d;
  144. // else if( strmatch(next, "l1" ) ) d= 1 + d2*(d - 1);
  145. // else if( strmatch(next, "sq01" ) ) d= (d >= 0.0 && d <=1.0) ? 1.0 : 0.0;
  146. else{
  147. for(i=0; p->func1_name && p->func1_name[i]; i++){
  148. if(strmatch(next, p->func1_name[i])){
  149. return p->func1[i](p->opaque, d);
  150. }
  151. }
  152. for(i=0; p->func2_name && p->func2_name[i]; i++){
  153. if(strmatch(next, p->func2_name[i])){
  154. return p->func2[i](p->opaque, d, d2);
  155. }
  156. }
  157. av_log(NULL, AV_LOG_ERROR, "Parser: unknown function in \"%s\"\n", next);
  158. return NAN;
  159. }
  160. return d;
  161. }
  162. static double evalPow(Parser *p){
  163. int sign= (*p->s == '+') - (*p->s == '-');
  164. p->s += sign&1;
  165. return (sign|1) * evalPrimary(p);
  166. }
  167. static double evalFactor(Parser *p){
  168. double ret= evalPow(p);
  169. while(p->s[0]=='^'){
  170. p->s++;
  171. ret= pow(ret, evalPow(p));
  172. }
  173. return ret;
  174. }
  175. static double evalTerm(Parser *p){
  176. double ret= evalFactor(p);
  177. while(p->s[0]=='*' || p->s[0]=='/'){
  178. if(*p->s++ == '*') ret*= evalFactor(p);
  179. else ret/= evalFactor(p);
  180. }
  181. return ret;
  182. }
  183. static double evalExpression(Parser *p){
  184. double ret= 0;
  185. if(p->stack_index <= 0) //protect against stack overflows
  186. return NAN;
  187. p->stack_index--;
  188. do{
  189. ret += evalTerm(p);
  190. }while(*p->s == '+' || *p->s == '-');
  191. p->stack_index++;
  192. return ret;
  193. }
  194. double ff_eval(char *s, double *const_value, const char **const_name,
  195. double (**func1)(void *, double), const char **func1_name,
  196. double (**func2)(void *, double, double), char **func2_name,
  197. void *opaque){
  198. Parser p;
  199. p.stack_index=100;
  200. p.s= s;
  201. p.const_value= const_value;
  202. p.const_name = const_name;
  203. p.func1 = func1;
  204. p.func1_name = func1_name;
  205. p.func2 = func2;
  206. p.func2_name = func2_name;
  207. p.opaque = opaque;
  208. return evalExpression(&p);
  209. }
  210. #ifdef TEST
  211. #undef printf
  212. static double const_values[]={
  213. M_PI,
  214. M_E,
  215. 0
  216. };
  217. static const char *const_names[]={
  218. "PI",
  219. "E",
  220. 0
  221. };
  222. main(){
  223. int i;
  224. 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));
  225. printf("%f == 0.931322575\n", ff_eval("80G/80Gi", const_values, const_names, NULL, NULL, NULL, NULL, NULL));
  226. for(i=0; i<1050; i++){
  227. START_TIMER
  228. 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);
  229. STOP_TIMER("ff_eval")
  230. }
  231. }
  232. #endif