Add function to compute ceil(log2(x)).
[FFMpeg-mirror/lagarith.git] / libavcodec / eval.c
blob1d52ba582bf45dfe4fe085f96a6b395f28cef0c1
1 /*
2 * simple arithmetic expression evaluator
4 * Copyright (c) 2002-2006 Michael Niedermayer <michaelni@gmx.at>
5 * Copyright (c) 2006 Oded Shimon <ods15@ods15.dyndns.org>
7 * This file is part of FFmpeg.
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 /**
25 * @file libavcodec/eval.c
26 * simple arithmetic expression evaluator.
28 * see http://joe.hotchkiss.com/programming/eval/eval.html
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <math.h>
36 #include "libavutil/mathematics.h"
37 #include "avcodec.h"
38 #include "eval.h"
40 typedef struct Parser{
41 int stack_index;
42 char *s;
43 const double *const_value;
44 const char * const *const_name; // NULL terminated
45 double (**func1)(void *, double a); // NULL terminated
46 const char **func1_name; // NULL terminated
47 double (**func2)(void *, double a, double b); // NULL terminated
48 const char **func2_name; // NULL terminated
49 void *opaque;
50 const char **error;
51 #define VARS 10
52 double var[VARS];
53 } Parser;
55 static const int8_t si_prefixes['z' - 'E' + 1]={
56 ['y'-'E']= -24,
57 ['z'-'E']= -21,
58 ['a'-'E']= -18,
59 ['f'-'E']= -15,
60 ['p'-'E']= -12,
61 ['n'-'E']= - 9,
62 ['u'-'E']= - 6,
63 ['m'-'E']= - 3,
64 ['c'-'E']= - 2,
65 ['d'-'E']= - 1,
66 ['h'-'E']= 2,
67 ['k'-'E']= 3,
68 ['K'-'E']= 3,
69 ['M'-'E']= 6,
70 ['G'-'E']= 9,
71 ['T'-'E']= 12,
72 ['P'-'E']= 15,
73 ['E'-'E']= 18,
74 ['Z'-'E']= 21,
75 ['Y'-'E']= 24,
78 double av_strtod(const char *numstr, char **tail) {
79 double d;
80 char *next;
81 d = strtod(numstr, &next);
82 /* if parsing succeeded, check for and interpret postfixes */
83 if (next!=numstr) {
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++;
98 if(*next=='B') {
99 d*=8;
100 next++;
103 /* if requested, fill in tail with the position after the last parsed
104 character */
105 if (tail)
106 *tail = next;
107 return d;
110 static int strmatch(const char *s, const char *prefix){
111 int i;
112 for(i=0; prefix[i]; i++){
113 if(prefix[i] != s[i]) return 0;
115 return 1;
118 struct ff_expr_s {
119 enum {
120 e_value, e_const, e_func0, e_func1, e_func2,
121 e_squish, e_gauss, e_ld,
122 e_mod, e_max, e_min, e_eq, e_gt, e_gte,
123 e_pow, e_mul, e_div, e_add,
124 e_last, e_st, e_while,
125 } type;
126 double value; // is sign in other types
127 union {
128 int const_index;
129 double (*func0)(double);
130 double (*func1)(void *, double);
131 double (*func2)(void *, double, double);
132 } a;
133 AVEvalExpr * param[2];
136 static double eval_expr(Parser * p, AVEvalExpr * e) {
137 switch (e->type) {
138 case e_value: return e->value;
139 case e_const: return e->value * p->const_value[e->a.const_index];
140 case e_func0: return e->value * e->a.func0(eval_expr(p, e->param[0]));
141 case e_func1: return e->value * e->a.func1(p->opaque, eval_expr(p, e->param[0]));
142 case e_func2: return e->value * e->a.func2(p->opaque, eval_expr(p, e->param[0]), eval_expr(p, e->param[1]));
143 case e_squish: return 1/(1+exp(4*eval_expr(p, e->param[0])));
144 case e_gauss: { double d = eval_expr(p, e->param[0]); return exp(-d*d/2)/sqrt(2*M_PI); }
145 case e_ld: return e->value * p->var[av_clip(eval_expr(p, e->param[0]), 0, VARS-1)];
146 case e_while: {
147 double d = NAN;
148 while(eval_expr(p, e->param[0]))
149 d=eval_expr(p, e->param[1]);
150 return d;
152 default: {
153 double d = eval_expr(p, e->param[0]);
154 double d2 = eval_expr(p, e->param[1]);
155 switch (e->type) {
156 case e_mod: return e->value * (d - floor(d/d2)*d2);
157 case e_max: return e->value * (d > d2 ? d : d2);
158 case e_min: return e->value * (d < d2 ? d : d2);
159 case e_eq: return e->value * (d == d2 ? 1.0 : 0.0);
160 case e_gt: return e->value * (d > d2 ? 1.0 : 0.0);
161 case e_gte: return e->value * (d >= d2 ? 1.0 : 0.0);
162 case e_pow: return e->value * pow(d, d2);
163 case e_mul: return e->value * (d * d2);
164 case e_div: return e->value * (d / d2);
165 case e_add: return e->value * (d + d2);
166 case e_last:return e->value * d2;
167 case e_st : return e->value * (p->var[av_clip(d, 0, VARS-1)]= d2);
171 return NAN;
174 static AVEvalExpr * parse_expr(Parser *p);
176 void ff_eval_free(AVEvalExpr * e) {
177 if (!e) return;
178 ff_eval_free(e->param[0]);
179 ff_eval_free(e->param[1]);
180 av_freep(&e);
183 static AVEvalExpr * parse_primary(Parser *p) {
184 AVEvalExpr * d = av_mallocz(sizeof(AVEvalExpr));
185 char *next= p->s;
186 int i;
188 /* number */
189 d->value = av_strtod(p->s, &next);
190 if(next != p->s){
191 d->type = e_value;
192 p->s= next;
193 return d;
195 d->value = 1;
197 /* named constants */
198 for(i=0; p->const_name && p->const_name[i]; i++){
199 if(strmatch(p->s, p->const_name[i])){
200 p->s+= strlen(p->const_name[i]);
201 d->type = e_const;
202 d->a.const_index = i;
203 return d;
207 p->s= strchr(p->s, '(');
208 if(p->s==NULL){
209 *p->error = "undefined constant or missing (";
210 p->s= next;
211 ff_eval_free(d);
212 return NULL;
214 p->s++; // "("
215 if (*next == '(') { // special case do-nothing
216 av_freep(&d);
217 d = parse_expr(p);
218 if(p->s[0] != ')'){
219 *p->error = "missing )";
220 ff_eval_free(d);
221 return NULL;
223 p->s++; // ")"
224 return d;
226 d->param[0] = parse_expr(p);
227 if(p->s[0]== ','){
228 p->s++; // ","
229 d->param[1] = parse_expr(p);
231 if(p->s[0] != ')'){
232 *p->error = "missing )";
233 ff_eval_free(d);
234 return NULL;
236 p->s++; // ")"
238 d->type = e_func0;
239 if( strmatch(next, "sinh" ) ) d->a.func0 = sinh;
240 else if( strmatch(next, "cosh" ) ) d->a.func0 = cosh;
241 else if( strmatch(next, "tanh" ) ) d->a.func0 = tanh;
242 else if( strmatch(next, "sin" ) ) d->a.func0 = sin;
243 else if( strmatch(next, "cos" ) ) d->a.func0 = cos;
244 else if( strmatch(next, "tan" ) ) d->a.func0 = tan;
245 else if( strmatch(next, "atan" ) ) d->a.func0 = atan;
246 else if( strmatch(next, "asin" ) ) d->a.func0 = asin;
247 else if( strmatch(next, "acos" ) ) d->a.func0 = acos;
248 else if( strmatch(next, "exp" ) ) d->a.func0 = exp;
249 else if( strmatch(next, "log" ) ) d->a.func0 = log;
250 else if( strmatch(next, "abs" ) ) d->a.func0 = fabs;
251 else if( strmatch(next, "squish") ) d->type = e_squish;
252 else if( strmatch(next, "gauss" ) ) d->type = e_gauss;
253 else if( strmatch(next, "mod" ) ) d->type = e_mod;
254 else if( strmatch(next, "max" ) ) d->type = e_max;
255 else if( strmatch(next, "min" ) ) d->type = e_min;
256 else if( strmatch(next, "eq" ) ) d->type = e_eq;
257 else if( strmatch(next, "gte" ) ) d->type = e_gte;
258 else if( strmatch(next, "gt" ) ) d->type = e_gt;
259 else if( strmatch(next, "lte" ) ) { AVEvalExpr * tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gt; }
260 else if( strmatch(next, "lt" ) ) { AVEvalExpr * tmp = d->param[1]; d->param[1] = d->param[0]; d->param[0] = tmp; d->type = e_gte; }
261 else if( strmatch(next, "ld" ) ) d->type = e_ld;
262 else if( strmatch(next, "st" ) ) d->type = e_st;
263 else if( strmatch(next, "while" ) ) d->type = e_while;
264 else {
265 for(i=0; p->func1_name && p->func1_name[i]; i++){
266 if(strmatch(next, p->func1_name[i])){
267 d->a.func1 = p->func1[i];
268 d->type = e_func1;
269 return d;
273 for(i=0; p->func2_name && p->func2_name[i]; i++){
274 if(strmatch(next, p->func2_name[i])){
275 d->a.func2 = p->func2[i];
276 d->type = e_func2;
277 return d;
281 *p->error = "unknown function";
282 ff_eval_free(d);
283 return NULL;
286 return d;
289 static AVEvalExpr * new_eval_expr(int type, int value, AVEvalExpr *p0, AVEvalExpr *p1){
290 AVEvalExpr * e = av_mallocz(sizeof(AVEvalExpr));
291 e->type =type ;
292 e->value =value ;
293 e->param[0] =p0 ;
294 e->param[1] =p1 ;
295 return e;
298 static AVEvalExpr * parse_pow(Parser *p, int *sign){
299 *sign= (*p->s == '+') - (*p->s == '-');
300 p->s += *sign&1;
301 return parse_primary(p);
304 static AVEvalExpr * parse_factor(Parser *p){
305 int sign, sign2;
306 AVEvalExpr * e = parse_pow(p, &sign);
307 while(p->s[0]=='^'){
308 p->s++;
309 e= new_eval_expr(e_pow, 1, e, parse_pow(p, &sign2));
310 if (e->param[1]) e->param[1]->value *= (sign2|1);
312 if (e) e->value *= (sign|1);
313 return e;
316 static AVEvalExpr * parse_term(Parser *p){
317 AVEvalExpr * e = parse_factor(p);
318 while(p->s[0]=='*' || p->s[0]=='/'){
319 int c= *p->s++;
320 e= new_eval_expr(c == '*' ? e_mul : e_div, 1, e, parse_factor(p));
322 return e;
325 static AVEvalExpr * parse_subexpr(Parser *p) {
326 AVEvalExpr * e = parse_term(p);
327 while(*p->s == '+' || *p->s == '-') {
328 e= new_eval_expr(e_add, 1, e, parse_term(p));
331 return e;
334 static AVEvalExpr * parse_expr(Parser *p) {
335 AVEvalExpr * e;
337 if(p->stack_index <= 0) //protect against stack overflows
338 return NULL;
339 p->stack_index--;
341 e = parse_subexpr(p);
343 while(*p->s == ';') {
344 p->s++;
345 e= new_eval_expr(e_last, 1, e, parse_subexpr(p));
348 p->stack_index++;
350 return e;
353 static int verify_expr(AVEvalExpr * e) {
354 if (!e) return 0;
355 switch (e->type) {
356 case e_value:
357 case e_const: return 1;
358 case e_func0:
359 case e_func1:
360 case e_squish:
361 case e_ld:
362 case e_gauss: return verify_expr(e->param[0]);
363 default: return verify_expr(e->param[0]) && verify_expr(e->param[1]);
367 AVEvalExpr * ff_parse(const char *s, const char * const *const_name,
368 double (**func1)(void *, double), const char **func1_name,
369 double (**func2)(void *, double, double), const char **func2_name,
370 const char **error){
371 Parser p;
372 AVEvalExpr *e = NULL;
373 char *w = av_malloc(strlen(s) + 1);
374 char *wp = w;
376 if (!w)
377 goto end;
379 while (*s)
380 if (!isspace(*s++)) *wp++ = s[-1];
381 *wp++ = 0;
383 p.stack_index=100;
384 p.s= w;
385 p.const_name = const_name;
386 p.func1 = func1;
387 p.func1_name = func1_name;
388 p.func2 = func2;
389 p.func2_name = func2_name;
390 p.error= error;
392 e = parse_expr(&p);
393 if (!verify_expr(e)) {
394 ff_eval_free(e);
395 e = NULL;
397 end:
398 av_free(w);
399 return e;
402 double ff_parse_eval(AVEvalExpr * e, const double *const_value, void *opaque) {
403 Parser p;
405 p.const_value= const_value;
406 p.opaque = opaque;
407 return eval_expr(&p, e);
410 double ff_eval2(const char *s, const double *const_value, const char * const *const_name,
411 double (**func1)(void *, double), const char **func1_name,
412 double (**func2)(void *, double, double), const char **func2_name,
413 void *opaque, const char **error){
414 AVEvalExpr * e = ff_parse(s, const_name, func1, func1_name, func2, func2_name, error);
415 double d;
416 if (!e) return NAN;
417 d = ff_parse_eval(e, const_value, opaque);
418 ff_eval_free(e);
419 return d;
422 #ifdef TEST
423 #undef printf
424 static double const_values[]={
425 M_PI,
426 M_E,
429 static const char *const_names[]={
430 "PI",
431 "E",
434 int main(void){
435 int i;
436 printf("%f == 12.7\n", ff_eval2("1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", const_values, const_names, NULL, NULL, NULL, NULL, NULL, NULL));
437 printf("%f == 0.931322575\n", ff_eval2("80G/80Gi", const_values, const_names, NULL, NULL, NULL, NULL, NULL, NULL));
439 for(i=0; i<1050; i++){
440 START_TIMER
441 ff_eval2("1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)", const_values, const_names, NULL, NULL, NULL, NULL, NULL, NULL);
442 STOP_TIMER("ff_eval2")
444 return 0;
446 #endif