You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

304 lines
8.2KB

  1. @chapter Expression Evaluation
  2. @c man begin EXPRESSION EVALUATION
  3. When evaluating an arithmetic expression, FFmpeg uses an internal
  4. formula evaluator, implemented through the @file{libavutil/eval.h}
  5. interface.
  6. An expression may contain unary, binary operators, constants, and
  7. functions.
  8. Two expressions @var{expr1} and @var{expr2} can be combined to form
  9. another expression "@var{expr1};@var{expr2}".
  10. @var{expr1} and @var{expr2} are evaluated in turn, and the new
  11. expression evaluates to the value of @var{expr2}.
  12. The following binary operators are available: @code{+}, @code{-},
  13. @code{*}, @code{/}, @code{^}.
  14. The following unary operators are available: @code{+}, @code{-}.
  15. The following functions are available:
  16. @table @option
  17. @item abs(x)
  18. Compute absolute value of @var{x}.
  19. @item acos(x)
  20. Compute arccosine of @var{x}.
  21. @item asin(x)
  22. Compute arcsine of @var{x}.
  23. @item atan(x)
  24. Compute arctangent of @var{x}.
  25. @item between(x, min, max)
  26. Return 1 if @var{x} is greater than or equal to @var{min} and lesser than or
  27. equal to @var{max}, 0 otherwise.
  28. @item bitand(x, y)
  29. @item bitor(x, y)
  30. Compute bitwise and/or operation on @var{x} and @var{y}.
  31. The results of the evaluation of @var{x} and @var{y} are converted to
  32. integers before executing the bitwise operation.
  33. Note that both the conversion to integer and the conversion back to
  34. floating point can lose precision. Beware of unexpected results for
  35. large numbers (usually 2^53 and larger).
  36. @item ceil(expr)
  37. Round the value of expression @var{expr} upwards to the nearest
  38. integer. For example, "ceil(1.5)" is "2.0".
  39. @item cos(x)
  40. Compute cosine of @var{x}.
  41. @item cosh(x)
  42. Compute hyperbolic cosine of @var{x}.
  43. @item eq(x, y)
  44. Return 1 if @var{x} and @var{y} are equivalent, 0 otherwise.
  45. @item exp(x)
  46. Compute exponential of @var{x} (with base @code{e}, the Euler's number).
  47. @item floor(expr)
  48. Round the value of expression @var{expr} downwards to the nearest
  49. integer. For example, "floor(-1.5)" is "-2.0".
  50. @item gauss(x)
  51. Compute Gauss function of @var{x}, corresponding to
  52. @code{exp(-x*x/2) / sqrt(2*PI)}.
  53. @item gcd(x, y)
  54. Return the greatest common divisor of @var{x} and @var{y}. If both @var{x} and
  55. @var{y} are 0 or either or both are less than zero then behavior is undefined.
  56. @item gt(x, y)
  57. Return 1 if @var{x} is greater than @var{y}, 0 otherwise.
  58. @item gte(x, y)
  59. Return 1 if @var{x} is greater than or equal to @var{y}, 0 otherwise.
  60. @item hypot(x, y)
  61. This function is similar to the C function with the same name; it returns
  62. "sqrt(@var{x}*@var{x} + @var{y}*@var{y})", the length of the hypotenuse of a
  63. right triangle with sides of length @var{x} and @var{y}, or the distance of the
  64. point (@var{x}, @var{y}) from the origin.
  65. @item if(x, y)
  66. Evaluate @var{x}, and if the result is non-zero return the result of
  67. the evaluation of @var{y}, return 0 otherwise.
  68. @item if(x, y, z)
  69. Evaluate @var{x}, and if the result is non-zero return the evaluation
  70. result of @var{y}, otherwise the evaluation result of @var{z}.
  71. @item ifnot(x, y)
  72. Evaluate @var{x}, and if the result is zero return the result of the
  73. evaluation of @var{y}, return 0 otherwise.
  74. @item ifnot(x, y, z)
  75. Evaluate @var{x}, and if the result is zero return the evaluation
  76. result of @var{y}, otherwise the evaluation result of @var{z}.
  77. @item isinf(x)
  78. Return 1.0 if @var{x} is +/-INFINITY, 0.0 otherwise.
  79. @item isnan(x)
  80. Return 1.0 if @var{x} is NAN, 0.0 otherwise.
  81. @item ld(var)
  82. Allow to load the value of the internal variable with number
  83. @var{var}, which was previously stored with st(@var{var}, @var{expr}).
  84. The function returns the loaded value.
  85. @item log(x)
  86. Compute natural logarithm of @var{x}.
  87. @item lt(x, y)
  88. Return 1 if @var{x} is lesser than @var{y}, 0 otherwise.
  89. @item lte(x, y)
  90. Return 1 if @var{x} is lesser than or equal to @var{y}, 0 otherwise.
  91. @item max(x, y)
  92. Return the maximum between @var{x} and @var{y}.
  93. @item min(x, y)
  94. Return the maximum between @var{x} and @var{y}.
  95. @item mod(x, y)
  96. Compute the remainder of division of @var{x} by @var{y}.
  97. @item not(expr)
  98. Return 1.0 if @var{expr} is zero, 0.0 otherwise.
  99. @item pow(x, y)
  100. Compute the power of @var{x} elevated @var{y}, it is equivalent to
  101. "(@var{x})^(@var{y})".
  102. @item print(t)
  103. @item print(t, l)
  104. Print the value of expression @var{t} with loglevel @var{l}. If
  105. @var{l} is not specified then a default log level is used.
  106. Returns the value of the expression printed.
  107. Prints t with loglevel l
  108. @item random(x)
  109. Return a pseudo random value between 0.0 and 1.0. @var{x} is the index of the
  110. internal variable which will be used to save the seed/state.
  111. @item root(expr, max)
  112. Find an input value for which the function represented by @var{expr}
  113. with argument @var{ld(0)} is 0 in the interval 0..@var{max}.
  114. The expression in @var{expr} must denote a continuous function or the
  115. result is undefined.
  116. @var{ld(0)} is used to represent the function input value, which means
  117. that the given expression will be evaluated multiple times with
  118. various input values that the expression can access through
  119. @code{ld(0)}. When the expression evaluates to 0 then the
  120. corresponding input value will be returned.
  121. @item sin(x)
  122. Compute sine of @var{x}.
  123. @item sinh(x)
  124. Compute hyperbolic sine of @var{x}.
  125. @item sqrt(expr)
  126. Compute the square root of @var{expr}. This is equivalent to
  127. "(@var{expr})^.5".
  128. @item squish(x)
  129. Compute expression @code{1/(1 + exp(4*x))}.
  130. @item st(var, expr)
  131. Allow to store the value of the expression @var{expr} in an internal
  132. variable. @var{var} specifies the number of the variable where to
  133. store the value, and it is a value ranging from 0 to 9. The function
  134. returns the value stored in the internal variable.
  135. Note, Variables are currently not shared between expressions.
  136. @item tan(x)
  137. Compute tangent of @var{x}.
  138. @item tanh(x)
  139. Compute hyperbolic tangent of @var{x}.
  140. @item taylor(expr, x)
  141. @item taylor(expr, x, id)
  142. Evaluate a Taylor series at @var{x}, given an expression representing
  143. the @code{ld(id)}-th derivative of a function at 0.
  144. When the series does not converge the result is undefined.
  145. @var{ld(id)} is used to represent the derivative order in @var{expr},
  146. which means that the given expression will be evaluated multiple times
  147. with various input values that the expression can access through
  148. @code{ld(id)}. If @var{id} is not specified then 0 is assumed.
  149. Note, when you have the derivatives at y instead of 0,
  150. @code{taylor(expr, x-y)} can be used.
  151. @item time(0)
  152. Return the current (wallclock) time in seconds.
  153. @item trunc(expr)
  154. Round the value of expression @var{expr} towards zero to the nearest
  155. integer. For example, "trunc(-1.5)" is "-1.0".
  156. @item while(cond, expr)
  157. Evaluate expression @var{expr} while the expression @var{cond} is
  158. non-zero, and returns the value of the last @var{expr} evaluation, or
  159. NAN if @var{cond} was always false.
  160. @end table
  161. The following constants are available:
  162. @table @option
  163. @item PI
  164. area of the unit disc, approximately 3.14
  165. @item E
  166. exp(1) (Euler's number), approximately 2.718
  167. @item PHI
  168. golden ratio (1+sqrt(5))/2, approximately 1.618
  169. @end table
  170. Assuming that an expression is considered "true" if it has a non-zero
  171. value, note that:
  172. @code{*} works like AND
  173. @code{+} works like OR
  174. For example the construct:
  175. @example
  176. if (A AND B) then C
  177. @end example
  178. is equivalent to:
  179. @example
  180. if(A*B, C)
  181. @end example
  182. In your C code, you can extend the list of unary and binary functions,
  183. and define recognized constants, so that they are available for your
  184. expressions.
  185. The evaluator also recognizes the International System unit prefixes.
  186. If 'i' is appended after the prefix, binary prefixes are used, which
  187. are based on powers of 1024 instead of powers of 1000.
  188. The 'B' postfix multiplies the value by 8, and can be appended after a
  189. unit prefix or used alone. This allows using for example 'KB', 'MiB',
  190. 'G' and 'B' as number postfix.
  191. The list of available International System prefixes follows, with
  192. indication of the corresponding powers of 10 and of 2.
  193. @table @option
  194. @item y
  195. 10^-24 / 2^-80
  196. @item z
  197. 10^-21 / 2^-70
  198. @item a
  199. 10^-18 / 2^-60
  200. @item f
  201. 10^-15 / 2^-50
  202. @item p
  203. 10^-12 / 2^-40
  204. @item n
  205. 10^-9 / 2^-30
  206. @item u
  207. 10^-6 / 2^-20
  208. @item m
  209. 10^-3 / 2^-10
  210. @item c
  211. 10^-2
  212. @item d
  213. 10^-1
  214. @item h
  215. 10^2
  216. @item k
  217. 10^3 / 2^10
  218. @item K
  219. 10^3 / 2^10
  220. @item M
  221. 10^6 / 2^20
  222. @item G
  223. 10^9 / 2^30
  224. @item T
  225. 10^12 / 2^40
  226. @item P
  227. 10^15 / 2^40
  228. @item E
  229. 10^18 / 2^50
  230. @item Z
  231. 10^21 / 2^60
  232. @item Y
  233. 10^24 / 2^70
  234. @end table
  235. @c man end