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 /**
23 * @file
24 * simple arithmetic expression evaluator.
25 *
26 * see http://joe.hotchkiss.com/programming/eval/eval.html
27 */
28
42
59
65 .log_level_offset_offset = offsetof(
Parser, log_offset),
66 .parent_log_context_offset = offsetof(
Parser, log_ctx),
67 };
68
69 static const struct {
74 ['y'-'E']= { 8.271806125530276749e-25, 1e-24, -24 },
75 ['z'-'E']= { 8.4703294725430034e-22, 1e-21, -21 },
76 ['a'-'E']= { 8.6736173798840355e-19, 1e-18, -18 },
77 ['f'-'E']= { 8.8817841970012523e-16, 1e-15, -15 },
78 ['p'-'E']= { 9.0949470177292824e-13, 1e-12, -12 },
79 ['n'-'E']= { 9.3132257461547852e-10, 1e-9, -9 },
80 ['u'-'E']= { 9.5367431640625e-7, 1e-6, -6 },
81 ['m'-'E']= { 9.765625e-4, 1e-3, -3 },
82 ['c'-'E']= { 9.8431332023036951e-3, 1e-2, -2 },
83 ['d'-'E']= { 9.921256574801246e-2, 1e-1, -1 },
84 ['h'-'E']= { 1.0159366732596479e2, 1e2, 2 },
85 ['k'-'E']= { 1.024e3, 1e3, 3 },
86 ['K'-'E']= { 1.024e3, 1e3, 3 },
87 ['M'-'E']= { 1.048576e6, 1e6, 6 },
88 ['G'-'E']= { 1.073741824e9, 1e9, 9 },
89 ['T'-'E']= { 1.099511627776e12, 1e12, 12 },
90 ['P'-'E']= { 1.125899906842624e15, 1e15, 15 },
91 ['E'-'E']= { 1.152921504606847e18, 1e18, 18 },
92 ['Z'-'E']= { 1.1805916207174113e21, 1e21, 21 },
93 ['Y'-'E']= { 1.2089258196146292e24, 1e24, 24 },
94 };
95
96 static const struct {
104 };
105
107 {
109 char *next;
110 if(numstr[0]=='0' && (numstr[1]|0x20)=='x') {
111 d = strtoul(numstr, &next, 16);
112 } else
114 /* if parsing succeeded, check for and interpret postfixes */
115 if (next!=numstr) {
116 if (next[0] == 'd' && next[1] == 'B') {
117 /* treat dB as decibels instead of decibytes */
119 next += 2;
120 } else if (*next >= 'E' && *next <= 'z') {
122 if (e) {
123 if (next[1] == 'i') {
125 next+=2;
126 } else {
128 next++;
129 }
130 }
131 }
132
133 if (*next=='B') {
135 next++;
136 }
137 }
138 /* if requested, fill in tail with the position after the last parsed
139 character */
140 if (tail)
141 *tail = next;
143 }
144
145 #define IS_IDENTIFIER_CHAR(c) ((c) - '0' <= 9U || (c) - 'a' <= 25U || (c) - 'A' <= 25U || (c) == '_')
146
148 {
150 for (
i=0; prefix[
i];
i++) {
151 if (prefix[
i] !=
s[
i])
return 0;
152 }
153 /* return 1 only if the s identifier is terminated */
155 }
156
158 enum {
168 double value;
// is sign in other types
170 union {
177 };
178
180 {
182 }
183
185 {
214 }
219 }
224 return v0 + (v1 -
v0) *
f;
225 }
230 return x;
231 }
235 r=
r*1664525+1013904223;
237 return e->
value * (
r * (1.0/UINT64_MAX));
238 }
244 }
246 double t = 1,
d = 0, v;
250 double var0 = p->
var[
id];
251 for(
i=0;
i<1000;
i++) {
257 break;
259 }
262 }
265 double low = -1, high = -1, v, low_v = -DBL_MAX, high_v = DBL_MAX;
266 double var0 = p->
var[0];
268 for(
i=-1;
i<1024;
i++) {
271 } else {
272 p->
var[0] = x_max*pow(0.9,
i-255);
273 if (
i&1) p->
var[0] *= -1;
274 if (
i&2) p->
var[0] += low;
275 else p->
var[0] += high;
276 }
278 if (v<=0 && v>low_v) {
280 low_v = v;
281 }
282 if (v>=0 && v<high_v) {
284 high_v = v;
285 }
286 if (low>=0 && high>=0){
287 for (j=0; j<1000; j++) {
288 p->
var[0] = (low+high)*0.5;
289 if (low == p->
var[0] || high == p->
var[0])
290 break;
292 if (v<=0) low = p->
var[0];
293 if (v>=0) high= p->
var[0];
295 low = high = v;
296 break;
297 }
298 }
299 break;
300 }
301 }
303 return -low_v<high_v ? low : high;
304 }
305 default: {
313 case e_eq:
return e->
value * (
d == d2 ? 1.0 : 0.0);
314 case e_gt:
return e->
value * (
d > d2 ? 1.0 : 0.0);
315 case e_gte:
return e->
value * (
d >= d2 ? 1.0 : 0.0);
316 case e_lt:
return e->
value * (
d < d2 ? 1.0 : 0.0);
317 case e_lte:
return e->
value * (
d <= d2 ? 1.0 : 0.0);
328 }
329 }
330 }
332 }
333
335
337 {
338 if (!e) return;
344 }
345
347 {
349 char *next = p->
s, *
s0 = p->
s;
351
354
355 /* number */
361 return 0;
362 }
364
365 /* named constants */
372 return 0;
373 }
374 }
381 return 0;
382 }
383 }
384
385 p->
s= strchr(p->
s,
'(');
391 }
393 if (*next == '(') { // special case do-nothing
397 if (p->
s[0] !=
')') {
401 }
404 return 0;
405 }
409 }
413 }
417 }
418 if (p->
s[0] !=
')') {
422 }
424
426 if (
strmatch(next,
"sinh" ))
d->a.func0 = sinh;
427 else if (
strmatch(next,
"cosh" ))
d->a.func0 = cosh;
428 else if (
strmatch(next,
"tanh" ))
d->a.func0 = tanh;
429 else if (
strmatch(next,
"sin" ))
d->a.func0 = sin;
430 else if (
strmatch(next,
"cos" ))
d->a.func0 = cos;
431 else if (
strmatch(next,
"tan" ))
d->a.func0 = tan;
432 else if (
strmatch(next,
"atan" ))
d->a.func0 = atan;
433 else if (
strmatch(next,
"asin" ))
d->a.func0 = asin;
434 else if (
strmatch(next,
"acos" ))
d->a.func0 = acos;
436 else if (
strmatch(next,
"log" ))
d->a.func0 = log;
476 else {
483 return 0;
484 }
485 }
486
493 return 0;
494 }
495 }
496
500 }
501
503 return 0;
504 }
505
507 {
509 if (!e)
515 return e;
516 }
517
519 {
520 *sign= (*p->
s ==
'+') - (*p->
s ==
'-');
523 }
524
526 {
527 /* do not filter out the negative sign when parsing a dB value.
528 for example, -3dB is not the same as -(3dB) */
530 char *next;
532 if (next != p->
s && next[0] ==
'd' && next[1] ==
'B') {
533 *sign = 0;
535 }
536 }
538 }
539
541 {
542 int sign, sign2,
ret;
547 e1 = e0;
552 }
554 if (!e0) {
558 }
560 }
561 if (e0) e0->
value *= (sign|1);
562
563 *e = e0;
564 return 0;
565 }
566
568 {
573 while (p->
s[0]==
'*' || p->
s[0]==
'/') {
575 e1 = e0;
579 }
581 if (!e0) {
585 }
586 }
587 *e = e0;
588 return 0;
589 }
590
592 {
597 while (*p->
s ==
'+' || *p->
s ==
'-') {
598 e1 = e0;
602 }
604 if (!e0) {
608 }
609 };
610
611 *e = e0;
612 return 0;
613 }
614
616 {
619 if (p->
stack_index <= 0)
//protect against stack overflows
622
625 while (*p->
s ==
';') {
627 e1 = e0;
631 }
633 if (!e0) {
637 }
638 };
639
641 *e = e0;
642 return 0;
643 }
644
646 {
647 if (!e) return 0;
682 }
683 }
684
688 const char *
const *
func2_names,
double (*
const *funcs2)(
void *,
double,
double),
689 int log_offset, void *log_ctx)
690 {
697
700
703 *wp++ = 0;
704
715
717 goto end;
721 goto end;
722 }
725 goto end;
726 }
730 goto end;
731 }
732 *expr = e;
734 end:
738 }
739
741 {
743
744 if (!e || !counter || !
size)
746
749
752
753 return 0;
754 }
755
757 {
759 }
760
762 {
764 }
765
767 {
770
774 }
775
779 const char *
const *
func2_names,
double (*
const *funcs2)(
void *,
double,
double),
780 void *opaque, int log_offset, void *log_ctx)
781 {
784
788 }
792 }