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.

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