]> git.sesse.net Git - ffmpeg/blob - doc/eval.texi
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / doc / eval.texi
1 @chapter Expression Evaluation
2 @c man begin EXPRESSION EVALUATION
3
4 When evaluating an arithmetic expression, FFmpeg uses an internal
5 formula evaluator, implemented through the @file{libavutil/eval.h}
6 interface.
7
8 An expression may contain unary, binary operators, constants, and
9 functions.
10
11 Two expressions @var{expr1} and @var{expr2} can be combined to form
12 another expression "@var{expr1};@var{expr2}".
13 @var{expr1} and @var{expr2} are evaluated in turn, and the new
14 expression evaluates to the value of @var{expr2}.
15
16 The following binary operators are available: @code{+}, @code{-},
17 @code{*}, @code{/}, @code{^}.
18
19 The following unary operators are available: @code{+}, @code{-}.
20
21 The following functions are available:
22 @table @option
23 @item sinh(x)
24 @item cosh(x)
25 @item tanh(x)
26 @item sin(x)
27 @item cos(x)
28 @item tan(x)
29 @item atan(x)
30 @item asin(x)
31 @item acos(x)
32 @item exp(x)
33 @item log(x)
34 @item abs(x)
35 @item squish(x)
36 @item gauss(x)
37 @item isnan(x)
38 Return 1.0 if @var{x} is NAN, 0.0 otherwise.
39
40 @item mod(x, y)
41 @item max(x, y)
42 @item min(x, y)
43 @item eq(x, y)
44 @item gte(x, y)
45 @item gt(x, y)
46 @item lte(x, y)
47 @item lt(x, y)
48 @item st(var, expr)
49 Allow to store the value of the expression @var{expr} in an internal
50 variable. @var{var} specifies the number of the variable where to
51 store the value, and it is a value ranging from 0 to 9. The function
52 returns the value stored in the internal variable.
53 Note, Variables are currently not shared between expressions.
54
55 @item ld(var)
56 Allow to load the value of the internal variable with number
57 @var{var}, which was previously stored with st(@var{var}, @var{expr}).
58 The function returns the loaded value.
59
60 @item while(cond, expr)
61 Evaluate expression @var{expr} while the expression @var{cond} is
62 non-zero, and returns the value of the last @var{expr} evaluation, or
63 NAN if @var{cond} was always false.
64
65 @item ceil(expr)
66 Round the value of expression @var{expr} upwards to the nearest
67 integer. For example, "ceil(1.5)" is "2.0".
68
69 @item floor(expr)
70 Round the value of expression @var{expr} downwards to the nearest
71 integer. For example, "floor(-1.5)" is "-2.0".
72
73 @item trunc(expr)
74 Round the value of expression @var{expr} towards zero to the nearest
75 integer. For example, "trunc(-1.5)" is "-1.0".
76
77 @item sqrt(expr)
78 Compute the square root of @var{expr}. This is equivalent to
79 "(@var{expr})^.5".
80
81 @item not(expr)
82 Return 1.0 if @var{expr} is zero, 0.0 otherwise.
83
84 @item pow(x, y)
85 Compute the power of @var{x} elevated @var{y}, it is equivalent to
86 "(@var{x})^(@var{y})".
87
88 @item random(x)
89 Return a pseudo random value between 0.0 and 1.0. @var{x} is the index of the
90 internal variable which will be used to save the seed/state.
91
92 @item hypot(x, y)
93 This function is similar to the C function with the same name; it returns
94 "sqrt(@var{x}*@var{x} + @var{y}*@var{y})", the length of the hypotenuse of a
95 right triangle with sides of length @var{x} and @var{y}, or the distance of the
96 point (@var{x}, @var{y}) from the origin.
97
98 @item gcd(x, y)
99 Return the greatest common divisor of @var{x} and @var{y}. If both @var{x} and
100 @var{y} are 0 or either or both are less than zero then behavior is undefined.
101
102 @item if(x, y)
103 Evaluate @var{x}, and if the result is non-zero return the result of
104 the evaluation of @var{y}, return 0 otherwise.
105
106 @item ifnot(x, y)
107 Evaluate @var{x}, and if the result is zero return the result of the
108 evaluation of @var{y}, return 0 otherwise.
109 @end table
110
111 The following constants are available:
112 @table @option
113 @item PI
114 area of the unit disc, approximately 3.14
115 @item E
116 exp(1) (Euler's number), approximately 2.718
117 @item PHI
118 golden ratio (1+sqrt(5))/2, approximately 1.618
119 @end table
120
121 Assuming that an expression is considered "true" if it has a non-zero
122 value, note that:
123
124 @code{*} works like AND
125
126 @code{+} works like OR
127
128 and the construct:
129 @example
130 if A then B else C
131 @end example
132 is equivalent to
133 @example
134 if(A,B) + ifnot(A,C)
135 @end example
136
137 In your C code, you can extend the list of unary and binary functions,
138 and define recognized constants, so that they are available for your
139 expressions.
140
141 The evaluator also recognizes the International System number
142 postfixes. If 'i' is appended after the postfix, powers of 2 are used
143 instead of powers of 10. The 'B' postfix multiplies the value for 8,
144 and can be appended after another postfix or used alone. This allows
145 using for example 'KB', 'MiB', 'G' and 'B' as postfix.
146
147 Follows the list of available International System postfixes, with
148 indication of the corresponding powers of 10 and of 2.
149 @table @option
150 @item y
151 -24 / -80
152 @item z
153 -21 / -70
154 @item a
155 -18 / -60
156 @item f
157 -15 / -50
158 @item p
159 -12 / -40
160 @item n
161 -9 / -30
162 @item u
163 -6 / -20
164 @item m
165 -3 / -10
166 @item c
167 -2
168 @item d
169 -1
170 @item h
171 2
172 @item k
173 3 / 10
174 @item K
175 3 / 10
176 @item M
177 6 / 20
178 @item G
179 9 / 30
180 @item T
181 12 / 40
182 @item P
183 15 / 40
184 @item E
185 18 / 50
186 @item Z
187 21 / 60
188 @item Y
189 24 / 70
190 @end table
191
192 @c man end