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
49 double (*
const *
funcs1)(
void *,
double a);
// NULL terminated
51 double (*
const *
funcs2)(
void *,
double a,
double b);
// NULL terminated
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 {
108 double d;
109 char *next;
110 if(numstr[0]=='0' && (numstr[1]|0x20)=='x') {
111 d = strtoul(numstr, &next, 16);
112 } else
113 d =
strtod(numstr, &next);
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') {
134 d*=8;
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;
142 return d;
143 }
144
145 #define IS_IDENTIFIER_CHAR(c) ((c) - '0' <= 9U || (c) - 'a' <= 25U || (c) - 'A' <= 25U || (c) == '_')
146
148 {
149 int i;
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 {
167 double value;
// is sign in other types
168 union {
172 double (*
func2)(
void *, double, double);
176 };
177
179 {
181 }
182
184 {
212 }
217 }
222 return v0 + (v1 -
v0) * f;
223 }
227 av_log(p, level,
"%f\n", x);
228 return x;
229 }
233 r= r*1664525+1013904223;
235 return e->
value * (r * (1.0/UINT64_MAX));
236 }
241 return d;
242 }
244 double t = 1, d = 0, v;
247 int i;
248 double var0 = p->
var[
id];
249 for(i=0; i<1000; i++) {
250 double ld = d;
253 d += t*v;
254 if(ld==d && v)
255 break;
256 t *= x / (i+1);
257 }
259 return d;
260 }
262 int i, j;
263 double low = -1, high = -1, v, low_v = -DBL_MAX, high_v = DBL_MAX;
264 double var0 = p->
var[0];
266 for(i=-1; i<1024; i++) {
267 if(i<255) {
269 } else {
270 p->
var[0] = x_max*pow(0.9, i-255);
271 if (i&1) p->
var[0] *= -1;
272 if (i&2) p->
var[0] += low;
273 else p->
var[0] += high;
274 }
276 if (v<=0 && v>low_v) {
278 low_v = v;
279 }
280 if (v>=0 && v<high_v) {
282 high_v = v;
283 }
284 if (low>=0 && high>=0){
285 for (j=0; j<1000; j++) {
286 p->
var[0] = (low+high)*0.5;
287 if (low == p->
var[0] || high == p->
var[0])
288 break;
290 if (v<=0) low = p->
var[0];
291 if (v>=0) high= p->
var[0];
293 low = high = v;
294 break;
295 }
296 }
297 break;
298 }
299 }
301 return -low_v<high_v ? low : high;
302 }
303 default: {
307 case e_mod:
return e->
value * (d - floor((!CONFIG_FTRAPV || d2) ? d / d2 : d *
INFINITY) * d2);
309 case e_max:
return e->
value * (d > d2 ? d : d2);
310 case e_min:
return e->
value * (d < d2 ? d : d2);
311 case e_eq:
return e->
value * (d == d2 ? 1.0 : 0.0);
312 case e_gt:
return e->
value * (d > d2 ? 1.0 : 0.0);
313 case e_gte:
return e->
value * (d >= d2 ? 1.0 : 0.0);
314 case e_lt:
return e->
value * (d < d2 ? 1.0 : 0.0);
315 case e_lte:
return e->
value * (d <= d2 ? 1.0 : 0.0);
326 }
327 }
328 }
330 }
331
333
335 {
336 if (!e) return;
342 }
343
345 {
347 char *next = p->
s, *
s0 = p->
s;
348 int ret, i;
349
350 if (!d)
352
353 /* number */
358 *e = d;
359 return 0;
360 }
362
363 /* named constants */
369 *e = d;
370 return 0;
371 }
372 }
378 *e = d;
379 return 0;
380 }
381 }
382
383 p->
s= strchr(p->
s,
'(');
389 }
391 if (*next == '(') { // special case do-nothing
394 return ret;
395 if (p->
s[0] !=
')') {
399 }
401 *e = d;
402 return 0;
403 }
406 return ret;
407 }
411 }
415 }
416 if (p->
s[0] !=
')') {
420 }
422
473 else {
478 *e = d;
479 return 0;
480 }
481 }
482
487 *e = d;
488 return 0;
489 }
490 }
491
495 }
496
497 *e = d;
498 return 0;
499 }
500
502 {
504 if (!e)
510 return e;
511 }
512
514 {
515 *sign= (*p->
s ==
'+') - (*p->
s ==
'-');
518 }
519
521 {
522 /* do not filter out the negative sign when parsing a dB value.
523 for example, -3dB is not the same as -(3dB) */
525 char *next;
527 if (next != p->
s && next[0] ==
'd' && next[1] ==
'B') {
528 *sign = 0;
530 }
531 }
533 }
534
536 {
537 int sign, sign2, ret;
539 if ((ret =
parse_dB(&e0, p, &sign)) < 0)
540 return ret;
542 e1 = e0;
544 if ((ret =
parse_dB(&e2, p, &sign2)) < 0) {
546 return ret;
547 }
549 if (!e0) {
553 }
555 }
556 if (e0) e0->
value *= (sign|1);
557
558 *e = e0;
559 return 0;
560 }
561
563 {
564 int ret;
567 return ret;
568 while (p->
s[0]==
'*' || p->
s[0]==
'/') {
570 e1 = e0;
573 return ret;
574 }
576 if (!e0) {
580 }
581 }
582 *e = e0;
583 return 0;
584 }
585
587 {
588 int ret;
591 return ret;
592 while (*p->
s ==
'+' || *p->
s ==
'-') {
593 e1 = e0;
596 return ret;
597 }
599 if (!e0) {
603 }
604 };
605
606 *e = e0;
607 return 0;
608 }
609
611 {
612 int ret;
614 if (p->
stack_index <= 0)
//protect against stack overflows
617
619 return ret;
620 while (*p->
s ==
';') {
622 e1 = e0;
625 return ret;
626 }
628 if (!e0) {
632 }
633 };
634
636 *e = e0;
637 return 0;
638 }
639
641 {
642 if (!e) return 0;
676 }
677 }
678
682 const char * const *func2_names, double (* const *funcs2)(void *, double, double),
683 int log_offset, void *log_ctx)
684 {
690 int ret = 0;
691
692 if (!w)
694
695 while (*s)
697 *wp++ = 0;
698
709
716 }
720 }
725 }
726 *expr = e;
731 return ret;
732 }
733
735 {
738
742 }
743
745 const char *
const *const_names,
const double *
const_values,
746 const char *
const *func1_names,
double (*
const *
funcs1)(
void *,
double),
747 const char * const *func2_names, double (* const *funcs2)(void *, double, double),
748 void *opaque, int log_offset, void *log_ctx)
749 {
751 int ret =
av_expr_parse(&e, s, const_names, func1_names,
funcs1, func2_names, funcs2, log_offset, log_ctx);
752
753 if (ret < 0) {
755 return ret;
756 }
760 }
static int verify_expr(AVExpr *e)
const char *const * func1_names
static const char *const func1_names[]
#define LIBAVUTIL_VERSION_INT
static const struct @243 si_prefixes['z'- 'E'+1]
const uint8_t ff_reverse[256]
const char * av_default_item_name(void *ptr)
Return the context name.
static av_const int av_isspace(int c)
Locale-independent conversion of ASCII isspace.
Convenience header that includes libavutil's core.
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
int av_expr_parse(AVExpr **expr, const char *s, const char *const *const_names, const char *const *func1_names, double(*const *funcs1)(void *, double), const char *const *func2_names, double(*const *funcs2)(void *, double, double), int log_offset, void *log_ctx)
Parse an expression.
Macro definitions for various function/variable attributes.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
static int parse_dB(AVExpr **e, Parser *p, int *sign)
static av_cold int end(AVCodecContext *avctx)
static int parse_expr(AVExpr **e, Parser *p)
static double(*const funcs1[])(void *, double)
double strtod(const char *, char **)
const char *const * const_names
double(* func1)(void *, double)
static int parse_pow(AVExpr **e, Parser *p, int *sign)
static int parse_factor(AVExpr **e, Parser *p)
high precision timer, useful to profile code
static const double const_values[]
static int parse_subexpr(AVExpr **e, Parser *p)
double(* func2)(void *, double, double)
int av_expr_parse_and_eval(double *d, const char *s, const char *const *const_names, const double *const_values, const char *const *func1_names, double(*const *funcs1)(void *, double), const char *const *func2_names, double(*const *funcs2)(void *, double, double), void *opaque, int log_offset, void *log_ctx)
Parse and evaluate an expression.
const double * const_values
static av_always_inline double ff_exp10(double x)
Compute 10^x for floating point values.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define IS_IDENTIFIER_CHAR(c)
static int parse_term(AVExpr **e, Parser *p)
static const struct @244 constants[]
int64_t av_gcd(int64_t a, int64_t b)
Compute the greatest common divisor of two integer operands.
static av_always_inline av_const double round(double x)
common internal API header
static av_const double hypot(double x, double y)
static double etime(double v)
GLsizei GLboolean const GLfloat * value
static av_always_inline av_const double trunc(double x)
static int strmatch(const char *s, const char *prefix)
static int parse_primary(AVExpr **e, Parser *p)
#define FF_ARRAY_ELEMS(a)
int64_t av_gettime(void)
Get the current time in microseconds.
double av_strtod(const char *numstr, char **tail)
Parse the string in numstr and return its value as a double.
#define AV_LOG_INFO
Standard information.
static double eval_expr(Parser *p, AVExpr *e)
void av_expr_free(AVExpr *e)
Free a parsed expression previously created with av_expr_parse().
Describe the class of an AVClass context structure.
static const AVClass eval_class
double(*const funcs1)(void *, double a)
const char *const * func2_names
internal math functions header
common internal and external API header
double(*const funcs2)(void *, double a, double b)
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
Evaluate a previously parsed expression.
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
static AVExpr * make_eval_expr(int type, int value, AVExpr *p0, AVExpr *p1)
static const char *const const_names[]
simple arithmetic expression evaluator