FFmpeg
eval.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /**
22  * @file
23  * simple arithmetic expression evaluator
24  */
25 
26 #ifndef AVUTIL_EVAL_H
27 #define AVUTIL_EVAL_H
28 
29 typedef struct AVExpr AVExpr;
30 
31 /**
32  * Parse and evaluate an expression.
33  * Note, this is significantly slower than av_expr_eval().
34  *
35  * @param res a pointer to a double where is put the result value of
36  * the expression, or NAN in case of error
37  * @param s expression as a zero terminated string, for example "1+2^3+5*5+sin(2/3)"
38  * @param const_names NULL terminated array of zero terminated strings of constant identifiers, for example {"PI", "E", 0}
39  * @param const_values a zero terminated array of values for the identifiers from const_names
40  * @param func1_names NULL terminated array of zero terminated strings of funcs1 identifiers
41  * @param funcs1 NULL terminated array of function pointers for functions which take 1 argument
42  * @param func2_names NULL terminated array of zero terminated strings of funcs2 identifiers
43  * @param funcs2 NULL terminated array of function pointers for functions which take 2 arguments
44  * @param opaque a pointer which will be passed to all functions from funcs1 and funcs2
45  * @param log_ctx parent logging context
46  * @return >= 0 in case of success, a negative value corresponding to an
47  * AVERROR code otherwise
48  */
49 int av_expr_parse_and_eval(double *res, const char *s,
50  const char * const *const_names, const double *const_values,
51  const char * const *func1_names, double (* const *funcs1)(void *, double),
52  const char * const *func2_names, double (* const *funcs2)(void *, double, double),
53  void *opaque, int log_offset, void *log_ctx);
54 
55 /**
56  * Parse an expression.
57  *
58  * @param expr a pointer where is put an AVExpr containing the parsed
59  * value in case of successful parsing, or NULL otherwise.
60  * The pointed to AVExpr must be freed with av_expr_free() by the user
61  * when it is not needed anymore.
62  * @param s expression as a zero terminated string, for example "1+2^3+5*5+sin(2/3)"
63  * @param const_names NULL terminated array of zero terminated strings of constant identifiers, for example {"PI", "E", 0}
64  * @param func1_names NULL terminated array of zero terminated strings of funcs1 identifiers
65  * @param funcs1 NULL terminated array of function pointers for functions which take 1 argument
66  * @param func2_names NULL terminated array of zero terminated strings of funcs2 identifiers
67  * @param funcs2 NULL terminated array of function pointers for functions which take 2 arguments
68  * @param log_ctx parent logging context
69  * @return >= 0 in case of success, a negative value corresponding to an
70  * AVERROR code otherwise
71  */
72 int av_expr_parse(AVExpr **expr, const char *s,
73  const char * const *const_names,
74  const char * const *func1_names, double (* const *funcs1)(void *, double),
75  const char * const *func2_names, double (* const *funcs2)(void *, double, double),
76  int log_offset, void *log_ctx);
77 
78 /**
79  * Evaluate a previously parsed expression.
80  *
81  * @param const_values a zero terminated array of values for the identifiers from av_expr_parse() const_names
82  * @param opaque a pointer which will be passed to all functions from funcs1 and funcs2
83  * @return the value of the expression
84  */
85 double av_expr_eval(AVExpr *e, const double *const_values, void *opaque);
86 
87 /**
88  * Track the presence of variables and their number of occurrences in a parsed expression
89  *
90  * @param counter a zero-initialized array where the count of each variable will be stored
91  * @param size size of array
92  * @return 0 on success, a negative value indicates that no expression or array was passed
93  * or size was zero
94  */
95 int av_expr_count_vars(AVExpr *e, unsigned *counter, int size);
96 
97 /**
98  * Track the presence of user provided functions and their number of occurrences
99  * in a parsed expression.
100  *
101  * @param counter a zero-initialized array where the count of each function will be stored
102  * if you passed 5 functions with 2 arguments to av_expr_parse()
103  * then for arg=2 this will use upto 5 entries.
104  * @param size size of array
105  * @param arg number of arguments the counted functions have
106  * @return 0 on success, a negative value indicates that no expression or array was passed
107  * or size was zero
108  */
109 int av_expr_count_func(AVExpr *e, unsigned *counter, int size, int arg);
110 
111 /**
112  * Free a parsed expression previously created with av_expr_parse().
113  */
114 void av_expr_free(AVExpr *e);
115 
116 /**
117  * Parse the string in numstr and return its value as a double. If
118  * the string is empty, contains only whitespaces, or does not contain
119  * an initial substring that has the expected syntax for a
120  * floating-point number, no conversion is performed. In this case,
121  * returns a value of zero and the value returned in tail is the value
122  * of numstr.
123  *
124  * @param numstr a string representing a number, may contain one of
125  * the International System number postfixes, for example 'K', 'M',
126  * 'G'. If 'i' is appended after the postfix, powers of 2 are used
127  * instead of powers of 10. The 'B' postfix multiplies the value by
128  * 8, and can be appended after another postfix or used alone. This
129  * allows using for example 'KB', 'MiB', 'G' and 'B' as postfix.
130  * @param tail if non-NULL puts here the pointer to the char next
131  * after the last parsed character
132  */
133 double av_strtod(const char *numstr, char **tail);
134 
135 #endif /* AVUTIL_EVAL_H */
av_expr_parse_and_eval
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.
Definition: eval.c:776
av_expr_eval
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
Evaluate a previously parsed expression.
Definition: eval.c:766
func2_names
static const char *const func2_names[]
Definition: af_afftfilt.c:98
const_values
static const double const_values[]
Definition: eval.c:28
func1_names
static const char *const func1_names[]
Definition: vf_rotate.c:188
funcs1
static double(*const funcs1[])(void *, double)
Definition: vf_lut.c:198
s
#define s(width, name)
Definition: cbs_vp9.c:256
AVExpr
Definition: eval.c:157
arg
const char * arg
Definition: jacosubdec.c:67
size
int size
Definition: twinvq_data.h:10344
av_expr_parse
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.
Definition: eval.c:685
av_expr_count_func
int av_expr_count_func(AVExpr *e, unsigned *counter, int size, int arg)
Track the presence of user provided functions and their number of occurrences in a parsed expression.
Definition: eval.c:761
const_names
static const char *const const_names[]
Definition: eval.c:34
av_expr_free
void av_expr_free(AVExpr *e)
Free a parsed expression previously created with av_expr_parse().
Definition: eval.c:336
av_expr_count_vars
int av_expr_count_vars(AVExpr *e, unsigned *counter, int size)
Track the presence of variables and their number of occurrences in a parsed expression.
Definition: eval.c:756
av_strtod
double av_strtod(const char *numstr, char **tail)
Parse the string in numstr and return its value as a double.
Definition: eval.c:106