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.

231 lines
6.3KB

  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.0/0.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. extern double av_strtod(const char *name, char **tail);
  51. static double evalExpression(Parser *p);
  52. static int strmatch(const char *s, const char *prefix){
  53. int i;
  54. for(i=0; prefix[i]; i++){
  55. if(prefix[i] != s[i]) return 0;
  56. }
  57. return 1;
  58. }
  59. static double evalPrimary(Parser *p){
  60. double d, d2=NAN;
  61. char *next= p->s;
  62. int i;
  63. /* number */
  64. d= av_strtod(p->s, &next);
  65. if(next != p->s){
  66. p->s= next;
  67. return d;
  68. }
  69. /* named constants */
  70. for(i=0; p->const_name && p->const_name[i]; i++){
  71. if(strmatch(p->s, p->const_name[i])){
  72. p->s+= strlen(p->const_name[i]);
  73. return p->const_value[i];
  74. }
  75. }
  76. p->s= strchr(p->s, '(');
  77. if(p->s==NULL){
  78. av_log(NULL, AV_LOG_ERROR, "Parser: missing ( in \"%s\"\n", next);
  79. p->s= next;
  80. return NAN;
  81. }
  82. p->s++; // "("
  83. d= evalExpression(p);
  84. if(p->s[0]== ','){
  85. p->s++; // ","
  86. d2= evalExpression(p);
  87. }
  88. if(p->s[0] != ')'){
  89. av_log(NULL, AV_LOG_ERROR, "Parser: missing ) in \"%s\"\n", next);
  90. return NAN;
  91. }
  92. p->s++; // ")"
  93. if( strmatch(next, "sinh" ) ) d= sinh(d);
  94. else if( strmatch(next, "cosh" ) ) d= cosh(d);
  95. else if( strmatch(next, "tanh" ) ) d= tanh(d);
  96. else if( strmatch(next, "sin" ) ) d= sin(d);
  97. else if( strmatch(next, "cos" ) ) d= cos(d);
  98. else if( strmatch(next, "tan" ) ) d= tan(d);
  99. else if( strmatch(next, "exp" ) ) d= exp(d);
  100. else if( strmatch(next, "log" ) ) d= log(d);
  101. else if( strmatch(next, "squish") ) d= 1/(1+exp(4*d));
  102. else if( strmatch(next, "gauss" ) ) d= exp(-d*d/2)/sqrt(2*M_PI);
  103. else if( strmatch(next, "abs" ) ) d= fabs(d);
  104. else if( strmatch(next, "max" ) ) d= d > d2 ? d : d2;
  105. else if( strmatch(next, "min" ) ) d= d < d2 ? d : d2;
  106. else if( strmatch(next, "gt" ) ) d= d > d2 ? 1.0 : 0.0;
  107. else if( strmatch(next, "gte" ) ) d= d >= d2 ? 1.0 : 0.0;
  108. else if( strmatch(next, "lt" ) ) d= d > d2 ? 0.0 : 1.0;
  109. else if( strmatch(next, "lte" ) ) d= d >= d2 ? 0.0 : 1.0;
  110. else if( strmatch(next, "eq" ) ) d= d == d2 ? 1.0 : 0.0;
  111. else if( strmatch(next, "(" ) ) d= d;
  112. // else if( strmatch(next, "l1" ) ) d= 1 + d2*(d - 1);
  113. // else if( strmatch(next, "sq01" ) ) d= (d >= 0.0 && d <=1.0) ? 1.0 : 0.0;
  114. else{
  115. for(i=0; p->func1_name && p->func1_name[i]; i++){
  116. if(strmatch(next, p->func1_name[i])){
  117. return p->func1[i](p->opaque, d);
  118. }
  119. }
  120. for(i=0; p->func2_name && p->func2_name[i]; i++){
  121. if(strmatch(next, p->func2_name[i])){
  122. return p->func2[i](p->opaque, d, d2);
  123. }
  124. }
  125. av_log(NULL, AV_LOG_ERROR, "Parser: unknown function in \"%s\"\n", next);
  126. return NAN;
  127. }
  128. return d;
  129. }
  130. static double evalPow(Parser *p){
  131. int sign= (*p->s == '+') - (*p->s == '-');
  132. p->s += sign&1;
  133. return (sign|1) * evalPrimary(p);
  134. }
  135. static double evalFactor(Parser *p){
  136. double ret= evalPow(p);
  137. while(p->s[0]=='^'){
  138. p->s++;
  139. ret= pow(ret, evalPow(p));
  140. }
  141. return ret;
  142. }
  143. static double evalTerm(Parser *p){
  144. double ret= evalFactor(p);
  145. while(p->s[0]=='*' || p->s[0]=='/'){
  146. if(*p->s++ == '*') ret*= evalFactor(p);
  147. else ret/= evalFactor(p);
  148. }
  149. return ret;
  150. }
  151. static double evalExpression(Parser *p){
  152. double ret= 0;
  153. if(p->stack_index <= 0) //protect against stack overflows
  154. return NAN;
  155. p->stack_index--;
  156. do{
  157. ret += evalTerm(p);
  158. }while(*p->s == '+' || *p->s == '-');
  159. p->stack_index++;
  160. return ret;
  161. }
  162. double ff_eval(char *s, double *const_value, const char **const_name,
  163. double (**func1)(void *, double), const char **func1_name,
  164. double (**func2)(void *, double, double), char **func2_name,
  165. void *opaque){
  166. Parser p;
  167. p.stack_index=100;
  168. p.s= s;
  169. p.const_value= const_value;
  170. p.const_name = const_name;
  171. p.func1 = func1;
  172. p.func1_name = func1_name;
  173. p.func2 = func2;
  174. p.func2_name = func2_name;
  175. p.opaque = opaque;
  176. return evalExpression(&p);
  177. }
  178. #ifdef TEST
  179. #undef printf
  180. static double const_values[]={
  181. M_PI,
  182. M_E,
  183. 0
  184. };
  185. static const char *const_names[]={
  186. "PI",
  187. "E",
  188. 0
  189. };
  190. main(){
  191. int i;
  192. 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));
  193. printf("%f == 0.931322575\n", ff_eval("80G/80Gi", const_values, const_names, NULL, NULL, NULL, NULL, NULL));
  194. for(i=0; i<1050; i++){
  195. START_TIMER
  196. 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);
  197. STOP_TIMER("ff_eval")
  198. }
  199. }
  200. #endif