eval.texi 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 sinh(x)
  18. Compute hyperbolic sine of @var{x}.
  19. @item cosh(x)
  20. Compute hyperbolic cosine of @var{x}.
  21. @item tanh(x)
  22. Compute hyperbolic tangent of @var{x}.
  23. @item sin(x)
  24. Compute sine of @var{x}.
  25. @item cos(x)
  26. Compute cosine of @var{x}.
  27. @item tan(x)
  28. Compute tangent of @var{x}.
  29. @item atan(x)
  30. Compute arctangent of @var{x}.
  31. @item asin(x)
  32. Compute arcsine of @var{x}.
  33. @item acos(x)
  34. Compute arccosine of @var{x}.
  35. @item exp(x)
  36. Compute exponential of @var{x} (with base @code{e}, the Euler's number).
  37. @item log(x)
  38. Compute natural logarithm of @var{x}.
  39. @item abs(x)
  40. Compute absolute value of @var{x}.
  41. @item squish(x)
  42. Compute expression @code{1/(1 + exp(4*x))}.
  43. @item gauss(x)
  44. Compute Gauss function of @var{x}, corresponding to
  45. @code{exp(-x*x/2) / sqrt(2*PI)}.
  46. @item isinf(x)
  47. Return 1.0 if @var{x} is +/-INFINITY, 0.0 otherwise.
  48. @item isnan(x)
  49. Return 1.0 if @var{x} is NAN, 0.0 otherwise.
  50. @item mod(x, y)
  51. Compute the remainder of division of @var{x} by @var{y}.
  52. @item max(x, y)
  53. Return the maximum between @var{x} and @var{y}.
  54. @item min(x, y)
  55. Return the maximum between @var{x} and @var{y}.
  56. @item eq(x, y)
  57. Return 1 if @var{x} and @var{y} are equivalent, 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 gt(x, y)
  61. Return 1 if @var{x} is greater than @var{y}, 0 otherwise.
  62. @item lte(x, y)
  63. Return 1 if @var{x} is lesser than or equal to @var{y}, 0 otherwise.
  64. @item lt(x, y)
  65. Return 1 if @var{x} is lesser than @var{y}, 0 otherwise.
  66. @item st(var, expr)
  67. Allow to store the value of the expression @var{expr} in an internal
  68. variable. @var{var} specifies the number of the variable where to
  69. store the value, and it is a value ranging from 0 to 9. The function
  70. returns the value stored in the internal variable.
  71. Note, Variables are currently not shared between expressions.
  72. @item ld(var)
  73. Allow to load the value of the internal variable with number
  74. @var{var}, which was previously stored with st(@var{var}, @var{expr}).
  75. The function returns the loaded value.
  76. @item while(cond, expr)
  77. Evaluate expression @var{expr} while the expression @var{cond} is
  78. non-zero, and returns the value of the last @var{expr} evaluation, or
  79. NAN if @var{cond} was always false.
  80. @item ceil(expr)
  81. Round the value of expression @var{expr} upwards to the nearest
  82. integer. For example, "ceil(1.5)" is "2.0".
  83. @item floor(expr)
  84. Round the value of expression @var{expr} downwards to the nearest
  85. integer. For example, "floor(-1.5)" is "-2.0".
  86. @item trunc(expr)
  87. Round the value of expression @var{expr} towards zero to the nearest
  88. integer. For example, "trunc(-1.5)" is "-1.0".
  89. @item sqrt(expr)
  90. Compute the square root of @var{expr}. This is equivalent to
  91. "(@var{expr})^.5".
  92. @item not(expr)
  93. Return 1.0 if @var{expr} is zero, 0.0 otherwise.
  94. @item pow(x, y)
  95. Compute the power of @var{x} elevated @var{y}, it is equivalent to
  96. "(@var{x})^(@var{y})".
  97. @item random(x)
  98. Return a pseudo random value between 0.0 and 1.0. @var{x} is the index of the
  99. internal variable which will be used to save the seed/state.
  100. @item hypot(x, y)
  101. This function is similar to the C function with the same name; it returns
  102. "sqrt(@var{x}*@var{x} + @var{y}*@var{y})", the length of the hypotenuse of a
  103. right triangle with sides of length @var{x} and @var{y}, or the distance of the
  104. point (@var{x}, @var{y}) from the origin.
  105. @item gcd(x, y)
  106. Return the greatest common divisor of @var{x} and @var{y}. If both @var{x} and
  107. @var{y} are 0 or either or both are less than zero then behavior is undefined.
  108. @item if(x, y)
  109. Evaluate @var{x}, and if the result is non-zero return the result of
  110. the evaluation of @var{y}, return 0 otherwise.
  111. @item ifnot(x, y)
  112. Evaluate @var{x}, and if the result is zero return the result of the
  113. evaluation of @var{y}, return 0 otherwise.
  114. @item taylor(expr, x) taylor(expr, x, id)
  115. Evaluate a taylor series at x.
  116. expr represents the LD(id)-th derivates of f(x) at 0. If id is not specified
  117. then 0 is assumed.
  118. note, when you have the derivatives at y instead of 0
  119. taylor(expr, x-y) can be used
  120. When the series does not converge the results are undefined.
  121. @item root(expr, max)
  122. Finds x where f(x)=0 in the interval 0..max.
  123. f() must be continuous or the result is undefined.
  124. @end table
  125. The following constants are available:
  126. @table @option
  127. @item PI
  128. area of the unit disc, approximately 3.14
  129. @item E
  130. exp(1) (Euler's number), approximately 2.718
  131. @item PHI
  132. golden ratio (1+sqrt(5))/2, approximately 1.618
  133. @end table
  134. Assuming that an expression is considered "true" if it has a non-zero
  135. value, note that:
  136. @code{*} works like AND
  137. @code{+} works like OR
  138. and the construct:
  139. @example
  140. if A then B else C
  141. @end example
  142. is equivalent to
  143. @example
  144. if(A,B) + ifnot(A,C)
  145. @end example
  146. In your C code, you can extend the list of unary and binary functions,
  147. and define recognized constants, so that they are available for your
  148. expressions.
  149. The evaluator also recognizes the International System number
  150. postfixes. If 'i' is appended after the postfix, powers of 2 are used
  151. instead of powers of 10. The 'B' postfix multiplies the value for 8,
  152. and can be appended after another postfix or used alone. This allows
  153. using for example 'KB', 'MiB', 'G' and 'B' as postfix.
  154. Follows the list of available International System postfixes, with
  155. indication of the corresponding powers of 10 and of 2.
  156. @table @option
  157. @item y
  158. -24 / -80
  159. @item z
  160. -21 / -70
  161. @item a
  162. -18 / -60
  163. @item f
  164. -15 / -50
  165. @item p
  166. -12 / -40
  167. @item n
  168. -9 / -30
  169. @item u
  170. -6 / -20
  171. @item m
  172. -3 / -10
  173. @item c
  174. -2
  175. @item d
  176. -1
  177. @item h
  178. 2
  179. @item k
  180. 3 / 10
  181. @item K
  182. 3 / 10
  183. @item M
  184. 6 / 20
  185. @item G
  186. 9 / 30
  187. @item T
  188. 12 / 40
  189. @item P
  190. 15 / 40
  191. @item E
  192. 18 / 50
  193. @item Z
  194. 21 / 60
  195. @item Y
  196. 24 / 70
  197. @end table
  198. @c man end