eval.texi 5.1 KB

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