45 double (*
const *
funcs1)(
void *,
double a);
47 double (*
const *
funcs2)(
void *,
double a,
double b);
94 if(numstr[0]==
'0' && (numstr[1]|0x20)==
'x') {
95 d = strtoul(numstr, &next, 16);
100 if (next[0] ==
'd' && next[1] ==
'B') {
104 }
else if (*next >=
'E' && *next <=
'z') {
107 if (next[1] ==
'i') {
129 #define IS_IDENTIFIER_CHAR(c) ((c) - '0' <= 9U || (c) - 'a' <= 25U || (c) - 'A' <= 25U || (c) == '_')
134 for (i=0; prefix[i]; i++) {
135 if (prefix[i] != s[i])
return 0;
156 double (*
func2)(
void *, double, double);
197 av_log(p, level,
"%f\n", x);
203 r= r*1664525+1013904223;
205 return e->
value * (r * (1.0/UINT64_MAX));
214 double t = 1, d = 0,
v;
218 double var0 = p->
var[
id];
219 for(i=0; i<1000; i++) {
233 double low = -1, high = -1,
v, low_v = -DBL_MAX, high_v = DBL_MAX;
234 double var0 = p->
var[0];
236 for(i=-1; i<1024; i++) {
240 p->
var[0] = x_max*pow(0.9, i-255);
241 if (i&1) p->
var[0] *= -1;
242 if (i&2) p->
var[0] += low;
243 else p->
var[0] += high;
246 if (v<=0 && v>low_v) {
250 if (
v>=0 &&
v<high_v) {
254 if (low>=0 && high>=0){
255 for (j=0; j<1000; j++) {
256 p->
var[0] = (low+high)*0.5;
257 if (low == p->
var[0] || high == p->
var[0])
260 if (
v<=0) low = p->
var[0];
261 if (
v>=0) high= p->
var[0];
271 return -low_v<high_v ? low : high;
279 case e_max:
return e->
value * (d > d2 ? d : d2);
280 case e_min:
return e->
value * (d < d2 ? d : d2);
281 case e_eq:
return e->
value * (d == d2 ? 1.0 : 0.0);
282 case e_gt:
return e->
value * (d > d2 ? 1.0 : 0.0);
283 case e_gte:
return e->
value * (d >= d2 ? 1.0 : 0.0);
284 case e_lt:
return e->
value * (d < d2 ? 1.0 : 0.0);
285 case e_lte:
return e->
value * (d <= d2 ? 1.0 : 0.0);
316 char *next = p->
s, *
s0 = p->
s;
352 p->
s= strchr(p->
s,
'(');
364 if (p->
s[0] !=
')') {
385 if (p->
s[0] !=
')') {
480 *sign= (*p->
s ==
'+') - (*p->
s ==
'-');
492 if (next != p->
s && next[0] ==
'd' && next[1] ==
'B') {
502 int sign, sign2,
ret;
504 if ((ret =
parse_dB(&e0, p, &sign)) < 0)
509 if ((ret =
parse_dB(&e2, p, &sign2)) < 0) {
521 if (e0) e0->
value *= (sign|1);
533 while (p->
s[0]==
'*' || p->
s[0]==
'/') {
557 while (*p->
s ==
'+' || *p->
s ==
'-') {
585 while (*p->
s ==
';') {
644 const char *
const *func2_names,
double (*
const *funcs2)(
void *,
double,
double),
645 int log_offset,
void *log_ctx)
703 const char *
const *const_names,
const double *
const_values,
704 const char *
const *func1_names,
double (*
const *
funcs1)(
void *,
double),
705 const char *
const *func2_names,
double (*
const *funcs2)(
void *,
double,
double),
706 void *opaque,
int log_offset,
void *log_ctx)
709 int ret =
av_expr_parse(&e, s, const_names, func1_names,
funcs1, func2_names, funcs2, log_offset, log_ctx);
723 static const double const_values[] = {
729 static const char *
const const_names[] = {
735 int main(
int argc,
char **argv)
739 const char *
const *expr;
740 static const char *
const exprs[] = {
746 "1+(5-2)^(3-1)+1/2+sin(PI)-max(-2.2,-3.1)",
767 "bar + PI + E + 100f*2 + foo",
768 "13k + 12f - foo(1, 2)",
782 "st(0, 1); while(lte(ld(0), 100), st(1, ld(1)+ld(0));st(0, ld(0)+1)); ld(1)",
784 "st(1, 1); st(2, 2); st(0, 1); while(lte(ld(0),10), st(3, ld(1)+ld(2)); st(1, ld(2)); st(2, ld(3)); st(0, ld(0)+1)); ld(3)",
786 "st(0, 1); while(lte(ld(0),100), st(1, ld(1)+ld(0)); st(0, ld(0)+1))",
815 "ifnot(1, NaN) + if(0, 1)",
819 "taylor(eq(mod(ld(1),4),1)-eq(mod(ld(1),4),3), PI/2, 1)",
820 "root(sin(ld(0))-1, 2)",
821 "root(sin(ld(0))+6+sin(ld(0)/12)-log(ld(0)), 100)",
822 "7000000B*random(0)",
826 "gcd(30,55)*print(min(9,1))",
830 "between(10, -3, 10)",
831 "between(-4, -2, -1)",
836 for (expr = exprs; *expr; expr++) {
837 printf(
"Evaluating '%s'\n", *expr);
839 const_names, const_values,
840 NULL, NULL, NULL, NULL, NULL, 0, NULL);
842 printf(
"'%s' -> nan\n\n", *expr);
844 printf(
"'%s' -> %f\n\n", *expr, d);
848 const_names, const_values,
849 NULL, NULL, NULL, NULL, NULL, 0, NULL);
850 printf(
"%f == 12.7\n", d);
852 const_names, const_values,
853 NULL, NULL, NULL, NULL, NULL, 0, NULL);
854 printf(
"%f == 0.931322575\n", d);
856 if (argc > 1 && !strcmp(argv[1],
"-t")) {
857 for (i = 0; i < 1050; i++) {
860 const_names, const_values,
861 NULL, NULL, NULL, NULL, NULL, 0, NULL);
static int verify_expr(AVExpr *e)
const char *const * func1_names
static const char *const func1_names[]
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
Evaluate a previously parsed expression.
#define LIBAVUTIL_VERSION_INT
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
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.
void av_expr_free(AVExpr *e)
Free a parsed expression previously created with av_expr_parse().
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
static int parse_dB(AVExpr **e, Parser *p, int *sign)
const char * av_default_item_name(void *ctx)
Return the context name.
static av_always_inline av_const int isnan(float x)
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
static const double const_values[]
double(* func1)(void *, double)
static int parse_pow(AVExpr **e, Parser *p, int *sign)
static int parse_factor(AVExpr **e, Parser *p)
double av_strtod(const char *numstr, char **tail)
Parse the string in numstr and return its value as a double.
static int parse_subexpr(AVExpr **e, Parser *p)
double(* func2)(void *, double, double)
const double * const_values
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
#define IS_IDENTIFIER_CHAR(c)
static int parse_term(AVExpr **e, Parser *p)
int64_t av_const av_gcd(int64_t a, int64_t b)
Return the greatest common divisor of a and b.
int av_isspace(int c)
Locale-independent conversion of ASCII isspace.
#define av_unused
Disable warnings about deprecated features This is useful for sections of code kept for backward comp...
static const char *const const_names[]
#define FF_ARRAY_ELEMS(a)
static double etime(double v)
int64_t av_gettime(void)
Get the current time in microseconds.
static const int8_t si_prefixes['z'- 'E'+1]
static av_always_inline av_const double trunc(double x)
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
static int strmatch(const char *s, const char *prefix)
static int parse_primary(AVExpr **e, Parser *p)
int av_expr_parse_and_eval(double *res, 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.
static struct @164 constants[]
#define AV_LOG_INFO
Standard information.
static double eval_expr(Parser *p, AVExpr *e)
simple arithmetic expression evaluator
Describe the class of an AVClass context structure.
static const AVClass eval_class
double(*const funcs1)(void *, double a)
const char *const * func2_names
attribute_deprecated const uint8_t av_reverse[256]
Reverse the order of the bits of an 8-bits unsigned integer.
Macro definitions for various function/variable attributes.
double(*const funcs2)(void *, double a, double b)
static AVExpr * make_eval_expr(int type, int value, AVExpr *p0, AVExpr *p1)
static av_always_inline av_const int isinf(float x)
int main(int argc, char **argv)
common internal and external API header
void * av_mallocz(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...