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.

304 lines
8.2KB

  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. char **error;
  50. } Parser;
  51. static double evalExpression(Parser *p);
  52. static int8_t si_prefixes['z' - 'E' + 1]={
  53. ['y'-'E']= -24,
  54. ['z'-'E']= -21,
  55. ['a'-'E']= -18,
  56. ['f'-'E']= -15,
  57. ['p'-'E']= -12,
  58. ['n'-'E']= - 9,
  59. ['u'-'E']= - 6,
  60. ['m'-'E']= - 3,
  61. ['c'-'E']= - 2,
  62. ['d'-'E']= - 1,
  63. ['h'-'E']= 2,
  64. ['k'-'E']= 3,
  65. ['K'-'E']= 3,
  66. ['M'-'E']= 6,
  67. ['G'-'E']= 9,
  68. ['T'-'E']= 12,
  69. ['P'-'E']= 15,
  70. ['E'-'E']= 18,
  71. ['Z'-'E']= 21,
  72. ['Y'-'E']= 24,
  73. };
  74. /** strtod() function extended with 'k', 'M', 'G', 'ki', 'Mi', 'Gi' and 'B'
  75. * postfixes. This allows using f.e. kB, MiB, G and B as a postfix. This
  76. * function assumes that the unit of numbers is bits not bytes.
  77. */
  78. static double av_strtod(const char *name, char **tail) {
  79. double d;
  80. char *next;
  81. d = strtod(name, &next);
  82. /* if parsing succeeded, check for and interpret postfixes */
  83. if (next!=name) {
  84. if(*next >= 'E' && *next <= 'z'){
  85. int e= si_prefixes[*next - 'E'];
  86. if(e){
  87. if(next[1] == 'i'){
  88. d*= pow( 2, e/0.3);
  89. next+=2;
  90. }else{
  91. d*= pow(10, e);
  92. next++;
  93. }
  94. }
  95. }
  96. if(*next=='B') {
  97. d*=8;
  98. *next++;
  99. }
  100. }
  101. /* if requested, fill in tail with the position after the last parsed
  102. character */
  103. if (tail)
  104. *tail = next;
  105. return d;
  106. }
  107. static int strmatch(const char *s, const char *prefix){
  108. int i;
  109. for(i=0; prefix[i]; i++){
  110. if(prefix[i] != s[i]) return 0;
  111. }
  112. return 1;
  113. }
  114. static double evalPrimary(Parser *p){
  115. double d, d2=NAN;
  116. char *next= p->s;
  117. int i;
  118. /* number */
  119. d= av_strtod(p->s, &next);
  120. if(next != p->s){
  121. p->s= next;
  122. return d;
  123. }
  124. /* named constants */
  125. for(i=0; p->const_name && p->const_name[i]; i++){
  126. if(strmatch(p->s, p->const_name[i])){
  127. p->s+= strlen(p->const_name[i]);
  128. return p->const_value[i];
  129. }
  130. }
  131. p->s= strchr(p->s, '(');
  132. if(p->s==NULL){
  133. *p->error = "missing (";
  134. p->s= next;
  135. return NAN;
  136. }
  137. p->s++; // "("
  138. d= evalExpression(p);
  139. if(p->s[0]== ','){
  140. p->s++; // ","
  141. d2= evalExpression(p);
  142. }
  143. if(p->s[0] != ')'){
  144. *p->error = "missing )";
  145. return NAN;
  146. }
  147. p->s++; // ")"
  148. if( strmatch(next, "sinh" ) ) d= sinh(d);
  149. else if( strmatch(next, "cosh" ) ) d= cosh(d);
  150. else if( strmatch(next, "tanh" ) ) d= tanh(d);
  151. else if( strmatch(next, "sin" ) ) d= sin(d);
  152. else if( strmatch(next, "cos" ) ) d= cos(d);
  153. else if( strmatch(next, "tan" ) ) d= tan(d);
  154. else if( strmatch(next, "exp" ) ) d= exp(d);
  155. else if( strmatch(next, "log" ) ) d= log(d);
  156. else if( strmatch(next, "squish") ) d= 1/(1+exp(4*d));
  157. else if( strmatch(next, "gauss" ) ) d= exp(-d*d/2)/sqrt(2*M_PI);
  158. else if( strmatch(next, "abs" ) ) d= fabs(d);
  159. else if( strmatch(next, "max" ) ) d= d > d2 ? d : d2;
  160. else if( strmatch(next, "min" ) ) d= d < d2 ? d : d2;
  161. else if( strmatch(next, "gt" ) ) d= d > d2 ? 1.0 : 0.0;
  162. else if( strmatch(next, "gte" ) ) d= d >= d2 ? 1.0 : 0.0;
  163. else if( strmatch(next, "lt" ) ) d= d > d2 ? 0.0 : 1.0;
  164. else if( strmatch(next, "lte" ) ) d= d >= d2 ? 0.0 : 1.0;
  165. else if( strmatch(next, "eq" ) ) d= d == d2 ? 1.0 : 0.0;
  166. else if( strmatch(next, "(" ) ) d= d;
  167. // else if( strmatch(next, "l1" ) ) d= 1 + d2*(d - 1);
  168. // else if( strmatch(next, "sq01" ) ) d= (d >= 0.0 && d <=1.0) ? 1.0 : 0.0;
  169. else{
  170. for(i=0; p->func1_name && p->func1_name[i]; i++){
  171. if(strmatch(next, p->func1_name[i])){
  172. return p->func1[i](p->opaque, d);
  173. }
  174. }
  175. for(i=0; p->func2_name && p->func2_name[i]; i++){
  176. if(strmatch(next, p->func2_name[i])){
  177. return p->func2[i](p->opaque, d, d2);
  178. }
  179. }
  180. *p->error = "unknown function";
  181. return NAN;
  182. }
  183. return d;
  184. }
  185. static double evalPow(Parser *p){
  186. int sign= (*p->s == '+') - (*p->s == '-');
  187. p->s += sign&1;
  188. return (sign|1) * evalPrimary(p);
  189. }
  190. static double evalFactor(Parser *p){
  191. double ret= evalPow(p);
  192. while(p->s[0]=='^'){
  193. p->s++;
  194. ret= pow(ret, evalPow(p));
  195. }
  196. return ret;
  197. }
  198. static double evalTerm(Parser *p){
  199. double ret= evalFactor(p);
  200. while(p->s[0]=='*' || p->s[0]=='/'){
  201. if(*p->s++ == '*') ret*= evalFactor(p);
  202. else ret/= evalFactor(p);
  203. }
  204. return ret;
  205. }
  206. static double evalExpression(Parser *p){
  207. double ret= 0;
  208. if(p->stack_index <= 0) //protect against stack overflows
  209. return NAN;
  210. p->stack_index--;
  211. do{
  212. ret += evalTerm(p);
  213. }while(*p->s == '+' || *p->s == '-');
  214. p->stack_index++;
  215. return ret;
  216. }
  217. double ff_eval2(char *s, double *const_value, const char **const_name,
  218. double (**func1)(void *, double), const char **func1_name,
  219. double (**func2)(void *, double, double), char **func2_name,
  220. void *opaque, char **error){
  221. Parser p;
  222. p.stack_index=100;
  223. p.s= s;
  224. p.const_value= const_value;
  225. p.const_name = const_name;
  226. p.func1 = func1;
  227. p.func1_name = func1_name;
  228. p.func2 = func2;
  229. p.func2_name = func2_name;
  230. p.opaque = opaque;
  231. p.error= error;
  232. return evalExpression(&p);
  233. }
  234. #if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0)
  235. attribute_deprecated double ff_eval(char *s, double *const_value, const char **const_name,
  236. double (**func1)(void *, double), const char **func1_name,
  237. double (**func2)(void *, double, double), char **func2_name,
  238. void *opaque){
  239. char *error=NULL;
  240. double ret;
  241. ret = ff_eval2(s, const_value, const_name, func1, func1_name, func2, func2_name, opaque, &error);
  242. if (error)
  243. av_log(NULL, AV_LOG_ERROR, "Error evaluating \"%s\": %s\n", s, error);
  244. return ret;
  245. }
  246. #endif
  247. #ifdef TEST
  248. #undef printf
  249. static double const_values[]={
  250. M_PI,
  251. M_E,
  252. 0
  253. };
  254. static const char *const_names[]={
  255. "PI",
  256. "E",
  257. 0
  258. };
  259. main(){
  260. int i;
  261. 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));
  262. printf("%f == 0.931322575\n", ff_eval("80G/80Gi", const_values, const_names, NULL, NULL, NULL, NULL, NULL));
  263. for(i=0; i<1050; i++){
  264. START_TIMER
  265. 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);
  266. STOP_TIMER("ff_eval")
  267. }
  268. }
  269. #endif