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.

462 lines
14KB

  1. /*
  2. * Copyright (c) 2002-2006 Michael Niedermayer <michaelni@gmx.at>
  3. * Copyright (c) 2006 Oded Shimon <ods15@ods15.dyndns.org>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * simple arithmetic expression evaluator.
  24. *
  25. * see http://joe.hotchkiss.com/programming/eval/eval.html
  26. */
  27. #include "libavutil/avutil.h"
  28. #include "eval.h"
  29. typedef struct Parser{
  30. const AVClass *class;
  31. int stack_index;
  32. char *s;
  33. const double *const_value;
  34. const char * const *const_name; // NULL terminated
  35. double (* const *func1)(void *, double a); // NULL terminated
  36. const char * const *func1_name; // NULL terminated
  37. double (* const *func2)(void *, double a, double b); // NULL terminated
  38. const char * const *func2_name; // NULL terminated
  39. void *opaque;
  40. int log_offset;
  41. void *log_ctx;
  42. #define VARS 10
  43. double var[VARS];
  44. } Parser;
  45. static const AVClass class = { "Eval", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT, offsetof(Parser,log_offset), offsetof(Parser,log_ctx) };
  46. static const int8_t si_prefixes['z' - 'E' + 1]={
  47. ['y'-'E']= -24,
  48. ['z'-'E']= -21,
  49. ['a'-'E']= -18,
  50. ['f'-'E']= -15,
  51. ['p'-'E']= -12,
  52. ['n'-'E']= - 9,
  53. ['u'-'E']= - 6,
  54. ['m'-'E']= - 3,
  55. ['c'-'E']= - 2,
  56. ['d'-'E']= - 1,
  57. ['h'-'E']= 2,
  58. ['k'-'E']= 3,
  59. ['K'-'E']= 3,
  60. ['M'-'E']= 6,
  61. ['G'-'E']= 9,
  62. ['T'-'E']= 12,
  63. ['P'-'E']= 15,
  64. ['E'-'E']= 18,
  65. ['Z'-'E']= 21,
  66. ['Y'-'E']= 24,
  67. };
  68. double av_strtod(const char *numstr, char **tail) {
  69. double d;
  70. char *next;
  71. d = strtod(numstr, &next);
  72. /* if parsing succeeded, check for and interpret postfixes */
  73. if (next!=numstr) {
  74. if(*next >= 'E' && *next <= 'z'){
  75. int e= si_prefixes[*next - 'E'];
  76. if(e){
  77. if(next[1] == 'i'){
  78. d*= pow( 2, e/0.3);
  79. next+=2;
  80. }else{
  81. d*= pow(10, e);
  82. next++;
  83. }
  84. }
  85. }
  86. if(*next=='B') {
  87. d*=8;
  88. next++;
  89. }
  90. }
  91. /* if requested, fill in tail with the position after the last parsed
  92. character */
  93. if (tail)
  94. *tail = next;
  95. return d;
  96. }
  97. static int strmatch(const char *s, const char *prefix){
  98. int i;
  99. for(i=0; prefix[i]; i++){
  100. if(prefix[i] != s[i]) return 0;
  101. }
  102. return 1;
  103. }
  104. struct AVExpr {
  105. enum {
  106. e_value, e_const, e_func0, e_func1, e_func2,
  107. e_squish, e_gauss, e_ld,
  108. e_mod, e_max, e_min, e_eq, e_gt, e_gte,
  109. e_pow, e_mul, e_div, e_add,
  110. e_last, e_st, e_while,
  111. } type;
  112. double value; // is sign in other types
  113. union {
  114. int const_index;
  115. double (*func0)(double);
  116. double (*func1)(void *, double);
  117. double (*func2)(void *, double, double);
  118. } a;
  119. struct AVExpr *param[2];
  120. };
  121. static double eval_expr(Parser * p, AVExpr * e) {
  122. switch (e->type) {
  123. case e_value: return e->value;
  124. case e_const: return e->value * p->const_value[e->a.const_index];
  125. case e_func0: return e->value * e->a.func0(eval_expr(p, e->param[0]));
  126. case e_func1: return e->value * e->a.func1(p->opaque, eval_expr(p, e->param[0]));
  127. case e_func2: return e->value * e->a.func2(p->opaque, eval_expr(p, e->param[0]), eval_expr(p, e->param[1]));
  128. case e_squish: return 1/(1+exp(4*eval_expr(p, e->param[0])));
  129. case e_gauss: { double d = eval_expr(p, e->param[0]); return exp(-d*d/2)/sqrt(2*M_PI); }
  130. case e_ld: return e->value * p->var[av_clip(eval_expr(p, e->param[0]), 0, VARS-1)];
  131. case e_while: {
  132. double d = NAN;
  133. while(eval_expr(p, e->param[0]))
  134. d=eval_expr(p, e->param[1]);
  135. return d;
  136. }
  137. default: {
  138. double d = eval_expr(p, e->param[0]);
  139. double d2 = eval_expr(p, e->param[1]);
  140. switch (e->type) {
  141. case e_mod: return e->value * (d - floor(d/d2)*d2);
  142. case e_max: return e->value * (d > d2 ? d : d2);
  143. case e_min: return e->value * (d < d2 ? d : d2);
  144. case e_eq: return e->value * (d == d2 ? 1.0 : 0.0);
  145. case e_gt: return e->value * (d > d2 ? 1.0 : 0.0);
  146. case e_gte: return e->value * (d >= d2 ? 1.0 : 0.0);
  147. case e_pow: return e->value * pow(d, d2);
  148. case e_mul: return e->value * (d * d2);
  149. case e_div: return e->value * (d / d2);
  150. case e_add: return e->value * (d + d2);
  151. case e_last:return e->value * d2;
  152. case e_st : return e->value * (p->var[av_clip(d, 0, VARS-1)]= d2);
  153. }
  154. }
  155. }
  156. return NAN;
  157. }
  158. static AVExpr * parse_expr(Parser *p);
  159. void ff_free_expr(AVExpr * e) {
  160. if (!e) return;
  161. ff_free_expr(e->param[0]);
  162. ff_free_expr(e->param[1]);
  163. av_freep(&e);
  164. }
  165. static AVExpr * parse_primary(Parser *p) {
  166. AVExpr * d = av_mallocz(sizeof(AVExpr));
  167. char *next= p->s;
  168. int i;
  169. if (!d)
  170. return NULL;
  171. /* number */
  172. d->value = av_strtod(p->s, &next);
  173. if(next != p->s){
  174. d->type = e_value;
  175. p->s= next;
  176. return d;
  177. }
  178. d->value = 1;
  179. /* named constants */
  180. for(i=0; p->const_name && p->const_name[i]; i++){
  181. if(strmatch(p->s, p->const_name[i])){
  182. p->s+= strlen(p->const_name[i]);
  183. d->type = e_const;
  184. d->a.const_index = i;
  185. return d;
  186. }
  187. }
  188. p->s= strchr(p->s, '(');
  189. if(p->s==NULL){
  190. av_log(p, AV_LOG_ERROR, "undefined constant or missing (\n");
  191. p->s= next;
  192. ff_free_expr(d);
  193. return NULL;
  194. }
  195. p->s++; // "("
  196. if (*next == '(') { // special case do-nothing
  197. av_freep(&d);
  198. d = parse_expr(p);
  199. if(p->s[0] != ')'){
  200. av_log(p, AV_LOG_ERROR, "missing )\n");
  201. ff_free_expr(d);
  202. return NULL;
  203. }
  204. p->s++; // ")"
  205. return d;
  206. }
  207. d->param[0] = parse_expr(p);
  208. if(p->s[0]== ','){
  209. p->s++; // ","
  210. d->param[1] = parse_expr(p);
  211. }
  212. if(p->s[0] != ')'){
  213. av_log(p, AV_LOG_ERROR, "missing )\n");
  214. ff_free_expr(d);
  215. return NULL;
  216. }
  217. p->s++; // ")"
  218. d->type = e_func0;
  219. if( strmatch(next, "sinh" ) ) d->a.func0 = sinh;
  220. else if( strmatch(next, "cosh" ) ) d->a.func0 = cosh;
  221. else if( strmatch(next, "tanh" ) ) d->a.func0 = tanh;
  222. else if( strmatch(next, "sin" ) ) d->a.func0 = sin;
  223. else if( strmatch(next, "cos" ) ) d->a.func0 = cos;
  224. else if( strmatch(next, "tan" ) ) d->a.func0 = tan;
  225. else if( strmatch(next, "atan" ) ) d->a.func0 = atan;
  226. else if( strmatch(next, "asin" ) ) d->a.func0 = asin;
  227. else if( strmatch(next, "acos" ) ) d->a.func0 = acos;
  228. else if( strmatch(next, "exp" ) ) d->a.func0 = exp;
  229. else if( strmatch(next, "log" ) ) d->a.func0 = log;
  230. else if( strmatch(next, "abs" ) ) d->a.func0 = fabs;
  231. else if( strmatch(next, "squish") ) d->type = e_squish;
  232. else if( strmatch(next, "gauss" ) ) d->type = e_gauss;
  233. else if( strmatch(next, "mod" ) ) d->type = e_mod;
  234. else if( strmatch(next, "max" ) ) d->type = e_max;
  235. else if( strmatch(next, "min" ) ) d->type = e_min;
  236. else if( strmatch(next, "eq" ) ) d->type = e_eq;
  237. else if( strmatch(next, "gte" ) ) d->type = e_gte;
  238. else if( strmatch(next, "gt" ) ) d->type = e_gt;
  239. else if( strmatch(next, "lte" ) ) { AVExpr * tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gt; }
  240. else if( strmatch(next, "lt" ) ) { AVExpr * tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gte; }
  241. else if( strmatch(next, "ld" ) ) d->type = e_ld;
  242. else if( strmatch(next, "st" ) ) d->type = e_st;
  243. else if( strmatch(next, "while" ) ) d->type = e_while;
  244. else {
  245. for(i=0; p->func1_name && p->func1_name[i]; i++){
  246. if(strmatch(next, p->func1_name[i])){
  247. d->a.func1 = p->func1[i];
  248. d->type = e_func1;
  249. return d;
  250. }
  251. }
  252. for(i=0; p->func2_name && p->func2_name[i]; i++){
  253. if(strmatch(next, p->func2_name[i])){
  254. d->a.func2 = p->func2[i];
  255. d->type = e_func2;
  256. return d;
  257. }
  258. }
  259. av_log(p, AV_LOG_ERROR, "unknown function\n");
  260. ff_free_expr(d);
  261. return NULL;
  262. }
  263. return d;
  264. }
  265. static AVExpr * new_eval_expr(int type, int value, AVExpr *p0, AVExpr *p1){
  266. AVExpr * e = av_mallocz(sizeof(AVExpr));
  267. if (!e)
  268. return NULL;
  269. e->type =type ;
  270. e->value =value ;
  271. e->param[0] =p0 ;
  272. e->param[1] =p1 ;
  273. return e;
  274. }
  275. static AVExpr * parse_pow(Parser *p, int *sign){
  276. *sign= (*p->s == '+') - (*p->s == '-');
  277. p->s += *sign&1;
  278. return parse_primary(p);
  279. }
  280. static AVExpr * parse_factor(Parser *p){
  281. int sign, sign2;
  282. AVExpr * e = parse_pow(p, &sign);
  283. while(p->s[0]=='^'){
  284. p->s++;
  285. e= new_eval_expr(e_pow, 1, e, parse_pow(p, &sign2));
  286. if (!e)
  287. return NULL;
  288. if (e->param[1]) e->param[1]->value *= (sign2|1);
  289. }
  290. if (e) e->value *= (sign|1);
  291. return e;
  292. }
  293. static AVExpr * parse_term(Parser *p){
  294. AVExpr * e = parse_factor(p);
  295. while(p->s[0]=='*' || p->s[0]=='/'){
  296. int c= *p->s++;
  297. e= new_eval_expr(c == '*' ? e_mul : e_div, 1, e, parse_factor(p));
  298. if (!e)
  299. return NULL;
  300. }
  301. return e;
  302. }
  303. static AVExpr * parse_subexpr(Parser *p) {
  304. AVExpr * e = parse_term(p);
  305. while(*p->s == '+' || *p->s == '-') {
  306. e= new_eval_expr(e_add, 1, e, parse_term(p));
  307. if (!e)
  308. return NULL;
  309. };
  310. return e;
  311. }
  312. static AVExpr * parse_expr(Parser *p) {
  313. AVExpr * e;
  314. if(p->stack_index <= 0) //protect against stack overflows
  315. return NULL;
  316. p->stack_index--;
  317. e = parse_subexpr(p);
  318. while(*p->s == ';') {
  319. p->s++;
  320. e= new_eval_expr(e_last, 1, e, parse_subexpr(p));
  321. if (!e)
  322. return NULL;
  323. };
  324. p->stack_index++;
  325. return e;
  326. }
  327. static int verify_expr(AVExpr * e) {
  328. if (!e) return 0;
  329. switch (e->type) {
  330. case e_value:
  331. case e_const: return 1;
  332. case e_func0:
  333. case e_func1:
  334. case e_squish:
  335. case e_ld:
  336. case e_gauss: return verify_expr(e->param[0]);
  337. default: return verify_expr(e->param[0]) && verify_expr(e->param[1]);
  338. }
  339. }
  340. AVExpr *ff_parse_expr(const char *s,
  341. const char * const *const_name,
  342. const char * const *func1_name, double (* const *func1)(void *, double),
  343. const char * const *func2_name, double (* const *func2)(void *, double, double),
  344. int log_offset, void *log_ctx)
  345. {
  346. Parser p;
  347. AVExpr *e = NULL;
  348. char *w = av_malloc(strlen(s) + 1);
  349. char *wp = w;
  350. if (!w)
  351. goto end;
  352. while (*s)
  353. if (!isspace(*s++)) *wp++ = s[-1];
  354. *wp++ = 0;
  355. p.class = &class;
  356. p.stack_index=100;
  357. p.s= w;
  358. p.const_name = const_name;
  359. p.func1 = func1;
  360. p.func1_name = func1_name;
  361. p.func2 = func2;
  362. p.func2_name = func2_name;
  363. p.log_offset = log_offset;
  364. p.log_ctx = log_ctx;
  365. e = parse_expr(&p);
  366. if (!verify_expr(e)) {
  367. ff_free_expr(e);
  368. e = NULL;
  369. }
  370. end:
  371. av_free(w);
  372. return e;
  373. }
  374. double ff_eval_expr(AVExpr * e, const double *const_value, void *opaque) {
  375. Parser p;
  376. p.const_value= const_value;
  377. p.opaque = opaque;
  378. return eval_expr(&p, e);
  379. }
  380. double ff_parse_and_eval_expr(const char *s,
  381. const char * const *const_name, const double *const_value,
  382. const char * const *func1_name, double (* const *func1)(void *, double),
  383. const char * const *func2_name, double (* const *func2)(void *, double, double),
  384. void *opaque, int log_offset, void *log_ctx)
  385. {
  386. AVExpr *e = ff_parse_expr(s, const_name, func1_name, func1, func2_name, func2, log_offset, log_ctx);
  387. double d;
  388. if (!e) return NAN;
  389. d = ff_eval_expr(e, const_value, opaque);
  390. ff_free_expr(e);
  391. return d;
  392. }
  393. #ifdef TEST
  394. #undef printf
  395. static double const_values[]={
  396. M_PI,
  397. M_E,
  398. 0
  399. };
  400. static const char *const_names[]={
  401. "PI",
  402. "E",
  403. 0
  404. };
  405. int main(void){
  406. int i;
  407. printf("%f == 12.7\n", ff_parse_and_eval_expr("1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, NULL));
  408. printf("%f == 0.931322575\n", ff_parse_and_eval_expr("80G/80Gi", const_names, const_values, NULL, NULL, NULL, NULL, NULL, NULL));
  409. for(i=0; i<1050; i++){
  410. START_TIMER
  411. ff_parse_and_eval_expr("1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", const_names, const_values, NULL, NULL, NULL, NULL, NULL, 0, NULL);
  412. STOP_TIMER("ff_parse_and_eval_expr")
  413. }
  414. return 0;
  415. }
  416. #endif