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.

306 lines
8.2KB

  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 <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <math.h>
  35. #ifndef NAN
  36. #define NAN 0.0/0.0
  37. #endif
  38. #ifndef M_PI
  39. #define M_PI 3.14159265358979323846
  40. #endif
  41. typedef struct Parser{
  42. int stack_index;
  43. char *s;
  44. double *const_value;
  45. const char **const_name; // NULL terminated
  46. double (**func1)(void *, double a); // NULL terminated
  47. const char **func1_name; // NULL terminated
  48. double (**func2)(void *, double a, double b); // NULL terminated
  49. char **func2_name; // NULL terminated
  50. void *opaque;
  51. char **error;
  52. } Parser;
  53. static double evalExpression(Parser *p);
  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. static double evalPrimary(Parser *p){
  117. double d, d2=NAN;
  118. char *next= p->s;
  119. int i;
  120. /* number */
  121. d= av_strtod(p->s, &next);
  122. if(next != p->s){
  123. p->s= next;
  124. return d;
  125. }
  126. /* named constants */
  127. for(i=0; p->const_name && p->const_name[i]; i++){
  128. if(strmatch(p->s, p->const_name[i])){
  129. p->s+= strlen(p->const_name[i]);
  130. return p->const_value[i];
  131. }
  132. }
  133. p->s= strchr(p->s, '(');
  134. if(p->s==NULL){
  135. *p->error = "missing (";
  136. p->s= next;
  137. return NAN;
  138. }
  139. p->s++; // "("
  140. d= evalExpression(p);
  141. if(p->s[0]== ','){
  142. p->s++; // ","
  143. d2= evalExpression(p);
  144. }
  145. if(p->s[0] != ')'){
  146. *p->error = "missing )";
  147. return NAN;
  148. }
  149. p->s++; // ")"
  150. if( strmatch(next, "sinh" ) ) d= sinh(d);
  151. else if( strmatch(next, "cosh" ) ) d= cosh(d);
  152. else if( strmatch(next, "tanh" ) ) d= tanh(d);
  153. else if( strmatch(next, "sin" ) ) d= sin(d);
  154. else if( strmatch(next, "cos" ) ) d= cos(d);
  155. else if( strmatch(next, "tan" ) ) d= tan(d);
  156. else if( strmatch(next, "exp" ) ) d= exp(d);
  157. else if( strmatch(next, "log" ) ) d= log(d);
  158. else if( strmatch(next, "squish") ) d= 1/(1+exp(4*d));
  159. else if( strmatch(next, "gauss" ) ) d= exp(-d*d/2)/sqrt(2*M_PI);
  160. else if( strmatch(next, "abs" ) ) d= fabs(d);
  161. else if( strmatch(next, "max" ) ) d= d > d2 ? d : d2;
  162. else if( strmatch(next, "min" ) ) d= d < d2 ? d : d2;
  163. else if( strmatch(next, "gt" ) ) d= d > d2 ? 1.0 : 0.0;
  164. else if( strmatch(next, "gte" ) ) d= d >= d2 ? 1.0 : 0.0;
  165. else if( strmatch(next, "lt" ) ) d= d > d2 ? 0.0 : 1.0;
  166. else if( strmatch(next, "lte" ) ) d= d >= d2 ? 0.0 : 1.0;
  167. else if( strmatch(next, "eq" ) ) d= d == d2 ? 1.0 : 0.0;
  168. else if( strmatch(next, "(" ) ) d= d;
  169. // else if( strmatch(next, "l1" ) ) d= 1 + d2*(d - 1);
  170. // else if( strmatch(next, "sq01" ) ) d= (d >= 0.0 && d <=1.0) ? 1.0 : 0.0;
  171. else{
  172. for(i=0; p->func1_name && p->func1_name[i]; i++){
  173. if(strmatch(next, p->func1_name[i])){
  174. return p->func1[i](p->opaque, d);
  175. }
  176. }
  177. for(i=0; p->func2_name && p->func2_name[i]; i++){
  178. if(strmatch(next, p->func2_name[i])){
  179. return p->func2[i](p->opaque, d, d2);
  180. }
  181. }
  182. *p->error = "unknown function";
  183. return NAN;
  184. }
  185. return d;
  186. }
  187. static double evalPow(Parser *p){
  188. int sign= (*p->s == '+') - (*p->s == '-');
  189. p->s += sign&1;
  190. return (sign|1) * evalPrimary(p);
  191. }
  192. static double evalFactor(Parser *p){
  193. double ret= evalPow(p);
  194. while(p->s[0]=='^'){
  195. p->s++;
  196. ret= pow(ret, evalPow(p));
  197. }
  198. return ret;
  199. }
  200. static double evalTerm(Parser *p){
  201. double ret= evalFactor(p);
  202. while(p->s[0]=='*' || p->s[0]=='/'){
  203. if(*p->s++ == '*') ret*= evalFactor(p);
  204. else ret/= evalFactor(p);
  205. }
  206. return ret;
  207. }
  208. static double evalExpression(Parser *p){
  209. double ret= 0;
  210. if(p->stack_index <= 0) //protect against stack overflows
  211. return NAN;
  212. p->stack_index--;
  213. do{
  214. ret += evalTerm(p);
  215. }while(*p->s == '+' || *p->s == '-');
  216. p->stack_index++;
  217. return ret;
  218. }
  219. double ff_eval2(char *s, double *const_value, const char **const_name,
  220. double (**func1)(void *, double), const char **func1_name,
  221. double (**func2)(void *, double, double), char **func2_name,
  222. void *opaque, char **error){
  223. Parser p;
  224. p.stack_index=100;
  225. p.s= s;
  226. p.const_value= const_value;
  227. p.const_name = const_name;
  228. p.func1 = func1;
  229. p.func1_name = func1_name;
  230. p.func2 = func2;
  231. p.func2_name = func2_name;
  232. p.opaque = opaque;
  233. p.error= error;
  234. return evalExpression(&p);
  235. }
  236. #if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0)
  237. attribute_deprecated double ff_eval(char *s, double *const_value, const char **const_name,
  238. double (**func1)(void *, double), const char **func1_name,
  239. double (**func2)(void *, double, double), char **func2_name,
  240. void *opaque){
  241. char *error=NULL;
  242. double ret;
  243. ret = ff_eval2(s, const_value, const_name, func1, func1_name, func2, func2_name, opaque, &error);
  244. if (error)
  245. av_log(NULL, AV_LOG_ERROR, "Error evaluating \"%s\": %s\n", s, error);
  246. return ret;
  247. }
  248. #endif
  249. #ifdef TEST
  250. #undef printf
  251. static double const_values[]={
  252. M_PI,
  253. M_E,
  254. 0
  255. };
  256. static const char *const_names[]={
  257. "PI",
  258. "E",
  259. 0
  260. };
  261. main(){
  262. int i;
  263. 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));
  264. printf("%f == 0.931322575\n", ff_eval("80G/80Gi", const_values, const_names, NULL, NULL, NULL, NULL, NULL));
  265. for(i=0; i<1050; i++){
  266. START_TIMER
  267. 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);
  268. STOP_TIMER("ff_eval")
  269. }
  270. }
  271. #endif